blob: 309150beb1d4392fb3d284d82de76f4cf19c7958 [file] [log] [blame]
Radek Krejcib1646a92018-11-02 16:08:26 +01001/**
2 * @file xpath.c
3 * @author Michal Vasko <mvasko@cesnet.cz>
4 * @brief YANG XPath evaluation functions
5 *
Michal Vasko49fec8e2022-05-24 10:28:33 +02006 * Copyright (c) 2015 - 2022 CESNET, z.s.p.o.
Radek Krejcib1646a92018-11-02 16:08:26 +01007 *
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 */
Christian Hopps32874e12021-05-01 09:43:54 -040014#define _GNU_SOURCE /* asprintf, strdup */
Radek Krejcib1646a92018-11-02 16:08:26 +010015
Radek Krejci535ea9f2020-05-29 16:01:05 +020016#include "xpath.h"
Radek Krejcib1646a92018-11-02 16:08:26 +010017
Radek Krejci535ea9f2020-05-29 16:01:05 +020018#include <assert.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010019#include <ctype.h>
Radek Krejci535ea9f2020-05-29 16:01:05 +020020#include <errno.h>
Radek Krejci535ea9f2020-05-29 16:01:05 +020021#include <math.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020022#include <stdint.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010023#include <stdio.h>
24#include <stdlib.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010025#include <string.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010026
Radek Krejci535ea9f2020-05-29 16:01:05 +020027#include "common.h"
Michal Vasko5aa44c02020-06-29 11:47:02 +020028#include "compat.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020029#include "context.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020030#include "dict.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020031#include "hash_table.h"
Radek Krejci47fab892020-11-05 17:02:41 +010032#include "out.h"
Radek Krejci7931b192020-06-25 17:05:03 +020033#include "parser_data.h"
Michal Vasko004d3152020-06-11 19:59:22 +020034#include "path.h"
Michal Vaskob4750962022-10-06 15:33:35 +020035#include "plugins_exts/metadata.h"
Michal Vasko03ff5a72019-09-11 13:49:33 +020036#include "plugins_types.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020037#include "printer_data.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020038#include "schema_compile_node.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020039#include "tree.h"
Radek Krejci77114102021-03-10 15:21:57 +010040#include "tree_data.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020041#include "tree_data_internal.h"
Radek Krejci859a15a2021-03-05 20:56:59 +010042#include "tree_edit.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020043#include "tree_schema_internal.h"
44#include "xml.h"
Michal Vasko03ff5a72019-09-11 13:49:33 +020045
Michal Vaskodd528af2022-08-08 14:35:07 +020046static LY_ERR reparse_or_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint32_t *tok_idx, uint32_t depth);
47static LY_ERR eval_expr_select(const struct lyxp_expr *exp, uint32_t *tok_idx, enum lyxp_expr_type etype,
Michal Vasko40308e72020-10-20 16:38:40 +020048 struct lyxp_set *set, uint32_t options);
Michal Vaskofe1af042022-07-29 14:58:59 +020049static LY_ERR moveto_resolve_model(const char **qname, uint32_t *qname_len, const struct lyxp_set *set,
Michal Vasko93923692021-05-07 15:28:02 +020050 const struct lysc_node *ctx_scnode, const struct lys_module **moveto_mod);
Michal Vasko47da6562022-07-14 15:43:15 +020051static LY_ERR moveto_axis_node_next(const struct lyd_node **iter, enum lyxp_node_type *iter_type,
52 const struct lyd_node *node, enum lyxp_node_type node_type, enum lyxp_axis axis, struct lyxp_set *set);
Michal Vasko49fec8e2022-05-24 10:28:33 +020053static LY_ERR moveto_node(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname,
54 enum lyxp_axis axis, uint32_t options);
55static LY_ERR moveto_scnode(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname,
56 enum lyxp_axis axis, uint32_t options);
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +020057static LY_ERR moveto_op_comp(struct lyxp_set *set1, struct lyxp_set *set2, const char *op, ly_bool *result);
Michal Vasko03ff5a72019-09-11 13:49:33 +020058
aPiecek96dc1e32021-10-08 15:45:59 +020059/* Functions are divided into the following basic classes:
60 *
61 * (re)parse functions:
62 * Parse functions parse the expression into
63 * tokens (syntactic analysis).
64 * Reparse functions perform semantic analysis
65 * (do not save the result, just a check) of
66 * the expression and fill repeat indices.
67 *
68 * warn functions:
69 * Warn functions check specific reasonable conditions for schema XPath
70 * and print a warning if they are not satisfied.
71 *
72 * moveto functions:
73 * They and only they actually change the context (set).
74 *
75 * eval functions:
76 * They execute a parsed XPath expression on some data subtree.
77 */
78
Michal Vasko03ff5a72019-09-11 13:49:33 +020079/**
80 * @brief Print the type of an XPath \p set.
81 *
82 * @param[in] set Set to use.
83 * @return Set type string.
84 */
85static const char *
86print_set_type(struct lyxp_set *set)
87{
88 switch (set->type) {
Michal Vasko03ff5a72019-09-11 13:49:33 +020089 case LYXP_SET_NODE_SET:
90 return "node set";
91 case LYXP_SET_SCNODE_SET:
92 return "schema node set";
93 case LYXP_SET_BOOLEAN:
94 return "boolean";
95 case LYXP_SET_NUMBER:
96 return "number";
97 case LYXP_SET_STRING:
98 return "string";
99 }
100
101 return NULL;
102}
103
Michal Vasko24cddf82020-06-01 08:17:01 +0200104const char *
Michal Vasko49fec8e2022-05-24 10:28:33 +0200105lyxp_token2str(enum lyxp_token tok)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200106{
107 switch (tok) {
108 case LYXP_TOKEN_PAR1:
109 return "(";
110 case LYXP_TOKEN_PAR2:
111 return ")";
112 case LYXP_TOKEN_BRACK1:
113 return "[";
114 case LYXP_TOKEN_BRACK2:
115 return "]";
116 case LYXP_TOKEN_DOT:
117 return ".";
118 case LYXP_TOKEN_DDOT:
119 return "..";
120 case LYXP_TOKEN_AT:
121 return "@";
122 case LYXP_TOKEN_COMMA:
123 return ",";
Michal Vaskoe0a8ce72023-04-04 08:30:44 +0200124 case LYXP_TOKEN_DCOLON:
125 return "::";
Michal Vasko03ff5a72019-09-11 13:49:33 +0200126 case LYXP_TOKEN_NAMETEST:
127 return "NameTest";
128 case LYXP_TOKEN_NODETYPE:
129 return "NodeType";
aPiecekfba75362021-10-07 12:39:48 +0200130 case LYXP_TOKEN_VARREF:
131 return "VariableReference";
Michal Vasko03ff5a72019-09-11 13:49:33 +0200132 case LYXP_TOKEN_FUNCNAME:
133 return "FunctionName";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200134 case LYXP_TOKEN_OPER_LOG:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200135 return "Operator(Logic)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200136 case LYXP_TOKEN_OPER_EQUAL:
137 return "Operator(Equal)";
138 case LYXP_TOKEN_OPER_NEQUAL:
139 return "Operator(Non-equal)";
140 case LYXP_TOKEN_OPER_COMP:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200141 return "Operator(Comparison)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200142 case LYXP_TOKEN_OPER_MATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200143 return "Operator(Math)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200144 case LYXP_TOKEN_OPER_UNI:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200145 return "Operator(Union)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200146 case LYXP_TOKEN_OPER_PATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200147 return "Operator(Path)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200148 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko14676352020-05-29 11:35:55 +0200149 return "Operator(Recursive Path)";
Michal Vaskoe0a8ce72023-04-04 08:30:44 +0200150 case LYXP_TOKEN_AXISNAME:
151 return "AxisName";
Michal Vasko03ff5a72019-09-11 13:49:33 +0200152 case LYXP_TOKEN_LITERAL:
153 return "Literal";
154 case LYXP_TOKEN_NUMBER:
155 return "Number";
156 default:
157 LOGINT(NULL);
158 return "";
159 }
160}
161
162/**
Michal Vasko49fec8e2022-05-24 10:28:33 +0200163 * @brief Transform string into an axis.
164 *
165 * @param[in] str String to transform.
166 * @param[in] str_len Length of @p str.
167 * @return Transformed axis.
168 */
169static enum lyxp_axis
Michal Vaskodd528af2022-08-08 14:35:07 +0200170str2axis(const char *str, uint32_t str_len)
Michal Vasko49fec8e2022-05-24 10:28:33 +0200171{
172 switch (str_len) {
173 case 4:
174 assert(!strncmp("self", str, str_len));
175 return LYXP_AXIS_SELF;
176 case 5:
177 assert(!strncmp("child", str, str_len));
178 return LYXP_AXIS_CHILD;
179 case 6:
180 assert(!strncmp("parent", str, str_len));
181 return LYXP_AXIS_PARENT;
182 case 8:
183 assert(!strncmp("ancestor", str, str_len));
184 return LYXP_AXIS_ANCESTOR;
185 case 9:
186 if (str[0] == 'a') {
187 assert(!strncmp("attribute", str, str_len));
188 return LYXP_AXIS_ATTRIBUTE;
189 } else if (str[0] == 'f') {
190 assert(!strncmp("following", str, str_len));
191 return LYXP_AXIS_FOLLOWING;
192 } else {
193 assert(!strncmp("preceding", str, str_len));
194 return LYXP_AXIS_PRECEDING;
195 }
196 break;
197 case 10:
198 assert(!strncmp("descendant", str, str_len));
199 return LYXP_AXIS_DESCENDANT;
200 case 16:
201 assert(!strncmp("ancestor-or-self", str, str_len));
202 return LYXP_AXIS_ANCESTOR_OR_SELF;
203 case 17:
204 if (str[0] == 'f') {
205 assert(!strncmp("following-sibling", str, str_len));
206 return LYXP_AXIS_FOLLOWING_SIBLING;
207 } else {
208 assert(!strncmp("preceding-sibling", str, str_len));
209 return LYXP_AXIS_PRECEDING_SIBLING;
210 }
211 break;
212 case 18:
213 assert(!strncmp("descendant-or-self", str, str_len));
214 return LYXP_AXIS_DESCENDANT_OR_SELF;
215 }
216
217 LOGINT(NULL);
218 return 0;
219}
220
221/**
Michal Vasko16961782023-11-01 10:55:36 +0100222 * @brief Append a string to a dynamic string variable.
223 *
224 * @param[in,out] str String to use.
225 * @param[in,out] size String size.
226 * @param[in,out] used String used size excluding terminating zero.
227 * @param[in] format Message format.
228 * @param[in] ... Message format arguments.
229 */
230static void
231print_expr_str(char **str, size_t *size, size_t *used, const char *format, ...)
232{
233 int p;
234 va_list ap;
235
236 va_start(ap, format);
237
238 /* try to append the string */
239 p = vsnprintf(*str + *used, *size - *used, format, ap);
240
241 if ((unsigned)p >= *size - *used) {
242 /* realloc */
243 *str = ly_realloc(*str, *size + p + 1);
244 *size += p + 1;
245
246 /* restart ap */
247 va_end(ap);
248 va_start(ap, format);
249
250 /* print */
251 p = vsnprintf(*str + *used, *size - *used, format, ap);
252 }
253
254 *used += p;
255 va_end(ap);
256}
257
258/**
259 * @brief Print the whole expression @p exp to debug output.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200260 *
261 * @param[in] exp Expression to use.
262 */
263static void
Michal Vasko40308e72020-10-20 16:38:40 +0200264print_expr_struct_debug(const struct lyxp_expr *exp)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200265{
Michal Vasko16961782023-11-01 10:55:36 +0100266 char *buf = NULL;
Michal Vasko1fdd8fa2021-01-08 09:21:45 +0100267 uint32_t i, j;
Michal Vasko16961782023-11-01 10:55:36 +0100268 size_t size = 0, used = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200269
Radek Krejci52b6d512020-10-12 12:33:17 +0200270 if (!exp || (ly_ll < LY_LLDBG)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200271 return;
272 }
273
274 LOGDBG(LY_LDGXPATH, "expression \"%s\":", exp->expr);
275 for (i = 0; i < exp->used; ++i) {
Michal Vasko16961782023-11-01 10:55:36 +0100276 print_expr_str(&buf, &size, &used, "\ttoken %s, in expression \"%.*s\"",
277 lyxp_token2str(exp->tokens[i]), exp->tok_len[i], &exp->expr[exp->tok_pos[i]]);
278
Michal Vasko23049552021-03-04 15:57:44 +0100279 if (exp->repeat && exp->repeat[i]) {
Michal Vasko16961782023-11-01 10:55:36 +0100280 print_expr_str(&buf, &size, &used, " (repeat %d", exp->repeat[i][0]);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200281 for (j = 1; exp->repeat[i][j]; ++j) {
Michal Vasko16961782023-11-01 10:55:36 +0100282 print_expr_str(&buf, &size, &used, ", %d", exp->repeat[i][j]);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200283 }
Michal Vasko16961782023-11-01 10:55:36 +0100284 print_expr_str(&buf, &size, &used, ")");
Michal Vasko03ff5a72019-09-11 13:49:33 +0200285 }
Michal Vasko16961782023-11-01 10:55:36 +0100286 LOGDBG(LY_LDGXPATH, buf);
287 used = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200288 }
Michal Vasko16961782023-11-01 10:55:36 +0100289
290 free(buf);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200291}
292
293#ifndef NDEBUG
294
295/**
296 * @brief Print XPath set content to debug output.
297 *
298 * @param[in] set Set to print.
299 */
300static void
301print_set_debug(struct lyxp_set *set)
302{
303 uint32_t i;
304 char *str;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200305 struct lyxp_set_node *item;
306 struct lyxp_set_scnode *sitem;
307
Radek Krejci52b6d512020-10-12 12:33:17 +0200308 if (ly_ll < LY_LLDBG) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200309 return;
310 }
311
312 switch (set->type) {
313 case LYXP_SET_NODE_SET:
314 LOGDBG(LY_LDGXPATH, "set NODE SET:");
315 for (i = 0; i < set->used; ++i) {
316 item = &set->val.nodes[i];
317
318 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +0100319 case LYXP_NODE_NONE:
320 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): NONE", i + 1, item->pos);
321 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200322 case LYXP_NODE_ROOT:
323 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT", i + 1, item->pos);
324 break;
325 case LYXP_NODE_ROOT_CONFIG:
326 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT CONFIG", i + 1, item->pos);
327 break;
328 case LYXP_NODE_ELEM:
Michal Vasko222be682023-08-24 08:17:12 +0200329 if (item->node->schema && (item->node->schema->nodetype == LYS_LIST) &&
330 (lyd_child(item->node)->schema->nodetype == LYS_LEAF)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200331 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (1st child val: %s)", i + 1, item->pos,
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200332 item->node->schema->name, lyd_get_value(lyd_child(item->node)));
Michal Vasko222be682023-08-24 08:17:12 +0200333 } else if ((!item->node->schema && !lyd_child(item->node)) || (item->node->schema->nodetype == LYS_LEAFLIST)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200334 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (val: %s)", i + 1, item->pos,
Michal Vasko222be682023-08-24 08:17:12 +0200335 LYD_NAME(item->node), lyd_get_value(item->node));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200336 } else {
Michal Vasko222be682023-08-24 08:17:12 +0200337 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s", i + 1, item->pos, LYD_NAME(item->node));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200338 }
339 break;
340 case LYXP_NODE_TEXT:
Michal Vasko222be682023-08-24 08:17:12 +0200341 if (item->node->schema && (item->node->schema->nodetype & LYS_ANYDATA)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200342 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): TEXT <%s>", i + 1, item->pos,
Michal Vasko69730152020-10-09 16:30:07 +0200343 item->node->schema->nodetype == LYS_ANYXML ? "anyxml" : "anydata");
Michal Vasko03ff5a72019-09-11 13:49:33 +0200344 } else {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200345 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): TEXT %s", i + 1, item->pos, lyd_get_value(item->node));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200346 }
347 break;
Michal Vasko9f96a052020-03-10 09:41:45 +0100348 case LYXP_NODE_META:
349 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): META %s = %s", i + 1, item->pos, set->val.meta[i].meta->name,
Michal Vasko69730152020-10-09 16:30:07 +0200350 set->val.meta[i].meta->value);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200351 break;
352 }
353 }
354 break;
355
356 case LYXP_SET_SCNODE_SET:
357 LOGDBG(LY_LDGXPATH, "set SCNODE SET:");
358 for (i = 0; i < set->used; ++i) {
359 sitem = &set->val.scnodes[i];
360
361 switch (sitem->type) {
362 case LYXP_NODE_ROOT:
363 LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT", i + 1, sitem->in_ctx);
364 break;
365 case LYXP_NODE_ROOT_CONFIG:
366 LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT CONFIG", i + 1, sitem->in_ctx);
367 break;
368 case LYXP_NODE_ELEM:
369 LOGDBG(LY_LDGXPATH, "\t%d (%u): ELEM %s", i + 1, sitem->in_ctx, sitem->scnode->name);
370 break;
371 default:
372 LOGINT(NULL);
373 break;
374 }
375 }
376 break;
377
Michal Vasko03ff5a72019-09-11 13:49:33 +0200378 case LYXP_SET_BOOLEAN:
379 LOGDBG(LY_LDGXPATH, "set BOOLEAN");
Michal Vasko004d3152020-06-11 19:59:22 +0200380 LOGDBG(LY_LDGXPATH, "\t%s", (set->val.bln ? "true" : "false"));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200381 break;
382
383 case LYXP_SET_STRING:
384 LOGDBG(LY_LDGXPATH, "set STRING");
385 LOGDBG(LY_LDGXPATH, "\t%s", set->val.str);
386 break;
387
388 case LYXP_SET_NUMBER:
389 LOGDBG(LY_LDGXPATH, "set NUMBER");
390
391 if (isnan(set->val.num)) {
392 str = strdup("NaN");
393 } else if ((set->val.num == 0) || (set->val.num == -0.0f)) {
394 str = strdup("0");
395 } else if (isinf(set->val.num) && !signbit(set->val.num)) {
396 str = strdup("Infinity");
397 } else if (isinf(set->val.num) && signbit(set->val.num)) {
398 str = strdup("-Infinity");
399 } else if ((long long)set->val.num == set->val.num) {
400 if (asprintf(&str, "%lld", (long long)set->val.num) == -1) {
401 str = NULL;
402 }
403 } else {
404 if (asprintf(&str, "%03.1Lf", set->val.num) == -1) {
405 str = NULL;
406 }
407 }
408 LY_CHECK_ERR_RET(!str, LOGMEM(NULL), );
409
410 LOGDBG(LY_LDGXPATH, "\t%s", str);
411 free(str);
412 }
413}
414
415#endif
416
417/**
418 * @brief Realloc the string \p str.
419 *
420 * @param[in] ctx libyang context for logging.
421 * @param[in] needed How much free space is required.
422 * @param[in,out] str Pointer to the string to use.
423 * @param[in,out] used Used bytes in \p str.
424 * @param[in,out] size Allocated bytes in \p str.
425 * @return LY_ERR
426 */
427static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +0200428cast_string_realloc(const struct ly_ctx *ctx, uint64_t needed, char **str, uint32_t *used, uint32_t *size)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200429{
Michal Vasko2291ab92022-08-08 08:53:12 +0200430 if (*size - (unsigned)*used < needed) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200431 do {
Michal Vaskodd528af2022-08-08 14:35:07 +0200432 if ((UINT32_MAX - *size) < LYXP_STRING_CAST_SIZE_STEP) {
433 LOGERR(ctx, LY_EINVAL, "XPath string length limit (%" PRIu32 ") reached.", UINT32_MAX);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200434 return LY_EINVAL;
435 }
436 *size += LYXP_STRING_CAST_SIZE_STEP;
Michal Vasko2291ab92022-08-08 08:53:12 +0200437 } while (*size - (unsigned)*used < needed);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200438 *str = ly_realloc(*str, *size * sizeof(char));
439 LY_CHECK_ERR_RET(!(*str), LOGMEM(ctx), LY_EMEM);
440 }
441
442 return LY_SUCCESS;
443}
444
445/**
446 * @brief Cast nodes recursively to one string @p str.
447 *
Michal Vasko47da6562022-07-14 15:43:15 +0200448 * @param[in] node Node to cast, NULL if root.
449 * @param[in] set XPath set.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200450 * @param[in] indent Current indent.
451 * @param[in,out] str Resulting string.
452 * @param[in,out] used Used bytes in @p str.
453 * @param[in,out] size Allocated bytes in @p str.
Michal Vasko47da6562022-07-14 15:43:15 +0200454 * @return LY_ERR value.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200455 */
456static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +0200457cast_string_recursive(const struct lyd_node *node, struct lyxp_set *set, uint32_t indent, char **str, uint32_t *used,
458 uint32_t *size)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200459{
Radek Krejci7f769d72020-07-11 23:13:56 +0200460 char *buf, *line, *ptr = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200461 const char *value_str;
Michal Vasko222be682023-08-24 08:17:12 +0200462 uint16_t nodetype;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200463 const struct lyd_node *child;
Michal Vasko47da6562022-07-14 15:43:15 +0200464 enum lyxp_node_type child_type;
Michal Vasko60ea6352020-06-29 13:39:39 +0200465 struct lyd_node *tree;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200466 struct lyd_node_any *any;
467 LY_ERR rc;
468
Michal Vasko222be682023-08-24 08:17:12 +0200469 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && node && node->schema && (node->schema->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200470 return LY_SUCCESS;
471 }
472
Michal Vasko47da6562022-07-14 15:43:15 +0200473 if (!node) {
474 /* fake container */
475 LY_CHECK_RET(cast_string_realloc(set->ctx, 1, str, used, size));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200476 strcpy(*str + (*used - 1), "\n");
477 ++(*used);
478
479 ++indent;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200480
Michal Vasko47da6562022-07-14 15:43:15 +0200481 /* print all the top-level nodes */
482 child = NULL;
483 child_type = 0;
484 while (!moveto_axis_node_next(&child, &child_type, NULL, set->root_type, LYXP_AXIS_CHILD, set)) {
485 LY_CHECK_RET(cast_string_recursive(child, set, indent, str, used, size));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200486 }
487
Michal Vasko47da6562022-07-14 15:43:15 +0200488 /* end fake container */
489 LY_CHECK_RET(cast_string_realloc(set->ctx, 1, str, used, size));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200490 strcpy(*str + (*used - 1), "\n");
491 ++(*used);
492
493 --indent;
Michal Vasko47da6562022-07-14 15:43:15 +0200494 } else {
Michal Vasko222be682023-08-24 08:17:12 +0200495 if (node->schema) {
496 nodetype = node->schema->nodetype;
497 } else if (lyd_child(node)) {
498 nodetype = LYS_CONTAINER;
499 } else {
500 nodetype = LYS_LEAF;
501 }
502
503 switch (nodetype) {
Michal Vasko47da6562022-07-14 15:43:15 +0200504 case LYS_CONTAINER:
505 case LYS_LIST:
506 case LYS_RPC:
507 case LYS_NOTIF:
508 LY_CHECK_RET(cast_string_realloc(set->ctx, 1, str, used, size));
509 strcpy(*str + (*used - 1), "\n");
510 ++(*used);
511
512 for (child = lyd_child(node); child; child = child->next) {
513 LY_CHECK_RET(cast_string_recursive(child, set, indent + 1, str, used, size));
514 }
515
516 break;
517
518 case LYS_LEAF:
519 case LYS_LEAFLIST:
520 value_str = lyd_get_value(node);
521
522 /* print indent */
523 LY_CHECK_RET(cast_string_realloc(set->ctx, indent * 2 + strlen(value_str) + 1, str, used, size));
524 memset(*str + (*used - 1), ' ', indent * 2);
525 *used += indent * 2;
526
527 /* print value */
528 if (*used == 1) {
529 sprintf(*str + (*used - 1), "%s", value_str);
530 *used += strlen(value_str);
531 } else {
532 sprintf(*str + (*used - 1), "%s\n", value_str);
533 *used += strlen(value_str) + 1;
534 }
535
536 break;
537
538 case LYS_ANYXML:
539 case LYS_ANYDATA:
540 any = (struct lyd_node_any *)node;
541 if (!(void *)any->value.tree) {
542 /* no content */
543 buf = strdup("");
544 LY_CHECK_ERR_RET(!buf, LOGMEM(set->ctx), LY_EMEM);
545 } else {
546 struct ly_out *out;
547
548 if (any->value_type == LYD_ANYDATA_LYB) {
549 /* try to parse it into a data tree */
550 if (lyd_parse_data_mem((struct ly_ctx *)set->ctx, any->value.mem, LYD_LYB,
551 LYD_PARSE_ONLY | LYD_PARSE_STRICT, 0, &tree) == LY_SUCCESS) {
552 /* successfully parsed */
553 free(any->value.mem);
554 any->value.tree = tree;
555 any->value_type = LYD_ANYDATA_DATATREE;
556 }
557 /* error is covered by the following switch where LYD_ANYDATA_LYB causes failure */
558 }
559
560 switch (any->value_type) {
561 case LYD_ANYDATA_STRING:
562 case LYD_ANYDATA_XML:
563 case LYD_ANYDATA_JSON:
564 buf = strdup(any->value.json);
565 LY_CHECK_ERR_RET(!buf, LOGMEM(set->ctx), LY_EMEM);
566 break;
567 case LYD_ANYDATA_DATATREE:
568 LY_CHECK_RET(ly_out_new_memory(&buf, 0, &out));
569 rc = lyd_print_all(out, any->value.tree, LYD_XML, 0);
570 ly_out_free(out, NULL, 0);
571 LY_CHECK_RET(rc < 0, -rc);
572 break;
573 case LYD_ANYDATA_LYB:
574 LOGERR(set->ctx, LY_EINVAL, "Cannot convert LYB anydata into string.");
575 return LY_EINVAL;
576 }
577 }
578
579 line = strtok_r(buf, "\n", &ptr);
580 do {
581 rc = cast_string_realloc(set->ctx, indent * 2 + strlen(line) + 1, str, used, size);
582 if (rc != LY_SUCCESS) {
583 free(buf);
584 return rc;
585 }
586 memset(*str + (*used - 1), ' ', indent * 2);
587 *used += indent * 2;
588
589 strcpy(*str + (*used - 1), line);
590 *used += strlen(line);
591
592 strcpy(*str + (*used - 1), "\n");
593 *used += 1;
594 } while ((line = strtok_r(NULL, "\n", &ptr)));
595
596 free(buf);
597 break;
598
599 default:
600 LOGINT_RET(set->ctx);
601 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200602 }
603
604 return LY_SUCCESS;
605}
606
607/**
608 * @brief Cast an element into a string.
609 *
Michal Vasko47da6562022-07-14 15:43:15 +0200610 * @param[in] node Node to cast, NULL if root.
611 * @param[in] set XPath set.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200612 * @param[out] str Element cast to dynamically-allocated string.
613 * @return LY_ERR
614 */
615static LY_ERR
Michal Vasko47da6562022-07-14 15:43:15 +0200616cast_string_elem(const struct lyd_node *node, struct lyxp_set *set, char **str)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200617{
Michal Vaskodd528af2022-08-08 14:35:07 +0200618 uint32_t used, size;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200619 LY_ERR rc;
620
621 *str = malloc(LYXP_STRING_CAST_SIZE_START * sizeof(char));
Michal Vasko47da6562022-07-14 15:43:15 +0200622 LY_CHECK_ERR_RET(!*str, LOGMEM(set->ctx), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200623 (*str)[0] = '\0';
624 used = 1;
625 size = LYXP_STRING_CAST_SIZE_START;
626
Michal Vasko47da6562022-07-14 15:43:15 +0200627 rc = cast_string_recursive(node, set, 0, str, &used, &size);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200628 if (rc != LY_SUCCESS) {
629 free(*str);
630 return rc;
631 }
632
633 if (size > used) {
634 *str = ly_realloc(*str, used * sizeof(char));
Michal Vasko47da6562022-07-14 15:43:15 +0200635 LY_CHECK_ERR_RET(!*str, LOGMEM(set->ctx), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200636 }
637 return LY_SUCCESS;
638}
639
640/**
641 * @brief Cast a LYXP_SET_NODE_SET set into a string.
642 * Context position aware.
643 *
644 * @param[in] set Set to cast.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200645 * @param[out] str Cast dynamically-allocated string.
646 * @return LY_ERR
647 */
648static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100649cast_node_set_to_string(struct lyxp_set *set, char **str)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200650{
Michal Vaskoaa677062021-03-09 13:52:53 +0100651 if (!set->used) {
652 *str = strdup("");
653 if (!*str) {
654 LOGMEM_RET(set->ctx);
655 }
656 return LY_SUCCESS;
657 }
658
Michal Vasko03ff5a72019-09-11 13:49:33 +0200659 switch (set->val.nodes[0].type) {
Michal Vasko2caefc12019-11-14 16:07:56 +0100660 case LYXP_NODE_NONE:
661 /* invalid */
662 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200663 case LYXP_NODE_ROOT:
664 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200665 case LYXP_NODE_ELEM:
666 case LYXP_NODE_TEXT:
Michal Vasko47da6562022-07-14 15:43:15 +0200667 return cast_string_elem(set->val.nodes[0].node, set, str);
Michal Vasko9f96a052020-03-10 09:41:45 +0100668 case LYXP_NODE_META:
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200669 *str = strdup(lyd_get_meta_value(set->val.meta[0].meta));
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200670 if (!*str) {
671 LOGMEM_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200672 }
673 return LY_SUCCESS;
674 }
675
676 LOGINT_RET(set->ctx);
677}
678
679/**
680 * @brief Cast a string into an XPath number.
681 *
682 * @param[in] str String to use.
683 * @return Cast number.
684 */
685static long double
686cast_string_to_number(const char *str)
687{
688 long double num;
689 char *ptr;
690
691 errno = 0;
692 num = strtold(str, &ptr);
693 if (errno || *ptr) {
694 num = NAN;
695 }
696 return num;
697}
698
699/**
700 * @brief Callback for checking value equality.
701 *
Michal Vasko62524a92021-02-26 10:08:50 +0100702 * Implementation of ::lyht_value_equal_cb.
Radek Krejci857189e2020-09-01 13:26:36 +0200703 *
Michal Vasko03ff5a72019-09-11 13:49:33 +0200704 * @param[in] val1_p First value.
705 * @param[in] val2_p Second value.
706 * @param[in] mod Whether hash table is being modified.
707 * @param[in] cb_data Callback data.
Radek Krejci857189e2020-09-01 13:26:36 +0200708 * @return Boolean value whether values are equal or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200709 */
Radek Krejci857189e2020-09-01 13:26:36 +0200710static ly_bool
711set_values_equal_cb(void *val1_p, void *val2_p, ly_bool UNUSED(mod), void *UNUSED(cb_data))
Michal Vasko03ff5a72019-09-11 13:49:33 +0200712{
713 struct lyxp_set_hash_node *val1, *val2;
714
715 val1 = (struct lyxp_set_hash_node *)val1_p;
716 val2 = (struct lyxp_set_hash_node *)val2_p;
717
718 if ((val1->node == val2->node) && (val1->type == val2->type)) {
719 return 1;
720 }
721
722 return 0;
723}
724
725/**
726 * @brief Insert node and its hash into set.
727 *
728 * @param[in] set et to insert to.
729 * @param[in] node Node with hash.
730 * @param[in] type Node type.
731 */
732static void
733set_insert_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type)
734{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200735 LY_ERR r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200736 uint32_t i, hash;
737 struct lyxp_set_hash_node hnode;
738
739 if (!set->ht && (set->used >= LYD_HT_MIN_ITEMS)) {
740 /* create hash table and add all the nodes */
741 set->ht = lyht_new(1, sizeof(struct lyxp_set_hash_node), set_values_equal_cb, NULL, 1);
742 for (i = 0; i < set->used; ++i) {
743 hnode.node = set->val.nodes[i].node;
744 hnode.type = set->val.nodes[i].type;
745
Michal Vaskoae130f52023-04-20 14:25:16 +0200746 hash = lyht_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
747 hash = lyht_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
748 hash = lyht_hash_multi(hash, NULL, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200749
750 r = lyht_insert(set->ht, &hnode, hash, NULL);
751 assert(!r);
752 (void)r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200753
Michal Vasko513b87d2023-08-04 14:03:40 +0200754 if ((hnode.node == node) && (hnode.type == type)) {
Michal Vasko9d6befd2019-12-11 14:56:56 +0100755 /* it was just added, do not add it twice */
Michal Vasko513b87d2023-08-04 14:03:40 +0200756 return;
Michal Vasko9d6befd2019-12-11 14:56:56 +0100757 }
758 }
759 }
760
Michal Vasko513b87d2023-08-04 14:03:40 +0200761 if (set->ht) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200762 /* add the new node into hash table */
763 hnode.node = node;
764 hnode.type = type;
765
Michal Vaskoae130f52023-04-20 14:25:16 +0200766 hash = lyht_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
767 hash = lyht_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
768 hash = lyht_hash_multi(hash, NULL, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200769
770 r = lyht_insert(set->ht, &hnode, hash, NULL);
771 assert(!r);
772 (void)r;
773 }
774}
775
776/**
777 * @brief Remove node and its hash from set.
778 *
779 * @param[in] set Set to remove from.
780 * @param[in] node Node to remove.
781 * @param[in] type Node type.
782 */
783static void
784set_remove_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type)
785{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200786 LY_ERR r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200787 struct lyxp_set_hash_node hnode;
788 uint32_t hash;
789
790 if (set->ht) {
791 hnode.node = node;
792 hnode.type = type;
793
Michal Vaskoae130f52023-04-20 14:25:16 +0200794 hash = lyht_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
795 hash = lyht_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
796 hash = lyht_hash_multi(hash, NULL, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200797
798 r = lyht_remove(set->ht, &hnode, hash);
799 assert(!r);
800 (void)r;
801
802 if (!set->ht->used) {
Michal Vasko77b7f90a2023-01-31 15:42:41 +0100803 lyht_free(set->ht, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200804 set->ht = NULL;
805 }
806 }
807}
808
809/**
810 * @brief Check whether node is in set based on its hash.
811 *
812 * @param[in] set Set to search in.
813 * @param[in] node Node to search for.
814 * @param[in] type Node type.
815 * @param[in] skip_idx Index in @p set to skip.
816 * @return LY_ERR
817 */
818static LY_ERR
819set_dup_node_hash_check(const struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type, int skip_idx)
820{
821 struct lyxp_set_hash_node hnode, *match_p;
822 uint32_t hash;
823
824 hnode.node = node;
825 hnode.type = type;
826
Michal Vaskoae130f52023-04-20 14:25:16 +0200827 hash = lyht_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
828 hash = lyht_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
829 hash = lyht_hash_multi(hash, NULL, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200830
831 if (!lyht_find(set->ht, &hnode, hash, (void **)&match_p)) {
832 if ((skip_idx > -1) && (set->val.nodes[skip_idx].node == match_p->node) && (set->val.nodes[skip_idx].type == match_p->type)) {
833 /* we found it on the index that should be skipped, find another */
834 hnode = *match_p;
835 if (lyht_find_next(set->ht, &hnode, hash, (void **)&match_p)) {
836 /* none other found */
837 return LY_SUCCESS;
838 }
839 }
840
841 return LY_EEXIST;
842 }
843
844 /* not found */
845 return LY_SUCCESS;
846}
847
Michal Vaskod3678892020-05-21 10:06:58 +0200848void
849lyxp_set_free_content(struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200850{
851 if (!set) {
852 return;
853 }
854
855 if (set->type == LYXP_SET_NODE_SET) {
856 free(set->val.nodes);
Michal Vasko77b7f90a2023-01-31 15:42:41 +0100857 lyht_free(set->ht, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200858 } else if (set->type == LYXP_SET_SCNODE_SET) {
859 free(set->val.scnodes);
Michal Vasko77b7f90a2023-01-31 15:42:41 +0100860 lyht_free(set->ht, NULL);
Michal Vaskod3678892020-05-21 10:06:58 +0200861 } else {
862 if (set->type == LYXP_SET_STRING) {
863 free(set->val.str);
864 }
865 set->type = LYXP_SET_NODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200866 }
Michal Vaskod3678892020-05-21 10:06:58 +0200867
868 set->val.nodes = NULL;
869 set->used = 0;
870 set->size = 0;
871 set->ht = NULL;
872 set->ctx_pos = 0;
aPiecek748da732021-06-01 09:56:43 +0200873 set->ctx_size = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200874}
875
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100876/**
877 * @brief Free dynamically-allocated set.
878 *
879 * @param[in] set Set to free.
880 */
881static void
Michal Vasko03ff5a72019-09-11 13:49:33 +0200882lyxp_set_free(struct lyxp_set *set)
883{
884 if (!set) {
885 return;
886 }
887
Michal Vaskod3678892020-05-21 10:06:58 +0200888 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200889 free(set);
890}
891
892/**
893 * @brief Initialize set context.
894 *
895 * @param[in] new Set to initialize.
896 * @param[in] set Arbitrary initialized set.
897 */
898static void
Michal Vasko4c7763f2020-07-27 17:40:37 +0200899set_init(struct lyxp_set *new, const struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200900{
901 memset(new, 0, sizeof *new);
Michal Vasko02a77382019-09-12 11:47:35 +0200902 if (set) {
Michal Vasko306e2832022-07-25 09:15:17 +0200903 new->non_child_axis = set->non_child_axis;
Michal Vasko02a77382019-09-12 11:47:35 +0200904 new->ctx = set->ctx;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200905 new->cur_node = set->cur_node;
Michal Vasko588112f2019-12-10 14:51:53 +0100906 new->root_type = set->root_type;
Michal Vasko6b26e742020-07-17 15:02:10 +0200907 new->context_op = set->context_op;
Michal Vaskof03ed032020-03-04 13:31:44 +0100908 new->tree = set->tree;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200909 new->cur_mod = set->cur_mod;
Michal Vasko02a77382019-09-12 11:47:35 +0200910 new->format = set->format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200911 new->prefix_data = set->prefix_data;
aPiecekfba75362021-10-07 12:39:48 +0200912 new->vars = set->vars;
Michal Vasko02a77382019-09-12 11:47:35 +0200913 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200914}
915
916/**
917 * @brief Create a deep copy of a set.
918 *
919 * @param[in] set Set to copy.
920 * @return Copy of @p set.
921 */
922static struct lyxp_set *
923set_copy(struct lyxp_set *set)
924{
925 struct lyxp_set *ret;
Michal Vasko1fdd8fa2021-01-08 09:21:45 +0100926 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200927
928 if (!set) {
929 return NULL;
930 }
931
932 ret = malloc(sizeof *ret);
933 LY_CHECK_ERR_RET(!ret, LOGMEM(set->ctx), NULL);
934 set_init(ret, set);
935
936 if (set->type == LYXP_SET_SCNODE_SET) {
937 ret->type = set->type;
938
939 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100940 if ((set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) ||
941 (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +0200942 uint32_t idx;
Michal Vasko26bbb272022-08-02 14:54:33 +0200943
Michal Vaskoe4a6d012023-05-22 14:34:52 +0200944 LY_CHECK_ERR_RET(lyxp_set_scnode_insert_node(ret, set->val.scnodes[i].scnode, set->val.scnodes[i].type,
Michal Vasko7333cb32022-07-29 16:30:29 +0200945 set->val.scnodes[i].axis, &idx), lyxp_set_free(ret), NULL);
Michal Vasko3f27c522020-01-06 08:37:49 +0100946 /* coverity seems to think scnodes can be NULL */
Radek Krejciaa6b53f2020-08-27 15:19:03 +0200947 if (!ret->val.scnodes) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200948 lyxp_set_free(ret);
949 return NULL;
950 }
Michal Vaskoba716542019-12-16 10:01:58 +0100951 ret->val.scnodes[idx].in_ctx = set->val.scnodes[i].in_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200952 }
953 }
954 } else if (set->type == LYXP_SET_NODE_SET) {
955 ret->type = set->type;
Michal Vasko08e9b112021-06-11 15:41:17 +0200956 if (set->used) {
957 ret->val.nodes = malloc(set->used * sizeof *ret->val.nodes);
958 LY_CHECK_ERR_RET(!ret->val.nodes, LOGMEM(set->ctx); free(ret), NULL);
959 memcpy(ret->val.nodes, set->val.nodes, set->used * sizeof *ret->val.nodes);
960 } else {
961 ret->val.nodes = NULL;
962 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200963
964 ret->used = ret->size = set->used;
965 ret->ctx_pos = set->ctx_pos;
966 ret->ctx_size = set->ctx_size;
Michal Vasko4a04e542021-02-01 08:58:30 +0100967 if (set->ht) {
968 ret->ht = lyht_dup(set->ht);
969 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200970 } else {
Radek Krejci0f969882020-08-21 16:56:47 +0200971 memcpy(ret, set, sizeof *ret);
972 if (set->type == LYXP_SET_STRING) {
973 ret->val.str = strdup(set->val.str);
974 LY_CHECK_ERR_RET(!ret->val.str, LOGMEM(set->ctx); free(ret), NULL);
975 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200976 }
977
978 return ret;
979}
980
981/**
982 * @brief Fill XPath set with a string. Any current data are disposed of.
983 *
984 * @param[in] set Set to fill.
985 * @param[in] string String to fill into \p set.
986 * @param[in] str_len Length of \p string. 0 is a valid value!
987 */
988static void
Michal Vaskodd528af2022-08-08 14:35:07 +0200989set_fill_string(struct lyxp_set *set, const char *string, uint32_t str_len)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200990{
Michal Vaskod3678892020-05-21 10:06:58 +0200991 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200992
993 set->type = LYXP_SET_STRING;
994 if ((str_len == 0) && (string[0] != '\0')) {
995 string = "";
996 }
997 set->val.str = strndup(string, str_len);
998}
999
1000/**
1001 * @brief Fill XPath set with a number. Any current data are disposed of.
1002 *
1003 * @param[in] set Set to fill.
1004 * @param[in] number Number to fill into \p set.
1005 */
1006static void
1007set_fill_number(struct lyxp_set *set, long double number)
1008{
Michal Vaskod3678892020-05-21 10:06:58 +02001009 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001010
1011 set->type = LYXP_SET_NUMBER;
1012 set->val.num = number;
1013}
1014
1015/**
1016 * @brief Fill XPath set with a boolean. Any current data are disposed of.
1017 *
1018 * @param[in] set Set to fill.
1019 * @param[in] boolean Boolean to fill into \p set.
1020 */
1021static void
Radek Krejci857189e2020-09-01 13:26:36 +02001022set_fill_boolean(struct lyxp_set *set, ly_bool boolean)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001023{
Michal Vaskod3678892020-05-21 10:06:58 +02001024 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001025
1026 set->type = LYXP_SET_BOOLEAN;
Michal Vasko004d3152020-06-11 19:59:22 +02001027 set->val.bln = boolean;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001028}
1029
1030/**
1031 * @brief Fill XPath set with the value from another set (deep assign).
1032 * Any current data are disposed of.
1033 *
1034 * @param[in] trg Set to fill.
1035 * @param[in] src Source set to copy into \p trg.
1036 */
1037static void
Michal Vasko4c7763f2020-07-27 17:40:37 +02001038set_fill_set(struct lyxp_set *trg, const struct lyxp_set *src)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001039{
1040 if (!trg || !src) {
1041 return;
1042 }
1043
Michal Vaskof06d19d2023-08-04 14:04:08 +02001044 lyxp_set_free_content(trg);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001045 set_init(trg, src);
1046
1047 if (src->type == LYXP_SET_SCNODE_SET) {
1048 trg->type = LYXP_SET_SCNODE_SET;
1049 trg->used = src->used;
1050 trg->size = src->used;
1051
Michal Vasko08e9b112021-06-11 15:41:17 +02001052 if (trg->size) {
1053 trg->val.scnodes = ly_realloc(trg->val.scnodes, trg->size * sizeof *trg->val.scnodes);
1054 LY_CHECK_ERR_RET(!trg->val.scnodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), );
1055 memcpy(trg->val.scnodes, src->val.scnodes, src->used * sizeof *src->val.scnodes);
1056 } else {
1057 trg->val.scnodes = NULL;
1058 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001059 } else if (src->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +02001060 set_fill_boolean(trg, src->val.bln);
Michal Vasko44f3d2c2020-08-24 09:49:38 +02001061 } else if (src->type == LYXP_SET_NUMBER) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001062 set_fill_number(trg, src->val.num);
1063 } else if (src->type == LYXP_SET_STRING) {
1064 set_fill_string(trg, src->val.str, strlen(src->val.str));
1065 } else {
1066 if (trg->type == LYXP_SET_NODE_SET) {
1067 free(trg->val.nodes);
1068 } else if (trg->type == LYXP_SET_STRING) {
1069 free(trg->val.str);
1070 }
1071
Michal Vaskod3678892020-05-21 10:06:58 +02001072 assert(src->type == LYXP_SET_NODE_SET);
1073
1074 trg->type = LYXP_SET_NODE_SET;
1075 trg->used = src->used;
1076 trg->size = src->used;
1077 trg->ctx_pos = src->ctx_pos;
1078 trg->ctx_size = src->ctx_size;
1079
Michal Vasko08e9b112021-06-11 15:41:17 +02001080 if (trg->size) {
1081 trg->val.nodes = malloc(trg->size * sizeof *trg->val.nodes);
1082 LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), );
1083 memcpy(trg->val.nodes, src->val.nodes, src->used * sizeof *src->val.nodes);
1084 } else {
1085 trg->val.nodes = NULL;
1086 }
Michal Vaskod3678892020-05-21 10:06:58 +02001087 if (src->ht) {
1088 trg->ht = lyht_dup(src->ht);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001089 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02001090 trg->ht = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001091 }
1092 }
1093}
1094
1095/**
1096 * @brief Clear context of all schema nodes.
1097 *
1098 * @param[in] set Set to clear.
Michal Vasko1a09b212021-05-06 13:00:10 +02001099 * @param[in] new_ctx New context state for all the nodes currently in the context.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001100 */
1101static void
Michal Vasko1a09b212021-05-06 13:00:10 +02001102set_scnode_clear_ctx(struct lyxp_set *set, int32_t new_ctx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001103{
1104 uint32_t i;
1105
1106 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001107 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko1a09b212021-05-06 13:00:10 +02001108 set->val.scnodes[i].in_ctx = new_ctx;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001109 } else if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START) {
1110 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001111 }
1112 }
1113}
1114
1115/**
1116 * @brief Remove a node from a set. Removing last node changes
1117 * set into LYXP_SET_EMPTY. Context position aware.
1118 *
1119 * @param[in] set Set to use.
1120 * @param[in] idx Index from @p set of the node to be removed.
1121 */
1122static void
1123set_remove_node(struct lyxp_set *set, uint32_t idx)
1124{
1125 assert(set && (set->type == LYXP_SET_NODE_SET));
1126 assert(idx < set->used);
1127
1128 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
1129
1130 --set->used;
Michal Vasko08e9b112021-06-11 15:41:17 +02001131 if (idx < set->used) {
1132 memmove(&set->val.nodes[idx], &set->val.nodes[idx + 1], (set->used - idx) * sizeof *set->val.nodes);
1133 } else if (!set->used) {
Michal Vaskod3678892020-05-21 10:06:58 +02001134 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001135 }
1136}
1137
1138/**
Michal Vasko2caefc12019-11-14 16:07:56 +01001139 * @brief Remove a node from a set by setting its type to LYXP_NODE_NONE.
Michal Vasko57eab132019-09-24 11:46:26 +02001140 *
1141 * @param[in] set Set to use.
1142 * @param[in] idx Index from @p set of the node to be removed.
1143 */
1144static void
Michal Vasko2caefc12019-11-14 16:07:56 +01001145set_remove_node_none(struct lyxp_set *set, uint32_t idx)
Michal Vasko57eab132019-09-24 11:46:26 +02001146{
1147 assert(set && (set->type == LYXP_SET_NODE_SET));
1148 assert(idx < set->used);
1149
Michal Vasko2caefc12019-11-14 16:07:56 +01001150 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1151 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
1152 }
1153 set->val.nodes[idx].type = LYXP_NODE_NONE;
Michal Vasko57eab132019-09-24 11:46:26 +02001154}
1155
1156/**
Michal Vasko2caefc12019-11-14 16:07:56 +01001157 * @brief Remove all LYXP_NODE_NONE nodes from a set. Removing last node changes
Michal Vasko57eab132019-09-24 11:46:26 +02001158 * set into LYXP_SET_EMPTY. Context position aware.
1159 *
1160 * @param[in] set Set to consolidate.
1161 */
1162static void
Michal Vasko2caefc12019-11-14 16:07:56 +01001163set_remove_nodes_none(struct lyxp_set *set)
Michal Vasko57eab132019-09-24 11:46:26 +02001164{
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01001165 uint32_t i, orig_used, end = 0;
1166 int64_t start;
Michal Vasko57eab132019-09-24 11:46:26 +02001167
Michal Vaskod3678892020-05-21 10:06:58 +02001168 assert(set);
Michal Vasko57eab132019-09-24 11:46:26 +02001169
1170 orig_used = set->used;
1171 set->used = 0;
Michal Vaskod989ba02020-08-24 10:59:24 +02001172 for (i = 0; i < orig_used; ) {
Michal Vasko57eab132019-09-24 11:46:26 +02001173 start = -1;
1174 do {
Michal Vasko2caefc12019-11-14 16:07:56 +01001175 if ((set->val.nodes[i].type != LYXP_NODE_NONE) && (start == -1)) {
Michal Vasko57eab132019-09-24 11:46:26 +02001176 start = i;
Michal Vasko2caefc12019-11-14 16:07:56 +01001177 } else if ((start > -1) && (set->val.nodes[i].type == LYXP_NODE_NONE)) {
Michal Vasko57eab132019-09-24 11:46:26 +02001178 end = i;
1179 ++i;
1180 break;
1181 }
1182
1183 ++i;
1184 if (i == orig_used) {
1185 end = i;
1186 }
1187 } while (i < orig_used);
1188
1189 if (start > -1) {
1190 /* move the whole chunk of valid nodes together */
1191 if (set->used != (unsigned)start) {
1192 memmove(&set->val.nodes[set->used], &set->val.nodes[start], (end - start) * sizeof *set->val.nodes);
1193 }
1194 set->used += end - start;
1195 }
1196 }
Michal Vasko57eab132019-09-24 11:46:26 +02001197}
1198
1199/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02001200 * @brief Check for duplicates in a node set.
1201 *
1202 * @param[in] set Set to check.
1203 * @param[in] node Node to look for in @p set.
1204 * @param[in] node_type Type of @p node.
1205 * @param[in] skip_idx Index from @p set to skip.
1206 * @return LY_ERR
1207 */
1208static LY_ERR
1209set_dup_node_check(const struct lyxp_set *set, const struct lyd_node *node, enum lyxp_node_type node_type, int skip_idx)
1210{
1211 uint32_t i;
1212
Michal Vasko2caefc12019-11-14 16:07:56 +01001213 if (set->ht && node) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001214 return set_dup_node_hash_check(set, (struct lyd_node *)node, node_type, skip_idx);
1215 }
1216
1217 for (i = 0; i < set->used; ++i) {
1218 if ((skip_idx > -1) && (i == (unsigned)skip_idx)) {
1219 continue;
1220 }
1221
1222 if ((set->val.nodes[i].node == node) && (set->val.nodes[i].type == node_type)) {
1223 return LY_EEXIST;
1224 }
1225 }
1226
1227 return LY_SUCCESS;
1228}
1229
Radek Krejci857189e2020-09-01 13:26:36 +02001230ly_bool
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001231lyxp_set_scnode_contains(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type, int skip_idx,
1232 uint32_t *index_p)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001233{
1234 uint32_t i;
1235
1236 for (i = 0; i < set->used; ++i) {
1237 if ((skip_idx > -1) && (i == (unsigned)skip_idx)) {
1238 continue;
1239 }
1240
1241 if ((set->val.scnodes[i].scnode == node) && (set->val.scnodes[i].type == node_type)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001242 if (index_p) {
1243 *index_p = i;
1244 }
1245 return 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001246 }
1247 }
1248
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001249 return 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001250}
1251
Michal Vaskoecd62de2019-11-13 12:35:11 +01001252void
1253lyxp_set_scnode_merge(struct lyxp_set *set1, struct lyxp_set *set2)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001254{
1255 uint32_t orig_used, i, j;
1256
Michal Vaskod3678892020-05-21 10:06:58 +02001257 assert((set1->type == LYXP_SET_SCNODE_SET) && (set2->type == LYXP_SET_SCNODE_SET));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001258
Michal Vaskod3678892020-05-21 10:06:58 +02001259 if (!set2->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001260 return;
1261 }
1262
Michal Vaskod3678892020-05-21 10:06:58 +02001263 if (!set1->used) {
aPiecekadc1e4f2021-10-07 11:15:12 +02001264 /* release hidden allocated data (lyxp_set.size) */
1265 lyxp_set_free_content(set1);
1266 /* direct copying of the entire structure */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001267 memcpy(set1, set2, sizeof *set1);
1268 return;
1269 }
1270
1271 if (set1->used + set2->used > set1->size) {
1272 set1->size = set1->used + set2->used;
1273 set1->val.scnodes = ly_realloc(set1->val.scnodes, set1->size * sizeof *set1->val.scnodes);
1274 LY_CHECK_ERR_RET(!set1->val.scnodes, LOGMEM(set1->ctx), );
1275 }
1276
1277 orig_used = set1->used;
1278
1279 for (i = 0; i < set2->used; ++i) {
1280 for (j = 0; j < orig_used; ++j) {
1281 /* detect duplicities */
1282 if (set1->val.scnodes[j].scnode == set2->val.scnodes[i].scnode) {
1283 break;
1284 }
1285 }
1286
Michal Vasko0587bce2020-12-10 12:19:21 +01001287 if (j < orig_used) {
1288 /* node is there, but update its status if needed */
1289 if (set1->val.scnodes[j].in_ctx == LYXP_SET_SCNODE_START_USED) {
1290 set1->val.scnodes[j].in_ctx = set2->val.scnodes[i].in_ctx;
Michal Vasko1a09b212021-05-06 13:00:10 +02001291 } else if ((set1->val.scnodes[j].in_ctx == LYXP_SET_SCNODE_ATOM_NODE) &&
1292 (set2->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_VAL)) {
1293 set1->val.scnodes[j].in_ctx = set2->val.scnodes[i].in_ctx;
Michal Vasko0587bce2020-12-10 12:19:21 +01001294 }
1295 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001296 memcpy(&set1->val.scnodes[set1->used], &set2->val.scnodes[i], sizeof *set2->val.scnodes);
1297 ++set1->used;
1298 }
1299 }
1300
Michal Vaskod3678892020-05-21 10:06:58 +02001301 lyxp_set_free_content(set2);
1302 set2->type = LYXP_SET_SCNODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001303}
1304
1305/**
1306 * @brief Insert a node into a set. Context position aware.
1307 *
1308 * @param[in] set Set to use.
1309 * @param[in] node Node to insert to @p set.
1310 * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting.
1311 * @param[in] node_type Node type of @p node.
1312 * @param[in] idx Index in @p set to insert into.
1313 */
1314static void
1315set_insert_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx)
1316{
Michal Vaskod3678892020-05-21 10:06:58 +02001317 assert(set && (set->type == LYXP_SET_NODE_SET));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001318
Michal Vaskod3678892020-05-21 10:06:58 +02001319 if (!set->size) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001320 /* first item */
1321 if (idx) {
1322 /* no real harm done, but it is a bug */
1323 LOGINT(set->ctx);
1324 idx = 0;
1325 }
1326 set->val.nodes = malloc(LYXP_SET_SIZE_START * sizeof *set->val.nodes);
1327 LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), );
1328 set->type = LYXP_SET_NODE_SET;
1329 set->used = 0;
1330 set->size = LYXP_SET_SIZE_START;
1331 set->ctx_pos = 1;
1332 set->ctx_size = 1;
1333 set->ht = NULL;
1334 } else {
1335 /* not an empty set */
1336 if (set->used == set->size) {
1337
1338 /* set is full */
Michal Vasko871df522022-04-06 12:14:41 +02001339 set->val.nodes = ly_realloc(set->val.nodes, (set->size * LYXP_SET_SIZE_MUL_STEP) * sizeof *set->val.nodes);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001340 LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), );
Michal Vasko871df522022-04-06 12:14:41 +02001341 set->size *= LYXP_SET_SIZE_MUL_STEP;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001342 }
1343
1344 if (idx > set->used) {
1345 LOGINT(set->ctx);
1346 idx = set->used;
1347 }
1348
1349 /* make space for the new node */
1350 if (idx < set->used) {
1351 memmove(&set->val.nodes[idx + 1], &set->val.nodes[idx], (set->used - idx) * sizeof *set->val.nodes);
1352 }
1353 }
1354
1355 /* finally assign the value */
1356 set->val.nodes[idx].node = (struct lyd_node *)node;
1357 set->val.nodes[idx].type = node_type;
1358 set->val.nodes[idx].pos = pos;
1359 ++set->used;
1360
Michal Vasko2416fdd2021-10-01 11:07:10 +02001361 /* add into hash table */
1362 set_insert_node_hash(set, (struct lyd_node *)node, node_type);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001363}
1364
Michal Vaskoe4a6d012023-05-22 14:34:52 +02001365LY_ERR
1366lyxp_set_scnode_insert_node(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type,
Michal Vasko7333cb32022-07-29 16:30:29 +02001367 enum lyxp_axis axis, uint32_t *index_p)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001368{
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001369 uint32_t index;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001370
1371 assert(set->type == LYXP_SET_SCNODE_SET);
1372
Michal Vasko871df522022-04-06 12:14:41 +02001373 if (!set->size) {
1374 /* first item */
1375 set->val.scnodes = malloc(LYXP_SET_SIZE_START * sizeof *set->val.scnodes);
1376 LY_CHECK_ERR_RET(!set->val.scnodes, LOGMEM(set->ctx), LY_EMEM);
1377 set->type = LYXP_SET_SCNODE_SET;
1378 set->used = 0;
1379 set->size = LYXP_SET_SIZE_START;
1380 set->ctx_pos = 1;
1381 set->ctx_size = 1;
1382 set->ht = NULL;
1383 }
1384
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001385 if (lyxp_set_scnode_contains(set, node, node_type, -1, &index)) {
Michal Vaskoe4a6d012023-05-22 14:34:52 +02001386 /* BUG if axes differ, this new one is thrown away */
Radek Krejcif13b87b2020-12-01 22:02:17 +01001387 set->val.scnodes[index].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001388 } else {
1389 if (set->used == set->size) {
Michal Vasko871df522022-04-06 12:14:41 +02001390 set->val.scnodes = ly_realloc(set->val.scnodes, (set->size * LYXP_SET_SIZE_MUL_STEP) * sizeof *set->val.scnodes);
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001391 LY_CHECK_ERR_RET(!set->val.scnodes, LOGMEM(set->ctx), LY_EMEM);
Michal Vasko871df522022-04-06 12:14:41 +02001392 set->size *= LYXP_SET_SIZE_MUL_STEP;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001393 }
1394
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001395 index = set->used;
1396 set->val.scnodes[index].scnode = (struct lysc_node *)node;
1397 set->val.scnodes[index].type = node_type;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001398 set->val.scnodes[index].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko7333cb32022-07-29 16:30:29 +02001399 set->val.scnodes[index].axis = axis;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001400 ++set->used;
1401 }
1402
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001403 if (index_p) {
1404 *index_p = index;
1405 }
1406
1407 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001408}
1409
1410/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02001411 * @brief Set all nodes with ctx 1 to a new unique context value.
1412 *
1413 * @param[in] set Set to modify.
1414 * @return New context value.
1415 */
Michal Vasko5c4e5892019-11-14 12:31:38 +01001416static int32_t
Michal Vasko03ff5a72019-09-11 13:49:33 +02001417set_scnode_new_in_ctx(struct lyxp_set *set)
1418{
Michal Vasko5c4e5892019-11-14 12:31:38 +01001419 uint32_t i;
1420 int32_t ret_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001421
1422 assert(set->type == LYXP_SET_SCNODE_SET);
1423
Radek Krejcif13b87b2020-12-01 22:02:17 +01001424 ret_ctx = LYXP_SET_SCNODE_ATOM_PRED_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001425retry:
1426 for (i = 0; i < set->used; ++i) {
1427 if (set->val.scnodes[i].in_ctx >= ret_ctx) {
1428 ret_ctx = set->val.scnodes[i].in_ctx + 1;
1429 goto retry;
1430 }
1431 }
1432 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001433 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001434 set->val.scnodes[i].in_ctx = ret_ctx;
1435 }
1436 }
1437
1438 return ret_ctx;
1439}
1440
1441/**
1442 * @brief Get unique @p node position in the data.
1443 *
1444 * @param[in] node Node to find.
1445 * @param[in] node_type Node type of @p node.
1446 * @param[in] root Root node.
1447 * @param[in] root_type Type of the XPath @p root node.
1448 * @param[in] prev Node that we think is before @p node in DFS from @p root. Can optionally
1449 * be used to increase efficiency and start the DFS from this node.
1450 * @param[in] prev_pos Node @p prev position. Optional, but must be set if @p prev is set.
1451 * @return Node position.
1452 */
1453static uint32_t
1454get_node_pos(const struct lyd_node *node, enum lyxp_node_type node_type, const struct lyd_node *root,
Radek Krejci0f969882020-08-21 16:56:47 +02001455 enum lyxp_node_type root_type, const struct lyd_node **prev, uint32_t *prev_pos)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001456{
Michal Vasko56daf732020-08-10 10:57:18 +02001457 const struct lyd_node *elem = NULL, *top_sibling;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001458 uint32_t pos = 1;
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001459 ly_bool found = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001460
1461 assert(prev && prev_pos && !root->prev->next);
1462
1463 if ((node_type == LYXP_NODE_ROOT) || (node_type == LYXP_NODE_ROOT_CONFIG)) {
1464 return 0;
1465 }
1466
1467 if (*prev) {
1468 /* start from the previous element instead from the root */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001469 pos = *prev_pos;
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001470 for (top_sibling = *prev; top_sibling->parent; top_sibling = lyd_parent(top_sibling)) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001471 goto dfs_search;
1472 }
1473
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001474 LY_LIST_FOR(root, top_sibling) {
Michal Vasko56daf732020-08-10 10:57:18 +02001475 LYD_TREE_DFS_BEGIN(top_sibling, elem) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001476dfs_search:
Michal Vaskoa9309bb2021-07-09 09:31:55 +02001477 LYD_TREE_DFS_continue = 0;
1478
Michal Vasko56daf732020-08-10 10:57:18 +02001479 if (*prev && !elem) {
1480 /* resume previous DFS */
1481 elem = LYD_TREE_DFS_next = (struct lyd_node *)*prev;
1482 LYD_TREE_DFS_continue = 0;
1483 }
1484
Michal Vasko222be682023-08-24 08:17:12 +02001485 if ((root_type == LYXP_NODE_ROOT_CONFIG) && (elem->schema->flags & LYS_CONFIG_R)) {
Michal Vasko56daf732020-08-10 10:57:18 +02001486 /* skip */
1487 LYD_TREE_DFS_continue = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001488 } else {
Michal Vasko56daf732020-08-10 10:57:18 +02001489 if (elem == node) {
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001490 found = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001491 break;
1492 }
Michal Vasko56daf732020-08-10 10:57:18 +02001493 ++pos;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001494 }
Michal Vasko56daf732020-08-10 10:57:18 +02001495
1496 LYD_TREE_DFS_END(top_sibling, elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001497 }
1498
1499 /* node found */
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001500 if (found) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001501 break;
1502 }
1503 }
1504
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001505 if (!found) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001506 if (!(*prev)) {
1507 /* we went from root and failed to find it, cannot be */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001508 LOGINT(LYD_CTX(node));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001509 return 0;
1510 } else {
Michal Vasko56daf732020-08-10 10:57:18 +02001511 /* start the search again from the beginning */
1512 *prev = root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001513
Michal Vasko56daf732020-08-10 10:57:18 +02001514 top_sibling = root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001515 pos = 1;
1516 goto dfs_search;
1517 }
1518 }
1519
1520 /* remember the last found node for next time */
1521 *prev = node;
1522 *prev_pos = pos;
1523
1524 return pos;
1525}
1526
1527/**
1528 * @brief Assign (fill) missing node positions.
1529 *
1530 * @param[in] set Set to fill positions in.
1531 * @param[in] root Context root node.
1532 * @param[in] root_type Context root type.
1533 * @return LY_ERR
1534 */
1535static LY_ERR
1536set_assign_pos(struct lyxp_set *set, const struct lyd_node *root, enum lyxp_node_type root_type)
1537{
1538 const struct lyd_node *prev = NULL, *tmp_node;
1539 uint32_t i, tmp_pos = 0;
1540
1541 for (i = 0; i < set->used; ++i) {
1542 if (!set->val.nodes[i].pos) {
1543 tmp_node = NULL;
1544 switch (set->val.nodes[i].type) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001545 case LYXP_NODE_META:
1546 tmp_node = set->val.meta[i].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001547 if (!tmp_node) {
1548 LOGINT_RET(root->schema->module->ctx);
1549 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01001550 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001551 case LYXP_NODE_ELEM:
1552 case LYXP_NODE_TEXT:
1553 if (!tmp_node) {
1554 tmp_node = set->val.nodes[i].node;
1555 }
1556 set->val.nodes[i].pos = get_node_pos(tmp_node, set->val.nodes[i].type, root, root_type, &prev, &tmp_pos);
1557 break;
1558 default:
1559 /* all roots have position 0 */
1560 break;
1561 }
1562 }
1563 }
1564
1565 return LY_SUCCESS;
1566}
1567
1568/**
Michal Vasko9f96a052020-03-10 09:41:45 +01001569 * @brief Get unique @p meta position in the parent metadata.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001570 *
Michal Vasko9f96a052020-03-10 09:41:45 +01001571 * @param[in] meta Metadata to use.
1572 * @return Metadata position.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001573 */
Michal Vaskodd528af2022-08-08 14:35:07 +02001574static uint32_t
Michal Vasko9f96a052020-03-10 09:41:45 +01001575get_meta_pos(struct lyd_meta *meta)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001576{
Michal Vaskodd528af2022-08-08 14:35:07 +02001577 uint32_t pos = 0;
Michal Vasko9f96a052020-03-10 09:41:45 +01001578 struct lyd_meta *meta2;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001579
Michal Vasko9f96a052020-03-10 09:41:45 +01001580 for (meta2 = meta->parent->meta; meta2 && (meta2 != meta); meta2 = meta2->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001581 ++pos;
1582 }
1583
Michal Vasko9f96a052020-03-10 09:41:45 +01001584 assert(meta2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001585 return pos;
1586}
1587
1588/**
1589 * @brief Compare 2 nodes in respect to XPath document order.
1590 *
1591 * @param[in] item1 1st node.
1592 * @param[in] item2 2nd node.
1593 * @return If 1st > 2nd returns 1, 1st == 2nd returns 0, and 1st < 2nd returns -1.
1594 */
1595static int
1596set_sort_compare(struct lyxp_set_node *item1, struct lyxp_set_node *item2)
1597{
Michal Vasko9f96a052020-03-10 09:41:45 +01001598 uint32_t meta_pos1 = 0, meta_pos2 = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001599
1600 if (item1->pos < item2->pos) {
1601 return -1;
1602 }
1603
1604 if (item1->pos > item2->pos) {
1605 return 1;
1606 }
1607
1608 /* node positions are equal, the fun case */
1609
1610 /* 1st ELEM - == - 2nd TEXT, 1st TEXT - == - 2nd ELEM */
1611 /* special case since text nodes are actually saved as their parents */
1612 if ((item1->node == item2->node) && (item1->type != item2->type)) {
1613 if (item1->type == LYXP_NODE_ELEM) {
1614 assert(item2->type == LYXP_NODE_TEXT);
1615 return -1;
1616 } else {
1617 assert((item1->type == LYXP_NODE_TEXT) && (item2->type == LYXP_NODE_ELEM));
1618 return 1;
1619 }
1620 }
1621
Michal Vasko9f96a052020-03-10 09:41:45 +01001622 /* we need meta positions now */
1623 if (item1->type == LYXP_NODE_META) {
1624 meta_pos1 = get_meta_pos((struct lyd_meta *)item1->node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001625 }
Michal Vasko9f96a052020-03-10 09:41:45 +01001626 if (item2->type == LYXP_NODE_META) {
1627 meta_pos2 = get_meta_pos((struct lyd_meta *)item2->node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001628 }
1629
Michal Vasko9f96a052020-03-10 09:41:45 +01001630 /* 1st ROOT - 2nd ROOT, 1st ELEM - 2nd ELEM, 1st TEXT - 2nd TEXT, 1st META - =pos= - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001631 /* check for duplicates */
1632 if (item1->node == item2->node) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001633 assert((item1->type == item2->type) && ((item1->type != LYXP_NODE_META) || (meta_pos1 == meta_pos2)));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001634 return 0;
1635 }
1636
Michal Vasko9f96a052020-03-10 09:41:45 +01001637 /* 1st ELEM - 2nd TEXT, 1st ELEM - any pos - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001638 /* elem is always first, 2nd node is after it */
1639 if (item1->type == LYXP_NODE_ELEM) {
1640 assert(item2->type != LYXP_NODE_ELEM);
1641 return -1;
1642 }
1643
Michal Vasko9f96a052020-03-10 09:41:45 +01001644 /* 1st TEXT - 2nd ELEM, 1st TEXT - any pos - 2nd META, 1st META - any pos - 2nd ELEM, 1st META - >pos> - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001645 /* 2nd is before 1st */
Michal Vasko69730152020-10-09 16:30:07 +02001646 if (((item1->type == LYXP_NODE_TEXT) &&
1647 ((item2->type == LYXP_NODE_ELEM) || (item2->type == LYXP_NODE_META))) ||
1648 ((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_ELEM)) ||
1649 (((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_META)) &&
1650 (meta_pos1 > meta_pos2))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001651 return 1;
1652 }
1653
Michal Vasko9f96a052020-03-10 09:41:45 +01001654 /* 1st META - any pos - 2nd TEXT, 1st META <pos< - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001655 /* 2nd is after 1st */
1656 return -1;
1657}
1658
1659/**
1660 * @brief Set cast for comparisons.
1661 *
Michal Vasko8abcecc2022-07-28 09:55:01 +02001662 * @param[in,out] trg Target set to cast source into.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001663 * @param[in] src Source set.
1664 * @param[in] type Target set type.
1665 * @param[in] src_idx Source set node index.
Michal Vasko8abcecc2022-07-28 09:55:01 +02001666 * @return LY_SUCCESS on success.
1667 * @return LY_ERR value on error.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001668 */
1669static LY_ERR
Michal Vasko8abcecc2022-07-28 09:55:01 +02001670set_comp_cast(struct lyxp_set *trg, const struct lyxp_set *src, enum lyxp_set_type type, uint32_t src_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001671{
1672 assert(src->type == LYXP_SET_NODE_SET);
1673
1674 set_init(trg, src);
1675
1676 /* insert node into target set */
1677 set_insert_node(trg, src->val.nodes[src_idx].node, src->val.nodes[src_idx].pos, src->val.nodes[src_idx].type, 0);
1678
1679 /* cast target set appropriately */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001680 return lyxp_set_cast(trg, type);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001681}
1682
Michal Vasko4c7763f2020-07-27 17:40:37 +02001683/**
1684 * @brief Set content canonization for comparisons.
1685 *
Michal Vasko8abcecc2022-07-28 09:55:01 +02001686 * @param[in,out] set Set to canonize.
Michal Vasko4c7763f2020-07-27 17:40:37 +02001687 * @param[in] xp_node Source XPath node/meta to use for canonization.
Michal Vasko8abcecc2022-07-28 09:55:01 +02001688 * @return LY_SUCCESS on success.
1689 * @return LY_ERR value on error.
Michal Vasko4c7763f2020-07-27 17:40:37 +02001690 */
1691static LY_ERR
Michal Vasko8abcecc2022-07-28 09:55:01 +02001692set_comp_canonize(struct lyxp_set *set, const struct lyxp_set_node *xp_node)
Michal Vasko4c7763f2020-07-27 17:40:37 +02001693{
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001694 const struct lysc_type *type = NULL;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001695 struct lyd_value val;
1696 struct ly_err_item *err = NULL;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001697 LY_ERR rc;
1698
1699 /* is there anything to canonize even? */
Michal Vasko8abcecc2022-07-28 09:55:01 +02001700 if (set->type == LYXP_SET_STRING) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02001701 /* do we have a type to use for canonization? */
Michal Vasko222be682023-08-24 08:17:12 +02001702 if ((xp_node->type == LYXP_NODE_ELEM) && xp_node->node->schema && (xp_node->node->schema->nodetype & LYD_NODE_TERM)) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02001703 type = ((struct lyd_node_term *)xp_node->node)->value.realtype;
1704 } else if (xp_node->type == LYXP_NODE_META) {
1705 type = ((struct lyd_meta *)xp_node->node)->value.realtype;
1706 }
1707 }
1708 if (!type) {
Michal Vasko8abcecc2022-07-28 09:55:01 +02001709 /* no canonization needed/possible */
1710 return LY_SUCCESS;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001711 }
1712
Michal Vasko8abcecc2022-07-28 09:55:01 +02001713 /* check for built-in types without required canonization */
1714 if ((type->basetype == LY_TYPE_STRING) && (type->plugin->store == lyplg_type_store_string)) {
1715 /* string */
1716 return LY_SUCCESS;
1717 }
1718 if ((type->basetype == LY_TYPE_BOOL) && (type->plugin->store == lyplg_type_store_boolean)) {
1719 /* boolean */
1720 return LY_SUCCESS;
1721 }
1722 if ((type->basetype == LY_TYPE_ENUM) && (type->plugin->store == lyplg_type_store_enum)) {
1723 /* enumeration */
1724 return LY_SUCCESS;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001725 }
1726
Michal Vasko8abcecc2022-07-28 09:55:01 +02001727 /* print canonized string, ignore errors, the value may not satisfy schema constraints */
1728 rc = type->plugin->store(set->ctx, type, set->val.str, strlen(set->val.str), 0, set->format, set->prefix_data,
Michal Vasko405cc9e2020-12-01 12:01:27 +01001729 LYD_HINT_DATA, xp_node->node->schema, &val, NULL, &err);
Michal Vasko4c7763f2020-07-27 17:40:37 +02001730 ly_err_free(err);
1731 if (rc) {
Michal Vasko8abcecc2022-07-28 09:55:01 +02001732 /* invalid value, function store automaticaly dealloc value when fail */
1733 return LY_SUCCESS;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001734 }
1735
Michal Vasko8abcecc2022-07-28 09:55:01 +02001736 /* use the canonized string value */
1737 free(set->val.str);
1738 set->val.str = strdup(lyd_value_get_canonical(set->ctx, &val));
1739 type->plugin->free(set->ctx, &val);
1740 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), LY_EMEM);
Michal Vasko4c7763f2020-07-27 17:40:37 +02001741
Michal Vasko4c7763f2020-07-27 17:40:37 +02001742 return LY_SUCCESS;
1743}
1744
Michal Vasko03ff5a72019-09-11 13:49:33 +02001745/**
1746 * @brief Bubble sort @p set into XPath document order.
Michal Vasko49fec8e2022-05-24 10:28:33 +02001747 * Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001748 *
1749 * @param[in] set Set to sort.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001750 * @return How many times the whole set was traversed - 1 (if set was sorted, returns 0).
1751 */
1752static int
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001753set_sort(struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001754{
1755 uint32_t i, j;
Radek Krejci1deb5be2020-08-26 16:43:36 +02001756 int ret = 0, cmp;
Radek Krejci857189e2020-09-01 13:26:36 +02001757 ly_bool inverted, change;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001758 const struct lyd_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001759 struct lyxp_set_node item;
1760 struct lyxp_set_hash_node hnode;
1761 uint64_t hash;
1762
Michal Vasko3cf8fbf2020-05-27 15:21:21 +02001763 if ((set->type != LYXP_SET_NODE_SET) || (set->used < 2)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001764 return 0;
1765 }
1766
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001767 /* find first top-level node to be used as anchor for positions */
Michal Vasko4a7d4d62021-12-13 17:05:06 +01001768 for (root = set->tree; root->parent; root = lyd_parent(root)) {}
Michal Vaskod989ba02020-08-24 10:59:24 +02001769 for ( ; root->prev->next; root = root->prev) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001770
1771 /* fill positions */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001772 if (set_assign_pos(set, root, set->root_type)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001773 return -1;
1774 }
1775
Michal Vasko88a9e802022-05-24 10:50:28 +02001776#ifndef NDEBUG
Michal Vasko03ff5a72019-09-11 13:49:33 +02001777 LOGDBG(LY_LDGXPATH, "SORT BEGIN");
1778 print_set_debug(set);
Michal Vasko88a9e802022-05-24 10:50:28 +02001779#endif
Michal Vasko03ff5a72019-09-11 13:49:33 +02001780
1781 for (i = 0; i < set->used; ++i) {
1782 inverted = 0;
1783 change = 0;
1784
1785 for (j = 1; j < set->used - i; ++j) {
1786 /* compare node positions */
1787 if (inverted) {
1788 cmp = set_sort_compare(&set->val.nodes[j], &set->val.nodes[j - 1]);
1789 } else {
1790 cmp = set_sort_compare(&set->val.nodes[j - 1], &set->val.nodes[j]);
1791 }
1792
1793 /* swap if needed */
1794 if ((inverted && (cmp < 0)) || (!inverted && (cmp > 0))) {
1795 change = 1;
1796
1797 item = set->val.nodes[j - 1];
1798 set->val.nodes[j - 1] = set->val.nodes[j];
1799 set->val.nodes[j] = item;
1800 } else {
1801 /* whether node_pos1 should be smaller than node_pos2 or the other way around */
1802 inverted = !inverted;
1803 }
1804 }
1805
1806 ++ret;
1807
1808 if (!change) {
1809 break;
1810 }
1811 }
1812
Michal Vasko88a9e802022-05-24 10:50:28 +02001813#ifndef NDEBUG
Michal Vasko03ff5a72019-09-11 13:49:33 +02001814 LOGDBG(LY_LDGXPATH, "SORT END %d", ret);
1815 print_set_debug(set);
Michal Vasko88a9e802022-05-24 10:50:28 +02001816#endif
Michal Vasko03ff5a72019-09-11 13:49:33 +02001817
1818 /* check node hashes */
1819 if (set->used >= LYD_HT_MIN_ITEMS) {
1820 assert(set->ht);
1821 for (i = 0; i < set->used; ++i) {
1822 hnode.node = set->val.nodes[i].node;
1823 hnode.type = set->val.nodes[i].type;
1824
Michal Vaskoae130f52023-04-20 14:25:16 +02001825 hash = lyht_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
1826 hash = lyht_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
1827 hash = lyht_hash_multi(hash, NULL, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001828
1829 assert(!lyht_find(set->ht, &hnode, hash, NULL));
1830 }
1831 }
1832
1833 return ret - 1;
1834}
1835
Michal Vasko03ff5a72019-09-11 13:49:33 +02001836/**
1837 * @brief Merge 2 sorted sets into one.
1838 *
1839 * @param[in,out] trg Set to merge into. Duplicates are removed.
1840 * @param[in] src Set to be merged into @p trg. It is cast to #LYXP_SET_EMPTY on success.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001841 * @return LY_ERR
1842 */
1843static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001844set_sorted_merge(struct lyxp_set *trg, struct lyxp_set *src)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001845{
1846 uint32_t i, j, k, count, dup_count;
1847 int cmp;
1848 const struct lyd_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001849
Michal Vaskod3678892020-05-21 10:06:58 +02001850 if ((trg->type != LYXP_SET_NODE_SET) || (src->type != LYXP_SET_NODE_SET)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001851 return LY_EINVAL;
1852 }
1853
Michal Vaskod3678892020-05-21 10:06:58 +02001854 if (!src->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001855 return LY_SUCCESS;
Michal Vaskod3678892020-05-21 10:06:58 +02001856 } else if (!trg->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001857 set_fill_set(trg, src);
Michal Vaskod3678892020-05-21 10:06:58 +02001858 lyxp_set_free_content(src);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001859 return LY_SUCCESS;
1860 }
1861
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001862 /* find first top-level node to be used as anchor for positions */
Michal Vasko0143b682021-12-13 17:13:09 +01001863 for (root = trg->tree; root->parent; root = lyd_parent(root)) {}
Michal Vaskod989ba02020-08-24 10:59:24 +02001864 for ( ; root->prev->next; root = root->prev) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001865
1866 /* fill positions */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001867 if (set_assign_pos(trg, root, trg->root_type) || set_assign_pos(src, root, src->root_type)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001868 return LY_EINT;
1869 }
1870
1871#ifndef NDEBUG
1872 LOGDBG(LY_LDGXPATH, "MERGE target");
1873 print_set_debug(trg);
1874 LOGDBG(LY_LDGXPATH, "MERGE source");
1875 print_set_debug(src);
1876#endif
1877
1878 /* make memory for the merge (duplicates are not detected yet, so space
1879 * will likely be wasted on them, too bad) */
1880 if (trg->size - trg->used < src->used) {
1881 trg->size = trg->used + src->used;
1882
1883 trg->val.nodes = ly_realloc(trg->val.nodes, trg->size * sizeof *trg->val.nodes);
1884 LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx), LY_EMEM);
1885 }
1886
1887 i = 0;
1888 j = 0;
1889 count = 0;
1890 dup_count = 0;
1891 do {
1892 cmp = set_sort_compare(&src->val.nodes[i], &trg->val.nodes[j]);
1893 if (!cmp) {
1894 if (!count) {
1895 /* duplicate, just skip it */
1896 ++i;
1897 ++j;
1898 } else {
1899 /* we are copying something already, so let's copy the duplicate too,
1900 * we are hoping that afterwards there are some more nodes to
1901 * copy and this way we can copy them all together */
1902 ++count;
1903 ++dup_count;
1904 ++i;
1905 ++j;
1906 }
1907 } else if (cmp < 0) {
1908 /* inserting src node into trg, just remember it for now */
1909 ++count;
1910 ++i;
1911
1912 /* insert the hash now */
1913 set_insert_node_hash(trg, src->val.nodes[i - 1].node, src->val.nodes[i - 1].type);
1914 } else if (count) {
1915copy_nodes:
1916 /* time to actually copy the nodes, we have found the largest block of nodes */
1917 memmove(&trg->val.nodes[j + (count - dup_count)],
1918 &trg->val.nodes[j],
1919 (trg->used - j) * sizeof *trg->val.nodes);
1920 memcpy(&trg->val.nodes[j - dup_count], &src->val.nodes[i - count], count * sizeof *src->val.nodes);
1921
1922 trg->used += count - dup_count;
1923 /* do not change i, except the copying above, we are basically doing exactly what is in the else branch below */
1924 j += count - dup_count;
1925
1926 count = 0;
1927 dup_count = 0;
1928 } else {
1929 ++j;
1930 }
1931 } while ((i < src->used) && (j < trg->used));
1932
1933 if ((i < src->used) || count) {
1934 /* insert all the hashes first */
1935 for (k = i; k < src->used; ++k) {
1936 set_insert_node_hash(trg, src->val.nodes[k].node, src->val.nodes[k].type);
1937 }
1938
1939 /* loop ended, but we need to copy something at trg end */
1940 count += src->used - i;
1941 i = src->used;
1942 goto copy_nodes;
1943 }
1944
1945 /* we are inserting hashes before the actual node insert, which causes
1946 * situations when there were initially not enough items for a hash table,
1947 * but even after some were inserted, hash table was not created (during
1948 * insertion the number of items is not updated yet) */
1949 if (!trg->ht && (trg->used >= LYD_HT_MIN_ITEMS)) {
1950 set_insert_node_hash(trg, NULL, 0);
1951 }
1952
1953#ifndef NDEBUG
1954 LOGDBG(LY_LDGXPATH, "MERGE result");
1955 print_set_debug(trg);
1956#endif
1957
Michal Vaskod3678892020-05-21 10:06:58 +02001958 lyxp_set_free_content(src);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001959 return LY_SUCCESS;
1960}
1961
Michal Vasko14676352020-05-29 11:35:55 +02001962LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02001963lyxp_check_token(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint32_t tok_idx, enum lyxp_token want_tok)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001964{
Michal Vasko004d3152020-06-11 19:59:22 +02001965 if (exp->used == tok_idx) {
Michal Vasko14676352020-05-29 11:35:55 +02001966 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001967 LOGVAL(ctx, LY_VCODE_XP_EOF);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001968 }
Michal Vasko14676352020-05-29 11:35:55 +02001969 return LY_EINCOMPLETE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001970 }
1971
Michal Vasko004d3152020-06-11 19:59:22 +02001972 if (want_tok && (exp->tokens[tok_idx] != want_tok)) {
Michal Vasko14676352020-05-29 11:35:55 +02001973 if (ctx) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02001974 LOGVAL(ctx, LY_VCODE_XP_INTOK2, lyxp_token2str(exp->tokens[tok_idx]),
1975 &exp->expr[exp->tok_pos[tok_idx]], lyxp_token2str(want_tok));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001976 }
Michal Vasko14676352020-05-29 11:35:55 +02001977 return LY_ENOT;
1978 }
1979
1980 return LY_SUCCESS;
1981}
1982
Michal Vasko004d3152020-06-11 19:59:22 +02001983LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02001984lyxp_next_token(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint32_t *tok_idx, enum lyxp_token want_tok)
Michal Vasko004d3152020-06-11 19:59:22 +02001985{
1986 LY_CHECK_RET(lyxp_check_token(ctx, exp, *tok_idx, want_tok));
1987
1988 /* skip the token */
1989 ++(*tok_idx);
1990
1991 return LY_SUCCESS;
1992}
1993
Michal Vasko14676352020-05-29 11:35:55 +02001994/* just like lyxp_check_token() but tests for 2 tokens */
1995static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02001996exp_check_token2(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint32_t tok_idx, enum lyxp_token want_tok1,
Radek Krejci0f969882020-08-21 16:56:47 +02001997 enum lyxp_token want_tok2)
Michal Vasko14676352020-05-29 11:35:55 +02001998{
Michal Vasko004d3152020-06-11 19:59:22 +02001999 if (exp->used == tok_idx) {
Michal Vasko14676352020-05-29 11:35:55 +02002000 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002001 LOGVAL(ctx, LY_VCODE_XP_EOF);
Michal Vasko14676352020-05-29 11:35:55 +02002002 }
2003 return LY_EINCOMPLETE;
2004 }
2005
Michal Vasko004d3152020-06-11 19:59:22 +02002006 if ((exp->tokens[tok_idx] != want_tok1) && (exp->tokens[tok_idx] != want_tok2)) {
Michal Vasko14676352020-05-29 11:35:55 +02002007 if (ctx) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02002008 LOGVAL(ctx, LY_VCODE_XP_INTOK, lyxp_token2str(exp->tokens[tok_idx]),
Michal Vasko0b468e62020-10-19 09:33:04 +02002009 &exp->expr[exp->tok_pos[tok_idx]]);
Michal Vasko14676352020-05-29 11:35:55 +02002010 }
2011 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002012 }
2013
2014 return LY_SUCCESS;
2015}
2016
Michal Vasko4911eeb2021-06-28 11:23:05 +02002017LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02002018lyxp_next_token2(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint32_t *tok_idx, enum lyxp_token want_tok1,
Michal Vasko4911eeb2021-06-28 11:23:05 +02002019 enum lyxp_token want_tok2)
2020{
2021 LY_CHECK_RET(exp_check_token2(ctx, exp, *tok_idx, want_tok1, want_tok2));
2022
2023 /* skip the token */
2024 ++(*tok_idx);
2025
2026 return LY_SUCCESS;
2027}
2028
Michal Vasko03ff5a72019-09-11 13:49:33 +02002029/**
2030 * @brief Stack operation push on the repeat array.
2031 *
2032 * @param[in] exp Expression to use.
Michal Vasko831e3bd2023-04-24 10:43:38 +02002033 * @param[in] tok_idx Position in the expresion @p exp.
2034 * @param[in] repeat_expr_type Repeated expression type, this value is pushed.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002035 */
2036static void
Michal Vasko831e3bd2023-04-24 10:43:38 +02002037exp_repeat_push(struct lyxp_expr *exp, uint32_t tok_idx, enum lyxp_expr_type repeat_expr_type)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002038{
Michal Vaskodd528af2022-08-08 14:35:07 +02002039 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002040
Michal Vasko004d3152020-06-11 19:59:22 +02002041 if (exp->repeat[tok_idx]) {
Radek Krejci1e008d22020-08-17 11:37:37 +02002042 for (i = 0; exp->repeat[tok_idx][i]; ++i) {}
Michal Vasko004d3152020-06-11 19:59:22 +02002043 exp->repeat[tok_idx] = realloc(exp->repeat[tok_idx], (i + 2) * sizeof *exp->repeat[tok_idx]);
2044 LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), );
Michal Vasko831e3bd2023-04-24 10:43:38 +02002045 exp->repeat[tok_idx][i] = repeat_expr_type;
Michal Vasko004d3152020-06-11 19:59:22 +02002046 exp->repeat[tok_idx][i + 1] = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002047 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02002048 exp->repeat[tok_idx] = calloc(2, sizeof *exp->repeat[tok_idx]);
2049 LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), );
Michal Vasko831e3bd2023-04-24 10:43:38 +02002050 exp->repeat[tok_idx][0] = repeat_expr_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002051 }
2052}
2053
2054/**
2055 * @brief Reparse Predicate. Logs directly on error.
2056 *
2057 * [7] Predicate ::= '[' Expr ']'
2058 *
2059 * @param[in] ctx Context for logging.
2060 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002061 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002062 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002063 * @return LY_ERR
2064 */
2065static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02002066reparse_predicate(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint32_t *tok_idx, uint32_t depth)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002067{
2068 LY_ERR rc;
2069
Michal Vasko004d3152020-06-11 19:59:22 +02002070 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002071 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002072 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002073
aPiecekbf968d92021-05-27 14:35:05 +02002074 rc = reparse_or_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002075 LY_CHECK_RET(rc);
2076
Michal Vasko004d3152020-06-11 19:59:22 +02002077 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002078 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002079 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002080
2081 return LY_SUCCESS;
2082}
2083
2084/**
2085 * @brief Reparse RelativeLocationPath. Logs directly on error.
2086 *
2087 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
2088 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
2089 * [6] NodeTest ::= NameTest | NodeType '(' ')'
2090 *
2091 * @param[in] ctx Context for logging.
2092 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002093 * @param[in] tok_idx Position in the expression \p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002094 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002095 * @return LY_ERR (LY_EINCOMPLETE on forward reference)
2096 */
2097static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02002098reparse_relative_location_path(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint32_t *tok_idx, uint32_t depth)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002099{
2100 LY_ERR rc;
2101
Michal Vasko004d3152020-06-11 19:59:22 +02002102 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002103 LY_CHECK_RET(rc);
2104
2105 goto step;
2106 do {
2107 /* '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02002108 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002109
Michal Vasko004d3152020-06-11 19:59:22 +02002110 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002111 LY_CHECK_RET(rc);
2112step:
2113 /* Step */
Michal Vasko004d3152020-06-11 19:59:22 +02002114 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002115 case LYXP_TOKEN_DOT:
Michal Vasko004d3152020-06-11 19:59:22 +02002116 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002117 break;
2118
2119 case LYXP_TOKEN_DDOT:
Michal Vasko004d3152020-06-11 19:59:22 +02002120 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002121 break;
2122
Michal Vasko49fec8e2022-05-24 10:28:33 +02002123 case LYXP_TOKEN_AXISNAME:
2124 ++(*tok_idx);
2125
2126 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_DCOLON);
2127 LY_CHECK_RET(rc);
2128
2129 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02002130 case LYXP_TOKEN_AT:
Michal Vasko004d3152020-06-11 19:59:22 +02002131 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002132
Michal Vasko004d3152020-06-11 19:59:22 +02002133 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002134 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002135 if ((exp->tokens[*tok_idx] != LYXP_TOKEN_NAMETEST) && (exp->tokens[*tok_idx] != LYXP_TOKEN_NODETYPE)) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02002136 LOGVAL(ctx, LY_VCODE_XP_INTOK, lyxp_token2str(exp->tokens[*tok_idx]), &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002137 return LY_EVALID;
2138 }
Michal Vasko49fec8e2022-05-24 10:28:33 +02002139 if (exp->tokens[*tok_idx] == LYXP_TOKEN_NODETYPE) {
2140 goto reparse_nodetype;
2141 }
Radek Krejci0f969882020-08-21 16:56:47 +02002142 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02002143 case LYXP_TOKEN_NAMETEST:
Michal Vasko004d3152020-06-11 19:59:22 +02002144 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002145 goto reparse_predicate;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002146
2147 case LYXP_TOKEN_NODETYPE:
Michal Vasko49fec8e2022-05-24 10:28:33 +02002148reparse_nodetype:
Michal Vasko004d3152020-06-11 19:59:22 +02002149 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002150
2151 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02002152 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002153 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002154 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002155
2156 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02002157 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002158 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002159 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002160
2161reparse_predicate:
2162 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002163 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
aPiecekbf968d92021-05-27 14:35:05 +02002164 rc = reparse_predicate(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002165 LY_CHECK_RET(rc);
2166 }
2167 break;
2168 default:
Michal Vasko49fec8e2022-05-24 10:28:33 +02002169 LOGVAL(ctx, LY_VCODE_XP_INTOK, lyxp_token2str(exp->tokens[*tok_idx]), &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002170 return LY_EVALID;
2171 }
Michal Vasko004d3152020-06-11 19:59:22 +02002172 } while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02002173
2174 return LY_SUCCESS;
2175}
2176
2177/**
2178 * @brief Reparse AbsoluteLocationPath. Logs directly on error.
2179 *
2180 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
2181 *
2182 * @param[in] ctx Context for logging.
2183 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002184 * @param[in] tok_idx Position in the expression \p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002185 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002186 * @return LY_ERR
2187 */
2188static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02002189reparse_absolute_location_path(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint32_t *tok_idx, uint32_t depth)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002190{
2191 LY_ERR rc;
2192
Michal Vasko004d3152020-06-11 19:59:22 +02002193 LY_CHECK_RET(exp_check_token2(ctx, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02002194
2195 /* '/' RelativeLocationPath? */
Michal Vasko004d3152020-06-11 19:59:22 +02002196 if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002197 /* '/' */
Michal Vasko004d3152020-06-11 19:59:22 +02002198 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002199
Michal Vasko004d3152020-06-11 19:59:22 +02002200 if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002201 return LY_SUCCESS;
2202 }
Michal Vasko004d3152020-06-11 19:59:22 +02002203 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002204 case LYXP_TOKEN_DOT:
2205 case LYXP_TOKEN_DDOT:
Michal Vasko49fec8e2022-05-24 10:28:33 +02002206 case LYXP_TOKEN_AXISNAME:
Michal Vasko03ff5a72019-09-11 13:49:33 +02002207 case LYXP_TOKEN_AT:
2208 case LYXP_TOKEN_NAMETEST:
2209 case LYXP_TOKEN_NODETYPE:
aPiecekbf968d92021-05-27 14:35:05 +02002210 rc = reparse_relative_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002211 LY_CHECK_RET(rc);
Radek Krejci0f969882020-08-21 16:56:47 +02002212 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02002213 default:
2214 break;
2215 }
2216
Michal Vasko03ff5a72019-09-11 13:49:33 +02002217 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02002218 /* '//' RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002219 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002220
aPiecekbf968d92021-05-27 14:35:05 +02002221 rc = reparse_relative_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002222 LY_CHECK_RET(rc);
2223 }
2224
2225 return LY_SUCCESS;
2226}
2227
2228/**
2229 * @brief Reparse FunctionCall. Logs directly on error.
2230 *
2231 * [9] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
2232 *
2233 * @param[in] ctx Context for logging.
2234 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002235 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002236 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002237 * @return LY_ERR
2238 */
2239static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02002240reparse_function_call(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint32_t *tok_idx, uint32_t depth)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002241{
Radek Krejci1deb5be2020-08-26 16:43:36 +02002242 int8_t min_arg_count = -1;
Michal Vaskodd528af2022-08-08 14:35:07 +02002243 uint32_t arg_count, max_arg_count = 0, func_tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002244 LY_ERR rc;
2245
Michal Vasko004d3152020-06-11 19:59:22 +02002246 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_FUNCNAME);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002247 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002248 func_tok_idx = *tok_idx;
2249 switch (exp->tok_len[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002250 case 3:
Michal Vasko004d3152020-06-11 19:59:22 +02002251 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002252 min_arg_count = 1;
2253 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002254 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002255 min_arg_count = 1;
2256 max_arg_count = 1;
2257 }
2258 break;
2259 case 4:
Michal Vasko004d3152020-06-11 19:59:22 +02002260 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002261 min_arg_count = 1;
2262 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002263 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002264 min_arg_count = 0;
2265 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002266 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002267 min_arg_count = 0;
2268 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002269 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002270 min_arg_count = 0;
2271 max_arg_count = 0;
2272 }
2273 break;
2274 case 5:
Michal Vasko004d3152020-06-11 19:59:22 +02002275 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002276 min_arg_count = 1;
2277 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002278 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002279 min_arg_count = 0;
2280 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002281 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002282 min_arg_count = 1;
2283 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002284 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002285 min_arg_count = 1;
2286 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002287 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002288 min_arg_count = 1;
2289 max_arg_count = 1;
2290 }
2291 break;
2292 case 6:
Michal Vasko004d3152020-06-11 19:59:22 +02002293 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002294 min_arg_count = 2;
Radek Krejci1deb5be2020-08-26 16:43:36 +02002295 max_arg_count = UINT32_MAX;
Michal Vasko004d3152020-06-11 19:59:22 +02002296 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002297 min_arg_count = 0;
2298 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002299 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002300 min_arg_count = 0;
2301 max_arg_count = 1;
2302 }
2303 break;
2304 case 7:
Michal Vasko004d3152020-06-11 19:59:22 +02002305 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002306 min_arg_count = 1;
2307 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002308 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002309 min_arg_count = 1;
2310 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002311 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002312 min_arg_count = 0;
2313 max_arg_count = 0;
2314 }
2315 break;
2316 case 8:
Michal Vasko004d3152020-06-11 19:59:22 +02002317 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002318 min_arg_count = 2;
2319 max_arg_count = 2;
Michal Vasko004d3152020-06-11 19:59:22 +02002320 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002321 min_arg_count = 0;
2322 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002323 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "re-match", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002324 min_arg_count = 2;
2325 max_arg_count = 2;
2326 }
2327 break;
2328 case 9:
Michal Vasko004d3152020-06-11 19:59:22 +02002329 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002330 min_arg_count = 2;
2331 max_arg_count = 3;
Michal Vasko004d3152020-06-11 19:59:22 +02002332 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002333 min_arg_count = 3;
2334 max_arg_count = 3;
2335 }
2336 break;
2337 case 10:
Michal Vasko004d3152020-06-11 19:59:22 +02002338 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002339 min_arg_count = 0;
2340 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002341 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002342 min_arg_count = 1;
2343 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002344 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002345 min_arg_count = 2;
2346 max_arg_count = 2;
2347 }
2348 break;
2349 case 11:
Michal Vasko004d3152020-06-11 19:59:22 +02002350 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002351 min_arg_count = 2;
2352 max_arg_count = 2;
2353 }
2354 break;
2355 case 12:
Michal Vasko004d3152020-06-11 19:59:22 +02002356 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002357 min_arg_count = 2;
2358 max_arg_count = 2;
2359 }
2360 break;
2361 case 13:
Michal Vasko004d3152020-06-11 19:59:22 +02002362 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002363 min_arg_count = 0;
2364 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002365 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string-length", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002366 min_arg_count = 0;
2367 max_arg_count = 1;
2368 }
2369 break;
2370 case 15:
Michal Vasko004d3152020-06-11 19:59:22 +02002371 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002372 min_arg_count = 0;
2373 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002374 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-after", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002375 min_arg_count = 2;
2376 max_arg_count = 2;
2377 }
2378 break;
2379 case 16:
Michal Vasko004d3152020-06-11 19:59:22 +02002380 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002381 min_arg_count = 2;
2382 max_arg_count = 2;
2383 }
2384 break;
2385 case 20:
Michal Vasko004d3152020-06-11 19:59:22 +02002386 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from-or-self", 20)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002387 min_arg_count = 2;
2388 max_arg_count = 2;
2389 }
2390 break;
2391 }
2392 if (min_arg_count == -1) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002393 LOGVAL(ctx, LY_VCODE_XP_INFUNC, exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002394 return LY_EINVAL;
2395 }
Michal Vasko004d3152020-06-11 19:59:22 +02002396 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002397
2398 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02002399 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002400 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002401 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002402
2403 /* ( Expr ( ',' Expr )* )? */
2404 arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002405 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002406 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002407 if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002408 ++arg_count;
aPiecekbf968d92021-05-27 14:35:05 +02002409 rc = reparse_or_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002410 LY_CHECK_RET(rc);
2411 }
Michal Vasko004d3152020-06-11 19:59:22 +02002412 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) {
2413 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002414
2415 ++arg_count;
aPiecekbf968d92021-05-27 14:35:05 +02002416 rc = reparse_or_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002417 LY_CHECK_RET(rc);
2418 }
2419
2420 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02002421 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002422 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002423 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002424
Radek Krejci857189e2020-09-01 13:26:36 +02002425 if ((arg_count < (uint32_t)min_arg_count) || (arg_count > max_arg_count)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002426 LOGVAL(ctx, LY_VCODE_XP_INARGCOUNT, arg_count, exp->tok_len[func_tok_idx], &exp->expr[exp->tok_pos[func_tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002427 return LY_EVALID;
2428 }
2429
2430 return LY_SUCCESS;
2431}
2432
2433/**
2434 * @brief Reparse PathExpr. Logs directly on error.
2435 *
2436 * [10] PathExpr ::= LocationPath | PrimaryExpr Predicate*
2437 * | PrimaryExpr Predicate* '/' RelativeLocationPath
2438 * | PrimaryExpr Predicate* '//' RelativeLocationPath
2439 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
aPiecekfba75362021-10-07 12:39:48 +02002440 * [8] PrimaryExpr ::= VariableReference | '(' Expr ')' | Literal | Number | FunctionCall
Michal Vasko03ff5a72019-09-11 13:49:33 +02002441 *
2442 * @param[in] ctx Context for logging.
2443 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002444 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002445 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002446 * @return LY_ERR
2447 */
2448static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02002449reparse_path_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint32_t *tok_idx, uint32_t depth)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002450{
2451 LY_ERR rc;
2452
Michal Vasko004d3152020-06-11 19:59:22 +02002453 if (lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko14676352020-05-29 11:35:55 +02002454 return LY_EVALID;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002455 }
2456
Michal Vasko004d3152020-06-11 19:59:22 +02002457 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002458 case LYXP_TOKEN_PAR1:
2459 /* '(' Expr ')' Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002460 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002461
aPiecekbf968d92021-05-27 14:35:05 +02002462 rc = reparse_or_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002463 LY_CHECK_RET(rc);
2464
Michal Vasko004d3152020-06-11 19:59:22 +02002465 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002466 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002467 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002468 goto predicate;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002469 case LYXP_TOKEN_DOT:
2470 case LYXP_TOKEN_DDOT:
Michal Vasko49fec8e2022-05-24 10:28:33 +02002471 case LYXP_TOKEN_AXISNAME:
Michal Vasko03ff5a72019-09-11 13:49:33 +02002472 case LYXP_TOKEN_AT:
2473 case LYXP_TOKEN_NAMETEST:
2474 case LYXP_TOKEN_NODETYPE:
2475 /* RelativeLocationPath */
aPiecekbf968d92021-05-27 14:35:05 +02002476 rc = reparse_relative_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002477 LY_CHECK_RET(rc);
2478 break;
aPiecekfba75362021-10-07 12:39:48 +02002479 case LYXP_TOKEN_VARREF:
2480 /* VariableReference */
2481 ++(*tok_idx);
2482 goto predicate;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002483 case LYXP_TOKEN_FUNCNAME:
2484 /* FunctionCall */
aPiecekbf968d92021-05-27 14:35:05 +02002485 rc = reparse_function_call(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002486 LY_CHECK_RET(rc);
2487 goto predicate;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002488 case LYXP_TOKEN_OPER_PATH:
2489 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +02002490 /* AbsoluteLocationPath */
aPiecekbf968d92021-05-27 14:35:05 +02002491 rc = reparse_absolute_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002492 LY_CHECK_RET(rc);
2493 break;
2494 case LYXP_TOKEN_LITERAL:
2495 /* Literal */
Michal Vasko004d3152020-06-11 19:59:22 +02002496 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002497 goto predicate;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002498 case LYXP_TOKEN_NUMBER:
2499 /* Number */
Michal Vasko004d3152020-06-11 19:59:22 +02002500 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002501 goto predicate;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002502 default:
Michal Vasko49fec8e2022-05-24 10:28:33 +02002503 LOGVAL(ctx, LY_VCODE_XP_INTOK, lyxp_token2str(exp->tokens[*tok_idx]), &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002504 return LY_EVALID;
2505 }
2506
2507 return LY_SUCCESS;
2508
2509predicate:
2510 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002511 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
aPiecekbf968d92021-05-27 14:35:05 +02002512 rc = reparse_predicate(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002513 LY_CHECK_RET(rc);
2514 }
2515
2516 /* ('/' or '//') RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002517 if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002518
2519 /* '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02002520 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002521
aPiecekbf968d92021-05-27 14:35:05 +02002522 rc = reparse_relative_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002523 LY_CHECK_RET(rc);
2524 }
2525
2526 return LY_SUCCESS;
2527}
2528
2529/**
2530 * @brief Reparse UnaryExpr. Logs directly on error.
2531 *
2532 * [17] UnaryExpr ::= UnionExpr | '-' UnaryExpr
2533 * [18] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
2534 *
2535 * @param[in] ctx Context for logging.
2536 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002537 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002538 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002539 * @return LY_ERR
2540 */
2541static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02002542reparse_unary_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint32_t *tok_idx, uint32_t depth)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002543{
Michal Vaskodd528af2022-08-08 14:35:07 +02002544 uint32_t prev_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002545 LY_ERR rc;
2546
2547 /* ('-')* */
Michal Vasko004d3152020-06-11 19:59:22 +02002548 prev_exp = *tok_idx;
Michal Vasko69730152020-10-09 16:30:07 +02002549 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2550 (exp->expr[exp->tok_pos[*tok_idx]] == '-')) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002551 exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNARY);
Michal Vasko004d3152020-06-11 19:59:22 +02002552 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002553 }
2554
2555 /* PathExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002556 prev_exp = *tok_idx;
aPiecekbf968d92021-05-27 14:35:05 +02002557 rc = reparse_path_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002558 LY_CHECK_RET(rc);
2559
2560 /* ('|' PathExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002561 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_UNI)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002562 exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNION);
Michal Vasko004d3152020-06-11 19:59:22 +02002563 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002564
aPiecekbf968d92021-05-27 14:35:05 +02002565 rc = reparse_path_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002566 LY_CHECK_RET(rc);
2567 }
2568
2569 return LY_SUCCESS;
2570}
2571
2572/**
2573 * @brief Reparse AdditiveExpr. Logs directly on error.
2574 *
2575 * [15] AdditiveExpr ::= MultiplicativeExpr
2576 * | AdditiveExpr '+' MultiplicativeExpr
2577 * | AdditiveExpr '-' MultiplicativeExpr
2578 * [16] MultiplicativeExpr ::= UnaryExpr
2579 * | MultiplicativeExpr '*' UnaryExpr
2580 * | MultiplicativeExpr 'div' UnaryExpr
2581 * | MultiplicativeExpr 'mod' UnaryExpr
2582 *
2583 * @param[in] ctx Context for logging.
2584 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002585 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002586 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002587 * @return LY_ERR
2588 */
2589static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02002590reparse_additive_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint32_t *tok_idx, uint32_t depth)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002591{
Michal Vaskodd528af2022-08-08 14:35:07 +02002592 uint32_t prev_add_exp, prev_mul_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002593 LY_ERR rc;
2594
Michal Vasko004d3152020-06-11 19:59:22 +02002595 prev_add_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002596 goto reparse_multiplicative_expr;
2597
2598 /* ('+' / '-' MultiplicativeExpr)* */
Michal Vasko69730152020-10-09 16:30:07 +02002599 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2600 ((exp->expr[exp->tok_pos[*tok_idx]] == '+') || (exp->expr[exp->tok_pos[*tok_idx]] == '-'))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002601 exp_repeat_push(exp, prev_add_exp, LYXP_EXPR_ADDITIVE);
Michal Vasko004d3152020-06-11 19:59:22 +02002602 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002603
2604reparse_multiplicative_expr:
2605 /* UnaryExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002606 prev_mul_exp = *tok_idx;
aPiecekbf968d92021-05-27 14:35:05 +02002607 rc = reparse_unary_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002608 LY_CHECK_RET(rc);
2609
2610 /* ('*' / 'div' / 'mod' UnaryExpr)* */
Michal Vasko69730152020-10-09 16:30:07 +02002611 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2612 ((exp->expr[exp->tok_pos[*tok_idx]] == '*') || (exp->tok_len[*tok_idx] == 3))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002613 exp_repeat_push(exp, prev_mul_exp, LYXP_EXPR_MULTIPLICATIVE);
Michal Vasko004d3152020-06-11 19:59:22 +02002614 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002615
aPiecekbf968d92021-05-27 14:35:05 +02002616 rc = reparse_unary_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002617 LY_CHECK_RET(rc);
2618 }
2619 }
2620
2621 return LY_SUCCESS;
2622}
2623
2624/**
2625 * @brief Reparse EqualityExpr. Logs directly on error.
2626 *
2627 * [13] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
2628 * | EqualityExpr '!=' RelationalExpr
2629 * [14] RelationalExpr ::= AdditiveExpr
2630 * | RelationalExpr '<' AdditiveExpr
2631 * | RelationalExpr '>' AdditiveExpr
2632 * | RelationalExpr '<=' AdditiveExpr
2633 * | RelationalExpr '>=' AdditiveExpr
2634 *
2635 * @param[in] ctx Context for logging.
2636 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002637 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002638 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002639 * @return LY_ERR
2640 */
2641static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02002642reparse_equality_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint32_t *tok_idx, uint32_t depth)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002643{
Michal Vaskodd528af2022-08-08 14:35:07 +02002644 uint32_t prev_eq_exp, prev_rel_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002645 LY_ERR rc;
2646
Michal Vasko004d3152020-06-11 19:59:22 +02002647 prev_eq_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002648 goto reparse_additive_expr;
2649
2650 /* ('=' / '!=' RelationalExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002651 while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_EQUAL, LYXP_TOKEN_OPER_NEQUAL)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002652 exp_repeat_push(exp, prev_eq_exp, LYXP_EXPR_EQUALITY);
Michal Vasko004d3152020-06-11 19:59:22 +02002653 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002654
2655reparse_additive_expr:
2656 /* AdditiveExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002657 prev_rel_exp = *tok_idx;
aPiecekbf968d92021-05-27 14:35:05 +02002658 rc = reparse_additive_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002659 LY_CHECK_RET(rc);
2660
2661 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002662 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_COMP)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002663 exp_repeat_push(exp, prev_rel_exp, LYXP_EXPR_RELATIONAL);
Michal Vasko004d3152020-06-11 19:59:22 +02002664 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002665
aPiecekbf968d92021-05-27 14:35:05 +02002666 rc = reparse_additive_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002667 LY_CHECK_RET(rc);
2668 }
2669 }
2670
2671 return LY_SUCCESS;
2672}
2673
2674/**
2675 * @brief Reparse OrExpr. Logs directly on error.
2676 *
2677 * [11] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
2678 * [12] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
2679 *
2680 * @param[in] ctx Context for logging.
2681 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002682 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002683 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002684 * @return LY_ERR
2685 */
2686static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02002687reparse_or_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint32_t *tok_idx, uint32_t depth)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002688{
Michal Vaskodd528af2022-08-08 14:35:07 +02002689 uint32_t prev_or_exp, prev_and_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002690 LY_ERR rc;
2691
aPiecekbf968d92021-05-27 14:35:05 +02002692 ++depth;
2693 LY_CHECK_ERR_RET(depth > LYXP_MAX_BLOCK_DEPTH, LOGVAL(ctx, LY_VCODE_XP_DEPTH), LY_EINVAL);
2694
Michal Vasko004d3152020-06-11 19:59:22 +02002695 prev_or_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002696 goto reparse_equality_expr;
2697
2698 /* ('or' AndExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002699 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_LOG) && (exp->tok_len[*tok_idx] == 2)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002700 exp_repeat_push(exp, prev_or_exp, LYXP_EXPR_OR);
Michal Vasko004d3152020-06-11 19:59:22 +02002701 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002702
2703reparse_equality_expr:
2704 /* EqualityExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002705 prev_and_exp = *tok_idx;
aPiecekbf968d92021-05-27 14:35:05 +02002706 rc = reparse_equality_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002707 LY_CHECK_RET(rc);
2708
2709 /* ('and' EqualityExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002710 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_LOG) && (exp->tok_len[*tok_idx] == 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002711 exp_repeat_push(exp, prev_and_exp, LYXP_EXPR_AND);
Michal Vasko004d3152020-06-11 19:59:22 +02002712 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002713
aPiecekbf968d92021-05-27 14:35:05 +02002714 rc = reparse_equality_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002715 LY_CHECK_RET(rc);
2716 }
2717 }
2718
2719 return LY_SUCCESS;
2720}
Radek Krejcib1646a92018-11-02 16:08:26 +01002721
2722/**
2723 * @brief Parse NCName.
2724 *
2725 * @param[in] ncname Name to parse.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002726 * @return Length of @p ncname valid bytes.
Radek Krejcib1646a92018-11-02 16:08:26 +01002727 */
Michal Vasko49fec8e2022-05-24 10:28:33 +02002728static ssize_t
Radek Krejcib1646a92018-11-02 16:08:26 +01002729parse_ncname(const char *ncname)
2730{
Radek Krejci1deb5be2020-08-26 16:43:36 +02002731 uint32_t uc;
Radek Krejcid4270262019-01-07 15:07:25 +01002732 size_t size;
Michal Vasko49fec8e2022-05-24 10:28:33 +02002733 ssize_t len = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01002734
2735 LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), 0);
2736 if (!is_xmlqnamestartchar(uc) || (uc == ':')) {
2737 return len;
2738 }
2739
2740 do {
2741 len += size;
Radek Krejci9a564c92019-01-07 14:53:57 +01002742 if (!*ncname) {
2743 break;
2744 }
Radek Krejcid4270262019-01-07 15:07:25 +01002745 LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), -len);
Radek Krejcib1646a92018-11-02 16:08:26 +01002746 } while (is_xmlqnamechar(uc) && (uc != ':'));
2747
2748 return len;
2749}
2750
2751/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02002752 * @brief Add @p token into the expression @p exp.
Radek Krejcib1646a92018-11-02 16:08:26 +01002753 *
Michal Vasko03ff5a72019-09-11 13:49:33 +02002754 * @param[in] ctx Context for logging.
Radek Krejcib1646a92018-11-02 16:08:26 +01002755 * @param[in] exp Expression to use.
2756 * @param[in] token Token to add.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002757 * @param[in] tok_pos Token position in the XPath expression.
Radek Krejcib1646a92018-11-02 16:08:26 +01002758 * @param[in] tok_len Token length in the XPath expression.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002759 * @return LY_ERR
Radek Krejcib1646a92018-11-02 16:08:26 +01002760 */
2761static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02002762exp_add_token(const struct ly_ctx *ctx, struct lyxp_expr *exp, enum lyxp_token token, uint32_t tok_pos, uint32_t tok_len)
Radek Krejcib1646a92018-11-02 16:08:26 +01002763{
2764 uint32_t prev;
2765
2766 if (exp->used == exp->size) {
2767 prev = exp->size;
2768 exp->size += LYXP_EXPR_SIZE_STEP;
2769 if (prev > exp->size) {
2770 LOGINT(ctx);
2771 return LY_EINT;
2772 }
2773
2774 exp->tokens = ly_realloc(exp->tokens, exp->size * sizeof *exp->tokens);
2775 LY_CHECK_ERR_RET(!exp->tokens, LOGMEM(ctx), LY_EMEM);
2776 exp->tok_pos = ly_realloc(exp->tok_pos, exp->size * sizeof *exp->tok_pos);
2777 LY_CHECK_ERR_RET(!exp->tok_pos, LOGMEM(ctx), LY_EMEM);
2778 exp->tok_len = ly_realloc(exp->tok_len, exp->size * sizeof *exp->tok_len);
2779 LY_CHECK_ERR_RET(!exp->tok_len, LOGMEM(ctx), LY_EMEM);
2780 }
2781
2782 exp->tokens[exp->used] = token;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002783 exp->tok_pos[exp->used] = tok_pos;
Radek Krejcib1646a92018-11-02 16:08:26 +01002784 exp->tok_len[exp->used] = tok_len;
2785 ++exp->used;
2786 return LY_SUCCESS;
2787}
2788
2789void
Michal Vasko14676352020-05-29 11:35:55 +02002790lyxp_expr_free(const struct ly_ctx *ctx, struct lyxp_expr *expr)
Radek Krejcib1646a92018-11-02 16:08:26 +01002791{
Michal Vaskodd528af2022-08-08 14:35:07 +02002792 uint32_t i;
Radek Krejcib1646a92018-11-02 16:08:26 +01002793
2794 if (!expr) {
2795 return;
2796 }
2797
2798 lydict_remove(ctx, expr->expr);
2799 free(expr->tokens);
2800 free(expr->tok_pos);
2801 free(expr->tok_len);
2802 if (expr->repeat) {
2803 for (i = 0; i < expr->used; ++i) {
2804 free(expr->repeat[i]);
2805 }
2806 }
2807 free(expr->repeat);
2808 free(expr);
2809}
2810
Michal Vasko49fec8e2022-05-24 10:28:33 +02002811/**
2812 * @brief Parse Axis name.
2813 *
2814 * @param[in] str String to parse.
2815 * @param[in] str_len Length of @p str.
2816 * @return LY_SUCCESS if an axis.
2817 * @return LY_ENOT otherwise.
2818 */
2819static LY_ERR
2820expr_parse_axis(const char *str, size_t str_len)
2821{
2822 switch (str_len) {
2823 case 4:
2824 if (!strncmp("self", str, str_len)) {
2825 return LY_SUCCESS;
2826 }
2827 break;
2828 case 5:
2829 if (!strncmp("child", str, str_len)) {
2830 return LY_SUCCESS;
2831 }
2832 break;
2833 case 6:
2834 if (!strncmp("parent", str, str_len)) {
2835 return LY_SUCCESS;
2836 }
2837 break;
2838 case 8:
2839 if (!strncmp("ancestor", str, str_len)) {
2840 return LY_SUCCESS;
2841 }
2842 break;
2843 case 9:
2844 if (!strncmp("attribute", str, str_len)) {
2845 return LY_SUCCESS;
2846 } else if (!strncmp("following", str, str_len)) {
2847 return LY_SUCCESS;
2848 } else if (!strncmp("namespace", str, str_len)) {
2849 LOGERR(NULL, LY_EINVAL, "Axis \"namespace\" not supported.");
2850 return LY_ENOT;
2851 } else if (!strncmp("preceding", str, str_len)) {
2852 return LY_SUCCESS;
2853 }
2854 break;
2855 case 10:
2856 if (!strncmp("descendant", str, str_len)) {
2857 return LY_SUCCESS;
2858 }
2859 break;
2860 case 16:
2861 if (!strncmp("ancestor-or-self", str, str_len)) {
2862 return LY_SUCCESS;
2863 }
2864 break;
2865 case 17:
2866 if (!strncmp("following-sibling", str, str_len)) {
2867 return LY_SUCCESS;
2868 } else if (!strncmp("preceding-sibling", str, str_len)) {
2869 return LY_SUCCESS;
2870 }
2871 break;
2872 case 18:
2873 if (!strncmp("descendant-or-self", str, str_len)) {
2874 return LY_SUCCESS;
2875 }
2876 break;
2877 }
2878
2879 return LY_ENOT;
2880}
2881
Radek Krejcif03a9e22020-09-18 20:09:31 +02002882LY_ERR
2883lyxp_expr_parse(const struct ly_ctx *ctx, const char *expr_str, size_t expr_len, ly_bool reparse, struct lyxp_expr **expr_p)
Radek Krejcib1646a92018-11-02 16:08:26 +01002884{
Radek Krejcif03a9e22020-09-18 20:09:31 +02002885 LY_ERR ret = LY_SUCCESS;
2886 struct lyxp_expr *expr;
Radek Krejcid4270262019-01-07 15:07:25 +01002887 size_t parsed = 0, tok_len;
Radek Krejcib1646a92018-11-02 16:08:26 +01002888 enum lyxp_token tok_type;
Michal Vasko49fec8e2022-05-24 10:28:33 +02002889 ly_bool prev_func_check = 0, prev_ntype_check = 0, has_axis;
Michal Vaskodd528af2022-08-08 14:35:07 +02002890 uint32_t tok_idx = 0;
Michal Vasko49fec8e2022-05-24 10:28:33 +02002891 ssize_t ncname_len;
Radek Krejcib1646a92018-11-02 16:08:26 +01002892
Radek Krejcif03a9e22020-09-18 20:09:31 +02002893 assert(expr_p);
2894
2895 if (!expr_str[0]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002896 LOGVAL(ctx, LY_VCODE_XP_EOF);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002897 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02002898 }
2899
2900 if (!expr_len) {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002901 expr_len = strlen(expr_str);
Michal Vasko004d3152020-06-11 19:59:22 +02002902 }
Michal Vaskodd528af2022-08-08 14:35:07 +02002903 if (expr_len > UINT32_MAX) {
2904 LOGVAL(ctx, LYVE_XPATH, "XPath expression cannot be longer than %" PRIu32 " characters.", UINT32_MAX);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002905 return LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01002906 }
2907
2908 /* init lyxp_expr structure */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002909 expr = calloc(1, sizeof *expr);
2910 LY_CHECK_ERR_GOTO(!expr, LOGMEM(ctx); ret = LY_EMEM, error);
2911 LY_CHECK_GOTO(ret = lydict_insert(ctx, expr_str, expr_len, &expr->expr), error);
2912 expr->used = 0;
2913 expr->size = LYXP_EXPR_SIZE_START;
2914 expr->tokens = malloc(expr->size * sizeof *expr->tokens);
2915 LY_CHECK_ERR_GOTO(!expr->tokens, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002916
Radek Krejcif03a9e22020-09-18 20:09:31 +02002917 expr->tok_pos = malloc(expr->size * sizeof *expr->tok_pos);
2918 LY_CHECK_ERR_GOTO(!expr->tok_pos, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002919
Radek Krejcif03a9e22020-09-18 20:09:31 +02002920 expr->tok_len = malloc(expr->size * sizeof *expr->tok_len);
2921 LY_CHECK_ERR_GOTO(!expr->tok_len, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002922
Michal Vasko004d3152020-06-11 19:59:22 +02002923 /* make expr 0-terminated */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002924 expr_str = expr->expr;
Michal Vasko004d3152020-06-11 19:59:22 +02002925
Radek Krejcif03a9e22020-09-18 20:09:31 +02002926 while (is_xmlws(expr_str[parsed])) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002927 ++parsed;
2928 }
2929
2930 do {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002931 if (expr_str[parsed] == '(') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002932
2933 /* '(' */
2934 tok_len = 1;
2935 tok_type = LYXP_TOKEN_PAR1;
2936
Michal Vasko49fec8e2022-05-24 10:28:33 +02002937 if (prev_ntype_check && expr->used && (expr->tokens[expr->used - 1] == LYXP_TOKEN_NAMETEST) &&
2938 (((expr->tok_len[expr->used - 1] == 4) &&
2939 (!strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "node", 4) ||
2940 !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "text", 4))) ||
2941 ((expr->tok_len[expr->used - 1] == 7) &&
2942 !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "comment", 7)))) {
2943 /* it is NodeType after all */
2944 expr->tokens[expr->used - 1] = LYXP_TOKEN_NODETYPE;
2945
2946 prev_ntype_check = 0;
2947 prev_func_check = 0;
2948 } else if (prev_func_check && expr->used && (expr->tokens[expr->used - 1] == LYXP_TOKEN_NAMETEST)) {
2949 /* it is FunctionName after all */
2950 expr->tokens[expr->used - 1] = LYXP_TOKEN_FUNCNAME;
2951
2952 prev_ntype_check = 0;
2953 prev_func_check = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01002954 }
2955
Radek Krejcif03a9e22020-09-18 20:09:31 +02002956 } else if (expr_str[parsed] == ')') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002957
2958 /* ')' */
2959 tok_len = 1;
2960 tok_type = LYXP_TOKEN_PAR2;
2961
Radek Krejcif03a9e22020-09-18 20:09:31 +02002962 } else if (expr_str[parsed] == '[') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002963
2964 /* '[' */
2965 tok_len = 1;
2966 tok_type = LYXP_TOKEN_BRACK1;
2967
Radek Krejcif03a9e22020-09-18 20:09:31 +02002968 } else if (expr_str[parsed] == ']') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002969
2970 /* ']' */
2971 tok_len = 1;
2972 tok_type = LYXP_TOKEN_BRACK2;
2973
Radek Krejcif03a9e22020-09-18 20:09:31 +02002974 } else if (!strncmp(&expr_str[parsed], "..", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002975
2976 /* '..' */
2977 tok_len = 2;
2978 tok_type = LYXP_TOKEN_DDOT;
2979
Radek Krejcif03a9e22020-09-18 20:09:31 +02002980 } else if ((expr_str[parsed] == '.') && (!isdigit(expr_str[parsed + 1]))) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002981
2982 /* '.' */
2983 tok_len = 1;
2984 tok_type = LYXP_TOKEN_DOT;
2985
Radek Krejcif03a9e22020-09-18 20:09:31 +02002986 } else if (expr_str[parsed] == '@') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002987
2988 /* '@' */
2989 tok_len = 1;
2990 tok_type = LYXP_TOKEN_AT;
2991
Radek Krejcif03a9e22020-09-18 20:09:31 +02002992 } else if (expr_str[parsed] == ',') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002993
2994 /* ',' */
2995 tok_len = 1;
2996 tok_type = LYXP_TOKEN_COMMA;
2997
Radek Krejcif03a9e22020-09-18 20:09:31 +02002998 } else if (expr_str[parsed] == '\'') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002999
3000 /* Literal with ' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02003001 for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\''); ++tok_len) {}
3002 LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0',
Radek Krejci2efc45b2020-12-22 16:25:44 +01003003 LOGVAL(ctx, LY_VCODE_XP_EOE, expr_str[parsed], &expr_str[parsed]); ret = LY_EVALID,
Michal Vasko69730152020-10-09 16:30:07 +02003004 error);
Radek Krejcib1646a92018-11-02 16:08:26 +01003005 ++tok_len;
3006 tok_type = LYXP_TOKEN_LITERAL;
3007
Radek Krejcif03a9e22020-09-18 20:09:31 +02003008 } else if (expr_str[parsed] == '\"') {
Radek Krejcib1646a92018-11-02 16:08:26 +01003009
3010 /* Literal with " */
Radek Krejcif03a9e22020-09-18 20:09:31 +02003011 for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\"'); ++tok_len) {}
3012 LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0',
Radek Krejci2efc45b2020-12-22 16:25:44 +01003013 LOGVAL(ctx, LY_VCODE_XP_EOE, expr_str[parsed], &expr_str[parsed]); ret = LY_EVALID,
Michal Vasko69730152020-10-09 16:30:07 +02003014 error);
Radek Krejcib1646a92018-11-02 16:08:26 +01003015 ++tok_len;
3016 tok_type = LYXP_TOKEN_LITERAL;
3017
Radek Krejcif03a9e22020-09-18 20:09:31 +02003018 } else if ((expr_str[parsed] == '.') || (isdigit(expr_str[parsed]))) {
Radek Krejcib1646a92018-11-02 16:08:26 +01003019
3020 /* Number */
Radek Krejcif03a9e22020-09-18 20:09:31 +02003021 for (tok_len = 0; isdigit(expr_str[parsed + tok_len]); ++tok_len) {}
3022 if (expr_str[parsed + tok_len] == '.') {
Radek Krejcib1646a92018-11-02 16:08:26 +01003023 ++tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02003024 for ( ; isdigit(expr_str[parsed + tok_len]); ++tok_len) {}
Radek Krejcib1646a92018-11-02 16:08:26 +01003025 }
3026 tok_type = LYXP_TOKEN_NUMBER;
3027
aPiecekfba75362021-10-07 12:39:48 +02003028 } else if (expr_str[parsed] == '$') {
3029
3030 /* VariableReference */
3031 parsed++;
Michal Vasko49fec8e2022-05-24 10:28:33 +02003032 ncname_len = parse_ncname(&expr_str[parsed]);
aPiecekfba75362021-10-07 12:39:48 +02003033 LY_CHECK_ERR_GOTO(ncname_len < 1, LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed - ncname_len],
Michal Vasko7b3a00e2023-08-09 11:58:03 +02003034 (uint32_t)(parsed - ncname_len + 1), expr_str); ret = LY_EVALID, error);
aPiecekfba75362021-10-07 12:39:48 +02003035 tok_len = ncname_len;
3036 LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == ':',
3037 LOGVAL(ctx, LYVE_XPATH, "Variable with prefix is not supported."); ret = LY_EVALID,
3038 error);
3039 tok_type = LYXP_TOKEN_VARREF;
3040
Radek Krejcif03a9e22020-09-18 20:09:31 +02003041 } else if (expr_str[parsed] == '/') {
Radek Krejcib1646a92018-11-02 16:08:26 +01003042
3043 /* Operator '/', '//' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02003044 if (!strncmp(&expr_str[parsed], "//", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01003045 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003046 tok_type = LYXP_TOKEN_OPER_RPATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01003047 } else {
3048 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003049 tok_type = LYXP_TOKEN_OPER_PATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01003050 }
Radek Krejcib1646a92018-11-02 16:08:26 +01003051
Radek Krejcif03a9e22020-09-18 20:09:31 +02003052 } else if (!strncmp(&expr_str[parsed], "!=", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01003053
Michal Vasko3e48bf32020-06-01 08:39:07 +02003054 /* Operator '!=' */
Radek Krejcib1646a92018-11-02 16:08:26 +01003055 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003056 tok_type = LYXP_TOKEN_OPER_NEQUAL;
3057
Radek Krejcif03a9e22020-09-18 20:09:31 +02003058 } else if (!strncmp(&expr_str[parsed], "<=", 2) || !strncmp(&expr_str[parsed], ">=", 2)) {
Michal Vasko3e48bf32020-06-01 08:39:07 +02003059
3060 /* Operator '<=', '>=' */
3061 tok_len = 2;
3062 tok_type = LYXP_TOKEN_OPER_COMP;
Radek Krejcib1646a92018-11-02 16:08:26 +01003063
Radek Krejcif03a9e22020-09-18 20:09:31 +02003064 } else if (expr_str[parsed] == '|') {
Radek Krejcib1646a92018-11-02 16:08:26 +01003065
3066 /* Operator '|' */
3067 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003068 tok_type = LYXP_TOKEN_OPER_UNI;
Radek Krejcib1646a92018-11-02 16:08:26 +01003069
Radek Krejcif03a9e22020-09-18 20:09:31 +02003070 } else if ((expr_str[parsed] == '+') || (expr_str[parsed] == '-')) {
Radek Krejcib1646a92018-11-02 16:08:26 +01003071
3072 /* Operator '+', '-' */
3073 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003074 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01003075
Radek Krejcif03a9e22020-09-18 20:09:31 +02003076 } else if (expr_str[parsed] == '=') {
Radek Krejcib1646a92018-11-02 16:08:26 +01003077
Michal Vasko3e48bf32020-06-01 08:39:07 +02003078 /* Operator '=' */
Radek Krejcib1646a92018-11-02 16:08:26 +01003079 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003080 tok_type = LYXP_TOKEN_OPER_EQUAL;
3081
Radek Krejcif03a9e22020-09-18 20:09:31 +02003082 } else if ((expr_str[parsed] == '<') || (expr_str[parsed] == '>')) {
Michal Vasko3e48bf32020-06-01 08:39:07 +02003083
3084 /* Operator '<', '>' */
3085 tok_len = 1;
3086 tok_type = LYXP_TOKEN_OPER_COMP;
Radek Krejcib1646a92018-11-02 16:08:26 +01003087
Michal Vasko69730152020-10-09 16:30:07 +02003088 } else if (expr->used && (expr->tokens[expr->used - 1] != LYXP_TOKEN_AT) &&
3089 (expr->tokens[expr->used - 1] != LYXP_TOKEN_PAR1) &&
3090 (expr->tokens[expr->used - 1] != LYXP_TOKEN_BRACK1) &&
3091 (expr->tokens[expr->used - 1] != LYXP_TOKEN_COMMA) &&
3092 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_LOG) &&
3093 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_EQUAL) &&
3094 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_NEQUAL) &&
3095 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_COMP) &&
3096 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_MATH) &&
3097 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_UNI) &&
3098 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_PATH) &&
3099 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_RPATH)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01003100
3101 /* Operator '*', 'or', 'and', 'mod', or 'div' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02003102 if (expr_str[parsed] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01003103 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003104 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01003105
Radek Krejcif03a9e22020-09-18 20:09:31 +02003106 } else if (!strncmp(&expr_str[parsed], "or", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01003107 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003108 tok_type = LYXP_TOKEN_OPER_LOG;
Radek Krejcib1646a92018-11-02 16:08:26 +01003109
Radek Krejcif03a9e22020-09-18 20:09:31 +02003110 } else if (!strncmp(&expr_str[parsed], "and", 3)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01003111 tok_len = 3;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003112 tok_type = LYXP_TOKEN_OPER_LOG;
Radek Krejcib1646a92018-11-02 16:08:26 +01003113
Radek Krejcif03a9e22020-09-18 20:09:31 +02003114 } else if (!strncmp(&expr_str[parsed], "mod", 3) || !strncmp(&expr_str[parsed], "div", 3)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01003115 tok_len = 3;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003116 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01003117
Michal Vasko49fec8e2022-05-24 10:28:33 +02003118 } else if (prev_ntype_check || prev_func_check) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003119 LOGVAL(ctx, LYVE_XPATH, "Invalid character 0x%x ('%c'), perhaps \"%.*s\" is supposed to be a function call.",
Michal Vasko69730152020-10-09 16:30:07 +02003120 expr_str[parsed], expr_str[parsed], expr->tok_len[expr->used - 1], &expr->expr[expr->tok_pos[expr->used - 1]]);
Radek Krejcif03a9e22020-09-18 20:09:31 +02003121 ret = LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01003122 goto error;
3123 } else {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02003124 LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed], (uint32_t)(parsed + 1), expr_str);
Radek Krejcif03a9e22020-09-18 20:09:31 +02003125 ret = LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01003126 goto error;
3127 }
Radek Krejcib1646a92018-11-02 16:08:26 +01003128 } else {
3129
Michal Vasko49fec8e2022-05-24 10:28:33 +02003130 /* (AxisName '::')? ((NCName ':')? '*' | QName) or NodeType/FunctionName */
3131 if (expr_str[parsed] == '*') {
3132 ncname_len = 1;
3133 } else {
3134 ncname_len = parse_ncname(&expr_str[parsed]);
3135 LY_CHECK_ERR_GOTO(ncname_len < 1, LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed - ncname_len],
Michal Vasko7b3a00e2023-08-09 11:58:03 +02003136 (uint32_t)(parsed - ncname_len + 1), expr_str); ret = LY_EVALID, error);
Michal Vasko49fec8e2022-05-24 10:28:33 +02003137 }
Radek Krejcib1646a92018-11-02 16:08:26 +01003138 tok_len = ncname_len;
3139
Michal Vasko49fec8e2022-05-24 10:28:33 +02003140 has_axis = 0;
3141 if (!strncmp(&expr_str[parsed + tok_len], "::", 2)) {
3142 /* axis */
3143 LY_CHECK_ERR_GOTO(expr_parse_axis(&expr_str[parsed], ncname_len),
Michal Vasko7b3a00e2023-08-09 11:58:03 +02003144 LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed], (uint32_t)(parsed + 1), expr_str); ret = LY_EVALID,
3145 error);
Michal Vasko49fec8e2022-05-24 10:28:33 +02003146 tok_type = LYXP_TOKEN_AXISNAME;
3147
3148 LY_CHECK_GOTO(ret = exp_add_token(ctx, expr, tok_type, parsed, tok_len), error);
3149 parsed += tok_len;
3150
3151 /* '::' */
3152 tok_len = 2;
3153 tok_type = LYXP_TOKEN_DCOLON;
3154
3155 LY_CHECK_GOTO(ret = exp_add_token(ctx, expr, tok_type, parsed, tok_len), error);
3156 parsed += tok_len;
3157
3158 if (expr_str[parsed] == '*') {
3159 ncname_len = 1;
3160 } else {
3161 ncname_len = parse_ncname(&expr_str[parsed]);
3162 LY_CHECK_ERR_GOTO(ncname_len < 1, LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed - ncname_len],
Michal Vasko7b3a00e2023-08-09 11:58:03 +02003163 (uint32_t)(parsed - ncname_len + 1), expr_str); ret = LY_EVALID, error);
Michal Vasko49fec8e2022-05-24 10:28:33 +02003164 }
3165 tok_len = ncname_len;
3166
3167 has_axis = 1;
3168 }
3169
Radek Krejcif03a9e22020-09-18 20:09:31 +02003170 if (expr_str[parsed + tok_len] == ':') {
Radek Krejcib1646a92018-11-02 16:08:26 +01003171 ++tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02003172 if (expr_str[parsed + tok_len] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01003173 ++tok_len;
3174 } else {
Radek Krejcif03a9e22020-09-18 20:09:31 +02003175 ncname_len = parse_ncname(&expr_str[parsed + tok_len]);
Michal Vaskoe2be5462021-08-04 10:49:42 +02003176 LY_CHECK_ERR_GOTO(ncname_len < 1, LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed - ncname_len],
Michal Vasko7b3a00e2023-08-09 11:58:03 +02003177 (uint32_t)(parsed - ncname_len + 1), expr_str); ret = LY_EVALID, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01003178 tok_len += ncname_len;
3179 }
Michal Vasko49fec8e2022-05-24 10:28:33 +02003180 /* remove old flags to prevent ambiguities */
3181 prev_ntype_check = 0;
3182 prev_func_check = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01003183 tok_type = LYXP_TOKEN_NAMETEST;
3184 } else {
Michal Vasko49fec8e2022-05-24 10:28:33 +02003185 /* if not '*', there is no prefix so it can still be NodeType/FunctionName, we can't finally decide now */
3186 prev_ntype_check = (expr_str[parsed] == '*') ? 0 : 1;
3187 prev_func_check = (prev_ntype_check && !has_axis) ? 1 : 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01003188 tok_type = LYXP_TOKEN_NAMETEST;
3189 }
3190 }
3191
3192 /* store the token, move on to the next one */
Radek Krejcif03a9e22020-09-18 20:09:31 +02003193 LY_CHECK_GOTO(ret = exp_add_token(ctx, expr, tok_type, parsed, tok_len), error);
Radek Krejcib1646a92018-11-02 16:08:26 +01003194 parsed += tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02003195 while (is_xmlws(expr_str[parsed])) {
Radek Krejcib1646a92018-11-02 16:08:26 +01003196 ++parsed;
3197 }
3198
Radek Krejcif03a9e22020-09-18 20:09:31 +02003199 } while (expr_str[parsed]);
Radek Krejcib1646a92018-11-02 16:08:26 +01003200
Michal Vasko004d3152020-06-11 19:59:22 +02003201 if (reparse) {
3202 /* prealloc repeat */
Radek Krejcif03a9e22020-09-18 20:09:31 +02003203 expr->repeat = calloc(expr->size, sizeof *expr->repeat);
3204 LY_CHECK_ERR_GOTO(!expr->repeat, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01003205
Michal Vasko004d3152020-06-11 19:59:22 +02003206 /* fill repeat */
aPiecekbf968d92021-05-27 14:35:05 +02003207 LY_CHECK_ERR_GOTO(reparse_or_expr(ctx, expr, &tok_idx, 0), ret = LY_EVALID, error);
Radek Krejcif03a9e22020-09-18 20:09:31 +02003208 if (expr->used > tok_idx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003209 LOGVAL(ctx, LYVE_XPATH, "Unparsed characters \"%s\" left at the end of an XPath expression.",
Michal Vasko69730152020-10-09 16:30:07 +02003210 &expr->expr[expr->tok_pos[tok_idx]]);
Radek Krejcif03a9e22020-09-18 20:09:31 +02003211 ret = LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02003212 goto error;
3213 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003214 }
3215
Radek Krejcif03a9e22020-09-18 20:09:31 +02003216 print_expr_struct_debug(expr);
3217 *expr_p = expr;
3218 return LY_SUCCESS;
Radek Krejcib1646a92018-11-02 16:08:26 +01003219
3220error:
Radek Krejcif03a9e22020-09-18 20:09:31 +02003221 lyxp_expr_free(ctx, expr);
3222 return ret;
Radek Krejcib1646a92018-11-02 16:08:26 +01003223}
3224
Michal Vasko1734be92020-09-22 08:55:10 +02003225LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02003226lyxp_expr_dup(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint32_t start_idx, uint32_t end_idx,
Michal Vaskoe33134a2022-07-29 14:54:40 +02003227 struct lyxp_expr **dup_p)
Michal Vasko004d3152020-06-11 19:59:22 +02003228{
Michal Vasko1734be92020-09-22 08:55:10 +02003229 LY_ERR ret = LY_SUCCESS;
3230 struct lyxp_expr *dup = NULL;
Michal Vaskod59039d2022-09-09 09:07:30 +02003231 uint32_t used = 0, i, j, expr_len;
Michal Vaskoe33134a2022-07-29 14:54:40 +02003232 const char *expr_start;
3233
3234 assert((!start_idx && !end_idx) || ((start_idx < exp->used) && (end_idx < exp->used) && (start_idx <= end_idx)));
Michal Vasko004d3152020-06-11 19:59:22 +02003235
Michal Vasko7f45cf22020-10-01 12:49:44 +02003236 if (!exp) {
3237 goto cleanup;
3238 }
3239
Michal Vaskoe33134a2022-07-29 14:54:40 +02003240 if (!start_idx && !end_idx) {
3241 end_idx = exp->used - 1;
3242 }
3243
3244 expr_start = exp->expr + exp->tok_pos[start_idx];
3245 expr_len = (exp->tok_pos[end_idx] + exp->tok_len[end_idx]) - exp->tok_pos[start_idx];
3246
Michal Vasko004d3152020-06-11 19:59:22 +02003247 dup = calloc(1, sizeof *dup);
Michal Vasko1734be92020-09-22 08:55:10 +02003248 LY_CHECK_ERR_GOTO(!dup, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02003249
Michal Vasko08e9b112021-06-11 15:41:17 +02003250 if (exp->used) {
Michal Vaskoe33134a2022-07-29 14:54:40 +02003251 used = (end_idx - start_idx) + 1;
3252
3253 dup->tokens = malloc(used * sizeof *dup->tokens);
Michal Vasko08e9b112021-06-11 15:41:17 +02003254 LY_CHECK_ERR_GOTO(!dup->tokens, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vaskoe33134a2022-07-29 14:54:40 +02003255 memcpy(dup->tokens, exp->tokens + start_idx, used * sizeof *dup->tokens);
Michal Vasko004d3152020-06-11 19:59:22 +02003256
Michal Vaskoe33134a2022-07-29 14:54:40 +02003257 dup->tok_pos = malloc(used * sizeof *dup->tok_pos);
Michal Vasko08e9b112021-06-11 15:41:17 +02003258 LY_CHECK_ERR_GOTO(!dup->tok_pos, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vaskoe33134a2022-07-29 14:54:40 +02003259 memcpy(dup->tok_pos, exp->tok_pos + start_idx, used * sizeof *dup->tok_pos);
Michal Vasko004d3152020-06-11 19:59:22 +02003260
Michal Vaskoe33134a2022-07-29 14:54:40 +02003261 if (start_idx) {
3262 /* fix the indices in the expression */
3263 for (i = 0; i < used; ++i) {
3264 dup->tok_pos[i] -= expr_start - exp->expr;
3265 }
3266 }
3267
3268 dup->tok_len = malloc(used * sizeof *dup->tok_len);
Michal Vasko08e9b112021-06-11 15:41:17 +02003269 LY_CHECK_ERR_GOTO(!dup->tok_len, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vaskoe33134a2022-07-29 14:54:40 +02003270 memcpy(dup->tok_len, exp->tok_len + start_idx, used * sizeof *dup->tok_len);
Michal Vasko004d3152020-06-11 19:59:22 +02003271
Michal Vasko79a7a872022-06-17 09:00:48 +02003272 if (exp->repeat) {
Michal Vaskoe33134a2022-07-29 14:54:40 +02003273 dup->repeat = malloc(used * sizeof *dup->repeat);
Michal Vasko79a7a872022-06-17 09:00:48 +02003274 LY_CHECK_ERR_GOTO(!dup->repeat, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vaskoe33134a2022-07-29 14:54:40 +02003275 for (i = start_idx; i <= end_idx; ++i) {
Michal Vasko79a7a872022-06-17 09:00:48 +02003276 if (!exp->repeat[i]) {
Michal Vaskoe33134a2022-07-29 14:54:40 +02003277 dup->repeat[i - start_idx] = NULL;
Michal Vasko79a7a872022-06-17 09:00:48 +02003278 } else {
3279 for (j = 0; exp->repeat[i][j]; ++j) {}
3280 /* the ending 0 as well */
3281 ++j;
Michal Vasko004d3152020-06-11 19:59:22 +02003282
Michal Vaskoe33134a2022-07-29 14:54:40 +02003283 dup->repeat[i - start_idx] = malloc(j * sizeof **dup->repeat);
3284 LY_CHECK_ERR_GOTO(!dup->repeat[i - start_idx], LOGMEM(ctx); ret = LY_EMEM, cleanup);
3285 memcpy(dup->repeat[i - start_idx], exp->repeat[i], j * sizeof **dup->repeat);
Michal Vasko79a7a872022-06-17 09:00:48 +02003286 }
Michal Vasko08e9b112021-06-11 15:41:17 +02003287 }
Michal Vasko004d3152020-06-11 19:59:22 +02003288 }
3289 }
3290
Michal Vaskoe33134a2022-07-29 14:54:40 +02003291 dup->used = used;
3292 dup->size = used;
3293
3294 /* copy only subexpression */
3295 LY_CHECK_GOTO(ret = lydict_insert(ctx, expr_start, expr_len, &dup->expr), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02003296
Michal Vasko1734be92020-09-22 08:55:10 +02003297cleanup:
3298 if (ret) {
3299 lyxp_expr_free(ctx, dup);
3300 } else {
3301 *dup_p = dup;
3302 }
3303 return ret;
Michal Vasko004d3152020-06-11 19:59:22 +02003304}
3305
Michal Vasko03ff5a72019-09-11 13:49:33 +02003306/**
3307 * @brief Get the last-added schema node that is currently in the context.
3308 *
3309 * @param[in] set Set to search in.
3310 * @return Last-added schema context node, NULL if no node is in context.
3311 */
3312static struct lysc_node *
3313warn_get_scnode_in_ctx(struct lyxp_set *set)
3314{
3315 uint32_t i;
3316
3317 if (!set || (set->type != LYXP_SET_SCNODE_SET)) {
3318 return NULL;
3319 }
3320
3321 i = set->used;
3322 do {
3323 --i;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003324 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003325 /* if there are more, simply return the first found (last added) */
3326 return set->val.scnodes[i].scnode;
3327 }
3328 } while (i);
3329
3330 return NULL;
3331}
3332
3333/**
3334 * @brief Test whether a type is numeric - integer type or decimal64.
3335 *
3336 * @param[in] type Type to test.
Radek Krejci857189e2020-09-01 13:26:36 +02003337 * @return Boolean value whether @p type is numeric type or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003338 */
Radek Krejci857189e2020-09-01 13:26:36 +02003339static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003340warn_is_numeric_type(struct lysc_type *type)
3341{
3342 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003343 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003344 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003345
3346 switch (type->basetype) {
3347 case LY_TYPE_DEC64:
3348 case LY_TYPE_INT8:
3349 case LY_TYPE_UINT8:
3350 case LY_TYPE_INT16:
3351 case LY_TYPE_UINT16:
3352 case LY_TYPE_INT32:
3353 case LY_TYPE_UINT32:
3354 case LY_TYPE_INT64:
3355 case LY_TYPE_UINT64:
3356 return 1;
3357 case LY_TYPE_UNION:
3358 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003359 LY_ARRAY_FOR(uni->types, u) {
3360 ret = warn_is_numeric_type(uni->types[u]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003361 if (ret) {
3362 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003363 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003364 }
3365 }
3366 /* did not find any suitable type */
3367 return 0;
3368 case LY_TYPE_LEAFREF:
3369 return warn_is_numeric_type(((struct lysc_type_leafref *)type)->realtype);
3370 default:
3371 return 0;
3372 }
3373}
3374
3375/**
3376 * @brief Test whether a type is string-like - no integers, decimal64 or binary.
3377 *
3378 * @param[in] type Type to test.
Radek Krejci857189e2020-09-01 13:26:36 +02003379 * @return Boolean value whether @p type's basetype is string type or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003380 */
Radek Krejci857189e2020-09-01 13:26:36 +02003381static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003382warn_is_string_type(struct lysc_type *type)
3383{
3384 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003385 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003386 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003387
3388 switch (type->basetype) {
3389 case LY_TYPE_BITS:
3390 case LY_TYPE_ENUM:
3391 case LY_TYPE_IDENT:
3392 case LY_TYPE_INST:
3393 case LY_TYPE_STRING:
3394 return 1;
3395 case LY_TYPE_UNION:
3396 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003397 LY_ARRAY_FOR(uni->types, u) {
3398 ret = warn_is_string_type(uni->types[u]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003399 if (ret) {
3400 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003401 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003402 }
3403 }
3404 /* did not find any suitable type */
3405 return 0;
3406 case LY_TYPE_LEAFREF:
3407 return warn_is_string_type(((struct lysc_type_leafref *)type)->realtype);
3408 default:
3409 return 0;
3410 }
3411}
3412
3413/**
3414 * @brief Test whether a type is one specific type.
3415 *
3416 * @param[in] type Type to test.
3417 * @param[in] base Expected type.
Radek Krejci857189e2020-09-01 13:26:36 +02003418 * @return Boolean value whether the given @p type is of the specific basetype @p base.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003419 */
Radek Krejci857189e2020-09-01 13:26:36 +02003420static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003421warn_is_specific_type(struct lysc_type *type, LY_DATA_TYPE base)
3422{
3423 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003424 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003425 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003426
3427 if (type->basetype == base) {
3428 return 1;
3429 } else if (type->basetype == LY_TYPE_UNION) {
3430 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003431 LY_ARRAY_FOR(uni->types, u) {
3432 ret = warn_is_specific_type(uni->types[u], base);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003433 if (ret) {
3434 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003435 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003436 }
3437 }
3438 /* did not find any suitable type */
3439 return 0;
3440 } else if (type->basetype == LY_TYPE_LEAFREF) {
3441 return warn_is_specific_type(((struct lysc_type_leafref *)type)->realtype, base);
3442 }
3443
3444 return 0;
3445}
3446
3447/**
3448 * @brief Get next type of a (union) type.
3449 *
3450 * @param[in] type Base type.
3451 * @param[in] prev_type Previously returned type.
3452 * @return Next type or NULL.
3453 */
3454static struct lysc_type *
3455warn_is_equal_type_next_type(struct lysc_type *type, struct lysc_type *prev_type)
3456{
3457 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003458 ly_bool found = 0;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003459 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003460
Michal Vasko4e55f5a2022-12-14 12:15:00 +01003461 if (type->basetype == LY_TYPE_UNION) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003462 uni = (struct lysc_type_union *)type;
3463 if (!prev_type) {
3464 return uni->types[0];
3465 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003466 LY_ARRAY_FOR(uni->types, u) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003467 if (found) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003468 return uni->types[u];
Michal Vasko03ff5a72019-09-11 13:49:33 +02003469 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003470 if (prev_type == uni->types[u]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003471 found = 1;
3472 }
3473 }
3474 return NULL;
Michal Vasko4e55f5a2022-12-14 12:15:00 +01003475 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003476 if (prev_type) {
3477 assert(type == prev_type);
3478 return NULL;
3479 } else {
3480 return type;
3481 }
3482 }
3483}
3484
3485/**
3486 * @brief Test whether 2 types have a common type.
3487 *
3488 * @param[in] type1 First type.
3489 * @param[in] type2 Second type.
3490 * @return 1 if they do, 0 otherwise.
3491 */
3492static int
3493warn_is_equal_type(struct lysc_type *type1, struct lysc_type *type2)
3494{
3495 struct lysc_type *t1, *rt1, *t2, *rt2;
3496
3497 t1 = NULL;
3498 while ((t1 = warn_is_equal_type_next_type(type1, t1))) {
3499 if (t1->basetype == LY_TYPE_LEAFREF) {
3500 rt1 = ((struct lysc_type_leafref *)t1)->realtype;
3501 } else {
3502 rt1 = t1;
3503 }
3504
3505 t2 = NULL;
3506 while ((t2 = warn_is_equal_type_next_type(type2, t2))) {
3507 if (t2->basetype == LY_TYPE_LEAFREF) {
3508 rt2 = ((struct lysc_type_leafref *)t2)->realtype;
3509 } else {
3510 rt2 = t2;
3511 }
3512
3513 if (rt2->basetype == rt1->basetype) {
3514 /* match found */
3515 return 1;
3516 }
3517 }
3518 }
3519
3520 return 0;
3521}
3522
3523/**
Michal Vaskoaa956522021-11-11 10:45:34 +01003524 * @brief Print warning with information about the XPath subexpression that caused previous warning.
3525 *
3526 * @param[in] ctx Context for logging.
3527 * @param[in] tok_pos Index of the subexpression in the whole expression.
3528 * @param[in] subexpr Subexpression start.
3529 * @param[in] subexpr_len Length of @p subexpr to print.
3530 * @param[in] cur_scnode Expression context node.
3531 */
3532static void
Michal Vaskodd528af2022-08-08 14:35:07 +02003533warn_subexpr_log(const struct ly_ctx *ctx, uint32_t tok_pos, const char *subexpr, int subexpr_len,
Michal Vaskoaa956522021-11-11 10:45:34 +01003534 const struct lysc_node *cur_scnode)
3535{
3536 char *path;
3537
3538 path = lysc_path(cur_scnode, LYSC_PATH_LOG, NULL, 0);
Michal Vaskodd528af2022-08-08 14:35:07 +02003539 LOGWRN(ctx, "Previous warning generated by XPath subexpression[%" PRIu32 "] \"%.*s\" with context node \"%s\".",
Michal Vaskoaa956522021-11-11 10:45:34 +01003540 tok_pos, subexpr_len, subexpr, path);
3541 free(path);
3542}
3543
3544/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02003545 * @brief Check both operands of comparison operators.
3546 *
3547 * @param[in] ctx Context for errors.
3548 * @param[in] set1 First operand set.
3549 * @param[in] set2 Second operand set.
3550 * @param[in] numbers_only Whether accept only numbers or other types are fine too (for '=' and '!=').
3551 * @param[in] expr Start of the expression to print with the warning.
3552 * @param[in] tok_pos Token position.
3553 */
3554static void
Michal Vaskoaa956522021-11-11 10:45:34 +01003555warn_operands(struct ly_ctx *ctx, struct lyxp_set *set1, struct lyxp_set *set2, ly_bool numbers_only, const char *expr,
Michal Vaskodd528af2022-08-08 14:35:07 +02003556 uint32_t tok_pos)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003557{
3558 struct lysc_node_leaf *node1, *node2;
Radek Krejci857189e2020-09-01 13:26:36 +02003559 ly_bool leaves = 1, warning = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003560
3561 node1 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set1);
3562 node2 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set2);
3563
3564 if (!node1 && !node2) {
3565 /* no node-sets involved, nothing to do */
3566 return;
3567 }
3568
3569 if (node1) {
3570 if (!(node1->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3571 LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node1->nodetype), node1->name);
3572 warning = 1;
3573 leaves = 0;
3574 } else if (numbers_only && !warn_is_numeric_type(node1->type)) {
3575 LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node1->name);
3576 warning = 1;
3577 }
3578 }
3579
3580 if (node2) {
3581 if (!(node2->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3582 LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node2->nodetype), node2->name);
3583 warning = 1;
3584 leaves = 0;
3585 } else if (numbers_only && !warn_is_numeric_type(node2->type)) {
3586 LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node2->name);
3587 warning = 1;
3588 }
3589 }
3590
3591 if (node1 && node2 && leaves && !numbers_only) {
Michal Vasko69730152020-10-09 16:30:07 +02003592 if ((warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type)) ||
3593 (!warn_is_numeric_type(node1->type) && warn_is_numeric_type(node2->type)) ||
3594 (!warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type) &&
3595 !warn_is_equal_type(node1->type, node2->type))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003596 LOGWRN(ctx, "Incompatible types of operands \"%s\" and \"%s\" for comparison.", node1->name, node2->name);
3597 warning = 1;
3598 }
3599 }
3600
3601 if (warning) {
Michal Vaskoaa956522021-11-11 10:45:34 +01003602 warn_subexpr_log(ctx, tok_pos, expr + tok_pos, 20, set1->cur_scnode);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003603 }
3604}
3605
3606/**
3607 * @brief Check that a value is valid for a leaf. If not applicable, does nothing.
3608 *
3609 * @param[in] exp Parsed XPath expression.
3610 * @param[in] set Set with the leaf/leaf-list.
3611 * @param[in] val_exp Index of the value (literal/number) in @p exp.
3612 * @param[in] equal_exp Index of the start of the equality expression in @p exp.
3613 * @param[in] last_equal_exp Index of the end of the equality expression in @p exp.
3614 */
3615static void
Michal Vaskodd528af2022-08-08 14:35:07 +02003616warn_equality_value(const struct lyxp_expr *exp, struct lyxp_set *set, uint32_t val_exp, uint32_t equal_exp,
3617 uint32_t last_equal_exp)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003618{
3619 struct lysc_node *scnode;
3620 struct lysc_type *type;
3621 char *value;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003622 struct lyd_value storage;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003623 LY_ERR rc;
3624 struct ly_err_item *err = NULL;
3625
Michal Vasko69730152020-10-09 16:30:07 +02003626 if ((scnode = warn_get_scnode_in_ctx(set)) && (scnode->nodetype & (LYS_LEAF | LYS_LEAFLIST)) &&
3627 ((exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) || (exp->tokens[val_exp] == LYXP_TOKEN_NUMBER))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003628 /* check that the node can have the specified value */
3629 if (exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) {
3630 value = strndup(exp->expr + exp->tok_pos[val_exp] + 1, exp->tok_len[val_exp] - 2);
3631 } else {
3632 value = strndup(exp->expr + exp->tok_pos[val_exp], exp->tok_len[val_exp]);
3633 }
3634 if (!value) {
3635 LOGMEM(set->ctx);
3636 return;
3637 }
3638
3639 if ((((struct lysc_node_leaf *)scnode)->type->basetype == LY_TYPE_IDENT) && !strchr(value, ':')) {
3640 LOGWRN(set->ctx, "Identityref \"%s\" comparison with identity \"%s\" without prefix, consider adding"
Michal Vasko69730152020-10-09 16:30:07 +02003641 " a prefix or best using \"derived-from(-or-self)()\" functions.", scnode->name, value);
Michal Vaskoaa956522021-11-11 10:45:34 +01003642 warn_subexpr_log(set->ctx, exp->tok_pos[equal_exp], exp->expr + exp->tok_pos[equal_exp],
Radek Krejci0f969882020-08-21 16:56:47 +02003643 (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp],
Michal Vaskoaa956522021-11-11 10:45:34 +01003644 set->cur_scnode);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003645 }
3646
3647 type = ((struct lysc_node_leaf *)scnode)->type;
3648 if (type->basetype != LY_TYPE_IDENT) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003649 rc = type->plugin->store(set->ctx, type, value, strlen(value), 0, set->format, set->prefix_data,
Michal Vasko405cc9e2020-12-01 12:01:27 +01003650 LYD_HINT_DATA, scnode, &storage, NULL, &err);
Michal Vaskobf42e832020-11-23 16:59:42 +01003651 if (rc == LY_EINCOMPLETE) {
3652 rc = LY_SUCCESS;
3653 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003654
3655 if (err) {
3656 LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type (%s).", value, err->msg);
3657 ly_err_free(err);
3658 } else if (rc != LY_SUCCESS) {
3659 LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type.", value);
3660 }
3661 if (rc != LY_SUCCESS) {
Michal Vaskoaa956522021-11-11 10:45:34 +01003662 warn_subexpr_log(set->ctx, exp->tok_pos[equal_exp], exp->expr + exp->tok_pos[equal_exp],
Radek Krejci0f969882020-08-21 16:56:47 +02003663 (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp],
Michal Vaskoaa956522021-11-11 10:45:34 +01003664 set->cur_scnode);
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003665 } else {
3666 type->plugin->free(set->ctx, &storage);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003667 }
3668 }
3669 free(value);
3670 }
3671}
3672
3673/*
3674 * XPath functions
3675 */
3676
3677/**
3678 * @brief Execute the YANG 1.1 bit-is-set(node-set, string) function. Returns LYXP_SET_BOOLEAN
3679 * depending on whether the first node bit value from the second argument is set.
3680 *
3681 * @param[in] args Array of arguments.
3682 * @param[in] arg_count Count of elements in @p args.
3683 * @param[in,out] set Context and result set at the same time.
3684 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003685 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003686 */
3687static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02003688xpath_bit_is_set(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003689{
3690 struct lyd_node_term *leaf;
3691 struct lysc_node_leaf *sleaf;
Michal Vasko2588b952021-07-29 07:43:26 +02003692 struct lyd_value_bits *bits;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003693 LY_ERR rc = LY_SUCCESS;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003694 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003695
3696 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003697 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003698 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003699 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3700 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3701 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3702 sleaf->name);
3703 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_BITS)) {
3704 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"bits\".", __func__, sleaf->name);
3705 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003706 }
3707
3708 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3709 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003710 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3711 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003712 } else if (!warn_is_string_type(sleaf->type)) {
3713 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003714 }
3715 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003716 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003717 return rc;
3718 }
3719
Michal Vaskod3678892020-05-21 10:06:58 +02003720 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003721 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "bit-is-set(node-set, string)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003722 return LY_EVALID;
3723 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003724 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003725 LY_CHECK_RET(rc);
3726
3727 set_fill_boolean(set, 0);
Michal Vaskod3678892020-05-21 10:06:58 +02003728 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003729 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
Michal Vasko2588b952021-07-29 07:43:26 +02003730 if ((leaf->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (leaf->value.realtype->basetype == LY_TYPE_BITS)) {
3731 LYD_VALUE_GET(&leaf->value, bits);
3732 LY_ARRAY_FOR(bits->items, u) {
3733 if (!strcmp(bits->items[u]->name, args[1]->val.str)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003734 set_fill_boolean(set, 1);
3735 break;
3736 }
3737 }
3738 }
3739 }
3740
3741 return LY_SUCCESS;
3742}
3743
3744/**
3745 * @brief Execute the XPath boolean(object) function. Returns LYXP_SET_BOOLEAN
3746 * with the argument converted to boolean.
3747 *
3748 * @param[in] args Array of arguments.
3749 * @param[in] arg_count Count of elements in @p args.
3750 * @param[in,out] set Context and result set at the same time.
3751 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003752 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003753 */
3754static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02003755xpath_boolean(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003756{
3757 LY_ERR rc;
3758
3759 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02003760 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003761 return LY_SUCCESS;
3762 }
3763
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003764 rc = lyxp_set_cast(args[0], LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003765 LY_CHECK_RET(rc);
3766 set_fill_set(set, args[0]);
3767
3768 return LY_SUCCESS;
3769}
3770
3771/**
3772 * @brief Execute the XPath ceiling(number) function. Returns LYXP_SET_NUMBER
3773 * with the first argument rounded up to the nearest integer.
3774 *
3775 * @param[in] args Array of arguments.
3776 * @param[in] arg_count Count of elements in @p args.
3777 * @param[in,out] set Context and result set at the same time.
3778 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003779 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003780 */
3781static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02003782xpath_ceiling(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003783{
3784 struct lysc_node_leaf *sleaf;
3785 LY_ERR rc = LY_SUCCESS;
3786
3787 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003788 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003789 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003790 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3791 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3792 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3793 sleaf->name);
3794 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
3795 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
3796 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003797 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003798 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003799 return rc;
3800 }
3801
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003802 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003803 LY_CHECK_RET(rc);
3804 if ((long long)args[0]->val.num != args[0]->val.num) {
3805 set_fill_number(set, ((long long)args[0]->val.num) + 1);
3806 } else {
3807 set_fill_number(set, args[0]->val.num);
3808 }
3809
3810 return LY_SUCCESS;
3811}
3812
3813/**
3814 * @brief Execute the XPath concat(string, string, string*) function.
3815 * Returns LYXP_SET_STRING with the concatenation of all the arguments.
3816 *
3817 * @param[in] args Array of arguments.
3818 * @param[in] arg_count Count of elements in @p args.
3819 * @param[in,out] set Context and result set at the same time.
3820 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003821 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003822 */
3823static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02003824xpath_concat(struct lyxp_set **args, uint32_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003825{
Michal Vaskodd528af2022-08-08 14:35:07 +02003826 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003827 char *str = NULL;
3828 size_t used = 1;
3829 LY_ERR rc = LY_SUCCESS;
3830 struct lysc_node_leaf *sleaf;
3831
3832 if (options & LYXP_SCNODE_ALL) {
3833 for (i = 0; i < arg_count; ++i) {
3834 if ((args[i]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[i]))) {
3835 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3836 LOGWRN(set->ctx, "Argument #%u of %s is a %s node \"%s\".",
Michal Vasko69730152020-10-09 16:30:07 +02003837 i + 1, __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003838 } else if (!warn_is_string_type(sleaf->type)) {
Radek Krejci70124c82020-08-14 22:17:03 +02003839 LOGWRN(set->ctx, "Argument #%u of %s is node \"%s\", not of string-type.", i + 1, __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003840 }
3841 }
3842 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003843 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003844 return rc;
3845 }
3846
3847 for (i = 0; i < arg_count; ++i) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003848 rc = lyxp_set_cast(args[i], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003849 if (rc != LY_SUCCESS) {
3850 free(str);
3851 return rc;
3852 }
3853
3854 str = ly_realloc(str, (used + strlen(args[i]->val.str)) * sizeof(char));
3855 LY_CHECK_ERR_RET(!str, LOGMEM(set->ctx), LY_EMEM);
3856 strcpy(str + used - 1, args[i]->val.str);
3857 used += strlen(args[i]->val.str);
3858 }
3859
3860 /* free, kind of */
Michal Vaskod3678892020-05-21 10:06:58 +02003861 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003862 set->type = LYXP_SET_STRING;
3863 set->val.str = str;
3864
3865 return LY_SUCCESS;
3866}
3867
3868/**
3869 * @brief Execute the XPath contains(string, string) function.
3870 * Returns LYXP_SET_BOOLEAN whether the second argument can
3871 * be found in the first or not.
3872 *
3873 * @param[in] args Array of arguments.
3874 * @param[in] arg_count Count of elements in @p args.
3875 * @param[in,out] set Context and result set at the same time.
3876 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003877 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003878 */
3879static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02003880xpath_contains(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003881{
3882 struct lysc_node_leaf *sleaf;
3883 LY_ERR rc = LY_SUCCESS;
3884
3885 if (options & LYXP_SCNODE_ALL) {
3886 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3887 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003888 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3889 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003890 } else if (!warn_is_string_type(sleaf->type)) {
3891 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003892 }
3893 }
3894
3895 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3896 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003897 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3898 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003899 } else if (!warn_is_string_type(sleaf->type)) {
3900 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003901 }
3902 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003903 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003904 return rc;
3905 }
3906
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003907 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003908 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003909 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003910 LY_CHECK_RET(rc);
3911
3912 if (strstr(args[0]->val.str, args[1]->val.str)) {
3913 set_fill_boolean(set, 1);
3914 } else {
3915 set_fill_boolean(set, 0);
3916 }
3917
3918 return LY_SUCCESS;
3919}
3920
3921/**
3922 * @brief Execute the XPath count(node-set) function. Returns LYXP_SET_NUMBER
3923 * with the size of the node-set from the argument.
3924 *
3925 * @param[in] args Array of arguments.
3926 * @param[in] arg_count Count of elements in @p args.
3927 * @param[in,out] set Context and result set at the same time.
3928 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003929 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003930 */
3931static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02003932xpath_count(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003933{
Michal Vasko03ff5a72019-09-11 13:49:33 +02003934 LY_ERR rc = LY_SUCCESS;
3935
3936 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003937 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003938 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003939 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003940 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003941 return rc;
3942 }
3943
Michal Vasko03ff5a72019-09-11 13:49:33 +02003944 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003945 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "count(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003946 return LY_EVALID;
3947 }
3948
3949 set_fill_number(set, args[0]->used);
3950 return LY_SUCCESS;
3951}
3952
3953/**
3954 * @brief Execute the XPath current() function. Returns LYXP_SET_NODE_SET
3955 * with the context with the intial node.
3956 *
3957 * @param[in] args Array of arguments.
3958 * @param[in] arg_count Count of elements in @p args.
3959 * @param[in,out] set Context and result set at the same time.
3960 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003961 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003962 */
3963static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02003964xpath_current(struct lyxp_set **args, uint32_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003965{
3966 if (arg_count || args) {
Radek Krejcie87c7dc2021-06-02 21:25:42 +02003967 LOGVAL(set->ctx, LY_VCODE_XP_INARGCOUNT, arg_count, LY_PRI_LENSTR("current()"));
Michal Vasko03ff5a72019-09-11 13:49:33 +02003968 return LY_EVALID;
3969 }
3970
3971 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02003972 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003973
Michal Vasko296dfaf2021-12-13 16:57:42 +01003974 if (set->cur_scnode) {
Michal Vaskoe4a6d012023-05-22 14:34:52 +02003975 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, set->cur_scnode, LYXP_NODE_ELEM, LYXP_AXIS_SELF, NULL));
Michal Vasko296dfaf2021-12-13 16:57:42 +01003976 } else {
3977 /* root node */
Michal Vaskoe4a6d012023-05-22 14:34:52 +02003978 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, NULL, set->root_type, LYXP_AXIS_SELF, NULL));
Michal Vasko296dfaf2021-12-13 16:57:42 +01003979 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003980 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02003981 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003982
Michal Vasko296dfaf2021-12-13 16:57:42 +01003983 if (set->cur_node) {
3984 /* position is filled later */
3985 set_insert_node(set, set->cur_node, 0, LYXP_NODE_ELEM, 0);
3986 } else {
3987 /* root node */
3988 set_insert_node(set, NULL, 0, set->root_type, 0);
3989 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003990 }
3991
3992 return LY_SUCCESS;
3993}
3994
3995/**
3996 * @brief Execute the YANG 1.1 deref(node-set) function. Returns LYXP_SET_NODE_SET with either
3997 * leafref or instance-identifier target node(s).
3998 *
3999 * @param[in] args Array of arguments.
4000 * @param[in] arg_count Count of elements in @p args.
4001 * @param[in,out] set Context and result set at the same time.
4002 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004003 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004004 */
4005static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004006xpath_deref(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004007{
4008 struct lyd_node_term *leaf;
Michal Vasko42e497c2020-01-06 08:38:25 +01004009 struct lysc_node_leaf *sleaf = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02004010 struct lysc_type_leafref *lref;
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02004011 const struct lysc_node *target;
Michal Vasko004d3152020-06-11 19:59:22 +02004012 struct ly_path *p;
4013 struct lyd_node *node;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004014 char *errmsg = NULL;
Michal Vasko00cbf532020-06-15 13:58:47 +02004015 uint8_t oper;
Michal Vasko741bb562021-06-24 11:59:50 +02004016 LY_ERR r;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004017
4018 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02004019 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004020 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02004021 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
Michal Vasko423ba3f2021-07-19 13:08:50 +02004022 if (!(sleaf->nodetype & LYD_NODE_TERM)) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02004023 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
4024 sleaf->name);
Michal Vaskoed725d72021-06-23 12:03:45 +02004025 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_LEAFREF) &&
4026 !warn_is_specific_type(sleaf->type, LY_TYPE_INST)) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02004027 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"leafref\" nor \"instance-identifier\".",
4028 __func__, sleaf->name);
4029 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004030 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004031 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko423ba3f2021-07-19 13:08:50 +02004032 if (sleaf && (sleaf->nodetype & LYD_NODE_TERM) && (sleaf->type->basetype == LY_TYPE_LEAFREF)) {
Michal Vasko004d3152020-06-11 19:59:22 +02004033 lref = (struct lysc_type_leafref *)sleaf->type;
Michal Vaskod1e53b92021-01-28 13:11:06 +01004034 oper = (sleaf->flags & LYS_IS_OUTPUT) ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT;
Michal Vasko004d3152020-06-11 19:59:22 +02004035
4036 /* it was already evaluated on schema, it must succeed */
Michal Vasko741bb562021-06-24 11:59:50 +02004037 r = ly_path_compile_leafref(set->ctx, &sleaf->node, NULL, lref->path, oper, LY_PATH_TARGET_MANY,
Michal Vasko24fc4d12021-07-12 14:41:20 +02004038 LY_VALUE_SCHEMA_RESOLVED, lref->prefixes, &p);
Michal Vasko741bb562021-06-24 11:59:50 +02004039 if (!r) {
4040 /* get the target node */
4041 target = p[LY_ARRAY_COUNT(p) - 1].node;
4042 ly_path_free(set->ctx, p);
Michal Vasko004d3152020-06-11 19:59:22 +02004043
Michal Vaskoe4a6d012023-05-22 14:34:52 +02004044 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, target, LYXP_NODE_ELEM, LYXP_AXIS_SELF, NULL));
Michal Vasko741bb562021-06-24 11:59:50 +02004045 } /* else the target was found before but is disabled so it was removed */
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02004046 }
4047
Michal Vasko741bb562021-06-24 11:59:50 +02004048 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004049 }
4050
Michal Vaskod3678892020-05-21 10:06:58 +02004051 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004052 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "deref(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004053 return LY_EVALID;
4054 }
4055
Michal Vaskod3678892020-05-21 10:06:58 +02004056 lyxp_set_free_content(set);
4057 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004058 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
4059 sleaf = (struct lysc_node_leaf *)leaf->schema;
4060 if (sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
4061 if (sleaf->type->basetype == LY_TYPE_LEAFREF) {
4062 /* find leafref target */
Radek Krejci0b013302021-03-29 15:22:32 +02004063 if (lyplg_type_resolve_leafref((struct lysc_type_leafref *)sleaf->type, &leaf->node, &leaf->value, set->tree,
Michal Vasko9e685082021-01-29 14:49:09 +01004064 &node, &errmsg)) {
Michal Vasko1a2b8ee2022-12-05 10:41:55 +01004065 LOGERR(set->ctx, LY_EVALID, "%s", errmsg);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004066 free(errmsg);
Michal Vasko004d3152020-06-11 19:59:22 +02004067 return LY_EVALID;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004068 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004069 } else {
4070 assert(sleaf->type->basetype == LY_TYPE_INST);
Michal Vasko90189962023-02-28 12:10:34 +01004071 if (ly_path_eval(leaf->value.target, set->tree, NULL, &node)) {
Michal Vaskoba99a3e2020-08-18 15:50:05 +02004072 LOGERR(set->ctx, LY_EVALID, "Invalid instance-identifier \"%s\" value - required instance not found.",
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02004073 lyd_get_value(&leaf->node));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004074 return LY_EVALID;
4075 }
4076 }
Michal Vasko004d3152020-06-11 19:59:22 +02004077
4078 /* insert it */
4079 set_insert_node(set, node, 0, LYXP_NODE_ELEM, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004080 }
4081 }
4082
4083 return LY_SUCCESS;
4084}
4085
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004086static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02004087xpath_derived_(struct lyxp_set **args, struct lyxp_set *set, uint32_t options, ly_bool self_match, const char *func)
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004088{
Michal Vaskofe1af042022-07-29 14:58:59 +02004089 uint32_t i, id_len;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004090 LY_ARRAY_COUNT_TYPE u;
4091 struct lyd_node_term *leaf;
4092 struct lysc_node_leaf *sleaf;
4093 struct lyd_meta *meta;
Michal Vasko93923692021-05-07 15:28:02 +02004094 struct lyd_value *val;
4095 const struct lys_module *mod;
4096 const char *id_name;
Michal Vasko93923692021-05-07 15:28:02 +02004097 struct lysc_ident *id;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004098 LY_ERR rc = LY_SUCCESS;
Radek Krejci857189e2020-09-01 13:26:36 +02004099 ly_bool found;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004100
4101 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02004102 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004103 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", func);
Michal Vasko5676f4e2021-04-06 17:14:45 +02004104 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4105 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4106 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype),
4107 sleaf->name);
4108 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_IDENT)) {
4109 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"identityref\".", func, sleaf->name);
4110 }
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004111 }
4112
4113 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4114 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4115 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype),
Michal Vasko69730152020-10-09 16:30:07 +02004116 sleaf->name);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004117 } else if (!warn_is_string_type(sleaf->type)) {
4118 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", func, sleaf->name);
4119 }
4120 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004121 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004122 return rc;
4123 }
4124
4125 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004126 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "derived-from(-or-self)(node-set, string)");
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004127 return LY_EVALID;
4128 }
4129 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
4130 LY_CHECK_RET(rc);
4131
Michal Vasko93923692021-05-07 15:28:02 +02004132 /* parse the identity */
4133 id_name = args[1]->val.str;
4134 id_len = strlen(id_name);
4135 rc = moveto_resolve_model(&id_name, &id_len, set, set->cur_node ? set->cur_node->schema : NULL, &mod);
4136 LY_CHECK_RET(rc);
4137 if (!mod) {
4138 LOGVAL(set->ctx, LYVE_XPATH, "Identity \"%.*s\" without a prefix.", (int)id_len, id_name);
4139 return LY_EVALID;
4140 }
4141
4142 /* find the identity */
4143 found = 0;
4144 LY_ARRAY_FOR(mod->identities, u) {
4145 if (!ly_strncmp(mod->identities[u].name, id_name, id_len)) {
4146 /* we have match */
4147 found = 1;
4148 break;
4149 }
4150 }
4151 if (!found) {
4152 LOGVAL(set->ctx, LYVE_XPATH, "Identity \"%.*s\" not found in module \"%s\".", (int)id_len, id_name, mod->name);
4153 return LY_EVALID;
4154 }
4155 id = &mod->identities[u];
4156
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004157 set_fill_boolean(set, 0);
4158 found = 0;
4159 for (i = 0; i < args[0]->used; ++i) {
4160 if ((args[0]->val.nodes[i].type != LYXP_NODE_ELEM) && (args[0]->val.nodes[i].type != LYXP_NODE_META)) {
4161 continue;
4162 }
4163
4164 if (args[0]->val.nodes[i].type == LYXP_NODE_ELEM) {
4165 leaf = (struct lyd_node_term *)args[0]->val.nodes[i].node;
4166 sleaf = (struct lysc_node_leaf *)leaf->schema;
4167 val = &leaf->value;
4168 if (!(sleaf->nodetype & LYD_NODE_TERM) || (leaf->value.realtype->basetype != LY_TYPE_IDENT)) {
4169 /* uninteresting */
4170 continue;
4171 }
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004172 } else {
4173 meta = args[0]->val.meta[i].meta;
4174 val = &meta->value;
4175 if (val->realtype->basetype != LY_TYPE_IDENT) {
4176 /* uninteresting */
4177 continue;
4178 }
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004179 }
4180
Michal Vasko93923692021-05-07 15:28:02 +02004181 /* check the identity itself */
4182 if (self_match && (id == val->ident)) {
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004183 set_fill_boolean(set, 1);
4184 found = 1;
4185 }
Michal Vasko93923692021-05-07 15:28:02 +02004186 if (!found && !lyplg_type_identity_isderived(id, val->ident)) {
4187 set_fill_boolean(set, 1);
4188 found = 1;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004189 }
4190
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004191 if (found) {
4192 break;
4193 }
4194 }
4195
4196 return LY_SUCCESS;
4197}
4198
Michal Vasko03ff5a72019-09-11 13:49:33 +02004199/**
4200 * @brief Execute the YANG 1.1 derived-from(node-set, string) function. Returns LYXP_SET_BOOLEAN depending
4201 * on whether the first argument nodes contain a node of an identity derived from the second
4202 * argument identity.
4203 *
4204 * @param[in] args Array of arguments.
4205 * @param[in] arg_count Count of elements in @p args.
4206 * @param[in,out] set Context and result set at the same time.
4207 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004208 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004209 */
4210static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004211xpath_derived_from(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004212{
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004213 return xpath_derived_(args, set, options, 0, __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004214}
4215
4216/**
4217 * @brief Execute the YANG 1.1 derived-from-or-self(node-set, string) function. Returns LYXP_SET_BOOLEAN depending
4218 * on whether the first argument nodes contain a node of an identity that either is or is derived from
4219 * the second argument identity.
4220 *
4221 * @param[in] args Array of arguments.
4222 * @param[in] arg_count Count of elements in @p args.
4223 * @param[in,out] set Context and result set at the same time.
4224 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004225 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004226 */
4227static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004228xpath_derived_from_or_self(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004229{
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004230 return xpath_derived_(args, set, options, 1, __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004231}
4232
4233/**
4234 * @brief Execute the YANG 1.1 enum-value(node-set) function. Returns LYXP_SET_NUMBER
4235 * with the integer value of the first node's enum value, otherwise NaN.
4236 *
4237 * @param[in] args Array of arguments.
4238 * @param[in] arg_count Count of elements in @p args.
4239 * @param[in,out] set Context and result set at the same time.
4240 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004241 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004242 */
4243static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004244xpath_enum_value(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004245{
4246 struct lyd_node_term *leaf;
4247 struct lysc_node_leaf *sleaf;
4248 LY_ERR rc = LY_SUCCESS;
4249
4250 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02004251 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004252 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02004253 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4254 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4255 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
4256 sleaf->name);
4257 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_ENUM)) {
4258 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"enumeration\".", __func__, sleaf->name);
4259 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004260 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004261 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004262 return rc;
4263 }
4264
Michal Vaskod3678892020-05-21 10:06:58 +02004265 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004266 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "enum-value(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004267 return LY_EVALID;
4268 }
4269
4270 set_fill_number(set, NAN);
Michal Vaskod3678892020-05-21 10:06:58 +02004271 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004272 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
4273 sleaf = (struct lysc_node_leaf *)leaf->schema;
4274 if ((sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (sleaf->type->basetype == LY_TYPE_ENUM)) {
4275 set_fill_number(set, leaf->value.enum_item->value);
4276 }
4277 }
4278
4279 return LY_SUCCESS;
4280}
4281
4282/**
4283 * @brief Execute the XPath false() function. Returns LYXP_SET_BOOLEAN
4284 * with false value.
4285 *
4286 * @param[in] args Array of arguments.
4287 * @param[in] arg_count Count of elements in @p args.
4288 * @param[in,out] set Context and result set at the same time.
4289 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004290 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004291 */
4292static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004293xpath_false(struct lyxp_set **UNUSED(args), uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004294{
4295 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004296 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004297 return LY_SUCCESS;
4298 }
4299
4300 set_fill_boolean(set, 0);
4301 return LY_SUCCESS;
4302}
4303
4304/**
4305 * @brief Execute the XPath floor(number) function. Returns LYXP_SET_NUMBER
4306 * with the first argument floored (truncated).
4307 *
4308 * @param[in] args Array of arguments.
4309 * @param[in] arg_count Count of elements in @p args.
4310 * @param[in,out] set Context and result set at the same time.
4311 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004312 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004313 */
4314static LY_ERR
Michal Vasko0ff8d632023-04-06 12:31:17 +02004315xpath_floor(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004316{
Michal Vasko0ff8d632023-04-06 12:31:17 +02004317 struct lysc_node_leaf *sleaf;
4318 LY_ERR rc = LY_SUCCESS;
4319
4320 if (options & LYXP_SCNODE_ALL) {
4321 if (args[0]->type != LYXP_SET_SCNODE_SET) {
4322 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
4323 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4324 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4325 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
4326 sleaf->name);
4327 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
4328 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
4329 }
4330 }
4331 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
4332 return rc;
4333 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004334
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004335 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004336 LY_CHECK_RET(rc);
4337 if (isfinite(args[0]->val.num)) {
4338 set_fill_number(set, (long long)args[0]->val.num);
4339 }
4340
4341 return LY_SUCCESS;
4342}
4343
4344/**
4345 * @brief Execute the XPath lang(string) function. Returns LYXP_SET_BOOLEAN
4346 * whether the language of the text matches the one from the argument.
4347 *
4348 * @param[in] args Array of arguments.
4349 * @param[in] arg_count Count of elements in @p args.
4350 * @param[in,out] set Context and result set at the same time.
4351 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004352 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004353 */
4354static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004355xpath_lang(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004356{
4357 const struct lyd_node *node;
4358 struct lysc_node_leaf *sleaf;
Michal Vasko9f96a052020-03-10 09:41:45 +01004359 struct lyd_meta *meta = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004360 const char *val;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004361 LY_ERR rc = LY_SUCCESS;
4362
4363 if (options & LYXP_SCNODE_ALL) {
4364 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4365 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004366 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
4367 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004368 } else if (!warn_is_string_type(sleaf->type)) {
4369 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004370 }
4371 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004372 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004373 return rc;
4374 }
4375
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004376 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004377 LY_CHECK_RET(rc);
4378
Michal Vasko03ff5a72019-09-11 13:49:33 +02004379 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004380 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "lang(string)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004381 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004382 } else if (!set->used) {
4383 set_fill_boolean(set, 0);
4384 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004385 }
4386
4387 switch (set->val.nodes[0].type) {
4388 case LYXP_NODE_ELEM:
4389 case LYXP_NODE_TEXT:
4390 node = set->val.nodes[0].node;
4391 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004392 case LYXP_NODE_META:
4393 node = set->val.meta[0].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004394 break;
4395 default:
4396 /* nothing to do with roots */
4397 set_fill_boolean(set, 0);
4398 return LY_SUCCESS;
4399 }
4400
Michal Vasko9f96a052020-03-10 09:41:45 +01004401 /* find lang metadata */
Michal Vasko9e685082021-01-29 14:49:09 +01004402 for ( ; node; node = lyd_parent(node)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01004403 for (meta = node->meta; meta; meta = meta->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004404 /* annotations */
Michal Vasko9f96a052020-03-10 09:41:45 +01004405 if (meta->name && !strcmp(meta->name, "lang") && !strcmp(meta->annotation->module->name, "xml")) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004406 break;
4407 }
4408 }
4409
Michal Vasko9f96a052020-03-10 09:41:45 +01004410 if (meta) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004411 break;
4412 }
4413 }
4414
4415 /* compare languages */
Michal Vasko9f96a052020-03-10 09:41:45 +01004416 if (!meta) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004417 set_fill_boolean(set, 0);
4418 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004419 uint64_t i;
4420
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02004421 val = lyd_get_meta_value(meta);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004422 for (i = 0; args[0]->val.str[i]; ++i) {
4423 if (tolower(args[0]->val.str[i]) != tolower(val[i])) {
4424 set_fill_boolean(set, 0);
4425 break;
4426 }
4427 }
4428 if (!args[0]->val.str[i]) {
4429 if (!val[i] || (val[i] == '-')) {
4430 set_fill_boolean(set, 1);
4431 } else {
4432 set_fill_boolean(set, 0);
4433 }
4434 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004435 }
4436
4437 return LY_SUCCESS;
4438}
4439
4440/**
4441 * @brief Execute the XPath last() function. Returns LYXP_SET_NUMBER
4442 * with the context size.
4443 *
4444 * @param[in] args Array of arguments.
4445 * @param[in] arg_count Count of elements in @p args.
4446 * @param[in,out] set Context and result set at the same time.
4447 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004448 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004449 */
4450static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004451xpath_last(struct lyxp_set **UNUSED(args), uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004452{
4453 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004454 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004455 return LY_SUCCESS;
4456 }
4457
Michal Vasko03ff5a72019-09-11 13:49:33 +02004458 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004459 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "last()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004460 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004461 } else if (!set->used) {
4462 set_fill_number(set, 0);
4463 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004464 }
4465
4466 set_fill_number(set, set->ctx_size);
4467 return LY_SUCCESS;
4468}
4469
4470/**
4471 * @brief Execute the XPath local-name(node-set?) function. Returns LYXP_SET_STRING
4472 * with the node name without namespace from the argument or the context.
4473 *
4474 * @param[in] args Array of arguments.
4475 * @param[in] arg_count Count of elements in @p args.
4476 * @param[in,out] set Context and result set at the same time.
4477 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004478 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004479 */
4480static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004481xpath_local_name(struct lyxp_set **args, uint32_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004482{
4483 struct lyxp_set_node *item;
Michal Vasko69730152020-10-09 16:30:07 +02004484
Michal Vasko03ff5a72019-09-11 13:49:33 +02004485 /* suppress unused variable warning */
4486 (void)options;
4487
4488 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004489 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004490 return LY_SUCCESS;
4491 }
4492
4493 if (arg_count) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004494 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004495 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004496 "local-name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004497 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004498 } else if (!args[0]->used) {
4499 set_fill_string(set, "", 0);
4500 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004501 }
4502
4503 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004504 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004505
4506 item = &args[0]->val.nodes[0];
4507 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004508 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004509 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "local-name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004510 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004511 } else if (!set->used) {
4512 set_fill_string(set, "", 0);
4513 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004514 }
4515
4516 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004517 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004518
4519 item = &set->val.nodes[0];
4520 }
4521
4522 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004523 case LYXP_NODE_NONE:
4524 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004525 case LYXP_NODE_ROOT:
4526 case LYXP_NODE_ROOT_CONFIG:
4527 case LYXP_NODE_TEXT:
4528 set_fill_string(set, "", 0);
4529 break;
4530 case LYXP_NODE_ELEM:
Michal Vasko222be682023-08-24 08:17:12 +02004531 set_fill_string(set, LYD_NAME(item->node), strlen(LYD_NAME(item->node)));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004532 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004533 case LYXP_NODE_META:
4534 set_fill_string(set, ((struct lyd_meta *)item->node)->name, strlen(((struct lyd_meta *)item->node)->name));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004535 break;
4536 }
4537
4538 return LY_SUCCESS;
4539}
4540
4541/**
4542 * @brief Execute the XPath name(node-set?) function. Returns LYXP_SET_STRING
4543 * with the node name fully qualified (with namespace) from the argument or the context.
4544 *
4545 * @param[in] args Array of arguments.
4546 * @param[in] arg_count Count of elements in @p args.
4547 * @param[in,out] set Context and result set at the same time.
4548 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004549 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004550 */
4551static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004552xpath_name(struct lyxp_set **args, uint32_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004553{
4554 struct lyxp_set_node *item;
Michal Vasko420cc252023-08-24 08:14:24 +02004555 const struct lys_module *mod = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004556 char *str;
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004557 const char *name = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004558
4559 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004560 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004561 return LY_SUCCESS;
4562 }
4563
4564 if (arg_count) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004565 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004566 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004567 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004568 } else if (!args[0]->used) {
4569 set_fill_string(set, "", 0);
4570 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004571 }
4572
4573 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004574 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004575
4576 item = &args[0]->val.nodes[0];
4577 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004578 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004579 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004580 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004581 } else if (!set->used) {
4582 set_fill_string(set, "", 0);
4583 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004584 }
4585
4586 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004587 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004588
4589 item = &set->val.nodes[0];
4590 }
4591
4592 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004593 case LYXP_NODE_NONE:
4594 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004595 case LYXP_NODE_ROOT:
4596 case LYXP_NODE_ROOT_CONFIG:
4597 case LYXP_NODE_TEXT:
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004598 /* keep NULL */
Michal Vasko03ff5a72019-09-11 13:49:33 +02004599 break;
4600 case LYXP_NODE_ELEM:
Michal Vasko420cc252023-08-24 08:14:24 +02004601 mod = lyd_node_module(item->node);
4602 name = LYD_NAME(item->node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004603 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004604 case LYXP_NODE_META:
4605 mod = ((struct lyd_meta *)item->node)->annotation->module;
4606 name = ((struct lyd_meta *)item->node)->name;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004607 break;
4608 }
4609
4610 if (mod && name) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004611 int rc = asprintf(&str, "%s:%s", ly_get_prefix(mod, set->format, set->prefix_data), name);
Michal Vasko26bbb272022-08-02 14:54:33 +02004612
Michal Vasko03ff5a72019-09-11 13:49:33 +02004613 LY_CHECK_ERR_RET(rc == -1, LOGMEM(set->ctx), LY_EMEM);
4614 set_fill_string(set, str, strlen(str));
4615 free(str);
4616 } else {
4617 set_fill_string(set, "", 0);
4618 }
4619
4620 return LY_SUCCESS;
4621}
4622
4623/**
4624 * @brief Execute the XPath namespace-uri(node-set?) function. Returns LYXP_SET_STRING
4625 * with the namespace of the node from the argument or the context.
4626 *
4627 * @param[in] args Array of arguments.
4628 * @param[in] arg_count Count of elements in @p args.
4629 * @param[in,out] set Context and result set at the same time.
4630 * @param[in] options XPath options.
4631 * @return LY_ERR (LY_EINVAL for wrong arguments on schema)
4632 */
4633static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004634xpath_namespace_uri(struct lyxp_set **args, uint32_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004635{
4636 struct lyxp_set_node *item;
Michal Vasko420cc252023-08-24 08:14:24 +02004637 const struct lys_module *mod;
Michal Vasko69730152020-10-09 16:30:07 +02004638
Michal Vasko03ff5a72019-09-11 13:49:33 +02004639 /* suppress unused variable warning */
4640 (void)options;
4641
4642 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004643 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004644 return LY_SUCCESS;
4645 }
4646
4647 if (arg_count) {
Michal Vaskod3678892020-05-21 10:06:58 +02004648 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004649 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
Michal Vasko69730152020-10-09 16:30:07 +02004650 "namespace-uri(node-set?)");
Michal Vaskod3678892020-05-21 10:06:58 +02004651 return LY_EVALID;
4652 } else if (!args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004653 set_fill_string(set, "", 0);
4654 return LY_SUCCESS;
4655 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004656
4657 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004658 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004659
4660 item = &args[0]->val.nodes[0];
4661 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004662 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004663 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "namespace-uri(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004664 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004665 } else if (!set->used) {
4666 set_fill_string(set, "", 0);
4667 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004668 }
4669
4670 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004671 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004672
4673 item = &set->val.nodes[0];
4674 }
4675
4676 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004677 case LYXP_NODE_NONE:
4678 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004679 case LYXP_NODE_ROOT:
4680 case LYXP_NODE_ROOT_CONFIG:
4681 case LYXP_NODE_TEXT:
4682 set_fill_string(set, "", 0);
4683 break;
4684 case LYXP_NODE_ELEM:
Michal Vasko9f96a052020-03-10 09:41:45 +01004685 case LYXP_NODE_META:
Michal Vasko03ff5a72019-09-11 13:49:33 +02004686 if (item->type == LYXP_NODE_ELEM) {
Michal Vasko420cc252023-08-24 08:14:24 +02004687 mod = lyd_node_module(item->node);
Michal Vasko9f96a052020-03-10 09:41:45 +01004688 } else { /* LYXP_NODE_META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02004689 /* annotations */
Michal Vasko9f96a052020-03-10 09:41:45 +01004690 mod = ((struct lyd_meta *)item->node)->annotation->module;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004691 }
4692
4693 set_fill_string(set, mod->ns, strlen(mod->ns));
4694 break;
4695 }
4696
4697 return LY_SUCCESS;
4698}
4699
4700/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02004701 * @brief Execute the XPath normalize-space(string?) function. Returns LYXP_SET_STRING
4702 * with normalized value (no leading, trailing, double white spaces) of the node
4703 * from the argument or the context.
4704 *
4705 * @param[in] args Array of arguments.
4706 * @param[in] arg_count Count of elements in @p args.
4707 * @param[in,out] set Context and result set at the same time.
4708 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004709 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004710 */
4711static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004712xpath_normalize_space(struct lyxp_set **args, uint32_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004713{
Michal Vaskodd528af2022-08-08 14:35:07 +02004714 uint32_t i, new_used;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004715 char *new;
Radek Krejci857189e2020-09-01 13:26:36 +02004716 ly_bool have_spaces = 0, space_before = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004717 struct lysc_node_leaf *sleaf;
4718 LY_ERR rc = LY_SUCCESS;
4719
4720 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004721 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) &&
4722 (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004723 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004724 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
4725 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004726 } else if (!warn_is_string_type(sleaf->type)) {
4727 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004728 }
4729 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004730 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004731 return rc;
4732 }
4733
4734 if (arg_count) {
4735 set_fill_set(set, args[0]);
4736 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004737 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004738 LY_CHECK_RET(rc);
4739
4740 /* is there any normalization necessary? */
4741 for (i = 0; set->val.str[i]; ++i) {
4742 if (is_xmlws(set->val.str[i])) {
4743 if ((i == 0) || space_before || (!set->val.str[i + 1])) {
4744 have_spaces = 1;
4745 break;
4746 }
4747 space_before = 1;
4748 } else {
4749 space_before = 0;
4750 }
4751 }
4752
4753 /* yep, there is */
4754 if (have_spaces) {
4755 /* it's enough, at least one character will go, makes space for ending '\0' */
4756 new = malloc(strlen(set->val.str) * sizeof(char));
4757 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4758 new_used = 0;
4759
4760 space_before = 0;
4761 for (i = 0; set->val.str[i]; ++i) {
4762 if (is_xmlws(set->val.str[i])) {
4763 if ((i == 0) || space_before) {
4764 space_before = 1;
4765 continue;
4766 } else {
4767 space_before = 1;
4768 }
4769 } else {
4770 space_before = 0;
4771 }
4772
4773 new[new_used] = (space_before ? ' ' : set->val.str[i]);
4774 ++new_used;
4775 }
4776
4777 /* at worst there is one trailing space now */
4778 if (new_used && is_xmlws(new[new_used - 1])) {
4779 --new_used;
4780 }
4781
4782 new = ly_realloc(new, (new_used + 1) * sizeof(char));
4783 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4784 new[new_used] = '\0';
4785
4786 free(set->val.str);
4787 set->val.str = new;
4788 }
4789
4790 return LY_SUCCESS;
4791}
4792
4793/**
4794 * @brief Execute the XPath not(boolean) function. Returns LYXP_SET_BOOLEAN
4795 * with the argument converted to boolean and logically inverted.
4796 *
4797 * @param[in] args Array of arguments.
4798 * @param[in] arg_count Count of elements in @p args.
4799 * @param[in,out] set Context and result set at the same time.
4800 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004801 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004802 */
4803static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004804xpath_not(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004805{
4806 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004807 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004808 return LY_SUCCESS;
4809 }
4810
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004811 lyxp_set_cast(args[0], LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +02004812 if (args[0]->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004813 set_fill_boolean(set, 0);
4814 } else {
4815 set_fill_boolean(set, 1);
4816 }
4817
4818 return LY_SUCCESS;
4819}
4820
4821/**
4822 * @brief Execute the XPath number(object?) function. Returns LYXP_SET_NUMBER
4823 * with the number representation of either the argument or the context.
4824 *
4825 * @param[in] args Array of arguments.
4826 * @param[in] arg_count Count of elements in @p args.
4827 * @param[in,out] set Context and result set at the same time.
4828 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004829 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004830 */
4831static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004832xpath_number(struct lyxp_set **args, uint32_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004833{
4834 LY_ERR rc;
4835
4836 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004837 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004838 return LY_SUCCESS;
4839 }
4840
4841 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004842 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004843 LY_CHECK_RET(rc);
4844 set_fill_set(set, args[0]);
4845 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004846 rc = lyxp_set_cast(set, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004847 LY_CHECK_RET(rc);
4848 }
4849
4850 return LY_SUCCESS;
4851}
4852
4853/**
4854 * @brief Execute the XPath position() function. Returns LYXP_SET_NUMBER
4855 * with the context position.
4856 *
4857 * @param[in] args Array of arguments.
4858 * @param[in] arg_count Count of elements in @p args.
4859 * @param[in,out] set Context and result set at the same time.
4860 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004861 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004862 */
4863static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004864xpath_position(struct lyxp_set **UNUSED(args), uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004865{
4866 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004867 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004868 return LY_SUCCESS;
4869 }
4870
Michal Vasko03ff5a72019-09-11 13:49:33 +02004871 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004872 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "position()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004873 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004874 } else if (!set->used) {
4875 set_fill_number(set, 0);
4876 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004877 }
4878
4879 set_fill_number(set, set->ctx_pos);
4880
4881 /* UNUSED in 'Release' build type */
4882 (void)options;
4883 return LY_SUCCESS;
4884}
4885
4886/**
4887 * @brief Execute the YANG 1.1 re-match(string, string) function. Returns LYXP_SET_BOOLEAN
4888 * depending on whether the second argument regex matches the first argument string. For details refer to
4889 * YANG 1.1 RFC section 10.2.1.
4890 *
4891 * @param[in] args Array of arguments.
4892 * @param[in] arg_count Count of elements in @p args.
4893 * @param[in,out] set Context and result set at the same time.
4894 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004895 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004896 */
4897static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004898xpath_re_match(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004899{
4900 struct lysc_pattern **patterns = NULL, **pattern;
4901 struct lysc_node_leaf *sleaf;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004902 LY_ERR rc = LY_SUCCESS;
4903 struct ly_err_item *err;
4904
4905 if (options & LYXP_SCNODE_ALL) {
4906 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4907 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4908 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004909 } else if (!warn_is_string_type(sleaf->type)) {
4910 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004911 }
4912 }
4913
4914 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4915 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4916 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004917 } else if (!warn_is_string_type(sleaf->type)) {
4918 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004919 }
4920 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004921 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004922 return rc;
4923 }
4924
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004925 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004926 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004927 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004928 LY_CHECK_RET(rc);
4929
4930 LY_ARRAY_NEW_RET(set->ctx, patterns, pattern, LY_EMEM);
Radek Iša45802b52021-02-09 09:21:58 +01004931 *pattern = calloc(1, sizeof **pattern);
Radek Krejciddace2c2021-01-08 11:30:56 +01004932 LOG_LOCSET(NULL, set->cur_node, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01004933 rc = lys_compile_type_pattern_check(set->ctx, args[1]->val.str, &(*pattern)->code);
Michal Vasko4a7d4d62021-12-13 17:05:06 +01004934 if (set->cur_node) {
4935 LOG_LOCBACK(0, 1, 0, 0);
4936 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004937 if (rc != LY_SUCCESS) {
4938 LY_ARRAY_FREE(patterns);
4939 return rc;
4940 }
4941
Radek Krejci0b013302021-03-29 15:22:32 +02004942 rc = lyplg_type_validate_patterns(patterns, args[0]->val.str, strlen(args[0]->val.str), &err);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004943 pcre2_code_free((*pattern)->code);
4944 free(*pattern);
4945 LY_ARRAY_FREE(patterns);
4946 if (rc && (rc != LY_EVALID)) {
Michal Vasko177d0ed2020-11-23 16:43:03 +01004947 ly_err_print(set->ctx, err);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004948 ly_err_free(err);
4949 return rc;
4950 }
4951
4952 if (rc == LY_EVALID) {
4953 ly_err_free(err);
4954 set_fill_boolean(set, 0);
4955 } else {
4956 set_fill_boolean(set, 1);
4957 }
4958
4959 return LY_SUCCESS;
4960}
4961
4962/**
4963 * @brief Execute the XPath round(number) function. Returns LYXP_SET_NUMBER
4964 * with the rounded first argument. For details refer to
4965 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-round.
4966 *
4967 * @param[in] args Array of arguments.
4968 * @param[in] arg_count Count of elements in @p args.
4969 * @param[in,out] set Context and result set at the same time.
4970 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004971 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004972 */
4973static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004974xpath_round(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004975{
4976 struct lysc_node_leaf *sleaf;
4977 LY_ERR rc = LY_SUCCESS;
4978
4979 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02004980 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004981 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02004982 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4983 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4984 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
4985 sleaf->name);
4986 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
4987 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
4988 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004989 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004990 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004991 return rc;
4992 }
4993
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004994 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004995 LY_CHECK_RET(rc);
4996
4997 /* cover only the cases where floor can't be used */
4998 if ((args[0]->val.num == -0.0f) || ((args[0]->val.num < 0) && (args[0]->val.num >= -0.5))) {
4999 set_fill_number(set, -0.0f);
5000 } else {
5001 args[0]->val.num += 0.5;
5002 rc = xpath_floor(args, 1, args[0], options);
5003 LY_CHECK_RET(rc);
5004 set_fill_number(set, args[0]->val.num);
5005 }
5006
5007 return LY_SUCCESS;
5008}
5009
5010/**
5011 * @brief Execute the XPath starts-with(string, string) function.
5012 * Returns LYXP_SET_BOOLEAN whether the second argument is
5013 * the prefix of the first or not.
5014 *
5015 * @param[in] args Array of arguments.
5016 * @param[in] arg_count Count of elements in @p args.
5017 * @param[in,out] set Context and result set at the same time.
5018 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005019 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005020 */
5021static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02005022xpath_starts_with(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005023{
5024 struct lysc_node_leaf *sleaf;
5025 LY_ERR rc = LY_SUCCESS;
5026
5027 if (options & LYXP_SCNODE_ALL) {
5028 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5029 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5030 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005031 } else if (!warn_is_string_type(sleaf->type)) {
5032 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005033 }
5034 }
5035
5036 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5037 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5038 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005039 } else if (!warn_is_string_type(sleaf->type)) {
5040 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005041 }
5042 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005043 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005044 return rc;
5045 }
5046
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005047 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005048 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005049 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005050 LY_CHECK_RET(rc);
5051
5052 if (strncmp(args[0]->val.str, args[1]->val.str, strlen(args[1]->val.str))) {
5053 set_fill_boolean(set, 0);
5054 } else {
5055 set_fill_boolean(set, 1);
5056 }
5057
5058 return LY_SUCCESS;
5059}
5060
5061/**
5062 * @brief Execute the XPath string(object?) function. Returns LYXP_SET_STRING
5063 * with the string representation of either the argument or the context.
5064 *
5065 * @param[in] args Array of arguments.
5066 * @param[in] arg_count Count of elements in @p args.
5067 * @param[in,out] set Context and result set at the same time.
5068 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005069 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005070 */
5071static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02005072xpath_string(struct lyxp_set **args, uint32_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005073{
5074 LY_ERR rc;
5075
5076 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02005077 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005078 return LY_SUCCESS;
5079 }
5080
5081 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005082 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005083 LY_CHECK_RET(rc);
5084 set_fill_set(set, args[0]);
5085 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005086 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005087 LY_CHECK_RET(rc);
5088 }
5089
5090 return LY_SUCCESS;
5091}
5092
5093/**
5094 * @brief Execute the XPath string-length(string?) function. Returns LYXP_SET_NUMBER
5095 * with the length of the string in either the argument or the context.
5096 *
5097 * @param[in] args Array of arguments.
5098 * @param[in] arg_count Count of elements in @p args.
5099 * @param[in,out] set Context and result set at the same time.
5100 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005101 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005102 */
5103static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02005104xpath_string_length(struct lyxp_set **args, uint32_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005105{
5106 struct lysc_node_leaf *sleaf;
5107 LY_ERR rc = LY_SUCCESS;
5108
5109 if (options & LYXP_SCNODE_ALL) {
5110 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5111 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5112 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005113 } else if (!warn_is_string_type(sleaf->type)) {
5114 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005115 }
5116 }
5117 if (!arg_count && (set->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set))) {
5118 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5119 LOGWRN(set->ctx, "Argument #0 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005120 } else if (!warn_is_string_type(sleaf->type)) {
5121 LOGWRN(set->ctx, "Argument #0 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005122 }
5123 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005124 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005125 return rc;
5126 }
5127
5128 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005129 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005130 LY_CHECK_RET(rc);
5131 set_fill_number(set, strlen(args[0]->val.str));
5132 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005133 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005134 LY_CHECK_RET(rc);
5135 set_fill_number(set, strlen(set->val.str));
5136 }
5137
5138 return LY_SUCCESS;
5139}
5140
5141/**
5142 * @brief Execute the XPath substring(string, number, number?) function.
5143 * Returns LYXP_SET_STRING substring of the first argument starting
5144 * on the second argument index ending on the third argument index,
5145 * indexed from 1. For exact definition refer to
5146 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-substring.
5147 *
5148 * @param[in] args Array of arguments.
5149 * @param[in] arg_count Count of elements in @p args.
5150 * @param[in,out] set Context and result set at the same time.
5151 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005152 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005153 */
5154static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02005155xpath_substring(struct lyxp_set **args, uint32_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005156{
Michal Vasko6db996e2022-07-28 10:28:04 +02005157 int64_t start;
5158 int32_t len;
Michal Vaskodd528af2022-08-08 14:35:07 +02005159 uint32_t str_start, str_len, pos;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005160 struct lysc_node_leaf *sleaf;
5161 LY_ERR rc = LY_SUCCESS;
5162
5163 if (options & LYXP_SCNODE_ALL) {
5164 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5165 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5166 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005167 } else if (!warn_is_string_type(sleaf->type)) {
5168 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005169 }
5170 }
5171
5172 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5173 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5174 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005175 } else if (!warn_is_numeric_type(sleaf->type)) {
5176 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005177 }
5178 }
5179
Michal Vasko69730152020-10-09 16:30:07 +02005180 if ((arg_count == 3) && (args[2]->type == LYXP_SET_SCNODE_SET) &&
5181 (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005182 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5183 LOGWRN(set->ctx, "Argument #3 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005184 } else if (!warn_is_numeric_type(sleaf->type)) {
5185 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005186 }
5187 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005188 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005189 return rc;
5190 }
5191
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005192 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005193 LY_CHECK_RET(rc);
5194
5195 /* start */
5196 if (xpath_round(&args[1], 1, args[1], options)) {
5197 return -1;
5198 }
5199 if (isfinite(args[1]->val.num)) {
5200 start = args[1]->val.num - 1;
5201 } else if (isinf(args[1]->val.num) && signbit(args[1]->val.num)) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02005202 start = INT32_MIN;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005203 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02005204 start = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005205 }
5206
5207 /* len */
5208 if (arg_count == 3) {
5209 rc = xpath_round(&args[2], 1, args[2], options);
5210 LY_CHECK_RET(rc);
Radek Krejci1deb5be2020-08-26 16:43:36 +02005211 if (isnan(args[2]->val.num) || signbit(args[2]->val.num)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005212 len = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005213 } else if (isfinite(args[2]->val.num)) {
5214 len = args[2]->val.num;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005215 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02005216 len = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005217 }
5218 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02005219 len = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005220 }
5221
5222 /* find matching character positions */
5223 str_start = 0;
5224 str_len = 0;
5225 for (pos = 0; args[0]->val.str[pos]; ++pos) {
5226 if (pos < start) {
5227 ++str_start;
5228 } else if (pos < start + len) {
5229 ++str_len;
5230 } else {
5231 break;
5232 }
5233 }
5234
5235 set_fill_string(set, args[0]->val.str + str_start, str_len);
5236 return LY_SUCCESS;
5237}
5238
5239/**
5240 * @brief Execute the XPath substring-after(string, string) function.
5241 * Returns LYXP_SET_STRING with the string succeeding the occurance
5242 * of the second argument in the first or an empty string.
5243 *
5244 * @param[in] args Array of arguments.
5245 * @param[in] arg_count Count of elements in @p args.
5246 * @param[in,out] set Context and result set at the same time.
5247 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005248 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005249 */
5250static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02005251xpath_substring_after(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005252{
5253 char *ptr;
5254 struct lysc_node_leaf *sleaf;
5255 LY_ERR rc = LY_SUCCESS;
5256
5257 if (options & LYXP_SCNODE_ALL) {
5258 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5259 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5260 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005261 } else if (!warn_is_string_type(sleaf->type)) {
5262 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005263 }
5264 }
5265
5266 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5267 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5268 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005269 } else if (!warn_is_string_type(sleaf->type)) {
5270 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005271 }
5272 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005273 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005274 return rc;
5275 }
5276
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005277 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005278 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005279 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005280 LY_CHECK_RET(rc);
5281
5282 ptr = strstr(args[0]->val.str, args[1]->val.str);
5283 if (ptr) {
5284 set_fill_string(set, ptr + strlen(args[1]->val.str), strlen(ptr + strlen(args[1]->val.str)));
5285 } else {
5286 set_fill_string(set, "", 0);
5287 }
5288
5289 return LY_SUCCESS;
5290}
5291
5292/**
5293 * @brief Execute the XPath substring-before(string, string) function.
5294 * Returns LYXP_SET_STRING with the string preceding the occurance
5295 * of the second argument in the first or an empty string.
5296 *
5297 * @param[in] args Array of arguments.
5298 * @param[in] arg_count Count of elements in @p args.
5299 * @param[in,out] set Context and result set at the same time.
5300 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005301 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005302 */
5303static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02005304xpath_substring_before(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005305{
5306 char *ptr;
5307 struct lysc_node_leaf *sleaf;
5308 LY_ERR rc = LY_SUCCESS;
5309
5310 if (options & LYXP_SCNODE_ALL) {
5311 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5312 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5313 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005314 } else if (!warn_is_string_type(sleaf->type)) {
5315 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005316 }
5317 }
5318
5319 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5320 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5321 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005322 } else if (!warn_is_string_type(sleaf->type)) {
5323 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005324 }
5325 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005326 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005327 return rc;
5328 }
5329
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005330 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005331 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005332 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005333 LY_CHECK_RET(rc);
5334
5335 ptr = strstr(args[0]->val.str, args[1]->val.str);
5336 if (ptr) {
5337 set_fill_string(set, args[0]->val.str, ptr - args[0]->val.str);
5338 } else {
5339 set_fill_string(set, "", 0);
5340 }
5341
5342 return LY_SUCCESS;
5343}
5344
5345/**
5346 * @brief Execute the XPath sum(node-set) function. Returns LYXP_SET_NUMBER
5347 * with the sum of all the nodes in the context.
5348 *
5349 * @param[in] args Array of arguments.
5350 * @param[in] arg_count Count of elements in @p args.
5351 * @param[in,out] set Context and result set at the same time.
5352 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005353 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005354 */
5355static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02005356xpath_sum(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005357{
5358 long double num;
5359 char *str;
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01005360 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005361 struct lyxp_set set_item;
5362 struct lysc_node_leaf *sleaf;
5363 LY_ERR rc = LY_SUCCESS;
5364
5365 if (options & LYXP_SCNODE_ALL) {
5366 if (args[0]->type == LYXP_SET_SCNODE_SET) {
5367 for (i = 0; i < args[0]->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005368 if (args[0]->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005369 sleaf = (struct lysc_node_leaf *)args[0]->val.scnodes[i].scnode;
5370 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5371 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__,
Michal Vasko69730152020-10-09 16:30:07 +02005372 lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005373 } else if (!warn_is_numeric_type(sleaf->type)) {
5374 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005375 }
5376 }
5377 }
5378 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005379 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005380 return rc;
5381 }
5382
5383 set_fill_number(set, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005384
5385 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005386 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "sum(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02005387 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02005388 } else if (!args[0]->used) {
5389 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005390 }
5391
Michal Vasko5c4e5892019-11-14 12:31:38 +01005392 set_init(&set_item, set);
5393
Michal Vasko03ff5a72019-09-11 13:49:33 +02005394 set_item.type = LYXP_SET_NODE_SET;
Michal Vasko41decbf2021-11-02 11:50:21 +01005395 set_item.val.nodes = calloc(1, sizeof *set_item.val.nodes);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005396 LY_CHECK_ERR_RET(!set_item.val.nodes, LOGMEM(set->ctx), LY_EMEM);
5397
5398 set_item.used = 1;
5399 set_item.size = 1;
5400
5401 for (i = 0; i < args[0]->used; ++i) {
5402 set_item.val.nodes[0] = args[0]->val.nodes[i];
5403
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005404 rc = cast_node_set_to_string(&set_item, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005405 LY_CHECK_RET(rc);
5406 num = cast_string_to_number(str);
5407 free(str);
5408 set->val.num += num;
5409 }
5410
5411 free(set_item.val.nodes);
5412
5413 return LY_SUCCESS;
5414}
5415
5416/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02005417 * @brief Execute the XPath translate(string, string, string) function.
5418 * Returns LYXP_SET_STRING with the first argument with the characters
5419 * from the second argument replaced by those on the corresponding
5420 * positions in the third argument.
5421 *
5422 * @param[in] args Array of arguments.
5423 * @param[in] arg_count Count of elements in @p args.
5424 * @param[in,out] set Context and result set at the same time.
5425 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005426 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005427 */
5428static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02005429xpath_translate(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005430{
Michal Vaskodd528af2022-08-08 14:35:07 +02005431 uint32_t i, j, new_used;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005432 char *new;
Radek Krejci857189e2020-09-01 13:26:36 +02005433 ly_bool have_removed;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005434 struct lysc_node_leaf *sleaf;
5435 LY_ERR rc = LY_SUCCESS;
5436
5437 if (options & LYXP_SCNODE_ALL) {
5438 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5439 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5440 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005441 } else if (!warn_is_string_type(sleaf->type)) {
5442 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005443 }
5444 }
5445
5446 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5447 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5448 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005449 } else if (!warn_is_string_type(sleaf->type)) {
5450 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005451 }
5452 }
5453
5454 if ((args[2]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
5455 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5456 LOGWRN(set->ctx, "Argument #3 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005457 } else if (!warn_is_string_type(sleaf->type)) {
5458 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005459 }
5460 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005461 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005462 return rc;
5463 }
5464
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005465 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005466 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005467 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005468 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005469 rc = lyxp_set_cast(args[2], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005470 LY_CHECK_RET(rc);
5471
5472 new = malloc((strlen(args[0]->val.str) + 1) * sizeof(char));
5473 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5474 new_used = 0;
5475
5476 have_removed = 0;
5477 for (i = 0; args[0]->val.str[i]; ++i) {
Radek Krejci857189e2020-09-01 13:26:36 +02005478 ly_bool found = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005479
5480 for (j = 0; args[1]->val.str[j]; ++j) {
5481 if (args[0]->val.str[i] == args[1]->val.str[j]) {
5482 /* removing this char */
5483 if (j >= strlen(args[2]->val.str)) {
5484 have_removed = 1;
5485 found = 1;
5486 break;
5487 }
5488 /* replacing this char */
5489 new[new_used] = args[2]->val.str[j];
5490 ++new_used;
5491 found = 1;
5492 break;
5493 }
5494 }
5495
5496 /* copying this char */
5497 if (!found) {
5498 new[new_used] = args[0]->val.str[i];
5499 ++new_used;
5500 }
5501 }
5502
5503 if (have_removed) {
5504 new = ly_realloc(new, (new_used + 1) * sizeof(char));
5505 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5506 }
5507 new[new_used] = '\0';
5508
Michal Vaskod3678892020-05-21 10:06:58 +02005509 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005510 set->type = LYXP_SET_STRING;
5511 set->val.str = new;
5512
5513 return LY_SUCCESS;
5514}
5515
5516/**
5517 * @brief Execute the XPath true() function. Returns LYXP_SET_BOOLEAN
5518 * with true value.
5519 *
5520 * @param[in] args Array of arguments.
5521 * @param[in] arg_count Count of elements in @p args.
5522 * @param[in,out] set Context and result set at the same time.
5523 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005524 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005525 */
5526static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02005527xpath_true(struct lyxp_set **UNUSED(args), uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005528{
5529 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02005530 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005531 return LY_SUCCESS;
5532 }
5533
5534 set_fill_boolean(set, 1);
5535 return LY_SUCCESS;
5536}
5537
Michal Vasko03ff5a72019-09-11 13:49:33 +02005538/**
Michal Vasko49fec8e2022-05-24 10:28:33 +02005539 * @brief Execute the XPath node() processing instruction (node type). Returns LYXP_SET_NODE_SET
5540 * with only nodes from the context.
5541 *
5542 * @param[in,out] set Context and result set at the same time.
5543 * @param[in] axis Axis to search on.
5544 * @param[in] options XPath options.
5545 * @return LY_ERR
5546 */
5547static LY_ERR
5548xpath_pi_node(struct lyxp_set *set, enum lyxp_axis axis, uint32_t options)
5549{
5550 if (options & LYXP_SCNODE_ALL) {
5551 return moveto_scnode(set, NULL, NULL, axis, options);
5552 }
5553
5554 if (set->type != LYXP_SET_NODE_SET) {
5555 lyxp_set_free_content(set);
5556 return LY_SUCCESS;
5557 }
5558
5559 /* just like moving to a node with no restrictions */
5560 return moveto_node(set, NULL, NULL, axis, options);
5561}
5562
5563/**
5564 * @brief Execute the XPath text() processing instruction (node type). Returns LYXP_SET_NODE_SET
5565 * with the text content of the nodes in the context.
5566 *
5567 * @param[in,out] set Context and result set at the same time.
5568 * @param[in] axis Axis to search on.
5569 * @param[in] options XPath options.
5570 * @return LY_ERR
5571 */
5572static LY_ERR
5573xpath_pi_text(struct lyxp_set *set, enum lyxp_axis axis, uint32_t options)
5574{
5575 uint32_t i;
5576
5577 if (options & LYXP_SCNODE_ALL) {
5578 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
5579 return LY_SUCCESS;
5580 }
5581
5582 if (set->type != LYXP_SET_NODE_SET) {
5583 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "text()");
5584 return LY_EVALID;
5585 }
5586
5587 if (axis != LYXP_AXIS_CHILD) {
5588 /* even following and preceding axescan return text nodes, but whatever */
5589 lyxp_set_free_content(set);
5590 return LY_SUCCESS;
5591 }
5592
5593 for (i = 0; i < set->used; ++i) {
5594 switch (set->val.nodes[i].type) {
5595 case LYXP_NODE_NONE:
5596 LOGINT_RET(set->ctx);
5597 case LYXP_NODE_ELEM:
Michal Vasko222be682023-08-24 08:17:12 +02005598 if (!set->val.nodes[i].node->schema || (set->val.nodes[i].node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02005599 set->val.nodes[i].type = LYXP_NODE_TEXT;
5600 break;
5601 }
5602 /* fall through */
5603 case LYXP_NODE_ROOT:
5604 case LYXP_NODE_ROOT_CONFIG:
5605 case LYXP_NODE_TEXT:
5606 case LYXP_NODE_META:
5607 set_remove_node_none(set, i);
5608 break;
5609 }
5610 }
5611 set_remove_nodes_none(set);
5612
5613 return LY_SUCCESS;
5614}
5615
5616/**
Michal Vasko6346ece2019-09-24 13:12:53 +02005617 * @brief Skip prefix and return corresponding model if there is a prefix. Logs directly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005618 *
Michal Vasko2104e9f2020-03-06 08:23:25 +01005619 * XPath @p set is expected to be a (sc)node set!
5620 *
Michal Vasko6346ece2019-09-24 13:12:53 +02005621 * @param[in,out] qname Qualified node name. If includes prefix, it is skipped.
5622 * @param[in,out] qname_len Length of @p qname, is updated accordingly.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005623 * @param[in] set Set with general XPath context.
5624 * @param[in] ctx_scnode Context node to inherit module for unprefixed node for ::LY_PREF_JSON.
Michal Vasko6346ece2019-09-24 13:12:53 +02005625 * @param[out] moveto_mod Expected module of a matching node.
5626 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005627 */
Michal Vasko6346ece2019-09-24 13:12:53 +02005628static LY_ERR
Michal Vaskofe1af042022-07-29 14:58:59 +02005629moveto_resolve_model(const char **qname, uint32_t *qname_len, const struct lyxp_set *set,
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005630 const struct lysc_node *ctx_scnode, const struct lys_module **moveto_mod)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005631{
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02005632 const struct lys_module *mod = NULL;
Michal Vasko6346ece2019-09-24 13:12:53 +02005633 const char *ptr;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005634 size_t pref_len;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005635
Michal Vasko2104e9f2020-03-06 08:23:25 +01005636 assert((set->type == LYXP_SET_NODE_SET) || (set->type == LYXP_SET_SCNODE_SET));
5637
Michal Vasko6346ece2019-09-24 13:12:53 +02005638 if ((ptr = ly_strnchr(*qname, ':', *qname_len))) {
5639 /* specific module */
5640 pref_len = ptr - *qname;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005641 mod = ly_resolve_prefix(set->ctx, *qname, pref_len, set->format, set->prefix_data);
Michal Vasko6346ece2019-09-24 13:12:53 +02005642
Michal Vasko004d3152020-06-11 19:59:22 +02005643 /* check for errors and non-implemented modules, as they are not valid */
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005644 if (!mod || !mod->implemented) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02005645 LOGVAL(set->ctx, LY_VCODE_XP_INMOD, (int)pref_len, *qname);
Michal Vasko6346ece2019-09-24 13:12:53 +02005646 return LY_EVALID;
5647 }
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005648
Michal Vasko6346ece2019-09-24 13:12:53 +02005649 *qname += pref_len + 1;
5650 *qname_len -= pref_len + 1;
5651 } else if (((*qname)[0] == '*') && (*qname_len == 1)) {
5652 /* all modules - special case */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005653 mod = NULL;
Michal Vasko6346ece2019-09-24 13:12:53 +02005654 } else {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005655 switch (set->format) {
Radek Krejci8df109d2021-04-23 12:19:08 +02005656 case LY_VALUE_SCHEMA:
5657 case LY_VALUE_SCHEMA_RESOLVED:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005658 /* current module */
5659 mod = set->cur_mod;
5660 break;
Radek Krejci224d4b42021-04-23 13:54:59 +02005661 case LY_VALUE_CANON:
Radek Krejci8df109d2021-04-23 12:19:08 +02005662 case LY_VALUE_JSON:
Radek Krejcif9943642021-04-26 10:18:21 +02005663 case LY_VALUE_LYB:
Michal Vaskoddd76592022-01-17 13:34:48 +01005664 case LY_VALUE_STR_NS:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005665 /* inherit parent (context node) module */
5666 if (ctx_scnode) {
5667 mod = ctx_scnode->module;
5668 } else {
5669 mod = NULL;
5670 }
5671 break;
Radek Krejci8df109d2021-04-23 12:19:08 +02005672 case LY_VALUE_XML:
Michal Vasko52143d12021-04-14 15:36:39 +02005673 /* all nodes need to be prefixed */
5674 LOGVAL(set->ctx, LYVE_DATA, "Non-prefixed node \"%.*s\" in XML xpath found.", *qname_len, *qname);
5675 return LY_EVALID;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005676 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005677 }
5678
Michal Vasko6346ece2019-09-24 13:12:53 +02005679 *moveto_mod = mod;
5680 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005681}
5682
5683/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02005684 * @brief Move context @p set to the root. Handles absolute path.
5685 * Result is LYXP_SET_NODE_SET.
5686 *
5687 * @param[in,out] set Set to use.
5688 * @param[in] options Xpath options.
Michal Vaskob0099a92020-08-31 14:55:23 +02005689 * @return LY_ERR value.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005690 */
Michal Vaskob0099a92020-08-31 14:55:23 +02005691static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005692moveto_root(struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005693{
aPiecek8b0cc152021-05-31 16:40:31 +02005694 assert(!(options & LYXP_SKIP_EXPR));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005695
5696 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02005697 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskoe4a6d012023-05-22 14:34:52 +02005698 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, NULL, set->root_type, LYXP_AXIS_SELF, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005699 } else {
Michal Vaskoef71b392023-08-16 09:45:23 +02005700 lyxp_set_free_content(set);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005701 set_insert_node(set, NULL, 0, set->root_type, 0);
Michal Vasko306e2832022-07-25 09:15:17 +02005702 set->non_child_axis = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005703 }
Michal Vaskob0099a92020-08-31 14:55:23 +02005704
5705 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005706}
5707
5708/**
5709 * @brief Check @p node as a part of NameTest processing.
5710 *
5711 * @param[in] node Node to check.
Michal Vasko49fec8e2022-05-24 10:28:33 +02005712 * @param[in] node_type Node type of @p node.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005713 * @param[in] set Set to read general context from.
Michal Vaskod3678892020-05-21 10:06:58 +02005714 * @param[in] node_name Node name in the dictionary to move to, NULL for any node.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005715 * @param[in] moveto_mod Expected module of the node, NULL for no prefix.
Michal Vaskocdad7122020-11-09 21:04:44 +01005716 * @param[in] options XPath options.
Michal Vasko6346ece2019-09-24 13:12:53 +02005717 * @return LY_ERR (LY_ENOT if node does not match, LY_EINCOMPLETE on unresolved when,
Michal Vaskocd2c88a2022-06-07 10:54:34 +02005718 * LY_EINVAL if neither node nor any children match)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005719 */
5720static LY_ERR
Michal Vasko49fec8e2022-05-24 10:28:33 +02005721moveto_node_check(const struct lyd_node *node, enum lyxp_node_type node_type, const struct lyxp_set *set,
5722 const char *node_name, const struct lys_module *moveto_mod, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005723{
Michal Vaskofbd0da42023-08-17 14:57:44 +02005724 const struct lysc_node *schema;
5725
Michal Vasko49fec8e2022-05-24 10:28:33 +02005726 if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT)) {
5727 assert(node_type == set->root_type);
5728
5729 if (node_name || moveto_mod) {
5730 /* root will not match a specific node */
5731 return LY_ENOT;
5732 }
5733 return LY_SUCCESS;
5734 } else if (node_type != LYXP_NODE_ELEM) {
5735 /* other types will not match */
5736 return LY_ENOT;
5737 }
5738
Michal Vaskofbd0da42023-08-17 14:57:44 +02005739 /* get schema node even of an opaque node */
5740 schema = lyd_node_schema(node);
5741 if (!schema) {
5742 /* unknown opaque node never matches */
Michal Vaskodca9f122021-07-16 13:56:22 +02005743 return LY_ENOT;
5744 }
5745
Michal Vasko03ff5a72019-09-11 13:49:33 +02005746 /* module check */
Michal Vasko19089f02022-06-07 11:02:11 +02005747 if (moveto_mod) {
Michal Vaskofbd0da42023-08-17 14:57:44 +02005748 if ((set->ctx == LYD_CTX(node)) && (schema->module != moveto_mod)) {
Michal Vasko19089f02022-06-07 11:02:11 +02005749 return LY_ENOT;
Michal Vaskofbd0da42023-08-17 14:57:44 +02005750 } else if ((set->ctx != LYD_CTX(node)) && strcmp(schema->module->name, moveto_mod->name)) {
Michal Vasko19089f02022-06-07 11:02:11 +02005751 return LY_ENOT;
5752 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005753 }
5754
Michal Vasko5c4e5892019-11-14 12:31:38 +01005755 /* context check */
Michal Vaskofbd0da42023-08-17 14:57:44 +02005756 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (schema->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005757 return LY_EINVAL;
Michal Vaskofbd0da42023-08-17 14:57:44 +02005758 } else if (set->context_op && (schema->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && (schema != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005759 return LY_EINVAL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005760 }
5761
5762 /* name check */
Michal Vasko19089f02022-06-07 11:02:11 +02005763 if (node_name) {
Michal Vaskofbd0da42023-08-17 14:57:44 +02005764 if ((set->ctx == LYD_CTX(node)) && (schema->name != node_name)) {
Michal Vasko19089f02022-06-07 11:02:11 +02005765 return LY_ENOT;
Michal Vaskofbd0da42023-08-17 14:57:44 +02005766 } else if ((set->ctx != LYD_CTX(node)) && strcmp(schema->name, node_name)) {
Michal Vasko19089f02022-06-07 11:02:11 +02005767 return LY_ENOT;
5768 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005769 }
5770
Michal Vasko6d657122022-10-19 14:38:35 +02005771 /* when check, accept the context node because it should only be the path ".", we have checked the when is valid before */
Michal Vaskofbd0da42023-08-17 14:57:44 +02005772 if (!(options & LYXP_IGNORE_WHEN) && lysc_has_when(schema) && !(node->flags & LYD_WHEN_TRUE) &&
Michal Vasko6d657122022-10-19 14:38:35 +02005773 (node != set->cur_node)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005774 return LY_EINCOMPLETE;
Michal Vaskoa1424542019-11-14 16:08:52 +01005775 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005776
5777 /* match */
5778 return LY_SUCCESS;
5779}
5780
5781/**
Michal Vasko49fec8e2022-05-24 10:28:33 +02005782 * @brief Get the next node in a forward DFS.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005783 *
Michal Vasko49fec8e2022-05-24 10:28:33 +02005784 * @param[in] iter Last returned node.
5785 * @param[in] stop Node to stop the search on and not return.
5786 * @return Next node, NULL if there are no more.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005787 */
Michal Vasko49fec8e2022-05-24 10:28:33 +02005788static const struct lyd_node *
5789moveto_axis_node_next_dfs_forward(const struct lyd_node *iter, const struct lyd_node *stop)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005790{
Michal Vasko49fec8e2022-05-24 10:28:33 +02005791 const struct lyd_node *next = NULL;
5792
5793 /* 1) child */
5794 next = lyd_child(iter);
5795 if (!next) {
5796 if (iter == stop) {
5797 /* reached stop, no more descendants */
5798 return NULL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005799 }
Michal Vasko49fec8e2022-05-24 10:28:33 +02005800 /* 2) child next sibling */
5801 next = iter->next;
5802 }
5803 while (!next) {
5804 iter = lyd_parent(iter);
5805 if ((!stop && !iter) || (stop && (lyd_parent(iter) == lyd_parent(stop)))) {
5806 return NULL;
5807 }
5808 next = iter->next;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005809 }
5810
Michal Vasko49fec8e2022-05-24 10:28:33 +02005811 return next;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005812}
5813
5814/**
Michal Vasko49fec8e2022-05-24 10:28:33 +02005815 * @brief Get the next node in a backward DFS.
5816 *
5817 * @param[in] iter Last returned node.
5818 * @param[in] stop Node to stop the search on and not return.
5819 * @return Next node, NULL if there are no more.
5820 */
5821static const struct lyd_node *
5822moveto_axis_node_next_dfs_backward(const struct lyd_node *iter, const struct lyd_node *stop)
5823{
5824 const struct lyd_node *next = NULL;
5825
5826 /* 1) previous sibling innermost last child */
5827 next = iter->prev->next ? iter->prev : NULL;
5828 while (next && lyd_child(next)) {
5829 next = lyd_child(next);
5830 next = next->prev;
5831 }
5832
5833 if (!next) {
5834 /* 2) parent */
5835 iter = lyd_parent(iter);
5836 if ((!stop && !iter) || (stop && (lyd_parent(iter) == lyd_parent(stop)))) {
5837 return NULL;
5838 }
5839 next = iter;
5840 }
5841
5842 return next;
5843}
5844
5845/**
5846 * @brief Get the first node on an axis for a context node.
5847 *
5848 * @param[in,out] iter NULL, updated to the next node.
5849 * @param[in,out] iter_type Node type 0 of @p iter, updated to the node type of the next node.
5850 * @param[in] node Context node.
5851 * @param[in] node_type Type of @p node.
5852 * @param[in] axis Axis to use.
5853 * @param[in] set XPath set with the general context.
5854 * @return LY_SUCCESS on success.
5855 * @return LY_ENOTFOUND if no next node found.
5856 */
5857static LY_ERR
5858moveto_axis_node_next_first(const struct lyd_node **iter, enum lyxp_node_type *iter_type, const struct lyd_node *node,
5859 enum lyxp_node_type node_type, enum lyxp_axis axis, struct lyxp_set *set)
5860{
5861 const struct lyd_node *next = NULL;
5862 enum lyxp_node_type next_type = 0;
5863
5864 assert(!*iter);
5865 assert(!*iter_type);
5866
5867 switch (axis) {
5868 case LYXP_AXIS_ANCESTOR_OR_SELF:
5869 case LYXP_AXIS_DESCENDANT_OR_SELF:
5870 case LYXP_AXIS_SELF:
5871 /* return the context node */
5872 next = node;
5873 next_type = node_type;
5874 break;
5875
5876 case LYXP_AXIS_ANCESTOR:
5877 case LYXP_AXIS_PARENT:
5878 if (node_type == LYXP_NODE_ELEM) {
5879 next = lyd_parent(node);
5880 next_type = next ? LYXP_NODE_ELEM : set->root_type;
5881 } else if (node_type == LYXP_NODE_TEXT) {
5882 next = node;
5883 next_type = LYXP_NODE_ELEM;
5884 } else if (node_type == LYXP_NODE_META) {
5885 next = ((struct lyd_meta *)node)->parent;
5886 next_type = LYXP_NODE_ELEM;
5887 } /* else root does not have a parent */
5888 break;
5889
5890 case LYXP_AXIS_CHILD:
5891 if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT)) {
5892 assert(!node);
5893
5894 /* search in all the trees */
5895 next = set->tree;
5896 next_type = next ? LYXP_NODE_ELEM : 0;
5897 } else {
5898 /* search in children */
5899 next = lyd_child(node);
5900 next_type = next ? LYXP_NODE_ELEM : 0;
5901 }
5902 break;
5903
5904 case LYXP_AXIS_DESCENDANT:
5905 if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT)) {
5906 /* top-level nodes */
5907 next = set->tree;
5908 next_type = LYXP_NODE_ELEM;
5909 } else if (node_type == LYXP_NODE_ELEM) {
5910 /* start from the context node */
5911 next = moveto_axis_node_next_dfs_forward(node, node);
5912 next_type = next ? LYXP_NODE_ELEM : 0;
5913 } /* else no children */
5914 break;
5915
5916 case LYXP_AXIS_FOLLOWING:
5917 case LYXP_AXIS_FOLLOWING_SIBLING:
5918 if (node_type == LYXP_NODE_ELEM) {
5919 /* first next sibling */
5920 next = node->next;
5921 next_type = next ? LYXP_NODE_ELEM : 0;
5922 } /* else no sibling */
5923 break;
5924
5925 case LYXP_AXIS_PRECEDING:
5926 if ((node_type == LYXP_NODE_ELEM) && node->prev->next) {
5927 /* skip ancestors */
5928 next = moveto_axis_node_next_dfs_backward(node, NULL);
5929 assert(next);
5930 next_type = LYXP_NODE_ELEM;
5931 } /* else no sibling */
5932 break;
5933
5934 case LYXP_AXIS_PRECEDING_SIBLING:
5935 if (node_type == LYXP_NODE_ELEM) {
5936 /* first previous sibling */
5937 next = node->prev->next ? node->prev : NULL;
5938 next_type = next ? LYXP_NODE_ELEM : 0;
5939 } /* else no sibling */
5940 break;
5941
5942 case LYXP_AXIS_ATTRIBUTE:
5943 /* handled specially */
5944 assert(0);
5945 LOGINT(set->ctx);
5946 break;
5947 }
5948
5949 *iter = next;
5950 *iter_type = next_type;
5951 return next_type ? LY_SUCCESS : LY_ENOTFOUND;
5952}
5953
5954/**
5955 * @brief Iterate over all nodes on an axis for a context node.
5956 *
5957 * @param[in,out] iter Last returned node, start with NULL, updated to the next node.
5958 * @param[in,out] iter_type Node type of @p iter, start with 0, updated to the node type of the next node.
5959 * @param[in] node Context node.
5960 * @param[in] node_type Type of @p node.
5961 * @param[in] axis Axis to use.
5962 * @param[in] set XPath set with the general context.
5963 * @return LY_SUCCESS on success.
5964 * @return LY_ENOTFOUND if no next node found.
5965 */
5966static LY_ERR
5967moveto_axis_node_next(const struct lyd_node **iter, enum lyxp_node_type *iter_type, const struct lyd_node *node,
5968 enum lyxp_node_type node_type, enum lyxp_axis axis, struct lyxp_set *set)
5969{
5970 const struct lyd_node *next = NULL;
5971 enum lyxp_node_type next_type = 0;
5972
5973 if (!*iter_type) {
5974 /* first returned node */
5975 return moveto_axis_node_next_first(iter, iter_type, node, node_type, axis, set);
5976 }
5977
5978 switch (axis) {
5979 case LYXP_AXIS_ANCESTOR_OR_SELF:
5980 if ((*iter == node) && (*iter_type == node_type)) {
5981 /* fake first ancestor, we returned self before */
5982 *iter = NULL;
5983 *iter_type = 0;
5984 return moveto_axis_node_next_first(iter, iter_type, node, node_type, LYXP_AXIS_ANCESTOR, set);
5985 } /* else continue ancestor */
5986
5987 /* fallthrough */
5988 case LYXP_AXIS_ANCESTOR:
5989 if (*iter_type == LYXP_NODE_ELEM) {
5990 /* iter parent */
5991 next = lyd_parent(*iter);
5992 next_type = next ? LYXP_NODE_ELEM : set->root_type;
5993 } /* else root, no ancestors */
5994 break;
5995
5996 case LYXP_AXIS_CHILD:
5997 assert(*iter_type == LYXP_NODE_ELEM);
5998
5999 /* next sibling (child) */
6000 next = (*iter)->next;
6001 next_type = next ? LYXP_NODE_ELEM : 0;
6002 break;
6003
6004 case LYXP_AXIS_DESCENDANT_OR_SELF:
6005 if ((*iter == node) && (*iter_type == node_type)) {
6006 /* fake first descendant, we returned self before */
6007 *iter = NULL;
6008 *iter_type = 0;
6009 return moveto_axis_node_next_first(iter, iter_type, node, node_type, LYXP_AXIS_DESCENDANT, set);
6010 } /* else continue descendant */
6011
6012 /* fallthrough */
6013 case LYXP_AXIS_DESCENDANT:
6014 assert(*iter_type == LYXP_NODE_ELEM);
6015 next = moveto_axis_node_next_dfs_forward(*iter, node);
6016 next_type = next ? LYXP_NODE_ELEM : 0;
6017 break;
6018
6019 case LYXP_AXIS_FOLLOWING:
6020 assert(*iter_type == LYXP_NODE_ELEM);
6021 next = moveto_axis_node_next_dfs_forward(*iter, NULL);
6022 next_type = next ? LYXP_NODE_ELEM : 0;
6023 break;
6024
6025 case LYXP_AXIS_FOLLOWING_SIBLING:
6026 assert(*iter_type == LYXP_NODE_ELEM);
6027
6028 /* next sibling */
6029 next = (*iter)->next;
6030 next_type = next ? LYXP_NODE_ELEM : 0;
6031 break;
6032
6033 case LYXP_AXIS_PARENT:
6034 case LYXP_AXIS_SELF:
6035 /* parent/self was returned before */
6036 break;
6037
6038 case LYXP_AXIS_PRECEDING:
6039 assert(*iter_type == LYXP_NODE_ELEM);
6040 next = moveto_axis_node_next_dfs_backward(*iter, NULL);
6041 next_type = next ? LYXP_NODE_ELEM : 0;
6042 break;
6043
6044 case LYXP_AXIS_PRECEDING_SIBLING:
6045 assert(*iter_type == LYXP_NODE_ELEM);
6046
6047 /* previous sibling */
6048 next = (*iter)->prev->next ? (*iter)->prev : NULL;
6049 next_type = next ? LYXP_NODE_ELEM : 0;
6050 break;
6051
6052 case LYXP_AXIS_ATTRIBUTE:
6053 /* handled specially */
6054 assert(0);
6055 LOGINT(set->ctx);
6056 break;
6057 }
6058
6059 *iter = next;
6060 *iter_type = next_type;
6061 return next_type ? LY_SUCCESS : LY_ENOTFOUND;
6062}
6063
6064/**
6065 * @brief Move context @p set to a node. Result is LYXP_SET_NODE_SET. Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006066 *
6067 * @param[in,out] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006068 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02006069 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko49fec8e2022-05-24 10:28:33 +02006070 * @param[in] axis Axis to search on.
Michal Vaskocdad7122020-11-09 21:04:44 +01006071 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006072 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6073 */
6074static LY_ERR
Michal Vasko49fec8e2022-05-24 10:28:33 +02006075moveto_node(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, enum lyxp_axis axis,
6076 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006077{
Michal Vasko230cf972021-12-02 12:31:00 +01006078 LY_ERR r, rc = LY_SUCCESS;
Michal Vasko49fec8e2022-05-24 10:28:33 +02006079 const struct lyd_node *iter;
6080 enum lyxp_node_type iter_type;
Michal Vasko230cf972021-12-02 12:31:00 +01006081 struct lyxp_set result;
Michal Vasko49fec8e2022-05-24 10:28:33 +02006082 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006083
aPiecek8b0cc152021-05-31 16:40:31 +02006084 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006085 return LY_SUCCESS;
6086 }
6087
6088 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006089 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006090 return LY_EVALID;
6091 }
6092
Michal Vasko230cf972021-12-02 12:31:00 +01006093 /* init result set */
6094 set_init(&result, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006095
Michal Vasko49fec8e2022-05-24 10:28:33 +02006096 for (i = 0; i < set->used; ++i) {
6097 /* iterate over all the nodes on the axis of the node */
6098 iter = NULL;
6099 iter_type = 0;
6100 while (!moveto_axis_node_next(&iter, &iter_type, set->val.nodes[i].node, set->val.nodes[i].type, axis, set)) {
6101 r = moveto_node_check(iter, iter_type, set, ncname, moveto_mod, options);
6102 if (r == LY_EINCOMPLETE) {
Michal Vasko230cf972021-12-02 12:31:00 +01006103 rc = r;
6104 goto cleanup;
Michal Vasko49fec8e2022-05-24 10:28:33 +02006105 } else if (r) {
6106 continue;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006107 }
Michal Vasko49fec8e2022-05-24 10:28:33 +02006108
6109 /* check for duplicates if they are possible */
6110 switch (axis) {
6111 case LYXP_AXIS_ANCESTOR:
6112 case LYXP_AXIS_ANCESTOR_OR_SELF:
6113 case LYXP_AXIS_DESCENDANT:
6114 case LYXP_AXIS_DESCENDANT_OR_SELF:
6115 case LYXP_AXIS_FOLLOWING:
6116 case LYXP_AXIS_FOLLOWING_SIBLING:
6117 case LYXP_AXIS_PARENT:
6118 case LYXP_AXIS_PRECEDING:
6119 case LYXP_AXIS_PRECEDING_SIBLING:
Michal Vasko306e2832022-07-25 09:15:17 +02006120 result.non_child_axis = 1;
Michal Vasko49fec8e2022-05-24 10:28:33 +02006121 if (set_dup_node_check(&result, iter, iter_type, -1)) {
6122 continue;
6123 }
6124 break;
6125 case LYXP_AXIS_CHILD:
6126 case LYXP_AXIS_SELF:
6127 break;
6128 case LYXP_AXIS_ATTRIBUTE:
6129 /* handled specially */
6130 assert(0);
6131 LOGINT(set->ctx);
6132 break;
6133 }
6134
6135 /* matching node */
6136 set_insert_node(&result, iter, 0, iter_type, result.used);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006137 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006138 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006139
Michal Vasko230cf972021-12-02 12:31:00 +01006140 /* move result to the set */
6141 lyxp_set_free_content(set);
6142 *set = result;
6143 result.type = LYXP_SET_NUMBER;
Michal Vasko49fec8e2022-05-24 10:28:33 +02006144
Michal Vasko306e2832022-07-25 09:15:17 +02006145 /* sort the final set if the document order could have been broken */
6146 if (set->non_child_axis) {
6147 set_sort(set);
6148 } else {
6149 assert(!set_sort(set));
6150 }
Michal Vasko230cf972021-12-02 12:31:00 +01006151
6152cleanup:
6153 lyxp_set_free_content(&result);
6154 return rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006155}
6156
6157/**
Michal Vasko49fec8e2022-05-24 10:28:33 +02006158 * @brief Move context @p set to child nodes using hashes. Result is LYXP_SET_NODE_SET. Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006159 *
6160 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02006161 * @param[in] scnode Matching node schema.
Michal Vasko004d3152020-06-11 19:59:22 +02006162 * @param[in] predicates If @p scnode is ::LYS_LIST or ::LYS_LEAFLIST, the predicates specifying a single instance.
Michal Vaskocdad7122020-11-09 21:04:44 +01006163 * @param[in] options XPath options.
Michal Vaskod3678892020-05-21 10:06:58 +02006164 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6165 */
6166static LY_ERR
Michal Vasko49fec8e2022-05-24 10:28:33 +02006167moveto_node_hash_child(struct lyxp_set *set, const struct lysc_node *scnode, const struct ly_path_predicate *predicates,
Michal Vaskocdad7122020-11-09 21:04:44 +01006168 uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02006169{
Michal Vaskoddd76592022-01-17 13:34:48 +01006170 LY_ERR ret = LY_SUCCESS, r;
Michal Vaskod3678892020-05-21 10:06:58 +02006171 uint32_t i;
Michal Vaskod3678892020-05-21 10:06:58 +02006172 const struct lyd_node *siblings;
Michal Vasko230cf972021-12-02 12:31:00 +01006173 struct lyxp_set result;
Michal Vasko004d3152020-06-11 19:59:22 +02006174 struct lyd_node *sub, *inst = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02006175
Michal Vasko004d3152020-06-11 19:59:22 +02006176 assert(scnode && (!(scnode->nodetype & (LYS_LIST | LYS_LEAFLIST)) || predicates));
Michal Vaskod3678892020-05-21 10:06:58 +02006177
Michal Vasko50aaa072021-12-02 13:11:56 +01006178 /* init result set */
6179 set_init(&result, set);
6180
aPiecek8b0cc152021-05-31 16:40:31 +02006181 if (options & LYXP_SKIP_EXPR) {
Michal Vasko004d3152020-06-11 19:59:22 +02006182 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02006183 }
6184
6185 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006186 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko004d3152020-06-11 19:59:22 +02006187 ret = LY_EVALID;
6188 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02006189 }
6190
6191 /* context check for all the nodes since we have the schema node */
6192 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) {
6193 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02006194 goto cleanup;
Michal Vasko69730152020-10-09 16:30:07 +02006195 } else if (set->context_op && (scnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) &&
6196 (scnode != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02006197 lyxp_set_free_content(set);
6198 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +02006199 }
6200
6201 /* create specific data instance if needed */
6202 if (scnode->nodetype == LYS_LIST) {
Michal Vasko90189962023-02-28 12:10:34 +01006203 LY_CHECK_GOTO(ret = lyd_create_list(scnode, predicates, NULL, &inst), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02006204 } else if (scnode->nodetype == LYS_LEAFLIST) {
6205 LY_CHECK_GOTO(ret = lyd_create_term2(scnode, &predicates[0].value, &inst), cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02006206 }
6207
Michal Vasko230cf972021-12-02 12:31:00 +01006208 for (i = 0; i < set->used; ++i) {
Michal Vaskod3678892020-05-21 10:06:58 +02006209 siblings = NULL;
6210
6211 if ((set->val.nodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.nodes[i].type == LYXP_NODE_ROOT)) {
6212 assert(!set->val.nodes[i].node);
6213
6214 /* search in all the trees */
6215 siblings = set->tree;
6216 } else if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
6217 /* search in children */
Radek Krejcia1c1e542020-09-29 16:06:52 +02006218 siblings = lyd_child(set->val.nodes[i].node);
Michal Vaskod3678892020-05-21 10:06:58 +02006219 }
6220
6221 /* find the node using hashes */
Michal Vasko004d3152020-06-11 19:59:22 +02006222 if (inst) {
Michal Vaskoddd76592022-01-17 13:34:48 +01006223 r = lyd_find_sibling_first(siblings, inst, &sub);
Michal Vaskod3678892020-05-21 10:06:58 +02006224 } else {
Michal Vaskoddd76592022-01-17 13:34:48 +01006225 r = lyd_find_sibling_val(siblings, scnode, NULL, 0, &sub);
Michal Vaskod3678892020-05-21 10:06:58 +02006226 }
Michal Vaskod8a52012023-08-15 11:38:10 +02006227 if (r == LY_ENOTFOUND) {
6228 /* may still be an opaque node */
6229 r = lyd_find_sibling_opaq_next(siblings, scnode->name, &sub);
6230 }
Michal Vaskoddd76592022-01-17 13:34:48 +01006231 LY_CHECK_ERR_GOTO(r && (r != LY_ENOTFOUND), ret = r, cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02006232
6233 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01006234 if (!(options & LYXP_IGNORE_WHEN) && sub && lysc_has_when(sub->schema) && !(sub->flags & LYD_WHEN_TRUE)) {
Michal Vasko004d3152020-06-11 19:59:22 +02006235 ret = LY_EINCOMPLETE;
6236 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02006237 }
6238
6239 if (sub) {
6240 /* pos filled later */
Michal Vasko230cf972021-12-02 12:31:00 +01006241 set_insert_node(&result, sub, 0, LYXP_NODE_ELEM, result.used);
Michal Vaskod3678892020-05-21 10:06:58 +02006242 }
6243 }
6244
Michal Vasko230cf972021-12-02 12:31:00 +01006245 /* move result to the set */
6246 lyxp_set_free_content(set);
6247 *set = result;
6248 result.type = LYXP_SET_NUMBER;
6249 assert(!set_sort(set));
6250
Michal Vasko004d3152020-06-11 19:59:22 +02006251cleanup:
Michal Vasko230cf972021-12-02 12:31:00 +01006252 lyxp_set_free_content(&result);
Michal Vasko004d3152020-06-11 19:59:22 +02006253 lyd_free_tree(inst);
6254 return ret;
Michal Vaskod3678892020-05-21 10:06:58 +02006255}
6256
6257/**
Michal Vasko49fec8e2022-05-24 10:28:33 +02006258 * @brief Check @p node as a part of schema NameTest processing.
6259 *
6260 * @param[in] node Schema node to check.
6261 * @param[in] ctx_scnode Context node.
6262 * @param[in] set Set to read general context from.
6263 * @param[in] node_name Node name in the dictionary to move to, NULL for any nodes.
6264 * @param[in] moveto_mod Expected module of the node, NULL for no prefix.
6265 * @return LY_ERR (LY_ENOT if node does not match, LY_EINVAL if neither node nor any children match)
6266 */
6267static LY_ERR
6268moveto_scnode_check(const struct lysc_node *node, const struct lysc_node *ctx_scnode, const struct lyxp_set *set,
6269 const char *node_name, const struct lys_module *moveto_mod)
6270{
6271 if (!moveto_mod && node_name) {
6272 switch (set->format) {
6273 case LY_VALUE_SCHEMA:
6274 case LY_VALUE_SCHEMA_RESOLVED:
6275 /* use current module */
6276 moveto_mod = set->cur_mod;
6277 break;
6278 case LY_VALUE_JSON:
6279 case LY_VALUE_LYB:
6280 case LY_VALUE_STR_NS:
6281 /* inherit module of the context node, if any */
6282 if (ctx_scnode) {
6283 moveto_mod = ctx_scnode->module;
6284 }
6285 break;
6286 case LY_VALUE_CANON:
6287 case LY_VALUE_XML:
6288 /* not defined */
6289 LOGINT(set->ctx);
6290 return LY_EINVAL;
6291 }
6292 }
6293
6294 if (!node) {
6295 /* root will not match a specific node */
6296 if (node_name || moveto_mod) {
6297 return LY_ENOT;
6298 }
6299 return LY_SUCCESS;
6300 }
6301
6302 /* module check */
6303 if (moveto_mod && (node->module != moveto_mod)) {
6304 return LY_ENOT;
6305 }
6306
6307 /* context check */
6308 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (node->flags & LYS_CONFIG_R)) {
6309 return LY_EINVAL;
6310 } else if (set->context_op && (node->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && (node != set->context_op)) {
6311 return LY_EINVAL;
6312 }
6313
6314 /* name check */
6315 if (node_name && (node->name != node_name)) {
6316 return LY_ENOT;
6317 }
6318
6319 /* match */
6320 return LY_SUCCESS;
6321}
6322
6323/**
6324 * @brief Get the next node in a forward schema node DFS.
6325 *
6326 * @param[in] iter Last returned node.
6327 * @param[in] stop Node to stop the search on and not return.
6328 * @param[in] getnext_opts Options for ::lys_getnext().
6329 * @return Next node, NULL if there are no more.
6330 */
6331static const struct lysc_node *
6332moveto_axis_scnode_next_dfs_forward(const struct lysc_node *iter, const struct lysc_node *stop, uint32_t getnext_opts)
6333{
6334 const struct lysc_node *next = NULL;
6335
6336 next = lysc_node_child(iter);
6337 if (!next) {
6338 /* no children, try siblings */
Michal Vasko34a22fe2022-06-15 07:58:55 +02006339 if ((iter == stop) || !lysc_data_parent(iter)) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02006340 /* we are done, no next element to process */
6341 return NULL;
6342 }
6343
6344 next = lys_getnext(iter, lysc_data_parent(iter), NULL, getnext_opts);
6345 }
6346 while (!next && iter) {
6347 /* parent is already processed, go to its sibling */
6348 iter = iter->parent;
Michal Vasko34a22fe2022-06-15 07:58:55 +02006349 if ((iter == stop) || !lysc_data_parent(iter)) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02006350 /* we are done, no next element to process */
6351 return NULL;
6352 }
6353 next = lys_getnext(iter, lysc_data_parent(iter), NULL, getnext_opts);
6354 }
6355
6356 return next;
6357}
6358
6359/**
6360 * @brief Consider schema node based on its in_ctx enum value.
6361 *
6362 * @param[in,out] in_ctx In_ctx enum of the schema node, may be updated.
6363 * @param[in] axis Axis to use.
6364 * @return LY_SUCCESS on success.
6365 * @return LY_ENOT if the node should not be returned.
6366 */
6367static LY_ERR
6368moveto_axis_scnode_next_in_ctx(int32_t *in_ctx, enum lyxp_axis axis)
6369{
6370 switch (axis) {
6371 case LYXP_AXIS_SELF:
6372 if ((*in_ctx == LYXP_SET_SCNODE_START) || (*in_ctx == LYXP_SET_SCNODE_ATOM_CTX)) {
6373 /* additionally put the start node into context */
6374 *in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
6375 return LY_SUCCESS;
6376 }
6377 break;
6378 case LYXP_AXIS_PARENT:
6379 case LYXP_AXIS_ANCESTOR_OR_SELF:
6380 case LYXP_AXIS_ANCESTOR:
6381 case LYXP_AXIS_DESCENDANT_OR_SELF:
6382 case LYXP_AXIS_DESCENDANT:
6383 case LYXP_AXIS_FOLLOWING:
6384 case LYXP_AXIS_FOLLOWING_SIBLING:
6385 case LYXP_AXIS_PRECEDING:
6386 case LYXP_AXIS_PRECEDING_SIBLING:
6387 case LYXP_AXIS_CHILD:
6388 if (*in_ctx == LYXP_SET_SCNODE_START) {
6389 /* remember that context node was used */
6390 *in_ctx = LYXP_SET_SCNODE_START_USED;
6391 return LY_SUCCESS;
6392 } else if (*in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
6393 /* traversed */
6394 *in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
6395 return LY_SUCCESS;
6396 }
6397 break;
6398 case LYXP_AXIS_ATTRIBUTE:
6399 /* unreachable */
6400 assert(0);
6401 LOGINT(NULL);
6402 break;
6403 }
6404
6405 return LY_ENOT;
6406}
6407
6408/**
6409 * @brief Get previous sibling for a schema node.
6410 *
6411 * @param[in] scnode Schema node.
6412 * @param[in] getnext_opts Options for ::lys_getnext().
6413 * @return Previous sibling, NULL if none.
6414 */
6415static const struct lysc_node *
6416moveto_axis_scnode_preceding_sibling(const struct lysc_node *scnode, uint32_t getnext_opts)
6417{
6418 const struct lysc_node *next = NULL, *prev = NULL;
6419
6420 while ((next = lys_getnext(next, lysc_data_parent(scnode), scnode->module->compiled, getnext_opts))) {
6421 if (next == scnode) {
6422 break;
6423 }
6424
6425 prev = next;
6426 }
6427
6428 return prev;
6429}
6430
6431/**
6432 * @brief Get the first schema node on an axis for a context node.
6433 *
6434 * @param[in,out] iter Last returned node, start with NULL, updated to the next node.
6435 * @param[in,out] iter_type Node type of @p iter, start with 0, updated to the node type of the next node.
6436 * @param[in,out] iter_mod Internal module iterator, do not change.
6437 * @param[in,out] iter_mod_idx Internal module index iterator, do not change.
6438 * @param[in] scnode Context node.
6439 * @param[in] node_type Type of @p scnode.
6440 * @param[in] in_ctx In_ctx enum of @p scnode.
6441 * @param[in] axis Axis to use.
6442 * @param[in] set XPath set with the general context.
6443 * @param[in] getnext_opts Options for ::lys_getnext().
6444 * @return LY_SUCCESS on success.
6445 * @return LY_ENOTFOUND if no next node found.
6446 */
6447static LY_ERR
6448moveto_axis_scnode_next_first(const struct lysc_node **iter, enum lyxp_node_type *iter_type, const struct lys_module **iter_mod,
6449 uint32_t *iter_mod_idx, const struct lysc_node *scnode, enum lyxp_node_type node_type, enum lyxp_axis axis,
6450 struct lyxp_set *set, uint32_t getnext_opts)
6451{
6452 const struct lysc_node *next = NULL;
6453 enum lyxp_node_type next_type = 0;
6454
6455 assert(!*iter);
6456 assert(!*iter_type);
6457
6458 *iter_mod = NULL;
6459 *iter_mod_idx = 0;
6460
6461 switch (axis) {
6462 case LYXP_AXIS_ANCESTOR_OR_SELF:
6463 case LYXP_AXIS_DESCENDANT_OR_SELF:
6464 case LYXP_AXIS_SELF:
6465 if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT) || (node_type == LYXP_NODE_ELEM)) {
6466 /* just return the node */
6467 next = scnode;
6468 next_type = node_type;
6469 }
6470 break;
6471
6472 case LYXP_AXIS_ANCESTOR:
6473 case LYXP_AXIS_PARENT:
6474 if (node_type == LYXP_NODE_ELEM) {
6475 next = lysc_data_parent(scnode);
6476 next_type = next ? LYXP_NODE_ELEM : set->root_type;
6477 } /* else no parent */
6478 break;
6479
6480 case LYXP_AXIS_DESCENDANT:
6481 case LYXP_AXIS_CHILD:
6482 if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT)) {
6483 /* it can actually be in any module, it's all <running>, and even if it's moveto_mod (if set),
6484 * it can be in a top-level augment */
6485 while ((*iter_mod = ly_ctx_get_module_iter(set->ctx, iter_mod_idx))) {
6486 /* module may not be implemented or not compiled yet */
6487 if (!(*iter_mod)->compiled) {
6488 continue;
6489 }
6490
6491 /* get next node */
6492 if ((next = lys_getnext(NULL, NULL, (*iter_mod)->compiled, getnext_opts))) {
6493 next_type = LYXP_NODE_ELEM;
6494 break;
6495 }
6496 }
6497 } else if (node_type == LYXP_NODE_ELEM) {
6498 /* get next node */
6499 next = lys_getnext(NULL, scnode, NULL, getnext_opts);
6500 next_type = next ? LYXP_NODE_ELEM : 0;
6501 }
6502 break;
6503
6504 case LYXP_AXIS_FOLLOWING:
6505 case LYXP_AXIS_FOLLOWING_SIBLING:
6506 if (node_type == LYXP_NODE_ELEM) {
6507 /* first next sibling */
6508 next = lys_getnext(scnode, lysc_data_parent(scnode), scnode->module->compiled, getnext_opts);
6509 next_type = next ? LYXP_NODE_ELEM : 0;
6510 } /* else no sibling */
6511 break;
6512
6513 case LYXP_AXIS_PRECEDING:
6514 case LYXP_AXIS_PRECEDING_SIBLING:
6515 if (node_type == LYXP_NODE_ELEM) {
6516 /* first parent sibling */
6517 next = lys_getnext(NULL, lysc_data_parent(scnode), scnode->module->compiled, getnext_opts);
6518 if (next == scnode) {
6519 /* no preceding sibling */
6520 next = NULL;
6521 }
6522 next_type = next ? LYXP_NODE_ELEM : 0;
6523 } /* else no sibling */
6524 break;
6525
6526 case LYXP_AXIS_ATTRIBUTE:
6527 /* unreachable */
6528 assert(0);
6529 LOGINT(set->ctx);
6530 break;
6531 }
6532
6533 *iter = next;
6534 *iter_type = next_type;
6535 return next_type ? LY_SUCCESS : LY_ENOTFOUND;
6536}
6537
6538/**
6539 * @brief Iterate over all schema nodes on an axis for a context node.
6540 *
6541 * @param[in,out] iter Last returned node, start with NULL, updated to the next node.
6542 * @param[in,out] iter_type Node type of @p iter, start with 0, updated to the node type of the next node.
6543 * @param[in,out] iter_mod Internal module iterator, do not change.
6544 * @param[in,out] iter_mod_idx Internal module index iterator, do not change.
6545 * @param[in] scnode Context node.
6546 * @param[in] node_type Type of @p scnode.
6547 * @param[in] axis Axis to use.
6548 * @param[in] set XPath set with the general context.
6549 * @param[in] getnext_opts Options for ::lys_getnext().
6550 * @return LY_SUCCESS on success.
6551 * @return LY_ENOTFOUND if no next node found.
6552 */
6553static LY_ERR
6554moveto_axis_scnode_next(const struct lysc_node **iter, enum lyxp_node_type *iter_type, const struct lys_module **iter_mod,
6555 uint32_t *iter_mod_idx, const struct lysc_node *scnode, enum lyxp_node_type node_type, enum lyxp_axis axis,
6556 struct lyxp_set *set, uint32_t getnext_opts)
6557{
6558 const struct lysc_node *next = NULL, *dfs_stop;
6559 enum lyxp_node_type next_type = 0;
6560
6561 if (!*iter_type) {
6562 /* first returned node */
6563 return moveto_axis_scnode_next_first(iter, iter_type, iter_mod, iter_mod_idx, scnode, node_type, axis, set,
6564 getnext_opts);
6565 }
6566
6567 switch (axis) {
6568 case LYXP_AXIS_PARENT:
6569 case LYXP_AXIS_SELF:
6570 /* parent/self was returned before */
6571 break;
6572
6573 case LYXP_AXIS_ANCESTOR_OR_SELF:
6574 if ((*iter == scnode) && (*iter_type == node_type)) {
6575 /* fake first ancestor, we returned self before */
6576 *iter = NULL;
6577 *iter_type = 0;
6578 return moveto_axis_scnode_next_first(iter, iter_type, iter_mod, iter_mod_idx, scnode, node_type,
6579 LYXP_AXIS_ANCESTOR, set, getnext_opts);
6580 } /* else continue ancestor */
6581
6582 /* fallthrough */
6583 case LYXP_AXIS_ANCESTOR:
6584 if (*iter_type == LYXP_NODE_ELEM) {
6585 next = lysc_data_parent(*iter);
6586 next_type = next ? LYXP_NODE_ELEM : set->root_type;
6587 } /* else no ancestor */
6588 break;
6589
6590 case LYXP_AXIS_DESCENDANT_OR_SELF:
6591 if ((*iter == scnode) && (*iter_type == node_type)) {
6592 /* fake first descendant, we returned self before */
6593 *iter = NULL;
6594 *iter_type = 0;
6595 return moveto_axis_scnode_next_first(iter, iter_type, iter_mod, iter_mod_idx, scnode, node_type,
6596 LYXP_AXIS_DESCENDANT, set, getnext_opts);
6597 } /* else DFS until context node */
6598 dfs_stop = scnode;
6599
6600 /* fallthrough */
6601 case LYXP_AXIS_DESCENDANT:
6602 if (axis == LYXP_AXIS_DESCENDANT) {
6603 /* DFS until the context node */
6604 dfs_stop = scnode;
6605 }
6606
6607 /* fallthrough */
6608 case LYXP_AXIS_PRECEDING:
6609 if (axis == LYXP_AXIS_PRECEDING) {
6610 /* DFS until the previous sibling */
6611 dfs_stop = moveto_axis_scnode_preceding_sibling(scnode, getnext_opts);
6612 assert(dfs_stop);
6613
6614 if (*iter == dfs_stop) {
6615 /* we are done */
6616 break;
6617 }
6618 }
6619
6620 /* fallthrough */
6621 case LYXP_AXIS_FOLLOWING:
6622 if (axis == LYXP_AXIS_FOLLOWING) {
6623 /* DFS through the whole module */
6624 dfs_stop = NULL;
6625 }
6626
6627 /* nested nodes */
6628 assert(*iter);
6629 next = moveto_axis_scnode_next_dfs_forward(*iter, dfs_stop, getnext_opts);
6630 if (next) {
6631 next_type = LYXP_NODE_ELEM;
6632 break;
6633 } /* else get next top-level node just like a child */
6634
6635 /* fallthrough */
6636 case LYXP_AXIS_CHILD:
6637 case LYXP_AXIS_FOLLOWING_SIBLING:
6638 if (!*iter_mod) {
6639 /* nodes from a single module */
6640 if ((next = lys_getnext(*iter, lysc_data_parent(*iter), (*iter)->module->compiled, getnext_opts))) {
6641 next_type = LYXP_NODE_ELEM;
6642 break;
6643 }
6644
6645 assert(scnode);
Michal Vaskoa353cce2022-11-14 10:09:55 +01006646 if ((axis != LYXP_AXIS_CHILD) && !lysc_data_parent(scnode)) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02006647 /* iterating over top-level nodes, find next */
6648 while (lysc_data_parent(*iter)) {
6649 *iter = lysc_data_parent(*iter);
6650 }
6651 if ((next = lys_getnext(*iter, NULL, (*iter)->module->compiled, getnext_opts))) {
6652 next_type = LYXP_NODE_ELEM;
6653 break;
6654 }
6655 }
6656 }
6657
6658 while (*iter_mod) {
6659 /* module top-level nodes */
6660 if ((next = lys_getnext(*iter, NULL, (*iter_mod)->compiled, getnext_opts))) {
6661 next_type = LYXP_NODE_ELEM;
6662 break;
6663 }
6664
6665 /* get next module */
6666 while ((*iter_mod = ly_ctx_get_module_iter(set->ctx, iter_mod_idx))) {
6667 /* module may not be implemented or not compiled yet */
6668 if ((*iter_mod)->compiled) {
6669 break;
6670 }
6671 }
6672
6673 /* new module, start over */
6674 *iter = NULL;
6675 }
6676 break;
6677
6678 case LYXP_AXIS_PRECEDING_SIBLING:
6679 assert(*iter);
6680
6681 /* next parent sibling until scnode */
6682 next = lys_getnext(*iter, lysc_data_parent(*iter), (*iter)->module->compiled, getnext_opts);
6683 if (next == scnode) {
6684 /* no previous sibling */
6685 next = NULL;
6686 }
6687 next_type = next ? LYXP_NODE_ELEM : 0;
6688 break;
6689
6690 case LYXP_AXIS_ATTRIBUTE:
6691 /* unreachable */
6692 assert(0);
6693 LOGINT(set->ctx);
6694 break;
6695 }
6696
6697 *iter = next;
6698 *iter_type = next_type;
6699 return next_type ? LY_SUCCESS : LY_ENOTFOUND;
6700}
6701
6702/**
Michal Vaskod3678892020-05-21 10:06:58 +02006703 * @brief Move context @p set to a schema node. Result is LYXP_SET_SCNODE_SET (or LYXP_SET_EMPTY).
6704 *
6705 * @param[in,out] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006706 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02006707 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko49fec8e2022-05-24 10:28:33 +02006708 * @param[in] axis Axis to search on.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006709 * @param[in] options XPath options.
6710 * @return LY_ERR
6711 */
6712static LY_ERR
Michal Vasko49fec8e2022-05-24 10:28:33 +02006713moveto_scnode(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, enum lyxp_axis axis,
6714 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006715{
Radek Krejci857189e2020-09-01 13:26:36 +02006716 ly_bool temp_ctx = 0;
Michal Vasko49fec8e2022-05-24 10:28:33 +02006717 uint32_t getnext_opts, orig_used, i, mod_idx, idx;
stewegd8e2fc92023-05-31 09:52:56 +02006718 const struct lys_module *mod = NULL;
Michal Vasko49fec8e2022-05-24 10:28:33 +02006719 const struct lysc_node *iter;
6720 enum lyxp_node_type iter_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006721
aPiecek8b0cc152021-05-31 16:40:31 +02006722 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006723 return LY_SUCCESS;
6724 }
6725
6726 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006727 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006728 return LY_EVALID;
6729 }
6730
Michal Vaskocafad9d2019-11-07 15:20:03 +01006731 /* getnext opts */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01006732 getnext_opts = 0;
Michal Vaskocafad9d2019-11-07 15:20:03 +01006733 if (options & LYXP_SCNODE_OUTPUT) {
6734 getnext_opts |= LYS_GETNEXT_OUTPUT;
6735 }
Michal Vaskoe482bb92023-02-01 11:41:41 +01006736 if (options & LYXP_SCNODE_SCHEMAMOUNT) {
6737 getnext_opts |= LYS_GETNEXT_WITHSCHEMAMOUNT;
6738 }
Michal Vaskocafad9d2019-11-07 15:20:03 +01006739
Michal Vasko03ff5a72019-09-11 13:49:33 +02006740 orig_used = set->used;
6741 for (i = 0; i < orig_used; ++i) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02006742 /* update in_ctx first */
6743 if (moveto_axis_scnode_next_in_ctx(&set->val.scnodes[i].in_ctx, axis)) {
6744 /* not usable, skip */
6745 continue;
6746 }
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006747
Michal Vasko49fec8e2022-05-24 10:28:33 +02006748 iter = NULL;
6749 iter_type = 0;
6750 while (!moveto_axis_scnode_next(&iter, &iter_type, &mod, &mod_idx, set->val.scnodes[i].scnode,
6751 set->val.scnodes[i].type, axis, set, getnext_opts)) {
6752 if (moveto_scnode_check(iter, NULL, set, ncname, moveto_mod)) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006753 continue;
6754 }
6755
Michal Vasko49fec8e2022-05-24 10:28:33 +02006756 /* insert */
Michal Vaskoe4a6d012023-05-22 14:34:52 +02006757 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, iter_type, axis, &idx));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006758
Michal Vasko49fec8e2022-05-24 10:28:33 +02006759 /* we need to prevent these nodes from being considered in this moveto */
6760 if ((idx < orig_used) && (idx > i)) {
6761 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX;
6762 temp_ctx = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006763 }
6764 }
Michal Vasko66330fc2022-11-21 15:52:24 +01006765
6766 if (moveto_mod && ncname && ((axis == LYXP_AXIS_DESCENDANT) || (axis == LYXP_AXIS_CHILD)) &&
6767 (set->val.scnodes[i].type == LYXP_NODE_ELEM) && !ly_nested_ext_schema(NULL, set->val.scnodes[i].scnode,
6768 moveto_mod->name, strlen(moveto_mod->name), LY_VALUE_JSON, NULL, ncname, strlen(ncname), &iter, NULL)) {
6769 /* there is a matching node from an extension, use it */
Michal Vaskoe4a6d012023-05-22 14:34:52 +02006770 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, axis, &idx));
Michal Vasko66330fc2022-11-21 15:52:24 +01006771 if ((idx < orig_used) && (idx > i)) {
6772 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX;
6773 temp_ctx = 1;
6774 }
6775 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006776 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006777
6778 /* correct temporary in_ctx values */
6779 if (temp_ctx) {
6780 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006781 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_NEW_CTX) {
6782 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006783 }
6784 }
6785 }
6786
6787 return LY_SUCCESS;
6788}
6789
6790/**
Michal Vasko49fec8e2022-05-24 10:28:33 +02006791 * @brief Move context @p set to a child node and all its descendants. Result is LYXP_SET_NODE_SET.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006792 * Context position aware.
6793 *
6794 * @param[in] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006795 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02006796 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vaskocdad7122020-11-09 21:04:44 +01006797 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006798 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6799 */
6800static LY_ERR
Michal Vasko49fec8e2022-05-24 10:28:33 +02006801moveto_node_alldesc_child(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006802{
6803 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006804 const struct lyd_node *next, *elem, *start;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006805 struct lyxp_set ret_set;
6806 LY_ERR rc;
6807
aPiecek8b0cc152021-05-31 16:40:31 +02006808 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006809 return LY_SUCCESS;
6810 }
6811
6812 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006813 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006814 return LY_EVALID;
6815 }
6816
Michal Vasko9f96a052020-03-10 09:41:45 +01006817 /* replace the original nodes (and throws away all text and meta nodes, root is replaced by a child) */
Michal Vasko49fec8e2022-05-24 10:28:33 +02006818 rc = xpath_pi_node(set, LYXP_AXIS_CHILD, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006819 LY_CHECK_RET(rc);
6820
Michal Vasko6346ece2019-09-24 13:12:53 +02006821 /* this loop traverses all the nodes in the set and adds/keeps only those that match qname */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006822 set_init(&ret_set, set);
6823 for (i = 0; i < set->used; ++i) {
6824
6825 /* TREE DFS */
6826 start = set->val.nodes[i].node;
6827 for (elem = next = start; elem; elem = next) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02006828 rc = moveto_node_check(elem, LYXP_NODE_ELEM, set, ncname, moveto_mod, options);
Michal Vasko6346ece2019-09-24 13:12:53 +02006829 if (!rc) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006830 /* add matching node into result set */
6831 set_insert_node(&ret_set, elem, 0, LYXP_NODE_ELEM, ret_set.used);
6832 if (set_dup_node_check(set, elem, LYXP_NODE_ELEM, i)) {
6833 /* the node is a duplicate, we'll process it later in the set */
6834 goto skip_children;
6835 }
Michal Vasko6346ece2019-09-24 13:12:53 +02006836 } else if (rc == LY_EINCOMPLETE) {
Michal Vasko6346ece2019-09-24 13:12:53 +02006837 return rc;
6838 } else if (rc == LY_EINVAL) {
6839 goto skip_children;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006840 }
6841
6842 /* TREE DFS NEXT ELEM */
6843 /* select element for the next run - children first */
Radek Krejcia1c1e542020-09-29 16:06:52 +02006844 next = lyd_child(elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006845 if (!next) {
6846skip_children:
6847 /* no children, so try siblings, but only if it's not the start,
6848 * that is considered to be the root and it's siblings are not traversed */
6849 if (elem != start) {
6850 next = elem->next;
6851 } else {
6852 break;
6853 }
6854 }
6855 while (!next) {
6856 /* no siblings, go back through the parents */
Michal Vasko9e685082021-01-29 14:49:09 +01006857 if (lyd_parent(elem) == start) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006858 /* we are done, no next element to process */
6859 break;
6860 }
6861 /* parent is already processed, go to its sibling */
Michal Vasko9e685082021-01-29 14:49:09 +01006862 elem = lyd_parent(elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006863 next = elem->next;
6864 }
6865 }
6866 }
6867
6868 /* make the temporary set the current one */
6869 ret_set.ctx_pos = set->ctx_pos;
6870 ret_set.ctx_size = set->ctx_size;
Michal Vaskod3678892020-05-21 10:06:58 +02006871 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006872 memcpy(set, &ret_set, sizeof *set);
Michal Vasko306e2832022-07-25 09:15:17 +02006873 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006874
6875 return LY_SUCCESS;
6876}
6877
6878/**
Michal Vasko49fec8e2022-05-24 10:28:33 +02006879 * @brief Move context @p set to a child schema node and all its descendants starting from a node.
6880 * Result is LYXP_SET_NODE_SET.
6881 *
6882 * @param[in] set Set to use.
6883 * @param[in] start Start node whose subtree to add.
6884 * @param[in] start_idx Index of @p start in @p set.
6885 * @param[in] moveto_mod Matching node module, NULL for no prefix.
6886 * @param[in] ncname Matching node name in the dictionary, NULL for any.
6887 * @param[in] options XPath options.
6888 * @return LY_ERR value.
6889 */
6890static LY_ERR
6891moveto_scnode_dfs(struct lyxp_set *set, const struct lysc_node *start, uint32_t start_idx,
6892 const struct lys_module *moveto_mod, const char *ncname, uint32_t options)
6893{
6894 const struct lysc_node *next, *elem;
6895 uint32_t idx;
6896 LY_ERR rc;
6897
6898 /* TREE DFS */
6899 for (elem = next = start; elem; elem = next) {
6900 if ((elem == start) || (elem->nodetype & (LYS_CHOICE | LYS_CASE))) {
6901 /* schema-only nodes, skip root */
6902 goto next_iter;
6903 }
6904
6905 rc = moveto_scnode_check(elem, start, set, ncname, moveto_mod);
6906 if (!rc) {
6907 if (lyxp_set_scnode_contains(set, elem, LYXP_NODE_ELEM, start_idx, &idx)) {
6908 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
6909 if (idx > start_idx) {
6910 /* we will process it later in the set */
6911 goto skip_children;
6912 }
6913 } else {
Michal Vaskoe4a6d012023-05-22 14:34:52 +02006914 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, elem, LYXP_NODE_ELEM, LYXP_AXIS_DESCENDANT, NULL));
Michal Vasko49fec8e2022-05-24 10:28:33 +02006915 }
6916 } else if (rc == LY_EINVAL) {
6917 goto skip_children;
6918 }
6919
6920next_iter:
6921 /* TREE DFS NEXT ELEM */
6922 /* select element for the next run - children first */
6923 next = lysc_node_child(elem);
6924 if (next && (next->nodetype == LYS_INPUT) && (options & LYXP_SCNODE_OUTPUT)) {
6925 next = next->next;
6926 } else if (next && (next->nodetype == LYS_OUTPUT) && !(options & LYXP_SCNODE_OUTPUT)) {
6927 next = next->next;
6928 }
6929 if (!next) {
6930skip_children:
6931 /* no children, so try siblings, but only if it's not the start,
6932 * that is considered to be the root and it's siblings are not traversed */
6933 if (elem != start) {
6934 next = elem->next;
6935 } else {
6936 break;
6937 }
6938 }
6939 while (!next) {
6940 /* no siblings, go back through the parents */
6941 if (elem->parent == start) {
6942 /* we are done, no next element to process */
6943 break;
6944 }
6945 /* parent is already processed, go to its sibling */
6946 elem = elem->parent;
6947 next = elem->next;
6948 }
6949 }
6950
6951 return LY_SUCCESS;
6952}
6953
6954/**
6955 * @brief Move context @p set to a child schema node and all its descendants. Result is LYXP_SET_NODE_SET.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006956 *
6957 * @param[in] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006958 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02006959 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006960 * @param[in] options XPath options.
Michal Vasko49fec8e2022-05-24 10:28:33 +02006961 * @return LY_ERR value.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006962 */
6963static LY_ERR
Michal Vasko49fec8e2022-05-24 10:28:33 +02006964moveto_scnode_alldesc_child(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006965{
Michal Vasko49fec8e2022-05-24 10:28:33 +02006966 uint32_t i, orig_used, mod_idx;
6967 const struct lys_module *mod;
6968 const struct lysc_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006969
aPiecek8b0cc152021-05-31 16:40:31 +02006970 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006971 return LY_SUCCESS;
6972 }
6973
6974 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006975 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006976 return LY_EVALID;
6977 }
6978
Michal Vasko03ff5a72019-09-11 13:49:33 +02006979 orig_used = set->used;
6980 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006981 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
6982 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006983 continue;
6984 }
6985
6986 /* remember context node */
Radek Krejcif13b87b2020-12-01 22:02:17 +01006987 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vaskoec4df482019-12-16 10:02:18 +01006988 } else {
Michal Vasko1a09b212021-05-06 13:00:10 +02006989 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006990 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006991
Michal Vasko49fec8e2022-05-24 10:28:33 +02006992 if ((set->val.scnodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.scnodes[i].type == LYXP_NODE_ROOT)) {
6993 /* traverse all top-level nodes in all the modules */
6994 mod_idx = 0;
6995 while ((mod = ly_ctx_get_module_iter(set->ctx, &mod_idx))) {
6996 /* module may not be implemented or not compiled yet */
6997 if (!mod->compiled) {
6998 continue;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006999 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02007000
Michal Vasko49fec8e2022-05-24 10:28:33 +02007001 root = NULL;
7002 /* no getnext opts needed */
7003 while ((root = lys_getnext(root, NULL, mod->compiled, 0))) {
7004 LY_CHECK_RET(moveto_scnode_dfs(set, root, i, moveto_mod, ncname, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007005 }
7006 }
Michal Vasko49fec8e2022-05-24 10:28:33 +02007007
7008 } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
7009 /* add all the descendants recursively */
7010 LY_CHECK_RET(moveto_scnode_dfs(set, set->val.scnodes[i].scnode, i, moveto_mod, ncname, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007011 }
7012 }
7013
7014 return LY_SUCCESS;
7015}
7016
7017/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02007018 * @brief Move context @p set to an attribute. Result is LYXP_SET_NODE_SET.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007019 * Indirectly context position aware.
7020 *
7021 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02007022 * @param[in] mod Matching metadata module, NULL for any.
7023 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
aPiecek8b0cc152021-05-31 16:40:31 +02007024 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007025 * @return LY_ERR
7026 */
7027static LY_ERR
aPiecek8b0cc152021-05-31 16:40:31 +02007028moveto_attr(struct lyxp_set *set, const struct lys_module *mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007029{
Michal Vasko9f96a052020-03-10 09:41:45 +01007030 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007031
aPiecek8b0cc152021-05-31 16:40:31 +02007032 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007033 return LY_SUCCESS;
7034 }
7035
7036 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01007037 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007038 return LY_EVALID;
7039 }
7040
Radek Krejci1deb5be2020-08-26 16:43:36 +02007041 for (uint32_t i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02007042 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007043
7044 /* only attributes of an elem (not dummy) can be in the result, skip all the rest;
7045 * our attributes are always qualified */
Michal Vasko5c4e5892019-11-14 12:31:38 +01007046 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01007047 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007048
7049 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02007050 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007051 continue;
7052 }
7053
Michal Vaskod3678892020-05-21 10:06:58 +02007054 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007055 /* match */
7056 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01007057 set->val.meta[i].meta = sub;
7058 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007059 /* pos does not change */
7060 replaced = 1;
7061 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01007062 set_insert_node(set, (struct lyd_node *)sub, set->val.nodes[i].pos, LYXP_NODE_META, i + 1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007063 }
7064 ++i;
7065 }
7066 }
7067 }
7068
7069 if (!replaced) {
7070 /* no match */
7071 set_remove_node(set, i);
7072 }
7073 }
7074
7075 return LY_SUCCESS;
7076}
7077
7078/**
7079 * @brief Move context @p set1 to union with @p set2. @p set2 is emptied afterwards.
Michal Vasko61ac2f62020-05-25 12:39:51 +02007080 * Result is LYXP_SET_NODE_SET. Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007081 *
7082 * @param[in,out] set1 Set to use for the result.
7083 * @param[in] set2 Set that is copied to @p set1.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007084 * @return LY_ERR
7085 */
7086static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007087moveto_union(struct lyxp_set *set1, struct lyxp_set *set2)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007088{
7089 LY_ERR rc;
7090
Michal Vaskod3678892020-05-21 10:06:58 +02007091 if ((set1->type != LYXP_SET_NODE_SET) || (set2->type != LYXP_SET_NODE_SET)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01007092 LOGVAL(set1->ctx, LY_VCODE_XP_INOP_2, "union", print_set_type(set1), print_set_type(set2));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007093 return LY_EVALID;
7094 }
7095
7096 /* set2 is empty or both set1 and set2 */
Michal Vaskod3678892020-05-21 10:06:58 +02007097 if (!set2->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007098 return LY_SUCCESS;
7099 }
7100
Michal Vaskod3678892020-05-21 10:06:58 +02007101 if (!set1->used) {
aPiecekadc1e4f2021-10-07 11:15:12 +02007102 /* release hidden allocated data (lyxp_set.size) */
7103 lyxp_set_free_content(set1);
7104 /* direct copying of the entire structure */
Michal Vasko03ff5a72019-09-11 13:49:33 +02007105 memcpy(set1, set2, sizeof *set1);
7106 /* dynamic memory belongs to set1 now, do not free */
Michal Vaskod3678892020-05-21 10:06:58 +02007107 memset(set2, 0, sizeof *set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007108 return LY_SUCCESS;
7109 }
7110
7111 /* we assume sets are sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007112 assert(!set_sort(set1) && !set_sort(set2));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007113
7114 /* sort, remove duplicates */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007115 rc = set_sorted_merge(set1, set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007116 LY_CHECK_RET(rc);
7117
7118 /* final set must be sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007119 assert(!set_sort(set1));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007120
7121 return LY_SUCCESS;
7122}
7123
7124/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02007125 * @brief Move context @p set to an attribute in any of the descendants. Result is LYXP_SET_NODE_SET.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007126 * Context position aware.
7127 *
7128 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02007129 * @param[in] mod Matching metadata module, NULL for any.
7130 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
Michal Vaskocdad7122020-11-09 21:04:44 +01007131 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007132 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7133 */
7134static int
Michal Vaskocdad7122020-11-09 21:04:44 +01007135moveto_attr_alldesc(struct lyxp_set *set, const struct lys_module *mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007136{
Michal Vasko9f96a052020-03-10 09:41:45 +01007137 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007138 struct lyxp_set *set_all_desc = NULL;
7139 LY_ERR rc;
7140
aPiecek8b0cc152021-05-31 16:40:31 +02007141 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007142 return LY_SUCCESS;
7143 }
7144
7145 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01007146 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007147 return LY_EVALID;
7148 }
7149
Michal Vasko03ff5a72019-09-11 13:49:33 +02007150 /* can be optimized similarly to moveto_node_alldesc() and save considerable amount of memory,
7151 * but it likely won't be used much, so it's a waste of time */
7152 /* copy the context */
7153 set_all_desc = set_copy(set);
7154 /* get all descendant nodes (the original context nodes are removed) */
Michal Vasko49fec8e2022-05-24 10:28:33 +02007155 rc = moveto_node_alldesc_child(set_all_desc, NULL, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007156 if (rc != LY_SUCCESS) {
7157 lyxp_set_free(set_all_desc);
7158 return rc;
7159 }
7160 /* prepend the original context nodes */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007161 rc = moveto_union(set, set_all_desc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007162 if (rc != LY_SUCCESS) {
7163 lyxp_set_free(set_all_desc);
7164 return rc;
7165 }
7166 lyxp_set_free(set_all_desc);
7167
Radek Krejci1deb5be2020-08-26 16:43:36 +02007168 for (uint32_t i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02007169 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007170
7171 /* only attributes of an elem can be in the result, skip all the rest,
7172 * we have all attributes qualified in lyd tree */
7173 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01007174 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007175 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02007176 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007177 continue;
7178 }
7179
Michal Vaskod3678892020-05-21 10:06:58 +02007180 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007181 /* match */
7182 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01007183 set->val.meta[i].meta = sub;
7184 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007185 /* pos does not change */
7186 replaced = 1;
7187 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01007188 set_insert_node(set, (struct lyd_node *)sub, set->val.meta[i].pos, LYXP_NODE_META, i + 1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007189 }
7190 ++i;
7191 }
7192 }
7193 }
7194
7195 if (!replaced) {
7196 /* no match */
7197 set_remove_node(set, i);
7198 }
7199 }
7200
7201 return LY_SUCCESS;
7202}
7203
7204/**
Michal Vasko8abcecc2022-07-28 09:55:01 +02007205 * @brief Move context @p set1 single item to the result of a comparison.
7206 *
7207 * @param[in] set1 First set with the item to compare.
7208 * @param[in] idx1 Index of the item in @p set1.
7209 * @param[in] set2 Second set.
7210 * @param[in] op Comparison operator to process.
7211 * @param[in] switch_operands Whether to switch sets as operands; whether it is `set1 op set2` or `set2 op set1`.
7212 * @param[out] result Result of the comparison.
7213 * @return LY_ERR value.
7214 */
7215static LY_ERR
7216moveto_op_comp_item(const struct lyxp_set *set1, uint32_t idx1, struct lyxp_set *set2, const char *op,
7217 ly_bool switch_operands, ly_bool *result)
7218{
7219 struct lyxp_set tmp1 = {0};
7220 LY_ERR rc = LY_SUCCESS;
7221
7222 assert(set1->type == LYXP_SET_NODE_SET);
7223
7224 /* cast set1 */
7225 switch (set2->type) {
7226 case LYXP_SET_NUMBER:
7227 rc = set_comp_cast(&tmp1, set1, LYXP_SET_NUMBER, idx1);
7228 break;
7229 case LYXP_SET_BOOLEAN:
7230 rc = set_comp_cast(&tmp1, set1, LYXP_SET_BOOLEAN, idx1);
7231 break;
7232 default:
7233 rc = set_comp_cast(&tmp1, set1, LYXP_SET_STRING, idx1);
7234 break;
7235 }
7236 LY_CHECK_GOTO(rc, cleanup);
7237
7238 /* canonize set2 */
7239 LY_CHECK_GOTO(rc = set_comp_canonize(set2, &set1->val.nodes[idx1]), cleanup);
7240
7241 /* compare recursively and store the result */
7242 if (switch_operands) {
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007243 LY_CHECK_GOTO(rc = moveto_op_comp(set2, &tmp1, op, result), cleanup);
Michal Vasko8abcecc2022-07-28 09:55:01 +02007244 } else {
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007245 LY_CHECK_GOTO(rc = moveto_op_comp(&tmp1, set2, op, result), cleanup);
Michal Vasko8abcecc2022-07-28 09:55:01 +02007246 }
7247
7248cleanup:
7249 lyxp_set_free_content(&tmp1);
7250 return rc;
7251}
7252
7253/**
7254 * @brief Move context @p set1 to the result of a comparison. Handles '=', '!=', '<=', '<', '>=', or '>'.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007255 * Result is LYXP_SET_BOOLEAN. Indirectly context position aware.
7256 *
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007257 * @param[in] set1 Set acting as the first operand for @p op.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007258 * @param[in] set2 Set acting as the second operand for @p op.
7259 * @param[in] op Comparison operator to process.
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007260 * @param[out] result Result of the comparison.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007261 * @return LY_ERR
7262 */
7263static LY_ERR
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007264moveto_op_comp(struct lyxp_set *set1, struct lyxp_set *set2, const char *op, ly_bool *result)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007265{
7266 /*
7267 * NODE SET + NODE SET = NODE SET + STRING /(1 NODE SET) 2 STRING
7268 * NODE SET + STRING = STRING + STRING /1 STRING (2 STRING)
7269 * NODE SET + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
7270 * NODE SET + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
7271 * STRING + NODE SET = STRING + STRING /(1 STRING) 2 STRING
7272 * NUMBER + NODE SET = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
7273 * BOOLEAN + NODE SET = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
7274 *
7275 * '=' or '!='
7276 * BOOLEAN + BOOLEAN
7277 * BOOLEAN + STRING = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
7278 * BOOLEAN + NUMBER = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
7279 * STRING + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
7280 * NUMBER + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
7281 * NUMBER + NUMBER
7282 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
7283 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
7284 * STRING + STRING
7285 *
7286 * '<=', '<', '>=', '>'
7287 * NUMBER + NUMBER
7288 * BOOLEAN + BOOLEAN = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
7289 * BOOLEAN + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
7290 * BOOLEAN + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
7291 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
7292 * STRING + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
7293 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
7294 * NUMBER + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
7295 * STRING + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
7296 */
Michal Vasko8abcecc2022-07-28 09:55:01 +02007297 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007298 LY_ERR rc;
7299
Michal Vasko03ff5a72019-09-11 13:49:33 +02007300 /* iterative evaluation with node-sets */
7301 if ((set1->type == LYXP_SET_NODE_SET) || (set2->type == LYXP_SET_NODE_SET)) {
7302 if (set1->type == LYXP_SET_NODE_SET) {
7303 for (i = 0; i < set1->used; ++i) {
Michal Vasko8abcecc2022-07-28 09:55:01 +02007304 /* evaluate for the single item */
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007305 LY_CHECK_RET(moveto_op_comp_item(set1, i, set2, op, 0, result));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007306
7307 /* lazy evaluation until true */
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007308 if (*result) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007309 return LY_SUCCESS;
7310 }
7311 }
7312 } else {
7313 for (i = 0; i < set2->used; ++i) {
Michal Vasko8abcecc2022-07-28 09:55:01 +02007314 /* evaluate for the single item */
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007315 LY_CHECK_RET(moveto_op_comp_item(set2, i, set1, op, 1, result));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007316
7317 /* lazy evaluation until true */
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007318 if (*result) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007319 return LY_SUCCESS;
7320 }
7321 }
7322 }
7323
Michal Vasko8abcecc2022-07-28 09:55:01 +02007324 /* false for all the nodes */
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007325 *result = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007326 return LY_SUCCESS;
7327 }
7328
7329 /* first convert properly */
7330 if ((op[0] == '=') || (op[0] == '!')) {
7331 if ((set1->type == LYXP_SET_BOOLEAN) || (set2->type == LYXP_SET_BOOLEAN)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007332 lyxp_set_cast(set1, LYXP_SET_BOOLEAN);
7333 lyxp_set_cast(set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007334 } else if ((set1->type == LYXP_SET_NUMBER) || (set2->type == LYXP_SET_NUMBER)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007335 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007336 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007337 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007338 LY_CHECK_RET(rc);
7339 } /* else we have 2 strings */
7340 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007341 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007342 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007343 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007344 LY_CHECK_RET(rc);
7345 }
7346
7347 assert(set1->type == set2->type);
7348
7349 /* compute result */
7350 if (op[0] == '=') {
7351 if (set1->type == LYXP_SET_BOOLEAN) {
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007352 *result = (set1->val.bln == set2->val.bln);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007353 } else if (set1->type == LYXP_SET_NUMBER) {
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007354 *result = (set1->val.num == set2->val.num);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007355 } else {
7356 assert(set1->type == LYXP_SET_STRING);
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007357 *result = strcmp(set1->val.str, set2->val.str) ? 0 : 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007358 }
7359 } else if (op[0] == '!') {
7360 if (set1->type == LYXP_SET_BOOLEAN) {
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007361 *result = (set1->val.bln != set2->val.bln);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007362 } else if (set1->type == LYXP_SET_NUMBER) {
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007363 *result = (set1->val.num != set2->val.num);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007364 } else {
7365 assert(set1->type == LYXP_SET_STRING);
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007366 *result = strcmp(set1->val.str, set2->val.str) ? 1 : 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007367 }
7368 } else {
7369 assert(set1->type == LYXP_SET_NUMBER);
7370 if (op[0] == '<') {
7371 if (op[1] == '=') {
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007372 *result = (set1->val.num <= set2->val.num);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007373 } else {
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007374 *result = (set1->val.num < set2->val.num);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007375 }
7376 } else {
7377 if (op[1] == '=') {
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007378 *result = (set1->val.num >= set2->val.num);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007379 } else {
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007380 *result = (set1->val.num > set2->val.num);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007381 }
7382 }
7383 }
7384
Michal Vasko03ff5a72019-09-11 13:49:33 +02007385 return LY_SUCCESS;
7386}
7387
7388/**
7389 * @brief Move context @p set to the result of a basic operation. Handles '+', '-', unary '-', '*', 'div',
7390 * or 'mod'. Result is LYXP_SET_NUMBER. Indirectly context position aware.
7391 *
7392 * @param[in,out] set1 Set to use for the result.
7393 * @param[in] set2 Set acting as the second operand for @p op.
7394 * @param[in] op Operator to process.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007395 * @return LY_ERR
7396 */
7397static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007398moveto_op_math(struct lyxp_set *set1, struct lyxp_set *set2, const char *op)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007399{
7400 LY_ERR rc;
7401
7402 /* unary '-' */
7403 if (!set2 && (op[0] == '-')) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007404 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007405 LY_CHECK_RET(rc);
7406 set1->val.num *= -1;
7407 lyxp_set_free(set2);
7408 return LY_SUCCESS;
7409 }
7410
7411 assert(set1 && set2);
7412
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007413 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007414 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007415 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007416 LY_CHECK_RET(rc);
7417
7418 switch (op[0]) {
7419 /* '+' */
7420 case '+':
7421 set1->val.num += set2->val.num;
7422 break;
7423
7424 /* '-' */
7425 case '-':
7426 set1->val.num -= set2->val.num;
7427 break;
7428
7429 /* '*' */
7430 case '*':
7431 set1->val.num *= set2->val.num;
7432 break;
7433
7434 /* 'div' */
7435 case 'd':
7436 set1->val.num /= set2->val.num;
7437 break;
7438
7439 /* 'mod' */
7440 case 'm':
7441 set1->val.num = ((long long)set1->val.num) % ((long long)set2->val.num);
7442 break;
7443
7444 default:
7445 LOGINT_RET(set1->ctx);
7446 }
7447
7448 return LY_SUCCESS;
7449}
7450
Michal Vasko03ff5a72019-09-11 13:49:33 +02007451/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02007452 * @brief Evaluate Predicate. Logs directly on error.
7453 *
Michal Vaskod3678892020-05-21 10:06:58 +02007454 * [9] Predicate ::= '[' Expr ']'
Michal Vasko03ff5a72019-09-11 13:49:33 +02007455 *
7456 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007457 * @param[in] tok_idx Position in the expression @p exp.
aPiecek8b0cc152021-05-31 16:40:31 +02007458 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007459 * @param[in] options XPath options.
Michal Vasko49fec8e2022-05-24 10:28:33 +02007460 * @param[in] axis Axis to search on.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007461 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7462 */
7463static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02007464eval_predicate(const struct lyxp_expr *exp, uint32_t *tok_idx, struct lyxp_set *set, uint32_t options, enum lyxp_axis axis)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007465{
7466 LY_ERR rc;
Michal Vaskodd528af2022-08-08 14:35:07 +02007467 uint32_t i, orig_exp, orig_pos, orig_size;
Michal Vasko5c4e5892019-11-14 12:31:38 +01007468 int32_t pred_in_ctx;
Michal Vasko88a9e802022-05-24 10:50:28 +02007469 ly_bool reverse_axis = 0;
aPiecekfff4dca2021-10-07 10:59:53 +02007470 struct lyxp_set set2 = {0};
Michal Vasko03ff5a72019-09-11 13:49:33 +02007471
7472 /* '[' */
aPiecek8b0cc152021-05-31 16:40:31 +02007473 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02007474 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007475 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007476
aPiecek8b0cc152021-05-31 16:40:31 +02007477 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007478only_parse:
aPiecek8b0cc152021-05-31 16:40:31 +02007479 rc = eval_expr_select(exp, tok_idx, 0, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007480 LY_CHECK_RET(rc);
7481 } else if (set->type == LYXP_SET_NODE_SET) {
7482 /* we (possibly) need the set sorted, it can affect the result (if the predicate result is a number) */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007483 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007484
7485 /* empty set, nothing to evaluate */
7486 if (!set->used) {
7487 goto only_parse;
7488 }
7489
Michal Vasko49fec8e2022-05-24 10:28:33 +02007490 /* decide forward or reverse axis */
7491 switch (axis) {
7492 case LYXP_AXIS_ANCESTOR:
7493 case LYXP_AXIS_ANCESTOR_OR_SELF:
7494 case LYXP_AXIS_PRECEDING:
7495 case LYXP_AXIS_PRECEDING_SIBLING:
7496 reverse_axis = 1;
7497 break;
7498 case LYXP_AXIS_DESCENDANT:
7499 case LYXP_AXIS_DESCENDANT_OR_SELF:
7500 case LYXP_AXIS_FOLLOWING:
7501 case LYXP_AXIS_FOLLOWING_SIBLING:
7502 case LYXP_AXIS_PARENT:
7503 case LYXP_AXIS_CHILD:
7504 case LYXP_AXIS_SELF:
7505 case LYXP_AXIS_ATTRIBUTE:
7506 reverse_axis = 0;
7507 break;
7508 }
7509
Michal Vasko004d3152020-06-11 19:59:22 +02007510 orig_exp = *tok_idx;
Michal Vasko49fec8e2022-05-24 10:28:33 +02007511 orig_pos = reverse_axis ? set->used + 1 : 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007512 orig_size = set->used;
Michal Vasko39dbf352020-05-21 10:08:59 +02007513 for (i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007514 set_init(&set2, set);
7515 set_insert_node(&set2, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, 0);
Michal Vasko49fec8e2022-05-24 10:28:33 +02007516
7517 /* remember the node context position for position() and context size for last() */
7518 orig_pos += reverse_axis ? -1 : 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007519
7520 set2.ctx_pos = orig_pos;
7521 set2.ctx_size = orig_size;
Michal Vasko004d3152020-06-11 19:59:22 +02007522 *tok_idx = orig_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007523
Michal Vasko004d3152020-06-11 19:59:22 +02007524 rc = eval_expr_select(exp, tok_idx, 0, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007525 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02007526 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007527 return rc;
7528 }
7529
Michal Vasko49fec8e2022-05-24 10:28:33 +02007530 /* number is a proximity position */
Michal Vasko03ff5a72019-09-11 13:49:33 +02007531 if (set2.type == LYXP_SET_NUMBER) {
7532 if ((long long)set2.val.num == orig_pos) {
7533 set2.val.num = 1;
7534 } else {
7535 set2.val.num = 0;
7536 }
7537 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007538 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007539
7540 /* predicate satisfied or not? */
Michal Vasko004d3152020-06-11 19:59:22 +02007541 if (!set2.val.bln) {
Michal Vasko2caefc12019-11-14 16:07:56 +01007542 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007543 }
7544 }
Michal Vasko2caefc12019-11-14 16:07:56 +01007545 set_remove_nodes_none(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007546
7547 } else if (set->type == LYXP_SET_SCNODE_SET) {
7548 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01007549 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007550 /* there is a currently-valid node */
7551 break;
7552 }
7553 }
7554 /* empty set, nothing to evaluate */
7555 if (i == set->used) {
7556 goto only_parse;
7557 }
7558
Michal Vasko004d3152020-06-11 19:59:22 +02007559 orig_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007560
Michal Vasko03ff5a72019-09-11 13:49:33 +02007561 /* set special in_ctx to all the valid snodes */
7562 pred_in_ctx = set_scnode_new_in_ctx(set);
7563
7564 /* use the valid snodes one-by-one */
7565 for (i = 0; i < set->used; ++i) {
7566 if (set->val.scnodes[i].in_ctx != pred_in_ctx) {
7567 continue;
7568 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01007569 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007570
Michal Vasko004d3152020-06-11 19:59:22 +02007571 *tok_idx = orig_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007572
Michal Vasko004d3152020-06-11 19:59:22 +02007573 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007574 LY_CHECK_RET(rc);
7575
7576 set->val.scnodes[i].in_ctx = pred_in_ctx;
7577 }
7578
7579 /* restore the state as it was before the predicate */
7580 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01007581 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007582 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007583 } else if (set->val.scnodes[i].in_ctx == pred_in_ctx) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01007584 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007585 }
7586 }
7587
7588 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02007589 set2.type = LYXP_SET_NODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007590 set_fill_set(&set2, set);
7591
Michal Vasko004d3152020-06-11 19:59:22 +02007592 rc = eval_expr_select(exp, tok_idx, 0, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007593 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02007594 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007595 return rc;
7596 }
7597
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007598 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +02007599 if (!set2.val.bln) {
Michal Vaskod3678892020-05-21 10:06:58 +02007600 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007601 }
Michal Vaskod3678892020-05-21 10:06:58 +02007602 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007603 }
7604
7605 /* ']' */
Michal Vasko004d3152020-06-11 19:59:22 +02007606 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_BRACK2);
aPiecek8b0cc152021-05-31 16:40:31 +02007607 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02007608 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007609 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007610
7611 return LY_SUCCESS;
7612}
7613
7614/**
Michal Vaskod3678892020-05-21 10:06:58 +02007615 * @brief Evaluate Literal. Logs directly on error.
7616 *
7617 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007618 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02007619 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7620 */
7621static void
Michal Vaskodd528af2022-08-08 14:35:07 +02007622eval_literal(const struct lyxp_expr *exp, uint32_t *tok_idx, struct lyxp_set *set)
Michal Vaskod3678892020-05-21 10:06:58 +02007623{
7624 if (set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007625 if (exp->tok_len[*tok_idx] == 2) {
Michal Vaskod3678892020-05-21 10:06:58 +02007626 set_fill_string(set, "", 0);
7627 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007628 set_fill_string(set, &exp->expr[exp->tok_pos[*tok_idx] + 1], exp->tok_len[*tok_idx] - 2);
Michal Vaskod3678892020-05-21 10:06:58 +02007629 }
7630 }
7631 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02007632 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007633 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007634}
7635
7636/**
Michal Vasko3d969ff2022-07-29 15:02:08 +02007637 * @brief Check that a nametest in a predicate matches a key node.
7638 *
7639 * @param[in] nametest Nametest to check.
7640 * @param[in] len Length of @p nametest.
7641 * @param[in] ctx_scnode Found schema node as the context for the predicate.
7642 * @param[in] set Context set.
7643 * @param[in] key Expected key node.
7644 * @return LY_SUCCESS on success,
7645 * @return LY_ENOT if a predicate could not be compiled.
7646 * @return LY_ERR on any error.
7647 */
7648static LY_ERR
7649eval_name_test_try_compile_predicate_key(const char *nametest, uint32_t len, const struct lysc_node *ctx_scnode,
7650 const struct lyxp_set *set, const struct lysc_node *key)
7651{
7652 const struct lys_module *mod;
7653
7654 /* prefix (module) */
7655 LY_CHECK_RET(moveto_resolve_model(&nametest, &len, set, ctx_scnode, &mod));
7656 if (mod != key->module) {
7657 return LY_ENOT;
7658 }
7659
7660 /* node name */
7661 if (ly_strncmp(key->name, nametest, len)) {
7662 return LY_ENOT;
7663 }
7664
7665 return LY_SUCCESS;
7666}
7667
7668/**
7669 * @brief Append a simple predicate for the node.
7670 *
7671 * @param[in] exp Full parsed XPath expression.
7672 * @param[in] tok_idx Predicate start index in @p exp.
7673 * @param[in] end_tok_idx Predicate end index in @p exp.
7674 * @param[in] ctx_scnode Found schema node as the context for the predicate.
7675 * @param[in] set Context set.
7676 * @param[in] pred_node Node with the value referenced in the predicate.
7677 * @param[in,out] pred Predicate to append to.
7678 * @param[in,out] pred_len Length of @p pred, is updated.
7679 * @return LY_SUCCESS on success,
7680 * @return LY_ENOT if a predicate could not be compiled.
7681 * @return LY_ERR on any error.
7682 */
7683static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02007684eval_name_test_try_compile_predicate_append(const struct lyxp_expr *exp, uint32_t tok_idx, uint32_t end_tok_idx,
Michal Vasko3d969ff2022-07-29 15:02:08 +02007685 const struct lysc_node *ctx_scnode, const struct lyxp_set *set, const struct lysc_node *pred_node, char **pred,
7686 uint32_t *pred_len)
7687{
7688 LY_ERR rc = LY_SUCCESS;
7689 uint32_t i;
7690 const struct lyd_node *siblings;
7691 struct lyd_node *ctx_node;
Michal Vasko5fb92a22022-08-02 09:21:37 +02007692 const struct lysc_node *sparent, *cur_scnode;
Michal Vasko3d969ff2022-07-29 15:02:08 +02007693 struct lyxp_expr *val_exp = NULL;
7694 struct lyxp_set set2 = {0};
7695 char quot;
7696
7697 /* duplicate the value expression */
7698 LY_CHECK_GOTO(rc = lyxp_expr_dup(set->ctx, exp, tok_idx, end_tok_idx, &val_exp), cleanup);
7699
7700 /* get its atoms */
Michal Vasko5fb92a22022-08-02 09:21:37 +02007701 cur_scnode = set->cur_node ? set->cur_node->schema : NULL;
7702 LY_CHECK_GOTO(rc = lyxp_atomize(set->ctx, val_exp, set->cur_mod, set->format, set->prefix_data, cur_scnode,
7703 ctx_scnode, &set2, LYXP_SCNODE), cleanup);
Michal Vasko3d969ff2022-07-29 15:02:08 +02007704
7705 /* check whether we can compile a single predicate (evaluation result value is always the same) */
7706 for (i = 0; i < set2.used; ++i) {
7707 if ((set2.val.scnodes[i].type != LYXP_NODE_ELEM) || (set2.val.scnodes[i].in_ctx < LYXP_SET_SCNODE_ATOM_NODE)) {
7708 /* skip root and context node */
7709 continue;
7710 }
7711
Michal Vasko5fb92a22022-08-02 09:21:37 +02007712 /* 1) context node descendants are traversed - do best-effort detection of the value dependency on the
7713 * context node instance */
7714 if ((set2.val.scnodes[i].axis == LYXP_AXIS_CHILD) && (set2.val.scnodes[i].scnode->parent == ctx_scnode)) {
7715 /* 1.1) context node child was accessed on the child axis, certain dependency */
Michal Vasko3d969ff2022-07-29 15:02:08 +02007716 rc = LY_ENOT;
7717 goto cleanup;
7718 }
Michal Vasko5fb92a22022-08-02 09:21:37 +02007719 if ((set2.val.scnodes[i].axis == LYXP_AXIS_DESCENDANT) || (set2.val.scnodes[i].axis == LYXP_AXIS_DESCENDANT_OR_SELF)) {
7720 for (sparent = set2.val.scnodes[i].scnode->parent; sparent && (sparent != ctx_scnode); sparent = sparent->parent) {}
7721 if (sparent) {
7722 /* 1.2) context node descendant was accessed on the descendant axis, probable dependency */
7723 rc = LY_ENOT;
7724 goto cleanup;
7725 }
7726 }
Michal Vasko3d969ff2022-07-29 15:02:08 +02007727
Michal Vasko5fb92a22022-08-02 09:21:37 +02007728 /* 2) multi-instance nodes (list or leaf-list) are traversed - all the instances need to be considered,
7729 * but the current node can be safely ignored, it is always the same data instance */
7730 if ((set2.val.scnodes[i].scnode->nodetype & (LYS_LIST | LYS_LEAFLIST)) && (cur_scnode != set2.val.scnodes[i].scnode)) {
Michal Vasko3d969ff2022-07-29 15:02:08 +02007731 rc = LY_ENOT;
7732 goto cleanup;
7733 }
Michal Vasko3d969ff2022-07-29 15:02:08 +02007734 }
7735
7736 /* get any data instance of the context node, we checked it makes no difference */
7737 siblings = set->val.nodes[0].node ? lyd_child(set->val.nodes[0].node) : set->tree;
7738 LY_CHECK_GOTO(rc = lyd_find_sibling_schema(siblings, ctx_scnode, &ctx_node), cleanup);
7739
7740 /* evaluate the value subexpression with the root context node */
7741 lyxp_set_free_content(&set2);
7742 LY_CHECK_GOTO(rc = lyxp_eval(set->ctx, val_exp, set->cur_mod, set->format, set->prefix_data, set->cur_node,
7743 ctx_node, set->tree, NULL, &set2, 0), cleanup);
7744
7745 /* cast it into a string */
7746 LY_CHECK_GOTO(rc = lyxp_set_cast(&set2, LYXP_SET_STRING), cleanup);
7747
7748 /* append the JSON predicate */
7749 *pred = ly_realloc(*pred, *pred_len + 1 + strlen(pred_node->name) + 2 + strlen(set2.val.str) + 3);
7750 LY_CHECK_ERR_GOTO(!*pred, LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
7751 quot = strchr(set2.val.str, '\'') ? '\"' : '\'';
7752 *pred_len += sprintf(*pred + *pred_len, "[%s=%c%s%c]", pred_node->name, quot, set2.val.str, quot);
7753
7754cleanup:
7755 lyxp_expr_free(set->ctx, val_exp);
7756 lyxp_set_free_content(&set2);
7757 return rc;
7758}
7759
7760/**
Michal Vasko004d3152020-06-11 19:59:22 +02007761 * @brief Try to compile list or leaf-list predicate in the known format to be used for hash-based instance search.
Michal Vaskod3678892020-05-21 10:06:58 +02007762 *
Michal Vasko004d3152020-06-11 19:59:22 +02007763 * @param[in] exp Full parsed XPath expression.
7764 * @param[in,out] tok_idx Index in @p exp at the beginning of the predicate, is updated on success.
Michal Vasko3d969ff2022-07-29 15:02:08 +02007765 * @param[in] ctx_scnode Found schema node as the context for the predicate.
7766 * @param[in] set Context set.
Michal Vasko004d3152020-06-11 19:59:22 +02007767 * @param[out] predicates Parsed predicates.
Michal Vasko004d3152020-06-11 19:59:22 +02007768 * @return LY_SUCCESS on success,
Michal Vasko3d969ff2022-07-29 15:02:08 +02007769 * @return LY_ENOT if a predicate could not be compiled.
Michal Vasko004d3152020-06-11 19:59:22 +02007770 * @return LY_ERR on any error.
Michal Vaskod3678892020-05-21 10:06:58 +02007771 */
7772static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02007773eval_name_test_try_compile_predicates(const struct lyxp_expr *exp, uint32_t *tok_idx, const struct lysc_node *ctx_scnode,
Michal Vasko90189962023-02-28 12:10:34 +01007774 const struct lyxp_set *set, struct ly_path_predicate **predicates)
Michal Vaskod3678892020-05-21 10:06:58 +02007775{
Michal Vasko3d969ff2022-07-29 15:02:08 +02007776 LY_ERR rc = LY_SUCCESS;
Michal Vaskod4a6d042022-12-08 08:34:29 +01007777 uint32_t e_idx, val_start_idx, pred_idx = 0, temp_lo = 0, pred_len = 0, nested_pred;
Michal Vaskod3678892020-05-21 10:06:58 +02007778 const struct lysc_node *key;
Michal Vasko3d969ff2022-07-29 15:02:08 +02007779 char *pred = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02007780 struct lyxp_expr *exp2 = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02007781
Michal Vasko3d969ff2022-07-29 15:02:08 +02007782 assert(ctx_scnode->nodetype & (LYS_LIST | LYS_LEAFLIST));
Michal Vaskod3678892020-05-21 10:06:58 +02007783
Michal Vasko3d969ff2022-07-29 15:02:08 +02007784 /* turn logging off */
Michal Vaskod4a6d042022-12-08 08:34:29 +01007785 ly_temp_log_options(&temp_lo);
Michal Vasko3d969ff2022-07-29 15:02:08 +02007786
7787 if (ctx_scnode->nodetype == LYS_LIST) {
7788 /* check for predicates "[key1=...][key2=...]..." */
7789
Michal Vasko004d3152020-06-11 19:59:22 +02007790 /* get key count */
Michal Vasko3d969ff2022-07-29 15:02:08 +02007791 if (ctx_scnode->flags & LYS_KEYLESS) {
7792 rc = LY_ENOT;
7793 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +02007794 }
Michal Vaskod3678892020-05-21 10:06:58 +02007795
Michal Vasko004d3152020-06-11 19:59:22 +02007796 /* learn where the predicates end */
7797 e_idx = *tok_idx;
Michal Vasko3d969ff2022-07-29 15:02:08 +02007798 for (key = lysc_node_child(ctx_scnode); key && (key->flags & LYS_KEY); key = key->next) {
Michal Vasko004d3152020-06-11 19:59:22 +02007799 /* '[' */
7800 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
Michal Vasko3d969ff2022-07-29 15:02:08 +02007801 rc = LY_ENOT;
7802 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +02007803 }
7804 ++e_idx;
7805
Michal Vasko3354d272021-04-06 09:40:06 +02007806 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_NAMETEST)) {
Michal Vasko3d969ff2022-07-29 15:02:08 +02007807 /* not a key */
7808 rc = LY_ENOT;
7809 goto cleanup;
Michal Vasko3354d272021-04-06 09:40:06 +02007810 }
7811
Michal Vasko3d969ff2022-07-29 15:02:08 +02007812 /* check key */
7813 LY_CHECK_GOTO(rc = eval_name_test_try_compile_predicate_key(exp->expr + exp->tok_pos[e_idx],
7814 exp->tok_len[e_idx], ctx_scnode, set, key), cleanup);
7815
7816 ++e_idx;
7817
7818 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_OPER_EQUAL)) {
7819 /* not '=' */
7820 rc = LY_ENOT;
7821 goto cleanup;
7822 }
7823 ++e_idx;
7824
7825 /* value start */
7826 val_start_idx = e_idx;
7827
Michal Vasko004d3152020-06-11 19:59:22 +02007828 /* ']' */
Michal Vasko3ef7e252022-11-16 08:29:49 +01007829 nested_pred = 1;
7830 do {
7831 ++e_idx;
7832
7833 if ((nested_pred == 1) && !lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_OPER_LOG)) {
Michal Vaskob8ee3562022-08-02 10:43:17 +02007834 /* higher priority than '=' */
7835 rc = LY_ENOT;
7836 goto cleanup;
Michal Vasko3ef7e252022-11-16 08:29:49 +01007837 } else if (!lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
7838 /* nested predicate */
7839 ++nested_pred;
7840 } else if (!lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) {
7841 /* predicate end */
7842 --nested_pred;
Michal Vaskob8ee3562022-08-02 10:43:17 +02007843 }
Michal Vasko3ef7e252022-11-16 08:29:49 +01007844 } while (nested_pred);
Michal Vasko004d3152020-06-11 19:59:22 +02007845
Michal Vasko3d969ff2022-07-29 15:02:08 +02007846 /* try to evaluate the value */
7847 LY_CHECK_GOTO(rc = eval_name_test_try_compile_predicate_append(exp, val_start_idx, e_idx - 1, ctx_scnode,
7848 set, key, &pred, &pred_len), cleanup);
7849
7850 ++e_idx;
Michal Vasko004d3152020-06-11 19:59:22 +02007851 }
Michal Vasko004d3152020-06-11 19:59:22 +02007852 } else {
Michal Vasko3d969ff2022-07-29 15:02:08 +02007853 /* check for predicate "[.=...]" */
7854
Michal Vasko004d3152020-06-11 19:59:22 +02007855 /* learn just where this single predicate ends */
7856 e_idx = *tok_idx;
7857
Michal Vaskod3678892020-05-21 10:06:58 +02007858 /* '[' */
Michal Vasko004d3152020-06-11 19:59:22 +02007859 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
Michal Vasko3d969ff2022-07-29 15:02:08 +02007860 rc = LY_ENOT;
7861 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +02007862 }
7863 ++e_idx;
Michal Vaskod3678892020-05-21 10:06:58 +02007864
Michal Vasko3354d272021-04-06 09:40:06 +02007865 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_DOT)) {
Michal Vasko3d969ff2022-07-29 15:02:08 +02007866 /* not the node value */
7867 rc = LY_ENOT;
7868 goto cleanup;
Michal Vasko3354d272021-04-06 09:40:06 +02007869 }
Michal Vasko3d969ff2022-07-29 15:02:08 +02007870 ++e_idx;
7871
7872 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_OPER_EQUAL)) {
7873 /* not '=' */
7874 rc = LY_ENOT;
7875 goto cleanup;
7876 }
7877 ++e_idx;
7878
7879 /* value start */
7880 val_start_idx = e_idx;
Michal Vasko3354d272021-04-06 09:40:06 +02007881
Michal Vaskod3678892020-05-21 10:06:58 +02007882 /* ']' */
Michal Vasko31f19632022-11-16 09:38:40 +01007883 nested_pred = 1;
7884 do {
7885 ++e_idx;
7886
7887 if ((nested_pred == 1) && !lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_OPER_LOG)) {
Michal Vaskob8ee3562022-08-02 10:43:17 +02007888 /* higher priority than '=' */
7889 rc = LY_ENOT;
7890 goto cleanup;
Michal Vasko31f19632022-11-16 09:38:40 +01007891 } else if (!lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
7892 /* nested predicate */
7893 ++nested_pred;
7894 } else if (!lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) {
7895 /* predicate end */
7896 --nested_pred;
Michal Vaskob8ee3562022-08-02 10:43:17 +02007897 }
Michal Vasko31f19632022-11-16 09:38:40 +01007898 } while (nested_pred);
Michal Vasko3d969ff2022-07-29 15:02:08 +02007899
7900 /* try to evaluate the value */
7901 LY_CHECK_GOTO(rc = eval_name_test_try_compile_predicate_append(exp, val_start_idx, e_idx - 1, ctx_scnode, set,
7902 ctx_scnode, &pred, &pred_len), cleanup);
7903
Michal Vasko004d3152020-06-11 19:59:22 +02007904 ++e_idx;
Michal Vaskod3678892020-05-21 10:06:58 +02007905 }
7906
Michal Vasko004d3152020-06-11 19:59:22 +02007907 /* parse the predicate(s) */
Michal Vasko3d969ff2022-07-29 15:02:08 +02007908 LY_CHECK_GOTO(rc = ly_path_parse_predicate(set->ctx, ctx_scnode, pred, pred_len, LY_PATH_PREFIX_OPTIONAL,
7909 LY_PATH_PRED_SIMPLE, &exp2), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02007910
7911 /* compile */
Michal Vasko3d969ff2022-07-29 15:02:08 +02007912 rc = ly_path_compile_predicate(set->ctx, set->cur_node ? set->cur_node->schema : NULL, set->cur_mod, ctx_scnode, exp2,
Michal Vasko90189962023-02-28 12:10:34 +01007913 &pred_idx, LY_VALUE_JSON, NULL, predicates);
Michal Vasko3d969ff2022-07-29 15:02:08 +02007914 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02007915
7916 /* success, the predicate must include all the needed information for hash-based search */
7917 *tok_idx = e_idx;
7918
7919cleanup:
Michal Vaskod4a6d042022-12-08 08:34:29 +01007920 ly_temp_log_options(NULL);
Michal Vasko3d969ff2022-07-29 15:02:08 +02007921 lyxp_expr_free(set->ctx, exp2);
7922 free(pred);
7923 return rc;
Michal Vaskod3678892020-05-21 10:06:58 +02007924}
7925
7926/**
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007927 * @brief Search for/check the next schema node that could be the only matching schema node meaning the
7928 * data node(s) could be found using a single hash-based search.
7929 *
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007930 * @param[in] ctx libyang context.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007931 * @param[in] node Next context node to check.
7932 * @param[in] name Expected node name.
7933 * @param[in] name_len Length of @p name.
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007934 * @param[in] moveto_mod Expected node module, can be NULL for JSON format with no prefix.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007935 * @param[in] root_type XPath root type.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007936 * @param[in] format Prefix format.
7937 * @param[in,out] found Previously found node, is updated.
7938 * @return LY_SUCCESS on success,
7939 * @return LY_ENOT if the whole check failed and hashes cannot be used.
7940 */
7941static LY_ERR
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007942eval_name_test_with_predicate_get_scnode(const struct ly_ctx *ctx, const struct lyd_node *node, const char *name,
Michal Vaskodd528af2022-08-08 14:35:07 +02007943 uint32_t name_len, const struct lys_module *moveto_mod, enum lyxp_node_type root_type, LY_VALUE_FORMAT format,
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007944 const struct lysc_node **found)
7945{
7946 const struct lysc_node *scnode;
7947 const struct lys_module *mod;
7948 uint32_t idx = 0;
7949
Radek Krejci8df109d2021-04-23 12:19:08 +02007950 assert((format == LY_VALUE_JSON) || moveto_mod);
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007951
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007952continue_search:
Michal Vasko7d1d0912020-10-16 08:38:30 +02007953 scnode = NULL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007954 if (!node) {
Radek Krejci8df109d2021-04-23 12:19:08 +02007955 if ((format == LY_VALUE_JSON) && !moveto_mod) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007956 /* search all modules for a single match */
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007957 while ((mod = ly_ctx_get_module_iter(ctx, &idx))) {
Michal Vasko35a3b1d2021-07-14 09:34:16 +02007958 if (!mod->implemented) {
7959 continue;
7960 }
7961
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007962 scnode = lys_find_child(NULL, mod, name, name_len, 0, 0);
7963 if (scnode) {
7964 /* we have found a match */
7965 break;
7966 }
7967 }
7968
Michal Vasko7d1d0912020-10-16 08:38:30 +02007969 if (!scnode) {
7970 /* all modules searched */
7971 idx = 0;
7972 }
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007973 } else {
7974 /* search in top-level */
7975 scnode = lys_find_child(NULL, moveto_mod, name, name_len, 0, 0);
7976 }
7977 } else if (!*found || (lysc_data_parent(*found) != node->schema)) {
Radek Krejci8df109d2021-04-23 12:19:08 +02007978 if ((format == LY_VALUE_JSON) && !moveto_mod) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007979 /* we must adjust the module to inherit the one from the context node */
7980 moveto_mod = node->schema->module;
7981 }
7982
7983 /* search in children, do not repeat the same search */
7984 scnode = lys_find_child(node->schema, moveto_mod, name, name_len, 0, 0);
Michal Vasko7d1d0912020-10-16 08:38:30 +02007985 } /* else skip redundant search */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007986
7987 /* additional context check */
7988 if (scnode && (root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) {
7989 scnode = NULL;
7990 }
7991
7992 if (scnode) {
7993 if (*found) {
7994 /* we found a schema node with the same name but at different level, give up, too complicated
7995 * (more hash-based searches would be required, not supported) */
7996 return LY_ENOT;
7997 } else {
7998 /* remember the found schema node and continue to make sure it can be used */
7999 *found = scnode;
8000 }
8001 }
8002
8003 if (idx) {
8004 /* continue searching all the following models */
8005 goto continue_search;
8006 }
8007
8008 return LY_SUCCESS;
8009}
8010
8011/**
Michal Vasko4ad69e72021-10-26 16:25:55 +02008012 * @brief Generate message when no matching schema nodes were found for a path segment.
8013 *
8014 * @param[in] set XPath set.
8015 * @param[in] scparent Previous schema parent in the context, if only one.
8016 * @param[in] ncname XPath NCName being evaluated.
8017 * @param[in] ncname_len Length of @p ncname.
8018 * @param[in] expr Whole XPath expression.
8019 * @param[in] options XPath options.
8020 */
8021static void
8022eval_name_test_scnode_no_match_msg(struct lyxp_set *set, const struct lyxp_set_scnode *scparent, const char *ncname,
Michal Vaskodd528af2022-08-08 14:35:07 +02008023 uint32_t ncname_len, const char *expr, uint32_t options)
Michal Vasko4ad69e72021-10-26 16:25:55 +02008024{
8025 const char *format;
8026 char *path = NULL, *ppath = NULL;
8027
8028 path = lysc_path(set->cur_scnode, LYSC_PATH_LOG, NULL, 0);
8029 if (scparent) {
8030 /* generate path for the parent */
8031 if (scparent->type == LYXP_NODE_ELEM) {
8032 ppath = lysc_path(scparent->scnode, LYSC_PATH_LOG, NULL, 0);
8033 } else if (scparent->type == LYXP_NODE_ROOT) {
8034 ppath = strdup("<root>");
8035 } else if (scparent->type == LYXP_NODE_ROOT_CONFIG) {
8036 ppath = strdup("<config-root>");
8037 }
8038 }
8039 if (ppath) {
8040 format = "Schema node \"%.*s\" for parent \"%s\" not found; in expr \"%.*s\" with context node \"%s\".";
8041 if (options & LYXP_SCNODE_ERROR) {
8042 LOGERR(set->ctx, LY_EVALID, format, ncname_len, ncname, ppath, (ncname - expr) + ncname_len, expr, path);
8043 } else {
8044 LOGWRN(set->ctx, format, ncname_len, ncname, ppath, (ncname - expr) + ncname_len, expr, path);
8045 }
8046 } else {
8047 format = "Schema node \"%.*s\" not found; in expr \"%.*s\" with context node \"%s\".";
8048 if (options & LYXP_SCNODE_ERROR) {
8049 LOGERR(set->ctx, LY_EVALID, format, ncname_len, ncname, (ncname - expr) + ncname_len, expr, path);
8050 } else {
8051 LOGWRN(set->ctx, format, ncname_len, ncname, (ncname - expr) + ncname_len, expr, path);
8052 }
8053 }
8054 free(path);
8055 free(ppath);
8056}
8057
8058/**
Michal Vaskod3678892020-05-21 10:06:58 +02008059 * @brief Evaluate NameTest and any following Predicates. Logs directly on error.
8060 *
8061 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
8062 * [6] NodeTest ::= NameTest | NodeType '(' ')'
8063 * [7] NameTest ::= '*' | NCName ':' '*' | QName
8064 *
8065 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008066 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko49fec8e2022-05-24 10:28:33 +02008067 * @param[in] axis What axis to search on.
Michal Vaskod3678892020-05-21 10:06:58 +02008068 * @param[in] all_desc Whether to search all the descendants or children only.
aPiecek8b0cc152021-05-31 16:40:31 +02008069 * @param[in,out] set Context and result set.
Michal Vaskod3678892020-05-21 10:06:58 +02008070 * @param[in] options XPath options.
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008071 * @return LY_ERR (LY_EINCOMPLETE on unresolved when, LY_ENOT for not found schema node)
Michal Vaskod3678892020-05-21 10:06:58 +02008072 */
8073static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02008074eval_name_test_with_predicate(const struct lyxp_expr *exp, uint32_t *tok_idx, enum lyxp_axis axis, ly_bool all_desc,
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008075 struct lyxp_set *set, uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02008076{
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008077 LY_ERR rc = LY_SUCCESS, r;
Michal Vasko004d3152020-06-11 19:59:22 +02008078 const char *ncname, *ncname_dict = NULL;
Michal Vasko56c008c2022-07-29 14:57:47 +02008079 uint32_t i, ncname_len;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008080 const struct lys_module *moveto_mod = NULL;
Michal Vaskoc71c37b2021-01-11 13:40:02 +01008081 const struct lysc_node *scnode = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02008082 struct ly_path_predicate *predicates = NULL;
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008083 int scnode_skip_pred = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02008084
aPiecek8b0cc152021-05-31 16:40:31 +02008085 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008086 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008087 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02008088
aPiecek8b0cc152021-05-31 16:40:31 +02008089 if (options & LYXP_SKIP_EXPR) {
Michal Vaskod3678892020-05-21 10:06:58 +02008090 goto moveto;
8091 }
8092
Michal Vasko004d3152020-06-11 19:59:22 +02008093 ncname = &exp->expr[exp->tok_pos[*tok_idx - 1]];
8094 ncname_len = exp->tok_len[*tok_idx - 1];
Michal Vaskod3678892020-05-21 10:06:58 +02008095
Michal Vaskoc71c37b2021-01-11 13:40:02 +01008096 if ((ncname[0] == '*') && (ncname_len == 1)) {
8097 /* all nodes will match */
8098 goto moveto;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008099 }
8100
Michal Vaskoc71c37b2021-01-11 13:40:02 +01008101 /* parse (and skip) module name */
8102 rc = moveto_resolve_model(&ncname, &ncname_len, set, NULL, &moveto_mod);
Michal Vaskod3678892020-05-21 10:06:58 +02008103 LY_CHECK_GOTO(rc, cleanup);
8104
Michal Vasko49fec8e2022-05-24 10:28:33 +02008105 if ((ncname[0] == '*') && (ncname_len == 1)) {
8106 /* all nodes from the module will match */
8107 goto moveto;
8108 }
8109
8110 if (((set->format == LY_VALUE_JSON) || moveto_mod) && (axis == LYXP_AXIS_CHILD) && !all_desc &&
8111 (set->type == LYXP_SET_NODE_SET)) {
Michal Vaskod3678892020-05-21 10:06:58 +02008112 /* find the matching schema node in some parent in the context */
Michal Vasko56c008c2022-07-29 14:57:47 +02008113 for (i = 0; i < set->used; ++i) {
Michal Vaskoc71c37b2021-01-11 13:40:02 +01008114 if (eval_name_test_with_predicate_get_scnode(set->ctx, set->val.nodes[i].node, ncname, ncname_len,
8115 moveto_mod, set->root_type, set->format, &scnode)) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008116 /* check failed */
8117 scnode = NULL;
8118 break;
Michal Vaskod3678892020-05-21 10:06:58 +02008119 }
8120 }
8121
Michal Vasko004d3152020-06-11 19:59:22 +02008122 if (scnode && (scnode->nodetype & (LYS_LIST | LYS_LEAFLIST))) {
8123 /* try to create the predicates */
Michal Vasko90189962023-02-28 12:10:34 +01008124 if (eval_name_test_try_compile_predicates(exp, tok_idx, scnode, set, &predicates)) {
Michal Vasko004d3152020-06-11 19:59:22 +02008125 /* hashes cannot be used */
Michal Vaskod3678892020-05-21 10:06:58 +02008126 scnode = NULL;
8127 }
8128 }
8129 }
8130
Michal Vaskoc71c37b2021-01-11 13:40:02 +01008131 if (!scnode) {
8132 /* we are not able to match based on a schema node and not all the modules match ("*"),
Michal Vasko004d3152020-06-11 19:59:22 +02008133 * use dictionary for efficient comparison */
Radek Krejci011e4aa2020-09-04 15:22:31 +02008134 LY_CHECK_GOTO(rc = lydict_insert(set->ctx, ncname, ncname_len, &ncname_dict), cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02008135 }
8136
8137moveto:
8138 /* move to the attribute(s), data node(s), or schema node(s) */
Michal Vasko49fec8e2022-05-24 10:28:33 +02008139 if (axis == LYXP_AXIS_ATTRIBUTE) {
aPiecek8b0cc152021-05-31 16:40:31 +02008140 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008141 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskod3678892020-05-21 10:06:58 +02008142 } else {
8143 if (all_desc) {
Michal Vaskocdad7122020-11-09 21:04:44 +01008144 rc = moveto_attr_alldesc(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02008145 } else {
aPiecek8b0cc152021-05-31 16:40:31 +02008146 rc = moveto_attr(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02008147 }
8148 LY_CHECK_GOTO(rc, cleanup);
8149 }
8150 } else {
aPiecek8b0cc152021-05-31 16:40:31 +02008151 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008152 const struct lyxp_set_scnode *scparent = NULL;
Michal Vasko56c008c2022-07-29 14:57:47 +02008153 ly_bool found = 0;
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008154
8155 /* remember parent if there is only one, to print in the warning */
8156 for (i = 0; i < set->used; ++i) {
8157 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
8158 if (!scparent) {
8159 /* remember the context node */
8160 scparent = &set->val.scnodes[i];
8161 } else {
8162 /* several context nodes, no reasonable error possible */
8163 scparent = NULL;
8164 break;
8165 }
8166 }
8167 }
Radek Krejci1deb5be2020-08-26 16:43:36 +02008168
Michal Vasko49fec8e2022-05-24 10:28:33 +02008169 if (all_desc && (axis == LYXP_AXIS_CHILD)) {
8170 /* efficient evaluation that does not add all the descendants into the set */
8171 rc = moveto_scnode_alldesc_child(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02008172 } else {
Michal Vasko49fec8e2022-05-24 10:28:33 +02008173 if (all_desc) {
8174 /* "//" == "/descendant-or-self::node()/" */
8175 rc = xpath_pi_node(set, LYXP_AXIS_DESCENDANT_OR_SELF, options);
8176 LY_CHECK_GOTO(rc, cleanup);
8177 }
8178 rc = moveto_scnode(set, moveto_mod, ncname_dict, axis, options);
Michal Vaskod3678892020-05-21 10:06:58 +02008179 }
8180 LY_CHECK_GOTO(rc, cleanup);
8181
Michal Vasko56c008c2022-07-29 14:57:47 +02008182 i = set->used;
8183 do {
8184 --i;
Michal Vasko1a09b212021-05-06 13:00:10 +02008185 if (set->val.scnodes[i].in_ctx > LYXP_SET_SCNODE_ATOM_NODE) {
Michal Vasko56c008c2022-07-29 14:57:47 +02008186 found = 1;
Michal Vaskod3678892020-05-21 10:06:58 +02008187 break;
8188 }
Michal Vasko56c008c2022-07-29 14:57:47 +02008189 } while (i);
8190 if (!found) {
Michal Vasko4ad69e72021-10-26 16:25:55 +02008191 /* generate message */
8192 eval_name_test_scnode_no_match_msg(set, scparent, ncname, ncname_len, exp->expr, options);
8193
8194 if (options & LYXP_SCNODE_ERROR) {
8195 /* error */
8196 rc = LY_EVALID;
8197 goto cleanup;
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008198 }
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008199
8200 /* skip the predicates and the rest of this path to not generate invalid warnings */
8201 rc = LY_ENOT;
8202 scnode_skip_pred = 1;
Michal Vaskod3678892020-05-21 10:06:58 +02008203 }
8204 } else {
Michal Vasko49fec8e2022-05-24 10:28:33 +02008205 if (all_desc && (axis == LYXP_AXIS_CHILD)) {
8206 /* efficient evaluation */
8207 rc = moveto_node_alldesc_child(set, moveto_mod, ncname_dict, options);
8208 } else if (scnode && (axis == LYXP_AXIS_CHILD)) {
8209 /* we can find the child nodes using hashes */
8210 rc = moveto_node_hash_child(set, scnode, predicates, options);
Michal Vaskod3678892020-05-21 10:06:58 +02008211 } else {
Michal Vasko49fec8e2022-05-24 10:28:33 +02008212 if (all_desc) {
8213 /* "//" == "/descendant-or-self::node()/" */
8214 rc = xpath_pi_node(set, LYXP_AXIS_DESCENDANT_OR_SELF, options);
8215 LY_CHECK_GOTO(rc, cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02008216 }
Michal Vasko49fec8e2022-05-24 10:28:33 +02008217 rc = moveto_node(set, moveto_mod, ncname_dict, axis, options);
Michal Vaskod3678892020-05-21 10:06:58 +02008218 }
8219 LY_CHECK_GOTO(rc, cleanup);
8220 }
8221 }
8222
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008223 if (scnode_skip_pred) {
8224 /* skip predicates */
8225 options |= LYXP_SKIP_EXPR;
8226 }
8227
Michal Vaskod3678892020-05-21 10:06:58 +02008228 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02008229 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02008230 r = eval_predicate(exp, tok_idx, set, options, axis);
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008231 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02008232 }
8233
8234cleanup:
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008235 if (scnode_skip_pred) {
8236 /* restore options */
8237 options &= ~LYXP_SKIP_EXPR;
8238 }
aPiecek8b0cc152021-05-31 16:40:31 +02008239 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko004d3152020-06-11 19:59:22 +02008240 lydict_remove(set->ctx, ncname_dict);
Michal Vasko90189962023-02-28 12:10:34 +01008241 ly_path_predicates_free(set->ctx, predicates);
Michal Vaskodb51a8d2020-05-27 15:22:29 +02008242 }
Michal Vaskod3678892020-05-21 10:06:58 +02008243 return rc;
8244}
8245
8246/**
8247 * @brief Evaluate NodeType and any following Predicates. Logs directly on error.
8248 *
8249 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
8250 * [6] NodeTest ::= NameTest | NodeType '(' ')'
8251 * [8] NodeType ::= 'text' | 'node'
8252 *
8253 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008254 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko49fec8e2022-05-24 10:28:33 +02008255 * @param[in] axis Axis to search on.
8256 * @param[in] all_desc Whether to search all the descendants or axis only.
aPiecek8b0cc152021-05-31 16:40:31 +02008257 * @param[in,out] set Context and result set.
Michal Vaskod3678892020-05-21 10:06:58 +02008258 * @param[in] options XPath options.
8259 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8260 */
8261static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02008262eval_node_type_with_predicate(const struct lyxp_expr *exp, uint32_t *tok_idx, enum lyxp_axis axis, ly_bool all_desc,
Radek Krejci1deb5be2020-08-26 16:43:36 +02008263 struct lyxp_set *set, uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02008264{
8265 LY_ERR rc;
8266
Michal Vaskod3678892020-05-21 10:06:58 +02008267 (void)all_desc;
8268
aPiecek8b0cc152021-05-31 16:40:31 +02008269 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko004d3152020-06-11 19:59:22 +02008270 assert(exp->tok_len[*tok_idx] == 4);
Michal Vasko49fec8e2022-05-24 10:28:33 +02008271 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "node", 4)) {
8272 rc = xpath_pi_node(set, axis, options);
Michal Vaskod3678892020-05-21 10:06:58 +02008273 } else {
Michal Vasko49fec8e2022-05-24 10:28:33 +02008274 assert(!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "text", 4));
8275 rc = xpath_pi_text(set, axis, options);
Michal Vaskod3678892020-05-21 10:06:58 +02008276 }
Michal Vasko49fec8e2022-05-24 10:28:33 +02008277 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02008278 }
aPiecek8b0cc152021-05-31 16:40:31 +02008279 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008280 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008281 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02008282
8283 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02008284 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
aPiecek8b0cc152021-05-31 16:40:31 +02008285 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008286 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008287 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02008288
8289 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02008290 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
aPiecek8b0cc152021-05-31 16:40:31 +02008291 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008292 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008293 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02008294
8295 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02008296 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02008297 rc = eval_predicate(exp, tok_idx, set, options, axis);
Michal Vaskod3678892020-05-21 10:06:58 +02008298 LY_CHECK_RET(rc);
8299 }
8300
8301 return LY_SUCCESS;
8302}
8303
8304/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02008305 * @brief Evaluate RelativeLocationPath. Logs directly on error.
8306 *
8307 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
8308 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
Michal Vaskod3678892020-05-21 10:06:58 +02008309 * [6] NodeTest ::= NameTest | NodeType '(' ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02008310 *
8311 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008312 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008313 * @param[in] all_desc Whether to search all the descendants or children only.
aPiecek8b0cc152021-05-31 16:40:31 +02008314 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008315 * @param[in] options XPath options.
8316 * @return LY_ERR (YL_EINCOMPLETE on unresolved when)
8317 */
8318static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02008319eval_relative_location_path(const struct lyxp_expr *exp, uint32_t *tok_idx, ly_bool all_desc, struct lyxp_set *set,
Michal Vasko40308e72020-10-20 16:38:40 +02008320 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008321{
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008322 LY_ERR rc = LY_SUCCESS;
Michal Vasko49fec8e2022-05-24 10:28:33 +02008323 enum lyxp_axis axis;
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008324 int scnode_skip_path = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008325
8326 goto step;
8327 do {
8328 /* evaluate '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02008329 if (exp->tok_len[*tok_idx] == 1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008330 all_desc = 0;
8331 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02008332 assert(exp->tok_len[*tok_idx] == 2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008333 all_desc = 1;
8334 }
aPiecek8b0cc152021-05-31 16:40:31 +02008335 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008336 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008337 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008338
8339step:
Michal Vasko49fec8e2022-05-24 10:28:33 +02008340 /* AxisSpecifier */
8341 if (exp->tokens[*tok_idx] == LYXP_TOKEN_AXISNAME) {
8342 axis = str2axis(exp->expr + exp->tok_pos[*tok_idx], exp->tok_len[*tok_idx]);
Michal Vaskod3678892020-05-21 10:06:58 +02008343
aPiecek8b0cc152021-05-31 16:40:31 +02008344 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008345 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
8346 ++(*tok_idx);
8347
8348 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_DCOLON);
8349 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
8350 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
8351 ++(*tok_idx);
8352 } else if (exp->tokens[*tok_idx] == LYXP_TOKEN_AT) {
8353 axis = LYXP_AXIS_ATTRIBUTE;
8354
8355 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
8356 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008357 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02008358 } else {
Michal Vasko49fec8e2022-05-24 10:28:33 +02008359 /* default */
8360 axis = LYXP_AXIS_CHILD;
Michal Vaskod3678892020-05-21 10:06:58 +02008361 }
8362
Michal Vasko49fec8e2022-05-24 10:28:33 +02008363 /* NodeTest Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02008364 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008365 case LYXP_TOKEN_DOT:
8366 /* evaluate '.' */
Michal Vasko49fec8e2022-05-24 10:28:33 +02008367 if (!(options & LYXP_SKIP_EXPR)) {
8368 if (((options & LYXP_SCNODE_ALL) && (set->type != LYXP_SET_SCNODE_SET)) ||
8369 (!(options & LYXP_SCNODE_ALL) && (set->type != LYXP_SET_NODE_SET))) {
8370 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
8371 rc = LY_EVALID;
8372 goto cleanup;
8373 }
8374
8375 if (all_desc) {
8376 rc = xpath_pi_node(set, LYXP_AXIS_DESCENDANT_OR_SELF, options);
8377 LY_CHECK_GOTO(rc, cleanup);
8378 }
8379 rc = xpath_pi_node(set, LYXP_AXIS_SELF, options);
8380 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008381 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008382 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008383 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008384 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008385 break;
8386
8387 case LYXP_TOKEN_DDOT:
8388 /* evaluate '..' */
Michal Vasko49fec8e2022-05-24 10:28:33 +02008389 if (!(options & LYXP_SKIP_EXPR)) {
8390 if (((options & LYXP_SCNODE_ALL) && (set->type != LYXP_SET_SCNODE_SET)) ||
8391 (!(options & LYXP_SCNODE_ALL) && (set->type != LYXP_SET_NODE_SET))) {
8392 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
8393 rc = LY_EVALID;
8394 goto cleanup;
8395 }
8396
8397 if (all_desc) {
8398 rc = xpath_pi_node(set, LYXP_AXIS_DESCENDANT_OR_SELF, options);
8399 LY_CHECK_GOTO(rc, cleanup);
8400 }
8401 rc = xpath_pi_node(set, LYXP_AXIS_PARENT, options);
8402 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008403 }
aPiecek8b0cc152021-05-31 16:40:31 +02008404 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008405 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008406 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008407 break;
8408
Michal Vasko03ff5a72019-09-11 13:49:33 +02008409 case LYXP_TOKEN_NAMETEST:
Michal Vaskod3678892020-05-21 10:06:58 +02008410 /* evaluate NameTest Predicate* */
Michal Vasko49fec8e2022-05-24 10:28:33 +02008411 rc = eval_name_test_with_predicate(exp, tok_idx, axis, all_desc, set, options);
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008412 if (rc == LY_ENOT) {
8413 assert(options & LYXP_SCNODE_ALL);
8414 /* skip the rest of this path */
8415 rc = LY_SUCCESS;
8416 scnode_skip_path = 1;
8417 options |= LYXP_SKIP_EXPR;
8418 }
8419 LY_CHECK_GOTO(rc, cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02008420 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008421
Michal Vaskod3678892020-05-21 10:06:58 +02008422 case LYXP_TOKEN_NODETYPE:
8423 /* evaluate NodeType Predicate* */
Michal Vasko49fec8e2022-05-24 10:28:33 +02008424 rc = eval_node_type_with_predicate(exp, tok_idx, axis, all_desc, set, options);
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008425 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008426 break;
8427
8428 default:
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008429 LOGINT(set->ctx);
8430 rc = LY_EINT;
8431 goto cleanup;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008432 }
Michal Vasko004d3152020-06-11 19:59:22 +02008433 } while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008434
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008435cleanup:
8436 if (scnode_skip_path) {
8437 options &= ~LYXP_SKIP_EXPR;
8438 }
8439 return rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008440}
8441
8442/**
8443 * @brief Evaluate AbsoluteLocationPath. Logs directly on error.
8444 *
8445 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
8446 *
8447 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008448 * @param[in] tok_idx Position in the expression @p exp.
aPiecek8b0cc152021-05-31 16:40:31 +02008449 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008450 * @param[in] options XPath options.
8451 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8452 */
8453static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02008454eval_absolute_location_path(const struct lyxp_expr *exp, uint32_t *tok_idx, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008455{
Radek Krejci857189e2020-09-01 13:26:36 +02008456 ly_bool all_desc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008457
aPiecek8b0cc152021-05-31 16:40:31 +02008458 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008459 /* no matter what tokens follow, we need to be at the root */
Michal Vaskob0099a92020-08-31 14:55:23 +02008460 LY_CHECK_RET(moveto_root(set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008461 }
8462
8463 /* '/' RelativeLocationPath? */
Michal Vasko004d3152020-06-11 19:59:22 +02008464 if (exp->tok_len[*tok_idx] == 1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008465 /* evaluate '/' - deferred */
8466 all_desc = 0;
aPiecek8b0cc152021-05-31 16:40:31 +02008467 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008468 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008469 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008470
Michal Vasko004d3152020-06-11 19:59:22 +02008471 if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008472 return LY_SUCCESS;
8473 }
Michal Vasko004d3152020-06-11 19:59:22 +02008474 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008475 case LYXP_TOKEN_DOT:
8476 case LYXP_TOKEN_DDOT:
Michal Vasko49fec8e2022-05-24 10:28:33 +02008477 case LYXP_TOKEN_AXISNAME:
Michal Vasko03ff5a72019-09-11 13:49:33 +02008478 case LYXP_TOKEN_AT:
8479 case LYXP_TOKEN_NAMETEST:
8480 case LYXP_TOKEN_NODETYPE:
Michal Vaskob0099a92020-08-31 14:55:23 +02008481 LY_CHECK_RET(eval_relative_location_path(exp, tok_idx, all_desc, set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008482 break;
8483 default:
8484 break;
8485 }
8486
Michal Vasko03ff5a72019-09-11 13:49:33 +02008487 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02008488 /* '//' RelativeLocationPath */
Michal Vasko03ff5a72019-09-11 13:49:33 +02008489 /* evaluate '//' - deferred so as not to waste memory by remembering all the nodes */
8490 all_desc = 1;
aPiecek8b0cc152021-05-31 16:40:31 +02008491 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008492 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008493 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008494
Michal Vaskob0099a92020-08-31 14:55:23 +02008495 LY_CHECK_RET(eval_relative_location_path(exp, tok_idx, all_desc, set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008496 }
8497
8498 return LY_SUCCESS;
8499}
8500
8501/**
8502 * @brief Evaluate FunctionCall. Logs directly on error.
8503 *
Michal Vaskod3678892020-05-21 10:06:58 +02008504 * [11] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02008505 *
8506 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008507 * @param[in] tok_idx Position in the expression @p exp.
aPiecek8b0cc152021-05-31 16:40:31 +02008508 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008509 * @param[in] options XPath options.
8510 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8511 */
8512static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02008513eval_function_call(const struct lyxp_expr *exp, uint32_t *tok_idx, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008514{
8515 LY_ERR rc;
Michal Vasko69730152020-10-09 16:30:07 +02008516
Michal Vaskodd528af2022-08-08 14:35:07 +02008517 LY_ERR (*xpath_func)(struct lyxp_set **, uint32_t, struct lyxp_set *, uint32_t) = NULL;
8518 uint32_t arg_count = 0, i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008519 struct lyxp_set **args = NULL, **args_aux;
8520
aPiecek8b0cc152021-05-31 16:40:31 +02008521 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008522 /* FunctionName */
Michal Vasko004d3152020-06-11 19:59:22 +02008523 switch (exp->tok_len[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008524 case 3:
Michal Vasko004d3152020-06-11 19:59:22 +02008525 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008526 xpath_func = &xpath_not;
Michal Vasko004d3152020-06-11 19:59:22 +02008527 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008528 xpath_func = &xpath_sum;
8529 }
8530 break;
8531 case 4:
Michal Vasko004d3152020-06-11 19:59:22 +02008532 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008533 xpath_func = &xpath_lang;
Michal Vasko004d3152020-06-11 19:59:22 +02008534 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008535 xpath_func = &xpath_last;
Michal Vasko004d3152020-06-11 19:59:22 +02008536 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008537 xpath_func = &xpath_name;
Michal Vasko004d3152020-06-11 19:59:22 +02008538 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008539 xpath_func = &xpath_true;
8540 }
8541 break;
8542 case 5:
Michal Vasko004d3152020-06-11 19:59:22 +02008543 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008544 xpath_func = &xpath_count;
Michal Vasko004d3152020-06-11 19:59:22 +02008545 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008546 xpath_func = &xpath_false;
Michal Vasko004d3152020-06-11 19:59:22 +02008547 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008548 xpath_func = &xpath_floor;
Michal Vasko004d3152020-06-11 19:59:22 +02008549 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008550 xpath_func = &xpath_round;
Michal Vasko004d3152020-06-11 19:59:22 +02008551 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008552 xpath_func = &xpath_deref;
8553 }
8554 break;
8555 case 6:
Michal Vasko004d3152020-06-11 19:59:22 +02008556 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008557 xpath_func = &xpath_concat;
Michal Vasko004d3152020-06-11 19:59:22 +02008558 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008559 xpath_func = &xpath_number;
Michal Vasko004d3152020-06-11 19:59:22 +02008560 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008561 xpath_func = &xpath_string;
8562 }
8563 break;
8564 case 7:
Michal Vasko004d3152020-06-11 19:59:22 +02008565 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008566 xpath_func = &xpath_boolean;
Michal Vasko004d3152020-06-11 19:59:22 +02008567 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008568 xpath_func = &xpath_ceiling;
Michal Vasko004d3152020-06-11 19:59:22 +02008569 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008570 xpath_func = &xpath_current;
8571 }
8572 break;
8573 case 8:
Michal Vasko004d3152020-06-11 19:59:22 +02008574 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008575 xpath_func = &xpath_contains;
Michal Vasko004d3152020-06-11 19:59:22 +02008576 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008577 xpath_func = &xpath_position;
Michal Vasko004d3152020-06-11 19:59:22 +02008578 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "re-match", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008579 xpath_func = &xpath_re_match;
8580 }
8581 break;
8582 case 9:
Michal Vasko004d3152020-06-11 19:59:22 +02008583 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008584 xpath_func = &xpath_substring;
Michal Vasko004d3152020-06-11 19:59:22 +02008585 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008586 xpath_func = &xpath_translate;
8587 }
8588 break;
8589 case 10:
Michal Vasko004d3152020-06-11 19:59:22 +02008590 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008591 xpath_func = &xpath_local_name;
Michal Vasko004d3152020-06-11 19:59:22 +02008592 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008593 xpath_func = &xpath_enum_value;
Michal Vasko004d3152020-06-11 19:59:22 +02008594 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008595 xpath_func = &xpath_bit_is_set;
8596 }
8597 break;
8598 case 11:
Michal Vasko004d3152020-06-11 19:59:22 +02008599 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008600 xpath_func = &xpath_starts_with;
8601 }
8602 break;
8603 case 12:
Michal Vasko004d3152020-06-11 19:59:22 +02008604 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008605 xpath_func = &xpath_derived_from;
8606 }
8607 break;
8608 case 13:
Michal Vasko004d3152020-06-11 19:59:22 +02008609 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008610 xpath_func = &xpath_namespace_uri;
Michal Vasko004d3152020-06-11 19:59:22 +02008611 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string-length", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008612 xpath_func = &xpath_string_length;
8613 }
8614 break;
8615 case 15:
Michal Vasko004d3152020-06-11 19:59:22 +02008616 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008617 xpath_func = &xpath_normalize_space;
Michal Vasko004d3152020-06-11 19:59:22 +02008618 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-after", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008619 xpath_func = &xpath_substring_after;
8620 }
8621 break;
8622 case 16:
Michal Vasko004d3152020-06-11 19:59:22 +02008623 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008624 xpath_func = &xpath_substring_before;
8625 }
8626 break;
8627 case 20:
Michal Vasko004d3152020-06-11 19:59:22 +02008628 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from-or-self", 20)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008629 xpath_func = &xpath_derived_from_or_self;
8630 }
8631 break;
8632 }
8633
8634 if (!xpath_func) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01008635 LOGVAL(set->ctx, LY_VCODE_XP_INFUNC, exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008636 return LY_EVALID;
8637 }
8638 }
8639
aPiecek8b0cc152021-05-31 16:40:31 +02008640 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008641 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008642 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008643
8644 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02008645 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
aPiecek8b0cc152021-05-31 16:40:31 +02008646 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008647 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008648 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008649
8650 /* ( Expr ( ',' Expr )* )? */
Michal Vasko004d3152020-06-11 19:59:22 +02008651 if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) {
aPiecek8b0cc152021-05-31 16:40:31 +02008652 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008653 args = malloc(sizeof *args);
8654 LY_CHECK_ERR_GOTO(!args, LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
8655 arg_count = 1;
8656 args[0] = set_copy(set);
8657 if (!args[0]) {
8658 rc = LY_EMEM;
8659 goto cleanup;
8660 }
8661
Michal Vasko004d3152020-06-11 19:59:22 +02008662 rc = eval_expr_select(exp, tok_idx, 0, args[0], options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008663 LY_CHECK_GOTO(rc, cleanup);
8664 } else {
aPiecek8b0cc152021-05-31 16:40:31 +02008665 rc = eval_expr_select(exp, tok_idx, 0, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008666 LY_CHECK_GOTO(rc, cleanup);
8667 }
8668 }
Michal Vasko004d3152020-06-11 19:59:22 +02008669 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) {
aPiecek8b0cc152021-05-31 16:40:31 +02008670 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008671 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008672 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008673
aPiecek8b0cc152021-05-31 16:40:31 +02008674 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008675 ++arg_count;
8676 args_aux = realloc(args, arg_count * sizeof *args);
8677 LY_CHECK_ERR_GOTO(!args_aux, arg_count--; LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
8678 args = args_aux;
8679 args[arg_count - 1] = set_copy(set);
8680 if (!args[arg_count - 1]) {
8681 rc = LY_EMEM;
8682 goto cleanup;
8683 }
8684
Michal Vasko004d3152020-06-11 19:59:22 +02008685 rc = eval_expr_select(exp, tok_idx, 0, args[arg_count - 1], options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008686 LY_CHECK_GOTO(rc, cleanup);
8687 } else {
aPiecek8b0cc152021-05-31 16:40:31 +02008688 rc = eval_expr_select(exp, tok_idx, 0, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008689 LY_CHECK_GOTO(rc, cleanup);
8690 }
8691 }
8692
8693 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02008694 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
aPiecek8b0cc152021-05-31 16:40:31 +02008695 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008696 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008697 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008698
aPiecek8b0cc152021-05-31 16:40:31 +02008699 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008700 /* evaluate function */
8701 rc = xpath_func(args, arg_count, set, options);
8702
8703 if (options & LYXP_SCNODE_ALL) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008704 /* merge all nodes from arg evaluations */
8705 for (i = 0; i < arg_count; ++i) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008706 set_scnode_clear_ctx(args[i], LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008707 lyxp_set_scnode_merge(set, args[i]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008708 }
8709 }
8710 } else {
8711 rc = LY_SUCCESS;
8712 }
8713
8714cleanup:
8715 for (i = 0; i < arg_count; ++i) {
8716 lyxp_set_free(args[i]);
8717 }
8718 free(args);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008719 return rc;
8720}
8721
8722/**
8723 * @brief Evaluate Number. Logs directly on error.
8724 *
8725 * @param[in] ctx Context for errors.
8726 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008727 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008728 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8729 * @return LY_ERR
8730 */
8731static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02008732eval_number(struct ly_ctx *ctx, const struct lyxp_expr *exp, uint32_t *tok_idx, struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008733{
8734 long double num;
8735 char *endptr;
8736
8737 if (set) {
8738 errno = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02008739 num = strtold(&exp->expr[exp->tok_pos[*tok_idx]], &endptr);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008740 if (errno) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01008741 LOGVAL(ctx, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]);
8742 LOGVAL(ctx, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double (%s).",
Michal Vasko69730152020-10-09 16:30:07 +02008743 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]], strerror(errno));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008744 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02008745 } else if (endptr - &exp->expr[exp->tok_pos[*tok_idx]] != exp->tok_len[*tok_idx]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01008746 LOGVAL(ctx, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]);
8747 LOGVAL(ctx, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double.",
Michal Vasko69730152020-10-09 16:30:07 +02008748 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008749 return LY_EVALID;
8750 }
8751
8752 set_fill_number(set, num);
8753 }
8754
8755 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008756 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008757 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008758 return LY_SUCCESS;
8759}
8760
aPiecekdf23eee2021-10-07 12:21:50 +02008761LY_ERR
Michal Vasko90189962023-02-28 12:10:34 +01008762lyxp_vars_find(const struct ly_ctx *ctx, const struct lyxp_var *vars, const char *name, size_t name_len,
8763 struct lyxp_var **var)
aPiecekdf23eee2021-10-07 12:21:50 +02008764{
aPiecekdf23eee2021-10-07 12:21:50 +02008765 LY_ARRAY_COUNT_TYPE u;
8766
Michal Vasko90189962023-02-28 12:10:34 +01008767 assert(name);
aPiecekdf23eee2021-10-07 12:21:50 +02008768
Michal Vasko90189962023-02-28 12:10:34 +01008769 if (!name_len) {
8770 name_len = strlen(name);
8771 }
aPiecekdf23eee2021-10-07 12:21:50 +02008772
8773 LY_ARRAY_FOR(vars, u) {
8774 if (!strncmp(vars[u].name, name, name_len)) {
Michal Vasko90189962023-02-28 12:10:34 +01008775 if (var) {
8776 *var = (struct lyxp_var *)&vars[u];
8777 }
8778 return LY_SUCCESS;
aPiecekdf23eee2021-10-07 12:21:50 +02008779 }
8780 }
8781
Michal Vasko90189962023-02-28 12:10:34 +01008782 if (ctx) {
8783 LOGERR(ctx, LY_ENOTFOUND, "Variable \"%.*s\" not defined.", (int)name_len, name);
aPiecekdf23eee2021-10-07 12:21:50 +02008784 }
Michal Vasko90189962023-02-28 12:10:34 +01008785 return LY_ENOTFOUND;
aPiecekdf23eee2021-10-07 12:21:50 +02008786}
8787
Michal Vasko03ff5a72019-09-11 13:49:33 +02008788/**
aPiecekfba75362021-10-07 12:39:48 +02008789 * @brief Evaluate VariableReference.
8790 *
8791 * @param[in] exp Parsed XPath expression.
8792 * @param[in] tok_idx Position in the expression @p exp.
8793 * @param[in] vars [Sized array](@ref sizedarrays) of XPath variables.
8794 * @param[in,out] set Context and result set.
8795 * @param[in] options XPath options.
8796 * @return LY_ERR value.
8797 */
8798static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02008799eval_variable_reference(const struct lyxp_expr *exp, uint32_t *tok_idx, struct lyxp_set *set, uint32_t options)
aPiecekfba75362021-10-07 12:39:48 +02008800{
8801 LY_ERR ret;
8802 const char *name;
8803 struct lyxp_var *var;
aPiecekfba75362021-10-07 12:39:48 +02008804 struct lyxp_expr *tokens = NULL;
Michal Vaskodd528af2022-08-08 14:35:07 +02008805 uint32_t token_index, name_len;
aPiecekfba75362021-10-07 12:39:48 +02008806
Michal Vasko49fec8e2022-05-24 10:28:33 +02008807 /* find out the name and value of the variable */
aPiecekfba75362021-10-07 12:39:48 +02008808 name = &exp->expr[exp->tok_pos[*tok_idx]];
Michal Vaskoe68b5402022-07-29 14:58:15 +02008809 name_len = exp->tok_len[*tok_idx];
Michal Vasko90189962023-02-28 12:10:34 +01008810 ret = lyxp_vars_find(set->ctx, set->vars, name, name_len, &var);
8811 LY_CHECK_RET(ret);
aPiecekfba75362021-10-07 12:39:48 +02008812
Michal Vasko49fec8e2022-05-24 10:28:33 +02008813 /* parse value */
aPiecekfba75362021-10-07 12:39:48 +02008814 ret = lyxp_expr_parse(set->ctx, var->value, 0, 1, &tokens);
8815 LY_CHECK_GOTO(ret, cleanup);
8816
Michal Vasko49fec8e2022-05-24 10:28:33 +02008817 /* evaluate value */
aPiecekfba75362021-10-07 12:39:48 +02008818 token_index = 0;
8819 ret = eval_expr_select(tokens, &token_index, 0, set, options);
8820 LY_CHECK_GOTO(ret, cleanup);
8821
8822cleanup:
8823 lyxp_expr_free(set->ctx, tokens);
8824
8825 return ret;
8826}
8827
8828/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02008829 * @brief Evaluate PathExpr. Logs directly on error.
8830 *
Michal Vaskod3678892020-05-21 10:06:58 +02008831 * [12] PathExpr ::= LocationPath | PrimaryExpr Predicate*
Michal Vasko03ff5a72019-09-11 13:49:33 +02008832 * | PrimaryExpr Predicate* '/' RelativeLocationPath
8833 * | PrimaryExpr Predicate* '//' RelativeLocationPath
8834 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
aPiecekfba75362021-10-07 12:39:48 +02008835 * [10] PrimaryExpr ::= VariableReference | '(' Expr ')' | Literal | Number | FunctionCall
Michal Vasko03ff5a72019-09-11 13:49:33 +02008836 *
8837 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008838 * @param[in] tok_idx Position in the expression @p exp.
aPiecek8b0cc152021-05-31 16:40:31 +02008839 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008840 * @param[in] options XPath options.
8841 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8842 */
8843static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02008844eval_path_expr(const struct lyxp_expr *exp, uint32_t *tok_idx, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008845{
Michal Vasko49fec8e2022-05-24 10:28:33 +02008846 ly_bool all_desc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008847 LY_ERR rc;
8848
Michal Vasko004d3152020-06-11 19:59:22 +02008849 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008850 case LYXP_TOKEN_PAR1:
8851 /* '(' Expr ')' */
8852
8853 /* '(' */
aPiecek8b0cc152021-05-31 16:40:31 +02008854 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008855 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008856 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008857
8858 /* Expr */
Michal Vasko004d3152020-06-11 19:59:22 +02008859 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008860 LY_CHECK_RET(rc);
8861
8862 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02008863 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
aPiecek8b0cc152021-05-31 16:40:31 +02008864 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008865 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008866 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008867
Michal Vasko03ff5a72019-09-11 13:49:33 +02008868 goto predicate;
8869
8870 case LYXP_TOKEN_DOT:
8871 case LYXP_TOKEN_DDOT:
Michal Vasko49fec8e2022-05-24 10:28:33 +02008872 case LYXP_TOKEN_AXISNAME:
Michal Vasko03ff5a72019-09-11 13:49:33 +02008873 case LYXP_TOKEN_AT:
8874 case LYXP_TOKEN_NAMETEST:
8875 case LYXP_TOKEN_NODETYPE:
8876 /* RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02008877 rc = eval_relative_location_path(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008878 LY_CHECK_RET(rc);
8879 break;
8880
aPiecekfba75362021-10-07 12:39:48 +02008881 case LYXP_TOKEN_VARREF:
8882 /* VariableReference */
8883 rc = eval_variable_reference(exp, tok_idx, set, options);
8884 LY_CHECK_RET(rc);
8885 ++(*tok_idx);
8886
aPiecekfba75362021-10-07 12:39:48 +02008887 goto predicate;
8888
Michal Vasko03ff5a72019-09-11 13:49:33 +02008889 case LYXP_TOKEN_FUNCNAME:
8890 /* FunctionCall */
aPiecek8b0cc152021-05-31 16:40:31 +02008891 rc = eval_function_call(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008892 LY_CHECK_RET(rc);
8893
Michal Vasko03ff5a72019-09-11 13:49:33 +02008894 goto predicate;
8895
Michal Vasko3e48bf32020-06-01 08:39:07 +02008896 case LYXP_TOKEN_OPER_PATH:
8897 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +02008898 /* AbsoluteLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02008899 rc = eval_absolute_location_path(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008900 LY_CHECK_RET(rc);
8901 break;
8902
8903 case LYXP_TOKEN_LITERAL:
8904 /* Literal */
aPiecek8b0cc152021-05-31 16:40:31 +02008905 if ((options & LYXP_SKIP_EXPR) || (options & LYXP_SCNODE_ALL)) {
8906 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008907 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008908 }
Michal Vasko004d3152020-06-11 19:59:22 +02008909 eval_literal(exp, tok_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008910 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02008911 eval_literal(exp, tok_idx, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008912 }
8913
Michal Vasko03ff5a72019-09-11 13:49:33 +02008914 goto predicate;
8915
8916 case LYXP_TOKEN_NUMBER:
8917 /* Number */
aPiecek8b0cc152021-05-31 16:40:31 +02008918 if ((options & LYXP_SKIP_EXPR) || (options & LYXP_SCNODE_ALL)) {
8919 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008920 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008921 }
Michal Vasko004d3152020-06-11 19:59:22 +02008922 rc = eval_number(NULL, exp, tok_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008923 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02008924 rc = eval_number(set->ctx, exp, tok_idx, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008925 }
8926 LY_CHECK_RET(rc);
8927
Michal Vasko03ff5a72019-09-11 13:49:33 +02008928 goto predicate;
8929
8930 default:
Michal Vasko49fec8e2022-05-24 10:28:33 +02008931 LOGVAL(set->ctx, LY_VCODE_XP_INTOK, lyxp_token2str(exp->tokens[*tok_idx]), &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008932 return LY_EVALID;
8933 }
8934
8935 return LY_SUCCESS;
8936
8937predicate:
8938 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02008939 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02008940 rc = eval_predicate(exp, tok_idx, set, options, LYXP_AXIS_CHILD);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008941 LY_CHECK_RET(rc);
8942 }
8943
8944 /* ('/' or '//') RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02008945 if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008946
8947 /* evaluate '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02008948 if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008949 all_desc = 0;
8950 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008951 all_desc = 1;
8952 }
8953
aPiecek8b0cc152021-05-31 16:40:31 +02008954 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008955 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008956 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008957
Michal Vasko004d3152020-06-11 19:59:22 +02008958 rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008959 LY_CHECK_RET(rc);
8960 }
8961
8962 return LY_SUCCESS;
8963}
8964
8965/**
8966 * @brief Evaluate UnionExpr. Logs directly on error.
8967 *
Michal Vaskod3678892020-05-21 10:06:58 +02008968 * [20] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008969 *
8970 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008971 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008972 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008973 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008974 * @param[in] options XPath options.
8975 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8976 */
8977static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02008978eval_union_expr(const struct lyxp_expr *exp, uint32_t *tok_idx, uint32_t repeat, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008979{
8980 LY_ERR rc = LY_SUCCESS;
8981 struct lyxp_set orig_set, set2;
Michal Vaskodd528af2022-08-08 14:35:07 +02008982 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008983
8984 assert(repeat);
8985
8986 set_init(&orig_set, set);
8987 set_init(&set2, set);
8988
8989 set_fill_set(&orig_set, set);
8990
Michal Vasko004d3152020-06-11 19:59:22 +02008991 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008992 LY_CHECK_GOTO(rc, cleanup);
8993
8994 /* ('|' PathExpr)* */
8995 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008996 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_UNI);
aPiecek8b0cc152021-05-31 16:40:31 +02008997 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008998 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008999 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009000
aPiecek8b0cc152021-05-31 16:40:31 +02009001 if (options & LYXP_SKIP_EXPR) {
9002 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009003 LY_CHECK_GOTO(rc, cleanup);
9004 continue;
9005 }
9006
9007 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02009008 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009009 LY_CHECK_GOTO(rc, cleanup);
9010
9011 /* eval */
9012 if (options & LYXP_SCNODE_ALL) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01009013 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009014 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009015 rc = moveto_union(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009016 LY_CHECK_GOTO(rc, cleanup);
9017 }
9018 }
9019
9020cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02009021 lyxp_set_free_content(&orig_set);
9022 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009023 return rc;
9024}
9025
9026/**
9027 * @brief Evaluate UnaryExpr. Logs directly on error.
9028 *
Michal Vaskod3678892020-05-21 10:06:58 +02009029 * [19] UnaryExpr ::= UnionExpr | '-' UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02009030 *
9031 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02009032 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009033 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02009034 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009035 * @param[in] options XPath options.
9036 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
9037 */
9038static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02009039eval_unary_expr(const struct lyxp_expr *exp, uint32_t *tok_idx, uint32_t repeat, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02009040{
9041 LY_ERR rc;
Michal Vaskodd528af2022-08-08 14:35:07 +02009042 uint32_t this_op, i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009043
9044 assert(repeat);
9045
9046 /* ('-')+ */
Michal Vasko004d3152020-06-11 19:59:22 +02009047 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009048 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02009049 assert(!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) && (exp->expr[exp->tok_pos[*tok_idx]] == '-'));
Michal Vasko03ff5a72019-09-11 13:49:33 +02009050
aPiecek8b0cc152021-05-31 16:40:31 +02009051 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02009052 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02009053 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009054 }
9055
Michal Vasko004d3152020-06-11 19:59:22 +02009056 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNARY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009057 LY_CHECK_RET(rc);
9058
aPiecek8b0cc152021-05-31 16:40:31 +02009059 if (!(options & LYXP_SKIP_EXPR) && (repeat % 2)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02009060 if (options & LYXP_SCNODE_ALL) {
9061 warn_operands(set->ctx, set, NULL, 1, exp->expr, exp->tok_pos[this_op]);
9062 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009063 rc = moveto_op_math(set, NULL, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009064 LY_CHECK_RET(rc);
9065 }
9066 }
9067
9068 return LY_SUCCESS;
9069}
9070
9071/**
9072 * @brief Evaluate MultiplicativeExpr. Logs directly on error.
9073 *
Michal Vaskod3678892020-05-21 10:06:58 +02009074 * [18] MultiplicativeExpr ::= UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02009075 * | MultiplicativeExpr '*' UnaryExpr
9076 * | MultiplicativeExpr 'div' UnaryExpr
9077 * | MultiplicativeExpr 'mod' UnaryExpr
9078 *
9079 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02009080 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009081 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02009082 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009083 * @param[in] options XPath options.
9084 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
9085 */
9086static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02009087eval_multiplicative_expr(const struct lyxp_expr *exp, uint32_t *tok_idx, uint32_t repeat, struct lyxp_set *set,
Michal Vasko40308e72020-10-20 16:38:40 +02009088 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02009089{
9090 LY_ERR rc;
Michal Vaskodd528af2022-08-08 14:35:07 +02009091 uint32_t i, this_op;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009092 struct lyxp_set orig_set, set2;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009093
9094 assert(repeat);
9095
9096 set_init(&orig_set, set);
9097 set_init(&set2, set);
9098
9099 set_fill_set(&orig_set, set);
9100
Michal Vasko004d3152020-06-11 19:59:22 +02009101 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009102 LY_CHECK_GOTO(rc, cleanup);
9103
9104 /* ('*' / 'div' / 'mod' UnaryExpr)* */
9105 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02009106 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009107
Michal Vasko004d3152020-06-11 19:59:22 +02009108 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH);
aPiecek8b0cc152021-05-31 16:40:31 +02009109 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02009110 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02009111 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009112
aPiecek8b0cc152021-05-31 16:40:31 +02009113 if (options & LYXP_SKIP_EXPR) {
9114 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009115 LY_CHECK_GOTO(rc, cleanup);
9116 continue;
9117 }
9118
9119 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02009120 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009121 LY_CHECK_GOTO(rc, cleanup);
9122
9123 /* eval */
9124 if (options & LYXP_SCNODE_ALL) {
9125 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01009126 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02009127 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009128 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009129 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009130 LY_CHECK_GOTO(rc, cleanup);
9131 }
9132 }
9133
9134cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02009135 lyxp_set_free_content(&orig_set);
9136 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009137 return rc;
9138}
9139
9140/**
9141 * @brief Evaluate AdditiveExpr. Logs directly on error.
9142 *
Michal Vaskod3678892020-05-21 10:06:58 +02009143 * [17] AdditiveExpr ::= MultiplicativeExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02009144 * | AdditiveExpr '+' MultiplicativeExpr
9145 * | AdditiveExpr '-' MultiplicativeExpr
9146 *
9147 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02009148 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009149 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02009150 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009151 * @param[in] options XPath options.
9152 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
9153 */
9154static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02009155eval_additive_expr(const struct lyxp_expr *exp, uint32_t *tok_idx, uint32_t repeat, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02009156{
9157 LY_ERR rc;
Michal Vaskodd528af2022-08-08 14:35:07 +02009158 uint32_t i, this_op;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009159 struct lyxp_set orig_set, set2;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009160
9161 assert(repeat);
9162
9163 set_init(&orig_set, set);
9164 set_init(&set2, set);
9165
9166 set_fill_set(&orig_set, set);
9167
Michal Vasko004d3152020-06-11 19:59:22 +02009168 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009169 LY_CHECK_GOTO(rc, cleanup);
9170
9171 /* ('+' / '-' MultiplicativeExpr)* */
9172 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02009173 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009174
Michal Vasko004d3152020-06-11 19:59:22 +02009175 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009176 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02009177 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02009178 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009179
aPiecek8b0cc152021-05-31 16:40:31 +02009180 if (options & LYXP_SKIP_EXPR) {
9181 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009182 LY_CHECK_GOTO(rc, cleanup);
9183 continue;
9184 }
9185
9186 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02009187 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009188 LY_CHECK_GOTO(rc, cleanup);
9189
9190 /* eval */
9191 if (options & LYXP_SCNODE_ALL) {
9192 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01009193 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02009194 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009195 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009196 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009197 LY_CHECK_GOTO(rc, cleanup);
9198 }
9199 }
9200
9201cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02009202 lyxp_set_free_content(&orig_set);
9203 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009204 return rc;
9205}
9206
9207/**
9208 * @brief Evaluate RelationalExpr. Logs directly on error.
9209 *
Michal Vaskod3678892020-05-21 10:06:58 +02009210 * [16] RelationalExpr ::= AdditiveExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02009211 * | RelationalExpr '<' AdditiveExpr
9212 * | RelationalExpr '>' AdditiveExpr
9213 * | RelationalExpr '<=' AdditiveExpr
9214 * | RelationalExpr '>=' AdditiveExpr
9215 *
9216 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02009217 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009218 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02009219 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009220 * @param[in] options XPath options.
9221 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
9222 */
9223static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02009224eval_relational_expr(const struct lyxp_expr *exp, uint32_t *tok_idx, uint32_t repeat, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02009225{
9226 LY_ERR rc;
Michal Vaskodd528af2022-08-08 14:35:07 +02009227 uint32_t i, this_op;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009228 struct lyxp_set orig_set, set2;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009229
9230 assert(repeat);
9231
9232 set_init(&orig_set, set);
9233 set_init(&set2, set);
9234
9235 set_fill_set(&orig_set, set);
9236
Michal Vasko004d3152020-06-11 19:59:22 +02009237 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009238 LY_CHECK_GOTO(rc, cleanup);
9239
9240 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
9241 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02009242 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009243
Michal Vasko004d3152020-06-11 19:59:22 +02009244 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_COMP);
aPiecek8b0cc152021-05-31 16:40:31 +02009245 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02009246 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02009247 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009248
aPiecek8b0cc152021-05-31 16:40:31 +02009249 if (options & LYXP_SKIP_EXPR) {
9250 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009251 LY_CHECK_GOTO(rc, cleanup);
9252 continue;
9253 }
9254
9255 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02009256 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009257 LY_CHECK_GOTO(rc, cleanup);
9258
9259 /* eval */
9260 if (options & LYXP_SCNODE_ALL) {
9261 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01009262 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02009263 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009264 } else {
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02009265 ly_bool result;
9266
9267 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], &result);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009268 LY_CHECK_GOTO(rc, cleanup);
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02009269 set_fill_boolean(set, result);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009270 }
9271 }
9272
9273cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02009274 lyxp_set_free_content(&orig_set);
9275 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009276 return rc;
9277}
9278
9279/**
9280 * @brief Evaluate EqualityExpr. Logs directly on error.
9281 *
Michal Vaskod3678892020-05-21 10:06:58 +02009282 * [15] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02009283 * | EqualityExpr '!=' RelationalExpr
9284 *
9285 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02009286 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009287 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02009288 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009289 * @param[in] options XPath options.
9290 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
9291 */
9292static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02009293eval_equality_expr(const struct lyxp_expr *exp, uint32_t *tok_idx, uint32_t repeat, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02009294{
9295 LY_ERR rc;
Michal Vaskodd528af2022-08-08 14:35:07 +02009296 uint32_t i, this_op;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009297 struct lyxp_set orig_set, set2;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009298
9299 assert(repeat);
9300
9301 set_init(&orig_set, set);
9302 set_init(&set2, set);
9303
9304 set_fill_set(&orig_set, set);
9305
Michal Vasko004d3152020-06-11 19:59:22 +02009306 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009307 LY_CHECK_GOTO(rc, cleanup);
9308
9309 /* ('=' / '!=' RelationalExpr)* */
9310 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02009311 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009312
Michal Vasko004d3152020-06-11 19:59:22 +02009313 assert((exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_EQUAL) || (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_NEQUAL));
aPiecek8b0cc152021-05-31 16:40:31 +02009314 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02009315 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02009316 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009317
aPiecek8b0cc152021-05-31 16:40:31 +02009318 if (options & LYXP_SKIP_EXPR) {
9319 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009320 LY_CHECK_GOTO(rc, cleanup);
9321 continue;
9322 }
9323
9324 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02009325 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009326 LY_CHECK_GOTO(rc, cleanup);
9327
9328 /* eval */
9329 if (options & LYXP_SCNODE_ALL) {
9330 warn_operands(set->ctx, set, &set2, 0, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vasko004d3152020-06-11 19:59:22 +02009331 warn_equality_value(exp, set, *tok_idx - 1, this_op - 1, *tok_idx - 1);
9332 warn_equality_value(exp, &set2, this_op - 1, this_op - 1, *tok_idx - 1);
Michal Vaskoecd62de2019-11-13 12:35:11 +01009333 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02009334 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009335 } else {
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02009336 ly_bool result;
9337
9338 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], &result);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009339 LY_CHECK_GOTO(rc, cleanup);
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02009340 set_fill_boolean(set, result);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009341 }
9342 }
9343
9344cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02009345 lyxp_set_free_content(&orig_set);
9346 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009347 return rc;
9348}
9349
9350/**
9351 * @brief Evaluate AndExpr. Logs directly on error.
9352 *
Michal Vaskod3678892020-05-21 10:06:58 +02009353 * [14] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02009354 *
9355 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02009356 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009357 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02009358 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009359 * @param[in] options XPath options.
9360 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
9361 */
9362static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02009363eval_and_expr(const struct lyxp_expr *exp, uint32_t *tok_idx, uint32_t repeat, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02009364{
9365 LY_ERR rc;
9366 struct lyxp_set orig_set, set2;
Michal Vaskodd528af2022-08-08 14:35:07 +02009367 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009368
9369 assert(repeat);
9370
9371 set_init(&orig_set, set);
9372 set_init(&set2, set);
9373
9374 set_fill_set(&orig_set, set);
9375
Michal Vasko004d3152020-06-11 19:59:22 +02009376 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009377 LY_CHECK_GOTO(rc, cleanup);
9378
Michal Vasko3ff82dd2023-05-09 14:09:21 +02009379 if (!(options & LYXP_SKIP_EXPR)) {
9380 if (options & LYXP_SCNODE_ALL) {
9381 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
9382 } else {
9383 /* cast to boolean, we know that will be the final result */
9384 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
9385 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02009386 }
9387
9388 /* ('and' EqualityExpr)* */
9389 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02009390 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG);
aPiecek8b0cc152021-05-31 16:40:31 +02009391 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, ((options & LYXP_SKIP_EXPR) || !set->val.bln ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02009392 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02009393 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009394
9395 /* lazy evaluation */
aPiecek8b0cc152021-05-31 16:40:31 +02009396 if ((options & LYXP_SKIP_EXPR) || ((set->type == LYXP_SET_BOOLEAN) && !set->val.bln)) {
9397 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009398 LY_CHECK_GOTO(rc, cleanup);
9399 continue;
9400 }
9401
9402 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02009403 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009404 LY_CHECK_GOTO(rc, cleanup);
9405
9406 /* eval - just get boolean value actually */
9407 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vasko1a09b212021-05-06 13:00:10 +02009408 set_scnode_clear_ctx(&set2, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskoecd62de2019-11-13 12:35:11 +01009409 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009410 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009411 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009412 set_fill_set(set, &set2);
9413 }
9414 }
9415
9416cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02009417 lyxp_set_free_content(&orig_set);
9418 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009419 return rc;
9420}
9421
9422/**
9423 * @brief Evaluate OrExpr. Logs directly on error.
9424 *
Michal Vaskod3678892020-05-21 10:06:58 +02009425 * [13] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02009426 *
9427 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02009428 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009429 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02009430 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009431 * @param[in] options XPath options.
9432 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
9433 */
9434static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02009435eval_or_expr(const struct lyxp_expr *exp, uint32_t *tok_idx, uint32_t repeat, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02009436{
9437 LY_ERR rc;
9438 struct lyxp_set orig_set, set2;
Michal Vaskodd528af2022-08-08 14:35:07 +02009439 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009440
9441 assert(repeat);
9442
9443 set_init(&orig_set, set);
9444 set_init(&set2, set);
9445
9446 set_fill_set(&orig_set, set);
9447
Michal Vasko004d3152020-06-11 19:59:22 +02009448 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009449 LY_CHECK_GOTO(rc, cleanup);
9450
Michal Vasko3ff82dd2023-05-09 14:09:21 +02009451 if (!(options & LYXP_SKIP_EXPR)) {
9452 if (options & LYXP_SCNODE_ALL) {
9453 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
9454 } else {
9455 /* cast to boolean, we know that will be the final result */
9456 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
9457 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02009458 }
9459
9460 /* ('or' AndExpr)* */
9461 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02009462 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG);
aPiecek8b0cc152021-05-31 16:40:31 +02009463 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, ((options & LYXP_SKIP_EXPR) || set->val.bln ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02009464 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02009465 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009466
9467 /* lazy evaluation */
aPiecek8b0cc152021-05-31 16:40:31 +02009468 if ((options & LYXP_SKIP_EXPR) || ((set->type == LYXP_SET_BOOLEAN) && set->val.bln)) {
9469 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009470 LY_CHECK_GOTO(rc, cleanup);
9471 continue;
9472 }
9473
9474 set_fill_set(&set2, &orig_set);
9475 /* expr_type cound have been LYXP_EXPR_NONE in all these later calls (except for the first one),
9476 * but it does not matter */
Michal Vasko004d3152020-06-11 19:59:22 +02009477 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009478 LY_CHECK_GOTO(rc, cleanup);
9479
9480 /* eval - just get boolean value actually */
9481 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vasko1a09b212021-05-06 13:00:10 +02009482 set_scnode_clear_ctx(&set2, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskoecd62de2019-11-13 12:35:11 +01009483 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009484 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009485 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009486 set_fill_set(set, &set2);
9487 }
9488 }
9489
9490cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02009491 lyxp_set_free_content(&orig_set);
9492 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009493 return rc;
9494}
9495
9496/**
Michal Vasko004d3152020-06-11 19:59:22 +02009497 * @brief Decide what expression is at the pointer @p tok_idx and evaluate it accordingly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009498 *
9499 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02009500 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009501 * @param[in] etype Expression type to evaluate.
aPiecek8b0cc152021-05-31 16:40:31 +02009502 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009503 * @param[in] options XPath options.
9504 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
9505 */
9506static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02009507eval_expr_select(const struct lyxp_expr *exp, uint32_t *tok_idx, enum lyxp_expr_type etype, struct lyxp_set *set,
Michal Vasko40308e72020-10-20 16:38:40 +02009508 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02009509{
Michal Vaskodd528af2022-08-08 14:35:07 +02009510 uint32_t i, count;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009511 enum lyxp_expr_type next_etype;
9512 LY_ERR rc;
9513
9514 /* process operator repeats */
Michal Vasko004d3152020-06-11 19:59:22 +02009515 if (!exp->repeat[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02009516 next_etype = LYXP_EXPR_NONE;
9517 } else {
9518 /* find etype repeat */
Radek Krejci1e008d22020-08-17 11:37:37 +02009519 for (i = 0; exp->repeat[*tok_idx][i] > etype; ++i) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02009520
9521 /* select one-priority lower because etype expression called us */
9522 if (i) {
Michal Vasko004d3152020-06-11 19:59:22 +02009523 next_etype = exp->repeat[*tok_idx][i - 1];
Michal Vasko03ff5a72019-09-11 13:49:33 +02009524 /* count repeats for that expression */
Radek Krejci1e008d22020-08-17 11:37:37 +02009525 for (count = 0; i && exp->repeat[*tok_idx][i - 1] == next_etype; ++count, --i) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02009526 } else {
9527 next_etype = LYXP_EXPR_NONE;
9528 }
9529 }
9530
9531 /* decide what expression are we parsing based on the repeat */
9532 switch (next_etype) {
9533 case LYXP_EXPR_OR:
Michal Vasko004d3152020-06-11 19:59:22 +02009534 rc = eval_or_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009535 break;
9536 case LYXP_EXPR_AND:
Michal Vasko004d3152020-06-11 19:59:22 +02009537 rc = eval_and_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009538 break;
9539 case LYXP_EXPR_EQUALITY:
Michal Vasko004d3152020-06-11 19:59:22 +02009540 rc = eval_equality_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009541 break;
9542 case LYXP_EXPR_RELATIONAL:
Michal Vasko004d3152020-06-11 19:59:22 +02009543 rc = eval_relational_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009544 break;
9545 case LYXP_EXPR_ADDITIVE:
Michal Vasko004d3152020-06-11 19:59:22 +02009546 rc = eval_additive_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009547 break;
9548 case LYXP_EXPR_MULTIPLICATIVE:
Michal Vasko004d3152020-06-11 19:59:22 +02009549 rc = eval_multiplicative_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009550 break;
9551 case LYXP_EXPR_UNARY:
Michal Vasko004d3152020-06-11 19:59:22 +02009552 rc = eval_unary_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009553 break;
9554 case LYXP_EXPR_UNION:
Michal Vasko004d3152020-06-11 19:59:22 +02009555 rc = eval_union_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009556 break;
9557 case LYXP_EXPR_NONE:
Michal Vasko004d3152020-06-11 19:59:22 +02009558 rc = eval_path_expr(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009559 break;
9560 default:
9561 LOGINT_RET(set->ctx);
9562 }
9563
9564 return rc;
9565}
9566
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009567/**
9568 * @brief Get root type.
9569 *
9570 * @param[in] ctx_node Context node.
9571 * @param[in] ctx_scnode Schema context node.
9572 * @param[in] options XPath options.
9573 * @return Root type.
9574 */
9575static enum lyxp_node_type
Radek Krejci1deb5be2020-08-26 16:43:36 +02009576lyxp_get_root_type(const struct lyd_node *ctx_node, const struct lysc_node *ctx_scnode, uint32_t options)
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009577{
Michal Vasko6b26e742020-07-17 15:02:10 +02009578 const struct lysc_node *op;
9579
Michal Vaskoa27245c2022-05-02 09:01:35 +02009580 /* explicit */
9581 if (options & LYXP_ACCESS_TREE_ALL) {
9582 return LYXP_NODE_ROOT;
9583 } else if (options & LYXP_ACCESS_TREE_CONFIG) {
9584 return LYXP_NODE_ROOT_CONFIG;
9585 }
9586
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009587 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009588 /* schema */
Radek Krejci1e008d22020-08-17 11:37:37 +02009589 for (op = ctx_scnode; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent) {}
Michal Vasko6b26e742020-07-17 15:02:10 +02009590
9591 if (op || (options & LYXP_SCNODE)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009592 /* general root that can access everything */
9593 return LYXP_NODE_ROOT;
9594 } else if (!ctx_scnode || (ctx_scnode->flags & LYS_CONFIG_W)) {
9595 /* root context node can access only config data (because we said so, it is unspecified) */
9596 return LYXP_NODE_ROOT_CONFIG;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009597 }
Michal Vasko6b26e742020-07-17 15:02:10 +02009598 return LYXP_NODE_ROOT;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009599 }
9600
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009601 /* data */
Michal Vasko6b26e742020-07-17 15:02:10 +02009602 op = ctx_node ? ctx_node->schema : NULL;
Michal Vaskod989ba02020-08-24 10:59:24 +02009603 for ( ; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent) {}
Michal Vasko6b26e742020-07-17 15:02:10 +02009604
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009605 if (op || !(options & LYXP_SCHEMA)) {
9606 /* general root that can access everything */
9607 return LYXP_NODE_ROOT;
Christian Hoppsb6ecaea2021-02-06 09:45:38 -05009608 } else if (!ctx_node || !ctx_node->schema || (ctx_node->schema->flags & LYS_CONFIG_W)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009609 /* root context node can access only config data (because we said so, it is unspecified) */
9610 return LYXP_NODE_ROOT_CONFIG;
9611 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009612 return LYXP_NODE_ROOT;
9613}
9614
Michal Vasko03ff5a72019-09-11 13:49:33 +02009615LY_ERR
Michal Vasko400e9672021-01-11 13:39:17 +01009616lyxp_eval(const struct ly_ctx *ctx, const struct lyxp_expr *exp, const struct lys_module *cur_mod,
Michal Vaskoa3e92bc2022-07-29 14:56:23 +02009617 LY_VALUE_FORMAT format, void *prefix_data, const struct lyd_node *cur_node, const struct lyd_node *ctx_node,
9618 const struct lyd_node *tree, const struct lyxp_var *vars, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02009619{
Michal Vaskodd528af2022-08-08 14:35:07 +02009620 uint32_t tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009621 LY_ERR rc;
9622
Michal Vasko400e9672021-01-11 13:39:17 +01009623 LY_CHECK_ARG_RET(ctx, ctx, exp, set, LY_EINVAL);
Radek Krejci8df109d2021-04-23 12:19:08 +02009624 if (!cur_mod && ((format == LY_VALUE_SCHEMA) || (format == LY_VALUE_SCHEMA_RESOLVED))) {
Michal Vaskoddd76592022-01-17 13:34:48 +01009625 LOGERR(ctx, LY_EINVAL, "Current module must be set if schema format is used.");
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009626 return LY_EINVAL;
Michal Vasko004d3152020-06-11 19:59:22 +02009627 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02009628
Michal Vaskod3bb12f2020-12-04 14:33:09 +01009629 if (tree) {
9630 /* adjust the pointer to be the first top-level sibling */
9631 while (tree->parent) {
9632 tree = lyd_parent(tree);
9633 }
9634 tree = lyd_first_sibling(tree);
Michal Vaskoddd76592022-01-17 13:34:48 +01009635
Michal Vasko6f78a772022-11-10 08:13:52 +01009636 if (lysc_data_parent(tree->schema)) {
Michal Vaskoddd76592022-01-17 13:34:48 +01009637 /* unable to evaluate absolute paths */
9638 LOGERR(ctx, LY_EINVAL, "Data node \"%s\" has no parent but is not instance of a top-level schema node.",
9639 LYD_NAME(tree));
9640 return LY_EINVAL;
9641 }
Michal Vaskod3bb12f2020-12-04 14:33:09 +01009642 }
9643
Michal Vasko03ff5a72019-09-11 13:49:33 +02009644 /* prepare set for evaluation */
Michal Vasko03ff5a72019-09-11 13:49:33 +02009645 memset(set, 0, sizeof *set);
Michal Vaskod3678892020-05-21 10:06:58 +02009646 set->type = LYXP_SET_NODE_SET;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009647 set->root_type = lyxp_get_root_type(ctx_node, NULL, options);
9648 set_insert_node(set, (struct lyd_node *)ctx_node, 0, ctx_node ? LYXP_NODE_ELEM : set->root_type, 0);
9649
Michal Vasko400e9672021-01-11 13:39:17 +01009650 set->ctx = (struct ly_ctx *)ctx;
Michal Vaskoa3e92bc2022-07-29 14:56:23 +02009651 set->cur_node = cur_node;
9652 for (set->context_op = cur_node ? cur_node->schema : NULL;
Radek Krejci0f969882020-08-21 16:56:47 +02009653 set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF));
9654 set->context_op = set->context_op->parent) {}
Michal Vaskof03ed032020-03-04 13:31:44 +01009655 set->tree = tree;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009656 set->cur_mod = cur_mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009657 set->format = format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009658 set->prefix_data = prefix_data;
aPiecekfba75362021-10-07 12:39:48 +02009659 set->vars = vars;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009660
Radek Krejciddace2c2021-01-08 11:30:56 +01009661 LOG_LOCSET(NULL, set->cur_node, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01009662
Michal Vasko03ff5a72019-09-11 13:49:33 +02009663 /* evaluate */
Michal Vasko004d3152020-06-11 19:59:22 +02009664 rc = eval_expr_select(exp, &tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009665 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02009666 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009667 }
9668
Michal Vasko4a7d4d62021-12-13 17:05:06 +01009669 if (set->cur_node) {
9670 LOG_LOCBACK(0, 1, 0, 0);
9671 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02009672 return rc;
9673}
9674
9675#if 0
9676
9677/* full xml printing of set elements, not used currently */
9678
9679void
9680lyxp_set_print_xml(FILE *f, struct lyxp_set *set)
9681{
9682 uint32_t i;
9683 char *str_num;
9684 struct lyout out;
9685
9686 memset(&out, 0, sizeof out);
9687
9688 out.type = LYOUT_STREAM;
9689 out.method.f = f;
9690
9691 switch (set->type) {
9692 case LYXP_SET_EMPTY:
Michal Vasko5233e962020-08-14 14:26:20 +02009693 ly_print_(&out, "Empty XPath set\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02009694 break;
9695 case LYXP_SET_BOOLEAN:
Michal Vasko5233e962020-08-14 14:26:20 +02009696 ly_print_(&out, "Boolean XPath set:\n");
9697 ly_print_(&out, "%s\n\n", set->value.bool ? "true" : "false");
Michal Vasko03ff5a72019-09-11 13:49:33 +02009698 break;
9699 case LYXP_SET_STRING:
Michal Vasko5233e962020-08-14 14:26:20 +02009700 ly_print_(&out, "String XPath set:\n");
9701 ly_print_(&out, "\"%s\"\n\n", set->value.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009702 break;
9703 case LYXP_SET_NUMBER:
Michal Vasko5233e962020-08-14 14:26:20 +02009704 ly_print_(&out, "Number XPath set:\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02009705
9706 if (isnan(set->value.num)) {
9707 str_num = strdup("NaN");
9708 } else if ((set->value.num == 0) || (set->value.num == -0.0f)) {
9709 str_num = strdup("0");
9710 } else if (isinf(set->value.num) && !signbit(set->value.num)) {
9711 str_num = strdup("Infinity");
9712 } else if (isinf(set->value.num) && signbit(set->value.num)) {
9713 str_num = strdup("-Infinity");
9714 } else if ((long long)set->value.num == set->value.num) {
9715 if (asprintf(&str_num, "%lld", (long long)set->value.num) == -1) {
9716 str_num = NULL;
9717 }
9718 } else {
9719 if (asprintf(&str_num, "%03.1Lf", set->value.num) == -1) {
9720 str_num = NULL;
9721 }
9722 }
9723 if (!str_num) {
9724 LOGMEM;
9725 return;
9726 }
Michal Vasko5233e962020-08-14 14:26:20 +02009727 ly_print_(&out, "%s\n\n", str_num);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009728 free(str_num);
9729 break;
9730 case LYXP_SET_NODE_SET:
Michal Vasko5233e962020-08-14 14:26:20 +02009731 ly_print_(&out, "Node XPath set:\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02009732
9733 for (i = 0; i < set->used; ++i) {
Michal Vasko5233e962020-08-14 14:26:20 +02009734 ly_print_(&out, "%d. ", i + 1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009735 switch (set->node_type[i]) {
9736 case LYXP_NODE_ROOT_ALL:
Michal Vasko5233e962020-08-14 14:26:20 +02009737 ly_print_(&out, "ROOT all\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02009738 break;
9739 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko5233e962020-08-14 14:26:20 +02009740 ly_print_(&out, "ROOT config\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02009741 break;
9742 case LYXP_NODE_ROOT_STATE:
Michal Vasko5233e962020-08-14 14:26:20 +02009743 ly_print_(&out, "ROOT state\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02009744 break;
9745 case LYXP_NODE_ROOT_NOTIF:
Michal Vasko5233e962020-08-14 14:26:20 +02009746 ly_print_(&out, "ROOT notification \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009747 break;
9748 case LYXP_NODE_ROOT_RPC:
Michal Vasko5233e962020-08-14 14:26:20 +02009749 ly_print_(&out, "ROOT rpc \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009750 break;
9751 case LYXP_NODE_ROOT_OUTPUT:
Michal Vasko5233e962020-08-14 14:26:20 +02009752 ly_print_(&out, "ROOT output \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009753 break;
9754 case LYXP_NODE_ELEM:
Michal Vasko5233e962020-08-14 14:26:20 +02009755 ly_print_(&out, "ELEM \"%s\"\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009756 xml_print_node(&out, 1, set->value.nodes[i], 1, LYP_FORMAT);
Michal Vasko5233e962020-08-14 14:26:20 +02009757 ly_print_(&out, "\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02009758 break;
9759 case LYXP_NODE_TEXT:
Michal Vasko5233e962020-08-14 14:26:20 +02009760 ly_print_(&out, "TEXT \"%s\"\n\n", ((struct lyd_node_leaf_list *)set->value.nodes[i])->value_str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009761 break;
9762 case LYXP_NODE_ATTR:
Michal Vasko5233e962020-08-14 14:26:20 +02009763 ly_print_(&out, "ATTR \"%s\" = \"%s\"\n\n", set->value.attrs[i]->name, set->value.attrs[i]->value);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009764 break;
9765 }
9766 }
9767 break;
9768 }
9769}
9770
9771#endif
9772
9773LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009774lyxp_set_cast(struct lyxp_set *set, enum lyxp_set_type target)
Michal Vasko03ff5a72019-09-11 13:49:33 +02009775{
9776 long double num;
9777 char *str;
9778 LY_ERR rc;
9779
9780 if (!set || (set->type == target)) {
9781 return LY_SUCCESS;
9782 }
9783
9784 /* it's not possible to convert anything into a node set */
Michal Vaskod3678892020-05-21 10:06:58 +02009785 assert(target != LYXP_SET_NODE_SET);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009786
9787 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vaskod3678892020-05-21 10:06:58 +02009788 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009789 return LY_EINVAL;
9790 }
9791
9792 /* to STRING */
Michal Vaskod3678892020-05-21 10:06:58 +02009793 if ((target == LYXP_SET_STRING) || ((target == LYXP_SET_NUMBER) && (set->type == LYXP_SET_NODE_SET))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02009794 switch (set->type) {
9795 case LYXP_SET_NUMBER:
9796 if (isnan(set->val.num)) {
9797 set->val.str = strdup("NaN");
9798 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
9799 } else if ((set->val.num == 0) || (set->val.num == -0.0f)) {
9800 set->val.str = strdup("0");
9801 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
9802 } else if (isinf(set->val.num) && !signbit(set->val.num)) {
9803 set->val.str = strdup("Infinity");
9804 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
9805 } else if (isinf(set->val.num) && signbit(set->val.num)) {
9806 set->val.str = strdup("-Infinity");
9807 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
9808 } else if ((long long)set->val.num == set->val.num) {
9809 if (asprintf(&str, "%lld", (long long)set->val.num) == -1) {
9810 LOGMEM_RET(set->ctx);
9811 }
9812 set->val.str = str;
9813 } else {
9814 if (asprintf(&str, "%03.1Lf", set->val.num) == -1) {
9815 LOGMEM_RET(set->ctx);
9816 }
9817 set->val.str = str;
9818 }
9819 break;
9820 case LYXP_SET_BOOLEAN:
Michal Vasko004d3152020-06-11 19:59:22 +02009821 if (set->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02009822 set->val.str = strdup("true");
9823 } else {
9824 set->val.str = strdup("false");
9825 }
9826 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), LY_EMEM);
9827 break;
9828 case LYXP_SET_NODE_SET:
Michal Vasko03ff5a72019-09-11 13:49:33 +02009829 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009830 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02009831
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009832 rc = cast_node_set_to_string(set, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009833 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02009834 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009835 set->val.str = str;
9836 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009837 default:
9838 LOGINT_RET(set->ctx);
9839 }
9840 set->type = LYXP_SET_STRING;
9841 }
9842
9843 /* to NUMBER */
9844 if (target == LYXP_SET_NUMBER) {
9845 switch (set->type) {
9846 case LYXP_SET_STRING:
9847 num = cast_string_to_number(set->val.str);
Michal Vaskod3678892020-05-21 10:06:58 +02009848 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009849 set->val.num = num;
9850 break;
9851 case LYXP_SET_BOOLEAN:
Michal Vasko004d3152020-06-11 19:59:22 +02009852 if (set->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02009853 set->val.num = 1;
9854 } else {
9855 set->val.num = 0;
9856 }
9857 break;
9858 default:
9859 LOGINT_RET(set->ctx);
9860 }
9861 set->type = LYXP_SET_NUMBER;
9862 }
9863
9864 /* to BOOLEAN */
9865 if (target == LYXP_SET_BOOLEAN) {
9866 switch (set->type) {
9867 case LYXP_SET_NUMBER:
9868 if ((set->val.num == 0) || (set->val.num == -0.0f) || isnan(set->val.num)) {
Michal Vasko004d3152020-06-11 19:59:22 +02009869 set->val.bln = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009870 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02009871 set->val.bln = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009872 }
9873 break;
9874 case LYXP_SET_STRING:
9875 if (set->val.str[0]) {
Michal Vaskod3678892020-05-21 10:06:58 +02009876 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02009877 set->val.bln = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009878 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02009879 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02009880 set->val.bln = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009881 }
9882 break;
9883 case LYXP_SET_NODE_SET:
Michal Vaskod3678892020-05-21 10:06:58 +02009884 if (set->used) {
9885 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02009886 set->val.bln = 1;
Michal Vaskod3678892020-05-21 10:06:58 +02009887 } else {
9888 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02009889 set->val.bln = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02009890 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02009891 break;
9892 default:
9893 LOGINT_RET(set->ctx);
9894 }
9895 set->type = LYXP_SET_BOOLEAN;
9896 }
9897
Michal Vasko03ff5a72019-09-11 13:49:33 +02009898 return LY_SUCCESS;
9899}
9900
9901LY_ERR
Michal Vasko400e9672021-01-11 13:39:17 +01009902lyxp_atomize(const struct ly_ctx *ctx, const struct lyxp_expr *exp, const struct lys_module *cur_mod,
Michal Vaskoa3e92bc2022-07-29 14:56:23 +02009903 LY_VALUE_FORMAT format, void *prefix_data, const struct lysc_node *cur_scnode,
9904 const struct lysc_node *ctx_scnode, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02009905{
Radek Krejci2efc45b2020-12-22 16:25:44 +01009906 LY_ERR ret;
Michal Vaskodd528af2022-08-08 14:35:07 +02009907 uint32_t tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009908
Michal Vasko400e9672021-01-11 13:39:17 +01009909 LY_CHECK_ARG_RET(ctx, ctx, exp, set, LY_EINVAL);
Radek Krejci8df109d2021-04-23 12:19:08 +02009910 if (!cur_mod && ((format == LY_VALUE_SCHEMA) || (format == LY_VALUE_SCHEMA_RESOLVED))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009911 LOGARG(NULL, "Current module must be set if schema format is used.");
9912 return LY_EINVAL;
Michal Vasko004d3152020-06-11 19:59:22 +02009913 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02009914
9915 /* prepare set for evaluation */
Michal Vasko03ff5a72019-09-11 13:49:33 +02009916 memset(set, 0, sizeof *set);
9917 set->type = LYXP_SET_SCNODE_SET;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009918 set->root_type = lyxp_get_root_type(NULL, ctx_scnode, options);
Michal Vaskoe4a6d012023-05-22 14:34:52 +02009919 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, ctx_scnode, ctx_scnode ? LYXP_NODE_ELEM : set->root_type, LYXP_AXIS_SELF, NULL));
Radek Krejcif13b87b2020-12-01 22:02:17 +01009920 set->val.scnodes[0].in_ctx = LYXP_SET_SCNODE_START;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009921
Michal Vasko400e9672021-01-11 13:39:17 +01009922 set->ctx = (struct ly_ctx *)ctx;
Michal Vaskoa3e92bc2022-07-29 14:56:23 +02009923 set->cur_scnode = cur_scnode;
9924 for (set->context_op = cur_scnode;
Radek Krejci0f969882020-08-21 16:56:47 +02009925 set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF));
9926 set->context_op = set->context_op->parent) {}
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009927 set->cur_mod = cur_mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009928 set->format = format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009929 set->prefix_data = prefix_data;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009930
Radek Krejciddace2c2021-01-08 11:30:56 +01009931 LOG_LOCSET(set->cur_scnode, NULL, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01009932
Michal Vasko03ff5a72019-09-11 13:49:33 +02009933 /* evaluate */
Radek Krejci2efc45b2020-12-22 16:25:44 +01009934 ret = eval_expr_select(exp, &tok_idx, 0, set, options);
9935
Michal Vaskof69e4552022-12-02 10:27:42 +01009936 LOG_LOCBACK(set->cur_scnode ? 1 : 0, 0, 0, 0);
Radek Krejci2efc45b2020-12-22 16:25:44 +01009937 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009938}
Michal Vaskod43d71a2020-08-07 14:54:58 +02009939
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01009940LIBYANG_API_DEF const char *
Michal Vaskod43d71a2020-08-07 14:54:58 +02009941lyxp_get_expr(const struct lyxp_expr *path)
9942{
9943 if (!path) {
9944 return NULL;
9945 }
9946
9947 return path->expr;
9948}