blob: f549b498651c5f74141ef262d81c6f1009968854 [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
Michal Vasko5aa44c02020-06-29 11:47:02 +020027#include "compat.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020028#include "context.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020029#include "dict.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020030#include "hash_table.h"
Michal Vasko8f702ee2024-02-20 15:44:24 +010031#include "ly_common.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);
Michal Vasko01f1eb72024-02-20 15:49:49 +0100571 LY_CHECK_RET(rc);
Michal Vasko47da6562022-07-14 15:43:15 +0200572 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 Vaskoce46ced2024-01-25 15:28:41 +01001700 LY_ERR r;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001701
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 */
Michal Vaskoce46ced2024-01-25 15:28:41 +01001731 r = 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);
Michal Vaskoce46ced2024-01-25 15:28:41 +01001734 if (r && (r != LY_EINCOMPLETE)) {
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) {
Michal Vasko21eaa392024-02-20 15:48:42 +01002396 LOGVAL(ctx, LY_VCODE_XP_INFUNC, (int)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)) {
Michal Vasko21eaa392024-02-20 15:48:42 +01002429 LOGVAL(ctx, LY_VCODE_XP_INARGCOUNT, arg_count, (int)exp->tok_len[func_tok_idx],
2430 &exp->expr[exp->tok_pos[func_tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002431 return LY_EVALID;
2432 }
2433
2434 return LY_SUCCESS;
2435}
2436
2437/**
2438 * @brief Reparse PathExpr. Logs directly on error.
2439 *
2440 * [10] PathExpr ::= LocationPath | PrimaryExpr Predicate*
2441 * | PrimaryExpr Predicate* '/' RelativeLocationPath
2442 * | PrimaryExpr Predicate* '//' RelativeLocationPath
2443 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
aPiecekfba75362021-10-07 12:39:48 +02002444 * [8] PrimaryExpr ::= VariableReference | '(' Expr ')' | Literal | Number | FunctionCall
Michal Vasko03ff5a72019-09-11 13:49:33 +02002445 *
2446 * @param[in] ctx Context for logging.
2447 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002448 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002449 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002450 * @return LY_ERR
2451 */
2452static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02002453reparse_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 +02002454{
2455 LY_ERR rc;
2456
Michal Vasko004d3152020-06-11 19:59:22 +02002457 if (lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko14676352020-05-29 11:35:55 +02002458 return LY_EVALID;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002459 }
2460
Michal Vasko004d3152020-06-11 19:59:22 +02002461 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002462 case LYXP_TOKEN_PAR1:
2463 /* '(' Expr ')' Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002464 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002465
aPiecekbf968d92021-05-27 14:35:05 +02002466 rc = reparse_or_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002467 LY_CHECK_RET(rc);
2468
Michal Vasko004d3152020-06-11 19:59:22 +02002469 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002470 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002471 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002472 goto predicate;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002473 case LYXP_TOKEN_DOT:
2474 case LYXP_TOKEN_DDOT:
Michal Vasko49fec8e2022-05-24 10:28:33 +02002475 case LYXP_TOKEN_AXISNAME:
Michal Vasko03ff5a72019-09-11 13:49:33 +02002476 case LYXP_TOKEN_AT:
2477 case LYXP_TOKEN_NAMETEST:
2478 case LYXP_TOKEN_NODETYPE:
2479 /* RelativeLocationPath */
aPiecekbf968d92021-05-27 14:35:05 +02002480 rc = reparse_relative_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002481 LY_CHECK_RET(rc);
2482 break;
aPiecekfba75362021-10-07 12:39:48 +02002483 case LYXP_TOKEN_VARREF:
2484 /* VariableReference */
2485 ++(*tok_idx);
2486 goto predicate;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002487 case LYXP_TOKEN_FUNCNAME:
2488 /* FunctionCall */
aPiecekbf968d92021-05-27 14:35:05 +02002489 rc = reparse_function_call(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002490 LY_CHECK_RET(rc);
2491 goto predicate;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002492 case LYXP_TOKEN_OPER_PATH:
2493 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +02002494 /* AbsoluteLocationPath */
aPiecekbf968d92021-05-27 14:35:05 +02002495 rc = reparse_absolute_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002496 LY_CHECK_RET(rc);
2497 break;
2498 case LYXP_TOKEN_LITERAL:
2499 /* Literal */
Michal Vasko004d3152020-06-11 19:59:22 +02002500 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002501 goto predicate;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002502 case LYXP_TOKEN_NUMBER:
2503 /* Number */
Michal Vasko004d3152020-06-11 19:59:22 +02002504 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002505 goto predicate;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002506 default:
Michal Vasko49fec8e2022-05-24 10:28:33 +02002507 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 +02002508 return LY_EVALID;
2509 }
2510
2511 return LY_SUCCESS;
2512
2513predicate:
2514 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002515 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
aPiecekbf968d92021-05-27 14:35:05 +02002516 rc = reparse_predicate(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002517 LY_CHECK_RET(rc);
2518 }
2519
2520 /* ('/' or '//') RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002521 if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002522
2523 /* '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02002524 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002525
aPiecekbf968d92021-05-27 14:35:05 +02002526 rc = reparse_relative_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002527 LY_CHECK_RET(rc);
2528 }
2529
2530 return LY_SUCCESS;
2531}
2532
2533/**
2534 * @brief Reparse UnaryExpr. Logs directly on error.
2535 *
2536 * [17] UnaryExpr ::= UnionExpr | '-' UnaryExpr
2537 * [18] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
2538 *
2539 * @param[in] ctx Context for logging.
2540 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002541 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002542 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002543 * @return LY_ERR
2544 */
2545static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02002546reparse_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 +02002547{
Michal Vaskodd528af2022-08-08 14:35:07 +02002548 uint32_t prev_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002549 LY_ERR rc;
2550
2551 /* ('-')* */
Michal Vasko004d3152020-06-11 19:59:22 +02002552 prev_exp = *tok_idx;
Michal Vasko69730152020-10-09 16:30:07 +02002553 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2554 (exp->expr[exp->tok_pos[*tok_idx]] == '-')) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002555 exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNARY);
Michal Vasko004d3152020-06-11 19:59:22 +02002556 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002557 }
2558
2559 /* PathExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002560 prev_exp = *tok_idx;
aPiecekbf968d92021-05-27 14:35:05 +02002561 rc = reparse_path_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002562 LY_CHECK_RET(rc);
2563
2564 /* ('|' PathExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002565 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_UNI)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002566 exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNION);
Michal Vasko004d3152020-06-11 19:59:22 +02002567 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002568
aPiecekbf968d92021-05-27 14:35:05 +02002569 rc = reparse_path_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002570 LY_CHECK_RET(rc);
2571 }
2572
2573 return LY_SUCCESS;
2574}
2575
2576/**
2577 * @brief Reparse AdditiveExpr. Logs directly on error.
2578 *
2579 * [15] AdditiveExpr ::= MultiplicativeExpr
2580 * | AdditiveExpr '+' MultiplicativeExpr
2581 * | AdditiveExpr '-' MultiplicativeExpr
2582 * [16] MultiplicativeExpr ::= UnaryExpr
2583 * | MultiplicativeExpr '*' UnaryExpr
2584 * | MultiplicativeExpr 'div' UnaryExpr
2585 * | MultiplicativeExpr 'mod' UnaryExpr
2586 *
2587 * @param[in] ctx Context for logging.
2588 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002589 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002590 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002591 * @return LY_ERR
2592 */
2593static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02002594reparse_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 +02002595{
Michal Vaskodd528af2022-08-08 14:35:07 +02002596 uint32_t prev_add_exp, prev_mul_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002597 LY_ERR rc;
2598
Michal Vasko004d3152020-06-11 19:59:22 +02002599 prev_add_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002600 goto reparse_multiplicative_expr;
2601
2602 /* ('+' / '-' MultiplicativeExpr)* */
Michal Vasko69730152020-10-09 16:30:07 +02002603 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2604 ((exp->expr[exp->tok_pos[*tok_idx]] == '+') || (exp->expr[exp->tok_pos[*tok_idx]] == '-'))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002605 exp_repeat_push(exp, prev_add_exp, LYXP_EXPR_ADDITIVE);
Michal Vasko004d3152020-06-11 19:59:22 +02002606 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002607
2608reparse_multiplicative_expr:
2609 /* UnaryExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002610 prev_mul_exp = *tok_idx;
aPiecekbf968d92021-05-27 14:35:05 +02002611 rc = reparse_unary_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002612 LY_CHECK_RET(rc);
2613
2614 /* ('*' / 'div' / 'mod' UnaryExpr)* */
Michal Vasko69730152020-10-09 16:30:07 +02002615 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2616 ((exp->expr[exp->tok_pos[*tok_idx]] == '*') || (exp->tok_len[*tok_idx] == 3))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002617 exp_repeat_push(exp, prev_mul_exp, LYXP_EXPR_MULTIPLICATIVE);
Michal Vasko004d3152020-06-11 19:59:22 +02002618 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002619
aPiecekbf968d92021-05-27 14:35:05 +02002620 rc = reparse_unary_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002621 LY_CHECK_RET(rc);
2622 }
2623 }
2624
2625 return LY_SUCCESS;
2626}
2627
2628/**
2629 * @brief Reparse EqualityExpr. Logs directly on error.
2630 *
2631 * [13] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
2632 * | EqualityExpr '!=' RelationalExpr
2633 * [14] RelationalExpr ::= AdditiveExpr
2634 * | RelationalExpr '<' AdditiveExpr
2635 * | RelationalExpr '>' AdditiveExpr
2636 * | RelationalExpr '<=' AdditiveExpr
2637 * | RelationalExpr '>=' AdditiveExpr
2638 *
2639 * @param[in] ctx Context for logging.
2640 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002641 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002642 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002643 * @return LY_ERR
2644 */
2645static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02002646reparse_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 +02002647{
Michal Vaskodd528af2022-08-08 14:35:07 +02002648 uint32_t prev_eq_exp, prev_rel_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002649 LY_ERR rc;
2650
Michal Vasko004d3152020-06-11 19:59:22 +02002651 prev_eq_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002652 goto reparse_additive_expr;
2653
2654 /* ('=' / '!=' RelationalExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002655 while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_EQUAL, LYXP_TOKEN_OPER_NEQUAL)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002656 exp_repeat_push(exp, prev_eq_exp, LYXP_EXPR_EQUALITY);
Michal Vasko004d3152020-06-11 19:59:22 +02002657 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002658
2659reparse_additive_expr:
2660 /* AdditiveExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002661 prev_rel_exp = *tok_idx;
aPiecekbf968d92021-05-27 14:35:05 +02002662 rc = reparse_additive_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002663 LY_CHECK_RET(rc);
2664
2665 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002666 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_COMP)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002667 exp_repeat_push(exp, prev_rel_exp, LYXP_EXPR_RELATIONAL);
Michal Vasko004d3152020-06-11 19:59:22 +02002668 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002669
aPiecekbf968d92021-05-27 14:35:05 +02002670 rc = reparse_additive_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002671 LY_CHECK_RET(rc);
2672 }
2673 }
2674
2675 return LY_SUCCESS;
2676}
2677
2678/**
2679 * @brief Reparse OrExpr. Logs directly on error.
2680 *
2681 * [11] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
2682 * [12] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
2683 *
2684 * @param[in] ctx Context for logging.
2685 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002686 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002687 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002688 * @return LY_ERR
2689 */
2690static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02002691reparse_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 +02002692{
Michal Vaskodd528af2022-08-08 14:35:07 +02002693 uint32_t prev_or_exp, prev_and_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002694 LY_ERR rc;
2695
aPiecekbf968d92021-05-27 14:35:05 +02002696 ++depth;
2697 LY_CHECK_ERR_RET(depth > LYXP_MAX_BLOCK_DEPTH, LOGVAL(ctx, LY_VCODE_XP_DEPTH), LY_EINVAL);
2698
Michal Vasko004d3152020-06-11 19:59:22 +02002699 prev_or_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002700 goto reparse_equality_expr;
2701
2702 /* ('or' AndExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002703 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 +02002704 exp_repeat_push(exp, prev_or_exp, LYXP_EXPR_OR);
Michal Vasko004d3152020-06-11 19:59:22 +02002705 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002706
2707reparse_equality_expr:
2708 /* EqualityExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002709 prev_and_exp = *tok_idx;
aPiecekbf968d92021-05-27 14:35:05 +02002710 rc = reparse_equality_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002711 LY_CHECK_RET(rc);
2712
2713 /* ('and' EqualityExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002714 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 +02002715 exp_repeat_push(exp, prev_and_exp, LYXP_EXPR_AND);
Michal Vasko004d3152020-06-11 19:59:22 +02002716 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002717
aPiecekbf968d92021-05-27 14:35:05 +02002718 rc = reparse_equality_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002719 LY_CHECK_RET(rc);
2720 }
2721 }
2722
2723 return LY_SUCCESS;
2724}
Radek Krejcib1646a92018-11-02 16:08:26 +01002725
2726/**
2727 * @brief Parse NCName.
2728 *
2729 * @param[in] ncname Name to parse.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002730 * @return Length of @p ncname valid bytes.
Radek Krejcib1646a92018-11-02 16:08:26 +01002731 */
Michal Vasko49fec8e2022-05-24 10:28:33 +02002732static ssize_t
Radek Krejcib1646a92018-11-02 16:08:26 +01002733parse_ncname(const char *ncname)
2734{
Radek Krejci1deb5be2020-08-26 16:43:36 +02002735 uint32_t uc;
Radek Krejcid4270262019-01-07 15:07:25 +01002736 size_t size;
Michal Vasko49fec8e2022-05-24 10:28:33 +02002737 ssize_t len = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01002738
2739 LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), 0);
2740 if (!is_xmlqnamestartchar(uc) || (uc == ':')) {
2741 return len;
2742 }
2743
2744 do {
2745 len += size;
Radek Krejci9a564c92019-01-07 14:53:57 +01002746 if (!*ncname) {
2747 break;
2748 }
Radek Krejcid4270262019-01-07 15:07:25 +01002749 LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), -len);
Radek Krejcib1646a92018-11-02 16:08:26 +01002750 } while (is_xmlqnamechar(uc) && (uc != ':'));
2751
2752 return len;
2753}
2754
2755/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02002756 * @brief Add @p token into the expression @p exp.
Radek Krejcib1646a92018-11-02 16:08:26 +01002757 *
Michal Vasko03ff5a72019-09-11 13:49:33 +02002758 * @param[in] ctx Context for logging.
Radek Krejcib1646a92018-11-02 16:08:26 +01002759 * @param[in] exp Expression to use.
2760 * @param[in] token Token to add.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002761 * @param[in] tok_pos Token position in the XPath expression.
Radek Krejcib1646a92018-11-02 16:08:26 +01002762 * @param[in] tok_len Token length in the XPath expression.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002763 * @return LY_ERR
Radek Krejcib1646a92018-11-02 16:08:26 +01002764 */
2765static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02002766exp_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 +01002767{
2768 uint32_t prev;
2769
2770 if (exp->used == exp->size) {
2771 prev = exp->size;
2772 exp->size += LYXP_EXPR_SIZE_STEP;
2773 if (prev > exp->size) {
2774 LOGINT(ctx);
2775 return LY_EINT;
2776 }
2777
2778 exp->tokens = ly_realloc(exp->tokens, exp->size * sizeof *exp->tokens);
2779 LY_CHECK_ERR_RET(!exp->tokens, LOGMEM(ctx), LY_EMEM);
2780 exp->tok_pos = ly_realloc(exp->tok_pos, exp->size * sizeof *exp->tok_pos);
2781 LY_CHECK_ERR_RET(!exp->tok_pos, LOGMEM(ctx), LY_EMEM);
2782 exp->tok_len = ly_realloc(exp->tok_len, exp->size * sizeof *exp->tok_len);
2783 LY_CHECK_ERR_RET(!exp->tok_len, LOGMEM(ctx), LY_EMEM);
2784 }
2785
2786 exp->tokens[exp->used] = token;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002787 exp->tok_pos[exp->used] = tok_pos;
Radek Krejcib1646a92018-11-02 16:08:26 +01002788 exp->tok_len[exp->used] = tok_len;
2789 ++exp->used;
2790 return LY_SUCCESS;
2791}
2792
2793void
Michal Vasko14676352020-05-29 11:35:55 +02002794lyxp_expr_free(const struct ly_ctx *ctx, struct lyxp_expr *expr)
Radek Krejcib1646a92018-11-02 16:08:26 +01002795{
Michal Vaskodd528af2022-08-08 14:35:07 +02002796 uint32_t i;
Radek Krejcib1646a92018-11-02 16:08:26 +01002797
2798 if (!expr) {
2799 return;
2800 }
2801
2802 lydict_remove(ctx, expr->expr);
2803 free(expr->tokens);
2804 free(expr->tok_pos);
2805 free(expr->tok_len);
2806 if (expr->repeat) {
2807 for (i = 0; i < expr->used; ++i) {
2808 free(expr->repeat[i]);
2809 }
2810 }
2811 free(expr->repeat);
2812 free(expr);
2813}
2814
Michal Vasko49fec8e2022-05-24 10:28:33 +02002815/**
2816 * @brief Parse Axis name.
2817 *
2818 * @param[in] str String to parse.
2819 * @param[in] str_len Length of @p str.
2820 * @return LY_SUCCESS if an axis.
2821 * @return LY_ENOT otherwise.
2822 */
2823static LY_ERR
2824expr_parse_axis(const char *str, size_t str_len)
2825{
2826 switch (str_len) {
2827 case 4:
2828 if (!strncmp("self", str, str_len)) {
2829 return LY_SUCCESS;
2830 }
2831 break;
2832 case 5:
2833 if (!strncmp("child", str, str_len)) {
2834 return LY_SUCCESS;
2835 }
2836 break;
2837 case 6:
2838 if (!strncmp("parent", str, str_len)) {
2839 return LY_SUCCESS;
2840 }
2841 break;
2842 case 8:
2843 if (!strncmp("ancestor", str, str_len)) {
2844 return LY_SUCCESS;
2845 }
2846 break;
2847 case 9:
2848 if (!strncmp("attribute", str, str_len)) {
2849 return LY_SUCCESS;
2850 } else if (!strncmp("following", str, str_len)) {
2851 return LY_SUCCESS;
2852 } else if (!strncmp("namespace", str, str_len)) {
2853 LOGERR(NULL, LY_EINVAL, "Axis \"namespace\" not supported.");
2854 return LY_ENOT;
2855 } else if (!strncmp("preceding", str, str_len)) {
2856 return LY_SUCCESS;
2857 }
2858 break;
2859 case 10:
2860 if (!strncmp("descendant", str, str_len)) {
2861 return LY_SUCCESS;
2862 }
2863 break;
2864 case 16:
2865 if (!strncmp("ancestor-or-self", str, str_len)) {
2866 return LY_SUCCESS;
2867 }
2868 break;
2869 case 17:
2870 if (!strncmp("following-sibling", str, str_len)) {
2871 return LY_SUCCESS;
2872 } else if (!strncmp("preceding-sibling", str, str_len)) {
2873 return LY_SUCCESS;
2874 }
2875 break;
2876 case 18:
2877 if (!strncmp("descendant-or-self", str, str_len)) {
2878 return LY_SUCCESS;
2879 }
2880 break;
2881 }
2882
2883 return LY_ENOT;
2884}
2885
Radek Krejcif03a9e22020-09-18 20:09:31 +02002886LY_ERR
2887lyxp_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 +01002888{
Radek Krejcif03a9e22020-09-18 20:09:31 +02002889 LY_ERR ret = LY_SUCCESS;
2890 struct lyxp_expr *expr;
Radek Krejcid4270262019-01-07 15:07:25 +01002891 size_t parsed = 0, tok_len;
Radek Krejcib1646a92018-11-02 16:08:26 +01002892 enum lyxp_token tok_type;
Michal Vasko49fec8e2022-05-24 10:28:33 +02002893 ly_bool prev_func_check = 0, prev_ntype_check = 0, has_axis;
Michal Vaskodd528af2022-08-08 14:35:07 +02002894 uint32_t tok_idx = 0;
Michal Vasko49fec8e2022-05-24 10:28:33 +02002895 ssize_t ncname_len;
Radek Krejcib1646a92018-11-02 16:08:26 +01002896
Radek Krejcif03a9e22020-09-18 20:09:31 +02002897 assert(expr_p);
2898
2899 if (!expr_str[0]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002900 LOGVAL(ctx, LY_VCODE_XP_EOF);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002901 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02002902 }
2903
2904 if (!expr_len) {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002905 expr_len = strlen(expr_str);
Michal Vasko004d3152020-06-11 19:59:22 +02002906 }
Michal Vaskodd528af2022-08-08 14:35:07 +02002907 if (expr_len > UINT32_MAX) {
2908 LOGVAL(ctx, LYVE_XPATH, "XPath expression cannot be longer than %" PRIu32 " characters.", UINT32_MAX);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002909 return LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01002910 }
2911
2912 /* init lyxp_expr structure */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002913 expr = calloc(1, sizeof *expr);
2914 LY_CHECK_ERR_GOTO(!expr, LOGMEM(ctx); ret = LY_EMEM, error);
2915 LY_CHECK_GOTO(ret = lydict_insert(ctx, expr_str, expr_len, &expr->expr), error);
2916 expr->used = 0;
2917 expr->size = LYXP_EXPR_SIZE_START;
2918 expr->tokens = malloc(expr->size * sizeof *expr->tokens);
2919 LY_CHECK_ERR_GOTO(!expr->tokens, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002920
Radek Krejcif03a9e22020-09-18 20:09:31 +02002921 expr->tok_pos = malloc(expr->size * sizeof *expr->tok_pos);
2922 LY_CHECK_ERR_GOTO(!expr->tok_pos, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002923
Radek Krejcif03a9e22020-09-18 20:09:31 +02002924 expr->tok_len = malloc(expr->size * sizeof *expr->tok_len);
2925 LY_CHECK_ERR_GOTO(!expr->tok_len, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002926
Michal Vasko004d3152020-06-11 19:59:22 +02002927 /* make expr 0-terminated */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002928 expr_str = expr->expr;
Michal Vasko004d3152020-06-11 19:59:22 +02002929
Radek Krejcif03a9e22020-09-18 20:09:31 +02002930 while (is_xmlws(expr_str[parsed])) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002931 ++parsed;
2932 }
2933
2934 do {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002935 if (expr_str[parsed] == '(') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002936
2937 /* '(' */
2938 tok_len = 1;
2939 tok_type = LYXP_TOKEN_PAR1;
2940
Michal Vasko49fec8e2022-05-24 10:28:33 +02002941 if (prev_ntype_check && expr->used && (expr->tokens[expr->used - 1] == LYXP_TOKEN_NAMETEST) &&
2942 (((expr->tok_len[expr->used - 1] == 4) &&
2943 (!strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "node", 4) ||
2944 !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "text", 4))) ||
2945 ((expr->tok_len[expr->used - 1] == 7) &&
2946 !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "comment", 7)))) {
2947 /* it is NodeType after all */
2948 expr->tokens[expr->used - 1] = LYXP_TOKEN_NODETYPE;
2949
2950 prev_ntype_check = 0;
2951 prev_func_check = 0;
2952 } else if (prev_func_check && expr->used && (expr->tokens[expr->used - 1] == LYXP_TOKEN_NAMETEST)) {
2953 /* it is FunctionName after all */
2954 expr->tokens[expr->used - 1] = LYXP_TOKEN_FUNCNAME;
2955
2956 prev_ntype_check = 0;
2957 prev_func_check = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01002958 }
2959
Radek Krejcif03a9e22020-09-18 20:09:31 +02002960 } else if (expr_str[parsed] == ')') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002961
2962 /* ')' */
2963 tok_len = 1;
2964 tok_type = LYXP_TOKEN_PAR2;
2965
Radek Krejcif03a9e22020-09-18 20:09:31 +02002966 } else if (expr_str[parsed] == '[') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002967
2968 /* '[' */
2969 tok_len = 1;
2970 tok_type = LYXP_TOKEN_BRACK1;
2971
Radek Krejcif03a9e22020-09-18 20:09:31 +02002972 } else if (expr_str[parsed] == ']') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002973
2974 /* ']' */
2975 tok_len = 1;
2976 tok_type = LYXP_TOKEN_BRACK2;
2977
Radek Krejcif03a9e22020-09-18 20:09:31 +02002978 } else if (!strncmp(&expr_str[parsed], "..", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002979
2980 /* '..' */
2981 tok_len = 2;
2982 tok_type = LYXP_TOKEN_DDOT;
2983
Radek Krejcif03a9e22020-09-18 20:09:31 +02002984 } else if ((expr_str[parsed] == '.') && (!isdigit(expr_str[parsed + 1]))) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002985
2986 /* '.' */
2987 tok_len = 1;
2988 tok_type = LYXP_TOKEN_DOT;
2989
Radek Krejcif03a9e22020-09-18 20:09:31 +02002990 } else if (expr_str[parsed] == '@') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002991
2992 /* '@' */
2993 tok_len = 1;
2994 tok_type = LYXP_TOKEN_AT;
2995
Radek Krejcif03a9e22020-09-18 20:09:31 +02002996 } else if (expr_str[parsed] == ',') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002997
2998 /* ',' */
2999 tok_len = 1;
3000 tok_type = LYXP_TOKEN_COMMA;
3001
Radek Krejcif03a9e22020-09-18 20:09:31 +02003002 } else if (expr_str[parsed] == '\'') {
Radek Krejcib1646a92018-11-02 16:08:26 +01003003
3004 /* Literal with ' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02003005 for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\''); ++tok_len) {}
3006 LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0',
Radek Krejci2efc45b2020-12-22 16:25:44 +01003007 LOGVAL(ctx, LY_VCODE_XP_EOE, expr_str[parsed], &expr_str[parsed]); ret = LY_EVALID,
Michal Vasko69730152020-10-09 16:30:07 +02003008 error);
Radek Krejcib1646a92018-11-02 16:08:26 +01003009 ++tok_len;
3010 tok_type = LYXP_TOKEN_LITERAL;
3011
Radek Krejcif03a9e22020-09-18 20:09:31 +02003012 } else if (expr_str[parsed] == '\"') {
Radek Krejcib1646a92018-11-02 16:08:26 +01003013
3014 /* Literal with " */
Radek Krejcif03a9e22020-09-18 20:09:31 +02003015 for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\"'); ++tok_len) {}
3016 LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0',
Radek Krejci2efc45b2020-12-22 16:25:44 +01003017 LOGVAL(ctx, LY_VCODE_XP_EOE, expr_str[parsed], &expr_str[parsed]); ret = LY_EVALID,
Michal Vasko69730152020-10-09 16:30:07 +02003018 error);
Radek Krejcib1646a92018-11-02 16:08:26 +01003019 ++tok_len;
3020 tok_type = LYXP_TOKEN_LITERAL;
3021
Radek Krejcif03a9e22020-09-18 20:09:31 +02003022 } else if ((expr_str[parsed] == '.') || (isdigit(expr_str[parsed]))) {
Radek Krejcib1646a92018-11-02 16:08:26 +01003023
3024 /* Number */
Radek Krejcif03a9e22020-09-18 20:09:31 +02003025 for (tok_len = 0; isdigit(expr_str[parsed + tok_len]); ++tok_len) {}
3026 if (expr_str[parsed + tok_len] == '.') {
Radek Krejcib1646a92018-11-02 16:08:26 +01003027 ++tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02003028 for ( ; isdigit(expr_str[parsed + tok_len]); ++tok_len) {}
Radek Krejcib1646a92018-11-02 16:08:26 +01003029 }
3030 tok_type = LYXP_TOKEN_NUMBER;
3031
aPiecekfba75362021-10-07 12:39:48 +02003032 } else if (expr_str[parsed] == '$') {
3033
3034 /* VariableReference */
3035 parsed++;
Michal Vasko49fec8e2022-05-24 10:28:33 +02003036 ncname_len = parse_ncname(&expr_str[parsed]);
aPiecekfba75362021-10-07 12:39:48 +02003037 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 +02003038 (uint32_t)(parsed - ncname_len + 1), expr_str); ret = LY_EVALID, error);
aPiecekfba75362021-10-07 12:39:48 +02003039 tok_len = ncname_len;
3040 LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == ':',
3041 LOGVAL(ctx, LYVE_XPATH, "Variable with prefix is not supported."); ret = LY_EVALID,
3042 error);
3043 tok_type = LYXP_TOKEN_VARREF;
3044
Radek Krejcif03a9e22020-09-18 20:09:31 +02003045 } else if (expr_str[parsed] == '/') {
Radek Krejcib1646a92018-11-02 16:08:26 +01003046
3047 /* Operator '/', '//' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02003048 if (!strncmp(&expr_str[parsed], "//", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01003049 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003050 tok_type = LYXP_TOKEN_OPER_RPATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01003051 } else {
3052 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003053 tok_type = LYXP_TOKEN_OPER_PATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01003054 }
Radek Krejcib1646a92018-11-02 16:08:26 +01003055
Radek Krejcif03a9e22020-09-18 20:09:31 +02003056 } else if (!strncmp(&expr_str[parsed], "!=", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01003057
Michal Vasko3e48bf32020-06-01 08:39:07 +02003058 /* Operator '!=' */
Radek Krejcib1646a92018-11-02 16:08:26 +01003059 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003060 tok_type = LYXP_TOKEN_OPER_NEQUAL;
3061
Radek Krejcif03a9e22020-09-18 20:09:31 +02003062 } else if (!strncmp(&expr_str[parsed], "<=", 2) || !strncmp(&expr_str[parsed], ">=", 2)) {
Michal Vasko3e48bf32020-06-01 08:39:07 +02003063
3064 /* Operator '<=', '>=' */
3065 tok_len = 2;
3066 tok_type = LYXP_TOKEN_OPER_COMP;
Radek Krejcib1646a92018-11-02 16:08:26 +01003067
Radek Krejcif03a9e22020-09-18 20:09:31 +02003068 } else if (expr_str[parsed] == '|') {
Radek Krejcib1646a92018-11-02 16:08:26 +01003069
3070 /* Operator '|' */
3071 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003072 tok_type = LYXP_TOKEN_OPER_UNI;
Radek Krejcib1646a92018-11-02 16:08:26 +01003073
Radek Krejcif03a9e22020-09-18 20:09:31 +02003074 } else if ((expr_str[parsed] == '+') || (expr_str[parsed] == '-')) {
Radek Krejcib1646a92018-11-02 16:08:26 +01003075
3076 /* Operator '+', '-' */
3077 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003078 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01003079
Radek Krejcif03a9e22020-09-18 20:09:31 +02003080 } else if (expr_str[parsed] == '=') {
Radek Krejcib1646a92018-11-02 16:08:26 +01003081
Michal Vasko3e48bf32020-06-01 08:39:07 +02003082 /* Operator '=' */
Radek Krejcib1646a92018-11-02 16:08:26 +01003083 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003084 tok_type = LYXP_TOKEN_OPER_EQUAL;
3085
Radek Krejcif03a9e22020-09-18 20:09:31 +02003086 } else if ((expr_str[parsed] == '<') || (expr_str[parsed] == '>')) {
Michal Vasko3e48bf32020-06-01 08:39:07 +02003087
3088 /* Operator '<', '>' */
3089 tok_len = 1;
3090 tok_type = LYXP_TOKEN_OPER_COMP;
Radek Krejcib1646a92018-11-02 16:08:26 +01003091
Michal Vasko69730152020-10-09 16:30:07 +02003092 } else if (expr->used && (expr->tokens[expr->used - 1] != LYXP_TOKEN_AT) &&
3093 (expr->tokens[expr->used - 1] != LYXP_TOKEN_PAR1) &&
3094 (expr->tokens[expr->used - 1] != LYXP_TOKEN_BRACK1) &&
3095 (expr->tokens[expr->used - 1] != LYXP_TOKEN_COMMA) &&
3096 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_LOG) &&
3097 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_EQUAL) &&
3098 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_NEQUAL) &&
3099 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_COMP) &&
3100 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_MATH) &&
3101 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_UNI) &&
3102 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_PATH) &&
3103 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_RPATH)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01003104
3105 /* Operator '*', 'or', 'and', 'mod', or 'div' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02003106 if (expr_str[parsed] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01003107 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003108 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01003109
Radek Krejcif03a9e22020-09-18 20:09:31 +02003110 } else if (!strncmp(&expr_str[parsed], "or", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01003111 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003112 tok_type = LYXP_TOKEN_OPER_LOG;
Radek Krejcib1646a92018-11-02 16:08:26 +01003113
Radek Krejcif03a9e22020-09-18 20:09:31 +02003114 } else if (!strncmp(&expr_str[parsed], "and", 3)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01003115 tok_len = 3;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003116 tok_type = LYXP_TOKEN_OPER_LOG;
Radek Krejcib1646a92018-11-02 16:08:26 +01003117
Radek Krejcif03a9e22020-09-18 20:09:31 +02003118 } else if (!strncmp(&expr_str[parsed], "mod", 3) || !strncmp(&expr_str[parsed], "div", 3)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01003119 tok_len = 3;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003120 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01003121
Michal Vasko49fec8e2022-05-24 10:28:33 +02003122 } else if (prev_ntype_check || prev_func_check) {
Michal Vasko21eaa392024-02-20 15:48:42 +01003123 LOGVAL(ctx, LYVE_XPATH,
3124 "Invalid character 0x%x ('%c'), perhaps \"%.*s\" is supposed to be a function call.",
3125 expr_str[parsed], expr_str[parsed], (int)expr->tok_len[expr->used - 1],
3126 &expr->expr[expr->tok_pos[expr->used - 1]]);
Radek Krejcif03a9e22020-09-18 20:09:31 +02003127 ret = LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01003128 goto error;
3129 } else {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02003130 LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed], (uint32_t)(parsed + 1), expr_str);
Radek Krejcif03a9e22020-09-18 20:09:31 +02003131 ret = LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01003132 goto error;
3133 }
Radek Krejcib1646a92018-11-02 16:08:26 +01003134 } else {
3135
Michal Vasko49fec8e2022-05-24 10:28:33 +02003136 /* (AxisName '::')? ((NCName ':')? '*' | QName) or NodeType/FunctionName */
3137 if (expr_str[parsed] == '*') {
3138 ncname_len = 1;
3139 } else {
3140 ncname_len = parse_ncname(&expr_str[parsed]);
3141 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 +02003142 (uint32_t)(parsed - ncname_len + 1), expr_str); ret = LY_EVALID, error);
Michal Vasko49fec8e2022-05-24 10:28:33 +02003143 }
Radek Krejcib1646a92018-11-02 16:08:26 +01003144 tok_len = ncname_len;
3145
Michal Vasko49fec8e2022-05-24 10:28:33 +02003146 has_axis = 0;
3147 if (!strncmp(&expr_str[parsed + tok_len], "::", 2)) {
3148 /* axis */
3149 LY_CHECK_ERR_GOTO(expr_parse_axis(&expr_str[parsed], ncname_len),
Michal Vasko7b3a00e2023-08-09 11:58:03 +02003150 LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed], (uint32_t)(parsed + 1), expr_str); ret = LY_EVALID,
3151 error);
Michal Vasko49fec8e2022-05-24 10:28:33 +02003152 tok_type = LYXP_TOKEN_AXISNAME;
3153
3154 LY_CHECK_GOTO(ret = exp_add_token(ctx, expr, tok_type, parsed, tok_len), error);
3155 parsed += tok_len;
3156
3157 /* '::' */
3158 tok_len = 2;
3159 tok_type = LYXP_TOKEN_DCOLON;
3160
3161 LY_CHECK_GOTO(ret = exp_add_token(ctx, expr, tok_type, parsed, tok_len), error);
3162 parsed += tok_len;
3163
3164 if (expr_str[parsed] == '*') {
3165 ncname_len = 1;
3166 } else {
3167 ncname_len = parse_ncname(&expr_str[parsed]);
3168 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 +02003169 (uint32_t)(parsed - ncname_len + 1), expr_str); ret = LY_EVALID, error);
Michal Vasko49fec8e2022-05-24 10:28:33 +02003170 }
3171 tok_len = ncname_len;
3172
3173 has_axis = 1;
3174 }
3175
Radek Krejcif03a9e22020-09-18 20:09:31 +02003176 if (expr_str[parsed + tok_len] == ':') {
Radek Krejcib1646a92018-11-02 16:08:26 +01003177 ++tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02003178 if (expr_str[parsed + tok_len] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01003179 ++tok_len;
3180 } else {
Radek Krejcif03a9e22020-09-18 20:09:31 +02003181 ncname_len = parse_ncname(&expr_str[parsed + tok_len]);
Michal Vaskoe2be5462021-08-04 10:49:42 +02003182 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 +02003183 (uint32_t)(parsed - ncname_len + 1), expr_str); ret = LY_EVALID, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01003184 tok_len += ncname_len;
3185 }
Michal Vasko49fec8e2022-05-24 10:28:33 +02003186 /* remove old flags to prevent ambiguities */
3187 prev_ntype_check = 0;
3188 prev_func_check = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01003189 tok_type = LYXP_TOKEN_NAMETEST;
3190 } else {
Michal Vasko49fec8e2022-05-24 10:28:33 +02003191 /* if not '*', there is no prefix so it can still be NodeType/FunctionName, we can't finally decide now */
3192 prev_ntype_check = (expr_str[parsed] == '*') ? 0 : 1;
3193 prev_func_check = (prev_ntype_check && !has_axis) ? 1 : 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01003194 tok_type = LYXP_TOKEN_NAMETEST;
3195 }
3196 }
3197
3198 /* store the token, move on to the next one */
Radek Krejcif03a9e22020-09-18 20:09:31 +02003199 LY_CHECK_GOTO(ret = exp_add_token(ctx, expr, tok_type, parsed, tok_len), error);
Radek Krejcib1646a92018-11-02 16:08:26 +01003200 parsed += tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02003201 while (is_xmlws(expr_str[parsed])) {
Radek Krejcib1646a92018-11-02 16:08:26 +01003202 ++parsed;
3203 }
3204
Radek Krejcif03a9e22020-09-18 20:09:31 +02003205 } while (expr_str[parsed]);
Radek Krejcib1646a92018-11-02 16:08:26 +01003206
Michal Vasko004d3152020-06-11 19:59:22 +02003207 if (reparse) {
3208 /* prealloc repeat */
Radek Krejcif03a9e22020-09-18 20:09:31 +02003209 expr->repeat = calloc(expr->size, sizeof *expr->repeat);
3210 LY_CHECK_ERR_GOTO(!expr->repeat, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01003211
Michal Vasko004d3152020-06-11 19:59:22 +02003212 /* fill repeat */
aPiecekbf968d92021-05-27 14:35:05 +02003213 LY_CHECK_ERR_GOTO(reparse_or_expr(ctx, expr, &tok_idx, 0), ret = LY_EVALID, error);
Radek Krejcif03a9e22020-09-18 20:09:31 +02003214 if (expr->used > tok_idx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003215 LOGVAL(ctx, LYVE_XPATH, "Unparsed characters \"%s\" left at the end of an XPath expression.",
Michal Vasko69730152020-10-09 16:30:07 +02003216 &expr->expr[expr->tok_pos[tok_idx]]);
Radek Krejcif03a9e22020-09-18 20:09:31 +02003217 ret = LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02003218 goto error;
3219 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003220 }
3221
Radek Krejcif03a9e22020-09-18 20:09:31 +02003222 print_expr_struct_debug(expr);
3223 *expr_p = expr;
3224 return LY_SUCCESS;
Radek Krejcib1646a92018-11-02 16:08:26 +01003225
3226error:
Radek Krejcif03a9e22020-09-18 20:09:31 +02003227 lyxp_expr_free(ctx, expr);
3228 return ret;
Radek Krejcib1646a92018-11-02 16:08:26 +01003229}
3230
Michal Vasko1734be92020-09-22 08:55:10 +02003231LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02003232lyxp_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 +02003233 struct lyxp_expr **dup_p)
Michal Vasko004d3152020-06-11 19:59:22 +02003234{
Michal Vasko1734be92020-09-22 08:55:10 +02003235 LY_ERR ret = LY_SUCCESS;
3236 struct lyxp_expr *dup = NULL;
Michal Vaskod59039d2022-09-09 09:07:30 +02003237 uint32_t used = 0, i, j, expr_len;
Michal Vaskoe33134a2022-07-29 14:54:40 +02003238 const char *expr_start;
3239
3240 assert((!start_idx && !end_idx) || ((start_idx < exp->used) && (end_idx < exp->used) && (start_idx <= end_idx)));
Michal Vasko004d3152020-06-11 19:59:22 +02003241
Michal Vasko7f45cf22020-10-01 12:49:44 +02003242 if (!exp) {
3243 goto cleanup;
3244 }
3245
Michal Vaskoe33134a2022-07-29 14:54:40 +02003246 if (!start_idx && !end_idx) {
3247 end_idx = exp->used - 1;
3248 }
3249
3250 expr_start = exp->expr + exp->tok_pos[start_idx];
3251 expr_len = (exp->tok_pos[end_idx] + exp->tok_len[end_idx]) - exp->tok_pos[start_idx];
3252
Michal Vasko004d3152020-06-11 19:59:22 +02003253 dup = calloc(1, sizeof *dup);
Michal Vasko1734be92020-09-22 08:55:10 +02003254 LY_CHECK_ERR_GOTO(!dup, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02003255
Michal Vasko08e9b112021-06-11 15:41:17 +02003256 if (exp->used) {
Michal Vaskoe33134a2022-07-29 14:54:40 +02003257 used = (end_idx - start_idx) + 1;
3258
3259 dup->tokens = malloc(used * sizeof *dup->tokens);
Michal Vasko08e9b112021-06-11 15:41:17 +02003260 LY_CHECK_ERR_GOTO(!dup->tokens, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vaskoe33134a2022-07-29 14:54:40 +02003261 memcpy(dup->tokens, exp->tokens + start_idx, used * sizeof *dup->tokens);
Michal Vasko004d3152020-06-11 19:59:22 +02003262
Michal Vaskoe33134a2022-07-29 14:54:40 +02003263 dup->tok_pos = malloc(used * sizeof *dup->tok_pos);
Michal Vasko08e9b112021-06-11 15:41:17 +02003264 LY_CHECK_ERR_GOTO(!dup->tok_pos, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vaskoe33134a2022-07-29 14:54:40 +02003265 memcpy(dup->tok_pos, exp->tok_pos + start_idx, used * sizeof *dup->tok_pos);
Michal Vasko004d3152020-06-11 19:59:22 +02003266
Michal Vaskoe33134a2022-07-29 14:54:40 +02003267 if (start_idx) {
3268 /* fix the indices in the expression */
3269 for (i = 0; i < used; ++i) {
3270 dup->tok_pos[i] -= expr_start - exp->expr;
3271 }
3272 }
3273
3274 dup->tok_len = malloc(used * sizeof *dup->tok_len);
Michal Vasko08e9b112021-06-11 15:41:17 +02003275 LY_CHECK_ERR_GOTO(!dup->tok_len, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vaskoe33134a2022-07-29 14:54:40 +02003276 memcpy(dup->tok_len, exp->tok_len + start_idx, used * sizeof *dup->tok_len);
Michal Vasko004d3152020-06-11 19:59:22 +02003277
Michal Vasko79a7a872022-06-17 09:00:48 +02003278 if (exp->repeat) {
Michal Vaskoe33134a2022-07-29 14:54:40 +02003279 dup->repeat = malloc(used * sizeof *dup->repeat);
Michal Vasko79a7a872022-06-17 09:00:48 +02003280 LY_CHECK_ERR_GOTO(!dup->repeat, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vaskoe33134a2022-07-29 14:54:40 +02003281 for (i = start_idx; i <= end_idx; ++i) {
Michal Vasko79a7a872022-06-17 09:00:48 +02003282 if (!exp->repeat[i]) {
Michal Vaskoe33134a2022-07-29 14:54:40 +02003283 dup->repeat[i - start_idx] = NULL;
Michal Vasko79a7a872022-06-17 09:00:48 +02003284 } else {
3285 for (j = 0; exp->repeat[i][j]; ++j) {}
3286 /* the ending 0 as well */
3287 ++j;
Michal Vasko004d3152020-06-11 19:59:22 +02003288
Michal Vaskoe33134a2022-07-29 14:54:40 +02003289 dup->repeat[i - start_idx] = malloc(j * sizeof **dup->repeat);
3290 LY_CHECK_ERR_GOTO(!dup->repeat[i - start_idx], LOGMEM(ctx); ret = LY_EMEM, cleanup);
3291 memcpy(dup->repeat[i - start_idx], exp->repeat[i], j * sizeof **dup->repeat);
Michal Vasko79a7a872022-06-17 09:00:48 +02003292 }
Michal Vasko08e9b112021-06-11 15:41:17 +02003293 }
Michal Vasko004d3152020-06-11 19:59:22 +02003294 }
3295 }
3296
Michal Vaskoe33134a2022-07-29 14:54:40 +02003297 dup->used = used;
3298 dup->size = used;
3299
3300 /* copy only subexpression */
3301 LY_CHECK_GOTO(ret = lydict_insert(ctx, expr_start, expr_len, &dup->expr), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02003302
Michal Vasko1734be92020-09-22 08:55:10 +02003303cleanup:
3304 if (ret) {
3305 lyxp_expr_free(ctx, dup);
3306 } else {
3307 *dup_p = dup;
3308 }
3309 return ret;
Michal Vasko004d3152020-06-11 19:59:22 +02003310}
3311
Michal Vasko03ff5a72019-09-11 13:49:33 +02003312/**
3313 * @brief Get the last-added schema node that is currently in the context.
3314 *
3315 * @param[in] set Set to search in.
3316 * @return Last-added schema context node, NULL if no node is in context.
3317 */
3318static struct lysc_node *
3319warn_get_scnode_in_ctx(struct lyxp_set *set)
3320{
3321 uint32_t i;
3322
3323 if (!set || (set->type != LYXP_SET_SCNODE_SET)) {
3324 return NULL;
3325 }
3326
3327 i = set->used;
3328 do {
3329 --i;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003330 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003331 /* if there are more, simply return the first found (last added) */
3332 return set->val.scnodes[i].scnode;
3333 }
3334 } while (i);
3335
3336 return NULL;
3337}
3338
3339/**
3340 * @brief Test whether a type is numeric - integer type or decimal64.
3341 *
3342 * @param[in] type Type to test.
Radek Krejci857189e2020-09-01 13:26:36 +02003343 * @return Boolean value whether @p type is numeric type or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003344 */
Radek Krejci857189e2020-09-01 13:26:36 +02003345static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003346warn_is_numeric_type(struct lysc_type *type)
3347{
3348 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003349 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003350 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003351
3352 switch (type->basetype) {
3353 case LY_TYPE_DEC64:
3354 case LY_TYPE_INT8:
3355 case LY_TYPE_UINT8:
3356 case LY_TYPE_INT16:
3357 case LY_TYPE_UINT16:
3358 case LY_TYPE_INT32:
3359 case LY_TYPE_UINT32:
3360 case LY_TYPE_INT64:
3361 case LY_TYPE_UINT64:
3362 return 1;
3363 case LY_TYPE_UNION:
3364 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003365 LY_ARRAY_FOR(uni->types, u) {
3366 ret = warn_is_numeric_type(uni->types[u]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003367 if (ret) {
3368 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003369 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003370 }
3371 }
3372 /* did not find any suitable type */
3373 return 0;
3374 case LY_TYPE_LEAFREF:
3375 return warn_is_numeric_type(((struct lysc_type_leafref *)type)->realtype);
3376 default:
3377 return 0;
3378 }
3379}
3380
3381/**
3382 * @brief Test whether a type is string-like - no integers, decimal64 or binary.
3383 *
3384 * @param[in] type Type to test.
Radek Krejci857189e2020-09-01 13:26:36 +02003385 * @return Boolean value whether @p type's basetype is string type or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003386 */
Radek Krejci857189e2020-09-01 13:26:36 +02003387static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003388warn_is_string_type(struct lysc_type *type)
3389{
3390 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003391 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003392 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003393
3394 switch (type->basetype) {
3395 case LY_TYPE_BITS:
3396 case LY_TYPE_ENUM:
3397 case LY_TYPE_IDENT:
3398 case LY_TYPE_INST:
3399 case LY_TYPE_STRING:
3400 return 1;
3401 case LY_TYPE_UNION:
3402 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003403 LY_ARRAY_FOR(uni->types, u) {
3404 ret = warn_is_string_type(uni->types[u]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003405 if (ret) {
3406 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003407 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003408 }
3409 }
3410 /* did not find any suitable type */
3411 return 0;
3412 case LY_TYPE_LEAFREF:
3413 return warn_is_string_type(((struct lysc_type_leafref *)type)->realtype);
3414 default:
3415 return 0;
3416 }
3417}
3418
3419/**
3420 * @brief Test whether a type is one specific type.
3421 *
3422 * @param[in] type Type to test.
3423 * @param[in] base Expected type.
Radek Krejci857189e2020-09-01 13:26:36 +02003424 * @return Boolean value whether the given @p type is of the specific basetype @p base.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003425 */
Radek Krejci857189e2020-09-01 13:26:36 +02003426static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003427warn_is_specific_type(struct lysc_type *type, LY_DATA_TYPE base)
3428{
3429 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003430 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003431 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003432
3433 if (type->basetype == base) {
3434 return 1;
3435 } else if (type->basetype == LY_TYPE_UNION) {
3436 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003437 LY_ARRAY_FOR(uni->types, u) {
3438 ret = warn_is_specific_type(uni->types[u], base);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003439 if (ret) {
3440 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003441 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003442 }
3443 }
3444 /* did not find any suitable type */
3445 return 0;
3446 } else if (type->basetype == LY_TYPE_LEAFREF) {
3447 return warn_is_specific_type(((struct lysc_type_leafref *)type)->realtype, base);
3448 }
3449
3450 return 0;
3451}
3452
3453/**
3454 * @brief Get next type of a (union) type.
3455 *
3456 * @param[in] type Base type.
3457 * @param[in] prev_type Previously returned type.
3458 * @return Next type or NULL.
3459 */
3460static struct lysc_type *
3461warn_is_equal_type_next_type(struct lysc_type *type, struct lysc_type *prev_type)
3462{
3463 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003464 ly_bool found = 0;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003465 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003466
Michal Vasko4e55f5a2022-12-14 12:15:00 +01003467 if (type->basetype == LY_TYPE_UNION) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003468 uni = (struct lysc_type_union *)type;
3469 if (!prev_type) {
3470 return uni->types[0];
3471 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003472 LY_ARRAY_FOR(uni->types, u) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003473 if (found) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003474 return uni->types[u];
Michal Vasko03ff5a72019-09-11 13:49:33 +02003475 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003476 if (prev_type == uni->types[u]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003477 found = 1;
3478 }
3479 }
3480 return NULL;
Michal Vasko4e55f5a2022-12-14 12:15:00 +01003481 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003482 if (prev_type) {
3483 assert(type == prev_type);
3484 return NULL;
3485 } else {
3486 return type;
3487 }
3488 }
3489}
3490
3491/**
3492 * @brief Test whether 2 types have a common type.
3493 *
3494 * @param[in] type1 First type.
3495 * @param[in] type2 Second type.
3496 * @return 1 if they do, 0 otherwise.
3497 */
3498static int
3499warn_is_equal_type(struct lysc_type *type1, struct lysc_type *type2)
3500{
3501 struct lysc_type *t1, *rt1, *t2, *rt2;
3502
3503 t1 = NULL;
3504 while ((t1 = warn_is_equal_type_next_type(type1, t1))) {
3505 if (t1->basetype == LY_TYPE_LEAFREF) {
3506 rt1 = ((struct lysc_type_leafref *)t1)->realtype;
3507 } else {
3508 rt1 = t1;
3509 }
3510
3511 t2 = NULL;
3512 while ((t2 = warn_is_equal_type_next_type(type2, t2))) {
3513 if (t2->basetype == LY_TYPE_LEAFREF) {
3514 rt2 = ((struct lysc_type_leafref *)t2)->realtype;
3515 } else {
3516 rt2 = t2;
3517 }
3518
3519 if (rt2->basetype == rt1->basetype) {
3520 /* match found */
3521 return 1;
3522 }
3523 }
3524 }
3525
3526 return 0;
3527}
3528
3529/**
Michal Vaskoaa956522021-11-11 10:45:34 +01003530 * @brief Print warning with information about the XPath subexpression that caused previous warning.
3531 *
3532 * @param[in] ctx Context for logging.
3533 * @param[in] tok_pos Index of the subexpression in the whole expression.
3534 * @param[in] subexpr Subexpression start.
3535 * @param[in] subexpr_len Length of @p subexpr to print.
3536 * @param[in] cur_scnode Expression context node.
3537 */
3538static void
Michal Vaskodd528af2022-08-08 14:35:07 +02003539warn_subexpr_log(const struct ly_ctx *ctx, uint32_t tok_pos, const char *subexpr, int subexpr_len,
Michal Vaskoaa956522021-11-11 10:45:34 +01003540 const struct lysc_node *cur_scnode)
3541{
3542 char *path;
3543
3544 path = lysc_path(cur_scnode, LYSC_PATH_LOG, NULL, 0);
Michal Vaskodd528af2022-08-08 14:35:07 +02003545 LOGWRN(ctx, "Previous warning generated by XPath subexpression[%" PRIu32 "] \"%.*s\" with context node \"%s\".",
Michal Vaskoaa956522021-11-11 10:45:34 +01003546 tok_pos, subexpr_len, subexpr, path);
3547 free(path);
3548}
3549
3550/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02003551 * @brief Check both operands of comparison operators.
3552 *
3553 * @param[in] ctx Context for errors.
3554 * @param[in] set1 First operand set.
3555 * @param[in] set2 Second operand set.
3556 * @param[in] numbers_only Whether accept only numbers or other types are fine too (for '=' and '!=').
3557 * @param[in] expr Start of the expression to print with the warning.
3558 * @param[in] tok_pos Token position.
3559 */
3560static void
Michal Vaskoaa956522021-11-11 10:45:34 +01003561warn_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 +02003562 uint32_t tok_pos)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003563{
3564 struct lysc_node_leaf *node1, *node2;
Radek Krejci857189e2020-09-01 13:26:36 +02003565 ly_bool leaves = 1, warning = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003566
3567 node1 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set1);
3568 node2 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set2);
3569
3570 if (!node1 && !node2) {
3571 /* no node-sets involved, nothing to do */
3572 return;
3573 }
3574
3575 if (node1) {
3576 if (!(node1->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3577 LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node1->nodetype), node1->name);
3578 warning = 1;
3579 leaves = 0;
3580 } else if (numbers_only && !warn_is_numeric_type(node1->type)) {
3581 LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node1->name);
3582 warning = 1;
3583 }
3584 }
3585
3586 if (node2) {
3587 if (!(node2->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3588 LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node2->nodetype), node2->name);
3589 warning = 1;
3590 leaves = 0;
3591 } else if (numbers_only && !warn_is_numeric_type(node2->type)) {
3592 LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node2->name);
3593 warning = 1;
3594 }
3595 }
3596
3597 if (node1 && node2 && leaves && !numbers_only) {
Michal Vasko69730152020-10-09 16:30:07 +02003598 if ((warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type)) ||
3599 (!warn_is_numeric_type(node1->type) && warn_is_numeric_type(node2->type)) ||
3600 (!warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type) &&
3601 !warn_is_equal_type(node1->type, node2->type))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003602 LOGWRN(ctx, "Incompatible types of operands \"%s\" and \"%s\" for comparison.", node1->name, node2->name);
3603 warning = 1;
3604 }
3605 }
3606
3607 if (warning) {
Michal Vaskoaa956522021-11-11 10:45:34 +01003608 warn_subexpr_log(ctx, tok_pos, expr + tok_pos, 20, set1->cur_scnode);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003609 }
3610}
3611
3612/**
3613 * @brief Check that a value is valid for a leaf. If not applicable, does nothing.
3614 *
3615 * @param[in] exp Parsed XPath expression.
3616 * @param[in] set Set with the leaf/leaf-list.
3617 * @param[in] val_exp Index of the value (literal/number) in @p exp.
3618 * @param[in] equal_exp Index of the start of the equality expression in @p exp.
3619 * @param[in] last_equal_exp Index of the end of the equality expression in @p exp.
3620 */
3621static void
Michal Vaskodd528af2022-08-08 14:35:07 +02003622warn_equality_value(const struct lyxp_expr *exp, struct lyxp_set *set, uint32_t val_exp, uint32_t equal_exp,
3623 uint32_t last_equal_exp)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003624{
3625 struct lysc_node *scnode;
3626 struct lysc_type *type;
3627 char *value;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003628 struct lyd_value storage;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003629 LY_ERR rc;
3630 struct ly_err_item *err = NULL;
3631
Michal Vasko69730152020-10-09 16:30:07 +02003632 if ((scnode = warn_get_scnode_in_ctx(set)) && (scnode->nodetype & (LYS_LEAF | LYS_LEAFLIST)) &&
3633 ((exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) || (exp->tokens[val_exp] == LYXP_TOKEN_NUMBER))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003634 /* check that the node can have the specified value */
3635 if (exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) {
3636 value = strndup(exp->expr + exp->tok_pos[val_exp] + 1, exp->tok_len[val_exp] - 2);
3637 } else {
3638 value = strndup(exp->expr + exp->tok_pos[val_exp], exp->tok_len[val_exp]);
3639 }
3640 if (!value) {
3641 LOGMEM(set->ctx);
3642 return;
3643 }
3644
3645 if ((((struct lysc_node_leaf *)scnode)->type->basetype == LY_TYPE_IDENT) && !strchr(value, ':')) {
3646 LOGWRN(set->ctx, "Identityref \"%s\" comparison with identity \"%s\" without prefix, consider adding"
Michal Vasko69730152020-10-09 16:30:07 +02003647 " a prefix or best using \"derived-from(-or-self)()\" functions.", scnode->name, value);
Michal Vaskoaa956522021-11-11 10:45:34 +01003648 warn_subexpr_log(set->ctx, exp->tok_pos[equal_exp], exp->expr + exp->tok_pos[equal_exp],
Radek Krejci0f969882020-08-21 16:56:47 +02003649 (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp],
Michal Vaskoaa956522021-11-11 10:45:34 +01003650 set->cur_scnode);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003651 }
3652
3653 type = ((struct lysc_node_leaf *)scnode)->type;
3654 if (type->basetype != LY_TYPE_IDENT) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003655 rc = type->plugin->store(set->ctx, type, value, strlen(value), 0, set->format, set->prefix_data,
Michal Vasko405cc9e2020-12-01 12:01:27 +01003656 LYD_HINT_DATA, scnode, &storage, NULL, &err);
Michal Vaskobf42e832020-11-23 16:59:42 +01003657 if (rc == LY_EINCOMPLETE) {
3658 rc = LY_SUCCESS;
3659 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003660
3661 if (err) {
3662 LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type (%s).", value, err->msg);
3663 ly_err_free(err);
3664 } else if (rc != LY_SUCCESS) {
3665 LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type.", value);
3666 }
3667 if (rc != LY_SUCCESS) {
Michal Vaskoaa956522021-11-11 10:45:34 +01003668 warn_subexpr_log(set->ctx, exp->tok_pos[equal_exp], exp->expr + exp->tok_pos[equal_exp],
Radek Krejci0f969882020-08-21 16:56:47 +02003669 (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp],
Michal Vaskoaa956522021-11-11 10:45:34 +01003670 set->cur_scnode);
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003671 } else {
3672 type->plugin->free(set->ctx, &storage);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003673 }
3674 }
3675 free(value);
3676 }
3677}
3678
3679/*
3680 * XPath functions
3681 */
3682
3683/**
3684 * @brief Execute the YANG 1.1 bit-is-set(node-set, string) function. Returns LYXP_SET_BOOLEAN
3685 * depending on whether the first node bit value from the second argument is set.
3686 *
3687 * @param[in] args Array of arguments.
3688 * @param[in] arg_count Count of elements in @p args.
3689 * @param[in,out] set Context and result set at the same time.
3690 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003691 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003692 */
3693static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02003694xpath_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 +02003695{
3696 struct lyd_node_term *leaf;
3697 struct lysc_node_leaf *sleaf;
Michal Vasko2588b952021-07-29 07:43:26 +02003698 struct lyd_value_bits *bits;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003699 LY_ERR rc = LY_SUCCESS;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003700 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003701
3702 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003703 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003704 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003705 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3706 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3707 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3708 sleaf->name);
3709 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_BITS)) {
3710 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"bits\".", __func__, sleaf->name);
3711 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003712 }
3713
3714 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3715 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003716 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3717 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003718 } else if (!warn_is_string_type(sleaf->type)) {
3719 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003720 }
3721 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003722 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003723 return rc;
3724 }
3725
Michal Vaskod3678892020-05-21 10:06:58 +02003726 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003727 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 +02003728 return LY_EVALID;
3729 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003730 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003731 LY_CHECK_RET(rc);
3732
3733 set_fill_boolean(set, 0);
Michal Vaskod3678892020-05-21 10:06:58 +02003734 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003735 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
Michal Vasko2588b952021-07-29 07:43:26 +02003736 if ((leaf->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (leaf->value.realtype->basetype == LY_TYPE_BITS)) {
3737 LYD_VALUE_GET(&leaf->value, bits);
3738 LY_ARRAY_FOR(bits->items, u) {
3739 if (!strcmp(bits->items[u]->name, args[1]->val.str)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003740 set_fill_boolean(set, 1);
3741 break;
3742 }
3743 }
3744 }
3745 }
3746
3747 return LY_SUCCESS;
3748}
3749
3750/**
3751 * @brief Execute the XPath boolean(object) function. Returns LYXP_SET_BOOLEAN
3752 * with the argument converted to boolean.
3753 *
3754 * @param[in] args Array of arguments.
3755 * @param[in] arg_count Count of elements in @p args.
3756 * @param[in,out] set Context and result set at the same time.
3757 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003758 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003759 */
3760static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02003761xpath_boolean(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003762{
3763 LY_ERR rc;
3764
3765 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02003766 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003767 return LY_SUCCESS;
3768 }
3769
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003770 rc = lyxp_set_cast(args[0], LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003771 LY_CHECK_RET(rc);
3772 set_fill_set(set, args[0]);
3773
3774 return LY_SUCCESS;
3775}
3776
3777/**
3778 * @brief Execute the XPath ceiling(number) function. Returns LYXP_SET_NUMBER
3779 * with the first argument rounded up to the nearest integer.
3780 *
3781 * @param[in] args Array of arguments.
3782 * @param[in] arg_count Count of elements in @p args.
3783 * @param[in,out] set Context and result set at the same time.
3784 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003785 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003786 */
3787static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02003788xpath_ceiling(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003789{
3790 struct lysc_node_leaf *sleaf;
3791 LY_ERR rc = LY_SUCCESS;
3792
3793 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003794 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003795 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003796 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3797 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3798 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3799 sleaf->name);
3800 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
3801 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
3802 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003803 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003804 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003805 return rc;
3806 }
3807
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003808 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003809 LY_CHECK_RET(rc);
3810 if ((long long)args[0]->val.num != args[0]->val.num) {
3811 set_fill_number(set, ((long long)args[0]->val.num) + 1);
3812 } else {
3813 set_fill_number(set, args[0]->val.num);
3814 }
3815
3816 return LY_SUCCESS;
3817}
3818
3819/**
3820 * @brief Execute the XPath concat(string, string, string*) function.
3821 * Returns LYXP_SET_STRING with the concatenation of all the arguments.
3822 *
3823 * @param[in] args Array of arguments.
3824 * @param[in] arg_count Count of elements in @p args.
3825 * @param[in,out] set Context and result set at the same time.
3826 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003827 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003828 */
3829static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02003830xpath_concat(struct lyxp_set **args, uint32_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003831{
Michal Vaskodd528af2022-08-08 14:35:07 +02003832 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003833 char *str = NULL;
3834 size_t used = 1;
3835 LY_ERR rc = LY_SUCCESS;
3836 struct lysc_node_leaf *sleaf;
3837
3838 if (options & LYXP_SCNODE_ALL) {
3839 for (i = 0; i < arg_count; ++i) {
3840 if ((args[i]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[i]))) {
3841 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko21eaa392024-02-20 15:48:42 +01003842 LOGWRN(set->ctx, "Argument #%" PRIu32 " of %s is a %s node \"%s\".",
Michal Vasko69730152020-10-09 16:30:07 +02003843 i + 1, __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003844 } else if (!warn_is_string_type(sleaf->type)) {
Michal Vasko21eaa392024-02-20 15:48:42 +01003845 LOGWRN(set->ctx, "Argument #%" PRIu32 " of %s is node \"%s\", not of string-type.", i + 1, __func__,
3846 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003847 }
3848 }
3849 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003850 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003851 return rc;
3852 }
3853
3854 for (i = 0; i < arg_count; ++i) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003855 rc = lyxp_set_cast(args[i], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003856 if (rc != LY_SUCCESS) {
3857 free(str);
3858 return rc;
3859 }
3860
3861 str = ly_realloc(str, (used + strlen(args[i]->val.str)) * sizeof(char));
3862 LY_CHECK_ERR_RET(!str, LOGMEM(set->ctx), LY_EMEM);
3863 strcpy(str + used - 1, args[i]->val.str);
3864 used += strlen(args[i]->val.str);
3865 }
3866
3867 /* free, kind of */
Michal Vaskod3678892020-05-21 10:06:58 +02003868 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003869 set->type = LYXP_SET_STRING;
3870 set->val.str = str;
3871
3872 return LY_SUCCESS;
3873}
3874
3875/**
3876 * @brief Execute the XPath contains(string, string) function.
3877 * Returns LYXP_SET_BOOLEAN whether the second argument can
3878 * be found in the first or not.
3879 *
3880 * @param[in] args Array of arguments.
3881 * @param[in] arg_count Count of elements in @p args.
3882 * @param[in,out] set Context and result set at the same time.
3883 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003884 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003885 */
3886static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02003887xpath_contains(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003888{
3889 struct lysc_node_leaf *sleaf;
3890 LY_ERR rc = LY_SUCCESS;
3891
3892 if (options & LYXP_SCNODE_ALL) {
3893 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3894 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003895 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3896 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003897 } else if (!warn_is_string_type(sleaf->type)) {
3898 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003899 }
3900 }
3901
3902 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3903 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003904 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3905 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003906 } else if (!warn_is_string_type(sleaf->type)) {
3907 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003908 }
3909 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003910 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003911 return rc;
3912 }
3913
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003914 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003915 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003916 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003917 LY_CHECK_RET(rc);
3918
3919 if (strstr(args[0]->val.str, args[1]->val.str)) {
3920 set_fill_boolean(set, 1);
3921 } else {
3922 set_fill_boolean(set, 0);
3923 }
3924
3925 return LY_SUCCESS;
3926}
3927
3928/**
3929 * @brief Execute the XPath count(node-set) function. Returns LYXP_SET_NUMBER
3930 * with the size of the node-set from the argument.
3931 *
3932 * @param[in] args Array of arguments.
3933 * @param[in] arg_count Count of elements in @p args.
3934 * @param[in,out] set Context and result set at the same time.
3935 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003936 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003937 */
3938static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02003939xpath_count(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003940{
Michal Vasko03ff5a72019-09-11 13:49:33 +02003941 LY_ERR rc = LY_SUCCESS;
3942
3943 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003944 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003945 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003946 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003947 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003948 return rc;
3949 }
3950
Michal Vasko03ff5a72019-09-11 13:49:33 +02003951 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003952 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "count(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003953 return LY_EVALID;
3954 }
3955
3956 set_fill_number(set, args[0]->used);
3957 return LY_SUCCESS;
3958}
3959
3960/**
3961 * @brief Execute the XPath current() function. Returns LYXP_SET_NODE_SET
3962 * with the context with the intial node.
3963 *
3964 * @param[in] args Array of arguments.
3965 * @param[in] arg_count Count of elements in @p args.
3966 * @param[in,out] set Context and result set at the same time.
3967 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003968 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003969 */
3970static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02003971xpath_current(struct lyxp_set **args, uint32_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003972{
3973 if (arg_count || args) {
Radek Krejcie87c7dc2021-06-02 21:25:42 +02003974 LOGVAL(set->ctx, LY_VCODE_XP_INARGCOUNT, arg_count, LY_PRI_LENSTR("current()"));
Michal Vasko03ff5a72019-09-11 13:49:33 +02003975 return LY_EVALID;
3976 }
3977
3978 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02003979 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003980
Michal Vasko296dfaf2021-12-13 16:57:42 +01003981 if (set->cur_scnode) {
Michal Vaskoe4a6d012023-05-22 14:34:52 +02003982 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 +01003983 } else {
3984 /* root node */
Michal Vaskoe4a6d012023-05-22 14:34:52 +02003985 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, NULL, set->root_type, LYXP_AXIS_SELF, NULL));
Michal Vasko296dfaf2021-12-13 16:57:42 +01003986 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003987 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02003988 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003989
Michal Vasko296dfaf2021-12-13 16:57:42 +01003990 if (set->cur_node) {
3991 /* position is filled later */
3992 set_insert_node(set, set->cur_node, 0, LYXP_NODE_ELEM, 0);
3993 } else {
3994 /* root node */
3995 set_insert_node(set, NULL, 0, set->root_type, 0);
3996 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003997 }
3998
3999 return LY_SUCCESS;
4000}
4001
4002/**
4003 * @brief Execute the YANG 1.1 deref(node-set) function. Returns LYXP_SET_NODE_SET with either
4004 * leafref or instance-identifier target node(s).
4005 *
4006 * @param[in] args Array of arguments.
4007 * @param[in] arg_count Count of elements in @p args.
4008 * @param[in,out] set Context and result set at the same time.
4009 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004010 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004011 */
4012static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004013xpath_deref(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004014{
4015 struct lyd_node_term *leaf;
Michal Vasko42e497c2020-01-06 08:38:25 +01004016 struct lysc_node_leaf *sleaf = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02004017 struct lysc_type_leafref *lref;
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02004018 const struct lysc_node *target;
Michal Vasko004d3152020-06-11 19:59:22 +02004019 struct ly_path *p;
4020 struct lyd_node *node;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004021 char *errmsg = NULL;
Michal Vasko00cbf532020-06-15 13:58:47 +02004022 uint8_t oper;
Michal Vasko741bb562021-06-24 11:59:50 +02004023 LY_ERR r;
steweg67388952024-01-25 12:14:50 +01004024 LY_ERR ret = LY_SUCCESS;
4025 struct ly_set *targets = NULL;
4026 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004027
4028 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02004029 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004030 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02004031 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
Michal Vasko423ba3f2021-07-19 13:08:50 +02004032 if (!(sleaf->nodetype & LYD_NODE_TERM)) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02004033 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
4034 sleaf->name);
Michal Vaskoed725d72021-06-23 12:03:45 +02004035 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_LEAFREF) &&
4036 !warn_is_specific_type(sleaf->type, LY_TYPE_INST)) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02004037 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"leafref\" nor \"instance-identifier\".",
4038 __func__, sleaf->name);
4039 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004040 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004041 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko423ba3f2021-07-19 13:08:50 +02004042 if (sleaf && (sleaf->nodetype & LYD_NODE_TERM) && (sleaf->type->basetype == LY_TYPE_LEAFREF)) {
Michal Vasko004d3152020-06-11 19:59:22 +02004043 lref = (struct lysc_type_leafref *)sleaf->type;
Michal Vaskod1e53b92021-01-28 13:11:06 +01004044 oper = (sleaf->flags & LYS_IS_OUTPUT) ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT;
Michal Vasko004d3152020-06-11 19:59:22 +02004045
4046 /* it was already evaluated on schema, it must succeed */
Michal Vasko741bb562021-06-24 11:59:50 +02004047 r = ly_path_compile_leafref(set->ctx, &sleaf->node, NULL, lref->path, oper, LY_PATH_TARGET_MANY,
Michal Vasko24fc4d12021-07-12 14:41:20 +02004048 LY_VALUE_SCHEMA_RESOLVED, lref->prefixes, &p);
Michal Vasko741bb562021-06-24 11:59:50 +02004049 if (!r) {
4050 /* get the target node */
4051 target = p[LY_ARRAY_COUNT(p) - 1].node;
4052 ly_path_free(set->ctx, p);
Michal Vasko004d3152020-06-11 19:59:22 +02004053
Michal Vaskoe4a6d012023-05-22 14:34:52 +02004054 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, target, LYXP_NODE_ELEM, LYXP_AXIS_SELF, NULL));
Michal Vasko741bb562021-06-24 11:59:50 +02004055 } /* else the target was found before but is disabled so it was removed */
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02004056 }
4057
steweg67388952024-01-25 12:14:50 +01004058 ret = LY_SUCCESS;
4059 goto cleanup;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004060 }
4061
Michal Vaskod3678892020-05-21 10:06:58 +02004062 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004063 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "deref(node-set)");
steweg67388952024-01-25 12:14:50 +01004064 ret = LY_EVALID;
4065 goto cleanup;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004066 }
4067
Michal Vaskod3678892020-05-21 10:06:58 +02004068 lyxp_set_free_content(set);
4069 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004070 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
4071 sleaf = (struct lysc_node_leaf *)leaf->schema;
4072 if (sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
4073 if (sleaf->type->basetype == LY_TYPE_LEAFREF) {
4074 /* find leafref target */
steweg67388952024-01-25 12:14:50 +01004075 r = lyplg_type_resolve_leafref((struct lysc_type_leafref *)sleaf->type, &leaf->node, &leaf->value, set->tree,
4076 &targets, &errmsg);
4077 if (r) {
Michal Vasko1a2b8ee2022-12-05 10:41:55 +01004078 LOGERR(set->ctx, LY_EVALID, "%s", errmsg);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004079 free(errmsg);
steweg67388952024-01-25 12:14:50 +01004080 ret = LY_EVALID;
4081 goto cleanup;
4082 }
4083
4084 /* insert nodes into set */
4085 for (i = 0; i < targets->count; ++i) {
4086 set_insert_node(set, targets->dnodes[i], 0, LYXP_NODE_ELEM, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004087 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004088 } else {
4089 assert(sleaf->type->basetype == LY_TYPE_INST);
Michal Vasko90189962023-02-28 12:10:34 +01004090 if (ly_path_eval(leaf->value.target, set->tree, NULL, &node)) {
Michal Vaskoba99a3e2020-08-18 15:50:05 +02004091 LOGERR(set->ctx, LY_EVALID, "Invalid instance-identifier \"%s\" value - required instance not found.",
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02004092 lyd_get_value(&leaf->node));
steweg67388952024-01-25 12:14:50 +01004093 ret = LY_EVALID;
4094 goto cleanup;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004095 }
Michal Vasko004d3152020-06-11 19:59:22 +02004096
steweg67388952024-01-25 12:14:50 +01004097 /* insert it */
4098 set_insert_node(set, node, 0, LYXP_NODE_ELEM, 0);
4099 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004100 }
4101 }
4102
steweg67388952024-01-25 12:14:50 +01004103cleanup:
4104 ly_set_free(targets, NULL);
4105 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004106}
4107
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004108static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02004109xpath_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 +02004110{
Michal Vaskofe1af042022-07-29 14:58:59 +02004111 uint32_t i, id_len;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004112 LY_ARRAY_COUNT_TYPE u;
4113 struct lyd_node_term *leaf;
4114 struct lysc_node_leaf *sleaf;
4115 struct lyd_meta *meta;
Michal Vasko93923692021-05-07 15:28:02 +02004116 struct lyd_value *val;
4117 const struct lys_module *mod;
4118 const char *id_name;
Michal Vasko93923692021-05-07 15:28:02 +02004119 struct lysc_ident *id;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004120 LY_ERR rc = LY_SUCCESS;
Radek Krejci857189e2020-09-01 13:26:36 +02004121 ly_bool found;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004122
4123 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02004124 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004125 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", func);
Michal Vasko5676f4e2021-04-06 17:14:45 +02004126 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4127 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4128 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype),
4129 sleaf->name);
4130 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_IDENT)) {
4131 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"identityref\".", func, sleaf->name);
4132 }
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004133 }
4134
4135 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4136 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4137 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype),
Michal Vasko69730152020-10-09 16:30:07 +02004138 sleaf->name);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004139 } else if (!warn_is_string_type(sleaf->type)) {
4140 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", func, sleaf->name);
4141 }
4142 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004143 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004144 return rc;
4145 }
4146
4147 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004148 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 +02004149 return LY_EVALID;
4150 }
4151 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
4152 LY_CHECK_RET(rc);
4153
Michal Vasko93923692021-05-07 15:28:02 +02004154 /* parse the identity */
4155 id_name = args[1]->val.str;
4156 id_len = strlen(id_name);
4157 rc = moveto_resolve_model(&id_name, &id_len, set, set->cur_node ? set->cur_node->schema : NULL, &mod);
4158 LY_CHECK_RET(rc);
4159 if (!mod) {
4160 LOGVAL(set->ctx, LYVE_XPATH, "Identity \"%.*s\" without a prefix.", (int)id_len, id_name);
4161 return LY_EVALID;
4162 }
4163
4164 /* find the identity */
4165 found = 0;
4166 LY_ARRAY_FOR(mod->identities, u) {
4167 if (!ly_strncmp(mod->identities[u].name, id_name, id_len)) {
4168 /* we have match */
4169 found = 1;
4170 break;
4171 }
4172 }
4173 if (!found) {
4174 LOGVAL(set->ctx, LYVE_XPATH, "Identity \"%.*s\" not found in module \"%s\".", (int)id_len, id_name, mod->name);
4175 return LY_EVALID;
4176 }
4177 id = &mod->identities[u];
4178
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004179 set_fill_boolean(set, 0);
4180 found = 0;
4181 for (i = 0; i < args[0]->used; ++i) {
4182 if ((args[0]->val.nodes[i].type != LYXP_NODE_ELEM) && (args[0]->val.nodes[i].type != LYXP_NODE_META)) {
4183 continue;
4184 }
4185
4186 if (args[0]->val.nodes[i].type == LYXP_NODE_ELEM) {
4187 leaf = (struct lyd_node_term *)args[0]->val.nodes[i].node;
4188 sleaf = (struct lysc_node_leaf *)leaf->schema;
4189 val = &leaf->value;
zhangtaogb3d079f2024-02-21 16:34:11 +08004190 if (!sleaf || !(sleaf->nodetype & LYD_NODE_TERM) || (leaf->value.realtype->basetype != LY_TYPE_IDENT)) {
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004191 /* uninteresting */
4192 continue;
4193 }
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004194 } else {
4195 meta = args[0]->val.meta[i].meta;
4196 val = &meta->value;
4197 if (val->realtype->basetype != LY_TYPE_IDENT) {
4198 /* uninteresting */
4199 continue;
4200 }
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004201 }
4202
Michal Vasko93923692021-05-07 15:28:02 +02004203 /* check the identity itself */
4204 if (self_match && (id == val->ident)) {
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004205 set_fill_boolean(set, 1);
4206 found = 1;
4207 }
Michal Vasko93923692021-05-07 15:28:02 +02004208 if (!found && !lyplg_type_identity_isderived(id, val->ident)) {
4209 set_fill_boolean(set, 1);
4210 found = 1;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004211 }
4212
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004213 if (found) {
4214 break;
4215 }
4216 }
4217
4218 return LY_SUCCESS;
4219}
4220
Michal Vasko03ff5a72019-09-11 13:49:33 +02004221/**
4222 * @brief Execute the YANG 1.1 derived-from(node-set, string) function. Returns LYXP_SET_BOOLEAN depending
4223 * on whether the first argument nodes contain a node of an identity derived from the second
4224 * argument identity.
4225 *
4226 * @param[in] args Array of arguments.
4227 * @param[in] arg_count Count of elements in @p args.
4228 * @param[in,out] set Context and result set at the same time.
4229 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004230 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004231 */
4232static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004233xpath_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 +02004234{
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004235 return xpath_derived_(args, set, options, 0, __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004236}
4237
4238/**
4239 * @brief Execute the YANG 1.1 derived-from-or-self(node-set, string) function. Returns LYXP_SET_BOOLEAN depending
4240 * on whether the first argument nodes contain a node of an identity that either is or is derived from
4241 * the second argument identity.
4242 *
4243 * @param[in] args Array of arguments.
4244 * @param[in] arg_count Count of elements in @p args.
4245 * @param[in,out] set Context and result set at the same time.
4246 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004247 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004248 */
4249static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004250xpath_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 +02004251{
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004252 return xpath_derived_(args, set, options, 1, __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004253}
4254
4255/**
4256 * @brief Execute the YANG 1.1 enum-value(node-set) function. Returns LYXP_SET_NUMBER
4257 * with the integer value of the first node's enum value, otherwise NaN.
4258 *
4259 * @param[in] args Array of arguments.
4260 * @param[in] arg_count Count of elements in @p args.
4261 * @param[in,out] set Context and result set at the same time.
4262 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004263 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004264 */
4265static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004266xpath_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 +02004267{
4268 struct lyd_node_term *leaf;
4269 struct lysc_node_leaf *sleaf;
4270 LY_ERR rc = LY_SUCCESS;
4271
4272 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02004273 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004274 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02004275 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4276 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4277 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
4278 sleaf->name);
4279 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_ENUM)) {
4280 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"enumeration\".", __func__, sleaf->name);
4281 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004282 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004283 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004284 return rc;
4285 }
4286
Michal Vaskod3678892020-05-21 10:06:58 +02004287 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004288 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "enum-value(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004289 return LY_EVALID;
4290 }
4291
4292 set_fill_number(set, NAN);
Michal Vaskod3678892020-05-21 10:06:58 +02004293 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004294 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
4295 sleaf = (struct lysc_node_leaf *)leaf->schema;
4296 if ((sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (sleaf->type->basetype == LY_TYPE_ENUM)) {
4297 set_fill_number(set, leaf->value.enum_item->value);
4298 }
4299 }
4300
4301 return LY_SUCCESS;
4302}
4303
4304/**
4305 * @brief Execute the XPath false() function. Returns LYXP_SET_BOOLEAN
4306 * with false value.
4307 *
4308 * @param[in] args Array of arguments.
4309 * @param[in] arg_count Count of elements in @p args.
4310 * @param[in,out] set Context and result set at the same time.
4311 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004312 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004313 */
4314static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004315xpath_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 +02004316{
4317 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004318 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004319 return LY_SUCCESS;
4320 }
4321
4322 set_fill_boolean(set, 0);
4323 return LY_SUCCESS;
4324}
4325
4326/**
4327 * @brief Execute the XPath floor(number) function. Returns LYXP_SET_NUMBER
4328 * with the first argument floored (truncated).
4329 *
4330 * @param[in] args Array of arguments.
4331 * @param[in] arg_count Count of elements in @p args.
4332 * @param[in,out] set Context and result set at the same time.
4333 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004334 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004335 */
4336static LY_ERR
Michal Vasko0ff8d632023-04-06 12:31:17 +02004337xpath_floor(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004338{
Michal Vasko0ff8d632023-04-06 12:31:17 +02004339 struct lysc_node_leaf *sleaf;
4340 LY_ERR rc = LY_SUCCESS;
4341
4342 if (options & LYXP_SCNODE_ALL) {
4343 if (args[0]->type != LYXP_SET_SCNODE_SET) {
4344 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
4345 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4346 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4347 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
4348 sleaf->name);
4349 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
4350 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
4351 }
4352 }
4353 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
4354 return rc;
4355 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004356
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004357 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004358 LY_CHECK_RET(rc);
4359 if (isfinite(args[0]->val.num)) {
4360 set_fill_number(set, (long long)args[0]->val.num);
4361 }
4362
4363 return LY_SUCCESS;
4364}
4365
4366/**
4367 * @brief Execute the XPath lang(string) function. Returns LYXP_SET_BOOLEAN
4368 * whether the language of the text matches the one from the argument.
4369 *
4370 * @param[in] args Array of arguments.
4371 * @param[in] arg_count Count of elements in @p args.
4372 * @param[in,out] set Context and result set at the same time.
4373 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004374 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004375 */
4376static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004377xpath_lang(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004378{
4379 const struct lyd_node *node;
4380 struct lysc_node_leaf *sleaf;
Michal Vasko9f96a052020-03-10 09:41:45 +01004381 struct lyd_meta *meta = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004382 const char *val;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004383 LY_ERR rc = LY_SUCCESS;
4384
4385 if (options & LYXP_SCNODE_ALL) {
4386 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4387 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004388 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
4389 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004390 } else if (!warn_is_string_type(sleaf->type)) {
4391 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004392 }
4393 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004394 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004395 return rc;
4396 }
4397
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004398 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004399 LY_CHECK_RET(rc);
4400
Michal Vasko03ff5a72019-09-11 13:49:33 +02004401 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004402 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "lang(string)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004403 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004404 } else if (!set->used) {
4405 set_fill_boolean(set, 0);
4406 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004407 }
4408
4409 switch (set->val.nodes[0].type) {
4410 case LYXP_NODE_ELEM:
4411 case LYXP_NODE_TEXT:
4412 node = set->val.nodes[0].node;
4413 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004414 case LYXP_NODE_META:
4415 node = set->val.meta[0].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004416 break;
4417 default:
4418 /* nothing to do with roots */
4419 set_fill_boolean(set, 0);
4420 return LY_SUCCESS;
4421 }
4422
Michal Vasko9f96a052020-03-10 09:41:45 +01004423 /* find lang metadata */
Michal Vasko9e685082021-01-29 14:49:09 +01004424 for ( ; node; node = lyd_parent(node)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01004425 for (meta = node->meta; meta; meta = meta->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004426 /* annotations */
Michal Vasko9f96a052020-03-10 09:41:45 +01004427 if (meta->name && !strcmp(meta->name, "lang") && !strcmp(meta->annotation->module->name, "xml")) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004428 break;
4429 }
4430 }
4431
Michal Vasko9f96a052020-03-10 09:41:45 +01004432 if (meta) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004433 break;
4434 }
4435 }
4436
4437 /* compare languages */
Michal Vasko9f96a052020-03-10 09:41:45 +01004438 if (!meta) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004439 set_fill_boolean(set, 0);
4440 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004441 uint64_t i;
4442
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02004443 val = lyd_get_meta_value(meta);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004444 for (i = 0; args[0]->val.str[i]; ++i) {
4445 if (tolower(args[0]->val.str[i]) != tolower(val[i])) {
4446 set_fill_boolean(set, 0);
4447 break;
4448 }
4449 }
4450 if (!args[0]->val.str[i]) {
4451 if (!val[i] || (val[i] == '-')) {
4452 set_fill_boolean(set, 1);
4453 } else {
4454 set_fill_boolean(set, 0);
4455 }
4456 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004457 }
4458
4459 return LY_SUCCESS;
4460}
4461
4462/**
4463 * @brief Execute the XPath last() function. Returns LYXP_SET_NUMBER
4464 * with the context size.
4465 *
4466 * @param[in] args Array of arguments.
4467 * @param[in] arg_count Count of elements in @p args.
4468 * @param[in,out] set Context and result set at the same time.
4469 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004470 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004471 */
4472static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004473xpath_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 +02004474{
4475 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004476 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004477 return LY_SUCCESS;
4478 }
4479
Michal Vasko03ff5a72019-09-11 13:49:33 +02004480 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004481 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "last()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004482 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004483 } else if (!set->used) {
4484 set_fill_number(set, 0);
4485 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004486 }
4487
4488 set_fill_number(set, set->ctx_size);
4489 return LY_SUCCESS;
4490}
4491
4492/**
4493 * @brief Execute the XPath local-name(node-set?) function. Returns LYXP_SET_STRING
4494 * with the node name without namespace from the argument or the context.
4495 *
4496 * @param[in] args Array of arguments.
4497 * @param[in] arg_count Count of elements in @p args.
4498 * @param[in,out] set Context and result set at the same time.
4499 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004500 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004501 */
4502static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004503xpath_local_name(struct lyxp_set **args, uint32_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004504{
4505 struct lyxp_set_node *item;
Michal Vasko69730152020-10-09 16:30:07 +02004506
Michal Vasko03ff5a72019-09-11 13:49:33 +02004507 /* suppress unused variable warning */
4508 (void)options;
4509
4510 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004511 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004512 return LY_SUCCESS;
4513 }
4514
4515 if (arg_count) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004516 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004517 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004518 "local-name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004519 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004520 } else if (!args[0]->used) {
4521 set_fill_string(set, "", 0);
4522 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004523 }
4524
4525 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004526 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004527
4528 item = &args[0]->val.nodes[0];
4529 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004530 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004531 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "local-name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004532 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004533 } else if (!set->used) {
4534 set_fill_string(set, "", 0);
4535 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004536 }
4537
4538 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004539 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004540
4541 item = &set->val.nodes[0];
4542 }
4543
4544 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004545 case LYXP_NODE_NONE:
4546 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004547 case LYXP_NODE_ROOT:
4548 case LYXP_NODE_ROOT_CONFIG:
4549 case LYXP_NODE_TEXT:
4550 set_fill_string(set, "", 0);
4551 break;
4552 case LYXP_NODE_ELEM:
Michal Vasko222be682023-08-24 08:17:12 +02004553 set_fill_string(set, LYD_NAME(item->node), strlen(LYD_NAME(item->node)));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004554 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004555 case LYXP_NODE_META:
4556 set_fill_string(set, ((struct lyd_meta *)item->node)->name, strlen(((struct lyd_meta *)item->node)->name));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004557 break;
4558 }
4559
4560 return LY_SUCCESS;
4561}
4562
4563/**
4564 * @brief Execute the XPath name(node-set?) function. Returns LYXP_SET_STRING
4565 * with the node name fully qualified (with namespace) from the argument or the context.
4566 *
4567 * @param[in] args Array of arguments.
4568 * @param[in] arg_count Count of elements in @p args.
4569 * @param[in,out] set Context and result set at the same time.
4570 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004571 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004572 */
4573static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004574xpath_name(struct lyxp_set **args, uint32_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004575{
4576 struct lyxp_set_node *item;
Michal Vasko420cc252023-08-24 08:14:24 +02004577 const struct lys_module *mod = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004578 char *str;
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004579 const char *name = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004580
4581 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004582 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004583 return LY_SUCCESS;
4584 }
4585
4586 if (arg_count) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004587 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004588 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004589 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004590 } else if (!args[0]->used) {
4591 set_fill_string(set, "", 0);
4592 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004593 }
4594
4595 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004596 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004597
4598 item = &args[0]->val.nodes[0];
4599 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004600 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004601 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004602 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004603 } else if (!set->used) {
4604 set_fill_string(set, "", 0);
4605 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004606 }
4607
4608 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004609 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004610
4611 item = &set->val.nodes[0];
4612 }
4613
4614 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004615 case LYXP_NODE_NONE:
4616 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004617 case LYXP_NODE_ROOT:
4618 case LYXP_NODE_ROOT_CONFIG:
4619 case LYXP_NODE_TEXT:
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004620 /* keep NULL */
Michal Vasko03ff5a72019-09-11 13:49:33 +02004621 break;
4622 case LYXP_NODE_ELEM:
Michal Vasko420cc252023-08-24 08:14:24 +02004623 mod = lyd_node_module(item->node);
4624 name = LYD_NAME(item->node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004625 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004626 case LYXP_NODE_META:
4627 mod = ((struct lyd_meta *)item->node)->annotation->module;
4628 name = ((struct lyd_meta *)item->node)->name;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004629 break;
4630 }
4631
4632 if (mod && name) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004633 int rc = asprintf(&str, "%s:%s", ly_get_prefix(mod, set->format, set->prefix_data), name);
Michal Vasko26bbb272022-08-02 14:54:33 +02004634
Michal Vasko03ff5a72019-09-11 13:49:33 +02004635 LY_CHECK_ERR_RET(rc == -1, LOGMEM(set->ctx), LY_EMEM);
4636 set_fill_string(set, str, strlen(str));
4637 free(str);
4638 } else {
4639 set_fill_string(set, "", 0);
4640 }
4641
4642 return LY_SUCCESS;
4643}
4644
4645/**
4646 * @brief Execute the XPath namespace-uri(node-set?) function. Returns LYXP_SET_STRING
4647 * with the namespace of the node from the argument or the context.
4648 *
4649 * @param[in] args Array of arguments.
4650 * @param[in] arg_count Count of elements in @p args.
4651 * @param[in,out] set Context and result set at the same time.
4652 * @param[in] options XPath options.
4653 * @return LY_ERR (LY_EINVAL for wrong arguments on schema)
4654 */
4655static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004656xpath_namespace_uri(struct lyxp_set **args, uint32_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004657{
4658 struct lyxp_set_node *item;
Michal Vasko420cc252023-08-24 08:14:24 +02004659 const struct lys_module *mod;
Michal Vasko69730152020-10-09 16:30:07 +02004660
Michal Vasko03ff5a72019-09-11 13:49:33 +02004661 /* suppress unused variable warning */
4662 (void)options;
4663
4664 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004665 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004666 return LY_SUCCESS;
4667 }
4668
4669 if (arg_count) {
Michal Vaskod3678892020-05-21 10:06:58 +02004670 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004671 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
Michal Vasko69730152020-10-09 16:30:07 +02004672 "namespace-uri(node-set?)");
Michal Vaskod3678892020-05-21 10:06:58 +02004673 return LY_EVALID;
4674 } else if (!args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004675 set_fill_string(set, "", 0);
4676 return LY_SUCCESS;
4677 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004678
4679 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004680 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004681
4682 item = &args[0]->val.nodes[0];
4683 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004684 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004685 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "namespace-uri(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004686 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004687 } else if (!set->used) {
4688 set_fill_string(set, "", 0);
4689 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004690 }
4691
4692 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004693 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004694
4695 item = &set->val.nodes[0];
4696 }
4697
4698 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004699 case LYXP_NODE_NONE:
4700 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004701 case LYXP_NODE_ROOT:
4702 case LYXP_NODE_ROOT_CONFIG:
4703 case LYXP_NODE_TEXT:
4704 set_fill_string(set, "", 0);
4705 break;
4706 case LYXP_NODE_ELEM:
Michal Vasko9f96a052020-03-10 09:41:45 +01004707 case LYXP_NODE_META:
Michal Vasko03ff5a72019-09-11 13:49:33 +02004708 if (item->type == LYXP_NODE_ELEM) {
Michal Vasko420cc252023-08-24 08:14:24 +02004709 mod = lyd_node_module(item->node);
Michal Vasko9f96a052020-03-10 09:41:45 +01004710 } else { /* LYXP_NODE_META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02004711 /* annotations */
Michal Vasko9f96a052020-03-10 09:41:45 +01004712 mod = ((struct lyd_meta *)item->node)->annotation->module;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004713 }
4714
4715 set_fill_string(set, mod->ns, strlen(mod->ns));
4716 break;
4717 }
4718
4719 return LY_SUCCESS;
4720}
4721
4722/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02004723 * @brief Execute the XPath normalize-space(string?) function. Returns LYXP_SET_STRING
4724 * with normalized value (no leading, trailing, double white spaces) of the node
4725 * from the argument or the context.
4726 *
4727 * @param[in] args Array of arguments.
4728 * @param[in] arg_count Count of elements in @p args.
4729 * @param[in,out] set Context and result set at the same time.
4730 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004731 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004732 */
4733static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004734xpath_normalize_space(struct lyxp_set **args, uint32_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004735{
Michal Vaskodd528af2022-08-08 14:35:07 +02004736 uint32_t i, new_used;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004737 char *new;
Radek Krejci857189e2020-09-01 13:26:36 +02004738 ly_bool have_spaces = 0, space_before = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004739 struct lysc_node_leaf *sleaf;
4740 LY_ERR rc = LY_SUCCESS;
4741
4742 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004743 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) &&
4744 (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004745 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004746 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
4747 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004748 } else if (!warn_is_string_type(sleaf->type)) {
4749 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004750 }
4751 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004752 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004753 return rc;
4754 }
4755
4756 if (arg_count) {
4757 set_fill_set(set, args[0]);
4758 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004759 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004760 LY_CHECK_RET(rc);
4761
4762 /* is there any normalization necessary? */
4763 for (i = 0; set->val.str[i]; ++i) {
4764 if (is_xmlws(set->val.str[i])) {
4765 if ((i == 0) || space_before || (!set->val.str[i + 1])) {
4766 have_spaces = 1;
4767 break;
4768 }
4769 space_before = 1;
4770 } else {
4771 space_before = 0;
4772 }
4773 }
4774
4775 /* yep, there is */
4776 if (have_spaces) {
4777 /* it's enough, at least one character will go, makes space for ending '\0' */
4778 new = malloc(strlen(set->val.str) * sizeof(char));
4779 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4780 new_used = 0;
4781
4782 space_before = 0;
4783 for (i = 0; set->val.str[i]; ++i) {
4784 if (is_xmlws(set->val.str[i])) {
4785 if ((i == 0) || space_before) {
4786 space_before = 1;
4787 continue;
4788 } else {
4789 space_before = 1;
4790 }
4791 } else {
4792 space_before = 0;
4793 }
4794
4795 new[new_used] = (space_before ? ' ' : set->val.str[i]);
4796 ++new_used;
4797 }
4798
4799 /* at worst there is one trailing space now */
4800 if (new_used && is_xmlws(new[new_used - 1])) {
4801 --new_used;
4802 }
4803
4804 new = ly_realloc(new, (new_used + 1) * sizeof(char));
4805 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4806 new[new_used] = '\0';
4807
4808 free(set->val.str);
4809 set->val.str = new;
4810 }
4811
4812 return LY_SUCCESS;
4813}
4814
4815/**
4816 * @brief Execute the XPath not(boolean) function. Returns LYXP_SET_BOOLEAN
4817 * with the argument converted to boolean and logically inverted.
4818 *
4819 * @param[in] args Array of arguments.
4820 * @param[in] arg_count Count of elements in @p args.
4821 * @param[in,out] set Context and result set at the same time.
4822 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004823 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004824 */
4825static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004826xpath_not(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004827{
4828 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004829 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004830 return LY_SUCCESS;
4831 }
4832
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004833 lyxp_set_cast(args[0], LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +02004834 if (args[0]->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004835 set_fill_boolean(set, 0);
4836 } else {
4837 set_fill_boolean(set, 1);
4838 }
4839
4840 return LY_SUCCESS;
4841}
4842
4843/**
4844 * @brief Execute the XPath number(object?) function. Returns LYXP_SET_NUMBER
4845 * with the number representation of either the argument or the context.
4846 *
4847 * @param[in] args Array of arguments.
4848 * @param[in] arg_count Count of elements in @p args.
4849 * @param[in,out] set Context and result set at the same time.
4850 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004851 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004852 */
4853static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004854xpath_number(struct lyxp_set **args, uint32_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004855{
4856 LY_ERR rc;
4857
4858 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004859 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004860 return LY_SUCCESS;
4861 }
4862
4863 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004864 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004865 LY_CHECK_RET(rc);
4866 set_fill_set(set, args[0]);
4867 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004868 rc = lyxp_set_cast(set, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004869 LY_CHECK_RET(rc);
4870 }
4871
4872 return LY_SUCCESS;
4873}
4874
4875/**
4876 * @brief Execute the XPath position() function. Returns LYXP_SET_NUMBER
4877 * with the context position.
4878 *
4879 * @param[in] args Array of arguments.
4880 * @param[in] arg_count Count of elements in @p args.
4881 * @param[in,out] set Context and result set at the same time.
4882 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004883 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004884 */
4885static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004886xpath_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 +02004887{
4888 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004889 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004890 return LY_SUCCESS;
4891 }
4892
Michal Vasko03ff5a72019-09-11 13:49:33 +02004893 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004894 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "position()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004895 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004896 } else if (!set->used) {
4897 set_fill_number(set, 0);
4898 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004899 }
4900
4901 set_fill_number(set, set->ctx_pos);
4902
4903 /* UNUSED in 'Release' build type */
4904 (void)options;
4905 return LY_SUCCESS;
4906}
4907
4908/**
4909 * @brief Execute the YANG 1.1 re-match(string, string) function. Returns LYXP_SET_BOOLEAN
4910 * depending on whether the second argument regex matches the first argument string. For details refer to
4911 * YANG 1.1 RFC section 10.2.1.
4912 *
4913 * @param[in] args Array of arguments.
4914 * @param[in] arg_count Count of elements in @p args.
4915 * @param[in,out] set Context and result set at the same time.
4916 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004917 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004918 */
4919static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004920xpath_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 +02004921{
4922 struct lysc_pattern **patterns = NULL, **pattern;
4923 struct lysc_node_leaf *sleaf;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004924 LY_ERR rc = LY_SUCCESS;
4925 struct ly_err_item *err;
4926
4927 if (options & LYXP_SCNODE_ALL) {
4928 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4929 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4930 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 +02004931 } else if (!warn_is_string_type(sleaf->type)) {
4932 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004933 }
4934 }
4935
4936 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4937 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4938 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 +02004939 } else if (!warn_is_string_type(sleaf->type)) {
4940 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004941 }
4942 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004943 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004944 return rc;
4945 }
4946
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004947 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004948 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004949 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004950 LY_CHECK_RET(rc);
4951
4952 LY_ARRAY_NEW_RET(set->ctx, patterns, pattern, LY_EMEM);
Radek Iša45802b52021-02-09 09:21:58 +01004953 *pattern = calloc(1, sizeof **pattern);
Radek Krejciddace2c2021-01-08 11:30:56 +01004954 LOG_LOCSET(NULL, set->cur_node, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01004955 rc = lys_compile_type_pattern_check(set->ctx, args[1]->val.str, &(*pattern)->code);
Michal Vasko4a7d4d62021-12-13 17:05:06 +01004956 if (set->cur_node) {
4957 LOG_LOCBACK(0, 1, 0, 0);
4958 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004959 if (rc != LY_SUCCESS) {
4960 LY_ARRAY_FREE(patterns);
4961 return rc;
4962 }
4963
Radek Krejci0b013302021-03-29 15:22:32 +02004964 rc = lyplg_type_validate_patterns(patterns, args[0]->val.str, strlen(args[0]->val.str), &err);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004965 pcre2_code_free((*pattern)->code);
4966 free(*pattern);
4967 LY_ARRAY_FREE(patterns);
4968 if (rc && (rc != LY_EVALID)) {
Michal Vasko177d0ed2020-11-23 16:43:03 +01004969 ly_err_print(set->ctx, err);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004970 ly_err_free(err);
4971 return rc;
4972 }
4973
4974 if (rc == LY_EVALID) {
4975 ly_err_free(err);
4976 set_fill_boolean(set, 0);
4977 } else {
4978 set_fill_boolean(set, 1);
4979 }
4980
4981 return LY_SUCCESS;
4982}
4983
4984/**
4985 * @brief Execute the XPath round(number) function. Returns LYXP_SET_NUMBER
4986 * with the rounded first argument. For details refer to
4987 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-round.
4988 *
4989 * @param[in] args Array of arguments.
4990 * @param[in] arg_count Count of elements in @p args.
4991 * @param[in,out] set Context and result set at the same time.
4992 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004993 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004994 */
4995static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004996xpath_round(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004997{
4998 struct lysc_node_leaf *sleaf;
4999 LY_ERR rc = LY_SUCCESS;
5000
5001 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02005002 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005003 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02005004 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5005 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5006 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
5007 sleaf->name);
5008 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
5009 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
5010 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005011 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005012 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005013 return rc;
5014 }
5015
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005016 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005017 LY_CHECK_RET(rc);
5018
5019 /* cover only the cases where floor can't be used */
5020 if ((args[0]->val.num == -0.0f) || ((args[0]->val.num < 0) && (args[0]->val.num >= -0.5))) {
5021 set_fill_number(set, -0.0f);
5022 } else {
5023 args[0]->val.num += 0.5;
5024 rc = xpath_floor(args, 1, args[0], options);
5025 LY_CHECK_RET(rc);
5026 set_fill_number(set, args[0]->val.num);
5027 }
5028
5029 return LY_SUCCESS;
5030}
5031
5032/**
5033 * @brief Execute the XPath starts-with(string, string) function.
5034 * Returns LYXP_SET_BOOLEAN whether the second argument is
5035 * the prefix of the first or not.
5036 *
5037 * @param[in] args Array of arguments.
5038 * @param[in] arg_count Count of elements in @p args.
5039 * @param[in,out] set Context and result set at the same time.
5040 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005041 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005042 */
5043static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02005044xpath_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 +02005045{
5046 struct lysc_node_leaf *sleaf;
5047 LY_ERR rc = LY_SUCCESS;
5048
5049 if (options & LYXP_SCNODE_ALL) {
5050 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5051 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5052 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 +02005053 } else if (!warn_is_string_type(sleaf->type)) {
5054 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005055 }
5056 }
5057
5058 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5059 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5060 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 +02005061 } else if (!warn_is_string_type(sleaf->type)) {
5062 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005063 }
5064 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005065 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005066 return rc;
5067 }
5068
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005069 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005070 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005071 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005072 LY_CHECK_RET(rc);
5073
5074 if (strncmp(args[0]->val.str, args[1]->val.str, strlen(args[1]->val.str))) {
5075 set_fill_boolean(set, 0);
5076 } else {
5077 set_fill_boolean(set, 1);
5078 }
5079
5080 return LY_SUCCESS;
5081}
5082
5083/**
5084 * @brief Execute the XPath string(object?) function. Returns LYXP_SET_STRING
5085 * with the string representation of either the argument or the context.
5086 *
5087 * @param[in] args Array of arguments.
5088 * @param[in] arg_count Count of elements in @p args.
5089 * @param[in,out] set Context and result set at the same time.
5090 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005091 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005092 */
5093static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02005094xpath_string(struct lyxp_set **args, uint32_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005095{
5096 LY_ERR rc;
5097
5098 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02005099 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005100 return LY_SUCCESS;
5101 }
5102
5103 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005104 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005105 LY_CHECK_RET(rc);
5106 set_fill_set(set, args[0]);
5107 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005108 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005109 LY_CHECK_RET(rc);
5110 }
5111
5112 return LY_SUCCESS;
5113}
5114
5115/**
5116 * @brief Execute the XPath string-length(string?) function. Returns LYXP_SET_NUMBER
5117 * with the length of the string in either the argument or the context.
5118 *
5119 * @param[in] args Array of arguments.
5120 * @param[in] arg_count Count of elements in @p args.
5121 * @param[in,out] set Context and result set at the same time.
5122 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005123 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005124 */
5125static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02005126xpath_string_length(struct lyxp_set **args, uint32_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005127{
5128 struct lysc_node_leaf *sleaf;
5129 LY_ERR rc = LY_SUCCESS;
5130
5131 if (options & LYXP_SCNODE_ALL) {
5132 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5133 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5134 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 +02005135 } else if (!warn_is_string_type(sleaf->type)) {
5136 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005137 }
5138 }
5139 if (!arg_count && (set->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set))) {
5140 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5141 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 +02005142 } else if (!warn_is_string_type(sleaf->type)) {
5143 LOGWRN(set->ctx, "Argument #0 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005144 }
5145 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005146 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005147 return rc;
5148 }
5149
5150 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005151 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005152 LY_CHECK_RET(rc);
5153 set_fill_number(set, strlen(args[0]->val.str));
5154 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005155 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005156 LY_CHECK_RET(rc);
5157 set_fill_number(set, strlen(set->val.str));
5158 }
5159
5160 return LY_SUCCESS;
5161}
5162
5163/**
5164 * @brief Execute the XPath substring(string, number, number?) function.
5165 * Returns LYXP_SET_STRING substring of the first argument starting
5166 * on the second argument index ending on the third argument index,
5167 * indexed from 1. For exact definition refer to
5168 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-substring.
5169 *
5170 * @param[in] args Array of arguments.
5171 * @param[in] arg_count Count of elements in @p args.
5172 * @param[in,out] set Context and result set at the same time.
5173 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005174 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005175 */
5176static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02005177xpath_substring(struct lyxp_set **args, uint32_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005178{
Michal Vasko6db996e2022-07-28 10:28:04 +02005179 int64_t start;
5180 int32_t len;
Michal Vaskodd528af2022-08-08 14:35:07 +02005181 uint32_t str_start, str_len, pos;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005182 struct lysc_node_leaf *sleaf;
5183 LY_ERR rc = LY_SUCCESS;
5184
5185 if (options & LYXP_SCNODE_ALL) {
5186 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5187 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5188 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 +02005189 } else if (!warn_is_string_type(sleaf->type)) {
5190 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005191 }
5192 }
5193
5194 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5195 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5196 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 +02005197 } else if (!warn_is_numeric_type(sleaf->type)) {
5198 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005199 }
5200 }
5201
Michal Vasko69730152020-10-09 16:30:07 +02005202 if ((arg_count == 3) && (args[2]->type == LYXP_SET_SCNODE_SET) &&
5203 (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005204 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5205 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 +02005206 } else if (!warn_is_numeric_type(sleaf->type)) {
5207 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005208 }
5209 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005210 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005211 return rc;
5212 }
5213
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005214 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005215 LY_CHECK_RET(rc);
5216
5217 /* start */
5218 if (xpath_round(&args[1], 1, args[1], options)) {
5219 return -1;
5220 }
5221 if (isfinite(args[1]->val.num)) {
5222 start = args[1]->val.num - 1;
5223 } else if (isinf(args[1]->val.num) && signbit(args[1]->val.num)) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02005224 start = INT32_MIN;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005225 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02005226 start = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005227 }
5228
5229 /* len */
5230 if (arg_count == 3) {
5231 rc = xpath_round(&args[2], 1, args[2], options);
5232 LY_CHECK_RET(rc);
Radek Krejci1deb5be2020-08-26 16:43:36 +02005233 if (isnan(args[2]->val.num) || signbit(args[2]->val.num)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005234 len = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005235 } else if (isfinite(args[2]->val.num)) {
5236 len = args[2]->val.num;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005237 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02005238 len = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005239 }
5240 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02005241 len = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005242 }
5243
5244 /* find matching character positions */
5245 str_start = 0;
5246 str_len = 0;
5247 for (pos = 0; args[0]->val.str[pos]; ++pos) {
5248 if (pos < start) {
5249 ++str_start;
5250 } else if (pos < start + len) {
5251 ++str_len;
5252 } else {
5253 break;
5254 }
5255 }
5256
5257 set_fill_string(set, args[0]->val.str + str_start, str_len);
5258 return LY_SUCCESS;
5259}
5260
5261/**
5262 * @brief Execute the XPath substring-after(string, string) function.
5263 * Returns LYXP_SET_STRING with the string succeeding the occurance
5264 * of the second argument in the first or an empty string.
5265 *
5266 * @param[in] args Array of arguments.
5267 * @param[in] arg_count Count of elements in @p args.
5268 * @param[in,out] set Context and result set at the same time.
5269 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005270 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005271 */
5272static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02005273xpath_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 +02005274{
5275 char *ptr;
5276 struct lysc_node_leaf *sleaf;
5277 LY_ERR rc = LY_SUCCESS;
5278
5279 if (options & LYXP_SCNODE_ALL) {
5280 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5281 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5282 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 +02005283 } else if (!warn_is_string_type(sleaf->type)) {
5284 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005285 }
5286 }
5287
5288 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5289 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5290 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 +02005291 } else if (!warn_is_string_type(sleaf->type)) {
5292 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005293 }
5294 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005295 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005296 return rc;
5297 }
5298
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005299 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005300 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005301 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005302 LY_CHECK_RET(rc);
5303
5304 ptr = strstr(args[0]->val.str, args[1]->val.str);
5305 if (ptr) {
5306 set_fill_string(set, ptr + strlen(args[1]->val.str), strlen(ptr + strlen(args[1]->val.str)));
5307 } else {
5308 set_fill_string(set, "", 0);
5309 }
5310
5311 return LY_SUCCESS;
5312}
5313
5314/**
5315 * @brief Execute the XPath substring-before(string, string) function.
5316 * Returns LYXP_SET_STRING with the string preceding the occurance
5317 * of the second argument in the first or an empty string.
5318 *
5319 * @param[in] args Array of arguments.
5320 * @param[in] arg_count Count of elements in @p args.
5321 * @param[in,out] set Context and result set at the same time.
5322 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005323 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005324 */
5325static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02005326xpath_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 +02005327{
5328 char *ptr;
5329 struct lysc_node_leaf *sleaf;
5330 LY_ERR rc = LY_SUCCESS;
5331
5332 if (options & LYXP_SCNODE_ALL) {
5333 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5334 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5335 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 +02005336 } else if (!warn_is_string_type(sleaf->type)) {
5337 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005338 }
5339 }
5340
5341 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5342 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5343 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 +02005344 } else if (!warn_is_string_type(sleaf->type)) {
5345 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005346 }
5347 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005348 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005349 return rc;
5350 }
5351
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005352 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005353 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005354 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005355 LY_CHECK_RET(rc);
5356
5357 ptr = strstr(args[0]->val.str, args[1]->val.str);
5358 if (ptr) {
5359 set_fill_string(set, args[0]->val.str, ptr - args[0]->val.str);
5360 } else {
5361 set_fill_string(set, "", 0);
5362 }
5363
5364 return LY_SUCCESS;
5365}
5366
5367/**
5368 * @brief Execute the XPath sum(node-set) function. Returns LYXP_SET_NUMBER
5369 * with the sum of all the nodes in the context.
5370 *
5371 * @param[in] args Array of arguments.
5372 * @param[in] arg_count Count of elements in @p args.
5373 * @param[in,out] set Context and result set at the same time.
5374 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005375 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005376 */
5377static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02005378xpath_sum(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005379{
5380 long double num;
5381 char *str;
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01005382 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005383 struct lyxp_set set_item;
5384 struct lysc_node_leaf *sleaf;
5385 LY_ERR rc = LY_SUCCESS;
5386
5387 if (options & LYXP_SCNODE_ALL) {
5388 if (args[0]->type == LYXP_SET_SCNODE_SET) {
5389 for (i = 0; i < args[0]->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005390 if (args[0]->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005391 sleaf = (struct lysc_node_leaf *)args[0]->val.scnodes[i].scnode;
5392 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5393 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__,
Michal Vasko69730152020-10-09 16:30:07 +02005394 lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005395 } else if (!warn_is_numeric_type(sleaf->type)) {
5396 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005397 }
5398 }
5399 }
5400 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005401 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005402 return rc;
5403 }
5404
5405 set_fill_number(set, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005406
5407 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005408 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "sum(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02005409 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02005410 } else if (!args[0]->used) {
5411 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005412 }
5413
Michal Vasko5c4e5892019-11-14 12:31:38 +01005414 set_init(&set_item, set);
5415
Michal Vasko03ff5a72019-09-11 13:49:33 +02005416 set_item.type = LYXP_SET_NODE_SET;
Michal Vasko41decbf2021-11-02 11:50:21 +01005417 set_item.val.nodes = calloc(1, sizeof *set_item.val.nodes);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005418 LY_CHECK_ERR_RET(!set_item.val.nodes, LOGMEM(set->ctx), LY_EMEM);
5419
5420 set_item.used = 1;
5421 set_item.size = 1;
5422
5423 for (i = 0; i < args[0]->used; ++i) {
5424 set_item.val.nodes[0] = args[0]->val.nodes[i];
5425
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005426 rc = cast_node_set_to_string(&set_item, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005427 LY_CHECK_RET(rc);
5428 num = cast_string_to_number(str);
5429 free(str);
5430 set->val.num += num;
5431 }
5432
5433 free(set_item.val.nodes);
5434
5435 return LY_SUCCESS;
5436}
5437
5438/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02005439 * @brief Execute the XPath translate(string, string, string) function.
5440 * Returns LYXP_SET_STRING with the first argument with the characters
5441 * from the second argument replaced by those on the corresponding
5442 * positions in the third argument.
5443 *
5444 * @param[in] args Array of arguments.
5445 * @param[in] arg_count Count of elements in @p args.
5446 * @param[in,out] set Context and result set at the same time.
5447 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005448 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005449 */
5450static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02005451xpath_translate(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005452{
Michal Vaskodd528af2022-08-08 14:35:07 +02005453 uint32_t i, j, new_used;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005454 char *new;
Radek Krejci857189e2020-09-01 13:26:36 +02005455 ly_bool have_removed;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005456 struct lysc_node_leaf *sleaf;
5457 LY_ERR rc = LY_SUCCESS;
5458
5459 if (options & LYXP_SCNODE_ALL) {
5460 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5461 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5462 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 +02005463 } else if (!warn_is_string_type(sleaf->type)) {
5464 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005465 }
5466 }
5467
5468 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5469 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5470 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 +02005471 } else if (!warn_is_string_type(sleaf->type)) {
5472 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005473 }
5474 }
5475
5476 if ((args[2]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
5477 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5478 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 +02005479 } else if (!warn_is_string_type(sleaf->type)) {
5480 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005481 }
5482 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005483 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005484 return rc;
5485 }
5486
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005487 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005488 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005489 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005490 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005491 rc = lyxp_set_cast(args[2], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005492 LY_CHECK_RET(rc);
5493
5494 new = malloc((strlen(args[0]->val.str) + 1) * sizeof(char));
5495 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5496 new_used = 0;
5497
5498 have_removed = 0;
5499 for (i = 0; args[0]->val.str[i]; ++i) {
Radek Krejci857189e2020-09-01 13:26:36 +02005500 ly_bool found = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005501
5502 for (j = 0; args[1]->val.str[j]; ++j) {
5503 if (args[0]->val.str[i] == args[1]->val.str[j]) {
5504 /* removing this char */
5505 if (j >= strlen(args[2]->val.str)) {
5506 have_removed = 1;
5507 found = 1;
5508 break;
5509 }
5510 /* replacing this char */
5511 new[new_used] = args[2]->val.str[j];
5512 ++new_used;
5513 found = 1;
5514 break;
5515 }
5516 }
5517
5518 /* copying this char */
5519 if (!found) {
5520 new[new_used] = args[0]->val.str[i];
5521 ++new_used;
5522 }
5523 }
5524
5525 if (have_removed) {
5526 new = ly_realloc(new, (new_used + 1) * sizeof(char));
5527 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5528 }
5529 new[new_used] = '\0';
5530
Michal Vaskod3678892020-05-21 10:06:58 +02005531 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005532 set->type = LYXP_SET_STRING;
5533 set->val.str = new;
5534
5535 return LY_SUCCESS;
5536}
5537
5538/**
5539 * @brief Execute the XPath true() function. Returns LYXP_SET_BOOLEAN
5540 * with true value.
5541 *
5542 * @param[in] args Array of arguments.
5543 * @param[in] arg_count Count of elements in @p args.
5544 * @param[in,out] set Context and result set at the same time.
5545 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005546 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005547 */
5548static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02005549xpath_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 +02005550{
5551 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02005552 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005553 return LY_SUCCESS;
5554 }
5555
5556 set_fill_boolean(set, 1);
5557 return LY_SUCCESS;
5558}
5559
Michal Vasko03ff5a72019-09-11 13:49:33 +02005560/**
Michal Vasko49fec8e2022-05-24 10:28:33 +02005561 * @brief Execute the XPath node() processing instruction (node type). Returns LYXP_SET_NODE_SET
5562 * with only nodes from the context.
5563 *
5564 * @param[in,out] set Context and result set at the same time.
5565 * @param[in] axis Axis to search on.
5566 * @param[in] options XPath options.
5567 * @return LY_ERR
5568 */
5569static LY_ERR
5570xpath_pi_node(struct lyxp_set *set, enum lyxp_axis axis, uint32_t options)
5571{
5572 if (options & LYXP_SCNODE_ALL) {
5573 return moveto_scnode(set, NULL, NULL, axis, options);
5574 }
5575
5576 if (set->type != LYXP_SET_NODE_SET) {
5577 lyxp_set_free_content(set);
5578 return LY_SUCCESS;
5579 }
5580
5581 /* just like moving to a node with no restrictions */
5582 return moveto_node(set, NULL, NULL, axis, options);
5583}
5584
5585/**
5586 * @brief Execute the XPath text() processing instruction (node type). Returns LYXP_SET_NODE_SET
5587 * with the text content of the nodes in the context.
5588 *
5589 * @param[in,out] set Context and result set at the same time.
5590 * @param[in] axis Axis to search on.
5591 * @param[in] options XPath options.
5592 * @return LY_ERR
5593 */
5594static LY_ERR
5595xpath_pi_text(struct lyxp_set *set, enum lyxp_axis axis, uint32_t options)
5596{
5597 uint32_t i;
5598
5599 if (options & LYXP_SCNODE_ALL) {
5600 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
5601 return LY_SUCCESS;
5602 }
5603
5604 if (set->type != LYXP_SET_NODE_SET) {
5605 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "text()");
5606 return LY_EVALID;
5607 }
5608
5609 if (axis != LYXP_AXIS_CHILD) {
5610 /* even following and preceding axescan return text nodes, but whatever */
5611 lyxp_set_free_content(set);
5612 return LY_SUCCESS;
5613 }
5614
5615 for (i = 0; i < set->used; ++i) {
5616 switch (set->val.nodes[i].type) {
5617 case LYXP_NODE_NONE:
5618 LOGINT_RET(set->ctx);
5619 case LYXP_NODE_ELEM:
Michal Vasko222be682023-08-24 08:17:12 +02005620 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 +02005621 set->val.nodes[i].type = LYXP_NODE_TEXT;
5622 break;
5623 }
5624 /* fall through */
5625 case LYXP_NODE_ROOT:
5626 case LYXP_NODE_ROOT_CONFIG:
5627 case LYXP_NODE_TEXT:
5628 case LYXP_NODE_META:
5629 set_remove_node_none(set, i);
5630 break;
5631 }
5632 }
5633 set_remove_nodes_none(set);
5634
5635 return LY_SUCCESS;
5636}
5637
5638/**
Michal Vasko6346ece2019-09-24 13:12:53 +02005639 * @brief Skip prefix and return corresponding model if there is a prefix. Logs directly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005640 *
Michal Vasko2104e9f2020-03-06 08:23:25 +01005641 * XPath @p set is expected to be a (sc)node set!
5642 *
Michal Vasko6346ece2019-09-24 13:12:53 +02005643 * @param[in,out] qname Qualified node name. If includes prefix, it is skipped.
5644 * @param[in,out] qname_len Length of @p qname, is updated accordingly.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005645 * @param[in] set Set with general XPath context.
5646 * @param[in] ctx_scnode Context node to inherit module for unprefixed node for ::LY_PREF_JSON.
Michal Vasko6346ece2019-09-24 13:12:53 +02005647 * @param[out] moveto_mod Expected module of a matching node.
5648 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005649 */
Michal Vasko6346ece2019-09-24 13:12:53 +02005650static LY_ERR
Michal Vaskofe1af042022-07-29 14:58:59 +02005651moveto_resolve_model(const char **qname, uint32_t *qname_len, const struct lyxp_set *set,
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005652 const struct lysc_node *ctx_scnode, const struct lys_module **moveto_mod)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005653{
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02005654 const struct lys_module *mod = NULL;
Michal Vasko6346ece2019-09-24 13:12:53 +02005655 const char *ptr;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005656 size_t pref_len;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005657
Michal Vasko2104e9f2020-03-06 08:23:25 +01005658 assert((set->type == LYXP_SET_NODE_SET) || (set->type == LYXP_SET_SCNODE_SET));
5659
Michal Vasko6346ece2019-09-24 13:12:53 +02005660 if ((ptr = ly_strnchr(*qname, ':', *qname_len))) {
5661 /* specific module */
5662 pref_len = ptr - *qname;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005663 mod = ly_resolve_prefix(set->ctx, *qname, pref_len, set->format, set->prefix_data);
Michal Vasko6346ece2019-09-24 13:12:53 +02005664
Michal Vasko004d3152020-06-11 19:59:22 +02005665 /* check for errors and non-implemented modules, as they are not valid */
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005666 if (!mod || !mod->implemented) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02005667 LOGVAL(set->ctx, LY_VCODE_XP_INMOD, (int)pref_len, *qname);
Michal Vasko6346ece2019-09-24 13:12:53 +02005668 return LY_EVALID;
5669 }
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005670
Michal Vasko6346ece2019-09-24 13:12:53 +02005671 *qname += pref_len + 1;
5672 *qname_len -= pref_len + 1;
5673 } else if (((*qname)[0] == '*') && (*qname_len == 1)) {
5674 /* all modules - special case */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005675 mod = NULL;
Michal Vasko6346ece2019-09-24 13:12:53 +02005676 } else {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005677 switch (set->format) {
Radek Krejci8df109d2021-04-23 12:19:08 +02005678 case LY_VALUE_SCHEMA:
5679 case LY_VALUE_SCHEMA_RESOLVED:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005680 /* current module */
5681 mod = set->cur_mod;
5682 break;
Radek Krejci224d4b42021-04-23 13:54:59 +02005683 case LY_VALUE_CANON:
Radek Krejci8df109d2021-04-23 12:19:08 +02005684 case LY_VALUE_JSON:
Radek Krejcif9943642021-04-26 10:18:21 +02005685 case LY_VALUE_LYB:
Michal Vaskoddd76592022-01-17 13:34:48 +01005686 case LY_VALUE_STR_NS:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005687 /* inherit parent (context node) module */
5688 if (ctx_scnode) {
5689 mod = ctx_scnode->module;
5690 } else {
5691 mod = NULL;
5692 }
5693 break;
Radek Krejci8df109d2021-04-23 12:19:08 +02005694 case LY_VALUE_XML:
Michal Vasko52143d12021-04-14 15:36:39 +02005695 /* all nodes need to be prefixed */
Michal Vasko21eaa392024-02-20 15:48:42 +01005696 LOGVAL(set->ctx, LYVE_DATA, "Non-prefixed node \"%.*s\" in XML xpath found.", (int)*qname_len, *qname);
Michal Vasko52143d12021-04-14 15:36:39 +02005697 return LY_EVALID;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005698 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005699 }
5700
Michal Vasko6346ece2019-09-24 13:12:53 +02005701 *moveto_mod = mod;
5702 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005703}
5704
5705/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02005706 * @brief Move context @p set to the root. Handles absolute path.
5707 * Result is LYXP_SET_NODE_SET.
5708 *
5709 * @param[in,out] set Set to use.
5710 * @param[in] options Xpath options.
Michal Vaskob0099a92020-08-31 14:55:23 +02005711 * @return LY_ERR value.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005712 */
Michal Vaskob0099a92020-08-31 14:55:23 +02005713static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005714moveto_root(struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005715{
aPiecek8b0cc152021-05-31 16:40:31 +02005716 assert(!(options & LYXP_SKIP_EXPR));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005717
5718 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02005719 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskoe4a6d012023-05-22 14:34:52 +02005720 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, NULL, set->root_type, LYXP_AXIS_SELF, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005721 } else {
Michal Vaskoef71b392023-08-16 09:45:23 +02005722 lyxp_set_free_content(set);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005723 set_insert_node(set, NULL, 0, set->root_type, 0);
Michal Vasko306e2832022-07-25 09:15:17 +02005724 set->non_child_axis = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005725 }
Michal Vaskob0099a92020-08-31 14:55:23 +02005726
5727 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005728}
5729
5730/**
5731 * @brief Check @p node as a part of NameTest processing.
5732 *
5733 * @param[in] node Node to check.
Michal Vasko49fec8e2022-05-24 10:28:33 +02005734 * @param[in] node_type Node type of @p node.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005735 * @param[in] set Set to read general context from.
Michal Vaskod3678892020-05-21 10:06:58 +02005736 * @param[in] node_name Node name in the dictionary to move to, NULL for any node.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005737 * @param[in] moveto_mod Expected module of the node, NULL for no prefix.
Michal Vaskocdad7122020-11-09 21:04:44 +01005738 * @param[in] options XPath options.
Michal Vasko6346ece2019-09-24 13:12:53 +02005739 * @return LY_ERR (LY_ENOT if node does not match, LY_EINCOMPLETE on unresolved when,
Michal Vaskocd2c88a2022-06-07 10:54:34 +02005740 * LY_EINVAL if neither node nor any children match)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005741 */
5742static LY_ERR
Michal Vasko49fec8e2022-05-24 10:28:33 +02005743moveto_node_check(const struct lyd_node *node, enum lyxp_node_type node_type, const struct lyxp_set *set,
5744 const char *node_name, const struct lys_module *moveto_mod, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005745{
Michal Vaskofbd0da42023-08-17 14:57:44 +02005746 const struct lysc_node *schema;
5747
Michal Vasko49fec8e2022-05-24 10:28:33 +02005748 if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT)) {
5749 assert(node_type == set->root_type);
5750
5751 if (node_name || moveto_mod) {
5752 /* root will not match a specific node */
5753 return LY_ENOT;
5754 }
5755 return LY_SUCCESS;
5756 } else if (node_type != LYXP_NODE_ELEM) {
5757 /* other types will not match */
5758 return LY_ENOT;
5759 }
5760
Michal Vaskofbd0da42023-08-17 14:57:44 +02005761 /* get schema node even of an opaque node */
5762 schema = lyd_node_schema(node);
5763 if (!schema) {
5764 /* unknown opaque node never matches */
Michal Vaskodca9f122021-07-16 13:56:22 +02005765 return LY_ENOT;
5766 }
5767
Michal Vasko03ff5a72019-09-11 13:49:33 +02005768 /* module check */
Michal Vasko19089f02022-06-07 11:02:11 +02005769 if (moveto_mod) {
Michal Vaskofbd0da42023-08-17 14:57:44 +02005770 if ((set->ctx == LYD_CTX(node)) && (schema->module != moveto_mod)) {
Michal Vasko19089f02022-06-07 11:02:11 +02005771 return LY_ENOT;
Michal Vaskofbd0da42023-08-17 14:57:44 +02005772 } else if ((set->ctx != LYD_CTX(node)) && strcmp(schema->module->name, moveto_mod->name)) {
Michal Vasko19089f02022-06-07 11:02:11 +02005773 return LY_ENOT;
5774 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005775 }
5776
Michal Vasko5c4e5892019-11-14 12:31:38 +01005777 /* context check */
Michal Vaskofbd0da42023-08-17 14:57:44 +02005778 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (schema->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005779 return LY_EINVAL;
Michal Vaskofbd0da42023-08-17 14:57:44 +02005780 } else if (set->context_op && (schema->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && (schema != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005781 return LY_EINVAL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005782 }
5783
5784 /* name check */
Michal Vasko19089f02022-06-07 11:02:11 +02005785 if (node_name) {
Michal Vaskofbd0da42023-08-17 14:57:44 +02005786 if ((set->ctx == LYD_CTX(node)) && (schema->name != node_name)) {
Michal Vasko19089f02022-06-07 11:02:11 +02005787 return LY_ENOT;
Michal Vaskofbd0da42023-08-17 14:57:44 +02005788 } else if ((set->ctx != LYD_CTX(node)) && strcmp(schema->name, node_name)) {
Michal Vasko19089f02022-06-07 11:02:11 +02005789 return LY_ENOT;
5790 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005791 }
5792
Michal Vasko6d657122022-10-19 14:38:35 +02005793 /* 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 +02005794 if (!(options & LYXP_IGNORE_WHEN) && lysc_has_when(schema) && !(node->flags & LYD_WHEN_TRUE) &&
Michal Vasko6d657122022-10-19 14:38:35 +02005795 (node != set->cur_node)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005796 return LY_EINCOMPLETE;
Michal Vaskoa1424542019-11-14 16:08:52 +01005797 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005798
5799 /* match */
5800 return LY_SUCCESS;
5801}
5802
5803/**
Michal Vasko49fec8e2022-05-24 10:28:33 +02005804 * @brief Get the next node in a forward DFS.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005805 *
Michal Vasko49fec8e2022-05-24 10:28:33 +02005806 * @param[in] iter Last returned node.
5807 * @param[in] stop Node to stop the search on and not return.
5808 * @return Next node, NULL if there are no more.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005809 */
Michal Vasko49fec8e2022-05-24 10:28:33 +02005810static const struct lyd_node *
5811moveto_axis_node_next_dfs_forward(const struct lyd_node *iter, const struct lyd_node *stop)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005812{
Michal Vasko49fec8e2022-05-24 10:28:33 +02005813 const struct lyd_node *next = NULL;
5814
5815 /* 1) child */
5816 next = lyd_child(iter);
5817 if (!next) {
5818 if (iter == stop) {
5819 /* reached stop, no more descendants */
5820 return NULL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005821 }
Michal Vasko49fec8e2022-05-24 10:28:33 +02005822 /* 2) child next sibling */
5823 next = iter->next;
5824 }
5825 while (!next) {
5826 iter = lyd_parent(iter);
5827 if ((!stop && !iter) || (stop && (lyd_parent(iter) == lyd_parent(stop)))) {
5828 return NULL;
5829 }
5830 next = iter->next;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005831 }
5832
Michal Vasko49fec8e2022-05-24 10:28:33 +02005833 return next;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005834}
5835
5836/**
Michal Vasko49fec8e2022-05-24 10:28:33 +02005837 * @brief Get the next node in a backward DFS.
5838 *
5839 * @param[in] iter Last returned node.
5840 * @param[in] stop Node to stop the search on and not return.
5841 * @return Next node, NULL if there are no more.
5842 */
5843static const struct lyd_node *
5844moveto_axis_node_next_dfs_backward(const struct lyd_node *iter, const struct lyd_node *stop)
5845{
5846 const struct lyd_node *next = NULL;
5847
5848 /* 1) previous sibling innermost last child */
5849 next = iter->prev->next ? iter->prev : NULL;
5850 while (next && lyd_child(next)) {
5851 next = lyd_child(next);
5852 next = next->prev;
5853 }
5854
5855 if (!next) {
5856 /* 2) parent */
5857 iter = lyd_parent(iter);
5858 if ((!stop && !iter) || (stop && (lyd_parent(iter) == lyd_parent(stop)))) {
5859 return NULL;
5860 }
5861 next = iter;
5862 }
5863
5864 return next;
5865}
5866
5867/**
5868 * @brief Get the first node on an axis for a context node.
5869 *
5870 * @param[in,out] iter NULL, updated to the next node.
5871 * @param[in,out] iter_type Node type 0 of @p iter, updated to the node type of the next node.
5872 * @param[in] node Context node.
5873 * @param[in] node_type Type of @p node.
5874 * @param[in] axis Axis to use.
5875 * @param[in] set XPath set with the general context.
5876 * @return LY_SUCCESS on success.
5877 * @return LY_ENOTFOUND if no next node found.
5878 */
5879static LY_ERR
5880moveto_axis_node_next_first(const struct lyd_node **iter, enum lyxp_node_type *iter_type, const struct lyd_node *node,
5881 enum lyxp_node_type node_type, enum lyxp_axis axis, struct lyxp_set *set)
5882{
5883 const struct lyd_node *next = NULL;
5884 enum lyxp_node_type next_type = 0;
5885
5886 assert(!*iter);
5887 assert(!*iter_type);
5888
5889 switch (axis) {
5890 case LYXP_AXIS_ANCESTOR_OR_SELF:
5891 case LYXP_AXIS_DESCENDANT_OR_SELF:
5892 case LYXP_AXIS_SELF:
5893 /* return the context node */
5894 next = node;
5895 next_type = node_type;
5896 break;
5897
5898 case LYXP_AXIS_ANCESTOR:
5899 case LYXP_AXIS_PARENT:
5900 if (node_type == LYXP_NODE_ELEM) {
5901 next = lyd_parent(node);
5902 next_type = next ? LYXP_NODE_ELEM : set->root_type;
5903 } else if (node_type == LYXP_NODE_TEXT) {
5904 next = node;
5905 next_type = LYXP_NODE_ELEM;
5906 } else if (node_type == LYXP_NODE_META) {
5907 next = ((struct lyd_meta *)node)->parent;
5908 next_type = LYXP_NODE_ELEM;
5909 } /* else root does not have a parent */
5910 break;
5911
5912 case LYXP_AXIS_CHILD:
5913 if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT)) {
5914 assert(!node);
5915
5916 /* search in all the trees */
5917 next = set->tree;
5918 next_type = next ? LYXP_NODE_ELEM : 0;
5919 } else {
5920 /* search in children */
5921 next = lyd_child(node);
5922 next_type = next ? LYXP_NODE_ELEM : 0;
5923 }
5924 break;
5925
5926 case LYXP_AXIS_DESCENDANT:
5927 if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT)) {
5928 /* top-level nodes */
5929 next = set->tree;
5930 next_type = LYXP_NODE_ELEM;
5931 } else if (node_type == LYXP_NODE_ELEM) {
5932 /* start from the context node */
5933 next = moveto_axis_node_next_dfs_forward(node, node);
5934 next_type = next ? LYXP_NODE_ELEM : 0;
5935 } /* else no children */
5936 break;
5937
5938 case LYXP_AXIS_FOLLOWING:
5939 case LYXP_AXIS_FOLLOWING_SIBLING:
5940 if (node_type == LYXP_NODE_ELEM) {
5941 /* first next sibling */
5942 next = node->next;
5943 next_type = next ? LYXP_NODE_ELEM : 0;
5944 } /* else no sibling */
5945 break;
5946
5947 case LYXP_AXIS_PRECEDING:
5948 if ((node_type == LYXP_NODE_ELEM) && node->prev->next) {
5949 /* skip ancestors */
5950 next = moveto_axis_node_next_dfs_backward(node, NULL);
5951 assert(next);
5952 next_type = LYXP_NODE_ELEM;
5953 } /* else no sibling */
5954 break;
5955
5956 case LYXP_AXIS_PRECEDING_SIBLING:
5957 if (node_type == LYXP_NODE_ELEM) {
5958 /* first previous sibling */
5959 next = node->prev->next ? node->prev : NULL;
5960 next_type = next ? LYXP_NODE_ELEM : 0;
5961 } /* else no sibling */
5962 break;
5963
5964 case LYXP_AXIS_ATTRIBUTE:
5965 /* handled specially */
5966 assert(0);
5967 LOGINT(set->ctx);
5968 break;
5969 }
5970
5971 *iter = next;
5972 *iter_type = next_type;
5973 return next_type ? LY_SUCCESS : LY_ENOTFOUND;
5974}
5975
5976/**
5977 * @brief Iterate over all nodes on an axis for a context node.
5978 *
5979 * @param[in,out] iter Last returned node, start with NULL, updated to the next node.
5980 * @param[in,out] iter_type Node type of @p iter, start with 0, updated to the node type of the next node.
5981 * @param[in] node Context node.
5982 * @param[in] node_type Type of @p node.
5983 * @param[in] axis Axis to use.
5984 * @param[in] set XPath set with the general context.
5985 * @return LY_SUCCESS on success.
5986 * @return LY_ENOTFOUND if no next node found.
5987 */
5988static LY_ERR
5989moveto_axis_node_next(const struct lyd_node **iter, enum lyxp_node_type *iter_type, const struct lyd_node *node,
5990 enum lyxp_node_type node_type, enum lyxp_axis axis, struct lyxp_set *set)
5991{
5992 const struct lyd_node *next = NULL;
5993 enum lyxp_node_type next_type = 0;
5994
5995 if (!*iter_type) {
5996 /* first returned node */
5997 return moveto_axis_node_next_first(iter, iter_type, node, node_type, axis, set);
5998 }
5999
6000 switch (axis) {
6001 case LYXP_AXIS_ANCESTOR_OR_SELF:
6002 if ((*iter == node) && (*iter_type == node_type)) {
6003 /* fake first ancestor, we returned self before */
6004 *iter = NULL;
6005 *iter_type = 0;
6006 return moveto_axis_node_next_first(iter, iter_type, node, node_type, LYXP_AXIS_ANCESTOR, set);
6007 } /* else continue ancestor */
6008
6009 /* fallthrough */
6010 case LYXP_AXIS_ANCESTOR:
6011 if (*iter_type == LYXP_NODE_ELEM) {
6012 /* iter parent */
6013 next = lyd_parent(*iter);
6014 next_type = next ? LYXP_NODE_ELEM : set->root_type;
6015 } /* else root, no ancestors */
6016 break;
6017
6018 case LYXP_AXIS_CHILD:
6019 assert(*iter_type == LYXP_NODE_ELEM);
6020
6021 /* next sibling (child) */
6022 next = (*iter)->next;
6023 next_type = next ? LYXP_NODE_ELEM : 0;
6024 break;
6025
6026 case LYXP_AXIS_DESCENDANT_OR_SELF:
6027 if ((*iter == node) && (*iter_type == node_type)) {
6028 /* fake first descendant, we returned self before */
6029 *iter = NULL;
6030 *iter_type = 0;
6031 return moveto_axis_node_next_first(iter, iter_type, node, node_type, LYXP_AXIS_DESCENDANT, set);
6032 } /* else continue descendant */
6033
6034 /* fallthrough */
6035 case LYXP_AXIS_DESCENDANT:
6036 assert(*iter_type == LYXP_NODE_ELEM);
6037 next = moveto_axis_node_next_dfs_forward(*iter, node);
6038 next_type = next ? LYXP_NODE_ELEM : 0;
6039 break;
6040
6041 case LYXP_AXIS_FOLLOWING:
6042 assert(*iter_type == LYXP_NODE_ELEM);
6043 next = moveto_axis_node_next_dfs_forward(*iter, NULL);
6044 next_type = next ? LYXP_NODE_ELEM : 0;
6045 break;
6046
6047 case LYXP_AXIS_FOLLOWING_SIBLING:
6048 assert(*iter_type == LYXP_NODE_ELEM);
6049
6050 /* next sibling */
6051 next = (*iter)->next;
6052 next_type = next ? LYXP_NODE_ELEM : 0;
6053 break;
6054
6055 case LYXP_AXIS_PARENT:
6056 case LYXP_AXIS_SELF:
6057 /* parent/self was returned before */
6058 break;
6059
6060 case LYXP_AXIS_PRECEDING:
6061 assert(*iter_type == LYXP_NODE_ELEM);
6062 next = moveto_axis_node_next_dfs_backward(*iter, NULL);
6063 next_type = next ? LYXP_NODE_ELEM : 0;
6064 break;
6065
6066 case LYXP_AXIS_PRECEDING_SIBLING:
6067 assert(*iter_type == LYXP_NODE_ELEM);
6068
6069 /* previous sibling */
6070 next = (*iter)->prev->next ? (*iter)->prev : NULL;
6071 next_type = next ? LYXP_NODE_ELEM : 0;
6072 break;
6073
6074 case LYXP_AXIS_ATTRIBUTE:
6075 /* handled specially */
6076 assert(0);
6077 LOGINT(set->ctx);
6078 break;
6079 }
6080
6081 *iter = next;
6082 *iter_type = next_type;
6083 return next_type ? LY_SUCCESS : LY_ENOTFOUND;
6084}
6085
6086/**
6087 * @brief Move context @p set to a node. Result is LYXP_SET_NODE_SET. Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006088 *
6089 * @param[in,out] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006090 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02006091 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko49fec8e2022-05-24 10:28:33 +02006092 * @param[in] axis Axis to search on.
Michal Vaskocdad7122020-11-09 21:04:44 +01006093 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006094 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6095 */
6096static LY_ERR
Michal Vasko49fec8e2022-05-24 10:28:33 +02006097moveto_node(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, enum lyxp_axis axis,
6098 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006099{
Michal Vasko230cf972021-12-02 12:31:00 +01006100 LY_ERR r, rc = LY_SUCCESS;
Michal Vasko49fec8e2022-05-24 10:28:33 +02006101 const struct lyd_node *iter;
6102 enum lyxp_node_type iter_type;
Michal Vasko230cf972021-12-02 12:31:00 +01006103 struct lyxp_set result;
Michal Vasko49fec8e2022-05-24 10:28:33 +02006104 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006105
aPiecek8b0cc152021-05-31 16:40:31 +02006106 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006107 return LY_SUCCESS;
6108 }
6109
6110 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006111 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006112 return LY_EVALID;
6113 }
6114
Michal Vasko230cf972021-12-02 12:31:00 +01006115 /* init result set */
6116 set_init(&result, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006117
Michal Vasko49fec8e2022-05-24 10:28:33 +02006118 for (i = 0; i < set->used; ++i) {
6119 /* iterate over all the nodes on the axis of the node */
6120 iter = NULL;
6121 iter_type = 0;
6122 while (!moveto_axis_node_next(&iter, &iter_type, set->val.nodes[i].node, set->val.nodes[i].type, axis, set)) {
6123 r = moveto_node_check(iter, iter_type, set, ncname, moveto_mod, options);
6124 if (r == LY_EINCOMPLETE) {
Michal Vasko230cf972021-12-02 12:31:00 +01006125 rc = r;
6126 goto cleanup;
Michal Vasko49fec8e2022-05-24 10:28:33 +02006127 } else if (r) {
6128 continue;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006129 }
Michal Vasko49fec8e2022-05-24 10:28:33 +02006130
6131 /* check for duplicates if they are possible */
6132 switch (axis) {
6133 case LYXP_AXIS_ANCESTOR:
6134 case LYXP_AXIS_ANCESTOR_OR_SELF:
6135 case LYXP_AXIS_DESCENDANT:
6136 case LYXP_AXIS_DESCENDANT_OR_SELF:
6137 case LYXP_AXIS_FOLLOWING:
6138 case LYXP_AXIS_FOLLOWING_SIBLING:
6139 case LYXP_AXIS_PARENT:
6140 case LYXP_AXIS_PRECEDING:
6141 case LYXP_AXIS_PRECEDING_SIBLING:
Michal Vasko306e2832022-07-25 09:15:17 +02006142 result.non_child_axis = 1;
Michal Vasko49fec8e2022-05-24 10:28:33 +02006143 if (set_dup_node_check(&result, iter, iter_type, -1)) {
6144 continue;
6145 }
6146 break;
6147 case LYXP_AXIS_CHILD:
6148 case LYXP_AXIS_SELF:
6149 break;
6150 case LYXP_AXIS_ATTRIBUTE:
6151 /* handled specially */
6152 assert(0);
6153 LOGINT(set->ctx);
6154 break;
6155 }
6156
6157 /* matching node */
6158 set_insert_node(&result, iter, 0, iter_type, result.used);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006159 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006160 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006161
Michal Vasko230cf972021-12-02 12:31:00 +01006162 /* move result to the set */
6163 lyxp_set_free_content(set);
6164 *set = result;
6165 result.type = LYXP_SET_NUMBER;
Michal Vasko49fec8e2022-05-24 10:28:33 +02006166
Michal Vasko306e2832022-07-25 09:15:17 +02006167 /* sort the final set if the document order could have been broken */
6168 if (set->non_child_axis) {
6169 set_sort(set);
6170 } else {
6171 assert(!set_sort(set));
6172 }
Michal Vasko230cf972021-12-02 12:31:00 +01006173
6174cleanup:
6175 lyxp_set_free_content(&result);
6176 return rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006177}
6178
6179/**
Michal Vasko49fec8e2022-05-24 10:28:33 +02006180 * @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 +02006181 *
6182 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02006183 * @param[in] scnode Matching node schema.
Michal Vasko004d3152020-06-11 19:59:22 +02006184 * @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 +01006185 * @param[in] options XPath options.
Michal Vaskod3678892020-05-21 10:06:58 +02006186 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6187 */
6188static LY_ERR
Michal Vasko49fec8e2022-05-24 10:28:33 +02006189moveto_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 +01006190 uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02006191{
Michal Vaskoddd76592022-01-17 13:34:48 +01006192 LY_ERR ret = LY_SUCCESS, r;
Michal Vaskod3678892020-05-21 10:06:58 +02006193 uint32_t i;
Michal Vaskod3678892020-05-21 10:06:58 +02006194 const struct lyd_node *siblings;
Michal Vasko230cf972021-12-02 12:31:00 +01006195 struct lyxp_set result;
Michal Vasko004d3152020-06-11 19:59:22 +02006196 struct lyd_node *sub, *inst = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02006197
Michal Vasko004d3152020-06-11 19:59:22 +02006198 assert(scnode && (!(scnode->nodetype & (LYS_LIST | LYS_LEAFLIST)) || predicates));
Michal Vaskod3678892020-05-21 10:06:58 +02006199
Michal Vasko50aaa072021-12-02 13:11:56 +01006200 /* init result set */
6201 set_init(&result, set);
6202
aPiecek8b0cc152021-05-31 16:40:31 +02006203 if (options & LYXP_SKIP_EXPR) {
Michal Vasko004d3152020-06-11 19:59:22 +02006204 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02006205 }
6206
6207 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006208 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko004d3152020-06-11 19:59:22 +02006209 ret = LY_EVALID;
6210 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02006211 }
6212
6213 /* context check for all the nodes since we have the schema node */
6214 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) {
6215 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02006216 goto cleanup;
Michal Vasko69730152020-10-09 16:30:07 +02006217 } else if (set->context_op && (scnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) &&
6218 (scnode != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02006219 lyxp_set_free_content(set);
6220 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +02006221 }
6222
6223 /* create specific data instance if needed */
6224 if (scnode->nodetype == LYS_LIST) {
Michal Vasko90189962023-02-28 12:10:34 +01006225 LY_CHECK_GOTO(ret = lyd_create_list(scnode, predicates, NULL, &inst), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02006226 } else if (scnode->nodetype == LYS_LEAFLIST) {
6227 LY_CHECK_GOTO(ret = lyd_create_term2(scnode, &predicates[0].value, &inst), cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02006228 }
6229
Michal Vasko230cf972021-12-02 12:31:00 +01006230 for (i = 0; i < set->used; ++i) {
Michal Vaskod3678892020-05-21 10:06:58 +02006231 siblings = NULL;
6232
6233 if ((set->val.nodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.nodes[i].type == LYXP_NODE_ROOT)) {
6234 assert(!set->val.nodes[i].node);
6235
6236 /* search in all the trees */
6237 siblings = set->tree;
6238 } else if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
6239 /* search in children */
Radek Krejcia1c1e542020-09-29 16:06:52 +02006240 siblings = lyd_child(set->val.nodes[i].node);
Michal Vaskod3678892020-05-21 10:06:58 +02006241 }
6242
6243 /* find the node using hashes */
Michal Vasko004d3152020-06-11 19:59:22 +02006244 if (inst) {
Michal Vaskoddd76592022-01-17 13:34:48 +01006245 r = lyd_find_sibling_first(siblings, inst, &sub);
Michal Vaskod3678892020-05-21 10:06:58 +02006246 } else {
Michal Vaskoddd76592022-01-17 13:34:48 +01006247 r = lyd_find_sibling_val(siblings, scnode, NULL, 0, &sub);
Michal Vaskod3678892020-05-21 10:06:58 +02006248 }
Michal Vaskod8a52012023-08-15 11:38:10 +02006249 if (r == LY_ENOTFOUND) {
6250 /* may still be an opaque node */
6251 r = lyd_find_sibling_opaq_next(siblings, scnode->name, &sub);
6252 }
Michal Vaskoddd76592022-01-17 13:34:48 +01006253 LY_CHECK_ERR_GOTO(r && (r != LY_ENOTFOUND), ret = r, cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02006254
6255 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01006256 if (!(options & LYXP_IGNORE_WHEN) && sub && lysc_has_when(sub->schema) && !(sub->flags & LYD_WHEN_TRUE)) {
Michal Vasko004d3152020-06-11 19:59:22 +02006257 ret = LY_EINCOMPLETE;
6258 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02006259 }
6260
6261 if (sub) {
6262 /* pos filled later */
Michal Vasko230cf972021-12-02 12:31:00 +01006263 set_insert_node(&result, sub, 0, LYXP_NODE_ELEM, result.used);
Michal Vaskod3678892020-05-21 10:06:58 +02006264 }
6265 }
6266
Michal Vasko230cf972021-12-02 12:31:00 +01006267 /* move result to the set */
6268 lyxp_set_free_content(set);
6269 *set = result;
6270 result.type = LYXP_SET_NUMBER;
6271 assert(!set_sort(set));
6272
Michal Vasko004d3152020-06-11 19:59:22 +02006273cleanup:
Michal Vasko230cf972021-12-02 12:31:00 +01006274 lyxp_set_free_content(&result);
Michal Vasko004d3152020-06-11 19:59:22 +02006275 lyd_free_tree(inst);
6276 return ret;
Michal Vaskod3678892020-05-21 10:06:58 +02006277}
6278
6279/**
Michal Vasko49fec8e2022-05-24 10:28:33 +02006280 * @brief Check @p node as a part of schema NameTest processing.
6281 *
6282 * @param[in] node Schema node to check.
6283 * @param[in] ctx_scnode Context node.
6284 * @param[in] set Set to read general context from.
6285 * @param[in] node_name Node name in the dictionary to move to, NULL for any nodes.
6286 * @param[in] moveto_mod Expected module of the node, NULL for no prefix.
6287 * @return LY_ERR (LY_ENOT if node does not match, LY_EINVAL if neither node nor any children match)
6288 */
6289static LY_ERR
6290moveto_scnode_check(const struct lysc_node *node, const struct lysc_node *ctx_scnode, const struct lyxp_set *set,
6291 const char *node_name, const struct lys_module *moveto_mod)
6292{
6293 if (!moveto_mod && node_name) {
6294 switch (set->format) {
6295 case LY_VALUE_SCHEMA:
6296 case LY_VALUE_SCHEMA_RESOLVED:
6297 /* use current module */
6298 moveto_mod = set->cur_mod;
6299 break;
6300 case LY_VALUE_JSON:
6301 case LY_VALUE_LYB:
6302 case LY_VALUE_STR_NS:
6303 /* inherit module of the context node, if any */
6304 if (ctx_scnode) {
6305 moveto_mod = ctx_scnode->module;
6306 }
6307 break;
6308 case LY_VALUE_CANON:
6309 case LY_VALUE_XML:
6310 /* not defined */
6311 LOGINT(set->ctx);
6312 return LY_EINVAL;
6313 }
6314 }
6315
6316 if (!node) {
6317 /* root will not match a specific node */
6318 if (node_name || moveto_mod) {
6319 return LY_ENOT;
6320 }
6321 return LY_SUCCESS;
6322 }
6323
6324 /* module check */
6325 if (moveto_mod && (node->module != moveto_mod)) {
6326 return LY_ENOT;
6327 }
6328
6329 /* context check */
6330 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (node->flags & LYS_CONFIG_R)) {
6331 return LY_EINVAL;
6332 } else if (set->context_op && (node->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && (node != set->context_op)) {
6333 return LY_EINVAL;
6334 }
6335
6336 /* name check */
6337 if (node_name && (node->name != node_name)) {
6338 return LY_ENOT;
6339 }
6340
6341 /* match */
6342 return LY_SUCCESS;
6343}
6344
6345/**
6346 * @brief Get the next node in a forward schema node DFS.
6347 *
6348 * @param[in] iter Last returned node.
6349 * @param[in] stop Node to stop the search on and not return.
6350 * @param[in] getnext_opts Options for ::lys_getnext().
6351 * @return Next node, NULL if there are no more.
6352 */
6353static const struct lysc_node *
6354moveto_axis_scnode_next_dfs_forward(const struct lysc_node *iter, const struct lysc_node *stop, uint32_t getnext_opts)
6355{
6356 const struct lysc_node *next = NULL;
6357
6358 next = lysc_node_child(iter);
6359 if (!next) {
6360 /* no children, try siblings */
Michal Vasko34a22fe2022-06-15 07:58:55 +02006361 if ((iter == stop) || !lysc_data_parent(iter)) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02006362 /* we are done, no next element to process */
6363 return NULL;
6364 }
6365
6366 next = lys_getnext(iter, lysc_data_parent(iter), NULL, getnext_opts);
6367 }
6368 while (!next && iter) {
6369 /* parent is already processed, go to its sibling */
6370 iter = iter->parent;
Michal Vasko34a22fe2022-06-15 07:58:55 +02006371 if ((iter == stop) || !lysc_data_parent(iter)) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02006372 /* we are done, no next element to process */
6373 return NULL;
6374 }
6375 next = lys_getnext(iter, lysc_data_parent(iter), NULL, getnext_opts);
6376 }
6377
6378 return next;
6379}
6380
6381/**
6382 * @brief Consider schema node based on its in_ctx enum value.
6383 *
6384 * @param[in,out] in_ctx In_ctx enum of the schema node, may be updated.
6385 * @param[in] axis Axis to use.
6386 * @return LY_SUCCESS on success.
6387 * @return LY_ENOT if the node should not be returned.
6388 */
6389static LY_ERR
6390moveto_axis_scnode_next_in_ctx(int32_t *in_ctx, enum lyxp_axis axis)
6391{
6392 switch (axis) {
6393 case LYXP_AXIS_SELF:
6394 if ((*in_ctx == LYXP_SET_SCNODE_START) || (*in_ctx == LYXP_SET_SCNODE_ATOM_CTX)) {
6395 /* additionally put the start node into context */
6396 *in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
6397 return LY_SUCCESS;
6398 }
6399 break;
6400 case LYXP_AXIS_PARENT:
6401 case LYXP_AXIS_ANCESTOR_OR_SELF:
6402 case LYXP_AXIS_ANCESTOR:
6403 case LYXP_AXIS_DESCENDANT_OR_SELF:
6404 case LYXP_AXIS_DESCENDANT:
6405 case LYXP_AXIS_FOLLOWING:
6406 case LYXP_AXIS_FOLLOWING_SIBLING:
6407 case LYXP_AXIS_PRECEDING:
6408 case LYXP_AXIS_PRECEDING_SIBLING:
6409 case LYXP_AXIS_CHILD:
6410 if (*in_ctx == LYXP_SET_SCNODE_START) {
6411 /* remember that context node was used */
6412 *in_ctx = LYXP_SET_SCNODE_START_USED;
6413 return LY_SUCCESS;
6414 } else if (*in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
6415 /* traversed */
6416 *in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
6417 return LY_SUCCESS;
6418 }
6419 break;
6420 case LYXP_AXIS_ATTRIBUTE:
6421 /* unreachable */
6422 assert(0);
6423 LOGINT(NULL);
6424 break;
6425 }
6426
6427 return LY_ENOT;
6428}
6429
6430/**
6431 * @brief Get previous sibling for a schema node.
6432 *
6433 * @param[in] scnode Schema node.
6434 * @param[in] getnext_opts Options for ::lys_getnext().
6435 * @return Previous sibling, NULL if none.
6436 */
6437static const struct lysc_node *
6438moveto_axis_scnode_preceding_sibling(const struct lysc_node *scnode, uint32_t getnext_opts)
6439{
6440 const struct lysc_node *next = NULL, *prev = NULL;
6441
6442 while ((next = lys_getnext(next, lysc_data_parent(scnode), scnode->module->compiled, getnext_opts))) {
6443 if (next == scnode) {
6444 break;
6445 }
6446
6447 prev = next;
6448 }
6449
6450 return prev;
6451}
6452
6453/**
6454 * @brief Get the first schema node on an axis for a context node.
6455 *
6456 * @param[in,out] iter Last returned node, start with NULL, updated to the next node.
6457 * @param[in,out] iter_type Node type of @p iter, start with 0, updated to the node type of the next node.
6458 * @param[in,out] iter_mod Internal module iterator, do not change.
6459 * @param[in,out] iter_mod_idx Internal module index iterator, do not change.
6460 * @param[in] scnode Context node.
6461 * @param[in] node_type Type of @p scnode.
6462 * @param[in] in_ctx In_ctx enum of @p scnode.
6463 * @param[in] axis Axis to use.
6464 * @param[in] set XPath set with the general context.
6465 * @param[in] getnext_opts Options for ::lys_getnext().
6466 * @return LY_SUCCESS on success.
6467 * @return LY_ENOTFOUND if no next node found.
6468 */
6469static LY_ERR
6470moveto_axis_scnode_next_first(const struct lysc_node **iter, enum lyxp_node_type *iter_type, const struct lys_module **iter_mod,
6471 uint32_t *iter_mod_idx, const struct lysc_node *scnode, enum lyxp_node_type node_type, enum lyxp_axis axis,
6472 struct lyxp_set *set, uint32_t getnext_opts)
6473{
6474 const struct lysc_node *next = NULL;
6475 enum lyxp_node_type next_type = 0;
6476
6477 assert(!*iter);
6478 assert(!*iter_type);
6479
6480 *iter_mod = NULL;
6481 *iter_mod_idx = 0;
6482
6483 switch (axis) {
6484 case LYXP_AXIS_ANCESTOR_OR_SELF:
6485 case LYXP_AXIS_DESCENDANT_OR_SELF:
6486 case LYXP_AXIS_SELF:
6487 if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT) || (node_type == LYXP_NODE_ELEM)) {
6488 /* just return the node */
6489 next = scnode;
6490 next_type = node_type;
6491 }
6492 break;
6493
6494 case LYXP_AXIS_ANCESTOR:
6495 case LYXP_AXIS_PARENT:
6496 if (node_type == LYXP_NODE_ELEM) {
6497 next = lysc_data_parent(scnode);
6498 next_type = next ? LYXP_NODE_ELEM : set->root_type;
6499 } /* else no parent */
6500 break;
6501
6502 case LYXP_AXIS_DESCENDANT:
6503 case LYXP_AXIS_CHILD:
6504 if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT)) {
6505 /* it can actually be in any module, it's all <running>, and even if it's moveto_mod (if set),
6506 * it can be in a top-level augment */
6507 while ((*iter_mod = ly_ctx_get_module_iter(set->ctx, iter_mod_idx))) {
6508 /* module may not be implemented or not compiled yet */
6509 if (!(*iter_mod)->compiled) {
6510 continue;
6511 }
6512
6513 /* get next node */
6514 if ((next = lys_getnext(NULL, NULL, (*iter_mod)->compiled, getnext_opts))) {
6515 next_type = LYXP_NODE_ELEM;
6516 break;
6517 }
6518 }
6519 } else if (node_type == LYXP_NODE_ELEM) {
6520 /* get next node */
6521 next = lys_getnext(NULL, scnode, NULL, getnext_opts);
6522 next_type = next ? LYXP_NODE_ELEM : 0;
6523 }
6524 break;
6525
6526 case LYXP_AXIS_FOLLOWING:
6527 case LYXP_AXIS_FOLLOWING_SIBLING:
6528 if (node_type == LYXP_NODE_ELEM) {
6529 /* first next sibling */
6530 next = lys_getnext(scnode, lysc_data_parent(scnode), scnode->module->compiled, getnext_opts);
6531 next_type = next ? LYXP_NODE_ELEM : 0;
6532 } /* else no sibling */
6533 break;
6534
6535 case LYXP_AXIS_PRECEDING:
6536 case LYXP_AXIS_PRECEDING_SIBLING:
6537 if (node_type == LYXP_NODE_ELEM) {
6538 /* first parent sibling */
6539 next = lys_getnext(NULL, lysc_data_parent(scnode), scnode->module->compiled, getnext_opts);
6540 if (next == scnode) {
6541 /* no preceding sibling */
6542 next = NULL;
6543 }
6544 next_type = next ? LYXP_NODE_ELEM : 0;
6545 } /* else no sibling */
6546 break;
6547
6548 case LYXP_AXIS_ATTRIBUTE:
6549 /* unreachable */
6550 assert(0);
6551 LOGINT(set->ctx);
6552 break;
6553 }
6554
6555 *iter = next;
6556 *iter_type = next_type;
6557 return next_type ? LY_SUCCESS : LY_ENOTFOUND;
6558}
6559
6560/**
6561 * @brief Iterate over all schema nodes on an axis for a context node.
6562 *
6563 * @param[in,out] iter Last returned node, start with NULL, updated to the next node.
6564 * @param[in,out] iter_type Node type of @p iter, start with 0, updated to the node type of the next node.
6565 * @param[in,out] iter_mod Internal module iterator, do not change.
6566 * @param[in,out] iter_mod_idx Internal module index iterator, do not change.
6567 * @param[in] scnode Context node.
6568 * @param[in] node_type Type of @p scnode.
6569 * @param[in] axis Axis to use.
6570 * @param[in] set XPath set with the general context.
6571 * @param[in] getnext_opts Options for ::lys_getnext().
6572 * @return LY_SUCCESS on success.
6573 * @return LY_ENOTFOUND if no next node found.
6574 */
6575static LY_ERR
6576moveto_axis_scnode_next(const struct lysc_node **iter, enum lyxp_node_type *iter_type, const struct lys_module **iter_mod,
6577 uint32_t *iter_mod_idx, const struct lysc_node *scnode, enum lyxp_node_type node_type, enum lyxp_axis axis,
6578 struct lyxp_set *set, uint32_t getnext_opts)
6579{
6580 const struct lysc_node *next = NULL, *dfs_stop;
6581 enum lyxp_node_type next_type = 0;
6582
6583 if (!*iter_type) {
6584 /* first returned node */
6585 return moveto_axis_scnode_next_first(iter, iter_type, iter_mod, iter_mod_idx, scnode, node_type, axis, set,
6586 getnext_opts);
6587 }
6588
6589 switch (axis) {
6590 case LYXP_AXIS_PARENT:
6591 case LYXP_AXIS_SELF:
6592 /* parent/self was returned before */
6593 break;
6594
6595 case LYXP_AXIS_ANCESTOR_OR_SELF:
6596 if ((*iter == scnode) && (*iter_type == node_type)) {
6597 /* fake first ancestor, we returned self before */
6598 *iter = NULL;
6599 *iter_type = 0;
6600 return moveto_axis_scnode_next_first(iter, iter_type, iter_mod, iter_mod_idx, scnode, node_type,
6601 LYXP_AXIS_ANCESTOR, set, getnext_opts);
6602 } /* else continue ancestor */
6603
6604 /* fallthrough */
6605 case LYXP_AXIS_ANCESTOR:
6606 if (*iter_type == LYXP_NODE_ELEM) {
6607 next = lysc_data_parent(*iter);
6608 next_type = next ? LYXP_NODE_ELEM : set->root_type;
6609 } /* else no ancestor */
6610 break;
6611
6612 case LYXP_AXIS_DESCENDANT_OR_SELF:
6613 if ((*iter == scnode) && (*iter_type == node_type)) {
6614 /* fake first descendant, we returned self before */
6615 *iter = NULL;
6616 *iter_type = 0;
6617 return moveto_axis_scnode_next_first(iter, iter_type, iter_mod, iter_mod_idx, scnode, node_type,
6618 LYXP_AXIS_DESCENDANT, set, getnext_opts);
6619 } /* else DFS until context node */
6620 dfs_stop = scnode;
6621
6622 /* fallthrough */
6623 case LYXP_AXIS_DESCENDANT:
6624 if (axis == LYXP_AXIS_DESCENDANT) {
6625 /* DFS until the context node */
6626 dfs_stop = scnode;
6627 }
6628
6629 /* fallthrough */
6630 case LYXP_AXIS_PRECEDING:
6631 if (axis == LYXP_AXIS_PRECEDING) {
6632 /* DFS until the previous sibling */
6633 dfs_stop = moveto_axis_scnode_preceding_sibling(scnode, getnext_opts);
6634 assert(dfs_stop);
6635
6636 if (*iter == dfs_stop) {
6637 /* we are done */
6638 break;
6639 }
6640 }
6641
6642 /* fallthrough */
6643 case LYXP_AXIS_FOLLOWING:
6644 if (axis == LYXP_AXIS_FOLLOWING) {
6645 /* DFS through the whole module */
6646 dfs_stop = NULL;
6647 }
6648
6649 /* nested nodes */
6650 assert(*iter);
6651 next = moveto_axis_scnode_next_dfs_forward(*iter, dfs_stop, getnext_opts);
6652 if (next) {
6653 next_type = LYXP_NODE_ELEM;
6654 break;
6655 } /* else get next top-level node just like a child */
6656
6657 /* fallthrough */
6658 case LYXP_AXIS_CHILD:
6659 case LYXP_AXIS_FOLLOWING_SIBLING:
6660 if (!*iter_mod) {
6661 /* nodes from a single module */
6662 if ((next = lys_getnext(*iter, lysc_data_parent(*iter), (*iter)->module->compiled, getnext_opts))) {
6663 next_type = LYXP_NODE_ELEM;
6664 break;
6665 }
6666
6667 assert(scnode);
Michal Vaskoa353cce2022-11-14 10:09:55 +01006668 if ((axis != LYXP_AXIS_CHILD) && !lysc_data_parent(scnode)) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02006669 /* iterating over top-level nodes, find next */
6670 while (lysc_data_parent(*iter)) {
6671 *iter = lysc_data_parent(*iter);
6672 }
6673 if ((next = lys_getnext(*iter, NULL, (*iter)->module->compiled, getnext_opts))) {
6674 next_type = LYXP_NODE_ELEM;
6675 break;
6676 }
6677 }
6678 }
6679
6680 while (*iter_mod) {
6681 /* module top-level nodes */
6682 if ((next = lys_getnext(*iter, NULL, (*iter_mod)->compiled, getnext_opts))) {
6683 next_type = LYXP_NODE_ELEM;
6684 break;
6685 }
6686
6687 /* get next module */
6688 while ((*iter_mod = ly_ctx_get_module_iter(set->ctx, iter_mod_idx))) {
6689 /* module may not be implemented or not compiled yet */
6690 if ((*iter_mod)->compiled) {
6691 break;
6692 }
6693 }
6694
6695 /* new module, start over */
6696 *iter = NULL;
6697 }
6698 break;
6699
6700 case LYXP_AXIS_PRECEDING_SIBLING:
6701 assert(*iter);
6702
6703 /* next parent sibling until scnode */
6704 next = lys_getnext(*iter, lysc_data_parent(*iter), (*iter)->module->compiled, getnext_opts);
6705 if (next == scnode) {
6706 /* no previous sibling */
6707 next = NULL;
6708 }
6709 next_type = next ? LYXP_NODE_ELEM : 0;
6710 break;
6711
6712 case LYXP_AXIS_ATTRIBUTE:
6713 /* unreachable */
6714 assert(0);
6715 LOGINT(set->ctx);
6716 break;
6717 }
6718
6719 *iter = next;
6720 *iter_type = next_type;
6721 return next_type ? LY_SUCCESS : LY_ENOTFOUND;
6722}
6723
6724/**
Michal Vaskod3678892020-05-21 10:06:58 +02006725 * @brief Move context @p set to a schema node. Result is LYXP_SET_SCNODE_SET (or LYXP_SET_EMPTY).
6726 *
6727 * @param[in,out] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006728 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02006729 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko49fec8e2022-05-24 10:28:33 +02006730 * @param[in] axis Axis to search on.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006731 * @param[in] options XPath options.
6732 * @return LY_ERR
6733 */
6734static LY_ERR
Michal Vasko49fec8e2022-05-24 10:28:33 +02006735moveto_scnode(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, enum lyxp_axis axis,
6736 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006737{
Radek Krejci857189e2020-09-01 13:26:36 +02006738 ly_bool temp_ctx = 0;
Michal Vasko49fec8e2022-05-24 10:28:33 +02006739 uint32_t getnext_opts, orig_used, i, mod_idx, idx;
stewegd8e2fc92023-05-31 09:52:56 +02006740 const struct lys_module *mod = NULL;
Michal Vasko49fec8e2022-05-24 10:28:33 +02006741 const struct lysc_node *iter;
6742 enum lyxp_node_type iter_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006743
aPiecek8b0cc152021-05-31 16:40:31 +02006744 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006745 return LY_SUCCESS;
6746 }
6747
6748 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006749 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006750 return LY_EVALID;
6751 }
6752
Michal Vaskocafad9d2019-11-07 15:20:03 +01006753 /* getnext opts */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01006754 getnext_opts = 0;
Michal Vaskocafad9d2019-11-07 15:20:03 +01006755 if (options & LYXP_SCNODE_OUTPUT) {
6756 getnext_opts |= LYS_GETNEXT_OUTPUT;
6757 }
Michal Vaskoe482bb92023-02-01 11:41:41 +01006758 if (options & LYXP_SCNODE_SCHEMAMOUNT) {
6759 getnext_opts |= LYS_GETNEXT_WITHSCHEMAMOUNT;
6760 }
Michal Vaskocafad9d2019-11-07 15:20:03 +01006761
Michal Vasko03ff5a72019-09-11 13:49:33 +02006762 orig_used = set->used;
6763 for (i = 0; i < orig_used; ++i) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02006764 /* update in_ctx first */
6765 if (moveto_axis_scnode_next_in_ctx(&set->val.scnodes[i].in_ctx, axis)) {
6766 /* not usable, skip */
6767 continue;
6768 }
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006769
Michal Vasko49fec8e2022-05-24 10:28:33 +02006770 iter = NULL;
6771 iter_type = 0;
6772 while (!moveto_axis_scnode_next(&iter, &iter_type, &mod, &mod_idx, set->val.scnodes[i].scnode,
6773 set->val.scnodes[i].type, axis, set, getnext_opts)) {
6774 if (moveto_scnode_check(iter, NULL, set, ncname, moveto_mod)) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006775 continue;
6776 }
6777
Michal Vasko49fec8e2022-05-24 10:28:33 +02006778 /* insert */
Michal Vaskoe4a6d012023-05-22 14:34:52 +02006779 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, iter_type, axis, &idx));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006780
Michal Vasko49fec8e2022-05-24 10:28:33 +02006781 /* we need to prevent these nodes from being considered in this moveto */
6782 if ((idx < orig_used) && (idx > i)) {
6783 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX;
6784 temp_ctx = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006785 }
6786 }
Michal Vasko66330fc2022-11-21 15:52:24 +01006787
6788 if (moveto_mod && ncname && ((axis == LYXP_AXIS_DESCENDANT) || (axis == LYXP_AXIS_CHILD)) &&
6789 (set->val.scnodes[i].type == LYXP_NODE_ELEM) && !ly_nested_ext_schema(NULL, set->val.scnodes[i].scnode,
6790 moveto_mod->name, strlen(moveto_mod->name), LY_VALUE_JSON, NULL, ncname, strlen(ncname), &iter, NULL)) {
6791 /* there is a matching node from an extension, use it */
Michal Vaskoe4a6d012023-05-22 14:34:52 +02006792 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, axis, &idx));
Michal Vasko66330fc2022-11-21 15:52:24 +01006793 if ((idx < orig_used) && (idx > i)) {
6794 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX;
6795 temp_ctx = 1;
6796 }
6797 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006798 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006799
6800 /* correct temporary in_ctx values */
6801 if (temp_ctx) {
6802 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006803 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_NEW_CTX) {
6804 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006805 }
6806 }
6807 }
6808
6809 return LY_SUCCESS;
6810}
6811
6812/**
Michal Vasko49fec8e2022-05-24 10:28:33 +02006813 * @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 +02006814 * Context position aware.
6815 *
6816 * @param[in] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006817 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02006818 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vaskocdad7122020-11-09 21:04:44 +01006819 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006820 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6821 */
6822static LY_ERR
Michal Vasko49fec8e2022-05-24 10:28:33 +02006823moveto_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 +02006824{
6825 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006826 const struct lyd_node *next, *elem, *start;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006827 struct lyxp_set ret_set;
6828 LY_ERR rc;
6829
aPiecek8b0cc152021-05-31 16:40:31 +02006830 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006831 return LY_SUCCESS;
6832 }
6833
6834 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006835 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006836 return LY_EVALID;
6837 }
6838
Michal Vasko9f96a052020-03-10 09:41:45 +01006839 /* 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 +02006840 rc = xpath_pi_node(set, LYXP_AXIS_CHILD, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006841 LY_CHECK_RET(rc);
6842
Michal Vasko6346ece2019-09-24 13:12:53 +02006843 /* this loop traverses all the nodes in the set and adds/keeps only those that match qname */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006844 set_init(&ret_set, set);
6845 for (i = 0; i < set->used; ++i) {
6846
6847 /* TREE DFS */
6848 start = set->val.nodes[i].node;
6849 for (elem = next = start; elem; elem = next) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02006850 rc = moveto_node_check(elem, LYXP_NODE_ELEM, set, ncname, moveto_mod, options);
Michal Vasko6346ece2019-09-24 13:12:53 +02006851 if (!rc) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006852 /* add matching node into result set */
6853 set_insert_node(&ret_set, elem, 0, LYXP_NODE_ELEM, ret_set.used);
6854 if (set_dup_node_check(set, elem, LYXP_NODE_ELEM, i)) {
6855 /* the node is a duplicate, we'll process it later in the set */
6856 goto skip_children;
6857 }
Michal Vasko6346ece2019-09-24 13:12:53 +02006858 } else if (rc == LY_EINCOMPLETE) {
Michal Vasko6346ece2019-09-24 13:12:53 +02006859 return rc;
6860 } else if (rc == LY_EINVAL) {
6861 goto skip_children;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006862 }
6863
6864 /* TREE DFS NEXT ELEM */
6865 /* select element for the next run - children first */
Radek Krejcia1c1e542020-09-29 16:06:52 +02006866 next = lyd_child(elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006867 if (!next) {
6868skip_children:
6869 /* no children, so try siblings, but only if it's not the start,
6870 * that is considered to be the root and it's siblings are not traversed */
6871 if (elem != start) {
6872 next = elem->next;
6873 } else {
6874 break;
6875 }
6876 }
6877 while (!next) {
6878 /* no siblings, go back through the parents */
Michal Vasko9e685082021-01-29 14:49:09 +01006879 if (lyd_parent(elem) == start) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006880 /* we are done, no next element to process */
6881 break;
6882 }
6883 /* parent is already processed, go to its sibling */
Michal Vasko9e685082021-01-29 14:49:09 +01006884 elem = lyd_parent(elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006885 next = elem->next;
6886 }
6887 }
6888 }
6889
6890 /* make the temporary set the current one */
6891 ret_set.ctx_pos = set->ctx_pos;
6892 ret_set.ctx_size = set->ctx_size;
Michal Vaskod3678892020-05-21 10:06:58 +02006893 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006894 memcpy(set, &ret_set, sizeof *set);
Michal Vasko306e2832022-07-25 09:15:17 +02006895 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006896
6897 return LY_SUCCESS;
6898}
6899
6900/**
Michal Vasko49fec8e2022-05-24 10:28:33 +02006901 * @brief Move context @p set to a child schema node and all its descendants starting from a node.
6902 * Result is LYXP_SET_NODE_SET.
6903 *
6904 * @param[in] set Set to use.
6905 * @param[in] start Start node whose subtree to add.
6906 * @param[in] start_idx Index of @p start in @p set.
6907 * @param[in] moveto_mod Matching node module, NULL for no prefix.
6908 * @param[in] ncname Matching node name in the dictionary, NULL for any.
6909 * @param[in] options XPath options.
6910 * @return LY_ERR value.
6911 */
6912static LY_ERR
6913moveto_scnode_dfs(struct lyxp_set *set, const struct lysc_node *start, uint32_t start_idx,
6914 const struct lys_module *moveto_mod, const char *ncname, uint32_t options)
6915{
6916 const struct lysc_node *next, *elem;
6917 uint32_t idx;
6918 LY_ERR rc;
6919
6920 /* TREE DFS */
6921 for (elem = next = start; elem; elem = next) {
6922 if ((elem == start) || (elem->nodetype & (LYS_CHOICE | LYS_CASE))) {
6923 /* schema-only nodes, skip root */
6924 goto next_iter;
6925 }
6926
6927 rc = moveto_scnode_check(elem, start, set, ncname, moveto_mod);
6928 if (!rc) {
6929 if (lyxp_set_scnode_contains(set, elem, LYXP_NODE_ELEM, start_idx, &idx)) {
6930 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
6931 if (idx > start_idx) {
6932 /* we will process it later in the set */
6933 goto skip_children;
6934 }
6935 } else {
Michal Vaskoe4a6d012023-05-22 14:34:52 +02006936 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, elem, LYXP_NODE_ELEM, LYXP_AXIS_DESCENDANT, NULL));
Michal Vasko49fec8e2022-05-24 10:28:33 +02006937 }
6938 } else if (rc == LY_EINVAL) {
6939 goto skip_children;
6940 }
6941
6942next_iter:
6943 /* TREE DFS NEXT ELEM */
6944 /* select element for the next run - children first */
6945 next = lysc_node_child(elem);
6946 if (next && (next->nodetype == LYS_INPUT) && (options & LYXP_SCNODE_OUTPUT)) {
6947 next = next->next;
6948 } else if (next && (next->nodetype == LYS_OUTPUT) && !(options & LYXP_SCNODE_OUTPUT)) {
6949 next = next->next;
6950 }
6951 if (!next) {
6952skip_children:
6953 /* no children, so try siblings, but only if it's not the start,
6954 * that is considered to be the root and it's siblings are not traversed */
6955 if (elem != start) {
6956 next = elem->next;
6957 } else {
6958 break;
6959 }
6960 }
6961 while (!next) {
6962 /* no siblings, go back through the parents */
6963 if (elem->parent == start) {
6964 /* we are done, no next element to process */
6965 break;
6966 }
6967 /* parent is already processed, go to its sibling */
6968 elem = elem->parent;
6969 next = elem->next;
6970 }
6971 }
6972
6973 return LY_SUCCESS;
6974}
6975
6976/**
6977 * @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 +02006978 *
6979 * @param[in] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006980 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02006981 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006982 * @param[in] options XPath options.
Michal Vasko49fec8e2022-05-24 10:28:33 +02006983 * @return LY_ERR value.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006984 */
6985static LY_ERR
Michal Vasko49fec8e2022-05-24 10:28:33 +02006986moveto_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 +02006987{
Michal Vasko49fec8e2022-05-24 10:28:33 +02006988 uint32_t i, orig_used, mod_idx;
6989 const struct lys_module *mod;
6990 const struct lysc_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006991
aPiecek8b0cc152021-05-31 16:40:31 +02006992 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006993 return LY_SUCCESS;
6994 }
6995
6996 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006997 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006998 return LY_EVALID;
6999 }
7000
Michal Vasko03ff5a72019-09-11 13:49:33 +02007001 orig_used = set->used;
7002 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01007003 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
7004 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01007005 continue;
7006 }
7007
7008 /* remember context node */
Radek Krejcif13b87b2020-12-01 22:02:17 +01007009 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vaskoec4df482019-12-16 10:02:18 +01007010 } else {
Michal Vasko1a09b212021-05-06 13:00:10 +02007011 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007012 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02007013
Michal Vasko49fec8e2022-05-24 10:28:33 +02007014 if ((set->val.scnodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.scnodes[i].type == LYXP_NODE_ROOT)) {
7015 /* traverse all top-level nodes in all the modules */
7016 mod_idx = 0;
7017 while ((mod = ly_ctx_get_module_iter(set->ctx, &mod_idx))) {
7018 /* module may not be implemented or not compiled yet */
7019 if (!mod->compiled) {
7020 continue;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007021 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02007022
Michal Vasko49fec8e2022-05-24 10:28:33 +02007023 root = NULL;
7024 /* no getnext opts needed */
7025 while ((root = lys_getnext(root, NULL, mod->compiled, 0))) {
7026 LY_CHECK_RET(moveto_scnode_dfs(set, root, i, moveto_mod, ncname, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007027 }
7028 }
Michal Vasko49fec8e2022-05-24 10:28:33 +02007029
7030 } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
7031 /* add all the descendants recursively */
7032 LY_CHECK_RET(moveto_scnode_dfs(set, set->val.scnodes[i].scnode, i, moveto_mod, ncname, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007033 }
7034 }
7035
7036 return LY_SUCCESS;
7037}
7038
7039/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02007040 * @brief Move context @p set to an attribute. Result is LYXP_SET_NODE_SET.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007041 * Indirectly context position aware.
7042 *
7043 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02007044 * @param[in] mod Matching metadata module, NULL for any.
7045 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
aPiecek8b0cc152021-05-31 16:40:31 +02007046 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007047 * @return LY_ERR
7048 */
7049static LY_ERR
aPiecek8b0cc152021-05-31 16:40:31 +02007050moveto_attr(struct lyxp_set *set, const struct lys_module *mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007051{
Michal Vasko9f96a052020-03-10 09:41:45 +01007052 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007053
aPiecek8b0cc152021-05-31 16:40:31 +02007054 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007055 return LY_SUCCESS;
7056 }
7057
7058 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01007059 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007060 return LY_EVALID;
7061 }
7062
Radek Krejci1deb5be2020-08-26 16:43:36 +02007063 for (uint32_t i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02007064 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007065
7066 /* only attributes of an elem (not dummy) can be in the result, skip all the rest;
7067 * our attributes are always qualified */
Michal Vasko5c4e5892019-11-14 12:31:38 +01007068 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01007069 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007070
7071 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02007072 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007073 continue;
7074 }
7075
Michal Vaskod3678892020-05-21 10:06:58 +02007076 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007077 /* match */
7078 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01007079 set->val.meta[i].meta = sub;
7080 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007081 /* pos does not change */
7082 replaced = 1;
7083 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01007084 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 +02007085 }
7086 ++i;
7087 }
7088 }
7089 }
7090
7091 if (!replaced) {
7092 /* no match */
7093 set_remove_node(set, i);
7094 }
7095 }
7096
7097 return LY_SUCCESS;
7098}
7099
7100/**
7101 * @brief Move context @p set1 to union with @p set2. @p set2 is emptied afterwards.
Michal Vasko61ac2f62020-05-25 12:39:51 +02007102 * Result is LYXP_SET_NODE_SET. Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007103 *
7104 * @param[in,out] set1 Set to use for the result.
7105 * @param[in] set2 Set that is copied to @p set1.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007106 * @return LY_ERR
7107 */
7108static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007109moveto_union(struct lyxp_set *set1, struct lyxp_set *set2)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007110{
7111 LY_ERR rc;
7112
Michal Vaskod3678892020-05-21 10:06:58 +02007113 if ((set1->type != LYXP_SET_NODE_SET) || (set2->type != LYXP_SET_NODE_SET)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01007114 LOGVAL(set1->ctx, LY_VCODE_XP_INOP_2, "union", print_set_type(set1), print_set_type(set2));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007115 return LY_EVALID;
7116 }
7117
7118 /* set2 is empty or both set1 and set2 */
Michal Vaskod3678892020-05-21 10:06:58 +02007119 if (!set2->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007120 return LY_SUCCESS;
7121 }
7122
Michal Vaskod3678892020-05-21 10:06:58 +02007123 if (!set1->used) {
aPiecekadc1e4f2021-10-07 11:15:12 +02007124 /* release hidden allocated data (lyxp_set.size) */
7125 lyxp_set_free_content(set1);
7126 /* direct copying of the entire structure */
Michal Vasko03ff5a72019-09-11 13:49:33 +02007127 memcpy(set1, set2, sizeof *set1);
7128 /* dynamic memory belongs to set1 now, do not free */
Michal Vaskod3678892020-05-21 10:06:58 +02007129 memset(set2, 0, sizeof *set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007130 return LY_SUCCESS;
7131 }
7132
7133 /* we assume sets are sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007134 assert(!set_sort(set1) && !set_sort(set2));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007135
7136 /* sort, remove duplicates */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007137 rc = set_sorted_merge(set1, set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007138 LY_CHECK_RET(rc);
7139
7140 /* final set must be sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007141 assert(!set_sort(set1));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007142
7143 return LY_SUCCESS;
7144}
7145
7146/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02007147 * @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 +02007148 * Context position aware.
7149 *
7150 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02007151 * @param[in] mod Matching metadata module, NULL for any.
7152 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
Michal Vaskocdad7122020-11-09 21:04:44 +01007153 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007154 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7155 */
7156static int
Michal Vaskocdad7122020-11-09 21:04:44 +01007157moveto_attr_alldesc(struct lyxp_set *set, const struct lys_module *mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007158{
Michal Vasko9f96a052020-03-10 09:41:45 +01007159 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007160 struct lyxp_set *set_all_desc = NULL;
7161 LY_ERR rc;
7162
aPiecek8b0cc152021-05-31 16:40:31 +02007163 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007164 return LY_SUCCESS;
7165 }
7166
7167 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01007168 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007169 return LY_EVALID;
7170 }
7171
Michal Vasko03ff5a72019-09-11 13:49:33 +02007172 /* can be optimized similarly to moveto_node_alldesc() and save considerable amount of memory,
7173 * but it likely won't be used much, so it's a waste of time */
7174 /* copy the context */
7175 set_all_desc = set_copy(set);
7176 /* get all descendant nodes (the original context nodes are removed) */
Michal Vasko49fec8e2022-05-24 10:28:33 +02007177 rc = moveto_node_alldesc_child(set_all_desc, NULL, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007178 if (rc != LY_SUCCESS) {
7179 lyxp_set_free(set_all_desc);
7180 return rc;
7181 }
7182 /* prepend the original context nodes */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007183 rc = moveto_union(set, set_all_desc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007184 if (rc != LY_SUCCESS) {
7185 lyxp_set_free(set_all_desc);
7186 return rc;
7187 }
7188 lyxp_set_free(set_all_desc);
7189
Radek Krejci1deb5be2020-08-26 16:43:36 +02007190 for (uint32_t i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02007191 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007192
7193 /* only attributes of an elem can be in the result, skip all the rest,
7194 * we have all attributes qualified in lyd tree */
7195 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01007196 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007197 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02007198 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007199 continue;
7200 }
7201
Michal Vaskod3678892020-05-21 10:06:58 +02007202 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007203 /* match */
7204 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01007205 set->val.meta[i].meta = sub;
7206 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007207 /* pos does not change */
7208 replaced = 1;
7209 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01007210 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 +02007211 }
7212 ++i;
7213 }
7214 }
7215 }
7216
7217 if (!replaced) {
7218 /* no match */
7219 set_remove_node(set, i);
7220 }
7221 }
7222
7223 return LY_SUCCESS;
7224}
7225
7226/**
Michal Vasko8abcecc2022-07-28 09:55:01 +02007227 * @brief Move context @p set1 single item to the result of a comparison.
7228 *
7229 * @param[in] set1 First set with the item to compare.
7230 * @param[in] idx1 Index of the item in @p set1.
7231 * @param[in] set2 Second set.
7232 * @param[in] op Comparison operator to process.
7233 * @param[in] switch_operands Whether to switch sets as operands; whether it is `set1 op set2` or `set2 op set1`.
7234 * @param[out] result Result of the comparison.
7235 * @return LY_ERR value.
7236 */
7237static LY_ERR
7238moveto_op_comp_item(const struct lyxp_set *set1, uint32_t idx1, struct lyxp_set *set2, const char *op,
7239 ly_bool switch_operands, ly_bool *result)
7240{
7241 struct lyxp_set tmp1 = {0};
7242 LY_ERR rc = LY_SUCCESS;
7243
7244 assert(set1->type == LYXP_SET_NODE_SET);
7245
7246 /* cast set1 */
7247 switch (set2->type) {
7248 case LYXP_SET_NUMBER:
7249 rc = set_comp_cast(&tmp1, set1, LYXP_SET_NUMBER, idx1);
7250 break;
7251 case LYXP_SET_BOOLEAN:
7252 rc = set_comp_cast(&tmp1, set1, LYXP_SET_BOOLEAN, idx1);
7253 break;
7254 default:
7255 rc = set_comp_cast(&tmp1, set1, LYXP_SET_STRING, idx1);
7256 break;
7257 }
7258 LY_CHECK_GOTO(rc, cleanup);
7259
7260 /* canonize set2 */
7261 LY_CHECK_GOTO(rc = set_comp_canonize(set2, &set1->val.nodes[idx1]), cleanup);
7262
7263 /* compare recursively and store the result */
7264 if (switch_operands) {
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007265 LY_CHECK_GOTO(rc = moveto_op_comp(set2, &tmp1, op, result), cleanup);
Michal Vasko8abcecc2022-07-28 09:55:01 +02007266 } else {
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007267 LY_CHECK_GOTO(rc = moveto_op_comp(&tmp1, set2, op, result), cleanup);
Michal Vasko8abcecc2022-07-28 09:55:01 +02007268 }
7269
7270cleanup:
7271 lyxp_set_free_content(&tmp1);
7272 return rc;
7273}
7274
7275/**
7276 * @brief Move context @p set1 to the result of a comparison. Handles '=', '!=', '<=', '<', '>=', or '>'.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007277 * Result is LYXP_SET_BOOLEAN. Indirectly context position aware.
7278 *
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007279 * @param[in] set1 Set acting as the first operand for @p op.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007280 * @param[in] set2 Set acting as the second operand for @p op.
7281 * @param[in] op Comparison operator to process.
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007282 * @param[out] result Result of the comparison.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007283 * @return LY_ERR
7284 */
7285static LY_ERR
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007286moveto_op_comp(struct lyxp_set *set1, struct lyxp_set *set2, const char *op, ly_bool *result)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007287{
7288 /*
7289 * NODE SET + NODE SET = NODE SET + STRING /(1 NODE SET) 2 STRING
7290 * NODE SET + STRING = STRING + STRING /1 STRING (2 STRING)
7291 * NODE SET + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
7292 * NODE SET + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
7293 * STRING + NODE SET = STRING + STRING /(1 STRING) 2 STRING
7294 * NUMBER + NODE SET = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
7295 * BOOLEAN + NODE SET = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
7296 *
7297 * '=' or '!='
7298 * BOOLEAN + BOOLEAN
7299 * BOOLEAN + STRING = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
7300 * BOOLEAN + NUMBER = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
7301 * STRING + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
7302 * NUMBER + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
7303 * NUMBER + NUMBER
7304 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
7305 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
7306 * STRING + STRING
7307 *
7308 * '<=', '<', '>=', '>'
7309 * NUMBER + NUMBER
7310 * BOOLEAN + BOOLEAN = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
7311 * BOOLEAN + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
7312 * BOOLEAN + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
7313 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
7314 * STRING + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
7315 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
7316 * NUMBER + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
7317 * STRING + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
7318 */
Michal Vasko8abcecc2022-07-28 09:55:01 +02007319 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007320 LY_ERR rc;
7321
Michal Vasko03ff5a72019-09-11 13:49:33 +02007322 /* iterative evaluation with node-sets */
7323 if ((set1->type == LYXP_SET_NODE_SET) || (set2->type == LYXP_SET_NODE_SET)) {
7324 if (set1->type == LYXP_SET_NODE_SET) {
7325 for (i = 0; i < set1->used; ++i) {
Michal Vasko8abcecc2022-07-28 09:55:01 +02007326 /* evaluate for the single item */
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007327 LY_CHECK_RET(moveto_op_comp_item(set1, i, set2, op, 0, result));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007328
7329 /* lazy evaluation until true */
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007330 if (*result) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007331 return LY_SUCCESS;
7332 }
7333 }
7334 } else {
7335 for (i = 0; i < set2->used; ++i) {
Michal Vasko8abcecc2022-07-28 09:55:01 +02007336 /* evaluate for the single item */
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007337 LY_CHECK_RET(moveto_op_comp_item(set2, i, set1, op, 1, result));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007338
7339 /* lazy evaluation until true */
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007340 if (*result) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007341 return LY_SUCCESS;
7342 }
7343 }
7344 }
7345
Michal Vasko8abcecc2022-07-28 09:55:01 +02007346 /* false for all the nodes */
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007347 *result = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007348 return LY_SUCCESS;
7349 }
7350
7351 /* first convert properly */
7352 if ((op[0] == '=') || (op[0] == '!')) {
7353 if ((set1->type == LYXP_SET_BOOLEAN) || (set2->type == LYXP_SET_BOOLEAN)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007354 lyxp_set_cast(set1, LYXP_SET_BOOLEAN);
7355 lyxp_set_cast(set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007356 } else if ((set1->type == LYXP_SET_NUMBER) || (set2->type == LYXP_SET_NUMBER)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007357 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007358 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007359 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007360 LY_CHECK_RET(rc);
7361 } /* else we have 2 strings */
7362 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007363 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007364 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007365 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007366 LY_CHECK_RET(rc);
7367 }
7368
7369 assert(set1->type == set2->type);
7370
7371 /* compute result */
7372 if (op[0] == '=') {
7373 if (set1->type == LYXP_SET_BOOLEAN) {
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007374 *result = (set1->val.bln == set2->val.bln);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007375 } else if (set1->type == LYXP_SET_NUMBER) {
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007376 *result = (set1->val.num == set2->val.num);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007377 } else {
7378 assert(set1->type == LYXP_SET_STRING);
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007379 *result = strcmp(set1->val.str, set2->val.str) ? 0 : 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007380 }
7381 } else if (op[0] == '!') {
7382 if (set1->type == LYXP_SET_BOOLEAN) {
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007383 *result = (set1->val.bln != set2->val.bln);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007384 } else if (set1->type == LYXP_SET_NUMBER) {
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007385 *result = (set1->val.num != set2->val.num);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007386 } else {
7387 assert(set1->type == LYXP_SET_STRING);
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007388 *result = strcmp(set1->val.str, set2->val.str) ? 1 : 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007389 }
7390 } else {
7391 assert(set1->type == LYXP_SET_NUMBER);
7392 if (op[0] == '<') {
7393 if (op[1] == '=') {
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007394 *result = (set1->val.num <= set2->val.num);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007395 } else {
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007396 *result = (set1->val.num < set2->val.num);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007397 }
7398 } else {
7399 if (op[1] == '=') {
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007400 *result = (set1->val.num >= set2->val.num);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007401 } else {
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007402 *result = (set1->val.num > set2->val.num);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007403 }
7404 }
7405 }
7406
Michal Vasko03ff5a72019-09-11 13:49:33 +02007407 return LY_SUCCESS;
7408}
7409
7410/**
7411 * @brief Move context @p set to the result of a basic operation. Handles '+', '-', unary '-', '*', 'div',
7412 * or 'mod'. Result is LYXP_SET_NUMBER. Indirectly context position aware.
7413 *
7414 * @param[in,out] set1 Set to use for the result.
7415 * @param[in] set2 Set acting as the second operand for @p op.
7416 * @param[in] op Operator to process.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007417 * @return LY_ERR
7418 */
7419static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007420moveto_op_math(struct lyxp_set *set1, struct lyxp_set *set2, const char *op)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007421{
7422 LY_ERR rc;
7423
7424 /* unary '-' */
7425 if (!set2 && (op[0] == '-')) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007426 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007427 LY_CHECK_RET(rc);
7428 set1->val.num *= -1;
7429 lyxp_set_free(set2);
7430 return LY_SUCCESS;
7431 }
7432
7433 assert(set1 && set2);
7434
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007435 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007436 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007437 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007438 LY_CHECK_RET(rc);
7439
7440 switch (op[0]) {
7441 /* '+' */
7442 case '+':
7443 set1->val.num += set2->val.num;
7444 break;
7445
7446 /* '-' */
7447 case '-':
7448 set1->val.num -= set2->val.num;
7449 break;
7450
7451 /* '*' */
7452 case '*':
7453 set1->val.num *= set2->val.num;
7454 break;
7455
7456 /* 'div' */
7457 case 'd':
7458 set1->val.num /= set2->val.num;
7459 break;
7460
7461 /* 'mod' */
7462 case 'm':
7463 set1->val.num = ((long long)set1->val.num) % ((long long)set2->val.num);
7464 break;
7465
7466 default:
7467 LOGINT_RET(set1->ctx);
7468 }
7469
7470 return LY_SUCCESS;
7471}
7472
Michal Vasko03ff5a72019-09-11 13:49:33 +02007473/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02007474 * @brief Evaluate Predicate. Logs directly on error.
7475 *
Michal Vaskod3678892020-05-21 10:06:58 +02007476 * [9] Predicate ::= '[' Expr ']'
Michal Vasko03ff5a72019-09-11 13:49:33 +02007477 *
7478 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007479 * @param[in] tok_idx Position in the expression @p exp.
aPiecek8b0cc152021-05-31 16:40:31 +02007480 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007481 * @param[in] options XPath options.
Michal Vasko49fec8e2022-05-24 10:28:33 +02007482 * @param[in] axis Axis to search on.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007483 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7484 */
7485static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02007486eval_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 +02007487{
7488 LY_ERR rc;
Michal Vaskodd528af2022-08-08 14:35:07 +02007489 uint32_t i, orig_exp, orig_pos, orig_size;
Michal Vasko5c4e5892019-11-14 12:31:38 +01007490 int32_t pred_in_ctx;
Michal Vasko88a9e802022-05-24 10:50:28 +02007491 ly_bool reverse_axis = 0;
aPiecekfff4dca2021-10-07 10:59:53 +02007492 struct lyxp_set set2 = {0};
Michal Vasko03ff5a72019-09-11 13:49:33 +02007493
7494 /* '[' */
aPiecek8b0cc152021-05-31 16:40:31 +02007495 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02007496 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007497 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007498
aPiecek8b0cc152021-05-31 16:40:31 +02007499 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007500only_parse:
aPiecek8b0cc152021-05-31 16:40:31 +02007501 rc = eval_expr_select(exp, tok_idx, 0, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007502 LY_CHECK_RET(rc);
7503 } else if (set->type == LYXP_SET_NODE_SET) {
7504 /* 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 +01007505 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007506
7507 /* empty set, nothing to evaluate */
7508 if (!set->used) {
7509 goto only_parse;
7510 }
7511
Michal Vasko49fec8e2022-05-24 10:28:33 +02007512 /* decide forward or reverse axis */
7513 switch (axis) {
7514 case LYXP_AXIS_ANCESTOR:
7515 case LYXP_AXIS_ANCESTOR_OR_SELF:
7516 case LYXP_AXIS_PRECEDING:
7517 case LYXP_AXIS_PRECEDING_SIBLING:
7518 reverse_axis = 1;
7519 break;
7520 case LYXP_AXIS_DESCENDANT:
7521 case LYXP_AXIS_DESCENDANT_OR_SELF:
7522 case LYXP_AXIS_FOLLOWING:
7523 case LYXP_AXIS_FOLLOWING_SIBLING:
7524 case LYXP_AXIS_PARENT:
7525 case LYXP_AXIS_CHILD:
7526 case LYXP_AXIS_SELF:
7527 case LYXP_AXIS_ATTRIBUTE:
7528 reverse_axis = 0;
7529 break;
7530 }
7531
Michal Vasko004d3152020-06-11 19:59:22 +02007532 orig_exp = *tok_idx;
Michal Vasko49fec8e2022-05-24 10:28:33 +02007533 orig_pos = reverse_axis ? set->used + 1 : 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007534 orig_size = set->used;
Michal Vasko39dbf352020-05-21 10:08:59 +02007535 for (i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007536 set_init(&set2, set);
7537 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 +02007538
7539 /* remember the node context position for position() and context size for last() */
7540 orig_pos += reverse_axis ? -1 : 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007541
7542 set2.ctx_pos = orig_pos;
7543 set2.ctx_size = orig_size;
Michal Vasko004d3152020-06-11 19:59:22 +02007544 *tok_idx = orig_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007545
Michal Vasko004d3152020-06-11 19:59:22 +02007546 rc = eval_expr_select(exp, tok_idx, 0, &set2, options);
Michal Vaskob7ba2c92024-01-05 15:17:53 +01007547 if (!rc && set2.not_found) {
7548 set->not_found = 1;
7549 break;
7550 }
7551 if (rc) {
Michal Vaskod3678892020-05-21 10:06:58 +02007552 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007553 return rc;
7554 }
7555
Michal Vasko49fec8e2022-05-24 10:28:33 +02007556 /* number is a proximity position */
Michal Vasko03ff5a72019-09-11 13:49:33 +02007557 if (set2.type == LYXP_SET_NUMBER) {
7558 if ((long long)set2.val.num == orig_pos) {
7559 set2.val.num = 1;
7560 } else {
7561 set2.val.num = 0;
7562 }
7563 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007564 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007565
7566 /* predicate satisfied or not? */
Michal Vasko004d3152020-06-11 19:59:22 +02007567 if (!set2.val.bln) {
Michal Vasko2caefc12019-11-14 16:07:56 +01007568 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007569 }
7570 }
Michal Vasko2caefc12019-11-14 16:07:56 +01007571 set_remove_nodes_none(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007572
7573 } else if (set->type == LYXP_SET_SCNODE_SET) {
7574 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01007575 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007576 /* there is a currently-valid node */
7577 break;
7578 }
7579 }
7580 /* empty set, nothing to evaluate */
7581 if (i == set->used) {
7582 goto only_parse;
7583 }
7584
Michal Vasko004d3152020-06-11 19:59:22 +02007585 orig_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007586
Michal Vasko03ff5a72019-09-11 13:49:33 +02007587 /* set special in_ctx to all the valid snodes */
7588 pred_in_ctx = set_scnode_new_in_ctx(set);
7589
7590 /* use the valid snodes one-by-one */
7591 for (i = 0; i < set->used; ++i) {
7592 if (set->val.scnodes[i].in_ctx != pred_in_ctx) {
7593 continue;
7594 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01007595 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007596
Michal Vasko004d3152020-06-11 19:59:22 +02007597 *tok_idx = orig_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007598
Michal Vasko004d3152020-06-11 19:59:22 +02007599 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vaskob7ba2c92024-01-05 15:17:53 +01007600 if (!rc && set->not_found) {
7601 break;
7602 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02007603 LY_CHECK_RET(rc);
7604
7605 set->val.scnodes[i].in_ctx = pred_in_ctx;
7606 }
7607
7608 /* restore the state as it was before the predicate */
7609 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01007610 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007611 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007612 } else if (set->val.scnodes[i].in_ctx == pred_in_ctx) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01007613 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007614 }
7615 }
7616
7617 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02007618 set2.type = LYXP_SET_NODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007619 set_fill_set(&set2, set);
7620
Michal Vasko004d3152020-06-11 19:59:22 +02007621 rc = eval_expr_select(exp, tok_idx, 0, &set2, options);
Michal Vaskob7ba2c92024-01-05 15:17:53 +01007622 if (rc) {
Michal Vaskod3678892020-05-21 10:06:58 +02007623 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007624 return rc;
7625 }
7626
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007627 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +02007628 if (!set2.val.bln) {
Michal Vaskod3678892020-05-21 10:06:58 +02007629 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007630 }
Michal Vaskod3678892020-05-21 10:06:58 +02007631 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007632 }
7633
7634 /* ']' */
Michal Vasko004d3152020-06-11 19:59:22 +02007635 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_BRACK2);
aPiecek8b0cc152021-05-31 16:40:31 +02007636 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02007637 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007638 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007639
7640 return LY_SUCCESS;
7641}
7642
7643/**
Michal Vaskod3678892020-05-21 10:06:58 +02007644 * @brief Evaluate Literal. Logs directly on error.
7645 *
7646 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007647 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02007648 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7649 */
7650static void
Michal Vaskodd528af2022-08-08 14:35:07 +02007651eval_literal(const struct lyxp_expr *exp, uint32_t *tok_idx, struct lyxp_set *set)
Michal Vaskod3678892020-05-21 10:06:58 +02007652{
7653 if (set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007654 if (exp->tok_len[*tok_idx] == 2) {
Michal Vaskod3678892020-05-21 10:06:58 +02007655 set_fill_string(set, "", 0);
7656 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007657 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 +02007658 }
7659 }
7660 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02007661 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007662 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007663}
7664
7665/**
Michal Vasko3d969ff2022-07-29 15:02:08 +02007666 * @brief Check that a nametest in a predicate matches a key node.
7667 *
7668 * @param[in] nametest Nametest to check.
7669 * @param[in] len Length of @p nametest.
7670 * @param[in] ctx_scnode Found schema node as the context for the predicate.
7671 * @param[in] set Context set.
7672 * @param[in] key Expected key node.
7673 * @return LY_SUCCESS on success,
7674 * @return LY_ENOT if a predicate could not be compiled.
7675 * @return LY_ERR on any error.
7676 */
7677static LY_ERR
7678eval_name_test_try_compile_predicate_key(const char *nametest, uint32_t len, const struct lysc_node *ctx_scnode,
7679 const struct lyxp_set *set, const struct lysc_node *key)
7680{
7681 const struct lys_module *mod;
7682
7683 /* prefix (module) */
7684 LY_CHECK_RET(moveto_resolve_model(&nametest, &len, set, ctx_scnode, &mod));
7685 if (mod != key->module) {
7686 return LY_ENOT;
7687 }
7688
7689 /* node name */
7690 if (ly_strncmp(key->name, nametest, len)) {
7691 return LY_ENOT;
7692 }
7693
7694 return LY_SUCCESS;
7695}
7696
7697/**
7698 * @brief Append a simple predicate for the node.
7699 *
7700 * @param[in] exp Full parsed XPath expression.
7701 * @param[in] tok_idx Predicate start index in @p exp.
7702 * @param[in] end_tok_idx Predicate end index in @p exp.
7703 * @param[in] ctx_scnode Found schema node as the context for the predicate.
7704 * @param[in] set Context set.
7705 * @param[in] pred_node Node with the value referenced in the predicate.
7706 * @param[in,out] pred Predicate to append to.
7707 * @param[in,out] pred_len Length of @p pred, is updated.
7708 * @return LY_SUCCESS on success,
7709 * @return LY_ENOT if a predicate could not be compiled.
7710 * @return LY_ERR on any error.
7711 */
7712static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02007713eval_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 +02007714 const struct lysc_node *ctx_scnode, const struct lyxp_set *set, const struct lysc_node *pred_node, char **pred,
7715 uint32_t *pred_len)
7716{
7717 LY_ERR rc = LY_SUCCESS;
7718 uint32_t i;
7719 const struct lyd_node *siblings;
7720 struct lyd_node *ctx_node;
Michal Vasko5fb92a22022-08-02 09:21:37 +02007721 const struct lysc_node *sparent, *cur_scnode;
Michal Vasko3d969ff2022-07-29 15:02:08 +02007722 struct lyxp_expr *val_exp = NULL;
7723 struct lyxp_set set2 = {0};
7724 char quot;
7725
7726 /* duplicate the value expression */
7727 LY_CHECK_GOTO(rc = lyxp_expr_dup(set->ctx, exp, tok_idx, end_tok_idx, &val_exp), cleanup);
7728
7729 /* get its atoms */
Michal Vasko5fb92a22022-08-02 09:21:37 +02007730 cur_scnode = set->cur_node ? set->cur_node->schema : NULL;
7731 LY_CHECK_GOTO(rc = lyxp_atomize(set->ctx, val_exp, set->cur_mod, set->format, set->prefix_data, cur_scnode,
7732 ctx_scnode, &set2, LYXP_SCNODE), cleanup);
Michal Vasko3d969ff2022-07-29 15:02:08 +02007733
7734 /* check whether we can compile a single predicate (evaluation result value is always the same) */
7735 for (i = 0; i < set2.used; ++i) {
7736 if ((set2.val.scnodes[i].type != LYXP_NODE_ELEM) || (set2.val.scnodes[i].in_ctx < LYXP_SET_SCNODE_ATOM_NODE)) {
7737 /* skip root and context node */
7738 continue;
7739 }
7740
Michal Vasko5fb92a22022-08-02 09:21:37 +02007741 /* 1) context node descendants are traversed - do best-effort detection of the value dependency on the
7742 * context node instance */
7743 if ((set2.val.scnodes[i].axis == LYXP_AXIS_CHILD) && (set2.val.scnodes[i].scnode->parent == ctx_scnode)) {
7744 /* 1.1) context node child was accessed on the child axis, certain dependency */
Michal Vasko3d969ff2022-07-29 15:02:08 +02007745 rc = LY_ENOT;
7746 goto cleanup;
7747 }
Michal Vasko5fb92a22022-08-02 09:21:37 +02007748 if ((set2.val.scnodes[i].axis == LYXP_AXIS_DESCENDANT) || (set2.val.scnodes[i].axis == LYXP_AXIS_DESCENDANT_OR_SELF)) {
7749 for (sparent = set2.val.scnodes[i].scnode->parent; sparent && (sparent != ctx_scnode); sparent = sparent->parent) {}
7750 if (sparent) {
7751 /* 1.2) context node descendant was accessed on the descendant axis, probable dependency */
7752 rc = LY_ENOT;
7753 goto cleanup;
7754 }
7755 }
Michal Vasko3d969ff2022-07-29 15:02:08 +02007756
Michal Vasko5fb92a22022-08-02 09:21:37 +02007757 /* 2) multi-instance nodes (list or leaf-list) are traversed - all the instances need to be considered,
7758 * but the current node can be safely ignored, it is always the same data instance */
7759 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 +02007760 rc = LY_ENOT;
7761 goto cleanup;
7762 }
Michal Vasko3d969ff2022-07-29 15:02:08 +02007763 }
7764
7765 /* get any data instance of the context node, we checked it makes no difference */
7766 siblings = set->val.nodes[0].node ? lyd_child(set->val.nodes[0].node) : set->tree;
7767 LY_CHECK_GOTO(rc = lyd_find_sibling_schema(siblings, ctx_scnode, &ctx_node), cleanup);
7768
7769 /* evaluate the value subexpression with the root context node */
7770 lyxp_set_free_content(&set2);
7771 LY_CHECK_GOTO(rc = lyxp_eval(set->ctx, val_exp, set->cur_mod, set->format, set->prefix_data, set->cur_node,
7772 ctx_node, set->tree, NULL, &set2, 0), cleanup);
7773
7774 /* cast it into a string */
7775 LY_CHECK_GOTO(rc = lyxp_set_cast(&set2, LYXP_SET_STRING), cleanup);
7776
7777 /* append the JSON predicate */
7778 *pred = ly_realloc(*pred, *pred_len + 1 + strlen(pred_node->name) + 2 + strlen(set2.val.str) + 3);
7779 LY_CHECK_ERR_GOTO(!*pred, LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
7780 quot = strchr(set2.val.str, '\'') ? '\"' : '\'';
7781 *pred_len += sprintf(*pred + *pred_len, "[%s=%c%s%c]", pred_node->name, quot, set2.val.str, quot);
7782
7783cleanup:
7784 lyxp_expr_free(set->ctx, val_exp);
7785 lyxp_set_free_content(&set2);
7786 return rc;
7787}
7788
7789/**
Michal Vasko004d3152020-06-11 19:59:22 +02007790 * @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 +02007791 *
Michal Vasko004d3152020-06-11 19:59:22 +02007792 * @param[in] exp Full parsed XPath expression.
7793 * @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 +02007794 * @param[in] ctx_scnode Found schema node as the context for the predicate.
7795 * @param[in] set Context set.
Michal Vasko004d3152020-06-11 19:59:22 +02007796 * @param[out] predicates Parsed predicates.
Michal Vasko004d3152020-06-11 19:59:22 +02007797 * @return LY_SUCCESS on success,
Michal Vasko3d969ff2022-07-29 15:02:08 +02007798 * @return LY_ENOT if a predicate could not be compiled.
Michal Vasko004d3152020-06-11 19:59:22 +02007799 * @return LY_ERR on any error.
Michal Vaskod3678892020-05-21 10:06:58 +02007800 */
7801static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02007802eval_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 +01007803 const struct lyxp_set *set, struct ly_path_predicate **predicates)
Michal Vaskod3678892020-05-21 10:06:58 +02007804{
Michal Vasko3d969ff2022-07-29 15:02:08 +02007805 LY_ERR rc = LY_SUCCESS;
Michal Vasko59004e32024-01-30 16:09:54 +01007806 uint32_t e_idx, val_start_idx, pred_idx = 0, *prev_lo, temp_lo = 0, pred_len = 0, nested_pred;
Michal Vaskod3678892020-05-21 10:06:58 +02007807 const struct lysc_node *key;
Michal Vasko3d969ff2022-07-29 15:02:08 +02007808 char *pred = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02007809 struct lyxp_expr *exp2 = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02007810
Michal Vasko3d969ff2022-07-29 15:02:08 +02007811 assert(ctx_scnode->nodetype & (LYS_LIST | LYS_LEAFLIST));
Michal Vaskod3678892020-05-21 10:06:58 +02007812
Michal Vasko3d969ff2022-07-29 15:02:08 +02007813 /* turn logging off */
Michal Vasko59004e32024-01-30 16:09:54 +01007814 prev_lo = ly_temp_log_options(&temp_lo);
Michal Vasko3d969ff2022-07-29 15:02:08 +02007815
7816 if (ctx_scnode->nodetype == LYS_LIST) {
7817 /* check for predicates "[key1=...][key2=...]..." */
7818
Michal Vasko004d3152020-06-11 19:59:22 +02007819 /* get key count */
Michal Vasko3d969ff2022-07-29 15:02:08 +02007820 if (ctx_scnode->flags & LYS_KEYLESS) {
7821 rc = LY_ENOT;
7822 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +02007823 }
Michal Vaskod3678892020-05-21 10:06:58 +02007824
Michal Vasko004d3152020-06-11 19:59:22 +02007825 /* learn where the predicates end */
7826 e_idx = *tok_idx;
Michal Vasko3d969ff2022-07-29 15:02:08 +02007827 for (key = lysc_node_child(ctx_scnode); key && (key->flags & LYS_KEY); key = key->next) {
Michal Vasko004d3152020-06-11 19:59:22 +02007828 /* '[' */
7829 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
Michal Vasko3d969ff2022-07-29 15:02:08 +02007830 rc = LY_ENOT;
7831 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +02007832 }
7833 ++e_idx;
7834
Michal Vasko3354d272021-04-06 09:40:06 +02007835 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_NAMETEST)) {
Michal Vasko3d969ff2022-07-29 15:02:08 +02007836 /* not a key */
7837 rc = LY_ENOT;
7838 goto cleanup;
Michal Vasko3354d272021-04-06 09:40:06 +02007839 }
7840
Michal Vasko3d969ff2022-07-29 15:02:08 +02007841 /* check key */
7842 LY_CHECK_GOTO(rc = eval_name_test_try_compile_predicate_key(exp->expr + exp->tok_pos[e_idx],
7843 exp->tok_len[e_idx], ctx_scnode, set, key), cleanup);
7844
7845 ++e_idx;
7846
7847 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_OPER_EQUAL)) {
7848 /* not '=' */
7849 rc = LY_ENOT;
7850 goto cleanup;
7851 }
7852 ++e_idx;
7853
7854 /* value start */
7855 val_start_idx = e_idx;
7856
Michal Vasko004d3152020-06-11 19:59:22 +02007857 /* ']' */
Michal Vasko3ef7e252022-11-16 08:29:49 +01007858 nested_pred = 1;
7859 do {
7860 ++e_idx;
7861
7862 if ((nested_pred == 1) && !lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_OPER_LOG)) {
Michal Vaskob8ee3562022-08-02 10:43:17 +02007863 /* higher priority than '=' */
7864 rc = LY_ENOT;
7865 goto cleanup;
Michal Vasko3ef7e252022-11-16 08:29:49 +01007866 } else if (!lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
7867 /* nested predicate */
7868 ++nested_pred;
7869 } else if (!lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) {
7870 /* predicate end */
7871 --nested_pred;
Michal Vaskob8ee3562022-08-02 10:43:17 +02007872 }
Michal Vasko3ef7e252022-11-16 08:29:49 +01007873 } while (nested_pred);
Michal Vasko004d3152020-06-11 19:59:22 +02007874
Michal Vasko3d969ff2022-07-29 15:02:08 +02007875 /* try to evaluate the value */
7876 LY_CHECK_GOTO(rc = eval_name_test_try_compile_predicate_append(exp, val_start_idx, e_idx - 1, ctx_scnode,
7877 set, key, &pred, &pred_len), cleanup);
7878
7879 ++e_idx;
Michal Vasko004d3152020-06-11 19:59:22 +02007880 }
Michal Vasko004d3152020-06-11 19:59:22 +02007881 } else {
Michal Vasko3d969ff2022-07-29 15:02:08 +02007882 /* check for predicate "[.=...]" */
7883
Michal Vasko004d3152020-06-11 19:59:22 +02007884 /* learn just where this single predicate ends */
7885 e_idx = *tok_idx;
7886
Michal Vaskod3678892020-05-21 10:06:58 +02007887 /* '[' */
Michal Vasko004d3152020-06-11 19:59:22 +02007888 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
Michal Vasko3d969ff2022-07-29 15:02:08 +02007889 rc = LY_ENOT;
7890 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +02007891 }
7892 ++e_idx;
Michal Vaskod3678892020-05-21 10:06:58 +02007893
Michal Vasko3354d272021-04-06 09:40:06 +02007894 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_DOT)) {
Michal Vasko3d969ff2022-07-29 15:02:08 +02007895 /* not the node value */
7896 rc = LY_ENOT;
7897 goto cleanup;
Michal Vasko3354d272021-04-06 09:40:06 +02007898 }
Michal Vasko3d969ff2022-07-29 15:02:08 +02007899 ++e_idx;
7900
7901 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_OPER_EQUAL)) {
7902 /* not '=' */
7903 rc = LY_ENOT;
7904 goto cleanup;
7905 }
7906 ++e_idx;
7907
7908 /* value start */
7909 val_start_idx = e_idx;
Michal Vasko3354d272021-04-06 09:40:06 +02007910
Michal Vaskod3678892020-05-21 10:06:58 +02007911 /* ']' */
Michal Vasko31f19632022-11-16 09:38:40 +01007912 nested_pred = 1;
7913 do {
7914 ++e_idx;
7915
7916 if ((nested_pred == 1) && !lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_OPER_LOG)) {
Michal Vaskob8ee3562022-08-02 10:43:17 +02007917 /* higher priority than '=' */
7918 rc = LY_ENOT;
7919 goto cleanup;
Michal Vasko31f19632022-11-16 09:38:40 +01007920 } else if (!lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
7921 /* nested predicate */
7922 ++nested_pred;
7923 } else if (!lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) {
7924 /* predicate end */
7925 --nested_pred;
Michal Vaskob8ee3562022-08-02 10:43:17 +02007926 }
Michal Vasko31f19632022-11-16 09:38:40 +01007927 } while (nested_pred);
Michal Vasko3d969ff2022-07-29 15:02:08 +02007928
7929 /* try to evaluate the value */
7930 LY_CHECK_GOTO(rc = eval_name_test_try_compile_predicate_append(exp, val_start_idx, e_idx - 1, ctx_scnode, set,
7931 ctx_scnode, &pred, &pred_len), cleanup);
7932
Michal Vasko004d3152020-06-11 19:59:22 +02007933 ++e_idx;
Michal Vaskod3678892020-05-21 10:06:58 +02007934 }
7935
Michal Vasko004d3152020-06-11 19:59:22 +02007936 /* parse the predicate(s) */
Michal Vasko3d969ff2022-07-29 15:02:08 +02007937 LY_CHECK_GOTO(rc = ly_path_parse_predicate(set->ctx, ctx_scnode, pred, pred_len, LY_PATH_PREFIX_OPTIONAL,
7938 LY_PATH_PRED_SIMPLE, &exp2), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02007939
7940 /* compile */
Michal Vasko3d969ff2022-07-29 15:02:08 +02007941 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 +01007942 &pred_idx, LY_VALUE_JSON, NULL, predicates);
Michal Vasko3d969ff2022-07-29 15:02:08 +02007943 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02007944
7945 /* success, the predicate must include all the needed information for hash-based search */
7946 *tok_idx = e_idx;
7947
7948cleanup:
Michal Vasko59004e32024-01-30 16:09:54 +01007949 ly_temp_log_options(prev_lo);
Michal Vasko3d969ff2022-07-29 15:02:08 +02007950 lyxp_expr_free(set->ctx, exp2);
7951 free(pred);
7952 return rc;
Michal Vaskod3678892020-05-21 10:06:58 +02007953}
7954
7955/**
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007956 * @brief Search for/check the next schema node that could be the only matching schema node meaning the
7957 * data node(s) could be found using a single hash-based search.
7958 *
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007959 * @param[in] ctx libyang context.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007960 * @param[in] node Next context node to check.
7961 * @param[in] name Expected node name.
7962 * @param[in] name_len Length of @p name.
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007963 * @param[in] moveto_mod Expected node module, can be NULL for JSON format with no prefix.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007964 * @param[in] root_type XPath root type.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007965 * @param[in] format Prefix format.
7966 * @param[in,out] found Previously found node, is updated.
7967 * @return LY_SUCCESS on success,
7968 * @return LY_ENOT if the whole check failed and hashes cannot be used.
7969 */
7970static LY_ERR
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007971eval_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 +02007972 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 +02007973 const struct lysc_node **found)
7974{
Michal Vaskoc7304482023-11-30 15:59:30 +01007975 const struct lysc_node *scnode, *scnode2;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007976 const struct lys_module *mod;
7977 uint32_t idx = 0;
7978
Radek Krejci8df109d2021-04-23 12:19:08 +02007979 assert((format == LY_VALUE_JSON) || moveto_mod);
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007980
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007981continue_search:
Michal Vasko7d1d0912020-10-16 08:38:30 +02007982 scnode = NULL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007983 if (!node) {
Radek Krejci8df109d2021-04-23 12:19:08 +02007984 if ((format == LY_VALUE_JSON) && !moveto_mod) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007985 /* search all modules for a single match */
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007986 while ((mod = ly_ctx_get_module_iter(ctx, &idx))) {
Michal Vasko35a3b1d2021-07-14 09:34:16 +02007987 if (!mod->implemented) {
7988 continue;
7989 }
7990
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007991 scnode = lys_find_child(NULL, mod, name, name_len, 0, 0);
7992 if (scnode) {
7993 /* we have found a match */
7994 break;
7995 }
7996 }
7997
Michal Vasko7d1d0912020-10-16 08:38:30 +02007998 if (!scnode) {
7999 /* all modules searched */
8000 idx = 0;
8001 }
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008002 } else {
8003 /* search in top-level */
8004 scnode = lys_find_child(NULL, moveto_mod, name, name_len, 0, 0);
8005 }
8006 } else if (!*found || (lysc_data_parent(*found) != node->schema)) {
Radek Krejci8df109d2021-04-23 12:19:08 +02008007 if ((format == LY_VALUE_JSON) && !moveto_mod) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008008 /* we must adjust the module to inherit the one from the context node */
8009 moveto_mod = node->schema->module;
8010 }
8011
8012 /* search in children, do not repeat the same search */
Michal Vaskoc7304482023-11-30 15:59:30 +01008013 if (node->schema->nodetype & (LYS_RPC | LYS_ACTION)) {
8014 /* make sure the node is unique, whether in input or output */
8015 scnode = lys_find_child(node->schema, moveto_mod, name, name_len, 0, 0);
8016 scnode2 = lys_find_child(node->schema, moveto_mod, name, name_len, 0, LYS_GETNEXT_OUTPUT);
8017 if (scnode && scnode2) {
8018 /* conflict, do not use hashes */
8019 scnode = NULL;
8020 } else if (scnode2) {
8021 scnode = scnode2;
8022 }
8023 } else {
8024 scnode = lys_find_child(node->schema, moveto_mod, name, name_len, 0, 0);
8025 }
Michal Vasko7d1d0912020-10-16 08:38:30 +02008026 } /* else skip redundant search */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008027
8028 /* additional context check */
8029 if (scnode && (root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) {
8030 scnode = NULL;
8031 }
8032
8033 if (scnode) {
8034 if (*found) {
8035 /* we found a schema node with the same name but at different level, give up, too complicated
8036 * (more hash-based searches would be required, not supported) */
8037 return LY_ENOT;
8038 } else {
8039 /* remember the found schema node and continue to make sure it can be used */
8040 *found = scnode;
8041 }
8042 }
8043
8044 if (idx) {
8045 /* continue searching all the following models */
8046 goto continue_search;
8047 }
8048
8049 return LY_SUCCESS;
8050}
8051
8052/**
Michal Vasko4ad69e72021-10-26 16:25:55 +02008053 * @brief Generate message when no matching schema nodes were found for a path segment.
8054 *
8055 * @param[in] set XPath set.
8056 * @param[in] scparent Previous schema parent in the context, if only one.
8057 * @param[in] ncname XPath NCName being evaluated.
8058 * @param[in] ncname_len Length of @p ncname.
8059 * @param[in] expr Whole XPath expression.
8060 * @param[in] options XPath options.
8061 */
8062static void
8063eval_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 +02008064 uint32_t ncname_len, const char *expr, uint32_t options)
Michal Vasko4ad69e72021-10-26 16:25:55 +02008065{
8066 const char *format;
8067 char *path = NULL, *ppath = NULL;
8068
8069 path = lysc_path(set->cur_scnode, LYSC_PATH_LOG, NULL, 0);
8070 if (scparent) {
8071 /* generate path for the parent */
8072 if (scparent->type == LYXP_NODE_ELEM) {
8073 ppath = lysc_path(scparent->scnode, LYSC_PATH_LOG, NULL, 0);
8074 } else if (scparent->type == LYXP_NODE_ROOT) {
8075 ppath = strdup("<root>");
8076 } else if (scparent->type == LYXP_NODE_ROOT_CONFIG) {
8077 ppath = strdup("<config-root>");
8078 }
8079 }
8080 if (ppath) {
8081 format = "Schema node \"%.*s\" for parent \"%s\" not found; in expr \"%.*s\" with context node \"%s\".";
8082 if (options & LYXP_SCNODE_ERROR) {
Michal Vasko8d9889c2023-11-22 12:28:57 +01008083 LOGERR(set->ctx, LY_ENOTFOUND, format, ncname_len, ncname, ppath, (ncname - expr) + ncname_len, expr, path);
Michal Vasko4ad69e72021-10-26 16:25:55 +02008084 } else {
8085 LOGWRN(set->ctx, format, ncname_len, ncname, ppath, (ncname - expr) + ncname_len, expr, path);
8086 }
8087 } else {
8088 format = "Schema node \"%.*s\" not found; in expr \"%.*s\" with context node \"%s\".";
8089 if (options & LYXP_SCNODE_ERROR) {
Michal Vasko8d9889c2023-11-22 12:28:57 +01008090 LOGERR(set->ctx, LY_ENOTFOUND, format, ncname_len, ncname, (ncname - expr) + ncname_len, expr, path);
Michal Vasko4ad69e72021-10-26 16:25:55 +02008091 } else {
8092 LOGWRN(set->ctx, format, ncname_len, ncname, (ncname - expr) + ncname_len, expr, path);
8093 }
8094 }
8095 free(path);
8096 free(ppath);
8097}
8098
8099/**
Michal Vaskod3678892020-05-21 10:06:58 +02008100 * @brief Evaluate NameTest and any following Predicates. Logs directly on error.
8101 *
8102 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
8103 * [6] NodeTest ::= NameTest | NodeType '(' ')'
8104 * [7] NameTest ::= '*' | NCName ':' '*' | QName
8105 *
8106 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008107 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko49fec8e2022-05-24 10:28:33 +02008108 * @param[in] axis What axis to search on.
Michal Vaskod3678892020-05-21 10:06:58 +02008109 * @param[in] all_desc Whether to search all the descendants or children only.
aPiecek8b0cc152021-05-31 16:40:31 +02008110 * @param[in,out] set Context and result set.
Michal Vaskod3678892020-05-21 10:06:58 +02008111 * @param[in] options XPath options.
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008112 * @return LY_ERR (LY_EINCOMPLETE on unresolved when, LY_ENOT for not found schema node)
Michal Vaskod3678892020-05-21 10:06:58 +02008113 */
8114static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02008115eval_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 +02008116 struct lyxp_set *set, uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02008117{
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008118 LY_ERR rc = LY_SUCCESS, r;
Michal Vasko004d3152020-06-11 19:59:22 +02008119 const char *ncname, *ncname_dict = NULL;
Michal Vasko56c008c2022-07-29 14:57:47 +02008120 uint32_t i, ncname_len;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008121 const struct lys_module *moveto_mod = NULL;
Michal Vaskoc71c37b2021-01-11 13:40:02 +01008122 const struct lysc_node *scnode = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02008123 struct ly_path_predicate *predicates = NULL;
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008124 int scnode_skip_pred = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02008125
aPiecek8b0cc152021-05-31 16:40:31 +02008126 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008127 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008128 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02008129
aPiecek8b0cc152021-05-31 16:40:31 +02008130 if (options & LYXP_SKIP_EXPR) {
Michal Vaskod3678892020-05-21 10:06:58 +02008131 goto moveto;
8132 }
8133
Michal Vasko004d3152020-06-11 19:59:22 +02008134 ncname = &exp->expr[exp->tok_pos[*tok_idx - 1]];
8135 ncname_len = exp->tok_len[*tok_idx - 1];
Michal Vaskod3678892020-05-21 10:06:58 +02008136
Michal Vaskoc71c37b2021-01-11 13:40:02 +01008137 if ((ncname[0] == '*') && (ncname_len == 1)) {
8138 /* all nodes will match */
8139 goto moveto;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008140 }
8141
Michal Vaskoc71c37b2021-01-11 13:40:02 +01008142 /* parse (and skip) module name */
8143 rc = moveto_resolve_model(&ncname, &ncname_len, set, NULL, &moveto_mod);
Michal Vaskod3678892020-05-21 10:06:58 +02008144 LY_CHECK_GOTO(rc, cleanup);
8145
Michal Vasko49fec8e2022-05-24 10:28:33 +02008146 if ((ncname[0] == '*') && (ncname_len == 1)) {
8147 /* all nodes from the module will match */
8148 goto moveto;
8149 }
8150
8151 if (((set->format == LY_VALUE_JSON) || moveto_mod) && (axis == LYXP_AXIS_CHILD) && !all_desc &&
8152 (set->type == LYXP_SET_NODE_SET)) {
Michal Vaskod3678892020-05-21 10:06:58 +02008153 /* find the matching schema node in some parent in the context */
Michal Vasko56c008c2022-07-29 14:57:47 +02008154 for (i = 0; i < set->used; ++i) {
Michal Vaskoc71c37b2021-01-11 13:40:02 +01008155 if (eval_name_test_with_predicate_get_scnode(set->ctx, set->val.nodes[i].node, ncname, ncname_len,
8156 moveto_mod, set->root_type, set->format, &scnode)) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008157 /* check failed */
8158 scnode = NULL;
8159 break;
Michal Vaskod3678892020-05-21 10:06:58 +02008160 }
8161 }
8162
Michal Vasko004d3152020-06-11 19:59:22 +02008163 if (scnode && (scnode->nodetype & (LYS_LIST | LYS_LEAFLIST))) {
8164 /* try to create the predicates */
Michal Vasko90189962023-02-28 12:10:34 +01008165 if (eval_name_test_try_compile_predicates(exp, tok_idx, scnode, set, &predicates)) {
Michal Vasko004d3152020-06-11 19:59:22 +02008166 /* hashes cannot be used */
Michal Vaskod3678892020-05-21 10:06:58 +02008167 scnode = NULL;
8168 }
8169 }
8170 }
8171
Michal Vaskoc71c37b2021-01-11 13:40:02 +01008172 if (!scnode) {
8173 /* we are not able to match based on a schema node and not all the modules match ("*"),
Michal Vasko004d3152020-06-11 19:59:22 +02008174 * use dictionary for efficient comparison */
Radek Krejci011e4aa2020-09-04 15:22:31 +02008175 LY_CHECK_GOTO(rc = lydict_insert(set->ctx, ncname, ncname_len, &ncname_dict), cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02008176 }
8177
8178moveto:
8179 /* move to the attribute(s), data node(s), or schema node(s) */
Michal Vasko49fec8e2022-05-24 10:28:33 +02008180 if (axis == LYXP_AXIS_ATTRIBUTE) {
aPiecek8b0cc152021-05-31 16:40:31 +02008181 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008182 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskod3678892020-05-21 10:06:58 +02008183 } else {
8184 if (all_desc) {
Michal Vaskocdad7122020-11-09 21:04:44 +01008185 rc = moveto_attr_alldesc(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02008186 } else {
aPiecek8b0cc152021-05-31 16:40:31 +02008187 rc = moveto_attr(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02008188 }
8189 LY_CHECK_GOTO(rc, cleanup);
8190 }
8191 } else {
aPiecek8b0cc152021-05-31 16:40:31 +02008192 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008193 const struct lyxp_set_scnode *scparent = NULL;
Michal Vasko56c008c2022-07-29 14:57:47 +02008194 ly_bool found = 0;
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008195
8196 /* remember parent if there is only one, to print in the warning */
8197 for (i = 0; i < set->used; ++i) {
8198 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
8199 if (!scparent) {
8200 /* remember the context node */
8201 scparent = &set->val.scnodes[i];
8202 } else {
8203 /* several context nodes, no reasonable error possible */
8204 scparent = NULL;
8205 break;
8206 }
8207 }
8208 }
Radek Krejci1deb5be2020-08-26 16:43:36 +02008209
Michal Vasko49fec8e2022-05-24 10:28:33 +02008210 if (all_desc && (axis == LYXP_AXIS_CHILD)) {
8211 /* efficient evaluation that does not add all the descendants into the set */
8212 rc = moveto_scnode_alldesc_child(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02008213 } else {
Michal Vasko49fec8e2022-05-24 10:28:33 +02008214 if (all_desc) {
8215 /* "//" == "/descendant-or-self::node()/" */
8216 rc = xpath_pi_node(set, LYXP_AXIS_DESCENDANT_OR_SELF, options);
8217 LY_CHECK_GOTO(rc, cleanup);
8218 }
8219 rc = moveto_scnode(set, moveto_mod, ncname_dict, axis, options);
Michal Vaskod3678892020-05-21 10:06:58 +02008220 }
8221 LY_CHECK_GOTO(rc, cleanup);
8222
Michal Vasko56c008c2022-07-29 14:57:47 +02008223 i = set->used;
8224 do {
8225 --i;
Michal Vasko1a09b212021-05-06 13:00:10 +02008226 if (set->val.scnodes[i].in_ctx > LYXP_SET_SCNODE_ATOM_NODE) {
Michal Vasko56c008c2022-07-29 14:57:47 +02008227 found = 1;
Michal Vaskod3678892020-05-21 10:06:58 +02008228 break;
8229 }
Michal Vasko56c008c2022-07-29 14:57:47 +02008230 } while (i);
8231 if (!found) {
Michal Vasko4ad69e72021-10-26 16:25:55 +02008232 /* generate message */
8233 eval_name_test_scnode_no_match_msg(set, scparent, ncname, ncname_len, exp->expr, options);
8234
8235 if (options & LYXP_SCNODE_ERROR) {
8236 /* error */
Michal Vaskob7ba2c92024-01-05 15:17:53 +01008237 set->not_found = 1;
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008238 }
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008239
8240 /* skip the predicates and the rest of this path to not generate invalid warnings */
8241 rc = LY_ENOT;
8242 scnode_skip_pred = 1;
Michal Vaskod3678892020-05-21 10:06:58 +02008243 }
8244 } else {
Michal Vasko49fec8e2022-05-24 10:28:33 +02008245 if (all_desc && (axis == LYXP_AXIS_CHILD)) {
8246 /* efficient evaluation */
8247 rc = moveto_node_alldesc_child(set, moveto_mod, ncname_dict, options);
8248 } else if (scnode && (axis == LYXP_AXIS_CHILD)) {
8249 /* we can find the child nodes using hashes */
8250 rc = moveto_node_hash_child(set, scnode, predicates, options);
Michal Vaskod3678892020-05-21 10:06:58 +02008251 } else {
Michal Vasko49fec8e2022-05-24 10:28:33 +02008252 if (all_desc) {
8253 /* "//" == "/descendant-or-self::node()/" */
8254 rc = xpath_pi_node(set, LYXP_AXIS_DESCENDANT_OR_SELF, options);
8255 LY_CHECK_GOTO(rc, cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02008256 }
Michal Vasko49fec8e2022-05-24 10:28:33 +02008257 rc = moveto_node(set, moveto_mod, ncname_dict, axis, options);
Michal Vaskod3678892020-05-21 10:06:58 +02008258 }
8259 LY_CHECK_GOTO(rc, cleanup);
8260 }
8261 }
8262
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008263 if (scnode_skip_pred) {
8264 /* skip predicates */
8265 options |= LYXP_SKIP_EXPR;
8266 }
8267
Michal Vaskod3678892020-05-21 10:06:58 +02008268 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02008269 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02008270 r = eval_predicate(exp, tok_idx, set, options, axis);
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008271 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02008272 }
8273
8274cleanup:
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008275 if (scnode_skip_pred) {
8276 /* restore options */
8277 options &= ~LYXP_SKIP_EXPR;
8278 }
Michal Vaskob7ba2c92024-01-05 15:17:53 +01008279 lydict_remove(set->ctx, ncname_dict);
8280 ly_path_predicates_free(set->ctx, predicates);
Michal Vaskod3678892020-05-21 10:06:58 +02008281 return rc;
8282}
8283
8284/**
8285 * @brief Evaluate NodeType and any following Predicates. Logs directly on error.
8286 *
8287 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
8288 * [6] NodeTest ::= NameTest | NodeType '(' ')'
8289 * [8] NodeType ::= 'text' | 'node'
8290 *
8291 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008292 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko49fec8e2022-05-24 10:28:33 +02008293 * @param[in] axis Axis to search on.
8294 * @param[in] all_desc Whether to search all the descendants or axis only.
aPiecek8b0cc152021-05-31 16:40:31 +02008295 * @param[in,out] set Context and result set.
Michal Vaskod3678892020-05-21 10:06:58 +02008296 * @param[in] options XPath options.
8297 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8298 */
8299static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02008300eval_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 +02008301 struct lyxp_set *set, uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02008302{
8303 LY_ERR rc;
8304
Michal Vaskod3678892020-05-21 10:06:58 +02008305 (void)all_desc;
8306
aPiecek8b0cc152021-05-31 16:40:31 +02008307 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko004d3152020-06-11 19:59:22 +02008308 assert(exp->tok_len[*tok_idx] == 4);
Michal Vasko49fec8e2022-05-24 10:28:33 +02008309 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "node", 4)) {
8310 rc = xpath_pi_node(set, axis, options);
Michal Vaskod3678892020-05-21 10:06:58 +02008311 } else {
Michal Vasko49fec8e2022-05-24 10:28:33 +02008312 assert(!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "text", 4));
8313 rc = xpath_pi_text(set, axis, options);
Michal Vaskod3678892020-05-21 10:06:58 +02008314 }
Michal Vasko49fec8e2022-05-24 10:28:33 +02008315 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02008316 }
aPiecek8b0cc152021-05-31 16:40:31 +02008317 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008318 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008319 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02008320
8321 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02008322 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
aPiecek8b0cc152021-05-31 16:40:31 +02008323 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008324 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008325 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02008326
8327 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02008328 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
aPiecek8b0cc152021-05-31 16:40:31 +02008329 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008330 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008331 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02008332
8333 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02008334 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02008335 rc = eval_predicate(exp, tok_idx, set, options, axis);
Michal Vaskod3678892020-05-21 10:06:58 +02008336 LY_CHECK_RET(rc);
8337 }
8338
8339 return LY_SUCCESS;
8340}
8341
8342/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02008343 * @brief Evaluate RelativeLocationPath. Logs directly on error.
8344 *
8345 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
8346 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
Michal Vaskod3678892020-05-21 10:06:58 +02008347 * [6] NodeTest ::= NameTest | NodeType '(' ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02008348 *
8349 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008350 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008351 * @param[in] all_desc Whether to search all the descendants or children only.
aPiecek8b0cc152021-05-31 16:40:31 +02008352 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008353 * @param[in] options XPath options.
8354 * @return LY_ERR (YL_EINCOMPLETE on unresolved when)
8355 */
8356static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02008357eval_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 +02008358 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008359{
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008360 LY_ERR rc = LY_SUCCESS;
Michal Vasko49fec8e2022-05-24 10:28:33 +02008361 enum lyxp_axis axis;
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008362 int scnode_skip_path = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008363
8364 goto step;
8365 do {
8366 /* evaluate '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02008367 if (exp->tok_len[*tok_idx] == 1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008368 all_desc = 0;
8369 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02008370 assert(exp->tok_len[*tok_idx] == 2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008371 all_desc = 1;
8372 }
aPiecek8b0cc152021-05-31 16:40:31 +02008373 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008374 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008375 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008376
8377step:
Michal Vasko49fec8e2022-05-24 10:28:33 +02008378 /* AxisSpecifier */
8379 if (exp->tokens[*tok_idx] == LYXP_TOKEN_AXISNAME) {
8380 axis = str2axis(exp->expr + exp->tok_pos[*tok_idx], exp->tok_len[*tok_idx]);
Michal Vaskod3678892020-05-21 10:06:58 +02008381
aPiecek8b0cc152021-05-31 16:40:31 +02008382 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008383 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
8384 ++(*tok_idx);
8385
8386 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_DCOLON);
8387 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
8388 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
8389 ++(*tok_idx);
8390 } else if (exp->tokens[*tok_idx] == LYXP_TOKEN_AT) {
8391 axis = LYXP_AXIS_ATTRIBUTE;
8392
8393 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
8394 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008395 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02008396 } else {
Michal Vasko49fec8e2022-05-24 10:28:33 +02008397 /* default */
8398 axis = LYXP_AXIS_CHILD;
Michal Vaskod3678892020-05-21 10:06:58 +02008399 }
8400
Michal Vasko49fec8e2022-05-24 10:28:33 +02008401 /* NodeTest Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02008402 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008403 case LYXP_TOKEN_DOT:
8404 /* evaluate '.' */
Michal Vasko49fec8e2022-05-24 10:28:33 +02008405 if (!(options & LYXP_SKIP_EXPR)) {
8406 if (((options & LYXP_SCNODE_ALL) && (set->type != LYXP_SET_SCNODE_SET)) ||
8407 (!(options & LYXP_SCNODE_ALL) && (set->type != LYXP_SET_NODE_SET))) {
8408 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
8409 rc = LY_EVALID;
8410 goto cleanup;
8411 }
8412
8413 if (all_desc) {
8414 rc = xpath_pi_node(set, LYXP_AXIS_DESCENDANT_OR_SELF, options);
8415 LY_CHECK_GOTO(rc, cleanup);
8416 }
8417 rc = xpath_pi_node(set, LYXP_AXIS_SELF, options);
8418 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008419 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008420 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008421 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008422 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008423 break;
8424
8425 case LYXP_TOKEN_DDOT:
8426 /* evaluate '..' */
Michal Vasko49fec8e2022-05-24 10:28:33 +02008427 if (!(options & LYXP_SKIP_EXPR)) {
8428 if (((options & LYXP_SCNODE_ALL) && (set->type != LYXP_SET_SCNODE_SET)) ||
8429 (!(options & LYXP_SCNODE_ALL) && (set->type != LYXP_SET_NODE_SET))) {
8430 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
8431 rc = LY_EVALID;
8432 goto cleanup;
8433 }
8434
8435 if (all_desc) {
8436 rc = xpath_pi_node(set, LYXP_AXIS_DESCENDANT_OR_SELF, options);
8437 LY_CHECK_GOTO(rc, cleanup);
8438 }
8439 rc = xpath_pi_node(set, LYXP_AXIS_PARENT, options);
8440 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008441 }
aPiecek8b0cc152021-05-31 16:40:31 +02008442 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008443 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008444 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008445 break;
8446
Michal Vasko03ff5a72019-09-11 13:49:33 +02008447 case LYXP_TOKEN_NAMETEST:
Michal Vaskod3678892020-05-21 10:06:58 +02008448 /* evaluate NameTest Predicate* */
Michal Vasko49fec8e2022-05-24 10:28:33 +02008449 rc = eval_name_test_with_predicate(exp, tok_idx, axis, all_desc, set, options);
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008450 if (rc == LY_ENOT) {
8451 assert(options & LYXP_SCNODE_ALL);
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008452 rc = LY_SUCCESS;
Michal Vaskob7ba2c92024-01-05 15:17:53 +01008453
8454 /* skip the rest of this path */
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008455 scnode_skip_path = 1;
8456 options |= LYXP_SKIP_EXPR;
8457 }
8458 LY_CHECK_GOTO(rc, cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02008459 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008460
Michal Vaskod3678892020-05-21 10:06:58 +02008461 case LYXP_TOKEN_NODETYPE:
8462 /* evaluate NodeType Predicate* */
Michal Vasko49fec8e2022-05-24 10:28:33 +02008463 rc = eval_node_type_with_predicate(exp, tok_idx, axis, all_desc, set, options);
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008464 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008465 break;
8466
8467 default:
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008468 LOGINT(set->ctx);
8469 rc = LY_EINT;
8470 goto cleanup;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008471 }
Michal Vasko004d3152020-06-11 19:59:22 +02008472 } while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008473
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008474cleanup:
8475 if (scnode_skip_path) {
8476 options &= ~LYXP_SKIP_EXPR;
8477 }
8478 return rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008479}
8480
8481/**
8482 * @brief Evaluate AbsoluteLocationPath. Logs directly on error.
8483 *
8484 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
8485 *
8486 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008487 * @param[in] tok_idx Position in the expression @p exp.
aPiecek8b0cc152021-05-31 16:40:31 +02008488 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008489 * @param[in] options XPath options.
8490 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8491 */
8492static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02008493eval_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 +02008494{
Radek Krejci857189e2020-09-01 13:26:36 +02008495 ly_bool all_desc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008496
aPiecek8b0cc152021-05-31 16:40:31 +02008497 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008498 /* no matter what tokens follow, we need to be at the root */
Michal Vaskob0099a92020-08-31 14:55:23 +02008499 LY_CHECK_RET(moveto_root(set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008500 }
8501
8502 /* '/' RelativeLocationPath? */
Michal Vasko004d3152020-06-11 19:59:22 +02008503 if (exp->tok_len[*tok_idx] == 1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008504 /* evaluate '/' - deferred */
8505 all_desc = 0;
aPiecek8b0cc152021-05-31 16:40:31 +02008506 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008507 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008508 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008509
Michal Vasko004d3152020-06-11 19:59:22 +02008510 if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008511 return LY_SUCCESS;
8512 }
Michal Vasko004d3152020-06-11 19:59:22 +02008513 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008514 case LYXP_TOKEN_DOT:
8515 case LYXP_TOKEN_DDOT:
Michal Vasko49fec8e2022-05-24 10:28:33 +02008516 case LYXP_TOKEN_AXISNAME:
Michal Vasko03ff5a72019-09-11 13:49:33 +02008517 case LYXP_TOKEN_AT:
8518 case LYXP_TOKEN_NAMETEST:
8519 case LYXP_TOKEN_NODETYPE:
Michal Vaskob0099a92020-08-31 14:55:23 +02008520 LY_CHECK_RET(eval_relative_location_path(exp, tok_idx, all_desc, set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008521 break;
8522 default:
8523 break;
8524 }
8525
Michal Vasko03ff5a72019-09-11 13:49:33 +02008526 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02008527 /* '//' RelativeLocationPath */
Michal Vasko03ff5a72019-09-11 13:49:33 +02008528 /* evaluate '//' - deferred so as not to waste memory by remembering all the nodes */
8529 all_desc = 1;
aPiecek8b0cc152021-05-31 16:40:31 +02008530 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008531 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008532 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008533
Michal Vaskob0099a92020-08-31 14:55:23 +02008534 LY_CHECK_RET(eval_relative_location_path(exp, tok_idx, all_desc, set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008535 }
8536
8537 return LY_SUCCESS;
8538}
8539
8540/**
8541 * @brief Evaluate FunctionCall. Logs directly on error.
8542 *
Michal Vaskod3678892020-05-21 10:06:58 +02008543 * [11] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02008544 *
8545 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008546 * @param[in] tok_idx Position in the expression @p exp.
aPiecek8b0cc152021-05-31 16:40:31 +02008547 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008548 * @param[in] options XPath options.
8549 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8550 */
8551static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02008552eval_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 +02008553{
8554 LY_ERR rc;
Michal Vasko69730152020-10-09 16:30:07 +02008555
Michal Vaskodd528af2022-08-08 14:35:07 +02008556 LY_ERR (*xpath_func)(struct lyxp_set **, uint32_t, struct lyxp_set *, uint32_t) = NULL;
8557 uint32_t arg_count = 0, i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008558 struct lyxp_set **args = NULL, **args_aux;
8559
aPiecek8b0cc152021-05-31 16:40:31 +02008560 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008561 /* FunctionName */
Michal Vasko004d3152020-06-11 19:59:22 +02008562 switch (exp->tok_len[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008563 case 3:
Michal Vasko004d3152020-06-11 19:59:22 +02008564 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008565 xpath_func = &xpath_not;
Michal Vasko004d3152020-06-11 19:59:22 +02008566 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008567 xpath_func = &xpath_sum;
8568 }
8569 break;
8570 case 4:
Michal Vasko004d3152020-06-11 19:59:22 +02008571 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008572 xpath_func = &xpath_lang;
Michal Vasko004d3152020-06-11 19:59:22 +02008573 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008574 xpath_func = &xpath_last;
Michal Vasko004d3152020-06-11 19:59:22 +02008575 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008576 xpath_func = &xpath_name;
Michal Vasko004d3152020-06-11 19:59:22 +02008577 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008578 xpath_func = &xpath_true;
8579 }
8580 break;
8581 case 5:
Michal Vasko004d3152020-06-11 19:59:22 +02008582 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008583 xpath_func = &xpath_count;
Michal Vasko004d3152020-06-11 19:59:22 +02008584 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008585 xpath_func = &xpath_false;
Michal Vasko004d3152020-06-11 19:59:22 +02008586 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008587 xpath_func = &xpath_floor;
Michal Vasko004d3152020-06-11 19:59:22 +02008588 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008589 xpath_func = &xpath_round;
Michal Vasko004d3152020-06-11 19:59:22 +02008590 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008591 xpath_func = &xpath_deref;
8592 }
8593 break;
8594 case 6:
Michal Vasko004d3152020-06-11 19:59:22 +02008595 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008596 xpath_func = &xpath_concat;
Michal Vasko004d3152020-06-11 19:59:22 +02008597 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008598 xpath_func = &xpath_number;
Michal Vasko004d3152020-06-11 19:59:22 +02008599 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008600 xpath_func = &xpath_string;
8601 }
8602 break;
8603 case 7:
Michal Vasko004d3152020-06-11 19:59:22 +02008604 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008605 xpath_func = &xpath_boolean;
Michal Vasko004d3152020-06-11 19:59:22 +02008606 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008607 xpath_func = &xpath_ceiling;
Michal Vasko004d3152020-06-11 19:59:22 +02008608 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008609 xpath_func = &xpath_current;
8610 }
8611 break;
8612 case 8:
Michal Vasko004d3152020-06-11 19:59:22 +02008613 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008614 xpath_func = &xpath_contains;
Michal Vasko004d3152020-06-11 19:59:22 +02008615 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008616 xpath_func = &xpath_position;
Michal Vasko004d3152020-06-11 19:59:22 +02008617 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "re-match", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008618 xpath_func = &xpath_re_match;
8619 }
8620 break;
8621 case 9:
Michal Vasko004d3152020-06-11 19:59:22 +02008622 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008623 xpath_func = &xpath_substring;
Michal Vasko004d3152020-06-11 19:59:22 +02008624 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008625 xpath_func = &xpath_translate;
8626 }
8627 break;
8628 case 10:
Michal Vasko004d3152020-06-11 19:59:22 +02008629 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008630 xpath_func = &xpath_local_name;
Michal Vasko004d3152020-06-11 19:59:22 +02008631 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008632 xpath_func = &xpath_enum_value;
Michal Vasko004d3152020-06-11 19:59:22 +02008633 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008634 xpath_func = &xpath_bit_is_set;
8635 }
8636 break;
8637 case 11:
Michal Vasko004d3152020-06-11 19:59:22 +02008638 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008639 xpath_func = &xpath_starts_with;
8640 }
8641 break;
8642 case 12:
Michal Vasko004d3152020-06-11 19:59:22 +02008643 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008644 xpath_func = &xpath_derived_from;
8645 }
8646 break;
8647 case 13:
Michal Vasko004d3152020-06-11 19:59:22 +02008648 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008649 xpath_func = &xpath_namespace_uri;
Michal Vasko004d3152020-06-11 19:59:22 +02008650 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string-length", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008651 xpath_func = &xpath_string_length;
8652 }
8653 break;
8654 case 15:
Michal Vasko004d3152020-06-11 19:59:22 +02008655 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008656 xpath_func = &xpath_normalize_space;
Michal Vasko004d3152020-06-11 19:59:22 +02008657 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-after", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008658 xpath_func = &xpath_substring_after;
8659 }
8660 break;
8661 case 16:
Michal Vasko004d3152020-06-11 19:59:22 +02008662 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008663 xpath_func = &xpath_substring_before;
8664 }
8665 break;
8666 case 20:
Michal Vasko004d3152020-06-11 19:59:22 +02008667 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from-or-self", 20)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008668 xpath_func = &xpath_derived_from_or_self;
8669 }
8670 break;
8671 }
8672
8673 if (!xpath_func) {
Michal Vasko21eaa392024-02-20 15:48:42 +01008674 LOGVAL(set->ctx, LY_VCODE_XP_INFUNC, (int)exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008675 return LY_EVALID;
8676 }
8677 }
8678
aPiecek8b0cc152021-05-31 16:40:31 +02008679 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008680 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008681 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008682
8683 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02008684 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
aPiecek8b0cc152021-05-31 16:40:31 +02008685 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008686 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008687 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008688
8689 /* ( Expr ( ',' Expr )* )? */
Michal Vasko004d3152020-06-11 19:59:22 +02008690 if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) {
aPiecek8b0cc152021-05-31 16:40:31 +02008691 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008692 args = malloc(sizeof *args);
8693 LY_CHECK_ERR_GOTO(!args, LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
8694 arg_count = 1;
8695 args[0] = set_copy(set);
8696 if (!args[0]) {
8697 rc = LY_EMEM;
8698 goto cleanup;
8699 }
8700
Michal Vasko004d3152020-06-11 19:59:22 +02008701 rc = eval_expr_select(exp, tok_idx, 0, args[0], options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008702 LY_CHECK_GOTO(rc, cleanup);
Michal Vaskob7ba2c92024-01-05 15:17:53 +01008703 set->not_found = args[0]->not_found;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008704 } else {
Michal Vaskob7ba2c92024-01-05 15:17:53 +01008705 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008706 LY_CHECK_GOTO(rc, cleanup);
8707 }
8708 }
Michal Vasko004d3152020-06-11 19:59:22 +02008709 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) {
aPiecek8b0cc152021-05-31 16:40:31 +02008710 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008711 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008712 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008713
aPiecek8b0cc152021-05-31 16:40:31 +02008714 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008715 ++arg_count;
8716 args_aux = realloc(args, arg_count * sizeof *args);
8717 LY_CHECK_ERR_GOTO(!args_aux, arg_count--; LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
8718 args = args_aux;
8719 args[arg_count - 1] = set_copy(set);
8720 if (!args[arg_count - 1]) {
8721 rc = LY_EMEM;
8722 goto cleanup;
8723 }
8724
Michal Vasko004d3152020-06-11 19:59:22 +02008725 rc = eval_expr_select(exp, tok_idx, 0, args[arg_count - 1], options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008726 LY_CHECK_GOTO(rc, cleanup);
Michal Vaskob7ba2c92024-01-05 15:17:53 +01008727 if (args[arg_count - 1]->not_found) {
8728 set->not_found = 1;
8729 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008730 } else {
Michal Vaskob7ba2c92024-01-05 15:17:53 +01008731 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008732 LY_CHECK_GOTO(rc, cleanup);
8733 }
8734 }
8735
8736 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02008737 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
aPiecek8b0cc152021-05-31 16:40:31 +02008738 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008739 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008740 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008741
aPiecek8b0cc152021-05-31 16:40:31 +02008742 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008743 /* evaluate function */
8744 rc = xpath_func(args, arg_count, set, options);
8745
8746 if (options & LYXP_SCNODE_ALL) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008747 /* merge all nodes from arg evaluations */
8748 for (i = 0; i < arg_count; ++i) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008749 set_scnode_clear_ctx(args[i], LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008750 lyxp_set_scnode_merge(set, args[i]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008751 }
8752 }
8753 } else {
8754 rc = LY_SUCCESS;
8755 }
8756
8757cleanup:
8758 for (i = 0; i < arg_count; ++i) {
8759 lyxp_set_free(args[i]);
8760 }
8761 free(args);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008762 return rc;
8763}
8764
8765/**
8766 * @brief Evaluate Number. Logs directly on error.
8767 *
8768 * @param[in] ctx Context for errors.
8769 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008770 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008771 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8772 * @return LY_ERR
8773 */
8774static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02008775eval_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 +02008776{
8777 long double num;
8778 char *endptr;
8779
8780 if (set) {
8781 errno = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02008782 num = strtold(&exp->expr[exp->tok_pos[*tok_idx]], &endptr);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008783 if (errno) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01008784 LOGVAL(ctx, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]);
8785 LOGVAL(ctx, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double (%s).",
Michal Vasko21eaa392024-02-20 15:48:42 +01008786 (int)exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]], strerror(errno));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008787 return LY_EVALID;
Michal Vasko21eaa392024-02-20 15:48:42 +01008788 } else if ((uint32_t)(endptr - &exp->expr[exp->tok_pos[*tok_idx]]) != exp->tok_len[*tok_idx]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01008789 LOGVAL(ctx, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]);
8790 LOGVAL(ctx, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double.",
Michal Vasko21eaa392024-02-20 15:48:42 +01008791 (int)exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008792 return LY_EVALID;
8793 }
8794
8795 set_fill_number(set, num);
8796 }
8797
8798 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008799 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008800 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008801 return LY_SUCCESS;
8802}
8803
aPiecekdf23eee2021-10-07 12:21:50 +02008804LY_ERR
Michal Vasko90189962023-02-28 12:10:34 +01008805lyxp_vars_find(const struct ly_ctx *ctx, const struct lyxp_var *vars, const char *name, size_t name_len,
8806 struct lyxp_var **var)
aPiecekdf23eee2021-10-07 12:21:50 +02008807{
aPiecekdf23eee2021-10-07 12:21:50 +02008808 LY_ARRAY_COUNT_TYPE u;
8809
Michal Vasko90189962023-02-28 12:10:34 +01008810 assert(name);
aPiecekdf23eee2021-10-07 12:21:50 +02008811
Michal Vasko90189962023-02-28 12:10:34 +01008812 if (!name_len) {
8813 name_len = strlen(name);
8814 }
aPiecekdf23eee2021-10-07 12:21:50 +02008815
8816 LY_ARRAY_FOR(vars, u) {
8817 if (!strncmp(vars[u].name, name, name_len)) {
Michal Vasko90189962023-02-28 12:10:34 +01008818 if (var) {
8819 *var = (struct lyxp_var *)&vars[u];
8820 }
8821 return LY_SUCCESS;
aPiecekdf23eee2021-10-07 12:21:50 +02008822 }
8823 }
8824
Michal Vasko90189962023-02-28 12:10:34 +01008825 if (ctx) {
8826 LOGERR(ctx, LY_ENOTFOUND, "Variable \"%.*s\" not defined.", (int)name_len, name);
aPiecekdf23eee2021-10-07 12:21:50 +02008827 }
Michal Vasko90189962023-02-28 12:10:34 +01008828 return LY_ENOTFOUND;
aPiecekdf23eee2021-10-07 12:21:50 +02008829}
8830
Michal Vasko03ff5a72019-09-11 13:49:33 +02008831/**
aPiecekfba75362021-10-07 12:39:48 +02008832 * @brief Evaluate VariableReference.
8833 *
8834 * @param[in] exp Parsed XPath expression.
8835 * @param[in] tok_idx Position in the expression @p exp.
8836 * @param[in] vars [Sized array](@ref sizedarrays) of XPath variables.
8837 * @param[in,out] set Context and result set.
8838 * @param[in] options XPath options.
8839 * @return LY_ERR value.
8840 */
8841static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02008842eval_variable_reference(const struct lyxp_expr *exp, uint32_t *tok_idx, struct lyxp_set *set, uint32_t options)
aPiecekfba75362021-10-07 12:39:48 +02008843{
8844 LY_ERR ret;
8845 const char *name;
8846 struct lyxp_var *var;
aPiecekfba75362021-10-07 12:39:48 +02008847 struct lyxp_expr *tokens = NULL;
Michal Vaskodd528af2022-08-08 14:35:07 +02008848 uint32_t token_index, name_len;
aPiecekfba75362021-10-07 12:39:48 +02008849
Michal Vasko49fec8e2022-05-24 10:28:33 +02008850 /* find out the name and value of the variable */
aPiecekfba75362021-10-07 12:39:48 +02008851 name = &exp->expr[exp->tok_pos[*tok_idx]];
Michal Vaskoe68b5402022-07-29 14:58:15 +02008852 name_len = exp->tok_len[*tok_idx];
Michal Vasko90189962023-02-28 12:10:34 +01008853 ret = lyxp_vars_find(set->ctx, set->vars, name, name_len, &var);
8854 LY_CHECK_RET(ret);
aPiecekfba75362021-10-07 12:39:48 +02008855
Michal Vasko49fec8e2022-05-24 10:28:33 +02008856 /* parse value */
aPiecekfba75362021-10-07 12:39:48 +02008857 ret = lyxp_expr_parse(set->ctx, var->value, 0, 1, &tokens);
8858 LY_CHECK_GOTO(ret, cleanup);
8859
Michal Vasko49fec8e2022-05-24 10:28:33 +02008860 /* evaluate value */
aPiecekfba75362021-10-07 12:39:48 +02008861 token_index = 0;
8862 ret = eval_expr_select(tokens, &token_index, 0, set, options);
8863 LY_CHECK_GOTO(ret, cleanup);
8864
8865cleanup:
8866 lyxp_expr_free(set->ctx, tokens);
8867
8868 return ret;
8869}
8870
8871/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02008872 * @brief Evaluate PathExpr. Logs directly on error.
8873 *
Michal Vaskod3678892020-05-21 10:06:58 +02008874 * [12] PathExpr ::= LocationPath | PrimaryExpr Predicate*
Michal Vasko03ff5a72019-09-11 13:49:33 +02008875 * | PrimaryExpr Predicate* '/' RelativeLocationPath
8876 * | PrimaryExpr Predicate* '//' RelativeLocationPath
8877 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
aPiecekfba75362021-10-07 12:39:48 +02008878 * [10] PrimaryExpr ::= VariableReference | '(' Expr ')' | Literal | Number | FunctionCall
Michal Vasko03ff5a72019-09-11 13:49:33 +02008879 *
8880 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008881 * @param[in] tok_idx Position in the expression @p exp.
aPiecek8b0cc152021-05-31 16:40:31 +02008882 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008883 * @param[in] options XPath options.
8884 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8885 */
8886static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02008887eval_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 +02008888{
Michal Vasko49fec8e2022-05-24 10:28:33 +02008889 ly_bool all_desc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008890 LY_ERR rc;
8891
Michal Vasko004d3152020-06-11 19:59:22 +02008892 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008893 case LYXP_TOKEN_PAR1:
8894 /* '(' Expr ')' */
8895
8896 /* '(' */
aPiecek8b0cc152021-05-31 16:40:31 +02008897 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008898 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008899 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008900
8901 /* Expr */
Michal Vasko004d3152020-06-11 19:59:22 +02008902 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008903 LY_CHECK_RET(rc);
8904
8905 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02008906 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
aPiecek8b0cc152021-05-31 16:40:31 +02008907 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008908 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008909 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008910
Michal Vasko03ff5a72019-09-11 13:49:33 +02008911 goto predicate;
8912
8913 case LYXP_TOKEN_DOT:
8914 case LYXP_TOKEN_DDOT:
Michal Vasko49fec8e2022-05-24 10:28:33 +02008915 case LYXP_TOKEN_AXISNAME:
Michal Vasko03ff5a72019-09-11 13:49:33 +02008916 case LYXP_TOKEN_AT:
8917 case LYXP_TOKEN_NAMETEST:
8918 case LYXP_TOKEN_NODETYPE:
8919 /* RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02008920 rc = eval_relative_location_path(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008921 LY_CHECK_RET(rc);
8922 break;
8923
aPiecekfba75362021-10-07 12:39:48 +02008924 case LYXP_TOKEN_VARREF:
8925 /* VariableReference */
8926 rc = eval_variable_reference(exp, tok_idx, set, options);
8927 LY_CHECK_RET(rc);
8928 ++(*tok_idx);
8929
aPiecekfba75362021-10-07 12:39:48 +02008930 goto predicate;
8931
Michal Vasko03ff5a72019-09-11 13:49:33 +02008932 case LYXP_TOKEN_FUNCNAME:
8933 /* FunctionCall */
aPiecek8b0cc152021-05-31 16:40:31 +02008934 rc = eval_function_call(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008935 LY_CHECK_RET(rc);
8936
Michal Vasko03ff5a72019-09-11 13:49:33 +02008937 goto predicate;
8938
Michal Vasko3e48bf32020-06-01 08:39:07 +02008939 case LYXP_TOKEN_OPER_PATH:
8940 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +02008941 /* AbsoluteLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02008942 rc = eval_absolute_location_path(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008943 LY_CHECK_RET(rc);
8944 break;
8945
8946 case LYXP_TOKEN_LITERAL:
8947 /* Literal */
aPiecek8b0cc152021-05-31 16:40:31 +02008948 if ((options & LYXP_SKIP_EXPR) || (options & LYXP_SCNODE_ALL)) {
8949 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008950 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008951 }
Michal Vasko004d3152020-06-11 19:59:22 +02008952 eval_literal(exp, tok_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008953 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02008954 eval_literal(exp, tok_idx, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008955 }
8956
Michal Vasko03ff5a72019-09-11 13:49:33 +02008957 goto predicate;
8958
8959 case LYXP_TOKEN_NUMBER:
8960 /* Number */
aPiecek8b0cc152021-05-31 16:40:31 +02008961 if ((options & LYXP_SKIP_EXPR) || (options & LYXP_SCNODE_ALL)) {
8962 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008963 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008964 }
Michal Vasko004d3152020-06-11 19:59:22 +02008965 rc = eval_number(NULL, exp, tok_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008966 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02008967 rc = eval_number(set->ctx, exp, tok_idx, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008968 }
8969 LY_CHECK_RET(rc);
8970
Michal Vasko03ff5a72019-09-11 13:49:33 +02008971 goto predicate;
8972
8973 default:
Michal Vasko49fec8e2022-05-24 10:28:33 +02008974 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 +02008975 return LY_EVALID;
8976 }
8977
8978 return LY_SUCCESS;
8979
8980predicate:
8981 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02008982 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02008983 rc = eval_predicate(exp, tok_idx, set, options, LYXP_AXIS_CHILD);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008984 LY_CHECK_RET(rc);
8985 }
8986
8987 /* ('/' or '//') RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02008988 if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008989
8990 /* evaluate '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02008991 if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008992 all_desc = 0;
8993 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008994 all_desc = 1;
8995 }
8996
aPiecek8b0cc152021-05-31 16:40:31 +02008997 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008998 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008999 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009000
Michal Vasko004d3152020-06-11 19:59:22 +02009001 rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009002 LY_CHECK_RET(rc);
9003 }
9004
9005 return LY_SUCCESS;
9006}
9007
9008/**
9009 * @brief Evaluate UnionExpr. Logs directly on error.
9010 *
Michal Vaskod3678892020-05-21 10:06:58 +02009011 * [20] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02009012 *
9013 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02009014 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009015 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02009016 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009017 * @param[in] options XPath options.
9018 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
9019 */
9020static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02009021eval_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 +02009022{
9023 LY_ERR rc = LY_SUCCESS;
Michal Vaskodd528af2022-08-08 14:35:07 +02009024 uint32_t i;
Michal Vaskob7ba2c92024-01-05 15:17:53 +01009025 struct lyxp_set orig_set, set2;
9026 ly_bool found = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009027
9028 assert(repeat);
9029
9030 set_init(&orig_set, set);
9031 set_init(&set2, set);
9032
9033 set_fill_set(&orig_set, set);
9034
Michal Vasko004d3152020-06-11 19:59:22 +02009035 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009036 LY_CHECK_GOTO(rc, cleanup);
Michal Vaskob7ba2c92024-01-05 15:17:53 +01009037 if (set->not_found) {
9038 set->not_found = 0;
9039 } else {
9040 found = 1;
9041 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02009042
9043 /* ('|' PathExpr)* */
9044 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02009045 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_UNI);
aPiecek8b0cc152021-05-31 16:40:31 +02009046 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02009047 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02009048 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009049
aPiecek8b0cc152021-05-31 16:40:31 +02009050 if (options & LYXP_SKIP_EXPR) {
9051 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009052 LY_CHECK_GOTO(rc, cleanup);
9053 continue;
9054 }
9055
9056 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02009057 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009058 LY_CHECK_GOTO(rc, cleanup);
Michal Vaskob7ba2c92024-01-05 15:17:53 +01009059 if (!set2.not_found) {
9060 found = 1;
9061 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02009062
9063 /* eval */
9064 if (options & LYXP_SCNODE_ALL) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01009065 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009066 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009067 rc = moveto_union(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009068 LY_CHECK_GOTO(rc, cleanup);
9069 }
9070 }
9071
9072cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02009073 lyxp_set_free_content(&orig_set);
9074 lyxp_set_free_content(&set2);
Michal Vaskob7ba2c92024-01-05 15:17:53 +01009075 if (!found) {
9076 set->not_found = 1;
9077 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02009078 return rc;
9079}
9080
9081/**
9082 * @brief Evaluate UnaryExpr. Logs directly on error.
9083 *
Michal Vaskod3678892020-05-21 10:06:58 +02009084 * [19] UnaryExpr ::= UnionExpr | '-' UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02009085 *
9086 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02009087 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009088 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02009089 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009090 * @param[in] options XPath options.
9091 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
9092 */
9093static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02009094eval_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 +02009095{
9096 LY_ERR rc;
Michal Vaskodd528af2022-08-08 14:35:07 +02009097 uint32_t this_op, i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009098
9099 assert(repeat);
9100
9101 /* ('-')+ */
Michal Vasko004d3152020-06-11 19:59:22 +02009102 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009103 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02009104 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 +02009105
aPiecek8b0cc152021-05-31 16:40:31 +02009106 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02009107 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02009108 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009109 }
9110
Michal Vasko004d3152020-06-11 19:59:22 +02009111 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNARY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009112 LY_CHECK_RET(rc);
9113
aPiecek8b0cc152021-05-31 16:40:31 +02009114 if (!(options & LYXP_SKIP_EXPR) && (repeat % 2)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02009115 if (options & LYXP_SCNODE_ALL) {
9116 warn_operands(set->ctx, set, NULL, 1, exp->expr, exp->tok_pos[this_op]);
9117 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009118 rc = moveto_op_math(set, NULL, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009119 LY_CHECK_RET(rc);
9120 }
9121 }
9122
9123 return LY_SUCCESS;
9124}
9125
9126/**
9127 * @brief Evaluate MultiplicativeExpr. Logs directly on error.
9128 *
Michal Vaskod3678892020-05-21 10:06:58 +02009129 * [18] MultiplicativeExpr ::= UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02009130 * | MultiplicativeExpr '*' UnaryExpr
9131 * | MultiplicativeExpr 'div' UnaryExpr
9132 * | MultiplicativeExpr 'mod' UnaryExpr
9133 *
9134 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02009135 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009136 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02009137 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009138 * @param[in] options XPath options.
9139 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
9140 */
9141static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02009142eval_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 +02009143 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02009144{
Michal Vaskob7ba2c92024-01-05 15:17:53 +01009145 LY_ERR rc = LY_SUCCESS;
Michal Vaskodd528af2022-08-08 14:35:07 +02009146 uint32_t i, this_op;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009147 struct lyxp_set orig_set, set2;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009148
9149 assert(repeat);
9150
9151 set_init(&orig_set, set);
9152 set_init(&set2, set);
9153
9154 set_fill_set(&orig_set, set);
9155
Michal Vasko004d3152020-06-11 19:59:22 +02009156 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009157 LY_CHECK_GOTO(rc, cleanup);
9158
9159 /* ('*' / 'div' / 'mod' UnaryExpr)* */
9160 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02009161 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009162
Michal Vasko004d3152020-06-11 19:59:22 +02009163 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH);
aPiecek8b0cc152021-05-31 16:40:31 +02009164 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02009165 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02009166 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009167
aPiecek8b0cc152021-05-31 16:40:31 +02009168 if (options & LYXP_SKIP_EXPR) {
9169 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009170 LY_CHECK_GOTO(rc, cleanup);
9171 continue;
9172 }
9173
9174 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02009175 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009176 LY_CHECK_GOTO(rc, cleanup);
Michal Vaskob7ba2c92024-01-05 15:17:53 +01009177 if (set2.not_found) {
9178 set->not_found = 1;
9179 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02009180
9181 /* eval */
9182 if (options & LYXP_SCNODE_ALL) {
9183 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01009184 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02009185 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009186 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009187 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009188 LY_CHECK_GOTO(rc, cleanup);
9189 }
9190 }
9191
9192cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02009193 lyxp_set_free_content(&orig_set);
9194 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009195 return rc;
9196}
9197
9198/**
9199 * @brief Evaluate AdditiveExpr. Logs directly on error.
9200 *
Michal Vaskod3678892020-05-21 10:06:58 +02009201 * [17] AdditiveExpr ::= MultiplicativeExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02009202 * | AdditiveExpr '+' MultiplicativeExpr
9203 * | AdditiveExpr '-' MultiplicativeExpr
9204 *
9205 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02009206 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009207 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02009208 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009209 * @param[in] options XPath options.
9210 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
9211 */
9212static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02009213eval_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 +02009214{
Michal Vaskob7ba2c92024-01-05 15:17:53 +01009215 LY_ERR rc = LY_SUCCESS;
Michal Vaskodd528af2022-08-08 14:35:07 +02009216 uint32_t i, this_op;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009217 struct lyxp_set orig_set, set2;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009218
9219 assert(repeat);
9220
9221 set_init(&orig_set, set);
9222 set_init(&set2, set);
9223
9224 set_fill_set(&orig_set, set);
9225
Michal Vasko004d3152020-06-11 19:59:22 +02009226 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009227 LY_CHECK_GOTO(rc, cleanup);
9228
9229 /* ('+' / '-' MultiplicativeExpr)* */
9230 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02009231 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009232
Michal Vasko004d3152020-06-11 19:59:22 +02009233 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009234 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02009235 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02009236 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009237
aPiecek8b0cc152021-05-31 16:40:31 +02009238 if (options & LYXP_SKIP_EXPR) {
9239 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009240 LY_CHECK_GOTO(rc, cleanup);
9241 continue;
9242 }
9243
9244 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02009245 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009246 LY_CHECK_GOTO(rc, cleanup);
Michal Vaskob7ba2c92024-01-05 15:17:53 +01009247 if (set2.not_found) {
9248 set->not_found = 1;
9249 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02009250
9251 /* eval */
9252 if (options & LYXP_SCNODE_ALL) {
9253 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01009254 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02009255 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009256 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009257 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009258 LY_CHECK_GOTO(rc, cleanup);
9259 }
9260 }
9261
9262cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02009263 lyxp_set_free_content(&orig_set);
9264 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009265 return rc;
9266}
9267
9268/**
9269 * @brief Evaluate RelationalExpr. Logs directly on error.
9270 *
Michal Vaskod3678892020-05-21 10:06:58 +02009271 * [16] RelationalExpr ::= AdditiveExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02009272 * | RelationalExpr '<' AdditiveExpr
9273 * | RelationalExpr '>' AdditiveExpr
9274 * | RelationalExpr '<=' AdditiveExpr
9275 * | RelationalExpr '>=' AdditiveExpr
9276 *
9277 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02009278 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009279 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02009280 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009281 * @param[in] options XPath options.
9282 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
9283 */
9284static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02009285eval_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 +02009286{
Michal Vaskob7ba2c92024-01-05 15:17:53 +01009287 LY_ERR rc = LY_SUCCESS;
Michal Vaskodd528af2022-08-08 14:35:07 +02009288 uint32_t i, this_op;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009289 struct lyxp_set orig_set, set2;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009290
9291 assert(repeat);
9292
9293 set_init(&orig_set, set);
9294 set_init(&set2, set);
9295
9296 set_fill_set(&orig_set, set);
9297
Michal Vasko004d3152020-06-11 19:59:22 +02009298 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009299 LY_CHECK_GOTO(rc, cleanup);
9300
9301 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
9302 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02009303 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009304
Michal Vasko004d3152020-06-11 19:59:22 +02009305 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_COMP);
aPiecek8b0cc152021-05-31 16:40:31 +02009306 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02009307 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02009308 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009309
aPiecek8b0cc152021-05-31 16:40:31 +02009310 if (options & LYXP_SKIP_EXPR) {
9311 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009312 LY_CHECK_GOTO(rc, cleanup);
9313 continue;
9314 }
9315
9316 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02009317 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009318 LY_CHECK_GOTO(rc, cleanup);
Michal Vaskob7ba2c92024-01-05 15:17:53 +01009319 if (set2.not_found) {
9320 set->not_found = 1;
9321 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02009322
9323 /* eval */
9324 if (options & LYXP_SCNODE_ALL) {
9325 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01009326 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02009327 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009328 } else {
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02009329 ly_bool result;
9330
9331 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], &result);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009332 LY_CHECK_GOTO(rc, cleanup);
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02009333 set_fill_boolean(set, result);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009334 }
9335 }
9336
9337cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02009338 lyxp_set_free_content(&orig_set);
9339 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009340 return rc;
9341}
9342
9343/**
9344 * @brief Evaluate EqualityExpr. Logs directly on error.
9345 *
Michal Vaskod3678892020-05-21 10:06:58 +02009346 * [15] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02009347 * | EqualityExpr '!=' RelationalExpr
9348 *
9349 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02009350 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009351 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02009352 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009353 * @param[in] options XPath options.
9354 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
9355 */
9356static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02009357eval_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 +02009358{
Michal Vaskob7ba2c92024-01-05 15:17:53 +01009359 LY_ERR rc = LY_SUCCESS;
Michal Vaskodd528af2022-08-08 14:35:07 +02009360 uint32_t i, this_op;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009361 struct lyxp_set orig_set, set2;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009362
9363 assert(repeat);
9364
9365 set_init(&orig_set, set);
9366 set_init(&set2, set);
9367
9368 set_fill_set(&orig_set, set);
9369
Michal Vasko004d3152020-06-11 19:59:22 +02009370 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009371 LY_CHECK_GOTO(rc, cleanup);
9372
9373 /* ('=' / '!=' RelationalExpr)* */
9374 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02009375 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009376
Michal Vasko004d3152020-06-11 19:59:22 +02009377 assert((exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_EQUAL) || (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_NEQUAL));
aPiecek8b0cc152021-05-31 16:40:31 +02009378 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02009379 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02009380 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009381
aPiecek8b0cc152021-05-31 16:40:31 +02009382 if (options & LYXP_SKIP_EXPR) {
9383 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009384 LY_CHECK_GOTO(rc, cleanup);
9385 continue;
9386 }
9387
9388 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02009389 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009390 LY_CHECK_GOTO(rc, cleanup);
Michal Vaskob7ba2c92024-01-05 15:17:53 +01009391 if (set2.not_found) {
9392 set->not_found = 1;
9393 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02009394
9395 /* eval */
9396 if (options & LYXP_SCNODE_ALL) {
9397 warn_operands(set->ctx, set, &set2, 0, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vasko004d3152020-06-11 19:59:22 +02009398 warn_equality_value(exp, set, *tok_idx - 1, this_op - 1, *tok_idx - 1);
9399 warn_equality_value(exp, &set2, this_op - 1, this_op - 1, *tok_idx - 1);
Michal Vaskoecd62de2019-11-13 12:35:11 +01009400 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02009401 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009402 } else {
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02009403 ly_bool result;
9404
9405 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], &result);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009406 LY_CHECK_GOTO(rc, cleanup);
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02009407 set_fill_boolean(set, result);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009408 }
9409 }
9410
9411cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02009412 lyxp_set_free_content(&orig_set);
9413 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009414 return rc;
9415}
9416
9417/**
9418 * @brief Evaluate AndExpr. Logs directly on error.
9419 *
Michal Vaskod3678892020-05-21 10:06:58 +02009420 * [14] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02009421 *
9422 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02009423 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009424 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02009425 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009426 * @param[in] options XPath options.
9427 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
9428 */
9429static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02009430eval_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 +02009431{
Michal Vaskob7ba2c92024-01-05 15:17:53 +01009432 LY_ERR rc = LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009433 struct lyxp_set orig_set, set2;
Michal Vaskodd528af2022-08-08 14:35:07 +02009434 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009435
9436 assert(repeat);
9437
9438 set_init(&orig_set, set);
9439 set_init(&set2, set);
9440
9441 set_fill_set(&orig_set, set);
9442
Michal Vasko004d3152020-06-11 19:59:22 +02009443 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009444 LY_CHECK_GOTO(rc, cleanup);
9445
Michal Vasko3ff82dd2023-05-09 14:09:21 +02009446 if (!(options & LYXP_SKIP_EXPR)) {
9447 if (options & LYXP_SCNODE_ALL) {
9448 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
9449 } else {
9450 /* cast to boolean, we know that will be the final result */
9451 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
9452 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02009453 }
9454
9455 /* ('and' EqualityExpr)* */
9456 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02009457 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG);
aPiecek8b0cc152021-05-31 16:40:31 +02009458 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, ((options & LYXP_SKIP_EXPR) || !set->val.bln ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02009459 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02009460 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009461
9462 /* lazy evaluation */
aPiecek8b0cc152021-05-31 16:40:31 +02009463 if ((options & LYXP_SKIP_EXPR) || ((set->type == LYXP_SET_BOOLEAN) && !set->val.bln)) {
9464 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009465 LY_CHECK_GOTO(rc, cleanup);
9466 continue;
9467 }
9468
9469 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02009470 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009471 LY_CHECK_GOTO(rc, cleanup);
Michal Vaskob7ba2c92024-01-05 15:17:53 +01009472 if (set2.not_found) {
9473 set->not_found = 1;
9474 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02009475
9476 /* eval - just get boolean value actually */
9477 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vasko1a09b212021-05-06 13:00:10 +02009478 set_scnode_clear_ctx(&set2, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskoecd62de2019-11-13 12:35:11 +01009479 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009480 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009481 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009482 set_fill_set(set, &set2);
9483 }
9484 }
9485
9486cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02009487 lyxp_set_free_content(&orig_set);
9488 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009489 return rc;
9490}
9491
9492/**
9493 * @brief Evaluate OrExpr. Logs directly on error.
9494 *
Michal Vaskod3678892020-05-21 10:06:58 +02009495 * [13] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02009496 *
9497 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02009498 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009499 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02009500 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009501 * @param[in] options XPath options.
9502 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
9503 */
9504static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02009505eval_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 +02009506{
Michal Vaskob7ba2c92024-01-05 15:17:53 +01009507 LY_ERR rc = LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009508 struct lyxp_set orig_set, set2;
Michal Vaskodd528af2022-08-08 14:35:07 +02009509 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009510
9511 assert(repeat);
9512
9513 set_init(&orig_set, set);
9514 set_init(&set2, set);
9515
9516 set_fill_set(&orig_set, set);
9517
Michal Vasko004d3152020-06-11 19:59:22 +02009518 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009519 LY_CHECK_GOTO(rc, cleanup);
9520
Michal Vasko3ff82dd2023-05-09 14:09:21 +02009521 if (!(options & LYXP_SKIP_EXPR)) {
9522 if (options & LYXP_SCNODE_ALL) {
9523 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
9524 } else {
9525 /* cast to boolean, we know that will be the final result */
9526 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
9527 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02009528 }
9529
9530 /* ('or' AndExpr)* */
9531 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02009532 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG);
aPiecek8b0cc152021-05-31 16:40:31 +02009533 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, ((options & LYXP_SKIP_EXPR) || set->val.bln ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02009534 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02009535 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009536
9537 /* lazy evaluation */
aPiecek8b0cc152021-05-31 16:40:31 +02009538 if ((options & LYXP_SKIP_EXPR) || ((set->type == LYXP_SET_BOOLEAN) && set->val.bln)) {
9539 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009540 LY_CHECK_GOTO(rc, cleanup);
9541 continue;
9542 }
9543
9544 set_fill_set(&set2, &orig_set);
9545 /* expr_type cound have been LYXP_EXPR_NONE in all these later calls (except for the first one),
9546 * but it does not matter */
Michal Vasko004d3152020-06-11 19:59:22 +02009547 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009548 LY_CHECK_GOTO(rc, cleanup);
Michal Vaskob7ba2c92024-01-05 15:17:53 +01009549 if (set2.not_found) {
9550 set->not_found = 1;
9551 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02009552
9553 /* eval - just get boolean value actually */
9554 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vasko1a09b212021-05-06 13:00:10 +02009555 set_scnode_clear_ctx(&set2, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskoecd62de2019-11-13 12:35:11 +01009556 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009557 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009558 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009559 set_fill_set(set, &set2);
9560 }
9561 }
9562
9563cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02009564 lyxp_set_free_content(&orig_set);
9565 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009566 return rc;
9567}
9568
9569/**
Michal Vasko004d3152020-06-11 19:59:22 +02009570 * @brief Decide what expression is at the pointer @p tok_idx and evaluate it accordingly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009571 *
9572 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02009573 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009574 * @param[in] etype Expression type to evaluate.
aPiecek8b0cc152021-05-31 16:40:31 +02009575 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009576 * @param[in] options XPath options.
9577 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
9578 */
9579static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02009580eval_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 +02009581 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02009582{
Michal Vaskodd528af2022-08-08 14:35:07 +02009583 uint32_t i, count;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009584 enum lyxp_expr_type next_etype;
9585 LY_ERR rc;
9586
9587 /* process operator repeats */
Michal Vasko004d3152020-06-11 19:59:22 +02009588 if (!exp->repeat[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02009589 next_etype = LYXP_EXPR_NONE;
9590 } else {
9591 /* find etype repeat */
Radek Krejci1e008d22020-08-17 11:37:37 +02009592 for (i = 0; exp->repeat[*tok_idx][i] > etype; ++i) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02009593
9594 /* select one-priority lower because etype expression called us */
9595 if (i) {
Michal Vasko004d3152020-06-11 19:59:22 +02009596 next_etype = exp->repeat[*tok_idx][i - 1];
Michal Vasko03ff5a72019-09-11 13:49:33 +02009597 /* count repeats for that expression */
Radek Krejci1e008d22020-08-17 11:37:37 +02009598 for (count = 0; i && exp->repeat[*tok_idx][i - 1] == next_etype; ++count, --i) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02009599 } else {
9600 next_etype = LYXP_EXPR_NONE;
9601 }
9602 }
9603
9604 /* decide what expression are we parsing based on the repeat */
9605 switch (next_etype) {
9606 case LYXP_EXPR_OR:
Michal Vasko004d3152020-06-11 19:59:22 +02009607 rc = eval_or_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009608 break;
9609 case LYXP_EXPR_AND:
Michal Vasko004d3152020-06-11 19:59:22 +02009610 rc = eval_and_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009611 break;
9612 case LYXP_EXPR_EQUALITY:
Michal Vasko004d3152020-06-11 19:59:22 +02009613 rc = eval_equality_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009614 break;
9615 case LYXP_EXPR_RELATIONAL:
Michal Vasko004d3152020-06-11 19:59:22 +02009616 rc = eval_relational_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009617 break;
9618 case LYXP_EXPR_ADDITIVE:
Michal Vasko004d3152020-06-11 19:59:22 +02009619 rc = eval_additive_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009620 break;
9621 case LYXP_EXPR_MULTIPLICATIVE:
Michal Vasko004d3152020-06-11 19:59:22 +02009622 rc = eval_multiplicative_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009623 break;
9624 case LYXP_EXPR_UNARY:
Michal Vasko004d3152020-06-11 19:59:22 +02009625 rc = eval_unary_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009626 break;
9627 case LYXP_EXPR_UNION:
Michal Vasko004d3152020-06-11 19:59:22 +02009628 rc = eval_union_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009629 break;
9630 case LYXP_EXPR_NONE:
Michal Vasko004d3152020-06-11 19:59:22 +02009631 rc = eval_path_expr(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009632 break;
9633 default:
9634 LOGINT_RET(set->ctx);
9635 }
9636
9637 return rc;
9638}
9639
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009640/**
9641 * @brief Get root type.
9642 *
9643 * @param[in] ctx_node Context node.
9644 * @param[in] ctx_scnode Schema context node.
9645 * @param[in] options XPath options.
9646 * @return Root type.
9647 */
9648static enum lyxp_node_type
Radek Krejci1deb5be2020-08-26 16:43:36 +02009649lyxp_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 +01009650{
Michal Vasko6b26e742020-07-17 15:02:10 +02009651 const struct lysc_node *op;
9652
Michal Vaskoa27245c2022-05-02 09:01:35 +02009653 /* explicit */
9654 if (options & LYXP_ACCESS_TREE_ALL) {
9655 return LYXP_NODE_ROOT;
9656 } else if (options & LYXP_ACCESS_TREE_CONFIG) {
9657 return LYXP_NODE_ROOT_CONFIG;
9658 }
9659
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009660 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009661 /* schema */
Radek Krejci1e008d22020-08-17 11:37:37 +02009662 for (op = ctx_scnode; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent) {}
Michal Vasko6b26e742020-07-17 15:02:10 +02009663
Michal Vasko53575612023-12-15 16:09:11 +01009664 if (op || !(options & LYXP_SCNODE_SCHEMA)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009665 /* general root that can access everything */
9666 return LYXP_NODE_ROOT;
9667 } else if (!ctx_scnode || (ctx_scnode->flags & LYS_CONFIG_W)) {
9668 /* root context node can access only config data (because we said so, it is unspecified) */
9669 return LYXP_NODE_ROOT_CONFIG;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009670 }
Michal Vasko6b26e742020-07-17 15:02:10 +02009671 return LYXP_NODE_ROOT;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009672 }
9673
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009674 /* data */
Michal Vasko6b26e742020-07-17 15:02:10 +02009675 op = ctx_node ? ctx_node->schema : NULL;
Michal Vaskod989ba02020-08-24 10:59:24 +02009676 for ( ; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent) {}
Michal Vasko6b26e742020-07-17 15:02:10 +02009677
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009678 if (op || !(options & LYXP_SCHEMA)) {
9679 /* general root that can access everything */
9680 return LYXP_NODE_ROOT;
Christian Hoppsb6ecaea2021-02-06 09:45:38 -05009681 } else if (!ctx_node || !ctx_node->schema || (ctx_node->schema->flags & LYS_CONFIG_W)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009682 /* root context node can access only config data (because we said so, it is unspecified) */
9683 return LYXP_NODE_ROOT_CONFIG;
9684 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009685 return LYXP_NODE_ROOT;
9686}
9687
Michal Vasko03ff5a72019-09-11 13:49:33 +02009688LY_ERR
Michal Vasko400e9672021-01-11 13:39:17 +01009689lyxp_eval(const struct ly_ctx *ctx, const struct lyxp_expr *exp, const struct lys_module *cur_mod,
Michal Vaskoa3e92bc2022-07-29 14:56:23 +02009690 LY_VALUE_FORMAT format, void *prefix_data, const struct lyd_node *cur_node, const struct lyd_node *ctx_node,
9691 const struct lyd_node *tree, const struct lyxp_var *vars, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02009692{
Michal Vaskodd528af2022-08-08 14:35:07 +02009693 uint32_t tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009694 LY_ERR rc;
9695
Michal Vasko400e9672021-01-11 13:39:17 +01009696 LY_CHECK_ARG_RET(ctx, ctx, exp, set, LY_EINVAL);
Radek Krejci8df109d2021-04-23 12:19:08 +02009697 if (!cur_mod && ((format == LY_VALUE_SCHEMA) || (format == LY_VALUE_SCHEMA_RESOLVED))) {
Michal Vaskoddd76592022-01-17 13:34:48 +01009698 LOGERR(ctx, LY_EINVAL, "Current module must be set if schema format is used.");
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009699 return LY_EINVAL;
Michal Vasko004d3152020-06-11 19:59:22 +02009700 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02009701
Michal Vaskod3bb12f2020-12-04 14:33:09 +01009702 if (tree) {
9703 /* adjust the pointer to be the first top-level sibling */
9704 while (tree->parent) {
9705 tree = lyd_parent(tree);
9706 }
9707 tree = lyd_first_sibling(tree);
Michal Vaskoddd76592022-01-17 13:34:48 +01009708
Michal Vasko6f78a772022-11-10 08:13:52 +01009709 if (lysc_data_parent(tree->schema)) {
Michal Vaskoddd76592022-01-17 13:34:48 +01009710 /* unable to evaluate absolute paths */
9711 LOGERR(ctx, LY_EINVAL, "Data node \"%s\" has no parent but is not instance of a top-level schema node.",
9712 LYD_NAME(tree));
9713 return LY_EINVAL;
9714 }
Michal Vaskod3bb12f2020-12-04 14:33:09 +01009715 }
9716
Michal Vasko03ff5a72019-09-11 13:49:33 +02009717 /* prepare set for evaluation */
Michal Vasko03ff5a72019-09-11 13:49:33 +02009718 memset(set, 0, sizeof *set);
Michal Vaskod3678892020-05-21 10:06:58 +02009719 set->type = LYXP_SET_NODE_SET;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009720 set->root_type = lyxp_get_root_type(ctx_node, NULL, options);
9721 set_insert_node(set, (struct lyd_node *)ctx_node, 0, ctx_node ? LYXP_NODE_ELEM : set->root_type, 0);
9722
Michal Vasko400e9672021-01-11 13:39:17 +01009723 set->ctx = (struct ly_ctx *)ctx;
Michal Vaskoa3e92bc2022-07-29 14:56:23 +02009724 set->cur_node = cur_node;
9725 for (set->context_op = cur_node ? cur_node->schema : NULL;
Radek Krejci0f969882020-08-21 16:56:47 +02009726 set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF));
9727 set->context_op = set->context_op->parent) {}
Michal Vaskof03ed032020-03-04 13:31:44 +01009728 set->tree = tree;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009729 set->cur_mod = cur_mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009730 set->format = format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009731 set->prefix_data = prefix_data;
aPiecekfba75362021-10-07 12:39:48 +02009732 set->vars = vars;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009733
Radek Krejciddace2c2021-01-08 11:30:56 +01009734 LOG_LOCSET(NULL, set->cur_node, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01009735
Michal Vasko03ff5a72019-09-11 13:49:33 +02009736 /* evaluate */
Michal Vasko004d3152020-06-11 19:59:22 +02009737 rc = eval_expr_select(exp, &tok_idx, 0, set, options);
Michal Vaskob7ba2c92024-01-05 15:17:53 +01009738 if (!rc && set->not_found) {
9739 rc = LY_ENOTFOUND;
9740 }
9741 if (rc) {
Michal Vaskod3678892020-05-21 10:06:58 +02009742 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009743 }
9744
Michal Vasko4a7d4d62021-12-13 17:05:06 +01009745 if (set->cur_node) {
9746 LOG_LOCBACK(0, 1, 0, 0);
9747 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02009748 return rc;
9749}
9750
9751#if 0
9752
9753/* full xml printing of set elements, not used currently */
9754
9755void
9756lyxp_set_print_xml(FILE *f, struct lyxp_set *set)
9757{
9758 uint32_t i;
9759 char *str_num;
9760 struct lyout out;
9761
9762 memset(&out, 0, sizeof out);
9763
9764 out.type = LYOUT_STREAM;
9765 out.method.f = f;
9766
9767 switch (set->type) {
9768 case LYXP_SET_EMPTY:
Michal Vasko5233e962020-08-14 14:26:20 +02009769 ly_print_(&out, "Empty XPath set\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02009770 break;
9771 case LYXP_SET_BOOLEAN:
Michal Vasko5233e962020-08-14 14:26:20 +02009772 ly_print_(&out, "Boolean XPath set:\n");
9773 ly_print_(&out, "%s\n\n", set->value.bool ? "true" : "false");
Michal Vasko03ff5a72019-09-11 13:49:33 +02009774 break;
9775 case LYXP_SET_STRING:
Michal Vasko5233e962020-08-14 14:26:20 +02009776 ly_print_(&out, "String XPath set:\n");
9777 ly_print_(&out, "\"%s\"\n\n", set->value.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009778 break;
9779 case LYXP_SET_NUMBER:
Michal Vasko5233e962020-08-14 14:26:20 +02009780 ly_print_(&out, "Number XPath set:\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02009781
9782 if (isnan(set->value.num)) {
9783 str_num = strdup("NaN");
9784 } else if ((set->value.num == 0) || (set->value.num == -0.0f)) {
9785 str_num = strdup("0");
9786 } else if (isinf(set->value.num) && !signbit(set->value.num)) {
9787 str_num = strdup("Infinity");
9788 } else if (isinf(set->value.num) && signbit(set->value.num)) {
9789 str_num = strdup("-Infinity");
9790 } else if ((long long)set->value.num == set->value.num) {
9791 if (asprintf(&str_num, "%lld", (long long)set->value.num) == -1) {
9792 str_num = NULL;
9793 }
9794 } else {
9795 if (asprintf(&str_num, "%03.1Lf", set->value.num) == -1) {
9796 str_num = NULL;
9797 }
9798 }
9799 if (!str_num) {
9800 LOGMEM;
9801 return;
9802 }
Michal Vasko5233e962020-08-14 14:26:20 +02009803 ly_print_(&out, "%s\n\n", str_num);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009804 free(str_num);
9805 break;
9806 case LYXP_SET_NODE_SET:
Michal Vasko5233e962020-08-14 14:26:20 +02009807 ly_print_(&out, "Node XPath set:\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02009808
9809 for (i = 0; i < set->used; ++i) {
Michal Vasko5233e962020-08-14 14:26:20 +02009810 ly_print_(&out, "%d. ", i + 1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009811 switch (set->node_type[i]) {
9812 case LYXP_NODE_ROOT_ALL:
Michal Vasko5233e962020-08-14 14:26:20 +02009813 ly_print_(&out, "ROOT all\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02009814 break;
9815 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko5233e962020-08-14 14:26:20 +02009816 ly_print_(&out, "ROOT config\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02009817 break;
9818 case LYXP_NODE_ROOT_STATE:
Michal Vasko5233e962020-08-14 14:26:20 +02009819 ly_print_(&out, "ROOT state\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02009820 break;
9821 case LYXP_NODE_ROOT_NOTIF:
Michal Vasko5233e962020-08-14 14:26:20 +02009822 ly_print_(&out, "ROOT notification \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009823 break;
9824 case LYXP_NODE_ROOT_RPC:
Michal Vasko5233e962020-08-14 14:26:20 +02009825 ly_print_(&out, "ROOT rpc \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009826 break;
9827 case LYXP_NODE_ROOT_OUTPUT:
Michal Vasko5233e962020-08-14 14:26:20 +02009828 ly_print_(&out, "ROOT output \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009829 break;
9830 case LYXP_NODE_ELEM:
Michal Vasko5233e962020-08-14 14:26:20 +02009831 ly_print_(&out, "ELEM \"%s\"\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009832 xml_print_node(&out, 1, set->value.nodes[i], 1, LYP_FORMAT);
Michal Vasko5233e962020-08-14 14:26:20 +02009833 ly_print_(&out, "\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02009834 break;
9835 case LYXP_NODE_TEXT:
Michal Vasko5233e962020-08-14 14:26:20 +02009836 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 +02009837 break;
9838 case LYXP_NODE_ATTR:
Michal Vasko5233e962020-08-14 14:26:20 +02009839 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 +02009840 break;
9841 }
9842 }
9843 break;
9844 }
9845}
9846
9847#endif
9848
9849LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009850lyxp_set_cast(struct lyxp_set *set, enum lyxp_set_type target)
Michal Vasko03ff5a72019-09-11 13:49:33 +02009851{
9852 long double num;
9853 char *str;
9854 LY_ERR rc;
9855
9856 if (!set || (set->type == target)) {
9857 return LY_SUCCESS;
9858 }
9859
9860 /* it's not possible to convert anything into a node set */
Michal Vaskod3678892020-05-21 10:06:58 +02009861 assert(target != LYXP_SET_NODE_SET);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009862
9863 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vaskod3678892020-05-21 10:06:58 +02009864 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009865 return LY_EINVAL;
9866 }
9867
9868 /* to STRING */
Michal Vaskod3678892020-05-21 10:06:58 +02009869 if ((target == LYXP_SET_STRING) || ((target == LYXP_SET_NUMBER) && (set->type == LYXP_SET_NODE_SET))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02009870 switch (set->type) {
9871 case LYXP_SET_NUMBER:
9872 if (isnan(set->val.num)) {
9873 set->val.str = strdup("NaN");
9874 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
9875 } else if ((set->val.num == 0) || (set->val.num == -0.0f)) {
9876 set->val.str = strdup("0");
9877 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
9878 } else if (isinf(set->val.num) && !signbit(set->val.num)) {
9879 set->val.str = strdup("Infinity");
9880 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
9881 } else if (isinf(set->val.num) && signbit(set->val.num)) {
9882 set->val.str = strdup("-Infinity");
9883 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
9884 } else if ((long long)set->val.num == set->val.num) {
9885 if (asprintf(&str, "%lld", (long long)set->val.num) == -1) {
9886 LOGMEM_RET(set->ctx);
9887 }
9888 set->val.str = str;
9889 } else {
9890 if (asprintf(&str, "%03.1Lf", set->val.num) == -1) {
9891 LOGMEM_RET(set->ctx);
9892 }
9893 set->val.str = str;
9894 }
9895 break;
9896 case LYXP_SET_BOOLEAN:
Michal Vasko004d3152020-06-11 19:59:22 +02009897 if (set->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02009898 set->val.str = strdup("true");
9899 } else {
9900 set->val.str = strdup("false");
9901 }
9902 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), LY_EMEM);
9903 break;
9904 case LYXP_SET_NODE_SET:
Michal Vasko03ff5a72019-09-11 13:49:33 +02009905 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009906 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02009907
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009908 rc = cast_node_set_to_string(set, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009909 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02009910 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009911 set->val.str = str;
9912 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009913 default:
9914 LOGINT_RET(set->ctx);
9915 }
9916 set->type = LYXP_SET_STRING;
9917 }
9918
9919 /* to NUMBER */
9920 if (target == LYXP_SET_NUMBER) {
9921 switch (set->type) {
9922 case LYXP_SET_STRING:
9923 num = cast_string_to_number(set->val.str);
Michal Vaskod3678892020-05-21 10:06:58 +02009924 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009925 set->val.num = num;
9926 break;
9927 case LYXP_SET_BOOLEAN:
Michal Vasko004d3152020-06-11 19:59:22 +02009928 if (set->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02009929 set->val.num = 1;
9930 } else {
9931 set->val.num = 0;
9932 }
9933 break;
9934 default:
9935 LOGINT_RET(set->ctx);
9936 }
9937 set->type = LYXP_SET_NUMBER;
9938 }
9939
9940 /* to BOOLEAN */
9941 if (target == LYXP_SET_BOOLEAN) {
9942 switch (set->type) {
9943 case LYXP_SET_NUMBER:
9944 if ((set->val.num == 0) || (set->val.num == -0.0f) || isnan(set->val.num)) {
Michal Vasko004d3152020-06-11 19:59:22 +02009945 set->val.bln = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009946 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02009947 set->val.bln = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009948 }
9949 break;
9950 case LYXP_SET_STRING:
9951 if (set->val.str[0]) {
Michal Vaskod3678892020-05-21 10:06:58 +02009952 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02009953 set->val.bln = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009954 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02009955 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02009956 set->val.bln = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009957 }
9958 break;
9959 case LYXP_SET_NODE_SET:
Michal Vaskod3678892020-05-21 10:06:58 +02009960 if (set->used) {
9961 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02009962 set->val.bln = 1;
Michal Vaskod3678892020-05-21 10:06:58 +02009963 } else {
9964 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02009965 set->val.bln = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02009966 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02009967 break;
9968 default:
9969 LOGINT_RET(set->ctx);
9970 }
9971 set->type = LYXP_SET_BOOLEAN;
9972 }
9973
Michal Vasko03ff5a72019-09-11 13:49:33 +02009974 return LY_SUCCESS;
9975}
9976
9977LY_ERR
Michal Vasko400e9672021-01-11 13:39:17 +01009978lyxp_atomize(const struct ly_ctx *ctx, const struct lyxp_expr *exp, const struct lys_module *cur_mod,
Michal Vaskoa3e92bc2022-07-29 14:56:23 +02009979 LY_VALUE_FORMAT format, void *prefix_data, const struct lysc_node *cur_scnode,
9980 const struct lysc_node *ctx_scnode, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02009981{
Michal Vaskob7ba2c92024-01-05 15:17:53 +01009982 LY_ERR rc;
Michal Vaskodd528af2022-08-08 14:35:07 +02009983 uint32_t tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009984
Michal Vasko400e9672021-01-11 13:39:17 +01009985 LY_CHECK_ARG_RET(ctx, ctx, exp, set, LY_EINVAL);
Radek Krejci8df109d2021-04-23 12:19:08 +02009986 if (!cur_mod && ((format == LY_VALUE_SCHEMA) || (format == LY_VALUE_SCHEMA_RESOLVED))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009987 LOGARG(NULL, "Current module must be set if schema format is used.");
9988 return LY_EINVAL;
Michal Vasko004d3152020-06-11 19:59:22 +02009989 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02009990
9991 /* prepare set for evaluation */
Michal Vasko03ff5a72019-09-11 13:49:33 +02009992 memset(set, 0, sizeof *set);
9993 set->type = LYXP_SET_SCNODE_SET;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009994 set->root_type = lyxp_get_root_type(NULL, ctx_scnode, options);
Michal Vaskoe4a6d012023-05-22 14:34:52 +02009995 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 +01009996 set->val.scnodes[0].in_ctx = LYXP_SET_SCNODE_START;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009997
Michal Vasko400e9672021-01-11 13:39:17 +01009998 set->ctx = (struct ly_ctx *)ctx;
Michal Vaskoa3e92bc2022-07-29 14:56:23 +02009999 set->cur_scnode = cur_scnode;
10000 for (set->context_op = cur_scnode;
Radek Krejci0f969882020-08-21 16:56:47 +020010001 set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF));
10002 set->context_op = set->context_op->parent) {}
Michal Vasko5d24f6c2020-10-13 13:49:06 +020010003 set->cur_mod = cur_mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +020010004 set->format = format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +020010005 set->prefix_data = prefix_data;
Michal Vasko03ff5a72019-09-11 13:49:33 +020010006
Radek Krejciddace2c2021-01-08 11:30:56 +010010007 LOG_LOCSET(set->cur_scnode, NULL, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +010010008
Michal Vasko03ff5a72019-09-11 13:49:33 +020010009 /* evaluate */
Michal Vaskob7ba2c92024-01-05 15:17:53 +010010010 rc = eval_expr_select(exp, &tok_idx, 0, set, options);
10011 if (!rc && set->not_found) {
10012 rc = LY_ENOTFOUND;
10013 }
Radek Krejci2efc45b2020-12-22 16:25:44 +010010014
Michal Vaskof69e4552022-12-02 10:27:42 +010010015 LOG_LOCBACK(set->cur_scnode ? 1 : 0, 0, 0, 0);
Michal Vaskob7ba2c92024-01-05 15:17:53 +010010016 return rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +020010017}
Michal Vaskod43d71a2020-08-07 14:54:58 +020010018
Jan Kundrátc53a7ec2021-12-09 16:01:19 +010010019LIBYANG_API_DEF const char *
Michal Vaskod43d71a2020-08-07 14:54:58 +020010020lyxp_get_expr(const struct lyxp_expr *path)
10021{
10022 if (!path) {
10023 return NULL;
10024 }
10025
10026 return path->expr;
10027}