blob: bc5f695b54be8f7e0c81a9c85abf3f776cfcf18d [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 */
Jan Kundrát3a7d5f72023-11-02 08:02:07 +0100239 p = vsnprintf(*str ? *str + *used : NULL, *size - *used, format, ap);
Michal Vasko16961782023-11-01 10:55:36 +0100240
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 Vaskob7ba2c92024-01-05 15:17:53 +0100902 if (!set) {
903 return;
Michal Vasko02a77382019-09-12 11:47:35 +0200904 }
Michal Vaskob7ba2c92024-01-05 15:17:53 +0100905
906 new->non_child_axis = set->non_child_axis;
907 new->not_found = set->not_found;
908 new->ctx = set->ctx;
909 new->cur_node = set->cur_node;
910 new->root_type = set->root_type;
911 new->context_op = set->context_op;
912 new->tree = set->tree;
913 new->cur_mod = set->cur_mod;
914 new->format = set->format;
915 new->prefix_data = set->prefix_data;
916 new->vars = set->vars;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200917}
918
919/**
920 * @brief Create a deep copy of a set.
921 *
922 * @param[in] set Set to copy.
923 * @return Copy of @p set.
924 */
925static struct lyxp_set *
926set_copy(struct lyxp_set *set)
927{
928 struct lyxp_set *ret;
Michal Vasko1fdd8fa2021-01-08 09:21:45 +0100929 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200930
931 if (!set) {
932 return NULL;
933 }
934
935 ret = malloc(sizeof *ret);
936 LY_CHECK_ERR_RET(!ret, LOGMEM(set->ctx), NULL);
937 set_init(ret, set);
938
939 if (set->type == LYXP_SET_SCNODE_SET) {
940 ret->type = set->type;
941
942 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100943 if ((set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) ||
944 (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +0200945 uint32_t idx;
Michal Vasko26bbb272022-08-02 14:54:33 +0200946
Michal Vaskoe4a6d012023-05-22 14:34:52 +0200947 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 +0200948 set->val.scnodes[i].axis, &idx), lyxp_set_free(ret), NULL);
Michal Vasko3f27c522020-01-06 08:37:49 +0100949 /* coverity seems to think scnodes can be NULL */
Radek Krejciaa6b53f2020-08-27 15:19:03 +0200950 if (!ret->val.scnodes) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200951 lyxp_set_free(ret);
952 return NULL;
953 }
Michal Vaskoba716542019-12-16 10:01:58 +0100954 ret->val.scnodes[idx].in_ctx = set->val.scnodes[i].in_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200955 }
956 }
957 } else if (set->type == LYXP_SET_NODE_SET) {
958 ret->type = set->type;
Michal Vasko08e9b112021-06-11 15:41:17 +0200959 if (set->used) {
960 ret->val.nodes = malloc(set->used * sizeof *ret->val.nodes);
961 LY_CHECK_ERR_RET(!ret->val.nodes, LOGMEM(set->ctx); free(ret), NULL);
962 memcpy(ret->val.nodes, set->val.nodes, set->used * sizeof *ret->val.nodes);
963 } else {
964 ret->val.nodes = NULL;
965 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200966
967 ret->used = ret->size = set->used;
968 ret->ctx_pos = set->ctx_pos;
969 ret->ctx_size = set->ctx_size;
Michal Vasko4a04e542021-02-01 08:58:30 +0100970 if (set->ht) {
971 ret->ht = lyht_dup(set->ht);
972 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200973 } else {
Radek Krejci0f969882020-08-21 16:56:47 +0200974 memcpy(ret, set, sizeof *ret);
975 if (set->type == LYXP_SET_STRING) {
976 ret->val.str = strdup(set->val.str);
977 LY_CHECK_ERR_RET(!ret->val.str, LOGMEM(set->ctx); free(ret), NULL);
978 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200979 }
980
981 return ret;
982}
983
984/**
985 * @brief Fill XPath set with a string. Any current data are disposed of.
986 *
987 * @param[in] set Set to fill.
988 * @param[in] string String to fill into \p set.
989 * @param[in] str_len Length of \p string. 0 is a valid value!
990 */
991static void
Michal Vaskodd528af2022-08-08 14:35:07 +0200992set_fill_string(struct lyxp_set *set, const char *string, uint32_t str_len)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200993{
Michal Vaskod3678892020-05-21 10:06:58 +0200994 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200995
996 set->type = LYXP_SET_STRING;
997 if ((str_len == 0) && (string[0] != '\0')) {
998 string = "";
999 }
1000 set->val.str = strndup(string, str_len);
1001}
1002
1003/**
1004 * @brief Fill XPath set with a number. Any current data are disposed of.
1005 *
1006 * @param[in] set Set to fill.
1007 * @param[in] number Number to fill into \p set.
1008 */
1009static void
1010set_fill_number(struct lyxp_set *set, long double number)
1011{
Michal Vaskod3678892020-05-21 10:06:58 +02001012 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001013
1014 set->type = LYXP_SET_NUMBER;
1015 set->val.num = number;
1016}
1017
1018/**
1019 * @brief Fill XPath set with a boolean. Any current data are disposed of.
1020 *
1021 * @param[in] set Set to fill.
1022 * @param[in] boolean Boolean to fill into \p set.
1023 */
1024static void
Radek Krejci857189e2020-09-01 13:26:36 +02001025set_fill_boolean(struct lyxp_set *set, ly_bool boolean)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001026{
Michal Vaskod3678892020-05-21 10:06:58 +02001027 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001028
1029 set->type = LYXP_SET_BOOLEAN;
Michal Vasko004d3152020-06-11 19:59:22 +02001030 set->val.bln = boolean;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001031}
1032
1033/**
1034 * @brief Fill XPath set with the value from another set (deep assign).
1035 * Any current data are disposed of.
1036 *
1037 * @param[in] trg Set to fill.
1038 * @param[in] src Source set to copy into \p trg.
1039 */
1040static void
Michal Vasko4c7763f2020-07-27 17:40:37 +02001041set_fill_set(struct lyxp_set *trg, const struct lyxp_set *src)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001042{
1043 if (!trg || !src) {
1044 return;
1045 }
1046
Michal Vaskof06d19d2023-08-04 14:04:08 +02001047 lyxp_set_free_content(trg);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001048 set_init(trg, src);
1049
1050 if (src->type == LYXP_SET_SCNODE_SET) {
1051 trg->type = LYXP_SET_SCNODE_SET;
1052 trg->used = src->used;
1053 trg->size = src->used;
1054
Michal Vasko08e9b112021-06-11 15:41:17 +02001055 if (trg->size) {
1056 trg->val.scnodes = ly_realloc(trg->val.scnodes, trg->size * sizeof *trg->val.scnodes);
1057 LY_CHECK_ERR_RET(!trg->val.scnodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), );
1058 memcpy(trg->val.scnodes, src->val.scnodes, src->used * sizeof *src->val.scnodes);
1059 } else {
1060 trg->val.scnodes = NULL;
1061 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001062 } else if (src->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +02001063 set_fill_boolean(trg, src->val.bln);
Michal Vasko44f3d2c2020-08-24 09:49:38 +02001064 } else if (src->type == LYXP_SET_NUMBER) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001065 set_fill_number(trg, src->val.num);
1066 } else if (src->type == LYXP_SET_STRING) {
1067 set_fill_string(trg, src->val.str, strlen(src->val.str));
1068 } else {
1069 if (trg->type == LYXP_SET_NODE_SET) {
1070 free(trg->val.nodes);
1071 } else if (trg->type == LYXP_SET_STRING) {
1072 free(trg->val.str);
1073 }
1074
Michal Vaskod3678892020-05-21 10:06:58 +02001075 assert(src->type == LYXP_SET_NODE_SET);
1076
1077 trg->type = LYXP_SET_NODE_SET;
1078 trg->used = src->used;
1079 trg->size = src->used;
1080 trg->ctx_pos = src->ctx_pos;
1081 trg->ctx_size = src->ctx_size;
1082
Michal Vasko08e9b112021-06-11 15:41:17 +02001083 if (trg->size) {
1084 trg->val.nodes = malloc(trg->size * sizeof *trg->val.nodes);
1085 LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), );
1086 memcpy(trg->val.nodes, src->val.nodes, src->used * sizeof *src->val.nodes);
1087 } else {
1088 trg->val.nodes = NULL;
1089 }
Michal Vaskod3678892020-05-21 10:06:58 +02001090 if (src->ht) {
1091 trg->ht = lyht_dup(src->ht);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001092 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02001093 trg->ht = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001094 }
1095 }
1096}
1097
1098/**
1099 * @brief Clear context of all schema nodes.
1100 *
1101 * @param[in] set Set to clear.
Michal Vasko1a09b212021-05-06 13:00:10 +02001102 * @param[in] new_ctx New context state for all the nodes currently in the context.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001103 */
1104static void
Michal Vasko1a09b212021-05-06 13:00:10 +02001105set_scnode_clear_ctx(struct lyxp_set *set, int32_t new_ctx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001106{
1107 uint32_t i;
1108
1109 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001110 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko1a09b212021-05-06 13:00:10 +02001111 set->val.scnodes[i].in_ctx = new_ctx;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001112 } else if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START) {
1113 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001114 }
1115 }
1116}
1117
1118/**
1119 * @brief Remove a node from a set. Removing last node changes
1120 * set into LYXP_SET_EMPTY. Context position aware.
1121 *
1122 * @param[in] set Set to use.
1123 * @param[in] idx Index from @p set of the node to be removed.
1124 */
1125static void
1126set_remove_node(struct lyxp_set *set, uint32_t idx)
1127{
1128 assert(set && (set->type == LYXP_SET_NODE_SET));
1129 assert(idx < set->used);
1130
1131 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
1132
1133 --set->used;
Michal Vasko08e9b112021-06-11 15:41:17 +02001134 if (idx < set->used) {
1135 memmove(&set->val.nodes[idx], &set->val.nodes[idx + 1], (set->used - idx) * sizeof *set->val.nodes);
1136 } else if (!set->used) {
Michal Vaskod3678892020-05-21 10:06:58 +02001137 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001138 }
1139}
1140
1141/**
Michal Vasko2caefc12019-11-14 16:07:56 +01001142 * @brief Remove a node from a set by setting its type to LYXP_NODE_NONE.
Michal Vasko57eab132019-09-24 11:46:26 +02001143 *
1144 * @param[in] set Set to use.
1145 * @param[in] idx Index from @p set of the node to be removed.
1146 */
1147static void
Michal Vasko2caefc12019-11-14 16:07:56 +01001148set_remove_node_none(struct lyxp_set *set, uint32_t idx)
Michal Vasko57eab132019-09-24 11:46:26 +02001149{
1150 assert(set && (set->type == LYXP_SET_NODE_SET));
1151 assert(idx < set->used);
1152
Michal Vasko2caefc12019-11-14 16:07:56 +01001153 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1154 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
1155 }
1156 set->val.nodes[idx].type = LYXP_NODE_NONE;
Michal Vasko57eab132019-09-24 11:46:26 +02001157}
1158
1159/**
Michal Vasko2caefc12019-11-14 16:07:56 +01001160 * @brief Remove all LYXP_NODE_NONE nodes from a set. Removing last node changes
Michal Vasko57eab132019-09-24 11:46:26 +02001161 * set into LYXP_SET_EMPTY. Context position aware.
1162 *
1163 * @param[in] set Set to consolidate.
1164 */
1165static void
Michal Vasko2caefc12019-11-14 16:07:56 +01001166set_remove_nodes_none(struct lyxp_set *set)
Michal Vasko57eab132019-09-24 11:46:26 +02001167{
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01001168 uint32_t i, orig_used, end = 0;
1169 int64_t start;
Michal Vasko57eab132019-09-24 11:46:26 +02001170
Michal Vaskod3678892020-05-21 10:06:58 +02001171 assert(set);
Michal Vasko57eab132019-09-24 11:46:26 +02001172
1173 orig_used = set->used;
1174 set->used = 0;
Michal Vaskod989ba02020-08-24 10:59:24 +02001175 for (i = 0; i < orig_used; ) {
Michal Vasko57eab132019-09-24 11:46:26 +02001176 start = -1;
1177 do {
Michal Vasko2caefc12019-11-14 16:07:56 +01001178 if ((set->val.nodes[i].type != LYXP_NODE_NONE) && (start == -1)) {
Michal Vasko57eab132019-09-24 11:46:26 +02001179 start = i;
Michal Vasko2caefc12019-11-14 16:07:56 +01001180 } else if ((start > -1) && (set->val.nodes[i].type == LYXP_NODE_NONE)) {
Michal Vasko57eab132019-09-24 11:46:26 +02001181 end = i;
1182 ++i;
1183 break;
1184 }
1185
1186 ++i;
1187 if (i == orig_used) {
1188 end = i;
1189 }
1190 } while (i < orig_used);
1191
1192 if (start > -1) {
1193 /* move the whole chunk of valid nodes together */
1194 if (set->used != (unsigned)start) {
1195 memmove(&set->val.nodes[set->used], &set->val.nodes[start], (end - start) * sizeof *set->val.nodes);
1196 }
1197 set->used += end - start;
1198 }
1199 }
Michal Vasko57eab132019-09-24 11:46:26 +02001200}
1201
1202/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02001203 * @brief Check for duplicates in a node set.
1204 *
1205 * @param[in] set Set to check.
1206 * @param[in] node Node to look for in @p set.
1207 * @param[in] node_type Type of @p node.
1208 * @param[in] skip_idx Index from @p set to skip.
1209 * @return LY_ERR
1210 */
1211static LY_ERR
1212set_dup_node_check(const struct lyxp_set *set, const struct lyd_node *node, enum lyxp_node_type node_type, int skip_idx)
1213{
1214 uint32_t i;
1215
Michal Vasko2caefc12019-11-14 16:07:56 +01001216 if (set->ht && node) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001217 return set_dup_node_hash_check(set, (struct lyd_node *)node, node_type, skip_idx);
1218 }
1219
1220 for (i = 0; i < set->used; ++i) {
1221 if ((skip_idx > -1) && (i == (unsigned)skip_idx)) {
1222 continue;
1223 }
1224
1225 if ((set->val.nodes[i].node == node) && (set->val.nodes[i].type == node_type)) {
1226 return LY_EEXIST;
1227 }
1228 }
1229
1230 return LY_SUCCESS;
1231}
1232
Radek Krejci857189e2020-09-01 13:26:36 +02001233ly_bool
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001234lyxp_set_scnode_contains(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type, int skip_idx,
1235 uint32_t *index_p)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001236{
1237 uint32_t i;
1238
1239 for (i = 0; i < set->used; ++i) {
1240 if ((skip_idx > -1) && (i == (unsigned)skip_idx)) {
1241 continue;
1242 }
1243
1244 if ((set->val.scnodes[i].scnode == node) && (set->val.scnodes[i].type == node_type)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001245 if (index_p) {
1246 *index_p = i;
1247 }
1248 return 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001249 }
1250 }
1251
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001252 return 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001253}
1254
Michal Vaskoecd62de2019-11-13 12:35:11 +01001255void
1256lyxp_set_scnode_merge(struct lyxp_set *set1, struct lyxp_set *set2)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001257{
1258 uint32_t orig_used, i, j;
1259
Michal Vaskod3678892020-05-21 10:06:58 +02001260 assert((set1->type == LYXP_SET_SCNODE_SET) && (set2->type == LYXP_SET_SCNODE_SET));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001261
Michal Vaskod3678892020-05-21 10:06:58 +02001262 if (!set2->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001263 return;
1264 }
1265
Michal Vaskod3678892020-05-21 10:06:58 +02001266 if (!set1->used) {
aPiecekadc1e4f2021-10-07 11:15:12 +02001267 /* release hidden allocated data (lyxp_set.size) */
1268 lyxp_set_free_content(set1);
1269 /* direct copying of the entire structure */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001270 memcpy(set1, set2, sizeof *set1);
1271 return;
1272 }
1273
1274 if (set1->used + set2->used > set1->size) {
1275 set1->size = set1->used + set2->used;
1276 set1->val.scnodes = ly_realloc(set1->val.scnodes, set1->size * sizeof *set1->val.scnodes);
1277 LY_CHECK_ERR_RET(!set1->val.scnodes, LOGMEM(set1->ctx), );
1278 }
1279
1280 orig_used = set1->used;
1281
1282 for (i = 0; i < set2->used; ++i) {
1283 for (j = 0; j < orig_used; ++j) {
1284 /* detect duplicities */
1285 if (set1->val.scnodes[j].scnode == set2->val.scnodes[i].scnode) {
1286 break;
1287 }
1288 }
1289
Michal Vasko0587bce2020-12-10 12:19:21 +01001290 if (j < orig_used) {
1291 /* node is there, but update its status if needed */
1292 if (set1->val.scnodes[j].in_ctx == LYXP_SET_SCNODE_START_USED) {
1293 set1->val.scnodes[j].in_ctx = set2->val.scnodes[i].in_ctx;
Michal Vasko1a09b212021-05-06 13:00:10 +02001294 } else if ((set1->val.scnodes[j].in_ctx == LYXP_SET_SCNODE_ATOM_NODE) &&
1295 (set2->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_VAL)) {
1296 set1->val.scnodes[j].in_ctx = set2->val.scnodes[i].in_ctx;
Michal Vasko0587bce2020-12-10 12:19:21 +01001297 }
1298 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001299 memcpy(&set1->val.scnodes[set1->used], &set2->val.scnodes[i], sizeof *set2->val.scnodes);
1300 ++set1->used;
1301 }
1302 }
1303
Michal Vaskod3678892020-05-21 10:06:58 +02001304 lyxp_set_free_content(set2);
1305 set2->type = LYXP_SET_SCNODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001306}
1307
1308/**
1309 * @brief Insert a node into a set. Context position aware.
1310 *
1311 * @param[in] set Set to use.
1312 * @param[in] node Node to insert to @p set.
1313 * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting.
1314 * @param[in] node_type Node type of @p node.
1315 * @param[in] idx Index in @p set to insert into.
1316 */
1317static void
1318set_insert_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx)
1319{
Michal Vaskod3678892020-05-21 10:06:58 +02001320 assert(set && (set->type == LYXP_SET_NODE_SET));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001321
Michal Vaskod3678892020-05-21 10:06:58 +02001322 if (!set->size) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001323 /* first item */
1324 if (idx) {
1325 /* no real harm done, but it is a bug */
1326 LOGINT(set->ctx);
1327 idx = 0;
1328 }
1329 set->val.nodes = malloc(LYXP_SET_SIZE_START * sizeof *set->val.nodes);
1330 LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), );
1331 set->type = LYXP_SET_NODE_SET;
1332 set->used = 0;
1333 set->size = LYXP_SET_SIZE_START;
1334 set->ctx_pos = 1;
1335 set->ctx_size = 1;
1336 set->ht = NULL;
1337 } else {
1338 /* not an empty set */
1339 if (set->used == set->size) {
1340
1341 /* set is full */
Michal Vasko871df522022-04-06 12:14:41 +02001342 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 +02001343 LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), );
Michal Vasko871df522022-04-06 12:14:41 +02001344 set->size *= LYXP_SET_SIZE_MUL_STEP;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001345 }
1346
1347 if (idx > set->used) {
1348 LOGINT(set->ctx);
1349 idx = set->used;
1350 }
1351
1352 /* make space for the new node */
1353 if (idx < set->used) {
1354 memmove(&set->val.nodes[idx + 1], &set->val.nodes[idx], (set->used - idx) * sizeof *set->val.nodes);
1355 }
1356 }
1357
1358 /* finally assign the value */
1359 set->val.nodes[idx].node = (struct lyd_node *)node;
1360 set->val.nodes[idx].type = node_type;
1361 set->val.nodes[idx].pos = pos;
1362 ++set->used;
1363
Michal Vasko2416fdd2021-10-01 11:07:10 +02001364 /* add into hash table */
1365 set_insert_node_hash(set, (struct lyd_node *)node, node_type);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001366}
1367
Michal Vaskoe4a6d012023-05-22 14:34:52 +02001368LY_ERR
1369lyxp_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 +02001370 enum lyxp_axis axis, uint32_t *index_p)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001371{
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001372 uint32_t index;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001373
1374 assert(set->type == LYXP_SET_SCNODE_SET);
1375
Michal Vasko871df522022-04-06 12:14:41 +02001376 if (!set->size) {
1377 /* first item */
1378 set->val.scnodes = malloc(LYXP_SET_SIZE_START * sizeof *set->val.scnodes);
1379 LY_CHECK_ERR_RET(!set->val.scnodes, LOGMEM(set->ctx), LY_EMEM);
1380 set->type = LYXP_SET_SCNODE_SET;
1381 set->used = 0;
1382 set->size = LYXP_SET_SIZE_START;
1383 set->ctx_pos = 1;
1384 set->ctx_size = 1;
1385 set->ht = NULL;
1386 }
1387
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001388 if (lyxp_set_scnode_contains(set, node, node_type, -1, &index)) {
Michal Vaskoe4a6d012023-05-22 14:34:52 +02001389 /* BUG if axes differ, this new one is thrown away */
Radek Krejcif13b87b2020-12-01 22:02:17 +01001390 set->val.scnodes[index].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001391 } else {
1392 if (set->used == set->size) {
Michal Vasko871df522022-04-06 12:14:41 +02001393 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 +02001394 LY_CHECK_ERR_RET(!set->val.scnodes, LOGMEM(set->ctx), LY_EMEM);
Michal Vasko871df522022-04-06 12:14:41 +02001395 set->size *= LYXP_SET_SIZE_MUL_STEP;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001396 }
1397
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001398 index = set->used;
1399 set->val.scnodes[index].scnode = (struct lysc_node *)node;
1400 set->val.scnodes[index].type = node_type;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001401 set->val.scnodes[index].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko7333cb32022-07-29 16:30:29 +02001402 set->val.scnodes[index].axis = axis;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001403 ++set->used;
1404 }
1405
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001406 if (index_p) {
1407 *index_p = index;
1408 }
1409
1410 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001411}
1412
1413/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02001414 * @brief Set all nodes with ctx 1 to a new unique context value.
1415 *
1416 * @param[in] set Set to modify.
1417 * @return New context value.
1418 */
Michal Vasko5c4e5892019-11-14 12:31:38 +01001419static int32_t
Michal Vasko03ff5a72019-09-11 13:49:33 +02001420set_scnode_new_in_ctx(struct lyxp_set *set)
1421{
Michal Vasko5c4e5892019-11-14 12:31:38 +01001422 uint32_t i;
1423 int32_t ret_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001424
1425 assert(set->type == LYXP_SET_SCNODE_SET);
1426
Radek Krejcif13b87b2020-12-01 22:02:17 +01001427 ret_ctx = LYXP_SET_SCNODE_ATOM_PRED_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001428retry:
1429 for (i = 0; i < set->used; ++i) {
1430 if (set->val.scnodes[i].in_ctx >= ret_ctx) {
1431 ret_ctx = set->val.scnodes[i].in_ctx + 1;
1432 goto retry;
1433 }
1434 }
1435 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001436 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001437 set->val.scnodes[i].in_ctx = ret_ctx;
1438 }
1439 }
1440
1441 return ret_ctx;
1442}
1443
1444/**
1445 * @brief Get unique @p node position in the data.
1446 *
1447 * @param[in] node Node to find.
1448 * @param[in] node_type Node type of @p node.
1449 * @param[in] root Root node.
1450 * @param[in] root_type Type of the XPath @p root node.
1451 * @param[in] prev Node that we think is before @p node in DFS from @p root. Can optionally
1452 * be used to increase efficiency and start the DFS from this node.
1453 * @param[in] prev_pos Node @p prev position. Optional, but must be set if @p prev is set.
1454 * @return Node position.
1455 */
1456static uint32_t
1457get_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 +02001458 enum lyxp_node_type root_type, const struct lyd_node **prev, uint32_t *prev_pos)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001459{
Michal Vasko56daf732020-08-10 10:57:18 +02001460 const struct lyd_node *elem = NULL, *top_sibling;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001461 uint32_t pos = 1;
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001462 ly_bool found = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001463
1464 assert(prev && prev_pos && !root->prev->next);
1465
1466 if ((node_type == LYXP_NODE_ROOT) || (node_type == LYXP_NODE_ROOT_CONFIG)) {
1467 return 0;
1468 }
1469
1470 if (*prev) {
1471 /* start from the previous element instead from the root */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001472 pos = *prev_pos;
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001473 for (top_sibling = *prev; top_sibling->parent; top_sibling = lyd_parent(top_sibling)) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001474 goto dfs_search;
1475 }
1476
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001477 LY_LIST_FOR(root, top_sibling) {
Michal Vasko56daf732020-08-10 10:57:18 +02001478 LYD_TREE_DFS_BEGIN(top_sibling, elem) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001479dfs_search:
Michal Vaskoa9309bb2021-07-09 09:31:55 +02001480 LYD_TREE_DFS_continue = 0;
1481
Michal Vasko56daf732020-08-10 10:57:18 +02001482 if (*prev && !elem) {
1483 /* resume previous DFS */
1484 elem = LYD_TREE_DFS_next = (struct lyd_node *)*prev;
1485 LYD_TREE_DFS_continue = 0;
1486 }
1487
Michal Vasko1ef66d42023-12-14 11:41:18 +01001488 if ((root_type == LYXP_NODE_ROOT_CONFIG) && elem->schema && (elem->schema->flags & LYS_CONFIG_R)) {
Michal Vasko56daf732020-08-10 10:57:18 +02001489 /* skip */
1490 LYD_TREE_DFS_continue = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001491 } else {
Michal Vasko56daf732020-08-10 10:57:18 +02001492 if (elem == node) {
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001493 found = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001494 break;
1495 }
Michal Vasko56daf732020-08-10 10:57:18 +02001496 ++pos;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001497 }
Michal Vasko56daf732020-08-10 10:57:18 +02001498
1499 LYD_TREE_DFS_END(top_sibling, elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001500 }
1501
1502 /* node found */
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001503 if (found) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001504 break;
1505 }
1506 }
1507
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001508 if (!found) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001509 if (!(*prev)) {
1510 /* we went from root and failed to find it, cannot be */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001511 LOGINT(LYD_CTX(node));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001512 return 0;
1513 } else {
Michal Vasko56daf732020-08-10 10:57:18 +02001514 /* start the search again from the beginning */
1515 *prev = root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001516
Michal Vasko56daf732020-08-10 10:57:18 +02001517 top_sibling = root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001518 pos = 1;
1519 goto dfs_search;
1520 }
1521 }
1522
1523 /* remember the last found node for next time */
1524 *prev = node;
1525 *prev_pos = pos;
1526
1527 return pos;
1528}
1529
1530/**
1531 * @brief Assign (fill) missing node positions.
1532 *
1533 * @param[in] set Set to fill positions in.
1534 * @param[in] root Context root node.
1535 * @param[in] root_type Context root type.
1536 * @return LY_ERR
1537 */
1538static LY_ERR
1539set_assign_pos(struct lyxp_set *set, const struct lyd_node *root, enum lyxp_node_type root_type)
1540{
1541 const struct lyd_node *prev = NULL, *tmp_node;
1542 uint32_t i, tmp_pos = 0;
1543
1544 for (i = 0; i < set->used; ++i) {
1545 if (!set->val.nodes[i].pos) {
1546 tmp_node = NULL;
1547 switch (set->val.nodes[i].type) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001548 case LYXP_NODE_META:
1549 tmp_node = set->val.meta[i].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001550 if (!tmp_node) {
1551 LOGINT_RET(root->schema->module->ctx);
1552 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01001553 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001554 case LYXP_NODE_ELEM:
1555 case LYXP_NODE_TEXT:
1556 if (!tmp_node) {
1557 tmp_node = set->val.nodes[i].node;
1558 }
1559 set->val.nodes[i].pos = get_node_pos(tmp_node, set->val.nodes[i].type, root, root_type, &prev, &tmp_pos);
1560 break;
1561 default:
1562 /* all roots have position 0 */
1563 break;
1564 }
1565 }
1566 }
1567
1568 return LY_SUCCESS;
1569}
1570
1571/**
Michal Vasko9f96a052020-03-10 09:41:45 +01001572 * @brief Get unique @p meta position in the parent metadata.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001573 *
Michal Vasko9f96a052020-03-10 09:41:45 +01001574 * @param[in] meta Metadata to use.
1575 * @return Metadata position.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001576 */
Michal Vaskodd528af2022-08-08 14:35:07 +02001577static uint32_t
Michal Vasko9f96a052020-03-10 09:41:45 +01001578get_meta_pos(struct lyd_meta *meta)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001579{
Michal Vaskodd528af2022-08-08 14:35:07 +02001580 uint32_t pos = 0;
Michal Vasko9f96a052020-03-10 09:41:45 +01001581 struct lyd_meta *meta2;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001582
Michal Vasko9f96a052020-03-10 09:41:45 +01001583 for (meta2 = meta->parent->meta; meta2 && (meta2 != meta); meta2 = meta2->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001584 ++pos;
1585 }
1586
Michal Vasko9f96a052020-03-10 09:41:45 +01001587 assert(meta2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001588 return pos;
1589}
1590
1591/**
1592 * @brief Compare 2 nodes in respect to XPath document order.
1593 *
1594 * @param[in] item1 1st node.
1595 * @param[in] item2 2nd node.
1596 * @return If 1st > 2nd returns 1, 1st == 2nd returns 0, and 1st < 2nd returns -1.
1597 */
1598static int
1599set_sort_compare(struct lyxp_set_node *item1, struct lyxp_set_node *item2)
1600{
Michal Vasko9f96a052020-03-10 09:41:45 +01001601 uint32_t meta_pos1 = 0, meta_pos2 = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001602
1603 if (item1->pos < item2->pos) {
1604 return -1;
1605 }
1606
1607 if (item1->pos > item2->pos) {
1608 return 1;
1609 }
1610
1611 /* node positions are equal, the fun case */
1612
1613 /* 1st ELEM - == - 2nd TEXT, 1st TEXT - == - 2nd ELEM */
1614 /* special case since text nodes are actually saved as their parents */
1615 if ((item1->node == item2->node) && (item1->type != item2->type)) {
1616 if (item1->type == LYXP_NODE_ELEM) {
1617 assert(item2->type == LYXP_NODE_TEXT);
1618 return -1;
1619 } else {
1620 assert((item1->type == LYXP_NODE_TEXT) && (item2->type == LYXP_NODE_ELEM));
1621 return 1;
1622 }
1623 }
1624
Michal Vasko9f96a052020-03-10 09:41:45 +01001625 /* we need meta positions now */
1626 if (item1->type == LYXP_NODE_META) {
1627 meta_pos1 = get_meta_pos((struct lyd_meta *)item1->node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001628 }
Michal Vasko9f96a052020-03-10 09:41:45 +01001629 if (item2->type == LYXP_NODE_META) {
1630 meta_pos2 = get_meta_pos((struct lyd_meta *)item2->node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001631 }
1632
Michal Vasko9f96a052020-03-10 09:41:45 +01001633 /* 1st ROOT - 2nd ROOT, 1st ELEM - 2nd ELEM, 1st TEXT - 2nd TEXT, 1st META - =pos= - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001634 /* check for duplicates */
1635 if (item1->node == item2->node) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001636 assert((item1->type == item2->type) && ((item1->type != LYXP_NODE_META) || (meta_pos1 == meta_pos2)));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001637 return 0;
1638 }
1639
Michal Vasko9f96a052020-03-10 09:41:45 +01001640 /* 1st ELEM - 2nd TEXT, 1st ELEM - any pos - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001641 /* elem is always first, 2nd node is after it */
1642 if (item1->type == LYXP_NODE_ELEM) {
1643 assert(item2->type != LYXP_NODE_ELEM);
1644 return -1;
1645 }
1646
Michal Vasko9f96a052020-03-10 09:41:45 +01001647 /* 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 +02001648 /* 2nd is before 1st */
Michal Vasko69730152020-10-09 16:30:07 +02001649 if (((item1->type == LYXP_NODE_TEXT) &&
1650 ((item2->type == LYXP_NODE_ELEM) || (item2->type == LYXP_NODE_META))) ||
1651 ((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_ELEM)) ||
1652 (((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_META)) &&
1653 (meta_pos1 > meta_pos2))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001654 return 1;
1655 }
1656
Michal Vasko9f96a052020-03-10 09:41:45 +01001657 /* 1st META - any pos - 2nd TEXT, 1st META <pos< - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001658 /* 2nd is after 1st */
1659 return -1;
1660}
1661
1662/**
1663 * @brief Set cast for comparisons.
1664 *
Michal Vasko8abcecc2022-07-28 09:55:01 +02001665 * @param[in,out] trg Target set to cast source into.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001666 * @param[in] src Source set.
1667 * @param[in] type Target set type.
1668 * @param[in] src_idx Source set node index.
Michal Vasko8abcecc2022-07-28 09:55:01 +02001669 * @return LY_SUCCESS on success.
1670 * @return LY_ERR value on error.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001671 */
1672static LY_ERR
Michal Vasko8abcecc2022-07-28 09:55:01 +02001673set_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 +02001674{
1675 assert(src->type == LYXP_SET_NODE_SET);
1676
1677 set_init(trg, src);
1678
1679 /* insert node into target set */
1680 set_insert_node(trg, src->val.nodes[src_idx].node, src->val.nodes[src_idx].pos, src->val.nodes[src_idx].type, 0);
1681
1682 /* cast target set appropriately */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001683 return lyxp_set_cast(trg, type);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001684}
1685
Michal Vasko4c7763f2020-07-27 17:40:37 +02001686/**
1687 * @brief Set content canonization for comparisons.
1688 *
Michal Vasko8abcecc2022-07-28 09:55:01 +02001689 * @param[in,out] set Set to canonize.
Michal Vasko4c7763f2020-07-27 17:40:37 +02001690 * @param[in] xp_node Source XPath node/meta to use for canonization.
Michal Vasko8abcecc2022-07-28 09:55:01 +02001691 * @return LY_SUCCESS on success.
1692 * @return LY_ERR value on error.
Michal Vasko4c7763f2020-07-27 17:40:37 +02001693 */
1694static LY_ERR
Michal Vasko8abcecc2022-07-28 09:55:01 +02001695set_comp_canonize(struct lyxp_set *set, const struct lyxp_set_node *xp_node)
Michal Vasko4c7763f2020-07-27 17:40:37 +02001696{
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001697 const struct lysc_type *type = NULL;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001698 struct lyd_value val;
1699 struct ly_err_item *err = NULL;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001700 LY_ERR rc;
1701
1702 /* is there anything to canonize even? */
Michal Vasko8abcecc2022-07-28 09:55:01 +02001703 if (set->type == LYXP_SET_STRING) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02001704 /* do we have a type to use for canonization? */
Michal Vasko222be682023-08-24 08:17:12 +02001705 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 +02001706 type = ((struct lyd_node_term *)xp_node->node)->value.realtype;
1707 } else if (xp_node->type == LYXP_NODE_META) {
1708 type = ((struct lyd_meta *)xp_node->node)->value.realtype;
1709 }
1710 }
1711 if (!type) {
Michal Vasko8abcecc2022-07-28 09:55:01 +02001712 /* no canonization needed/possible */
1713 return LY_SUCCESS;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001714 }
1715
Michal Vasko8abcecc2022-07-28 09:55:01 +02001716 /* check for built-in types without required canonization */
1717 if ((type->basetype == LY_TYPE_STRING) && (type->plugin->store == lyplg_type_store_string)) {
1718 /* string */
1719 return LY_SUCCESS;
1720 }
1721 if ((type->basetype == LY_TYPE_BOOL) && (type->plugin->store == lyplg_type_store_boolean)) {
1722 /* boolean */
1723 return LY_SUCCESS;
1724 }
1725 if ((type->basetype == LY_TYPE_ENUM) && (type->plugin->store == lyplg_type_store_enum)) {
1726 /* enumeration */
1727 return LY_SUCCESS;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001728 }
1729
Michal Vasko8abcecc2022-07-28 09:55:01 +02001730 /* print canonized string, ignore errors, the value may not satisfy schema constraints */
1731 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 +01001732 LYD_HINT_DATA, xp_node->node->schema, &val, NULL, &err);
Michal Vasko4c7763f2020-07-27 17:40:37 +02001733 ly_err_free(err);
1734 if (rc) {
Michal Vasko8abcecc2022-07-28 09:55:01 +02001735 /* invalid value, function store automaticaly dealloc value when fail */
1736 return LY_SUCCESS;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001737 }
1738
Michal Vasko8abcecc2022-07-28 09:55:01 +02001739 /* use the canonized string value */
1740 free(set->val.str);
1741 set->val.str = strdup(lyd_value_get_canonical(set->ctx, &val));
1742 type->plugin->free(set->ctx, &val);
1743 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), LY_EMEM);
Michal Vasko4c7763f2020-07-27 17:40:37 +02001744
Michal Vasko4c7763f2020-07-27 17:40:37 +02001745 return LY_SUCCESS;
1746}
1747
Michal Vasko03ff5a72019-09-11 13:49:33 +02001748/**
1749 * @brief Bubble sort @p set into XPath document order.
Michal Vasko49fec8e2022-05-24 10:28:33 +02001750 * Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001751 *
1752 * @param[in] set Set to sort.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001753 * @return How many times the whole set was traversed - 1 (if set was sorted, returns 0).
1754 */
1755static int
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001756set_sort(struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001757{
1758 uint32_t i, j;
Radek Krejci1deb5be2020-08-26 16:43:36 +02001759 int ret = 0, cmp;
Radek Krejci857189e2020-09-01 13:26:36 +02001760 ly_bool inverted, change;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001761 const struct lyd_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001762 struct lyxp_set_node item;
1763 struct lyxp_set_hash_node hnode;
1764 uint64_t hash;
1765
Michal Vasko3cf8fbf2020-05-27 15:21:21 +02001766 if ((set->type != LYXP_SET_NODE_SET) || (set->used < 2)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001767 return 0;
1768 }
1769
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001770 /* find first top-level node to be used as anchor for positions */
Michal Vasko4a7d4d62021-12-13 17:05:06 +01001771 for (root = set->tree; root->parent; root = lyd_parent(root)) {}
Michal Vaskod989ba02020-08-24 10:59:24 +02001772 for ( ; root->prev->next; root = root->prev) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001773
1774 /* fill positions */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001775 if (set_assign_pos(set, root, set->root_type)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001776 return -1;
1777 }
1778
Michal Vasko88a9e802022-05-24 10:50:28 +02001779#ifndef NDEBUG
Michal Vasko03ff5a72019-09-11 13:49:33 +02001780 LOGDBG(LY_LDGXPATH, "SORT BEGIN");
1781 print_set_debug(set);
Michal Vasko88a9e802022-05-24 10:50:28 +02001782#endif
Michal Vasko03ff5a72019-09-11 13:49:33 +02001783
1784 for (i = 0; i < set->used; ++i) {
1785 inverted = 0;
1786 change = 0;
1787
1788 for (j = 1; j < set->used - i; ++j) {
1789 /* compare node positions */
1790 if (inverted) {
1791 cmp = set_sort_compare(&set->val.nodes[j], &set->val.nodes[j - 1]);
1792 } else {
1793 cmp = set_sort_compare(&set->val.nodes[j - 1], &set->val.nodes[j]);
1794 }
1795
1796 /* swap if needed */
1797 if ((inverted && (cmp < 0)) || (!inverted && (cmp > 0))) {
1798 change = 1;
1799
1800 item = set->val.nodes[j - 1];
1801 set->val.nodes[j - 1] = set->val.nodes[j];
1802 set->val.nodes[j] = item;
1803 } else {
1804 /* whether node_pos1 should be smaller than node_pos2 or the other way around */
1805 inverted = !inverted;
1806 }
1807 }
1808
1809 ++ret;
1810
1811 if (!change) {
1812 break;
1813 }
1814 }
1815
Michal Vasko88a9e802022-05-24 10:50:28 +02001816#ifndef NDEBUG
Michal Vasko03ff5a72019-09-11 13:49:33 +02001817 LOGDBG(LY_LDGXPATH, "SORT END %d", ret);
1818 print_set_debug(set);
Michal Vasko88a9e802022-05-24 10:50:28 +02001819#endif
Michal Vasko03ff5a72019-09-11 13:49:33 +02001820
1821 /* check node hashes */
1822 if (set->used >= LYD_HT_MIN_ITEMS) {
1823 assert(set->ht);
1824 for (i = 0; i < set->used; ++i) {
1825 hnode.node = set->val.nodes[i].node;
1826 hnode.type = set->val.nodes[i].type;
1827
Michal Vaskoae130f52023-04-20 14:25:16 +02001828 hash = lyht_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
1829 hash = lyht_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
1830 hash = lyht_hash_multi(hash, NULL, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001831
1832 assert(!lyht_find(set->ht, &hnode, hash, NULL));
1833 }
1834 }
1835
1836 return ret - 1;
1837}
1838
Michal Vasko03ff5a72019-09-11 13:49:33 +02001839/**
1840 * @brief Merge 2 sorted sets into one.
1841 *
1842 * @param[in,out] trg Set to merge into. Duplicates are removed.
1843 * @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 +02001844 * @return LY_ERR
1845 */
1846static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001847set_sorted_merge(struct lyxp_set *trg, struct lyxp_set *src)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001848{
1849 uint32_t i, j, k, count, dup_count;
1850 int cmp;
1851 const struct lyd_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001852
Michal Vaskod3678892020-05-21 10:06:58 +02001853 if ((trg->type != LYXP_SET_NODE_SET) || (src->type != LYXP_SET_NODE_SET)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001854 return LY_EINVAL;
1855 }
1856
Michal Vaskod3678892020-05-21 10:06:58 +02001857 if (!src->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001858 return LY_SUCCESS;
Michal Vaskod3678892020-05-21 10:06:58 +02001859 } else if (!trg->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001860 set_fill_set(trg, src);
Michal Vaskod3678892020-05-21 10:06:58 +02001861 lyxp_set_free_content(src);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001862 return LY_SUCCESS;
1863 }
1864
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001865 /* find first top-level node to be used as anchor for positions */
Michal Vasko0143b682021-12-13 17:13:09 +01001866 for (root = trg->tree; root->parent; root = lyd_parent(root)) {}
Michal Vaskod989ba02020-08-24 10:59:24 +02001867 for ( ; root->prev->next; root = root->prev) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001868
1869 /* fill positions */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001870 if (set_assign_pos(trg, root, trg->root_type) || set_assign_pos(src, root, src->root_type)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001871 return LY_EINT;
1872 }
1873
1874#ifndef NDEBUG
1875 LOGDBG(LY_LDGXPATH, "MERGE target");
1876 print_set_debug(trg);
1877 LOGDBG(LY_LDGXPATH, "MERGE source");
1878 print_set_debug(src);
1879#endif
1880
1881 /* make memory for the merge (duplicates are not detected yet, so space
1882 * will likely be wasted on them, too bad) */
1883 if (trg->size - trg->used < src->used) {
1884 trg->size = trg->used + src->used;
1885
1886 trg->val.nodes = ly_realloc(trg->val.nodes, trg->size * sizeof *trg->val.nodes);
1887 LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx), LY_EMEM);
1888 }
1889
1890 i = 0;
1891 j = 0;
1892 count = 0;
1893 dup_count = 0;
1894 do {
1895 cmp = set_sort_compare(&src->val.nodes[i], &trg->val.nodes[j]);
1896 if (!cmp) {
1897 if (!count) {
1898 /* duplicate, just skip it */
1899 ++i;
1900 ++j;
1901 } else {
1902 /* we are copying something already, so let's copy the duplicate too,
1903 * we are hoping that afterwards there are some more nodes to
1904 * copy and this way we can copy them all together */
1905 ++count;
1906 ++dup_count;
1907 ++i;
1908 ++j;
1909 }
1910 } else if (cmp < 0) {
1911 /* inserting src node into trg, just remember it for now */
1912 ++count;
1913 ++i;
1914
1915 /* insert the hash now */
1916 set_insert_node_hash(trg, src->val.nodes[i - 1].node, src->val.nodes[i - 1].type);
1917 } else if (count) {
1918copy_nodes:
1919 /* time to actually copy the nodes, we have found the largest block of nodes */
1920 memmove(&trg->val.nodes[j + (count - dup_count)],
1921 &trg->val.nodes[j],
1922 (trg->used - j) * sizeof *trg->val.nodes);
1923 memcpy(&trg->val.nodes[j - dup_count], &src->val.nodes[i - count], count * sizeof *src->val.nodes);
1924
1925 trg->used += count - dup_count;
1926 /* do not change i, except the copying above, we are basically doing exactly what is in the else branch below */
1927 j += count - dup_count;
1928
1929 count = 0;
1930 dup_count = 0;
1931 } else {
1932 ++j;
1933 }
1934 } while ((i < src->used) && (j < trg->used));
1935
1936 if ((i < src->used) || count) {
1937 /* insert all the hashes first */
1938 for (k = i; k < src->used; ++k) {
1939 set_insert_node_hash(trg, src->val.nodes[k].node, src->val.nodes[k].type);
1940 }
1941
1942 /* loop ended, but we need to copy something at trg end */
1943 count += src->used - i;
1944 i = src->used;
1945 goto copy_nodes;
1946 }
1947
1948 /* we are inserting hashes before the actual node insert, which causes
1949 * situations when there were initially not enough items for a hash table,
1950 * but even after some were inserted, hash table was not created (during
1951 * insertion the number of items is not updated yet) */
1952 if (!trg->ht && (trg->used >= LYD_HT_MIN_ITEMS)) {
1953 set_insert_node_hash(trg, NULL, 0);
1954 }
1955
1956#ifndef NDEBUG
1957 LOGDBG(LY_LDGXPATH, "MERGE result");
1958 print_set_debug(trg);
1959#endif
1960
Michal Vaskod3678892020-05-21 10:06:58 +02001961 lyxp_set_free_content(src);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001962 return LY_SUCCESS;
1963}
1964
Michal Vasko14676352020-05-29 11:35:55 +02001965LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02001966lyxp_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 +02001967{
Michal Vasko004d3152020-06-11 19:59:22 +02001968 if (exp->used == tok_idx) {
Michal Vasko14676352020-05-29 11:35:55 +02001969 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001970 LOGVAL(ctx, LY_VCODE_XP_EOF);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001971 }
Michal Vasko14676352020-05-29 11:35:55 +02001972 return LY_EINCOMPLETE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001973 }
1974
Michal Vasko004d3152020-06-11 19:59:22 +02001975 if (want_tok && (exp->tokens[tok_idx] != want_tok)) {
Michal Vasko14676352020-05-29 11:35:55 +02001976 if (ctx) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02001977 LOGVAL(ctx, LY_VCODE_XP_INTOK2, lyxp_token2str(exp->tokens[tok_idx]),
1978 &exp->expr[exp->tok_pos[tok_idx]], lyxp_token2str(want_tok));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001979 }
Michal Vasko14676352020-05-29 11:35:55 +02001980 return LY_ENOT;
1981 }
1982
1983 return LY_SUCCESS;
1984}
1985
Michal Vasko004d3152020-06-11 19:59:22 +02001986LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02001987lyxp_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 +02001988{
1989 LY_CHECK_RET(lyxp_check_token(ctx, exp, *tok_idx, want_tok));
1990
1991 /* skip the token */
1992 ++(*tok_idx);
1993
1994 return LY_SUCCESS;
1995}
1996
Michal Vasko14676352020-05-29 11:35:55 +02001997/* just like lyxp_check_token() but tests for 2 tokens */
1998static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02001999exp_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 +02002000 enum lyxp_token want_tok2)
Michal Vasko14676352020-05-29 11:35:55 +02002001{
Michal Vasko004d3152020-06-11 19:59:22 +02002002 if (exp->used == tok_idx) {
Michal Vasko14676352020-05-29 11:35:55 +02002003 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002004 LOGVAL(ctx, LY_VCODE_XP_EOF);
Michal Vasko14676352020-05-29 11:35:55 +02002005 }
2006 return LY_EINCOMPLETE;
2007 }
2008
Michal Vasko004d3152020-06-11 19:59:22 +02002009 if ((exp->tokens[tok_idx] != want_tok1) && (exp->tokens[tok_idx] != want_tok2)) {
Michal Vasko14676352020-05-29 11:35:55 +02002010 if (ctx) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02002011 LOGVAL(ctx, LY_VCODE_XP_INTOK, lyxp_token2str(exp->tokens[tok_idx]),
Michal Vasko0b468e62020-10-19 09:33:04 +02002012 &exp->expr[exp->tok_pos[tok_idx]]);
Michal Vasko14676352020-05-29 11:35:55 +02002013 }
2014 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002015 }
2016
2017 return LY_SUCCESS;
2018}
2019
Michal Vasko4911eeb2021-06-28 11:23:05 +02002020LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02002021lyxp_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 +02002022 enum lyxp_token want_tok2)
2023{
2024 LY_CHECK_RET(exp_check_token2(ctx, exp, *tok_idx, want_tok1, want_tok2));
2025
2026 /* skip the token */
2027 ++(*tok_idx);
2028
2029 return LY_SUCCESS;
2030}
2031
Michal Vasko03ff5a72019-09-11 13:49:33 +02002032/**
2033 * @brief Stack operation push on the repeat array.
2034 *
2035 * @param[in] exp Expression to use.
Michal Vasko831e3bd2023-04-24 10:43:38 +02002036 * @param[in] tok_idx Position in the expresion @p exp.
2037 * @param[in] repeat_expr_type Repeated expression type, this value is pushed.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002038 */
2039static void
Michal Vasko831e3bd2023-04-24 10:43:38 +02002040exp_repeat_push(struct lyxp_expr *exp, uint32_t tok_idx, enum lyxp_expr_type repeat_expr_type)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002041{
Michal Vaskodd528af2022-08-08 14:35:07 +02002042 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002043
Michal Vasko004d3152020-06-11 19:59:22 +02002044 if (exp->repeat[tok_idx]) {
Radek Krejci1e008d22020-08-17 11:37:37 +02002045 for (i = 0; exp->repeat[tok_idx][i]; ++i) {}
Michal Vasko004d3152020-06-11 19:59:22 +02002046 exp->repeat[tok_idx] = realloc(exp->repeat[tok_idx], (i + 2) * sizeof *exp->repeat[tok_idx]);
2047 LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), );
Michal Vasko831e3bd2023-04-24 10:43:38 +02002048 exp->repeat[tok_idx][i] = repeat_expr_type;
Michal Vasko004d3152020-06-11 19:59:22 +02002049 exp->repeat[tok_idx][i + 1] = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002050 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02002051 exp->repeat[tok_idx] = calloc(2, sizeof *exp->repeat[tok_idx]);
2052 LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), );
Michal Vasko831e3bd2023-04-24 10:43:38 +02002053 exp->repeat[tok_idx][0] = repeat_expr_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002054 }
2055}
2056
2057/**
2058 * @brief Reparse Predicate. Logs directly on error.
2059 *
2060 * [7] Predicate ::= '[' Expr ']'
2061 *
2062 * @param[in] ctx Context for logging.
2063 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002064 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002065 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002066 * @return LY_ERR
2067 */
2068static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02002069reparse_predicate(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint32_t *tok_idx, uint32_t depth)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002070{
2071 LY_ERR rc;
2072
Michal Vasko004d3152020-06-11 19:59:22 +02002073 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002074 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002075 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002076
aPiecekbf968d92021-05-27 14:35:05 +02002077 rc = reparse_or_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002078 LY_CHECK_RET(rc);
2079
Michal Vasko004d3152020-06-11 19:59:22 +02002080 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002081 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002082 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002083
2084 return LY_SUCCESS;
2085}
2086
2087/**
2088 * @brief Reparse RelativeLocationPath. Logs directly on error.
2089 *
2090 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
2091 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
2092 * [6] NodeTest ::= NameTest | NodeType '(' ')'
2093 *
2094 * @param[in] ctx Context for logging.
2095 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002096 * @param[in] tok_idx Position in the expression \p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002097 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002098 * @return LY_ERR (LY_EINCOMPLETE on forward reference)
2099 */
2100static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02002101reparse_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 +02002102{
2103 LY_ERR rc;
2104
Michal Vasko004d3152020-06-11 19:59:22 +02002105 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002106 LY_CHECK_RET(rc);
2107
2108 goto step;
2109 do {
2110 /* '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02002111 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002112
Michal Vasko004d3152020-06-11 19:59:22 +02002113 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002114 LY_CHECK_RET(rc);
2115step:
2116 /* Step */
Michal Vasko004d3152020-06-11 19:59:22 +02002117 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002118 case LYXP_TOKEN_DOT:
Michal Vasko004d3152020-06-11 19:59:22 +02002119 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002120 break;
2121
2122 case LYXP_TOKEN_DDOT:
Michal Vasko004d3152020-06-11 19:59:22 +02002123 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002124 break;
2125
Michal Vasko49fec8e2022-05-24 10:28:33 +02002126 case LYXP_TOKEN_AXISNAME:
2127 ++(*tok_idx);
2128
2129 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_DCOLON);
2130 LY_CHECK_RET(rc);
2131
2132 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02002133 case LYXP_TOKEN_AT:
Michal Vasko004d3152020-06-11 19:59:22 +02002134 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002135
Michal Vasko004d3152020-06-11 19:59:22 +02002136 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002137 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002138 if ((exp->tokens[*tok_idx] != LYXP_TOKEN_NAMETEST) && (exp->tokens[*tok_idx] != LYXP_TOKEN_NODETYPE)) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02002139 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 +02002140 return LY_EVALID;
2141 }
Michal Vasko49fec8e2022-05-24 10:28:33 +02002142 if (exp->tokens[*tok_idx] == LYXP_TOKEN_NODETYPE) {
2143 goto reparse_nodetype;
2144 }
Radek Krejci0f969882020-08-21 16:56:47 +02002145 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02002146 case LYXP_TOKEN_NAMETEST:
Michal Vasko004d3152020-06-11 19:59:22 +02002147 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002148 goto reparse_predicate;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002149
2150 case LYXP_TOKEN_NODETYPE:
Michal Vasko49fec8e2022-05-24 10:28:33 +02002151reparse_nodetype:
Michal Vasko004d3152020-06-11 19:59:22 +02002152 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002153
2154 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02002155 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002156 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002157 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002158
2159 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02002160 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002161 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002162 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002163
2164reparse_predicate:
2165 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002166 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
aPiecekbf968d92021-05-27 14:35:05 +02002167 rc = reparse_predicate(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002168 LY_CHECK_RET(rc);
2169 }
2170 break;
2171 default:
Michal Vasko49fec8e2022-05-24 10:28:33 +02002172 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 +02002173 return LY_EVALID;
2174 }
Michal Vasko004d3152020-06-11 19:59:22 +02002175 } while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02002176
2177 return LY_SUCCESS;
2178}
2179
2180/**
2181 * @brief Reparse AbsoluteLocationPath. Logs directly on error.
2182 *
2183 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
2184 *
2185 * @param[in] ctx Context for logging.
2186 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002187 * @param[in] tok_idx Position in the expression \p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002188 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002189 * @return LY_ERR
2190 */
2191static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02002192reparse_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 +02002193{
2194 LY_ERR rc;
2195
Michal Vasko004d3152020-06-11 19:59:22 +02002196 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 +02002197
2198 /* '/' RelativeLocationPath? */
Michal Vasko004d3152020-06-11 19:59:22 +02002199 if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002200 /* '/' */
Michal Vasko004d3152020-06-11 19:59:22 +02002201 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002202
Michal Vasko004d3152020-06-11 19:59:22 +02002203 if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002204 return LY_SUCCESS;
2205 }
Michal Vasko004d3152020-06-11 19:59:22 +02002206 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002207 case LYXP_TOKEN_DOT:
2208 case LYXP_TOKEN_DDOT:
Michal Vasko49fec8e2022-05-24 10:28:33 +02002209 case LYXP_TOKEN_AXISNAME:
Michal Vasko03ff5a72019-09-11 13:49:33 +02002210 case LYXP_TOKEN_AT:
2211 case LYXP_TOKEN_NAMETEST:
2212 case LYXP_TOKEN_NODETYPE:
aPiecekbf968d92021-05-27 14:35:05 +02002213 rc = reparse_relative_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002214 LY_CHECK_RET(rc);
Radek Krejci0f969882020-08-21 16:56:47 +02002215 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02002216 default:
2217 break;
2218 }
2219
Michal Vasko03ff5a72019-09-11 13:49:33 +02002220 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02002221 /* '//' RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002222 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002223
aPiecekbf968d92021-05-27 14:35:05 +02002224 rc = reparse_relative_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002225 LY_CHECK_RET(rc);
2226 }
2227
2228 return LY_SUCCESS;
2229}
2230
2231/**
2232 * @brief Reparse FunctionCall. Logs directly on error.
2233 *
2234 * [9] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
2235 *
2236 * @param[in] ctx Context for logging.
2237 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002238 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002239 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002240 * @return LY_ERR
2241 */
2242static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02002243reparse_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 +02002244{
Radek Krejci1deb5be2020-08-26 16:43:36 +02002245 int8_t min_arg_count = -1;
Michal Vaskodd528af2022-08-08 14:35:07 +02002246 uint32_t arg_count, max_arg_count = 0, func_tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002247 LY_ERR rc;
2248
Michal Vasko004d3152020-06-11 19:59:22 +02002249 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_FUNCNAME);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002250 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002251 func_tok_idx = *tok_idx;
2252 switch (exp->tok_len[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002253 case 3:
Michal Vasko004d3152020-06-11 19:59:22 +02002254 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002255 min_arg_count = 1;
2256 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002257 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002258 min_arg_count = 1;
2259 max_arg_count = 1;
2260 }
2261 break;
2262 case 4:
Michal Vasko004d3152020-06-11 19:59:22 +02002263 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002264 min_arg_count = 1;
2265 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002266 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002267 min_arg_count = 0;
2268 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002269 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002270 min_arg_count = 0;
2271 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002272 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002273 min_arg_count = 0;
2274 max_arg_count = 0;
2275 }
2276 break;
2277 case 5:
Michal Vasko004d3152020-06-11 19:59:22 +02002278 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002279 min_arg_count = 1;
2280 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002281 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002282 min_arg_count = 0;
2283 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002284 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 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]], "round", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002288 min_arg_count = 1;
2289 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002290 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002291 min_arg_count = 1;
2292 max_arg_count = 1;
2293 }
2294 break;
2295 case 6:
Michal Vasko004d3152020-06-11 19:59:22 +02002296 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002297 min_arg_count = 2;
Radek Krejci1deb5be2020-08-26 16:43:36 +02002298 max_arg_count = UINT32_MAX;
Michal Vasko004d3152020-06-11 19:59:22 +02002299 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002300 min_arg_count = 0;
2301 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002302 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002303 min_arg_count = 0;
2304 max_arg_count = 1;
2305 }
2306 break;
2307 case 7:
Michal Vasko004d3152020-06-11 19:59:22 +02002308 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 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]], "ceiling", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002312 min_arg_count = 1;
2313 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002314 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002315 min_arg_count = 0;
2316 max_arg_count = 0;
2317 }
2318 break;
2319 case 8:
Michal Vasko004d3152020-06-11 19:59:22 +02002320 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002321 min_arg_count = 2;
2322 max_arg_count = 2;
Michal Vasko004d3152020-06-11 19:59:22 +02002323 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002324 min_arg_count = 0;
2325 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002326 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "re-match", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002327 min_arg_count = 2;
2328 max_arg_count = 2;
2329 }
2330 break;
2331 case 9:
Michal Vasko004d3152020-06-11 19:59:22 +02002332 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002333 min_arg_count = 2;
2334 max_arg_count = 3;
Michal Vasko004d3152020-06-11 19:59:22 +02002335 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002336 min_arg_count = 3;
2337 max_arg_count = 3;
2338 }
2339 break;
2340 case 10:
Michal Vasko004d3152020-06-11 19:59:22 +02002341 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002342 min_arg_count = 0;
2343 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002344 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002345 min_arg_count = 1;
2346 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002347 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002348 min_arg_count = 2;
2349 max_arg_count = 2;
2350 }
2351 break;
2352 case 11:
Michal Vasko004d3152020-06-11 19:59:22 +02002353 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002354 min_arg_count = 2;
2355 max_arg_count = 2;
2356 }
2357 break;
2358 case 12:
Michal Vasko004d3152020-06-11 19:59:22 +02002359 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002360 min_arg_count = 2;
2361 max_arg_count = 2;
2362 }
2363 break;
2364 case 13:
Michal Vasko004d3152020-06-11 19:59:22 +02002365 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002366 min_arg_count = 0;
2367 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002368 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string-length", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002369 min_arg_count = 0;
2370 max_arg_count = 1;
2371 }
2372 break;
2373 case 15:
Michal Vasko004d3152020-06-11 19:59:22 +02002374 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002375 min_arg_count = 0;
2376 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002377 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-after", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002378 min_arg_count = 2;
2379 max_arg_count = 2;
2380 }
2381 break;
2382 case 16:
Michal Vasko004d3152020-06-11 19:59:22 +02002383 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002384 min_arg_count = 2;
2385 max_arg_count = 2;
2386 }
2387 break;
2388 case 20:
Michal Vasko004d3152020-06-11 19:59:22 +02002389 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from-or-self", 20)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002390 min_arg_count = 2;
2391 max_arg_count = 2;
2392 }
2393 break;
2394 }
2395 if (min_arg_count == -1) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002396 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 +02002397 return LY_EINVAL;
2398 }
Michal Vasko004d3152020-06-11 19:59:22 +02002399 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002400
2401 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02002402 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002403 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002404 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002405
2406 /* ( Expr ( ',' Expr )* )? */
2407 arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002408 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002409 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002410 if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002411 ++arg_count;
aPiecekbf968d92021-05-27 14:35:05 +02002412 rc = reparse_or_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002413 LY_CHECK_RET(rc);
2414 }
Michal Vasko004d3152020-06-11 19:59:22 +02002415 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) {
2416 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002417
2418 ++arg_count;
aPiecekbf968d92021-05-27 14:35:05 +02002419 rc = reparse_or_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002420 LY_CHECK_RET(rc);
2421 }
2422
2423 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02002424 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002425 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002426 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002427
Radek Krejci857189e2020-09-01 13:26:36 +02002428 if ((arg_count < (uint32_t)min_arg_count) || (arg_count > max_arg_count)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002429 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 +02002430 return LY_EVALID;
2431 }
2432
2433 return LY_SUCCESS;
2434}
2435
2436/**
2437 * @brief Reparse PathExpr. Logs directly on error.
2438 *
2439 * [10] PathExpr ::= LocationPath | PrimaryExpr Predicate*
2440 * | PrimaryExpr Predicate* '/' RelativeLocationPath
2441 * | PrimaryExpr Predicate* '//' RelativeLocationPath
2442 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
aPiecekfba75362021-10-07 12:39:48 +02002443 * [8] PrimaryExpr ::= VariableReference | '(' Expr ')' | Literal | Number | FunctionCall
Michal Vasko03ff5a72019-09-11 13:49:33 +02002444 *
2445 * @param[in] ctx Context for logging.
2446 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002447 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002448 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002449 * @return LY_ERR
2450 */
2451static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02002452reparse_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 +02002453{
2454 LY_ERR rc;
2455
Michal Vasko004d3152020-06-11 19:59:22 +02002456 if (lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko14676352020-05-29 11:35:55 +02002457 return LY_EVALID;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002458 }
2459
Michal Vasko004d3152020-06-11 19:59:22 +02002460 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002461 case LYXP_TOKEN_PAR1:
2462 /* '(' Expr ')' Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002463 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002464
aPiecekbf968d92021-05-27 14:35:05 +02002465 rc = reparse_or_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002466 LY_CHECK_RET(rc);
2467
Michal Vasko004d3152020-06-11 19:59:22 +02002468 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002469 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002470 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002471 goto predicate;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002472 case LYXP_TOKEN_DOT:
2473 case LYXP_TOKEN_DDOT:
Michal Vasko49fec8e2022-05-24 10:28:33 +02002474 case LYXP_TOKEN_AXISNAME:
Michal Vasko03ff5a72019-09-11 13:49:33 +02002475 case LYXP_TOKEN_AT:
2476 case LYXP_TOKEN_NAMETEST:
2477 case LYXP_TOKEN_NODETYPE:
2478 /* RelativeLocationPath */
aPiecekbf968d92021-05-27 14:35:05 +02002479 rc = reparse_relative_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002480 LY_CHECK_RET(rc);
2481 break;
aPiecekfba75362021-10-07 12:39:48 +02002482 case LYXP_TOKEN_VARREF:
2483 /* VariableReference */
2484 ++(*tok_idx);
2485 goto predicate;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002486 case LYXP_TOKEN_FUNCNAME:
2487 /* FunctionCall */
aPiecekbf968d92021-05-27 14:35:05 +02002488 rc = reparse_function_call(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002489 LY_CHECK_RET(rc);
2490 goto predicate;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002491 case LYXP_TOKEN_OPER_PATH:
2492 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +02002493 /* AbsoluteLocationPath */
aPiecekbf968d92021-05-27 14:35:05 +02002494 rc = reparse_absolute_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002495 LY_CHECK_RET(rc);
2496 break;
2497 case LYXP_TOKEN_LITERAL:
2498 /* Literal */
Michal Vasko004d3152020-06-11 19:59:22 +02002499 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002500 goto predicate;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002501 case LYXP_TOKEN_NUMBER:
2502 /* Number */
Michal Vasko004d3152020-06-11 19:59:22 +02002503 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002504 goto predicate;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002505 default:
Michal Vasko49fec8e2022-05-24 10:28:33 +02002506 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 +02002507 return LY_EVALID;
2508 }
2509
2510 return LY_SUCCESS;
2511
2512predicate:
2513 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002514 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
aPiecekbf968d92021-05-27 14:35:05 +02002515 rc = reparse_predicate(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002516 LY_CHECK_RET(rc);
2517 }
2518
2519 /* ('/' or '//') RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002520 if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002521
2522 /* '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02002523 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002524
aPiecekbf968d92021-05-27 14:35:05 +02002525 rc = reparse_relative_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002526 LY_CHECK_RET(rc);
2527 }
2528
2529 return LY_SUCCESS;
2530}
2531
2532/**
2533 * @brief Reparse UnaryExpr. Logs directly on error.
2534 *
2535 * [17] UnaryExpr ::= UnionExpr | '-' UnaryExpr
2536 * [18] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
2537 *
2538 * @param[in] ctx Context for logging.
2539 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002540 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002541 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002542 * @return LY_ERR
2543 */
2544static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02002545reparse_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 +02002546{
Michal Vaskodd528af2022-08-08 14:35:07 +02002547 uint32_t prev_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002548 LY_ERR rc;
2549
2550 /* ('-')* */
Michal Vasko004d3152020-06-11 19:59:22 +02002551 prev_exp = *tok_idx;
Michal Vasko69730152020-10-09 16:30:07 +02002552 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2553 (exp->expr[exp->tok_pos[*tok_idx]] == '-')) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002554 exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNARY);
Michal Vasko004d3152020-06-11 19:59:22 +02002555 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002556 }
2557
2558 /* PathExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002559 prev_exp = *tok_idx;
aPiecekbf968d92021-05-27 14:35:05 +02002560 rc = reparse_path_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002561 LY_CHECK_RET(rc);
2562
2563 /* ('|' PathExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002564 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_UNI)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002565 exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNION);
Michal Vasko004d3152020-06-11 19:59:22 +02002566 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002567
aPiecekbf968d92021-05-27 14:35:05 +02002568 rc = reparse_path_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002569 LY_CHECK_RET(rc);
2570 }
2571
2572 return LY_SUCCESS;
2573}
2574
2575/**
2576 * @brief Reparse AdditiveExpr. Logs directly on error.
2577 *
2578 * [15] AdditiveExpr ::= MultiplicativeExpr
2579 * | AdditiveExpr '+' MultiplicativeExpr
2580 * | AdditiveExpr '-' MultiplicativeExpr
2581 * [16] MultiplicativeExpr ::= UnaryExpr
2582 * | MultiplicativeExpr '*' UnaryExpr
2583 * | MultiplicativeExpr 'div' UnaryExpr
2584 * | MultiplicativeExpr 'mod' UnaryExpr
2585 *
2586 * @param[in] ctx Context for logging.
2587 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002588 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002589 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002590 * @return LY_ERR
2591 */
2592static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02002593reparse_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 +02002594{
Michal Vaskodd528af2022-08-08 14:35:07 +02002595 uint32_t prev_add_exp, prev_mul_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002596 LY_ERR rc;
2597
Michal Vasko004d3152020-06-11 19:59:22 +02002598 prev_add_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002599 goto reparse_multiplicative_expr;
2600
2601 /* ('+' / '-' MultiplicativeExpr)* */
Michal Vasko69730152020-10-09 16:30:07 +02002602 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2603 ((exp->expr[exp->tok_pos[*tok_idx]] == '+') || (exp->expr[exp->tok_pos[*tok_idx]] == '-'))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002604 exp_repeat_push(exp, prev_add_exp, LYXP_EXPR_ADDITIVE);
Michal Vasko004d3152020-06-11 19:59:22 +02002605 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002606
2607reparse_multiplicative_expr:
2608 /* UnaryExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002609 prev_mul_exp = *tok_idx;
aPiecekbf968d92021-05-27 14:35:05 +02002610 rc = reparse_unary_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002611 LY_CHECK_RET(rc);
2612
2613 /* ('*' / 'div' / 'mod' UnaryExpr)* */
Michal Vasko69730152020-10-09 16:30:07 +02002614 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2615 ((exp->expr[exp->tok_pos[*tok_idx]] == '*') || (exp->tok_len[*tok_idx] == 3))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002616 exp_repeat_push(exp, prev_mul_exp, LYXP_EXPR_MULTIPLICATIVE);
Michal Vasko004d3152020-06-11 19:59:22 +02002617 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002618
aPiecekbf968d92021-05-27 14:35:05 +02002619 rc = reparse_unary_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002620 LY_CHECK_RET(rc);
2621 }
2622 }
2623
2624 return LY_SUCCESS;
2625}
2626
2627/**
2628 * @brief Reparse EqualityExpr. Logs directly on error.
2629 *
2630 * [13] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
2631 * | EqualityExpr '!=' RelationalExpr
2632 * [14] RelationalExpr ::= AdditiveExpr
2633 * | RelationalExpr '<' AdditiveExpr
2634 * | RelationalExpr '>' AdditiveExpr
2635 * | RelationalExpr '<=' AdditiveExpr
2636 * | RelationalExpr '>=' AdditiveExpr
2637 *
2638 * @param[in] ctx Context for logging.
2639 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002640 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002641 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002642 * @return LY_ERR
2643 */
2644static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02002645reparse_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 +02002646{
Michal Vaskodd528af2022-08-08 14:35:07 +02002647 uint32_t prev_eq_exp, prev_rel_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002648 LY_ERR rc;
2649
Michal Vasko004d3152020-06-11 19:59:22 +02002650 prev_eq_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002651 goto reparse_additive_expr;
2652
2653 /* ('=' / '!=' RelationalExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002654 while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_EQUAL, LYXP_TOKEN_OPER_NEQUAL)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002655 exp_repeat_push(exp, prev_eq_exp, LYXP_EXPR_EQUALITY);
Michal Vasko004d3152020-06-11 19:59:22 +02002656 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002657
2658reparse_additive_expr:
2659 /* AdditiveExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002660 prev_rel_exp = *tok_idx;
aPiecekbf968d92021-05-27 14:35:05 +02002661 rc = reparse_additive_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002662 LY_CHECK_RET(rc);
2663
2664 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002665 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_COMP)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002666 exp_repeat_push(exp, prev_rel_exp, LYXP_EXPR_RELATIONAL);
Michal Vasko004d3152020-06-11 19:59:22 +02002667 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002668
aPiecekbf968d92021-05-27 14:35:05 +02002669 rc = reparse_additive_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002670 LY_CHECK_RET(rc);
2671 }
2672 }
2673
2674 return LY_SUCCESS;
2675}
2676
2677/**
2678 * @brief Reparse OrExpr. Logs directly on error.
2679 *
2680 * [11] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
2681 * [12] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
2682 *
2683 * @param[in] ctx Context for logging.
2684 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002685 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002686 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002687 * @return LY_ERR
2688 */
2689static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02002690reparse_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 +02002691{
Michal Vaskodd528af2022-08-08 14:35:07 +02002692 uint32_t prev_or_exp, prev_and_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002693 LY_ERR rc;
2694
aPiecekbf968d92021-05-27 14:35:05 +02002695 ++depth;
2696 LY_CHECK_ERR_RET(depth > LYXP_MAX_BLOCK_DEPTH, LOGVAL(ctx, LY_VCODE_XP_DEPTH), LY_EINVAL);
2697
Michal Vasko004d3152020-06-11 19:59:22 +02002698 prev_or_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002699 goto reparse_equality_expr;
2700
2701 /* ('or' AndExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002702 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 +02002703 exp_repeat_push(exp, prev_or_exp, LYXP_EXPR_OR);
Michal Vasko004d3152020-06-11 19:59:22 +02002704 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002705
2706reparse_equality_expr:
2707 /* EqualityExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002708 prev_and_exp = *tok_idx;
aPiecekbf968d92021-05-27 14:35:05 +02002709 rc = reparse_equality_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002710 LY_CHECK_RET(rc);
2711
2712 /* ('and' EqualityExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002713 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 +02002714 exp_repeat_push(exp, prev_and_exp, LYXP_EXPR_AND);
Michal Vasko004d3152020-06-11 19:59:22 +02002715 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002716
aPiecekbf968d92021-05-27 14:35:05 +02002717 rc = reparse_equality_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002718 LY_CHECK_RET(rc);
2719 }
2720 }
2721
2722 return LY_SUCCESS;
2723}
Radek Krejcib1646a92018-11-02 16:08:26 +01002724
2725/**
2726 * @brief Parse NCName.
2727 *
2728 * @param[in] ncname Name to parse.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002729 * @return Length of @p ncname valid bytes.
Radek Krejcib1646a92018-11-02 16:08:26 +01002730 */
Michal Vasko49fec8e2022-05-24 10:28:33 +02002731static ssize_t
Radek Krejcib1646a92018-11-02 16:08:26 +01002732parse_ncname(const char *ncname)
2733{
Radek Krejci1deb5be2020-08-26 16:43:36 +02002734 uint32_t uc;
Radek Krejcid4270262019-01-07 15:07:25 +01002735 size_t size;
Michal Vasko49fec8e2022-05-24 10:28:33 +02002736 ssize_t len = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01002737
2738 LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), 0);
2739 if (!is_xmlqnamestartchar(uc) || (uc == ':')) {
2740 return len;
2741 }
2742
2743 do {
2744 len += size;
Radek Krejci9a564c92019-01-07 14:53:57 +01002745 if (!*ncname) {
2746 break;
2747 }
Radek Krejcid4270262019-01-07 15:07:25 +01002748 LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), -len);
Radek Krejcib1646a92018-11-02 16:08:26 +01002749 } while (is_xmlqnamechar(uc) && (uc != ':'));
2750
2751 return len;
2752}
2753
2754/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02002755 * @brief Add @p token into the expression @p exp.
Radek Krejcib1646a92018-11-02 16:08:26 +01002756 *
Michal Vasko03ff5a72019-09-11 13:49:33 +02002757 * @param[in] ctx Context for logging.
Radek Krejcib1646a92018-11-02 16:08:26 +01002758 * @param[in] exp Expression to use.
2759 * @param[in] token Token to add.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002760 * @param[in] tok_pos Token position in the XPath expression.
Radek Krejcib1646a92018-11-02 16:08:26 +01002761 * @param[in] tok_len Token length in the XPath expression.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002762 * @return LY_ERR
Radek Krejcib1646a92018-11-02 16:08:26 +01002763 */
2764static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02002765exp_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 +01002766{
2767 uint32_t prev;
2768
2769 if (exp->used == exp->size) {
2770 prev = exp->size;
2771 exp->size += LYXP_EXPR_SIZE_STEP;
2772 if (prev > exp->size) {
2773 LOGINT(ctx);
2774 return LY_EINT;
2775 }
2776
2777 exp->tokens = ly_realloc(exp->tokens, exp->size * sizeof *exp->tokens);
2778 LY_CHECK_ERR_RET(!exp->tokens, LOGMEM(ctx), LY_EMEM);
2779 exp->tok_pos = ly_realloc(exp->tok_pos, exp->size * sizeof *exp->tok_pos);
2780 LY_CHECK_ERR_RET(!exp->tok_pos, LOGMEM(ctx), LY_EMEM);
2781 exp->tok_len = ly_realloc(exp->tok_len, exp->size * sizeof *exp->tok_len);
2782 LY_CHECK_ERR_RET(!exp->tok_len, LOGMEM(ctx), LY_EMEM);
2783 }
2784
2785 exp->tokens[exp->used] = token;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002786 exp->tok_pos[exp->used] = tok_pos;
Radek Krejcib1646a92018-11-02 16:08:26 +01002787 exp->tok_len[exp->used] = tok_len;
2788 ++exp->used;
2789 return LY_SUCCESS;
2790}
2791
2792void
Michal Vasko14676352020-05-29 11:35:55 +02002793lyxp_expr_free(const struct ly_ctx *ctx, struct lyxp_expr *expr)
Radek Krejcib1646a92018-11-02 16:08:26 +01002794{
Michal Vaskodd528af2022-08-08 14:35:07 +02002795 uint32_t i;
Radek Krejcib1646a92018-11-02 16:08:26 +01002796
2797 if (!expr) {
2798 return;
2799 }
2800
2801 lydict_remove(ctx, expr->expr);
2802 free(expr->tokens);
2803 free(expr->tok_pos);
2804 free(expr->tok_len);
2805 if (expr->repeat) {
2806 for (i = 0; i < expr->used; ++i) {
2807 free(expr->repeat[i]);
2808 }
2809 }
2810 free(expr->repeat);
2811 free(expr);
2812}
2813
Michal Vasko49fec8e2022-05-24 10:28:33 +02002814/**
2815 * @brief Parse Axis name.
2816 *
2817 * @param[in] str String to parse.
2818 * @param[in] str_len Length of @p str.
2819 * @return LY_SUCCESS if an axis.
2820 * @return LY_ENOT otherwise.
2821 */
2822static LY_ERR
2823expr_parse_axis(const char *str, size_t str_len)
2824{
2825 switch (str_len) {
2826 case 4:
2827 if (!strncmp("self", str, str_len)) {
2828 return LY_SUCCESS;
2829 }
2830 break;
2831 case 5:
2832 if (!strncmp("child", str, str_len)) {
2833 return LY_SUCCESS;
2834 }
2835 break;
2836 case 6:
2837 if (!strncmp("parent", str, str_len)) {
2838 return LY_SUCCESS;
2839 }
2840 break;
2841 case 8:
2842 if (!strncmp("ancestor", str, str_len)) {
2843 return LY_SUCCESS;
2844 }
2845 break;
2846 case 9:
2847 if (!strncmp("attribute", str, str_len)) {
2848 return LY_SUCCESS;
2849 } else if (!strncmp("following", str, str_len)) {
2850 return LY_SUCCESS;
2851 } else if (!strncmp("namespace", str, str_len)) {
2852 LOGERR(NULL, LY_EINVAL, "Axis \"namespace\" not supported.");
2853 return LY_ENOT;
2854 } else if (!strncmp("preceding", str, str_len)) {
2855 return LY_SUCCESS;
2856 }
2857 break;
2858 case 10:
2859 if (!strncmp("descendant", str, str_len)) {
2860 return LY_SUCCESS;
2861 }
2862 break;
2863 case 16:
2864 if (!strncmp("ancestor-or-self", str, str_len)) {
2865 return LY_SUCCESS;
2866 }
2867 break;
2868 case 17:
2869 if (!strncmp("following-sibling", str, str_len)) {
2870 return LY_SUCCESS;
2871 } else if (!strncmp("preceding-sibling", str, str_len)) {
2872 return LY_SUCCESS;
2873 }
2874 break;
2875 case 18:
2876 if (!strncmp("descendant-or-self", str, str_len)) {
2877 return LY_SUCCESS;
2878 }
2879 break;
2880 }
2881
2882 return LY_ENOT;
2883}
2884
Radek Krejcif03a9e22020-09-18 20:09:31 +02002885LY_ERR
2886lyxp_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 +01002887{
Radek Krejcif03a9e22020-09-18 20:09:31 +02002888 LY_ERR ret = LY_SUCCESS;
2889 struct lyxp_expr *expr;
Radek Krejcid4270262019-01-07 15:07:25 +01002890 size_t parsed = 0, tok_len;
Radek Krejcib1646a92018-11-02 16:08:26 +01002891 enum lyxp_token tok_type;
Michal Vasko49fec8e2022-05-24 10:28:33 +02002892 ly_bool prev_func_check = 0, prev_ntype_check = 0, has_axis;
Michal Vaskodd528af2022-08-08 14:35:07 +02002893 uint32_t tok_idx = 0;
Michal Vasko49fec8e2022-05-24 10:28:33 +02002894 ssize_t ncname_len;
Radek Krejcib1646a92018-11-02 16:08:26 +01002895
Radek Krejcif03a9e22020-09-18 20:09:31 +02002896 assert(expr_p);
2897
2898 if (!expr_str[0]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002899 LOGVAL(ctx, LY_VCODE_XP_EOF);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002900 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02002901 }
2902
2903 if (!expr_len) {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002904 expr_len = strlen(expr_str);
Michal Vasko004d3152020-06-11 19:59:22 +02002905 }
Michal Vaskodd528af2022-08-08 14:35:07 +02002906 if (expr_len > UINT32_MAX) {
2907 LOGVAL(ctx, LYVE_XPATH, "XPath expression cannot be longer than %" PRIu32 " characters.", UINT32_MAX);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002908 return LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01002909 }
2910
2911 /* init lyxp_expr structure */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002912 expr = calloc(1, sizeof *expr);
2913 LY_CHECK_ERR_GOTO(!expr, LOGMEM(ctx); ret = LY_EMEM, error);
2914 LY_CHECK_GOTO(ret = lydict_insert(ctx, expr_str, expr_len, &expr->expr), error);
2915 expr->used = 0;
2916 expr->size = LYXP_EXPR_SIZE_START;
2917 expr->tokens = malloc(expr->size * sizeof *expr->tokens);
2918 LY_CHECK_ERR_GOTO(!expr->tokens, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002919
Radek Krejcif03a9e22020-09-18 20:09:31 +02002920 expr->tok_pos = malloc(expr->size * sizeof *expr->tok_pos);
2921 LY_CHECK_ERR_GOTO(!expr->tok_pos, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002922
Radek Krejcif03a9e22020-09-18 20:09:31 +02002923 expr->tok_len = malloc(expr->size * sizeof *expr->tok_len);
2924 LY_CHECK_ERR_GOTO(!expr->tok_len, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002925
Michal Vasko004d3152020-06-11 19:59:22 +02002926 /* make expr 0-terminated */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002927 expr_str = expr->expr;
Michal Vasko004d3152020-06-11 19:59:22 +02002928
Radek Krejcif03a9e22020-09-18 20:09:31 +02002929 while (is_xmlws(expr_str[parsed])) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002930 ++parsed;
2931 }
2932
2933 do {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002934 if (expr_str[parsed] == '(') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002935
2936 /* '(' */
2937 tok_len = 1;
2938 tok_type = LYXP_TOKEN_PAR1;
2939
Michal Vasko49fec8e2022-05-24 10:28:33 +02002940 if (prev_ntype_check && expr->used && (expr->tokens[expr->used - 1] == LYXP_TOKEN_NAMETEST) &&
2941 (((expr->tok_len[expr->used - 1] == 4) &&
2942 (!strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "node", 4) ||
2943 !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "text", 4))) ||
2944 ((expr->tok_len[expr->used - 1] == 7) &&
2945 !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "comment", 7)))) {
2946 /* it is NodeType after all */
2947 expr->tokens[expr->used - 1] = LYXP_TOKEN_NODETYPE;
2948
2949 prev_ntype_check = 0;
2950 prev_func_check = 0;
2951 } else if (prev_func_check && expr->used && (expr->tokens[expr->used - 1] == LYXP_TOKEN_NAMETEST)) {
2952 /* it is FunctionName after all */
2953 expr->tokens[expr->used - 1] = LYXP_TOKEN_FUNCNAME;
2954
2955 prev_ntype_check = 0;
2956 prev_func_check = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01002957 }
2958
Radek Krejcif03a9e22020-09-18 20:09:31 +02002959 } else if (expr_str[parsed] == ')') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002960
2961 /* ')' */
2962 tok_len = 1;
2963 tok_type = LYXP_TOKEN_PAR2;
2964
Radek Krejcif03a9e22020-09-18 20:09:31 +02002965 } else if (expr_str[parsed] == '[') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002966
2967 /* '[' */
2968 tok_len = 1;
2969 tok_type = LYXP_TOKEN_BRACK1;
2970
Radek Krejcif03a9e22020-09-18 20:09:31 +02002971 } else if (expr_str[parsed] == ']') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002972
2973 /* ']' */
2974 tok_len = 1;
2975 tok_type = LYXP_TOKEN_BRACK2;
2976
Radek Krejcif03a9e22020-09-18 20:09:31 +02002977 } else if (!strncmp(&expr_str[parsed], "..", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002978
2979 /* '..' */
2980 tok_len = 2;
2981 tok_type = LYXP_TOKEN_DDOT;
2982
Radek Krejcif03a9e22020-09-18 20:09:31 +02002983 } else if ((expr_str[parsed] == '.') && (!isdigit(expr_str[parsed + 1]))) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002984
2985 /* '.' */
2986 tok_len = 1;
2987 tok_type = LYXP_TOKEN_DOT;
2988
Radek Krejcif03a9e22020-09-18 20:09:31 +02002989 } else if (expr_str[parsed] == '@') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002990
2991 /* '@' */
2992 tok_len = 1;
2993 tok_type = LYXP_TOKEN_AT;
2994
Radek Krejcif03a9e22020-09-18 20:09:31 +02002995 } else if (expr_str[parsed] == ',') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002996
2997 /* ',' */
2998 tok_len = 1;
2999 tok_type = LYXP_TOKEN_COMMA;
3000
Radek Krejcif03a9e22020-09-18 20:09:31 +02003001 } else if (expr_str[parsed] == '\'') {
Radek Krejcib1646a92018-11-02 16:08:26 +01003002
3003 /* Literal with ' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02003004 for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\''); ++tok_len) {}
3005 LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0',
Radek Krejci2efc45b2020-12-22 16:25:44 +01003006 LOGVAL(ctx, LY_VCODE_XP_EOE, expr_str[parsed], &expr_str[parsed]); ret = LY_EVALID,
Michal Vasko69730152020-10-09 16:30:07 +02003007 error);
Radek Krejcib1646a92018-11-02 16:08:26 +01003008 ++tok_len;
3009 tok_type = LYXP_TOKEN_LITERAL;
3010
Radek Krejcif03a9e22020-09-18 20:09:31 +02003011 } else if (expr_str[parsed] == '\"') {
Radek Krejcib1646a92018-11-02 16:08:26 +01003012
3013 /* Literal with " */
Radek Krejcif03a9e22020-09-18 20:09:31 +02003014 for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\"'); ++tok_len) {}
3015 LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0',
Radek Krejci2efc45b2020-12-22 16:25:44 +01003016 LOGVAL(ctx, LY_VCODE_XP_EOE, expr_str[parsed], &expr_str[parsed]); ret = LY_EVALID,
Michal Vasko69730152020-10-09 16:30:07 +02003017 error);
Radek Krejcib1646a92018-11-02 16:08:26 +01003018 ++tok_len;
3019 tok_type = LYXP_TOKEN_LITERAL;
3020
Radek Krejcif03a9e22020-09-18 20:09:31 +02003021 } else if ((expr_str[parsed] == '.') || (isdigit(expr_str[parsed]))) {
Radek Krejcib1646a92018-11-02 16:08:26 +01003022
3023 /* Number */
Radek Krejcif03a9e22020-09-18 20:09:31 +02003024 for (tok_len = 0; isdigit(expr_str[parsed + tok_len]); ++tok_len) {}
3025 if (expr_str[parsed + tok_len] == '.') {
Radek Krejcib1646a92018-11-02 16:08:26 +01003026 ++tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02003027 for ( ; isdigit(expr_str[parsed + tok_len]); ++tok_len) {}
Radek Krejcib1646a92018-11-02 16:08:26 +01003028 }
3029 tok_type = LYXP_TOKEN_NUMBER;
3030
aPiecekfba75362021-10-07 12:39:48 +02003031 } else if (expr_str[parsed] == '$') {
3032
3033 /* VariableReference */
3034 parsed++;
Michal Vasko49fec8e2022-05-24 10:28:33 +02003035 ncname_len = parse_ncname(&expr_str[parsed]);
aPiecekfba75362021-10-07 12:39:48 +02003036 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 +02003037 (uint32_t)(parsed - ncname_len + 1), expr_str); ret = LY_EVALID, error);
aPiecekfba75362021-10-07 12:39:48 +02003038 tok_len = ncname_len;
3039 LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == ':',
3040 LOGVAL(ctx, LYVE_XPATH, "Variable with prefix is not supported."); ret = LY_EVALID,
3041 error);
3042 tok_type = LYXP_TOKEN_VARREF;
3043
Radek Krejcif03a9e22020-09-18 20:09:31 +02003044 } else if (expr_str[parsed] == '/') {
Radek Krejcib1646a92018-11-02 16:08:26 +01003045
3046 /* Operator '/', '//' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02003047 if (!strncmp(&expr_str[parsed], "//", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01003048 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003049 tok_type = LYXP_TOKEN_OPER_RPATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01003050 } else {
3051 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003052 tok_type = LYXP_TOKEN_OPER_PATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01003053 }
Radek Krejcib1646a92018-11-02 16:08:26 +01003054
Radek Krejcif03a9e22020-09-18 20:09:31 +02003055 } else if (!strncmp(&expr_str[parsed], "!=", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01003056
Michal Vasko3e48bf32020-06-01 08:39:07 +02003057 /* Operator '!=' */
Radek Krejcib1646a92018-11-02 16:08:26 +01003058 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003059 tok_type = LYXP_TOKEN_OPER_NEQUAL;
3060
Radek Krejcif03a9e22020-09-18 20:09:31 +02003061 } else if (!strncmp(&expr_str[parsed], "<=", 2) || !strncmp(&expr_str[parsed], ">=", 2)) {
Michal Vasko3e48bf32020-06-01 08:39:07 +02003062
3063 /* Operator '<=', '>=' */
3064 tok_len = 2;
3065 tok_type = LYXP_TOKEN_OPER_COMP;
Radek Krejcib1646a92018-11-02 16:08:26 +01003066
Radek Krejcif03a9e22020-09-18 20:09:31 +02003067 } else if (expr_str[parsed] == '|') {
Radek Krejcib1646a92018-11-02 16:08:26 +01003068
3069 /* Operator '|' */
3070 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003071 tok_type = LYXP_TOKEN_OPER_UNI;
Radek Krejcib1646a92018-11-02 16:08:26 +01003072
Radek Krejcif03a9e22020-09-18 20:09:31 +02003073 } else if ((expr_str[parsed] == '+') || (expr_str[parsed] == '-')) {
Radek Krejcib1646a92018-11-02 16:08:26 +01003074
3075 /* Operator '+', '-' */
3076 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003077 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01003078
Radek Krejcif03a9e22020-09-18 20:09:31 +02003079 } else if (expr_str[parsed] == '=') {
Radek Krejcib1646a92018-11-02 16:08:26 +01003080
Michal Vasko3e48bf32020-06-01 08:39:07 +02003081 /* Operator '=' */
Radek Krejcib1646a92018-11-02 16:08:26 +01003082 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003083 tok_type = LYXP_TOKEN_OPER_EQUAL;
3084
Radek Krejcif03a9e22020-09-18 20:09:31 +02003085 } else if ((expr_str[parsed] == '<') || (expr_str[parsed] == '>')) {
Michal Vasko3e48bf32020-06-01 08:39:07 +02003086
3087 /* Operator '<', '>' */
3088 tok_len = 1;
3089 tok_type = LYXP_TOKEN_OPER_COMP;
Radek Krejcib1646a92018-11-02 16:08:26 +01003090
Michal Vasko69730152020-10-09 16:30:07 +02003091 } else if (expr->used && (expr->tokens[expr->used - 1] != LYXP_TOKEN_AT) &&
3092 (expr->tokens[expr->used - 1] != LYXP_TOKEN_PAR1) &&
3093 (expr->tokens[expr->used - 1] != LYXP_TOKEN_BRACK1) &&
3094 (expr->tokens[expr->used - 1] != LYXP_TOKEN_COMMA) &&
3095 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_LOG) &&
3096 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_EQUAL) &&
3097 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_NEQUAL) &&
3098 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_COMP) &&
3099 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_MATH) &&
3100 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_UNI) &&
3101 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_PATH) &&
3102 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_RPATH)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01003103
3104 /* Operator '*', 'or', 'and', 'mod', or 'div' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02003105 if (expr_str[parsed] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01003106 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003107 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01003108
Radek Krejcif03a9e22020-09-18 20:09:31 +02003109 } else if (!strncmp(&expr_str[parsed], "or", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01003110 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003111 tok_type = LYXP_TOKEN_OPER_LOG;
Radek Krejcib1646a92018-11-02 16:08:26 +01003112
Radek Krejcif03a9e22020-09-18 20:09:31 +02003113 } else if (!strncmp(&expr_str[parsed], "and", 3)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01003114 tok_len = 3;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003115 tok_type = LYXP_TOKEN_OPER_LOG;
Radek Krejcib1646a92018-11-02 16:08:26 +01003116
Radek Krejcif03a9e22020-09-18 20:09:31 +02003117 } else if (!strncmp(&expr_str[parsed], "mod", 3) || !strncmp(&expr_str[parsed], "div", 3)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01003118 tok_len = 3;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003119 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01003120
Michal Vasko49fec8e2022-05-24 10:28:33 +02003121 } else if (prev_ntype_check || prev_func_check) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003122 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 +02003123 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 +02003124 ret = LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01003125 goto error;
3126 } else {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02003127 LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed], (uint32_t)(parsed + 1), expr_str);
Radek Krejcif03a9e22020-09-18 20:09:31 +02003128 ret = LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01003129 goto error;
3130 }
Radek Krejcib1646a92018-11-02 16:08:26 +01003131 } else {
3132
Michal Vasko49fec8e2022-05-24 10:28:33 +02003133 /* (AxisName '::')? ((NCName ':')? '*' | QName) or NodeType/FunctionName */
3134 if (expr_str[parsed] == '*') {
3135 ncname_len = 1;
3136 } else {
3137 ncname_len = parse_ncname(&expr_str[parsed]);
3138 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 +02003139 (uint32_t)(parsed - ncname_len + 1), expr_str); ret = LY_EVALID, error);
Michal Vasko49fec8e2022-05-24 10:28:33 +02003140 }
Radek Krejcib1646a92018-11-02 16:08:26 +01003141 tok_len = ncname_len;
3142
Michal Vasko49fec8e2022-05-24 10:28:33 +02003143 has_axis = 0;
3144 if (!strncmp(&expr_str[parsed + tok_len], "::", 2)) {
3145 /* axis */
3146 LY_CHECK_ERR_GOTO(expr_parse_axis(&expr_str[parsed], ncname_len),
Michal Vasko7b3a00e2023-08-09 11:58:03 +02003147 LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed], (uint32_t)(parsed + 1), expr_str); ret = LY_EVALID,
3148 error);
Michal Vasko49fec8e2022-05-24 10:28:33 +02003149 tok_type = LYXP_TOKEN_AXISNAME;
3150
3151 LY_CHECK_GOTO(ret = exp_add_token(ctx, expr, tok_type, parsed, tok_len), error);
3152 parsed += tok_len;
3153
3154 /* '::' */
3155 tok_len = 2;
3156 tok_type = LYXP_TOKEN_DCOLON;
3157
3158 LY_CHECK_GOTO(ret = exp_add_token(ctx, expr, tok_type, parsed, tok_len), error);
3159 parsed += tok_len;
3160
3161 if (expr_str[parsed] == '*') {
3162 ncname_len = 1;
3163 } else {
3164 ncname_len = parse_ncname(&expr_str[parsed]);
3165 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 +02003166 (uint32_t)(parsed - ncname_len + 1), expr_str); ret = LY_EVALID, error);
Michal Vasko49fec8e2022-05-24 10:28:33 +02003167 }
3168 tok_len = ncname_len;
3169
3170 has_axis = 1;
3171 }
3172
Radek Krejcif03a9e22020-09-18 20:09:31 +02003173 if (expr_str[parsed + tok_len] == ':') {
Radek Krejcib1646a92018-11-02 16:08:26 +01003174 ++tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02003175 if (expr_str[parsed + tok_len] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01003176 ++tok_len;
3177 } else {
Radek Krejcif03a9e22020-09-18 20:09:31 +02003178 ncname_len = parse_ncname(&expr_str[parsed + tok_len]);
Michal Vaskoe2be5462021-08-04 10:49:42 +02003179 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 +02003180 (uint32_t)(parsed - ncname_len + 1), expr_str); ret = LY_EVALID, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01003181 tok_len += ncname_len;
3182 }
Michal Vasko49fec8e2022-05-24 10:28:33 +02003183 /* remove old flags to prevent ambiguities */
3184 prev_ntype_check = 0;
3185 prev_func_check = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01003186 tok_type = LYXP_TOKEN_NAMETEST;
3187 } else {
Michal Vasko49fec8e2022-05-24 10:28:33 +02003188 /* if not '*', there is no prefix so it can still be NodeType/FunctionName, we can't finally decide now */
3189 prev_ntype_check = (expr_str[parsed] == '*') ? 0 : 1;
3190 prev_func_check = (prev_ntype_check && !has_axis) ? 1 : 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01003191 tok_type = LYXP_TOKEN_NAMETEST;
3192 }
3193 }
3194
3195 /* store the token, move on to the next one */
Radek Krejcif03a9e22020-09-18 20:09:31 +02003196 LY_CHECK_GOTO(ret = exp_add_token(ctx, expr, tok_type, parsed, tok_len), error);
Radek Krejcib1646a92018-11-02 16:08:26 +01003197 parsed += tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02003198 while (is_xmlws(expr_str[parsed])) {
Radek Krejcib1646a92018-11-02 16:08:26 +01003199 ++parsed;
3200 }
3201
Radek Krejcif03a9e22020-09-18 20:09:31 +02003202 } while (expr_str[parsed]);
Radek Krejcib1646a92018-11-02 16:08:26 +01003203
Michal Vasko004d3152020-06-11 19:59:22 +02003204 if (reparse) {
3205 /* prealloc repeat */
Radek Krejcif03a9e22020-09-18 20:09:31 +02003206 expr->repeat = calloc(expr->size, sizeof *expr->repeat);
3207 LY_CHECK_ERR_GOTO(!expr->repeat, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01003208
Michal Vasko004d3152020-06-11 19:59:22 +02003209 /* fill repeat */
aPiecekbf968d92021-05-27 14:35:05 +02003210 LY_CHECK_ERR_GOTO(reparse_or_expr(ctx, expr, &tok_idx, 0), ret = LY_EVALID, error);
Radek Krejcif03a9e22020-09-18 20:09:31 +02003211 if (expr->used > tok_idx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003212 LOGVAL(ctx, LYVE_XPATH, "Unparsed characters \"%s\" left at the end of an XPath expression.",
Michal Vasko69730152020-10-09 16:30:07 +02003213 &expr->expr[expr->tok_pos[tok_idx]]);
Radek Krejcif03a9e22020-09-18 20:09:31 +02003214 ret = LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02003215 goto error;
3216 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003217 }
3218
Radek Krejcif03a9e22020-09-18 20:09:31 +02003219 print_expr_struct_debug(expr);
3220 *expr_p = expr;
3221 return LY_SUCCESS;
Radek Krejcib1646a92018-11-02 16:08:26 +01003222
3223error:
Radek Krejcif03a9e22020-09-18 20:09:31 +02003224 lyxp_expr_free(ctx, expr);
3225 return ret;
Radek Krejcib1646a92018-11-02 16:08:26 +01003226}
3227
Michal Vasko1734be92020-09-22 08:55:10 +02003228LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02003229lyxp_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 +02003230 struct lyxp_expr **dup_p)
Michal Vasko004d3152020-06-11 19:59:22 +02003231{
Michal Vasko1734be92020-09-22 08:55:10 +02003232 LY_ERR ret = LY_SUCCESS;
3233 struct lyxp_expr *dup = NULL;
Michal Vaskod59039d2022-09-09 09:07:30 +02003234 uint32_t used = 0, i, j, expr_len;
Michal Vaskoe33134a2022-07-29 14:54:40 +02003235 const char *expr_start;
3236
3237 assert((!start_idx && !end_idx) || ((start_idx < exp->used) && (end_idx < exp->used) && (start_idx <= end_idx)));
Michal Vasko004d3152020-06-11 19:59:22 +02003238
Michal Vasko7f45cf22020-10-01 12:49:44 +02003239 if (!exp) {
3240 goto cleanup;
3241 }
3242
Michal Vaskoe33134a2022-07-29 14:54:40 +02003243 if (!start_idx && !end_idx) {
3244 end_idx = exp->used - 1;
3245 }
3246
3247 expr_start = exp->expr + exp->tok_pos[start_idx];
3248 expr_len = (exp->tok_pos[end_idx] + exp->tok_len[end_idx]) - exp->tok_pos[start_idx];
3249
Michal Vasko004d3152020-06-11 19:59:22 +02003250 dup = calloc(1, sizeof *dup);
Michal Vasko1734be92020-09-22 08:55:10 +02003251 LY_CHECK_ERR_GOTO(!dup, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02003252
Michal Vasko08e9b112021-06-11 15:41:17 +02003253 if (exp->used) {
Michal Vaskoe33134a2022-07-29 14:54:40 +02003254 used = (end_idx - start_idx) + 1;
3255
3256 dup->tokens = malloc(used * sizeof *dup->tokens);
Michal Vasko08e9b112021-06-11 15:41:17 +02003257 LY_CHECK_ERR_GOTO(!dup->tokens, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vaskoe33134a2022-07-29 14:54:40 +02003258 memcpy(dup->tokens, exp->tokens + start_idx, used * sizeof *dup->tokens);
Michal Vasko004d3152020-06-11 19:59:22 +02003259
Michal Vaskoe33134a2022-07-29 14:54:40 +02003260 dup->tok_pos = malloc(used * sizeof *dup->tok_pos);
Michal Vasko08e9b112021-06-11 15:41:17 +02003261 LY_CHECK_ERR_GOTO(!dup->tok_pos, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vaskoe33134a2022-07-29 14:54:40 +02003262 memcpy(dup->tok_pos, exp->tok_pos + start_idx, used * sizeof *dup->tok_pos);
Michal Vasko004d3152020-06-11 19:59:22 +02003263
Michal Vaskoe33134a2022-07-29 14:54:40 +02003264 if (start_idx) {
3265 /* fix the indices in the expression */
3266 for (i = 0; i < used; ++i) {
3267 dup->tok_pos[i] -= expr_start - exp->expr;
3268 }
3269 }
3270
3271 dup->tok_len = malloc(used * sizeof *dup->tok_len);
Michal Vasko08e9b112021-06-11 15:41:17 +02003272 LY_CHECK_ERR_GOTO(!dup->tok_len, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vaskoe33134a2022-07-29 14:54:40 +02003273 memcpy(dup->tok_len, exp->tok_len + start_idx, used * sizeof *dup->tok_len);
Michal Vasko004d3152020-06-11 19:59:22 +02003274
Michal Vasko79a7a872022-06-17 09:00:48 +02003275 if (exp->repeat) {
Michal Vaskoe33134a2022-07-29 14:54:40 +02003276 dup->repeat = malloc(used * sizeof *dup->repeat);
Michal Vasko79a7a872022-06-17 09:00:48 +02003277 LY_CHECK_ERR_GOTO(!dup->repeat, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vaskoe33134a2022-07-29 14:54:40 +02003278 for (i = start_idx; i <= end_idx; ++i) {
Michal Vasko79a7a872022-06-17 09:00:48 +02003279 if (!exp->repeat[i]) {
Michal Vaskoe33134a2022-07-29 14:54:40 +02003280 dup->repeat[i - start_idx] = NULL;
Michal Vasko79a7a872022-06-17 09:00:48 +02003281 } else {
3282 for (j = 0; exp->repeat[i][j]; ++j) {}
3283 /* the ending 0 as well */
3284 ++j;
Michal Vasko004d3152020-06-11 19:59:22 +02003285
Michal Vaskoe33134a2022-07-29 14:54:40 +02003286 dup->repeat[i - start_idx] = malloc(j * sizeof **dup->repeat);
3287 LY_CHECK_ERR_GOTO(!dup->repeat[i - start_idx], LOGMEM(ctx); ret = LY_EMEM, cleanup);
3288 memcpy(dup->repeat[i - start_idx], exp->repeat[i], j * sizeof **dup->repeat);
Michal Vasko79a7a872022-06-17 09:00:48 +02003289 }
Michal Vasko08e9b112021-06-11 15:41:17 +02003290 }
Michal Vasko004d3152020-06-11 19:59:22 +02003291 }
3292 }
3293
Michal Vaskoe33134a2022-07-29 14:54:40 +02003294 dup->used = used;
3295 dup->size = used;
3296
3297 /* copy only subexpression */
3298 LY_CHECK_GOTO(ret = lydict_insert(ctx, expr_start, expr_len, &dup->expr), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02003299
Michal Vasko1734be92020-09-22 08:55:10 +02003300cleanup:
3301 if (ret) {
3302 lyxp_expr_free(ctx, dup);
3303 } else {
3304 *dup_p = dup;
3305 }
3306 return ret;
Michal Vasko004d3152020-06-11 19:59:22 +02003307}
3308
Michal Vasko03ff5a72019-09-11 13:49:33 +02003309/**
3310 * @brief Get the last-added schema node that is currently in the context.
3311 *
3312 * @param[in] set Set to search in.
3313 * @return Last-added schema context node, NULL if no node is in context.
3314 */
3315static struct lysc_node *
3316warn_get_scnode_in_ctx(struct lyxp_set *set)
3317{
3318 uint32_t i;
3319
3320 if (!set || (set->type != LYXP_SET_SCNODE_SET)) {
3321 return NULL;
3322 }
3323
3324 i = set->used;
3325 do {
3326 --i;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003327 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003328 /* if there are more, simply return the first found (last added) */
3329 return set->val.scnodes[i].scnode;
3330 }
3331 } while (i);
3332
3333 return NULL;
3334}
3335
3336/**
3337 * @brief Test whether a type is numeric - integer type or decimal64.
3338 *
3339 * @param[in] type Type to test.
Radek Krejci857189e2020-09-01 13:26:36 +02003340 * @return Boolean value whether @p type is numeric type or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003341 */
Radek Krejci857189e2020-09-01 13:26:36 +02003342static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003343warn_is_numeric_type(struct lysc_type *type)
3344{
3345 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003346 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003347 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003348
3349 switch (type->basetype) {
3350 case LY_TYPE_DEC64:
3351 case LY_TYPE_INT8:
3352 case LY_TYPE_UINT8:
3353 case LY_TYPE_INT16:
3354 case LY_TYPE_UINT16:
3355 case LY_TYPE_INT32:
3356 case LY_TYPE_UINT32:
3357 case LY_TYPE_INT64:
3358 case LY_TYPE_UINT64:
3359 return 1;
3360 case LY_TYPE_UNION:
3361 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003362 LY_ARRAY_FOR(uni->types, u) {
3363 ret = warn_is_numeric_type(uni->types[u]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003364 if (ret) {
3365 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003366 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003367 }
3368 }
3369 /* did not find any suitable type */
3370 return 0;
3371 case LY_TYPE_LEAFREF:
3372 return warn_is_numeric_type(((struct lysc_type_leafref *)type)->realtype);
3373 default:
3374 return 0;
3375 }
3376}
3377
3378/**
3379 * @brief Test whether a type is string-like - no integers, decimal64 or binary.
3380 *
3381 * @param[in] type Type to test.
Radek Krejci857189e2020-09-01 13:26:36 +02003382 * @return Boolean value whether @p type's basetype is string type or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003383 */
Radek Krejci857189e2020-09-01 13:26:36 +02003384static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003385warn_is_string_type(struct lysc_type *type)
3386{
3387 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003388 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003389 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003390
3391 switch (type->basetype) {
3392 case LY_TYPE_BITS:
3393 case LY_TYPE_ENUM:
3394 case LY_TYPE_IDENT:
3395 case LY_TYPE_INST:
3396 case LY_TYPE_STRING:
3397 return 1;
3398 case LY_TYPE_UNION:
3399 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003400 LY_ARRAY_FOR(uni->types, u) {
3401 ret = warn_is_string_type(uni->types[u]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003402 if (ret) {
3403 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003404 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003405 }
3406 }
3407 /* did not find any suitable type */
3408 return 0;
3409 case LY_TYPE_LEAFREF:
3410 return warn_is_string_type(((struct lysc_type_leafref *)type)->realtype);
3411 default:
3412 return 0;
3413 }
3414}
3415
3416/**
3417 * @brief Test whether a type is one specific type.
3418 *
3419 * @param[in] type Type to test.
3420 * @param[in] base Expected type.
Radek Krejci857189e2020-09-01 13:26:36 +02003421 * @return Boolean value whether the given @p type is of the specific basetype @p base.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003422 */
Radek Krejci857189e2020-09-01 13:26:36 +02003423static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003424warn_is_specific_type(struct lysc_type *type, LY_DATA_TYPE base)
3425{
3426 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003427 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003428 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003429
3430 if (type->basetype == base) {
3431 return 1;
3432 } else if (type->basetype == LY_TYPE_UNION) {
3433 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003434 LY_ARRAY_FOR(uni->types, u) {
3435 ret = warn_is_specific_type(uni->types[u], base);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003436 if (ret) {
3437 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003438 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003439 }
3440 }
3441 /* did not find any suitable type */
3442 return 0;
3443 } else if (type->basetype == LY_TYPE_LEAFREF) {
3444 return warn_is_specific_type(((struct lysc_type_leafref *)type)->realtype, base);
3445 }
3446
3447 return 0;
3448}
3449
3450/**
3451 * @brief Get next type of a (union) type.
3452 *
3453 * @param[in] type Base type.
3454 * @param[in] prev_type Previously returned type.
3455 * @return Next type or NULL.
3456 */
3457static struct lysc_type *
3458warn_is_equal_type_next_type(struct lysc_type *type, struct lysc_type *prev_type)
3459{
3460 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003461 ly_bool found = 0;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003462 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003463
Michal Vasko4e55f5a2022-12-14 12:15:00 +01003464 if (type->basetype == LY_TYPE_UNION) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003465 uni = (struct lysc_type_union *)type;
3466 if (!prev_type) {
3467 return uni->types[0];
3468 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003469 LY_ARRAY_FOR(uni->types, u) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003470 if (found) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003471 return uni->types[u];
Michal Vasko03ff5a72019-09-11 13:49:33 +02003472 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003473 if (prev_type == uni->types[u]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003474 found = 1;
3475 }
3476 }
3477 return NULL;
Michal Vasko4e55f5a2022-12-14 12:15:00 +01003478 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003479 if (prev_type) {
3480 assert(type == prev_type);
3481 return NULL;
3482 } else {
3483 return type;
3484 }
3485 }
3486}
3487
3488/**
3489 * @brief Test whether 2 types have a common type.
3490 *
3491 * @param[in] type1 First type.
3492 * @param[in] type2 Second type.
3493 * @return 1 if they do, 0 otherwise.
3494 */
3495static int
3496warn_is_equal_type(struct lysc_type *type1, struct lysc_type *type2)
3497{
3498 struct lysc_type *t1, *rt1, *t2, *rt2;
3499
3500 t1 = NULL;
3501 while ((t1 = warn_is_equal_type_next_type(type1, t1))) {
3502 if (t1->basetype == LY_TYPE_LEAFREF) {
3503 rt1 = ((struct lysc_type_leafref *)t1)->realtype;
3504 } else {
3505 rt1 = t1;
3506 }
3507
3508 t2 = NULL;
3509 while ((t2 = warn_is_equal_type_next_type(type2, t2))) {
3510 if (t2->basetype == LY_TYPE_LEAFREF) {
3511 rt2 = ((struct lysc_type_leafref *)t2)->realtype;
3512 } else {
3513 rt2 = t2;
3514 }
3515
3516 if (rt2->basetype == rt1->basetype) {
3517 /* match found */
3518 return 1;
3519 }
3520 }
3521 }
3522
3523 return 0;
3524}
3525
3526/**
Michal Vaskoaa956522021-11-11 10:45:34 +01003527 * @brief Print warning with information about the XPath subexpression that caused previous warning.
3528 *
3529 * @param[in] ctx Context for logging.
3530 * @param[in] tok_pos Index of the subexpression in the whole expression.
3531 * @param[in] subexpr Subexpression start.
3532 * @param[in] subexpr_len Length of @p subexpr to print.
3533 * @param[in] cur_scnode Expression context node.
3534 */
3535static void
Michal Vaskodd528af2022-08-08 14:35:07 +02003536warn_subexpr_log(const struct ly_ctx *ctx, uint32_t tok_pos, const char *subexpr, int subexpr_len,
Michal Vaskoaa956522021-11-11 10:45:34 +01003537 const struct lysc_node *cur_scnode)
3538{
3539 char *path;
3540
3541 path = lysc_path(cur_scnode, LYSC_PATH_LOG, NULL, 0);
Michal Vaskodd528af2022-08-08 14:35:07 +02003542 LOGWRN(ctx, "Previous warning generated by XPath subexpression[%" PRIu32 "] \"%.*s\" with context node \"%s\".",
Michal Vaskoaa956522021-11-11 10:45:34 +01003543 tok_pos, subexpr_len, subexpr, path);
3544 free(path);
3545}
3546
3547/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02003548 * @brief Check both operands of comparison operators.
3549 *
3550 * @param[in] ctx Context for errors.
3551 * @param[in] set1 First operand set.
3552 * @param[in] set2 Second operand set.
3553 * @param[in] numbers_only Whether accept only numbers or other types are fine too (for '=' and '!=').
3554 * @param[in] expr Start of the expression to print with the warning.
3555 * @param[in] tok_pos Token position.
3556 */
3557static void
Michal Vaskoaa956522021-11-11 10:45:34 +01003558warn_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 +02003559 uint32_t tok_pos)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003560{
3561 struct lysc_node_leaf *node1, *node2;
Radek Krejci857189e2020-09-01 13:26:36 +02003562 ly_bool leaves = 1, warning = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003563
3564 node1 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set1);
3565 node2 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set2);
3566
3567 if (!node1 && !node2) {
3568 /* no node-sets involved, nothing to do */
3569 return;
3570 }
3571
3572 if (node1) {
3573 if (!(node1->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3574 LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node1->nodetype), node1->name);
3575 warning = 1;
3576 leaves = 0;
3577 } else if (numbers_only && !warn_is_numeric_type(node1->type)) {
3578 LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node1->name);
3579 warning = 1;
3580 }
3581 }
3582
3583 if (node2) {
3584 if (!(node2->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3585 LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node2->nodetype), node2->name);
3586 warning = 1;
3587 leaves = 0;
3588 } else if (numbers_only && !warn_is_numeric_type(node2->type)) {
3589 LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node2->name);
3590 warning = 1;
3591 }
3592 }
3593
3594 if (node1 && node2 && leaves && !numbers_only) {
Michal Vasko69730152020-10-09 16:30:07 +02003595 if ((warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type)) ||
3596 (!warn_is_numeric_type(node1->type) && warn_is_numeric_type(node2->type)) ||
3597 (!warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type) &&
3598 !warn_is_equal_type(node1->type, node2->type))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003599 LOGWRN(ctx, "Incompatible types of operands \"%s\" and \"%s\" for comparison.", node1->name, node2->name);
3600 warning = 1;
3601 }
3602 }
3603
3604 if (warning) {
Michal Vaskoaa956522021-11-11 10:45:34 +01003605 warn_subexpr_log(ctx, tok_pos, expr + tok_pos, 20, set1->cur_scnode);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003606 }
3607}
3608
3609/**
3610 * @brief Check that a value is valid for a leaf. If not applicable, does nothing.
3611 *
3612 * @param[in] exp Parsed XPath expression.
3613 * @param[in] set Set with the leaf/leaf-list.
3614 * @param[in] val_exp Index of the value (literal/number) in @p exp.
3615 * @param[in] equal_exp Index of the start of the equality expression in @p exp.
3616 * @param[in] last_equal_exp Index of the end of the equality expression in @p exp.
3617 */
3618static void
Michal Vaskodd528af2022-08-08 14:35:07 +02003619warn_equality_value(const struct lyxp_expr *exp, struct lyxp_set *set, uint32_t val_exp, uint32_t equal_exp,
3620 uint32_t last_equal_exp)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003621{
3622 struct lysc_node *scnode;
3623 struct lysc_type *type;
3624 char *value;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003625 struct lyd_value storage;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003626 LY_ERR rc;
3627 struct ly_err_item *err = NULL;
3628
Michal Vasko69730152020-10-09 16:30:07 +02003629 if ((scnode = warn_get_scnode_in_ctx(set)) && (scnode->nodetype & (LYS_LEAF | LYS_LEAFLIST)) &&
3630 ((exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) || (exp->tokens[val_exp] == LYXP_TOKEN_NUMBER))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003631 /* check that the node can have the specified value */
3632 if (exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) {
3633 value = strndup(exp->expr + exp->tok_pos[val_exp] + 1, exp->tok_len[val_exp] - 2);
3634 } else {
3635 value = strndup(exp->expr + exp->tok_pos[val_exp], exp->tok_len[val_exp]);
3636 }
3637 if (!value) {
3638 LOGMEM(set->ctx);
3639 return;
3640 }
3641
3642 if ((((struct lysc_node_leaf *)scnode)->type->basetype == LY_TYPE_IDENT) && !strchr(value, ':')) {
3643 LOGWRN(set->ctx, "Identityref \"%s\" comparison with identity \"%s\" without prefix, consider adding"
Michal Vasko69730152020-10-09 16:30:07 +02003644 " a prefix or best using \"derived-from(-or-self)()\" functions.", scnode->name, value);
Michal Vaskoaa956522021-11-11 10:45:34 +01003645 warn_subexpr_log(set->ctx, exp->tok_pos[equal_exp], exp->expr + exp->tok_pos[equal_exp],
Radek Krejci0f969882020-08-21 16:56:47 +02003646 (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp],
Michal Vaskoaa956522021-11-11 10:45:34 +01003647 set->cur_scnode);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003648 }
3649
3650 type = ((struct lysc_node_leaf *)scnode)->type;
3651 if (type->basetype != LY_TYPE_IDENT) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003652 rc = type->plugin->store(set->ctx, type, value, strlen(value), 0, set->format, set->prefix_data,
Michal Vasko405cc9e2020-12-01 12:01:27 +01003653 LYD_HINT_DATA, scnode, &storage, NULL, &err);
Michal Vaskobf42e832020-11-23 16:59:42 +01003654 if (rc == LY_EINCOMPLETE) {
3655 rc = LY_SUCCESS;
3656 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003657
3658 if (err) {
3659 LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type (%s).", value, err->msg);
3660 ly_err_free(err);
3661 } else if (rc != LY_SUCCESS) {
3662 LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type.", value);
3663 }
3664 if (rc != LY_SUCCESS) {
Michal Vaskoaa956522021-11-11 10:45:34 +01003665 warn_subexpr_log(set->ctx, exp->tok_pos[equal_exp], exp->expr + exp->tok_pos[equal_exp],
Radek Krejci0f969882020-08-21 16:56:47 +02003666 (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp],
Michal Vaskoaa956522021-11-11 10:45:34 +01003667 set->cur_scnode);
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003668 } else {
3669 type->plugin->free(set->ctx, &storage);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003670 }
3671 }
3672 free(value);
3673 }
3674}
3675
3676/*
3677 * XPath functions
3678 */
3679
3680/**
3681 * @brief Execute the YANG 1.1 bit-is-set(node-set, string) function. Returns LYXP_SET_BOOLEAN
3682 * depending on whether the first node bit value from the second argument is set.
3683 *
3684 * @param[in] args Array of arguments.
3685 * @param[in] arg_count Count of elements in @p args.
3686 * @param[in,out] set Context and result set at the same time.
3687 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003688 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003689 */
3690static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02003691xpath_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 +02003692{
3693 struct lyd_node_term *leaf;
3694 struct lysc_node_leaf *sleaf;
Michal Vasko2588b952021-07-29 07:43:26 +02003695 struct lyd_value_bits *bits;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003696 LY_ERR rc = LY_SUCCESS;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003697 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003698
3699 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003700 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003701 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003702 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3703 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3704 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3705 sleaf->name);
3706 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_BITS)) {
3707 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"bits\".", __func__, sleaf->name);
3708 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003709 }
3710
3711 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3712 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003713 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3714 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003715 } else if (!warn_is_string_type(sleaf->type)) {
3716 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003717 }
3718 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003719 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003720 return rc;
3721 }
3722
Michal Vaskod3678892020-05-21 10:06:58 +02003723 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003724 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 +02003725 return LY_EVALID;
3726 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003727 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003728 LY_CHECK_RET(rc);
3729
3730 set_fill_boolean(set, 0);
Michal Vaskod3678892020-05-21 10:06:58 +02003731 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003732 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
Michal Vasko2588b952021-07-29 07:43:26 +02003733 if ((leaf->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (leaf->value.realtype->basetype == LY_TYPE_BITS)) {
3734 LYD_VALUE_GET(&leaf->value, bits);
3735 LY_ARRAY_FOR(bits->items, u) {
3736 if (!strcmp(bits->items[u]->name, args[1]->val.str)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003737 set_fill_boolean(set, 1);
3738 break;
3739 }
3740 }
3741 }
3742 }
3743
3744 return LY_SUCCESS;
3745}
3746
3747/**
3748 * @brief Execute the XPath boolean(object) function. Returns LYXP_SET_BOOLEAN
3749 * with the argument converted to boolean.
3750 *
3751 * @param[in] args Array of arguments.
3752 * @param[in] arg_count Count of elements in @p args.
3753 * @param[in,out] set Context and result set at the same time.
3754 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003755 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003756 */
3757static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02003758xpath_boolean(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003759{
3760 LY_ERR rc;
3761
3762 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02003763 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003764 return LY_SUCCESS;
3765 }
3766
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003767 rc = lyxp_set_cast(args[0], LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003768 LY_CHECK_RET(rc);
3769 set_fill_set(set, args[0]);
3770
3771 return LY_SUCCESS;
3772}
3773
3774/**
3775 * @brief Execute the XPath ceiling(number) function. Returns LYXP_SET_NUMBER
3776 * with the first argument rounded up to the nearest integer.
3777 *
3778 * @param[in] args Array of arguments.
3779 * @param[in] arg_count Count of elements in @p args.
3780 * @param[in,out] set Context and result set at the same time.
3781 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003782 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003783 */
3784static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02003785xpath_ceiling(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003786{
3787 struct lysc_node_leaf *sleaf;
3788 LY_ERR rc = LY_SUCCESS;
3789
3790 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003791 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003792 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003793 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3794 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3795 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3796 sleaf->name);
3797 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
3798 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
3799 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003800 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003801 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003802 return rc;
3803 }
3804
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003805 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003806 LY_CHECK_RET(rc);
3807 if ((long long)args[0]->val.num != args[0]->val.num) {
3808 set_fill_number(set, ((long long)args[0]->val.num) + 1);
3809 } else {
3810 set_fill_number(set, args[0]->val.num);
3811 }
3812
3813 return LY_SUCCESS;
3814}
3815
3816/**
3817 * @brief Execute the XPath concat(string, string, string*) function.
3818 * Returns LYXP_SET_STRING with the concatenation of all the arguments.
3819 *
3820 * @param[in] args Array of arguments.
3821 * @param[in] arg_count Count of elements in @p args.
3822 * @param[in,out] set Context and result set at the same time.
3823 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003824 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003825 */
3826static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02003827xpath_concat(struct lyxp_set **args, uint32_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003828{
Michal Vaskodd528af2022-08-08 14:35:07 +02003829 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003830 char *str = NULL;
3831 size_t used = 1;
3832 LY_ERR rc = LY_SUCCESS;
3833 struct lysc_node_leaf *sleaf;
3834
3835 if (options & LYXP_SCNODE_ALL) {
3836 for (i = 0; i < arg_count; ++i) {
3837 if ((args[i]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[i]))) {
3838 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3839 LOGWRN(set->ctx, "Argument #%u of %s is a %s node \"%s\".",
Michal Vasko69730152020-10-09 16:30:07 +02003840 i + 1, __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003841 } else if (!warn_is_string_type(sleaf->type)) {
Radek Krejci70124c82020-08-14 22:17:03 +02003842 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 +02003843 }
3844 }
3845 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003846 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003847 return rc;
3848 }
3849
3850 for (i = 0; i < arg_count; ++i) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003851 rc = lyxp_set_cast(args[i], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003852 if (rc != LY_SUCCESS) {
3853 free(str);
3854 return rc;
3855 }
3856
3857 str = ly_realloc(str, (used + strlen(args[i]->val.str)) * sizeof(char));
3858 LY_CHECK_ERR_RET(!str, LOGMEM(set->ctx), LY_EMEM);
3859 strcpy(str + used - 1, args[i]->val.str);
3860 used += strlen(args[i]->val.str);
3861 }
3862
3863 /* free, kind of */
Michal Vaskod3678892020-05-21 10:06:58 +02003864 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003865 set->type = LYXP_SET_STRING;
3866 set->val.str = str;
3867
3868 return LY_SUCCESS;
3869}
3870
3871/**
3872 * @brief Execute the XPath contains(string, string) function.
3873 * Returns LYXP_SET_BOOLEAN whether the second argument can
3874 * be found in the first or not.
3875 *
3876 * @param[in] args Array of arguments.
3877 * @param[in] arg_count Count of elements in @p args.
3878 * @param[in,out] set Context and result set at the same time.
3879 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003880 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003881 */
3882static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02003883xpath_contains(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003884{
3885 struct lysc_node_leaf *sleaf;
3886 LY_ERR rc = LY_SUCCESS;
3887
3888 if (options & LYXP_SCNODE_ALL) {
3889 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3890 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003891 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3892 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003893 } else if (!warn_is_string_type(sleaf->type)) {
3894 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003895 }
3896 }
3897
3898 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3899 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003900 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3901 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003902 } else if (!warn_is_string_type(sleaf->type)) {
3903 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003904 }
3905 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003906 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003907 return rc;
3908 }
3909
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003910 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003911 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003912 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003913 LY_CHECK_RET(rc);
3914
3915 if (strstr(args[0]->val.str, args[1]->val.str)) {
3916 set_fill_boolean(set, 1);
3917 } else {
3918 set_fill_boolean(set, 0);
3919 }
3920
3921 return LY_SUCCESS;
3922}
3923
3924/**
3925 * @brief Execute the XPath count(node-set) function. Returns LYXP_SET_NUMBER
3926 * with the size of the node-set from the argument.
3927 *
3928 * @param[in] args Array of arguments.
3929 * @param[in] arg_count Count of elements in @p args.
3930 * @param[in,out] set Context and result set at the same time.
3931 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003932 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003933 */
3934static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02003935xpath_count(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003936{
Michal Vasko03ff5a72019-09-11 13:49:33 +02003937 LY_ERR rc = LY_SUCCESS;
3938
3939 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003940 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003941 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003942 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003943 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003944 return rc;
3945 }
3946
Michal Vasko03ff5a72019-09-11 13:49:33 +02003947 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003948 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "count(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003949 return LY_EVALID;
3950 }
3951
3952 set_fill_number(set, args[0]->used);
3953 return LY_SUCCESS;
3954}
3955
3956/**
3957 * @brief Execute the XPath current() function. Returns LYXP_SET_NODE_SET
3958 * with the context with the intial node.
3959 *
3960 * @param[in] args Array of arguments.
3961 * @param[in] arg_count Count of elements in @p args.
3962 * @param[in,out] set Context and result set at the same time.
3963 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003964 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003965 */
3966static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02003967xpath_current(struct lyxp_set **args, uint32_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003968{
3969 if (arg_count || args) {
Radek Krejcie87c7dc2021-06-02 21:25:42 +02003970 LOGVAL(set->ctx, LY_VCODE_XP_INARGCOUNT, arg_count, LY_PRI_LENSTR("current()"));
Michal Vasko03ff5a72019-09-11 13:49:33 +02003971 return LY_EVALID;
3972 }
3973
3974 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02003975 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003976
Michal Vasko296dfaf2021-12-13 16:57:42 +01003977 if (set->cur_scnode) {
Michal Vaskoe4a6d012023-05-22 14:34:52 +02003978 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 +01003979 } else {
3980 /* root node */
Michal Vaskoe4a6d012023-05-22 14:34:52 +02003981 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, NULL, set->root_type, LYXP_AXIS_SELF, NULL));
Michal Vasko296dfaf2021-12-13 16:57:42 +01003982 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003983 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02003984 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003985
Michal Vasko296dfaf2021-12-13 16:57:42 +01003986 if (set->cur_node) {
3987 /* position is filled later */
3988 set_insert_node(set, set->cur_node, 0, LYXP_NODE_ELEM, 0);
3989 } else {
3990 /* root node */
3991 set_insert_node(set, NULL, 0, set->root_type, 0);
3992 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003993 }
3994
3995 return LY_SUCCESS;
3996}
3997
3998/**
3999 * @brief Execute the YANG 1.1 deref(node-set) function. Returns LYXP_SET_NODE_SET with either
4000 * leafref or instance-identifier target node(s).
4001 *
4002 * @param[in] args Array of arguments.
4003 * @param[in] arg_count Count of elements in @p args.
4004 * @param[in,out] set Context and result set at the same time.
4005 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004006 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004007 */
4008static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004009xpath_deref(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004010{
4011 struct lyd_node_term *leaf;
Michal Vasko42e497c2020-01-06 08:38:25 +01004012 struct lysc_node_leaf *sleaf = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02004013 struct lysc_type_leafref *lref;
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02004014 const struct lysc_node *target;
Michal Vasko004d3152020-06-11 19:59:22 +02004015 struct ly_path *p;
4016 struct lyd_node *node;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004017 char *errmsg = NULL;
Michal Vasko00cbf532020-06-15 13:58:47 +02004018 uint8_t oper;
Michal Vasko741bb562021-06-24 11:59:50 +02004019 LY_ERR r;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004020
4021 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02004022 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004023 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02004024 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
Michal Vasko423ba3f2021-07-19 13:08:50 +02004025 if (!(sleaf->nodetype & LYD_NODE_TERM)) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02004026 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
4027 sleaf->name);
Michal Vaskoed725d72021-06-23 12:03:45 +02004028 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_LEAFREF) &&
4029 !warn_is_specific_type(sleaf->type, LY_TYPE_INST)) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02004030 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"leafref\" nor \"instance-identifier\".",
4031 __func__, sleaf->name);
4032 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004033 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004034 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko423ba3f2021-07-19 13:08:50 +02004035 if (sleaf && (sleaf->nodetype & LYD_NODE_TERM) && (sleaf->type->basetype == LY_TYPE_LEAFREF)) {
Michal Vasko004d3152020-06-11 19:59:22 +02004036 lref = (struct lysc_type_leafref *)sleaf->type;
Michal Vaskod1e53b92021-01-28 13:11:06 +01004037 oper = (sleaf->flags & LYS_IS_OUTPUT) ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT;
Michal Vasko004d3152020-06-11 19:59:22 +02004038
4039 /* it was already evaluated on schema, it must succeed */
Michal Vasko741bb562021-06-24 11:59:50 +02004040 r = ly_path_compile_leafref(set->ctx, &sleaf->node, NULL, lref->path, oper, LY_PATH_TARGET_MANY,
Michal Vasko24fc4d12021-07-12 14:41:20 +02004041 LY_VALUE_SCHEMA_RESOLVED, lref->prefixes, &p);
Michal Vasko741bb562021-06-24 11:59:50 +02004042 if (!r) {
4043 /* get the target node */
4044 target = p[LY_ARRAY_COUNT(p) - 1].node;
4045 ly_path_free(set->ctx, p);
Michal Vasko004d3152020-06-11 19:59:22 +02004046
Michal Vaskoe4a6d012023-05-22 14:34:52 +02004047 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, target, LYXP_NODE_ELEM, LYXP_AXIS_SELF, NULL));
Michal Vasko741bb562021-06-24 11:59:50 +02004048 } /* else the target was found before but is disabled so it was removed */
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02004049 }
4050
Michal Vasko741bb562021-06-24 11:59:50 +02004051 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004052 }
4053
Michal Vaskod3678892020-05-21 10:06:58 +02004054 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004055 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "deref(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004056 return LY_EVALID;
4057 }
4058
Michal Vaskod3678892020-05-21 10:06:58 +02004059 lyxp_set_free_content(set);
4060 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004061 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
4062 sleaf = (struct lysc_node_leaf *)leaf->schema;
4063 if (sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
4064 if (sleaf->type->basetype == LY_TYPE_LEAFREF) {
4065 /* find leafref target */
Radek Krejci0b013302021-03-29 15:22:32 +02004066 if (lyplg_type_resolve_leafref((struct lysc_type_leafref *)sleaf->type, &leaf->node, &leaf->value, set->tree,
Michal Vasko9e685082021-01-29 14:49:09 +01004067 &node, &errmsg)) {
Michal Vasko1a2b8ee2022-12-05 10:41:55 +01004068 LOGERR(set->ctx, LY_EVALID, "%s", errmsg);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004069 free(errmsg);
Michal Vasko004d3152020-06-11 19:59:22 +02004070 return LY_EVALID;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004071 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004072 } else {
4073 assert(sleaf->type->basetype == LY_TYPE_INST);
Michal Vasko90189962023-02-28 12:10:34 +01004074 if (ly_path_eval(leaf->value.target, set->tree, NULL, &node)) {
Michal Vaskoba99a3e2020-08-18 15:50:05 +02004075 LOGERR(set->ctx, LY_EVALID, "Invalid instance-identifier \"%s\" value - required instance not found.",
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02004076 lyd_get_value(&leaf->node));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004077 return LY_EVALID;
4078 }
4079 }
Michal Vasko004d3152020-06-11 19:59:22 +02004080
4081 /* insert it */
4082 set_insert_node(set, node, 0, LYXP_NODE_ELEM, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004083 }
4084 }
4085
4086 return LY_SUCCESS;
4087}
4088
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004089static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02004090xpath_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 +02004091{
Michal Vaskofe1af042022-07-29 14:58:59 +02004092 uint32_t i, id_len;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004093 LY_ARRAY_COUNT_TYPE u;
4094 struct lyd_node_term *leaf;
4095 struct lysc_node_leaf *sleaf;
4096 struct lyd_meta *meta;
Michal Vasko93923692021-05-07 15:28:02 +02004097 struct lyd_value *val;
4098 const struct lys_module *mod;
4099 const char *id_name;
Michal Vasko93923692021-05-07 15:28:02 +02004100 struct lysc_ident *id;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004101 LY_ERR rc = LY_SUCCESS;
Radek Krejci857189e2020-09-01 13:26:36 +02004102 ly_bool found;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004103
4104 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02004105 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004106 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", func);
Michal Vasko5676f4e2021-04-06 17:14:45 +02004107 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4108 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4109 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype),
4110 sleaf->name);
4111 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_IDENT)) {
4112 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"identityref\".", func, sleaf->name);
4113 }
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004114 }
4115
4116 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4117 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4118 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype),
Michal Vasko69730152020-10-09 16:30:07 +02004119 sleaf->name);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004120 } else if (!warn_is_string_type(sleaf->type)) {
4121 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", func, sleaf->name);
4122 }
4123 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004124 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004125 return rc;
4126 }
4127
4128 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004129 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 +02004130 return LY_EVALID;
4131 }
4132 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
4133 LY_CHECK_RET(rc);
4134
Michal Vasko93923692021-05-07 15:28:02 +02004135 /* parse the identity */
4136 id_name = args[1]->val.str;
4137 id_len = strlen(id_name);
4138 rc = moveto_resolve_model(&id_name, &id_len, set, set->cur_node ? set->cur_node->schema : NULL, &mod);
4139 LY_CHECK_RET(rc);
4140 if (!mod) {
4141 LOGVAL(set->ctx, LYVE_XPATH, "Identity \"%.*s\" without a prefix.", (int)id_len, id_name);
4142 return LY_EVALID;
4143 }
4144
4145 /* find the identity */
4146 found = 0;
4147 LY_ARRAY_FOR(mod->identities, u) {
4148 if (!ly_strncmp(mod->identities[u].name, id_name, id_len)) {
4149 /* we have match */
4150 found = 1;
4151 break;
4152 }
4153 }
4154 if (!found) {
4155 LOGVAL(set->ctx, LYVE_XPATH, "Identity \"%.*s\" not found in module \"%s\".", (int)id_len, id_name, mod->name);
4156 return LY_EVALID;
4157 }
4158 id = &mod->identities[u];
4159
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004160 set_fill_boolean(set, 0);
4161 found = 0;
4162 for (i = 0; i < args[0]->used; ++i) {
4163 if ((args[0]->val.nodes[i].type != LYXP_NODE_ELEM) && (args[0]->val.nodes[i].type != LYXP_NODE_META)) {
4164 continue;
4165 }
4166
4167 if (args[0]->val.nodes[i].type == LYXP_NODE_ELEM) {
4168 leaf = (struct lyd_node_term *)args[0]->val.nodes[i].node;
4169 sleaf = (struct lysc_node_leaf *)leaf->schema;
4170 val = &leaf->value;
4171 if (!(sleaf->nodetype & LYD_NODE_TERM) || (leaf->value.realtype->basetype != LY_TYPE_IDENT)) {
4172 /* uninteresting */
4173 continue;
4174 }
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004175 } else {
4176 meta = args[0]->val.meta[i].meta;
4177 val = &meta->value;
4178 if (val->realtype->basetype != LY_TYPE_IDENT) {
4179 /* uninteresting */
4180 continue;
4181 }
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004182 }
4183
Michal Vasko93923692021-05-07 15:28:02 +02004184 /* check the identity itself */
4185 if (self_match && (id == val->ident)) {
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004186 set_fill_boolean(set, 1);
4187 found = 1;
4188 }
Michal Vasko93923692021-05-07 15:28:02 +02004189 if (!found && !lyplg_type_identity_isderived(id, val->ident)) {
4190 set_fill_boolean(set, 1);
4191 found = 1;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004192 }
4193
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004194 if (found) {
4195 break;
4196 }
4197 }
4198
4199 return LY_SUCCESS;
4200}
4201
Michal Vasko03ff5a72019-09-11 13:49:33 +02004202/**
4203 * @brief Execute the YANG 1.1 derived-from(node-set, string) function. Returns LYXP_SET_BOOLEAN depending
4204 * on whether the first argument nodes contain a node of an identity derived from the second
4205 * argument identity.
4206 *
4207 * @param[in] args Array of arguments.
4208 * @param[in] arg_count Count of elements in @p args.
4209 * @param[in,out] set Context and result set at the same time.
4210 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004211 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004212 */
4213static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004214xpath_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 +02004215{
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004216 return xpath_derived_(args, set, options, 0, __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004217}
4218
4219/**
4220 * @brief Execute the YANG 1.1 derived-from-or-self(node-set, string) function. Returns LYXP_SET_BOOLEAN depending
4221 * on whether the first argument nodes contain a node of an identity that either is or is derived from
4222 * the second argument identity.
4223 *
4224 * @param[in] args Array of arguments.
4225 * @param[in] arg_count Count of elements in @p args.
4226 * @param[in,out] set Context and result set at the same time.
4227 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004228 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004229 */
4230static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004231xpath_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 +02004232{
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004233 return xpath_derived_(args, set, options, 1, __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004234}
4235
4236/**
4237 * @brief Execute the YANG 1.1 enum-value(node-set) function. Returns LYXP_SET_NUMBER
4238 * with the integer value of the first node's enum value, otherwise NaN.
4239 *
4240 * @param[in] args Array of arguments.
4241 * @param[in] arg_count Count of elements in @p args.
4242 * @param[in,out] set Context and result set at the same time.
4243 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004244 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004245 */
4246static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004247xpath_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 +02004248{
4249 struct lyd_node_term *leaf;
4250 struct lysc_node_leaf *sleaf;
4251 LY_ERR rc = LY_SUCCESS;
4252
4253 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02004254 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004255 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02004256 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4257 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4258 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
4259 sleaf->name);
4260 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_ENUM)) {
4261 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"enumeration\".", __func__, sleaf->name);
4262 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004263 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004264 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004265 return rc;
4266 }
4267
Michal Vaskod3678892020-05-21 10:06:58 +02004268 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004269 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "enum-value(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004270 return LY_EVALID;
4271 }
4272
4273 set_fill_number(set, NAN);
Michal Vaskod3678892020-05-21 10:06:58 +02004274 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004275 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
4276 sleaf = (struct lysc_node_leaf *)leaf->schema;
4277 if ((sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (sleaf->type->basetype == LY_TYPE_ENUM)) {
4278 set_fill_number(set, leaf->value.enum_item->value);
4279 }
4280 }
4281
4282 return LY_SUCCESS;
4283}
4284
4285/**
4286 * @brief Execute the XPath false() function. Returns LYXP_SET_BOOLEAN
4287 * with false value.
4288 *
4289 * @param[in] args Array of arguments.
4290 * @param[in] arg_count Count of elements in @p args.
4291 * @param[in,out] set Context and result set at the same time.
4292 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004293 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004294 */
4295static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004296xpath_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 +02004297{
4298 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004299 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004300 return LY_SUCCESS;
4301 }
4302
4303 set_fill_boolean(set, 0);
4304 return LY_SUCCESS;
4305}
4306
4307/**
4308 * @brief Execute the XPath floor(number) function. Returns LYXP_SET_NUMBER
4309 * with the first argument floored (truncated).
4310 *
4311 * @param[in] args Array of arguments.
4312 * @param[in] arg_count Count of elements in @p args.
4313 * @param[in,out] set Context and result set at the same time.
4314 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004315 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004316 */
4317static LY_ERR
Michal Vasko0ff8d632023-04-06 12:31:17 +02004318xpath_floor(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004319{
Michal Vasko0ff8d632023-04-06 12:31:17 +02004320 struct lysc_node_leaf *sleaf;
4321 LY_ERR rc = LY_SUCCESS;
4322
4323 if (options & LYXP_SCNODE_ALL) {
4324 if (args[0]->type != LYXP_SET_SCNODE_SET) {
4325 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
4326 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4327 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4328 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
4329 sleaf->name);
4330 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
4331 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
4332 }
4333 }
4334 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
4335 return rc;
4336 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004337
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004338 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004339 LY_CHECK_RET(rc);
4340 if (isfinite(args[0]->val.num)) {
4341 set_fill_number(set, (long long)args[0]->val.num);
4342 }
4343
4344 return LY_SUCCESS;
4345}
4346
4347/**
4348 * @brief Execute the XPath lang(string) function. Returns LYXP_SET_BOOLEAN
4349 * whether the language of the text matches the one from the argument.
4350 *
4351 * @param[in] args Array of arguments.
4352 * @param[in] arg_count Count of elements in @p args.
4353 * @param[in,out] set Context and result set at the same time.
4354 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004355 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004356 */
4357static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004358xpath_lang(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004359{
4360 const struct lyd_node *node;
4361 struct lysc_node_leaf *sleaf;
Michal Vasko9f96a052020-03-10 09:41:45 +01004362 struct lyd_meta *meta = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004363 const char *val;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004364 LY_ERR rc = LY_SUCCESS;
4365
4366 if (options & LYXP_SCNODE_ALL) {
4367 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4368 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004369 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
4370 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004371 } else if (!warn_is_string_type(sleaf->type)) {
4372 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004373 }
4374 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004375 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004376 return rc;
4377 }
4378
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004379 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004380 LY_CHECK_RET(rc);
4381
Michal Vasko03ff5a72019-09-11 13:49:33 +02004382 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004383 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "lang(string)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004384 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004385 } else if (!set->used) {
4386 set_fill_boolean(set, 0);
4387 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004388 }
4389
4390 switch (set->val.nodes[0].type) {
4391 case LYXP_NODE_ELEM:
4392 case LYXP_NODE_TEXT:
4393 node = set->val.nodes[0].node;
4394 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004395 case LYXP_NODE_META:
4396 node = set->val.meta[0].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004397 break;
4398 default:
4399 /* nothing to do with roots */
4400 set_fill_boolean(set, 0);
4401 return LY_SUCCESS;
4402 }
4403
Michal Vasko9f96a052020-03-10 09:41:45 +01004404 /* find lang metadata */
Michal Vasko9e685082021-01-29 14:49:09 +01004405 for ( ; node; node = lyd_parent(node)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01004406 for (meta = node->meta; meta; meta = meta->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004407 /* annotations */
Michal Vasko9f96a052020-03-10 09:41:45 +01004408 if (meta->name && !strcmp(meta->name, "lang") && !strcmp(meta->annotation->module->name, "xml")) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004409 break;
4410 }
4411 }
4412
Michal Vasko9f96a052020-03-10 09:41:45 +01004413 if (meta) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004414 break;
4415 }
4416 }
4417
4418 /* compare languages */
Michal Vasko9f96a052020-03-10 09:41:45 +01004419 if (!meta) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004420 set_fill_boolean(set, 0);
4421 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004422 uint64_t i;
4423
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02004424 val = lyd_get_meta_value(meta);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004425 for (i = 0; args[0]->val.str[i]; ++i) {
4426 if (tolower(args[0]->val.str[i]) != tolower(val[i])) {
4427 set_fill_boolean(set, 0);
4428 break;
4429 }
4430 }
4431 if (!args[0]->val.str[i]) {
4432 if (!val[i] || (val[i] == '-')) {
4433 set_fill_boolean(set, 1);
4434 } else {
4435 set_fill_boolean(set, 0);
4436 }
4437 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004438 }
4439
4440 return LY_SUCCESS;
4441}
4442
4443/**
4444 * @brief Execute the XPath last() function. Returns LYXP_SET_NUMBER
4445 * with the context size.
4446 *
4447 * @param[in] args Array of arguments.
4448 * @param[in] arg_count Count of elements in @p args.
4449 * @param[in,out] set Context and result set at the same time.
4450 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004451 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004452 */
4453static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004454xpath_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 +02004455{
4456 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004457 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004458 return LY_SUCCESS;
4459 }
4460
Michal Vasko03ff5a72019-09-11 13:49:33 +02004461 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004462 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "last()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004463 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004464 } else if (!set->used) {
4465 set_fill_number(set, 0);
4466 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004467 }
4468
4469 set_fill_number(set, set->ctx_size);
4470 return LY_SUCCESS;
4471}
4472
4473/**
4474 * @brief Execute the XPath local-name(node-set?) function. Returns LYXP_SET_STRING
4475 * with the node name without namespace from the argument or the context.
4476 *
4477 * @param[in] args Array of arguments.
4478 * @param[in] arg_count Count of elements in @p args.
4479 * @param[in,out] set Context and result set at the same time.
4480 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004481 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004482 */
4483static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004484xpath_local_name(struct lyxp_set **args, uint32_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004485{
4486 struct lyxp_set_node *item;
Michal Vasko69730152020-10-09 16:30:07 +02004487
Michal Vasko03ff5a72019-09-11 13:49:33 +02004488 /* suppress unused variable warning */
4489 (void)options;
4490
4491 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004492 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004493 return LY_SUCCESS;
4494 }
4495
4496 if (arg_count) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004497 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004498 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004499 "local-name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004500 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004501 } else if (!args[0]->used) {
4502 set_fill_string(set, "", 0);
4503 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004504 }
4505
4506 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004507 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004508
4509 item = &args[0]->val.nodes[0];
4510 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004511 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004512 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "local-name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004513 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004514 } else if (!set->used) {
4515 set_fill_string(set, "", 0);
4516 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004517 }
4518
4519 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004520 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004521
4522 item = &set->val.nodes[0];
4523 }
4524
4525 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004526 case LYXP_NODE_NONE:
4527 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004528 case LYXP_NODE_ROOT:
4529 case LYXP_NODE_ROOT_CONFIG:
4530 case LYXP_NODE_TEXT:
4531 set_fill_string(set, "", 0);
4532 break;
4533 case LYXP_NODE_ELEM:
Michal Vasko222be682023-08-24 08:17:12 +02004534 set_fill_string(set, LYD_NAME(item->node), strlen(LYD_NAME(item->node)));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004535 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004536 case LYXP_NODE_META:
4537 set_fill_string(set, ((struct lyd_meta *)item->node)->name, strlen(((struct lyd_meta *)item->node)->name));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004538 break;
4539 }
4540
4541 return LY_SUCCESS;
4542}
4543
4544/**
4545 * @brief Execute the XPath name(node-set?) function. Returns LYXP_SET_STRING
4546 * with the node name fully qualified (with namespace) from the argument or the context.
4547 *
4548 * @param[in] args Array of arguments.
4549 * @param[in] arg_count Count of elements in @p args.
4550 * @param[in,out] set Context and result set at the same time.
4551 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004552 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004553 */
4554static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004555xpath_name(struct lyxp_set **args, uint32_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004556{
4557 struct lyxp_set_node *item;
Michal Vasko420cc252023-08-24 08:14:24 +02004558 const struct lys_module *mod = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004559 char *str;
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004560 const char *name = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004561
4562 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004563 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004564 return LY_SUCCESS;
4565 }
4566
4567 if (arg_count) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004568 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004569 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004570 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004571 } else if (!args[0]->used) {
4572 set_fill_string(set, "", 0);
4573 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004574 }
4575
4576 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004577 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004578
4579 item = &args[0]->val.nodes[0];
4580 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004581 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004582 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004583 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004584 } else if (!set->used) {
4585 set_fill_string(set, "", 0);
4586 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004587 }
4588
4589 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004590 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004591
4592 item = &set->val.nodes[0];
4593 }
4594
4595 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004596 case LYXP_NODE_NONE:
4597 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004598 case LYXP_NODE_ROOT:
4599 case LYXP_NODE_ROOT_CONFIG:
4600 case LYXP_NODE_TEXT:
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004601 /* keep NULL */
Michal Vasko03ff5a72019-09-11 13:49:33 +02004602 break;
4603 case LYXP_NODE_ELEM:
Michal Vasko420cc252023-08-24 08:14:24 +02004604 mod = lyd_node_module(item->node);
4605 name = LYD_NAME(item->node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004606 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004607 case LYXP_NODE_META:
4608 mod = ((struct lyd_meta *)item->node)->annotation->module;
4609 name = ((struct lyd_meta *)item->node)->name;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004610 break;
4611 }
4612
4613 if (mod && name) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004614 int rc = asprintf(&str, "%s:%s", ly_get_prefix(mod, set->format, set->prefix_data), name);
Michal Vasko26bbb272022-08-02 14:54:33 +02004615
Michal Vasko03ff5a72019-09-11 13:49:33 +02004616 LY_CHECK_ERR_RET(rc == -1, LOGMEM(set->ctx), LY_EMEM);
4617 set_fill_string(set, str, strlen(str));
4618 free(str);
4619 } else {
4620 set_fill_string(set, "", 0);
4621 }
4622
4623 return LY_SUCCESS;
4624}
4625
4626/**
4627 * @brief Execute the XPath namespace-uri(node-set?) function. Returns LYXP_SET_STRING
4628 * with the namespace of the node from the argument or the context.
4629 *
4630 * @param[in] args Array of arguments.
4631 * @param[in] arg_count Count of elements in @p args.
4632 * @param[in,out] set Context and result set at the same time.
4633 * @param[in] options XPath options.
4634 * @return LY_ERR (LY_EINVAL for wrong arguments on schema)
4635 */
4636static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004637xpath_namespace_uri(struct lyxp_set **args, uint32_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004638{
4639 struct lyxp_set_node *item;
Michal Vasko420cc252023-08-24 08:14:24 +02004640 const struct lys_module *mod;
Michal Vasko69730152020-10-09 16:30:07 +02004641
Michal Vasko03ff5a72019-09-11 13:49:33 +02004642 /* suppress unused variable warning */
4643 (void)options;
4644
4645 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004646 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004647 return LY_SUCCESS;
4648 }
4649
4650 if (arg_count) {
Michal Vaskod3678892020-05-21 10:06:58 +02004651 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004652 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
Michal Vasko69730152020-10-09 16:30:07 +02004653 "namespace-uri(node-set?)");
Michal Vaskod3678892020-05-21 10:06:58 +02004654 return LY_EVALID;
4655 } else if (!args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004656 set_fill_string(set, "", 0);
4657 return LY_SUCCESS;
4658 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004659
4660 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004661 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004662
4663 item = &args[0]->val.nodes[0];
4664 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004665 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004666 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "namespace-uri(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004667 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004668 } else if (!set->used) {
4669 set_fill_string(set, "", 0);
4670 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004671 }
4672
4673 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004674 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004675
4676 item = &set->val.nodes[0];
4677 }
4678
4679 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004680 case LYXP_NODE_NONE:
4681 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004682 case LYXP_NODE_ROOT:
4683 case LYXP_NODE_ROOT_CONFIG:
4684 case LYXP_NODE_TEXT:
4685 set_fill_string(set, "", 0);
4686 break;
4687 case LYXP_NODE_ELEM:
Michal Vasko9f96a052020-03-10 09:41:45 +01004688 case LYXP_NODE_META:
Michal Vasko03ff5a72019-09-11 13:49:33 +02004689 if (item->type == LYXP_NODE_ELEM) {
Michal Vasko420cc252023-08-24 08:14:24 +02004690 mod = lyd_node_module(item->node);
Michal Vasko9f96a052020-03-10 09:41:45 +01004691 } else { /* LYXP_NODE_META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02004692 /* annotations */
Michal Vasko9f96a052020-03-10 09:41:45 +01004693 mod = ((struct lyd_meta *)item->node)->annotation->module;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004694 }
4695
4696 set_fill_string(set, mod->ns, strlen(mod->ns));
4697 break;
4698 }
4699
4700 return LY_SUCCESS;
4701}
4702
4703/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02004704 * @brief Execute the XPath normalize-space(string?) function. Returns LYXP_SET_STRING
4705 * with normalized value (no leading, trailing, double white spaces) of the node
4706 * from the argument or the context.
4707 *
4708 * @param[in] args Array of arguments.
4709 * @param[in] arg_count Count of elements in @p args.
4710 * @param[in,out] set Context and result set at the same time.
4711 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004712 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004713 */
4714static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004715xpath_normalize_space(struct lyxp_set **args, uint32_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004716{
Michal Vaskodd528af2022-08-08 14:35:07 +02004717 uint32_t i, new_used;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004718 char *new;
Radek Krejci857189e2020-09-01 13:26:36 +02004719 ly_bool have_spaces = 0, space_before = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004720 struct lysc_node_leaf *sleaf;
4721 LY_ERR rc = LY_SUCCESS;
4722
4723 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004724 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) &&
4725 (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004726 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004727 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
4728 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004729 } else if (!warn_is_string_type(sleaf->type)) {
4730 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004731 }
4732 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004733 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004734 return rc;
4735 }
4736
4737 if (arg_count) {
4738 set_fill_set(set, args[0]);
4739 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004740 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004741 LY_CHECK_RET(rc);
4742
4743 /* is there any normalization necessary? */
4744 for (i = 0; set->val.str[i]; ++i) {
4745 if (is_xmlws(set->val.str[i])) {
4746 if ((i == 0) || space_before || (!set->val.str[i + 1])) {
4747 have_spaces = 1;
4748 break;
4749 }
4750 space_before = 1;
4751 } else {
4752 space_before = 0;
4753 }
4754 }
4755
4756 /* yep, there is */
4757 if (have_spaces) {
4758 /* it's enough, at least one character will go, makes space for ending '\0' */
4759 new = malloc(strlen(set->val.str) * sizeof(char));
4760 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4761 new_used = 0;
4762
4763 space_before = 0;
4764 for (i = 0; set->val.str[i]; ++i) {
4765 if (is_xmlws(set->val.str[i])) {
4766 if ((i == 0) || space_before) {
4767 space_before = 1;
4768 continue;
4769 } else {
4770 space_before = 1;
4771 }
4772 } else {
4773 space_before = 0;
4774 }
4775
4776 new[new_used] = (space_before ? ' ' : set->val.str[i]);
4777 ++new_used;
4778 }
4779
4780 /* at worst there is one trailing space now */
4781 if (new_used && is_xmlws(new[new_used - 1])) {
4782 --new_used;
4783 }
4784
4785 new = ly_realloc(new, (new_used + 1) * sizeof(char));
4786 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4787 new[new_used] = '\0';
4788
4789 free(set->val.str);
4790 set->val.str = new;
4791 }
4792
4793 return LY_SUCCESS;
4794}
4795
4796/**
4797 * @brief Execute the XPath not(boolean) function. Returns LYXP_SET_BOOLEAN
4798 * with the argument converted to boolean and logically inverted.
4799 *
4800 * @param[in] args Array of arguments.
4801 * @param[in] arg_count Count of elements in @p args.
4802 * @param[in,out] set Context and result set at the same time.
4803 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004804 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004805 */
4806static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004807xpath_not(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004808{
4809 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004810 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004811 return LY_SUCCESS;
4812 }
4813
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004814 lyxp_set_cast(args[0], LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +02004815 if (args[0]->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004816 set_fill_boolean(set, 0);
4817 } else {
4818 set_fill_boolean(set, 1);
4819 }
4820
4821 return LY_SUCCESS;
4822}
4823
4824/**
4825 * @brief Execute the XPath number(object?) function. Returns LYXP_SET_NUMBER
4826 * with the number representation of either the argument or the context.
4827 *
4828 * @param[in] args Array of arguments.
4829 * @param[in] arg_count Count of elements in @p args.
4830 * @param[in,out] set Context and result set at the same time.
4831 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004832 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004833 */
4834static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004835xpath_number(struct lyxp_set **args, uint32_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004836{
4837 LY_ERR rc;
4838
4839 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004840 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004841 return LY_SUCCESS;
4842 }
4843
4844 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004845 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004846 LY_CHECK_RET(rc);
4847 set_fill_set(set, args[0]);
4848 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004849 rc = lyxp_set_cast(set, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004850 LY_CHECK_RET(rc);
4851 }
4852
4853 return LY_SUCCESS;
4854}
4855
4856/**
4857 * @brief Execute the XPath position() function. Returns LYXP_SET_NUMBER
4858 * with the context position.
4859 *
4860 * @param[in] args Array of arguments.
4861 * @param[in] arg_count Count of elements in @p args.
4862 * @param[in,out] set Context and result set at the same time.
4863 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004864 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004865 */
4866static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004867xpath_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 +02004868{
4869 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004870 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004871 return LY_SUCCESS;
4872 }
4873
Michal Vasko03ff5a72019-09-11 13:49:33 +02004874 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004875 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "position()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004876 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004877 } else if (!set->used) {
4878 set_fill_number(set, 0);
4879 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004880 }
4881
4882 set_fill_number(set, set->ctx_pos);
4883
4884 /* UNUSED in 'Release' build type */
4885 (void)options;
4886 return LY_SUCCESS;
4887}
4888
4889/**
4890 * @brief Execute the YANG 1.1 re-match(string, string) function. Returns LYXP_SET_BOOLEAN
4891 * depending on whether the second argument regex matches the first argument string. For details refer to
4892 * YANG 1.1 RFC section 10.2.1.
4893 *
4894 * @param[in] args Array of arguments.
4895 * @param[in] arg_count Count of elements in @p args.
4896 * @param[in,out] set Context and result set at the same time.
4897 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004898 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004899 */
4900static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004901xpath_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 +02004902{
4903 struct lysc_pattern **patterns = NULL, **pattern;
4904 struct lysc_node_leaf *sleaf;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004905 LY_ERR rc = LY_SUCCESS;
4906 struct ly_err_item *err;
4907
4908 if (options & LYXP_SCNODE_ALL) {
4909 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4910 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4911 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 +02004912 } else if (!warn_is_string_type(sleaf->type)) {
4913 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004914 }
4915 }
4916
4917 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4918 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4919 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 +02004920 } else if (!warn_is_string_type(sleaf->type)) {
4921 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004922 }
4923 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004924 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004925 return rc;
4926 }
4927
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004928 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004929 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004930 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004931 LY_CHECK_RET(rc);
4932
4933 LY_ARRAY_NEW_RET(set->ctx, patterns, pattern, LY_EMEM);
Radek Iša45802b52021-02-09 09:21:58 +01004934 *pattern = calloc(1, sizeof **pattern);
Radek Krejciddace2c2021-01-08 11:30:56 +01004935 LOG_LOCSET(NULL, set->cur_node, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01004936 rc = lys_compile_type_pattern_check(set->ctx, args[1]->val.str, &(*pattern)->code);
Michal Vasko4a7d4d62021-12-13 17:05:06 +01004937 if (set->cur_node) {
4938 LOG_LOCBACK(0, 1, 0, 0);
4939 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004940 if (rc != LY_SUCCESS) {
4941 LY_ARRAY_FREE(patterns);
4942 return rc;
4943 }
4944
Radek Krejci0b013302021-03-29 15:22:32 +02004945 rc = lyplg_type_validate_patterns(patterns, args[0]->val.str, strlen(args[0]->val.str), &err);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004946 pcre2_code_free((*pattern)->code);
4947 free(*pattern);
4948 LY_ARRAY_FREE(patterns);
4949 if (rc && (rc != LY_EVALID)) {
Michal Vasko177d0ed2020-11-23 16:43:03 +01004950 ly_err_print(set->ctx, err);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004951 ly_err_free(err);
4952 return rc;
4953 }
4954
4955 if (rc == LY_EVALID) {
4956 ly_err_free(err);
4957 set_fill_boolean(set, 0);
4958 } else {
4959 set_fill_boolean(set, 1);
4960 }
4961
4962 return LY_SUCCESS;
4963}
4964
4965/**
4966 * @brief Execute the XPath round(number) function. Returns LYXP_SET_NUMBER
4967 * with the rounded first argument. For details refer to
4968 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-round.
4969 *
4970 * @param[in] args Array of arguments.
4971 * @param[in] arg_count Count of elements in @p args.
4972 * @param[in,out] set Context and result set at the same time.
4973 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004974 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004975 */
4976static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004977xpath_round(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004978{
4979 struct lysc_node_leaf *sleaf;
4980 LY_ERR rc = LY_SUCCESS;
4981
4982 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02004983 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004984 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02004985 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4986 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4987 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
4988 sleaf->name);
4989 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
4990 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
4991 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004992 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004993 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004994 return rc;
4995 }
4996
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004997 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004998 LY_CHECK_RET(rc);
4999
5000 /* cover only the cases where floor can't be used */
5001 if ((args[0]->val.num == -0.0f) || ((args[0]->val.num < 0) && (args[0]->val.num >= -0.5))) {
5002 set_fill_number(set, -0.0f);
5003 } else {
5004 args[0]->val.num += 0.5;
5005 rc = xpath_floor(args, 1, args[0], options);
5006 LY_CHECK_RET(rc);
5007 set_fill_number(set, args[0]->val.num);
5008 }
5009
5010 return LY_SUCCESS;
5011}
5012
5013/**
5014 * @brief Execute the XPath starts-with(string, string) function.
5015 * Returns LYXP_SET_BOOLEAN whether the second argument is
5016 * the prefix of the first or not.
5017 *
5018 * @param[in] args Array of arguments.
5019 * @param[in] arg_count Count of elements in @p args.
5020 * @param[in,out] set Context and result set at the same time.
5021 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005022 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005023 */
5024static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02005025xpath_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 +02005026{
5027 struct lysc_node_leaf *sleaf;
5028 LY_ERR rc = LY_SUCCESS;
5029
5030 if (options & LYXP_SCNODE_ALL) {
5031 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5032 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5033 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 +02005034 } else if (!warn_is_string_type(sleaf->type)) {
5035 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005036 }
5037 }
5038
5039 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5040 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5041 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 +02005042 } else if (!warn_is_string_type(sleaf->type)) {
5043 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005044 }
5045 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005046 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005047 return rc;
5048 }
5049
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005050 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005051 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005052 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005053 LY_CHECK_RET(rc);
5054
5055 if (strncmp(args[0]->val.str, args[1]->val.str, strlen(args[1]->val.str))) {
5056 set_fill_boolean(set, 0);
5057 } else {
5058 set_fill_boolean(set, 1);
5059 }
5060
5061 return LY_SUCCESS;
5062}
5063
5064/**
5065 * @brief Execute the XPath string(object?) function. Returns LYXP_SET_STRING
5066 * with the string representation of either the argument or the context.
5067 *
5068 * @param[in] args Array of arguments.
5069 * @param[in] arg_count Count of elements in @p args.
5070 * @param[in,out] set Context and result set at the same time.
5071 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005072 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005073 */
5074static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02005075xpath_string(struct lyxp_set **args, uint32_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005076{
5077 LY_ERR rc;
5078
5079 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02005080 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005081 return LY_SUCCESS;
5082 }
5083
5084 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005085 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005086 LY_CHECK_RET(rc);
5087 set_fill_set(set, args[0]);
5088 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005089 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005090 LY_CHECK_RET(rc);
5091 }
5092
5093 return LY_SUCCESS;
5094}
5095
5096/**
5097 * @brief Execute the XPath string-length(string?) function. Returns LYXP_SET_NUMBER
5098 * with the length of the string in either the argument or the context.
5099 *
5100 * @param[in] args Array of arguments.
5101 * @param[in] arg_count Count of elements in @p args.
5102 * @param[in,out] set Context and result set at the same time.
5103 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005104 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005105 */
5106static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02005107xpath_string_length(struct lyxp_set **args, uint32_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005108{
5109 struct lysc_node_leaf *sleaf;
5110 LY_ERR rc = LY_SUCCESS;
5111
5112 if (options & LYXP_SCNODE_ALL) {
5113 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5114 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5115 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 +02005116 } else if (!warn_is_string_type(sleaf->type)) {
5117 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005118 }
5119 }
5120 if (!arg_count && (set->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set))) {
5121 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5122 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 +02005123 } else if (!warn_is_string_type(sleaf->type)) {
5124 LOGWRN(set->ctx, "Argument #0 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005125 }
5126 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005127 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005128 return rc;
5129 }
5130
5131 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005132 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005133 LY_CHECK_RET(rc);
5134 set_fill_number(set, strlen(args[0]->val.str));
5135 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005136 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005137 LY_CHECK_RET(rc);
5138 set_fill_number(set, strlen(set->val.str));
5139 }
5140
5141 return LY_SUCCESS;
5142}
5143
5144/**
5145 * @brief Execute the XPath substring(string, number, number?) function.
5146 * Returns LYXP_SET_STRING substring of the first argument starting
5147 * on the second argument index ending on the third argument index,
5148 * indexed from 1. For exact definition refer to
5149 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-substring.
5150 *
5151 * @param[in] args Array of arguments.
5152 * @param[in] arg_count Count of elements in @p args.
5153 * @param[in,out] set Context and result set at the same time.
5154 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005155 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005156 */
5157static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02005158xpath_substring(struct lyxp_set **args, uint32_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005159{
Michal Vasko6db996e2022-07-28 10:28:04 +02005160 int64_t start;
5161 int32_t len;
Michal Vaskodd528af2022-08-08 14:35:07 +02005162 uint32_t str_start, str_len, pos;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005163 struct lysc_node_leaf *sleaf;
5164 LY_ERR rc = LY_SUCCESS;
5165
5166 if (options & LYXP_SCNODE_ALL) {
5167 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5168 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5169 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 +02005170 } else if (!warn_is_string_type(sleaf->type)) {
5171 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005172 }
5173 }
5174
5175 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5176 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5177 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 +02005178 } else if (!warn_is_numeric_type(sleaf->type)) {
5179 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005180 }
5181 }
5182
Michal Vasko69730152020-10-09 16:30:07 +02005183 if ((arg_count == 3) && (args[2]->type == LYXP_SET_SCNODE_SET) &&
5184 (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005185 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5186 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 +02005187 } else if (!warn_is_numeric_type(sleaf->type)) {
5188 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005189 }
5190 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005191 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005192 return rc;
5193 }
5194
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005195 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005196 LY_CHECK_RET(rc);
5197
5198 /* start */
5199 if (xpath_round(&args[1], 1, args[1], options)) {
5200 return -1;
5201 }
5202 if (isfinite(args[1]->val.num)) {
5203 start = args[1]->val.num - 1;
5204 } else if (isinf(args[1]->val.num) && signbit(args[1]->val.num)) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02005205 start = INT32_MIN;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005206 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02005207 start = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005208 }
5209
5210 /* len */
5211 if (arg_count == 3) {
5212 rc = xpath_round(&args[2], 1, args[2], options);
5213 LY_CHECK_RET(rc);
Radek Krejci1deb5be2020-08-26 16:43:36 +02005214 if (isnan(args[2]->val.num) || signbit(args[2]->val.num)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005215 len = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005216 } else if (isfinite(args[2]->val.num)) {
5217 len = args[2]->val.num;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005218 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02005219 len = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005220 }
5221 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02005222 len = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005223 }
5224
5225 /* find matching character positions */
5226 str_start = 0;
5227 str_len = 0;
5228 for (pos = 0; args[0]->val.str[pos]; ++pos) {
5229 if (pos < start) {
5230 ++str_start;
5231 } else if (pos < start + len) {
5232 ++str_len;
5233 } else {
5234 break;
5235 }
5236 }
5237
5238 set_fill_string(set, args[0]->val.str + str_start, str_len);
5239 return LY_SUCCESS;
5240}
5241
5242/**
5243 * @brief Execute the XPath substring-after(string, string) function.
5244 * Returns LYXP_SET_STRING with the string succeeding the occurance
5245 * of the second argument in the first or an empty string.
5246 *
5247 * @param[in] args Array of arguments.
5248 * @param[in] arg_count Count of elements in @p args.
5249 * @param[in,out] set Context and result set at the same time.
5250 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005251 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005252 */
5253static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02005254xpath_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 +02005255{
5256 char *ptr;
5257 struct lysc_node_leaf *sleaf;
5258 LY_ERR rc = LY_SUCCESS;
5259
5260 if (options & LYXP_SCNODE_ALL) {
5261 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5262 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5263 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 +02005264 } else if (!warn_is_string_type(sleaf->type)) {
5265 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005266 }
5267 }
5268
5269 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5270 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5271 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 +02005272 } else if (!warn_is_string_type(sleaf->type)) {
5273 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005274 }
5275 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005276 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005277 return rc;
5278 }
5279
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005280 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005281 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005282 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005283 LY_CHECK_RET(rc);
5284
5285 ptr = strstr(args[0]->val.str, args[1]->val.str);
5286 if (ptr) {
5287 set_fill_string(set, ptr + strlen(args[1]->val.str), strlen(ptr + strlen(args[1]->val.str)));
5288 } else {
5289 set_fill_string(set, "", 0);
5290 }
5291
5292 return LY_SUCCESS;
5293}
5294
5295/**
5296 * @brief Execute the XPath substring-before(string, string) function.
5297 * Returns LYXP_SET_STRING with the string preceding the occurance
5298 * of the second argument in the first or an empty string.
5299 *
5300 * @param[in] args Array of arguments.
5301 * @param[in] arg_count Count of elements in @p args.
5302 * @param[in,out] set Context and result set at the same time.
5303 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005304 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005305 */
5306static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02005307xpath_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 +02005308{
5309 char *ptr;
5310 struct lysc_node_leaf *sleaf;
5311 LY_ERR rc = LY_SUCCESS;
5312
5313 if (options & LYXP_SCNODE_ALL) {
5314 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5315 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5316 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 +02005317 } else if (!warn_is_string_type(sleaf->type)) {
5318 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005319 }
5320 }
5321
5322 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5323 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5324 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 +02005325 } else if (!warn_is_string_type(sleaf->type)) {
5326 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005327 }
5328 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005329 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005330 return rc;
5331 }
5332
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005333 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005334 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005335 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005336 LY_CHECK_RET(rc);
5337
5338 ptr = strstr(args[0]->val.str, args[1]->val.str);
5339 if (ptr) {
5340 set_fill_string(set, args[0]->val.str, ptr - args[0]->val.str);
5341 } else {
5342 set_fill_string(set, "", 0);
5343 }
5344
5345 return LY_SUCCESS;
5346}
5347
5348/**
5349 * @brief Execute the XPath sum(node-set) function. Returns LYXP_SET_NUMBER
5350 * with the sum of all the nodes in the context.
5351 *
5352 * @param[in] args Array of arguments.
5353 * @param[in] arg_count Count of elements in @p args.
5354 * @param[in,out] set Context and result set at the same time.
5355 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005356 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005357 */
5358static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02005359xpath_sum(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005360{
5361 long double num;
5362 char *str;
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01005363 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005364 struct lyxp_set set_item;
5365 struct lysc_node_leaf *sleaf;
5366 LY_ERR rc = LY_SUCCESS;
5367
5368 if (options & LYXP_SCNODE_ALL) {
5369 if (args[0]->type == LYXP_SET_SCNODE_SET) {
5370 for (i = 0; i < args[0]->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005371 if (args[0]->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005372 sleaf = (struct lysc_node_leaf *)args[0]->val.scnodes[i].scnode;
5373 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5374 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__,
Michal Vasko69730152020-10-09 16:30:07 +02005375 lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005376 } else if (!warn_is_numeric_type(sleaf->type)) {
5377 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005378 }
5379 }
5380 }
5381 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005382 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005383 return rc;
5384 }
5385
5386 set_fill_number(set, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005387
5388 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005389 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "sum(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02005390 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02005391 } else if (!args[0]->used) {
5392 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005393 }
5394
Michal Vasko5c4e5892019-11-14 12:31:38 +01005395 set_init(&set_item, set);
5396
Michal Vasko03ff5a72019-09-11 13:49:33 +02005397 set_item.type = LYXP_SET_NODE_SET;
Michal Vasko41decbf2021-11-02 11:50:21 +01005398 set_item.val.nodes = calloc(1, sizeof *set_item.val.nodes);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005399 LY_CHECK_ERR_RET(!set_item.val.nodes, LOGMEM(set->ctx), LY_EMEM);
5400
5401 set_item.used = 1;
5402 set_item.size = 1;
5403
5404 for (i = 0; i < args[0]->used; ++i) {
5405 set_item.val.nodes[0] = args[0]->val.nodes[i];
5406
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005407 rc = cast_node_set_to_string(&set_item, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005408 LY_CHECK_RET(rc);
5409 num = cast_string_to_number(str);
5410 free(str);
5411 set->val.num += num;
5412 }
5413
5414 free(set_item.val.nodes);
5415
5416 return LY_SUCCESS;
5417}
5418
5419/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02005420 * @brief Execute the XPath translate(string, string, string) function.
5421 * Returns LYXP_SET_STRING with the first argument with the characters
5422 * from the second argument replaced by those on the corresponding
5423 * positions in the third argument.
5424 *
5425 * @param[in] args Array of arguments.
5426 * @param[in] arg_count Count of elements in @p args.
5427 * @param[in,out] set Context and result set at the same time.
5428 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005429 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005430 */
5431static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02005432xpath_translate(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005433{
Michal Vaskodd528af2022-08-08 14:35:07 +02005434 uint32_t i, j, new_used;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005435 char *new;
Radek Krejci857189e2020-09-01 13:26:36 +02005436 ly_bool have_removed;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005437 struct lysc_node_leaf *sleaf;
5438 LY_ERR rc = LY_SUCCESS;
5439
5440 if (options & LYXP_SCNODE_ALL) {
5441 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5442 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5443 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 +02005444 } else if (!warn_is_string_type(sleaf->type)) {
5445 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005446 }
5447 }
5448
5449 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5450 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5451 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 +02005452 } else if (!warn_is_string_type(sleaf->type)) {
5453 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005454 }
5455 }
5456
5457 if ((args[2]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
5458 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5459 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 +02005460 } else if (!warn_is_string_type(sleaf->type)) {
5461 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005462 }
5463 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005464 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005465 return rc;
5466 }
5467
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005468 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005469 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005470 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005471 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005472 rc = lyxp_set_cast(args[2], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005473 LY_CHECK_RET(rc);
5474
5475 new = malloc((strlen(args[0]->val.str) + 1) * sizeof(char));
5476 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5477 new_used = 0;
5478
5479 have_removed = 0;
5480 for (i = 0; args[0]->val.str[i]; ++i) {
Radek Krejci857189e2020-09-01 13:26:36 +02005481 ly_bool found = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005482
5483 for (j = 0; args[1]->val.str[j]; ++j) {
5484 if (args[0]->val.str[i] == args[1]->val.str[j]) {
5485 /* removing this char */
5486 if (j >= strlen(args[2]->val.str)) {
5487 have_removed = 1;
5488 found = 1;
5489 break;
5490 }
5491 /* replacing this char */
5492 new[new_used] = args[2]->val.str[j];
5493 ++new_used;
5494 found = 1;
5495 break;
5496 }
5497 }
5498
5499 /* copying this char */
5500 if (!found) {
5501 new[new_used] = args[0]->val.str[i];
5502 ++new_used;
5503 }
5504 }
5505
5506 if (have_removed) {
5507 new = ly_realloc(new, (new_used + 1) * sizeof(char));
5508 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5509 }
5510 new[new_used] = '\0';
5511
Michal Vaskod3678892020-05-21 10:06:58 +02005512 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005513 set->type = LYXP_SET_STRING;
5514 set->val.str = new;
5515
5516 return LY_SUCCESS;
5517}
5518
5519/**
5520 * @brief Execute the XPath true() function. Returns LYXP_SET_BOOLEAN
5521 * with true value.
5522 *
5523 * @param[in] args Array of arguments.
5524 * @param[in] arg_count Count of elements in @p args.
5525 * @param[in,out] set Context and result set at the same time.
5526 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005527 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005528 */
5529static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02005530xpath_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 +02005531{
5532 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02005533 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005534 return LY_SUCCESS;
5535 }
5536
5537 set_fill_boolean(set, 1);
5538 return LY_SUCCESS;
5539}
5540
Michal Vasko03ff5a72019-09-11 13:49:33 +02005541/**
Michal Vasko49fec8e2022-05-24 10:28:33 +02005542 * @brief Execute the XPath node() processing instruction (node type). Returns LYXP_SET_NODE_SET
5543 * with only nodes from the context.
5544 *
5545 * @param[in,out] set Context and result set at the same time.
5546 * @param[in] axis Axis to search on.
5547 * @param[in] options XPath options.
5548 * @return LY_ERR
5549 */
5550static LY_ERR
5551xpath_pi_node(struct lyxp_set *set, enum lyxp_axis axis, uint32_t options)
5552{
5553 if (options & LYXP_SCNODE_ALL) {
5554 return moveto_scnode(set, NULL, NULL, axis, options);
5555 }
5556
5557 if (set->type != LYXP_SET_NODE_SET) {
5558 lyxp_set_free_content(set);
5559 return LY_SUCCESS;
5560 }
5561
5562 /* just like moving to a node with no restrictions */
5563 return moveto_node(set, NULL, NULL, axis, options);
5564}
5565
5566/**
5567 * @brief Execute the XPath text() processing instruction (node type). Returns LYXP_SET_NODE_SET
5568 * with the text content of the nodes in the context.
5569 *
5570 * @param[in,out] set Context and result set at the same time.
5571 * @param[in] axis Axis to search on.
5572 * @param[in] options XPath options.
5573 * @return LY_ERR
5574 */
5575static LY_ERR
5576xpath_pi_text(struct lyxp_set *set, enum lyxp_axis axis, uint32_t options)
5577{
5578 uint32_t i;
5579
5580 if (options & LYXP_SCNODE_ALL) {
5581 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
5582 return LY_SUCCESS;
5583 }
5584
5585 if (set->type != LYXP_SET_NODE_SET) {
5586 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "text()");
5587 return LY_EVALID;
5588 }
5589
5590 if (axis != LYXP_AXIS_CHILD) {
5591 /* even following and preceding axescan return text nodes, but whatever */
5592 lyxp_set_free_content(set);
5593 return LY_SUCCESS;
5594 }
5595
5596 for (i = 0; i < set->used; ++i) {
5597 switch (set->val.nodes[i].type) {
5598 case LYXP_NODE_NONE:
5599 LOGINT_RET(set->ctx);
5600 case LYXP_NODE_ELEM:
Michal Vasko222be682023-08-24 08:17:12 +02005601 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 +02005602 set->val.nodes[i].type = LYXP_NODE_TEXT;
5603 break;
5604 }
5605 /* fall through */
5606 case LYXP_NODE_ROOT:
5607 case LYXP_NODE_ROOT_CONFIG:
5608 case LYXP_NODE_TEXT:
5609 case LYXP_NODE_META:
5610 set_remove_node_none(set, i);
5611 break;
5612 }
5613 }
5614 set_remove_nodes_none(set);
5615
5616 return LY_SUCCESS;
5617}
5618
5619/**
Michal Vasko6346ece2019-09-24 13:12:53 +02005620 * @brief Skip prefix and return corresponding model if there is a prefix. Logs directly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005621 *
Michal Vasko2104e9f2020-03-06 08:23:25 +01005622 * XPath @p set is expected to be a (sc)node set!
5623 *
Michal Vasko6346ece2019-09-24 13:12:53 +02005624 * @param[in,out] qname Qualified node name. If includes prefix, it is skipped.
5625 * @param[in,out] qname_len Length of @p qname, is updated accordingly.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005626 * @param[in] set Set with general XPath context.
5627 * @param[in] ctx_scnode Context node to inherit module for unprefixed node for ::LY_PREF_JSON.
Michal Vasko6346ece2019-09-24 13:12:53 +02005628 * @param[out] moveto_mod Expected module of a matching node.
5629 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005630 */
Michal Vasko6346ece2019-09-24 13:12:53 +02005631static LY_ERR
Michal Vaskofe1af042022-07-29 14:58:59 +02005632moveto_resolve_model(const char **qname, uint32_t *qname_len, const struct lyxp_set *set,
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005633 const struct lysc_node *ctx_scnode, const struct lys_module **moveto_mod)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005634{
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02005635 const struct lys_module *mod = NULL;
Michal Vasko6346ece2019-09-24 13:12:53 +02005636 const char *ptr;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005637 size_t pref_len;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005638
Michal Vasko2104e9f2020-03-06 08:23:25 +01005639 assert((set->type == LYXP_SET_NODE_SET) || (set->type == LYXP_SET_SCNODE_SET));
5640
Michal Vasko6346ece2019-09-24 13:12:53 +02005641 if ((ptr = ly_strnchr(*qname, ':', *qname_len))) {
5642 /* specific module */
5643 pref_len = ptr - *qname;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005644 mod = ly_resolve_prefix(set->ctx, *qname, pref_len, set->format, set->prefix_data);
Michal Vasko6346ece2019-09-24 13:12:53 +02005645
Michal Vasko004d3152020-06-11 19:59:22 +02005646 /* check for errors and non-implemented modules, as they are not valid */
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005647 if (!mod || !mod->implemented) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02005648 LOGVAL(set->ctx, LY_VCODE_XP_INMOD, (int)pref_len, *qname);
Michal Vasko6346ece2019-09-24 13:12:53 +02005649 return LY_EVALID;
5650 }
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005651
Michal Vasko6346ece2019-09-24 13:12:53 +02005652 *qname += pref_len + 1;
5653 *qname_len -= pref_len + 1;
5654 } else if (((*qname)[0] == '*') && (*qname_len == 1)) {
5655 /* all modules - special case */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005656 mod = NULL;
Michal Vasko6346ece2019-09-24 13:12:53 +02005657 } else {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005658 switch (set->format) {
Radek Krejci8df109d2021-04-23 12:19:08 +02005659 case LY_VALUE_SCHEMA:
5660 case LY_VALUE_SCHEMA_RESOLVED:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005661 /* current module */
5662 mod = set->cur_mod;
5663 break;
Radek Krejci224d4b42021-04-23 13:54:59 +02005664 case LY_VALUE_CANON:
Radek Krejci8df109d2021-04-23 12:19:08 +02005665 case LY_VALUE_JSON:
Radek Krejcif9943642021-04-26 10:18:21 +02005666 case LY_VALUE_LYB:
Michal Vaskoddd76592022-01-17 13:34:48 +01005667 case LY_VALUE_STR_NS:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005668 /* inherit parent (context node) module */
5669 if (ctx_scnode) {
5670 mod = ctx_scnode->module;
5671 } else {
5672 mod = NULL;
5673 }
5674 break;
Radek Krejci8df109d2021-04-23 12:19:08 +02005675 case LY_VALUE_XML:
Michal Vasko52143d12021-04-14 15:36:39 +02005676 /* all nodes need to be prefixed */
5677 LOGVAL(set->ctx, LYVE_DATA, "Non-prefixed node \"%.*s\" in XML xpath found.", *qname_len, *qname);
5678 return LY_EVALID;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005679 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005680 }
5681
Michal Vasko6346ece2019-09-24 13:12:53 +02005682 *moveto_mod = mod;
5683 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005684}
5685
5686/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02005687 * @brief Move context @p set to the root. Handles absolute path.
5688 * Result is LYXP_SET_NODE_SET.
5689 *
5690 * @param[in,out] set Set to use.
5691 * @param[in] options Xpath options.
Michal Vaskob0099a92020-08-31 14:55:23 +02005692 * @return LY_ERR value.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005693 */
Michal Vaskob0099a92020-08-31 14:55:23 +02005694static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005695moveto_root(struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005696{
aPiecek8b0cc152021-05-31 16:40:31 +02005697 assert(!(options & LYXP_SKIP_EXPR));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005698
5699 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02005700 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskoe4a6d012023-05-22 14:34:52 +02005701 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, NULL, set->root_type, LYXP_AXIS_SELF, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005702 } else {
Michal Vaskoef71b392023-08-16 09:45:23 +02005703 lyxp_set_free_content(set);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005704 set_insert_node(set, NULL, 0, set->root_type, 0);
Michal Vasko306e2832022-07-25 09:15:17 +02005705 set->non_child_axis = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005706 }
Michal Vaskob0099a92020-08-31 14:55:23 +02005707
5708 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005709}
5710
5711/**
5712 * @brief Check @p node as a part of NameTest processing.
5713 *
5714 * @param[in] node Node to check.
Michal Vasko49fec8e2022-05-24 10:28:33 +02005715 * @param[in] node_type Node type of @p node.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005716 * @param[in] set Set to read general context from.
Michal Vaskod3678892020-05-21 10:06:58 +02005717 * @param[in] node_name Node name in the dictionary to move to, NULL for any node.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005718 * @param[in] moveto_mod Expected module of the node, NULL for no prefix.
Michal Vaskocdad7122020-11-09 21:04:44 +01005719 * @param[in] options XPath options.
Michal Vasko6346ece2019-09-24 13:12:53 +02005720 * @return LY_ERR (LY_ENOT if node does not match, LY_EINCOMPLETE on unresolved when,
Michal Vaskocd2c88a2022-06-07 10:54:34 +02005721 * LY_EINVAL if neither node nor any children match)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005722 */
5723static LY_ERR
Michal Vasko49fec8e2022-05-24 10:28:33 +02005724moveto_node_check(const struct lyd_node *node, enum lyxp_node_type node_type, const struct lyxp_set *set,
5725 const char *node_name, const struct lys_module *moveto_mod, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005726{
Michal Vaskofbd0da42023-08-17 14:57:44 +02005727 const struct lysc_node *schema;
5728
Michal Vasko49fec8e2022-05-24 10:28:33 +02005729 if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT)) {
5730 assert(node_type == set->root_type);
5731
5732 if (node_name || moveto_mod) {
5733 /* root will not match a specific node */
5734 return LY_ENOT;
5735 }
5736 return LY_SUCCESS;
5737 } else if (node_type != LYXP_NODE_ELEM) {
5738 /* other types will not match */
5739 return LY_ENOT;
5740 }
5741
Michal Vaskofbd0da42023-08-17 14:57:44 +02005742 /* get schema node even of an opaque node */
5743 schema = lyd_node_schema(node);
5744 if (!schema) {
5745 /* unknown opaque node never matches */
Michal Vaskodca9f122021-07-16 13:56:22 +02005746 return LY_ENOT;
5747 }
5748
Michal Vasko03ff5a72019-09-11 13:49:33 +02005749 /* module check */
Michal Vasko19089f02022-06-07 11:02:11 +02005750 if (moveto_mod) {
Michal Vaskofbd0da42023-08-17 14:57:44 +02005751 if ((set->ctx == LYD_CTX(node)) && (schema->module != moveto_mod)) {
Michal Vasko19089f02022-06-07 11:02:11 +02005752 return LY_ENOT;
Michal Vaskofbd0da42023-08-17 14:57:44 +02005753 } else if ((set->ctx != LYD_CTX(node)) && strcmp(schema->module->name, moveto_mod->name)) {
Michal Vasko19089f02022-06-07 11:02:11 +02005754 return LY_ENOT;
5755 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005756 }
5757
Michal Vasko5c4e5892019-11-14 12:31:38 +01005758 /* context check */
Michal Vaskofbd0da42023-08-17 14:57:44 +02005759 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (schema->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005760 return LY_EINVAL;
Michal Vaskofbd0da42023-08-17 14:57:44 +02005761 } else if (set->context_op && (schema->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && (schema != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005762 return LY_EINVAL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005763 }
5764
5765 /* name check */
Michal Vasko19089f02022-06-07 11:02:11 +02005766 if (node_name) {
Michal Vaskofbd0da42023-08-17 14:57:44 +02005767 if ((set->ctx == LYD_CTX(node)) && (schema->name != node_name)) {
Michal Vasko19089f02022-06-07 11:02:11 +02005768 return LY_ENOT;
Michal Vaskofbd0da42023-08-17 14:57:44 +02005769 } else if ((set->ctx != LYD_CTX(node)) && strcmp(schema->name, node_name)) {
Michal Vasko19089f02022-06-07 11:02:11 +02005770 return LY_ENOT;
5771 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005772 }
5773
Michal Vasko6d657122022-10-19 14:38:35 +02005774 /* 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 +02005775 if (!(options & LYXP_IGNORE_WHEN) && lysc_has_when(schema) && !(node->flags & LYD_WHEN_TRUE) &&
Michal Vasko6d657122022-10-19 14:38:35 +02005776 (node != set->cur_node)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005777 return LY_EINCOMPLETE;
Michal Vaskoa1424542019-11-14 16:08:52 +01005778 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005779
5780 /* match */
5781 return LY_SUCCESS;
5782}
5783
5784/**
Michal Vasko49fec8e2022-05-24 10:28:33 +02005785 * @brief Get the next node in a forward DFS.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005786 *
Michal Vasko49fec8e2022-05-24 10:28:33 +02005787 * @param[in] iter Last returned node.
5788 * @param[in] stop Node to stop the search on and not return.
5789 * @return Next node, NULL if there are no more.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005790 */
Michal Vasko49fec8e2022-05-24 10:28:33 +02005791static const struct lyd_node *
5792moveto_axis_node_next_dfs_forward(const struct lyd_node *iter, const struct lyd_node *stop)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005793{
Michal Vasko49fec8e2022-05-24 10:28:33 +02005794 const struct lyd_node *next = NULL;
5795
5796 /* 1) child */
5797 next = lyd_child(iter);
5798 if (!next) {
5799 if (iter == stop) {
5800 /* reached stop, no more descendants */
5801 return NULL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005802 }
Michal Vasko49fec8e2022-05-24 10:28:33 +02005803 /* 2) child next sibling */
5804 next = iter->next;
5805 }
5806 while (!next) {
5807 iter = lyd_parent(iter);
5808 if ((!stop && !iter) || (stop && (lyd_parent(iter) == lyd_parent(stop)))) {
5809 return NULL;
5810 }
5811 next = iter->next;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005812 }
5813
Michal Vasko49fec8e2022-05-24 10:28:33 +02005814 return next;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005815}
5816
5817/**
Michal Vasko49fec8e2022-05-24 10:28:33 +02005818 * @brief Get the next node in a backward DFS.
5819 *
5820 * @param[in] iter Last returned node.
5821 * @param[in] stop Node to stop the search on and not return.
5822 * @return Next node, NULL if there are no more.
5823 */
5824static const struct lyd_node *
5825moveto_axis_node_next_dfs_backward(const struct lyd_node *iter, const struct lyd_node *stop)
5826{
5827 const struct lyd_node *next = NULL;
5828
5829 /* 1) previous sibling innermost last child */
5830 next = iter->prev->next ? iter->prev : NULL;
5831 while (next && lyd_child(next)) {
5832 next = lyd_child(next);
5833 next = next->prev;
5834 }
5835
5836 if (!next) {
5837 /* 2) parent */
5838 iter = lyd_parent(iter);
5839 if ((!stop && !iter) || (stop && (lyd_parent(iter) == lyd_parent(stop)))) {
5840 return NULL;
5841 }
5842 next = iter;
5843 }
5844
5845 return next;
5846}
5847
5848/**
5849 * @brief Get the first node on an axis for a context node.
5850 *
5851 * @param[in,out] iter NULL, updated to the next node.
5852 * @param[in,out] iter_type Node type 0 of @p iter, updated to the node type of the next node.
5853 * @param[in] node Context node.
5854 * @param[in] node_type Type of @p node.
5855 * @param[in] axis Axis to use.
5856 * @param[in] set XPath set with the general context.
5857 * @return LY_SUCCESS on success.
5858 * @return LY_ENOTFOUND if no next node found.
5859 */
5860static LY_ERR
5861moveto_axis_node_next_first(const struct lyd_node **iter, enum lyxp_node_type *iter_type, const struct lyd_node *node,
5862 enum lyxp_node_type node_type, enum lyxp_axis axis, struct lyxp_set *set)
5863{
5864 const struct lyd_node *next = NULL;
5865 enum lyxp_node_type next_type = 0;
5866
5867 assert(!*iter);
5868 assert(!*iter_type);
5869
5870 switch (axis) {
5871 case LYXP_AXIS_ANCESTOR_OR_SELF:
5872 case LYXP_AXIS_DESCENDANT_OR_SELF:
5873 case LYXP_AXIS_SELF:
5874 /* return the context node */
5875 next = node;
5876 next_type = node_type;
5877 break;
5878
5879 case LYXP_AXIS_ANCESTOR:
5880 case LYXP_AXIS_PARENT:
5881 if (node_type == LYXP_NODE_ELEM) {
5882 next = lyd_parent(node);
5883 next_type = next ? LYXP_NODE_ELEM : set->root_type;
5884 } else if (node_type == LYXP_NODE_TEXT) {
5885 next = node;
5886 next_type = LYXP_NODE_ELEM;
5887 } else if (node_type == LYXP_NODE_META) {
5888 next = ((struct lyd_meta *)node)->parent;
5889 next_type = LYXP_NODE_ELEM;
5890 } /* else root does not have a parent */
5891 break;
5892
5893 case LYXP_AXIS_CHILD:
5894 if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT)) {
5895 assert(!node);
5896
5897 /* search in all the trees */
5898 next = set->tree;
5899 next_type = next ? LYXP_NODE_ELEM : 0;
5900 } else {
5901 /* search in children */
5902 next = lyd_child(node);
5903 next_type = next ? LYXP_NODE_ELEM : 0;
5904 }
5905 break;
5906
5907 case LYXP_AXIS_DESCENDANT:
5908 if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT)) {
5909 /* top-level nodes */
5910 next = set->tree;
5911 next_type = LYXP_NODE_ELEM;
5912 } else if (node_type == LYXP_NODE_ELEM) {
5913 /* start from the context node */
5914 next = moveto_axis_node_next_dfs_forward(node, node);
5915 next_type = next ? LYXP_NODE_ELEM : 0;
5916 } /* else no children */
5917 break;
5918
5919 case LYXP_AXIS_FOLLOWING:
5920 case LYXP_AXIS_FOLLOWING_SIBLING:
5921 if (node_type == LYXP_NODE_ELEM) {
5922 /* first next sibling */
5923 next = node->next;
5924 next_type = next ? LYXP_NODE_ELEM : 0;
5925 } /* else no sibling */
5926 break;
5927
5928 case LYXP_AXIS_PRECEDING:
5929 if ((node_type == LYXP_NODE_ELEM) && node->prev->next) {
5930 /* skip ancestors */
5931 next = moveto_axis_node_next_dfs_backward(node, NULL);
5932 assert(next);
5933 next_type = LYXP_NODE_ELEM;
5934 } /* else no sibling */
5935 break;
5936
5937 case LYXP_AXIS_PRECEDING_SIBLING:
5938 if (node_type == LYXP_NODE_ELEM) {
5939 /* first previous sibling */
5940 next = node->prev->next ? node->prev : NULL;
5941 next_type = next ? LYXP_NODE_ELEM : 0;
5942 } /* else no sibling */
5943 break;
5944
5945 case LYXP_AXIS_ATTRIBUTE:
5946 /* handled specially */
5947 assert(0);
5948 LOGINT(set->ctx);
5949 break;
5950 }
5951
5952 *iter = next;
5953 *iter_type = next_type;
5954 return next_type ? LY_SUCCESS : LY_ENOTFOUND;
5955}
5956
5957/**
5958 * @brief Iterate over all nodes on an axis for a context node.
5959 *
5960 * @param[in,out] iter Last returned node, start with NULL, updated to the next node.
5961 * @param[in,out] iter_type Node type of @p iter, start with 0, updated to the node type of the next node.
5962 * @param[in] node Context node.
5963 * @param[in] node_type Type of @p node.
5964 * @param[in] axis Axis to use.
5965 * @param[in] set XPath set with the general context.
5966 * @return LY_SUCCESS on success.
5967 * @return LY_ENOTFOUND if no next node found.
5968 */
5969static LY_ERR
5970moveto_axis_node_next(const struct lyd_node **iter, enum lyxp_node_type *iter_type, const struct lyd_node *node,
5971 enum lyxp_node_type node_type, enum lyxp_axis axis, struct lyxp_set *set)
5972{
5973 const struct lyd_node *next = NULL;
5974 enum lyxp_node_type next_type = 0;
5975
5976 if (!*iter_type) {
5977 /* first returned node */
5978 return moveto_axis_node_next_first(iter, iter_type, node, node_type, axis, set);
5979 }
5980
5981 switch (axis) {
5982 case LYXP_AXIS_ANCESTOR_OR_SELF:
5983 if ((*iter == node) && (*iter_type == node_type)) {
5984 /* fake first ancestor, we returned self before */
5985 *iter = NULL;
5986 *iter_type = 0;
5987 return moveto_axis_node_next_first(iter, iter_type, node, node_type, LYXP_AXIS_ANCESTOR, set);
5988 } /* else continue ancestor */
5989
5990 /* fallthrough */
5991 case LYXP_AXIS_ANCESTOR:
5992 if (*iter_type == LYXP_NODE_ELEM) {
5993 /* iter parent */
5994 next = lyd_parent(*iter);
5995 next_type = next ? LYXP_NODE_ELEM : set->root_type;
5996 } /* else root, no ancestors */
5997 break;
5998
5999 case LYXP_AXIS_CHILD:
6000 assert(*iter_type == LYXP_NODE_ELEM);
6001
6002 /* next sibling (child) */
6003 next = (*iter)->next;
6004 next_type = next ? LYXP_NODE_ELEM : 0;
6005 break;
6006
6007 case LYXP_AXIS_DESCENDANT_OR_SELF:
6008 if ((*iter == node) && (*iter_type == node_type)) {
6009 /* fake first descendant, we returned self before */
6010 *iter = NULL;
6011 *iter_type = 0;
6012 return moveto_axis_node_next_first(iter, iter_type, node, node_type, LYXP_AXIS_DESCENDANT, set);
6013 } /* else continue descendant */
6014
6015 /* fallthrough */
6016 case LYXP_AXIS_DESCENDANT:
6017 assert(*iter_type == LYXP_NODE_ELEM);
6018 next = moveto_axis_node_next_dfs_forward(*iter, node);
6019 next_type = next ? LYXP_NODE_ELEM : 0;
6020 break;
6021
6022 case LYXP_AXIS_FOLLOWING:
6023 assert(*iter_type == LYXP_NODE_ELEM);
6024 next = moveto_axis_node_next_dfs_forward(*iter, NULL);
6025 next_type = next ? LYXP_NODE_ELEM : 0;
6026 break;
6027
6028 case LYXP_AXIS_FOLLOWING_SIBLING:
6029 assert(*iter_type == LYXP_NODE_ELEM);
6030
6031 /* next sibling */
6032 next = (*iter)->next;
6033 next_type = next ? LYXP_NODE_ELEM : 0;
6034 break;
6035
6036 case LYXP_AXIS_PARENT:
6037 case LYXP_AXIS_SELF:
6038 /* parent/self was returned before */
6039 break;
6040
6041 case LYXP_AXIS_PRECEDING:
6042 assert(*iter_type == LYXP_NODE_ELEM);
6043 next = moveto_axis_node_next_dfs_backward(*iter, NULL);
6044 next_type = next ? LYXP_NODE_ELEM : 0;
6045 break;
6046
6047 case LYXP_AXIS_PRECEDING_SIBLING:
6048 assert(*iter_type == LYXP_NODE_ELEM);
6049
6050 /* previous sibling */
6051 next = (*iter)->prev->next ? (*iter)->prev : NULL;
6052 next_type = next ? LYXP_NODE_ELEM : 0;
6053 break;
6054
6055 case LYXP_AXIS_ATTRIBUTE:
6056 /* handled specially */
6057 assert(0);
6058 LOGINT(set->ctx);
6059 break;
6060 }
6061
6062 *iter = next;
6063 *iter_type = next_type;
6064 return next_type ? LY_SUCCESS : LY_ENOTFOUND;
6065}
6066
6067/**
6068 * @brief Move context @p set to a node. Result is LYXP_SET_NODE_SET. Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006069 *
6070 * @param[in,out] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006071 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02006072 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko49fec8e2022-05-24 10:28:33 +02006073 * @param[in] axis Axis to search on.
Michal Vaskocdad7122020-11-09 21:04:44 +01006074 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006075 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6076 */
6077static LY_ERR
Michal Vasko49fec8e2022-05-24 10:28:33 +02006078moveto_node(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, enum lyxp_axis axis,
6079 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006080{
Michal Vasko230cf972021-12-02 12:31:00 +01006081 LY_ERR r, rc = LY_SUCCESS;
Michal Vasko49fec8e2022-05-24 10:28:33 +02006082 const struct lyd_node *iter;
6083 enum lyxp_node_type iter_type;
Michal Vasko230cf972021-12-02 12:31:00 +01006084 struct lyxp_set result;
Michal Vasko49fec8e2022-05-24 10:28:33 +02006085 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006086
aPiecek8b0cc152021-05-31 16:40:31 +02006087 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006088 return LY_SUCCESS;
6089 }
6090
6091 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006092 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006093 return LY_EVALID;
6094 }
6095
Michal Vasko230cf972021-12-02 12:31:00 +01006096 /* init result set */
6097 set_init(&result, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006098
Michal Vasko49fec8e2022-05-24 10:28:33 +02006099 for (i = 0; i < set->used; ++i) {
6100 /* iterate over all the nodes on the axis of the node */
6101 iter = NULL;
6102 iter_type = 0;
6103 while (!moveto_axis_node_next(&iter, &iter_type, set->val.nodes[i].node, set->val.nodes[i].type, axis, set)) {
6104 r = moveto_node_check(iter, iter_type, set, ncname, moveto_mod, options);
6105 if (r == LY_EINCOMPLETE) {
Michal Vasko230cf972021-12-02 12:31:00 +01006106 rc = r;
6107 goto cleanup;
Michal Vasko49fec8e2022-05-24 10:28:33 +02006108 } else if (r) {
6109 continue;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006110 }
Michal Vasko49fec8e2022-05-24 10:28:33 +02006111
6112 /* check for duplicates if they are possible */
6113 switch (axis) {
6114 case LYXP_AXIS_ANCESTOR:
6115 case LYXP_AXIS_ANCESTOR_OR_SELF:
6116 case LYXP_AXIS_DESCENDANT:
6117 case LYXP_AXIS_DESCENDANT_OR_SELF:
6118 case LYXP_AXIS_FOLLOWING:
6119 case LYXP_AXIS_FOLLOWING_SIBLING:
6120 case LYXP_AXIS_PARENT:
6121 case LYXP_AXIS_PRECEDING:
6122 case LYXP_AXIS_PRECEDING_SIBLING:
Michal Vasko306e2832022-07-25 09:15:17 +02006123 result.non_child_axis = 1;
Michal Vasko49fec8e2022-05-24 10:28:33 +02006124 if (set_dup_node_check(&result, iter, iter_type, -1)) {
6125 continue;
6126 }
6127 break;
6128 case LYXP_AXIS_CHILD:
6129 case LYXP_AXIS_SELF:
6130 break;
6131 case LYXP_AXIS_ATTRIBUTE:
6132 /* handled specially */
6133 assert(0);
6134 LOGINT(set->ctx);
6135 break;
6136 }
6137
6138 /* matching node */
6139 set_insert_node(&result, iter, 0, iter_type, result.used);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006140 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006141 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006142
Michal Vasko230cf972021-12-02 12:31:00 +01006143 /* move result to the set */
6144 lyxp_set_free_content(set);
6145 *set = result;
6146 result.type = LYXP_SET_NUMBER;
Michal Vasko49fec8e2022-05-24 10:28:33 +02006147
Michal Vasko306e2832022-07-25 09:15:17 +02006148 /* sort the final set if the document order could have been broken */
6149 if (set->non_child_axis) {
6150 set_sort(set);
6151 } else {
6152 assert(!set_sort(set));
6153 }
Michal Vasko230cf972021-12-02 12:31:00 +01006154
6155cleanup:
6156 lyxp_set_free_content(&result);
6157 return rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006158}
6159
6160/**
Michal Vasko49fec8e2022-05-24 10:28:33 +02006161 * @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 +02006162 *
6163 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02006164 * @param[in] scnode Matching node schema.
Michal Vasko004d3152020-06-11 19:59:22 +02006165 * @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 +01006166 * @param[in] options XPath options.
Michal Vaskod3678892020-05-21 10:06:58 +02006167 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6168 */
6169static LY_ERR
Michal Vasko49fec8e2022-05-24 10:28:33 +02006170moveto_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 +01006171 uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02006172{
Michal Vaskoddd76592022-01-17 13:34:48 +01006173 LY_ERR ret = LY_SUCCESS, r;
Michal Vaskod3678892020-05-21 10:06:58 +02006174 uint32_t i;
Michal Vaskod3678892020-05-21 10:06:58 +02006175 const struct lyd_node *siblings;
Michal Vasko230cf972021-12-02 12:31:00 +01006176 struct lyxp_set result;
Michal Vasko004d3152020-06-11 19:59:22 +02006177 struct lyd_node *sub, *inst = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02006178
Michal Vasko004d3152020-06-11 19:59:22 +02006179 assert(scnode && (!(scnode->nodetype & (LYS_LIST | LYS_LEAFLIST)) || predicates));
Michal Vaskod3678892020-05-21 10:06:58 +02006180
Michal Vasko50aaa072021-12-02 13:11:56 +01006181 /* init result set */
6182 set_init(&result, set);
6183
aPiecek8b0cc152021-05-31 16:40:31 +02006184 if (options & LYXP_SKIP_EXPR) {
Michal Vasko004d3152020-06-11 19:59:22 +02006185 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02006186 }
6187
6188 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006189 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko004d3152020-06-11 19:59:22 +02006190 ret = LY_EVALID;
6191 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02006192 }
6193
6194 /* context check for all the nodes since we have the schema node */
6195 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) {
6196 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02006197 goto cleanup;
Michal Vasko69730152020-10-09 16:30:07 +02006198 } else if (set->context_op && (scnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) &&
6199 (scnode != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02006200 lyxp_set_free_content(set);
6201 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +02006202 }
6203
6204 /* create specific data instance if needed */
6205 if (scnode->nodetype == LYS_LIST) {
Michal Vasko90189962023-02-28 12:10:34 +01006206 LY_CHECK_GOTO(ret = lyd_create_list(scnode, predicates, NULL, &inst), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02006207 } else if (scnode->nodetype == LYS_LEAFLIST) {
6208 LY_CHECK_GOTO(ret = lyd_create_term2(scnode, &predicates[0].value, &inst), cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02006209 }
6210
Michal Vasko230cf972021-12-02 12:31:00 +01006211 for (i = 0; i < set->used; ++i) {
Michal Vaskod3678892020-05-21 10:06:58 +02006212 siblings = NULL;
6213
6214 if ((set->val.nodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.nodes[i].type == LYXP_NODE_ROOT)) {
6215 assert(!set->val.nodes[i].node);
6216
6217 /* search in all the trees */
6218 siblings = set->tree;
6219 } else if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
6220 /* search in children */
Radek Krejcia1c1e542020-09-29 16:06:52 +02006221 siblings = lyd_child(set->val.nodes[i].node);
Michal Vaskod3678892020-05-21 10:06:58 +02006222 }
6223
6224 /* find the node using hashes */
Michal Vasko004d3152020-06-11 19:59:22 +02006225 if (inst) {
Michal Vaskoddd76592022-01-17 13:34:48 +01006226 r = lyd_find_sibling_first(siblings, inst, &sub);
Michal Vaskod3678892020-05-21 10:06:58 +02006227 } else {
Michal Vaskoddd76592022-01-17 13:34:48 +01006228 r = lyd_find_sibling_val(siblings, scnode, NULL, 0, &sub);
Michal Vaskod3678892020-05-21 10:06:58 +02006229 }
Michal Vaskod8a52012023-08-15 11:38:10 +02006230 if (r == LY_ENOTFOUND) {
6231 /* may still be an opaque node */
6232 r = lyd_find_sibling_opaq_next(siblings, scnode->name, &sub);
6233 }
Michal Vaskoddd76592022-01-17 13:34:48 +01006234 LY_CHECK_ERR_GOTO(r && (r != LY_ENOTFOUND), ret = r, cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02006235
6236 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01006237 if (!(options & LYXP_IGNORE_WHEN) && sub && lysc_has_when(sub->schema) && !(sub->flags & LYD_WHEN_TRUE)) {
Michal Vasko004d3152020-06-11 19:59:22 +02006238 ret = LY_EINCOMPLETE;
6239 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02006240 }
6241
6242 if (sub) {
6243 /* pos filled later */
Michal Vasko230cf972021-12-02 12:31:00 +01006244 set_insert_node(&result, sub, 0, LYXP_NODE_ELEM, result.used);
Michal Vaskod3678892020-05-21 10:06:58 +02006245 }
6246 }
6247
Michal Vasko230cf972021-12-02 12:31:00 +01006248 /* move result to the set */
6249 lyxp_set_free_content(set);
6250 *set = result;
6251 result.type = LYXP_SET_NUMBER;
6252 assert(!set_sort(set));
6253
Michal Vasko004d3152020-06-11 19:59:22 +02006254cleanup:
Michal Vasko230cf972021-12-02 12:31:00 +01006255 lyxp_set_free_content(&result);
Michal Vasko004d3152020-06-11 19:59:22 +02006256 lyd_free_tree(inst);
6257 return ret;
Michal Vaskod3678892020-05-21 10:06:58 +02006258}
6259
6260/**
Michal Vasko49fec8e2022-05-24 10:28:33 +02006261 * @brief Check @p node as a part of schema NameTest processing.
6262 *
6263 * @param[in] node Schema node to check.
6264 * @param[in] ctx_scnode Context node.
6265 * @param[in] set Set to read general context from.
6266 * @param[in] node_name Node name in the dictionary to move to, NULL for any nodes.
6267 * @param[in] moveto_mod Expected module of the node, NULL for no prefix.
6268 * @return LY_ERR (LY_ENOT if node does not match, LY_EINVAL if neither node nor any children match)
6269 */
6270static LY_ERR
6271moveto_scnode_check(const struct lysc_node *node, const struct lysc_node *ctx_scnode, const struct lyxp_set *set,
6272 const char *node_name, const struct lys_module *moveto_mod)
6273{
6274 if (!moveto_mod && node_name) {
6275 switch (set->format) {
6276 case LY_VALUE_SCHEMA:
6277 case LY_VALUE_SCHEMA_RESOLVED:
6278 /* use current module */
6279 moveto_mod = set->cur_mod;
6280 break;
6281 case LY_VALUE_JSON:
6282 case LY_VALUE_LYB:
6283 case LY_VALUE_STR_NS:
6284 /* inherit module of the context node, if any */
6285 if (ctx_scnode) {
6286 moveto_mod = ctx_scnode->module;
6287 }
6288 break;
6289 case LY_VALUE_CANON:
6290 case LY_VALUE_XML:
6291 /* not defined */
6292 LOGINT(set->ctx);
6293 return LY_EINVAL;
6294 }
6295 }
6296
6297 if (!node) {
6298 /* root will not match a specific node */
6299 if (node_name || moveto_mod) {
6300 return LY_ENOT;
6301 }
6302 return LY_SUCCESS;
6303 }
6304
6305 /* module check */
6306 if (moveto_mod && (node->module != moveto_mod)) {
6307 return LY_ENOT;
6308 }
6309
6310 /* context check */
6311 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (node->flags & LYS_CONFIG_R)) {
6312 return LY_EINVAL;
6313 } else if (set->context_op && (node->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && (node != set->context_op)) {
6314 return LY_EINVAL;
6315 }
6316
6317 /* name check */
6318 if (node_name && (node->name != node_name)) {
6319 return LY_ENOT;
6320 }
6321
6322 /* match */
6323 return LY_SUCCESS;
6324}
6325
6326/**
6327 * @brief Get the next node in a forward schema node DFS.
6328 *
6329 * @param[in] iter Last returned node.
6330 * @param[in] stop Node to stop the search on and not return.
6331 * @param[in] getnext_opts Options for ::lys_getnext().
6332 * @return Next node, NULL if there are no more.
6333 */
6334static const struct lysc_node *
6335moveto_axis_scnode_next_dfs_forward(const struct lysc_node *iter, const struct lysc_node *stop, uint32_t getnext_opts)
6336{
6337 const struct lysc_node *next = NULL;
6338
6339 next = lysc_node_child(iter);
6340 if (!next) {
6341 /* no children, try siblings */
Michal Vasko34a22fe2022-06-15 07:58:55 +02006342 if ((iter == stop) || !lysc_data_parent(iter)) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02006343 /* we are done, no next element to process */
6344 return NULL;
6345 }
6346
6347 next = lys_getnext(iter, lysc_data_parent(iter), NULL, getnext_opts);
6348 }
6349 while (!next && iter) {
6350 /* parent is already processed, go to its sibling */
6351 iter = iter->parent;
Michal Vasko34a22fe2022-06-15 07:58:55 +02006352 if ((iter == stop) || !lysc_data_parent(iter)) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02006353 /* we are done, no next element to process */
6354 return NULL;
6355 }
6356 next = lys_getnext(iter, lysc_data_parent(iter), NULL, getnext_opts);
6357 }
6358
6359 return next;
6360}
6361
6362/**
6363 * @brief Consider schema node based on its in_ctx enum value.
6364 *
6365 * @param[in,out] in_ctx In_ctx enum of the schema node, may be updated.
6366 * @param[in] axis Axis to use.
6367 * @return LY_SUCCESS on success.
6368 * @return LY_ENOT if the node should not be returned.
6369 */
6370static LY_ERR
6371moveto_axis_scnode_next_in_ctx(int32_t *in_ctx, enum lyxp_axis axis)
6372{
6373 switch (axis) {
6374 case LYXP_AXIS_SELF:
6375 if ((*in_ctx == LYXP_SET_SCNODE_START) || (*in_ctx == LYXP_SET_SCNODE_ATOM_CTX)) {
6376 /* additionally put the start node into context */
6377 *in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
6378 return LY_SUCCESS;
6379 }
6380 break;
6381 case LYXP_AXIS_PARENT:
6382 case LYXP_AXIS_ANCESTOR_OR_SELF:
6383 case LYXP_AXIS_ANCESTOR:
6384 case LYXP_AXIS_DESCENDANT_OR_SELF:
6385 case LYXP_AXIS_DESCENDANT:
6386 case LYXP_AXIS_FOLLOWING:
6387 case LYXP_AXIS_FOLLOWING_SIBLING:
6388 case LYXP_AXIS_PRECEDING:
6389 case LYXP_AXIS_PRECEDING_SIBLING:
6390 case LYXP_AXIS_CHILD:
6391 if (*in_ctx == LYXP_SET_SCNODE_START) {
6392 /* remember that context node was used */
6393 *in_ctx = LYXP_SET_SCNODE_START_USED;
6394 return LY_SUCCESS;
6395 } else if (*in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
6396 /* traversed */
6397 *in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
6398 return LY_SUCCESS;
6399 }
6400 break;
6401 case LYXP_AXIS_ATTRIBUTE:
6402 /* unreachable */
6403 assert(0);
6404 LOGINT(NULL);
6405 break;
6406 }
6407
6408 return LY_ENOT;
6409}
6410
6411/**
6412 * @brief Get previous sibling for a schema node.
6413 *
6414 * @param[in] scnode Schema node.
6415 * @param[in] getnext_opts Options for ::lys_getnext().
6416 * @return Previous sibling, NULL if none.
6417 */
6418static const struct lysc_node *
6419moveto_axis_scnode_preceding_sibling(const struct lysc_node *scnode, uint32_t getnext_opts)
6420{
6421 const struct lysc_node *next = NULL, *prev = NULL;
6422
6423 while ((next = lys_getnext(next, lysc_data_parent(scnode), scnode->module->compiled, getnext_opts))) {
6424 if (next == scnode) {
6425 break;
6426 }
6427
6428 prev = next;
6429 }
6430
6431 return prev;
6432}
6433
6434/**
6435 * @brief Get the first schema node on an axis for a context node.
6436 *
6437 * @param[in,out] iter Last returned node, start with NULL, updated to the next node.
6438 * @param[in,out] iter_type Node type of @p iter, start with 0, updated to the node type of the next node.
6439 * @param[in,out] iter_mod Internal module iterator, do not change.
6440 * @param[in,out] iter_mod_idx Internal module index iterator, do not change.
6441 * @param[in] scnode Context node.
6442 * @param[in] node_type Type of @p scnode.
6443 * @param[in] in_ctx In_ctx enum of @p scnode.
6444 * @param[in] axis Axis to use.
6445 * @param[in] set XPath set with the general context.
6446 * @param[in] getnext_opts Options for ::lys_getnext().
6447 * @return LY_SUCCESS on success.
6448 * @return LY_ENOTFOUND if no next node found.
6449 */
6450static LY_ERR
6451moveto_axis_scnode_next_first(const struct lysc_node **iter, enum lyxp_node_type *iter_type, const struct lys_module **iter_mod,
6452 uint32_t *iter_mod_idx, const struct lysc_node *scnode, enum lyxp_node_type node_type, enum lyxp_axis axis,
6453 struct lyxp_set *set, uint32_t getnext_opts)
6454{
6455 const struct lysc_node *next = NULL;
6456 enum lyxp_node_type next_type = 0;
6457
6458 assert(!*iter);
6459 assert(!*iter_type);
6460
6461 *iter_mod = NULL;
6462 *iter_mod_idx = 0;
6463
6464 switch (axis) {
6465 case LYXP_AXIS_ANCESTOR_OR_SELF:
6466 case LYXP_AXIS_DESCENDANT_OR_SELF:
6467 case LYXP_AXIS_SELF:
6468 if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT) || (node_type == LYXP_NODE_ELEM)) {
6469 /* just return the node */
6470 next = scnode;
6471 next_type = node_type;
6472 }
6473 break;
6474
6475 case LYXP_AXIS_ANCESTOR:
6476 case LYXP_AXIS_PARENT:
6477 if (node_type == LYXP_NODE_ELEM) {
6478 next = lysc_data_parent(scnode);
6479 next_type = next ? LYXP_NODE_ELEM : set->root_type;
6480 } /* else no parent */
6481 break;
6482
6483 case LYXP_AXIS_DESCENDANT:
6484 case LYXP_AXIS_CHILD:
6485 if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT)) {
6486 /* it can actually be in any module, it's all <running>, and even if it's moveto_mod (if set),
6487 * it can be in a top-level augment */
6488 while ((*iter_mod = ly_ctx_get_module_iter(set->ctx, iter_mod_idx))) {
6489 /* module may not be implemented or not compiled yet */
6490 if (!(*iter_mod)->compiled) {
6491 continue;
6492 }
6493
6494 /* get next node */
6495 if ((next = lys_getnext(NULL, NULL, (*iter_mod)->compiled, getnext_opts))) {
6496 next_type = LYXP_NODE_ELEM;
6497 break;
6498 }
6499 }
6500 } else if (node_type == LYXP_NODE_ELEM) {
6501 /* get next node */
6502 next = lys_getnext(NULL, scnode, NULL, getnext_opts);
6503 next_type = next ? LYXP_NODE_ELEM : 0;
6504 }
6505 break;
6506
6507 case LYXP_AXIS_FOLLOWING:
6508 case LYXP_AXIS_FOLLOWING_SIBLING:
6509 if (node_type == LYXP_NODE_ELEM) {
6510 /* first next sibling */
6511 next = lys_getnext(scnode, lysc_data_parent(scnode), scnode->module->compiled, getnext_opts);
6512 next_type = next ? LYXP_NODE_ELEM : 0;
6513 } /* else no sibling */
6514 break;
6515
6516 case LYXP_AXIS_PRECEDING:
6517 case LYXP_AXIS_PRECEDING_SIBLING:
6518 if (node_type == LYXP_NODE_ELEM) {
6519 /* first parent sibling */
6520 next = lys_getnext(NULL, lysc_data_parent(scnode), scnode->module->compiled, getnext_opts);
6521 if (next == scnode) {
6522 /* no preceding sibling */
6523 next = NULL;
6524 }
6525 next_type = next ? LYXP_NODE_ELEM : 0;
6526 } /* else no sibling */
6527 break;
6528
6529 case LYXP_AXIS_ATTRIBUTE:
6530 /* unreachable */
6531 assert(0);
6532 LOGINT(set->ctx);
6533 break;
6534 }
6535
6536 *iter = next;
6537 *iter_type = next_type;
6538 return next_type ? LY_SUCCESS : LY_ENOTFOUND;
6539}
6540
6541/**
6542 * @brief Iterate over all schema nodes on an axis for a context node.
6543 *
6544 * @param[in,out] iter Last returned node, start with NULL, updated to the next node.
6545 * @param[in,out] iter_type Node type of @p iter, start with 0, updated to the node type of the next node.
6546 * @param[in,out] iter_mod Internal module iterator, do not change.
6547 * @param[in,out] iter_mod_idx Internal module index iterator, do not change.
6548 * @param[in] scnode Context node.
6549 * @param[in] node_type Type of @p scnode.
6550 * @param[in] axis Axis to use.
6551 * @param[in] set XPath set with the general context.
6552 * @param[in] getnext_opts Options for ::lys_getnext().
6553 * @return LY_SUCCESS on success.
6554 * @return LY_ENOTFOUND if no next node found.
6555 */
6556static LY_ERR
6557moveto_axis_scnode_next(const struct lysc_node **iter, enum lyxp_node_type *iter_type, const struct lys_module **iter_mod,
6558 uint32_t *iter_mod_idx, const struct lysc_node *scnode, enum lyxp_node_type node_type, enum lyxp_axis axis,
6559 struct lyxp_set *set, uint32_t getnext_opts)
6560{
6561 const struct lysc_node *next = NULL, *dfs_stop;
6562 enum lyxp_node_type next_type = 0;
6563
6564 if (!*iter_type) {
6565 /* first returned node */
6566 return moveto_axis_scnode_next_first(iter, iter_type, iter_mod, iter_mod_idx, scnode, node_type, axis, set,
6567 getnext_opts);
6568 }
6569
6570 switch (axis) {
6571 case LYXP_AXIS_PARENT:
6572 case LYXP_AXIS_SELF:
6573 /* parent/self was returned before */
6574 break;
6575
6576 case LYXP_AXIS_ANCESTOR_OR_SELF:
6577 if ((*iter == scnode) && (*iter_type == node_type)) {
6578 /* fake first ancestor, we returned self before */
6579 *iter = NULL;
6580 *iter_type = 0;
6581 return moveto_axis_scnode_next_first(iter, iter_type, iter_mod, iter_mod_idx, scnode, node_type,
6582 LYXP_AXIS_ANCESTOR, set, getnext_opts);
6583 } /* else continue ancestor */
6584
6585 /* fallthrough */
6586 case LYXP_AXIS_ANCESTOR:
6587 if (*iter_type == LYXP_NODE_ELEM) {
6588 next = lysc_data_parent(*iter);
6589 next_type = next ? LYXP_NODE_ELEM : set->root_type;
6590 } /* else no ancestor */
6591 break;
6592
6593 case LYXP_AXIS_DESCENDANT_OR_SELF:
6594 if ((*iter == scnode) && (*iter_type == node_type)) {
6595 /* fake first descendant, we returned self before */
6596 *iter = NULL;
6597 *iter_type = 0;
6598 return moveto_axis_scnode_next_first(iter, iter_type, iter_mod, iter_mod_idx, scnode, node_type,
6599 LYXP_AXIS_DESCENDANT, set, getnext_opts);
6600 } /* else DFS until context node */
6601 dfs_stop = scnode;
6602
6603 /* fallthrough */
6604 case LYXP_AXIS_DESCENDANT:
6605 if (axis == LYXP_AXIS_DESCENDANT) {
6606 /* DFS until the context node */
6607 dfs_stop = scnode;
6608 }
6609
6610 /* fallthrough */
6611 case LYXP_AXIS_PRECEDING:
6612 if (axis == LYXP_AXIS_PRECEDING) {
6613 /* DFS until the previous sibling */
6614 dfs_stop = moveto_axis_scnode_preceding_sibling(scnode, getnext_opts);
6615 assert(dfs_stop);
6616
6617 if (*iter == dfs_stop) {
6618 /* we are done */
6619 break;
6620 }
6621 }
6622
6623 /* fallthrough */
6624 case LYXP_AXIS_FOLLOWING:
6625 if (axis == LYXP_AXIS_FOLLOWING) {
6626 /* DFS through the whole module */
6627 dfs_stop = NULL;
6628 }
6629
6630 /* nested nodes */
6631 assert(*iter);
6632 next = moveto_axis_scnode_next_dfs_forward(*iter, dfs_stop, getnext_opts);
6633 if (next) {
6634 next_type = LYXP_NODE_ELEM;
6635 break;
6636 } /* else get next top-level node just like a child */
6637
6638 /* fallthrough */
6639 case LYXP_AXIS_CHILD:
6640 case LYXP_AXIS_FOLLOWING_SIBLING:
6641 if (!*iter_mod) {
6642 /* nodes from a single module */
6643 if ((next = lys_getnext(*iter, lysc_data_parent(*iter), (*iter)->module->compiled, getnext_opts))) {
6644 next_type = LYXP_NODE_ELEM;
6645 break;
6646 }
6647
6648 assert(scnode);
Michal Vaskoa353cce2022-11-14 10:09:55 +01006649 if ((axis != LYXP_AXIS_CHILD) && !lysc_data_parent(scnode)) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02006650 /* iterating over top-level nodes, find next */
6651 while (lysc_data_parent(*iter)) {
6652 *iter = lysc_data_parent(*iter);
6653 }
6654 if ((next = lys_getnext(*iter, NULL, (*iter)->module->compiled, getnext_opts))) {
6655 next_type = LYXP_NODE_ELEM;
6656 break;
6657 }
6658 }
6659 }
6660
6661 while (*iter_mod) {
6662 /* module top-level nodes */
6663 if ((next = lys_getnext(*iter, NULL, (*iter_mod)->compiled, getnext_opts))) {
6664 next_type = LYXP_NODE_ELEM;
6665 break;
6666 }
6667
6668 /* get next module */
6669 while ((*iter_mod = ly_ctx_get_module_iter(set->ctx, iter_mod_idx))) {
6670 /* module may not be implemented or not compiled yet */
6671 if ((*iter_mod)->compiled) {
6672 break;
6673 }
6674 }
6675
6676 /* new module, start over */
6677 *iter = NULL;
6678 }
6679 break;
6680
6681 case LYXP_AXIS_PRECEDING_SIBLING:
6682 assert(*iter);
6683
6684 /* next parent sibling until scnode */
6685 next = lys_getnext(*iter, lysc_data_parent(*iter), (*iter)->module->compiled, getnext_opts);
6686 if (next == scnode) {
6687 /* no previous sibling */
6688 next = NULL;
6689 }
6690 next_type = next ? LYXP_NODE_ELEM : 0;
6691 break;
6692
6693 case LYXP_AXIS_ATTRIBUTE:
6694 /* unreachable */
6695 assert(0);
6696 LOGINT(set->ctx);
6697 break;
6698 }
6699
6700 *iter = next;
6701 *iter_type = next_type;
6702 return next_type ? LY_SUCCESS : LY_ENOTFOUND;
6703}
6704
6705/**
Michal Vaskod3678892020-05-21 10:06:58 +02006706 * @brief Move context @p set to a schema node. Result is LYXP_SET_SCNODE_SET (or LYXP_SET_EMPTY).
6707 *
6708 * @param[in,out] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006709 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02006710 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko49fec8e2022-05-24 10:28:33 +02006711 * @param[in] axis Axis to search on.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006712 * @param[in] options XPath options.
6713 * @return LY_ERR
6714 */
6715static LY_ERR
Michal Vasko49fec8e2022-05-24 10:28:33 +02006716moveto_scnode(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, enum lyxp_axis axis,
6717 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006718{
Radek Krejci857189e2020-09-01 13:26:36 +02006719 ly_bool temp_ctx = 0;
Michal Vasko49fec8e2022-05-24 10:28:33 +02006720 uint32_t getnext_opts, orig_used, i, mod_idx, idx;
stewegd8e2fc92023-05-31 09:52:56 +02006721 const struct lys_module *mod = NULL;
Michal Vasko49fec8e2022-05-24 10:28:33 +02006722 const struct lysc_node *iter;
6723 enum lyxp_node_type iter_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006724
aPiecek8b0cc152021-05-31 16:40:31 +02006725 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006726 return LY_SUCCESS;
6727 }
6728
6729 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006730 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006731 return LY_EVALID;
6732 }
6733
Michal Vaskocafad9d2019-11-07 15:20:03 +01006734 /* getnext opts */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01006735 getnext_opts = 0;
Michal Vaskocafad9d2019-11-07 15:20:03 +01006736 if (options & LYXP_SCNODE_OUTPUT) {
6737 getnext_opts |= LYS_GETNEXT_OUTPUT;
6738 }
Michal Vaskoe482bb92023-02-01 11:41:41 +01006739 if (options & LYXP_SCNODE_SCHEMAMOUNT) {
6740 getnext_opts |= LYS_GETNEXT_WITHSCHEMAMOUNT;
6741 }
Michal Vaskocafad9d2019-11-07 15:20:03 +01006742
Michal Vasko03ff5a72019-09-11 13:49:33 +02006743 orig_used = set->used;
6744 for (i = 0; i < orig_used; ++i) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02006745 /* update in_ctx first */
6746 if (moveto_axis_scnode_next_in_ctx(&set->val.scnodes[i].in_ctx, axis)) {
6747 /* not usable, skip */
6748 continue;
6749 }
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006750
Michal Vasko49fec8e2022-05-24 10:28:33 +02006751 iter = NULL;
6752 iter_type = 0;
6753 while (!moveto_axis_scnode_next(&iter, &iter_type, &mod, &mod_idx, set->val.scnodes[i].scnode,
6754 set->val.scnodes[i].type, axis, set, getnext_opts)) {
6755 if (moveto_scnode_check(iter, NULL, set, ncname, moveto_mod)) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006756 continue;
6757 }
6758
Michal Vasko49fec8e2022-05-24 10:28:33 +02006759 /* insert */
Michal Vaskoe4a6d012023-05-22 14:34:52 +02006760 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, iter_type, axis, &idx));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006761
Michal Vasko49fec8e2022-05-24 10:28:33 +02006762 /* we need to prevent these nodes from being considered in this moveto */
6763 if ((idx < orig_used) && (idx > i)) {
6764 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX;
6765 temp_ctx = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006766 }
6767 }
Michal Vasko66330fc2022-11-21 15:52:24 +01006768
6769 if (moveto_mod && ncname && ((axis == LYXP_AXIS_DESCENDANT) || (axis == LYXP_AXIS_CHILD)) &&
6770 (set->val.scnodes[i].type == LYXP_NODE_ELEM) && !ly_nested_ext_schema(NULL, set->val.scnodes[i].scnode,
6771 moveto_mod->name, strlen(moveto_mod->name), LY_VALUE_JSON, NULL, ncname, strlen(ncname), &iter, NULL)) {
6772 /* there is a matching node from an extension, use it */
Michal Vaskoe4a6d012023-05-22 14:34:52 +02006773 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, axis, &idx));
Michal Vasko66330fc2022-11-21 15:52:24 +01006774 if ((idx < orig_used) && (idx > i)) {
6775 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX;
6776 temp_ctx = 1;
6777 }
6778 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006779 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006780
6781 /* correct temporary in_ctx values */
6782 if (temp_ctx) {
6783 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006784 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_NEW_CTX) {
6785 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006786 }
6787 }
6788 }
6789
6790 return LY_SUCCESS;
6791}
6792
6793/**
Michal Vasko49fec8e2022-05-24 10:28:33 +02006794 * @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 +02006795 * Context position aware.
6796 *
6797 * @param[in] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006798 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02006799 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vaskocdad7122020-11-09 21:04:44 +01006800 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006801 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6802 */
6803static LY_ERR
Michal Vasko49fec8e2022-05-24 10:28:33 +02006804moveto_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 +02006805{
6806 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006807 const struct lyd_node *next, *elem, *start;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006808 struct lyxp_set ret_set;
6809 LY_ERR rc;
6810
aPiecek8b0cc152021-05-31 16:40:31 +02006811 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006812 return LY_SUCCESS;
6813 }
6814
6815 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006816 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006817 return LY_EVALID;
6818 }
6819
Michal Vasko9f96a052020-03-10 09:41:45 +01006820 /* 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 +02006821 rc = xpath_pi_node(set, LYXP_AXIS_CHILD, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006822 LY_CHECK_RET(rc);
6823
Michal Vasko6346ece2019-09-24 13:12:53 +02006824 /* this loop traverses all the nodes in the set and adds/keeps only those that match qname */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006825 set_init(&ret_set, set);
6826 for (i = 0; i < set->used; ++i) {
6827
6828 /* TREE DFS */
6829 start = set->val.nodes[i].node;
6830 for (elem = next = start; elem; elem = next) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02006831 rc = moveto_node_check(elem, LYXP_NODE_ELEM, set, ncname, moveto_mod, options);
Michal Vasko6346ece2019-09-24 13:12:53 +02006832 if (!rc) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006833 /* add matching node into result set */
6834 set_insert_node(&ret_set, elem, 0, LYXP_NODE_ELEM, ret_set.used);
6835 if (set_dup_node_check(set, elem, LYXP_NODE_ELEM, i)) {
6836 /* the node is a duplicate, we'll process it later in the set */
6837 goto skip_children;
6838 }
Michal Vasko6346ece2019-09-24 13:12:53 +02006839 } else if (rc == LY_EINCOMPLETE) {
Michal Vasko6346ece2019-09-24 13:12:53 +02006840 return rc;
6841 } else if (rc == LY_EINVAL) {
6842 goto skip_children;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006843 }
6844
6845 /* TREE DFS NEXT ELEM */
6846 /* select element for the next run - children first */
Radek Krejcia1c1e542020-09-29 16:06:52 +02006847 next = lyd_child(elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006848 if (!next) {
6849skip_children:
6850 /* no children, so try siblings, but only if it's not the start,
6851 * that is considered to be the root and it's siblings are not traversed */
6852 if (elem != start) {
6853 next = elem->next;
6854 } else {
6855 break;
6856 }
6857 }
6858 while (!next) {
6859 /* no siblings, go back through the parents */
Michal Vasko9e685082021-01-29 14:49:09 +01006860 if (lyd_parent(elem) == start) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006861 /* we are done, no next element to process */
6862 break;
6863 }
6864 /* parent is already processed, go to its sibling */
Michal Vasko9e685082021-01-29 14:49:09 +01006865 elem = lyd_parent(elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006866 next = elem->next;
6867 }
6868 }
6869 }
6870
6871 /* make the temporary set the current one */
6872 ret_set.ctx_pos = set->ctx_pos;
6873 ret_set.ctx_size = set->ctx_size;
Michal Vaskod3678892020-05-21 10:06:58 +02006874 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006875 memcpy(set, &ret_set, sizeof *set);
Michal Vasko306e2832022-07-25 09:15:17 +02006876 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006877
6878 return LY_SUCCESS;
6879}
6880
6881/**
Michal Vasko49fec8e2022-05-24 10:28:33 +02006882 * @brief Move context @p set to a child schema node and all its descendants starting from a node.
6883 * Result is LYXP_SET_NODE_SET.
6884 *
6885 * @param[in] set Set to use.
6886 * @param[in] start Start node whose subtree to add.
6887 * @param[in] start_idx Index of @p start in @p set.
6888 * @param[in] moveto_mod Matching node module, NULL for no prefix.
6889 * @param[in] ncname Matching node name in the dictionary, NULL for any.
6890 * @param[in] options XPath options.
6891 * @return LY_ERR value.
6892 */
6893static LY_ERR
6894moveto_scnode_dfs(struct lyxp_set *set, const struct lysc_node *start, uint32_t start_idx,
6895 const struct lys_module *moveto_mod, const char *ncname, uint32_t options)
6896{
6897 const struct lysc_node *next, *elem;
6898 uint32_t idx;
6899 LY_ERR rc;
6900
6901 /* TREE DFS */
6902 for (elem = next = start; elem; elem = next) {
6903 if ((elem == start) || (elem->nodetype & (LYS_CHOICE | LYS_CASE))) {
6904 /* schema-only nodes, skip root */
6905 goto next_iter;
6906 }
6907
6908 rc = moveto_scnode_check(elem, start, set, ncname, moveto_mod);
6909 if (!rc) {
6910 if (lyxp_set_scnode_contains(set, elem, LYXP_NODE_ELEM, start_idx, &idx)) {
6911 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
6912 if (idx > start_idx) {
6913 /* we will process it later in the set */
6914 goto skip_children;
6915 }
6916 } else {
Michal Vaskoe4a6d012023-05-22 14:34:52 +02006917 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, elem, LYXP_NODE_ELEM, LYXP_AXIS_DESCENDANT, NULL));
Michal Vasko49fec8e2022-05-24 10:28:33 +02006918 }
6919 } else if (rc == LY_EINVAL) {
6920 goto skip_children;
6921 }
6922
6923next_iter:
6924 /* TREE DFS NEXT ELEM */
6925 /* select element for the next run - children first */
6926 next = lysc_node_child(elem);
6927 if (next && (next->nodetype == LYS_INPUT) && (options & LYXP_SCNODE_OUTPUT)) {
6928 next = next->next;
6929 } else if (next && (next->nodetype == LYS_OUTPUT) && !(options & LYXP_SCNODE_OUTPUT)) {
6930 next = next->next;
6931 }
6932 if (!next) {
6933skip_children:
6934 /* no children, so try siblings, but only if it's not the start,
6935 * that is considered to be the root and it's siblings are not traversed */
6936 if (elem != start) {
6937 next = elem->next;
6938 } else {
6939 break;
6940 }
6941 }
6942 while (!next) {
6943 /* no siblings, go back through the parents */
6944 if (elem->parent == start) {
6945 /* we are done, no next element to process */
6946 break;
6947 }
6948 /* parent is already processed, go to its sibling */
6949 elem = elem->parent;
6950 next = elem->next;
6951 }
6952 }
6953
6954 return LY_SUCCESS;
6955}
6956
6957/**
6958 * @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 +02006959 *
6960 * @param[in] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006961 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02006962 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006963 * @param[in] options XPath options.
Michal Vasko49fec8e2022-05-24 10:28:33 +02006964 * @return LY_ERR value.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006965 */
6966static LY_ERR
Michal Vasko49fec8e2022-05-24 10:28:33 +02006967moveto_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 +02006968{
Michal Vasko49fec8e2022-05-24 10:28:33 +02006969 uint32_t i, orig_used, mod_idx;
6970 const struct lys_module *mod;
6971 const struct lysc_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006972
aPiecek8b0cc152021-05-31 16:40:31 +02006973 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006974 return LY_SUCCESS;
6975 }
6976
6977 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006978 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006979 return LY_EVALID;
6980 }
6981
Michal Vasko03ff5a72019-09-11 13:49:33 +02006982 orig_used = set->used;
6983 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006984 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
6985 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006986 continue;
6987 }
6988
6989 /* remember context node */
Radek Krejcif13b87b2020-12-01 22:02:17 +01006990 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vaskoec4df482019-12-16 10:02:18 +01006991 } else {
Michal Vasko1a09b212021-05-06 13:00:10 +02006992 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006993 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006994
Michal Vasko49fec8e2022-05-24 10:28:33 +02006995 if ((set->val.scnodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.scnodes[i].type == LYXP_NODE_ROOT)) {
6996 /* traverse all top-level nodes in all the modules */
6997 mod_idx = 0;
6998 while ((mod = ly_ctx_get_module_iter(set->ctx, &mod_idx))) {
6999 /* module may not be implemented or not compiled yet */
7000 if (!mod->compiled) {
7001 continue;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007002 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02007003
Michal Vasko49fec8e2022-05-24 10:28:33 +02007004 root = NULL;
7005 /* no getnext opts needed */
7006 while ((root = lys_getnext(root, NULL, mod->compiled, 0))) {
7007 LY_CHECK_RET(moveto_scnode_dfs(set, root, i, moveto_mod, ncname, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007008 }
7009 }
Michal Vasko49fec8e2022-05-24 10:28:33 +02007010
7011 } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
7012 /* add all the descendants recursively */
7013 LY_CHECK_RET(moveto_scnode_dfs(set, set->val.scnodes[i].scnode, i, moveto_mod, ncname, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007014 }
7015 }
7016
7017 return LY_SUCCESS;
7018}
7019
7020/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02007021 * @brief Move context @p set to an attribute. Result is LYXP_SET_NODE_SET.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007022 * Indirectly context position aware.
7023 *
7024 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02007025 * @param[in] mod Matching metadata module, NULL for any.
7026 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
aPiecek8b0cc152021-05-31 16:40:31 +02007027 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007028 * @return LY_ERR
7029 */
7030static LY_ERR
aPiecek8b0cc152021-05-31 16:40:31 +02007031moveto_attr(struct lyxp_set *set, const struct lys_module *mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007032{
Michal Vasko9f96a052020-03-10 09:41:45 +01007033 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007034
aPiecek8b0cc152021-05-31 16:40:31 +02007035 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007036 return LY_SUCCESS;
7037 }
7038
7039 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01007040 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007041 return LY_EVALID;
7042 }
7043
Radek Krejci1deb5be2020-08-26 16:43:36 +02007044 for (uint32_t i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02007045 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007046
7047 /* only attributes of an elem (not dummy) can be in the result, skip all the rest;
7048 * our attributes are always qualified */
Michal Vasko5c4e5892019-11-14 12:31:38 +01007049 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01007050 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007051
7052 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02007053 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007054 continue;
7055 }
7056
Michal Vaskod3678892020-05-21 10:06:58 +02007057 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007058 /* match */
7059 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01007060 set->val.meta[i].meta = sub;
7061 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007062 /* pos does not change */
7063 replaced = 1;
7064 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01007065 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 +02007066 }
7067 ++i;
7068 }
7069 }
7070 }
7071
7072 if (!replaced) {
7073 /* no match */
7074 set_remove_node(set, i);
7075 }
7076 }
7077
7078 return LY_SUCCESS;
7079}
7080
7081/**
7082 * @brief Move context @p set1 to union with @p set2. @p set2 is emptied afterwards.
Michal Vasko61ac2f62020-05-25 12:39:51 +02007083 * Result is LYXP_SET_NODE_SET. Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007084 *
7085 * @param[in,out] set1 Set to use for the result.
7086 * @param[in] set2 Set that is copied to @p set1.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007087 * @return LY_ERR
7088 */
7089static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007090moveto_union(struct lyxp_set *set1, struct lyxp_set *set2)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007091{
7092 LY_ERR rc;
7093
Michal Vaskod3678892020-05-21 10:06:58 +02007094 if ((set1->type != LYXP_SET_NODE_SET) || (set2->type != LYXP_SET_NODE_SET)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01007095 LOGVAL(set1->ctx, LY_VCODE_XP_INOP_2, "union", print_set_type(set1), print_set_type(set2));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007096 return LY_EVALID;
7097 }
7098
7099 /* set2 is empty or both set1 and set2 */
Michal Vaskod3678892020-05-21 10:06:58 +02007100 if (!set2->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007101 return LY_SUCCESS;
7102 }
7103
Michal Vaskod3678892020-05-21 10:06:58 +02007104 if (!set1->used) {
aPiecekadc1e4f2021-10-07 11:15:12 +02007105 /* release hidden allocated data (lyxp_set.size) */
7106 lyxp_set_free_content(set1);
7107 /* direct copying of the entire structure */
Michal Vasko03ff5a72019-09-11 13:49:33 +02007108 memcpy(set1, set2, sizeof *set1);
7109 /* dynamic memory belongs to set1 now, do not free */
Michal Vaskod3678892020-05-21 10:06:58 +02007110 memset(set2, 0, sizeof *set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007111 return LY_SUCCESS;
7112 }
7113
7114 /* we assume sets are sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007115 assert(!set_sort(set1) && !set_sort(set2));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007116
7117 /* sort, remove duplicates */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007118 rc = set_sorted_merge(set1, set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007119 LY_CHECK_RET(rc);
7120
7121 /* final set must be sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007122 assert(!set_sort(set1));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007123
7124 return LY_SUCCESS;
7125}
7126
7127/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02007128 * @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 +02007129 * Context position aware.
7130 *
7131 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02007132 * @param[in] mod Matching metadata module, NULL for any.
7133 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
Michal Vaskocdad7122020-11-09 21:04:44 +01007134 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007135 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7136 */
7137static int
Michal Vaskocdad7122020-11-09 21:04:44 +01007138moveto_attr_alldesc(struct lyxp_set *set, const struct lys_module *mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007139{
Michal Vasko9f96a052020-03-10 09:41:45 +01007140 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007141 struct lyxp_set *set_all_desc = NULL;
7142 LY_ERR rc;
7143
aPiecek8b0cc152021-05-31 16:40:31 +02007144 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007145 return LY_SUCCESS;
7146 }
7147
7148 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01007149 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007150 return LY_EVALID;
7151 }
7152
Michal Vasko03ff5a72019-09-11 13:49:33 +02007153 /* can be optimized similarly to moveto_node_alldesc() and save considerable amount of memory,
7154 * but it likely won't be used much, so it's a waste of time */
7155 /* copy the context */
7156 set_all_desc = set_copy(set);
7157 /* get all descendant nodes (the original context nodes are removed) */
Michal Vasko49fec8e2022-05-24 10:28:33 +02007158 rc = moveto_node_alldesc_child(set_all_desc, NULL, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007159 if (rc != LY_SUCCESS) {
7160 lyxp_set_free(set_all_desc);
7161 return rc;
7162 }
7163 /* prepend the original context nodes */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007164 rc = moveto_union(set, set_all_desc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007165 if (rc != LY_SUCCESS) {
7166 lyxp_set_free(set_all_desc);
7167 return rc;
7168 }
7169 lyxp_set_free(set_all_desc);
7170
Radek Krejci1deb5be2020-08-26 16:43:36 +02007171 for (uint32_t i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02007172 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007173
7174 /* only attributes of an elem can be in the result, skip all the rest,
7175 * we have all attributes qualified in lyd tree */
7176 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01007177 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007178 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02007179 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007180 continue;
7181 }
7182
Michal Vaskod3678892020-05-21 10:06:58 +02007183 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007184 /* match */
7185 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01007186 set->val.meta[i].meta = sub;
7187 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007188 /* pos does not change */
7189 replaced = 1;
7190 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01007191 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 +02007192 }
7193 ++i;
7194 }
7195 }
7196 }
7197
7198 if (!replaced) {
7199 /* no match */
7200 set_remove_node(set, i);
7201 }
7202 }
7203
7204 return LY_SUCCESS;
7205}
7206
7207/**
Michal Vasko8abcecc2022-07-28 09:55:01 +02007208 * @brief Move context @p set1 single item to the result of a comparison.
7209 *
7210 * @param[in] set1 First set with the item to compare.
7211 * @param[in] idx1 Index of the item in @p set1.
7212 * @param[in] set2 Second set.
7213 * @param[in] op Comparison operator to process.
7214 * @param[in] switch_operands Whether to switch sets as operands; whether it is `set1 op set2` or `set2 op set1`.
7215 * @param[out] result Result of the comparison.
7216 * @return LY_ERR value.
7217 */
7218static LY_ERR
7219moveto_op_comp_item(const struct lyxp_set *set1, uint32_t idx1, struct lyxp_set *set2, const char *op,
7220 ly_bool switch_operands, ly_bool *result)
7221{
7222 struct lyxp_set tmp1 = {0};
7223 LY_ERR rc = LY_SUCCESS;
7224
7225 assert(set1->type == LYXP_SET_NODE_SET);
7226
7227 /* cast set1 */
7228 switch (set2->type) {
7229 case LYXP_SET_NUMBER:
7230 rc = set_comp_cast(&tmp1, set1, LYXP_SET_NUMBER, idx1);
7231 break;
7232 case LYXP_SET_BOOLEAN:
7233 rc = set_comp_cast(&tmp1, set1, LYXP_SET_BOOLEAN, idx1);
7234 break;
7235 default:
7236 rc = set_comp_cast(&tmp1, set1, LYXP_SET_STRING, idx1);
7237 break;
7238 }
7239 LY_CHECK_GOTO(rc, cleanup);
7240
7241 /* canonize set2 */
7242 LY_CHECK_GOTO(rc = set_comp_canonize(set2, &set1->val.nodes[idx1]), cleanup);
7243
7244 /* compare recursively and store the result */
7245 if (switch_operands) {
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007246 LY_CHECK_GOTO(rc = moveto_op_comp(set2, &tmp1, op, result), cleanup);
Michal Vasko8abcecc2022-07-28 09:55:01 +02007247 } else {
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007248 LY_CHECK_GOTO(rc = moveto_op_comp(&tmp1, set2, op, result), cleanup);
Michal Vasko8abcecc2022-07-28 09:55:01 +02007249 }
7250
7251cleanup:
7252 lyxp_set_free_content(&tmp1);
7253 return rc;
7254}
7255
7256/**
7257 * @brief Move context @p set1 to the result of a comparison. Handles '=', '!=', '<=', '<', '>=', or '>'.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007258 * Result is LYXP_SET_BOOLEAN. Indirectly context position aware.
7259 *
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007260 * @param[in] set1 Set acting as the first operand for @p op.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007261 * @param[in] set2 Set acting as the second operand for @p op.
7262 * @param[in] op Comparison operator to process.
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007263 * @param[out] result Result of the comparison.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007264 * @return LY_ERR
7265 */
7266static LY_ERR
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007267moveto_op_comp(struct lyxp_set *set1, struct lyxp_set *set2, const char *op, ly_bool *result)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007268{
7269 /*
7270 * NODE SET + NODE SET = NODE SET + STRING /(1 NODE SET) 2 STRING
7271 * NODE SET + STRING = STRING + STRING /1 STRING (2 STRING)
7272 * NODE SET + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
7273 * NODE SET + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
7274 * STRING + NODE SET = STRING + STRING /(1 STRING) 2 STRING
7275 * NUMBER + NODE SET = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
7276 * BOOLEAN + NODE SET = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
7277 *
7278 * '=' or '!='
7279 * BOOLEAN + BOOLEAN
7280 * BOOLEAN + STRING = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
7281 * BOOLEAN + NUMBER = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
7282 * STRING + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
7283 * NUMBER + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
7284 * NUMBER + NUMBER
7285 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
7286 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
7287 * STRING + STRING
7288 *
7289 * '<=', '<', '>=', '>'
7290 * NUMBER + NUMBER
7291 * BOOLEAN + BOOLEAN = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
7292 * BOOLEAN + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
7293 * BOOLEAN + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
7294 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
7295 * STRING + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
7296 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
7297 * NUMBER + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
7298 * STRING + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
7299 */
Michal Vasko8abcecc2022-07-28 09:55:01 +02007300 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007301 LY_ERR rc;
7302
Michal Vasko03ff5a72019-09-11 13:49:33 +02007303 /* iterative evaluation with node-sets */
7304 if ((set1->type == LYXP_SET_NODE_SET) || (set2->type == LYXP_SET_NODE_SET)) {
7305 if (set1->type == LYXP_SET_NODE_SET) {
7306 for (i = 0; i < set1->used; ++i) {
Michal Vasko8abcecc2022-07-28 09:55:01 +02007307 /* evaluate for the single item */
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007308 LY_CHECK_RET(moveto_op_comp_item(set1, i, set2, op, 0, result));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007309
7310 /* lazy evaluation until true */
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007311 if (*result) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007312 return LY_SUCCESS;
7313 }
7314 }
7315 } else {
7316 for (i = 0; i < set2->used; ++i) {
Michal Vasko8abcecc2022-07-28 09:55:01 +02007317 /* evaluate for the single item */
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007318 LY_CHECK_RET(moveto_op_comp_item(set2, i, set1, op, 1, result));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007319
7320 /* lazy evaluation until true */
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007321 if (*result) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007322 return LY_SUCCESS;
7323 }
7324 }
7325 }
7326
Michal Vasko8abcecc2022-07-28 09:55:01 +02007327 /* false for all the nodes */
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007328 *result = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007329 return LY_SUCCESS;
7330 }
7331
7332 /* first convert properly */
7333 if ((op[0] == '=') || (op[0] == '!')) {
7334 if ((set1->type == LYXP_SET_BOOLEAN) || (set2->type == LYXP_SET_BOOLEAN)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007335 lyxp_set_cast(set1, LYXP_SET_BOOLEAN);
7336 lyxp_set_cast(set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007337 } else if ((set1->type == LYXP_SET_NUMBER) || (set2->type == LYXP_SET_NUMBER)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007338 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007339 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007340 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007341 LY_CHECK_RET(rc);
7342 } /* else we have 2 strings */
7343 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007344 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007345 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007346 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007347 LY_CHECK_RET(rc);
7348 }
7349
7350 assert(set1->type == set2->type);
7351
7352 /* compute result */
7353 if (op[0] == '=') {
7354 if (set1->type == LYXP_SET_BOOLEAN) {
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007355 *result = (set1->val.bln == set2->val.bln);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007356 } else if (set1->type == LYXP_SET_NUMBER) {
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007357 *result = (set1->val.num == set2->val.num);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007358 } else {
7359 assert(set1->type == LYXP_SET_STRING);
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007360 *result = strcmp(set1->val.str, set2->val.str) ? 0 : 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007361 }
7362 } else if (op[0] == '!') {
7363 if (set1->type == LYXP_SET_BOOLEAN) {
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007364 *result = (set1->val.bln != set2->val.bln);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007365 } else if (set1->type == LYXP_SET_NUMBER) {
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007366 *result = (set1->val.num != set2->val.num);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007367 } else {
7368 assert(set1->type == LYXP_SET_STRING);
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007369 *result = strcmp(set1->val.str, set2->val.str) ? 1 : 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007370 }
7371 } else {
7372 assert(set1->type == LYXP_SET_NUMBER);
7373 if (op[0] == '<') {
7374 if (op[1] == '=') {
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007375 *result = (set1->val.num <= set2->val.num);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007376 } else {
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007377 *result = (set1->val.num < set2->val.num);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007378 }
7379 } else {
7380 if (op[1] == '=') {
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007381 *result = (set1->val.num >= set2->val.num);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007382 } else {
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007383 *result = (set1->val.num > set2->val.num);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007384 }
7385 }
7386 }
7387
Michal Vasko03ff5a72019-09-11 13:49:33 +02007388 return LY_SUCCESS;
7389}
7390
7391/**
7392 * @brief Move context @p set to the result of a basic operation. Handles '+', '-', unary '-', '*', 'div',
7393 * or 'mod'. Result is LYXP_SET_NUMBER. Indirectly context position aware.
7394 *
7395 * @param[in,out] set1 Set to use for the result.
7396 * @param[in] set2 Set acting as the second operand for @p op.
7397 * @param[in] op Operator to process.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007398 * @return LY_ERR
7399 */
7400static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007401moveto_op_math(struct lyxp_set *set1, struct lyxp_set *set2, const char *op)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007402{
7403 LY_ERR rc;
7404
7405 /* unary '-' */
7406 if (!set2 && (op[0] == '-')) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007407 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007408 LY_CHECK_RET(rc);
7409 set1->val.num *= -1;
7410 lyxp_set_free(set2);
7411 return LY_SUCCESS;
7412 }
7413
7414 assert(set1 && set2);
7415
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007416 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007417 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007418 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007419 LY_CHECK_RET(rc);
7420
7421 switch (op[0]) {
7422 /* '+' */
7423 case '+':
7424 set1->val.num += set2->val.num;
7425 break;
7426
7427 /* '-' */
7428 case '-':
7429 set1->val.num -= set2->val.num;
7430 break;
7431
7432 /* '*' */
7433 case '*':
7434 set1->val.num *= set2->val.num;
7435 break;
7436
7437 /* 'div' */
7438 case 'd':
7439 set1->val.num /= set2->val.num;
7440 break;
7441
7442 /* 'mod' */
7443 case 'm':
7444 set1->val.num = ((long long)set1->val.num) % ((long long)set2->val.num);
7445 break;
7446
7447 default:
7448 LOGINT_RET(set1->ctx);
7449 }
7450
7451 return LY_SUCCESS;
7452}
7453
Michal Vasko03ff5a72019-09-11 13:49:33 +02007454/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02007455 * @brief Evaluate Predicate. Logs directly on error.
7456 *
Michal Vaskod3678892020-05-21 10:06:58 +02007457 * [9] Predicate ::= '[' Expr ']'
Michal Vasko03ff5a72019-09-11 13:49:33 +02007458 *
7459 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007460 * @param[in] tok_idx Position in the expression @p exp.
aPiecek8b0cc152021-05-31 16:40:31 +02007461 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007462 * @param[in] options XPath options.
Michal Vasko49fec8e2022-05-24 10:28:33 +02007463 * @param[in] axis Axis to search on.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007464 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7465 */
7466static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02007467eval_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 +02007468{
7469 LY_ERR rc;
Michal Vaskodd528af2022-08-08 14:35:07 +02007470 uint32_t i, orig_exp, orig_pos, orig_size;
Michal Vasko5c4e5892019-11-14 12:31:38 +01007471 int32_t pred_in_ctx;
Michal Vasko88a9e802022-05-24 10:50:28 +02007472 ly_bool reverse_axis = 0;
aPiecekfff4dca2021-10-07 10:59:53 +02007473 struct lyxp_set set2 = {0};
Michal Vasko03ff5a72019-09-11 13:49:33 +02007474
7475 /* '[' */
aPiecek8b0cc152021-05-31 16:40:31 +02007476 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02007477 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007478 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007479
aPiecek8b0cc152021-05-31 16:40:31 +02007480 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007481only_parse:
aPiecek8b0cc152021-05-31 16:40:31 +02007482 rc = eval_expr_select(exp, tok_idx, 0, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007483 LY_CHECK_RET(rc);
7484 } else if (set->type == LYXP_SET_NODE_SET) {
7485 /* 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 +01007486 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007487
7488 /* empty set, nothing to evaluate */
7489 if (!set->used) {
7490 goto only_parse;
7491 }
7492
Michal Vasko49fec8e2022-05-24 10:28:33 +02007493 /* decide forward or reverse axis */
7494 switch (axis) {
7495 case LYXP_AXIS_ANCESTOR:
7496 case LYXP_AXIS_ANCESTOR_OR_SELF:
7497 case LYXP_AXIS_PRECEDING:
7498 case LYXP_AXIS_PRECEDING_SIBLING:
7499 reverse_axis = 1;
7500 break;
7501 case LYXP_AXIS_DESCENDANT:
7502 case LYXP_AXIS_DESCENDANT_OR_SELF:
7503 case LYXP_AXIS_FOLLOWING:
7504 case LYXP_AXIS_FOLLOWING_SIBLING:
7505 case LYXP_AXIS_PARENT:
7506 case LYXP_AXIS_CHILD:
7507 case LYXP_AXIS_SELF:
7508 case LYXP_AXIS_ATTRIBUTE:
7509 reverse_axis = 0;
7510 break;
7511 }
7512
Michal Vasko004d3152020-06-11 19:59:22 +02007513 orig_exp = *tok_idx;
Michal Vasko49fec8e2022-05-24 10:28:33 +02007514 orig_pos = reverse_axis ? set->used + 1 : 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007515 orig_size = set->used;
Michal Vasko39dbf352020-05-21 10:08:59 +02007516 for (i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007517 set_init(&set2, set);
7518 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 +02007519
7520 /* remember the node context position for position() and context size for last() */
7521 orig_pos += reverse_axis ? -1 : 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007522
7523 set2.ctx_pos = orig_pos;
7524 set2.ctx_size = orig_size;
Michal Vasko004d3152020-06-11 19:59:22 +02007525 *tok_idx = orig_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007526
Michal Vasko004d3152020-06-11 19:59:22 +02007527 rc = eval_expr_select(exp, tok_idx, 0, &set2, options);
Michal Vaskob7ba2c92024-01-05 15:17:53 +01007528 if (!rc && set2.not_found) {
7529 set->not_found = 1;
7530 break;
7531 }
7532 if (rc) {
Michal Vaskod3678892020-05-21 10:06:58 +02007533 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007534 return rc;
7535 }
7536
Michal Vasko49fec8e2022-05-24 10:28:33 +02007537 /* number is a proximity position */
Michal Vasko03ff5a72019-09-11 13:49:33 +02007538 if (set2.type == LYXP_SET_NUMBER) {
7539 if ((long long)set2.val.num == orig_pos) {
7540 set2.val.num = 1;
7541 } else {
7542 set2.val.num = 0;
7543 }
7544 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007545 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007546
7547 /* predicate satisfied or not? */
Michal Vasko004d3152020-06-11 19:59:22 +02007548 if (!set2.val.bln) {
Michal Vasko2caefc12019-11-14 16:07:56 +01007549 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007550 }
7551 }
Michal Vasko2caefc12019-11-14 16:07:56 +01007552 set_remove_nodes_none(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007553
7554 } else if (set->type == LYXP_SET_SCNODE_SET) {
7555 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01007556 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007557 /* there is a currently-valid node */
7558 break;
7559 }
7560 }
7561 /* empty set, nothing to evaluate */
7562 if (i == set->used) {
7563 goto only_parse;
7564 }
7565
Michal Vasko004d3152020-06-11 19:59:22 +02007566 orig_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007567
Michal Vasko03ff5a72019-09-11 13:49:33 +02007568 /* set special in_ctx to all the valid snodes */
7569 pred_in_ctx = set_scnode_new_in_ctx(set);
7570
7571 /* use the valid snodes one-by-one */
7572 for (i = 0; i < set->used; ++i) {
7573 if (set->val.scnodes[i].in_ctx != pred_in_ctx) {
7574 continue;
7575 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01007576 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007577
Michal Vasko004d3152020-06-11 19:59:22 +02007578 *tok_idx = orig_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007579
Michal Vasko004d3152020-06-11 19:59:22 +02007580 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vaskob7ba2c92024-01-05 15:17:53 +01007581 if (!rc && set->not_found) {
7582 break;
7583 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02007584 LY_CHECK_RET(rc);
7585
7586 set->val.scnodes[i].in_ctx = pred_in_ctx;
7587 }
7588
7589 /* restore the state as it was before the predicate */
7590 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01007591 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007592 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007593 } else if (set->val.scnodes[i].in_ctx == pred_in_ctx) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01007594 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007595 }
7596 }
7597
7598 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02007599 set2.type = LYXP_SET_NODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007600 set_fill_set(&set2, set);
7601
Michal Vasko004d3152020-06-11 19:59:22 +02007602 rc = eval_expr_select(exp, tok_idx, 0, &set2, options);
Michal Vaskob7ba2c92024-01-05 15:17:53 +01007603 if (rc) {
Michal Vaskod3678892020-05-21 10:06:58 +02007604 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007605 return rc;
7606 }
7607
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007608 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +02007609 if (!set2.val.bln) {
Michal Vaskod3678892020-05-21 10:06:58 +02007610 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007611 }
Michal Vaskod3678892020-05-21 10:06:58 +02007612 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007613 }
7614
7615 /* ']' */
Michal Vasko004d3152020-06-11 19:59:22 +02007616 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_BRACK2);
aPiecek8b0cc152021-05-31 16:40:31 +02007617 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02007618 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007619 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007620
7621 return LY_SUCCESS;
7622}
7623
7624/**
Michal Vaskod3678892020-05-21 10:06:58 +02007625 * @brief Evaluate Literal. Logs directly on error.
7626 *
7627 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007628 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02007629 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7630 */
7631static void
Michal Vaskodd528af2022-08-08 14:35:07 +02007632eval_literal(const struct lyxp_expr *exp, uint32_t *tok_idx, struct lyxp_set *set)
Michal Vaskod3678892020-05-21 10:06:58 +02007633{
7634 if (set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007635 if (exp->tok_len[*tok_idx] == 2) {
Michal Vaskod3678892020-05-21 10:06:58 +02007636 set_fill_string(set, "", 0);
7637 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007638 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 +02007639 }
7640 }
7641 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02007642 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007643 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007644}
7645
7646/**
Michal Vasko3d969ff2022-07-29 15:02:08 +02007647 * @brief Check that a nametest in a predicate matches a key node.
7648 *
7649 * @param[in] nametest Nametest to check.
7650 * @param[in] len Length of @p nametest.
7651 * @param[in] ctx_scnode Found schema node as the context for the predicate.
7652 * @param[in] set Context set.
7653 * @param[in] key Expected key node.
7654 * @return LY_SUCCESS on success,
7655 * @return LY_ENOT if a predicate could not be compiled.
7656 * @return LY_ERR on any error.
7657 */
7658static LY_ERR
7659eval_name_test_try_compile_predicate_key(const char *nametest, uint32_t len, const struct lysc_node *ctx_scnode,
7660 const struct lyxp_set *set, const struct lysc_node *key)
7661{
7662 const struct lys_module *mod;
7663
7664 /* prefix (module) */
7665 LY_CHECK_RET(moveto_resolve_model(&nametest, &len, set, ctx_scnode, &mod));
7666 if (mod != key->module) {
7667 return LY_ENOT;
7668 }
7669
7670 /* node name */
7671 if (ly_strncmp(key->name, nametest, len)) {
7672 return LY_ENOT;
7673 }
7674
7675 return LY_SUCCESS;
7676}
7677
7678/**
7679 * @brief Append a simple predicate for the node.
7680 *
7681 * @param[in] exp Full parsed XPath expression.
7682 * @param[in] tok_idx Predicate start index in @p exp.
7683 * @param[in] end_tok_idx Predicate end index in @p exp.
7684 * @param[in] ctx_scnode Found schema node as the context for the predicate.
7685 * @param[in] set Context set.
7686 * @param[in] pred_node Node with the value referenced in the predicate.
7687 * @param[in,out] pred Predicate to append to.
7688 * @param[in,out] pred_len Length of @p pred, is updated.
7689 * @return LY_SUCCESS on success,
7690 * @return LY_ENOT if a predicate could not be compiled.
7691 * @return LY_ERR on any error.
7692 */
7693static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02007694eval_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 +02007695 const struct lysc_node *ctx_scnode, const struct lyxp_set *set, const struct lysc_node *pred_node, char **pred,
7696 uint32_t *pred_len)
7697{
7698 LY_ERR rc = LY_SUCCESS;
7699 uint32_t i;
7700 const struct lyd_node *siblings;
7701 struct lyd_node *ctx_node;
Michal Vasko5fb92a22022-08-02 09:21:37 +02007702 const struct lysc_node *sparent, *cur_scnode;
Michal Vasko3d969ff2022-07-29 15:02:08 +02007703 struct lyxp_expr *val_exp = NULL;
7704 struct lyxp_set set2 = {0};
7705 char quot;
7706
7707 /* duplicate the value expression */
7708 LY_CHECK_GOTO(rc = lyxp_expr_dup(set->ctx, exp, tok_idx, end_tok_idx, &val_exp), cleanup);
7709
7710 /* get its atoms */
Michal Vasko5fb92a22022-08-02 09:21:37 +02007711 cur_scnode = set->cur_node ? set->cur_node->schema : NULL;
7712 LY_CHECK_GOTO(rc = lyxp_atomize(set->ctx, val_exp, set->cur_mod, set->format, set->prefix_data, cur_scnode,
7713 ctx_scnode, &set2, LYXP_SCNODE), cleanup);
Michal Vasko3d969ff2022-07-29 15:02:08 +02007714
7715 /* check whether we can compile a single predicate (evaluation result value is always the same) */
7716 for (i = 0; i < set2.used; ++i) {
7717 if ((set2.val.scnodes[i].type != LYXP_NODE_ELEM) || (set2.val.scnodes[i].in_ctx < LYXP_SET_SCNODE_ATOM_NODE)) {
7718 /* skip root and context node */
7719 continue;
7720 }
7721
Michal Vasko5fb92a22022-08-02 09:21:37 +02007722 /* 1) context node descendants are traversed - do best-effort detection of the value dependency on the
7723 * context node instance */
7724 if ((set2.val.scnodes[i].axis == LYXP_AXIS_CHILD) && (set2.val.scnodes[i].scnode->parent == ctx_scnode)) {
7725 /* 1.1) context node child was accessed on the child axis, certain dependency */
Michal Vasko3d969ff2022-07-29 15:02:08 +02007726 rc = LY_ENOT;
7727 goto cleanup;
7728 }
Michal Vasko5fb92a22022-08-02 09:21:37 +02007729 if ((set2.val.scnodes[i].axis == LYXP_AXIS_DESCENDANT) || (set2.val.scnodes[i].axis == LYXP_AXIS_DESCENDANT_OR_SELF)) {
7730 for (sparent = set2.val.scnodes[i].scnode->parent; sparent && (sparent != ctx_scnode); sparent = sparent->parent) {}
7731 if (sparent) {
7732 /* 1.2) context node descendant was accessed on the descendant axis, probable dependency */
7733 rc = LY_ENOT;
7734 goto cleanup;
7735 }
7736 }
Michal Vasko3d969ff2022-07-29 15:02:08 +02007737
Michal Vasko5fb92a22022-08-02 09:21:37 +02007738 /* 2) multi-instance nodes (list or leaf-list) are traversed - all the instances need to be considered,
7739 * but the current node can be safely ignored, it is always the same data instance */
7740 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 +02007741 rc = LY_ENOT;
7742 goto cleanup;
7743 }
Michal Vasko3d969ff2022-07-29 15:02:08 +02007744 }
7745
7746 /* get any data instance of the context node, we checked it makes no difference */
7747 siblings = set->val.nodes[0].node ? lyd_child(set->val.nodes[0].node) : set->tree;
7748 LY_CHECK_GOTO(rc = lyd_find_sibling_schema(siblings, ctx_scnode, &ctx_node), cleanup);
7749
7750 /* evaluate the value subexpression with the root context node */
7751 lyxp_set_free_content(&set2);
7752 LY_CHECK_GOTO(rc = lyxp_eval(set->ctx, val_exp, set->cur_mod, set->format, set->prefix_data, set->cur_node,
7753 ctx_node, set->tree, NULL, &set2, 0), cleanup);
7754
7755 /* cast it into a string */
7756 LY_CHECK_GOTO(rc = lyxp_set_cast(&set2, LYXP_SET_STRING), cleanup);
7757
7758 /* append the JSON predicate */
7759 *pred = ly_realloc(*pred, *pred_len + 1 + strlen(pred_node->name) + 2 + strlen(set2.val.str) + 3);
7760 LY_CHECK_ERR_GOTO(!*pred, LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
7761 quot = strchr(set2.val.str, '\'') ? '\"' : '\'';
7762 *pred_len += sprintf(*pred + *pred_len, "[%s=%c%s%c]", pred_node->name, quot, set2.val.str, quot);
7763
7764cleanup:
7765 lyxp_expr_free(set->ctx, val_exp);
7766 lyxp_set_free_content(&set2);
7767 return rc;
7768}
7769
7770/**
Michal Vasko004d3152020-06-11 19:59:22 +02007771 * @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 +02007772 *
Michal Vasko004d3152020-06-11 19:59:22 +02007773 * @param[in] exp Full parsed XPath expression.
7774 * @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 +02007775 * @param[in] ctx_scnode Found schema node as the context for the predicate.
7776 * @param[in] set Context set.
Michal Vasko004d3152020-06-11 19:59:22 +02007777 * @param[out] predicates Parsed predicates.
Michal Vasko004d3152020-06-11 19:59:22 +02007778 * @return LY_SUCCESS on success,
Michal Vasko3d969ff2022-07-29 15:02:08 +02007779 * @return LY_ENOT if a predicate could not be compiled.
Michal Vasko004d3152020-06-11 19:59:22 +02007780 * @return LY_ERR on any error.
Michal Vaskod3678892020-05-21 10:06:58 +02007781 */
7782static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02007783eval_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 +01007784 const struct lyxp_set *set, struct ly_path_predicate **predicates)
Michal Vaskod3678892020-05-21 10:06:58 +02007785{
Michal Vasko3d969ff2022-07-29 15:02:08 +02007786 LY_ERR rc = LY_SUCCESS;
Michal Vaskod4a6d042022-12-08 08:34:29 +01007787 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 +02007788 const struct lysc_node *key;
Michal Vasko3d969ff2022-07-29 15:02:08 +02007789 char *pred = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02007790 struct lyxp_expr *exp2 = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02007791
Michal Vasko3d969ff2022-07-29 15:02:08 +02007792 assert(ctx_scnode->nodetype & (LYS_LIST | LYS_LEAFLIST));
Michal Vaskod3678892020-05-21 10:06:58 +02007793
Michal Vasko3d969ff2022-07-29 15:02:08 +02007794 /* turn logging off */
Michal Vaskod4a6d042022-12-08 08:34:29 +01007795 ly_temp_log_options(&temp_lo);
Michal Vasko3d969ff2022-07-29 15:02:08 +02007796
7797 if (ctx_scnode->nodetype == LYS_LIST) {
7798 /* check for predicates "[key1=...][key2=...]..." */
7799
Michal Vasko004d3152020-06-11 19:59:22 +02007800 /* get key count */
Michal Vasko3d969ff2022-07-29 15:02:08 +02007801 if (ctx_scnode->flags & LYS_KEYLESS) {
7802 rc = LY_ENOT;
7803 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +02007804 }
Michal Vaskod3678892020-05-21 10:06:58 +02007805
Michal Vasko004d3152020-06-11 19:59:22 +02007806 /* learn where the predicates end */
7807 e_idx = *tok_idx;
Michal Vasko3d969ff2022-07-29 15:02:08 +02007808 for (key = lysc_node_child(ctx_scnode); key && (key->flags & LYS_KEY); key = key->next) {
Michal Vasko004d3152020-06-11 19:59:22 +02007809 /* '[' */
7810 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
Michal Vasko3d969ff2022-07-29 15:02:08 +02007811 rc = LY_ENOT;
7812 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +02007813 }
7814 ++e_idx;
7815
Michal Vasko3354d272021-04-06 09:40:06 +02007816 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_NAMETEST)) {
Michal Vasko3d969ff2022-07-29 15:02:08 +02007817 /* not a key */
7818 rc = LY_ENOT;
7819 goto cleanup;
Michal Vasko3354d272021-04-06 09:40:06 +02007820 }
7821
Michal Vasko3d969ff2022-07-29 15:02:08 +02007822 /* check key */
7823 LY_CHECK_GOTO(rc = eval_name_test_try_compile_predicate_key(exp->expr + exp->tok_pos[e_idx],
7824 exp->tok_len[e_idx], ctx_scnode, set, key), cleanup);
7825
7826 ++e_idx;
7827
7828 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_OPER_EQUAL)) {
7829 /* not '=' */
7830 rc = LY_ENOT;
7831 goto cleanup;
7832 }
7833 ++e_idx;
7834
7835 /* value start */
7836 val_start_idx = e_idx;
7837
Michal Vasko004d3152020-06-11 19:59:22 +02007838 /* ']' */
Michal Vasko3ef7e252022-11-16 08:29:49 +01007839 nested_pred = 1;
7840 do {
7841 ++e_idx;
7842
7843 if ((nested_pred == 1) && !lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_OPER_LOG)) {
Michal Vaskob8ee3562022-08-02 10:43:17 +02007844 /* higher priority than '=' */
7845 rc = LY_ENOT;
7846 goto cleanup;
Michal Vasko3ef7e252022-11-16 08:29:49 +01007847 } else if (!lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
7848 /* nested predicate */
7849 ++nested_pred;
7850 } else if (!lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) {
7851 /* predicate end */
7852 --nested_pred;
Michal Vaskob8ee3562022-08-02 10:43:17 +02007853 }
Michal Vasko3ef7e252022-11-16 08:29:49 +01007854 } while (nested_pred);
Michal Vasko004d3152020-06-11 19:59:22 +02007855
Michal Vasko3d969ff2022-07-29 15:02:08 +02007856 /* try to evaluate the value */
7857 LY_CHECK_GOTO(rc = eval_name_test_try_compile_predicate_append(exp, val_start_idx, e_idx - 1, ctx_scnode,
7858 set, key, &pred, &pred_len), cleanup);
7859
7860 ++e_idx;
Michal Vasko004d3152020-06-11 19:59:22 +02007861 }
Michal Vasko004d3152020-06-11 19:59:22 +02007862 } else {
Michal Vasko3d969ff2022-07-29 15:02:08 +02007863 /* check for predicate "[.=...]" */
7864
Michal Vasko004d3152020-06-11 19:59:22 +02007865 /* learn just where this single predicate ends */
7866 e_idx = *tok_idx;
7867
Michal Vaskod3678892020-05-21 10:06:58 +02007868 /* '[' */
Michal Vasko004d3152020-06-11 19:59:22 +02007869 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
Michal Vasko3d969ff2022-07-29 15:02:08 +02007870 rc = LY_ENOT;
7871 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +02007872 }
7873 ++e_idx;
Michal Vaskod3678892020-05-21 10:06:58 +02007874
Michal Vasko3354d272021-04-06 09:40:06 +02007875 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_DOT)) {
Michal Vasko3d969ff2022-07-29 15:02:08 +02007876 /* not the node value */
7877 rc = LY_ENOT;
7878 goto cleanup;
Michal Vasko3354d272021-04-06 09:40:06 +02007879 }
Michal Vasko3d969ff2022-07-29 15:02:08 +02007880 ++e_idx;
7881
7882 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_OPER_EQUAL)) {
7883 /* not '=' */
7884 rc = LY_ENOT;
7885 goto cleanup;
7886 }
7887 ++e_idx;
7888
7889 /* value start */
7890 val_start_idx = e_idx;
Michal Vasko3354d272021-04-06 09:40:06 +02007891
Michal Vaskod3678892020-05-21 10:06:58 +02007892 /* ']' */
Michal Vasko31f19632022-11-16 09:38:40 +01007893 nested_pred = 1;
7894 do {
7895 ++e_idx;
7896
7897 if ((nested_pred == 1) && !lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_OPER_LOG)) {
Michal Vaskob8ee3562022-08-02 10:43:17 +02007898 /* higher priority than '=' */
7899 rc = LY_ENOT;
7900 goto cleanup;
Michal Vasko31f19632022-11-16 09:38:40 +01007901 } else if (!lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
7902 /* nested predicate */
7903 ++nested_pred;
7904 } else if (!lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) {
7905 /* predicate end */
7906 --nested_pred;
Michal Vaskob8ee3562022-08-02 10:43:17 +02007907 }
Michal Vasko31f19632022-11-16 09:38:40 +01007908 } while (nested_pred);
Michal Vasko3d969ff2022-07-29 15:02:08 +02007909
7910 /* try to evaluate the value */
7911 LY_CHECK_GOTO(rc = eval_name_test_try_compile_predicate_append(exp, val_start_idx, e_idx - 1, ctx_scnode, set,
7912 ctx_scnode, &pred, &pred_len), cleanup);
7913
Michal Vasko004d3152020-06-11 19:59:22 +02007914 ++e_idx;
Michal Vaskod3678892020-05-21 10:06:58 +02007915 }
7916
Michal Vasko004d3152020-06-11 19:59:22 +02007917 /* parse the predicate(s) */
Michal Vasko3d969ff2022-07-29 15:02:08 +02007918 LY_CHECK_GOTO(rc = ly_path_parse_predicate(set->ctx, ctx_scnode, pred, pred_len, LY_PATH_PREFIX_OPTIONAL,
7919 LY_PATH_PRED_SIMPLE, &exp2), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02007920
7921 /* compile */
Michal Vasko3d969ff2022-07-29 15:02:08 +02007922 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 +01007923 &pred_idx, LY_VALUE_JSON, NULL, predicates);
Michal Vasko3d969ff2022-07-29 15:02:08 +02007924 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02007925
7926 /* success, the predicate must include all the needed information for hash-based search */
7927 *tok_idx = e_idx;
7928
7929cleanup:
Michal Vaskod4a6d042022-12-08 08:34:29 +01007930 ly_temp_log_options(NULL);
Michal Vasko3d969ff2022-07-29 15:02:08 +02007931 lyxp_expr_free(set->ctx, exp2);
7932 free(pred);
7933 return rc;
Michal Vaskod3678892020-05-21 10:06:58 +02007934}
7935
7936/**
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007937 * @brief Search for/check the next schema node that could be the only matching schema node meaning the
7938 * data node(s) could be found using a single hash-based search.
7939 *
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007940 * @param[in] ctx libyang context.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007941 * @param[in] node Next context node to check.
7942 * @param[in] name Expected node name.
7943 * @param[in] name_len Length of @p name.
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007944 * @param[in] moveto_mod Expected node module, can be NULL for JSON format with no prefix.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007945 * @param[in] root_type XPath root type.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007946 * @param[in] format Prefix format.
7947 * @param[in,out] found Previously found node, is updated.
7948 * @return LY_SUCCESS on success,
7949 * @return LY_ENOT if the whole check failed and hashes cannot be used.
7950 */
7951static LY_ERR
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007952eval_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 +02007953 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 +02007954 const struct lysc_node **found)
7955{
Michal Vaskoc7304482023-11-30 15:59:30 +01007956 const struct lysc_node *scnode, *scnode2;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007957 const struct lys_module *mod;
7958 uint32_t idx = 0;
7959
Radek Krejci8df109d2021-04-23 12:19:08 +02007960 assert((format == LY_VALUE_JSON) || moveto_mod);
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007961
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007962continue_search:
Michal Vasko7d1d0912020-10-16 08:38:30 +02007963 scnode = NULL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007964 if (!node) {
Radek Krejci8df109d2021-04-23 12:19:08 +02007965 if ((format == LY_VALUE_JSON) && !moveto_mod) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007966 /* search all modules for a single match */
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007967 while ((mod = ly_ctx_get_module_iter(ctx, &idx))) {
Michal Vasko35a3b1d2021-07-14 09:34:16 +02007968 if (!mod->implemented) {
7969 continue;
7970 }
7971
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007972 scnode = lys_find_child(NULL, mod, name, name_len, 0, 0);
7973 if (scnode) {
7974 /* we have found a match */
7975 break;
7976 }
7977 }
7978
Michal Vasko7d1d0912020-10-16 08:38:30 +02007979 if (!scnode) {
7980 /* all modules searched */
7981 idx = 0;
7982 }
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007983 } else {
7984 /* search in top-level */
7985 scnode = lys_find_child(NULL, moveto_mod, name, name_len, 0, 0);
7986 }
7987 } else if (!*found || (lysc_data_parent(*found) != node->schema)) {
Radek Krejci8df109d2021-04-23 12:19:08 +02007988 if ((format == LY_VALUE_JSON) && !moveto_mod) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007989 /* we must adjust the module to inherit the one from the context node */
7990 moveto_mod = node->schema->module;
7991 }
7992
7993 /* search in children, do not repeat the same search */
Michal Vaskoc7304482023-11-30 15:59:30 +01007994 if (node->schema->nodetype & (LYS_RPC | LYS_ACTION)) {
7995 /* make sure the node is unique, whether in input or output */
7996 scnode = lys_find_child(node->schema, moveto_mod, name, name_len, 0, 0);
7997 scnode2 = lys_find_child(node->schema, moveto_mod, name, name_len, 0, LYS_GETNEXT_OUTPUT);
7998 if (scnode && scnode2) {
7999 /* conflict, do not use hashes */
8000 scnode = NULL;
8001 } else if (scnode2) {
8002 scnode = scnode2;
8003 }
8004 } else {
8005 scnode = lys_find_child(node->schema, moveto_mod, name, name_len, 0, 0);
8006 }
Michal Vasko7d1d0912020-10-16 08:38:30 +02008007 } /* else skip redundant search */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008008
8009 /* additional context check */
8010 if (scnode && (root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) {
8011 scnode = NULL;
8012 }
8013
8014 if (scnode) {
8015 if (*found) {
8016 /* we found a schema node with the same name but at different level, give up, too complicated
8017 * (more hash-based searches would be required, not supported) */
8018 return LY_ENOT;
8019 } else {
8020 /* remember the found schema node and continue to make sure it can be used */
8021 *found = scnode;
8022 }
8023 }
8024
8025 if (idx) {
8026 /* continue searching all the following models */
8027 goto continue_search;
8028 }
8029
8030 return LY_SUCCESS;
8031}
8032
8033/**
Michal Vasko4ad69e72021-10-26 16:25:55 +02008034 * @brief Generate message when no matching schema nodes were found for a path segment.
8035 *
8036 * @param[in] set XPath set.
8037 * @param[in] scparent Previous schema parent in the context, if only one.
8038 * @param[in] ncname XPath NCName being evaluated.
8039 * @param[in] ncname_len Length of @p ncname.
8040 * @param[in] expr Whole XPath expression.
8041 * @param[in] options XPath options.
8042 */
8043static void
8044eval_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 +02008045 uint32_t ncname_len, const char *expr, uint32_t options)
Michal Vasko4ad69e72021-10-26 16:25:55 +02008046{
8047 const char *format;
8048 char *path = NULL, *ppath = NULL;
8049
8050 path = lysc_path(set->cur_scnode, LYSC_PATH_LOG, NULL, 0);
8051 if (scparent) {
8052 /* generate path for the parent */
8053 if (scparent->type == LYXP_NODE_ELEM) {
8054 ppath = lysc_path(scparent->scnode, LYSC_PATH_LOG, NULL, 0);
8055 } else if (scparent->type == LYXP_NODE_ROOT) {
8056 ppath = strdup("<root>");
8057 } else if (scparent->type == LYXP_NODE_ROOT_CONFIG) {
8058 ppath = strdup("<config-root>");
8059 }
8060 }
8061 if (ppath) {
8062 format = "Schema node \"%.*s\" for parent \"%s\" not found; in expr \"%.*s\" with context node \"%s\".";
8063 if (options & LYXP_SCNODE_ERROR) {
Michal Vasko8d9889c2023-11-22 12:28:57 +01008064 LOGERR(set->ctx, LY_ENOTFOUND, format, ncname_len, ncname, ppath, (ncname - expr) + ncname_len, expr, path);
Michal Vasko4ad69e72021-10-26 16:25:55 +02008065 } else {
8066 LOGWRN(set->ctx, format, ncname_len, ncname, ppath, (ncname - expr) + ncname_len, expr, path);
8067 }
8068 } else {
8069 format = "Schema node \"%.*s\" not found; in expr \"%.*s\" with context node \"%s\".";
8070 if (options & LYXP_SCNODE_ERROR) {
Michal Vasko8d9889c2023-11-22 12:28:57 +01008071 LOGERR(set->ctx, LY_ENOTFOUND, format, ncname_len, ncname, (ncname - expr) + ncname_len, expr, path);
Michal Vasko4ad69e72021-10-26 16:25:55 +02008072 } else {
8073 LOGWRN(set->ctx, format, ncname_len, ncname, (ncname - expr) + ncname_len, expr, path);
8074 }
8075 }
8076 free(path);
8077 free(ppath);
8078}
8079
8080/**
Michal Vaskod3678892020-05-21 10:06:58 +02008081 * @brief Evaluate NameTest and any following Predicates. Logs directly on error.
8082 *
8083 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
8084 * [6] NodeTest ::= NameTest | NodeType '(' ')'
8085 * [7] NameTest ::= '*' | NCName ':' '*' | QName
8086 *
8087 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008088 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko49fec8e2022-05-24 10:28:33 +02008089 * @param[in] axis What axis to search on.
Michal Vaskod3678892020-05-21 10:06:58 +02008090 * @param[in] all_desc Whether to search all the descendants or children only.
aPiecek8b0cc152021-05-31 16:40:31 +02008091 * @param[in,out] set Context and result set.
Michal Vaskod3678892020-05-21 10:06:58 +02008092 * @param[in] options XPath options.
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008093 * @return LY_ERR (LY_EINCOMPLETE on unresolved when, LY_ENOT for not found schema node)
Michal Vaskod3678892020-05-21 10:06:58 +02008094 */
8095static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02008096eval_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 +02008097 struct lyxp_set *set, uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02008098{
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008099 LY_ERR rc = LY_SUCCESS, r;
Michal Vasko004d3152020-06-11 19:59:22 +02008100 const char *ncname, *ncname_dict = NULL;
Michal Vasko56c008c2022-07-29 14:57:47 +02008101 uint32_t i, ncname_len;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008102 const struct lys_module *moveto_mod = NULL;
Michal Vaskoc71c37b2021-01-11 13:40:02 +01008103 const struct lysc_node *scnode = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02008104 struct ly_path_predicate *predicates = NULL;
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008105 int scnode_skip_pred = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02008106
aPiecek8b0cc152021-05-31 16:40:31 +02008107 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008108 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008109 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02008110
aPiecek8b0cc152021-05-31 16:40:31 +02008111 if (options & LYXP_SKIP_EXPR) {
Michal Vaskod3678892020-05-21 10:06:58 +02008112 goto moveto;
8113 }
8114
Michal Vasko004d3152020-06-11 19:59:22 +02008115 ncname = &exp->expr[exp->tok_pos[*tok_idx - 1]];
8116 ncname_len = exp->tok_len[*tok_idx - 1];
Michal Vaskod3678892020-05-21 10:06:58 +02008117
Michal Vaskoc71c37b2021-01-11 13:40:02 +01008118 if ((ncname[0] == '*') && (ncname_len == 1)) {
8119 /* all nodes will match */
8120 goto moveto;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008121 }
8122
Michal Vaskoc71c37b2021-01-11 13:40:02 +01008123 /* parse (and skip) module name */
8124 rc = moveto_resolve_model(&ncname, &ncname_len, set, NULL, &moveto_mod);
Michal Vaskod3678892020-05-21 10:06:58 +02008125 LY_CHECK_GOTO(rc, cleanup);
8126
Michal Vasko49fec8e2022-05-24 10:28:33 +02008127 if ((ncname[0] == '*') && (ncname_len == 1)) {
8128 /* all nodes from the module will match */
8129 goto moveto;
8130 }
8131
8132 if (((set->format == LY_VALUE_JSON) || moveto_mod) && (axis == LYXP_AXIS_CHILD) && !all_desc &&
8133 (set->type == LYXP_SET_NODE_SET)) {
Michal Vaskod3678892020-05-21 10:06:58 +02008134 /* find the matching schema node in some parent in the context */
Michal Vasko56c008c2022-07-29 14:57:47 +02008135 for (i = 0; i < set->used; ++i) {
Michal Vaskoc71c37b2021-01-11 13:40:02 +01008136 if (eval_name_test_with_predicate_get_scnode(set->ctx, set->val.nodes[i].node, ncname, ncname_len,
8137 moveto_mod, set->root_type, set->format, &scnode)) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008138 /* check failed */
8139 scnode = NULL;
8140 break;
Michal Vaskod3678892020-05-21 10:06:58 +02008141 }
8142 }
8143
Michal Vasko004d3152020-06-11 19:59:22 +02008144 if (scnode && (scnode->nodetype & (LYS_LIST | LYS_LEAFLIST))) {
8145 /* try to create the predicates */
Michal Vasko90189962023-02-28 12:10:34 +01008146 if (eval_name_test_try_compile_predicates(exp, tok_idx, scnode, set, &predicates)) {
Michal Vasko004d3152020-06-11 19:59:22 +02008147 /* hashes cannot be used */
Michal Vaskod3678892020-05-21 10:06:58 +02008148 scnode = NULL;
8149 }
8150 }
8151 }
8152
Michal Vaskoc71c37b2021-01-11 13:40:02 +01008153 if (!scnode) {
8154 /* we are not able to match based on a schema node and not all the modules match ("*"),
Michal Vasko004d3152020-06-11 19:59:22 +02008155 * use dictionary for efficient comparison */
Radek Krejci011e4aa2020-09-04 15:22:31 +02008156 LY_CHECK_GOTO(rc = lydict_insert(set->ctx, ncname, ncname_len, &ncname_dict), cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02008157 }
8158
8159moveto:
8160 /* move to the attribute(s), data node(s), or schema node(s) */
Michal Vasko49fec8e2022-05-24 10:28:33 +02008161 if (axis == LYXP_AXIS_ATTRIBUTE) {
aPiecek8b0cc152021-05-31 16:40:31 +02008162 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008163 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskod3678892020-05-21 10:06:58 +02008164 } else {
8165 if (all_desc) {
Michal Vaskocdad7122020-11-09 21:04:44 +01008166 rc = moveto_attr_alldesc(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02008167 } else {
aPiecek8b0cc152021-05-31 16:40:31 +02008168 rc = moveto_attr(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02008169 }
8170 LY_CHECK_GOTO(rc, cleanup);
8171 }
8172 } else {
aPiecek8b0cc152021-05-31 16:40:31 +02008173 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008174 const struct lyxp_set_scnode *scparent = NULL;
Michal Vasko56c008c2022-07-29 14:57:47 +02008175 ly_bool found = 0;
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008176
8177 /* remember parent if there is only one, to print in the warning */
8178 for (i = 0; i < set->used; ++i) {
8179 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
8180 if (!scparent) {
8181 /* remember the context node */
8182 scparent = &set->val.scnodes[i];
8183 } else {
8184 /* several context nodes, no reasonable error possible */
8185 scparent = NULL;
8186 break;
8187 }
8188 }
8189 }
Radek Krejci1deb5be2020-08-26 16:43:36 +02008190
Michal Vasko49fec8e2022-05-24 10:28:33 +02008191 if (all_desc && (axis == LYXP_AXIS_CHILD)) {
8192 /* efficient evaluation that does not add all the descendants into the set */
8193 rc = moveto_scnode_alldesc_child(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02008194 } else {
Michal Vasko49fec8e2022-05-24 10:28:33 +02008195 if (all_desc) {
8196 /* "//" == "/descendant-or-self::node()/" */
8197 rc = xpath_pi_node(set, LYXP_AXIS_DESCENDANT_OR_SELF, options);
8198 LY_CHECK_GOTO(rc, cleanup);
8199 }
8200 rc = moveto_scnode(set, moveto_mod, ncname_dict, axis, options);
Michal Vaskod3678892020-05-21 10:06:58 +02008201 }
8202 LY_CHECK_GOTO(rc, cleanup);
8203
Michal Vasko56c008c2022-07-29 14:57:47 +02008204 i = set->used;
8205 do {
8206 --i;
Michal Vasko1a09b212021-05-06 13:00:10 +02008207 if (set->val.scnodes[i].in_ctx > LYXP_SET_SCNODE_ATOM_NODE) {
Michal Vasko56c008c2022-07-29 14:57:47 +02008208 found = 1;
Michal Vaskod3678892020-05-21 10:06:58 +02008209 break;
8210 }
Michal Vasko56c008c2022-07-29 14:57:47 +02008211 } while (i);
8212 if (!found) {
Michal Vasko4ad69e72021-10-26 16:25:55 +02008213 /* generate message */
8214 eval_name_test_scnode_no_match_msg(set, scparent, ncname, ncname_len, exp->expr, options);
8215
8216 if (options & LYXP_SCNODE_ERROR) {
8217 /* error */
Michal Vaskob7ba2c92024-01-05 15:17:53 +01008218 set->not_found = 1;
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008219 }
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008220
8221 /* skip the predicates and the rest of this path to not generate invalid warnings */
8222 rc = LY_ENOT;
8223 scnode_skip_pred = 1;
Michal Vaskod3678892020-05-21 10:06:58 +02008224 }
8225 } else {
Michal Vasko49fec8e2022-05-24 10:28:33 +02008226 if (all_desc && (axis == LYXP_AXIS_CHILD)) {
8227 /* efficient evaluation */
8228 rc = moveto_node_alldesc_child(set, moveto_mod, ncname_dict, options);
8229 } else if (scnode && (axis == LYXP_AXIS_CHILD)) {
8230 /* we can find the child nodes using hashes */
8231 rc = moveto_node_hash_child(set, scnode, predicates, options);
Michal Vaskod3678892020-05-21 10:06:58 +02008232 } else {
Michal Vasko49fec8e2022-05-24 10:28:33 +02008233 if (all_desc) {
8234 /* "//" == "/descendant-or-self::node()/" */
8235 rc = xpath_pi_node(set, LYXP_AXIS_DESCENDANT_OR_SELF, options);
8236 LY_CHECK_GOTO(rc, cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02008237 }
Michal Vasko49fec8e2022-05-24 10:28:33 +02008238 rc = moveto_node(set, moveto_mod, ncname_dict, axis, options);
Michal Vaskod3678892020-05-21 10:06:58 +02008239 }
8240 LY_CHECK_GOTO(rc, cleanup);
8241 }
8242 }
8243
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008244 if (scnode_skip_pred) {
8245 /* skip predicates */
8246 options |= LYXP_SKIP_EXPR;
8247 }
8248
Michal Vaskod3678892020-05-21 10:06:58 +02008249 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02008250 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02008251 r = eval_predicate(exp, tok_idx, set, options, axis);
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008252 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02008253 }
8254
8255cleanup:
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008256 if (scnode_skip_pred) {
8257 /* restore options */
8258 options &= ~LYXP_SKIP_EXPR;
8259 }
Michal Vaskob7ba2c92024-01-05 15:17:53 +01008260 lydict_remove(set->ctx, ncname_dict);
8261 ly_path_predicates_free(set->ctx, predicates);
Michal Vaskod3678892020-05-21 10:06:58 +02008262 return rc;
8263}
8264
8265/**
8266 * @brief Evaluate NodeType and any following Predicates. Logs directly on error.
8267 *
8268 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
8269 * [6] NodeTest ::= NameTest | NodeType '(' ')'
8270 * [8] NodeType ::= 'text' | 'node'
8271 *
8272 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008273 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko49fec8e2022-05-24 10:28:33 +02008274 * @param[in] axis Axis to search on.
8275 * @param[in] all_desc Whether to search all the descendants or axis only.
aPiecek8b0cc152021-05-31 16:40:31 +02008276 * @param[in,out] set Context and result set.
Michal Vaskod3678892020-05-21 10:06:58 +02008277 * @param[in] options XPath options.
8278 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8279 */
8280static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02008281eval_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 +02008282 struct lyxp_set *set, uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02008283{
8284 LY_ERR rc;
8285
Michal Vaskod3678892020-05-21 10:06:58 +02008286 (void)all_desc;
8287
aPiecek8b0cc152021-05-31 16:40:31 +02008288 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko004d3152020-06-11 19:59:22 +02008289 assert(exp->tok_len[*tok_idx] == 4);
Michal Vasko49fec8e2022-05-24 10:28:33 +02008290 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "node", 4)) {
8291 rc = xpath_pi_node(set, axis, options);
Michal Vaskod3678892020-05-21 10:06:58 +02008292 } else {
Michal Vasko49fec8e2022-05-24 10:28:33 +02008293 assert(!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "text", 4));
8294 rc = xpath_pi_text(set, axis, options);
Michal Vaskod3678892020-05-21 10:06:58 +02008295 }
Michal Vasko49fec8e2022-05-24 10:28:33 +02008296 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02008297 }
aPiecek8b0cc152021-05-31 16:40:31 +02008298 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008299 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008300 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02008301
8302 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02008303 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
aPiecek8b0cc152021-05-31 16:40:31 +02008304 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008305 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008306 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02008307
8308 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02008309 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
aPiecek8b0cc152021-05-31 16:40:31 +02008310 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008311 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008312 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02008313
8314 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02008315 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02008316 rc = eval_predicate(exp, tok_idx, set, options, axis);
Michal Vaskod3678892020-05-21 10:06:58 +02008317 LY_CHECK_RET(rc);
8318 }
8319
8320 return LY_SUCCESS;
8321}
8322
8323/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02008324 * @brief Evaluate RelativeLocationPath. Logs directly on error.
8325 *
8326 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
8327 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
Michal Vaskod3678892020-05-21 10:06:58 +02008328 * [6] NodeTest ::= NameTest | NodeType '(' ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02008329 *
8330 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008331 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008332 * @param[in] all_desc Whether to search all the descendants or children only.
aPiecek8b0cc152021-05-31 16:40:31 +02008333 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008334 * @param[in] options XPath options.
8335 * @return LY_ERR (YL_EINCOMPLETE on unresolved when)
8336 */
8337static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02008338eval_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 +02008339 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008340{
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008341 LY_ERR rc = LY_SUCCESS;
Michal Vasko49fec8e2022-05-24 10:28:33 +02008342 enum lyxp_axis axis;
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008343 int scnode_skip_path = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008344
8345 goto step;
8346 do {
8347 /* evaluate '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02008348 if (exp->tok_len[*tok_idx] == 1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008349 all_desc = 0;
8350 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02008351 assert(exp->tok_len[*tok_idx] == 2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008352 all_desc = 1;
8353 }
aPiecek8b0cc152021-05-31 16:40:31 +02008354 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008355 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008356 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008357
8358step:
Michal Vasko49fec8e2022-05-24 10:28:33 +02008359 /* AxisSpecifier */
8360 if (exp->tokens[*tok_idx] == LYXP_TOKEN_AXISNAME) {
8361 axis = str2axis(exp->expr + exp->tok_pos[*tok_idx], exp->tok_len[*tok_idx]);
Michal Vaskod3678892020-05-21 10:06:58 +02008362
aPiecek8b0cc152021-05-31 16:40:31 +02008363 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008364 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
8365 ++(*tok_idx);
8366
8367 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_DCOLON);
8368 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
8369 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
8370 ++(*tok_idx);
8371 } else if (exp->tokens[*tok_idx] == LYXP_TOKEN_AT) {
8372 axis = LYXP_AXIS_ATTRIBUTE;
8373
8374 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
8375 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008376 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02008377 } else {
Michal Vasko49fec8e2022-05-24 10:28:33 +02008378 /* default */
8379 axis = LYXP_AXIS_CHILD;
Michal Vaskod3678892020-05-21 10:06:58 +02008380 }
8381
Michal Vasko49fec8e2022-05-24 10:28:33 +02008382 /* NodeTest Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02008383 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008384 case LYXP_TOKEN_DOT:
8385 /* evaluate '.' */
Michal Vasko49fec8e2022-05-24 10:28:33 +02008386 if (!(options & LYXP_SKIP_EXPR)) {
8387 if (((options & LYXP_SCNODE_ALL) && (set->type != LYXP_SET_SCNODE_SET)) ||
8388 (!(options & LYXP_SCNODE_ALL) && (set->type != LYXP_SET_NODE_SET))) {
8389 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
8390 rc = LY_EVALID;
8391 goto cleanup;
8392 }
8393
8394 if (all_desc) {
8395 rc = xpath_pi_node(set, LYXP_AXIS_DESCENDANT_OR_SELF, options);
8396 LY_CHECK_GOTO(rc, cleanup);
8397 }
8398 rc = xpath_pi_node(set, LYXP_AXIS_SELF, options);
8399 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008400 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008401 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008402 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008403 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008404 break;
8405
8406 case LYXP_TOKEN_DDOT:
8407 /* evaluate '..' */
Michal Vasko49fec8e2022-05-24 10:28:33 +02008408 if (!(options & LYXP_SKIP_EXPR)) {
8409 if (((options & LYXP_SCNODE_ALL) && (set->type != LYXP_SET_SCNODE_SET)) ||
8410 (!(options & LYXP_SCNODE_ALL) && (set->type != LYXP_SET_NODE_SET))) {
8411 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
8412 rc = LY_EVALID;
8413 goto cleanup;
8414 }
8415
8416 if (all_desc) {
8417 rc = xpath_pi_node(set, LYXP_AXIS_DESCENDANT_OR_SELF, options);
8418 LY_CHECK_GOTO(rc, cleanup);
8419 }
8420 rc = xpath_pi_node(set, LYXP_AXIS_PARENT, options);
8421 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008422 }
aPiecek8b0cc152021-05-31 16:40:31 +02008423 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008424 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008425 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008426 break;
8427
Michal Vasko03ff5a72019-09-11 13:49:33 +02008428 case LYXP_TOKEN_NAMETEST:
Michal Vaskod3678892020-05-21 10:06:58 +02008429 /* evaluate NameTest Predicate* */
Michal Vasko49fec8e2022-05-24 10:28:33 +02008430 rc = eval_name_test_with_predicate(exp, tok_idx, axis, all_desc, set, options);
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008431 if (rc == LY_ENOT) {
8432 assert(options & LYXP_SCNODE_ALL);
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008433 rc = LY_SUCCESS;
Michal Vaskob7ba2c92024-01-05 15:17:53 +01008434
8435 /* skip the rest of this path */
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008436 scnode_skip_path = 1;
8437 options |= LYXP_SKIP_EXPR;
8438 }
8439 LY_CHECK_GOTO(rc, cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02008440 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008441
Michal Vaskod3678892020-05-21 10:06:58 +02008442 case LYXP_TOKEN_NODETYPE:
8443 /* evaluate NodeType Predicate* */
Michal Vasko49fec8e2022-05-24 10:28:33 +02008444 rc = eval_node_type_with_predicate(exp, tok_idx, axis, all_desc, set, options);
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008445 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008446 break;
8447
8448 default:
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008449 LOGINT(set->ctx);
8450 rc = LY_EINT;
8451 goto cleanup;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008452 }
Michal Vasko004d3152020-06-11 19:59:22 +02008453 } while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008454
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008455cleanup:
8456 if (scnode_skip_path) {
8457 options &= ~LYXP_SKIP_EXPR;
8458 }
8459 return rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008460}
8461
8462/**
8463 * @brief Evaluate AbsoluteLocationPath. Logs directly on error.
8464 *
8465 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
8466 *
8467 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008468 * @param[in] tok_idx Position in the expression @p exp.
aPiecek8b0cc152021-05-31 16:40:31 +02008469 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008470 * @param[in] options XPath options.
8471 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8472 */
8473static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02008474eval_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 +02008475{
Radek Krejci857189e2020-09-01 13:26:36 +02008476 ly_bool all_desc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008477
aPiecek8b0cc152021-05-31 16:40:31 +02008478 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008479 /* no matter what tokens follow, we need to be at the root */
Michal Vaskob0099a92020-08-31 14:55:23 +02008480 LY_CHECK_RET(moveto_root(set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008481 }
8482
8483 /* '/' RelativeLocationPath? */
Michal Vasko004d3152020-06-11 19:59:22 +02008484 if (exp->tok_len[*tok_idx] == 1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008485 /* evaluate '/' - deferred */
8486 all_desc = 0;
aPiecek8b0cc152021-05-31 16:40:31 +02008487 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008488 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008489 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008490
Michal Vasko004d3152020-06-11 19:59:22 +02008491 if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008492 return LY_SUCCESS;
8493 }
Michal Vasko004d3152020-06-11 19:59:22 +02008494 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008495 case LYXP_TOKEN_DOT:
8496 case LYXP_TOKEN_DDOT:
Michal Vasko49fec8e2022-05-24 10:28:33 +02008497 case LYXP_TOKEN_AXISNAME:
Michal Vasko03ff5a72019-09-11 13:49:33 +02008498 case LYXP_TOKEN_AT:
8499 case LYXP_TOKEN_NAMETEST:
8500 case LYXP_TOKEN_NODETYPE:
Michal Vaskob0099a92020-08-31 14:55:23 +02008501 LY_CHECK_RET(eval_relative_location_path(exp, tok_idx, all_desc, set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008502 break;
8503 default:
8504 break;
8505 }
8506
Michal Vasko03ff5a72019-09-11 13:49:33 +02008507 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02008508 /* '//' RelativeLocationPath */
Michal Vasko03ff5a72019-09-11 13:49:33 +02008509 /* evaluate '//' - deferred so as not to waste memory by remembering all the nodes */
8510 all_desc = 1;
aPiecek8b0cc152021-05-31 16:40:31 +02008511 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008512 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008513 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008514
Michal Vaskob0099a92020-08-31 14:55:23 +02008515 LY_CHECK_RET(eval_relative_location_path(exp, tok_idx, all_desc, set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008516 }
8517
8518 return LY_SUCCESS;
8519}
8520
8521/**
8522 * @brief Evaluate FunctionCall. Logs directly on error.
8523 *
Michal Vaskod3678892020-05-21 10:06:58 +02008524 * [11] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02008525 *
8526 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008527 * @param[in] tok_idx Position in the expression @p exp.
aPiecek8b0cc152021-05-31 16:40:31 +02008528 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008529 * @param[in] options XPath options.
8530 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8531 */
8532static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02008533eval_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 +02008534{
8535 LY_ERR rc;
Michal Vasko69730152020-10-09 16:30:07 +02008536
Michal Vaskodd528af2022-08-08 14:35:07 +02008537 LY_ERR (*xpath_func)(struct lyxp_set **, uint32_t, struct lyxp_set *, uint32_t) = NULL;
8538 uint32_t arg_count = 0, i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008539 struct lyxp_set **args = NULL, **args_aux;
8540
aPiecek8b0cc152021-05-31 16:40:31 +02008541 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008542 /* FunctionName */
Michal Vasko004d3152020-06-11 19:59:22 +02008543 switch (exp->tok_len[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008544 case 3:
Michal Vasko004d3152020-06-11 19:59:22 +02008545 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008546 xpath_func = &xpath_not;
Michal Vasko004d3152020-06-11 19:59:22 +02008547 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008548 xpath_func = &xpath_sum;
8549 }
8550 break;
8551 case 4:
Michal Vasko004d3152020-06-11 19:59:22 +02008552 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008553 xpath_func = &xpath_lang;
Michal Vasko004d3152020-06-11 19:59:22 +02008554 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008555 xpath_func = &xpath_last;
Michal Vasko004d3152020-06-11 19:59:22 +02008556 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008557 xpath_func = &xpath_name;
Michal Vasko004d3152020-06-11 19:59:22 +02008558 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008559 xpath_func = &xpath_true;
8560 }
8561 break;
8562 case 5:
Michal Vasko004d3152020-06-11 19:59:22 +02008563 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008564 xpath_func = &xpath_count;
Michal Vasko004d3152020-06-11 19:59:22 +02008565 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008566 xpath_func = &xpath_false;
Michal Vasko004d3152020-06-11 19:59:22 +02008567 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008568 xpath_func = &xpath_floor;
Michal Vasko004d3152020-06-11 19:59:22 +02008569 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008570 xpath_func = &xpath_round;
Michal Vasko004d3152020-06-11 19:59:22 +02008571 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008572 xpath_func = &xpath_deref;
8573 }
8574 break;
8575 case 6:
Michal Vasko004d3152020-06-11 19:59:22 +02008576 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008577 xpath_func = &xpath_concat;
Michal Vasko004d3152020-06-11 19:59:22 +02008578 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008579 xpath_func = &xpath_number;
Michal Vasko004d3152020-06-11 19:59:22 +02008580 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008581 xpath_func = &xpath_string;
8582 }
8583 break;
8584 case 7:
Michal Vasko004d3152020-06-11 19:59:22 +02008585 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008586 xpath_func = &xpath_boolean;
Michal Vasko004d3152020-06-11 19:59:22 +02008587 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008588 xpath_func = &xpath_ceiling;
Michal Vasko004d3152020-06-11 19:59:22 +02008589 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008590 xpath_func = &xpath_current;
8591 }
8592 break;
8593 case 8:
Michal Vasko004d3152020-06-11 19:59:22 +02008594 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008595 xpath_func = &xpath_contains;
Michal Vasko004d3152020-06-11 19:59:22 +02008596 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008597 xpath_func = &xpath_position;
Michal Vasko004d3152020-06-11 19:59:22 +02008598 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "re-match", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008599 xpath_func = &xpath_re_match;
8600 }
8601 break;
8602 case 9:
Michal Vasko004d3152020-06-11 19:59:22 +02008603 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008604 xpath_func = &xpath_substring;
Michal Vasko004d3152020-06-11 19:59:22 +02008605 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008606 xpath_func = &xpath_translate;
8607 }
8608 break;
8609 case 10:
Michal Vasko004d3152020-06-11 19:59:22 +02008610 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008611 xpath_func = &xpath_local_name;
Michal Vasko004d3152020-06-11 19:59:22 +02008612 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008613 xpath_func = &xpath_enum_value;
Michal Vasko004d3152020-06-11 19:59:22 +02008614 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008615 xpath_func = &xpath_bit_is_set;
8616 }
8617 break;
8618 case 11:
Michal Vasko004d3152020-06-11 19:59:22 +02008619 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008620 xpath_func = &xpath_starts_with;
8621 }
8622 break;
8623 case 12:
Michal Vasko004d3152020-06-11 19:59:22 +02008624 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008625 xpath_func = &xpath_derived_from;
8626 }
8627 break;
8628 case 13:
Michal Vasko004d3152020-06-11 19:59:22 +02008629 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008630 xpath_func = &xpath_namespace_uri;
Michal Vasko004d3152020-06-11 19:59:22 +02008631 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string-length", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008632 xpath_func = &xpath_string_length;
8633 }
8634 break;
8635 case 15:
Michal Vasko004d3152020-06-11 19:59:22 +02008636 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008637 xpath_func = &xpath_normalize_space;
Michal Vasko004d3152020-06-11 19:59:22 +02008638 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-after", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008639 xpath_func = &xpath_substring_after;
8640 }
8641 break;
8642 case 16:
Michal Vasko004d3152020-06-11 19:59:22 +02008643 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008644 xpath_func = &xpath_substring_before;
8645 }
8646 break;
8647 case 20:
Michal Vasko004d3152020-06-11 19:59:22 +02008648 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from-or-self", 20)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008649 xpath_func = &xpath_derived_from_or_self;
8650 }
8651 break;
8652 }
8653
8654 if (!xpath_func) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01008655 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 +02008656 return LY_EVALID;
8657 }
8658 }
8659
aPiecek8b0cc152021-05-31 16:40:31 +02008660 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008661 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008662 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008663
8664 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02008665 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
aPiecek8b0cc152021-05-31 16:40:31 +02008666 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008667 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008668 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008669
8670 /* ( Expr ( ',' Expr )* )? */
Michal Vasko004d3152020-06-11 19:59:22 +02008671 if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) {
aPiecek8b0cc152021-05-31 16:40:31 +02008672 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008673 args = malloc(sizeof *args);
8674 LY_CHECK_ERR_GOTO(!args, LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
8675 arg_count = 1;
8676 args[0] = set_copy(set);
8677 if (!args[0]) {
8678 rc = LY_EMEM;
8679 goto cleanup;
8680 }
8681
Michal Vasko004d3152020-06-11 19:59:22 +02008682 rc = eval_expr_select(exp, tok_idx, 0, args[0], options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008683 LY_CHECK_GOTO(rc, cleanup);
Michal Vaskob7ba2c92024-01-05 15:17:53 +01008684 set->not_found = args[0]->not_found;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008685 } else {
Michal Vaskob7ba2c92024-01-05 15:17:53 +01008686 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008687 LY_CHECK_GOTO(rc, cleanup);
8688 }
8689 }
Michal Vasko004d3152020-06-11 19:59:22 +02008690 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) {
aPiecek8b0cc152021-05-31 16:40:31 +02008691 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008692 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008693 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008694
aPiecek8b0cc152021-05-31 16:40:31 +02008695 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008696 ++arg_count;
8697 args_aux = realloc(args, arg_count * sizeof *args);
8698 LY_CHECK_ERR_GOTO(!args_aux, arg_count--; LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
8699 args = args_aux;
8700 args[arg_count - 1] = set_copy(set);
8701 if (!args[arg_count - 1]) {
8702 rc = LY_EMEM;
8703 goto cleanup;
8704 }
8705
Michal Vasko004d3152020-06-11 19:59:22 +02008706 rc = eval_expr_select(exp, tok_idx, 0, args[arg_count - 1], options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008707 LY_CHECK_GOTO(rc, cleanup);
Michal Vaskob7ba2c92024-01-05 15:17:53 +01008708 if (args[arg_count - 1]->not_found) {
8709 set->not_found = 1;
8710 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008711 } else {
Michal Vaskob7ba2c92024-01-05 15:17:53 +01008712 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008713 LY_CHECK_GOTO(rc, cleanup);
8714 }
8715 }
8716
8717 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02008718 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
aPiecek8b0cc152021-05-31 16:40:31 +02008719 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008720 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008721 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008722
aPiecek8b0cc152021-05-31 16:40:31 +02008723 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008724 /* evaluate function */
8725 rc = xpath_func(args, arg_count, set, options);
8726
8727 if (options & LYXP_SCNODE_ALL) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008728 /* merge all nodes from arg evaluations */
8729 for (i = 0; i < arg_count; ++i) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008730 set_scnode_clear_ctx(args[i], LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008731 lyxp_set_scnode_merge(set, args[i]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008732 }
8733 }
8734 } else {
8735 rc = LY_SUCCESS;
8736 }
8737
8738cleanup:
8739 for (i = 0; i < arg_count; ++i) {
8740 lyxp_set_free(args[i]);
8741 }
8742 free(args);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008743 return rc;
8744}
8745
8746/**
8747 * @brief Evaluate Number. Logs directly on error.
8748 *
8749 * @param[in] ctx Context for errors.
8750 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008751 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008752 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8753 * @return LY_ERR
8754 */
8755static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02008756eval_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 +02008757{
8758 long double num;
8759 char *endptr;
8760
8761 if (set) {
8762 errno = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02008763 num = strtold(&exp->expr[exp->tok_pos[*tok_idx]], &endptr);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008764 if (errno) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01008765 LOGVAL(ctx, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]);
8766 LOGVAL(ctx, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double (%s).",
Michal Vasko69730152020-10-09 16:30:07 +02008767 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]], strerror(errno));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008768 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02008769 } else if (endptr - &exp->expr[exp->tok_pos[*tok_idx]] != exp->tok_len[*tok_idx]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01008770 LOGVAL(ctx, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]);
8771 LOGVAL(ctx, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double.",
Michal Vasko69730152020-10-09 16:30:07 +02008772 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008773 return LY_EVALID;
8774 }
8775
8776 set_fill_number(set, num);
8777 }
8778
8779 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008780 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008781 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008782 return LY_SUCCESS;
8783}
8784
aPiecekdf23eee2021-10-07 12:21:50 +02008785LY_ERR
Michal Vasko90189962023-02-28 12:10:34 +01008786lyxp_vars_find(const struct ly_ctx *ctx, const struct lyxp_var *vars, const char *name, size_t name_len,
8787 struct lyxp_var **var)
aPiecekdf23eee2021-10-07 12:21:50 +02008788{
aPiecekdf23eee2021-10-07 12:21:50 +02008789 LY_ARRAY_COUNT_TYPE u;
8790
Michal Vasko90189962023-02-28 12:10:34 +01008791 assert(name);
aPiecekdf23eee2021-10-07 12:21:50 +02008792
Michal Vasko90189962023-02-28 12:10:34 +01008793 if (!name_len) {
8794 name_len = strlen(name);
8795 }
aPiecekdf23eee2021-10-07 12:21:50 +02008796
8797 LY_ARRAY_FOR(vars, u) {
8798 if (!strncmp(vars[u].name, name, name_len)) {
Michal Vasko90189962023-02-28 12:10:34 +01008799 if (var) {
8800 *var = (struct lyxp_var *)&vars[u];
8801 }
8802 return LY_SUCCESS;
aPiecekdf23eee2021-10-07 12:21:50 +02008803 }
8804 }
8805
Michal Vasko90189962023-02-28 12:10:34 +01008806 if (ctx) {
8807 LOGERR(ctx, LY_ENOTFOUND, "Variable \"%.*s\" not defined.", (int)name_len, name);
aPiecekdf23eee2021-10-07 12:21:50 +02008808 }
Michal Vasko90189962023-02-28 12:10:34 +01008809 return LY_ENOTFOUND;
aPiecekdf23eee2021-10-07 12:21:50 +02008810}
8811
Michal Vasko03ff5a72019-09-11 13:49:33 +02008812/**
aPiecekfba75362021-10-07 12:39:48 +02008813 * @brief Evaluate VariableReference.
8814 *
8815 * @param[in] exp Parsed XPath expression.
8816 * @param[in] tok_idx Position in the expression @p exp.
8817 * @param[in] vars [Sized array](@ref sizedarrays) of XPath variables.
8818 * @param[in,out] set Context and result set.
8819 * @param[in] options XPath options.
8820 * @return LY_ERR value.
8821 */
8822static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02008823eval_variable_reference(const struct lyxp_expr *exp, uint32_t *tok_idx, struct lyxp_set *set, uint32_t options)
aPiecekfba75362021-10-07 12:39:48 +02008824{
8825 LY_ERR ret;
8826 const char *name;
8827 struct lyxp_var *var;
aPiecekfba75362021-10-07 12:39:48 +02008828 struct lyxp_expr *tokens = NULL;
Michal Vaskodd528af2022-08-08 14:35:07 +02008829 uint32_t token_index, name_len;
aPiecekfba75362021-10-07 12:39:48 +02008830
Michal Vasko49fec8e2022-05-24 10:28:33 +02008831 /* find out the name and value of the variable */
aPiecekfba75362021-10-07 12:39:48 +02008832 name = &exp->expr[exp->tok_pos[*tok_idx]];
Michal Vaskoe68b5402022-07-29 14:58:15 +02008833 name_len = exp->tok_len[*tok_idx];
Michal Vasko90189962023-02-28 12:10:34 +01008834 ret = lyxp_vars_find(set->ctx, set->vars, name, name_len, &var);
8835 LY_CHECK_RET(ret);
aPiecekfba75362021-10-07 12:39:48 +02008836
Michal Vasko49fec8e2022-05-24 10:28:33 +02008837 /* parse value */
aPiecekfba75362021-10-07 12:39:48 +02008838 ret = lyxp_expr_parse(set->ctx, var->value, 0, 1, &tokens);
8839 LY_CHECK_GOTO(ret, cleanup);
8840
Michal Vasko49fec8e2022-05-24 10:28:33 +02008841 /* evaluate value */
aPiecekfba75362021-10-07 12:39:48 +02008842 token_index = 0;
8843 ret = eval_expr_select(tokens, &token_index, 0, set, options);
8844 LY_CHECK_GOTO(ret, cleanup);
8845
8846cleanup:
8847 lyxp_expr_free(set->ctx, tokens);
8848
8849 return ret;
8850}
8851
8852/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02008853 * @brief Evaluate PathExpr. Logs directly on error.
8854 *
Michal Vaskod3678892020-05-21 10:06:58 +02008855 * [12] PathExpr ::= LocationPath | PrimaryExpr Predicate*
Michal Vasko03ff5a72019-09-11 13:49:33 +02008856 * | PrimaryExpr Predicate* '/' RelativeLocationPath
8857 * | PrimaryExpr Predicate* '//' RelativeLocationPath
8858 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
aPiecekfba75362021-10-07 12:39:48 +02008859 * [10] PrimaryExpr ::= VariableReference | '(' Expr ')' | Literal | Number | FunctionCall
Michal Vasko03ff5a72019-09-11 13:49:33 +02008860 *
8861 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008862 * @param[in] tok_idx Position in the expression @p exp.
aPiecek8b0cc152021-05-31 16:40:31 +02008863 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008864 * @param[in] options XPath options.
8865 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8866 */
8867static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02008868eval_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 +02008869{
Michal Vasko49fec8e2022-05-24 10:28:33 +02008870 ly_bool all_desc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008871 LY_ERR rc;
8872
Michal Vasko004d3152020-06-11 19:59:22 +02008873 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008874 case LYXP_TOKEN_PAR1:
8875 /* '(' Expr ')' */
8876
8877 /* '(' */
aPiecek8b0cc152021-05-31 16:40:31 +02008878 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008879 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008880 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008881
8882 /* Expr */
Michal Vasko004d3152020-06-11 19:59:22 +02008883 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008884 LY_CHECK_RET(rc);
8885
8886 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02008887 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
aPiecek8b0cc152021-05-31 16:40:31 +02008888 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008889 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008890 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008891
Michal Vasko03ff5a72019-09-11 13:49:33 +02008892 goto predicate;
8893
8894 case LYXP_TOKEN_DOT:
8895 case LYXP_TOKEN_DDOT:
Michal Vasko49fec8e2022-05-24 10:28:33 +02008896 case LYXP_TOKEN_AXISNAME:
Michal Vasko03ff5a72019-09-11 13:49:33 +02008897 case LYXP_TOKEN_AT:
8898 case LYXP_TOKEN_NAMETEST:
8899 case LYXP_TOKEN_NODETYPE:
8900 /* RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02008901 rc = eval_relative_location_path(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008902 LY_CHECK_RET(rc);
8903 break;
8904
aPiecekfba75362021-10-07 12:39:48 +02008905 case LYXP_TOKEN_VARREF:
8906 /* VariableReference */
8907 rc = eval_variable_reference(exp, tok_idx, set, options);
8908 LY_CHECK_RET(rc);
8909 ++(*tok_idx);
8910
aPiecekfba75362021-10-07 12:39:48 +02008911 goto predicate;
8912
Michal Vasko03ff5a72019-09-11 13:49:33 +02008913 case LYXP_TOKEN_FUNCNAME:
8914 /* FunctionCall */
aPiecek8b0cc152021-05-31 16:40:31 +02008915 rc = eval_function_call(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008916 LY_CHECK_RET(rc);
8917
Michal Vasko03ff5a72019-09-11 13:49:33 +02008918 goto predicate;
8919
Michal Vasko3e48bf32020-06-01 08:39:07 +02008920 case LYXP_TOKEN_OPER_PATH:
8921 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +02008922 /* AbsoluteLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02008923 rc = eval_absolute_location_path(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008924 LY_CHECK_RET(rc);
8925 break;
8926
8927 case LYXP_TOKEN_LITERAL:
8928 /* Literal */
aPiecek8b0cc152021-05-31 16:40:31 +02008929 if ((options & LYXP_SKIP_EXPR) || (options & LYXP_SCNODE_ALL)) {
8930 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008931 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008932 }
Michal Vasko004d3152020-06-11 19:59:22 +02008933 eval_literal(exp, tok_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008934 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02008935 eval_literal(exp, tok_idx, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008936 }
8937
Michal Vasko03ff5a72019-09-11 13:49:33 +02008938 goto predicate;
8939
8940 case LYXP_TOKEN_NUMBER:
8941 /* Number */
aPiecek8b0cc152021-05-31 16:40:31 +02008942 if ((options & LYXP_SKIP_EXPR) || (options & LYXP_SCNODE_ALL)) {
8943 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008944 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008945 }
Michal Vasko004d3152020-06-11 19:59:22 +02008946 rc = eval_number(NULL, exp, tok_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008947 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02008948 rc = eval_number(set->ctx, exp, tok_idx, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008949 }
8950 LY_CHECK_RET(rc);
8951
Michal Vasko03ff5a72019-09-11 13:49:33 +02008952 goto predicate;
8953
8954 default:
Michal Vasko49fec8e2022-05-24 10:28:33 +02008955 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 +02008956 return LY_EVALID;
8957 }
8958
8959 return LY_SUCCESS;
8960
8961predicate:
8962 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02008963 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02008964 rc = eval_predicate(exp, tok_idx, set, options, LYXP_AXIS_CHILD);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008965 LY_CHECK_RET(rc);
8966 }
8967
8968 /* ('/' or '//') RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02008969 if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008970
8971 /* evaluate '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02008972 if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008973 all_desc = 0;
8974 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008975 all_desc = 1;
8976 }
8977
aPiecek8b0cc152021-05-31 16:40:31 +02008978 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008979 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008980 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008981
Michal Vasko004d3152020-06-11 19:59:22 +02008982 rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008983 LY_CHECK_RET(rc);
8984 }
8985
8986 return LY_SUCCESS;
8987}
8988
8989/**
8990 * @brief Evaluate UnionExpr. Logs directly on error.
8991 *
Michal Vaskod3678892020-05-21 10:06:58 +02008992 * [20] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008993 *
8994 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008995 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008996 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008997 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008998 * @param[in] options XPath options.
8999 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
9000 */
9001static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02009002eval_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 +02009003{
9004 LY_ERR rc = LY_SUCCESS;
Michal Vaskodd528af2022-08-08 14:35:07 +02009005 uint32_t i;
Michal Vaskob7ba2c92024-01-05 15:17:53 +01009006 struct lyxp_set orig_set, set2;
9007 ly_bool found = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009008
9009 assert(repeat);
9010
9011 set_init(&orig_set, set);
9012 set_init(&set2, set);
9013
9014 set_fill_set(&orig_set, set);
9015
Michal Vasko004d3152020-06-11 19:59:22 +02009016 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009017 LY_CHECK_GOTO(rc, cleanup);
Michal Vaskob7ba2c92024-01-05 15:17:53 +01009018 if (set->not_found) {
9019 set->not_found = 0;
9020 } else {
9021 found = 1;
9022 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02009023
9024 /* ('|' PathExpr)* */
9025 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02009026 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_UNI);
aPiecek8b0cc152021-05-31 16:40:31 +02009027 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02009028 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02009029 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009030
aPiecek8b0cc152021-05-31 16:40:31 +02009031 if (options & LYXP_SKIP_EXPR) {
9032 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009033 LY_CHECK_GOTO(rc, cleanup);
9034 continue;
9035 }
9036
9037 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02009038 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009039 LY_CHECK_GOTO(rc, cleanup);
Michal Vaskob7ba2c92024-01-05 15:17:53 +01009040 if (!set2.not_found) {
9041 found = 1;
9042 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02009043
9044 /* eval */
9045 if (options & LYXP_SCNODE_ALL) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01009046 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009047 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009048 rc = moveto_union(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009049 LY_CHECK_GOTO(rc, cleanup);
9050 }
9051 }
9052
9053cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02009054 lyxp_set_free_content(&orig_set);
9055 lyxp_set_free_content(&set2);
Michal Vaskob7ba2c92024-01-05 15:17:53 +01009056 if (!found) {
9057 set->not_found = 1;
9058 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02009059 return rc;
9060}
9061
9062/**
9063 * @brief Evaluate UnaryExpr. Logs directly on error.
9064 *
Michal Vaskod3678892020-05-21 10:06:58 +02009065 * [19] UnaryExpr ::= UnionExpr | '-' UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02009066 *
9067 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02009068 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009069 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02009070 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009071 * @param[in] options XPath options.
9072 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
9073 */
9074static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02009075eval_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 +02009076{
9077 LY_ERR rc;
Michal Vaskodd528af2022-08-08 14:35:07 +02009078 uint32_t this_op, i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009079
9080 assert(repeat);
9081
9082 /* ('-')+ */
Michal Vasko004d3152020-06-11 19:59:22 +02009083 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009084 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02009085 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 +02009086
aPiecek8b0cc152021-05-31 16:40:31 +02009087 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02009088 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02009089 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009090 }
9091
Michal Vasko004d3152020-06-11 19:59:22 +02009092 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNARY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009093 LY_CHECK_RET(rc);
9094
aPiecek8b0cc152021-05-31 16:40:31 +02009095 if (!(options & LYXP_SKIP_EXPR) && (repeat % 2)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02009096 if (options & LYXP_SCNODE_ALL) {
9097 warn_operands(set->ctx, set, NULL, 1, exp->expr, exp->tok_pos[this_op]);
9098 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009099 rc = moveto_op_math(set, NULL, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009100 LY_CHECK_RET(rc);
9101 }
9102 }
9103
9104 return LY_SUCCESS;
9105}
9106
9107/**
9108 * @brief Evaluate MultiplicativeExpr. Logs directly on error.
9109 *
Michal Vaskod3678892020-05-21 10:06:58 +02009110 * [18] MultiplicativeExpr ::= UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02009111 * | MultiplicativeExpr '*' UnaryExpr
9112 * | MultiplicativeExpr 'div' UnaryExpr
9113 * | MultiplicativeExpr 'mod' UnaryExpr
9114 *
9115 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02009116 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009117 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02009118 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009119 * @param[in] options XPath options.
9120 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
9121 */
9122static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02009123eval_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 +02009124 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02009125{
Michal Vaskob7ba2c92024-01-05 15:17:53 +01009126 LY_ERR rc = LY_SUCCESS;
Michal Vaskodd528af2022-08-08 14:35:07 +02009127 uint32_t i, this_op;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009128 struct lyxp_set orig_set, set2;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009129
9130 assert(repeat);
9131
9132 set_init(&orig_set, set);
9133 set_init(&set2, set);
9134
9135 set_fill_set(&orig_set, set);
9136
Michal Vasko004d3152020-06-11 19:59:22 +02009137 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009138 LY_CHECK_GOTO(rc, cleanup);
9139
9140 /* ('*' / 'div' / 'mod' UnaryExpr)* */
9141 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02009142 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009143
Michal Vasko004d3152020-06-11 19:59:22 +02009144 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH);
aPiecek8b0cc152021-05-31 16:40:31 +02009145 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02009146 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02009147 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009148
aPiecek8b0cc152021-05-31 16:40:31 +02009149 if (options & LYXP_SKIP_EXPR) {
9150 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009151 LY_CHECK_GOTO(rc, cleanup);
9152 continue;
9153 }
9154
9155 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02009156 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009157 LY_CHECK_GOTO(rc, cleanup);
Michal Vaskob7ba2c92024-01-05 15:17:53 +01009158 if (set2.not_found) {
9159 set->not_found = 1;
9160 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02009161
9162 /* eval */
9163 if (options & LYXP_SCNODE_ALL) {
9164 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01009165 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02009166 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009167 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009168 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009169 LY_CHECK_GOTO(rc, cleanup);
9170 }
9171 }
9172
9173cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02009174 lyxp_set_free_content(&orig_set);
9175 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009176 return rc;
9177}
9178
9179/**
9180 * @brief Evaluate AdditiveExpr. Logs directly on error.
9181 *
Michal Vaskod3678892020-05-21 10:06:58 +02009182 * [17] AdditiveExpr ::= MultiplicativeExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02009183 * | AdditiveExpr '+' MultiplicativeExpr
9184 * | AdditiveExpr '-' MultiplicativeExpr
9185 *
9186 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02009187 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009188 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02009189 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009190 * @param[in] options XPath options.
9191 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
9192 */
9193static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02009194eval_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 +02009195{
Michal Vaskob7ba2c92024-01-05 15:17:53 +01009196 LY_ERR rc = LY_SUCCESS;
Michal Vaskodd528af2022-08-08 14:35:07 +02009197 uint32_t i, this_op;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009198 struct lyxp_set orig_set, set2;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009199
9200 assert(repeat);
9201
9202 set_init(&orig_set, set);
9203 set_init(&set2, set);
9204
9205 set_fill_set(&orig_set, set);
9206
Michal Vasko004d3152020-06-11 19:59:22 +02009207 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009208 LY_CHECK_GOTO(rc, cleanup);
9209
9210 /* ('+' / '-' MultiplicativeExpr)* */
9211 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02009212 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009213
Michal Vasko004d3152020-06-11 19:59:22 +02009214 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009215 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02009216 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02009217 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009218
aPiecek8b0cc152021-05-31 16:40:31 +02009219 if (options & LYXP_SKIP_EXPR) {
9220 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009221 LY_CHECK_GOTO(rc, cleanup);
9222 continue;
9223 }
9224
9225 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02009226 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009227 LY_CHECK_GOTO(rc, cleanup);
Michal Vaskob7ba2c92024-01-05 15:17:53 +01009228 if (set2.not_found) {
9229 set->not_found = 1;
9230 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02009231
9232 /* eval */
9233 if (options & LYXP_SCNODE_ALL) {
9234 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01009235 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02009236 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009237 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009238 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009239 LY_CHECK_GOTO(rc, cleanup);
9240 }
9241 }
9242
9243cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02009244 lyxp_set_free_content(&orig_set);
9245 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009246 return rc;
9247}
9248
9249/**
9250 * @brief Evaluate RelationalExpr. Logs directly on error.
9251 *
Michal Vaskod3678892020-05-21 10:06:58 +02009252 * [16] RelationalExpr ::= AdditiveExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02009253 * | RelationalExpr '<' AdditiveExpr
9254 * | RelationalExpr '>' AdditiveExpr
9255 * | RelationalExpr '<=' AdditiveExpr
9256 * | RelationalExpr '>=' AdditiveExpr
9257 *
9258 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02009259 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009260 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02009261 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009262 * @param[in] options XPath options.
9263 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
9264 */
9265static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02009266eval_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 +02009267{
Michal Vaskob7ba2c92024-01-05 15:17:53 +01009268 LY_ERR rc = LY_SUCCESS;
Michal Vaskodd528af2022-08-08 14:35:07 +02009269 uint32_t i, this_op;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009270 struct lyxp_set orig_set, set2;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009271
9272 assert(repeat);
9273
9274 set_init(&orig_set, set);
9275 set_init(&set2, set);
9276
9277 set_fill_set(&orig_set, set);
9278
Michal Vasko004d3152020-06-11 19:59:22 +02009279 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009280 LY_CHECK_GOTO(rc, cleanup);
9281
9282 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
9283 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02009284 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009285
Michal Vasko004d3152020-06-11 19:59:22 +02009286 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_COMP);
aPiecek8b0cc152021-05-31 16:40:31 +02009287 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02009288 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02009289 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009290
aPiecek8b0cc152021-05-31 16:40:31 +02009291 if (options & LYXP_SKIP_EXPR) {
9292 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009293 LY_CHECK_GOTO(rc, cleanup);
9294 continue;
9295 }
9296
9297 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02009298 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009299 LY_CHECK_GOTO(rc, cleanup);
Michal Vaskob7ba2c92024-01-05 15:17:53 +01009300 if (set2.not_found) {
9301 set->not_found = 1;
9302 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02009303
9304 /* eval */
9305 if (options & LYXP_SCNODE_ALL) {
9306 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01009307 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02009308 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009309 } else {
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02009310 ly_bool result;
9311
9312 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], &result);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009313 LY_CHECK_GOTO(rc, cleanup);
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02009314 set_fill_boolean(set, result);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009315 }
9316 }
9317
9318cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02009319 lyxp_set_free_content(&orig_set);
9320 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009321 return rc;
9322}
9323
9324/**
9325 * @brief Evaluate EqualityExpr. Logs directly on error.
9326 *
Michal Vaskod3678892020-05-21 10:06:58 +02009327 * [15] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02009328 * | EqualityExpr '!=' RelationalExpr
9329 *
9330 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02009331 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009332 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02009333 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009334 * @param[in] options XPath options.
9335 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
9336 */
9337static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02009338eval_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 +02009339{
Michal Vaskob7ba2c92024-01-05 15:17:53 +01009340 LY_ERR rc = LY_SUCCESS;
Michal Vaskodd528af2022-08-08 14:35:07 +02009341 uint32_t i, this_op;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009342 struct lyxp_set orig_set, set2;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009343
9344 assert(repeat);
9345
9346 set_init(&orig_set, set);
9347 set_init(&set2, set);
9348
9349 set_fill_set(&orig_set, set);
9350
Michal Vasko004d3152020-06-11 19:59:22 +02009351 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009352 LY_CHECK_GOTO(rc, cleanup);
9353
9354 /* ('=' / '!=' RelationalExpr)* */
9355 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02009356 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009357
Michal Vasko004d3152020-06-11 19:59:22 +02009358 assert((exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_EQUAL) || (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_NEQUAL));
aPiecek8b0cc152021-05-31 16:40:31 +02009359 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02009360 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02009361 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009362
aPiecek8b0cc152021-05-31 16:40:31 +02009363 if (options & LYXP_SKIP_EXPR) {
9364 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009365 LY_CHECK_GOTO(rc, cleanup);
9366 continue;
9367 }
9368
9369 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02009370 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009371 LY_CHECK_GOTO(rc, cleanup);
Michal Vaskob7ba2c92024-01-05 15:17:53 +01009372 if (set2.not_found) {
9373 set->not_found = 1;
9374 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02009375
9376 /* eval */
9377 if (options & LYXP_SCNODE_ALL) {
9378 warn_operands(set->ctx, set, &set2, 0, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vasko004d3152020-06-11 19:59:22 +02009379 warn_equality_value(exp, set, *tok_idx - 1, this_op - 1, *tok_idx - 1);
9380 warn_equality_value(exp, &set2, this_op - 1, this_op - 1, *tok_idx - 1);
Michal Vaskoecd62de2019-11-13 12:35:11 +01009381 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02009382 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009383 } else {
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02009384 ly_bool result;
9385
9386 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], &result);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009387 LY_CHECK_GOTO(rc, cleanup);
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02009388 set_fill_boolean(set, result);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009389 }
9390 }
9391
9392cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02009393 lyxp_set_free_content(&orig_set);
9394 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009395 return rc;
9396}
9397
9398/**
9399 * @brief Evaluate AndExpr. Logs directly on error.
9400 *
Michal Vaskod3678892020-05-21 10:06:58 +02009401 * [14] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02009402 *
9403 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02009404 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009405 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02009406 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009407 * @param[in] options XPath options.
9408 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
9409 */
9410static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02009411eval_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 +02009412{
Michal Vaskob7ba2c92024-01-05 15:17:53 +01009413 LY_ERR rc = LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009414 struct lyxp_set orig_set, set2;
Michal Vaskodd528af2022-08-08 14:35:07 +02009415 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009416
9417 assert(repeat);
9418
9419 set_init(&orig_set, set);
9420 set_init(&set2, set);
9421
9422 set_fill_set(&orig_set, set);
9423
Michal Vasko004d3152020-06-11 19:59:22 +02009424 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009425 LY_CHECK_GOTO(rc, cleanup);
9426
Michal Vasko3ff82dd2023-05-09 14:09:21 +02009427 if (!(options & LYXP_SKIP_EXPR)) {
9428 if (options & LYXP_SCNODE_ALL) {
9429 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
9430 } else {
9431 /* cast to boolean, we know that will be the final result */
9432 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
9433 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02009434 }
9435
9436 /* ('and' EqualityExpr)* */
9437 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02009438 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG);
aPiecek8b0cc152021-05-31 16:40:31 +02009439 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, ((options & LYXP_SKIP_EXPR) || !set->val.bln ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02009440 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02009441 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009442
9443 /* lazy evaluation */
aPiecek8b0cc152021-05-31 16:40:31 +02009444 if ((options & LYXP_SKIP_EXPR) || ((set->type == LYXP_SET_BOOLEAN) && !set->val.bln)) {
9445 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009446 LY_CHECK_GOTO(rc, cleanup);
9447 continue;
9448 }
9449
9450 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02009451 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009452 LY_CHECK_GOTO(rc, cleanup);
Michal Vaskob7ba2c92024-01-05 15:17:53 +01009453 if (set2.not_found) {
9454 set->not_found = 1;
9455 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02009456
9457 /* eval - just get boolean value actually */
9458 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vasko1a09b212021-05-06 13:00:10 +02009459 set_scnode_clear_ctx(&set2, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskoecd62de2019-11-13 12:35:11 +01009460 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009461 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009462 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009463 set_fill_set(set, &set2);
9464 }
9465 }
9466
9467cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02009468 lyxp_set_free_content(&orig_set);
9469 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009470 return rc;
9471}
9472
9473/**
9474 * @brief Evaluate OrExpr. Logs directly on error.
9475 *
Michal Vaskod3678892020-05-21 10:06:58 +02009476 * [13] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02009477 *
9478 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02009479 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009480 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02009481 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009482 * @param[in] options XPath options.
9483 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
9484 */
9485static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02009486eval_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 +02009487{
Michal Vaskob7ba2c92024-01-05 15:17:53 +01009488 LY_ERR rc = LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009489 struct lyxp_set orig_set, set2;
Michal Vaskodd528af2022-08-08 14:35:07 +02009490 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009491
9492 assert(repeat);
9493
9494 set_init(&orig_set, set);
9495 set_init(&set2, set);
9496
9497 set_fill_set(&orig_set, set);
9498
Michal Vasko004d3152020-06-11 19:59:22 +02009499 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009500 LY_CHECK_GOTO(rc, cleanup);
9501
Michal Vasko3ff82dd2023-05-09 14:09:21 +02009502 if (!(options & LYXP_SKIP_EXPR)) {
9503 if (options & LYXP_SCNODE_ALL) {
9504 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
9505 } else {
9506 /* cast to boolean, we know that will be the final result */
9507 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
9508 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02009509 }
9510
9511 /* ('or' AndExpr)* */
9512 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02009513 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG);
aPiecek8b0cc152021-05-31 16:40:31 +02009514 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, ((options & LYXP_SKIP_EXPR) || set->val.bln ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02009515 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02009516 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009517
9518 /* lazy evaluation */
aPiecek8b0cc152021-05-31 16:40:31 +02009519 if ((options & LYXP_SKIP_EXPR) || ((set->type == LYXP_SET_BOOLEAN) && set->val.bln)) {
9520 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009521 LY_CHECK_GOTO(rc, cleanup);
9522 continue;
9523 }
9524
9525 set_fill_set(&set2, &orig_set);
9526 /* expr_type cound have been LYXP_EXPR_NONE in all these later calls (except for the first one),
9527 * but it does not matter */
Michal Vasko004d3152020-06-11 19:59:22 +02009528 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009529 LY_CHECK_GOTO(rc, cleanup);
Michal Vaskob7ba2c92024-01-05 15:17:53 +01009530 if (set2.not_found) {
9531 set->not_found = 1;
9532 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02009533
9534 /* eval - just get boolean value actually */
9535 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vasko1a09b212021-05-06 13:00:10 +02009536 set_scnode_clear_ctx(&set2, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskoecd62de2019-11-13 12:35:11 +01009537 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009538 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009539 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009540 set_fill_set(set, &set2);
9541 }
9542 }
9543
9544cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02009545 lyxp_set_free_content(&orig_set);
9546 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009547 return rc;
9548}
9549
9550/**
Michal Vasko004d3152020-06-11 19:59:22 +02009551 * @brief Decide what expression is at the pointer @p tok_idx and evaluate it accordingly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009552 *
9553 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02009554 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009555 * @param[in] etype Expression type to evaluate.
aPiecek8b0cc152021-05-31 16:40:31 +02009556 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009557 * @param[in] options XPath options.
9558 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
9559 */
9560static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02009561eval_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 +02009562 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02009563{
Michal Vaskodd528af2022-08-08 14:35:07 +02009564 uint32_t i, count;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009565 enum lyxp_expr_type next_etype;
9566 LY_ERR rc;
9567
9568 /* process operator repeats */
Michal Vasko004d3152020-06-11 19:59:22 +02009569 if (!exp->repeat[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02009570 next_etype = LYXP_EXPR_NONE;
9571 } else {
9572 /* find etype repeat */
Radek Krejci1e008d22020-08-17 11:37:37 +02009573 for (i = 0; exp->repeat[*tok_idx][i] > etype; ++i) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02009574
9575 /* select one-priority lower because etype expression called us */
9576 if (i) {
Michal Vasko004d3152020-06-11 19:59:22 +02009577 next_etype = exp->repeat[*tok_idx][i - 1];
Michal Vasko03ff5a72019-09-11 13:49:33 +02009578 /* count repeats for that expression */
Radek Krejci1e008d22020-08-17 11:37:37 +02009579 for (count = 0; i && exp->repeat[*tok_idx][i - 1] == next_etype; ++count, --i) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02009580 } else {
9581 next_etype = LYXP_EXPR_NONE;
9582 }
9583 }
9584
9585 /* decide what expression are we parsing based on the repeat */
9586 switch (next_etype) {
9587 case LYXP_EXPR_OR:
Michal Vasko004d3152020-06-11 19:59:22 +02009588 rc = eval_or_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009589 break;
9590 case LYXP_EXPR_AND:
Michal Vasko004d3152020-06-11 19:59:22 +02009591 rc = eval_and_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009592 break;
9593 case LYXP_EXPR_EQUALITY:
Michal Vasko004d3152020-06-11 19:59:22 +02009594 rc = eval_equality_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009595 break;
9596 case LYXP_EXPR_RELATIONAL:
Michal Vasko004d3152020-06-11 19:59:22 +02009597 rc = eval_relational_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009598 break;
9599 case LYXP_EXPR_ADDITIVE:
Michal Vasko004d3152020-06-11 19:59:22 +02009600 rc = eval_additive_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009601 break;
9602 case LYXP_EXPR_MULTIPLICATIVE:
Michal Vasko004d3152020-06-11 19:59:22 +02009603 rc = eval_multiplicative_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009604 break;
9605 case LYXP_EXPR_UNARY:
Michal Vasko004d3152020-06-11 19:59:22 +02009606 rc = eval_unary_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009607 break;
9608 case LYXP_EXPR_UNION:
Michal Vasko004d3152020-06-11 19:59:22 +02009609 rc = eval_union_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009610 break;
9611 case LYXP_EXPR_NONE:
Michal Vasko004d3152020-06-11 19:59:22 +02009612 rc = eval_path_expr(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009613 break;
9614 default:
9615 LOGINT_RET(set->ctx);
9616 }
9617
9618 return rc;
9619}
9620
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009621/**
9622 * @brief Get root type.
9623 *
9624 * @param[in] ctx_node Context node.
9625 * @param[in] ctx_scnode Schema context node.
9626 * @param[in] options XPath options.
9627 * @return Root type.
9628 */
9629static enum lyxp_node_type
Radek Krejci1deb5be2020-08-26 16:43:36 +02009630lyxp_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 +01009631{
Michal Vasko6b26e742020-07-17 15:02:10 +02009632 const struct lysc_node *op;
9633
Michal Vaskoa27245c2022-05-02 09:01:35 +02009634 /* explicit */
9635 if (options & LYXP_ACCESS_TREE_ALL) {
9636 return LYXP_NODE_ROOT;
9637 } else if (options & LYXP_ACCESS_TREE_CONFIG) {
9638 return LYXP_NODE_ROOT_CONFIG;
9639 }
9640
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009641 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009642 /* schema */
Radek Krejci1e008d22020-08-17 11:37:37 +02009643 for (op = ctx_scnode; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent) {}
Michal Vasko6b26e742020-07-17 15:02:10 +02009644
Michal Vasko53575612023-12-15 16:09:11 +01009645 if (op || !(options & LYXP_SCNODE_SCHEMA)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009646 /* general root that can access everything */
9647 return LYXP_NODE_ROOT;
9648 } else if (!ctx_scnode || (ctx_scnode->flags & LYS_CONFIG_W)) {
9649 /* root context node can access only config data (because we said so, it is unspecified) */
9650 return LYXP_NODE_ROOT_CONFIG;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009651 }
Michal Vasko6b26e742020-07-17 15:02:10 +02009652 return LYXP_NODE_ROOT;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009653 }
9654
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009655 /* data */
Michal Vasko6b26e742020-07-17 15:02:10 +02009656 op = ctx_node ? ctx_node->schema : NULL;
Michal Vaskod989ba02020-08-24 10:59:24 +02009657 for ( ; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent) {}
Michal Vasko6b26e742020-07-17 15:02:10 +02009658
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009659 if (op || !(options & LYXP_SCHEMA)) {
9660 /* general root that can access everything */
9661 return LYXP_NODE_ROOT;
Christian Hoppsb6ecaea2021-02-06 09:45:38 -05009662 } else if (!ctx_node || !ctx_node->schema || (ctx_node->schema->flags & LYS_CONFIG_W)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009663 /* root context node can access only config data (because we said so, it is unspecified) */
9664 return LYXP_NODE_ROOT_CONFIG;
9665 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009666 return LYXP_NODE_ROOT;
9667}
9668
Michal Vasko03ff5a72019-09-11 13:49:33 +02009669LY_ERR
Michal Vasko400e9672021-01-11 13:39:17 +01009670lyxp_eval(const struct ly_ctx *ctx, const struct lyxp_expr *exp, const struct lys_module *cur_mod,
Michal Vaskoa3e92bc2022-07-29 14:56:23 +02009671 LY_VALUE_FORMAT format, void *prefix_data, const struct lyd_node *cur_node, const struct lyd_node *ctx_node,
9672 const struct lyd_node *tree, const struct lyxp_var *vars, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02009673{
Michal Vaskodd528af2022-08-08 14:35:07 +02009674 uint32_t tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009675 LY_ERR rc;
9676
Michal Vasko400e9672021-01-11 13:39:17 +01009677 LY_CHECK_ARG_RET(ctx, ctx, exp, set, LY_EINVAL);
Radek Krejci8df109d2021-04-23 12:19:08 +02009678 if (!cur_mod && ((format == LY_VALUE_SCHEMA) || (format == LY_VALUE_SCHEMA_RESOLVED))) {
Michal Vaskoddd76592022-01-17 13:34:48 +01009679 LOGERR(ctx, LY_EINVAL, "Current module must be set if schema format is used.");
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009680 return LY_EINVAL;
Michal Vasko004d3152020-06-11 19:59:22 +02009681 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02009682
Michal Vaskod3bb12f2020-12-04 14:33:09 +01009683 if (tree) {
9684 /* adjust the pointer to be the first top-level sibling */
9685 while (tree->parent) {
9686 tree = lyd_parent(tree);
9687 }
9688 tree = lyd_first_sibling(tree);
Michal Vaskoddd76592022-01-17 13:34:48 +01009689
Michal Vasko6f78a772022-11-10 08:13:52 +01009690 if (lysc_data_parent(tree->schema)) {
Michal Vaskoddd76592022-01-17 13:34:48 +01009691 /* unable to evaluate absolute paths */
9692 LOGERR(ctx, LY_EINVAL, "Data node \"%s\" has no parent but is not instance of a top-level schema node.",
9693 LYD_NAME(tree));
9694 return LY_EINVAL;
9695 }
Michal Vaskod3bb12f2020-12-04 14:33:09 +01009696 }
9697
Michal Vasko03ff5a72019-09-11 13:49:33 +02009698 /* prepare set for evaluation */
Michal Vasko03ff5a72019-09-11 13:49:33 +02009699 memset(set, 0, sizeof *set);
Michal Vaskod3678892020-05-21 10:06:58 +02009700 set->type = LYXP_SET_NODE_SET;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009701 set->root_type = lyxp_get_root_type(ctx_node, NULL, options);
9702 set_insert_node(set, (struct lyd_node *)ctx_node, 0, ctx_node ? LYXP_NODE_ELEM : set->root_type, 0);
9703
Michal Vasko400e9672021-01-11 13:39:17 +01009704 set->ctx = (struct ly_ctx *)ctx;
Michal Vaskoa3e92bc2022-07-29 14:56:23 +02009705 set->cur_node = cur_node;
9706 for (set->context_op = cur_node ? cur_node->schema : NULL;
Radek Krejci0f969882020-08-21 16:56:47 +02009707 set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF));
9708 set->context_op = set->context_op->parent) {}
Michal Vaskof03ed032020-03-04 13:31:44 +01009709 set->tree = tree;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009710 set->cur_mod = cur_mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009711 set->format = format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009712 set->prefix_data = prefix_data;
aPiecekfba75362021-10-07 12:39:48 +02009713 set->vars = vars;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009714
Radek Krejciddace2c2021-01-08 11:30:56 +01009715 LOG_LOCSET(NULL, set->cur_node, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01009716
Michal Vasko03ff5a72019-09-11 13:49:33 +02009717 /* evaluate */
Michal Vasko004d3152020-06-11 19:59:22 +02009718 rc = eval_expr_select(exp, &tok_idx, 0, set, options);
Michal Vaskob7ba2c92024-01-05 15:17:53 +01009719 if (!rc && set->not_found) {
9720 rc = LY_ENOTFOUND;
9721 }
9722 if (rc) {
Michal Vaskod3678892020-05-21 10:06:58 +02009723 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009724 }
9725
Michal Vasko4a7d4d62021-12-13 17:05:06 +01009726 if (set->cur_node) {
9727 LOG_LOCBACK(0, 1, 0, 0);
9728 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02009729 return rc;
9730}
9731
9732#if 0
9733
9734/* full xml printing of set elements, not used currently */
9735
9736void
9737lyxp_set_print_xml(FILE *f, struct lyxp_set *set)
9738{
9739 uint32_t i;
9740 char *str_num;
9741 struct lyout out;
9742
9743 memset(&out, 0, sizeof out);
9744
9745 out.type = LYOUT_STREAM;
9746 out.method.f = f;
9747
9748 switch (set->type) {
9749 case LYXP_SET_EMPTY:
Michal Vasko5233e962020-08-14 14:26:20 +02009750 ly_print_(&out, "Empty XPath set\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02009751 break;
9752 case LYXP_SET_BOOLEAN:
Michal Vasko5233e962020-08-14 14:26:20 +02009753 ly_print_(&out, "Boolean XPath set:\n");
9754 ly_print_(&out, "%s\n\n", set->value.bool ? "true" : "false");
Michal Vasko03ff5a72019-09-11 13:49:33 +02009755 break;
9756 case LYXP_SET_STRING:
Michal Vasko5233e962020-08-14 14:26:20 +02009757 ly_print_(&out, "String XPath set:\n");
9758 ly_print_(&out, "\"%s\"\n\n", set->value.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009759 break;
9760 case LYXP_SET_NUMBER:
Michal Vasko5233e962020-08-14 14:26:20 +02009761 ly_print_(&out, "Number XPath set:\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02009762
9763 if (isnan(set->value.num)) {
9764 str_num = strdup("NaN");
9765 } else if ((set->value.num == 0) || (set->value.num == -0.0f)) {
9766 str_num = strdup("0");
9767 } else if (isinf(set->value.num) && !signbit(set->value.num)) {
9768 str_num = strdup("Infinity");
9769 } else if (isinf(set->value.num) && signbit(set->value.num)) {
9770 str_num = strdup("-Infinity");
9771 } else if ((long long)set->value.num == set->value.num) {
9772 if (asprintf(&str_num, "%lld", (long long)set->value.num) == -1) {
9773 str_num = NULL;
9774 }
9775 } else {
9776 if (asprintf(&str_num, "%03.1Lf", set->value.num) == -1) {
9777 str_num = NULL;
9778 }
9779 }
9780 if (!str_num) {
9781 LOGMEM;
9782 return;
9783 }
Michal Vasko5233e962020-08-14 14:26:20 +02009784 ly_print_(&out, "%s\n\n", str_num);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009785 free(str_num);
9786 break;
9787 case LYXP_SET_NODE_SET:
Michal Vasko5233e962020-08-14 14:26:20 +02009788 ly_print_(&out, "Node XPath set:\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02009789
9790 for (i = 0; i < set->used; ++i) {
Michal Vasko5233e962020-08-14 14:26:20 +02009791 ly_print_(&out, "%d. ", i + 1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009792 switch (set->node_type[i]) {
9793 case LYXP_NODE_ROOT_ALL:
Michal Vasko5233e962020-08-14 14:26:20 +02009794 ly_print_(&out, "ROOT all\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02009795 break;
9796 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko5233e962020-08-14 14:26:20 +02009797 ly_print_(&out, "ROOT config\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02009798 break;
9799 case LYXP_NODE_ROOT_STATE:
Michal Vasko5233e962020-08-14 14:26:20 +02009800 ly_print_(&out, "ROOT state\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02009801 break;
9802 case LYXP_NODE_ROOT_NOTIF:
Michal Vasko5233e962020-08-14 14:26:20 +02009803 ly_print_(&out, "ROOT notification \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009804 break;
9805 case LYXP_NODE_ROOT_RPC:
Michal Vasko5233e962020-08-14 14:26:20 +02009806 ly_print_(&out, "ROOT rpc \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009807 break;
9808 case LYXP_NODE_ROOT_OUTPUT:
Michal Vasko5233e962020-08-14 14:26:20 +02009809 ly_print_(&out, "ROOT output \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009810 break;
9811 case LYXP_NODE_ELEM:
Michal Vasko5233e962020-08-14 14:26:20 +02009812 ly_print_(&out, "ELEM \"%s\"\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009813 xml_print_node(&out, 1, set->value.nodes[i], 1, LYP_FORMAT);
Michal Vasko5233e962020-08-14 14:26:20 +02009814 ly_print_(&out, "\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02009815 break;
9816 case LYXP_NODE_TEXT:
Michal Vasko5233e962020-08-14 14:26:20 +02009817 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 +02009818 break;
9819 case LYXP_NODE_ATTR:
Michal Vasko5233e962020-08-14 14:26:20 +02009820 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 +02009821 break;
9822 }
9823 }
9824 break;
9825 }
9826}
9827
9828#endif
9829
9830LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009831lyxp_set_cast(struct lyxp_set *set, enum lyxp_set_type target)
Michal Vasko03ff5a72019-09-11 13:49:33 +02009832{
9833 long double num;
9834 char *str;
9835 LY_ERR rc;
9836
9837 if (!set || (set->type == target)) {
9838 return LY_SUCCESS;
9839 }
9840
9841 /* it's not possible to convert anything into a node set */
Michal Vaskod3678892020-05-21 10:06:58 +02009842 assert(target != LYXP_SET_NODE_SET);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009843
9844 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vaskod3678892020-05-21 10:06:58 +02009845 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009846 return LY_EINVAL;
9847 }
9848
9849 /* to STRING */
Michal Vaskod3678892020-05-21 10:06:58 +02009850 if ((target == LYXP_SET_STRING) || ((target == LYXP_SET_NUMBER) && (set->type == LYXP_SET_NODE_SET))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02009851 switch (set->type) {
9852 case LYXP_SET_NUMBER:
9853 if (isnan(set->val.num)) {
9854 set->val.str = strdup("NaN");
9855 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
9856 } else if ((set->val.num == 0) || (set->val.num == -0.0f)) {
9857 set->val.str = strdup("0");
9858 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
9859 } else if (isinf(set->val.num) && !signbit(set->val.num)) {
9860 set->val.str = strdup("Infinity");
9861 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
9862 } else if (isinf(set->val.num) && signbit(set->val.num)) {
9863 set->val.str = strdup("-Infinity");
9864 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
9865 } else if ((long long)set->val.num == set->val.num) {
9866 if (asprintf(&str, "%lld", (long long)set->val.num) == -1) {
9867 LOGMEM_RET(set->ctx);
9868 }
9869 set->val.str = str;
9870 } else {
9871 if (asprintf(&str, "%03.1Lf", set->val.num) == -1) {
9872 LOGMEM_RET(set->ctx);
9873 }
9874 set->val.str = str;
9875 }
9876 break;
9877 case LYXP_SET_BOOLEAN:
Michal Vasko004d3152020-06-11 19:59:22 +02009878 if (set->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02009879 set->val.str = strdup("true");
9880 } else {
9881 set->val.str = strdup("false");
9882 }
9883 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), LY_EMEM);
9884 break;
9885 case LYXP_SET_NODE_SET:
Michal Vasko03ff5a72019-09-11 13:49:33 +02009886 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009887 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02009888
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009889 rc = cast_node_set_to_string(set, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009890 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02009891 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009892 set->val.str = str;
9893 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009894 default:
9895 LOGINT_RET(set->ctx);
9896 }
9897 set->type = LYXP_SET_STRING;
9898 }
9899
9900 /* to NUMBER */
9901 if (target == LYXP_SET_NUMBER) {
9902 switch (set->type) {
9903 case LYXP_SET_STRING:
9904 num = cast_string_to_number(set->val.str);
Michal Vaskod3678892020-05-21 10:06:58 +02009905 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009906 set->val.num = num;
9907 break;
9908 case LYXP_SET_BOOLEAN:
Michal Vasko004d3152020-06-11 19:59:22 +02009909 if (set->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02009910 set->val.num = 1;
9911 } else {
9912 set->val.num = 0;
9913 }
9914 break;
9915 default:
9916 LOGINT_RET(set->ctx);
9917 }
9918 set->type = LYXP_SET_NUMBER;
9919 }
9920
9921 /* to BOOLEAN */
9922 if (target == LYXP_SET_BOOLEAN) {
9923 switch (set->type) {
9924 case LYXP_SET_NUMBER:
9925 if ((set->val.num == 0) || (set->val.num == -0.0f) || isnan(set->val.num)) {
Michal Vasko004d3152020-06-11 19:59:22 +02009926 set->val.bln = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009927 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02009928 set->val.bln = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009929 }
9930 break;
9931 case LYXP_SET_STRING:
9932 if (set->val.str[0]) {
Michal Vaskod3678892020-05-21 10:06:58 +02009933 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02009934 set->val.bln = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009935 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02009936 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02009937 set->val.bln = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009938 }
9939 break;
9940 case LYXP_SET_NODE_SET:
Michal Vaskod3678892020-05-21 10:06:58 +02009941 if (set->used) {
9942 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02009943 set->val.bln = 1;
Michal Vaskod3678892020-05-21 10:06:58 +02009944 } else {
9945 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02009946 set->val.bln = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02009947 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02009948 break;
9949 default:
9950 LOGINT_RET(set->ctx);
9951 }
9952 set->type = LYXP_SET_BOOLEAN;
9953 }
9954
Michal Vasko03ff5a72019-09-11 13:49:33 +02009955 return LY_SUCCESS;
9956}
9957
9958LY_ERR
Michal Vasko400e9672021-01-11 13:39:17 +01009959lyxp_atomize(const struct ly_ctx *ctx, const struct lyxp_expr *exp, const struct lys_module *cur_mod,
Michal Vaskoa3e92bc2022-07-29 14:56:23 +02009960 LY_VALUE_FORMAT format, void *prefix_data, const struct lysc_node *cur_scnode,
9961 const struct lysc_node *ctx_scnode, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02009962{
Michal Vaskob7ba2c92024-01-05 15:17:53 +01009963 LY_ERR rc;
Michal Vaskodd528af2022-08-08 14:35:07 +02009964 uint32_t tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009965
Michal Vasko400e9672021-01-11 13:39:17 +01009966 LY_CHECK_ARG_RET(ctx, ctx, exp, set, LY_EINVAL);
Radek Krejci8df109d2021-04-23 12:19:08 +02009967 if (!cur_mod && ((format == LY_VALUE_SCHEMA) || (format == LY_VALUE_SCHEMA_RESOLVED))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009968 LOGARG(NULL, "Current module must be set if schema format is used.");
9969 return LY_EINVAL;
Michal Vasko004d3152020-06-11 19:59:22 +02009970 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02009971
9972 /* prepare set for evaluation */
Michal Vasko03ff5a72019-09-11 13:49:33 +02009973 memset(set, 0, sizeof *set);
9974 set->type = LYXP_SET_SCNODE_SET;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009975 set->root_type = lyxp_get_root_type(NULL, ctx_scnode, options);
Michal Vaskoe4a6d012023-05-22 14:34:52 +02009976 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 +01009977 set->val.scnodes[0].in_ctx = LYXP_SET_SCNODE_START;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009978
Michal Vasko400e9672021-01-11 13:39:17 +01009979 set->ctx = (struct ly_ctx *)ctx;
Michal Vaskoa3e92bc2022-07-29 14:56:23 +02009980 set->cur_scnode = cur_scnode;
9981 for (set->context_op = cur_scnode;
Radek Krejci0f969882020-08-21 16:56:47 +02009982 set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF));
9983 set->context_op = set->context_op->parent) {}
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009984 set->cur_mod = cur_mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009985 set->format = format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009986 set->prefix_data = prefix_data;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009987
Radek Krejciddace2c2021-01-08 11:30:56 +01009988 LOG_LOCSET(set->cur_scnode, NULL, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01009989
Michal Vasko03ff5a72019-09-11 13:49:33 +02009990 /* evaluate */
Michal Vaskob7ba2c92024-01-05 15:17:53 +01009991 rc = eval_expr_select(exp, &tok_idx, 0, set, options);
9992 if (!rc && set->not_found) {
9993 rc = LY_ENOTFOUND;
9994 }
Radek Krejci2efc45b2020-12-22 16:25:44 +01009995
Michal Vaskof69e4552022-12-02 10:27:42 +01009996 LOG_LOCBACK(set->cur_scnode ? 1 : 0, 0, 0, 0);
Michal Vaskob7ba2c92024-01-05 15:17:53 +01009997 return rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009998}
Michal Vaskod43d71a2020-08-07 14:54:58 +02009999
Jan Kundrátc53a7ec2021-12-09 16:01:19 +010010000LIBYANG_API_DEF const char *
Michal Vaskod43d71a2020-08-07 14:54:58 +020010001lyxp_get_expr(const struct lyxp_expr *path)
10002{
10003 if (!path) {
10004 return NULL;
10005 }
10006
10007 return path->expr;
10008}