blob: 259fd9c0fa15616fccf61a73b6662dc8b7642ed6 [file] [log] [blame]
Radek Krejcib1646a92018-11-02 16:08:26 +01001/**
2 * @file xpath.c
3 * @author Michal Vasko <mvasko@cesnet.cz>
4 * @brief YANG XPath evaluation functions
5 *
Michal Vasko49fec8e2022-05-24 10:28:33 +02006 * Copyright (c) 2015 - 2022 CESNET, z.s.p.o.
Radek Krejcib1646a92018-11-02 16:08:26 +01007 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
Christian Hopps32874e12021-05-01 09:43:54 -040014#define _GNU_SOURCE /* asprintf, strdup */
Radek Krejcib1646a92018-11-02 16:08:26 +010015
Radek Krejci535ea9f2020-05-29 16:01:05 +020016#include "xpath.h"
Radek Krejcib1646a92018-11-02 16:08:26 +010017
Radek Krejci535ea9f2020-05-29 16:01:05 +020018#include <assert.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010019#include <ctype.h>
Radek Krejci535ea9f2020-05-29 16:01:05 +020020#include <errno.h>
Radek Krejci535ea9f2020-05-29 16:01:05 +020021#include <math.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020022#include <stdint.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010023#include <stdio.h>
24#include <stdlib.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010025#include <string.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010026
Radek Krejci535ea9f2020-05-29 16:01:05 +020027#include "common.h"
Michal Vasko5aa44c02020-06-29 11:47:02 +020028#include "compat.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020029#include "context.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020030#include "dict.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020031#include "hash_table.h"
Radek Krejci47fab892020-11-05 17:02:41 +010032#include "out.h"
Radek Krejci7931b192020-06-25 17:05:03 +020033#include "parser_data.h"
Michal Vasko004d3152020-06-11 19:59:22 +020034#include "path.h"
Michal Vaskob4750962022-10-06 15:33:35 +020035#include "plugins_exts/metadata.h"
Michal Vasko03ff5a72019-09-11 13:49:33 +020036#include "plugins_types.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020037#include "printer_data.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020038#include "schema_compile_node.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020039#include "tree.h"
Radek Krejci77114102021-03-10 15:21:57 +010040#include "tree_data.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020041#include "tree_data_internal.h"
Radek Krejci859a15a2021-03-05 20:56:59 +010042#include "tree_edit.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020043#include "tree_schema_internal.h"
44#include "xml.h"
Michal Vasko03ff5a72019-09-11 13:49:33 +020045
Michal Vaskodd528af2022-08-08 14:35:07 +020046static LY_ERR reparse_or_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint32_t *tok_idx, uint32_t depth);
47static LY_ERR eval_expr_select(const struct lyxp_expr *exp, uint32_t *tok_idx, enum lyxp_expr_type etype,
Michal Vasko40308e72020-10-20 16:38:40 +020048 struct lyxp_set *set, uint32_t options);
Michal Vaskofe1af042022-07-29 14:58:59 +020049static LY_ERR moveto_resolve_model(const char **qname, uint32_t *qname_len, const struct lyxp_set *set,
Michal Vasko93923692021-05-07 15:28:02 +020050 const struct lysc_node *ctx_scnode, const struct lys_module **moveto_mod);
Michal Vasko47da6562022-07-14 15:43:15 +020051static LY_ERR moveto_axis_node_next(const struct lyd_node **iter, enum lyxp_node_type *iter_type,
52 const struct lyd_node *node, enum lyxp_node_type node_type, enum lyxp_axis axis, struct lyxp_set *set);
Michal Vasko49fec8e2022-05-24 10:28:33 +020053static LY_ERR moveto_node(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname,
54 enum lyxp_axis axis, uint32_t options);
55static LY_ERR moveto_scnode(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname,
56 enum lyxp_axis axis, uint32_t options);
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +020057static LY_ERR moveto_op_comp(struct lyxp_set *set1, struct lyxp_set *set2, const char *op, ly_bool *result);
Michal Vasko03ff5a72019-09-11 13:49:33 +020058
aPiecek96dc1e32021-10-08 15:45:59 +020059/* Functions are divided into the following basic classes:
60 *
61 * (re)parse functions:
62 * Parse functions parse the expression into
63 * tokens (syntactic analysis).
64 * Reparse functions perform semantic analysis
65 * (do not save the result, just a check) of
66 * the expression and fill repeat indices.
67 *
68 * warn functions:
69 * Warn functions check specific reasonable conditions for schema XPath
70 * and print a warning if they are not satisfied.
71 *
72 * moveto functions:
73 * They and only they actually change the context (set).
74 *
75 * eval functions:
76 * They execute a parsed XPath expression on some data subtree.
77 */
78
Michal Vasko03ff5a72019-09-11 13:49:33 +020079/**
80 * @brief Print the type of an XPath \p set.
81 *
82 * @param[in] set Set to use.
83 * @return Set type string.
84 */
85static const char *
86print_set_type(struct lyxp_set *set)
87{
88 switch (set->type) {
Michal Vasko03ff5a72019-09-11 13:49:33 +020089 case LYXP_SET_NODE_SET:
90 return "node set";
91 case LYXP_SET_SCNODE_SET:
92 return "schema node set";
93 case LYXP_SET_BOOLEAN:
94 return "boolean";
95 case LYXP_SET_NUMBER:
96 return "number";
97 case LYXP_SET_STRING:
98 return "string";
99 }
100
101 return NULL;
102}
103
Michal Vasko24cddf82020-06-01 08:17:01 +0200104const char *
Michal Vasko49fec8e2022-05-24 10:28:33 +0200105lyxp_token2str(enum lyxp_token tok)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200106{
107 switch (tok) {
108 case LYXP_TOKEN_PAR1:
109 return "(";
110 case LYXP_TOKEN_PAR2:
111 return ")";
112 case LYXP_TOKEN_BRACK1:
113 return "[";
114 case LYXP_TOKEN_BRACK2:
115 return "]";
116 case LYXP_TOKEN_DOT:
117 return ".";
118 case LYXP_TOKEN_DDOT:
119 return "..";
120 case LYXP_TOKEN_AT:
121 return "@";
122 case LYXP_TOKEN_COMMA:
123 return ",";
Michal Vaskoe0a8ce72023-04-04 08:30:44 +0200124 case LYXP_TOKEN_DCOLON:
125 return "::";
Michal Vasko03ff5a72019-09-11 13:49:33 +0200126 case LYXP_TOKEN_NAMETEST:
127 return "NameTest";
128 case LYXP_TOKEN_NODETYPE:
129 return "NodeType";
aPiecekfba75362021-10-07 12:39:48 +0200130 case LYXP_TOKEN_VARREF:
131 return "VariableReference";
Michal Vasko03ff5a72019-09-11 13:49:33 +0200132 case LYXP_TOKEN_FUNCNAME:
133 return "FunctionName";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200134 case LYXP_TOKEN_OPER_LOG:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200135 return "Operator(Logic)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200136 case LYXP_TOKEN_OPER_EQUAL:
137 return "Operator(Equal)";
138 case LYXP_TOKEN_OPER_NEQUAL:
139 return "Operator(Non-equal)";
140 case LYXP_TOKEN_OPER_COMP:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200141 return "Operator(Comparison)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200142 case LYXP_TOKEN_OPER_MATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200143 return "Operator(Math)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200144 case LYXP_TOKEN_OPER_UNI:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200145 return "Operator(Union)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200146 case LYXP_TOKEN_OPER_PATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200147 return "Operator(Path)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200148 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko14676352020-05-29 11:35:55 +0200149 return "Operator(Recursive Path)";
Michal Vaskoe0a8ce72023-04-04 08:30:44 +0200150 case LYXP_TOKEN_AXISNAME:
151 return "AxisName";
Michal Vasko03ff5a72019-09-11 13:49:33 +0200152 case LYXP_TOKEN_LITERAL:
153 return "Literal";
154 case LYXP_TOKEN_NUMBER:
155 return "Number";
156 default:
157 LOGINT(NULL);
158 return "";
159 }
160}
161
162/**
Michal Vasko49fec8e2022-05-24 10:28:33 +0200163 * @brief Transform string into an axis.
164 *
165 * @param[in] str String to transform.
166 * @param[in] str_len Length of @p str.
167 * @return Transformed axis.
168 */
169static enum lyxp_axis
Michal Vaskodd528af2022-08-08 14:35:07 +0200170str2axis(const char *str, uint32_t str_len)
Michal Vasko49fec8e2022-05-24 10:28:33 +0200171{
172 switch (str_len) {
173 case 4:
174 assert(!strncmp("self", str, str_len));
175 return LYXP_AXIS_SELF;
176 case 5:
177 assert(!strncmp("child", str, str_len));
178 return LYXP_AXIS_CHILD;
179 case 6:
180 assert(!strncmp("parent", str, str_len));
181 return LYXP_AXIS_PARENT;
182 case 8:
183 assert(!strncmp("ancestor", str, str_len));
184 return LYXP_AXIS_ANCESTOR;
185 case 9:
186 if (str[0] == 'a') {
187 assert(!strncmp("attribute", str, str_len));
188 return LYXP_AXIS_ATTRIBUTE;
189 } else if (str[0] == 'f') {
190 assert(!strncmp("following", str, str_len));
191 return LYXP_AXIS_FOLLOWING;
192 } else {
193 assert(!strncmp("preceding", str, str_len));
194 return LYXP_AXIS_PRECEDING;
195 }
196 break;
197 case 10:
198 assert(!strncmp("descendant", str, str_len));
199 return LYXP_AXIS_DESCENDANT;
200 case 16:
201 assert(!strncmp("ancestor-or-self", str, str_len));
202 return LYXP_AXIS_ANCESTOR_OR_SELF;
203 case 17:
204 if (str[0] == 'f') {
205 assert(!strncmp("following-sibling", str, str_len));
206 return LYXP_AXIS_FOLLOWING_SIBLING;
207 } else {
208 assert(!strncmp("preceding-sibling", str, str_len));
209 return LYXP_AXIS_PRECEDING_SIBLING;
210 }
211 break;
212 case 18:
213 assert(!strncmp("descendant-or-self", str, str_len));
214 return LYXP_AXIS_DESCENDANT_OR_SELF;
215 }
216
217 LOGINT(NULL);
218 return 0;
219}
220
221/**
Michal Vasko03ff5a72019-09-11 13:49:33 +0200222 * @brief Print the whole expression \p exp to debug output.
223 *
224 * @param[in] exp Expression to use.
225 */
226static void
Michal Vasko40308e72020-10-20 16:38:40 +0200227print_expr_struct_debug(const struct lyxp_expr *exp)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200228{
Radek Krejcif13b87b2020-12-01 22:02:17 +0100229#define MSG_BUFFER_SIZE 128
230 char tmp[MSG_BUFFER_SIZE];
Michal Vasko1fdd8fa2021-01-08 09:21:45 +0100231 uint32_t i, j;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200232
Radek Krejci52b6d512020-10-12 12:33:17 +0200233 if (!exp || (ly_ll < LY_LLDBG)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200234 return;
235 }
236
237 LOGDBG(LY_LDGXPATH, "expression \"%s\":", exp->expr);
238 for (i = 0; i < exp->used; ++i) {
Michal Vasko49fec8e2022-05-24 10:28:33 +0200239 sprintf(tmp, "\ttoken %s, in expression \"%.*s\"", lyxp_token2str(exp->tokens[i]), exp->tok_len[i],
Michal Vasko69730152020-10-09 16:30:07 +0200240 &exp->expr[exp->tok_pos[i]]);
Michal Vasko23049552021-03-04 15:57:44 +0100241 if (exp->repeat && exp->repeat[i]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200242 sprintf(tmp + strlen(tmp), " (repeat %d", exp->repeat[i][0]);
243 for (j = 1; exp->repeat[i][j]; ++j) {
244 sprintf(tmp + strlen(tmp), ", %d", exp->repeat[i][j]);
245 }
246 strcat(tmp, ")");
247 }
248 LOGDBG(LY_LDGXPATH, tmp);
249 }
Radek Krejcif13b87b2020-12-01 22:02:17 +0100250#undef MSG_BUFFER_SIZE
Michal Vasko03ff5a72019-09-11 13:49:33 +0200251}
252
253#ifndef NDEBUG
254
255/**
256 * @brief Print XPath set content to debug output.
257 *
258 * @param[in] set Set to print.
259 */
260static void
261print_set_debug(struct lyxp_set *set)
262{
263 uint32_t i;
264 char *str;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200265 struct lyxp_set_node *item;
266 struct lyxp_set_scnode *sitem;
267
Radek Krejci52b6d512020-10-12 12:33:17 +0200268 if (ly_ll < LY_LLDBG) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200269 return;
270 }
271
272 switch (set->type) {
273 case LYXP_SET_NODE_SET:
274 LOGDBG(LY_LDGXPATH, "set NODE SET:");
275 for (i = 0; i < set->used; ++i) {
276 item = &set->val.nodes[i];
277
278 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +0100279 case LYXP_NODE_NONE:
280 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): NONE", i + 1, item->pos);
281 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200282 case LYXP_NODE_ROOT:
283 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT", i + 1, item->pos);
284 break;
285 case LYXP_NODE_ROOT_CONFIG:
286 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT CONFIG", i + 1, item->pos);
287 break;
288 case LYXP_NODE_ELEM:
Michal Vasko9e685082021-01-29 14:49:09 +0100289 if ((item->node->schema->nodetype == LYS_LIST) && (lyd_child(item->node)->schema->nodetype == LYS_LEAF)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200290 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (1st child val: %s)", i + 1, item->pos,
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200291 item->node->schema->name, lyd_get_value(lyd_child(item->node)));
Michal Vasko9e685082021-01-29 14:49:09 +0100292 } else if (item->node->schema->nodetype == LYS_LEAFLIST) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200293 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (val: %s)", i + 1, item->pos,
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200294 item->node->schema->name, lyd_get_value(item->node));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200295 } else {
296 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s", i + 1, item->pos, item->node->schema->name);
297 }
298 break;
299 case LYXP_NODE_TEXT:
300 if (item->node->schema->nodetype & LYS_ANYDATA) {
301 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): TEXT <%s>", i + 1, item->pos,
Michal Vasko69730152020-10-09 16:30:07 +0200302 item->node->schema->nodetype == LYS_ANYXML ? "anyxml" : "anydata");
Michal Vasko03ff5a72019-09-11 13:49:33 +0200303 } else {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200304 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 +0200305 }
306 break;
Michal Vasko9f96a052020-03-10 09:41:45 +0100307 case LYXP_NODE_META:
308 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 +0200309 set->val.meta[i].meta->value);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200310 break;
311 }
312 }
313 break;
314
315 case LYXP_SET_SCNODE_SET:
316 LOGDBG(LY_LDGXPATH, "set SCNODE SET:");
317 for (i = 0; i < set->used; ++i) {
318 sitem = &set->val.scnodes[i];
319
320 switch (sitem->type) {
321 case LYXP_NODE_ROOT:
322 LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT", i + 1, sitem->in_ctx);
323 break;
324 case LYXP_NODE_ROOT_CONFIG:
325 LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT CONFIG", i + 1, sitem->in_ctx);
326 break;
327 case LYXP_NODE_ELEM:
328 LOGDBG(LY_LDGXPATH, "\t%d (%u): ELEM %s", i + 1, sitem->in_ctx, sitem->scnode->name);
329 break;
330 default:
331 LOGINT(NULL);
332 break;
333 }
334 }
335 break;
336
Michal Vasko03ff5a72019-09-11 13:49:33 +0200337 case LYXP_SET_BOOLEAN:
338 LOGDBG(LY_LDGXPATH, "set BOOLEAN");
Michal Vasko004d3152020-06-11 19:59:22 +0200339 LOGDBG(LY_LDGXPATH, "\t%s", (set->val.bln ? "true" : "false"));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200340 break;
341
342 case LYXP_SET_STRING:
343 LOGDBG(LY_LDGXPATH, "set STRING");
344 LOGDBG(LY_LDGXPATH, "\t%s", set->val.str);
345 break;
346
347 case LYXP_SET_NUMBER:
348 LOGDBG(LY_LDGXPATH, "set NUMBER");
349
350 if (isnan(set->val.num)) {
351 str = strdup("NaN");
352 } else if ((set->val.num == 0) || (set->val.num == -0.0f)) {
353 str = strdup("0");
354 } else if (isinf(set->val.num) && !signbit(set->val.num)) {
355 str = strdup("Infinity");
356 } else if (isinf(set->val.num) && signbit(set->val.num)) {
357 str = strdup("-Infinity");
358 } else if ((long long)set->val.num == set->val.num) {
359 if (asprintf(&str, "%lld", (long long)set->val.num) == -1) {
360 str = NULL;
361 }
362 } else {
363 if (asprintf(&str, "%03.1Lf", set->val.num) == -1) {
364 str = NULL;
365 }
366 }
367 LY_CHECK_ERR_RET(!str, LOGMEM(NULL), );
368
369 LOGDBG(LY_LDGXPATH, "\t%s", str);
370 free(str);
371 }
372}
373
374#endif
375
376/**
377 * @brief Realloc the string \p str.
378 *
379 * @param[in] ctx libyang context for logging.
380 * @param[in] needed How much free space is required.
381 * @param[in,out] str Pointer to the string to use.
382 * @param[in,out] used Used bytes in \p str.
383 * @param[in,out] size Allocated bytes in \p str.
384 * @return LY_ERR
385 */
386static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +0200387cast_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 +0200388{
Michal Vasko2291ab92022-08-08 08:53:12 +0200389 if (*size - (unsigned)*used < needed) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200390 do {
Michal Vaskodd528af2022-08-08 14:35:07 +0200391 if ((UINT32_MAX - *size) < LYXP_STRING_CAST_SIZE_STEP) {
392 LOGERR(ctx, LY_EINVAL, "XPath string length limit (%" PRIu32 ") reached.", UINT32_MAX);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200393 return LY_EINVAL;
394 }
395 *size += LYXP_STRING_CAST_SIZE_STEP;
Michal Vasko2291ab92022-08-08 08:53:12 +0200396 } while (*size - (unsigned)*used < needed);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200397 *str = ly_realloc(*str, *size * sizeof(char));
398 LY_CHECK_ERR_RET(!(*str), LOGMEM(ctx), LY_EMEM);
399 }
400
401 return LY_SUCCESS;
402}
403
404/**
405 * @brief Cast nodes recursively to one string @p str.
406 *
Michal Vasko47da6562022-07-14 15:43:15 +0200407 * @param[in] node Node to cast, NULL if root.
408 * @param[in] set XPath set.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200409 * @param[in] indent Current indent.
410 * @param[in,out] str Resulting string.
411 * @param[in,out] used Used bytes in @p str.
412 * @param[in,out] size Allocated bytes in @p str.
Michal Vasko47da6562022-07-14 15:43:15 +0200413 * @return LY_ERR value.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200414 */
415static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +0200416cast_string_recursive(const struct lyd_node *node, struct lyxp_set *set, uint32_t indent, char **str, uint32_t *used,
417 uint32_t *size)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200418{
Radek Krejci7f769d72020-07-11 23:13:56 +0200419 char *buf, *line, *ptr = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200420 const char *value_str;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200421 const struct lyd_node *child;
Michal Vasko47da6562022-07-14 15:43:15 +0200422 enum lyxp_node_type child_type;
Michal Vasko60ea6352020-06-29 13:39:39 +0200423 struct lyd_node *tree;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200424 struct lyd_node_any *any;
425 LY_ERR rc;
426
Michal Vasko47da6562022-07-14 15:43:15 +0200427 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && node && (node->schema->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200428 return LY_SUCCESS;
429 }
430
Michal Vasko47da6562022-07-14 15:43:15 +0200431 if (!node) {
432 /* fake container */
433 LY_CHECK_RET(cast_string_realloc(set->ctx, 1, str, used, size));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200434 strcpy(*str + (*used - 1), "\n");
435 ++(*used);
436
437 ++indent;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200438
Michal Vasko47da6562022-07-14 15:43:15 +0200439 /* print all the top-level nodes */
440 child = NULL;
441 child_type = 0;
442 while (!moveto_axis_node_next(&child, &child_type, NULL, set->root_type, LYXP_AXIS_CHILD, set)) {
443 LY_CHECK_RET(cast_string_recursive(child, set, indent, str, used, size));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200444 }
445
Michal Vasko47da6562022-07-14 15:43:15 +0200446 /* end fake container */
447 LY_CHECK_RET(cast_string_realloc(set->ctx, 1, str, used, size));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200448 strcpy(*str + (*used - 1), "\n");
449 ++(*used);
450
451 --indent;
Michal Vasko47da6562022-07-14 15:43:15 +0200452 } else {
453 switch (node->schema->nodetype) {
454 case LYS_CONTAINER:
455 case LYS_LIST:
456 case LYS_RPC:
457 case LYS_NOTIF:
458 LY_CHECK_RET(cast_string_realloc(set->ctx, 1, str, used, size));
459 strcpy(*str + (*used - 1), "\n");
460 ++(*used);
461
462 for (child = lyd_child(node); child; child = child->next) {
463 LY_CHECK_RET(cast_string_recursive(child, set, indent + 1, str, used, size));
464 }
465
466 break;
467
468 case LYS_LEAF:
469 case LYS_LEAFLIST:
470 value_str = lyd_get_value(node);
471
472 /* print indent */
473 LY_CHECK_RET(cast_string_realloc(set->ctx, indent * 2 + strlen(value_str) + 1, str, used, size));
474 memset(*str + (*used - 1), ' ', indent * 2);
475 *used += indent * 2;
476
477 /* print value */
478 if (*used == 1) {
479 sprintf(*str + (*used - 1), "%s", value_str);
480 *used += strlen(value_str);
481 } else {
482 sprintf(*str + (*used - 1), "%s\n", value_str);
483 *used += strlen(value_str) + 1;
484 }
485
486 break;
487
488 case LYS_ANYXML:
489 case LYS_ANYDATA:
490 any = (struct lyd_node_any *)node;
491 if (!(void *)any->value.tree) {
492 /* no content */
493 buf = strdup("");
494 LY_CHECK_ERR_RET(!buf, LOGMEM(set->ctx), LY_EMEM);
495 } else {
496 struct ly_out *out;
497
498 if (any->value_type == LYD_ANYDATA_LYB) {
499 /* try to parse it into a data tree */
500 if (lyd_parse_data_mem((struct ly_ctx *)set->ctx, any->value.mem, LYD_LYB,
501 LYD_PARSE_ONLY | LYD_PARSE_STRICT, 0, &tree) == LY_SUCCESS) {
502 /* successfully parsed */
503 free(any->value.mem);
504 any->value.tree = tree;
505 any->value_type = LYD_ANYDATA_DATATREE;
506 }
507 /* error is covered by the following switch where LYD_ANYDATA_LYB causes failure */
508 }
509
510 switch (any->value_type) {
511 case LYD_ANYDATA_STRING:
512 case LYD_ANYDATA_XML:
513 case LYD_ANYDATA_JSON:
514 buf = strdup(any->value.json);
515 LY_CHECK_ERR_RET(!buf, LOGMEM(set->ctx), LY_EMEM);
516 break;
517 case LYD_ANYDATA_DATATREE:
518 LY_CHECK_RET(ly_out_new_memory(&buf, 0, &out));
519 rc = lyd_print_all(out, any->value.tree, LYD_XML, 0);
520 ly_out_free(out, NULL, 0);
521 LY_CHECK_RET(rc < 0, -rc);
522 break;
523 case LYD_ANYDATA_LYB:
524 LOGERR(set->ctx, LY_EINVAL, "Cannot convert LYB anydata into string.");
525 return LY_EINVAL;
526 }
527 }
528
529 line = strtok_r(buf, "\n", &ptr);
530 do {
531 rc = cast_string_realloc(set->ctx, indent * 2 + strlen(line) + 1, str, used, size);
532 if (rc != LY_SUCCESS) {
533 free(buf);
534 return rc;
535 }
536 memset(*str + (*used - 1), ' ', indent * 2);
537 *used += indent * 2;
538
539 strcpy(*str + (*used - 1), line);
540 *used += strlen(line);
541
542 strcpy(*str + (*used - 1), "\n");
543 *used += 1;
544 } while ((line = strtok_r(NULL, "\n", &ptr)));
545
546 free(buf);
547 break;
548
549 default:
550 LOGINT_RET(set->ctx);
551 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200552 }
553
554 return LY_SUCCESS;
555}
556
557/**
558 * @brief Cast an element into a string.
559 *
Michal Vasko47da6562022-07-14 15:43:15 +0200560 * @param[in] node Node to cast, NULL if root.
561 * @param[in] set XPath set.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200562 * @param[out] str Element cast to dynamically-allocated string.
563 * @return LY_ERR
564 */
565static LY_ERR
Michal Vasko47da6562022-07-14 15:43:15 +0200566cast_string_elem(const struct lyd_node *node, struct lyxp_set *set, char **str)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200567{
Michal Vaskodd528af2022-08-08 14:35:07 +0200568 uint32_t used, size;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200569 LY_ERR rc;
570
571 *str = malloc(LYXP_STRING_CAST_SIZE_START * sizeof(char));
Michal Vasko47da6562022-07-14 15:43:15 +0200572 LY_CHECK_ERR_RET(!*str, LOGMEM(set->ctx), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200573 (*str)[0] = '\0';
574 used = 1;
575 size = LYXP_STRING_CAST_SIZE_START;
576
Michal Vasko47da6562022-07-14 15:43:15 +0200577 rc = cast_string_recursive(node, set, 0, str, &used, &size);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200578 if (rc != LY_SUCCESS) {
579 free(*str);
580 return rc;
581 }
582
583 if (size > used) {
584 *str = ly_realloc(*str, used * sizeof(char));
Michal Vasko47da6562022-07-14 15:43:15 +0200585 LY_CHECK_ERR_RET(!*str, LOGMEM(set->ctx), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200586 }
587 return LY_SUCCESS;
588}
589
590/**
591 * @brief Cast a LYXP_SET_NODE_SET set into a string.
592 * Context position aware.
593 *
594 * @param[in] set Set to cast.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200595 * @param[out] str Cast dynamically-allocated string.
596 * @return LY_ERR
597 */
598static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100599cast_node_set_to_string(struct lyxp_set *set, char **str)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200600{
Michal Vaskoaa677062021-03-09 13:52:53 +0100601 if (!set->used) {
602 *str = strdup("");
603 if (!*str) {
604 LOGMEM_RET(set->ctx);
605 }
606 return LY_SUCCESS;
607 }
608
Michal Vasko03ff5a72019-09-11 13:49:33 +0200609 switch (set->val.nodes[0].type) {
Michal Vasko2caefc12019-11-14 16:07:56 +0100610 case LYXP_NODE_NONE:
611 /* invalid */
612 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200613 case LYXP_NODE_ROOT:
614 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200615 case LYXP_NODE_ELEM:
616 case LYXP_NODE_TEXT:
Michal Vasko47da6562022-07-14 15:43:15 +0200617 return cast_string_elem(set->val.nodes[0].node, set, str);
Michal Vasko9f96a052020-03-10 09:41:45 +0100618 case LYXP_NODE_META:
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200619 *str = strdup(lyd_get_meta_value(set->val.meta[0].meta));
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200620 if (!*str) {
621 LOGMEM_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200622 }
623 return LY_SUCCESS;
624 }
625
626 LOGINT_RET(set->ctx);
627}
628
629/**
630 * @brief Cast a string into an XPath number.
631 *
632 * @param[in] str String to use.
633 * @return Cast number.
634 */
635static long double
636cast_string_to_number(const char *str)
637{
638 long double num;
639 char *ptr;
640
641 errno = 0;
642 num = strtold(str, &ptr);
643 if (errno || *ptr) {
644 num = NAN;
645 }
646 return num;
647}
648
649/**
650 * @brief Callback for checking value equality.
651 *
Michal Vasko62524a92021-02-26 10:08:50 +0100652 * Implementation of ::lyht_value_equal_cb.
Radek Krejci857189e2020-09-01 13:26:36 +0200653 *
Michal Vasko03ff5a72019-09-11 13:49:33 +0200654 * @param[in] val1_p First value.
655 * @param[in] val2_p Second value.
656 * @param[in] mod Whether hash table is being modified.
657 * @param[in] cb_data Callback data.
Radek Krejci857189e2020-09-01 13:26:36 +0200658 * @return Boolean value whether values are equal or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200659 */
Radek Krejci857189e2020-09-01 13:26:36 +0200660static ly_bool
661set_values_equal_cb(void *val1_p, void *val2_p, ly_bool UNUSED(mod), void *UNUSED(cb_data))
Michal Vasko03ff5a72019-09-11 13:49:33 +0200662{
663 struct lyxp_set_hash_node *val1, *val2;
664
665 val1 = (struct lyxp_set_hash_node *)val1_p;
666 val2 = (struct lyxp_set_hash_node *)val2_p;
667
668 if ((val1->node == val2->node) && (val1->type == val2->type)) {
669 return 1;
670 }
671
672 return 0;
673}
674
675/**
676 * @brief Insert node and its hash into set.
677 *
678 * @param[in] set et to insert to.
679 * @param[in] node Node with hash.
680 * @param[in] type Node type.
681 */
682static void
683set_insert_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type)
684{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200685 LY_ERR r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200686 uint32_t i, hash;
687 struct lyxp_set_hash_node hnode;
688
689 if (!set->ht && (set->used >= LYD_HT_MIN_ITEMS)) {
690 /* create hash table and add all the nodes */
691 set->ht = lyht_new(1, sizeof(struct lyxp_set_hash_node), set_values_equal_cb, NULL, 1);
692 for (i = 0; i < set->used; ++i) {
693 hnode.node = set->val.nodes[i].node;
694 hnode.type = set->val.nodes[i].type;
695
Michal Vaskoae130f52023-04-20 14:25:16 +0200696 hash = lyht_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
697 hash = lyht_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
698 hash = lyht_hash_multi(hash, NULL, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200699
700 r = lyht_insert(set->ht, &hnode, hash, NULL);
701 assert(!r);
702 (void)r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200703
Michal Vasko513b87d2023-08-04 14:03:40 +0200704 if ((hnode.node == node) && (hnode.type == type)) {
Michal Vasko9d6befd2019-12-11 14:56:56 +0100705 /* it was just added, do not add it twice */
Michal Vasko513b87d2023-08-04 14:03:40 +0200706 return;
Michal Vasko9d6befd2019-12-11 14:56:56 +0100707 }
708 }
709 }
710
Michal Vasko513b87d2023-08-04 14:03:40 +0200711 if (set->ht) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200712 /* add the new node into hash table */
713 hnode.node = node;
714 hnode.type = type;
715
Michal Vaskoae130f52023-04-20 14:25:16 +0200716 hash = lyht_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
717 hash = lyht_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
718 hash = lyht_hash_multi(hash, NULL, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200719
720 r = lyht_insert(set->ht, &hnode, hash, NULL);
721 assert(!r);
722 (void)r;
723 }
724}
725
726/**
727 * @brief Remove node and its hash from set.
728 *
729 * @param[in] set Set to remove from.
730 * @param[in] node Node to remove.
731 * @param[in] type Node type.
732 */
733static void
734set_remove_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type)
735{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200736 LY_ERR r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200737 struct lyxp_set_hash_node hnode;
738 uint32_t hash;
739
740 if (set->ht) {
741 hnode.node = node;
742 hnode.type = type;
743
Michal Vaskoae130f52023-04-20 14:25:16 +0200744 hash = lyht_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
745 hash = lyht_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
746 hash = lyht_hash_multi(hash, NULL, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200747
748 r = lyht_remove(set->ht, &hnode, hash);
749 assert(!r);
750 (void)r;
751
752 if (!set->ht->used) {
Michal Vasko77b7f90a2023-01-31 15:42:41 +0100753 lyht_free(set->ht, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200754 set->ht = NULL;
755 }
756 }
757}
758
759/**
760 * @brief Check whether node is in set based on its hash.
761 *
762 * @param[in] set Set to search in.
763 * @param[in] node Node to search for.
764 * @param[in] type Node type.
765 * @param[in] skip_idx Index in @p set to skip.
766 * @return LY_ERR
767 */
768static LY_ERR
769set_dup_node_hash_check(const struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type, int skip_idx)
770{
771 struct lyxp_set_hash_node hnode, *match_p;
772 uint32_t hash;
773
774 hnode.node = node;
775 hnode.type = type;
776
Michal Vaskoae130f52023-04-20 14:25:16 +0200777 hash = lyht_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
778 hash = lyht_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
779 hash = lyht_hash_multi(hash, NULL, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200780
781 if (!lyht_find(set->ht, &hnode, hash, (void **)&match_p)) {
782 if ((skip_idx > -1) && (set->val.nodes[skip_idx].node == match_p->node) && (set->val.nodes[skip_idx].type == match_p->type)) {
783 /* we found it on the index that should be skipped, find another */
784 hnode = *match_p;
785 if (lyht_find_next(set->ht, &hnode, hash, (void **)&match_p)) {
786 /* none other found */
787 return LY_SUCCESS;
788 }
789 }
790
791 return LY_EEXIST;
792 }
793
794 /* not found */
795 return LY_SUCCESS;
796}
797
Michal Vaskod3678892020-05-21 10:06:58 +0200798void
799lyxp_set_free_content(struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200800{
801 if (!set) {
802 return;
803 }
804
805 if (set->type == LYXP_SET_NODE_SET) {
806 free(set->val.nodes);
Michal Vasko77b7f90a2023-01-31 15:42:41 +0100807 lyht_free(set->ht, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200808 } else if (set->type == LYXP_SET_SCNODE_SET) {
809 free(set->val.scnodes);
Michal Vasko77b7f90a2023-01-31 15:42:41 +0100810 lyht_free(set->ht, NULL);
Michal Vaskod3678892020-05-21 10:06:58 +0200811 } else {
812 if (set->type == LYXP_SET_STRING) {
813 free(set->val.str);
814 }
815 set->type = LYXP_SET_NODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200816 }
Michal Vaskod3678892020-05-21 10:06:58 +0200817
818 set->val.nodes = NULL;
819 set->used = 0;
820 set->size = 0;
821 set->ht = NULL;
822 set->ctx_pos = 0;
aPiecek748da732021-06-01 09:56:43 +0200823 set->ctx_size = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200824}
825
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100826/**
827 * @brief Free dynamically-allocated set.
828 *
829 * @param[in] set Set to free.
830 */
831static void
Michal Vasko03ff5a72019-09-11 13:49:33 +0200832lyxp_set_free(struct lyxp_set *set)
833{
834 if (!set) {
835 return;
836 }
837
Michal Vaskod3678892020-05-21 10:06:58 +0200838 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200839 free(set);
840}
841
842/**
843 * @brief Initialize set context.
844 *
845 * @param[in] new Set to initialize.
846 * @param[in] set Arbitrary initialized set.
847 */
848static void
Michal Vasko4c7763f2020-07-27 17:40:37 +0200849set_init(struct lyxp_set *new, const struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200850{
851 memset(new, 0, sizeof *new);
Michal Vasko02a77382019-09-12 11:47:35 +0200852 if (set) {
Michal Vasko306e2832022-07-25 09:15:17 +0200853 new->non_child_axis = set->non_child_axis;
Michal Vasko02a77382019-09-12 11:47:35 +0200854 new->ctx = set->ctx;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200855 new->cur_node = set->cur_node;
Michal Vasko588112f2019-12-10 14:51:53 +0100856 new->root_type = set->root_type;
Michal Vasko6b26e742020-07-17 15:02:10 +0200857 new->context_op = set->context_op;
Michal Vaskof03ed032020-03-04 13:31:44 +0100858 new->tree = set->tree;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200859 new->cur_mod = set->cur_mod;
Michal Vasko02a77382019-09-12 11:47:35 +0200860 new->format = set->format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200861 new->prefix_data = set->prefix_data;
aPiecekfba75362021-10-07 12:39:48 +0200862 new->vars = set->vars;
Michal Vasko02a77382019-09-12 11:47:35 +0200863 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200864}
865
866/**
867 * @brief Create a deep copy of a set.
868 *
869 * @param[in] set Set to copy.
870 * @return Copy of @p set.
871 */
872static struct lyxp_set *
873set_copy(struct lyxp_set *set)
874{
875 struct lyxp_set *ret;
Michal Vasko1fdd8fa2021-01-08 09:21:45 +0100876 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200877
878 if (!set) {
879 return NULL;
880 }
881
882 ret = malloc(sizeof *ret);
883 LY_CHECK_ERR_RET(!ret, LOGMEM(set->ctx), NULL);
884 set_init(ret, set);
885
886 if (set->type == LYXP_SET_SCNODE_SET) {
887 ret->type = set->type;
888
889 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100890 if ((set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) ||
891 (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +0200892 uint32_t idx;
Michal Vasko26bbb272022-08-02 14:54:33 +0200893
Michal Vaskoe4a6d012023-05-22 14:34:52 +0200894 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 +0200895 set->val.scnodes[i].axis, &idx), lyxp_set_free(ret), NULL);
Michal Vasko3f27c522020-01-06 08:37:49 +0100896 /* coverity seems to think scnodes can be NULL */
Radek Krejciaa6b53f2020-08-27 15:19:03 +0200897 if (!ret->val.scnodes) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200898 lyxp_set_free(ret);
899 return NULL;
900 }
Michal Vaskoba716542019-12-16 10:01:58 +0100901 ret->val.scnodes[idx].in_ctx = set->val.scnodes[i].in_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200902 }
903 }
904 } else if (set->type == LYXP_SET_NODE_SET) {
905 ret->type = set->type;
Michal Vasko08e9b112021-06-11 15:41:17 +0200906 if (set->used) {
907 ret->val.nodes = malloc(set->used * sizeof *ret->val.nodes);
908 LY_CHECK_ERR_RET(!ret->val.nodes, LOGMEM(set->ctx); free(ret), NULL);
909 memcpy(ret->val.nodes, set->val.nodes, set->used * sizeof *ret->val.nodes);
910 } else {
911 ret->val.nodes = NULL;
912 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200913
914 ret->used = ret->size = set->used;
915 ret->ctx_pos = set->ctx_pos;
916 ret->ctx_size = set->ctx_size;
Michal Vasko4a04e542021-02-01 08:58:30 +0100917 if (set->ht) {
918 ret->ht = lyht_dup(set->ht);
919 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200920 } else {
Radek Krejci0f969882020-08-21 16:56:47 +0200921 memcpy(ret, set, sizeof *ret);
922 if (set->type == LYXP_SET_STRING) {
923 ret->val.str = strdup(set->val.str);
924 LY_CHECK_ERR_RET(!ret->val.str, LOGMEM(set->ctx); free(ret), NULL);
925 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200926 }
927
928 return ret;
929}
930
931/**
932 * @brief Fill XPath set with a string. Any current data are disposed of.
933 *
934 * @param[in] set Set to fill.
935 * @param[in] string String to fill into \p set.
936 * @param[in] str_len Length of \p string. 0 is a valid value!
937 */
938static void
Michal Vaskodd528af2022-08-08 14:35:07 +0200939set_fill_string(struct lyxp_set *set, const char *string, uint32_t str_len)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200940{
Michal Vaskod3678892020-05-21 10:06:58 +0200941 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200942
943 set->type = LYXP_SET_STRING;
944 if ((str_len == 0) && (string[0] != '\0')) {
945 string = "";
946 }
947 set->val.str = strndup(string, str_len);
948}
949
950/**
951 * @brief Fill XPath set with a number. Any current data are disposed of.
952 *
953 * @param[in] set Set to fill.
954 * @param[in] number Number to fill into \p set.
955 */
956static void
957set_fill_number(struct lyxp_set *set, long double number)
958{
Michal Vaskod3678892020-05-21 10:06:58 +0200959 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200960
961 set->type = LYXP_SET_NUMBER;
962 set->val.num = number;
963}
964
965/**
966 * @brief Fill XPath set with a boolean. Any current data are disposed of.
967 *
968 * @param[in] set Set to fill.
969 * @param[in] boolean Boolean to fill into \p set.
970 */
971static void
Radek Krejci857189e2020-09-01 13:26:36 +0200972set_fill_boolean(struct lyxp_set *set, ly_bool boolean)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200973{
Michal Vaskod3678892020-05-21 10:06:58 +0200974 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200975
976 set->type = LYXP_SET_BOOLEAN;
Michal Vasko004d3152020-06-11 19:59:22 +0200977 set->val.bln = boolean;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200978}
979
980/**
981 * @brief Fill XPath set with the value from another set (deep assign).
982 * Any current data are disposed of.
983 *
984 * @param[in] trg Set to fill.
985 * @param[in] src Source set to copy into \p trg.
986 */
987static void
Michal Vasko4c7763f2020-07-27 17:40:37 +0200988set_fill_set(struct lyxp_set *trg, const struct lyxp_set *src)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200989{
990 if (!trg || !src) {
991 return;
992 }
993
Michal Vaskof06d19d2023-08-04 14:04:08 +0200994 lyxp_set_free_content(trg);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200995 set_init(trg, src);
996
997 if (src->type == LYXP_SET_SCNODE_SET) {
998 trg->type = LYXP_SET_SCNODE_SET;
999 trg->used = src->used;
1000 trg->size = src->used;
1001
Michal Vasko08e9b112021-06-11 15:41:17 +02001002 if (trg->size) {
1003 trg->val.scnodes = ly_realloc(trg->val.scnodes, trg->size * sizeof *trg->val.scnodes);
1004 LY_CHECK_ERR_RET(!trg->val.scnodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), );
1005 memcpy(trg->val.scnodes, src->val.scnodes, src->used * sizeof *src->val.scnodes);
1006 } else {
1007 trg->val.scnodes = NULL;
1008 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001009 } else if (src->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +02001010 set_fill_boolean(trg, src->val.bln);
Michal Vasko44f3d2c2020-08-24 09:49:38 +02001011 } else if (src->type == LYXP_SET_NUMBER) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001012 set_fill_number(trg, src->val.num);
1013 } else if (src->type == LYXP_SET_STRING) {
1014 set_fill_string(trg, src->val.str, strlen(src->val.str));
1015 } else {
1016 if (trg->type == LYXP_SET_NODE_SET) {
1017 free(trg->val.nodes);
1018 } else if (trg->type == LYXP_SET_STRING) {
1019 free(trg->val.str);
1020 }
1021
Michal Vaskod3678892020-05-21 10:06:58 +02001022 assert(src->type == LYXP_SET_NODE_SET);
1023
1024 trg->type = LYXP_SET_NODE_SET;
1025 trg->used = src->used;
1026 trg->size = src->used;
1027 trg->ctx_pos = src->ctx_pos;
1028 trg->ctx_size = src->ctx_size;
1029
Michal Vasko08e9b112021-06-11 15:41:17 +02001030 if (trg->size) {
1031 trg->val.nodes = malloc(trg->size * sizeof *trg->val.nodes);
1032 LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), );
1033 memcpy(trg->val.nodes, src->val.nodes, src->used * sizeof *src->val.nodes);
1034 } else {
1035 trg->val.nodes = NULL;
1036 }
Michal Vaskod3678892020-05-21 10:06:58 +02001037 if (src->ht) {
1038 trg->ht = lyht_dup(src->ht);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001039 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02001040 trg->ht = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001041 }
1042 }
1043}
1044
1045/**
1046 * @brief Clear context of all schema nodes.
1047 *
1048 * @param[in] set Set to clear.
Michal Vasko1a09b212021-05-06 13:00:10 +02001049 * @param[in] new_ctx New context state for all the nodes currently in the context.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001050 */
1051static void
Michal Vasko1a09b212021-05-06 13:00:10 +02001052set_scnode_clear_ctx(struct lyxp_set *set, int32_t new_ctx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001053{
1054 uint32_t i;
1055
1056 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001057 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko1a09b212021-05-06 13:00:10 +02001058 set->val.scnodes[i].in_ctx = new_ctx;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001059 } else if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START) {
1060 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001061 }
1062 }
1063}
1064
1065/**
1066 * @brief Remove a node from a set. Removing last node changes
1067 * set into LYXP_SET_EMPTY. Context position aware.
1068 *
1069 * @param[in] set Set to use.
1070 * @param[in] idx Index from @p set of the node to be removed.
1071 */
1072static void
1073set_remove_node(struct lyxp_set *set, uint32_t idx)
1074{
1075 assert(set && (set->type == LYXP_SET_NODE_SET));
1076 assert(idx < set->used);
1077
1078 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
1079
1080 --set->used;
Michal Vasko08e9b112021-06-11 15:41:17 +02001081 if (idx < set->used) {
1082 memmove(&set->val.nodes[idx], &set->val.nodes[idx + 1], (set->used - idx) * sizeof *set->val.nodes);
1083 } else if (!set->used) {
Michal Vaskod3678892020-05-21 10:06:58 +02001084 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001085 }
1086}
1087
1088/**
Michal Vasko2caefc12019-11-14 16:07:56 +01001089 * @brief Remove a node from a set by setting its type to LYXP_NODE_NONE.
Michal Vasko57eab132019-09-24 11:46:26 +02001090 *
1091 * @param[in] set Set to use.
1092 * @param[in] idx Index from @p set of the node to be removed.
1093 */
1094static void
Michal Vasko2caefc12019-11-14 16:07:56 +01001095set_remove_node_none(struct lyxp_set *set, uint32_t idx)
Michal Vasko57eab132019-09-24 11:46:26 +02001096{
1097 assert(set && (set->type == LYXP_SET_NODE_SET));
1098 assert(idx < set->used);
1099
Michal Vasko2caefc12019-11-14 16:07:56 +01001100 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1101 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
1102 }
1103 set->val.nodes[idx].type = LYXP_NODE_NONE;
Michal Vasko57eab132019-09-24 11:46:26 +02001104}
1105
1106/**
Michal Vasko2caefc12019-11-14 16:07:56 +01001107 * @brief Remove all LYXP_NODE_NONE nodes from a set. Removing last node changes
Michal Vasko57eab132019-09-24 11:46:26 +02001108 * set into LYXP_SET_EMPTY. Context position aware.
1109 *
1110 * @param[in] set Set to consolidate.
1111 */
1112static void
Michal Vasko2caefc12019-11-14 16:07:56 +01001113set_remove_nodes_none(struct lyxp_set *set)
Michal Vasko57eab132019-09-24 11:46:26 +02001114{
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01001115 uint32_t i, orig_used, end = 0;
1116 int64_t start;
Michal Vasko57eab132019-09-24 11:46:26 +02001117
Michal Vaskod3678892020-05-21 10:06:58 +02001118 assert(set);
Michal Vasko57eab132019-09-24 11:46:26 +02001119
1120 orig_used = set->used;
1121 set->used = 0;
Michal Vaskod989ba02020-08-24 10:59:24 +02001122 for (i = 0; i < orig_used; ) {
Michal Vasko57eab132019-09-24 11:46:26 +02001123 start = -1;
1124 do {
Michal Vasko2caefc12019-11-14 16:07:56 +01001125 if ((set->val.nodes[i].type != LYXP_NODE_NONE) && (start == -1)) {
Michal Vasko57eab132019-09-24 11:46:26 +02001126 start = i;
Michal Vasko2caefc12019-11-14 16:07:56 +01001127 } else if ((start > -1) && (set->val.nodes[i].type == LYXP_NODE_NONE)) {
Michal Vasko57eab132019-09-24 11:46:26 +02001128 end = i;
1129 ++i;
1130 break;
1131 }
1132
1133 ++i;
1134 if (i == orig_used) {
1135 end = i;
1136 }
1137 } while (i < orig_used);
1138
1139 if (start > -1) {
1140 /* move the whole chunk of valid nodes together */
1141 if (set->used != (unsigned)start) {
1142 memmove(&set->val.nodes[set->used], &set->val.nodes[start], (end - start) * sizeof *set->val.nodes);
1143 }
1144 set->used += end - start;
1145 }
1146 }
Michal Vasko57eab132019-09-24 11:46:26 +02001147}
1148
1149/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02001150 * @brief Check for duplicates in a node set.
1151 *
1152 * @param[in] set Set to check.
1153 * @param[in] node Node to look for in @p set.
1154 * @param[in] node_type Type of @p node.
1155 * @param[in] skip_idx Index from @p set to skip.
1156 * @return LY_ERR
1157 */
1158static LY_ERR
1159set_dup_node_check(const struct lyxp_set *set, const struct lyd_node *node, enum lyxp_node_type node_type, int skip_idx)
1160{
1161 uint32_t i;
1162
Michal Vasko2caefc12019-11-14 16:07:56 +01001163 if (set->ht && node) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001164 return set_dup_node_hash_check(set, (struct lyd_node *)node, node_type, skip_idx);
1165 }
1166
1167 for (i = 0; i < set->used; ++i) {
1168 if ((skip_idx > -1) && (i == (unsigned)skip_idx)) {
1169 continue;
1170 }
1171
1172 if ((set->val.nodes[i].node == node) && (set->val.nodes[i].type == node_type)) {
1173 return LY_EEXIST;
1174 }
1175 }
1176
1177 return LY_SUCCESS;
1178}
1179
Radek Krejci857189e2020-09-01 13:26:36 +02001180ly_bool
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001181lyxp_set_scnode_contains(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type, int skip_idx,
1182 uint32_t *index_p)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001183{
1184 uint32_t i;
1185
1186 for (i = 0; i < set->used; ++i) {
1187 if ((skip_idx > -1) && (i == (unsigned)skip_idx)) {
1188 continue;
1189 }
1190
1191 if ((set->val.scnodes[i].scnode == node) && (set->val.scnodes[i].type == node_type)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001192 if (index_p) {
1193 *index_p = i;
1194 }
1195 return 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001196 }
1197 }
1198
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001199 return 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001200}
1201
Michal Vaskoecd62de2019-11-13 12:35:11 +01001202void
1203lyxp_set_scnode_merge(struct lyxp_set *set1, struct lyxp_set *set2)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001204{
1205 uint32_t orig_used, i, j;
1206
Michal Vaskod3678892020-05-21 10:06:58 +02001207 assert((set1->type == LYXP_SET_SCNODE_SET) && (set2->type == LYXP_SET_SCNODE_SET));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001208
Michal Vaskod3678892020-05-21 10:06:58 +02001209 if (!set2->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001210 return;
1211 }
1212
Michal Vaskod3678892020-05-21 10:06:58 +02001213 if (!set1->used) {
aPiecekadc1e4f2021-10-07 11:15:12 +02001214 /* release hidden allocated data (lyxp_set.size) */
1215 lyxp_set_free_content(set1);
1216 /* direct copying of the entire structure */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001217 memcpy(set1, set2, sizeof *set1);
1218 return;
1219 }
1220
1221 if (set1->used + set2->used > set1->size) {
1222 set1->size = set1->used + set2->used;
1223 set1->val.scnodes = ly_realloc(set1->val.scnodes, set1->size * sizeof *set1->val.scnodes);
1224 LY_CHECK_ERR_RET(!set1->val.scnodes, LOGMEM(set1->ctx), );
1225 }
1226
1227 orig_used = set1->used;
1228
1229 for (i = 0; i < set2->used; ++i) {
1230 for (j = 0; j < orig_used; ++j) {
1231 /* detect duplicities */
1232 if (set1->val.scnodes[j].scnode == set2->val.scnodes[i].scnode) {
1233 break;
1234 }
1235 }
1236
Michal Vasko0587bce2020-12-10 12:19:21 +01001237 if (j < orig_used) {
1238 /* node is there, but update its status if needed */
1239 if (set1->val.scnodes[j].in_ctx == LYXP_SET_SCNODE_START_USED) {
1240 set1->val.scnodes[j].in_ctx = set2->val.scnodes[i].in_ctx;
Michal Vasko1a09b212021-05-06 13:00:10 +02001241 } else if ((set1->val.scnodes[j].in_ctx == LYXP_SET_SCNODE_ATOM_NODE) &&
1242 (set2->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_VAL)) {
1243 set1->val.scnodes[j].in_ctx = set2->val.scnodes[i].in_ctx;
Michal Vasko0587bce2020-12-10 12:19:21 +01001244 }
1245 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001246 memcpy(&set1->val.scnodes[set1->used], &set2->val.scnodes[i], sizeof *set2->val.scnodes);
1247 ++set1->used;
1248 }
1249 }
1250
Michal Vaskod3678892020-05-21 10:06:58 +02001251 lyxp_set_free_content(set2);
1252 set2->type = LYXP_SET_SCNODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001253}
1254
1255/**
1256 * @brief Insert a node into a set. Context position aware.
1257 *
1258 * @param[in] set Set to use.
1259 * @param[in] node Node to insert to @p set.
1260 * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting.
1261 * @param[in] node_type Node type of @p node.
1262 * @param[in] idx Index in @p set to insert into.
1263 */
1264static void
1265set_insert_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx)
1266{
Michal Vaskod3678892020-05-21 10:06:58 +02001267 assert(set && (set->type == LYXP_SET_NODE_SET));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001268
Michal Vaskod3678892020-05-21 10:06:58 +02001269 if (!set->size) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001270 /* first item */
1271 if (idx) {
1272 /* no real harm done, but it is a bug */
1273 LOGINT(set->ctx);
1274 idx = 0;
1275 }
1276 set->val.nodes = malloc(LYXP_SET_SIZE_START * sizeof *set->val.nodes);
1277 LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), );
1278 set->type = LYXP_SET_NODE_SET;
1279 set->used = 0;
1280 set->size = LYXP_SET_SIZE_START;
1281 set->ctx_pos = 1;
1282 set->ctx_size = 1;
1283 set->ht = NULL;
1284 } else {
1285 /* not an empty set */
1286 if (set->used == set->size) {
1287
1288 /* set is full */
Michal Vasko871df522022-04-06 12:14:41 +02001289 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 +02001290 LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), );
Michal Vasko871df522022-04-06 12:14:41 +02001291 set->size *= LYXP_SET_SIZE_MUL_STEP;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001292 }
1293
1294 if (idx > set->used) {
1295 LOGINT(set->ctx);
1296 idx = set->used;
1297 }
1298
1299 /* make space for the new node */
1300 if (idx < set->used) {
1301 memmove(&set->val.nodes[idx + 1], &set->val.nodes[idx], (set->used - idx) * sizeof *set->val.nodes);
1302 }
1303 }
1304
1305 /* finally assign the value */
1306 set->val.nodes[idx].node = (struct lyd_node *)node;
1307 set->val.nodes[idx].type = node_type;
1308 set->val.nodes[idx].pos = pos;
1309 ++set->used;
1310
Michal Vasko2416fdd2021-10-01 11:07:10 +02001311 /* add into hash table */
1312 set_insert_node_hash(set, (struct lyd_node *)node, node_type);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001313}
1314
Michal Vaskoe4a6d012023-05-22 14:34:52 +02001315LY_ERR
1316lyxp_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 +02001317 enum lyxp_axis axis, uint32_t *index_p)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001318{
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001319 uint32_t index;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001320
1321 assert(set->type == LYXP_SET_SCNODE_SET);
1322
Michal Vasko871df522022-04-06 12:14:41 +02001323 if (!set->size) {
1324 /* first item */
1325 set->val.scnodes = malloc(LYXP_SET_SIZE_START * sizeof *set->val.scnodes);
1326 LY_CHECK_ERR_RET(!set->val.scnodes, LOGMEM(set->ctx), LY_EMEM);
1327 set->type = LYXP_SET_SCNODE_SET;
1328 set->used = 0;
1329 set->size = LYXP_SET_SIZE_START;
1330 set->ctx_pos = 1;
1331 set->ctx_size = 1;
1332 set->ht = NULL;
1333 }
1334
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001335 if (lyxp_set_scnode_contains(set, node, node_type, -1, &index)) {
Michal Vaskoe4a6d012023-05-22 14:34:52 +02001336 /* BUG if axes differ, this new one is thrown away */
Radek Krejcif13b87b2020-12-01 22:02:17 +01001337 set->val.scnodes[index].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001338 } else {
1339 if (set->used == set->size) {
Michal Vasko871df522022-04-06 12:14:41 +02001340 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 +02001341 LY_CHECK_ERR_RET(!set->val.scnodes, LOGMEM(set->ctx), LY_EMEM);
Michal Vasko871df522022-04-06 12:14:41 +02001342 set->size *= LYXP_SET_SIZE_MUL_STEP;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001343 }
1344
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001345 index = set->used;
1346 set->val.scnodes[index].scnode = (struct lysc_node *)node;
1347 set->val.scnodes[index].type = node_type;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001348 set->val.scnodes[index].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko7333cb32022-07-29 16:30:29 +02001349 set->val.scnodes[index].axis = axis;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001350 ++set->used;
1351 }
1352
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001353 if (index_p) {
1354 *index_p = index;
1355 }
1356
1357 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001358}
1359
1360/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02001361 * @brief Set all nodes with ctx 1 to a new unique context value.
1362 *
1363 * @param[in] set Set to modify.
1364 * @return New context value.
1365 */
Michal Vasko5c4e5892019-11-14 12:31:38 +01001366static int32_t
Michal Vasko03ff5a72019-09-11 13:49:33 +02001367set_scnode_new_in_ctx(struct lyxp_set *set)
1368{
Michal Vasko5c4e5892019-11-14 12:31:38 +01001369 uint32_t i;
1370 int32_t ret_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001371
1372 assert(set->type == LYXP_SET_SCNODE_SET);
1373
Radek Krejcif13b87b2020-12-01 22:02:17 +01001374 ret_ctx = LYXP_SET_SCNODE_ATOM_PRED_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001375retry:
1376 for (i = 0; i < set->used; ++i) {
1377 if (set->val.scnodes[i].in_ctx >= ret_ctx) {
1378 ret_ctx = set->val.scnodes[i].in_ctx + 1;
1379 goto retry;
1380 }
1381 }
1382 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001383 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001384 set->val.scnodes[i].in_ctx = ret_ctx;
1385 }
1386 }
1387
1388 return ret_ctx;
1389}
1390
1391/**
1392 * @brief Get unique @p node position in the data.
1393 *
1394 * @param[in] node Node to find.
1395 * @param[in] node_type Node type of @p node.
1396 * @param[in] root Root node.
1397 * @param[in] root_type Type of the XPath @p root node.
1398 * @param[in] prev Node that we think is before @p node in DFS from @p root. Can optionally
1399 * be used to increase efficiency and start the DFS from this node.
1400 * @param[in] prev_pos Node @p prev position. Optional, but must be set if @p prev is set.
1401 * @return Node position.
1402 */
1403static uint32_t
1404get_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 +02001405 enum lyxp_node_type root_type, const struct lyd_node **prev, uint32_t *prev_pos)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001406{
Michal Vasko56daf732020-08-10 10:57:18 +02001407 const struct lyd_node *elem = NULL, *top_sibling;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001408 uint32_t pos = 1;
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001409 ly_bool found = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001410
1411 assert(prev && prev_pos && !root->prev->next);
1412
1413 if ((node_type == LYXP_NODE_ROOT) || (node_type == LYXP_NODE_ROOT_CONFIG)) {
1414 return 0;
1415 }
1416
1417 if (*prev) {
1418 /* start from the previous element instead from the root */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001419 pos = *prev_pos;
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001420 for (top_sibling = *prev; top_sibling->parent; top_sibling = lyd_parent(top_sibling)) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001421 goto dfs_search;
1422 }
1423
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001424 LY_LIST_FOR(root, top_sibling) {
Michal Vasko56daf732020-08-10 10:57:18 +02001425 LYD_TREE_DFS_BEGIN(top_sibling, elem) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001426dfs_search:
Michal Vaskoa9309bb2021-07-09 09:31:55 +02001427 LYD_TREE_DFS_continue = 0;
1428
Michal Vasko56daf732020-08-10 10:57:18 +02001429 if (*prev && !elem) {
1430 /* resume previous DFS */
1431 elem = LYD_TREE_DFS_next = (struct lyd_node *)*prev;
1432 LYD_TREE_DFS_continue = 0;
1433 }
1434
Michal Vasko482a6822022-05-06 08:56:12 +02001435 if (!elem->schema || ((root_type == LYXP_NODE_ROOT_CONFIG) && (elem->schema->flags & LYS_CONFIG_R))) {
Michal Vasko56daf732020-08-10 10:57:18 +02001436 /* skip */
1437 LYD_TREE_DFS_continue = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001438 } else {
Michal Vasko56daf732020-08-10 10:57:18 +02001439 if (elem == node) {
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001440 found = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001441 break;
1442 }
Michal Vasko56daf732020-08-10 10:57:18 +02001443 ++pos;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001444 }
Michal Vasko56daf732020-08-10 10:57:18 +02001445
1446 LYD_TREE_DFS_END(top_sibling, elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001447 }
1448
1449 /* node found */
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001450 if (found) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001451 break;
1452 }
1453 }
1454
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001455 if (!found) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001456 if (!(*prev)) {
1457 /* we went from root and failed to find it, cannot be */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001458 LOGINT(LYD_CTX(node));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001459 return 0;
1460 } else {
Michal Vasko56daf732020-08-10 10:57:18 +02001461 /* start the search again from the beginning */
1462 *prev = root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001463
Michal Vasko56daf732020-08-10 10:57:18 +02001464 top_sibling = root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001465 pos = 1;
1466 goto dfs_search;
1467 }
1468 }
1469
1470 /* remember the last found node for next time */
1471 *prev = node;
1472 *prev_pos = pos;
1473
1474 return pos;
1475}
1476
1477/**
1478 * @brief Assign (fill) missing node positions.
1479 *
1480 * @param[in] set Set to fill positions in.
1481 * @param[in] root Context root node.
1482 * @param[in] root_type Context root type.
1483 * @return LY_ERR
1484 */
1485static LY_ERR
1486set_assign_pos(struct lyxp_set *set, const struct lyd_node *root, enum lyxp_node_type root_type)
1487{
1488 const struct lyd_node *prev = NULL, *tmp_node;
1489 uint32_t i, tmp_pos = 0;
1490
1491 for (i = 0; i < set->used; ++i) {
1492 if (!set->val.nodes[i].pos) {
1493 tmp_node = NULL;
1494 switch (set->val.nodes[i].type) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001495 case LYXP_NODE_META:
1496 tmp_node = set->val.meta[i].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001497 if (!tmp_node) {
1498 LOGINT_RET(root->schema->module->ctx);
1499 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01001500 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001501 case LYXP_NODE_ELEM:
1502 case LYXP_NODE_TEXT:
1503 if (!tmp_node) {
1504 tmp_node = set->val.nodes[i].node;
1505 }
1506 set->val.nodes[i].pos = get_node_pos(tmp_node, set->val.nodes[i].type, root, root_type, &prev, &tmp_pos);
1507 break;
1508 default:
1509 /* all roots have position 0 */
1510 break;
1511 }
1512 }
1513 }
1514
1515 return LY_SUCCESS;
1516}
1517
1518/**
Michal Vasko9f96a052020-03-10 09:41:45 +01001519 * @brief Get unique @p meta position in the parent metadata.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001520 *
Michal Vasko9f96a052020-03-10 09:41:45 +01001521 * @param[in] meta Metadata to use.
1522 * @return Metadata position.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001523 */
Michal Vaskodd528af2022-08-08 14:35:07 +02001524static uint32_t
Michal Vasko9f96a052020-03-10 09:41:45 +01001525get_meta_pos(struct lyd_meta *meta)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001526{
Michal Vaskodd528af2022-08-08 14:35:07 +02001527 uint32_t pos = 0;
Michal Vasko9f96a052020-03-10 09:41:45 +01001528 struct lyd_meta *meta2;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001529
Michal Vasko9f96a052020-03-10 09:41:45 +01001530 for (meta2 = meta->parent->meta; meta2 && (meta2 != meta); meta2 = meta2->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001531 ++pos;
1532 }
1533
Michal Vasko9f96a052020-03-10 09:41:45 +01001534 assert(meta2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001535 return pos;
1536}
1537
1538/**
1539 * @brief Compare 2 nodes in respect to XPath document order.
1540 *
1541 * @param[in] item1 1st node.
1542 * @param[in] item2 2nd node.
1543 * @return If 1st > 2nd returns 1, 1st == 2nd returns 0, and 1st < 2nd returns -1.
1544 */
1545static int
1546set_sort_compare(struct lyxp_set_node *item1, struct lyxp_set_node *item2)
1547{
Michal Vasko9f96a052020-03-10 09:41:45 +01001548 uint32_t meta_pos1 = 0, meta_pos2 = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001549
1550 if (item1->pos < item2->pos) {
1551 return -1;
1552 }
1553
1554 if (item1->pos > item2->pos) {
1555 return 1;
1556 }
1557
1558 /* node positions are equal, the fun case */
1559
1560 /* 1st ELEM - == - 2nd TEXT, 1st TEXT - == - 2nd ELEM */
1561 /* special case since text nodes are actually saved as their parents */
1562 if ((item1->node == item2->node) && (item1->type != item2->type)) {
1563 if (item1->type == LYXP_NODE_ELEM) {
1564 assert(item2->type == LYXP_NODE_TEXT);
1565 return -1;
1566 } else {
1567 assert((item1->type == LYXP_NODE_TEXT) && (item2->type == LYXP_NODE_ELEM));
1568 return 1;
1569 }
1570 }
1571
Michal Vasko9f96a052020-03-10 09:41:45 +01001572 /* we need meta positions now */
1573 if (item1->type == LYXP_NODE_META) {
1574 meta_pos1 = get_meta_pos((struct lyd_meta *)item1->node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001575 }
Michal Vasko9f96a052020-03-10 09:41:45 +01001576 if (item2->type == LYXP_NODE_META) {
1577 meta_pos2 = get_meta_pos((struct lyd_meta *)item2->node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001578 }
1579
Michal Vasko9f96a052020-03-10 09:41:45 +01001580 /* 1st ROOT - 2nd ROOT, 1st ELEM - 2nd ELEM, 1st TEXT - 2nd TEXT, 1st META - =pos= - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001581 /* check for duplicates */
1582 if (item1->node == item2->node) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001583 assert((item1->type == item2->type) && ((item1->type != LYXP_NODE_META) || (meta_pos1 == meta_pos2)));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001584 return 0;
1585 }
1586
Michal Vasko9f96a052020-03-10 09:41:45 +01001587 /* 1st ELEM - 2nd TEXT, 1st ELEM - any pos - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001588 /* elem is always first, 2nd node is after it */
1589 if (item1->type == LYXP_NODE_ELEM) {
1590 assert(item2->type != LYXP_NODE_ELEM);
1591 return -1;
1592 }
1593
Michal Vasko9f96a052020-03-10 09:41:45 +01001594 /* 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 +02001595 /* 2nd is before 1st */
Michal Vasko69730152020-10-09 16:30:07 +02001596 if (((item1->type == LYXP_NODE_TEXT) &&
1597 ((item2->type == LYXP_NODE_ELEM) || (item2->type == LYXP_NODE_META))) ||
1598 ((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_ELEM)) ||
1599 (((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_META)) &&
1600 (meta_pos1 > meta_pos2))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001601 return 1;
1602 }
1603
Michal Vasko9f96a052020-03-10 09:41:45 +01001604 /* 1st META - any pos - 2nd TEXT, 1st META <pos< - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001605 /* 2nd is after 1st */
1606 return -1;
1607}
1608
1609/**
1610 * @brief Set cast for comparisons.
1611 *
Michal Vasko8abcecc2022-07-28 09:55:01 +02001612 * @param[in,out] trg Target set to cast source into.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001613 * @param[in] src Source set.
1614 * @param[in] type Target set type.
1615 * @param[in] src_idx Source set node index.
Michal Vasko8abcecc2022-07-28 09:55:01 +02001616 * @return LY_SUCCESS on success.
1617 * @return LY_ERR value on error.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001618 */
1619static LY_ERR
Michal Vasko8abcecc2022-07-28 09:55:01 +02001620set_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 +02001621{
1622 assert(src->type == LYXP_SET_NODE_SET);
1623
1624 set_init(trg, src);
1625
1626 /* insert node into target set */
1627 set_insert_node(trg, src->val.nodes[src_idx].node, src->val.nodes[src_idx].pos, src->val.nodes[src_idx].type, 0);
1628
1629 /* cast target set appropriately */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001630 return lyxp_set_cast(trg, type);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001631}
1632
Michal Vasko4c7763f2020-07-27 17:40:37 +02001633/**
1634 * @brief Set content canonization for comparisons.
1635 *
Michal Vasko8abcecc2022-07-28 09:55:01 +02001636 * @param[in,out] set Set to canonize.
Michal Vasko4c7763f2020-07-27 17:40:37 +02001637 * @param[in] xp_node Source XPath node/meta to use for canonization.
Michal Vasko8abcecc2022-07-28 09:55:01 +02001638 * @return LY_SUCCESS on success.
1639 * @return LY_ERR value on error.
Michal Vasko4c7763f2020-07-27 17:40:37 +02001640 */
1641static LY_ERR
Michal Vasko8abcecc2022-07-28 09:55:01 +02001642set_comp_canonize(struct lyxp_set *set, const struct lyxp_set_node *xp_node)
Michal Vasko4c7763f2020-07-27 17:40:37 +02001643{
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001644 const struct lysc_type *type = NULL;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001645 struct lyd_value val;
1646 struct ly_err_item *err = NULL;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001647 LY_ERR rc;
1648
1649 /* is there anything to canonize even? */
Michal Vasko8abcecc2022-07-28 09:55:01 +02001650 if (set->type == LYXP_SET_STRING) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02001651 /* do we have a type to use for canonization? */
1652 if ((xp_node->type == LYXP_NODE_ELEM) && (xp_node->node->schema->nodetype & LYD_NODE_TERM)) {
1653 type = ((struct lyd_node_term *)xp_node->node)->value.realtype;
1654 } else if (xp_node->type == LYXP_NODE_META) {
1655 type = ((struct lyd_meta *)xp_node->node)->value.realtype;
1656 }
1657 }
1658 if (!type) {
Michal Vasko8abcecc2022-07-28 09:55:01 +02001659 /* no canonization needed/possible */
1660 return LY_SUCCESS;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001661 }
1662
Michal Vasko8abcecc2022-07-28 09:55:01 +02001663 /* check for built-in types without required canonization */
1664 if ((type->basetype == LY_TYPE_STRING) && (type->plugin->store == lyplg_type_store_string)) {
1665 /* string */
1666 return LY_SUCCESS;
1667 }
1668 if ((type->basetype == LY_TYPE_BOOL) && (type->plugin->store == lyplg_type_store_boolean)) {
1669 /* boolean */
1670 return LY_SUCCESS;
1671 }
1672 if ((type->basetype == LY_TYPE_ENUM) && (type->plugin->store == lyplg_type_store_enum)) {
1673 /* enumeration */
1674 return LY_SUCCESS;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001675 }
1676
Michal Vasko8abcecc2022-07-28 09:55:01 +02001677 /* print canonized string, ignore errors, the value may not satisfy schema constraints */
1678 rc = type->plugin->store(set->ctx, type, set->val.str, strlen(set->val.str), 0, set->format, set->prefix_data,
Michal Vasko405cc9e2020-12-01 12:01:27 +01001679 LYD_HINT_DATA, xp_node->node->schema, &val, NULL, &err);
Michal Vasko4c7763f2020-07-27 17:40:37 +02001680 ly_err_free(err);
1681 if (rc) {
Michal Vasko8abcecc2022-07-28 09:55:01 +02001682 /* invalid value, function store automaticaly dealloc value when fail */
1683 return LY_SUCCESS;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001684 }
1685
Michal Vasko8abcecc2022-07-28 09:55:01 +02001686 /* use the canonized string value */
1687 free(set->val.str);
1688 set->val.str = strdup(lyd_value_get_canonical(set->ctx, &val));
1689 type->plugin->free(set->ctx, &val);
1690 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), LY_EMEM);
Michal Vasko4c7763f2020-07-27 17:40:37 +02001691
Michal Vasko4c7763f2020-07-27 17:40:37 +02001692 return LY_SUCCESS;
1693}
1694
Michal Vasko03ff5a72019-09-11 13:49:33 +02001695/**
1696 * @brief Bubble sort @p set into XPath document order.
Michal Vasko49fec8e2022-05-24 10:28:33 +02001697 * Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001698 *
1699 * @param[in] set Set to sort.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001700 * @return How many times the whole set was traversed - 1 (if set was sorted, returns 0).
1701 */
1702static int
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001703set_sort(struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001704{
1705 uint32_t i, j;
Radek Krejci1deb5be2020-08-26 16:43:36 +02001706 int ret = 0, cmp;
Radek Krejci857189e2020-09-01 13:26:36 +02001707 ly_bool inverted, change;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001708 const struct lyd_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001709 struct lyxp_set_node item;
1710 struct lyxp_set_hash_node hnode;
1711 uint64_t hash;
1712
Michal Vasko3cf8fbf2020-05-27 15:21:21 +02001713 if ((set->type != LYXP_SET_NODE_SET) || (set->used < 2)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001714 return 0;
1715 }
1716
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001717 /* find first top-level node to be used as anchor for positions */
Michal Vasko4a7d4d62021-12-13 17:05:06 +01001718 for (root = set->tree; root->parent; root = lyd_parent(root)) {}
Michal Vaskod989ba02020-08-24 10:59:24 +02001719 for ( ; root->prev->next; root = root->prev) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001720
1721 /* fill positions */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001722 if (set_assign_pos(set, root, set->root_type)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001723 return -1;
1724 }
1725
Michal Vasko88a9e802022-05-24 10:50:28 +02001726#ifndef NDEBUG
Michal Vasko03ff5a72019-09-11 13:49:33 +02001727 LOGDBG(LY_LDGXPATH, "SORT BEGIN");
1728 print_set_debug(set);
Michal Vasko88a9e802022-05-24 10:50:28 +02001729#endif
Michal Vasko03ff5a72019-09-11 13:49:33 +02001730
1731 for (i = 0; i < set->used; ++i) {
1732 inverted = 0;
1733 change = 0;
1734
1735 for (j = 1; j < set->used - i; ++j) {
1736 /* compare node positions */
1737 if (inverted) {
1738 cmp = set_sort_compare(&set->val.nodes[j], &set->val.nodes[j - 1]);
1739 } else {
1740 cmp = set_sort_compare(&set->val.nodes[j - 1], &set->val.nodes[j]);
1741 }
1742
1743 /* swap if needed */
1744 if ((inverted && (cmp < 0)) || (!inverted && (cmp > 0))) {
1745 change = 1;
1746
1747 item = set->val.nodes[j - 1];
1748 set->val.nodes[j - 1] = set->val.nodes[j];
1749 set->val.nodes[j] = item;
1750 } else {
1751 /* whether node_pos1 should be smaller than node_pos2 or the other way around */
1752 inverted = !inverted;
1753 }
1754 }
1755
1756 ++ret;
1757
1758 if (!change) {
1759 break;
1760 }
1761 }
1762
Michal Vasko88a9e802022-05-24 10:50:28 +02001763#ifndef NDEBUG
Michal Vasko03ff5a72019-09-11 13:49:33 +02001764 LOGDBG(LY_LDGXPATH, "SORT END %d", ret);
1765 print_set_debug(set);
Michal Vasko88a9e802022-05-24 10:50:28 +02001766#endif
Michal Vasko03ff5a72019-09-11 13:49:33 +02001767
1768 /* check node hashes */
1769 if (set->used >= LYD_HT_MIN_ITEMS) {
1770 assert(set->ht);
1771 for (i = 0; i < set->used; ++i) {
1772 hnode.node = set->val.nodes[i].node;
1773 hnode.type = set->val.nodes[i].type;
1774
Michal Vaskoae130f52023-04-20 14:25:16 +02001775 hash = lyht_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
1776 hash = lyht_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
1777 hash = lyht_hash_multi(hash, NULL, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001778
1779 assert(!lyht_find(set->ht, &hnode, hash, NULL));
1780 }
1781 }
1782
1783 return ret - 1;
1784}
1785
Michal Vasko03ff5a72019-09-11 13:49:33 +02001786/**
1787 * @brief Merge 2 sorted sets into one.
1788 *
1789 * @param[in,out] trg Set to merge into. Duplicates are removed.
1790 * @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 +02001791 * @return LY_ERR
1792 */
1793static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001794set_sorted_merge(struct lyxp_set *trg, struct lyxp_set *src)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001795{
1796 uint32_t i, j, k, count, dup_count;
1797 int cmp;
1798 const struct lyd_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001799
Michal Vaskod3678892020-05-21 10:06:58 +02001800 if ((trg->type != LYXP_SET_NODE_SET) || (src->type != LYXP_SET_NODE_SET)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001801 return LY_EINVAL;
1802 }
1803
Michal Vaskod3678892020-05-21 10:06:58 +02001804 if (!src->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001805 return LY_SUCCESS;
Michal Vaskod3678892020-05-21 10:06:58 +02001806 } else if (!trg->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001807 set_fill_set(trg, src);
Michal Vaskod3678892020-05-21 10:06:58 +02001808 lyxp_set_free_content(src);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001809 return LY_SUCCESS;
1810 }
1811
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001812 /* find first top-level node to be used as anchor for positions */
Michal Vasko0143b682021-12-13 17:13:09 +01001813 for (root = trg->tree; root->parent; root = lyd_parent(root)) {}
Michal Vaskod989ba02020-08-24 10:59:24 +02001814 for ( ; root->prev->next; root = root->prev) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001815
1816 /* fill positions */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001817 if (set_assign_pos(trg, root, trg->root_type) || set_assign_pos(src, root, src->root_type)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001818 return LY_EINT;
1819 }
1820
1821#ifndef NDEBUG
1822 LOGDBG(LY_LDGXPATH, "MERGE target");
1823 print_set_debug(trg);
1824 LOGDBG(LY_LDGXPATH, "MERGE source");
1825 print_set_debug(src);
1826#endif
1827
1828 /* make memory for the merge (duplicates are not detected yet, so space
1829 * will likely be wasted on them, too bad) */
1830 if (trg->size - trg->used < src->used) {
1831 trg->size = trg->used + src->used;
1832
1833 trg->val.nodes = ly_realloc(trg->val.nodes, trg->size * sizeof *trg->val.nodes);
1834 LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx), LY_EMEM);
1835 }
1836
1837 i = 0;
1838 j = 0;
1839 count = 0;
1840 dup_count = 0;
1841 do {
1842 cmp = set_sort_compare(&src->val.nodes[i], &trg->val.nodes[j]);
1843 if (!cmp) {
1844 if (!count) {
1845 /* duplicate, just skip it */
1846 ++i;
1847 ++j;
1848 } else {
1849 /* we are copying something already, so let's copy the duplicate too,
1850 * we are hoping that afterwards there are some more nodes to
1851 * copy and this way we can copy them all together */
1852 ++count;
1853 ++dup_count;
1854 ++i;
1855 ++j;
1856 }
1857 } else if (cmp < 0) {
1858 /* inserting src node into trg, just remember it for now */
1859 ++count;
1860 ++i;
1861
1862 /* insert the hash now */
1863 set_insert_node_hash(trg, src->val.nodes[i - 1].node, src->val.nodes[i - 1].type);
1864 } else if (count) {
1865copy_nodes:
1866 /* time to actually copy the nodes, we have found the largest block of nodes */
1867 memmove(&trg->val.nodes[j + (count - dup_count)],
1868 &trg->val.nodes[j],
1869 (trg->used - j) * sizeof *trg->val.nodes);
1870 memcpy(&trg->val.nodes[j - dup_count], &src->val.nodes[i - count], count * sizeof *src->val.nodes);
1871
1872 trg->used += count - dup_count;
1873 /* do not change i, except the copying above, we are basically doing exactly what is in the else branch below */
1874 j += count - dup_count;
1875
1876 count = 0;
1877 dup_count = 0;
1878 } else {
1879 ++j;
1880 }
1881 } while ((i < src->used) && (j < trg->used));
1882
1883 if ((i < src->used) || count) {
1884 /* insert all the hashes first */
1885 for (k = i; k < src->used; ++k) {
1886 set_insert_node_hash(trg, src->val.nodes[k].node, src->val.nodes[k].type);
1887 }
1888
1889 /* loop ended, but we need to copy something at trg end */
1890 count += src->used - i;
1891 i = src->used;
1892 goto copy_nodes;
1893 }
1894
1895 /* we are inserting hashes before the actual node insert, which causes
1896 * situations when there were initially not enough items for a hash table,
1897 * but even after some were inserted, hash table was not created (during
1898 * insertion the number of items is not updated yet) */
1899 if (!trg->ht && (trg->used >= LYD_HT_MIN_ITEMS)) {
1900 set_insert_node_hash(trg, NULL, 0);
1901 }
1902
1903#ifndef NDEBUG
1904 LOGDBG(LY_LDGXPATH, "MERGE result");
1905 print_set_debug(trg);
1906#endif
1907
Michal Vaskod3678892020-05-21 10:06:58 +02001908 lyxp_set_free_content(src);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001909 return LY_SUCCESS;
1910}
1911
Michal Vasko14676352020-05-29 11:35:55 +02001912LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02001913lyxp_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 +02001914{
Michal Vasko004d3152020-06-11 19:59:22 +02001915 if (exp->used == tok_idx) {
Michal Vasko14676352020-05-29 11:35:55 +02001916 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001917 LOGVAL(ctx, LY_VCODE_XP_EOF);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001918 }
Michal Vasko14676352020-05-29 11:35:55 +02001919 return LY_EINCOMPLETE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001920 }
1921
Michal Vasko004d3152020-06-11 19:59:22 +02001922 if (want_tok && (exp->tokens[tok_idx] != want_tok)) {
Michal Vasko14676352020-05-29 11:35:55 +02001923 if (ctx) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02001924 LOGVAL(ctx, LY_VCODE_XP_INTOK2, lyxp_token2str(exp->tokens[tok_idx]),
1925 &exp->expr[exp->tok_pos[tok_idx]], lyxp_token2str(want_tok));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001926 }
Michal Vasko14676352020-05-29 11:35:55 +02001927 return LY_ENOT;
1928 }
1929
1930 return LY_SUCCESS;
1931}
1932
Michal Vasko004d3152020-06-11 19:59:22 +02001933LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02001934lyxp_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 +02001935{
1936 LY_CHECK_RET(lyxp_check_token(ctx, exp, *tok_idx, want_tok));
1937
1938 /* skip the token */
1939 ++(*tok_idx);
1940
1941 return LY_SUCCESS;
1942}
1943
Michal Vasko14676352020-05-29 11:35:55 +02001944/* just like lyxp_check_token() but tests for 2 tokens */
1945static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02001946exp_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 +02001947 enum lyxp_token want_tok2)
Michal Vasko14676352020-05-29 11:35:55 +02001948{
Michal Vasko004d3152020-06-11 19:59:22 +02001949 if (exp->used == tok_idx) {
Michal Vasko14676352020-05-29 11:35:55 +02001950 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001951 LOGVAL(ctx, LY_VCODE_XP_EOF);
Michal Vasko14676352020-05-29 11:35:55 +02001952 }
1953 return LY_EINCOMPLETE;
1954 }
1955
Michal Vasko004d3152020-06-11 19:59:22 +02001956 if ((exp->tokens[tok_idx] != want_tok1) && (exp->tokens[tok_idx] != want_tok2)) {
Michal Vasko14676352020-05-29 11:35:55 +02001957 if (ctx) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02001958 LOGVAL(ctx, LY_VCODE_XP_INTOK, lyxp_token2str(exp->tokens[tok_idx]),
Michal Vasko0b468e62020-10-19 09:33:04 +02001959 &exp->expr[exp->tok_pos[tok_idx]]);
Michal Vasko14676352020-05-29 11:35:55 +02001960 }
1961 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001962 }
1963
1964 return LY_SUCCESS;
1965}
1966
Michal Vasko4911eeb2021-06-28 11:23:05 +02001967LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02001968lyxp_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 +02001969 enum lyxp_token want_tok2)
1970{
1971 LY_CHECK_RET(exp_check_token2(ctx, exp, *tok_idx, want_tok1, want_tok2));
1972
1973 /* skip the token */
1974 ++(*tok_idx);
1975
1976 return LY_SUCCESS;
1977}
1978
Michal Vasko03ff5a72019-09-11 13:49:33 +02001979/**
1980 * @brief Stack operation push on the repeat array.
1981 *
1982 * @param[in] exp Expression to use.
Michal Vasko831e3bd2023-04-24 10:43:38 +02001983 * @param[in] tok_idx Position in the expresion @p exp.
1984 * @param[in] repeat_expr_type Repeated expression type, this value is pushed.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001985 */
1986static void
Michal Vasko831e3bd2023-04-24 10:43:38 +02001987exp_repeat_push(struct lyxp_expr *exp, uint32_t tok_idx, enum lyxp_expr_type repeat_expr_type)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001988{
Michal Vaskodd528af2022-08-08 14:35:07 +02001989 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001990
Michal Vasko004d3152020-06-11 19:59:22 +02001991 if (exp->repeat[tok_idx]) {
Radek Krejci1e008d22020-08-17 11:37:37 +02001992 for (i = 0; exp->repeat[tok_idx][i]; ++i) {}
Michal Vasko004d3152020-06-11 19:59:22 +02001993 exp->repeat[tok_idx] = realloc(exp->repeat[tok_idx], (i + 2) * sizeof *exp->repeat[tok_idx]);
1994 LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), );
Michal Vasko831e3bd2023-04-24 10:43:38 +02001995 exp->repeat[tok_idx][i] = repeat_expr_type;
Michal Vasko004d3152020-06-11 19:59:22 +02001996 exp->repeat[tok_idx][i + 1] = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001997 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02001998 exp->repeat[tok_idx] = calloc(2, sizeof *exp->repeat[tok_idx]);
1999 LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), );
Michal Vasko831e3bd2023-04-24 10:43:38 +02002000 exp->repeat[tok_idx][0] = repeat_expr_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002001 }
2002}
2003
2004/**
2005 * @brief Reparse Predicate. Logs directly on error.
2006 *
2007 * [7] Predicate ::= '[' Expr ']'
2008 *
2009 * @param[in] ctx Context for logging.
2010 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002011 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002012 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002013 * @return LY_ERR
2014 */
2015static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02002016reparse_predicate(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint32_t *tok_idx, uint32_t depth)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002017{
2018 LY_ERR rc;
2019
Michal Vasko004d3152020-06-11 19:59:22 +02002020 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002021 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002022 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002023
aPiecekbf968d92021-05-27 14:35:05 +02002024 rc = reparse_or_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002025 LY_CHECK_RET(rc);
2026
Michal Vasko004d3152020-06-11 19:59:22 +02002027 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002028 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002029 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002030
2031 return LY_SUCCESS;
2032}
2033
2034/**
2035 * @brief Reparse RelativeLocationPath. Logs directly on error.
2036 *
2037 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
2038 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
2039 * [6] NodeTest ::= NameTest | NodeType '(' ')'
2040 *
2041 * @param[in] ctx Context for logging.
2042 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002043 * @param[in] tok_idx Position in the expression \p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002044 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002045 * @return LY_ERR (LY_EINCOMPLETE on forward reference)
2046 */
2047static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02002048reparse_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 +02002049{
2050 LY_ERR rc;
2051
Michal Vasko004d3152020-06-11 19:59:22 +02002052 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002053 LY_CHECK_RET(rc);
2054
2055 goto step;
2056 do {
2057 /* '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02002058 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002059
Michal Vasko004d3152020-06-11 19:59:22 +02002060 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002061 LY_CHECK_RET(rc);
2062step:
2063 /* Step */
Michal Vasko004d3152020-06-11 19:59:22 +02002064 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002065 case LYXP_TOKEN_DOT:
Michal Vasko004d3152020-06-11 19:59:22 +02002066 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002067 break;
2068
2069 case LYXP_TOKEN_DDOT:
Michal Vasko004d3152020-06-11 19:59:22 +02002070 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002071 break;
2072
Michal Vasko49fec8e2022-05-24 10:28:33 +02002073 case LYXP_TOKEN_AXISNAME:
2074 ++(*tok_idx);
2075
2076 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_DCOLON);
2077 LY_CHECK_RET(rc);
2078
2079 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02002080 case LYXP_TOKEN_AT:
Michal Vasko004d3152020-06-11 19:59:22 +02002081 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002082
Michal Vasko004d3152020-06-11 19:59:22 +02002083 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002084 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002085 if ((exp->tokens[*tok_idx] != LYXP_TOKEN_NAMETEST) && (exp->tokens[*tok_idx] != LYXP_TOKEN_NODETYPE)) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02002086 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 +02002087 return LY_EVALID;
2088 }
Michal Vasko49fec8e2022-05-24 10:28:33 +02002089 if (exp->tokens[*tok_idx] == LYXP_TOKEN_NODETYPE) {
2090 goto reparse_nodetype;
2091 }
Radek Krejci0f969882020-08-21 16:56:47 +02002092 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02002093 case LYXP_TOKEN_NAMETEST:
Michal Vasko004d3152020-06-11 19:59:22 +02002094 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002095 goto reparse_predicate;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002096
2097 case LYXP_TOKEN_NODETYPE:
Michal Vasko49fec8e2022-05-24 10:28:33 +02002098reparse_nodetype:
Michal Vasko004d3152020-06-11 19:59:22 +02002099 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002100
2101 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02002102 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002103 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002104 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002105
2106 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02002107 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002108 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002109 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002110
2111reparse_predicate:
2112 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002113 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
aPiecekbf968d92021-05-27 14:35:05 +02002114 rc = reparse_predicate(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002115 LY_CHECK_RET(rc);
2116 }
2117 break;
2118 default:
Michal Vasko49fec8e2022-05-24 10:28:33 +02002119 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 +02002120 return LY_EVALID;
2121 }
Michal Vasko004d3152020-06-11 19:59:22 +02002122 } while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02002123
2124 return LY_SUCCESS;
2125}
2126
2127/**
2128 * @brief Reparse AbsoluteLocationPath. Logs directly on error.
2129 *
2130 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
2131 *
2132 * @param[in] ctx Context for logging.
2133 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002134 * @param[in] tok_idx Position in the expression \p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002135 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002136 * @return LY_ERR
2137 */
2138static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02002139reparse_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 +02002140{
2141 LY_ERR rc;
2142
Michal Vasko004d3152020-06-11 19:59:22 +02002143 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 +02002144
2145 /* '/' RelativeLocationPath? */
Michal Vasko004d3152020-06-11 19:59:22 +02002146 if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002147 /* '/' */
Michal Vasko004d3152020-06-11 19:59:22 +02002148 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002149
Michal Vasko004d3152020-06-11 19:59:22 +02002150 if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002151 return LY_SUCCESS;
2152 }
Michal Vasko004d3152020-06-11 19:59:22 +02002153 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002154 case LYXP_TOKEN_DOT:
2155 case LYXP_TOKEN_DDOT:
Michal Vasko49fec8e2022-05-24 10:28:33 +02002156 case LYXP_TOKEN_AXISNAME:
Michal Vasko03ff5a72019-09-11 13:49:33 +02002157 case LYXP_TOKEN_AT:
2158 case LYXP_TOKEN_NAMETEST:
2159 case LYXP_TOKEN_NODETYPE:
aPiecekbf968d92021-05-27 14:35:05 +02002160 rc = reparse_relative_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002161 LY_CHECK_RET(rc);
Radek Krejci0f969882020-08-21 16:56:47 +02002162 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02002163 default:
2164 break;
2165 }
2166
Michal Vasko03ff5a72019-09-11 13:49:33 +02002167 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02002168 /* '//' RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002169 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002170
aPiecekbf968d92021-05-27 14:35:05 +02002171 rc = reparse_relative_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002172 LY_CHECK_RET(rc);
2173 }
2174
2175 return LY_SUCCESS;
2176}
2177
2178/**
2179 * @brief Reparse FunctionCall. Logs directly on error.
2180 *
2181 * [9] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
2182 *
2183 * @param[in] ctx Context for logging.
2184 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002185 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002186 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002187 * @return LY_ERR
2188 */
2189static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02002190reparse_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 +02002191{
Radek Krejci1deb5be2020-08-26 16:43:36 +02002192 int8_t min_arg_count = -1;
Michal Vaskodd528af2022-08-08 14:35:07 +02002193 uint32_t arg_count, max_arg_count = 0, func_tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002194 LY_ERR rc;
2195
Michal Vasko004d3152020-06-11 19:59:22 +02002196 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_FUNCNAME);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002197 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002198 func_tok_idx = *tok_idx;
2199 switch (exp->tok_len[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002200 case 3:
Michal Vasko004d3152020-06-11 19:59:22 +02002201 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002202 min_arg_count = 1;
2203 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002204 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002205 min_arg_count = 1;
2206 max_arg_count = 1;
2207 }
2208 break;
2209 case 4:
Michal Vasko004d3152020-06-11 19:59:22 +02002210 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002211 min_arg_count = 1;
2212 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002213 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002214 min_arg_count = 0;
2215 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002216 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002217 min_arg_count = 0;
2218 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002219 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002220 min_arg_count = 0;
2221 max_arg_count = 0;
2222 }
2223 break;
2224 case 5:
Michal Vasko004d3152020-06-11 19:59:22 +02002225 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002226 min_arg_count = 1;
2227 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002228 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002229 min_arg_count = 0;
2230 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002231 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002232 min_arg_count = 1;
2233 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002234 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002235 min_arg_count = 1;
2236 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002237 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002238 min_arg_count = 1;
2239 max_arg_count = 1;
2240 }
2241 break;
2242 case 6:
Michal Vasko004d3152020-06-11 19:59:22 +02002243 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002244 min_arg_count = 2;
Radek Krejci1deb5be2020-08-26 16:43:36 +02002245 max_arg_count = UINT32_MAX;
Michal Vasko004d3152020-06-11 19:59:22 +02002246 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002247 min_arg_count = 0;
2248 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002249 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002250 min_arg_count = 0;
2251 max_arg_count = 1;
2252 }
2253 break;
2254 case 7:
Michal Vasko004d3152020-06-11 19:59:22 +02002255 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002256 min_arg_count = 1;
2257 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002258 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002259 min_arg_count = 1;
2260 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002261 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002262 min_arg_count = 0;
2263 max_arg_count = 0;
2264 }
2265 break;
2266 case 8:
Michal Vasko004d3152020-06-11 19:59:22 +02002267 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002268 min_arg_count = 2;
2269 max_arg_count = 2;
Michal Vasko004d3152020-06-11 19:59:22 +02002270 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002271 min_arg_count = 0;
2272 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002273 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "re-match", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002274 min_arg_count = 2;
2275 max_arg_count = 2;
2276 }
2277 break;
2278 case 9:
Michal Vasko004d3152020-06-11 19:59:22 +02002279 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002280 min_arg_count = 2;
2281 max_arg_count = 3;
Michal Vasko004d3152020-06-11 19:59:22 +02002282 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002283 min_arg_count = 3;
2284 max_arg_count = 3;
2285 }
2286 break;
2287 case 10:
Michal Vasko004d3152020-06-11 19:59:22 +02002288 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002289 min_arg_count = 0;
2290 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002291 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002292 min_arg_count = 1;
2293 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002294 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002295 min_arg_count = 2;
2296 max_arg_count = 2;
2297 }
2298 break;
2299 case 11:
Michal Vasko004d3152020-06-11 19:59:22 +02002300 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002301 min_arg_count = 2;
2302 max_arg_count = 2;
2303 }
2304 break;
2305 case 12:
Michal Vasko004d3152020-06-11 19:59:22 +02002306 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002307 min_arg_count = 2;
2308 max_arg_count = 2;
2309 }
2310 break;
2311 case 13:
Michal Vasko004d3152020-06-11 19:59:22 +02002312 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002313 min_arg_count = 0;
2314 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002315 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string-length", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002316 min_arg_count = 0;
2317 max_arg_count = 1;
2318 }
2319 break;
2320 case 15:
Michal Vasko004d3152020-06-11 19:59:22 +02002321 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002322 min_arg_count = 0;
2323 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002324 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-after", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002325 min_arg_count = 2;
2326 max_arg_count = 2;
2327 }
2328 break;
2329 case 16:
Michal Vasko004d3152020-06-11 19:59:22 +02002330 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002331 min_arg_count = 2;
2332 max_arg_count = 2;
2333 }
2334 break;
2335 case 20:
Michal Vasko004d3152020-06-11 19:59:22 +02002336 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from-or-self", 20)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002337 min_arg_count = 2;
2338 max_arg_count = 2;
2339 }
2340 break;
2341 }
2342 if (min_arg_count == -1) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002343 LOGVAL(ctx, LY_VCODE_XP_INFUNC, exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002344 return LY_EINVAL;
2345 }
Michal Vasko004d3152020-06-11 19:59:22 +02002346 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002347
2348 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02002349 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002350 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002351 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002352
2353 /* ( Expr ( ',' Expr )* )? */
2354 arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002355 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002356 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002357 if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002358 ++arg_count;
aPiecekbf968d92021-05-27 14:35:05 +02002359 rc = reparse_or_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002360 LY_CHECK_RET(rc);
2361 }
Michal Vasko004d3152020-06-11 19:59:22 +02002362 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) {
2363 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002364
2365 ++arg_count;
aPiecekbf968d92021-05-27 14:35:05 +02002366 rc = reparse_or_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002367 LY_CHECK_RET(rc);
2368 }
2369
2370 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02002371 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002372 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002373 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002374
Radek Krejci857189e2020-09-01 13:26:36 +02002375 if ((arg_count < (uint32_t)min_arg_count) || (arg_count > max_arg_count)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002376 LOGVAL(ctx, LY_VCODE_XP_INARGCOUNT, arg_count, exp->tok_len[func_tok_idx], &exp->expr[exp->tok_pos[func_tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002377 return LY_EVALID;
2378 }
2379
2380 return LY_SUCCESS;
2381}
2382
2383/**
2384 * @brief Reparse PathExpr. Logs directly on error.
2385 *
2386 * [10] PathExpr ::= LocationPath | PrimaryExpr Predicate*
2387 * | PrimaryExpr Predicate* '/' RelativeLocationPath
2388 * | PrimaryExpr Predicate* '//' RelativeLocationPath
2389 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
aPiecekfba75362021-10-07 12:39:48 +02002390 * [8] PrimaryExpr ::= VariableReference | '(' Expr ')' | Literal | Number | FunctionCall
Michal Vasko03ff5a72019-09-11 13:49:33 +02002391 *
2392 * @param[in] ctx Context for logging.
2393 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002394 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002395 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002396 * @return LY_ERR
2397 */
2398static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02002399reparse_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 +02002400{
2401 LY_ERR rc;
2402
Michal Vasko004d3152020-06-11 19:59:22 +02002403 if (lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko14676352020-05-29 11:35:55 +02002404 return LY_EVALID;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002405 }
2406
Michal Vasko004d3152020-06-11 19:59:22 +02002407 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002408 case LYXP_TOKEN_PAR1:
2409 /* '(' Expr ')' Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002410 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002411
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 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002416 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002417 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002418 goto predicate;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002419 case LYXP_TOKEN_DOT:
2420 case LYXP_TOKEN_DDOT:
Michal Vasko49fec8e2022-05-24 10:28:33 +02002421 case LYXP_TOKEN_AXISNAME:
Michal Vasko03ff5a72019-09-11 13:49:33 +02002422 case LYXP_TOKEN_AT:
2423 case LYXP_TOKEN_NAMETEST:
2424 case LYXP_TOKEN_NODETYPE:
2425 /* RelativeLocationPath */
aPiecekbf968d92021-05-27 14:35:05 +02002426 rc = reparse_relative_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002427 LY_CHECK_RET(rc);
2428 break;
aPiecekfba75362021-10-07 12:39:48 +02002429 case LYXP_TOKEN_VARREF:
2430 /* VariableReference */
2431 ++(*tok_idx);
2432 goto predicate;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002433 case LYXP_TOKEN_FUNCNAME:
2434 /* FunctionCall */
aPiecekbf968d92021-05-27 14:35:05 +02002435 rc = reparse_function_call(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002436 LY_CHECK_RET(rc);
2437 goto predicate;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002438 case LYXP_TOKEN_OPER_PATH:
2439 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +02002440 /* AbsoluteLocationPath */
aPiecekbf968d92021-05-27 14:35:05 +02002441 rc = reparse_absolute_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002442 LY_CHECK_RET(rc);
2443 break;
2444 case LYXP_TOKEN_LITERAL:
2445 /* Literal */
Michal Vasko004d3152020-06-11 19:59:22 +02002446 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002447 goto predicate;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002448 case LYXP_TOKEN_NUMBER:
2449 /* Number */
Michal Vasko004d3152020-06-11 19:59:22 +02002450 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002451 goto predicate;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002452 default:
Michal Vasko49fec8e2022-05-24 10:28:33 +02002453 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 +02002454 return LY_EVALID;
2455 }
2456
2457 return LY_SUCCESS;
2458
2459predicate:
2460 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002461 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
aPiecekbf968d92021-05-27 14:35:05 +02002462 rc = reparse_predicate(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002463 LY_CHECK_RET(rc);
2464 }
2465
2466 /* ('/' or '//') RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002467 if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002468
2469 /* '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02002470 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002471
aPiecekbf968d92021-05-27 14:35:05 +02002472 rc = reparse_relative_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002473 LY_CHECK_RET(rc);
2474 }
2475
2476 return LY_SUCCESS;
2477}
2478
2479/**
2480 * @brief Reparse UnaryExpr. Logs directly on error.
2481 *
2482 * [17] UnaryExpr ::= UnionExpr | '-' UnaryExpr
2483 * [18] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
2484 *
2485 * @param[in] ctx Context for logging.
2486 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002487 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002488 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002489 * @return LY_ERR
2490 */
2491static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02002492reparse_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 +02002493{
Michal Vaskodd528af2022-08-08 14:35:07 +02002494 uint32_t prev_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002495 LY_ERR rc;
2496
2497 /* ('-')* */
Michal Vasko004d3152020-06-11 19:59:22 +02002498 prev_exp = *tok_idx;
Michal Vasko69730152020-10-09 16:30:07 +02002499 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2500 (exp->expr[exp->tok_pos[*tok_idx]] == '-')) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002501 exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNARY);
Michal Vasko004d3152020-06-11 19:59:22 +02002502 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002503 }
2504
2505 /* PathExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002506 prev_exp = *tok_idx;
aPiecekbf968d92021-05-27 14:35:05 +02002507 rc = reparse_path_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002508 LY_CHECK_RET(rc);
2509
2510 /* ('|' PathExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002511 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_UNI)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002512 exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNION);
Michal Vasko004d3152020-06-11 19:59:22 +02002513 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002514
aPiecekbf968d92021-05-27 14:35:05 +02002515 rc = reparse_path_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002516 LY_CHECK_RET(rc);
2517 }
2518
2519 return LY_SUCCESS;
2520}
2521
2522/**
2523 * @brief Reparse AdditiveExpr. Logs directly on error.
2524 *
2525 * [15] AdditiveExpr ::= MultiplicativeExpr
2526 * | AdditiveExpr '+' MultiplicativeExpr
2527 * | AdditiveExpr '-' MultiplicativeExpr
2528 * [16] MultiplicativeExpr ::= UnaryExpr
2529 * | MultiplicativeExpr '*' UnaryExpr
2530 * | MultiplicativeExpr 'div' UnaryExpr
2531 * | MultiplicativeExpr 'mod' UnaryExpr
2532 *
2533 * @param[in] ctx Context for logging.
2534 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002535 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002536 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002537 * @return LY_ERR
2538 */
2539static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02002540reparse_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 +02002541{
Michal Vaskodd528af2022-08-08 14:35:07 +02002542 uint32_t prev_add_exp, prev_mul_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002543 LY_ERR rc;
2544
Michal Vasko004d3152020-06-11 19:59:22 +02002545 prev_add_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002546 goto reparse_multiplicative_expr;
2547
2548 /* ('+' / '-' MultiplicativeExpr)* */
Michal Vasko69730152020-10-09 16:30:07 +02002549 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2550 ((exp->expr[exp->tok_pos[*tok_idx]] == '+') || (exp->expr[exp->tok_pos[*tok_idx]] == '-'))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002551 exp_repeat_push(exp, prev_add_exp, LYXP_EXPR_ADDITIVE);
Michal Vasko004d3152020-06-11 19:59:22 +02002552 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002553
2554reparse_multiplicative_expr:
2555 /* UnaryExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002556 prev_mul_exp = *tok_idx;
aPiecekbf968d92021-05-27 14:35:05 +02002557 rc = reparse_unary_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002558 LY_CHECK_RET(rc);
2559
2560 /* ('*' / 'div' / 'mod' UnaryExpr)* */
Michal Vasko69730152020-10-09 16:30:07 +02002561 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2562 ((exp->expr[exp->tok_pos[*tok_idx]] == '*') || (exp->tok_len[*tok_idx] == 3))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002563 exp_repeat_push(exp, prev_mul_exp, LYXP_EXPR_MULTIPLICATIVE);
Michal Vasko004d3152020-06-11 19:59:22 +02002564 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002565
aPiecekbf968d92021-05-27 14:35:05 +02002566 rc = reparse_unary_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002567 LY_CHECK_RET(rc);
2568 }
2569 }
2570
2571 return LY_SUCCESS;
2572}
2573
2574/**
2575 * @brief Reparse EqualityExpr. Logs directly on error.
2576 *
2577 * [13] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
2578 * | EqualityExpr '!=' RelationalExpr
2579 * [14] RelationalExpr ::= AdditiveExpr
2580 * | RelationalExpr '<' AdditiveExpr
2581 * | RelationalExpr '>' AdditiveExpr
2582 * | RelationalExpr '<=' AdditiveExpr
2583 * | RelationalExpr '>=' AdditiveExpr
2584 *
2585 * @param[in] ctx Context for logging.
2586 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002587 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002588 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002589 * @return LY_ERR
2590 */
2591static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02002592reparse_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 +02002593{
Michal Vaskodd528af2022-08-08 14:35:07 +02002594 uint32_t prev_eq_exp, prev_rel_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002595 LY_ERR rc;
2596
Michal Vasko004d3152020-06-11 19:59:22 +02002597 prev_eq_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002598 goto reparse_additive_expr;
2599
2600 /* ('=' / '!=' RelationalExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002601 while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_EQUAL, LYXP_TOKEN_OPER_NEQUAL)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002602 exp_repeat_push(exp, prev_eq_exp, LYXP_EXPR_EQUALITY);
Michal Vasko004d3152020-06-11 19:59:22 +02002603 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002604
2605reparse_additive_expr:
2606 /* AdditiveExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002607 prev_rel_exp = *tok_idx;
aPiecekbf968d92021-05-27 14:35:05 +02002608 rc = reparse_additive_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002609 LY_CHECK_RET(rc);
2610
2611 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002612 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_COMP)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002613 exp_repeat_push(exp, prev_rel_exp, LYXP_EXPR_RELATIONAL);
Michal Vasko004d3152020-06-11 19:59:22 +02002614 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002615
aPiecekbf968d92021-05-27 14:35:05 +02002616 rc = reparse_additive_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002617 LY_CHECK_RET(rc);
2618 }
2619 }
2620
2621 return LY_SUCCESS;
2622}
2623
2624/**
2625 * @brief Reparse OrExpr. Logs directly on error.
2626 *
2627 * [11] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
2628 * [12] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
2629 *
2630 * @param[in] ctx Context for logging.
2631 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002632 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002633 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002634 * @return LY_ERR
2635 */
2636static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02002637reparse_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 +02002638{
Michal Vaskodd528af2022-08-08 14:35:07 +02002639 uint32_t prev_or_exp, prev_and_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002640 LY_ERR rc;
2641
aPiecekbf968d92021-05-27 14:35:05 +02002642 ++depth;
2643 LY_CHECK_ERR_RET(depth > LYXP_MAX_BLOCK_DEPTH, LOGVAL(ctx, LY_VCODE_XP_DEPTH), LY_EINVAL);
2644
Michal Vasko004d3152020-06-11 19:59:22 +02002645 prev_or_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002646 goto reparse_equality_expr;
2647
2648 /* ('or' AndExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002649 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 +02002650 exp_repeat_push(exp, prev_or_exp, LYXP_EXPR_OR);
Michal Vasko004d3152020-06-11 19:59:22 +02002651 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002652
2653reparse_equality_expr:
2654 /* EqualityExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002655 prev_and_exp = *tok_idx;
aPiecekbf968d92021-05-27 14:35:05 +02002656 rc = reparse_equality_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002657 LY_CHECK_RET(rc);
2658
2659 /* ('and' EqualityExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002660 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 +02002661 exp_repeat_push(exp, prev_and_exp, LYXP_EXPR_AND);
Michal Vasko004d3152020-06-11 19:59:22 +02002662 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002663
aPiecekbf968d92021-05-27 14:35:05 +02002664 rc = reparse_equality_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002665 LY_CHECK_RET(rc);
2666 }
2667 }
2668
2669 return LY_SUCCESS;
2670}
Radek Krejcib1646a92018-11-02 16:08:26 +01002671
2672/**
2673 * @brief Parse NCName.
2674 *
2675 * @param[in] ncname Name to parse.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002676 * @return Length of @p ncname valid bytes.
Radek Krejcib1646a92018-11-02 16:08:26 +01002677 */
Michal Vasko49fec8e2022-05-24 10:28:33 +02002678static ssize_t
Radek Krejcib1646a92018-11-02 16:08:26 +01002679parse_ncname(const char *ncname)
2680{
Radek Krejci1deb5be2020-08-26 16:43:36 +02002681 uint32_t uc;
Radek Krejcid4270262019-01-07 15:07:25 +01002682 size_t size;
Michal Vasko49fec8e2022-05-24 10:28:33 +02002683 ssize_t len = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01002684
2685 LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), 0);
2686 if (!is_xmlqnamestartchar(uc) || (uc == ':')) {
2687 return len;
2688 }
2689
2690 do {
2691 len += size;
Radek Krejci9a564c92019-01-07 14:53:57 +01002692 if (!*ncname) {
2693 break;
2694 }
Radek Krejcid4270262019-01-07 15:07:25 +01002695 LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), -len);
Radek Krejcib1646a92018-11-02 16:08:26 +01002696 } while (is_xmlqnamechar(uc) && (uc != ':'));
2697
2698 return len;
2699}
2700
2701/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02002702 * @brief Add @p token into the expression @p exp.
Radek Krejcib1646a92018-11-02 16:08:26 +01002703 *
Michal Vasko03ff5a72019-09-11 13:49:33 +02002704 * @param[in] ctx Context for logging.
Radek Krejcib1646a92018-11-02 16:08:26 +01002705 * @param[in] exp Expression to use.
2706 * @param[in] token Token to add.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002707 * @param[in] tok_pos Token position in the XPath expression.
Radek Krejcib1646a92018-11-02 16:08:26 +01002708 * @param[in] tok_len Token length in the XPath expression.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002709 * @return LY_ERR
Radek Krejcib1646a92018-11-02 16:08:26 +01002710 */
2711static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02002712exp_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 +01002713{
2714 uint32_t prev;
2715
2716 if (exp->used == exp->size) {
2717 prev = exp->size;
2718 exp->size += LYXP_EXPR_SIZE_STEP;
2719 if (prev > exp->size) {
2720 LOGINT(ctx);
2721 return LY_EINT;
2722 }
2723
2724 exp->tokens = ly_realloc(exp->tokens, exp->size * sizeof *exp->tokens);
2725 LY_CHECK_ERR_RET(!exp->tokens, LOGMEM(ctx), LY_EMEM);
2726 exp->tok_pos = ly_realloc(exp->tok_pos, exp->size * sizeof *exp->tok_pos);
2727 LY_CHECK_ERR_RET(!exp->tok_pos, LOGMEM(ctx), LY_EMEM);
2728 exp->tok_len = ly_realloc(exp->tok_len, exp->size * sizeof *exp->tok_len);
2729 LY_CHECK_ERR_RET(!exp->tok_len, LOGMEM(ctx), LY_EMEM);
2730 }
2731
2732 exp->tokens[exp->used] = token;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002733 exp->tok_pos[exp->used] = tok_pos;
Radek Krejcib1646a92018-11-02 16:08:26 +01002734 exp->tok_len[exp->used] = tok_len;
2735 ++exp->used;
2736 return LY_SUCCESS;
2737}
2738
2739void
Michal Vasko14676352020-05-29 11:35:55 +02002740lyxp_expr_free(const struct ly_ctx *ctx, struct lyxp_expr *expr)
Radek Krejcib1646a92018-11-02 16:08:26 +01002741{
Michal Vaskodd528af2022-08-08 14:35:07 +02002742 uint32_t i;
Radek Krejcib1646a92018-11-02 16:08:26 +01002743
2744 if (!expr) {
2745 return;
2746 }
2747
2748 lydict_remove(ctx, expr->expr);
2749 free(expr->tokens);
2750 free(expr->tok_pos);
2751 free(expr->tok_len);
2752 if (expr->repeat) {
2753 for (i = 0; i < expr->used; ++i) {
2754 free(expr->repeat[i]);
2755 }
2756 }
2757 free(expr->repeat);
2758 free(expr);
2759}
2760
Michal Vasko49fec8e2022-05-24 10:28:33 +02002761/**
2762 * @brief Parse Axis name.
2763 *
2764 * @param[in] str String to parse.
2765 * @param[in] str_len Length of @p str.
2766 * @return LY_SUCCESS if an axis.
2767 * @return LY_ENOT otherwise.
2768 */
2769static LY_ERR
2770expr_parse_axis(const char *str, size_t str_len)
2771{
2772 switch (str_len) {
2773 case 4:
2774 if (!strncmp("self", str, str_len)) {
2775 return LY_SUCCESS;
2776 }
2777 break;
2778 case 5:
2779 if (!strncmp("child", str, str_len)) {
2780 return LY_SUCCESS;
2781 }
2782 break;
2783 case 6:
2784 if (!strncmp("parent", str, str_len)) {
2785 return LY_SUCCESS;
2786 }
2787 break;
2788 case 8:
2789 if (!strncmp("ancestor", str, str_len)) {
2790 return LY_SUCCESS;
2791 }
2792 break;
2793 case 9:
2794 if (!strncmp("attribute", str, str_len)) {
2795 return LY_SUCCESS;
2796 } else if (!strncmp("following", str, str_len)) {
2797 return LY_SUCCESS;
2798 } else if (!strncmp("namespace", str, str_len)) {
2799 LOGERR(NULL, LY_EINVAL, "Axis \"namespace\" not supported.");
2800 return LY_ENOT;
2801 } else if (!strncmp("preceding", str, str_len)) {
2802 return LY_SUCCESS;
2803 }
2804 break;
2805 case 10:
2806 if (!strncmp("descendant", str, str_len)) {
2807 return LY_SUCCESS;
2808 }
2809 break;
2810 case 16:
2811 if (!strncmp("ancestor-or-self", str, str_len)) {
2812 return LY_SUCCESS;
2813 }
2814 break;
2815 case 17:
2816 if (!strncmp("following-sibling", str, str_len)) {
2817 return LY_SUCCESS;
2818 } else if (!strncmp("preceding-sibling", str, str_len)) {
2819 return LY_SUCCESS;
2820 }
2821 break;
2822 case 18:
2823 if (!strncmp("descendant-or-self", str, str_len)) {
2824 return LY_SUCCESS;
2825 }
2826 break;
2827 }
2828
2829 return LY_ENOT;
2830}
2831
Radek Krejcif03a9e22020-09-18 20:09:31 +02002832LY_ERR
2833lyxp_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 +01002834{
Radek Krejcif03a9e22020-09-18 20:09:31 +02002835 LY_ERR ret = LY_SUCCESS;
2836 struct lyxp_expr *expr;
Radek Krejcid4270262019-01-07 15:07:25 +01002837 size_t parsed = 0, tok_len;
Radek Krejcib1646a92018-11-02 16:08:26 +01002838 enum lyxp_token tok_type;
Michal Vasko49fec8e2022-05-24 10:28:33 +02002839 ly_bool prev_func_check = 0, prev_ntype_check = 0, has_axis;
Michal Vaskodd528af2022-08-08 14:35:07 +02002840 uint32_t tok_idx = 0;
Michal Vasko49fec8e2022-05-24 10:28:33 +02002841 ssize_t ncname_len;
Radek Krejcib1646a92018-11-02 16:08:26 +01002842
Radek Krejcif03a9e22020-09-18 20:09:31 +02002843 assert(expr_p);
2844
2845 if (!expr_str[0]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002846 LOGVAL(ctx, LY_VCODE_XP_EOF);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002847 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02002848 }
2849
2850 if (!expr_len) {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002851 expr_len = strlen(expr_str);
Michal Vasko004d3152020-06-11 19:59:22 +02002852 }
Michal Vaskodd528af2022-08-08 14:35:07 +02002853 if (expr_len > UINT32_MAX) {
2854 LOGVAL(ctx, LYVE_XPATH, "XPath expression cannot be longer than %" PRIu32 " characters.", UINT32_MAX);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002855 return LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01002856 }
2857
2858 /* init lyxp_expr structure */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002859 expr = calloc(1, sizeof *expr);
2860 LY_CHECK_ERR_GOTO(!expr, LOGMEM(ctx); ret = LY_EMEM, error);
2861 LY_CHECK_GOTO(ret = lydict_insert(ctx, expr_str, expr_len, &expr->expr), error);
2862 expr->used = 0;
2863 expr->size = LYXP_EXPR_SIZE_START;
2864 expr->tokens = malloc(expr->size * sizeof *expr->tokens);
2865 LY_CHECK_ERR_GOTO(!expr->tokens, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002866
Radek Krejcif03a9e22020-09-18 20:09:31 +02002867 expr->tok_pos = malloc(expr->size * sizeof *expr->tok_pos);
2868 LY_CHECK_ERR_GOTO(!expr->tok_pos, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002869
Radek Krejcif03a9e22020-09-18 20:09:31 +02002870 expr->tok_len = malloc(expr->size * sizeof *expr->tok_len);
2871 LY_CHECK_ERR_GOTO(!expr->tok_len, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002872
Michal Vasko004d3152020-06-11 19:59:22 +02002873 /* make expr 0-terminated */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002874 expr_str = expr->expr;
Michal Vasko004d3152020-06-11 19:59:22 +02002875
Radek Krejcif03a9e22020-09-18 20:09:31 +02002876 while (is_xmlws(expr_str[parsed])) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002877 ++parsed;
2878 }
2879
2880 do {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002881 if (expr_str[parsed] == '(') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002882
2883 /* '(' */
2884 tok_len = 1;
2885 tok_type = LYXP_TOKEN_PAR1;
2886
Michal Vasko49fec8e2022-05-24 10:28:33 +02002887 if (prev_ntype_check && expr->used && (expr->tokens[expr->used - 1] == LYXP_TOKEN_NAMETEST) &&
2888 (((expr->tok_len[expr->used - 1] == 4) &&
2889 (!strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "node", 4) ||
2890 !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "text", 4))) ||
2891 ((expr->tok_len[expr->used - 1] == 7) &&
2892 !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "comment", 7)))) {
2893 /* it is NodeType after all */
2894 expr->tokens[expr->used - 1] = LYXP_TOKEN_NODETYPE;
2895
2896 prev_ntype_check = 0;
2897 prev_func_check = 0;
2898 } else if (prev_func_check && expr->used && (expr->tokens[expr->used - 1] == LYXP_TOKEN_NAMETEST)) {
2899 /* it is FunctionName after all */
2900 expr->tokens[expr->used - 1] = LYXP_TOKEN_FUNCNAME;
2901
2902 prev_ntype_check = 0;
2903 prev_func_check = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01002904 }
2905
Radek Krejcif03a9e22020-09-18 20:09:31 +02002906 } else if (expr_str[parsed] == ')') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002907
2908 /* ')' */
2909 tok_len = 1;
2910 tok_type = LYXP_TOKEN_PAR2;
2911
Radek Krejcif03a9e22020-09-18 20:09:31 +02002912 } else if (expr_str[parsed] == '[') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002913
2914 /* '[' */
2915 tok_len = 1;
2916 tok_type = LYXP_TOKEN_BRACK1;
2917
Radek Krejcif03a9e22020-09-18 20:09:31 +02002918 } else if (expr_str[parsed] == ']') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002919
2920 /* ']' */
2921 tok_len = 1;
2922 tok_type = LYXP_TOKEN_BRACK2;
2923
Radek Krejcif03a9e22020-09-18 20:09:31 +02002924 } else if (!strncmp(&expr_str[parsed], "..", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002925
2926 /* '..' */
2927 tok_len = 2;
2928 tok_type = LYXP_TOKEN_DDOT;
2929
Radek Krejcif03a9e22020-09-18 20:09:31 +02002930 } else if ((expr_str[parsed] == '.') && (!isdigit(expr_str[parsed + 1]))) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002931
2932 /* '.' */
2933 tok_len = 1;
2934 tok_type = LYXP_TOKEN_DOT;
2935
Radek Krejcif03a9e22020-09-18 20:09:31 +02002936 } else if (expr_str[parsed] == '@') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002937
2938 /* '@' */
2939 tok_len = 1;
2940 tok_type = LYXP_TOKEN_AT;
2941
Radek Krejcif03a9e22020-09-18 20:09:31 +02002942 } else if (expr_str[parsed] == ',') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002943
2944 /* ',' */
2945 tok_len = 1;
2946 tok_type = LYXP_TOKEN_COMMA;
2947
Radek Krejcif03a9e22020-09-18 20:09:31 +02002948 } else if (expr_str[parsed] == '\'') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002949
2950 /* Literal with ' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002951 for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\''); ++tok_len) {}
2952 LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0',
Radek Krejci2efc45b2020-12-22 16:25:44 +01002953 LOGVAL(ctx, LY_VCODE_XP_EOE, expr_str[parsed], &expr_str[parsed]); ret = LY_EVALID,
Michal Vasko69730152020-10-09 16:30:07 +02002954 error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002955 ++tok_len;
2956 tok_type = LYXP_TOKEN_LITERAL;
2957
Radek Krejcif03a9e22020-09-18 20:09:31 +02002958 } else if (expr_str[parsed] == '\"') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002959
2960 /* Literal with " */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002961 for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\"'); ++tok_len) {}
2962 LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0',
Radek Krejci2efc45b2020-12-22 16:25:44 +01002963 LOGVAL(ctx, LY_VCODE_XP_EOE, expr_str[parsed], &expr_str[parsed]); ret = LY_EVALID,
Michal Vasko69730152020-10-09 16:30:07 +02002964 error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002965 ++tok_len;
2966 tok_type = LYXP_TOKEN_LITERAL;
2967
Radek Krejcif03a9e22020-09-18 20:09:31 +02002968 } else if ((expr_str[parsed] == '.') || (isdigit(expr_str[parsed]))) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002969
2970 /* Number */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002971 for (tok_len = 0; isdigit(expr_str[parsed + tok_len]); ++tok_len) {}
2972 if (expr_str[parsed + tok_len] == '.') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002973 ++tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02002974 for ( ; isdigit(expr_str[parsed + tok_len]); ++tok_len) {}
Radek Krejcib1646a92018-11-02 16:08:26 +01002975 }
2976 tok_type = LYXP_TOKEN_NUMBER;
2977
aPiecekfba75362021-10-07 12:39:48 +02002978 } else if (expr_str[parsed] == '$') {
2979
2980 /* VariableReference */
2981 parsed++;
Michal Vasko49fec8e2022-05-24 10:28:33 +02002982 ncname_len = parse_ncname(&expr_str[parsed]);
aPiecekfba75362021-10-07 12:39:48 +02002983 LY_CHECK_ERR_GOTO(ncname_len < 1, LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed - ncname_len],
2984 parsed - ncname_len + 1, expr_str); ret = LY_EVALID, error);
2985 tok_len = ncname_len;
2986 LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == ':',
2987 LOGVAL(ctx, LYVE_XPATH, "Variable with prefix is not supported."); ret = LY_EVALID,
2988 error);
2989 tok_type = LYXP_TOKEN_VARREF;
2990
Radek Krejcif03a9e22020-09-18 20:09:31 +02002991 } else if (expr_str[parsed] == '/') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002992
2993 /* Operator '/', '//' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002994 if (!strncmp(&expr_str[parsed], "//", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002995 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002996 tok_type = LYXP_TOKEN_OPER_RPATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002997 } else {
2998 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002999 tok_type = LYXP_TOKEN_OPER_PATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01003000 }
Radek Krejcib1646a92018-11-02 16:08:26 +01003001
Radek Krejcif03a9e22020-09-18 20:09:31 +02003002 } else if (!strncmp(&expr_str[parsed], "!=", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01003003
Michal Vasko3e48bf32020-06-01 08:39:07 +02003004 /* Operator '!=' */
Radek Krejcib1646a92018-11-02 16:08:26 +01003005 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003006 tok_type = LYXP_TOKEN_OPER_NEQUAL;
3007
Radek Krejcif03a9e22020-09-18 20:09:31 +02003008 } else if (!strncmp(&expr_str[parsed], "<=", 2) || !strncmp(&expr_str[parsed], ">=", 2)) {
Michal Vasko3e48bf32020-06-01 08:39:07 +02003009
3010 /* Operator '<=', '>=' */
3011 tok_len = 2;
3012 tok_type = LYXP_TOKEN_OPER_COMP;
Radek Krejcib1646a92018-11-02 16:08:26 +01003013
Radek Krejcif03a9e22020-09-18 20:09:31 +02003014 } else if (expr_str[parsed] == '|') {
Radek Krejcib1646a92018-11-02 16:08:26 +01003015
3016 /* Operator '|' */
3017 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003018 tok_type = LYXP_TOKEN_OPER_UNI;
Radek Krejcib1646a92018-11-02 16:08:26 +01003019
Radek Krejcif03a9e22020-09-18 20:09:31 +02003020 } else if ((expr_str[parsed] == '+') || (expr_str[parsed] == '-')) {
Radek Krejcib1646a92018-11-02 16:08:26 +01003021
3022 /* Operator '+', '-' */
3023 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003024 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01003025
Radek Krejcif03a9e22020-09-18 20:09:31 +02003026 } else if (expr_str[parsed] == '=') {
Radek Krejcib1646a92018-11-02 16:08:26 +01003027
Michal Vasko3e48bf32020-06-01 08:39:07 +02003028 /* Operator '=' */
Radek Krejcib1646a92018-11-02 16:08:26 +01003029 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003030 tok_type = LYXP_TOKEN_OPER_EQUAL;
3031
Radek Krejcif03a9e22020-09-18 20:09:31 +02003032 } else if ((expr_str[parsed] == '<') || (expr_str[parsed] == '>')) {
Michal Vasko3e48bf32020-06-01 08:39:07 +02003033
3034 /* Operator '<', '>' */
3035 tok_len = 1;
3036 tok_type = LYXP_TOKEN_OPER_COMP;
Radek Krejcib1646a92018-11-02 16:08:26 +01003037
Michal Vasko69730152020-10-09 16:30:07 +02003038 } else if (expr->used && (expr->tokens[expr->used - 1] != LYXP_TOKEN_AT) &&
3039 (expr->tokens[expr->used - 1] != LYXP_TOKEN_PAR1) &&
3040 (expr->tokens[expr->used - 1] != LYXP_TOKEN_BRACK1) &&
3041 (expr->tokens[expr->used - 1] != LYXP_TOKEN_COMMA) &&
3042 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_LOG) &&
3043 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_EQUAL) &&
3044 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_NEQUAL) &&
3045 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_COMP) &&
3046 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_MATH) &&
3047 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_UNI) &&
3048 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_PATH) &&
3049 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_RPATH)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01003050
3051 /* Operator '*', 'or', 'and', 'mod', or 'div' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02003052 if (expr_str[parsed] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01003053 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003054 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01003055
Radek Krejcif03a9e22020-09-18 20:09:31 +02003056 } else if (!strncmp(&expr_str[parsed], "or", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01003057 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003058 tok_type = LYXP_TOKEN_OPER_LOG;
Radek Krejcib1646a92018-11-02 16:08:26 +01003059
Radek Krejcif03a9e22020-09-18 20:09:31 +02003060 } else if (!strncmp(&expr_str[parsed], "and", 3)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01003061 tok_len = 3;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003062 tok_type = LYXP_TOKEN_OPER_LOG;
Radek Krejcib1646a92018-11-02 16:08:26 +01003063
Radek Krejcif03a9e22020-09-18 20:09:31 +02003064 } else if (!strncmp(&expr_str[parsed], "mod", 3) || !strncmp(&expr_str[parsed], "div", 3)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01003065 tok_len = 3;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003066 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01003067
Michal Vasko49fec8e2022-05-24 10:28:33 +02003068 } else if (prev_ntype_check || prev_func_check) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003069 LOGVAL(ctx, LYVE_XPATH, "Invalid character 0x%x ('%c'), perhaps \"%.*s\" is supposed to be a function call.",
Michal Vasko69730152020-10-09 16:30:07 +02003070 expr_str[parsed], expr_str[parsed], expr->tok_len[expr->used - 1], &expr->expr[expr->tok_pos[expr->used - 1]]);
Radek Krejcif03a9e22020-09-18 20:09:31 +02003071 ret = LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01003072 goto error;
3073 } else {
Michal Vasko774ce402021-04-14 15:35:06 +02003074 LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed], parsed + 1, expr_str);
Radek Krejcif03a9e22020-09-18 20:09:31 +02003075 ret = LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01003076 goto error;
3077 }
Radek Krejcib1646a92018-11-02 16:08:26 +01003078 } else {
3079
Michal Vasko49fec8e2022-05-24 10:28:33 +02003080 /* (AxisName '::')? ((NCName ':')? '*' | QName) or NodeType/FunctionName */
3081 if (expr_str[parsed] == '*') {
3082 ncname_len = 1;
3083 } else {
3084 ncname_len = parse_ncname(&expr_str[parsed]);
3085 LY_CHECK_ERR_GOTO(ncname_len < 1, LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed - ncname_len],
3086 parsed - ncname_len + 1, expr_str); ret = LY_EVALID, error);
3087 }
Radek Krejcib1646a92018-11-02 16:08:26 +01003088 tok_len = ncname_len;
3089
Michal Vasko49fec8e2022-05-24 10:28:33 +02003090 has_axis = 0;
3091 if (!strncmp(&expr_str[parsed + tok_len], "::", 2)) {
3092 /* axis */
3093 LY_CHECK_ERR_GOTO(expr_parse_axis(&expr_str[parsed], ncname_len),
3094 LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed], parsed + 1, expr_str); ret = LY_EVALID, error);
3095 tok_type = LYXP_TOKEN_AXISNAME;
3096
3097 LY_CHECK_GOTO(ret = exp_add_token(ctx, expr, tok_type, parsed, tok_len), error);
3098 parsed += tok_len;
3099
3100 /* '::' */
3101 tok_len = 2;
3102 tok_type = LYXP_TOKEN_DCOLON;
3103
3104 LY_CHECK_GOTO(ret = exp_add_token(ctx, expr, tok_type, parsed, tok_len), error);
3105 parsed += tok_len;
3106
3107 if (expr_str[parsed] == '*') {
3108 ncname_len = 1;
3109 } else {
3110 ncname_len = parse_ncname(&expr_str[parsed]);
3111 LY_CHECK_ERR_GOTO(ncname_len < 1, LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed - ncname_len],
3112 parsed - ncname_len + 1, expr_str); ret = LY_EVALID, error);
3113 }
3114 tok_len = ncname_len;
3115
3116 has_axis = 1;
3117 }
3118
Radek Krejcif03a9e22020-09-18 20:09:31 +02003119 if (expr_str[parsed + tok_len] == ':') {
Radek Krejcib1646a92018-11-02 16:08:26 +01003120 ++tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02003121 if (expr_str[parsed + tok_len] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01003122 ++tok_len;
3123 } else {
Radek Krejcif03a9e22020-09-18 20:09:31 +02003124 ncname_len = parse_ncname(&expr_str[parsed + tok_len]);
Michal Vaskoe2be5462021-08-04 10:49:42 +02003125 LY_CHECK_ERR_GOTO(ncname_len < 1, LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed - ncname_len],
Michal Vasko774ce402021-04-14 15:35:06 +02003126 parsed - ncname_len + 1, expr_str); ret = LY_EVALID, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01003127 tok_len += ncname_len;
3128 }
Michal Vasko49fec8e2022-05-24 10:28:33 +02003129 /* remove old flags to prevent ambiguities */
3130 prev_ntype_check = 0;
3131 prev_func_check = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01003132 tok_type = LYXP_TOKEN_NAMETEST;
3133 } else {
Michal Vasko49fec8e2022-05-24 10:28:33 +02003134 /* if not '*', there is no prefix so it can still be NodeType/FunctionName, we can't finally decide now */
3135 prev_ntype_check = (expr_str[parsed] == '*') ? 0 : 1;
3136 prev_func_check = (prev_ntype_check && !has_axis) ? 1 : 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01003137 tok_type = LYXP_TOKEN_NAMETEST;
3138 }
3139 }
3140
3141 /* store the token, move on to the next one */
Radek Krejcif03a9e22020-09-18 20:09:31 +02003142 LY_CHECK_GOTO(ret = exp_add_token(ctx, expr, tok_type, parsed, tok_len), error);
Radek Krejcib1646a92018-11-02 16:08:26 +01003143 parsed += tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02003144 while (is_xmlws(expr_str[parsed])) {
Radek Krejcib1646a92018-11-02 16:08:26 +01003145 ++parsed;
3146 }
3147
Radek Krejcif03a9e22020-09-18 20:09:31 +02003148 } while (expr_str[parsed]);
Radek Krejcib1646a92018-11-02 16:08:26 +01003149
Michal Vasko004d3152020-06-11 19:59:22 +02003150 if (reparse) {
3151 /* prealloc repeat */
Radek Krejcif03a9e22020-09-18 20:09:31 +02003152 expr->repeat = calloc(expr->size, sizeof *expr->repeat);
3153 LY_CHECK_ERR_GOTO(!expr->repeat, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01003154
Michal Vasko004d3152020-06-11 19:59:22 +02003155 /* fill repeat */
aPiecekbf968d92021-05-27 14:35:05 +02003156 LY_CHECK_ERR_GOTO(reparse_or_expr(ctx, expr, &tok_idx, 0), ret = LY_EVALID, error);
Radek Krejcif03a9e22020-09-18 20:09:31 +02003157 if (expr->used > tok_idx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003158 LOGVAL(ctx, LYVE_XPATH, "Unparsed characters \"%s\" left at the end of an XPath expression.",
Michal Vasko69730152020-10-09 16:30:07 +02003159 &expr->expr[expr->tok_pos[tok_idx]]);
Radek Krejcif03a9e22020-09-18 20:09:31 +02003160 ret = LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02003161 goto error;
3162 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003163 }
3164
Radek Krejcif03a9e22020-09-18 20:09:31 +02003165 print_expr_struct_debug(expr);
3166 *expr_p = expr;
3167 return LY_SUCCESS;
Radek Krejcib1646a92018-11-02 16:08:26 +01003168
3169error:
Radek Krejcif03a9e22020-09-18 20:09:31 +02003170 lyxp_expr_free(ctx, expr);
3171 return ret;
Radek Krejcib1646a92018-11-02 16:08:26 +01003172}
3173
Michal Vasko1734be92020-09-22 08:55:10 +02003174LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02003175lyxp_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 +02003176 struct lyxp_expr **dup_p)
Michal Vasko004d3152020-06-11 19:59:22 +02003177{
Michal Vasko1734be92020-09-22 08:55:10 +02003178 LY_ERR ret = LY_SUCCESS;
3179 struct lyxp_expr *dup = NULL;
Michal Vaskod59039d2022-09-09 09:07:30 +02003180 uint32_t used = 0, i, j, expr_len;
Michal Vaskoe33134a2022-07-29 14:54:40 +02003181 const char *expr_start;
3182
3183 assert((!start_idx && !end_idx) || ((start_idx < exp->used) && (end_idx < exp->used) && (start_idx <= end_idx)));
Michal Vasko004d3152020-06-11 19:59:22 +02003184
Michal Vasko7f45cf22020-10-01 12:49:44 +02003185 if (!exp) {
3186 goto cleanup;
3187 }
3188
Michal Vaskoe33134a2022-07-29 14:54:40 +02003189 if (!start_idx && !end_idx) {
3190 end_idx = exp->used - 1;
3191 }
3192
3193 expr_start = exp->expr + exp->tok_pos[start_idx];
3194 expr_len = (exp->tok_pos[end_idx] + exp->tok_len[end_idx]) - exp->tok_pos[start_idx];
3195
Michal Vasko004d3152020-06-11 19:59:22 +02003196 dup = calloc(1, sizeof *dup);
Michal Vasko1734be92020-09-22 08:55:10 +02003197 LY_CHECK_ERR_GOTO(!dup, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02003198
Michal Vasko08e9b112021-06-11 15:41:17 +02003199 if (exp->used) {
Michal Vaskoe33134a2022-07-29 14:54:40 +02003200 used = (end_idx - start_idx) + 1;
3201
3202 dup->tokens = malloc(used * sizeof *dup->tokens);
Michal Vasko08e9b112021-06-11 15:41:17 +02003203 LY_CHECK_ERR_GOTO(!dup->tokens, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vaskoe33134a2022-07-29 14:54:40 +02003204 memcpy(dup->tokens, exp->tokens + start_idx, used * sizeof *dup->tokens);
Michal Vasko004d3152020-06-11 19:59:22 +02003205
Michal Vaskoe33134a2022-07-29 14:54:40 +02003206 dup->tok_pos = malloc(used * sizeof *dup->tok_pos);
Michal Vasko08e9b112021-06-11 15:41:17 +02003207 LY_CHECK_ERR_GOTO(!dup->tok_pos, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vaskoe33134a2022-07-29 14:54:40 +02003208 memcpy(dup->tok_pos, exp->tok_pos + start_idx, used * sizeof *dup->tok_pos);
Michal Vasko004d3152020-06-11 19:59:22 +02003209
Michal Vaskoe33134a2022-07-29 14:54:40 +02003210 if (start_idx) {
3211 /* fix the indices in the expression */
3212 for (i = 0; i < used; ++i) {
3213 dup->tok_pos[i] -= expr_start - exp->expr;
3214 }
3215 }
3216
3217 dup->tok_len = malloc(used * sizeof *dup->tok_len);
Michal Vasko08e9b112021-06-11 15:41:17 +02003218 LY_CHECK_ERR_GOTO(!dup->tok_len, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vaskoe33134a2022-07-29 14:54:40 +02003219 memcpy(dup->tok_len, exp->tok_len + start_idx, used * sizeof *dup->tok_len);
Michal Vasko004d3152020-06-11 19:59:22 +02003220
Michal Vasko79a7a872022-06-17 09:00:48 +02003221 if (exp->repeat) {
Michal Vaskoe33134a2022-07-29 14:54:40 +02003222 dup->repeat = malloc(used * sizeof *dup->repeat);
Michal Vasko79a7a872022-06-17 09:00:48 +02003223 LY_CHECK_ERR_GOTO(!dup->repeat, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vaskoe33134a2022-07-29 14:54:40 +02003224 for (i = start_idx; i <= end_idx; ++i) {
Michal Vasko79a7a872022-06-17 09:00:48 +02003225 if (!exp->repeat[i]) {
Michal Vaskoe33134a2022-07-29 14:54:40 +02003226 dup->repeat[i - start_idx] = NULL;
Michal Vasko79a7a872022-06-17 09:00:48 +02003227 } else {
3228 for (j = 0; exp->repeat[i][j]; ++j) {}
3229 /* the ending 0 as well */
3230 ++j;
Michal Vasko004d3152020-06-11 19:59:22 +02003231
Michal Vaskoe33134a2022-07-29 14:54:40 +02003232 dup->repeat[i - start_idx] = malloc(j * sizeof **dup->repeat);
3233 LY_CHECK_ERR_GOTO(!dup->repeat[i - start_idx], LOGMEM(ctx); ret = LY_EMEM, cleanup);
3234 memcpy(dup->repeat[i - start_idx], exp->repeat[i], j * sizeof **dup->repeat);
Michal Vasko79a7a872022-06-17 09:00:48 +02003235 }
Michal Vasko08e9b112021-06-11 15:41:17 +02003236 }
Michal Vasko004d3152020-06-11 19:59:22 +02003237 }
3238 }
3239
Michal Vaskoe33134a2022-07-29 14:54:40 +02003240 dup->used = used;
3241 dup->size = used;
3242
3243 /* copy only subexpression */
3244 LY_CHECK_GOTO(ret = lydict_insert(ctx, expr_start, expr_len, &dup->expr), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02003245
Michal Vasko1734be92020-09-22 08:55:10 +02003246cleanup:
3247 if (ret) {
3248 lyxp_expr_free(ctx, dup);
3249 } else {
3250 *dup_p = dup;
3251 }
3252 return ret;
Michal Vasko004d3152020-06-11 19:59:22 +02003253}
3254
Michal Vasko03ff5a72019-09-11 13:49:33 +02003255/**
3256 * @brief Get the last-added schema node that is currently in the context.
3257 *
3258 * @param[in] set Set to search in.
3259 * @return Last-added schema context node, NULL if no node is in context.
3260 */
3261static struct lysc_node *
3262warn_get_scnode_in_ctx(struct lyxp_set *set)
3263{
3264 uint32_t i;
3265
3266 if (!set || (set->type != LYXP_SET_SCNODE_SET)) {
3267 return NULL;
3268 }
3269
3270 i = set->used;
3271 do {
3272 --i;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003273 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003274 /* if there are more, simply return the first found (last added) */
3275 return set->val.scnodes[i].scnode;
3276 }
3277 } while (i);
3278
3279 return NULL;
3280}
3281
3282/**
3283 * @brief Test whether a type is numeric - integer type or decimal64.
3284 *
3285 * @param[in] type Type to test.
Radek Krejci857189e2020-09-01 13:26:36 +02003286 * @return Boolean value whether @p type is numeric type or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003287 */
Radek Krejci857189e2020-09-01 13:26:36 +02003288static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003289warn_is_numeric_type(struct lysc_type *type)
3290{
3291 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003292 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003293 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003294
3295 switch (type->basetype) {
3296 case LY_TYPE_DEC64:
3297 case LY_TYPE_INT8:
3298 case LY_TYPE_UINT8:
3299 case LY_TYPE_INT16:
3300 case LY_TYPE_UINT16:
3301 case LY_TYPE_INT32:
3302 case LY_TYPE_UINT32:
3303 case LY_TYPE_INT64:
3304 case LY_TYPE_UINT64:
3305 return 1;
3306 case LY_TYPE_UNION:
3307 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003308 LY_ARRAY_FOR(uni->types, u) {
3309 ret = warn_is_numeric_type(uni->types[u]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003310 if (ret) {
3311 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003312 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003313 }
3314 }
3315 /* did not find any suitable type */
3316 return 0;
3317 case LY_TYPE_LEAFREF:
3318 return warn_is_numeric_type(((struct lysc_type_leafref *)type)->realtype);
3319 default:
3320 return 0;
3321 }
3322}
3323
3324/**
3325 * @brief Test whether a type is string-like - no integers, decimal64 or binary.
3326 *
3327 * @param[in] type Type to test.
Radek Krejci857189e2020-09-01 13:26:36 +02003328 * @return Boolean value whether @p type's basetype is string type or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003329 */
Radek Krejci857189e2020-09-01 13:26:36 +02003330static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003331warn_is_string_type(struct lysc_type *type)
3332{
3333 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003334 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003335 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003336
3337 switch (type->basetype) {
3338 case LY_TYPE_BITS:
3339 case LY_TYPE_ENUM:
3340 case LY_TYPE_IDENT:
3341 case LY_TYPE_INST:
3342 case LY_TYPE_STRING:
3343 return 1;
3344 case LY_TYPE_UNION:
3345 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003346 LY_ARRAY_FOR(uni->types, u) {
3347 ret = warn_is_string_type(uni->types[u]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003348 if (ret) {
3349 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003350 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003351 }
3352 }
3353 /* did not find any suitable type */
3354 return 0;
3355 case LY_TYPE_LEAFREF:
3356 return warn_is_string_type(((struct lysc_type_leafref *)type)->realtype);
3357 default:
3358 return 0;
3359 }
3360}
3361
3362/**
3363 * @brief Test whether a type is one specific type.
3364 *
3365 * @param[in] type Type to test.
3366 * @param[in] base Expected type.
Radek Krejci857189e2020-09-01 13:26:36 +02003367 * @return Boolean value whether the given @p type is of the specific basetype @p base.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003368 */
Radek Krejci857189e2020-09-01 13:26:36 +02003369static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003370warn_is_specific_type(struct lysc_type *type, LY_DATA_TYPE base)
3371{
3372 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003373 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003374 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003375
3376 if (type->basetype == base) {
3377 return 1;
3378 } else if (type->basetype == LY_TYPE_UNION) {
3379 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003380 LY_ARRAY_FOR(uni->types, u) {
3381 ret = warn_is_specific_type(uni->types[u], base);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003382 if (ret) {
3383 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003384 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003385 }
3386 }
3387 /* did not find any suitable type */
3388 return 0;
3389 } else if (type->basetype == LY_TYPE_LEAFREF) {
3390 return warn_is_specific_type(((struct lysc_type_leafref *)type)->realtype, base);
3391 }
3392
3393 return 0;
3394}
3395
3396/**
3397 * @brief Get next type of a (union) type.
3398 *
3399 * @param[in] type Base type.
3400 * @param[in] prev_type Previously returned type.
3401 * @return Next type or NULL.
3402 */
3403static struct lysc_type *
3404warn_is_equal_type_next_type(struct lysc_type *type, struct lysc_type *prev_type)
3405{
3406 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003407 ly_bool found = 0;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003408 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003409
Michal Vasko4e55f5a2022-12-14 12:15:00 +01003410 if (type->basetype == LY_TYPE_UNION) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003411 uni = (struct lysc_type_union *)type;
3412 if (!prev_type) {
3413 return uni->types[0];
3414 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003415 LY_ARRAY_FOR(uni->types, u) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003416 if (found) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003417 return uni->types[u];
Michal Vasko03ff5a72019-09-11 13:49:33 +02003418 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003419 if (prev_type == uni->types[u]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003420 found = 1;
3421 }
3422 }
3423 return NULL;
Michal Vasko4e55f5a2022-12-14 12:15:00 +01003424 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003425 if (prev_type) {
3426 assert(type == prev_type);
3427 return NULL;
3428 } else {
3429 return type;
3430 }
3431 }
3432}
3433
3434/**
3435 * @brief Test whether 2 types have a common type.
3436 *
3437 * @param[in] type1 First type.
3438 * @param[in] type2 Second type.
3439 * @return 1 if they do, 0 otherwise.
3440 */
3441static int
3442warn_is_equal_type(struct lysc_type *type1, struct lysc_type *type2)
3443{
3444 struct lysc_type *t1, *rt1, *t2, *rt2;
3445
3446 t1 = NULL;
3447 while ((t1 = warn_is_equal_type_next_type(type1, t1))) {
3448 if (t1->basetype == LY_TYPE_LEAFREF) {
3449 rt1 = ((struct lysc_type_leafref *)t1)->realtype;
3450 } else {
3451 rt1 = t1;
3452 }
3453
3454 t2 = NULL;
3455 while ((t2 = warn_is_equal_type_next_type(type2, t2))) {
3456 if (t2->basetype == LY_TYPE_LEAFREF) {
3457 rt2 = ((struct lysc_type_leafref *)t2)->realtype;
3458 } else {
3459 rt2 = t2;
3460 }
3461
3462 if (rt2->basetype == rt1->basetype) {
3463 /* match found */
3464 return 1;
3465 }
3466 }
3467 }
3468
3469 return 0;
3470}
3471
3472/**
Michal Vaskoaa956522021-11-11 10:45:34 +01003473 * @brief Print warning with information about the XPath subexpression that caused previous warning.
3474 *
3475 * @param[in] ctx Context for logging.
3476 * @param[in] tok_pos Index of the subexpression in the whole expression.
3477 * @param[in] subexpr Subexpression start.
3478 * @param[in] subexpr_len Length of @p subexpr to print.
3479 * @param[in] cur_scnode Expression context node.
3480 */
3481static void
Michal Vaskodd528af2022-08-08 14:35:07 +02003482warn_subexpr_log(const struct ly_ctx *ctx, uint32_t tok_pos, const char *subexpr, int subexpr_len,
Michal Vaskoaa956522021-11-11 10:45:34 +01003483 const struct lysc_node *cur_scnode)
3484{
3485 char *path;
3486
3487 path = lysc_path(cur_scnode, LYSC_PATH_LOG, NULL, 0);
Michal Vaskodd528af2022-08-08 14:35:07 +02003488 LOGWRN(ctx, "Previous warning generated by XPath subexpression[%" PRIu32 "] \"%.*s\" with context node \"%s\".",
Michal Vaskoaa956522021-11-11 10:45:34 +01003489 tok_pos, subexpr_len, subexpr, path);
3490 free(path);
3491}
3492
3493/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02003494 * @brief Check both operands of comparison operators.
3495 *
3496 * @param[in] ctx Context for errors.
3497 * @param[in] set1 First operand set.
3498 * @param[in] set2 Second operand set.
3499 * @param[in] numbers_only Whether accept only numbers or other types are fine too (for '=' and '!=').
3500 * @param[in] expr Start of the expression to print with the warning.
3501 * @param[in] tok_pos Token position.
3502 */
3503static void
Michal Vaskoaa956522021-11-11 10:45:34 +01003504warn_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 +02003505 uint32_t tok_pos)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003506{
3507 struct lysc_node_leaf *node1, *node2;
Radek Krejci857189e2020-09-01 13:26:36 +02003508 ly_bool leaves = 1, warning = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003509
3510 node1 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set1);
3511 node2 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set2);
3512
3513 if (!node1 && !node2) {
3514 /* no node-sets involved, nothing to do */
3515 return;
3516 }
3517
3518 if (node1) {
3519 if (!(node1->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3520 LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node1->nodetype), node1->name);
3521 warning = 1;
3522 leaves = 0;
3523 } else if (numbers_only && !warn_is_numeric_type(node1->type)) {
3524 LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node1->name);
3525 warning = 1;
3526 }
3527 }
3528
3529 if (node2) {
3530 if (!(node2->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3531 LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node2->nodetype), node2->name);
3532 warning = 1;
3533 leaves = 0;
3534 } else if (numbers_only && !warn_is_numeric_type(node2->type)) {
3535 LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node2->name);
3536 warning = 1;
3537 }
3538 }
3539
3540 if (node1 && node2 && leaves && !numbers_only) {
Michal Vasko69730152020-10-09 16:30:07 +02003541 if ((warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type)) ||
3542 (!warn_is_numeric_type(node1->type) && warn_is_numeric_type(node2->type)) ||
3543 (!warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type) &&
3544 !warn_is_equal_type(node1->type, node2->type))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003545 LOGWRN(ctx, "Incompatible types of operands \"%s\" and \"%s\" for comparison.", node1->name, node2->name);
3546 warning = 1;
3547 }
3548 }
3549
3550 if (warning) {
Michal Vaskoaa956522021-11-11 10:45:34 +01003551 warn_subexpr_log(ctx, tok_pos, expr + tok_pos, 20, set1->cur_scnode);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003552 }
3553}
3554
3555/**
3556 * @brief Check that a value is valid for a leaf. If not applicable, does nothing.
3557 *
3558 * @param[in] exp Parsed XPath expression.
3559 * @param[in] set Set with the leaf/leaf-list.
3560 * @param[in] val_exp Index of the value (literal/number) in @p exp.
3561 * @param[in] equal_exp Index of the start of the equality expression in @p exp.
3562 * @param[in] last_equal_exp Index of the end of the equality expression in @p exp.
3563 */
3564static void
Michal Vaskodd528af2022-08-08 14:35:07 +02003565warn_equality_value(const struct lyxp_expr *exp, struct lyxp_set *set, uint32_t val_exp, uint32_t equal_exp,
3566 uint32_t last_equal_exp)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003567{
3568 struct lysc_node *scnode;
3569 struct lysc_type *type;
3570 char *value;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003571 struct lyd_value storage;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003572 LY_ERR rc;
3573 struct ly_err_item *err = NULL;
3574
Michal Vasko69730152020-10-09 16:30:07 +02003575 if ((scnode = warn_get_scnode_in_ctx(set)) && (scnode->nodetype & (LYS_LEAF | LYS_LEAFLIST)) &&
3576 ((exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) || (exp->tokens[val_exp] == LYXP_TOKEN_NUMBER))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003577 /* check that the node can have the specified value */
3578 if (exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) {
3579 value = strndup(exp->expr + exp->tok_pos[val_exp] + 1, exp->tok_len[val_exp] - 2);
3580 } else {
3581 value = strndup(exp->expr + exp->tok_pos[val_exp], exp->tok_len[val_exp]);
3582 }
3583 if (!value) {
3584 LOGMEM(set->ctx);
3585 return;
3586 }
3587
3588 if ((((struct lysc_node_leaf *)scnode)->type->basetype == LY_TYPE_IDENT) && !strchr(value, ':')) {
3589 LOGWRN(set->ctx, "Identityref \"%s\" comparison with identity \"%s\" without prefix, consider adding"
Michal Vasko69730152020-10-09 16:30:07 +02003590 " a prefix or best using \"derived-from(-or-self)()\" functions.", scnode->name, value);
Michal Vaskoaa956522021-11-11 10:45:34 +01003591 warn_subexpr_log(set->ctx, exp->tok_pos[equal_exp], exp->expr + exp->tok_pos[equal_exp],
Radek Krejci0f969882020-08-21 16:56:47 +02003592 (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp],
Michal Vaskoaa956522021-11-11 10:45:34 +01003593 set->cur_scnode);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003594 }
3595
3596 type = ((struct lysc_node_leaf *)scnode)->type;
3597 if (type->basetype != LY_TYPE_IDENT) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003598 rc = type->plugin->store(set->ctx, type, value, strlen(value), 0, set->format, set->prefix_data,
Michal Vasko405cc9e2020-12-01 12:01:27 +01003599 LYD_HINT_DATA, scnode, &storage, NULL, &err);
Michal Vaskobf42e832020-11-23 16:59:42 +01003600 if (rc == LY_EINCOMPLETE) {
3601 rc = LY_SUCCESS;
3602 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003603
3604 if (err) {
3605 LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type (%s).", value, err->msg);
3606 ly_err_free(err);
3607 } else if (rc != LY_SUCCESS) {
3608 LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type.", value);
3609 }
3610 if (rc != LY_SUCCESS) {
Michal Vaskoaa956522021-11-11 10:45:34 +01003611 warn_subexpr_log(set->ctx, exp->tok_pos[equal_exp], exp->expr + exp->tok_pos[equal_exp],
Radek Krejci0f969882020-08-21 16:56:47 +02003612 (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp],
Michal Vaskoaa956522021-11-11 10:45:34 +01003613 set->cur_scnode);
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003614 } else {
3615 type->plugin->free(set->ctx, &storage);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003616 }
3617 }
3618 free(value);
3619 }
3620}
3621
3622/*
3623 * XPath functions
3624 */
3625
3626/**
3627 * @brief Execute the YANG 1.1 bit-is-set(node-set, string) function. Returns LYXP_SET_BOOLEAN
3628 * depending on whether the first node bit value from the second argument is set.
3629 *
3630 * @param[in] args Array of arguments.
3631 * @param[in] arg_count Count of elements in @p args.
3632 * @param[in,out] set Context and result set at the same time.
3633 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003634 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003635 */
3636static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02003637xpath_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 +02003638{
3639 struct lyd_node_term *leaf;
3640 struct lysc_node_leaf *sleaf;
Michal Vasko2588b952021-07-29 07:43:26 +02003641 struct lyd_value_bits *bits;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003642 LY_ERR rc = LY_SUCCESS;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003643 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003644
3645 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003646 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003647 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003648 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3649 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3650 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3651 sleaf->name);
3652 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_BITS)) {
3653 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"bits\".", __func__, sleaf->name);
3654 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003655 }
3656
3657 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3658 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003659 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3660 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003661 } else if (!warn_is_string_type(sleaf->type)) {
3662 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003663 }
3664 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003665 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003666 return rc;
3667 }
3668
Michal Vaskod3678892020-05-21 10:06:58 +02003669 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003670 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 +02003671 return LY_EVALID;
3672 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003673 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003674 LY_CHECK_RET(rc);
3675
3676 set_fill_boolean(set, 0);
Michal Vaskod3678892020-05-21 10:06:58 +02003677 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003678 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
Michal Vasko2588b952021-07-29 07:43:26 +02003679 if ((leaf->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (leaf->value.realtype->basetype == LY_TYPE_BITS)) {
3680 LYD_VALUE_GET(&leaf->value, bits);
3681 LY_ARRAY_FOR(bits->items, u) {
3682 if (!strcmp(bits->items[u]->name, args[1]->val.str)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003683 set_fill_boolean(set, 1);
3684 break;
3685 }
3686 }
3687 }
3688 }
3689
3690 return LY_SUCCESS;
3691}
3692
3693/**
3694 * @brief Execute the XPath boolean(object) function. Returns LYXP_SET_BOOLEAN
3695 * with the argument converted to boolean.
3696 *
3697 * @param[in] args Array of arguments.
3698 * @param[in] arg_count Count of elements in @p args.
3699 * @param[in,out] set Context and result set at the same time.
3700 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003701 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003702 */
3703static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02003704xpath_boolean(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003705{
3706 LY_ERR rc;
3707
3708 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02003709 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003710 return LY_SUCCESS;
3711 }
3712
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003713 rc = lyxp_set_cast(args[0], LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003714 LY_CHECK_RET(rc);
3715 set_fill_set(set, args[0]);
3716
3717 return LY_SUCCESS;
3718}
3719
3720/**
3721 * @brief Execute the XPath ceiling(number) function. Returns LYXP_SET_NUMBER
3722 * with the first argument rounded up to the nearest integer.
3723 *
3724 * @param[in] args Array of arguments.
3725 * @param[in] arg_count Count of elements in @p args.
3726 * @param[in,out] set Context and result set at the same time.
3727 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003728 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003729 */
3730static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02003731xpath_ceiling(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003732{
3733 struct lysc_node_leaf *sleaf;
3734 LY_ERR rc = LY_SUCCESS;
3735
3736 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003737 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003738 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003739 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3740 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3741 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3742 sleaf->name);
3743 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
3744 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
3745 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003746 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003747 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003748 return rc;
3749 }
3750
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003751 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003752 LY_CHECK_RET(rc);
3753 if ((long long)args[0]->val.num != args[0]->val.num) {
3754 set_fill_number(set, ((long long)args[0]->val.num) + 1);
3755 } else {
3756 set_fill_number(set, args[0]->val.num);
3757 }
3758
3759 return LY_SUCCESS;
3760}
3761
3762/**
3763 * @brief Execute the XPath concat(string, string, string*) function.
3764 * Returns LYXP_SET_STRING with the concatenation of all the arguments.
3765 *
3766 * @param[in] args Array of arguments.
3767 * @param[in] arg_count Count of elements in @p args.
3768 * @param[in,out] set Context and result set at the same time.
3769 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003770 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003771 */
3772static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02003773xpath_concat(struct lyxp_set **args, uint32_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003774{
Michal Vaskodd528af2022-08-08 14:35:07 +02003775 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003776 char *str = NULL;
3777 size_t used = 1;
3778 LY_ERR rc = LY_SUCCESS;
3779 struct lysc_node_leaf *sleaf;
3780
3781 if (options & LYXP_SCNODE_ALL) {
3782 for (i = 0; i < arg_count; ++i) {
3783 if ((args[i]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[i]))) {
3784 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3785 LOGWRN(set->ctx, "Argument #%u of %s is a %s node \"%s\".",
Michal Vasko69730152020-10-09 16:30:07 +02003786 i + 1, __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003787 } else if (!warn_is_string_type(sleaf->type)) {
Radek Krejci70124c82020-08-14 22:17:03 +02003788 LOGWRN(set->ctx, "Argument #%u of %s is node \"%s\", not of string-type.", i + 1, __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003789 }
3790 }
3791 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003792 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003793 return rc;
3794 }
3795
3796 for (i = 0; i < arg_count; ++i) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003797 rc = lyxp_set_cast(args[i], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003798 if (rc != LY_SUCCESS) {
3799 free(str);
3800 return rc;
3801 }
3802
3803 str = ly_realloc(str, (used + strlen(args[i]->val.str)) * sizeof(char));
3804 LY_CHECK_ERR_RET(!str, LOGMEM(set->ctx), LY_EMEM);
3805 strcpy(str + used - 1, args[i]->val.str);
3806 used += strlen(args[i]->val.str);
3807 }
3808
3809 /* free, kind of */
Michal Vaskod3678892020-05-21 10:06:58 +02003810 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003811 set->type = LYXP_SET_STRING;
3812 set->val.str = str;
3813
3814 return LY_SUCCESS;
3815}
3816
3817/**
3818 * @brief Execute the XPath contains(string, string) function.
3819 * Returns LYXP_SET_BOOLEAN whether the second argument can
3820 * be found in the first or not.
3821 *
3822 * @param[in] args Array of arguments.
3823 * @param[in] arg_count Count of elements in @p args.
3824 * @param[in,out] set Context and result set at the same time.
3825 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003826 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003827 */
3828static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02003829xpath_contains(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003830{
3831 struct lysc_node_leaf *sleaf;
3832 LY_ERR rc = LY_SUCCESS;
3833
3834 if (options & LYXP_SCNODE_ALL) {
3835 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3836 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003837 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3838 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003839 } else if (!warn_is_string_type(sleaf->type)) {
3840 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003841 }
3842 }
3843
3844 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3845 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003846 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3847 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003848 } else if (!warn_is_string_type(sleaf->type)) {
3849 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003850 }
3851 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003852 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003853 return rc;
3854 }
3855
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003856 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003857 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003858 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003859 LY_CHECK_RET(rc);
3860
3861 if (strstr(args[0]->val.str, args[1]->val.str)) {
3862 set_fill_boolean(set, 1);
3863 } else {
3864 set_fill_boolean(set, 0);
3865 }
3866
3867 return LY_SUCCESS;
3868}
3869
3870/**
3871 * @brief Execute the XPath count(node-set) function. Returns LYXP_SET_NUMBER
3872 * with the size of the node-set from the argument.
3873 *
3874 * @param[in] args Array of arguments.
3875 * @param[in] arg_count Count of elements in @p args.
3876 * @param[in,out] set Context and result set at the same time.
3877 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003878 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003879 */
3880static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02003881xpath_count(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003882{
Michal Vasko03ff5a72019-09-11 13:49:33 +02003883 LY_ERR rc = LY_SUCCESS;
3884
3885 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003886 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003887 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003888 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003889 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003890 return rc;
3891 }
3892
Michal Vasko03ff5a72019-09-11 13:49:33 +02003893 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003894 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "count(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003895 return LY_EVALID;
3896 }
3897
3898 set_fill_number(set, args[0]->used);
3899 return LY_SUCCESS;
3900}
3901
3902/**
3903 * @brief Execute the XPath current() function. Returns LYXP_SET_NODE_SET
3904 * with the context with the intial node.
3905 *
3906 * @param[in] args Array of arguments.
3907 * @param[in] arg_count Count of elements in @p args.
3908 * @param[in,out] set Context and result set at the same time.
3909 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003910 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003911 */
3912static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02003913xpath_current(struct lyxp_set **args, uint32_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003914{
3915 if (arg_count || args) {
Radek Krejcie87c7dc2021-06-02 21:25:42 +02003916 LOGVAL(set->ctx, LY_VCODE_XP_INARGCOUNT, arg_count, LY_PRI_LENSTR("current()"));
Michal Vasko03ff5a72019-09-11 13:49:33 +02003917 return LY_EVALID;
3918 }
3919
3920 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02003921 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003922
Michal Vasko296dfaf2021-12-13 16:57:42 +01003923 if (set->cur_scnode) {
Michal Vaskoe4a6d012023-05-22 14:34:52 +02003924 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 +01003925 } else {
3926 /* root node */
Michal Vaskoe4a6d012023-05-22 14:34:52 +02003927 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, NULL, set->root_type, LYXP_AXIS_SELF, NULL));
Michal Vasko296dfaf2021-12-13 16:57:42 +01003928 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003929 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02003930 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003931
Michal Vasko296dfaf2021-12-13 16:57:42 +01003932 if (set->cur_node) {
3933 /* position is filled later */
3934 set_insert_node(set, set->cur_node, 0, LYXP_NODE_ELEM, 0);
3935 } else {
3936 /* root node */
3937 set_insert_node(set, NULL, 0, set->root_type, 0);
3938 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003939 }
3940
3941 return LY_SUCCESS;
3942}
3943
3944/**
3945 * @brief Execute the YANG 1.1 deref(node-set) function. Returns LYXP_SET_NODE_SET with either
3946 * leafref or instance-identifier target node(s).
3947 *
3948 * @param[in] args Array of arguments.
3949 * @param[in] arg_count Count of elements in @p args.
3950 * @param[in,out] set Context and result set at the same time.
3951 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003952 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003953 */
3954static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02003955xpath_deref(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003956{
3957 struct lyd_node_term *leaf;
Michal Vasko42e497c2020-01-06 08:38:25 +01003958 struct lysc_node_leaf *sleaf = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02003959 struct lysc_type_leafref *lref;
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003960 const struct lysc_node *target;
Michal Vasko004d3152020-06-11 19:59:22 +02003961 struct ly_path *p;
3962 struct lyd_node *node;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003963 char *errmsg = NULL;
Michal Vasko00cbf532020-06-15 13:58:47 +02003964 uint8_t oper;
Michal Vasko741bb562021-06-24 11:59:50 +02003965 LY_ERR r;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003966
3967 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003968 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003969 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003970 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
Michal Vasko423ba3f2021-07-19 13:08:50 +02003971 if (!(sleaf->nodetype & LYD_NODE_TERM)) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003972 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3973 sleaf->name);
Michal Vaskoed725d72021-06-23 12:03:45 +02003974 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_LEAFREF) &&
3975 !warn_is_specific_type(sleaf->type, LY_TYPE_INST)) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003976 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"leafref\" nor \"instance-identifier\".",
3977 __func__, sleaf->name);
3978 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003979 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003980 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko423ba3f2021-07-19 13:08:50 +02003981 if (sleaf && (sleaf->nodetype & LYD_NODE_TERM) && (sleaf->type->basetype == LY_TYPE_LEAFREF)) {
Michal Vasko004d3152020-06-11 19:59:22 +02003982 lref = (struct lysc_type_leafref *)sleaf->type;
Michal Vaskod1e53b92021-01-28 13:11:06 +01003983 oper = (sleaf->flags & LYS_IS_OUTPUT) ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT;
Michal Vasko004d3152020-06-11 19:59:22 +02003984
3985 /* it was already evaluated on schema, it must succeed */
Michal Vasko741bb562021-06-24 11:59:50 +02003986 r = ly_path_compile_leafref(set->ctx, &sleaf->node, NULL, lref->path, oper, LY_PATH_TARGET_MANY,
Michal Vasko24fc4d12021-07-12 14:41:20 +02003987 LY_VALUE_SCHEMA_RESOLVED, lref->prefixes, &p);
Michal Vasko741bb562021-06-24 11:59:50 +02003988 if (!r) {
3989 /* get the target node */
3990 target = p[LY_ARRAY_COUNT(p) - 1].node;
3991 ly_path_free(set->ctx, p);
Michal Vasko004d3152020-06-11 19:59:22 +02003992
Michal Vaskoe4a6d012023-05-22 14:34:52 +02003993 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, target, LYXP_NODE_ELEM, LYXP_AXIS_SELF, NULL));
Michal Vasko741bb562021-06-24 11:59:50 +02003994 } /* else the target was found before but is disabled so it was removed */
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003995 }
3996
Michal Vasko741bb562021-06-24 11:59:50 +02003997 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003998 }
3999
Michal Vaskod3678892020-05-21 10:06:58 +02004000 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004001 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "deref(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004002 return LY_EVALID;
4003 }
4004
Michal Vaskod3678892020-05-21 10:06:58 +02004005 lyxp_set_free_content(set);
4006 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004007 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
4008 sleaf = (struct lysc_node_leaf *)leaf->schema;
4009 if (sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
4010 if (sleaf->type->basetype == LY_TYPE_LEAFREF) {
4011 /* find leafref target */
Radek Krejci0b013302021-03-29 15:22:32 +02004012 if (lyplg_type_resolve_leafref((struct lysc_type_leafref *)sleaf->type, &leaf->node, &leaf->value, set->tree,
Michal Vasko9e685082021-01-29 14:49:09 +01004013 &node, &errmsg)) {
Michal Vasko1a2b8ee2022-12-05 10:41:55 +01004014 LOGERR(set->ctx, LY_EVALID, "%s", errmsg);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004015 free(errmsg);
Michal Vasko004d3152020-06-11 19:59:22 +02004016 return LY_EVALID;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004017 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004018 } else {
4019 assert(sleaf->type->basetype == LY_TYPE_INST);
Michal Vasko90189962023-02-28 12:10:34 +01004020 if (ly_path_eval(leaf->value.target, set->tree, NULL, &node)) {
Michal Vaskoba99a3e2020-08-18 15:50:05 +02004021 LOGERR(set->ctx, LY_EVALID, "Invalid instance-identifier \"%s\" value - required instance not found.",
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02004022 lyd_get_value(&leaf->node));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004023 return LY_EVALID;
4024 }
4025 }
Michal Vasko004d3152020-06-11 19:59:22 +02004026
4027 /* insert it */
4028 set_insert_node(set, node, 0, LYXP_NODE_ELEM, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004029 }
4030 }
4031
4032 return LY_SUCCESS;
4033}
4034
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004035static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02004036xpath_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 +02004037{
Michal Vaskofe1af042022-07-29 14:58:59 +02004038 uint32_t i, id_len;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004039 LY_ARRAY_COUNT_TYPE u;
4040 struct lyd_node_term *leaf;
4041 struct lysc_node_leaf *sleaf;
4042 struct lyd_meta *meta;
Michal Vasko93923692021-05-07 15:28:02 +02004043 struct lyd_value *val;
4044 const struct lys_module *mod;
4045 const char *id_name;
Michal Vasko93923692021-05-07 15:28:02 +02004046 struct lysc_ident *id;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004047 LY_ERR rc = LY_SUCCESS;
Radek Krejci857189e2020-09-01 13:26:36 +02004048 ly_bool found;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004049
4050 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02004051 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004052 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", func);
Michal Vasko5676f4e2021-04-06 17:14:45 +02004053 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4054 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4055 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype),
4056 sleaf->name);
4057 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_IDENT)) {
4058 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"identityref\".", func, sleaf->name);
4059 }
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004060 }
4061
4062 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4063 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4064 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype),
Michal Vasko69730152020-10-09 16:30:07 +02004065 sleaf->name);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004066 } else if (!warn_is_string_type(sleaf->type)) {
4067 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", func, sleaf->name);
4068 }
4069 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004070 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004071 return rc;
4072 }
4073
4074 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004075 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 +02004076 return LY_EVALID;
4077 }
4078 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
4079 LY_CHECK_RET(rc);
4080
Michal Vasko93923692021-05-07 15:28:02 +02004081 /* parse the identity */
4082 id_name = args[1]->val.str;
4083 id_len = strlen(id_name);
4084 rc = moveto_resolve_model(&id_name, &id_len, set, set->cur_node ? set->cur_node->schema : NULL, &mod);
4085 LY_CHECK_RET(rc);
4086 if (!mod) {
4087 LOGVAL(set->ctx, LYVE_XPATH, "Identity \"%.*s\" without a prefix.", (int)id_len, id_name);
4088 return LY_EVALID;
4089 }
4090
4091 /* find the identity */
4092 found = 0;
4093 LY_ARRAY_FOR(mod->identities, u) {
4094 if (!ly_strncmp(mod->identities[u].name, id_name, id_len)) {
4095 /* we have match */
4096 found = 1;
4097 break;
4098 }
4099 }
4100 if (!found) {
4101 LOGVAL(set->ctx, LYVE_XPATH, "Identity \"%.*s\" not found in module \"%s\".", (int)id_len, id_name, mod->name);
4102 return LY_EVALID;
4103 }
4104 id = &mod->identities[u];
4105
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004106 set_fill_boolean(set, 0);
4107 found = 0;
4108 for (i = 0; i < args[0]->used; ++i) {
4109 if ((args[0]->val.nodes[i].type != LYXP_NODE_ELEM) && (args[0]->val.nodes[i].type != LYXP_NODE_META)) {
4110 continue;
4111 }
4112
4113 if (args[0]->val.nodes[i].type == LYXP_NODE_ELEM) {
4114 leaf = (struct lyd_node_term *)args[0]->val.nodes[i].node;
4115 sleaf = (struct lysc_node_leaf *)leaf->schema;
4116 val = &leaf->value;
4117 if (!(sleaf->nodetype & LYD_NODE_TERM) || (leaf->value.realtype->basetype != LY_TYPE_IDENT)) {
4118 /* uninteresting */
4119 continue;
4120 }
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004121 } else {
4122 meta = args[0]->val.meta[i].meta;
4123 val = &meta->value;
4124 if (val->realtype->basetype != LY_TYPE_IDENT) {
4125 /* uninteresting */
4126 continue;
4127 }
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004128 }
4129
Michal Vasko93923692021-05-07 15:28:02 +02004130 /* check the identity itself */
4131 if (self_match && (id == val->ident)) {
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004132 set_fill_boolean(set, 1);
4133 found = 1;
4134 }
Michal Vasko93923692021-05-07 15:28:02 +02004135 if (!found && !lyplg_type_identity_isderived(id, val->ident)) {
4136 set_fill_boolean(set, 1);
4137 found = 1;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004138 }
4139
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004140 if (found) {
4141 break;
4142 }
4143 }
4144
4145 return LY_SUCCESS;
4146}
4147
Michal Vasko03ff5a72019-09-11 13:49:33 +02004148/**
4149 * @brief Execute the YANG 1.1 derived-from(node-set, string) function. Returns LYXP_SET_BOOLEAN depending
4150 * on whether the first argument nodes contain a node of an identity derived from the second
4151 * argument identity.
4152 *
4153 * @param[in] args Array of arguments.
4154 * @param[in] arg_count Count of elements in @p args.
4155 * @param[in,out] set Context and result set at the same time.
4156 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004157 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004158 */
4159static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004160xpath_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 +02004161{
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004162 return xpath_derived_(args, set, options, 0, __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004163}
4164
4165/**
4166 * @brief Execute the YANG 1.1 derived-from-or-self(node-set, string) function. Returns LYXP_SET_BOOLEAN depending
4167 * on whether the first argument nodes contain a node of an identity that either is or is derived from
4168 * the second argument identity.
4169 *
4170 * @param[in] args Array of arguments.
4171 * @param[in] arg_count Count of elements in @p args.
4172 * @param[in,out] set Context and result set at the same time.
4173 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004174 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004175 */
4176static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004177xpath_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 +02004178{
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004179 return xpath_derived_(args, set, options, 1, __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004180}
4181
4182/**
4183 * @brief Execute the YANG 1.1 enum-value(node-set) function. Returns LYXP_SET_NUMBER
4184 * with the integer value of the first node's enum value, otherwise NaN.
4185 *
4186 * @param[in] args Array of arguments.
4187 * @param[in] arg_count Count of elements in @p args.
4188 * @param[in,out] set Context and result set at the same time.
4189 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004190 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004191 */
4192static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004193xpath_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 +02004194{
4195 struct lyd_node_term *leaf;
4196 struct lysc_node_leaf *sleaf;
4197 LY_ERR rc = LY_SUCCESS;
4198
4199 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02004200 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004201 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02004202 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4203 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4204 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
4205 sleaf->name);
4206 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_ENUM)) {
4207 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"enumeration\".", __func__, sleaf->name);
4208 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004209 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004210 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004211 return rc;
4212 }
4213
Michal Vaskod3678892020-05-21 10:06:58 +02004214 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004215 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "enum-value(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004216 return LY_EVALID;
4217 }
4218
4219 set_fill_number(set, NAN);
Michal Vaskod3678892020-05-21 10:06:58 +02004220 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004221 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
4222 sleaf = (struct lysc_node_leaf *)leaf->schema;
4223 if ((sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (sleaf->type->basetype == LY_TYPE_ENUM)) {
4224 set_fill_number(set, leaf->value.enum_item->value);
4225 }
4226 }
4227
4228 return LY_SUCCESS;
4229}
4230
4231/**
4232 * @brief Execute the XPath false() function. Returns LYXP_SET_BOOLEAN
4233 * with false value.
4234 *
4235 * @param[in] args Array of arguments.
4236 * @param[in] arg_count Count of elements in @p args.
4237 * @param[in,out] set Context and result set at the same time.
4238 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004239 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004240 */
4241static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004242xpath_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 +02004243{
4244 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004245 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004246 return LY_SUCCESS;
4247 }
4248
4249 set_fill_boolean(set, 0);
4250 return LY_SUCCESS;
4251}
4252
4253/**
4254 * @brief Execute the XPath floor(number) function. Returns LYXP_SET_NUMBER
4255 * with the first argument floored (truncated).
4256 *
4257 * @param[in] args Array of arguments.
4258 * @param[in] arg_count Count of elements in @p args.
4259 * @param[in,out] set Context and result set at the same time.
4260 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004261 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004262 */
4263static LY_ERR
Michal Vasko0ff8d632023-04-06 12:31:17 +02004264xpath_floor(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004265{
Michal Vasko0ff8d632023-04-06 12:31:17 +02004266 struct lysc_node_leaf *sleaf;
4267 LY_ERR rc = LY_SUCCESS;
4268
4269 if (options & LYXP_SCNODE_ALL) {
4270 if (args[0]->type != LYXP_SET_SCNODE_SET) {
4271 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
4272 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4273 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4274 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
4275 sleaf->name);
4276 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
4277 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
4278 }
4279 }
4280 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
4281 return rc;
4282 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004283
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004284 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004285 LY_CHECK_RET(rc);
4286 if (isfinite(args[0]->val.num)) {
4287 set_fill_number(set, (long long)args[0]->val.num);
4288 }
4289
4290 return LY_SUCCESS;
4291}
4292
4293/**
4294 * @brief Execute the XPath lang(string) function. Returns LYXP_SET_BOOLEAN
4295 * whether the language of the text matches the one from the argument.
4296 *
4297 * @param[in] args Array of arguments.
4298 * @param[in] arg_count Count of elements in @p args.
4299 * @param[in,out] set Context and result set at the same time.
4300 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004301 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004302 */
4303static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004304xpath_lang(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004305{
4306 const struct lyd_node *node;
4307 struct lysc_node_leaf *sleaf;
Michal Vasko9f96a052020-03-10 09:41:45 +01004308 struct lyd_meta *meta = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004309 const char *val;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004310 LY_ERR rc = LY_SUCCESS;
4311
4312 if (options & LYXP_SCNODE_ALL) {
4313 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4314 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004315 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
4316 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004317 } else if (!warn_is_string_type(sleaf->type)) {
4318 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004319 }
4320 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004321 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004322 return rc;
4323 }
4324
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004325 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004326 LY_CHECK_RET(rc);
4327
Michal Vasko03ff5a72019-09-11 13:49:33 +02004328 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004329 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "lang(string)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004330 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004331 } else if (!set->used) {
4332 set_fill_boolean(set, 0);
4333 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004334 }
4335
4336 switch (set->val.nodes[0].type) {
4337 case LYXP_NODE_ELEM:
4338 case LYXP_NODE_TEXT:
4339 node = set->val.nodes[0].node;
4340 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004341 case LYXP_NODE_META:
4342 node = set->val.meta[0].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004343 break;
4344 default:
4345 /* nothing to do with roots */
4346 set_fill_boolean(set, 0);
4347 return LY_SUCCESS;
4348 }
4349
Michal Vasko9f96a052020-03-10 09:41:45 +01004350 /* find lang metadata */
Michal Vasko9e685082021-01-29 14:49:09 +01004351 for ( ; node; node = lyd_parent(node)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01004352 for (meta = node->meta; meta; meta = meta->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004353 /* annotations */
Michal Vasko9f96a052020-03-10 09:41:45 +01004354 if (meta->name && !strcmp(meta->name, "lang") && !strcmp(meta->annotation->module->name, "xml")) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004355 break;
4356 }
4357 }
4358
Michal Vasko9f96a052020-03-10 09:41:45 +01004359 if (meta) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004360 break;
4361 }
4362 }
4363
4364 /* compare languages */
Michal Vasko9f96a052020-03-10 09:41:45 +01004365 if (!meta) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004366 set_fill_boolean(set, 0);
4367 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004368 uint64_t i;
4369
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02004370 val = lyd_get_meta_value(meta);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004371 for (i = 0; args[0]->val.str[i]; ++i) {
4372 if (tolower(args[0]->val.str[i]) != tolower(val[i])) {
4373 set_fill_boolean(set, 0);
4374 break;
4375 }
4376 }
4377 if (!args[0]->val.str[i]) {
4378 if (!val[i] || (val[i] == '-')) {
4379 set_fill_boolean(set, 1);
4380 } else {
4381 set_fill_boolean(set, 0);
4382 }
4383 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004384 }
4385
4386 return LY_SUCCESS;
4387}
4388
4389/**
4390 * @brief Execute the XPath last() function. Returns LYXP_SET_NUMBER
4391 * with the context size.
4392 *
4393 * @param[in] args Array of arguments.
4394 * @param[in] arg_count Count of elements in @p args.
4395 * @param[in,out] set Context and result set at the same time.
4396 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004397 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004398 */
4399static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004400xpath_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 +02004401{
4402 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004403 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004404 return LY_SUCCESS;
4405 }
4406
Michal Vasko03ff5a72019-09-11 13:49:33 +02004407 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004408 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "last()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004409 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004410 } else if (!set->used) {
4411 set_fill_number(set, 0);
4412 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004413 }
4414
4415 set_fill_number(set, set->ctx_size);
4416 return LY_SUCCESS;
4417}
4418
4419/**
4420 * @brief Execute the XPath local-name(node-set?) function. Returns LYXP_SET_STRING
4421 * with the node name without namespace from the argument or the context.
4422 *
4423 * @param[in] args Array of arguments.
4424 * @param[in] arg_count Count of elements in @p args.
4425 * @param[in,out] set Context and result set at the same time.
4426 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004427 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004428 */
4429static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004430xpath_local_name(struct lyxp_set **args, uint32_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004431{
4432 struct lyxp_set_node *item;
Michal Vasko69730152020-10-09 16:30:07 +02004433
Michal Vasko03ff5a72019-09-11 13:49:33 +02004434 /* suppress unused variable warning */
4435 (void)options;
4436
4437 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004438 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004439 return LY_SUCCESS;
4440 }
4441
4442 if (arg_count) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004443 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004444 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004445 "local-name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004446 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004447 } else if (!args[0]->used) {
4448 set_fill_string(set, "", 0);
4449 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004450 }
4451
4452 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004453 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004454
4455 item = &args[0]->val.nodes[0];
4456 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004457 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004458 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "local-name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004459 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004460 } else if (!set->used) {
4461 set_fill_string(set, "", 0);
4462 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004463 }
4464
4465 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004466 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004467
4468 item = &set->val.nodes[0];
4469 }
4470
4471 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004472 case LYXP_NODE_NONE:
4473 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004474 case LYXP_NODE_ROOT:
4475 case LYXP_NODE_ROOT_CONFIG:
4476 case LYXP_NODE_TEXT:
4477 set_fill_string(set, "", 0);
4478 break;
4479 case LYXP_NODE_ELEM:
4480 set_fill_string(set, item->node->schema->name, strlen(item->node->schema->name));
4481 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004482 case LYXP_NODE_META:
4483 set_fill_string(set, ((struct lyd_meta *)item->node)->name, strlen(((struct lyd_meta *)item->node)->name));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004484 break;
4485 }
4486
4487 return LY_SUCCESS;
4488}
4489
4490/**
4491 * @brief Execute the XPath name(node-set?) function. Returns LYXP_SET_STRING
4492 * with the node name fully qualified (with namespace) from the argument or the context.
4493 *
4494 * @param[in] args Array of arguments.
4495 * @param[in] arg_count Count of elements in @p args.
4496 * @param[in,out] set Context and result set at the same time.
4497 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004498 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004499 */
4500static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004501xpath_name(struct lyxp_set **args, uint32_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004502{
4503 struct lyxp_set_node *item;
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004504 struct lys_module *mod = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004505 char *str;
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004506 const char *name = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004507
4508 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004509 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004510 return LY_SUCCESS;
4511 }
4512
4513 if (arg_count) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004514 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004515 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004516 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004517 } else if (!args[0]->used) {
4518 set_fill_string(set, "", 0);
4519 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004520 }
4521
4522 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004523 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004524
4525 item = &args[0]->val.nodes[0];
4526 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004527 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004528 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004529 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004530 } else if (!set->used) {
4531 set_fill_string(set, "", 0);
4532 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004533 }
4534
4535 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004536 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004537
4538 item = &set->val.nodes[0];
4539 }
4540
4541 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004542 case LYXP_NODE_NONE:
4543 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004544 case LYXP_NODE_ROOT:
4545 case LYXP_NODE_ROOT_CONFIG:
4546 case LYXP_NODE_TEXT:
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004547 /* keep NULL */
Michal Vasko03ff5a72019-09-11 13:49:33 +02004548 break;
4549 case LYXP_NODE_ELEM:
4550 mod = item->node->schema->module;
4551 name = item->node->schema->name;
4552 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004553 case LYXP_NODE_META:
4554 mod = ((struct lyd_meta *)item->node)->annotation->module;
4555 name = ((struct lyd_meta *)item->node)->name;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004556 break;
4557 }
4558
4559 if (mod && name) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004560 int rc = asprintf(&str, "%s:%s", ly_get_prefix(mod, set->format, set->prefix_data), name);
Michal Vasko26bbb272022-08-02 14:54:33 +02004561
Michal Vasko03ff5a72019-09-11 13:49:33 +02004562 LY_CHECK_ERR_RET(rc == -1, LOGMEM(set->ctx), LY_EMEM);
4563 set_fill_string(set, str, strlen(str));
4564 free(str);
4565 } else {
4566 set_fill_string(set, "", 0);
4567 }
4568
4569 return LY_SUCCESS;
4570}
4571
4572/**
4573 * @brief Execute the XPath namespace-uri(node-set?) function. Returns LYXP_SET_STRING
4574 * with the namespace of the node from the argument or the context.
4575 *
4576 * @param[in] args Array of arguments.
4577 * @param[in] arg_count Count of elements in @p args.
4578 * @param[in,out] set Context and result set at the same time.
4579 * @param[in] options XPath options.
4580 * @return LY_ERR (LY_EINVAL for wrong arguments on schema)
4581 */
4582static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004583xpath_namespace_uri(struct lyxp_set **args, uint32_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004584{
4585 struct lyxp_set_node *item;
4586 struct lys_module *mod;
Michal Vasko69730152020-10-09 16:30:07 +02004587
Michal Vasko03ff5a72019-09-11 13:49:33 +02004588 /* suppress unused variable warning */
4589 (void)options;
4590
4591 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004592 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004593 return LY_SUCCESS;
4594 }
4595
4596 if (arg_count) {
Michal Vaskod3678892020-05-21 10:06:58 +02004597 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004598 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
Michal Vasko69730152020-10-09 16:30:07 +02004599 "namespace-uri(node-set?)");
Michal Vaskod3678892020-05-21 10:06:58 +02004600 return LY_EVALID;
4601 } else if (!args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004602 set_fill_string(set, "", 0);
4603 return LY_SUCCESS;
4604 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004605
4606 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004607 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004608
4609 item = &args[0]->val.nodes[0];
4610 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004611 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004612 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "namespace-uri(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004613 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004614 } else if (!set->used) {
4615 set_fill_string(set, "", 0);
4616 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004617 }
4618
4619 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004620 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004621
4622 item = &set->val.nodes[0];
4623 }
4624
4625 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004626 case LYXP_NODE_NONE:
4627 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004628 case LYXP_NODE_ROOT:
4629 case LYXP_NODE_ROOT_CONFIG:
4630 case LYXP_NODE_TEXT:
4631 set_fill_string(set, "", 0);
4632 break;
4633 case LYXP_NODE_ELEM:
Michal Vasko9f96a052020-03-10 09:41:45 +01004634 case LYXP_NODE_META:
Michal Vasko03ff5a72019-09-11 13:49:33 +02004635 if (item->type == LYXP_NODE_ELEM) {
4636 mod = item->node->schema->module;
Michal Vasko9f96a052020-03-10 09:41:45 +01004637 } else { /* LYXP_NODE_META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02004638 /* annotations */
Michal Vasko9f96a052020-03-10 09:41:45 +01004639 mod = ((struct lyd_meta *)item->node)->annotation->module;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004640 }
4641
4642 set_fill_string(set, mod->ns, strlen(mod->ns));
4643 break;
4644 }
4645
4646 return LY_SUCCESS;
4647}
4648
4649/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02004650 * @brief Execute the XPath normalize-space(string?) function. Returns LYXP_SET_STRING
4651 * with normalized value (no leading, trailing, double white spaces) of the node
4652 * from the argument or the context.
4653 *
4654 * @param[in] args Array of arguments.
4655 * @param[in] arg_count Count of elements in @p args.
4656 * @param[in,out] set Context and result set at the same time.
4657 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004658 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004659 */
4660static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004661xpath_normalize_space(struct lyxp_set **args, uint32_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004662{
Michal Vaskodd528af2022-08-08 14:35:07 +02004663 uint32_t i, new_used;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004664 char *new;
Radek Krejci857189e2020-09-01 13:26:36 +02004665 ly_bool have_spaces = 0, space_before = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004666 struct lysc_node_leaf *sleaf;
4667 LY_ERR rc = LY_SUCCESS;
4668
4669 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004670 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) &&
4671 (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004672 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004673 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
4674 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004675 } else if (!warn_is_string_type(sleaf->type)) {
4676 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004677 }
4678 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004679 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004680 return rc;
4681 }
4682
4683 if (arg_count) {
4684 set_fill_set(set, args[0]);
4685 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004686 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004687 LY_CHECK_RET(rc);
4688
4689 /* is there any normalization necessary? */
4690 for (i = 0; set->val.str[i]; ++i) {
4691 if (is_xmlws(set->val.str[i])) {
4692 if ((i == 0) || space_before || (!set->val.str[i + 1])) {
4693 have_spaces = 1;
4694 break;
4695 }
4696 space_before = 1;
4697 } else {
4698 space_before = 0;
4699 }
4700 }
4701
4702 /* yep, there is */
4703 if (have_spaces) {
4704 /* it's enough, at least one character will go, makes space for ending '\0' */
4705 new = malloc(strlen(set->val.str) * sizeof(char));
4706 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4707 new_used = 0;
4708
4709 space_before = 0;
4710 for (i = 0; set->val.str[i]; ++i) {
4711 if (is_xmlws(set->val.str[i])) {
4712 if ((i == 0) || space_before) {
4713 space_before = 1;
4714 continue;
4715 } else {
4716 space_before = 1;
4717 }
4718 } else {
4719 space_before = 0;
4720 }
4721
4722 new[new_used] = (space_before ? ' ' : set->val.str[i]);
4723 ++new_used;
4724 }
4725
4726 /* at worst there is one trailing space now */
4727 if (new_used && is_xmlws(new[new_used - 1])) {
4728 --new_used;
4729 }
4730
4731 new = ly_realloc(new, (new_used + 1) * sizeof(char));
4732 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4733 new[new_used] = '\0';
4734
4735 free(set->val.str);
4736 set->val.str = new;
4737 }
4738
4739 return LY_SUCCESS;
4740}
4741
4742/**
4743 * @brief Execute the XPath not(boolean) function. Returns LYXP_SET_BOOLEAN
4744 * with the argument converted to boolean and logically inverted.
4745 *
4746 * @param[in] args Array of arguments.
4747 * @param[in] arg_count Count of elements in @p args.
4748 * @param[in,out] set Context and result set at the same time.
4749 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004750 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004751 */
4752static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004753xpath_not(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004754{
4755 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004756 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004757 return LY_SUCCESS;
4758 }
4759
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004760 lyxp_set_cast(args[0], LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +02004761 if (args[0]->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004762 set_fill_boolean(set, 0);
4763 } else {
4764 set_fill_boolean(set, 1);
4765 }
4766
4767 return LY_SUCCESS;
4768}
4769
4770/**
4771 * @brief Execute the XPath number(object?) function. Returns LYXP_SET_NUMBER
4772 * with the number representation of either the argument or the context.
4773 *
4774 * @param[in] args Array of arguments.
4775 * @param[in] arg_count Count of elements in @p args.
4776 * @param[in,out] set Context and result set at the same time.
4777 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004778 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004779 */
4780static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004781xpath_number(struct lyxp_set **args, uint32_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004782{
4783 LY_ERR rc;
4784
4785 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004786 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004787 return LY_SUCCESS;
4788 }
4789
4790 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004791 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004792 LY_CHECK_RET(rc);
4793 set_fill_set(set, args[0]);
4794 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004795 rc = lyxp_set_cast(set, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004796 LY_CHECK_RET(rc);
4797 }
4798
4799 return LY_SUCCESS;
4800}
4801
4802/**
4803 * @brief Execute the XPath position() function. Returns LYXP_SET_NUMBER
4804 * with the context position.
4805 *
4806 * @param[in] args Array of arguments.
4807 * @param[in] arg_count Count of elements in @p args.
4808 * @param[in,out] set Context and result set at the same time.
4809 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004810 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004811 */
4812static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004813xpath_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 +02004814{
4815 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004816 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004817 return LY_SUCCESS;
4818 }
4819
Michal Vasko03ff5a72019-09-11 13:49:33 +02004820 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004821 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "position()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004822 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004823 } else if (!set->used) {
4824 set_fill_number(set, 0);
4825 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004826 }
4827
4828 set_fill_number(set, set->ctx_pos);
4829
4830 /* UNUSED in 'Release' build type */
4831 (void)options;
4832 return LY_SUCCESS;
4833}
4834
4835/**
4836 * @brief Execute the YANG 1.1 re-match(string, string) function. Returns LYXP_SET_BOOLEAN
4837 * depending on whether the second argument regex matches the first argument string. For details refer to
4838 * YANG 1.1 RFC section 10.2.1.
4839 *
4840 * @param[in] args Array of arguments.
4841 * @param[in] arg_count Count of elements in @p args.
4842 * @param[in,out] set Context and result set at the same time.
4843 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004844 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004845 */
4846static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004847xpath_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 +02004848{
4849 struct lysc_pattern **patterns = NULL, **pattern;
4850 struct lysc_node_leaf *sleaf;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004851 LY_ERR rc = LY_SUCCESS;
4852 struct ly_err_item *err;
4853
4854 if (options & LYXP_SCNODE_ALL) {
4855 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4856 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4857 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 +02004858 } else if (!warn_is_string_type(sleaf->type)) {
4859 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004860 }
4861 }
4862
4863 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4864 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4865 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 +02004866 } else if (!warn_is_string_type(sleaf->type)) {
4867 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004868 }
4869 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004870 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004871 return rc;
4872 }
4873
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004874 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004875 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004876 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004877 LY_CHECK_RET(rc);
4878
4879 LY_ARRAY_NEW_RET(set->ctx, patterns, pattern, LY_EMEM);
Radek Iša45802b52021-02-09 09:21:58 +01004880 *pattern = calloc(1, sizeof **pattern);
Radek Krejciddace2c2021-01-08 11:30:56 +01004881 LOG_LOCSET(NULL, set->cur_node, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01004882 rc = lys_compile_type_pattern_check(set->ctx, args[1]->val.str, &(*pattern)->code);
Michal Vasko4a7d4d62021-12-13 17:05:06 +01004883 if (set->cur_node) {
4884 LOG_LOCBACK(0, 1, 0, 0);
4885 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004886 if (rc != LY_SUCCESS) {
4887 LY_ARRAY_FREE(patterns);
4888 return rc;
4889 }
4890
Radek Krejci0b013302021-03-29 15:22:32 +02004891 rc = lyplg_type_validate_patterns(patterns, args[0]->val.str, strlen(args[0]->val.str), &err);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004892 pcre2_code_free((*pattern)->code);
4893 free(*pattern);
4894 LY_ARRAY_FREE(patterns);
4895 if (rc && (rc != LY_EVALID)) {
Michal Vasko177d0ed2020-11-23 16:43:03 +01004896 ly_err_print(set->ctx, err);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004897 ly_err_free(err);
4898 return rc;
4899 }
4900
4901 if (rc == LY_EVALID) {
4902 ly_err_free(err);
4903 set_fill_boolean(set, 0);
4904 } else {
4905 set_fill_boolean(set, 1);
4906 }
4907
4908 return LY_SUCCESS;
4909}
4910
4911/**
4912 * @brief Execute the XPath round(number) function. Returns LYXP_SET_NUMBER
4913 * with the rounded first argument. For details refer to
4914 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-round.
4915 *
4916 * @param[in] args Array of arguments.
4917 * @param[in] arg_count Count of elements in @p args.
4918 * @param[in,out] set Context and result set at the same time.
4919 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004920 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004921 */
4922static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004923xpath_round(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004924{
4925 struct lysc_node_leaf *sleaf;
4926 LY_ERR rc = LY_SUCCESS;
4927
4928 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02004929 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004930 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02004931 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4932 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4933 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
4934 sleaf->name);
4935 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
4936 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
4937 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004938 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004939 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004940 return rc;
4941 }
4942
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004943 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004944 LY_CHECK_RET(rc);
4945
4946 /* cover only the cases where floor can't be used */
4947 if ((args[0]->val.num == -0.0f) || ((args[0]->val.num < 0) && (args[0]->val.num >= -0.5))) {
4948 set_fill_number(set, -0.0f);
4949 } else {
4950 args[0]->val.num += 0.5;
4951 rc = xpath_floor(args, 1, args[0], options);
4952 LY_CHECK_RET(rc);
4953 set_fill_number(set, args[0]->val.num);
4954 }
4955
4956 return LY_SUCCESS;
4957}
4958
4959/**
4960 * @brief Execute the XPath starts-with(string, string) function.
4961 * Returns LYXP_SET_BOOLEAN whether the second argument is
4962 * the prefix of the first or not.
4963 *
4964 * @param[in] args Array of arguments.
4965 * @param[in] arg_count Count of elements in @p args.
4966 * @param[in,out] set Context and result set at the same time.
4967 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004968 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004969 */
4970static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004971xpath_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 +02004972{
4973 struct lysc_node_leaf *sleaf;
4974 LY_ERR rc = LY_SUCCESS;
4975
4976 if (options & LYXP_SCNODE_ALL) {
4977 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4978 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4979 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 +02004980 } else if (!warn_is_string_type(sleaf->type)) {
4981 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004982 }
4983 }
4984
4985 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4986 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4987 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 +02004988 } else if (!warn_is_string_type(sleaf->type)) {
4989 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004990 }
4991 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004992 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004993 return rc;
4994 }
4995
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004996 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004997 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004998 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004999 LY_CHECK_RET(rc);
5000
5001 if (strncmp(args[0]->val.str, args[1]->val.str, strlen(args[1]->val.str))) {
5002 set_fill_boolean(set, 0);
5003 } else {
5004 set_fill_boolean(set, 1);
5005 }
5006
5007 return LY_SUCCESS;
5008}
5009
5010/**
5011 * @brief Execute the XPath string(object?) function. Returns LYXP_SET_STRING
5012 * with the string representation of either the argument or the context.
5013 *
5014 * @param[in] args Array of arguments.
5015 * @param[in] arg_count Count of elements in @p args.
5016 * @param[in,out] set Context and result set at the same time.
5017 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005018 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005019 */
5020static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02005021xpath_string(struct lyxp_set **args, uint32_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005022{
5023 LY_ERR rc;
5024
5025 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02005026 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005027 return LY_SUCCESS;
5028 }
5029
5030 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005031 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005032 LY_CHECK_RET(rc);
5033 set_fill_set(set, args[0]);
5034 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005035 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005036 LY_CHECK_RET(rc);
5037 }
5038
5039 return LY_SUCCESS;
5040}
5041
5042/**
5043 * @brief Execute the XPath string-length(string?) function. Returns LYXP_SET_NUMBER
5044 * with the length of the string in either the argument or the context.
5045 *
5046 * @param[in] args Array of arguments.
5047 * @param[in] arg_count Count of elements in @p args.
5048 * @param[in,out] set Context and result set at the same time.
5049 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005050 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005051 */
5052static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02005053xpath_string_length(struct lyxp_set **args, uint32_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005054{
5055 struct lysc_node_leaf *sleaf;
5056 LY_ERR rc = LY_SUCCESS;
5057
5058 if (options & LYXP_SCNODE_ALL) {
5059 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5060 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5061 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 +02005062 } else if (!warn_is_string_type(sleaf->type)) {
5063 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005064 }
5065 }
5066 if (!arg_count && (set->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set))) {
5067 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5068 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 +02005069 } else if (!warn_is_string_type(sleaf->type)) {
5070 LOGWRN(set->ctx, "Argument #0 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005071 }
5072 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005073 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005074 return rc;
5075 }
5076
5077 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005078 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005079 LY_CHECK_RET(rc);
5080 set_fill_number(set, strlen(args[0]->val.str));
5081 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005082 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005083 LY_CHECK_RET(rc);
5084 set_fill_number(set, strlen(set->val.str));
5085 }
5086
5087 return LY_SUCCESS;
5088}
5089
5090/**
5091 * @brief Execute the XPath substring(string, number, number?) function.
5092 * Returns LYXP_SET_STRING substring of the first argument starting
5093 * on the second argument index ending on the third argument index,
5094 * indexed from 1. For exact definition refer to
5095 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-substring.
5096 *
5097 * @param[in] args Array of arguments.
5098 * @param[in] arg_count Count of elements in @p args.
5099 * @param[in,out] set Context and result set at the same time.
5100 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005101 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005102 */
5103static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02005104xpath_substring(struct lyxp_set **args, uint32_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005105{
Michal Vasko6db996e2022-07-28 10:28:04 +02005106 int64_t start;
5107 int32_t len;
Michal Vaskodd528af2022-08-08 14:35:07 +02005108 uint32_t str_start, str_len, pos;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005109 struct lysc_node_leaf *sleaf;
5110 LY_ERR rc = LY_SUCCESS;
5111
5112 if (options & LYXP_SCNODE_ALL) {
5113 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5114 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5115 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005116 } else if (!warn_is_string_type(sleaf->type)) {
5117 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005118 }
5119 }
5120
5121 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5122 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5123 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 +02005124 } else if (!warn_is_numeric_type(sleaf->type)) {
5125 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005126 }
5127 }
5128
Michal Vasko69730152020-10-09 16:30:07 +02005129 if ((arg_count == 3) && (args[2]->type == LYXP_SET_SCNODE_SET) &&
5130 (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005131 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5132 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 +02005133 } else if (!warn_is_numeric_type(sleaf->type)) {
5134 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005135 }
5136 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005137 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005138 return rc;
5139 }
5140
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005141 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005142 LY_CHECK_RET(rc);
5143
5144 /* start */
5145 if (xpath_round(&args[1], 1, args[1], options)) {
5146 return -1;
5147 }
5148 if (isfinite(args[1]->val.num)) {
5149 start = args[1]->val.num - 1;
5150 } else if (isinf(args[1]->val.num) && signbit(args[1]->val.num)) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02005151 start = INT32_MIN;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005152 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02005153 start = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005154 }
5155
5156 /* len */
5157 if (arg_count == 3) {
5158 rc = xpath_round(&args[2], 1, args[2], options);
5159 LY_CHECK_RET(rc);
Radek Krejci1deb5be2020-08-26 16:43:36 +02005160 if (isnan(args[2]->val.num) || signbit(args[2]->val.num)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005161 len = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005162 } else if (isfinite(args[2]->val.num)) {
5163 len = args[2]->val.num;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005164 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02005165 len = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005166 }
5167 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02005168 len = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005169 }
5170
5171 /* find matching character positions */
5172 str_start = 0;
5173 str_len = 0;
5174 for (pos = 0; args[0]->val.str[pos]; ++pos) {
5175 if (pos < start) {
5176 ++str_start;
5177 } else if (pos < start + len) {
5178 ++str_len;
5179 } else {
5180 break;
5181 }
5182 }
5183
5184 set_fill_string(set, args[0]->val.str + str_start, str_len);
5185 return LY_SUCCESS;
5186}
5187
5188/**
5189 * @brief Execute the XPath substring-after(string, string) function.
5190 * Returns LYXP_SET_STRING with the string succeeding the occurance
5191 * of the second argument in the first or an empty string.
5192 *
5193 * @param[in] args Array of arguments.
5194 * @param[in] arg_count Count of elements in @p args.
5195 * @param[in,out] set Context and result set at the same time.
5196 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005197 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005198 */
5199static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02005200xpath_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 +02005201{
5202 char *ptr;
5203 struct lysc_node_leaf *sleaf;
5204 LY_ERR rc = LY_SUCCESS;
5205
5206 if (options & LYXP_SCNODE_ALL) {
5207 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5208 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5209 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 +02005210 } else if (!warn_is_string_type(sleaf->type)) {
5211 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005212 }
5213 }
5214
5215 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5216 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5217 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 +02005218 } else if (!warn_is_string_type(sleaf->type)) {
5219 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005220 }
5221 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005222 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005223 return rc;
5224 }
5225
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005226 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005227 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005228 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005229 LY_CHECK_RET(rc);
5230
5231 ptr = strstr(args[0]->val.str, args[1]->val.str);
5232 if (ptr) {
5233 set_fill_string(set, ptr + strlen(args[1]->val.str), strlen(ptr + strlen(args[1]->val.str)));
5234 } else {
5235 set_fill_string(set, "", 0);
5236 }
5237
5238 return LY_SUCCESS;
5239}
5240
5241/**
5242 * @brief Execute the XPath substring-before(string, string) function.
5243 * Returns LYXP_SET_STRING with the string preceding the occurance
5244 * of the second argument in the first or an empty string.
5245 *
5246 * @param[in] args Array of arguments.
5247 * @param[in] arg_count Count of elements in @p args.
5248 * @param[in,out] set Context and result set at the same time.
5249 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005250 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005251 */
5252static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02005253xpath_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 +02005254{
5255 char *ptr;
5256 struct lysc_node_leaf *sleaf;
5257 LY_ERR rc = LY_SUCCESS;
5258
5259 if (options & LYXP_SCNODE_ALL) {
5260 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5261 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5262 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 +02005263 } else if (!warn_is_string_type(sleaf->type)) {
5264 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005265 }
5266 }
5267
5268 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5269 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5270 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 +02005271 } else if (!warn_is_string_type(sleaf->type)) {
5272 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005273 }
5274 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005275 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005276 return rc;
5277 }
5278
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005279 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005280 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005281 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005282 LY_CHECK_RET(rc);
5283
5284 ptr = strstr(args[0]->val.str, args[1]->val.str);
5285 if (ptr) {
5286 set_fill_string(set, args[0]->val.str, ptr - args[0]->val.str);
5287 } else {
5288 set_fill_string(set, "", 0);
5289 }
5290
5291 return LY_SUCCESS;
5292}
5293
5294/**
5295 * @brief Execute the XPath sum(node-set) function. Returns LYXP_SET_NUMBER
5296 * with the sum of all the nodes in the context.
5297 *
5298 * @param[in] args Array of arguments.
5299 * @param[in] arg_count Count of elements in @p args.
5300 * @param[in,out] set Context and result set at the same time.
5301 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005302 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005303 */
5304static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02005305xpath_sum(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005306{
5307 long double num;
5308 char *str;
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01005309 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005310 struct lyxp_set set_item;
5311 struct lysc_node_leaf *sleaf;
5312 LY_ERR rc = LY_SUCCESS;
5313
5314 if (options & LYXP_SCNODE_ALL) {
5315 if (args[0]->type == LYXP_SET_SCNODE_SET) {
5316 for (i = 0; i < args[0]->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005317 if (args[0]->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005318 sleaf = (struct lysc_node_leaf *)args[0]->val.scnodes[i].scnode;
5319 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5320 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__,
Michal Vasko69730152020-10-09 16:30:07 +02005321 lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005322 } else if (!warn_is_numeric_type(sleaf->type)) {
5323 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005324 }
5325 }
5326 }
5327 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005328 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005329 return rc;
5330 }
5331
5332 set_fill_number(set, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005333
5334 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005335 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "sum(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02005336 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02005337 } else if (!args[0]->used) {
5338 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005339 }
5340
Michal Vasko5c4e5892019-11-14 12:31:38 +01005341 set_init(&set_item, set);
5342
Michal Vasko03ff5a72019-09-11 13:49:33 +02005343 set_item.type = LYXP_SET_NODE_SET;
Michal Vasko41decbf2021-11-02 11:50:21 +01005344 set_item.val.nodes = calloc(1, sizeof *set_item.val.nodes);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005345 LY_CHECK_ERR_RET(!set_item.val.nodes, LOGMEM(set->ctx), LY_EMEM);
5346
5347 set_item.used = 1;
5348 set_item.size = 1;
5349
5350 for (i = 0; i < args[0]->used; ++i) {
5351 set_item.val.nodes[0] = args[0]->val.nodes[i];
5352
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005353 rc = cast_node_set_to_string(&set_item, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005354 LY_CHECK_RET(rc);
5355 num = cast_string_to_number(str);
5356 free(str);
5357 set->val.num += num;
5358 }
5359
5360 free(set_item.val.nodes);
5361
5362 return LY_SUCCESS;
5363}
5364
5365/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02005366 * @brief Execute the XPath translate(string, string, string) function.
5367 * Returns LYXP_SET_STRING with the first argument with the characters
5368 * from the second argument replaced by those on the corresponding
5369 * positions in the third argument.
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_translate(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005379{
Michal Vaskodd528af2022-08-08 14:35:07 +02005380 uint32_t i, j, new_used;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005381 char *new;
Radek Krejci857189e2020-09-01 13:26:36 +02005382 ly_bool have_removed;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005383 struct lysc_node_leaf *sleaf;
5384 LY_ERR rc = LY_SUCCESS;
5385
5386 if (options & LYXP_SCNODE_ALL) {
5387 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5388 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5389 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 +02005390 } else if (!warn_is_string_type(sleaf->type)) {
5391 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005392 }
5393 }
5394
5395 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5396 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5397 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 +02005398 } else if (!warn_is_string_type(sleaf->type)) {
5399 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005400 }
5401 }
5402
5403 if ((args[2]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
5404 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5405 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 +02005406 } else if (!warn_is_string_type(sleaf->type)) {
5407 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005408 }
5409 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005410 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005411 return rc;
5412 }
5413
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005414 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005415 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005416 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005417 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005418 rc = lyxp_set_cast(args[2], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005419 LY_CHECK_RET(rc);
5420
5421 new = malloc((strlen(args[0]->val.str) + 1) * sizeof(char));
5422 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5423 new_used = 0;
5424
5425 have_removed = 0;
5426 for (i = 0; args[0]->val.str[i]; ++i) {
Radek Krejci857189e2020-09-01 13:26:36 +02005427 ly_bool found = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005428
5429 for (j = 0; args[1]->val.str[j]; ++j) {
5430 if (args[0]->val.str[i] == args[1]->val.str[j]) {
5431 /* removing this char */
5432 if (j >= strlen(args[2]->val.str)) {
5433 have_removed = 1;
5434 found = 1;
5435 break;
5436 }
5437 /* replacing this char */
5438 new[new_used] = args[2]->val.str[j];
5439 ++new_used;
5440 found = 1;
5441 break;
5442 }
5443 }
5444
5445 /* copying this char */
5446 if (!found) {
5447 new[new_used] = args[0]->val.str[i];
5448 ++new_used;
5449 }
5450 }
5451
5452 if (have_removed) {
5453 new = ly_realloc(new, (new_used + 1) * sizeof(char));
5454 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5455 }
5456 new[new_used] = '\0';
5457
Michal Vaskod3678892020-05-21 10:06:58 +02005458 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005459 set->type = LYXP_SET_STRING;
5460 set->val.str = new;
5461
5462 return LY_SUCCESS;
5463}
5464
5465/**
5466 * @brief Execute the XPath true() function. Returns LYXP_SET_BOOLEAN
5467 * with true value.
5468 *
5469 * @param[in] args Array of arguments.
5470 * @param[in] arg_count Count of elements in @p args.
5471 * @param[in,out] set Context and result set at the same time.
5472 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005473 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005474 */
5475static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02005476xpath_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 +02005477{
5478 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02005479 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005480 return LY_SUCCESS;
5481 }
5482
5483 set_fill_boolean(set, 1);
5484 return LY_SUCCESS;
5485}
5486
Michal Vasko03ff5a72019-09-11 13:49:33 +02005487/**
Michal Vasko49fec8e2022-05-24 10:28:33 +02005488 * @brief Execute the XPath node() processing instruction (node type). Returns LYXP_SET_NODE_SET
5489 * with only nodes from the context.
5490 *
5491 * @param[in,out] set Context and result set at the same time.
5492 * @param[in] axis Axis to search on.
5493 * @param[in] options XPath options.
5494 * @return LY_ERR
5495 */
5496static LY_ERR
5497xpath_pi_node(struct lyxp_set *set, enum lyxp_axis axis, uint32_t options)
5498{
5499 if (options & LYXP_SCNODE_ALL) {
5500 return moveto_scnode(set, NULL, NULL, axis, options);
5501 }
5502
5503 if (set->type != LYXP_SET_NODE_SET) {
5504 lyxp_set_free_content(set);
5505 return LY_SUCCESS;
5506 }
5507
5508 /* just like moving to a node with no restrictions */
5509 return moveto_node(set, NULL, NULL, axis, options);
5510}
5511
5512/**
5513 * @brief Execute the XPath text() processing instruction (node type). Returns LYXP_SET_NODE_SET
5514 * with the text content of the nodes in the context.
5515 *
5516 * @param[in,out] set Context and result set at the same time.
5517 * @param[in] axis Axis to search on.
5518 * @param[in] options XPath options.
5519 * @return LY_ERR
5520 */
5521static LY_ERR
5522xpath_pi_text(struct lyxp_set *set, enum lyxp_axis axis, uint32_t options)
5523{
5524 uint32_t i;
5525
5526 if (options & LYXP_SCNODE_ALL) {
5527 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
5528 return LY_SUCCESS;
5529 }
5530
5531 if (set->type != LYXP_SET_NODE_SET) {
5532 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "text()");
5533 return LY_EVALID;
5534 }
5535
5536 if (axis != LYXP_AXIS_CHILD) {
5537 /* even following and preceding axescan return text nodes, but whatever */
5538 lyxp_set_free_content(set);
5539 return LY_SUCCESS;
5540 }
5541
5542 for (i = 0; i < set->used; ++i) {
5543 switch (set->val.nodes[i].type) {
5544 case LYXP_NODE_NONE:
5545 LOGINT_RET(set->ctx);
5546 case LYXP_NODE_ELEM:
5547 if (set->val.nodes[i].node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
5548 set->val.nodes[i].type = LYXP_NODE_TEXT;
5549 break;
5550 }
5551 /* fall through */
5552 case LYXP_NODE_ROOT:
5553 case LYXP_NODE_ROOT_CONFIG:
5554 case LYXP_NODE_TEXT:
5555 case LYXP_NODE_META:
5556 set_remove_node_none(set, i);
5557 break;
5558 }
5559 }
5560 set_remove_nodes_none(set);
5561
5562 return LY_SUCCESS;
5563}
5564
5565/**
Michal Vasko6346ece2019-09-24 13:12:53 +02005566 * @brief Skip prefix and return corresponding model if there is a prefix. Logs directly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005567 *
Michal Vasko2104e9f2020-03-06 08:23:25 +01005568 * XPath @p set is expected to be a (sc)node set!
5569 *
Michal Vasko6346ece2019-09-24 13:12:53 +02005570 * @param[in,out] qname Qualified node name. If includes prefix, it is skipped.
5571 * @param[in,out] qname_len Length of @p qname, is updated accordingly.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005572 * @param[in] set Set with general XPath context.
5573 * @param[in] ctx_scnode Context node to inherit module for unprefixed node for ::LY_PREF_JSON.
Michal Vasko6346ece2019-09-24 13:12:53 +02005574 * @param[out] moveto_mod Expected module of a matching node.
5575 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005576 */
Michal Vasko6346ece2019-09-24 13:12:53 +02005577static LY_ERR
Michal Vaskofe1af042022-07-29 14:58:59 +02005578moveto_resolve_model(const char **qname, uint32_t *qname_len, const struct lyxp_set *set,
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005579 const struct lysc_node *ctx_scnode, const struct lys_module **moveto_mod)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005580{
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02005581 const struct lys_module *mod = NULL;
Michal Vasko6346ece2019-09-24 13:12:53 +02005582 const char *ptr;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005583 size_t pref_len;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005584
Michal Vasko2104e9f2020-03-06 08:23:25 +01005585 assert((set->type == LYXP_SET_NODE_SET) || (set->type == LYXP_SET_SCNODE_SET));
5586
Michal Vasko6346ece2019-09-24 13:12:53 +02005587 if ((ptr = ly_strnchr(*qname, ':', *qname_len))) {
5588 /* specific module */
5589 pref_len = ptr - *qname;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005590 mod = ly_resolve_prefix(set->ctx, *qname, pref_len, set->format, set->prefix_data);
Michal Vasko6346ece2019-09-24 13:12:53 +02005591
Michal Vasko004d3152020-06-11 19:59:22 +02005592 /* check for errors and non-implemented modules, as they are not valid */
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005593 if (!mod || !mod->implemented) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005594 LOGVAL(set->ctx, LY_VCODE_XP_INMOD, pref_len, *qname);
Michal Vasko6346ece2019-09-24 13:12:53 +02005595 return LY_EVALID;
5596 }
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005597
Michal Vasko6346ece2019-09-24 13:12:53 +02005598 *qname += pref_len + 1;
5599 *qname_len -= pref_len + 1;
5600 } else if (((*qname)[0] == '*') && (*qname_len == 1)) {
5601 /* all modules - special case */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005602 mod = NULL;
Michal Vasko6346ece2019-09-24 13:12:53 +02005603 } else {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005604 switch (set->format) {
Radek Krejci8df109d2021-04-23 12:19:08 +02005605 case LY_VALUE_SCHEMA:
5606 case LY_VALUE_SCHEMA_RESOLVED:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005607 /* current module */
5608 mod = set->cur_mod;
5609 break;
Radek Krejci224d4b42021-04-23 13:54:59 +02005610 case LY_VALUE_CANON:
Radek Krejci8df109d2021-04-23 12:19:08 +02005611 case LY_VALUE_JSON:
Radek Krejcif9943642021-04-26 10:18:21 +02005612 case LY_VALUE_LYB:
Michal Vaskoddd76592022-01-17 13:34:48 +01005613 case LY_VALUE_STR_NS:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005614 /* inherit parent (context node) module */
5615 if (ctx_scnode) {
5616 mod = ctx_scnode->module;
5617 } else {
5618 mod = NULL;
5619 }
5620 break;
Radek Krejci8df109d2021-04-23 12:19:08 +02005621 case LY_VALUE_XML:
Michal Vasko52143d12021-04-14 15:36:39 +02005622 /* all nodes need to be prefixed */
5623 LOGVAL(set->ctx, LYVE_DATA, "Non-prefixed node \"%.*s\" in XML xpath found.", *qname_len, *qname);
5624 return LY_EVALID;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005625 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005626 }
5627
Michal Vasko6346ece2019-09-24 13:12:53 +02005628 *moveto_mod = mod;
5629 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005630}
5631
5632/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02005633 * @brief Move context @p set to the root. Handles absolute path.
5634 * Result is LYXP_SET_NODE_SET.
5635 *
5636 * @param[in,out] set Set to use.
5637 * @param[in] options Xpath options.
Michal Vaskob0099a92020-08-31 14:55:23 +02005638 * @return LY_ERR value.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005639 */
Michal Vaskob0099a92020-08-31 14:55:23 +02005640static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005641moveto_root(struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005642{
aPiecek8b0cc152021-05-31 16:40:31 +02005643 assert(!(options & LYXP_SKIP_EXPR));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005644
5645 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02005646 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskoe4a6d012023-05-22 14:34:52 +02005647 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, NULL, set->root_type, LYXP_AXIS_SELF, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005648 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005649 set->type = LYXP_SET_NODE_SET;
5650 set->used = 0;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005651 set_insert_node(set, NULL, 0, set->root_type, 0);
Michal Vasko306e2832022-07-25 09:15:17 +02005652 set->non_child_axis = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005653 }
Michal Vaskob0099a92020-08-31 14:55:23 +02005654
5655 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005656}
5657
5658/**
5659 * @brief Check @p node as a part of NameTest processing.
5660 *
5661 * @param[in] node Node to check.
Michal Vasko49fec8e2022-05-24 10:28:33 +02005662 * @param[in] node_type Node type of @p node.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005663 * @param[in] set Set to read general context from.
Michal Vaskod3678892020-05-21 10:06:58 +02005664 * @param[in] node_name Node name in the dictionary to move to, NULL for any node.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005665 * @param[in] moveto_mod Expected module of the node, NULL for no prefix.
Michal Vaskocdad7122020-11-09 21:04:44 +01005666 * @param[in] options XPath options.
Michal Vasko6346ece2019-09-24 13:12:53 +02005667 * @return LY_ERR (LY_ENOT if node does not match, LY_EINCOMPLETE on unresolved when,
Michal Vaskocd2c88a2022-06-07 10:54:34 +02005668 * LY_EINVAL if neither node nor any children match)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005669 */
5670static LY_ERR
Michal Vasko49fec8e2022-05-24 10:28:33 +02005671moveto_node_check(const struct lyd_node *node, enum lyxp_node_type node_type, const struct lyxp_set *set,
5672 const char *node_name, const struct lys_module *moveto_mod, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005673{
Michal Vasko49fec8e2022-05-24 10:28:33 +02005674 if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT)) {
5675 assert(node_type == set->root_type);
5676
5677 if (node_name || moveto_mod) {
5678 /* root will not match a specific node */
5679 return LY_ENOT;
5680 }
5681 return LY_SUCCESS;
5682 } else if (node_type != LYXP_NODE_ELEM) {
5683 /* other types will not match */
5684 return LY_ENOT;
5685 }
5686
Michal Vaskodca9f122021-07-16 13:56:22 +02005687 if (!node->schema) {
5688 /* opaque node never matches */
5689 return LY_ENOT;
5690 }
5691
Michal Vasko03ff5a72019-09-11 13:49:33 +02005692 /* module check */
Michal Vasko19089f02022-06-07 11:02:11 +02005693 if (moveto_mod) {
Michal Vasko0a1eb752022-09-29 11:00:34 +02005694 if ((set->ctx == LYD_CTX(node)) && (node->schema->module != moveto_mod)) {
Michal Vasko19089f02022-06-07 11:02:11 +02005695 return LY_ENOT;
Michal Vasko0a1eb752022-09-29 11:00:34 +02005696 } else if ((set->ctx != LYD_CTX(node)) && strcmp(node->schema->module->name, moveto_mod->name)) {
Michal Vasko19089f02022-06-07 11:02:11 +02005697 return LY_ENOT;
5698 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005699 }
5700
Michal Vasko5c4e5892019-11-14 12:31:38 +01005701 /* context check */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005702 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (node->schema->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005703 return LY_EINVAL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005704 } else if (set->context_op && (node->schema->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) &&
5705 (node->schema != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005706 return LY_EINVAL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005707 }
5708
5709 /* name check */
Michal Vasko19089f02022-06-07 11:02:11 +02005710 if (node_name) {
Michal Vasko0a1eb752022-09-29 11:00:34 +02005711 if ((set->ctx == LYD_CTX(node)) && (node->schema->name != node_name)) {
Michal Vasko19089f02022-06-07 11:02:11 +02005712 return LY_ENOT;
Michal Vasko0a1eb752022-09-29 11:00:34 +02005713 } else if ((set->ctx != LYD_CTX(node)) && strcmp(node->schema->name, node_name)) {
Michal Vasko19089f02022-06-07 11:02:11 +02005714 return LY_ENOT;
5715 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005716 }
5717
Michal Vasko6d657122022-10-19 14:38:35 +02005718 /* when check, accept the context node because it should only be the path ".", we have checked the when is valid before */
5719 if (!(options & LYXP_IGNORE_WHEN) && lysc_has_when(node->schema) && !(node->flags & LYD_WHEN_TRUE) &&
5720 (node != set->cur_node)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005721 return LY_EINCOMPLETE;
Michal Vaskoa1424542019-11-14 16:08:52 +01005722 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005723
5724 /* match */
5725 return LY_SUCCESS;
5726}
5727
5728/**
Michal Vasko49fec8e2022-05-24 10:28:33 +02005729 * @brief Get the next node in a forward DFS.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005730 *
Michal Vasko49fec8e2022-05-24 10:28:33 +02005731 * @param[in] iter Last returned node.
5732 * @param[in] stop Node to stop the search on and not return.
5733 * @return Next node, NULL if there are no more.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005734 */
Michal Vasko49fec8e2022-05-24 10:28:33 +02005735static const struct lyd_node *
5736moveto_axis_node_next_dfs_forward(const struct lyd_node *iter, const struct lyd_node *stop)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005737{
Michal Vasko49fec8e2022-05-24 10:28:33 +02005738 const struct lyd_node *next = NULL;
5739
5740 /* 1) child */
5741 next = lyd_child(iter);
5742 if (!next) {
5743 if (iter == stop) {
5744 /* reached stop, no more descendants */
5745 return NULL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005746 }
Michal Vasko49fec8e2022-05-24 10:28:33 +02005747 /* 2) child next sibling */
5748 next = iter->next;
5749 }
5750 while (!next) {
5751 iter = lyd_parent(iter);
5752 if ((!stop && !iter) || (stop && (lyd_parent(iter) == lyd_parent(stop)))) {
5753 return NULL;
5754 }
5755 next = iter->next;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005756 }
5757
Michal Vasko49fec8e2022-05-24 10:28:33 +02005758 return next;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005759}
5760
5761/**
Michal Vasko49fec8e2022-05-24 10:28:33 +02005762 * @brief Get the next node in a backward DFS.
5763 *
5764 * @param[in] iter Last returned node.
5765 * @param[in] stop Node to stop the search on and not return.
5766 * @return Next node, NULL if there are no more.
5767 */
5768static const struct lyd_node *
5769moveto_axis_node_next_dfs_backward(const struct lyd_node *iter, const struct lyd_node *stop)
5770{
5771 const struct lyd_node *next = NULL;
5772
5773 /* 1) previous sibling innermost last child */
5774 next = iter->prev->next ? iter->prev : NULL;
5775 while (next && lyd_child(next)) {
5776 next = lyd_child(next);
5777 next = next->prev;
5778 }
5779
5780 if (!next) {
5781 /* 2) parent */
5782 iter = lyd_parent(iter);
5783 if ((!stop && !iter) || (stop && (lyd_parent(iter) == lyd_parent(stop)))) {
5784 return NULL;
5785 }
5786 next = iter;
5787 }
5788
5789 return next;
5790}
5791
5792/**
5793 * @brief Get the first node on an axis for a context node.
5794 *
5795 * @param[in,out] iter NULL, updated to the next node.
5796 * @param[in,out] iter_type Node type 0 of @p iter, updated to the node type of the next node.
5797 * @param[in] node Context node.
5798 * @param[in] node_type Type of @p node.
5799 * @param[in] axis Axis to use.
5800 * @param[in] set XPath set with the general context.
5801 * @return LY_SUCCESS on success.
5802 * @return LY_ENOTFOUND if no next node found.
5803 */
5804static LY_ERR
5805moveto_axis_node_next_first(const struct lyd_node **iter, enum lyxp_node_type *iter_type, const struct lyd_node *node,
5806 enum lyxp_node_type node_type, enum lyxp_axis axis, struct lyxp_set *set)
5807{
5808 const struct lyd_node *next = NULL;
5809 enum lyxp_node_type next_type = 0;
5810
5811 assert(!*iter);
5812 assert(!*iter_type);
5813
5814 switch (axis) {
5815 case LYXP_AXIS_ANCESTOR_OR_SELF:
5816 case LYXP_AXIS_DESCENDANT_OR_SELF:
5817 case LYXP_AXIS_SELF:
5818 /* return the context node */
5819 next = node;
5820 next_type = node_type;
5821 break;
5822
5823 case LYXP_AXIS_ANCESTOR:
5824 case LYXP_AXIS_PARENT:
5825 if (node_type == LYXP_NODE_ELEM) {
5826 next = lyd_parent(node);
5827 next_type = next ? LYXP_NODE_ELEM : set->root_type;
5828 } else if (node_type == LYXP_NODE_TEXT) {
5829 next = node;
5830 next_type = LYXP_NODE_ELEM;
5831 } else if (node_type == LYXP_NODE_META) {
5832 next = ((struct lyd_meta *)node)->parent;
5833 next_type = LYXP_NODE_ELEM;
5834 } /* else root does not have a parent */
5835 break;
5836
5837 case LYXP_AXIS_CHILD:
5838 if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT)) {
5839 assert(!node);
5840
5841 /* search in all the trees */
5842 next = set->tree;
5843 next_type = next ? LYXP_NODE_ELEM : 0;
5844 } else {
5845 /* search in children */
5846 next = lyd_child(node);
5847 next_type = next ? LYXP_NODE_ELEM : 0;
5848 }
5849 break;
5850
5851 case LYXP_AXIS_DESCENDANT:
5852 if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT)) {
5853 /* top-level nodes */
5854 next = set->tree;
5855 next_type = LYXP_NODE_ELEM;
5856 } else if (node_type == LYXP_NODE_ELEM) {
5857 /* start from the context node */
5858 next = moveto_axis_node_next_dfs_forward(node, node);
5859 next_type = next ? LYXP_NODE_ELEM : 0;
5860 } /* else no children */
5861 break;
5862
5863 case LYXP_AXIS_FOLLOWING:
5864 case LYXP_AXIS_FOLLOWING_SIBLING:
5865 if (node_type == LYXP_NODE_ELEM) {
5866 /* first next sibling */
5867 next = node->next;
5868 next_type = next ? LYXP_NODE_ELEM : 0;
5869 } /* else no sibling */
5870 break;
5871
5872 case LYXP_AXIS_PRECEDING:
5873 if ((node_type == LYXP_NODE_ELEM) && node->prev->next) {
5874 /* skip ancestors */
5875 next = moveto_axis_node_next_dfs_backward(node, NULL);
5876 assert(next);
5877 next_type = LYXP_NODE_ELEM;
5878 } /* else no sibling */
5879 break;
5880
5881 case LYXP_AXIS_PRECEDING_SIBLING:
5882 if (node_type == LYXP_NODE_ELEM) {
5883 /* first previous sibling */
5884 next = node->prev->next ? node->prev : NULL;
5885 next_type = next ? LYXP_NODE_ELEM : 0;
5886 } /* else no sibling */
5887 break;
5888
5889 case LYXP_AXIS_ATTRIBUTE:
5890 /* handled specially */
5891 assert(0);
5892 LOGINT(set->ctx);
5893 break;
5894 }
5895
5896 *iter = next;
5897 *iter_type = next_type;
5898 return next_type ? LY_SUCCESS : LY_ENOTFOUND;
5899}
5900
5901/**
5902 * @brief Iterate over all nodes on an axis for a context node.
5903 *
5904 * @param[in,out] iter Last returned node, start with NULL, updated to the next node.
5905 * @param[in,out] iter_type Node type of @p iter, start with 0, updated to the node type of the next node.
5906 * @param[in] node Context node.
5907 * @param[in] node_type Type of @p node.
5908 * @param[in] axis Axis to use.
5909 * @param[in] set XPath set with the general context.
5910 * @return LY_SUCCESS on success.
5911 * @return LY_ENOTFOUND if no next node found.
5912 */
5913static LY_ERR
5914moveto_axis_node_next(const struct lyd_node **iter, enum lyxp_node_type *iter_type, const struct lyd_node *node,
5915 enum lyxp_node_type node_type, enum lyxp_axis axis, struct lyxp_set *set)
5916{
5917 const struct lyd_node *next = NULL;
5918 enum lyxp_node_type next_type = 0;
5919
5920 if (!*iter_type) {
5921 /* first returned node */
5922 return moveto_axis_node_next_first(iter, iter_type, node, node_type, axis, set);
5923 }
5924
5925 switch (axis) {
5926 case LYXP_AXIS_ANCESTOR_OR_SELF:
5927 if ((*iter == node) && (*iter_type == node_type)) {
5928 /* fake first ancestor, we returned self before */
5929 *iter = NULL;
5930 *iter_type = 0;
5931 return moveto_axis_node_next_first(iter, iter_type, node, node_type, LYXP_AXIS_ANCESTOR, set);
5932 } /* else continue ancestor */
5933
5934 /* fallthrough */
5935 case LYXP_AXIS_ANCESTOR:
5936 if (*iter_type == LYXP_NODE_ELEM) {
5937 /* iter parent */
5938 next = lyd_parent(*iter);
5939 next_type = next ? LYXP_NODE_ELEM : set->root_type;
5940 } /* else root, no ancestors */
5941 break;
5942
5943 case LYXP_AXIS_CHILD:
5944 assert(*iter_type == LYXP_NODE_ELEM);
5945
5946 /* next sibling (child) */
5947 next = (*iter)->next;
5948 next_type = next ? LYXP_NODE_ELEM : 0;
5949 break;
5950
5951 case LYXP_AXIS_DESCENDANT_OR_SELF:
5952 if ((*iter == node) && (*iter_type == node_type)) {
5953 /* fake first descendant, we returned self before */
5954 *iter = NULL;
5955 *iter_type = 0;
5956 return moveto_axis_node_next_first(iter, iter_type, node, node_type, LYXP_AXIS_DESCENDANT, set);
5957 } /* else continue descendant */
5958
5959 /* fallthrough */
5960 case LYXP_AXIS_DESCENDANT:
5961 assert(*iter_type == LYXP_NODE_ELEM);
5962 next = moveto_axis_node_next_dfs_forward(*iter, node);
5963 next_type = next ? LYXP_NODE_ELEM : 0;
5964 break;
5965
5966 case LYXP_AXIS_FOLLOWING:
5967 assert(*iter_type == LYXP_NODE_ELEM);
5968 next = moveto_axis_node_next_dfs_forward(*iter, NULL);
5969 next_type = next ? LYXP_NODE_ELEM : 0;
5970 break;
5971
5972 case LYXP_AXIS_FOLLOWING_SIBLING:
5973 assert(*iter_type == LYXP_NODE_ELEM);
5974
5975 /* next sibling */
5976 next = (*iter)->next;
5977 next_type = next ? LYXP_NODE_ELEM : 0;
5978 break;
5979
5980 case LYXP_AXIS_PARENT:
5981 case LYXP_AXIS_SELF:
5982 /* parent/self was returned before */
5983 break;
5984
5985 case LYXP_AXIS_PRECEDING:
5986 assert(*iter_type == LYXP_NODE_ELEM);
5987 next = moveto_axis_node_next_dfs_backward(*iter, NULL);
5988 next_type = next ? LYXP_NODE_ELEM : 0;
5989 break;
5990
5991 case LYXP_AXIS_PRECEDING_SIBLING:
5992 assert(*iter_type == LYXP_NODE_ELEM);
5993
5994 /* previous sibling */
5995 next = (*iter)->prev->next ? (*iter)->prev : NULL;
5996 next_type = next ? LYXP_NODE_ELEM : 0;
5997 break;
5998
5999 case LYXP_AXIS_ATTRIBUTE:
6000 /* handled specially */
6001 assert(0);
6002 LOGINT(set->ctx);
6003 break;
6004 }
6005
6006 *iter = next;
6007 *iter_type = next_type;
6008 return next_type ? LY_SUCCESS : LY_ENOTFOUND;
6009}
6010
6011/**
6012 * @brief Move context @p set to a node. Result is LYXP_SET_NODE_SET. Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006013 *
6014 * @param[in,out] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006015 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02006016 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko49fec8e2022-05-24 10:28:33 +02006017 * @param[in] axis Axis to search on.
Michal Vaskocdad7122020-11-09 21:04:44 +01006018 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006019 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6020 */
6021static LY_ERR
Michal Vasko49fec8e2022-05-24 10:28:33 +02006022moveto_node(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, enum lyxp_axis axis,
6023 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006024{
Michal Vasko230cf972021-12-02 12:31:00 +01006025 LY_ERR r, rc = LY_SUCCESS;
Michal Vasko49fec8e2022-05-24 10:28:33 +02006026 const struct lyd_node *iter;
6027 enum lyxp_node_type iter_type;
Michal Vasko230cf972021-12-02 12:31:00 +01006028 struct lyxp_set result;
Michal Vasko49fec8e2022-05-24 10:28:33 +02006029 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006030
aPiecek8b0cc152021-05-31 16:40:31 +02006031 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006032 return LY_SUCCESS;
6033 }
6034
6035 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006036 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006037 return LY_EVALID;
6038 }
6039
Michal Vasko230cf972021-12-02 12:31:00 +01006040 /* init result set */
6041 set_init(&result, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006042
Michal Vasko49fec8e2022-05-24 10:28:33 +02006043 for (i = 0; i < set->used; ++i) {
6044 /* iterate over all the nodes on the axis of the node */
6045 iter = NULL;
6046 iter_type = 0;
6047 while (!moveto_axis_node_next(&iter, &iter_type, set->val.nodes[i].node, set->val.nodes[i].type, axis, set)) {
6048 r = moveto_node_check(iter, iter_type, set, ncname, moveto_mod, options);
6049 if (r == LY_EINCOMPLETE) {
Michal Vasko230cf972021-12-02 12:31:00 +01006050 rc = r;
6051 goto cleanup;
Michal Vasko49fec8e2022-05-24 10:28:33 +02006052 } else if (r) {
6053 continue;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006054 }
Michal Vasko49fec8e2022-05-24 10:28:33 +02006055
6056 /* check for duplicates if they are possible */
6057 switch (axis) {
6058 case LYXP_AXIS_ANCESTOR:
6059 case LYXP_AXIS_ANCESTOR_OR_SELF:
6060 case LYXP_AXIS_DESCENDANT:
6061 case LYXP_AXIS_DESCENDANT_OR_SELF:
6062 case LYXP_AXIS_FOLLOWING:
6063 case LYXP_AXIS_FOLLOWING_SIBLING:
6064 case LYXP_AXIS_PARENT:
6065 case LYXP_AXIS_PRECEDING:
6066 case LYXP_AXIS_PRECEDING_SIBLING:
Michal Vasko306e2832022-07-25 09:15:17 +02006067 result.non_child_axis = 1;
Michal Vasko49fec8e2022-05-24 10:28:33 +02006068 if (set_dup_node_check(&result, iter, iter_type, -1)) {
6069 continue;
6070 }
6071 break;
6072 case LYXP_AXIS_CHILD:
6073 case LYXP_AXIS_SELF:
6074 break;
6075 case LYXP_AXIS_ATTRIBUTE:
6076 /* handled specially */
6077 assert(0);
6078 LOGINT(set->ctx);
6079 break;
6080 }
6081
6082 /* matching node */
6083 set_insert_node(&result, iter, 0, iter_type, result.used);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006084 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006085 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006086
Michal Vasko230cf972021-12-02 12:31:00 +01006087 /* move result to the set */
6088 lyxp_set_free_content(set);
6089 *set = result;
6090 result.type = LYXP_SET_NUMBER;
Michal Vasko49fec8e2022-05-24 10:28:33 +02006091
Michal Vasko306e2832022-07-25 09:15:17 +02006092 /* sort the final set if the document order could have been broken */
6093 if (set->non_child_axis) {
6094 set_sort(set);
6095 } else {
6096 assert(!set_sort(set));
6097 }
Michal Vasko230cf972021-12-02 12:31:00 +01006098
6099cleanup:
6100 lyxp_set_free_content(&result);
6101 return rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006102}
6103
6104/**
Michal Vasko49fec8e2022-05-24 10:28:33 +02006105 * @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 +02006106 *
6107 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02006108 * @param[in] scnode Matching node schema.
Michal Vasko004d3152020-06-11 19:59:22 +02006109 * @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 +01006110 * @param[in] options XPath options.
Michal Vaskod3678892020-05-21 10:06:58 +02006111 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6112 */
6113static LY_ERR
Michal Vasko49fec8e2022-05-24 10:28:33 +02006114moveto_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 +01006115 uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02006116{
Michal Vaskoddd76592022-01-17 13:34:48 +01006117 LY_ERR ret = LY_SUCCESS, r;
Michal Vaskod3678892020-05-21 10:06:58 +02006118 uint32_t i;
Michal Vaskod3678892020-05-21 10:06:58 +02006119 const struct lyd_node *siblings;
Michal Vasko230cf972021-12-02 12:31:00 +01006120 struct lyxp_set result;
Michal Vasko004d3152020-06-11 19:59:22 +02006121 struct lyd_node *sub, *inst = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02006122
Michal Vasko004d3152020-06-11 19:59:22 +02006123 assert(scnode && (!(scnode->nodetype & (LYS_LIST | LYS_LEAFLIST)) || predicates));
Michal Vaskod3678892020-05-21 10:06:58 +02006124
Michal Vasko50aaa072021-12-02 13:11:56 +01006125 /* init result set */
6126 set_init(&result, set);
6127
aPiecek8b0cc152021-05-31 16:40:31 +02006128 if (options & LYXP_SKIP_EXPR) {
Michal Vasko004d3152020-06-11 19:59:22 +02006129 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02006130 }
6131
6132 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006133 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko004d3152020-06-11 19:59:22 +02006134 ret = LY_EVALID;
6135 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02006136 }
6137
6138 /* context check for all the nodes since we have the schema node */
6139 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) {
6140 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02006141 goto cleanup;
Michal Vasko69730152020-10-09 16:30:07 +02006142 } else if (set->context_op && (scnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) &&
6143 (scnode != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02006144 lyxp_set_free_content(set);
6145 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +02006146 }
6147
6148 /* create specific data instance if needed */
6149 if (scnode->nodetype == LYS_LIST) {
Michal Vasko90189962023-02-28 12:10:34 +01006150 LY_CHECK_GOTO(ret = lyd_create_list(scnode, predicates, NULL, &inst), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02006151 } else if (scnode->nodetype == LYS_LEAFLIST) {
6152 LY_CHECK_GOTO(ret = lyd_create_term2(scnode, &predicates[0].value, &inst), cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02006153 }
6154
Michal Vasko230cf972021-12-02 12:31:00 +01006155 for (i = 0; i < set->used; ++i) {
Michal Vaskod3678892020-05-21 10:06:58 +02006156 siblings = NULL;
6157
6158 if ((set->val.nodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.nodes[i].type == LYXP_NODE_ROOT)) {
6159 assert(!set->val.nodes[i].node);
6160
6161 /* search in all the trees */
6162 siblings = set->tree;
6163 } else if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
6164 /* search in children */
Radek Krejcia1c1e542020-09-29 16:06:52 +02006165 siblings = lyd_child(set->val.nodes[i].node);
Michal Vaskod3678892020-05-21 10:06:58 +02006166 }
6167
6168 /* find the node using hashes */
Michal Vasko004d3152020-06-11 19:59:22 +02006169 if (inst) {
Michal Vaskoddd76592022-01-17 13:34:48 +01006170 r = lyd_find_sibling_first(siblings, inst, &sub);
Michal Vaskod3678892020-05-21 10:06:58 +02006171 } else {
Michal Vaskoddd76592022-01-17 13:34:48 +01006172 r = lyd_find_sibling_val(siblings, scnode, NULL, 0, &sub);
Michal Vaskod3678892020-05-21 10:06:58 +02006173 }
Michal Vaskoddd76592022-01-17 13:34:48 +01006174 LY_CHECK_ERR_GOTO(r && (r != LY_ENOTFOUND), ret = r, cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02006175
6176 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01006177 if (!(options & LYXP_IGNORE_WHEN) && sub && lysc_has_when(sub->schema) && !(sub->flags & LYD_WHEN_TRUE)) {
Michal Vasko004d3152020-06-11 19:59:22 +02006178 ret = LY_EINCOMPLETE;
6179 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02006180 }
6181
6182 if (sub) {
6183 /* pos filled later */
Michal Vasko230cf972021-12-02 12:31:00 +01006184 set_insert_node(&result, sub, 0, LYXP_NODE_ELEM, result.used);
Michal Vaskod3678892020-05-21 10:06:58 +02006185 }
6186 }
6187
Michal Vasko230cf972021-12-02 12:31:00 +01006188 /* move result to the set */
6189 lyxp_set_free_content(set);
6190 *set = result;
6191 result.type = LYXP_SET_NUMBER;
6192 assert(!set_sort(set));
6193
Michal Vasko004d3152020-06-11 19:59:22 +02006194cleanup:
Michal Vasko230cf972021-12-02 12:31:00 +01006195 lyxp_set_free_content(&result);
Michal Vasko004d3152020-06-11 19:59:22 +02006196 lyd_free_tree(inst);
6197 return ret;
Michal Vaskod3678892020-05-21 10:06:58 +02006198}
6199
6200/**
Michal Vasko49fec8e2022-05-24 10:28:33 +02006201 * @brief Check @p node as a part of schema NameTest processing.
6202 *
6203 * @param[in] node Schema node to check.
6204 * @param[in] ctx_scnode Context node.
6205 * @param[in] set Set to read general context from.
6206 * @param[in] node_name Node name in the dictionary to move to, NULL for any nodes.
6207 * @param[in] moveto_mod Expected module of the node, NULL for no prefix.
6208 * @return LY_ERR (LY_ENOT if node does not match, LY_EINVAL if neither node nor any children match)
6209 */
6210static LY_ERR
6211moveto_scnode_check(const struct lysc_node *node, const struct lysc_node *ctx_scnode, const struct lyxp_set *set,
6212 const char *node_name, const struct lys_module *moveto_mod)
6213{
6214 if (!moveto_mod && node_name) {
6215 switch (set->format) {
6216 case LY_VALUE_SCHEMA:
6217 case LY_VALUE_SCHEMA_RESOLVED:
6218 /* use current module */
6219 moveto_mod = set->cur_mod;
6220 break;
6221 case LY_VALUE_JSON:
6222 case LY_VALUE_LYB:
6223 case LY_VALUE_STR_NS:
6224 /* inherit module of the context node, if any */
6225 if (ctx_scnode) {
6226 moveto_mod = ctx_scnode->module;
6227 }
6228 break;
6229 case LY_VALUE_CANON:
6230 case LY_VALUE_XML:
6231 /* not defined */
6232 LOGINT(set->ctx);
6233 return LY_EINVAL;
6234 }
6235 }
6236
6237 if (!node) {
6238 /* root will not match a specific node */
6239 if (node_name || moveto_mod) {
6240 return LY_ENOT;
6241 }
6242 return LY_SUCCESS;
6243 }
6244
6245 /* module check */
6246 if (moveto_mod && (node->module != moveto_mod)) {
6247 return LY_ENOT;
6248 }
6249
6250 /* context check */
6251 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (node->flags & LYS_CONFIG_R)) {
6252 return LY_EINVAL;
6253 } else if (set->context_op && (node->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && (node != set->context_op)) {
6254 return LY_EINVAL;
6255 }
6256
6257 /* name check */
6258 if (node_name && (node->name != node_name)) {
6259 return LY_ENOT;
6260 }
6261
6262 /* match */
6263 return LY_SUCCESS;
6264}
6265
6266/**
6267 * @brief Get the next node in a forward schema node DFS.
6268 *
6269 * @param[in] iter Last returned node.
6270 * @param[in] stop Node to stop the search on and not return.
6271 * @param[in] getnext_opts Options for ::lys_getnext().
6272 * @return Next node, NULL if there are no more.
6273 */
6274static const struct lysc_node *
6275moveto_axis_scnode_next_dfs_forward(const struct lysc_node *iter, const struct lysc_node *stop, uint32_t getnext_opts)
6276{
6277 const struct lysc_node *next = NULL;
6278
6279 next = lysc_node_child(iter);
6280 if (!next) {
6281 /* no children, try siblings */
Michal Vasko34a22fe2022-06-15 07:58:55 +02006282 if ((iter == stop) || !lysc_data_parent(iter)) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02006283 /* we are done, no next element to process */
6284 return NULL;
6285 }
6286
6287 next = lys_getnext(iter, lysc_data_parent(iter), NULL, getnext_opts);
6288 }
6289 while (!next && iter) {
6290 /* parent is already processed, go to its sibling */
6291 iter = iter->parent;
Michal Vasko34a22fe2022-06-15 07:58:55 +02006292 if ((iter == stop) || !lysc_data_parent(iter)) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02006293 /* we are done, no next element to process */
6294 return NULL;
6295 }
6296 next = lys_getnext(iter, lysc_data_parent(iter), NULL, getnext_opts);
6297 }
6298
6299 return next;
6300}
6301
6302/**
6303 * @brief Consider schema node based on its in_ctx enum value.
6304 *
6305 * @param[in,out] in_ctx In_ctx enum of the schema node, may be updated.
6306 * @param[in] axis Axis to use.
6307 * @return LY_SUCCESS on success.
6308 * @return LY_ENOT if the node should not be returned.
6309 */
6310static LY_ERR
6311moveto_axis_scnode_next_in_ctx(int32_t *in_ctx, enum lyxp_axis axis)
6312{
6313 switch (axis) {
6314 case LYXP_AXIS_SELF:
6315 if ((*in_ctx == LYXP_SET_SCNODE_START) || (*in_ctx == LYXP_SET_SCNODE_ATOM_CTX)) {
6316 /* additionally put the start node into context */
6317 *in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
6318 return LY_SUCCESS;
6319 }
6320 break;
6321 case LYXP_AXIS_PARENT:
6322 case LYXP_AXIS_ANCESTOR_OR_SELF:
6323 case LYXP_AXIS_ANCESTOR:
6324 case LYXP_AXIS_DESCENDANT_OR_SELF:
6325 case LYXP_AXIS_DESCENDANT:
6326 case LYXP_AXIS_FOLLOWING:
6327 case LYXP_AXIS_FOLLOWING_SIBLING:
6328 case LYXP_AXIS_PRECEDING:
6329 case LYXP_AXIS_PRECEDING_SIBLING:
6330 case LYXP_AXIS_CHILD:
6331 if (*in_ctx == LYXP_SET_SCNODE_START) {
6332 /* remember that context node was used */
6333 *in_ctx = LYXP_SET_SCNODE_START_USED;
6334 return LY_SUCCESS;
6335 } else if (*in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
6336 /* traversed */
6337 *in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
6338 return LY_SUCCESS;
6339 }
6340 break;
6341 case LYXP_AXIS_ATTRIBUTE:
6342 /* unreachable */
6343 assert(0);
6344 LOGINT(NULL);
6345 break;
6346 }
6347
6348 return LY_ENOT;
6349}
6350
6351/**
6352 * @brief Get previous sibling for a schema node.
6353 *
6354 * @param[in] scnode Schema node.
6355 * @param[in] getnext_opts Options for ::lys_getnext().
6356 * @return Previous sibling, NULL if none.
6357 */
6358static const struct lysc_node *
6359moveto_axis_scnode_preceding_sibling(const struct lysc_node *scnode, uint32_t getnext_opts)
6360{
6361 const struct lysc_node *next = NULL, *prev = NULL;
6362
6363 while ((next = lys_getnext(next, lysc_data_parent(scnode), scnode->module->compiled, getnext_opts))) {
6364 if (next == scnode) {
6365 break;
6366 }
6367
6368 prev = next;
6369 }
6370
6371 return prev;
6372}
6373
6374/**
6375 * @brief Get the first schema node on an axis for a context node.
6376 *
6377 * @param[in,out] iter Last returned node, start with NULL, updated to the next node.
6378 * @param[in,out] iter_type Node type of @p iter, start with 0, updated to the node type of the next node.
6379 * @param[in,out] iter_mod Internal module iterator, do not change.
6380 * @param[in,out] iter_mod_idx Internal module index iterator, do not change.
6381 * @param[in] scnode Context node.
6382 * @param[in] node_type Type of @p scnode.
6383 * @param[in] in_ctx In_ctx enum of @p scnode.
6384 * @param[in] axis Axis to use.
6385 * @param[in] set XPath set with the general context.
6386 * @param[in] getnext_opts Options for ::lys_getnext().
6387 * @return LY_SUCCESS on success.
6388 * @return LY_ENOTFOUND if no next node found.
6389 */
6390static LY_ERR
6391moveto_axis_scnode_next_first(const struct lysc_node **iter, enum lyxp_node_type *iter_type, const struct lys_module **iter_mod,
6392 uint32_t *iter_mod_idx, const struct lysc_node *scnode, enum lyxp_node_type node_type, enum lyxp_axis axis,
6393 struct lyxp_set *set, uint32_t getnext_opts)
6394{
6395 const struct lysc_node *next = NULL;
6396 enum lyxp_node_type next_type = 0;
6397
6398 assert(!*iter);
6399 assert(!*iter_type);
6400
6401 *iter_mod = NULL;
6402 *iter_mod_idx = 0;
6403
6404 switch (axis) {
6405 case LYXP_AXIS_ANCESTOR_OR_SELF:
6406 case LYXP_AXIS_DESCENDANT_OR_SELF:
6407 case LYXP_AXIS_SELF:
6408 if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT) || (node_type == LYXP_NODE_ELEM)) {
6409 /* just return the node */
6410 next = scnode;
6411 next_type = node_type;
6412 }
6413 break;
6414
6415 case LYXP_AXIS_ANCESTOR:
6416 case LYXP_AXIS_PARENT:
6417 if (node_type == LYXP_NODE_ELEM) {
6418 next = lysc_data_parent(scnode);
6419 next_type = next ? LYXP_NODE_ELEM : set->root_type;
6420 } /* else no parent */
6421 break;
6422
6423 case LYXP_AXIS_DESCENDANT:
6424 case LYXP_AXIS_CHILD:
6425 if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT)) {
6426 /* it can actually be in any module, it's all <running>, and even if it's moveto_mod (if set),
6427 * it can be in a top-level augment */
6428 while ((*iter_mod = ly_ctx_get_module_iter(set->ctx, iter_mod_idx))) {
6429 /* module may not be implemented or not compiled yet */
6430 if (!(*iter_mod)->compiled) {
6431 continue;
6432 }
6433
6434 /* get next node */
6435 if ((next = lys_getnext(NULL, NULL, (*iter_mod)->compiled, getnext_opts))) {
6436 next_type = LYXP_NODE_ELEM;
6437 break;
6438 }
6439 }
6440 } else if (node_type == LYXP_NODE_ELEM) {
6441 /* get next node */
6442 next = lys_getnext(NULL, scnode, NULL, getnext_opts);
6443 next_type = next ? LYXP_NODE_ELEM : 0;
6444 }
6445 break;
6446
6447 case LYXP_AXIS_FOLLOWING:
6448 case LYXP_AXIS_FOLLOWING_SIBLING:
6449 if (node_type == LYXP_NODE_ELEM) {
6450 /* first next sibling */
6451 next = lys_getnext(scnode, lysc_data_parent(scnode), scnode->module->compiled, getnext_opts);
6452 next_type = next ? LYXP_NODE_ELEM : 0;
6453 } /* else no sibling */
6454 break;
6455
6456 case LYXP_AXIS_PRECEDING:
6457 case LYXP_AXIS_PRECEDING_SIBLING:
6458 if (node_type == LYXP_NODE_ELEM) {
6459 /* first parent sibling */
6460 next = lys_getnext(NULL, lysc_data_parent(scnode), scnode->module->compiled, getnext_opts);
6461 if (next == scnode) {
6462 /* no preceding sibling */
6463 next = NULL;
6464 }
6465 next_type = next ? LYXP_NODE_ELEM : 0;
6466 } /* else no sibling */
6467 break;
6468
6469 case LYXP_AXIS_ATTRIBUTE:
6470 /* unreachable */
6471 assert(0);
6472 LOGINT(set->ctx);
6473 break;
6474 }
6475
6476 *iter = next;
6477 *iter_type = next_type;
6478 return next_type ? LY_SUCCESS : LY_ENOTFOUND;
6479}
6480
6481/**
6482 * @brief Iterate over all schema nodes on an axis for a context node.
6483 *
6484 * @param[in,out] iter Last returned node, start with NULL, updated to the next node.
6485 * @param[in,out] iter_type Node type of @p iter, start with 0, updated to the node type of the next node.
6486 * @param[in,out] iter_mod Internal module iterator, do not change.
6487 * @param[in,out] iter_mod_idx Internal module index iterator, do not change.
6488 * @param[in] scnode Context node.
6489 * @param[in] node_type Type of @p scnode.
6490 * @param[in] axis Axis to use.
6491 * @param[in] set XPath set with the general context.
6492 * @param[in] getnext_opts Options for ::lys_getnext().
6493 * @return LY_SUCCESS on success.
6494 * @return LY_ENOTFOUND if no next node found.
6495 */
6496static LY_ERR
6497moveto_axis_scnode_next(const struct lysc_node **iter, enum lyxp_node_type *iter_type, const struct lys_module **iter_mod,
6498 uint32_t *iter_mod_idx, const struct lysc_node *scnode, enum lyxp_node_type node_type, enum lyxp_axis axis,
6499 struct lyxp_set *set, uint32_t getnext_opts)
6500{
6501 const struct lysc_node *next = NULL, *dfs_stop;
6502 enum lyxp_node_type next_type = 0;
6503
6504 if (!*iter_type) {
6505 /* first returned node */
6506 return moveto_axis_scnode_next_first(iter, iter_type, iter_mod, iter_mod_idx, scnode, node_type, axis, set,
6507 getnext_opts);
6508 }
6509
6510 switch (axis) {
6511 case LYXP_AXIS_PARENT:
6512 case LYXP_AXIS_SELF:
6513 /* parent/self was returned before */
6514 break;
6515
6516 case LYXP_AXIS_ANCESTOR_OR_SELF:
6517 if ((*iter == scnode) && (*iter_type == node_type)) {
6518 /* fake first ancestor, we returned self before */
6519 *iter = NULL;
6520 *iter_type = 0;
6521 return moveto_axis_scnode_next_first(iter, iter_type, iter_mod, iter_mod_idx, scnode, node_type,
6522 LYXP_AXIS_ANCESTOR, set, getnext_opts);
6523 } /* else continue ancestor */
6524
6525 /* fallthrough */
6526 case LYXP_AXIS_ANCESTOR:
6527 if (*iter_type == LYXP_NODE_ELEM) {
6528 next = lysc_data_parent(*iter);
6529 next_type = next ? LYXP_NODE_ELEM : set->root_type;
6530 } /* else no ancestor */
6531 break;
6532
6533 case LYXP_AXIS_DESCENDANT_OR_SELF:
6534 if ((*iter == scnode) && (*iter_type == node_type)) {
6535 /* fake first descendant, we returned self before */
6536 *iter = NULL;
6537 *iter_type = 0;
6538 return moveto_axis_scnode_next_first(iter, iter_type, iter_mod, iter_mod_idx, scnode, node_type,
6539 LYXP_AXIS_DESCENDANT, set, getnext_opts);
6540 } /* else DFS until context node */
6541 dfs_stop = scnode;
6542
6543 /* fallthrough */
6544 case LYXP_AXIS_DESCENDANT:
6545 if (axis == LYXP_AXIS_DESCENDANT) {
6546 /* DFS until the context node */
6547 dfs_stop = scnode;
6548 }
6549
6550 /* fallthrough */
6551 case LYXP_AXIS_PRECEDING:
6552 if (axis == LYXP_AXIS_PRECEDING) {
6553 /* DFS until the previous sibling */
6554 dfs_stop = moveto_axis_scnode_preceding_sibling(scnode, getnext_opts);
6555 assert(dfs_stop);
6556
6557 if (*iter == dfs_stop) {
6558 /* we are done */
6559 break;
6560 }
6561 }
6562
6563 /* fallthrough */
6564 case LYXP_AXIS_FOLLOWING:
6565 if (axis == LYXP_AXIS_FOLLOWING) {
6566 /* DFS through the whole module */
6567 dfs_stop = NULL;
6568 }
6569
6570 /* nested nodes */
6571 assert(*iter);
6572 next = moveto_axis_scnode_next_dfs_forward(*iter, dfs_stop, getnext_opts);
6573 if (next) {
6574 next_type = LYXP_NODE_ELEM;
6575 break;
6576 } /* else get next top-level node just like a child */
6577
6578 /* fallthrough */
6579 case LYXP_AXIS_CHILD:
6580 case LYXP_AXIS_FOLLOWING_SIBLING:
6581 if (!*iter_mod) {
6582 /* nodes from a single module */
6583 if ((next = lys_getnext(*iter, lysc_data_parent(*iter), (*iter)->module->compiled, getnext_opts))) {
6584 next_type = LYXP_NODE_ELEM;
6585 break;
6586 }
6587
6588 assert(scnode);
Michal Vaskoa353cce2022-11-14 10:09:55 +01006589 if ((axis != LYXP_AXIS_CHILD) && !lysc_data_parent(scnode)) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02006590 /* iterating over top-level nodes, find next */
6591 while (lysc_data_parent(*iter)) {
6592 *iter = lysc_data_parent(*iter);
6593 }
6594 if ((next = lys_getnext(*iter, NULL, (*iter)->module->compiled, getnext_opts))) {
6595 next_type = LYXP_NODE_ELEM;
6596 break;
6597 }
6598 }
6599 }
6600
6601 while (*iter_mod) {
6602 /* module top-level nodes */
6603 if ((next = lys_getnext(*iter, NULL, (*iter_mod)->compiled, getnext_opts))) {
6604 next_type = LYXP_NODE_ELEM;
6605 break;
6606 }
6607
6608 /* get next module */
6609 while ((*iter_mod = ly_ctx_get_module_iter(set->ctx, iter_mod_idx))) {
6610 /* module may not be implemented or not compiled yet */
6611 if ((*iter_mod)->compiled) {
6612 break;
6613 }
6614 }
6615
6616 /* new module, start over */
6617 *iter = NULL;
6618 }
6619 break;
6620
6621 case LYXP_AXIS_PRECEDING_SIBLING:
6622 assert(*iter);
6623
6624 /* next parent sibling until scnode */
6625 next = lys_getnext(*iter, lysc_data_parent(*iter), (*iter)->module->compiled, getnext_opts);
6626 if (next == scnode) {
6627 /* no previous sibling */
6628 next = NULL;
6629 }
6630 next_type = next ? LYXP_NODE_ELEM : 0;
6631 break;
6632
6633 case LYXP_AXIS_ATTRIBUTE:
6634 /* unreachable */
6635 assert(0);
6636 LOGINT(set->ctx);
6637 break;
6638 }
6639
6640 *iter = next;
6641 *iter_type = next_type;
6642 return next_type ? LY_SUCCESS : LY_ENOTFOUND;
6643}
6644
6645/**
Michal Vaskod3678892020-05-21 10:06:58 +02006646 * @brief Move context @p set to a schema node. Result is LYXP_SET_SCNODE_SET (or LYXP_SET_EMPTY).
6647 *
6648 * @param[in,out] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006649 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02006650 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko49fec8e2022-05-24 10:28:33 +02006651 * @param[in] axis Axis to search on.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006652 * @param[in] options XPath options.
6653 * @return LY_ERR
6654 */
6655static LY_ERR
Michal Vasko49fec8e2022-05-24 10:28:33 +02006656moveto_scnode(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, enum lyxp_axis axis,
6657 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006658{
Radek Krejci857189e2020-09-01 13:26:36 +02006659 ly_bool temp_ctx = 0;
Michal Vasko49fec8e2022-05-24 10:28:33 +02006660 uint32_t getnext_opts, orig_used, i, mod_idx, idx;
stewegd8e2fc92023-05-31 09:52:56 +02006661 const struct lys_module *mod = NULL;
Michal Vasko49fec8e2022-05-24 10:28:33 +02006662 const struct lysc_node *iter;
6663 enum lyxp_node_type iter_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006664
aPiecek8b0cc152021-05-31 16:40:31 +02006665 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006666 return LY_SUCCESS;
6667 }
6668
6669 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006670 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006671 return LY_EVALID;
6672 }
6673
Michal Vaskocafad9d2019-11-07 15:20:03 +01006674 /* getnext opts */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01006675 getnext_opts = 0;
Michal Vaskocafad9d2019-11-07 15:20:03 +01006676 if (options & LYXP_SCNODE_OUTPUT) {
6677 getnext_opts |= LYS_GETNEXT_OUTPUT;
6678 }
Michal Vaskoe482bb92023-02-01 11:41:41 +01006679 if (options & LYXP_SCNODE_SCHEMAMOUNT) {
6680 getnext_opts |= LYS_GETNEXT_WITHSCHEMAMOUNT;
6681 }
Michal Vaskocafad9d2019-11-07 15:20:03 +01006682
Michal Vasko03ff5a72019-09-11 13:49:33 +02006683 orig_used = set->used;
6684 for (i = 0; i < orig_used; ++i) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02006685 /* update in_ctx first */
6686 if (moveto_axis_scnode_next_in_ctx(&set->val.scnodes[i].in_ctx, axis)) {
6687 /* not usable, skip */
6688 continue;
6689 }
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006690
Michal Vasko49fec8e2022-05-24 10:28:33 +02006691 iter = NULL;
6692 iter_type = 0;
6693 while (!moveto_axis_scnode_next(&iter, &iter_type, &mod, &mod_idx, set->val.scnodes[i].scnode,
6694 set->val.scnodes[i].type, axis, set, getnext_opts)) {
6695 if (moveto_scnode_check(iter, NULL, set, ncname, moveto_mod)) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006696 continue;
6697 }
6698
Michal Vasko49fec8e2022-05-24 10:28:33 +02006699 /* insert */
Michal Vaskoe4a6d012023-05-22 14:34:52 +02006700 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, iter_type, axis, &idx));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006701
Michal Vasko49fec8e2022-05-24 10:28:33 +02006702 /* we need to prevent these nodes from being considered in this moveto */
6703 if ((idx < orig_used) && (idx > i)) {
6704 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX;
6705 temp_ctx = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006706 }
6707 }
Michal Vasko66330fc2022-11-21 15:52:24 +01006708
6709 if (moveto_mod && ncname && ((axis == LYXP_AXIS_DESCENDANT) || (axis == LYXP_AXIS_CHILD)) &&
6710 (set->val.scnodes[i].type == LYXP_NODE_ELEM) && !ly_nested_ext_schema(NULL, set->val.scnodes[i].scnode,
6711 moveto_mod->name, strlen(moveto_mod->name), LY_VALUE_JSON, NULL, ncname, strlen(ncname), &iter, NULL)) {
6712 /* there is a matching node from an extension, use it */
Michal Vaskoe4a6d012023-05-22 14:34:52 +02006713 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, axis, &idx));
Michal Vasko66330fc2022-11-21 15:52:24 +01006714 if ((idx < orig_used) && (idx > i)) {
6715 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX;
6716 temp_ctx = 1;
6717 }
6718 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006719 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006720
6721 /* correct temporary in_ctx values */
6722 if (temp_ctx) {
6723 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006724 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_NEW_CTX) {
6725 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006726 }
6727 }
6728 }
6729
6730 return LY_SUCCESS;
6731}
6732
6733/**
Michal Vasko49fec8e2022-05-24 10:28:33 +02006734 * @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 +02006735 * Context position aware.
6736 *
6737 * @param[in] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006738 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02006739 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vaskocdad7122020-11-09 21:04:44 +01006740 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006741 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6742 */
6743static LY_ERR
Michal Vasko49fec8e2022-05-24 10:28:33 +02006744moveto_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 +02006745{
6746 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006747 const struct lyd_node *next, *elem, *start;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006748 struct lyxp_set ret_set;
6749 LY_ERR rc;
6750
aPiecek8b0cc152021-05-31 16:40:31 +02006751 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006752 return LY_SUCCESS;
6753 }
6754
6755 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006756 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006757 return LY_EVALID;
6758 }
6759
Michal Vasko9f96a052020-03-10 09:41:45 +01006760 /* 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 +02006761 rc = xpath_pi_node(set, LYXP_AXIS_CHILD, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006762 LY_CHECK_RET(rc);
6763
Michal Vasko6346ece2019-09-24 13:12:53 +02006764 /* this loop traverses all the nodes in the set and adds/keeps only those that match qname */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006765 set_init(&ret_set, set);
6766 for (i = 0; i < set->used; ++i) {
6767
6768 /* TREE DFS */
6769 start = set->val.nodes[i].node;
6770 for (elem = next = start; elem; elem = next) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02006771 rc = moveto_node_check(elem, LYXP_NODE_ELEM, set, ncname, moveto_mod, options);
Michal Vasko6346ece2019-09-24 13:12:53 +02006772 if (!rc) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006773 /* add matching node into result set */
6774 set_insert_node(&ret_set, elem, 0, LYXP_NODE_ELEM, ret_set.used);
6775 if (set_dup_node_check(set, elem, LYXP_NODE_ELEM, i)) {
6776 /* the node is a duplicate, we'll process it later in the set */
6777 goto skip_children;
6778 }
Michal Vasko6346ece2019-09-24 13:12:53 +02006779 } else if (rc == LY_EINCOMPLETE) {
Michal Vasko6346ece2019-09-24 13:12:53 +02006780 return rc;
6781 } else if (rc == LY_EINVAL) {
6782 goto skip_children;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006783 }
6784
6785 /* TREE DFS NEXT ELEM */
6786 /* select element for the next run - children first */
Radek Krejcia1c1e542020-09-29 16:06:52 +02006787 next = lyd_child(elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006788 if (!next) {
6789skip_children:
6790 /* no children, so try siblings, but only if it's not the start,
6791 * that is considered to be the root and it's siblings are not traversed */
6792 if (elem != start) {
6793 next = elem->next;
6794 } else {
6795 break;
6796 }
6797 }
6798 while (!next) {
6799 /* no siblings, go back through the parents */
Michal Vasko9e685082021-01-29 14:49:09 +01006800 if (lyd_parent(elem) == start) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006801 /* we are done, no next element to process */
6802 break;
6803 }
6804 /* parent is already processed, go to its sibling */
Michal Vasko9e685082021-01-29 14:49:09 +01006805 elem = lyd_parent(elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006806 next = elem->next;
6807 }
6808 }
6809 }
6810
6811 /* make the temporary set the current one */
6812 ret_set.ctx_pos = set->ctx_pos;
6813 ret_set.ctx_size = set->ctx_size;
Michal Vaskod3678892020-05-21 10:06:58 +02006814 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006815 memcpy(set, &ret_set, sizeof *set);
Michal Vasko306e2832022-07-25 09:15:17 +02006816 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006817
6818 return LY_SUCCESS;
6819}
6820
6821/**
Michal Vasko49fec8e2022-05-24 10:28:33 +02006822 * @brief Move context @p set to a child schema node and all its descendants starting from a node.
6823 * Result is LYXP_SET_NODE_SET.
6824 *
6825 * @param[in] set Set to use.
6826 * @param[in] start Start node whose subtree to add.
6827 * @param[in] start_idx Index of @p start in @p set.
6828 * @param[in] moveto_mod Matching node module, NULL for no prefix.
6829 * @param[in] ncname Matching node name in the dictionary, NULL for any.
6830 * @param[in] options XPath options.
6831 * @return LY_ERR value.
6832 */
6833static LY_ERR
6834moveto_scnode_dfs(struct lyxp_set *set, const struct lysc_node *start, uint32_t start_idx,
6835 const struct lys_module *moveto_mod, const char *ncname, uint32_t options)
6836{
6837 const struct lysc_node *next, *elem;
6838 uint32_t idx;
6839 LY_ERR rc;
6840
6841 /* TREE DFS */
6842 for (elem = next = start; elem; elem = next) {
6843 if ((elem == start) || (elem->nodetype & (LYS_CHOICE | LYS_CASE))) {
6844 /* schema-only nodes, skip root */
6845 goto next_iter;
6846 }
6847
6848 rc = moveto_scnode_check(elem, start, set, ncname, moveto_mod);
6849 if (!rc) {
6850 if (lyxp_set_scnode_contains(set, elem, LYXP_NODE_ELEM, start_idx, &idx)) {
6851 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
6852 if (idx > start_idx) {
6853 /* we will process it later in the set */
6854 goto skip_children;
6855 }
6856 } else {
Michal Vaskoe4a6d012023-05-22 14:34:52 +02006857 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, elem, LYXP_NODE_ELEM, LYXP_AXIS_DESCENDANT, NULL));
Michal Vasko49fec8e2022-05-24 10:28:33 +02006858 }
6859 } else if (rc == LY_EINVAL) {
6860 goto skip_children;
6861 }
6862
6863next_iter:
6864 /* TREE DFS NEXT ELEM */
6865 /* select element for the next run - children first */
6866 next = lysc_node_child(elem);
6867 if (next && (next->nodetype == LYS_INPUT) && (options & LYXP_SCNODE_OUTPUT)) {
6868 next = next->next;
6869 } else if (next && (next->nodetype == LYS_OUTPUT) && !(options & LYXP_SCNODE_OUTPUT)) {
6870 next = next->next;
6871 }
6872 if (!next) {
6873skip_children:
6874 /* no children, so try siblings, but only if it's not the start,
6875 * that is considered to be the root and it's siblings are not traversed */
6876 if (elem != start) {
6877 next = elem->next;
6878 } else {
6879 break;
6880 }
6881 }
6882 while (!next) {
6883 /* no siblings, go back through the parents */
6884 if (elem->parent == start) {
6885 /* we are done, no next element to process */
6886 break;
6887 }
6888 /* parent is already processed, go to its sibling */
6889 elem = elem->parent;
6890 next = elem->next;
6891 }
6892 }
6893
6894 return LY_SUCCESS;
6895}
6896
6897/**
6898 * @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 +02006899 *
6900 * @param[in] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006901 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02006902 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006903 * @param[in] options XPath options.
Michal Vasko49fec8e2022-05-24 10:28:33 +02006904 * @return LY_ERR value.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006905 */
6906static LY_ERR
Michal Vasko49fec8e2022-05-24 10:28:33 +02006907moveto_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 +02006908{
Michal Vasko49fec8e2022-05-24 10:28:33 +02006909 uint32_t i, orig_used, mod_idx;
6910 const struct lys_module *mod;
6911 const struct lysc_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006912
aPiecek8b0cc152021-05-31 16:40:31 +02006913 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006914 return LY_SUCCESS;
6915 }
6916
6917 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006918 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006919 return LY_EVALID;
6920 }
6921
Michal Vasko03ff5a72019-09-11 13:49:33 +02006922 orig_used = set->used;
6923 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006924 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
6925 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006926 continue;
6927 }
6928
6929 /* remember context node */
Radek Krejcif13b87b2020-12-01 22:02:17 +01006930 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vaskoec4df482019-12-16 10:02:18 +01006931 } else {
Michal Vasko1a09b212021-05-06 13:00:10 +02006932 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006933 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006934
Michal Vasko49fec8e2022-05-24 10:28:33 +02006935 if ((set->val.scnodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.scnodes[i].type == LYXP_NODE_ROOT)) {
6936 /* traverse all top-level nodes in all the modules */
6937 mod_idx = 0;
6938 while ((mod = ly_ctx_get_module_iter(set->ctx, &mod_idx))) {
6939 /* module may not be implemented or not compiled yet */
6940 if (!mod->compiled) {
6941 continue;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006942 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006943
Michal Vasko49fec8e2022-05-24 10:28:33 +02006944 root = NULL;
6945 /* no getnext opts needed */
6946 while ((root = lys_getnext(root, NULL, mod->compiled, 0))) {
6947 LY_CHECK_RET(moveto_scnode_dfs(set, root, i, moveto_mod, ncname, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006948 }
6949 }
Michal Vasko49fec8e2022-05-24 10:28:33 +02006950
6951 } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
6952 /* add all the descendants recursively */
6953 LY_CHECK_RET(moveto_scnode_dfs(set, set->val.scnodes[i].scnode, i, moveto_mod, ncname, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006954 }
6955 }
6956
6957 return LY_SUCCESS;
6958}
6959
6960/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02006961 * @brief Move context @p set to an attribute. Result is LYXP_SET_NODE_SET.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006962 * Indirectly context position aware.
6963 *
6964 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02006965 * @param[in] mod Matching metadata module, NULL for any.
6966 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
aPiecek8b0cc152021-05-31 16:40:31 +02006967 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006968 * @return LY_ERR
6969 */
6970static LY_ERR
aPiecek8b0cc152021-05-31 16:40:31 +02006971moveto_attr(struct lyxp_set *set, const struct lys_module *mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006972{
Michal Vasko9f96a052020-03-10 09:41:45 +01006973 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006974
aPiecek8b0cc152021-05-31 16:40:31 +02006975 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006976 return LY_SUCCESS;
6977 }
6978
6979 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006980 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006981 return LY_EVALID;
6982 }
6983
Radek Krejci1deb5be2020-08-26 16:43:36 +02006984 for (uint32_t i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02006985 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006986
6987 /* only attributes of an elem (not dummy) can be in the result, skip all the rest;
6988 * our attributes are always qualified */
Michal Vasko5c4e5892019-11-14 12:31:38 +01006989 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01006990 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006991
6992 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02006993 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006994 continue;
6995 }
6996
Michal Vaskod3678892020-05-21 10:06:58 +02006997 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006998 /* match */
6999 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01007000 set->val.meta[i].meta = sub;
7001 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007002 /* pos does not change */
7003 replaced = 1;
7004 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01007005 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 +02007006 }
7007 ++i;
7008 }
7009 }
7010 }
7011
7012 if (!replaced) {
7013 /* no match */
7014 set_remove_node(set, i);
7015 }
7016 }
7017
7018 return LY_SUCCESS;
7019}
7020
7021/**
7022 * @brief Move context @p set1 to union with @p set2. @p set2 is emptied afterwards.
Michal Vasko61ac2f62020-05-25 12:39:51 +02007023 * Result is LYXP_SET_NODE_SET. Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007024 *
7025 * @param[in,out] set1 Set to use for the result.
7026 * @param[in] set2 Set that is copied to @p set1.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007027 * @return LY_ERR
7028 */
7029static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007030moveto_union(struct lyxp_set *set1, struct lyxp_set *set2)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007031{
7032 LY_ERR rc;
7033
Michal Vaskod3678892020-05-21 10:06:58 +02007034 if ((set1->type != LYXP_SET_NODE_SET) || (set2->type != LYXP_SET_NODE_SET)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01007035 LOGVAL(set1->ctx, LY_VCODE_XP_INOP_2, "union", print_set_type(set1), print_set_type(set2));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007036 return LY_EVALID;
7037 }
7038
7039 /* set2 is empty or both set1 and set2 */
Michal Vaskod3678892020-05-21 10:06:58 +02007040 if (!set2->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007041 return LY_SUCCESS;
7042 }
7043
Michal Vaskod3678892020-05-21 10:06:58 +02007044 if (!set1->used) {
aPiecekadc1e4f2021-10-07 11:15:12 +02007045 /* release hidden allocated data (lyxp_set.size) */
7046 lyxp_set_free_content(set1);
7047 /* direct copying of the entire structure */
Michal Vasko03ff5a72019-09-11 13:49:33 +02007048 memcpy(set1, set2, sizeof *set1);
7049 /* dynamic memory belongs to set1 now, do not free */
Michal Vaskod3678892020-05-21 10:06:58 +02007050 memset(set2, 0, sizeof *set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007051 return LY_SUCCESS;
7052 }
7053
7054 /* we assume sets are sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007055 assert(!set_sort(set1) && !set_sort(set2));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007056
7057 /* sort, remove duplicates */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007058 rc = set_sorted_merge(set1, set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007059 LY_CHECK_RET(rc);
7060
7061 /* final set must be sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007062 assert(!set_sort(set1));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007063
7064 return LY_SUCCESS;
7065}
7066
7067/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02007068 * @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 +02007069 * Context position aware.
7070 *
7071 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02007072 * @param[in] mod Matching metadata module, NULL for any.
7073 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
Michal Vaskocdad7122020-11-09 21:04:44 +01007074 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007075 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7076 */
7077static int
Michal Vaskocdad7122020-11-09 21:04:44 +01007078moveto_attr_alldesc(struct lyxp_set *set, const struct lys_module *mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007079{
Michal Vasko9f96a052020-03-10 09:41:45 +01007080 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007081 struct lyxp_set *set_all_desc = NULL;
7082 LY_ERR rc;
7083
aPiecek8b0cc152021-05-31 16:40:31 +02007084 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007085 return LY_SUCCESS;
7086 }
7087
7088 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01007089 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007090 return LY_EVALID;
7091 }
7092
Michal Vasko03ff5a72019-09-11 13:49:33 +02007093 /* can be optimized similarly to moveto_node_alldesc() and save considerable amount of memory,
7094 * but it likely won't be used much, so it's a waste of time */
7095 /* copy the context */
7096 set_all_desc = set_copy(set);
7097 /* get all descendant nodes (the original context nodes are removed) */
Michal Vasko49fec8e2022-05-24 10:28:33 +02007098 rc = moveto_node_alldesc_child(set_all_desc, NULL, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007099 if (rc != LY_SUCCESS) {
7100 lyxp_set_free(set_all_desc);
7101 return rc;
7102 }
7103 /* prepend the original context nodes */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007104 rc = moveto_union(set, set_all_desc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007105 if (rc != LY_SUCCESS) {
7106 lyxp_set_free(set_all_desc);
7107 return rc;
7108 }
7109 lyxp_set_free(set_all_desc);
7110
Radek Krejci1deb5be2020-08-26 16:43:36 +02007111 for (uint32_t i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02007112 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007113
7114 /* only attributes of an elem can be in the result, skip all the rest,
7115 * we have all attributes qualified in lyd tree */
7116 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01007117 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007118 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02007119 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007120 continue;
7121 }
7122
Michal Vaskod3678892020-05-21 10:06:58 +02007123 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007124 /* match */
7125 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01007126 set->val.meta[i].meta = sub;
7127 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007128 /* pos does not change */
7129 replaced = 1;
7130 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01007131 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 +02007132 }
7133 ++i;
7134 }
7135 }
7136 }
7137
7138 if (!replaced) {
7139 /* no match */
7140 set_remove_node(set, i);
7141 }
7142 }
7143
7144 return LY_SUCCESS;
7145}
7146
7147/**
Michal Vasko8abcecc2022-07-28 09:55:01 +02007148 * @brief Move context @p set1 single item to the result of a comparison.
7149 *
7150 * @param[in] set1 First set with the item to compare.
7151 * @param[in] idx1 Index of the item in @p set1.
7152 * @param[in] set2 Second set.
7153 * @param[in] op Comparison operator to process.
7154 * @param[in] switch_operands Whether to switch sets as operands; whether it is `set1 op set2` or `set2 op set1`.
7155 * @param[out] result Result of the comparison.
7156 * @return LY_ERR value.
7157 */
7158static LY_ERR
7159moveto_op_comp_item(const struct lyxp_set *set1, uint32_t idx1, struct lyxp_set *set2, const char *op,
7160 ly_bool switch_operands, ly_bool *result)
7161{
7162 struct lyxp_set tmp1 = {0};
7163 LY_ERR rc = LY_SUCCESS;
7164
7165 assert(set1->type == LYXP_SET_NODE_SET);
7166
7167 /* cast set1 */
7168 switch (set2->type) {
7169 case LYXP_SET_NUMBER:
7170 rc = set_comp_cast(&tmp1, set1, LYXP_SET_NUMBER, idx1);
7171 break;
7172 case LYXP_SET_BOOLEAN:
7173 rc = set_comp_cast(&tmp1, set1, LYXP_SET_BOOLEAN, idx1);
7174 break;
7175 default:
7176 rc = set_comp_cast(&tmp1, set1, LYXP_SET_STRING, idx1);
7177 break;
7178 }
7179 LY_CHECK_GOTO(rc, cleanup);
7180
7181 /* canonize set2 */
7182 LY_CHECK_GOTO(rc = set_comp_canonize(set2, &set1->val.nodes[idx1]), cleanup);
7183
7184 /* compare recursively and store the result */
7185 if (switch_operands) {
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007186 LY_CHECK_GOTO(rc = moveto_op_comp(set2, &tmp1, op, result), cleanup);
Michal Vasko8abcecc2022-07-28 09:55:01 +02007187 } else {
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007188 LY_CHECK_GOTO(rc = moveto_op_comp(&tmp1, set2, op, result), cleanup);
Michal Vasko8abcecc2022-07-28 09:55:01 +02007189 }
7190
7191cleanup:
7192 lyxp_set_free_content(&tmp1);
7193 return rc;
7194}
7195
7196/**
7197 * @brief Move context @p set1 to the result of a comparison. Handles '=', '!=', '<=', '<', '>=', or '>'.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007198 * Result is LYXP_SET_BOOLEAN. Indirectly context position aware.
7199 *
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007200 * @param[in] set1 Set acting as the first operand for @p op.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007201 * @param[in] set2 Set acting as the second operand for @p op.
7202 * @param[in] op Comparison operator to process.
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007203 * @param[out] result Result of the comparison.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007204 * @return LY_ERR
7205 */
7206static LY_ERR
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007207moveto_op_comp(struct lyxp_set *set1, struct lyxp_set *set2, const char *op, ly_bool *result)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007208{
7209 /*
7210 * NODE SET + NODE SET = NODE SET + STRING /(1 NODE SET) 2 STRING
7211 * NODE SET + STRING = STRING + STRING /1 STRING (2 STRING)
7212 * NODE SET + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
7213 * NODE SET + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
7214 * STRING + NODE SET = STRING + STRING /(1 STRING) 2 STRING
7215 * NUMBER + NODE SET = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
7216 * BOOLEAN + NODE SET = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
7217 *
7218 * '=' or '!='
7219 * BOOLEAN + BOOLEAN
7220 * BOOLEAN + STRING = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
7221 * BOOLEAN + NUMBER = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
7222 * STRING + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
7223 * NUMBER + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
7224 * NUMBER + NUMBER
7225 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
7226 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
7227 * STRING + STRING
7228 *
7229 * '<=', '<', '>=', '>'
7230 * NUMBER + NUMBER
7231 * BOOLEAN + BOOLEAN = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
7232 * BOOLEAN + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
7233 * BOOLEAN + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
7234 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
7235 * STRING + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
7236 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
7237 * NUMBER + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
7238 * STRING + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
7239 */
Michal Vasko8abcecc2022-07-28 09:55:01 +02007240 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007241 LY_ERR rc;
7242
Michal Vasko03ff5a72019-09-11 13:49:33 +02007243 /* iterative evaluation with node-sets */
7244 if ((set1->type == LYXP_SET_NODE_SET) || (set2->type == LYXP_SET_NODE_SET)) {
7245 if (set1->type == LYXP_SET_NODE_SET) {
7246 for (i = 0; i < set1->used; ++i) {
Michal Vasko8abcecc2022-07-28 09:55:01 +02007247 /* evaluate for the single item */
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007248 LY_CHECK_RET(moveto_op_comp_item(set1, i, set2, op, 0, result));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007249
7250 /* lazy evaluation until true */
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007251 if (*result) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007252 return LY_SUCCESS;
7253 }
7254 }
7255 } else {
7256 for (i = 0; i < set2->used; ++i) {
Michal Vasko8abcecc2022-07-28 09:55:01 +02007257 /* evaluate for the single item */
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007258 LY_CHECK_RET(moveto_op_comp_item(set2, i, set1, op, 1, result));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007259
7260 /* lazy evaluation until true */
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007261 if (*result) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007262 return LY_SUCCESS;
7263 }
7264 }
7265 }
7266
Michal Vasko8abcecc2022-07-28 09:55:01 +02007267 /* false for all the nodes */
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007268 *result = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007269 return LY_SUCCESS;
7270 }
7271
7272 /* first convert properly */
7273 if ((op[0] == '=') || (op[0] == '!')) {
7274 if ((set1->type == LYXP_SET_BOOLEAN) || (set2->type == LYXP_SET_BOOLEAN)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007275 lyxp_set_cast(set1, LYXP_SET_BOOLEAN);
7276 lyxp_set_cast(set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007277 } else if ((set1->type == LYXP_SET_NUMBER) || (set2->type == LYXP_SET_NUMBER)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007278 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007279 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007280 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007281 LY_CHECK_RET(rc);
7282 } /* else we have 2 strings */
7283 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007284 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007285 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007286 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007287 LY_CHECK_RET(rc);
7288 }
7289
7290 assert(set1->type == set2->type);
7291
7292 /* compute result */
7293 if (op[0] == '=') {
7294 if (set1->type == LYXP_SET_BOOLEAN) {
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007295 *result = (set1->val.bln == set2->val.bln);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007296 } else if (set1->type == LYXP_SET_NUMBER) {
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007297 *result = (set1->val.num == set2->val.num);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007298 } else {
7299 assert(set1->type == LYXP_SET_STRING);
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007300 *result = strcmp(set1->val.str, set2->val.str) ? 0 : 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007301 }
7302 } else if (op[0] == '!') {
7303 if (set1->type == LYXP_SET_BOOLEAN) {
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007304 *result = (set1->val.bln != set2->val.bln);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007305 } else if (set1->type == LYXP_SET_NUMBER) {
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007306 *result = (set1->val.num != set2->val.num);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007307 } else {
7308 assert(set1->type == LYXP_SET_STRING);
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007309 *result = strcmp(set1->val.str, set2->val.str) ? 1 : 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007310 }
7311 } else {
7312 assert(set1->type == LYXP_SET_NUMBER);
7313 if (op[0] == '<') {
7314 if (op[1] == '=') {
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007315 *result = (set1->val.num <= set2->val.num);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007316 } else {
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007317 *result = (set1->val.num < set2->val.num);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007318 }
7319 } else {
7320 if (op[1] == '=') {
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007321 *result = (set1->val.num >= set2->val.num);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007322 } else {
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007323 *result = (set1->val.num > set2->val.num);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007324 }
7325 }
7326 }
7327
Michal Vasko03ff5a72019-09-11 13:49:33 +02007328 return LY_SUCCESS;
7329}
7330
7331/**
7332 * @brief Move context @p set to the result of a basic operation. Handles '+', '-', unary '-', '*', 'div',
7333 * or 'mod'. Result is LYXP_SET_NUMBER. Indirectly context position aware.
7334 *
7335 * @param[in,out] set1 Set to use for the result.
7336 * @param[in] set2 Set acting as the second operand for @p op.
7337 * @param[in] op Operator to process.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007338 * @return LY_ERR
7339 */
7340static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007341moveto_op_math(struct lyxp_set *set1, struct lyxp_set *set2, const char *op)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007342{
7343 LY_ERR rc;
7344
7345 /* unary '-' */
7346 if (!set2 && (op[0] == '-')) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007347 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007348 LY_CHECK_RET(rc);
7349 set1->val.num *= -1;
7350 lyxp_set_free(set2);
7351 return LY_SUCCESS;
7352 }
7353
7354 assert(set1 && set2);
7355
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007356 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007357 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007358 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007359 LY_CHECK_RET(rc);
7360
7361 switch (op[0]) {
7362 /* '+' */
7363 case '+':
7364 set1->val.num += set2->val.num;
7365 break;
7366
7367 /* '-' */
7368 case '-':
7369 set1->val.num -= set2->val.num;
7370 break;
7371
7372 /* '*' */
7373 case '*':
7374 set1->val.num *= set2->val.num;
7375 break;
7376
7377 /* 'div' */
7378 case 'd':
7379 set1->val.num /= set2->val.num;
7380 break;
7381
7382 /* 'mod' */
7383 case 'm':
7384 set1->val.num = ((long long)set1->val.num) % ((long long)set2->val.num);
7385 break;
7386
7387 default:
7388 LOGINT_RET(set1->ctx);
7389 }
7390
7391 return LY_SUCCESS;
7392}
7393
Michal Vasko03ff5a72019-09-11 13:49:33 +02007394/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02007395 * @brief Evaluate Predicate. Logs directly on error.
7396 *
Michal Vaskod3678892020-05-21 10:06:58 +02007397 * [9] Predicate ::= '[' Expr ']'
Michal Vasko03ff5a72019-09-11 13:49:33 +02007398 *
7399 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007400 * @param[in] tok_idx Position in the expression @p exp.
aPiecek8b0cc152021-05-31 16:40:31 +02007401 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007402 * @param[in] options XPath options.
Michal Vasko49fec8e2022-05-24 10:28:33 +02007403 * @param[in] axis Axis to search on.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007404 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7405 */
7406static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02007407eval_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 +02007408{
7409 LY_ERR rc;
Michal Vaskodd528af2022-08-08 14:35:07 +02007410 uint32_t i, orig_exp, orig_pos, orig_size;
Michal Vasko5c4e5892019-11-14 12:31:38 +01007411 int32_t pred_in_ctx;
Michal Vasko88a9e802022-05-24 10:50:28 +02007412 ly_bool reverse_axis = 0;
aPiecekfff4dca2021-10-07 10:59:53 +02007413 struct lyxp_set set2 = {0};
Michal Vasko03ff5a72019-09-11 13:49:33 +02007414
7415 /* '[' */
aPiecek8b0cc152021-05-31 16:40:31 +02007416 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02007417 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007418 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007419
aPiecek8b0cc152021-05-31 16:40:31 +02007420 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007421only_parse:
aPiecek8b0cc152021-05-31 16:40:31 +02007422 rc = eval_expr_select(exp, tok_idx, 0, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007423 LY_CHECK_RET(rc);
7424 } else if (set->type == LYXP_SET_NODE_SET) {
7425 /* 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 +01007426 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007427
7428 /* empty set, nothing to evaluate */
7429 if (!set->used) {
7430 goto only_parse;
7431 }
7432
Michal Vasko49fec8e2022-05-24 10:28:33 +02007433 /* decide forward or reverse axis */
7434 switch (axis) {
7435 case LYXP_AXIS_ANCESTOR:
7436 case LYXP_AXIS_ANCESTOR_OR_SELF:
7437 case LYXP_AXIS_PRECEDING:
7438 case LYXP_AXIS_PRECEDING_SIBLING:
7439 reverse_axis = 1;
7440 break;
7441 case LYXP_AXIS_DESCENDANT:
7442 case LYXP_AXIS_DESCENDANT_OR_SELF:
7443 case LYXP_AXIS_FOLLOWING:
7444 case LYXP_AXIS_FOLLOWING_SIBLING:
7445 case LYXP_AXIS_PARENT:
7446 case LYXP_AXIS_CHILD:
7447 case LYXP_AXIS_SELF:
7448 case LYXP_AXIS_ATTRIBUTE:
7449 reverse_axis = 0;
7450 break;
7451 }
7452
Michal Vasko004d3152020-06-11 19:59:22 +02007453 orig_exp = *tok_idx;
Michal Vasko49fec8e2022-05-24 10:28:33 +02007454 orig_pos = reverse_axis ? set->used + 1 : 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007455 orig_size = set->used;
Michal Vasko39dbf352020-05-21 10:08:59 +02007456 for (i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007457 set_init(&set2, set);
7458 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 +02007459
7460 /* remember the node context position for position() and context size for last() */
7461 orig_pos += reverse_axis ? -1 : 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007462
7463 set2.ctx_pos = orig_pos;
7464 set2.ctx_size = orig_size;
Michal Vasko004d3152020-06-11 19:59:22 +02007465 *tok_idx = orig_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007466
Michal Vasko004d3152020-06-11 19:59:22 +02007467 rc = eval_expr_select(exp, tok_idx, 0, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007468 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02007469 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007470 return rc;
7471 }
7472
Michal Vasko49fec8e2022-05-24 10:28:33 +02007473 /* number is a proximity position */
Michal Vasko03ff5a72019-09-11 13:49:33 +02007474 if (set2.type == LYXP_SET_NUMBER) {
7475 if ((long long)set2.val.num == orig_pos) {
7476 set2.val.num = 1;
7477 } else {
7478 set2.val.num = 0;
7479 }
7480 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007481 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007482
7483 /* predicate satisfied or not? */
Michal Vasko004d3152020-06-11 19:59:22 +02007484 if (!set2.val.bln) {
Michal Vasko2caefc12019-11-14 16:07:56 +01007485 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007486 }
7487 }
Michal Vasko2caefc12019-11-14 16:07:56 +01007488 set_remove_nodes_none(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007489
7490 } else if (set->type == LYXP_SET_SCNODE_SET) {
7491 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01007492 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007493 /* there is a currently-valid node */
7494 break;
7495 }
7496 }
7497 /* empty set, nothing to evaluate */
7498 if (i == set->used) {
7499 goto only_parse;
7500 }
7501
Michal Vasko004d3152020-06-11 19:59:22 +02007502 orig_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007503
Michal Vasko03ff5a72019-09-11 13:49:33 +02007504 /* set special in_ctx to all the valid snodes */
7505 pred_in_ctx = set_scnode_new_in_ctx(set);
7506
7507 /* use the valid snodes one-by-one */
7508 for (i = 0; i < set->used; ++i) {
7509 if (set->val.scnodes[i].in_ctx != pred_in_ctx) {
7510 continue;
7511 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01007512 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007513
Michal Vasko004d3152020-06-11 19:59:22 +02007514 *tok_idx = orig_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007515
Michal Vasko004d3152020-06-11 19:59:22 +02007516 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007517 LY_CHECK_RET(rc);
7518
7519 set->val.scnodes[i].in_ctx = pred_in_ctx;
7520 }
7521
7522 /* restore the state as it was before the predicate */
7523 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01007524 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007525 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007526 } else if (set->val.scnodes[i].in_ctx == pred_in_ctx) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01007527 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007528 }
7529 }
7530
7531 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02007532 set2.type = LYXP_SET_NODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007533 set_fill_set(&set2, set);
7534
Michal Vasko004d3152020-06-11 19:59:22 +02007535 rc = eval_expr_select(exp, tok_idx, 0, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007536 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02007537 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007538 return rc;
7539 }
7540
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007541 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +02007542 if (!set2.val.bln) {
Michal Vaskod3678892020-05-21 10:06:58 +02007543 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007544 }
Michal Vaskod3678892020-05-21 10:06:58 +02007545 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007546 }
7547
7548 /* ']' */
Michal Vasko004d3152020-06-11 19:59:22 +02007549 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_BRACK2);
aPiecek8b0cc152021-05-31 16:40:31 +02007550 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02007551 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007552 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007553
7554 return LY_SUCCESS;
7555}
7556
7557/**
Michal Vaskod3678892020-05-21 10:06:58 +02007558 * @brief Evaluate Literal. Logs directly on error.
7559 *
7560 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007561 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02007562 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7563 */
7564static void
Michal Vaskodd528af2022-08-08 14:35:07 +02007565eval_literal(const struct lyxp_expr *exp, uint32_t *tok_idx, struct lyxp_set *set)
Michal Vaskod3678892020-05-21 10:06:58 +02007566{
7567 if (set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007568 if (exp->tok_len[*tok_idx] == 2) {
Michal Vaskod3678892020-05-21 10:06:58 +02007569 set_fill_string(set, "", 0);
7570 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007571 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 +02007572 }
7573 }
7574 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02007575 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007576 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007577}
7578
7579/**
Michal Vasko3d969ff2022-07-29 15:02:08 +02007580 * @brief Check that a nametest in a predicate matches a key node.
7581 *
7582 * @param[in] nametest Nametest to check.
7583 * @param[in] len Length of @p nametest.
7584 * @param[in] ctx_scnode Found schema node as the context for the predicate.
7585 * @param[in] set Context set.
7586 * @param[in] key Expected key node.
7587 * @return LY_SUCCESS on success,
7588 * @return LY_ENOT if a predicate could not be compiled.
7589 * @return LY_ERR on any error.
7590 */
7591static LY_ERR
7592eval_name_test_try_compile_predicate_key(const char *nametest, uint32_t len, const struct lysc_node *ctx_scnode,
7593 const struct lyxp_set *set, const struct lysc_node *key)
7594{
7595 const struct lys_module *mod;
7596
7597 /* prefix (module) */
7598 LY_CHECK_RET(moveto_resolve_model(&nametest, &len, set, ctx_scnode, &mod));
7599 if (mod != key->module) {
7600 return LY_ENOT;
7601 }
7602
7603 /* node name */
7604 if (ly_strncmp(key->name, nametest, len)) {
7605 return LY_ENOT;
7606 }
7607
7608 return LY_SUCCESS;
7609}
7610
7611/**
7612 * @brief Append a simple predicate for the node.
7613 *
7614 * @param[in] exp Full parsed XPath expression.
7615 * @param[in] tok_idx Predicate start index in @p exp.
7616 * @param[in] end_tok_idx Predicate end index in @p exp.
7617 * @param[in] ctx_scnode Found schema node as the context for the predicate.
7618 * @param[in] set Context set.
7619 * @param[in] pred_node Node with the value referenced in the predicate.
7620 * @param[in,out] pred Predicate to append to.
7621 * @param[in,out] pred_len Length of @p pred, is updated.
7622 * @return LY_SUCCESS on success,
7623 * @return LY_ENOT if a predicate could not be compiled.
7624 * @return LY_ERR on any error.
7625 */
7626static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02007627eval_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 +02007628 const struct lysc_node *ctx_scnode, const struct lyxp_set *set, const struct lysc_node *pred_node, char **pred,
7629 uint32_t *pred_len)
7630{
7631 LY_ERR rc = LY_SUCCESS;
7632 uint32_t i;
7633 const struct lyd_node *siblings;
7634 struct lyd_node *ctx_node;
Michal Vasko5fb92a22022-08-02 09:21:37 +02007635 const struct lysc_node *sparent, *cur_scnode;
Michal Vasko3d969ff2022-07-29 15:02:08 +02007636 struct lyxp_expr *val_exp = NULL;
7637 struct lyxp_set set2 = {0};
7638 char quot;
7639
7640 /* duplicate the value expression */
7641 LY_CHECK_GOTO(rc = lyxp_expr_dup(set->ctx, exp, tok_idx, end_tok_idx, &val_exp), cleanup);
7642
7643 /* get its atoms */
Michal Vasko5fb92a22022-08-02 09:21:37 +02007644 cur_scnode = set->cur_node ? set->cur_node->schema : NULL;
7645 LY_CHECK_GOTO(rc = lyxp_atomize(set->ctx, val_exp, set->cur_mod, set->format, set->prefix_data, cur_scnode,
7646 ctx_scnode, &set2, LYXP_SCNODE), cleanup);
Michal Vasko3d969ff2022-07-29 15:02:08 +02007647
7648 /* check whether we can compile a single predicate (evaluation result value is always the same) */
7649 for (i = 0; i < set2.used; ++i) {
7650 if ((set2.val.scnodes[i].type != LYXP_NODE_ELEM) || (set2.val.scnodes[i].in_ctx < LYXP_SET_SCNODE_ATOM_NODE)) {
7651 /* skip root and context node */
7652 continue;
7653 }
7654
Michal Vasko5fb92a22022-08-02 09:21:37 +02007655 /* 1) context node descendants are traversed - do best-effort detection of the value dependency on the
7656 * context node instance */
7657 if ((set2.val.scnodes[i].axis == LYXP_AXIS_CHILD) && (set2.val.scnodes[i].scnode->parent == ctx_scnode)) {
7658 /* 1.1) context node child was accessed on the child axis, certain dependency */
Michal Vasko3d969ff2022-07-29 15:02:08 +02007659 rc = LY_ENOT;
7660 goto cleanup;
7661 }
Michal Vasko5fb92a22022-08-02 09:21:37 +02007662 if ((set2.val.scnodes[i].axis == LYXP_AXIS_DESCENDANT) || (set2.val.scnodes[i].axis == LYXP_AXIS_DESCENDANT_OR_SELF)) {
7663 for (sparent = set2.val.scnodes[i].scnode->parent; sparent && (sparent != ctx_scnode); sparent = sparent->parent) {}
7664 if (sparent) {
7665 /* 1.2) context node descendant was accessed on the descendant axis, probable dependency */
7666 rc = LY_ENOT;
7667 goto cleanup;
7668 }
7669 }
Michal Vasko3d969ff2022-07-29 15:02:08 +02007670
Michal Vasko5fb92a22022-08-02 09:21:37 +02007671 /* 2) multi-instance nodes (list or leaf-list) are traversed - all the instances need to be considered,
7672 * but the current node can be safely ignored, it is always the same data instance */
7673 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 +02007674 rc = LY_ENOT;
7675 goto cleanup;
7676 }
Michal Vasko3d969ff2022-07-29 15:02:08 +02007677 }
7678
7679 /* get any data instance of the context node, we checked it makes no difference */
7680 siblings = set->val.nodes[0].node ? lyd_child(set->val.nodes[0].node) : set->tree;
7681 LY_CHECK_GOTO(rc = lyd_find_sibling_schema(siblings, ctx_scnode, &ctx_node), cleanup);
7682
7683 /* evaluate the value subexpression with the root context node */
7684 lyxp_set_free_content(&set2);
7685 LY_CHECK_GOTO(rc = lyxp_eval(set->ctx, val_exp, set->cur_mod, set->format, set->prefix_data, set->cur_node,
7686 ctx_node, set->tree, NULL, &set2, 0), cleanup);
7687
7688 /* cast it into a string */
7689 LY_CHECK_GOTO(rc = lyxp_set_cast(&set2, LYXP_SET_STRING), cleanup);
7690
7691 /* append the JSON predicate */
7692 *pred = ly_realloc(*pred, *pred_len + 1 + strlen(pred_node->name) + 2 + strlen(set2.val.str) + 3);
7693 LY_CHECK_ERR_GOTO(!*pred, LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
7694 quot = strchr(set2.val.str, '\'') ? '\"' : '\'';
7695 *pred_len += sprintf(*pred + *pred_len, "[%s=%c%s%c]", pred_node->name, quot, set2.val.str, quot);
7696
7697cleanup:
7698 lyxp_expr_free(set->ctx, val_exp);
7699 lyxp_set_free_content(&set2);
7700 return rc;
7701}
7702
7703/**
Michal Vasko004d3152020-06-11 19:59:22 +02007704 * @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 +02007705 *
Michal Vasko004d3152020-06-11 19:59:22 +02007706 * @param[in] exp Full parsed XPath expression.
7707 * @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 +02007708 * @param[in] ctx_scnode Found schema node as the context for the predicate.
7709 * @param[in] set Context set.
Michal Vasko004d3152020-06-11 19:59:22 +02007710 * @param[out] predicates Parsed predicates.
Michal Vasko004d3152020-06-11 19:59:22 +02007711 * @return LY_SUCCESS on success,
Michal Vasko3d969ff2022-07-29 15:02:08 +02007712 * @return LY_ENOT if a predicate could not be compiled.
Michal Vasko004d3152020-06-11 19:59:22 +02007713 * @return LY_ERR on any error.
Michal Vaskod3678892020-05-21 10:06:58 +02007714 */
7715static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02007716eval_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 +01007717 const struct lyxp_set *set, struct ly_path_predicate **predicates)
Michal Vaskod3678892020-05-21 10:06:58 +02007718{
Michal Vasko3d969ff2022-07-29 15:02:08 +02007719 LY_ERR rc = LY_SUCCESS;
Michal Vaskod4a6d042022-12-08 08:34:29 +01007720 uint32_t e_idx, val_start_idx, pred_idx = 0, temp_lo = 0, pred_len = 0, nested_pred;
Michal Vaskod3678892020-05-21 10:06:58 +02007721 const struct lysc_node *key;
Michal Vasko3d969ff2022-07-29 15:02:08 +02007722 char *pred = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02007723 struct lyxp_expr *exp2 = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02007724
Michal Vasko3d969ff2022-07-29 15:02:08 +02007725 assert(ctx_scnode->nodetype & (LYS_LIST | LYS_LEAFLIST));
Michal Vaskod3678892020-05-21 10:06:58 +02007726
Michal Vasko3d969ff2022-07-29 15:02:08 +02007727 /* turn logging off */
Michal Vaskod4a6d042022-12-08 08:34:29 +01007728 ly_temp_log_options(&temp_lo);
Michal Vasko3d969ff2022-07-29 15:02:08 +02007729
7730 if (ctx_scnode->nodetype == LYS_LIST) {
7731 /* check for predicates "[key1=...][key2=...]..." */
7732
Michal Vasko004d3152020-06-11 19:59:22 +02007733 /* get key count */
Michal Vasko3d969ff2022-07-29 15:02:08 +02007734 if (ctx_scnode->flags & LYS_KEYLESS) {
7735 rc = LY_ENOT;
7736 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +02007737 }
Michal Vaskod3678892020-05-21 10:06:58 +02007738
Michal Vasko004d3152020-06-11 19:59:22 +02007739 /* learn where the predicates end */
7740 e_idx = *tok_idx;
Michal Vasko3d969ff2022-07-29 15:02:08 +02007741 for (key = lysc_node_child(ctx_scnode); key && (key->flags & LYS_KEY); key = key->next) {
Michal Vasko004d3152020-06-11 19:59:22 +02007742 /* '[' */
7743 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
Michal Vasko3d969ff2022-07-29 15:02:08 +02007744 rc = LY_ENOT;
7745 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +02007746 }
7747 ++e_idx;
7748
Michal Vasko3354d272021-04-06 09:40:06 +02007749 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_NAMETEST)) {
Michal Vasko3d969ff2022-07-29 15:02:08 +02007750 /* not a key */
7751 rc = LY_ENOT;
7752 goto cleanup;
Michal Vasko3354d272021-04-06 09:40:06 +02007753 }
7754
Michal Vasko3d969ff2022-07-29 15:02:08 +02007755 /* check key */
7756 LY_CHECK_GOTO(rc = eval_name_test_try_compile_predicate_key(exp->expr + exp->tok_pos[e_idx],
7757 exp->tok_len[e_idx], ctx_scnode, set, key), cleanup);
7758
7759 ++e_idx;
7760
7761 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_OPER_EQUAL)) {
7762 /* not '=' */
7763 rc = LY_ENOT;
7764 goto cleanup;
7765 }
7766 ++e_idx;
7767
7768 /* value start */
7769 val_start_idx = e_idx;
7770
Michal Vasko004d3152020-06-11 19:59:22 +02007771 /* ']' */
Michal Vasko3ef7e252022-11-16 08:29:49 +01007772 nested_pred = 1;
7773 do {
7774 ++e_idx;
7775
7776 if ((nested_pred == 1) && !lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_OPER_LOG)) {
Michal Vaskob8ee3562022-08-02 10:43:17 +02007777 /* higher priority than '=' */
7778 rc = LY_ENOT;
7779 goto cleanup;
Michal Vasko3ef7e252022-11-16 08:29:49 +01007780 } else if (!lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
7781 /* nested predicate */
7782 ++nested_pred;
7783 } else if (!lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) {
7784 /* predicate end */
7785 --nested_pred;
Michal Vaskob8ee3562022-08-02 10:43:17 +02007786 }
Michal Vasko3ef7e252022-11-16 08:29:49 +01007787 } while (nested_pred);
Michal Vasko004d3152020-06-11 19:59:22 +02007788
Michal Vasko3d969ff2022-07-29 15:02:08 +02007789 /* try to evaluate the value */
7790 LY_CHECK_GOTO(rc = eval_name_test_try_compile_predicate_append(exp, val_start_idx, e_idx - 1, ctx_scnode,
7791 set, key, &pred, &pred_len), cleanup);
7792
7793 ++e_idx;
Michal Vasko004d3152020-06-11 19:59:22 +02007794 }
Michal Vasko004d3152020-06-11 19:59:22 +02007795 } else {
Michal Vasko3d969ff2022-07-29 15:02:08 +02007796 /* check for predicate "[.=...]" */
7797
Michal Vasko004d3152020-06-11 19:59:22 +02007798 /* learn just where this single predicate ends */
7799 e_idx = *tok_idx;
7800
Michal Vaskod3678892020-05-21 10:06:58 +02007801 /* '[' */
Michal Vasko004d3152020-06-11 19:59:22 +02007802 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
Michal Vasko3d969ff2022-07-29 15:02:08 +02007803 rc = LY_ENOT;
7804 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +02007805 }
7806 ++e_idx;
Michal Vaskod3678892020-05-21 10:06:58 +02007807
Michal Vasko3354d272021-04-06 09:40:06 +02007808 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_DOT)) {
Michal Vasko3d969ff2022-07-29 15:02:08 +02007809 /* not the node value */
7810 rc = LY_ENOT;
7811 goto cleanup;
Michal Vasko3354d272021-04-06 09:40:06 +02007812 }
Michal Vasko3d969ff2022-07-29 15:02:08 +02007813 ++e_idx;
7814
7815 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_OPER_EQUAL)) {
7816 /* not '=' */
7817 rc = LY_ENOT;
7818 goto cleanup;
7819 }
7820 ++e_idx;
7821
7822 /* value start */
7823 val_start_idx = e_idx;
Michal Vasko3354d272021-04-06 09:40:06 +02007824
Michal Vaskod3678892020-05-21 10:06:58 +02007825 /* ']' */
Michal Vasko31f19632022-11-16 09:38:40 +01007826 nested_pred = 1;
7827 do {
7828 ++e_idx;
7829
7830 if ((nested_pred == 1) && !lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_OPER_LOG)) {
Michal Vaskob8ee3562022-08-02 10:43:17 +02007831 /* higher priority than '=' */
7832 rc = LY_ENOT;
7833 goto cleanup;
Michal Vasko31f19632022-11-16 09:38:40 +01007834 } else if (!lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
7835 /* nested predicate */
7836 ++nested_pred;
7837 } else if (!lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) {
7838 /* predicate end */
7839 --nested_pred;
Michal Vaskob8ee3562022-08-02 10:43:17 +02007840 }
Michal Vasko31f19632022-11-16 09:38:40 +01007841 } while (nested_pred);
Michal Vasko3d969ff2022-07-29 15:02:08 +02007842
7843 /* try to evaluate the value */
7844 LY_CHECK_GOTO(rc = eval_name_test_try_compile_predicate_append(exp, val_start_idx, e_idx - 1, ctx_scnode, set,
7845 ctx_scnode, &pred, &pred_len), cleanup);
7846
Michal Vasko004d3152020-06-11 19:59:22 +02007847 ++e_idx;
Michal Vaskod3678892020-05-21 10:06:58 +02007848 }
7849
Michal Vasko004d3152020-06-11 19:59:22 +02007850 /* parse the predicate(s) */
Michal Vasko3d969ff2022-07-29 15:02:08 +02007851 LY_CHECK_GOTO(rc = ly_path_parse_predicate(set->ctx, ctx_scnode, pred, pred_len, LY_PATH_PREFIX_OPTIONAL,
7852 LY_PATH_PRED_SIMPLE, &exp2), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02007853
7854 /* compile */
Michal Vasko3d969ff2022-07-29 15:02:08 +02007855 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 +01007856 &pred_idx, LY_VALUE_JSON, NULL, predicates);
Michal Vasko3d969ff2022-07-29 15:02:08 +02007857 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02007858
7859 /* success, the predicate must include all the needed information for hash-based search */
7860 *tok_idx = e_idx;
7861
7862cleanup:
Michal Vaskod4a6d042022-12-08 08:34:29 +01007863 ly_temp_log_options(NULL);
Michal Vasko3d969ff2022-07-29 15:02:08 +02007864 lyxp_expr_free(set->ctx, exp2);
7865 free(pred);
7866 return rc;
Michal Vaskod3678892020-05-21 10:06:58 +02007867}
7868
7869/**
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007870 * @brief Search for/check the next schema node that could be the only matching schema node meaning the
7871 * data node(s) could be found using a single hash-based search.
7872 *
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007873 * @param[in] ctx libyang context.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007874 * @param[in] node Next context node to check.
7875 * @param[in] name Expected node name.
7876 * @param[in] name_len Length of @p name.
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007877 * @param[in] moveto_mod Expected node module, can be NULL for JSON format with no prefix.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007878 * @param[in] root_type XPath root type.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007879 * @param[in] format Prefix format.
7880 * @param[in,out] found Previously found node, is updated.
7881 * @return LY_SUCCESS on success,
7882 * @return LY_ENOT if the whole check failed and hashes cannot be used.
7883 */
7884static LY_ERR
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007885eval_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 +02007886 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 +02007887 const struct lysc_node **found)
7888{
7889 const struct lysc_node *scnode;
7890 const struct lys_module *mod;
7891 uint32_t idx = 0;
7892
Radek Krejci8df109d2021-04-23 12:19:08 +02007893 assert((format == LY_VALUE_JSON) || moveto_mod);
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007894
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007895continue_search:
Michal Vasko7d1d0912020-10-16 08:38:30 +02007896 scnode = NULL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007897 if (!node) {
Radek Krejci8df109d2021-04-23 12:19:08 +02007898 if ((format == LY_VALUE_JSON) && !moveto_mod) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007899 /* search all modules for a single match */
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007900 while ((mod = ly_ctx_get_module_iter(ctx, &idx))) {
Michal Vasko35a3b1d2021-07-14 09:34:16 +02007901 if (!mod->implemented) {
7902 continue;
7903 }
7904
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007905 scnode = lys_find_child(NULL, mod, name, name_len, 0, 0);
7906 if (scnode) {
7907 /* we have found a match */
7908 break;
7909 }
7910 }
7911
Michal Vasko7d1d0912020-10-16 08:38:30 +02007912 if (!scnode) {
7913 /* all modules searched */
7914 idx = 0;
7915 }
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007916 } else {
7917 /* search in top-level */
7918 scnode = lys_find_child(NULL, moveto_mod, name, name_len, 0, 0);
7919 }
7920 } else if (!*found || (lysc_data_parent(*found) != node->schema)) {
Radek Krejci8df109d2021-04-23 12:19:08 +02007921 if ((format == LY_VALUE_JSON) && !moveto_mod) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007922 /* we must adjust the module to inherit the one from the context node */
7923 moveto_mod = node->schema->module;
7924 }
7925
7926 /* search in children, do not repeat the same search */
7927 scnode = lys_find_child(node->schema, moveto_mod, name, name_len, 0, 0);
Michal Vasko7d1d0912020-10-16 08:38:30 +02007928 } /* else skip redundant search */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007929
7930 /* additional context check */
7931 if (scnode && (root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) {
7932 scnode = NULL;
7933 }
7934
7935 if (scnode) {
7936 if (*found) {
7937 /* we found a schema node with the same name but at different level, give up, too complicated
7938 * (more hash-based searches would be required, not supported) */
7939 return LY_ENOT;
7940 } else {
7941 /* remember the found schema node and continue to make sure it can be used */
7942 *found = scnode;
7943 }
7944 }
7945
7946 if (idx) {
7947 /* continue searching all the following models */
7948 goto continue_search;
7949 }
7950
7951 return LY_SUCCESS;
7952}
7953
7954/**
Michal Vasko4ad69e72021-10-26 16:25:55 +02007955 * @brief Generate message when no matching schema nodes were found for a path segment.
7956 *
7957 * @param[in] set XPath set.
7958 * @param[in] scparent Previous schema parent in the context, if only one.
7959 * @param[in] ncname XPath NCName being evaluated.
7960 * @param[in] ncname_len Length of @p ncname.
7961 * @param[in] expr Whole XPath expression.
7962 * @param[in] options XPath options.
7963 */
7964static void
7965eval_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 +02007966 uint32_t ncname_len, const char *expr, uint32_t options)
Michal Vasko4ad69e72021-10-26 16:25:55 +02007967{
7968 const char *format;
7969 char *path = NULL, *ppath = NULL;
7970
7971 path = lysc_path(set->cur_scnode, LYSC_PATH_LOG, NULL, 0);
7972 if (scparent) {
7973 /* generate path for the parent */
7974 if (scparent->type == LYXP_NODE_ELEM) {
7975 ppath = lysc_path(scparent->scnode, LYSC_PATH_LOG, NULL, 0);
7976 } else if (scparent->type == LYXP_NODE_ROOT) {
7977 ppath = strdup("<root>");
7978 } else if (scparent->type == LYXP_NODE_ROOT_CONFIG) {
7979 ppath = strdup("<config-root>");
7980 }
7981 }
7982 if (ppath) {
7983 format = "Schema node \"%.*s\" for parent \"%s\" not found; in expr \"%.*s\" with context node \"%s\".";
7984 if (options & LYXP_SCNODE_ERROR) {
7985 LOGERR(set->ctx, LY_EVALID, format, ncname_len, ncname, ppath, (ncname - expr) + ncname_len, expr, path);
7986 } else {
7987 LOGWRN(set->ctx, format, ncname_len, ncname, ppath, (ncname - expr) + ncname_len, expr, path);
7988 }
7989 } else {
7990 format = "Schema node \"%.*s\" not found; in expr \"%.*s\" with context node \"%s\".";
7991 if (options & LYXP_SCNODE_ERROR) {
7992 LOGERR(set->ctx, LY_EVALID, format, ncname_len, ncname, (ncname - expr) + ncname_len, expr, path);
7993 } else {
7994 LOGWRN(set->ctx, format, ncname_len, ncname, (ncname - expr) + ncname_len, expr, path);
7995 }
7996 }
7997 free(path);
7998 free(ppath);
7999}
8000
8001/**
Michal Vaskod3678892020-05-21 10:06:58 +02008002 * @brief Evaluate NameTest and any following Predicates. Logs directly on error.
8003 *
8004 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
8005 * [6] NodeTest ::= NameTest | NodeType '(' ')'
8006 * [7] NameTest ::= '*' | NCName ':' '*' | QName
8007 *
8008 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008009 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko49fec8e2022-05-24 10:28:33 +02008010 * @param[in] axis What axis to search on.
Michal Vaskod3678892020-05-21 10:06:58 +02008011 * @param[in] all_desc Whether to search all the descendants or children only.
aPiecek8b0cc152021-05-31 16:40:31 +02008012 * @param[in,out] set Context and result set.
Michal Vaskod3678892020-05-21 10:06:58 +02008013 * @param[in] options XPath options.
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008014 * @return LY_ERR (LY_EINCOMPLETE on unresolved when, LY_ENOT for not found schema node)
Michal Vaskod3678892020-05-21 10:06:58 +02008015 */
8016static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02008017eval_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 +02008018 struct lyxp_set *set, uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02008019{
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008020 LY_ERR rc = LY_SUCCESS, r;
Michal Vasko004d3152020-06-11 19:59:22 +02008021 const char *ncname, *ncname_dict = NULL;
Michal Vasko56c008c2022-07-29 14:57:47 +02008022 uint32_t i, ncname_len;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008023 const struct lys_module *moveto_mod = NULL;
Michal Vaskoc71c37b2021-01-11 13:40:02 +01008024 const struct lysc_node *scnode = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02008025 struct ly_path_predicate *predicates = NULL;
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008026 int scnode_skip_pred = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02008027
aPiecek8b0cc152021-05-31 16:40:31 +02008028 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008029 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008030 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02008031
aPiecek8b0cc152021-05-31 16:40:31 +02008032 if (options & LYXP_SKIP_EXPR) {
Michal Vaskod3678892020-05-21 10:06:58 +02008033 goto moveto;
8034 }
8035
Michal Vasko004d3152020-06-11 19:59:22 +02008036 ncname = &exp->expr[exp->tok_pos[*tok_idx - 1]];
8037 ncname_len = exp->tok_len[*tok_idx - 1];
Michal Vaskod3678892020-05-21 10:06:58 +02008038
Michal Vaskoc71c37b2021-01-11 13:40:02 +01008039 if ((ncname[0] == '*') && (ncname_len == 1)) {
8040 /* all nodes will match */
8041 goto moveto;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008042 }
8043
Michal Vaskoc71c37b2021-01-11 13:40:02 +01008044 /* parse (and skip) module name */
8045 rc = moveto_resolve_model(&ncname, &ncname_len, set, NULL, &moveto_mod);
Michal Vaskod3678892020-05-21 10:06:58 +02008046 LY_CHECK_GOTO(rc, cleanup);
8047
Michal Vasko49fec8e2022-05-24 10:28:33 +02008048 if ((ncname[0] == '*') && (ncname_len == 1)) {
8049 /* all nodes from the module will match */
8050 goto moveto;
8051 }
8052
8053 if (((set->format == LY_VALUE_JSON) || moveto_mod) && (axis == LYXP_AXIS_CHILD) && !all_desc &&
8054 (set->type == LYXP_SET_NODE_SET)) {
Michal Vaskod3678892020-05-21 10:06:58 +02008055 /* find the matching schema node in some parent in the context */
Michal Vasko56c008c2022-07-29 14:57:47 +02008056 for (i = 0; i < set->used; ++i) {
Michal Vaskoc71c37b2021-01-11 13:40:02 +01008057 if (eval_name_test_with_predicate_get_scnode(set->ctx, set->val.nodes[i].node, ncname, ncname_len,
8058 moveto_mod, set->root_type, set->format, &scnode)) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008059 /* check failed */
8060 scnode = NULL;
8061 break;
Michal Vaskod3678892020-05-21 10:06:58 +02008062 }
8063 }
8064
Michal Vasko004d3152020-06-11 19:59:22 +02008065 if (scnode && (scnode->nodetype & (LYS_LIST | LYS_LEAFLIST))) {
8066 /* try to create the predicates */
Michal Vasko90189962023-02-28 12:10:34 +01008067 if (eval_name_test_try_compile_predicates(exp, tok_idx, scnode, set, &predicates)) {
Michal Vasko004d3152020-06-11 19:59:22 +02008068 /* hashes cannot be used */
Michal Vaskod3678892020-05-21 10:06:58 +02008069 scnode = NULL;
8070 }
8071 }
8072 }
8073
Michal Vaskoc71c37b2021-01-11 13:40:02 +01008074 if (!scnode) {
8075 /* we are not able to match based on a schema node and not all the modules match ("*"),
Michal Vasko004d3152020-06-11 19:59:22 +02008076 * use dictionary for efficient comparison */
Radek Krejci011e4aa2020-09-04 15:22:31 +02008077 LY_CHECK_GOTO(rc = lydict_insert(set->ctx, ncname, ncname_len, &ncname_dict), cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02008078 }
8079
8080moveto:
8081 /* move to the attribute(s), data node(s), or schema node(s) */
Michal Vasko49fec8e2022-05-24 10:28:33 +02008082 if (axis == LYXP_AXIS_ATTRIBUTE) {
aPiecek8b0cc152021-05-31 16:40:31 +02008083 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008084 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskod3678892020-05-21 10:06:58 +02008085 } else {
8086 if (all_desc) {
Michal Vaskocdad7122020-11-09 21:04:44 +01008087 rc = moveto_attr_alldesc(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02008088 } else {
aPiecek8b0cc152021-05-31 16:40:31 +02008089 rc = moveto_attr(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02008090 }
8091 LY_CHECK_GOTO(rc, cleanup);
8092 }
8093 } else {
aPiecek8b0cc152021-05-31 16:40:31 +02008094 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008095 const struct lyxp_set_scnode *scparent = NULL;
Michal Vasko56c008c2022-07-29 14:57:47 +02008096 ly_bool found = 0;
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008097
8098 /* remember parent if there is only one, to print in the warning */
8099 for (i = 0; i < set->used; ++i) {
8100 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
8101 if (!scparent) {
8102 /* remember the context node */
8103 scparent = &set->val.scnodes[i];
8104 } else {
8105 /* several context nodes, no reasonable error possible */
8106 scparent = NULL;
8107 break;
8108 }
8109 }
8110 }
Radek Krejci1deb5be2020-08-26 16:43:36 +02008111
Michal Vasko49fec8e2022-05-24 10:28:33 +02008112 if (all_desc && (axis == LYXP_AXIS_CHILD)) {
8113 /* efficient evaluation that does not add all the descendants into the set */
8114 rc = moveto_scnode_alldesc_child(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02008115 } else {
Michal Vasko49fec8e2022-05-24 10:28:33 +02008116 if (all_desc) {
8117 /* "//" == "/descendant-or-self::node()/" */
8118 rc = xpath_pi_node(set, LYXP_AXIS_DESCENDANT_OR_SELF, options);
8119 LY_CHECK_GOTO(rc, cleanup);
8120 }
8121 rc = moveto_scnode(set, moveto_mod, ncname_dict, axis, options);
Michal Vaskod3678892020-05-21 10:06:58 +02008122 }
8123 LY_CHECK_GOTO(rc, cleanup);
8124
Michal Vasko56c008c2022-07-29 14:57:47 +02008125 i = set->used;
8126 do {
8127 --i;
Michal Vasko1a09b212021-05-06 13:00:10 +02008128 if (set->val.scnodes[i].in_ctx > LYXP_SET_SCNODE_ATOM_NODE) {
Michal Vasko56c008c2022-07-29 14:57:47 +02008129 found = 1;
Michal Vaskod3678892020-05-21 10:06:58 +02008130 break;
8131 }
Michal Vasko56c008c2022-07-29 14:57:47 +02008132 } while (i);
8133 if (!found) {
Michal Vasko4ad69e72021-10-26 16:25:55 +02008134 /* generate message */
8135 eval_name_test_scnode_no_match_msg(set, scparent, ncname, ncname_len, exp->expr, options);
8136
8137 if (options & LYXP_SCNODE_ERROR) {
8138 /* error */
8139 rc = LY_EVALID;
8140 goto cleanup;
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008141 }
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008142
8143 /* skip the predicates and the rest of this path to not generate invalid warnings */
8144 rc = LY_ENOT;
8145 scnode_skip_pred = 1;
Michal Vaskod3678892020-05-21 10:06:58 +02008146 }
8147 } else {
Michal Vasko49fec8e2022-05-24 10:28:33 +02008148 if (all_desc && (axis == LYXP_AXIS_CHILD)) {
8149 /* efficient evaluation */
8150 rc = moveto_node_alldesc_child(set, moveto_mod, ncname_dict, options);
8151 } else if (scnode && (axis == LYXP_AXIS_CHILD)) {
8152 /* we can find the child nodes using hashes */
8153 rc = moveto_node_hash_child(set, scnode, predicates, options);
Michal Vaskod3678892020-05-21 10:06:58 +02008154 } else {
Michal Vasko49fec8e2022-05-24 10:28:33 +02008155 if (all_desc) {
8156 /* "//" == "/descendant-or-self::node()/" */
8157 rc = xpath_pi_node(set, LYXP_AXIS_DESCENDANT_OR_SELF, options);
8158 LY_CHECK_GOTO(rc, cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02008159 }
Michal Vasko49fec8e2022-05-24 10:28:33 +02008160 rc = moveto_node(set, moveto_mod, ncname_dict, axis, options);
Michal Vaskod3678892020-05-21 10:06:58 +02008161 }
8162 LY_CHECK_GOTO(rc, cleanup);
8163 }
8164 }
8165
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008166 if (scnode_skip_pred) {
8167 /* skip predicates */
8168 options |= LYXP_SKIP_EXPR;
8169 }
8170
Michal Vaskod3678892020-05-21 10:06:58 +02008171 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02008172 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02008173 r = eval_predicate(exp, tok_idx, set, options, axis);
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008174 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02008175 }
8176
8177cleanup:
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008178 if (scnode_skip_pred) {
8179 /* restore options */
8180 options &= ~LYXP_SKIP_EXPR;
8181 }
aPiecek8b0cc152021-05-31 16:40:31 +02008182 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko004d3152020-06-11 19:59:22 +02008183 lydict_remove(set->ctx, ncname_dict);
Michal Vasko90189962023-02-28 12:10:34 +01008184 ly_path_predicates_free(set->ctx, predicates);
Michal Vaskodb51a8d2020-05-27 15:22:29 +02008185 }
Michal Vaskod3678892020-05-21 10:06:58 +02008186 return rc;
8187}
8188
8189/**
8190 * @brief Evaluate NodeType and any following Predicates. Logs directly on error.
8191 *
8192 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
8193 * [6] NodeTest ::= NameTest | NodeType '(' ')'
8194 * [8] NodeType ::= 'text' | 'node'
8195 *
8196 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008197 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko49fec8e2022-05-24 10:28:33 +02008198 * @param[in] axis Axis to search on.
8199 * @param[in] all_desc Whether to search all the descendants or axis only.
aPiecek8b0cc152021-05-31 16:40:31 +02008200 * @param[in,out] set Context and result set.
Michal Vaskod3678892020-05-21 10:06:58 +02008201 * @param[in] options XPath options.
8202 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8203 */
8204static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02008205eval_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 +02008206 struct lyxp_set *set, uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02008207{
8208 LY_ERR rc;
8209
Michal Vaskod3678892020-05-21 10:06:58 +02008210 (void)all_desc;
8211
aPiecek8b0cc152021-05-31 16:40:31 +02008212 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko004d3152020-06-11 19:59:22 +02008213 assert(exp->tok_len[*tok_idx] == 4);
Michal Vasko49fec8e2022-05-24 10:28:33 +02008214 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "node", 4)) {
8215 rc = xpath_pi_node(set, axis, options);
Michal Vaskod3678892020-05-21 10:06:58 +02008216 } else {
Michal Vasko49fec8e2022-05-24 10:28:33 +02008217 assert(!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "text", 4));
8218 rc = xpath_pi_text(set, axis, options);
Michal Vaskod3678892020-05-21 10:06:58 +02008219 }
Michal Vasko49fec8e2022-05-24 10:28:33 +02008220 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02008221 }
aPiecek8b0cc152021-05-31 16:40:31 +02008222 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008223 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008224 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02008225
8226 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02008227 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
aPiecek8b0cc152021-05-31 16:40:31 +02008228 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008229 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008230 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02008231
8232 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02008233 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
aPiecek8b0cc152021-05-31 16:40:31 +02008234 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008235 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008236 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02008237
8238 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02008239 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02008240 rc = eval_predicate(exp, tok_idx, set, options, axis);
Michal Vaskod3678892020-05-21 10:06:58 +02008241 LY_CHECK_RET(rc);
8242 }
8243
8244 return LY_SUCCESS;
8245}
8246
8247/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02008248 * @brief Evaluate RelativeLocationPath. Logs directly on error.
8249 *
8250 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
8251 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
Michal Vaskod3678892020-05-21 10:06:58 +02008252 * [6] NodeTest ::= NameTest | NodeType '(' ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02008253 *
8254 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008255 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008256 * @param[in] all_desc Whether to search all the descendants or children only.
aPiecek8b0cc152021-05-31 16:40:31 +02008257 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008258 * @param[in] options XPath options.
8259 * @return LY_ERR (YL_EINCOMPLETE on unresolved when)
8260 */
8261static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02008262eval_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 +02008263 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008264{
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008265 LY_ERR rc = LY_SUCCESS;
Michal Vasko49fec8e2022-05-24 10:28:33 +02008266 enum lyxp_axis axis;
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008267 int scnode_skip_path = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008268
8269 goto step;
8270 do {
8271 /* evaluate '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02008272 if (exp->tok_len[*tok_idx] == 1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008273 all_desc = 0;
8274 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02008275 assert(exp->tok_len[*tok_idx] == 2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008276 all_desc = 1;
8277 }
aPiecek8b0cc152021-05-31 16:40:31 +02008278 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008279 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008280 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008281
8282step:
Michal Vasko49fec8e2022-05-24 10:28:33 +02008283 /* AxisSpecifier */
8284 if (exp->tokens[*tok_idx] == LYXP_TOKEN_AXISNAME) {
8285 axis = str2axis(exp->expr + exp->tok_pos[*tok_idx], exp->tok_len[*tok_idx]);
Michal Vaskod3678892020-05-21 10:06:58 +02008286
aPiecek8b0cc152021-05-31 16:40:31 +02008287 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008288 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
8289 ++(*tok_idx);
8290
8291 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_DCOLON);
8292 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
8293 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
8294 ++(*tok_idx);
8295 } else if (exp->tokens[*tok_idx] == LYXP_TOKEN_AT) {
8296 axis = LYXP_AXIS_ATTRIBUTE;
8297
8298 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
8299 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008300 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02008301 } else {
Michal Vasko49fec8e2022-05-24 10:28:33 +02008302 /* default */
8303 axis = LYXP_AXIS_CHILD;
Michal Vaskod3678892020-05-21 10:06:58 +02008304 }
8305
Michal Vasko49fec8e2022-05-24 10:28:33 +02008306 /* NodeTest Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02008307 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008308 case LYXP_TOKEN_DOT:
8309 /* evaluate '.' */
Michal Vasko49fec8e2022-05-24 10:28:33 +02008310 if (!(options & LYXP_SKIP_EXPR)) {
8311 if (((options & LYXP_SCNODE_ALL) && (set->type != LYXP_SET_SCNODE_SET)) ||
8312 (!(options & LYXP_SCNODE_ALL) && (set->type != LYXP_SET_NODE_SET))) {
8313 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
8314 rc = LY_EVALID;
8315 goto cleanup;
8316 }
8317
8318 if (all_desc) {
8319 rc = xpath_pi_node(set, LYXP_AXIS_DESCENDANT_OR_SELF, options);
8320 LY_CHECK_GOTO(rc, cleanup);
8321 }
8322 rc = xpath_pi_node(set, LYXP_AXIS_SELF, options);
8323 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008324 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008325 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008326 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008327 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008328 break;
8329
8330 case LYXP_TOKEN_DDOT:
8331 /* evaluate '..' */
Michal Vasko49fec8e2022-05-24 10:28:33 +02008332 if (!(options & LYXP_SKIP_EXPR)) {
8333 if (((options & LYXP_SCNODE_ALL) && (set->type != LYXP_SET_SCNODE_SET)) ||
8334 (!(options & LYXP_SCNODE_ALL) && (set->type != LYXP_SET_NODE_SET))) {
8335 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
8336 rc = LY_EVALID;
8337 goto cleanup;
8338 }
8339
8340 if (all_desc) {
8341 rc = xpath_pi_node(set, LYXP_AXIS_DESCENDANT_OR_SELF, options);
8342 LY_CHECK_GOTO(rc, cleanup);
8343 }
8344 rc = xpath_pi_node(set, LYXP_AXIS_PARENT, options);
8345 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008346 }
aPiecek8b0cc152021-05-31 16:40:31 +02008347 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008348 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008349 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008350 break;
8351
Michal Vasko03ff5a72019-09-11 13:49:33 +02008352 case LYXP_TOKEN_NAMETEST:
Michal Vaskod3678892020-05-21 10:06:58 +02008353 /* evaluate NameTest Predicate* */
Michal Vasko49fec8e2022-05-24 10:28:33 +02008354 rc = eval_name_test_with_predicate(exp, tok_idx, axis, all_desc, set, options);
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008355 if (rc == LY_ENOT) {
8356 assert(options & LYXP_SCNODE_ALL);
8357 /* skip the rest of this path */
8358 rc = LY_SUCCESS;
8359 scnode_skip_path = 1;
8360 options |= LYXP_SKIP_EXPR;
8361 }
8362 LY_CHECK_GOTO(rc, cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02008363 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008364
Michal Vaskod3678892020-05-21 10:06:58 +02008365 case LYXP_TOKEN_NODETYPE:
8366 /* evaluate NodeType Predicate* */
Michal Vasko49fec8e2022-05-24 10:28:33 +02008367 rc = eval_node_type_with_predicate(exp, tok_idx, axis, all_desc, set, options);
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008368 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008369 break;
8370
8371 default:
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008372 LOGINT(set->ctx);
8373 rc = LY_EINT;
8374 goto cleanup;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008375 }
Michal Vasko004d3152020-06-11 19:59:22 +02008376 } while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008377
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008378cleanup:
8379 if (scnode_skip_path) {
8380 options &= ~LYXP_SKIP_EXPR;
8381 }
8382 return rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008383}
8384
8385/**
8386 * @brief Evaluate AbsoluteLocationPath. Logs directly on error.
8387 *
8388 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
8389 *
8390 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008391 * @param[in] tok_idx Position in the expression @p exp.
aPiecek8b0cc152021-05-31 16:40:31 +02008392 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008393 * @param[in] options XPath options.
8394 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8395 */
8396static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02008397eval_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 +02008398{
Radek Krejci857189e2020-09-01 13:26:36 +02008399 ly_bool all_desc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008400
aPiecek8b0cc152021-05-31 16:40:31 +02008401 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008402 /* no matter what tokens follow, we need to be at the root */
Michal Vaskob0099a92020-08-31 14:55:23 +02008403 LY_CHECK_RET(moveto_root(set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008404 }
8405
8406 /* '/' RelativeLocationPath? */
Michal Vasko004d3152020-06-11 19:59:22 +02008407 if (exp->tok_len[*tok_idx] == 1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008408 /* evaluate '/' - deferred */
8409 all_desc = 0;
aPiecek8b0cc152021-05-31 16:40:31 +02008410 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008411 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008412 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008413
Michal Vasko004d3152020-06-11 19:59:22 +02008414 if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008415 return LY_SUCCESS;
8416 }
Michal Vasko004d3152020-06-11 19:59:22 +02008417 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008418 case LYXP_TOKEN_DOT:
8419 case LYXP_TOKEN_DDOT:
Michal Vasko49fec8e2022-05-24 10:28:33 +02008420 case LYXP_TOKEN_AXISNAME:
Michal Vasko03ff5a72019-09-11 13:49:33 +02008421 case LYXP_TOKEN_AT:
8422 case LYXP_TOKEN_NAMETEST:
8423 case LYXP_TOKEN_NODETYPE:
Michal Vaskob0099a92020-08-31 14:55:23 +02008424 LY_CHECK_RET(eval_relative_location_path(exp, tok_idx, all_desc, set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008425 break;
8426 default:
8427 break;
8428 }
8429
Michal Vasko03ff5a72019-09-11 13:49:33 +02008430 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02008431 /* '//' RelativeLocationPath */
Michal Vasko03ff5a72019-09-11 13:49:33 +02008432 /* evaluate '//' - deferred so as not to waste memory by remembering all the nodes */
8433 all_desc = 1;
aPiecek8b0cc152021-05-31 16:40:31 +02008434 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008435 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008436 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008437
Michal Vaskob0099a92020-08-31 14:55:23 +02008438 LY_CHECK_RET(eval_relative_location_path(exp, tok_idx, all_desc, set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008439 }
8440
8441 return LY_SUCCESS;
8442}
8443
8444/**
8445 * @brief Evaluate FunctionCall. Logs directly on error.
8446 *
Michal Vaskod3678892020-05-21 10:06:58 +02008447 * [11] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02008448 *
8449 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008450 * @param[in] tok_idx Position in the expression @p exp.
aPiecek8b0cc152021-05-31 16:40:31 +02008451 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008452 * @param[in] options XPath options.
8453 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8454 */
8455static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02008456eval_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 +02008457{
8458 LY_ERR rc;
Michal Vasko69730152020-10-09 16:30:07 +02008459
Michal Vaskodd528af2022-08-08 14:35:07 +02008460 LY_ERR (*xpath_func)(struct lyxp_set **, uint32_t, struct lyxp_set *, uint32_t) = NULL;
8461 uint32_t arg_count = 0, i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008462 struct lyxp_set **args = NULL, **args_aux;
8463
aPiecek8b0cc152021-05-31 16:40:31 +02008464 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008465 /* FunctionName */
Michal Vasko004d3152020-06-11 19:59:22 +02008466 switch (exp->tok_len[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008467 case 3:
Michal Vasko004d3152020-06-11 19:59:22 +02008468 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008469 xpath_func = &xpath_not;
Michal Vasko004d3152020-06-11 19:59:22 +02008470 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008471 xpath_func = &xpath_sum;
8472 }
8473 break;
8474 case 4:
Michal Vasko004d3152020-06-11 19:59:22 +02008475 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008476 xpath_func = &xpath_lang;
Michal Vasko004d3152020-06-11 19:59:22 +02008477 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008478 xpath_func = &xpath_last;
Michal Vasko004d3152020-06-11 19:59:22 +02008479 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008480 xpath_func = &xpath_name;
Michal Vasko004d3152020-06-11 19:59:22 +02008481 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008482 xpath_func = &xpath_true;
8483 }
8484 break;
8485 case 5:
Michal Vasko004d3152020-06-11 19:59:22 +02008486 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008487 xpath_func = &xpath_count;
Michal Vasko004d3152020-06-11 19:59:22 +02008488 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008489 xpath_func = &xpath_false;
Michal Vasko004d3152020-06-11 19:59:22 +02008490 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008491 xpath_func = &xpath_floor;
Michal Vasko004d3152020-06-11 19:59:22 +02008492 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008493 xpath_func = &xpath_round;
Michal Vasko004d3152020-06-11 19:59:22 +02008494 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008495 xpath_func = &xpath_deref;
8496 }
8497 break;
8498 case 6:
Michal Vasko004d3152020-06-11 19:59:22 +02008499 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008500 xpath_func = &xpath_concat;
Michal Vasko004d3152020-06-11 19:59:22 +02008501 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008502 xpath_func = &xpath_number;
Michal Vasko004d3152020-06-11 19:59:22 +02008503 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008504 xpath_func = &xpath_string;
8505 }
8506 break;
8507 case 7:
Michal Vasko004d3152020-06-11 19:59:22 +02008508 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008509 xpath_func = &xpath_boolean;
Michal Vasko004d3152020-06-11 19:59:22 +02008510 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008511 xpath_func = &xpath_ceiling;
Michal Vasko004d3152020-06-11 19:59:22 +02008512 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008513 xpath_func = &xpath_current;
8514 }
8515 break;
8516 case 8:
Michal Vasko004d3152020-06-11 19:59:22 +02008517 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008518 xpath_func = &xpath_contains;
Michal Vasko004d3152020-06-11 19:59:22 +02008519 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008520 xpath_func = &xpath_position;
Michal Vasko004d3152020-06-11 19:59:22 +02008521 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "re-match", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008522 xpath_func = &xpath_re_match;
8523 }
8524 break;
8525 case 9:
Michal Vasko004d3152020-06-11 19:59:22 +02008526 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008527 xpath_func = &xpath_substring;
Michal Vasko004d3152020-06-11 19:59:22 +02008528 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008529 xpath_func = &xpath_translate;
8530 }
8531 break;
8532 case 10:
Michal Vasko004d3152020-06-11 19:59:22 +02008533 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008534 xpath_func = &xpath_local_name;
Michal Vasko004d3152020-06-11 19:59:22 +02008535 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008536 xpath_func = &xpath_enum_value;
Michal Vasko004d3152020-06-11 19:59:22 +02008537 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008538 xpath_func = &xpath_bit_is_set;
8539 }
8540 break;
8541 case 11:
Michal Vasko004d3152020-06-11 19:59:22 +02008542 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008543 xpath_func = &xpath_starts_with;
8544 }
8545 break;
8546 case 12:
Michal Vasko004d3152020-06-11 19:59:22 +02008547 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008548 xpath_func = &xpath_derived_from;
8549 }
8550 break;
8551 case 13:
Michal Vasko004d3152020-06-11 19:59:22 +02008552 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008553 xpath_func = &xpath_namespace_uri;
Michal Vasko004d3152020-06-11 19:59:22 +02008554 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string-length", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008555 xpath_func = &xpath_string_length;
8556 }
8557 break;
8558 case 15:
Michal Vasko004d3152020-06-11 19:59:22 +02008559 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008560 xpath_func = &xpath_normalize_space;
Michal Vasko004d3152020-06-11 19:59:22 +02008561 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-after", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008562 xpath_func = &xpath_substring_after;
8563 }
8564 break;
8565 case 16:
Michal Vasko004d3152020-06-11 19:59:22 +02008566 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008567 xpath_func = &xpath_substring_before;
8568 }
8569 break;
8570 case 20:
Michal Vasko004d3152020-06-11 19:59:22 +02008571 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from-or-self", 20)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008572 xpath_func = &xpath_derived_from_or_self;
8573 }
8574 break;
8575 }
8576
8577 if (!xpath_func) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01008578 LOGVAL(set->ctx, LY_VCODE_XP_INFUNC, exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008579 return LY_EVALID;
8580 }
8581 }
8582
aPiecek8b0cc152021-05-31 16:40:31 +02008583 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008584 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008585 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008586
8587 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02008588 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
aPiecek8b0cc152021-05-31 16:40:31 +02008589 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008590 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008591 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008592
8593 /* ( Expr ( ',' Expr )* )? */
Michal Vasko004d3152020-06-11 19:59:22 +02008594 if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) {
aPiecek8b0cc152021-05-31 16:40:31 +02008595 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008596 args = malloc(sizeof *args);
8597 LY_CHECK_ERR_GOTO(!args, LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
8598 arg_count = 1;
8599 args[0] = set_copy(set);
8600 if (!args[0]) {
8601 rc = LY_EMEM;
8602 goto cleanup;
8603 }
8604
Michal Vasko004d3152020-06-11 19:59:22 +02008605 rc = eval_expr_select(exp, tok_idx, 0, args[0], options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008606 LY_CHECK_GOTO(rc, cleanup);
8607 } else {
aPiecek8b0cc152021-05-31 16:40:31 +02008608 rc = eval_expr_select(exp, tok_idx, 0, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008609 LY_CHECK_GOTO(rc, cleanup);
8610 }
8611 }
Michal Vasko004d3152020-06-11 19:59:22 +02008612 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) {
aPiecek8b0cc152021-05-31 16:40:31 +02008613 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008614 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008615 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008616
aPiecek8b0cc152021-05-31 16:40:31 +02008617 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008618 ++arg_count;
8619 args_aux = realloc(args, arg_count * sizeof *args);
8620 LY_CHECK_ERR_GOTO(!args_aux, arg_count--; LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
8621 args = args_aux;
8622 args[arg_count - 1] = set_copy(set);
8623 if (!args[arg_count - 1]) {
8624 rc = LY_EMEM;
8625 goto cleanup;
8626 }
8627
Michal Vasko004d3152020-06-11 19:59:22 +02008628 rc = eval_expr_select(exp, tok_idx, 0, args[arg_count - 1], options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008629 LY_CHECK_GOTO(rc, cleanup);
8630 } else {
aPiecek8b0cc152021-05-31 16:40:31 +02008631 rc = eval_expr_select(exp, tok_idx, 0, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008632 LY_CHECK_GOTO(rc, cleanup);
8633 }
8634 }
8635
8636 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02008637 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
aPiecek8b0cc152021-05-31 16:40:31 +02008638 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008639 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008640 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008641
aPiecek8b0cc152021-05-31 16:40:31 +02008642 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008643 /* evaluate function */
8644 rc = xpath_func(args, arg_count, set, options);
8645
8646 if (options & LYXP_SCNODE_ALL) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008647 /* merge all nodes from arg evaluations */
8648 for (i = 0; i < arg_count; ++i) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008649 set_scnode_clear_ctx(args[i], LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008650 lyxp_set_scnode_merge(set, args[i]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008651 }
8652 }
8653 } else {
8654 rc = LY_SUCCESS;
8655 }
8656
8657cleanup:
8658 for (i = 0; i < arg_count; ++i) {
8659 lyxp_set_free(args[i]);
8660 }
8661 free(args);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008662 return rc;
8663}
8664
8665/**
8666 * @brief Evaluate Number. Logs directly on error.
8667 *
8668 * @param[in] ctx Context for errors.
8669 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008670 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008671 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8672 * @return LY_ERR
8673 */
8674static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02008675eval_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 +02008676{
8677 long double num;
8678 char *endptr;
8679
8680 if (set) {
8681 errno = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02008682 num = strtold(&exp->expr[exp->tok_pos[*tok_idx]], &endptr);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008683 if (errno) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01008684 LOGVAL(ctx, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]);
8685 LOGVAL(ctx, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double (%s).",
Michal Vasko69730152020-10-09 16:30:07 +02008686 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]], strerror(errno));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008687 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02008688 } else if (endptr - &exp->expr[exp->tok_pos[*tok_idx]] != exp->tok_len[*tok_idx]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01008689 LOGVAL(ctx, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]);
8690 LOGVAL(ctx, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double.",
Michal Vasko69730152020-10-09 16:30:07 +02008691 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008692 return LY_EVALID;
8693 }
8694
8695 set_fill_number(set, num);
8696 }
8697
8698 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008699 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008700 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008701 return LY_SUCCESS;
8702}
8703
aPiecekdf23eee2021-10-07 12:21:50 +02008704LY_ERR
Michal Vasko90189962023-02-28 12:10:34 +01008705lyxp_vars_find(const struct ly_ctx *ctx, const struct lyxp_var *vars, const char *name, size_t name_len,
8706 struct lyxp_var **var)
aPiecekdf23eee2021-10-07 12:21:50 +02008707{
aPiecekdf23eee2021-10-07 12:21:50 +02008708 LY_ARRAY_COUNT_TYPE u;
8709
Michal Vasko90189962023-02-28 12:10:34 +01008710 assert(name);
aPiecekdf23eee2021-10-07 12:21:50 +02008711
Michal Vasko90189962023-02-28 12:10:34 +01008712 if (!name_len) {
8713 name_len = strlen(name);
8714 }
aPiecekdf23eee2021-10-07 12:21:50 +02008715
8716 LY_ARRAY_FOR(vars, u) {
8717 if (!strncmp(vars[u].name, name, name_len)) {
Michal Vasko90189962023-02-28 12:10:34 +01008718 if (var) {
8719 *var = (struct lyxp_var *)&vars[u];
8720 }
8721 return LY_SUCCESS;
aPiecekdf23eee2021-10-07 12:21:50 +02008722 }
8723 }
8724
Michal Vasko90189962023-02-28 12:10:34 +01008725 if (ctx) {
8726 LOGERR(ctx, LY_ENOTFOUND, "Variable \"%.*s\" not defined.", (int)name_len, name);
aPiecekdf23eee2021-10-07 12:21:50 +02008727 }
Michal Vasko90189962023-02-28 12:10:34 +01008728 return LY_ENOTFOUND;
aPiecekdf23eee2021-10-07 12:21:50 +02008729}
8730
Michal Vasko03ff5a72019-09-11 13:49:33 +02008731/**
aPiecekfba75362021-10-07 12:39:48 +02008732 * @brief Evaluate VariableReference.
8733 *
8734 * @param[in] exp Parsed XPath expression.
8735 * @param[in] tok_idx Position in the expression @p exp.
8736 * @param[in] vars [Sized array](@ref sizedarrays) of XPath variables.
8737 * @param[in,out] set Context and result set.
8738 * @param[in] options XPath options.
8739 * @return LY_ERR value.
8740 */
8741static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02008742eval_variable_reference(const struct lyxp_expr *exp, uint32_t *tok_idx, struct lyxp_set *set, uint32_t options)
aPiecekfba75362021-10-07 12:39:48 +02008743{
8744 LY_ERR ret;
8745 const char *name;
8746 struct lyxp_var *var;
aPiecekfba75362021-10-07 12:39:48 +02008747 struct lyxp_expr *tokens = NULL;
Michal Vaskodd528af2022-08-08 14:35:07 +02008748 uint32_t token_index, name_len;
aPiecekfba75362021-10-07 12:39:48 +02008749
Michal Vasko49fec8e2022-05-24 10:28:33 +02008750 /* find out the name and value of the variable */
aPiecekfba75362021-10-07 12:39:48 +02008751 name = &exp->expr[exp->tok_pos[*tok_idx]];
Michal Vaskoe68b5402022-07-29 14:58:15 +02008752 name_len = exp->tok_len[*tok_idx];
Michal Vasko90189962023-02-28 12:10:34 +01008753 ret = lyxp_vars_find(set->ctx, set->vars, name, name_len, &var);
8754 LY_CHECK_RET(ret);
aPiecekfba75362021-10-07 12:39:48 +02008755
Michal Vasko49fec8e2022-05-24 10:28:33 +02008756 /* parse value */
aPiecekfba75362021-10-07 12:39:48 +02008757 ret = lyxp_expr_parse(set->ctx, var->value, 0, 1, &tokens);
8758 LY_CHECK_GOTO(ret, cleanup);
8759
Michal Vasko49fec8e2022-05-24 10:28:33 +02008760 /* evaluate value */
aPiecekfba75362021-10-07 12:39:48 +02008761 token_index = 0;
8762 ret = eval_expr_select(tokens, &token_index, 0, set, options);
8763 LY_CHECK_GOTO(ret, cleanup);
8764
8765cleanup:
8766 lyxp_expr_free(set->ctx, tokens);
8767
8768 return ret;
8769}
8770
8771/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02008772 * @brief Evaluate PathExpr. Logs directly on error.
8773 *
Michal Vaskod3678892020-05-21 10:06:58 +02008774 * [12] PathExpr ::= LocationPath | PrimaryExpr Predicate*
Michal Vasko03ff5a72019-09-11 13:49:33 +02008775 * | PrimaryExpr Predicate* '/' RelativeLocationPath
8776 * | PrimaryExpr Predicate* '//' RelativeLocationPath
8777 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
aPiecekfba75362021-10-07 12:39:48 +02008778 * [10] PrimaryExpr ::= VariableReference | '(' Expr ')' | Literal | Number | FunctionCall
Michal Vasko03ff5a72019-09-11 13:49:33 +02008779 *
8780 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008781 * @param[in] tok_idx Position in the expression @p exp.
aPiecek8b0cc152021-05-31 16:40:31 +02008782 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008783 * @param[in] options XPath options.
8784 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8785 */
8786static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02008787eval_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 +02008788{
Michal Vasko49fec8e2022-05-24 10:28:33 +02008789 ly_bool all_desc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008790 LY_ERR rc;
8791
Michal Vasko004d3152020-06-11 19:59:22 +02008792 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008793 case LYXP_TOKEN_PAR1:
8794 /* '(' Expr ')' */
8795
8796 /* '(' */
aPiecek8b0cc152021-05-31 16:40:31 +02008797 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008798 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008799 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008800
8801 /* Expr */
Michal Vasko004d3152020-06-11 19:59:22 +02008802 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008803 LY_CHECK_RET(rc);
8804
8805 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02008806 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
aPiecek8b0cc152021-05-31 16:40:31 +02008807 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008808 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008809 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008810
Michal Vasko03ff5a72019-09-11 13:49:33 +02008811 goto predicate;
8812
8813 case LYXP_TOKEN_DOT:
8814 case LYXP_TOKEN_DDOT:
Michal Vasko49fec8e2022-05-24 10:28:33 +02008815 case LYXP_TOKEN_AXISNAME:
Michal Vasko03ff5a72019-09-11 13:49:33 +02008816 case LYXP_TOKEN_AT:
8817 case LYXP_TOKEN_NAMETEST:
8818 case LYXP_TOKEN_NODETYPE:
8819 /* RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02008820 rc = eval_relative_location_path(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008821 LY_CHECK_RET(rc);
8822 break;
8823
aPiecekfba75362021-10-07 12:39:48 +02008824 case LYXP_TOKEN_VARREF:
8825 /* VariableReference */
8826 rc = eval_variable_reference(exp, tok_idx, set, options);
8827 LY_CHECK_RET(rc);
8828 ++(*tok_idx);
8829
aPiecekfba75362021-10-07 12:39:48 +02008830 goto predicate;
8831
Michal Vasko03ff5a72019-09-11 13:49:33 +02008832 case LYXP_TOKEN_FUNCNAME:
8833 /* FunctionCall */
aPiecek8b0cc152021-05-31 16:40:31 +02008834 rc = eval_function_call(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008835 LY_CHECK_RET(rc);
8836
Michal Vasko03ff5a72019-09-11 13:49:33 +02008837 goto predicate;
8838
Michal Vasko3e48bf32020-06-01 08:39:07 +02008839 case LYXP_TOKEN_OPER_PATH:
8840 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +02008841 /* AbsoluteLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02008842 rc = eval_absolute_location_path(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008843 LY_CHECK_RET(rc);
8844 break;
8845
8846 case LYXP_TOKEN_LITERAL:
8847 /* Literal */
aPiecek8b0cc152021-05-31 16:40:31 +02008848 if ((options & LYXP_SKIP_EXPR) || (options & LYXP_SCNODE_ALL)) {
8849 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008850 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008851 }
Michal Vasko004d3152020-06-11 19:59:22 +02008852 eval_literal(exp, tok_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008853 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02008854 eval_literal(exp, tok_idx, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008855 }
8856
Michal Vasko03ff5a72019-09-11 13:49:33 +02008857 goto predicate;
8858
8859 case LYXP_TOKEN_NUMBER:
8860 /* Number */
aPiecek8b0cc152021-05-31 16:40:31 +02008861 if ((options & LYXP_SKIP_EXPR) || (options & LYXP_SCNODE_ALL)) {
8862 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008863 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008864 }
Michal Vasko004d3152020-06-11 19:59:22 +02008865 rc = eval_number(NULL, exp, tok_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008866 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02008867 rc = eval_number(set->ctx, exp, tok_idx, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008868 }
8869 LY_CHECK_RET(rc);
8870
Michal Vasko03ff5a72019-09-11 13:49:33 +02008871 goto predicate;
8872
8873 default:
Michal Vasko49fec8e2022-05-24 10:28:33 +02008874 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 +02008875 return LY_EVALID;
8876 }
8877
8878 return LY_SUCCESS;
8879
8880predicate:
8881 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02008882 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02008883 rc = eval_predicate(exp, tok_idx, set, options, LYXP_AXIS_CHILD);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008884 LY_CHECK_RET(rc);
8885 }
8886
8887 /* ('/' or '//') RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02008888 if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008889
8890 /* evaluate '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02008891 if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008892 all_desc = 0;
8893 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008894 all_desc = 1;
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
Michal Vasko004d3152020-06-11 19:59:22 +02008901 rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008902 LY_CHECK_RET(rc);
8903 }
8904
8905 return LY_SUCCESS;
8906}
8907
8908/**
8909 * @brief Evaluate UnionExpr. Logs directly on error.
8910 *
Michal Vaskod3678892020-05-21 10:06:58 +02008911 * [20] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008912 *
8913 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008914 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008915 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008916 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008917 * @param[in] options XPath options.
8918 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8919 */
8920static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02008921eval_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 +02008922{
8923 LY_ERR rc = LY_SUCCESS;
8924 struct lyxp_set orig_set, set2;
Michal Vaskodd528af2022-08-08 14:35:07 +02008925 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008926
8927 assert(repeat);
8928
8929 set_init(&orig_set, set);
8930 set_init(&set2, set);
8931
8932 set_fill_set(&orig_set, set);
8933
Michal Vasko004d3152020-06-11 19:59:22 +02008934 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008935 LY_CHECK_GOTO(rc, cleanup);
8936
8937 /* ('|' PathExpr)* */
8938 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008939 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_UNI);
aPiecek8b0cc152021-05-31 16:40:31 +02008940 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008941 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008942 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008943
aPiecek8b0cc152021-05-31 16:40:31 +02008944 if (options & LYXP_SKIP_EXPR) {
8945 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008946 LY_CHECK_GOTO(rc, cleanup);
8947 continue;
8948 }
8949
8950 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008951 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008952 LY_CHECK_GOTO(rc, cleanup);
8953
8954 /* eval */
8955 if (options & LYXP_SCNODE_ALL) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01008956 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008957 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008958 rc = moveto_union(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008959 LY_CHECK_GOTO(rc, cleanup);
8960 }
8961 }
8962
8963cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008964 lyxp_set_free_content(&orig_set);
8965 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008966 return rc;
8967}
8968
8969/**
8970 * @brief Evaluate UnaryExpr. Logs directly on error.
8971 *
Michal Vaskod3678892020-05-21 10:06:58 +02008972 * [19] UnaryExpr ::= UnionExpr | '-' UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008973 *
8974 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008975 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008976 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008977 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008978 * @param[in] options XPath options.
8979 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8980 */
8981static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02008982eval_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 +02008983{
8984 LY_ERR rc;
Michal Vaskodd528af2022-08-08 14:35:07 +02008985 uint32_t this_op, i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008986
8987 assert(repeat);
8988
8989 /* ('-')+ */
Michal Vasko004d3152020-06-11 19:59:22 +02008990 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008991 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008992 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 +02008993
aPiecek8b0cc152021-05-31 16:40:31 +02008994 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008995 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008996 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008997 }
8998
Michal Vasko004d3152020-06-11 19:59:22 +02008999 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNARY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009000 LY_CHECK_RET(rc);
9001
aPiecek8b0cc152021-05-31 16:40:31 +02009002 if (!(options & LYXP_SKIP_EXPR) && (repeat % 2)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02009003 if (options & LYXP_SCNODE_ALL) {
9004 warn_operands(set->ctx, set, NULL, 1, exp->expr, exp->tok_pos[this_op]);
9005 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009006 rc = moveto_op_math(set, NULL, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009007 LY_CHECK_RET(rc);
9008 }
9009 }
9010
9011 return LY_SUCCESS;
9012}
9013
9014/**
9015 * @brief Evaluate MultiplicativeExpr. Logs directly on error.
9016 *
Michal Vaskod3678892020-05-21 10:06:58 +02009017 * [18] MultiplicativeExpr ::= UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02009018 * | MultiplicativeExpr '*' UnaryExpr
9019 * | MultiplicativeExpr 'div' UnaryExpr
9020 * | MultiplicativeExpr 'mod' UnaryExpr
9021 *
9022 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02009023 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009024 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02009025 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009026 * @param[in] options XPath options.
9027 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
9028 */
9029static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02009030eval_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 +02009031 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02009032{
9033 LY_ERR rc;
Michal Vaskodd528af2022-08-08 14:35:07 +02009034 uint32_t i, this_op;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009035 struct lyxp_set orig_set, set2;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009036
9037 assert(repeat);
9038
9039 set_init(&orig_set, set);
9040 set_init(&set2, set);
9041
9042 set_fill_set(&orig_set, set);
9043
Michal Vasko004d3152020-06-11 19:59:22 +02009044 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009045 LY_CHECK_GOTO(rc, cleanup);
9046
9047 /* ('*' / 'div' / 'mod' UnaryExpr)* */
9048 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02009049 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009050
Michal Vasko004d3152020-06-11 19:59:22 +02009051 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH);
aPiecek8b0cc152021-05-31 16:40:31 +02009052 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02009053 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02009054 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009055
aPiecek8b0cc152021-05-31 16:40:31 +02009056 if (options & LYXP_SKIP_EXPR) {
9057 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009058 LY_CHECK_GOTO(rc, cleanup);
9059 continue;
9060 }
9061
9062 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02009063 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009064 LY_CHECK_GOTO(rc, cleanup);
9065
9066 /* eval */
9067 if (options & LYXP_SCNODE_ALL) {
9068 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01009069 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02009070 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009071 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009072 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009073 LY_CHECK_GOTO(rc, cleanup);
9074 }
9075 }
9076
9077cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02009078 lyxp_set_free_content(&orig_set);
9079 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009080 return rc;
9081}
9082
9083/**
9084 * @brief Evaluate AdditiveExpr. Logs directly on error.
9085 *
Michal Vaskod3678892020-05-21 10:06:58 +02009086 * [17] AdditiveExpr ::= MultiplicativeExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02009087 * | AdditiveExpr '+' MultiplicativeExpr
9088 * | AdditiveExpr '-' MultiplicativeExpr
9089 *
9090 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02009091 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009092 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02009093 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009094 * @param[in] options XPath options.
9095 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
9096 */
9097static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02009098eval_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 +02009099{
9100 LY_ERR rc;
Michal Vaskodd528af2022-08-08 14:35:07 +02009101 uint32_t i, this_op;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009102 struct lyxp_set orig_set, set2;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009103
9104 assert(repeat);
9105
9106 set_init(&orig_set, set);
9107 set_init(&set2, set);
9108
9109 set_fill_set(&orig_set, set);
9110
Michal Vasko004d3152020-06-11 19:59:22 +02009111 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009112 LY_CHECK_GOTO(rc, cleanup);
9113
9114 /* ('+' / '-' MultiplicativeExpr)* */
9115 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02009116 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009117
Michal Vasko004d3152020-06-11 19:59:22 +02009118 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009119 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02009120 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02009121 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009122
aPiecek8b0cc152021-05-31 16:40:31 +02009123 if (options & LYXP_SKIP_EXPR) {
9124 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009125 LY_CHECK_GOTO(rc, cleanup);
9126 continue;
9127 }
9128
9129 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02009130 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009131 LY_CHECK_GOTO(rc, cleanup);
9132
9133 /* eval */
9134 if (options & LYXP_SCNODE_ALL) {
9135 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01009136 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02009137 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009138 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009139 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009140 LY_CHECK_GOTO(rc, cleanup);
9141 }
9142 }
9143
9144cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02009145 lyxp_set_free_content(&orig_set);
9146 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009147 return rc;
9148}
9149
9150/**
9151 * @brief Evaluate RelationalExpr. Logs directly on error.
9152 *
Michal Vaskod3678892020-05-21 10:06:58 +02009153 * [16] RelationalExpr ::= AdditiveExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02009154 * | RelationalExpr '<' AdditiveExpr
9155 * | RelationalExpr '>' AdditiveExpr
9156 * | RelationalExpr '<=' AdditiveExpr
9157 * | RelationalExpr '>=' AdditiveExpr
9158 *
9159 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02009160 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009161 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02009162 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009163 * @param[in] options XPath options.
9164 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
9165 */
9166static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02009167eval_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 +02009168{
9169 LY_ERR rc;
Michal Vaskodd528af2022-08-08 14:35:07 +02009170 uint32_t i, this_op;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009171 struct lyxp_set orig_set, set2;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009172
9173 assert(repeat);
9174
9175 set_init(&orig_set, set);
9176 set_init(&set2, set);
9177
9178 set_fill_set(&orig_set, set);
9179
Michal Vasko004d3152020-06-11 19:59:22 +02009180 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009181 LY_CHECK_GOTO(rc, cleanup);
9182
9183 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
9184 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02009185 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009186
Michal Vasko004d3152020-06-11 19:59:22 +02009187 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_COMP);
aPiecek8b0cc152021-05-31 16:40:31 +02009188 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02009189 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02009190 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009191
aPiecek8b0cc152021-05-31 16:40:31 +02009192 if (options & LYXP_SKIP_EXPR) {
9193 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009194 LY_CHECK_GOTO(rc, cleanup);
9195 continue;
9196 }
9197
9198 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02009199 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009200 LY_CHECK_GOTO(rc, cleanup);
9201
9202 /* eval */
9203 if (options & LYXP_SCNODE_ALL) {
9204 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01009205 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02009206 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009207 } else {
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02009208 ly_bool result;
9209
9210 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], &result);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009211 LY_CHECK_GOTO(rc, cleanup);
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02009212 set_fill_boolean(set, result);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009213 }
9214 }
9215
9216cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02009217 lyxp_set_free_content(&orig_set);
9218 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009219 return rc;
9220}
9221
9222/**
9223 * @brief Evaluate EqualityExpr. Logs directly on error.
9224 *
Michal Vaskod3678892020-05-21 10:06:58 +02009225 * [15] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02009226 * | EqualityExpr '!=' RelationalExpr
9227 *
9228 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02009229 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009230 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02009231 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009232 * @param[in] options XPath options.
9233 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
9234 */
9235static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02009236eval_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 +02009237{
9238 LY_ERR rc;
Michal Vaskodd528af2022-08-08 14:35:07 +02009239 uint32_t i, this_op;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009240 struct lyxp_set orig_set, set2;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009241
9242 assert(repeat);
9243
9244 set_init(&orig_set, set);
9245 set_init(&set2, set);
9246
9247 set_fill_set(&orig_set, set);
9248
Michal Vasko004d3152020-06-11 19:59:22 +02009249 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009250 LY_CHECK_GOTO(rc, cleanup);
9251
9252 /* ('=' / '!=' RelationalExpr)* */
9253 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02009254 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009255
Michal Vasko004d3152020-06-11 19:59:22 +02009256 assert((exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_EQUAL) || (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_NEQUAL));
aPiecek8b0cc152021-05-31 16:40:31 +02009257 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02009258 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02009259 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009260
aPiecek8b0cc152021-05-31 16:40:31 +02009261 if (options & LYXP_SKIP_EXPR) {
9262 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009263 LY_CHECK_GOTO(rc, cleanup);
9264 continue;
9265 }
9266
9267 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02009268 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009269 LY_CHECK_GOTO(rc, cleanup);
9270
9271 /* eval */
9272 if (options & LYXP_SCNODE_ALL) {
9273 warn_operands(set->ctx, set, &set2, 0, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vasko004d3152020-06-11 19:59:22 +02009274 warn_equality_value(exp, set, *tok_idx - 1, this_op - 1, *tok_idx - 1);
9275 warn_equality_value(exp, &set2, this_op - 1, this_op - 1, *tok_idx - 1);
Michal Vaskoecd62de2019-11-13 12:35:11 +01009276 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02009277 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009278 } else {
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02009279 ly_bool result;
9280
9281 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], &result);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009282 LY_CHECK_GOTO(rc, cleanup);
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02009283 set_fill_boolean(set, result);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009284 }
9285 }
9286
9287cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02009288 lyxp_set_free_content(&orig_set);
9289 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009290 return rc;
9291}
9292
9293/**
9294 * @brief Evaluate AndExpr. Logs directly on error.
9295 *
Michal Vaskod3678892020-05-21 10:06:58 +02009296 * [14] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02009297 *
9298 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02009299 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009300 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02009301 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009302 * @param[in] options XPath options.
9303 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
9304 */
9305static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02009306eval_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 +02009307{
9308 LY_ERR rc;
9309 struct lyxp_set orig_set, set2;
Michal Vaskodd528af2022-08-08 14:35:07 +02009310 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009311
9312 assert(repeat);
9313
9314 set_init(&orig_set, set);
9315 set_init(&set2, set);
9316
9317 set_fill_set(&orig_set, set);
9318
Michal Vasko004d3152020-06-11 19:59:22 +02009319 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009320 LY_CHECK_GOTO(rc, cleanup);
9321
Michal Vasko3ff82dd2023-05-09 14:09:21 +02009322 if (!(options & LYXP_SKIP_EXPR)) {
9323 if (options & LYXP_SCNODE_ALL) {
9324 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
9325 } else {
9326 /* cast to boolean, we know that will be the final result */
9327 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
9328 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02009329 }
9330
9331 /* ('and' EqualityExpr)* */
9332 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02009333 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG);
aPiecek8b0cc152021-05-31 16:40:31 +02009334 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, ((options & LYXP_SKIP_EXPR) || !set->val.bln ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02009335 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02009336 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009337
9338 /* lazy evaluation */
aPiecek8b0cc152021-05-31 16:40:31 +02009339 if ((options & LYXP_SKIP_EXPR) || ((set->type == LYXP_SET_BOOLEAN) && !set->val.bln)) {
9340 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009341 LY_CHECK_GOTO(rc, cleanup);
9342 continue;
9343 }
9344
9345 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02009346 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009347 LY_CHECK_GOTO(rc, cleanup);
9348
9349 /* eval - just get boolean value actually */
9350 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vasko1a09b212021-05-06 13:00:10 +02009351 set_scnode_clear_ctx(&set2, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskoecd62de2019-11-13 12:35:11 +01009352 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009353 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009354 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009355 set_fill_set(set, &set2);
9356 }
9357 }
9358
9359cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02009360 lyxp_set_free_content(&orig_set);
9361 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009362 return rc;
9363}
9364
9365/**
9366 * @brief Evaluate OrExpr. Logs directly on error.
9367 *
Michal Vaskod3678892020-05-21 10:06:58 +02009368 * [13] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02009369 *
9370 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02009371 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009372 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02009373 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009374 * @param[in] options XPath options.
9375 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
9376 */
9377static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02009378eval_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 +02009379{
9380 LY_ERR rc;
9381 struct lyxp_set orig_set, set2;
Michal Vaskodd528af2022-08-08 14:35:07 +02009382 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009383
9384 assert(repeat);
9385
9386 set_init(&orig_set, set);
9387 set_init(&set2, set);
9388
9389 set_fill_set(&orig_set, set);
9390
Michal Vasko004d3152020-06-11 19:59:22 +02009391 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009392 LY_CHECK_GOTO(rc, cleanup);
9393
Michal Vasko3ff82dd2023-05-09 14:09:21 +02009394 if (!(options & LYXP_SKIP_EXPR)) {
9395 if (options & LYXP_SCNODE_ALL) {
9396 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
9397 } else {
9398 /* cast to boolean, we know that will be the final result */
9399 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
9400 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02009401 }
9402
9403 /* ('or' AndExpr)* */
9404 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02009405 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG);
aPiecek8b0cc152021-05-31 16:40:31 +02009406 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, ((options & LYXP_SKIP_EXPR) || set->val.bln ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02009407 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02009408 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009409
9410 /* lazy evaluation */
aPiecek8b0cc152021-05-31 16:40:31 +02009411 if ((options & LYXP_SKIP_EXPR) || ((set->type == LYXP_SET_BOOLEAN) && set->val.bln)) {
9412 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009413 LY_CHECK_GOTO(rc, cleanup);
9414 continue;
9415 }
9416
9417 set_fill_set(&set2, &orig_set);
9418 /* expr_type cound have been LYXP_EXPR_NONE in all these later calls (except for the first one),
9419 * but it does not matter */
Michal Vasko004d3152020-06-11 19:59:22 +02009420 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009421 LY_CHECK_GOTO(rc, cleanup);
9422
9423 /* eval - just get boolean value actually */
9424 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vasko1a09b212021-05-06 13:00:10 +02009425 set_scnode_clear_ctx(&set2, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskoecd62de2019-11-13 12:35:11 +01009426 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009427 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009428 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009429 set_fill_set(set, &set2);
9430 }
9431 }
9432
9433cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02009434 lyxp_set_free_content(&orig_set);
9435 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009436 return rc;
9437}
9438
9439/**
Michal Vasko004d3152020-06-11 19:59:22 +02009440 * @brief Decide what expression is at the pointer @p tok_idx and evaluate it accordingly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009441 *
9442 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02009443 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009444 * @param[in] etype Expression type to evaluate.
aPiecek8b0cc152021-05-31 16:40:31 +02009445 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009446 * @param[in] options XPath options.
9447 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
9448 */
9449static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02009450eval_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 +02009451 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02009452{
Michal Vaskodd528af2022-08-08 14:35:07 +02009453 uint32_t i, count;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009454 enum lyxp_expr_type next_etype;
9455 LY_ERR rc;
9456
9457 /* process operator repeats */
Michal Vasko004d3152020-06-11 19:59:22 +02009458 if (!exp->repeat[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02009459 next_etype = LYXP_EXPR_NONE;
9460 } else {
9461 /* find etype repeat */
Radek Krejci1e008d22020-08-17 11:37:37 +02009462 for (i = 0; exp->repeat[*tok_idx][i] > etype; ++i) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02009463
9464 /* select one-priority lower because etype expression called us */
9465 if (i) {
Michal Vasko004d3152020-06-11 19:59:22 +02009466 next_etype = exp->repeat[*tok_idx][i - 1];
Michal Vasko03ff5a72019-09-11 13:49:33 +02009467 /* count repeats for that expression */
Radek Krejci1e008d22020-08-17 11:37:37 +02009468 for (count = 0; i && exp->repeat[*tok_idx][i - 1] == next_etype; ++count, --i) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02009469 } else {
9470 next_etype = LYXP_EXPR_NONE;
9471 }
9472 }
9473
9474 /* decide what expression are we parsing based on the repeat */
9475 switch (next_etype) {
9476 case LYXP_EXPR_OR:
Michal Vasko004d3152020-06-11 19:59:22 +02009477 rc = eval_or_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009478 break;
9479 case LYXP_EXPR_AND:
Michal Vasko004d3152020-06-11 19:59:22 +02009480 rc = eval_and_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009481 break;
9482 case LYXP_EXPR_EQUALITY:
Michal Vasko004d3152020-06-11 19:59:22 +02009483 rc = eval_equality_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009484 break;
9485 case LYXP_EXPR_RELATIONAL:
Michal Vasko004d3152020-06-11 19:59:22 +02009486 rc = eval_relational_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009487 break;
9488 case LYXP_EXPR_ADDITIVE:
Michal Vasko004d3152020-06-11 19:59:22 +02009489 rc = eval_additive_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009490 break;
9491 case LYXP_EXPR_MULTIPLICATIVE:
Michal Vasko004d3152020-06-11 19:59:22 +02009492 rc = eval_multiplicative_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009493 break;
9494 case LYXP_EXPR_UNARY:
Michal Vasko004d3152020-06-11 19:59:22 +02009495 rc = eval_unary_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009496 break;
9497 case LYXP_EXPR_UNION:
Michal Vasko004d3152020-06-11 19:59:22 +02009498 rc = eval_union_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009499 break;
9500 case LYXP_EXPR_NONE:
Michal Vasko004d3152020-06-11 19:59:22 +02009501 rc = eval_path_expr(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009502 break;
9503 default:
9504 LOGINT_RET(set->ctx);
9505 }
9506
9507 return rc;
9508}
9509
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009510/**
9511 * @brief Get root type.
9512 *
9513 * @param[in] ctx_node Context node.
9514 * @param[in] ctx_scnode Schema context node.
9515 * @param[in] options XPath options.
9516 * @return Root type.
9517 */
9518static enum lyxp_node_type
Radek Krejci1deb5be2020-08-26 16:43:36 +02009519lyxp_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 +01009520{
Michal Vasko6b26e742020-07-17 15:02:10 +02009521 const struct lysc_node *op;
9522
Michal Vaskoa27245c2022-05-02 09:01:35 +02009523 /* explicit */
9524 if (options & LYXP_ACCESS_TREE_ALL) {
9525 return LYXP_NODE_ROOT;
9526 } else if (options & LYXP_ACCESS_TREE_CONFIG) {
9527 return LYXP_NODE_ROOT_CONFIG;
9528 }
9529
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009530 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009531 /* schema */
Radek Krejci1e008d22020-08-17 11:37:37 +02009532 for (op = ctx_scnode; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent) {}
Michal Vasko6b26e742020-07-17 15:02:10 +02009533
9534 if (op || (options & LYXP_SCNODE)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009535 /* general root that can access everything */
9536 return LYXP_NODE_ROOT;
9537 } else if (!ctx_scnode || (ctx_scnode->flags & LYS_CONFIG_W)) {
9538 /* root context node can access only config data (because we said so, it is unspecified) */
9539 return LYXP_NODE_ROOT_CONFIG;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009540 }
Michal Vasko6b26e742020-07-17 15:02:10 +02009541 return LYXP_NODE_ROOT;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009542 }
9543
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009544 /* data */
Michal Vasko6b26e742020-07-17 15:02:10 +02009545 op = ctx_node ? ctx_node->schema : NULL;
Michal Vaskod989ba02020-08-24 10:59:24 +02009546 for ( ; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent) {}
Michal Vasko6b26e742020-07-17 15:02:10 +02009547
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009548 if (op || !(options & LYXP_SCHEMA)) {
9549 /* general root that can access everything */
9550 return LYXP_NODE_ROOT;
Christian Hoppsb6ecaea2021-02-06 09:45:38 -05009551 } else if (!ctx_node || !ctx_node->schema || (ctx_node->schema->flags & LYS_CONFIG_W)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009552 /* root context node can access only config data (because we said so, it is unspecified) */
9553 return LYXP_NODE_ROOT_CONFIG;
9554 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009555 return LYXP_NODE_ROOT;
9556}
9557
Michal Vasko03ff5a72019-09-11 13:49:33 +02009558LY_ERR
Michal Vasko400e9672021-01-11 13:39:17 +01009559lyxp_eval(const struct ly_ctx *ctx, const struct lyxp_expr *exp, const struct lys_module *cur_mod,
Michal Vaskoa3e92bc2022-07-29 14:56:23 +02009560 LY_VALUE_FORMAT format, void *prefix_data, const struct lyd_node *cur_node, const struct lyd_node *ctx_node,
9561 const struct lyd_node *tree, const struct lyxp_var *vars, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02009562{
Michal Vaskodd528af2022-08-08 14:35:07 +02009563 uint32_t tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009564 LY_ERR rc;
9565
Michal Vasko400e9672021-01-11 13:39:17 +01009566 LY_CHECK_ARG_RET(ctx, ctx, exp, set, LY_EINVAL);
Radek Krejci8df109d2021-04-23 12:19:08 +02009567 if (!cur_mod && ((format == LY_VALUE_SCHEMA) || (format == LY_VALUE_SCHEMA_RESOLVED))) {
Michal Vaskoddd76592022-01-17 13:34:48 +01009568 LOGERR(ctx, LY_EINVAL, "Current module must be set if schema format is used.");
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009569 return LY_EINVAL;
Michal Vasko004d3152020-06-11 19:59:22 +02009570 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02009571
Michal Vaskod3bb12f2020-12-04 14:33:09 +01009572 if (tree) {
9573 /* adjust the pointer to be the first top-level sibling */
9574 while (tree->parent) {
9575 tree = lyd_parent(tree);
9576 }
9577 tree = lyd_first_sibling(tree);
Michal Vaskoddd76592022-01-17 13:34:48 +01009578
Michal Vasko6f78a772022-11-10 08:13:52 +01009579 if (lysc_data_parent(tree->schema)) {
Michal Vaskoddd76592022-01-17 13:34:48 +01009580 /* unable to evaluate absolute paths */
9581 LOGERR(ctx, LY_EINVAL, "Data node \"%s\" has no parent but is not instance of a top-level schema node.",
9582 LYD_NAME(tree));
9583 return LY_EINVAL;
9584 }
Michal Vaskod3bb12f2020-12-04 14:33:09 +01009585 }
9586
Michal Vasko03ff5a72019-09-11 13:49:33 +02009587 /* prepare set for evaluation */
Michal Vasko03ff5a72019-09-11 13:49:33 +02009588 memset(set, 0, sizeof *set);
Michal Vaskod3678892020-05-21 10:06:58 +02009589 set->type = LYXP_SET_NODE_SET;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009590 set->root_type = lyxp_get_root_type(ctx_node, NULL, options);
9591 set_insert_node(set, (struct lyd_node *)ctx_node, 0, ctx_node ? LYXP_NODE_ELEM : set->root_type, 0);
9592
Michal Vasko400e9672021-01-11 13:39:17 +01009593 set->ctx = (struct ly_ctx *)ctx;
Michal Vaskoa3e92bc2022-07-29 14:56:23 +02009594 set->cur_node = cur_node;
9595 for (set->context_op = cur_node ? cur_node->schema : NULL;
Radek Krejci0f969882020-08-21 16:56:47 +02009596 set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF));
9597 set->context_op = set->context_op->parent) {}
Michal Vaskof03ed032020-03-04 13:31:44 +01009598 set->tree = tree;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009599 set->cur_mod = cur_mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009600 set->format = format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009601 set->prefix_data = prefix_data;
aPiecekfba75362021-10-07 12:39:48 +02009602 set->vars = vars;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009603
Radek Krejciddace2c2021-01-08 11:30:56 +01009604 LOG_LOCSET(NULL, set->cur_node, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01009605
Michal Vasko03ff5a72019-09-11 13:49:33 +02009606 /* evaluate */
Michal Vasko004d3152020-06-11 19:59:22 +02009607 rc = eval_expr_select(exp, &tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009608 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02009609 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009610 }
9611
Michal Vasko4a7d4d62021-12-13 17:05:06 +01009612 if (set->cur_node) {
9613 LOG_LOCBACK(0, 1, 0, 0);
9614 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02009615 return rc;
9616}
9617
9618#if 0
9619
9620/* full xml printing of set elements, not used currently */
9621
9622void
9623lyxp_set_print_xml(FILE *f, struct lyxp_set *set)
9624{
9625 uint32_t i;
9626 char *str_num;
9627 struct lyout out;
9628
9629 memset(&out, 0, sizeof out);
9630
9631 out.type = LYOUT_STREAM;
9632 out.method.f = f;
9633
9634 switch (set->type) {
9635 case LYXP_SET_EMPTY:
Michal Vasko5233e962020-08-14 14:26:20 +02009636 ly_print_(&out, "Empty XPath set\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02009637 break;
9638 case LYXP_SET_BOOLEAN:
Michal Vasko5233e962020-08-14 14:26:20 +02009639 ly_print_(&out, "Boolean XPath set:\n");
9640 ly_print_(&out, "%s\n\n", set->value.bool ? "true" : "false");
Michal Vasko03ff5a72019-09-11 13:49:33 +02009641 break;
9642 case LYXP_SET_STRING:
Michal Vasko5233e962020-08-14 14:26:20 +02009643 ly_print_(&out, "String XPath set:\n");
9644 ly_print_(&out, "\"%s\"\n\n", set->value.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009645 break;
9646 case LYXP_SET_NUMBER:
Michal Vasko5233e962020-08-14 14:26:20 +02009647 ly_print_(&out, "Number XPath set:\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02009648
9649 if (isnan(set->value.num)) {
9650 str_num = strdup("NaN");
9651 } else if ((set->value.num == 0) || (set->value.num == -0.0f)) {
9652 str_num = strdup("0");
9653 } else if (isinf(set->value.num) && !signbit(set->value.num)) {
9654 str_num = strdup("Infinity");
9655 } else if (isinf(set->value.num) && signbit(set->value.num)) {
9656 str_num = strdup("-Infinity");
9657 } else if ((long long)set->value.num == set->value.num) {
9658 if (asprintf(&str_num, "%lld", (long long)set->value.num) == -1) {
9659 str_num = NULL;
9660 }
9661 } else {
9662 if (asprintf(&str_num, "%03.1Lf", set->value.num) == -1) {
9663 str_num = NULL;
9664 }
9665 }
9666 if (!str_num) {
9667 LOGMEM;
9668 return;
9669 }
Michal Vasko5233e962020-08-14 14:26:20 +02009670 ly_print_(&out, "%s\n\n", str_num);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009671 free(str_num);
9672 break;
9673 case LYXP_SET_NODE_SET:
Michal Vasko5233e962020-08-14 14:26:20 +02009674 ly_print_(&out, "Node XPath set:\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02009675
9676 for (i = 0; i < set->used; ++i) {
Michal Vasko5233e962020-08-14 14:26:20 +02009677 ly_print_(&out, "%d. ", i + 1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009678 switch (set->node_type[i]) {
9679 case LYXP_NODE_ROOT_ALL:
Michal Vasko5233e962020-08-14 14:26:20 +02009680 ly_print_(&out, "ROOT all\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02009681 break;
9682 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko5233e962020-08-14 14:26:20 +02009683 ly_print_(&out, "ROOT config\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02009684 break;
9685 case LYXP_NODE_ROOT_STATE:
Michal Vasko5233e962020-08-14 14:26:20 +02009686 ly_print_(&out, "ROOT state\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02009687 break;
9688 case LYXP_NODE_ROOT_NOTIF:
Michal Vasko5233e962020-08-14 14:26:20 +02009689 ly_print_(&out, "ROOT notification \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009690 break;
9691 case LYXP_NODE_ROOT_RPC:
Michal Vasko5233e962020-08-14 14:26:20 +02009692 ly_print_(&out, "ROOT rpc \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009693 break;
9694 case LYXP_NODE_ROOT_OUTPUT:
Michal Vasko5233e962020-08-14 14:26:20 +02009695 ly_print_(&out, "ROOT output \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009696 break;
9697 case LYXP_NODE_ELEM:
Michal Vasko5233e962020-08-14 14:26:20 +02009698 ly_print_(&out, "ELEM \"%s\"\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009699 xml_print_node(&out, 1, set->value.nodes[i], 1, LYP_FORMAT);
Michal Vasko5233e962020-08-14 14:26:20 +02009700 ly_print_(&out, "\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02009701 break;
9702 case LYXP_NODE_TEXT:
Michal Vasko5233e962020-08-14 14:26:20 +02009703 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 +02009704 break;
9705 case LYXP_NODE_ATTR:
Michal Vasko5233e962020-08-14 14:26:20 +02009706 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 +02009707 break;
9708 }
9709 }
9710 break;
9711 }
9712}
9713
9714#endif
9715
9716LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009717lyxp_set_cast(struct lyxp_set *set, enum lyxp_set_type target)
Michal Vasko03ff5a72019-09-11 13:49:33 +02009718{
9719 long double num;
9720 char *str;
9721 LY_ERR rc;
9722
9723 if (!set || (set->type == target)) {
9724 return LY_SUCCESS;
9725 }
9726
9727 /* it's not possible to convert anything into a node set */
Michal Vaskod3678892020-05-21 10:06:58 +02009728 assert(target != LYXP_SET_NODE_SET);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009729
9730 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vaskod3678892020-05-21 10:06:58 +02009731 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009732 return LY_EINVAL;
9733 }
9734
9735 /* to STRING */
Michal Vaskod3678892020-05-21 10:06:58 +02009736 if ((target == LYXP_SET_STRING) || ((target == LYXP_SET_NUMBER) && (set->type == LYXP_SET_NODE_SET))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02009737 switch (set->type) {
9738 case LYXP_SET_NUMBER:
9739 if (isnan(set->val.num)) {
9740 set->val.str = strdup("NaN");
9741 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
9742 } else if ((set->val.num == 0) || (set->val.num == -0.0f)) {
9743 set->val.str = strdup("0");
9744 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
9745 } else if (isinf(set->val.num) && !signbit(set->val.num)) {
9746 set->val.str = strdup("Infinity");
9747 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
9748 } else if (isinf(set->val.num) && signbit(set->val.num)) {
9749 set->val.str = strdup("-Infinity");
9750 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
9751 } else if ((long long)set->val.num == set->val.num) {
9752 if (asprintf(&str, "%lld", (long long)set->val.num) == -1) {
9753 LOGMEM_RET(set->ctx);
9754 }
9755 set->val.str = str;
9756 } else {
9757 if (asprintf(&str, "%03.1Lf", set->val.num) == -1) {
9758 LOGMEM_RET(set->ctx);
9759 }
9760 set->val.str = str;
9761 }
9762 break;
9763 case LYXP_SET_BOOLEAN:
Michal Vasko004d3152020-06-11 19:59:22 +02009764 if (set->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02009765 set->val.str = strdup("true");
9766 } else {
9767 set->val.str = strdup("false");
9768 }
9769 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), LY_EMEM);
9770 break;
9771 case LYXP_SET_NODE_SET:
Michal Vasko03ff5a72019-09-11 13:49:33 +02009772 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009773 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02009774
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009775 rc = cast_node_set_to_string(set, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009776 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02009777 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009778 set->val.str = str;
9779 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009780 default:
9781 LOGINT_RET(set->ctx);
9782 }
9783 set->type = LYXP_SET_STRING;
9784 }
9785
9786 /* to NUMBER */
9787 if (target == LYXP_SET_NUMBER) {
9788 switch (set->type) {
9789 case LYXP_SET_STRING:
9790 num = cast_string_to_number(set->val.str);
Michal Vaskod3678892020-05-21 10:06:58 +02009791 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009792 set->val.num = num;
9793 break;
9794 case LYXP_SET_BOOLEAN:
Michal Vasko004d3152020-06-11 19:59:22 +02009795 if (set->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02009796 set->val.num = 1;
9797 } else {
9798 set->val.num = 0;
9799 }
9800 break;
9801 default:
9802 LOGINT_RET(set->ctx);
9803 }
9804 set->type = LYXP_SET_NUMBER;
9805 }
9806
9807 /* to BOOLEAN */
9808 if (target == LYXP_SET_BOOLEAN) {
9809 switch (set->type) {
9810 case LYXP_SET_NUMBER:
9811 if ((set->val.num == 0) || (set->val.num == -0.0f) || isnan(set->val.num)) {
Michal Vasko004d3152020-06-11 19:59:22 +02009812 set->val.bln = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009813 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02009814 set->val.bln = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009815 }
9816 break;
9817 case LYXP_SET_STRING:
9818 if (set->val.str[0]) {
Michal Vaskod3678892020-05-21 10:06:58 +02009819 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02009820 set->val.bln = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009821 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02009822 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02009823 set->val.bln = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009824 }
9825 break;
9826 case LYXP_SET_NODE_SET:
Michal Vaskod3678892020-05-21 10:06:58 +02009827 if (set->used) {
9828 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02009829 set->val.bln = 1;
Michal Vaskod3678892020-05-21 10:06:58 +02009830 } else {
9831 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02009832 set->val.bln = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02009833 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02009834 break;
9835 default:
9836 LOGINT_RET(set->ctx);
9837 }
9838 set->type = LYXP_SET_BOOLEAN;
9839 }
9840
Michal Vasko03ff5a72019-09-11 13:49:33 +02009841 return LY_SUCCESS;
9842}
9843
9844LY_ERR
Michal Vasko400e9672021-01-11 13:39:17 +01009845lyxp_atomize(const struct ly_ctx *ctx, const struct lyxp_expr *exp, const struct lys_module *cur_mod,
Michal Vaskoa3e92bc2022-07-29 14:56:23 +02009846 LY_VALUE_FORMAT format, void *prefix_data, const struct lysc_node *cur_scnode,
9847 const struct lysc_node *ctx_scnode, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02009848{
Radek Krejci2efc45b2020-12-22 16:25:44 +01009849 LY_ERR ret;
Michal Vaskodd528af2022-08-08 14:35:07 +02009850 uint32_t tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009851
Michal Vasko400e9672021-01-11 13:39:17 +01009852 LY_CHECK_ARG_RET(ctx, ctx, exp, set, LY_EINVAL);
Radek Krejci8df109d2021-04-23 12:19:08 +02009853 if (!cur_mod && ((format == LY_VALUE_SCHEMA) || (format == LY_VALUE_SCHEMA_RESOLVED))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009854 LOGARG(NULL, "Current module must be set if schema format is used.");
9855 return LY_EINVAL;
Michal Vasko004d3152020-06-11 19:59:22 +02009856 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02009857
9858 /* prepare set for evaluation */
Michal Vasko03ff5a72019-09-11 13:49:33 +02009859 memset(set, 0, sizeof *set);
9860 set->type = LYXP_SET_SCNODE_SET;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009861 set->root_type = lyxp_get_root_type(NULL, ctx_scnode, options);
Michal Vaskoe4a6d012023-05-22 14:34:52 +02009862 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 +01009863 set->val.scnodes[0].in_ctx = LYXP_SET_SCNODE_START;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009864
Michal Vasko400e9672021-01-11 13:39:17 +01009865 set->ctx = (struct ly_ctx *)ctx;
Michal Vaskoa3e92bc2022-07-29 14:56:23 +02009866 set->cur_scnode = cur_scnode;
9867 for (set->context_op = cur_scnode;
Radek Krejci0f969882020-08-21 16:56:47 +02009868 set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF));
9869 set->context_op = set->context_op->parent) {}
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009870 set->cur_mod = cur_mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009871 set->format = format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009872 set->prefix_data = prefix_data;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009873
Radek Krejciddace2c2021-01-08 11:30:56 +01009874 LOG_LOCSET(set->cur_scnode, NULL, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01009875
Michal Vasko03ff5a72019-09-11 13:49:33 +02009876 /* evaluate */
Radek Krejci2efc45b2020-12-22 16:25:44 +01009877 ret = eval_expr_select(exp, &tok_idx, 0, set, options);
9878
Michal Vaskof69e4552022-12-02 10:27:42 +01009879 LOG_LOCBACK(set->cur_scnode ? 1 : 0, 0, 0, 0);
Radek Krejci2efc45b2020-12-22 16:25:44 +01009880 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009881}
Michal Vaskod43d71a2020-08-07 14:54:58 +02009882
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01009883LIBYANG_API_DEF const char *
Michal Vaskod43d71a2020-08-07 14:54:58 +02009884lyxp_get_expr(const struct lyxp_expr *path)
9885{
9886 if (!path) {
9887 return NULL;
9888 }
9889
9890 return path->expr;
9891}