blob: 49b87d6fa3e7b5be833349117bbc56b38c844bcb [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 Vasko03ff5a72019-09-11 13:49:33 +020035#include "plugins_types.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020036#include "printer_data.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020037#include "schema_compile_node.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020038#include "tree.h"
Radek Krejci77114102021-03-10 15:21:57 +010039#include "tree_data.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020040#include "tree_data_internal.h"
Radek Krejci859a15a2021-03-05 20:56:59 +010041#include "tree_edit.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020042#include "tree_schema_internal.h"
43#include "xml.h"
Michal Vasko03ff5a72019-09-11 13:49:33 +020044
Michal Vasko7333cb32022-07-29 16:30:29 +020045static LY_ERR set_scnode_insert_node(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type,
46 enum lyxp_axis axis, uint32_t *index_p);
Michal Vaskodd528af2022-08-08 14:35:07 +020047static LY_ERR reparse_or_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint32_t *tok_idx, uint32_t depth);
48static 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 +020049 struct lyxp_set *set, uint32_t options);
Michal Vaskofe1af042022-07-29 14:58:59 +020050static LY_ERR moveto_resolve_model(const char **qname, uint32_t *qname_len, const struct lyxp_set *set,
Michal Vasko93923692021-05-07 15:28:02 +020051 const struct lysc_node *ctx_scnode, const struct lys_module **moveto_mod);
Michal Vasko47da6562022-07-14 15:43:15 +020052static LY_ERR moveto_axis_node_next(const struct lyd_node **iter, enum lyxp_node_type *iter_type,
53 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 +020054static LY_ERR moveto_node(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname,
55 enum lyxp_axis axis, uint32_t options);
56static LY_ERR moveto_scnode(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname,
57 enum lyxp_axis axis, uint32_t options);
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +020058static 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 +020059
aPiecek96dc1e32021-10-08 15:45:59 +020060/* Functions are divided into the following basic classes:
61 *
62 * (re)parse functions:
63 * Parse functions parse the expression into
64 * tokens (syntactic analysis).
65 * Reparse functions perform semantic analysis
66 * (do not save the result, just a check) of
67 * the expression and fill repeat indices.
68 *
69 * warn functions:
70 * Warn functions check specific reasonable conditions for schema XPath
71 * and print a warning if they are not satisfied.
72 *
73 * moveto functions:
74 * They and only they actually change the context (set).
75 *
76 * eval functions:
77 * They execute a parsed XPath expression on some data subtree.
78 */
79
Michal Vasko03ff5a72019-09-11 13:49:33 +020080/**
81 * @brief Print the type of an XPath \p set.
82 *
83 * @param[in] set Set to use.
84 * @return Set type string.
85 */
86static const char *
87print_set_type(struct lyxp_set *set)
88{
89 switch (set->type) {
Michal Vasko03ff5a72019-09-11 13:49:33 +020090 case LYXP_SET_NODE_SET:
91 return "node set";
92 case LYXP_SET_SCNODE_SET:
93 return "schema node set";
94 case LYXP_SET_BOOLEAN:
95 return "boolean";
96 case LYXP_SET_NUMBER:
97 return "number";
98 case LYXP_SET_STRING:
99 return "string";
100 }
101
102 return NULL;
103}
104
Michal Vasko24cddf82020-06-01 08:17:01 +0200105const char *
Michal Vasko49fec8e2022-05-24 10:28:33 +0200106lyxp_token2str(enum lyxp_token tok)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200107{
108 switch (tok) {
109 case LYXP_TOKEN_PAR1:
110 return "(";
111 case LYXP_TOKEN_PAR2:
112 return ")";
113 case LYXP_TOKEN_BRACK1:
114 return "[";
115 case LYXP_TOKEN_BRACK2:
116 return "]";
117 case LYXP_TOKEN_DOT:
118 return ".";
119 case LYXP_TOKEN_DDOT:
120 return "..";
121 case LYXP_TOKEN_AT:
122 return "@";
123 case LYXP_TOKEN_COMMA:
124 return ",";
125 case LYXP_TOKEN_NAMETEST:
126 return "NameTest";
127 case LYXP_TOKEN_NODETYPE:
128 return "NodeType";
aPiecekfba75362021-10-07 12:39:48 +0200129 case LYXP_TOKEN_VARREF:
130 return "VariableReference";
Michal Vasko03ff5a72019-09-11 13:49:33 +0200131 case LYXP_TOKEN_FUNCNAME:
132 return "FunctionName";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200133 case LYXP_TOKEN_OPER_LOG:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200134 return "Operator(Logic)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200135 case LYXP_TOKEN_OPER_EQUAL:
136 return "Operator(Equal)";
137 case LYXP_TOKEN_OPER_NEQUAL:
138 return "Operator(Non-equal)";
139 case LYXP_TOKEN_OPER_COMP:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200140 return "Operator(Comparison)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200141 case LYXP_TOKEN_OPER_MATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200142 return "Operator(Math)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200143 case LYXP_TOKEN_OPER_UNI:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200144 return "Operator(Union)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200145 case LYXP_TOKEN_OPER_PATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200146 return "Operator(Path)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200147 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko14676352020-05-29 11:35:55 +0200148 return "Operator(Recursive Path)";
Michal Vasko03ff5a72019-09-11 13:49:33 +0200149 case LYXP_TOKEN_LITERAL:
150 return "Literal";
151 case LYXP_TOKEN_NUMBER:
152 return "Number";
153 default:
154 LOGINT(NULL);
155 return "";
156 }
157}
158
159/**
Michal Vasko49fec8e2022-05-24 10:28:33 +0200160 * @brief Transform string into an axis.
161 *
162 * @param[in] str String to transform.
163 * @param[in] str_len Length of @p str.
164 * @return Transformed axis.
165 */
166static enum lyxp_axis
Michal Vaskodd528af2022-08-08 14:35:07 +0200167str2axis(const char *str, uint32_t str_len)
Michal Vasko49fec8e2022-05-24 10:28:33 +0200168{
169 switch (str_len) {
170 case 4:
171 assert(!strncmp("self", str, str_len));
172 return LYXP_AXIS_SELF;
173 case 5:
174 assert(!strncmp("child", str, str_len));
175 return LYXP_AXIS_CHILD;
176 case 6:
177 assert(!strncmp("parent", str, str_len));
178 return LYXP_AXIS_PARENT;
179 case 8:
180 assert(!strncmp("ancestor", str, str_len));
181 return LYXP_AXIS_ANCESTOR;
182 case 9:
183 if (str[0] == 'a') {
184 assert(!strncmp("attribute", str, str_len));
185 return LYXP_AXIS_ATTRIBUTE;
186 } else if (str[0] == 'f') {
187 assert(!strncmp("following", str, str_len));
188 return LYXP_AXIS_FOLLOWING;
189 } else {
190 assert(!strncmp("preceding", str, str_len));
191 return LYXP_AXIS_PRECEDING;
192 }
193 break;
194 case 10:
195 assert(!strncmp("descendant", str, str_len));
196 return LYXP_AXIS_DESCENDANT;
197 case 16:
198 assert(!strncmp("ancestor-or-self", str, str_len));
199 return LYXP_AXIS_ANCESTOR_OR_SELF;
200 case 17:
201 if (str[0] == 'f') {
202 assert(!strncmp("following-sibling", str, str_len));
203 return LYXP_AXIS_FOLLOWING_SIBLING;
204 } else {
205 assert(!strncmp("preceding-sibling", str, str_len));
206 return LYXP_AXIS_PRECEDING_SIBLING;
207 }
208 break;
209 case 18:
210 assert(!strncmp("descendant-or-self", str, str_len));
211 return LYXP_AXIS_DESCENDANT_OR_SELF;
212 }
213
214 LOGINT(NULL);
215 return 0;
216}
217
218/**
Michal Vasko03ff5a72019-09-11 13:49:33 +0200219 * @brief Print the whole expression \p exp to debug output.
220 *
221 * @param[in] exp Expression to use.
222 */
223static void
Michal Vasko40308e72020-10-20 16:38:40 +0200224print_expr_struct_debug(const struct lyxp_expr *exp)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200225{
Radek Krejcif13b87b2020-12-01 22:02:17 +0100226#define MSG_BUFFER_SIZE 128
227 char tmp[MSG_BUFFER_SIZE];
Michal Vasko1fdd8fa2021-01-08 09:21:45 +0100228 uint32_t i, j;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200229
Radek Krejci52b6d512020-10-12 12:33:17 +0200230 if (!exp || (ly_ll < LY_LLDBG)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200231 return;
232 }
233
234 LOGDBG(LY_LDGXPATH, "expression \"%s\":", exp->expr);
235 for (i = 0; i < exp->used; ++i) {
Michal Vasko49fec8e2022-05-24 10:28:33 +0200236 sprintf(tmp, "\ttoken %s, in expression \"%.*s\"", lyxp_token2str(exp->tokens[i]), exp->tok_len[i],
Michal Vasko69730152020-10-09 16:30:07 +0200237 &exp->expr[exp->tok_pos[i]]);
Michal Vasko23049552021-03-04 15:57:44 +0100238 if (exp->repeat && exp->repeat[i]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200239 sprintf(tmp + strlen(tmp), " (repeat %d", exp->repeat[i][0]);
240 for (j = 1; exp->repeat[i][j]; ++j) {
241 sprintf(tmp + strlen(tmp), ", %d", exp->repeat[i][j]);
242 }
243 strcat(tmp, ")");
244 }
245 LOGDBG(LY_LDGXPATH, tmp);
246 }
Radek Krejcif13b87b2020-12-01 22:02:17 +0100247#undef MSG_BUFFER_SIZE
Michal Vasko03ff5a72019-09-11 13:49:33 +0200248}
249
250#ifndef NDEBUG
251
252/**
253 * @brief Print XPath set content to debug output.
254 *
255 * @param[in] set Set to print.
256 */
257static void
258print_set_debug(struct lyxp_set *set)
259{
260 uint32_t i;
261 char *str;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200262 struct lyxp_set_node *item;
263 struct lyxp_set_scnode *sitem;
264
Radek Krejci52b6d512020-10-12 12:33:17 +0200265 if (ly_ll < LY_LLDBG) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200266 return;
267 }
268
269 switch (set->type) {
270 case LYXP_SET_NODE_SET:
271 LOGDBG(LY_LDGXPATH, "set NODE SET:");
272 for (i = 0; i < set->used; ++i) {
273 item = &set->val.nodes[i];
274
275 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +0100276 case LYXP_NODE_NONE:
277 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): NONE", i + 1, item->pos);
278 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200279 case LYXP_NODE_ROOT:
280 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT", i + 1, item->pos);
281 break;
282 case LYXP_NODE_ROOT_CONFIG:
283 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT CONFIG", i + 1, item->pos);
284 break;
285 case LYXP_NODE_ELEM:
Michal Vasko9e685082021-01-29 14:49:09 +0100286 if ((item->node->schema->nodetype == LYS_LIST) && (lyd_child(item->node)->schema->nodetype == LYS_LEAF)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200287 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (1st child val: %s)", i + 1, item->pos,
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200288 item->node->schema->name, lyd_get_value(lyd_child(item->node)));
Michal Vasko9e685082021-01-29 14:49:09 +0100289 } else if (item->node->schema->nodetype == LYS_LEAFLIST) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200290 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (val: %s)", i + 1, item->pos,
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200291 item->node->schema->name, lyd_get_value(item->node));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200292 } else {
293 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s", i + 1, item->pos, item->node->schema->name);
294 }
295 break;
296 case LYXP_NODE_TEXT:
297 if (item->node->schema->nodetype & LYS_ANYDATA) {
298 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): TEXT <%s>", i + 1, item->pos,
Michal Vasko69730152020-10-09 16:30:07 +0200299 item->node->schema->nodetype == LYS_ANYXML ? "anyxml" : "anydata");
Michal Vasko03ff5a72019-09-11 13:49:33 +0200300 } else {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200301 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 +0200302 }
303 break;
Michal Vasko9f96a052020-03-10 09:41:45 +0100304 case LYXP_NODE_META:
305 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 +0200306 set->val.meta[i].meta->value);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200307 break;
308 }
309 }
310 break;
311
312 case LYXP_SET_SCNODE_SET:
313 LOGDBG(LY_LDGXPATH, "set SCNODE SET:");
314 for (i = 0; i < set->used; ++i) {
315 sitem = &set->val.scnodes[i];
316
317 switch (sitem->type) {
318 case LYXP_NODE_ROOT:
319 LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT", i + 1, sitem->in_ctx);
320 break;
321 case LYXP_NODE_ROOT_CONFIG:
322 LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT CONFIG", i + 1, sitem->in_ctx);
323 break;
324 case LYXP_NODE_ELEM:
325 LOGDBG(LY_LDGXPATH, "\t%d (%u): ELEM %s", i + 1, sitem->in_ctx, sitem->scnode->name);
326 break;
327 default:
328 LOGINT(NULL);
329 break;
330 }
331 }
332 break;
333
Michal Vasko03ff5a72019-09-11 13:49:33 +0200334 case LYXP_SET_BOOLEAN:
335 LOGDBG(LY_LDGXPATH, "set BOOLEAN");
Michal Vasko004d3152020-06-11 19:59:22 +0200336 LOGDBG(LY_LDGXPATH, "\t%s", (set->val.bln ? "true" : "false"));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200337 break;
338
339 case LYXP_SET_STRING:
340 LOGDBG(LY_LDGXPATH, "set STRING");
341 LOGDBG(LY_LDGXPATH, "\t%s", set->val.str);
342 break;
343
344 case LYXP_SET_NUMBER:
345 LOGDBG(LY_LDGXPATH, "set NUMBER");
346
347 if (isnan(set->val.num)) {
348 str = strdup("NaN");
349 } else if ((set->val.num == 0) || (set->val.num == -0.0f)) {
350 str = strdup("0");
351 } else if (isinf(set->val.num) && !signbit(set->val.num)) {
352 str = strdup("Infinity");
353 } else if (isinf(set->val.num) && signbit(set->val.num)) {
354 str = strdup("-Infinity");
355 } else if ((long long)set->val.num == set->val.num) {
356 if (asprintf(&str, "%lld", (long long)set->val.num) == -1) {
357 str = NULL;
358 }
359 } else {
360 if (asprintf(&str, "%03.1Lf", set->val.num) == -1) {
361 str = NULL;
362 }
363 }
364 LY_CHECK_ERR_RET(!str, LOGMEM(NULL), );
365
366 LOGDBG(LY_LDGXPATH, "\t%s", str);
367 free(str);
368 }
369}
370
371#endif
372
373/**
374 * @brief Realloc the string \p str.
375 *
376 * @param[in] ctx libyang context for logging.
377 * @param[in] needed How much free space is required.
378 * @param[in,out] str Pointer to the string to use.
379 * @param[in,out] used Used bytes in \p str.
380 * @param[in,out] size Allocated bytes in \p str.
381 * @return LY_ERR
382 */
383static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +0200384cast_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 +0200385{
Michal Vasko2291ab92022-08-08 08:53:12 +0200386 if (*size - (unsigned)*used < needed) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200387 do {
Michal Vaskodd528af2022-08-08 14:35:07 +0200388 if ((UINT32_MAX - *size) < LYXP_STRING_CAST_SIZE_STEP) {
389 LOGERR(ctx, LY_EINVAL, "XPath string length limit (%" PRIu32 ") reached.", UINT32_MAX);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200390 return LY_EINVAL;
391 }
392 *size += LYXP_STRING_CAST_SIZE_STEP;
Michal Vasko2291ab92022-08-08 08:53:12 +0200393 } while (*size - (unsigned)*used < needed);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200394 *str = ly_realloc(*str, *size * sizeof(char));
395 LY_CHECK_ERR_RET(!(*str), LOGMEM(ctx), LY_EMEM);
396 }
397
398 return LY_SUCCESS;
399}
400
401/**
402 * @brief Cast nodes recursively to one string @p str.
403 *
Michal Vasko47da6562022-07-14 15:43:15 +0200404 * @param[in] node Node to cast, NULL if root.
405 * @param[in] set XPath set.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200406 * @param[in] indent Current indent.
407 * @param[in,out] str Resulting string.
408 * @param[in,out] used Used bytes in @p str.
409 * @param[in,out] size Allocated bytes in @p str.
Michal Vasko47da6562022-07-14 15:43:15 +0200410 * @return LY_ERR value.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200411 */
412static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +0200413cast_string_recursive(const struct lyd_node *node, struct lyxp_set *set, uint32_t indent, char **str, uint32_t *used,
414 uint32_t *size)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200415{
Radek Krejci7f769d72020-07-11 23:13:56 +0200416 char *buf, *line, *ptr = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200417 const char *value_str;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200418 const struct lyd_node *child;
Michal Vasko47da6562022-07-14 15:43:15 +0200419 enum lyxp_node_type child_type;
Michal Vasko60ea6352020-06-29 13:39:39 +0200420 struct lyd_node *tree;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200421 struct lyd_node_any *any;
422 LY_ERR rc;
423
Michal Vasko47da6562022-07-14 15:43:15 +0200424 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && node && (node->schema->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200425 return LY_SUCCESS;
426 }
427
Michal Vasko47da6562022-07-14 15:43:15 +0200428 if (!node) {
429 /* fake container */
430 LY_CHECK_RET(cast_string_realloc(set->ctx, 1, str, used, size));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200431 strcpy(*str + (*used - 1), "\n");
432 ++(*used);
433
434 ++indent;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200435
Michal Vasko47da6562022-07-14 15:43:15 +0200436 /* print all the top-level nodes */
437 child = NULL;
438 child_type = 0;
439 while (!moveto_axis_node_next(&child, &child_type, NULL, set->root_type, LYXP_AXIS_CHILD, set)) {
440 LY_CHECK_RET(cast_string_recursive(child, set, indent, str, used, size));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200441 }
442
Michal Vasko47da6562022-07-14 15:43:15 +0200443 /* end fake container */
444 LY_CHECK_RET(cast_string_realloc(set->ctx, 1, str, used, size));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200445 strcpy(*str + (*used - 1), "\n");
446 ++(*used);
447
448 --indent;
Michal Vasko47da6562022-07-14 15:43:15 +0200449 } else {
450 switch (node->schema->nodetype) {
451 case LYS_CONTAINER:
452 case LYS_LIST:
453 case LYS_RPC:
454 case LYS_NOTIF:
455 LY_CHECK_RET(cast_string_realloc(set->ctx, 1, str, used, size));
456 strcpy(*str + (*used - 1), "\n");
457 ++(*used);
458
459 for (child = lyd_child(node); child; child = child->next) {
460 LY_CHECK_RET(cast_string_recursive(child, set, indent + 1, str, used, size));
461 }
462
463 break;
464
465 case LYS_LEAF:
466 case LYS_LEAFLIST:
467 value_str = lyd_get_value(node);
468
469 /* print indent */
470 LY_CHECK_RET(cast_string_realloc(set->ctx, indent * 2 + strlen(value_str) + 1, str, used, size));
471 memset(*str + (*used - 1), ' ', indent * 2);
472 *used += indent * 2;
473
474 /* print value */
475 if (*used == 1) {
476 sprintf(*str + (*used - 1), "%s", value_str);
477 *used += strlen(value_str);
478 } else {
479 sprintf(*str + (*used - 1), "%s\n", value_str);
480 *used += strlen(value_str) + 1;
481 }
482
483 break;
484
485 case LYS_ANYXML:
486 case LYS_ANYDATA:
487 any = (struct lyd_node_any *)node;
488 if (!(void *)any->value.tree) {
489 /* no content */
490 buf = strdup("");
491 LY_CHECK_ERR_RET(!buf, LOGMEM(set->ctx), LY_EMEM);
492 } else {
493 struct ly_out *out;
494
495 if (any->value_type == LYD_ANYDATA_LYB) {
496 /* try to parse it into a data tree */
497 if (lyd_parse_data_mem((struct ly_ctx *)set->ctx, any->value.mem, LYD_LYB,
498 LYD_PARSE_ONLY | LYD_PARSE_STRICT, 0, &tree) == LY_SUCCESS) {
499 /* successfully parsed */
500 free(any->value.mem);
501 any->value.tree = tree;
502 any->value_type = LYD_ANYDATA_DATATREE;
503 }
504 /* error is covered by the following switch where LYD_ANYDATA_LYB causes failure */
505 }
506
507 switch (any->value_type) {
508 case LYD_ANYDATA_STRING:
509 case LYD_ANYDATA_XML:
510 case LYD_ANYDATA_JSON:
511 buf = strdup(any->value.json);
512 LY_CHECK_ERR_RET(!buf, LOGMEM(set->ctx), LY_EMEM);
513 break;
514 case LYD_ANYDATA_DATATREE:
515 LY_CHECK_RET(ly_out_new_memory(&buf, 0, &out));
516 rc = lyd_print_all(out, any->value.tree, LYD_XML, 0);
517 ly_out_free(out, NULL, 0);
518 LY_CHECK_RET(rc < 0, -rc);
519 break;
520 case LYD_ANYDATA_LYB:
521 LOGERR(set->ctx, LY_EINVAL, "Cannot convert LYB anydata into string.");
522 return LY_EINVAL;
523 }
524 }
525
526 line = strtok_r(buf, "\n", &ptr);
527 do {
528 rc = cast_string_realloc(set->ctx, indent * 2 + strlen(line) + 1, str, used, size);
529 if (rc != LY_SUCCESS) {
530 free(buf);
531 return rc;
532 }
533 memset(*str + (*used - 1), ' ', indent * 2);
534 *used += indent * 2;
535
536 strcpy(*str + (*used - 1), line);
537 *used += strlen(line);
538
539 strcpy(*str + (*used - 1), "\n");
540 *used += 1;
541 } while ((line = strtok_r(NULL, "\n", &ptr)));
542
543 free(buf);
544 break;
545
546 default:
547 LOGINT_RET(set->ctx);
548 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200549 }
550
551 return LY_SUCCESS;
552}
553
554/**
555 * @brief Cast an element into a string.
556 *
Michal Vasko47da6562022-07-14 15:43:15 +0200557 * @param[in] node Node to cast, NULL if root.
558 * @param[in] set XPath set.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200559 * @param[out] str Element cast to dynamically-allocated string.
560 * @return LY_ERR
561 */
562static LY_ERR
Michal Vasko47da6562022-07-14 15:43:15 +0200563cast_string_elem(const struct lyd_node *node, struct lyxp_set *set, char **str)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200564{
Michal Vaskodd528af2022-08-08 14:35:07 +0200565 uint32_t used, size;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200566 LY_ERR rc;
567
568 *str = malloc(LYXP_STRING_CAST_SIZE_START * sizeof(char));
Michal Vasko47da6562022-07-14 15:43:15 +0200569 LY_CHECK_ERR_RET(!*str, LOGMEM(set->ctx), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200570 (*str)[0] = '\0';
571 used = 1;
572 size = LYXP_STRING_CAST_SIZE_START;
573
Michal Vasko47da6562022-07-14 15:43:15 +0200574 rc = cast_string_recursive(node, set, 0, str, &used, &size);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200575 if (rc != LY_SUCCESS) {
576 free(*str);
577 return rc;
578 }
579
580 if (size > used) {
581 *str = ly_realloc(*str, used * sizeof(char));
Michal Vasko47da6562022-07-14 15:43:15 +0200582 LY_CHECK_ERR_RET(!*str, LOGMEM(set->ctx), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200583 }
584 return LY_SUCCESS;
585}
586
587/**
588 * @brief Cast a LYXP_SET_NODE_SET set into a string.
589 * Context position aware.
590 *
591 * @param[in] set Set to cast.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200592 * @param[out] str Cast dynamically-allocated string.
593 * @return LY_ERR
594 */
595static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100596cast_node_set_to_string(struct lyxp_set *set, char **str)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200597{
Michal Vaskoaa677062021-03-09 13:52:53 +0100598 if (!set->used) {
599 *str = strdup("");
600 if (!*str) {
601 LOGMEM_RET(set->ctx);
602 }
603 return LY_SUCCESS;
604 }
605
Michal Vasko03ff5a72019-09-11 13:49:33 +0200606 switch (set->val.nodes[0].type) {
Michal Vasko2caefc12019-11-14 16:07:56 +0100607 case LYXP_NODE_NONE:
608 /* invalid */
609 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200610 case LYXP_NODE_ROOT:
611 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200612 case LYXP_NODE_ELEM:
613 case LYXP_NODE_TEXT:
Michal Vasko47da6562022-07-14 15:43:15 +0200614 return cast_string_elem(set->val.nodes[0].node, set, str);
Michal Vasko9f96a052020-03-10 09:41:45 +0100615 case LYXP_NODE_META:
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200616 *str = strdup(lyd_get_meta_value(set->val.meta[0].meta));
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200617 if (!*str) {
618 LOGMEM_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200619 }
620 return LY_SUCCESS;
621 }
622
623 LOGINT_RET(set->ctx);
624}
625
626/**
627 * @brief Cast a string into an XPath number.
628 *
629 * @param[in] str String to use.
630 * @return Cast number.
631 */
632static long double
633cast_string_to_number(const char *str)
634{
635 long double num;
636 char *ptr;
637
638 errno = 0;
639 num = strtold(str, &ptr);
640 if (errno || *ptr) {
641 num = NAN;
642 }
643 return num;
644}
645
646/**
647 * @brief Callback for checking value equality.
648 *
Michal Vasko62524a92021-02-26 10:08:50 +0100649 * Implementation of ::lyht_value_equal_cb.
Radek Krejci857189e2020-09-01 13:26:36 +0200650 *
Michal Vasko03ff5a72019-09-11 13:49:33 +0200651 * @param[in] val1_p First value.
652 * @param[in] val2_p Second value.
653 * @param[in] mod Whether hash table is being modified.
654 * @param[in] cb_data Callback data.
Radek Krejci857189e2020-09-01 13:26:36 +0200655 * @return Boolean value whether values are equal or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200656 */
Radek Krejci857189e2020-09-01 13:26:36 +0200657static ly_bool
658set_values_equal_cb(void *val1_p, void *val2_p, ly_bool UNUSED(mod), void *UNUSED(cb_data))
Michal Vasko03ff5a72019-09-11 13:49:33 +0200659{
660 struct lyxp_set_hash_node *val1, *val2;
661
662 val1 = (struct lyxp_set_hash_node *)val1_p;
663 val2 = (struct lyxp_set_hash_node *)val2_p;
664
665 if ((val1->node == val2->node) && (val1->type == val2->type)) {
666 return 1;
667 }
668
669 return 0;
670}
671
672/**
673 * @brief Insert node and its hash into set.
674 *
675 * @param[in] set et to insert to.
676 * @param[in] node Node with hash.
677 * @param[in] type Node type.
678 */
679static void
680set_insert_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type)
681{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200682 LY_ERR r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200683 uint32_t i, hash;
684 struct lyxp_set_hash_node hnode;
685
686 if (!set->ht && (set->used >= LYD_HT_MIN_ITEMS)) {
687 /* create hash table and add all the nodes */
688 set->ht = lyht_new(1, sizeof(struct lyxp_set_hash_node), set_values_equal_cb, NULL, 1);
689 for (i = 0; i < set->used; ++i) {
690 hnode.node = set->val.nodes[i].node;
691 hnode.type = set->val.nodes[i].type;
692
693 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
694 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
695 hash = dict_hash_multi(hash, NULL, 0);
696
697 r = lyht_insert(set->ht, &hnode, hash, NULL);
698 assert(!r);
699 (void)r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200700
Michal Vasko9d6befd2019-12-11 14:56:56 +0100701 if (hnode.node == node) {
702 /* it was just added, do not add it twice */
703 node = NULL;
704 }
705 }
706 }
707
708 if (set->ht && node) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200709 /* add the new node into hash table */
710 hnode.node = node;
711 hnode.type = type;
712
713 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
714 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
715 hash = dict_hash_multi(hash, NULL, 0);
716
717 r = lyht_insert(set->ht, &hnode, hash, NULL);
718 assert(!r);
719 (void)r;
720 }
721}
722
723/**
724 * @brief Remove node and its hash from set.
725 *
726 * @param[in] set Set to remove from.
727 * @param[in] node Node to remove.
728 * @param[in] type Node type.
729 */
730static void
731set_remove_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type)
732{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200733 LY_ERR r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200734 struct lyxp_set_hash_node hnode;
735 uint32_t hash;
736
737 if (set->ht) {
738 hnode.node = node;
739 hnode.type = type;
740
741 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
742 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
743 hash = dict_hash_multi(hash, NULL, 0);
744
745 r = lyht_remove(set->ht, &hnode, hash);
746 assert(!r);
747 (void)r;
748
749 if (!set->ht->used) {
750 lyht_free(set->ht);
751 set->ht = NULL;
752 }
753 }
754}
755
756/**
757 * @brief Check whether node is in set based on its hash.
758 *
759 * @param[in] set Set to search in.
760 * @param[in] node Node to search for.
761 * @param[in] type Node type.
762 * @param[in] skip_idx Index in @p set to skip.
763 * @return LY_ERR
764 */
765static LY_ERR
766set_dup_node_hash_check(const struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type, int skip_idx)
767{
768 struct lyxp_set_hash_node hnode, *match_p;
769 uint32_t hash;
770
771 hnode.node = node;
772 hnode.type = type;
773
774 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
775 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
776 hash = dict_hash_multi(hash, NULL, 0);
777
778 if (!lyht_find(set->ht, &hnode, hash, (void **)&match_p)) {
779 if ((skip_idx > -1) && (set->val.nodes[skip_idx].node == match_p->node) && (set->val.nodes[skip_idx].type == match_p->type)) {
780 /* we found it on the index that should be skipped, find another */
781 hnode = *match_p;
782 if (lyht_find_next(set->ht, &hnode, hash, (void **)&match_p)) {
783 /* none other found */
784 return LY_SUCCESS;
785 }
786 }
787
788 return LY_EEXIST;
789 }
790
791 /* not found */
792 return LY_SUCCESS;
793}
794
Michal Vaskod3678892020-05-21 10:06:58 +0200795void
796lyxp_set_free_content(struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200797{
798 if (!set) {
799 return;
800 }
801
802 if (set->type == LYXP_SET_NODE_SET) {
803 free(set->val.nodes);
804 lyht_free(set->ht);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200805 } else if (set->type == LYXP_SET_SCNODE_SET) {
806 free(set->val.scnodes);
Michal Vaskod3678892020-05-21 10:06:58 +0200807 lyht_free(set->ht);
808 } else {
809 if (set->type == LYXP_SET_STRING) {
810 free(set->val.str);
811 }
812 set->type = LYXP_SET_NODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200813 }
Michal Vaskod3678892020-05-21 10:06:58 +0200814
815 set->val.nodes = NULL;
816 set->used = 0;
817 set->size = 0;
818 set->ht = NULL;
819 set->ctx_pos = 0;
aPiecek748da732021-06-01 09:56:43 +0200820 set->ctx_size = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200821}
822
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100823/**
824 * @brief Free dynamically-allocated set.
825 *
826 * @param[in] set Set to free.
827 */
828static void
Michal Vasko03ff5a72019-09-11 13:49:33 +0200829lyxp_set_free(struct lyxp_set *set)
830{
831 if (!set) {
832 return;
833 }
834
Michal Vaskod3678892020-05-21 10:06:58 +0200835 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200836 free(set);
837}
838
839/**
840 * @brief Initialize set context.
841 *
842 * @param[in] new Set to initialize.
843 * @param[in] set Arbitrary initialized set.
844 */
845static void
Michal Vasko4c7763f2020-07-27 17:40:37 +0200846set_init(struct lyxp_set *new, const struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200847{
848 memset(new, 0, sizeof *new);
Michal Vasko02a77382019-09-12 11:47:35 +0200849 if (set) {
Michal Vasko306e2832022-07-25 09:15:17 +0200850 new->non_child_axis = set->non_child_axis;
Michal Vasko02a77382019-09-12 11:47:35 +0200851 new->ctx = set->ctx;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200852 new->cur_node = set->cur_node;
Michal Vasko588112f2019-12-10 14:51:53 +0100853 new->root_type = set->root_type;
Michal Vasko6b26e742020-07-17 15:02:10 +0200854 new->context_op = set->context_op;
Michal Vaskof03ed032020-03-04 13:31:44 +0100855 new->tree = set->tree;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200856 new->cur_mod = set->cur_mod;
Michal Vasko02a77382019-09-12 11:47:35 +0200857 new->format = set->format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200858 new->prefix_data = set->prefix_data;
aPiecekfba75362021-10-07 12:39:48 +0200859 new->vars = set->vars;
Michal Vasko02a77382019-09-12 11:47:35 +0200860 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200861}
862
863/**
864 * @brief Create a deep copy of a set.
865 *
866 * @param[in] set Set to copy.
867 * @return Copy of @p set.
868 */
869static struct lyxp_set *
870set_copy(struct lyxp_set *set)
871{
872 struct lyxp_set *ret;
Michal Vasko1fdd8fa2021-01-08 09:21:45 +0100873 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200874
875 if (!set) {
876 return NULL;
877 }
878
879 ret = malloc(sizeof *ret);
880 LY_CHECK_ERR_RET(!ret, LOGMEM(set->ctx), NULL);
881 set_init(ret, set);
882
883 if (set->type == LYXP_SET_SCNODE_SET) {
884 ret->type = set->type;
885
886 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100887 if ((set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) ||
888 (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +0200889 uint32_t idx;
Michal Vasko26bbb272022-08-02 14:54:33 +0200890
Michal Vasko7333cb32022-07-29 16:30:29 +0200891 LY_CHECK_ERR_RET(set_scnode_insert_node(ret, set->val.scnodes[i].scnode, set->val.scnodes[i].type,
892 set->val.scnodes[i].axis, &idx), lyxp_set_free(ret), NULL);
Michal Vasko3f27c522020-01-06 08:37:49 +0100893 /* coverity seems to think scnodes can be NULL */
Radek Krejciaa6b53f2020-08-27 15:19:03 +0200894 if (!ret->val.scnodes) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200895 lyxp_set_free(ret);
896 return NULL;
897 }
Michal Vaskoba716542019-12-16 10:01:58 +0100898 ret->val.scnodes[idx].in_ctx = set->val.scnodes[i].in_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200899 }
900 }
901 } else if (set->type == LYXP_SET_NODE_SET) {
902 ret->type = set->type;
Michal Vasko08e9b112021-06-11 15:41:17 +0200903 if (set->used) {
904 ret->val.nodes = malloc(set->used * sizeof *ret->val.nodes);
905 LY_CHECK_ERR_RET(!ret->val.nodes, LOGMEM(set->ctx); free(ret), NULL);
906 memcpy(ret->val.nodes, set->val.nodes, set->used * sizeof *ret->val.nodes);
907 } else {
908 ret->val.nodes = NULL;
909 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200910
911 ret->used = ret->size = set->used;
912 ret->ctx_pos = set->ctx_pos;
913 ret->ctx_size = set->ctx_size;
Michal Vasko4a04e542021-02-01 08:58:30 +0100914 if (set->ht) {
915 ret->ht = lyht_dup(set->ht);
916 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200917 } else {
Radek Krejci0f969882020-08-21 16:56:47 +0200918 memcpy(ret, set, sizeof *ret);
919 if (set->type == LYXP_SET_STRING) {
920 ret->val.str = strdup(set->val.str);
921 LY_CHECK_ERR_RET(!ret->val.str, LOGMEM(set->ctx); free(ret), NULL);
922 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200923 }
924
925 return ret;
926}
927
928/**
929 * @brief Fill XPath set with a string. Any current data are disposed of.
930 *
931 * @param[in] set Set to fill.
932 * @param[in] string String to fill into \p set.
933 * @param[in] str_len Length of \p string. 0 is a valid value!
934 */
935static void
Michal Vaskodd528af2022-08-08 14:35:07 +0200936set_fill_string(struct lyxp_set *set, const char *string, uint32_t str_len)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200937{
Michal Vaskod3678892020-05-21 10:06:58 +0200938 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200939
940 set->type = LYXP_SET_STRING;
941 if ((str_len == 0) && (string[0] != '\0')) {
942 string = "";
943 }
944 set->val.str = strndup(string, str_len);
945}
946
947/**
948 * @brief Fill XPath set with a number. Any current data are disposed of.
949 *
950 * @param[in] set Set to fill.
951 * @param[in] number Number to fill into \p set.
952 */
953static void
954set_fill_number(struct lyxp_set *set, long double number)
955{
Michal Vaskod3678892020-05-21 10:06:58 +0200956 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200957
958 set->type = LYXP_SET_NUMBER;
959 set->val.num = number;
960}
961
962/**
963 * @brief Fill XPath set with a boolean. Any current data are disposed of.
964 *
965 * @param[in] set Set to fill.
966 * @param[in] boolean Boolean to fill into \p set.
967 */
968static void
Radek Krejci857189e2020-09-01 13:26:36 +0200969set_fill_boolean(struct lyxp_set *set, ly_bool boolean)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200970{
Michal Vaskod3678892020-05-21 10:06:58 +0200971 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200972
973 set->type = LYXP_SET_BOOLEAN;
Michal Vasko004d3152020-06-11 19:59:22 +0200974 set->val.bln = boolean;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200975}
976
977/**
978 * @brief Fill XPath set with the value from another set (deep assign).
979 * Any current data are disposed of.
980 *
981 * @param[in] trg Set to fill.
982 * @param[in] src Source set to copy into \p trg.
983 */
984static void
Michal Vasko4c7763f2020-07-27 17:40:37 +0200985set_fill_set(struct lyxp_set *trg, const struct lyxp_set *src)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200986{
987 if (!trg || !src) {
988 return;
989 }
990
991 if (trg->type == LYXP_SET_NODE_SET) {
992 free(trg->val.nodes);
993 } else if (trg->type == LYXP_SET_STRING) {
994 free(trg->val.str);
995 }
996 set_init(trg, src);
997
998 if (src->type == LYXP_SET_SCNODE_SET) {
999 trg->type = LYXP_SET_SCNODE_SET;
1000 trg->used = src->used;
1001 trg->size = src->used;
1002
Michal Vasko08e9b112021-06-11 15:41:17 +02001003 if (trg->size) {
1004 trg->val.scnodes = ly_realloc(trg->val.scnodes, trg->size * sizeof *trg->val.scnodes);
1005 LY_CHECK_ERR_RET(!trg->val.scnodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), );
1006 memcpy(trg->val.scnodes, src->val.scnodes, src->used * sizeof *src->val.scnodes);
1007 } else {
1008 trg->val.scnodes = NULL;
1009 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001010 } else if (src->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +02001011 set_fill_boolean(trg, src->val.bln);
Michal Vasko44f3d2c2020-08-24 09:49:38 +02001012 } else if (src->type == LYXP_SET_NUMBER) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001013 set_fill_number(trg, src->val.num);
1014 } else if (src->type == LYXP_SET_STRING) {
1015 set_fill_string(trg, src->val.str, strlen(src->val.str));
1016 } else {
1017 if (trg->type == LYXP_SET_NODE_SET) {
1018 free(trg->val.nodes);
1019 } else if (trg->type == LYXP_SET_STRING) {
1020 free(trg->val.str);
1021 }
1022
Michal Vaskod3678892020-05-21 10:06:58 +02001023 assert(src->type == LYXP_SET_NODE_SET);
1024
1025 trg->type = LYXP_SET_NODE_SET;
1026 trg->used = src->used;
1027 trg->size = src->used;
1028 trg->ctx_pos = src->ctx_pos;
1029 trg->ctx_size = src->ctx_size;
1030
Michal Vasko08e9b112021-06-11 15:41:17 +02001031 if (trg->size) {
1032 trg->val.nodes = malloc(trg->size * sizeof *trg->val.nodes);
1033 LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), );
1034 memcpy(trg->val.nodes, src->val.nodes, src->used * sizeof *src->val.nodes);
1035 } else {
1036 trg->val.nodes = NULL;
1037 }
Michal Vaskod3678892020-05-21 10:06:58 +02001038 if (src->ht) {
1039 trg->ht = lyht_dup(src->ht);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001040 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02001041 trg->ht = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001042 }
1043 }
1044}
1045
1046/**
1047 * @brief Clear context of all schema nodes.
1048 *
1049 * @param[in] set Set to clear.
Michal Vasko1a09b212021-05-06 13:00:10 +02001050 * @param[in] new_ctx New context state for all the nodes currently in the context.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001051 */
1052static void
Michal Vasko1a09b212021-05-06 13:00:10 +02001053set_scnode_clear_ctx(struct lyxp_set *set, int32_t new_ctx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001054{
1055 uint32_t i;
1056
1057 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001058 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko1a09b212021-05-06 13:00:10 +02001059 set->val.scnodes[i].in_ctx = new_ctx;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001060 } else if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START) {
1061 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001062 }
1063 }
1064}
1065
1066/**
1067 * @brief Remove a node from a set. Removing last node changes
1068 * set into LYXP_SET_EMPTY. Context position aware.
1069 *
1070 * @param[in] set Set to use.
1071 * @param[in] idx Index from @p set of the node to be removed.
1072 */
1073static void
1074set_remove_node(struct lyxp_set *set, uint32_t idx)
1075{
1076 assert(set && (set->type == LYXP_SET_NODE_SET));
1077 assert(idx < set->used);
1078
1079 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
1080
1081 --set->used;
Michal Vasko08e9b112021-06-11 15:41:17 +02001082 if (idx < set->used) {
1083 memmove(&set->val.nodes[idx], &set->val.nodes[idx + 1], (set->used - idx) * sizeof *set->val.nodes);
1084 } else if (!set->used) {
Michal Vaskod3678892020-05-21 10:06:58 +02001085 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001086 }
1087}
1088
1089/**
Michal Vasko2caefc12019-11-14 16:07:56 +01001090 * @brief Remove a node from a set by setting its type to LYXP_NODE_NONE.
Michal Vasko57eab132019-09-24 11:46:26 +02001091 *
1092 * @param[in] set Set to use.
1093 * @param[in] idx Index from @p set of the node to be removed.
1094 */
1095static void
Michal Vasko2caefc12019-11-14 16:07:56 +01001096set_remove_node_none(struct lyxp_set *set, uint32_t idx)
Michal Vasko57eab132019-09-24 11:46:26 +02001097{
1098 assert(set && (set->type == LYXP_SET_NODE_SET));
1099 assert(idx < set->used);
1100
Michal Vasko2caefc12019-11-14 16:07:56 +01001101 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1102 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
1103 }
1104 set->val.nodes[idx].type = LYXP_NODE_NONE;
Michal Vasko57eab132019-09-24 11:46:26 +02001105}
1106
1107/**
Michal Vasko2caefc12019-11-14 16:07:56 +01001108 * @brief Remove all LYXP_NODE_NONE nodes from a set. Removing last node changes
Michal Vasko57eab132019-09-24 11:46:26 +02001109 * set into LYXP_SET_EMPTY. Context position aware.
1110 *
1111 * @param[in] set Set to consolidate.
1112 */
1113static void
Michal Vasko2caefc12019-11-14 16:07:56 +01001114set_remove_nodes_none(struct lyxp_set *set)
Michal Vasko57eab132019-09-24 11:46:26 +02001115{
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01001116 uint32_t i, orig_used, end = 0;
1117 int64_t start;
Michal Vasko57eab132019-09-24 11:46:26 +02001118
Michal Vaskod3678892020-05-21 10:06:58 +02001119 assert(set);
Michal Vasko57eab132019-09-24 11:46:26 +02001120
1121 orig_used = set->used;
1122 set->used = 0;
Michal Vaskod989ba02020-08-24 10:59:24 +02001123 for (i = 0; i < orig_used; ) {
Michal Vasko57eab132019-09-24 11:46:26 +02001124 start = -1;
1125 do {
Michal Vasko2caefc12019-11-14 16:07:56 +01001126 if ((set->val.nodes[i].type != LYXP_NODE_NONE) && (start == -1)) {
Michal Vasko57eab132019-09-24 11:46:26 +02001127 start = i;
Michal Vasko2caefc12019-11-14 16:07:56 +01001128 } else if ((start > -1) && (set->val.nodes[i].type == LYXP_NODE_NONE)) {
Michal Vasko57eab132019-09-24 11:46:26 +02001129 end = i;
1130 ++i;
1131 break;
1132 }
1133
1134 ++i;
1135 if (i == orig_used) {
1136 end = i;
1137 }
1138 } while (i < orig_used);
1139
1140 if (start > -1) {
1141 /* move the whole chunk of valid nodes together */
1142 if (set->used != (unsigned)start) {
1143 memmove(&set->val.nodes[set->used], &set->val.nodes[start], (end - start) * sizeof *set->val.nodes);
1144 }
1145 set->used += end - start;
1146 }
1147 }
Michal Vasko57eab132019-09-24 11:46:26 +02001148}
1149
1150/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02001151 * @brief Check for duplicates in a node set.
1152 *
1153 * @param[in] set Set to check.
1154 * @param[in] node Node to look for in @p set.
1155 * @param[in] node_type Type of @p node.
1156 * @param[in] skip_idx Index from @p set to skip.
1157 * @return LY_ERR
1158 */
1159static LY_ERR
1160set_dup_node_check(const struct lyxp_set *set, const struct lyd_node *node, enum lyxp_node_type node_type, int skip_idx)
1161{
1162 uint32_t i;
1163
Michal Vasko2caefc12019-11-14 16:07:56 +01001164 if (set->ht && node) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001165 return set_dup_node_hash_check(set, (struct lyd_node *)node, node_type, skip_idx);
1166 }
1167
1168 for (i = 0; i < set->used; ++i) {
1169 if ((skip_idx > -1) && (i == (unsigned)skip_idx)) {
1170 continue;
1171 }
1172
1173 if ((set->val.nodes[i].node == node) && (set->val.nodes[i].type == node_type)) {
1174 return LY_EEXIST;
1175 }
1176 }
1177
1178 return LY_SUCCESS;
1179}
1180
Radek Krejci857189e2020-09-01 13:26:36 +02001181ly_bool
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001182lyxp_set_scnode_contains(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type, int skip_idx,
1183 uint32_t *index_p)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001184{
1185 uint32_t i;
1186
1187 for (i = 0; i < set->used; ++i) {
1188 if ((skip_idx > -1) && (i == (unsigned)skip_idx)) {
1189 continue;
1190 }
1191
1192 if ((set->val.scnodes[i].scnode == node) && (set->val.scnodes[i].type == node_type)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001193 if (index_p) {
1194 *index_p = i;
1195 }
1196 return 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001197 }
1198 }
1199
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001200 return 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001201}
1202
Michal Vaskoecd62de2019-11-13 12:35:11 +01001203void
1204lyxp_set_scnode_merge(struct lyxp_set *set1, struct lyxp_set *set2)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001205{
1206 uint32_t orig_used, i, j;
1207
Michal Vaskod3678892020-05-21 10:06:58 +02001208 assert((set1->type == LYXP_SET_SCNODE_SET) && (set2->type == LYXP_SET_SCNODE_SET));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001209
Michal Vaskod3678892020-05-21 10:06:58 +02001210 if (!set2->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001211 return;
1212 }
1213
Michal Vaskod3678892020-05-21 10:06:58 +02001214 if (!set1->used) {
aPiecekadc1e4f2021-10-07 11:15:12 +02001215 /* release hidden allocated data (lyxp_set.size) */
1216 lyxp_set_free_content(set1);
1217 /* direct copying of the entire structure */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001218 memcpy(set1, set2, sizeof *set1);
1219 return;
1220 }
1221
1222 if (set1->used + set2->used > set1->size) {
1223 set1->size = set1->used + set2->used;
1224 set1->val.scnodes = ly_realloc(set1->val.scnodes, set1->size * sizeof *set1->val.scnodes);
1225 LY_CHECK_ERR_RET(!set1->val.scnodes, LOGMEM(set1->ctx), );
1226 }
1227
1228 orig_used = set1->used;
1229
1230 for (i = 0; i < set2->used; ++i) {
1231 for (j = 0; j < orig_used; ++j) {
1232 /* detect duplicities */
1233 if (set1->val.scnodes[j].scnode == set2->val.scnodes[i].scnode) {
1234 break;
1235 }
1236 }
1237
Michal Vasko0587bce2020-12-10 12:19:21 +01001238 if (j < orig_used) {
1239 /* node is there, but update its status if needed */
1240 if (set1->val.scnodes[j].in_ctx == LYXP_SET_SCNODE_START_USED) {
1241 set1->val.scnodes[j].in_ctx = set2->val.scnodes[i].in_ctx;
Michal Vasko1a09b212021-05-06 13:00:10 +02001242 } else if ((set1->val.scnodes[j].in_ctx == LYXP_SET_SCNODE_ATOM_NODE) &&
1243 (set2->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_VAL)) {
1244 set1->val.scnodes[j].in_ctx = set2->val.scnodes[i].in_ctx;
Michal Vasko0587bce2020-12-10 12:19:21 +01001245 }
1246 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001247 memcpy(&set1->val.scnodes[set1->used], &set2->val.scnodes[i], sizeof *set2->val.scnodes);
1248 ++set1->used;
1249 }
1250 }
1251
Michal Vaskod3678892020-05-21 10:06:58 +02001252 lyxp_set_free_content(set2);
1253 set2->type = LYXP_SET_SCNODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001254}
1255
1256/**
1257 * @brief Insert a node into a set. Context position aware.
1258 *
1259 * @param[in] set Set to use.
1260 * @param[in] node Node to insert to @p set.
1261 * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting.
1262 * @param[in] node_type Node type of @p node.
1263 * @param[in] idx Index in @p set to insert into.
1264 */
1265static void
1266set_insert_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx)
1267{
Michal Vaskod3678892020-05-21 10:06:58 +02001268 assert(set && (set->type == LYXP_SET_NODE_SET));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001269
Michal Vaskod3678892020-05-21 10:06:58 +02001270 if (!set->size) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001271 /* first item */
1272 if (idx) {
1273 /* no real harm done, but it is a bug */
1274 LOGINT(set->ctx);
1275 idx = 0;
1276 }
1277 set->val.nodes = malloc(LYXP_SET_SIZE_START * sizeof *set->val.nodes);
1278 LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), );
1279 set->type = LYXP_SET_NODE_SET;
1280 set->used = 0;
1281 set->size = LYXP_SET_SIZE_START;
1282 set->ctx_pos = 1;
1283 set->ctx_size = 1;
1284 set->ht = NULL;
1285 } else {
1286 /* not an empty set */
1287 if (set->used == set->size) {
1288
1289 /* set is full */
Michal Vasko871df522022-04-06 12:14:41 +02001290 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 +02001291 LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), );
Michal Vasko871df522022-04-06 12:14:41 +02001292 set->size *= LYXP_SET_SIZE_MUL_STEP;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001293 }
1294
1295 if (idx > set->used) {
1296 LOGINT(set->ctx);
1297 idx = set->used;
1298 }
1299
1300 /* make space for the new node */
1301 if (idx < set->used) {
1302 memmove(&set->val.nodes[idx + 1], &set->val.nodes[idx], (set->used - idx) * sizeof *set->val.nodes);
1303 }
1304 }
1305
1306 /* finally assign the value */
1307 set->val.nodes[idx].node = (struct lyd_node *)node;
1308 set->val.nodes[idx].type = node_type;
1309 set->val.nodes[idx].pos = pos;
1310 ++set->used;
1311
Michal Vasko2416fdd2021-10-01 11:07:10 +02001312 /* add into hash table */
1313 set_insert_node_hash(set, (struct lyd_node *)node, node_type);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001314}
1315
Michal Vasko7333cb32022-07-29 16:30:29 +02001316/**
1317 * @brief Insert schema node into set.
1318 *
1319 * @param[in] set Set to insert into.
1320 * @param[in] node Node to insert.
1321 * @param[in] node_type Node type of @p node.
1322 * @param[in] axis Axis that @p node was reached on.
1323 * @param[out] index_p Optional pointer to store index if the inserted @p node.
1324 * @return LY_SUCCESS on success.
1325 * @return LY_EMEM on memory allocation failure.
1326 */
1327static LY_ERR
1328set_scnode_insert_node(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type,
1329 enum lyxp_axis axis, uint32_t *index_p)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001330{
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001331 uint32_t index;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001332
1333 assert(set->type == LYXP_SET_SCNODE_SET);
1334
Michal Vasko871df522022-04-06 12:14:41 +02001335 if (!set->size) {
1336 /* first item */
1337 set->val.scnodes = malloc(LYXP_SET_SIZE_START * sizeof *set->val.scnodes);
1338 LY_CHECK_ERR_RET(!set->val.scnodes, LOGMEM(set->ctx), LY_EMEM);
1339 set->type = LYXP_SET_SCNODE_SET;
1340 set->used = 0;
1341 set->size = LYXP_SET_SIZE_START;
1342 set->ctx_pos = 1;
1343 set->ctx_size = 1;
1344 set->ht = NULL;
1345 }
1346
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001347 if (lyxp_set_scnode_contains(set, node, node_type, -1, &index)) {
Michal Vasko7333cb32022-07-29 16:30:29 +02001348 /* BUG if axes differs, this new one is thrown away */
Radek Krejcif13b87b2020-12-01 22:02:17 +01001349 set->val.scnodes[index].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001350 } else {
1351 if (set->used == set->size) {
Michal Vasko871df522022-04-06 12:14:41 +02001352 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 +02001353 LY_CHECK_ERR_RET(!set->val.scnodes, LOGMEM(set->ctx), LY_EMEM);
Michal Vasko871df522022-04-06 12:14:41 +02001354 set->size *= LYXP_SET_SIZE_MUL_STEP;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001355 }
1356
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001357 index = set->used;
1358 set->val.scnodes[index].scnode = (struct lysc_node *)node;
1359 set->val.scnodes[index].type = node_type;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001360 set->val.scnodes[index].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko7333cb32022-07-29 16:30:29 +02001361 set->val.scnodes[index].axis = axis;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001362 ++set->used;
1363 }
1364
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001365 if (index_p) {
1366 *index_p = index;
1367 }
1368
1369 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001370}
1371
1372/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02001373 * @brief Set all nodes with ctx 1 to a new unique context value.
1374 *
1375 * @param[in] set Set to modify.
1376 * @return New context value.
1377 */
Michal Vasko5c4e5892019-11-14 12:31:38 +01001378static int32_t
Michal Vasko03ff5a72019-09-11 13:49:33 +02001379set_scnode_new_in_ctx(struct lyxp_set *set)
1380{
Michal Vasko5c4e5892019-11-14 12:31:38 +01001381 uint32_t i;
1382 int32_t ret_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001383
1384 assert(set->type == LYXP_SET_SCNODE_SET);
1385
Radek Krejcif13b87b2020-12-01 22:02:17 +01001386 ret_ctx = LYXP_SET_SCNODE_ATOM_PRED_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001387retry:
1388 for (i = 0; i < set->used; ++i) {
1389 if (set->val.scnodes[i].in_ctx >= ret_ctx) {
1390 ret_ctx = set->val.scnodes[i].in_ctx + 1;
1391 goto retry;
1392 }
1393 }
1394 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001395 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001396 set->val.scnodes[i].in_ctx = ret_ctx;
1397 }
1398 }
1399
1400 return ret_ctx;
1401}
1402
1403/**
1404 * @brief Get unique @p node position in the data.
1405 *
1406 * @param[in] node Node to find.
1407 * @param[in] node_type Node type of @p node.
1408 * @param[in] root Root node.
1409 * @param[in] root_type Type of the XPath @p root node.
1410 * @param[in] prev Node that we think is before @p node in DFS from @p root. Can optionally
1411 * be used to increase efficiency and start the DFS from this node.
1412 * @param[in] prev_pos Node @p prev position. Optional, but must be set if @p prev is set.
1413 * @return Node position.
1414 */
1415static uint32_t
1416get_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 +02001417 enum lyxp_node_type root_type, const struct lyd_node **prev, uint32_t *prev_pos)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001418{
Michal Vasko56daf732020-08-10 10:57:18 +02001419 const struct lyd_node *elem = NULL, *top_sibling;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001420 uint32_t pos = 1;
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001421 ly_bool found = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001422
1423 assert(prev && prev_pos && !root->prev->next);
1424
1425 if ((node_type == LYXP_NODE_ROOT) || (node_type == LYXP_NODE_ROOT_CONFIG)) {
1426 return 0;
1427 }
1428
1429 if (*prev) {
1430 /* start from the previous element instead from the root */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001431 pos = *prev_pos;
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001432 for (top_sibling = *prev; top_sibling->parent; top_sibling = lyd_parent(top_sibling)) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001433 goto dfs_search;
1434 }
1435
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001436 LY_LIST_FOR(root, top_sibling) {
Michal Vasko56daf732020-08-10 10:57:18 +02001437 LYD_TREE_DFS_BEGIN(top_sibling, elem) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001438dfs_search:
Michal Vaskoa9309bb2021-07-09 09:31:55 +02001439 LYD_TREE_DFS_continue = 0;
1440
Michal Vasko56daf732020-08-10 10:57:18 +02001441 if (*prev && !elem) {
1442 /* resume previous DFS */
1443 elem = LYD_TREE_DFS_next = (struct lyd_node *)*prev;
1444 LYD_TREE_DFS_continue = 0;
1445 }
1446
Michal Vasko482a6822022-05-06 08:56:12 +02001447 if (!elem->schema || ((root_type == LYXP_NODE_ROOT_CONFIG) && (elem->schema->flags & LYS_CONFIG_R))) {
Michal Vasko56daf732020-08-10 10:57:18 +02001448 /* skip */
1449 LYD_TREE_DFS_continue = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001450 } else {
Michal Vasko56daf732020-08-10 10:57:18 +02001451 if (elem == node) {
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001452 found = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001453 break;
1454 }
Michal Vasko56daf732020-08-10 10:57:18 +02001455 ++pos;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001456 }
Michal Vasko56daf732020-08-10 10:57:18 +02001457
1458 LYD_TREE_DFS_END(top_sibling, elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001459 }
1460
1461 /* node found */
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001462 if (found) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001463 break;
1464 }
1465 }
1466
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001467 if (!found) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001468 if (!(*prev)) {
1469 /* we went from root and failed to find it, cannot be */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001470 LOGINT(LYD_CTX(node));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001471 return 0;
1472 } else {
Michal Vasko56daf732020-08-10 10:57:18 +02001473 /* start the search again from the beginning */
1474 *prev = root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001475
Michal Vasko56daf732020-08-10 10:57:18 +02001476 top_sibling = root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001477 pos = 1;
1478 goto dfs_search;
1479 }
1480 }
1481
1482 /* remember the last found node for next time */
1483 *prev = node;
1484 *prev_pos = pos;
1485
1486 return pos;
1487}
1488
1489/**
1490 * @brief Assign (fill) missing node positions.
1491 *
1492 * @param[in] set Set to fill positions in.
1493 * @param[in] root Context root node.
1494 * @param[in] root_type Context root type.
1495 * @return LY_ERR
1496 */
1497static LY_ERR
1498set_assign_pos(struct lyxp_set *set, const struct lyd_node *root, enum lyxp_node_type root_type)
1499{
1500 const struct lyd_node *prev = NULL, *tmp_node;
1501 uint32_t i, tmp_pos = 0;
1502
1503 for (i = 0; i < set->used; ++i) {
1504 if (!set->val.nodes[i].pos) {
1505 tmp_node = NULL;
1506 switch (set->val.nodes[i].type) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001507 case LYXP_NODE_META:
1508 tmp_node = set->val.meta[i].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001509 if (!tmp_node) {
1510 LOGINT_RET(root->schema->module->ctx);
1511 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01001512 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001513 case LYXP_NODE_ELEM:
1514 case LYXP_NODE_TEXT:
1515 if (!tmp_node) {
1516 tmp_node = set->val.nodes[i].node;
1517 }
1518 set->val.nodes[i].pos = get_node_pos(tmp_node, set->val.nodes[i].type, root, root_type, &prev, &tmp_pos);
1519 break;
1520 default:
1521 /* all roots have position 0 */
1522 break;
1523 }
1524 }
1525 }
1526
1527 return LY_SUCCESS;
1528}
1529
1530/**
Michal Vasko9f96a052020-03-10 09:41:45 +01001531 * @brief Get unique @p meta position in the parent metadata.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001532 *
Michal Vasko9f96a052020-03-10 09:41:45 +01001533 * @param[in] meta Metadata to use.
1534 * @return Metadata position.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001535 */
Michal Vaskodd528af2022-08-08 14:35:07 +02001536static uint32_t
Michal Vasko9f96a052020-03-10 09:41:45 +01001537get_meta_pos(struct lyd_meta *meta)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001538{
Michal Vaskodd528af2022-08-08 14:35:07 +02001539 uint32_t pos = 0;
Michal Vasko9f96a052020-03-10 09:41:45 +01001540 struct lyd_meta *meta2;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001541
Michal Vasko9f96a052020-03-10 09:41:45 +01001542 for (meta2 = meta->parent->meta; meta2 && (meta2 != meta); meta2 = meta2->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001543 ++pos;
1544 }
1545
Michal Vasko9f96a052020-03-10 09:41:45 +01001546 assert(meta2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001547 return pos;
1548}
1549
1550/**
1551 * @brief Compare 2 nodes in respect to XPath document order.
1552 *
1553 * @param[in] item1 1st node.
1554 * @param[in] item2 2nd node.
1555 * @return If 1st > 2nd returns 1, 1st == 2nd returns 0, and 1st < 2nd returns -1.
1556 */
1557static int
1558set_sort_compare(struct lyxp_set_node *item1, struct lyxp_set_node *item2)
1559{
Michal Vasko9f96a052020-03-10 09:41:45 +01001560 uint32_t meta_pos1 = 0, meta_pos2 = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001561
1562 if (item1->pos < item2->pos) {
1563 return -1;
1564 }
1565
1566 if (item1->pos > item2->pos) {
1567 return 1;
1568 }
1569
1570 /* node positions are equal, the fun case */
1571
1572 /* 1st ELEM - == - 2nd TEXT, 1st TEXT - == - 2nd ELEM */
1573 /* special case since text nodes are actually saved as their parents */
1574 if ((item1->node == item2->node) && (item1->type != item2->type)) {
1575 if (item1->type == LYXP_NODE_ELEM) {
1576 assert(item2->type == LYXP_NODE_TEXT);
1577 return -1;
1578 } else {
1579 assert((item1->type == LYXP_NODE_TEXT) && (item2->type == LYXP_NODE_ELEM));
1580 return 1;
1581 }
1582 }
1583
Michal Vasko9f96a052020-03-10 09:41:45 +01001584 /* we need meta positions now */
1585 if (item1->type == LYXP_NODE_META) {
1586 meta_pos1 = get_meta_pos((struct lyd_meta *)item1->node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001587 }
Michal Vasko9f96a052020-03-10 09:41:45 +01001588 if (item2->type == LYXP_NODE_META) {
1589 meta_pos2 = get_meta_pos((struct lyd_meta *)item2->node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001590 }
1591
Michal Vasko9f96a052020-03-10 09:41:45 +01001592 /* 1st ROOT - 2nd ROOT, 1st ELEM - 2nd ELEM, 1st TEXT - 2nd TEXT, 1st META - =pos= - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001593 /* check for duplicates */
1594 if (item1->node == item2->node) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001595 assert((item1->type == item2->type) && ((item1->type != LYXP_NODE_META) || (meta_pos1 == meta_pos2)));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001596 return 0;
1597 }
1598
Michal Vasko9f96a052020-03-10 09:41:45 +01001599 /* 1st ELEM - 2nd TEXT, 1st ELEM - any pos - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001600 /* elem is always first, 2nd node is after it */
1601 if (item1->type == LYXP_NODE_ELEM) {
1602 assert(item2->type != LYXP_NODE_ELEM);
1603 return -1;
1604 }
1605
Michal Vasko9f96a052020-03-10 09:41:45 +01001606 /* 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 +02001607 /* 2nd is before 1st */
Michal Vasko69730152020-10-09 16:30:07 +02001608 if (((item1->type == LYXP_NODE_TEXT) &&
1609 ((item2->type == LYXP_NODE_ELEM) || (item2->type == LYXP_NODE_META))) ||
1610 ((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_ELEM)) ||
1611 (((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_META)) &&
1612 (meta_pos1 > meta_pos2))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001613 return 1;
1614 }
1615
Michal Vasko9f96a052020-03-10 09:41:45 +01001616 /* 1st META - any pos - 2nd TEXT, 1st META <pos< - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001617 /* 2nd is after 1st */
1618 return -1;
1619}
1620
1621/**
1622 * @brief Set cast for comparisons.
1623 *
Michal Vasko8abcecc2022-07-28 09:55:01 +02001624 * @param[in,out] trg Target set to cast source into.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001625 * @param[in] src Source set.
1626 * @param[in] type Target set type.
1627 * @param[in] src_idx Source set node index.
Michal Vasko8abcecc2022-07-28 09:55:01 +02001628 * @return LY_SUCCESS on success.
1629 * @return LY_ERR value on error.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001630 */
1631static LY_ERR
Michal Vasko8abcecc2022-07-28 09:55:01 +02001632set_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 +02001633{
1634 assert(src->type == LYXP_SET_NODE_SET);
1635
1636 set_init(trg, src);
1637
1638 /* insert node into target set */
1639 set_insert_node(trg, src->val.nodes[src_idx].node, src->val.nodes[src_idx].pos, src->val.nodes[src_idx].type, 0);
1640
1641 /* cast target set appropriately */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001642 return lyxp_set_cast(trg, type);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001643}
1644
Michal Vasko4c7763f2020-07-27 17:40:37 +02001645/**
1646 * @brief Set content canonization for comparisons.
1647 *
Michal Vasko8abcecc2022-07-28 09:55:01 +02001648 * @param[in,out] set Set to canonize.
Michal Vasko4c7763f2020-07-27 17:40:37 +02001649 * @param[in] xp_node Source XPath node/meta to use for canonization.
Michal Vasko8abcecc2022-07-28 09:55:01 +02001650 * @return LY_SUCCESS on success.
1651 * @return LY_ERR value on error.
Michal Vasko4c7763f2020-07-27 17:40:37 +02001652 */
1653static LY_ERR
Michal Vasko8abcecc2022-07-28 09:55:01 +02001654set_comp_canonize(struct lyxp_set *set, const struct lyxp_set_node *xp_node)
Michal Vasko4c7763f2020-07-27 17:40:37 +02001655{
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001656 const struct lysc_type *type = NULL;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001657 struct lyd_value val;
1658 struct ly_err_item *err = NULL;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001659 LY_ERR rc;
1660
1661 /* is there anything to canonize even? */
Michal Vasko8abcecc2022-07-28 09:55:01 +02001662 if (set->type == LYXP_SET_STRING) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02001663 /* do we have a type to use for canonization? */
1664 if ((xp_node->type == LYXP_NODE_ELEM) && (xp_node->node->schema->nodetype & LYD_NODE_TERM)) {
1665 type = ((struct lyd_node_term *)xp_node->node)->value.realtype;
1666 } else if (xp_node->type == LYXP_NODE_META) {
1667 type = ((struct lyd_meta *)xp_node->node)->value.realtype;
1668 }
1669 }
1670 if (!type) {
Michal Vasko8abcecc2022-07-28 09:55:01 +02001671 /* no canonization needed/possible */
1672 return LY_SUCCESS;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001673 }
1674
Michal Vasko8abcecc2022-07-28 09:55:01 +02001675 /* check for built-in types without required canonization */
1676 if ((type->basetype == LY_TYPE_STRING) && (type->plugin->store == lyplg_type_store_string)) {
1677 /* string */
1678 return LY_SUCCESS;
1679 }
1680 if ((type->basetype == LY_TYPE_BOOL) && (type->plugin->store == lyplg_type_store_boolean)) {
1681 /* boolean */
1682 return LY_SUCCESS;
1683 }
1684 if ((type->basetype == LY_TYPE_ENUM) && (type->plugin->store == lyplg_type_store_enum)) {
1685 /* enumeration */
1686 return LY_SUCCESS;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001687 }
1688
Michal Vasko8abcecc2022-07-28 09:55:01 +02001689 /* print canonized string, ignore errors, the value may not satisfy schema constraints */
1690 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 +01001691 LYD_HINT_DATA, xp_node->node->schema, &val, NULL, &err);
Michal Vasko4c7763f2020-07-27 17:40:37 +02001692 ly_err_free(err);
1693 if (rc) {
Michal Vasko8abcecc2022-07-28 09:55:01 +02001694 /* invalid value, function store automaticaly dealloc value when fail */
1695 return LY_SUCCESS;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001696 }
1697
Michal Vasko8abcecc2022-07-28 09:55:01 +02001698 /* use the canonized string value */
1699 free(set->val.str);
1700 set->val.str = strdup(lyd_value_get_canonical(set->ctx, &val));
1701 type->plugin->free(set->ctx, &val);
1702 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), LY_EMEM);
Michal Vasko4c7763f2020-07-27 17:40:37 +02001703
Michal Vasko4c7763f2020-07-27 17:40:37 +02001704 return LY_SUCCESS;
1705}
1706
Michal Vasko03ff5a72019-09-11 13:49:33 +02001707/**
1708 * @brief Bubble sort @p set into XPath document order.
Michal Vasko49fec8e2022-05-24 10:28:33 +02001709 * Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001710 *
1711 * @param[in] set Set to sort.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001712 * @return How many times the whole set was traversed - 1 (if set was sorted, returns 0).
1713 */
1714static int
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001715set_sort(struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001716{
1717 uint32_t i, j;
Radek Krejci1deb5be2020-08-26 16:43:36 +02001718 int ret = 0, cmp;
Radek Krejci857189e2020-09-01 13:26:36 +02001719 ly_bool inverted, change;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001720 const struct lyd_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001721 struct lyxp_set_node item;
1722 struct lyxp_set_hash_node hnode;
1723 uint64_t hash;
1724
Michal Vasko3cf8fbf2020-05-27 15:21:21 +02001725 if ((set->type != LYXP_SET_NODE_SET) || (set->used < 2)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001726 return 0;
1727 }
1728
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001729 /* find first top-level node to be used as anchor for positions */
Michal Vasko4a7d4d62021-12-13 17:05:06 +01001730 for (root = set->tree; root->parent; root = lyd_parent(root)) {}
Michal Vaskod989ba02020-08-24 10:59:24 +02001731 for ( ; root->prev->next; root = root->prev) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001732
1733 /* fill positions */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001734 if (set_assign_pos(set, root, set->root_type)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001735 return -1;
1736 }
1737
Michal Vasko88a9e802022-05-24 10:50:28 +02001738#ifndef NDEBUG
Michal Vasko03ff5a72019-09-11 13:49:33 +02001739 LOGDBG(LY_LDGXPATH, "SORT BEGIN");
1740 print_set_debug(set);
Michal Vasko88a9e802022-05-24 10:50:28 +02001741#endif
Michal Vasko03ff5a72019-09-11 13:49:33 +02001742
1743 for (i = 0; i < set->used; ++i) {
1744 inverted = 0;
1745 change = 0;
1746
1747 for (j = 1; j < set->used - i; ++j) {
1748 /* compare node positions */
1749 if (inverted) {
1750 cmp = set_sort_compare(&set->val.nodes[j], &set->val.nodes[j - 1]);
1751 } else {
1752 cmp = set_sort_compare(&set->val.nodes[j - 1], &set->val.nodes[j]);
1753 }
1754
1755 /* swap if needed */
1756 if ((inverted && (cmp < 0)) || (!inverted && (cmp > 0))) {
1757 change = 1;
1758
1759 item = set->val.nodes[j - 1];
1760 set->val.nodes[j - 1] = set->val.nodes[j];
1761 set->val.nodes[j] = item;
1762 } else {
1763 /* whether node_pos1 should be smaller than node_pos2 or the other way around */
1764 inverted = !inverted;
1765 }
1766 }
1767
1768 ++ret;
1769
1770 if (!change) {
1771 break;
1772 }
1773 }
1774
Michal Vasko88a9e802022-05-24 10:50:28 +02001775#ifndef NDEBUG
Michal Vasko03ff5a72019-09-11 13:49:33 +02001776 LOGDBG(LY_LDGXPATH, "SORT END %d", ret);
1777 print_set_debug(set);
Michal Vasko88a9e802022-05-24 10:50:28 +02001778#endif
Michal Vasko03ff5a72019-09-11 13:49:33 +02001779
1780 /* check node hashes */
1781 if (set->used >= LYD_HT_MIN_ITEMS) {
1782 assert(set->ht);
1783 for (i = 0; i < set->used; ++i) {
1784 hnode.node = set->val.nodes[i].node;
1785 hnode.type = set->val.nodes[i].type;
1786
1787 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
1788 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
1789 hash = dict_hash_multi(hash, NULL, 0);
1790
1791 assert(!lyht_find(set->ht, &hnode, hash, NULL));
1792 }
1793 }
1794
1795 return ret - 1;
1796}
1797
Michal Vasko03ff5a72019-09-11 13:49:33 +02001798/**
1799 * @brief Merge 2 sorted sets into one.
1800 *
1801 * @param[in,out] trg Set to merge into. Duplicates are removed.
1802 * @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 +02001803 * @return LY_ERR
1804 */
1805static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001806set_sorted_merge(struct lyxp_set *trg, struct lyxp_set *src)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001807{
1808 uint32_t i, j, k, count, dup_count;
1809 int cmp;
1810 const struct lyd_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001811
Michal Vaskod3678892020-05-21 10:06:58 +02001812 if ((trg->type != LYXP_SET_NODE_SET) || (src->type != LYXP_SET_NODE_SET)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001813 return LY_EINVAL;
1814 }
1815
Michal Vaskod3678892020-05-21 10:06:58 +02001816 if (!src->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001817 return LY_SUCCESS;
Michal Vaskod3678892020-05-21 10:06:58 +02001818 } else if (!trg->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001819 set_fill_set(trg, src);
Michal Vaskod3678892020-05-21 10:06:58 +02001820 lyxp_set_free_content(src);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001821 return LY_SUCCESS;
1822 }
1823
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001824 /* find first top-level node to be used as anchor for positions */
Michal Vasko0143b682021-12-13 17:13:09 +01001825 for (root = trg->tree; root->parent; root = lyd_parent(root)) {}
Michal Vaskod989ba02020-08-24 10:59:24 +02001826 for ( ; root->prev->next; root = root->prev) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001827
1828 /* fill positions */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001829 if (set_assign_pos(trg, root, trg->root_type) || set_assign_pos(src, root, src->root_type)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001830 return LY_EINT;
1831 }
1832
1833#ifndef NDEBUG
1834 LOGDBG(LY_LDGXPATH, "MERGE target");
1835 print_set_debug(trg);
1836 LOGDBG(LY_LDGXPATH, "MERGE source");
1837 print_set_debug(src);
1838#endif
1839
1840 /* make memory for the merge (duplicates are not detected yet, so space
1841 * will likely be wasted on them, too bad) */
1842 if (trg->size - trg->used < src->used) {
1843 trg->size = trg->used + src->used;
1844
1845 trg->val.nodes = ly_realloc(trg->val.nodes, trg->size * sizeof *trg->val.nodes);
1846 LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx), LY_EMEM);
1847 }
1848
1849 i = 0;
1850 j = 0;
1851 count = 0;
1852 dup_count = 0;
1853 do {
1854 cmp = set_sort_compare(&src->val.nodes[i], &trg->val.nodes[j]);
1855 if (!cmp) {
1856 if (!count) {
1857 /* duplicate, just skip it */
1858 ++i;
1859 ++j;
1860 } else {
1861 /* we are copying something already, so let's copy the duplicate too,
1862 * we are hoping that afterwards there are some more nodes to
1863 * copy and this way we can copy them all together */
1864 ++count;
1865 ++dup_count;
1866 ++i;
1867 ++j;
1868 }
1869 } else if (cmp < 0) {
1870 /* inserting src node into trg, just remember it for now */
1871 ++count;
1872 ++i;
1873
1874 /* insert the hash now */
1875 set_insert_node_hash(trg, src->val.nodes[i - 1].node, src->val.nodes[i - 1].type);
1876 } else if (count) {
1877copy_nodes:
1878 /* time to actually copy the nodes, we have found the largest block of nodes */
1879 memmove(&trg->val.nodes[j + (count - dup_count)],
1880 &trg->val.nodes[j],
1881 (trg->used - j) * sizeof *trg->val.nodes);
1882 memcpy(&trg->val.nodes[j - dup_count], &src->val.nodes[i - count], count * sizeof *src->val.nodes);
1883
1884 trg->used += count - dup_count;
1885 /* do not change i, except the copying above, we are basically doing exactly what is in the else branch below */
1886 j += count - dup_count;
1887
1888 count = 0;
1889 dup_count = 0;
1890 } else {
1891 ++j;
1892 }
1893 } while ((i < src->used) && (j < trg->used));
1894
1895 if ((i < src->used) || count) {
1896 /* insert all the hashes first */
1897 for (k = i; k < src->used; ++k) {
1898 set_insert_node_hash(trg, src->val.nodes[k].node, src->val.nodes[k].type);
1899 }
1900
1901 /* loop ended, but we need to copy something at trg end */
1902 count += src->used - i;
1903 i = src->used;
1904 goto copy_nodes;
1905 }
1906
1907 /* we are inserting hashes before the actual node insert, which causes
1908 * situations when there were initially not enough items for a hash table,
1909 * but even after some were inserted, hash table was not created (during
1910 * insertion the number of items is not updated yet) */
1911 if (!trg->ht && (trg->used >= LYD_HT_MIN_ITEMS)) {
1912 set_insert_node_hash(trg, NULL, 0);
1913 }
1914
1915#ifndef NDEBUG
1916 LOGDBG(LY_LDGXPATH, "MERGE result");
1917 print_set_debug(trg);
1918#endif
1919
Michal Vaskod3678892020-05-21 10:06:58 +02001920 lyxp_set_free_content(src);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001921 return LY_SUCCESS;
1922}
1923
Michal Vasko14676352020-05-29 11:35:55 +02001924LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02001925lyxp_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 +02001926{
Michal Vasko004d3152020-06-11 19:59:22 +02001927 if (exp->used == tok_idx) {
Michal Vasko14676352020-05-29 11:35:55 +02001928 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001929 LOGVAL(ctx, LY_VCODE_XP_EOF);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001930 }
Michal Vasko14676352020-05-29 11:35:55 +02001931 return LY_EINCOMPLETE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001932 }
1933
Michal Vasko004d3152020-06-11 19:59:22 +02001934 if (want_tok && (exp->tokens[tok_idx] != want_tok)) {
Michal Vasko14676352020-05-29 11:35:55 +02001935 if (ctx) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02001936 LOGVAL(ctx, LY_VCODE_XP_INTOK2, lyxp_token2str(exp->tokens[tok_idx]),
1937 &exp->expr[exp->tok_pos[tok_idx]], lyxp_token2str(want_tok));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001938 }
Michal Vasko14676352020-05-29 11:35:55 +02001939 return LY_ENOT;
1940 }
1941
1942 return LY_SUCCESS;
1943}
1944
Michal Vasko004d3152020-06-11 19:59:22 +02001945LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02001946lyxp_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 +02001947{
1948 LY_CHECK_RET(lyxp_check_token(ctx, exp, *tok_idx, want_tok));
1949
1950 /* skip the token */
1951 ++(*tok_idx);
1952
1953 return LY_SUCCESS;
1954}
1955
Michal Vasko14676352020-05-29 11:35:55 +02001956/* just like lyxp_check_token() but tests for 2 tokens */
1957static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02001958exp_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 +02001959 enum lyxp_token want_tok2)
Michal Vasko14676352020-05-29 11:35:55 +02001960{
Michal Vasko004d3152020-06-11 19:59:22 +02001961 if (exp->used == tok_idx) {
Michal Vasko14676352020-05-29 11:35:55 +02001962 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001963 LOGVAL(ctx, LY_VCODE_XP_EOF);
Michal Vasko14676352020-05-29 11:35:55 +02001964 }
1965 return LY_EINCOMPLETE;
1966 }
1967
Michal Vasko004d3152020-06-11 19:59:22 +02001968 if ((exp->tokens[tok_idx] != want_tok1) && (exp->tokens[tok_idx] != want_tok2)) {
Michal Vasko14676352020-05-29 11:35:55 +02001969 if (ctx) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02001970 LOGVAL(ctx, LY_VCODE_XP_INTOK, lyxp_token2str(exp->tokens[tok_idx]),
Michal Vasko0b468e62020-10-19 09:33:04 +02001971 &exp->expr[exp->tok_pos[tok_idx]]);
Michal Vasko14676352020-05-29 11:35:55 +02001972 }
1973 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001974 }
1975
1976 return LY_SUCCESS;
1977}
1978
Michal Vasko4911eeb2021-06-28 11:23:05 +02001979LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02001980lyxp_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 +02001981 enum lyxp_token want_tok2)
1982{
1983 LY_CHECK_RET(exp_check_token2(ctx, exp, *tok_idx, want_tok1, want_tok2));
1984
1985 /* skip the token */
1986 ++(*tok_idx);
1987
1988 return LY_SUCCESS;
1989}
1990
Michal Vasko03ff5a72019-09-11 13:49:33 +02001991/**
1992 * @brief Stack operation push on the repeat array.
1993 *
1994 * @param[in] exp Expression to use.
Michal Vasko004d3152020-06-11 19:59:22 +02001995 * @param[in] tok_idx Position in the expresion \p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001996 * @param[in] repeat_op_idx Index from \p exp of the operator token. This value is pushed.
1997 */
1998static void
Michal Vaskodd528af2022-08-08 14:35:07 +02001999exp_repeat_push(struct lyxp_expr *exp, uint32_t tok_idx, uint32_t repeat_op_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002000{
Michal Vaskodd528af2022-08-08 14:35:07 +02002001 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002002
Michal Vasko004d3152020-06-11 19:59:22 +02002003 if (exp->repeat[tok_idx]) {
Radek Krejci1e008d22020-08-17 11:37:37 +02002004 for (i = 0; exp->repeat[tok_idx][i]; ++i) {}
Michal Vasko004d3152020-06-11 19:59:22 +02002005 exp->repeat[tok_idx] = realloc(exp->repeat[tok_idx], (i + 2) * sizeof *exp->repeat[tok_idx]);
2006 LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), );
2007 exp->repeat[tok_idx][i] = repeat_op_idx;
2008 exp->repeat[tok_idx][i + 1] = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002009 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02002010 exp->repeat[tok_idx] = calloc(2, sizeof *exp->repeat[tok_idx]);
2011 LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), );
2012 exp->repeat[tok_idx][0] = repeat_op_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002013 }
2014}
2015
2016/**
2017 * @brief Reparse Predicate. Logs directly on error.
2018 *
2019 * [7] Predicate ::= '[' Expr ']'
2020 *
2021 * @param[in] ctx Context for logging.
2022 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002023 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002024 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002025 * @return LY_ERR
2026 */
2027static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02002028reparse_predicate(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint32_t *tok_idx, uint32_t depth)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002029{
2030 LY_ERR rc;
2031
Michal Vasko004d3152020-06-11 19:59:22 +02002032 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002033 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002034 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002035
aPiecekbf968d92021-05-27 14:35:05 +02002036 rc = reparse_or_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002037 LY_CHECK_RET(rc);
2038
Michal Vasko004d3152020-06-11 19:59:22 +02002039 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002040 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002041 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002042
2043 return LY_SUCCESS;
2044}
2045
2046/**
2047 * @brief Reparse RelativeLocationPath. Logs directly on error.
2048 *
2049 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
2050 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
2051 * [6] NodeTest ::= NameTest | NodeType '(' ')'
2052 *
2053 * @param[in] ctx Context for logging.
2054 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002055 * @param[in] tok_idx Position in the expression \p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002056 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002057 * @return LY_ERR (LY_EINCOMPLETE on forward reference)
2058 */
2059static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02002060reparse_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 +02002061{
2062 LY_ERR rc;
2063
Michal Vasko004d3152020-06-11 19:59:22 +02002064 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002065 LY_CHECK_RET(rc);
2066
2067 goto step;
2068 do {
2069 /* '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02002070 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002071
Michal Vasko004d3152020-06-11 19:59:22 +02002072 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002073 LY_CHECK_RET(rc);
2074step:
2075 /* Step */
Michal Vasko004d3152020-06-11 19:59:22 +02002076 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002077 case LYXP_TOKEN_DOT:
Michal Vasko004d3152020-06-11 19:59:22 +02002078 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002079 break;
2080
2081 case LYXP_TOKEN_DDOT:
Michal Vasko004d3152020-06-11 19:59:22 +02002082 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002083 break;
2084
Michal Vasko49fec8e2022-05-24 10:28:33 +02002085 case LYXP_TOKEN_AXISNAME:
2086 ++(*tok_idx);
2087
2088 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_DCOLON);
2089 LY_CHECK_RET(rc);
2090
2091 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02002092 case LYXP_TOKEN_AT:
Michal Vasko004d3152020-06-11 19:59:22 +02002093 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002094
Michal Vasko004d3152020-06-11 19:59:22 +02002095 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002096 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002097 if ((exp->tokens[*tok_idx] != LYXP_TOKEN_NAMETEST) && (exp->tokens[*tok_idx] != LYXP_TOKEN_NODETYPE)) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02002098 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 +02002099 return LY_EVALID;
2100 }
Michal Vasko49fec8e2022-05-24 10:28:33 +02002101 if (exp->tokens[*tok_idx] == LYXP_TOKEN_NODETYPE) {
2102 goto reparse_nodetype;
2103 }
Radek Krejci0f969882020-08-21 16:56:47 +02002104 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02002105 case LYXP_TOKEN_NAMETEST:
Michal Vasko004d3152020-06-11 19:59:22 +02002106 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002107 goto reparse_predicate;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002108
2109 case LYXP_TOKEN_NODETYPE:
Michal Vasko49fec8e2022-05-24 10:28:33 +02002110reparse_nodetype:
Michal Vasko004d3152020-06-11 19:59:22 +02002111 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002112
2113 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02002114 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002115 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002116 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002117
2118 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02002119 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002120 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002121 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002122
2123reparse_predicate:
2124 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002125 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
aPiecekbf968d92021-05-27 14:35:05 +02002126 rc = reparse_predicate(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002127 LY_CHECK_RET(rc);
2128 }
2129 break;
2130 default:
Michal Vasko49fec8e2022-05-24 10:28:33 +02002131 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 +02002132 return LY_EVALID;
2133 }
Michal Vasko004d3152020-06-11 19:59:22 +02002134 } while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02002135
2136 return LY_SUCCESS;
2137}
2138
2139/**
2140 * @brief Reparse AbsoluteLocationPath. Logs directly on error.
2141 *
2142 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
2143 *
2144 * @param[in] ctx Context for logging.
2145 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002146 * @param[in] tok_idx Position in the expression \p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002147 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002148 * @return LY_ERR
2149 */
2150static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02002151reparse_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 +02002152{
2153 LY_ERR rc;
2154
Michal Vasko004d3152020-06-11 19:59:22 +02002155 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 +02002156
2157 /* '/' RelativeLocationPath? */
Michal Vasko004d3152020-06-11 19:59:22 +02002158 if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002159 /* '/' */
Michal Vasko004d3152020-06-11 19:59:22 +02002160 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002161
Michal Vasko004d3152020-06-11 19:59:22 +02002162 if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002163 return LY_SUCCESS;
2164 }
Michal Vasko004d3152020-06-11 19:59:22 +02002165 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002166 case LYXP_TOKEN_DOT:
2167 case LYXP_TOKEN_DDOT:
Michal Vasko49fec8e2022-05-24 10:28:33 +02002168 case LYXP_TOKEN_AXISNAME:
Michal Vasko03ff5a72019-09-11 13:49:33 +02002169 case LYXP_TOKEN_AT:
2170 case LYXP_TOKEN_NAMETEST:
2171 case LYXP_TOKEN_NODETYPE:
aPiecekbf968d92021-05-27 14:35:05 +02002172 rc = reparse_relative_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002173 LY_CHECK_RET(rc);
Radek Krejci0f969882020-08-21 16:56:47 +02002174 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02002175 default:
2176 break;
2177 }
2178
Michal Vasko03ff5a72019-09-11 13:49:33 +02002179 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02002180 /* '//' RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002181 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002182
aPiecekbf968d92021-05-27 14:35:05 +02002183 rc = reparse_relative_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002184 LY_CHECK_RET(rc);
2185 }
2186
2187 return LY_SUCCESS;
2188}
2189
2190/**
2191 * @brief Reparse FunctionCall. Logs directly on error.
2192 *
2193 * [9] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
2194 *
2195 * @param[in] ctx Context for logging.
2196 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002197 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002198 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002199 * @return LY_ERR
2200 */
2201static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02002202reparse_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 +02002203{
Radek Krejci1deb5be2020-08-26 16:43:36 +02002204 int8_t min_arg_count = -1;
Michal Vaskodd528af2022-08-08 14:35:07 +02002205 uint32_t arg_count, max_arg_count = 0, func_tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002206 LY_ERR rc;
2207
Michal Vasko004d3152020-06-11 19:59:22 +02002208 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_FUNCNAME);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002209 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002210 func_tok_idx = *tok_idx;
2211 switch (exp->tok_len[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002212 case 3:
Michal Vasko004d3152020-06-11 19:59:22 +02002213 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002214 min_arg_count = 1;
2215 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002216 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002217 min_arg_count = 1;
2218 max_arg_count = 1;
2219 }
2220 break;
2221 case 4:
Michal Vasko004d3152020-06-11 19:59:22 +02002222 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002223 min_arg_count = 1;
2224 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002225 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002226 min_arg_count = 0;
2227 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002228 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002229 min_arg_count = 0;
2230 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002231 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002232 min_arg_count = 0;
2233 max_arg_count = 0;
2234 }
2235 break;
2236 case 5:
Michal Vasko004d3152020-06-11 19:59:22 +02002237 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002238 min_arg_count = 1;
2239 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002240 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002241 min_arg_count = 0;
2242 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002243 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002244 min_arg_count = 1;
2245 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002246 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002247 min_arg_count = 1;
2248 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002249 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002250 min_arg_count = 1;
2251 max_arg_count = 1;
2252 }
2253 break;
2254 case 6:
Michal Vasko004d3152020-06-11 19:59:22 +02002255 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002256 min_arg_count = 2;
Radek Krejci1deb5be2020-08-26 16:43:36 +02002257 max_arg_count = UINT32_MAX;
Michal Vasko004d3152020-06-11 19:59:22 +02002258 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002259 min_arg_count = 0;
2260 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002261 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002262 min_arg_count = 0;
2263 max_arg_count = 1;
2264 }
2265 break;
2266 case 7:
Michal Vasko004d3152020-06-11 19:59:22 +02002267 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002268 min_arg_count = 1;
2269 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002270 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002271 min_arg_count = 1;
2272 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002273 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002274 min_arg_count = 0;
2275 max_arg_count = 0;
2276 }
2277 break;
2278 case 8:
Michal Vasko004d3152020-06-11 19:59:22 +02002279 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002280 min_arg_count = 2;
2281 max_arg_count = 2;
Michal Vasko004d3152020-06-11 19:59:22 +02002282 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002283 min_arg_count = 0;
2284 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002285 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "re-match", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002286 min_arg_count = 2;
2287 max_arg_count = 2;
2288 }
2289 break;
2290 case 9:
Michal Vasko004d3152020-06-11 19:59:22 +02002291 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002292 min_arg_count = 2;
2293 max_arg_count = 3;
Michal Vasko004d3152020-06-11 19:59:22 +02002294 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002295 min_arg_count = 3;
2296 max_arg_count = 3;
2297 }
2298 break;
2299 case 10:
Michal Vasko004d3152020-06-11 19:59:22 +02002300 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002301 min_arg_count = 0;
2302 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002303 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002304 min_arg_count = 1;
2305 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002306 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002307 min_arg_count = 2;
2308 max_arg_count = 2;
2309 }
2310 break;
2311 case 11:
Michal Vasko004d3152020-06-11 19:59:22 +02002312 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002313 min_arg_count = 2;
2314 max_arg_count = 2;
2315 }
2316 break;
2317 case 12:
Michal Vasko004d3152020-06-11 19:59:22 +02002318 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002319 min_arg_count = 2;
2320 max_arg_count = 2;
2321 }
2322 break;
2323 case 13:
Michal Vasko004d3152020-06-11 19:59:22 +02002324 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002325 min_arg_count = 0;
2326 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002327 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string-length", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002328 min_arg_count = 0;
2329 max_arg_count = 1;
2330 }
2331 break;
2332 case 15:
Michal Vasko004d3152020-06-11 19:59:22 +02002333 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002334 min_arg_count = 0;
2335 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002336 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-after", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002337 min_arg_count = 2;
2338 max_arg_count = 2;
2339 }
2340 break;
2341 case 16:
Michal Vasko004d3152020-06-11 19:59:22 +02002342 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002343 min_arg_count = 2;
2344 max_arg_count = 2;
2345 }
2346 break;
2347 case 20:
Michal Vasko004d3152020-06-11 19:59:22 +02002348 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from-or-self", 20)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002349 min_arg_count = 2;
2350 max_arg_count = 2;
2351 }
2352 break;
2353 }
2354 if (min_arg_count == -1) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002355 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 +02002356 return LY_EINVAL;
2357 }
Michal Vasko004d3152020-06-11 19:59:22 +02002358 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002359
2360 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02002361 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002362 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002363 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002364
2365 /* ( Expr ( ',' Expr )* )? */
2366 arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002367 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002368 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002369 if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002370 ++arg_count;
aPiecekbf968d92021-05-27 14:35:05 +02002371 rc = reparse_or_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002372 LY_CHECK_RET(rc);
2373 }
Michal Vasko004d3152020-06-11 19:59:22 +02002374 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) {
2375 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002376
2377 ++arg_count;
aPiecekbf968d92021-05-27 14:35:05 +02002378 rc = reparse_or_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002379 LY_CHECK_RET(rc);
2380 }
2381
2382 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02002383 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002384 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002385 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002386
Radek Krejci857189e2020-09-01 13:26:36 +02002387 if ((arg_count < (uint32_t)min_arg_count) || (arg_count > max_arg_count)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002388 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 +02002389 return LY_EVALID;
2390 }
2391
2392 return LY_SUCCESS;
2393}
2394
2395/**
2396 * @brief Reparse PathExpr. Logs directly on error.
2397 *
2398 * [10] PathExpr ::= LocationPath | PrimaryExpr Predicate*
2399 * | PrimaryExpr Predicate* '/' RelativeLocationPath
2400 * | PrimaryExpr Predicate* '//' RelativeLocationPath
2401 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
aPiecekfba75362021-10-07 12:39:48 +02002402 * [8] PrimaryExpr ::= VariableReference | '(' Expr ')' | Literal | Number | FunctionCall
Michal Vasko03ff5a72019-09-11 13:49:33 +02002403 *
2404 * @param[in] ctx Context for logging.
2405 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002406 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002407 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002408 * @return LY_ERR
2409 */
2410static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02002411reparse_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 +02002412{
2413 LY_ERR rc;
2414
Michal Vasko004d3152020-06-11 19:59:22 +02002415 if (lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko14676352020-05-29 11:35:55 +02002416 return LY_EVALID;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002417 }
2418
Michal Vasko004d3152020-06-11 19:59:22 +02002419 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002420 case LYXP_TOKEN_PAR1:
2421 /* '(' Expr ')' Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002422 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002423
aPiecekbf968d92021-05-27 14:35:05 +02002424 rc = reparse_or_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002425 LY_CHECK_RET(rc);
2426
Michal Vasko004d3152020-06-11 19:59:22 +02002427 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002428 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002429 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002430 goto predicate;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002431 case LYXP_TOKEN_DOT:
2432 case LYXP_TOKEN_DDOT:
Michal Vasko49fec8e2022-05-24 10:28:33 +02002433 case LYXP_TOKEN_AXISNAME:
Michal Vasko03ff5a72019-09-11 13:49:33 +02002434 case LYXP_TOKEN_AT:
2435 case LYXP_TOKEN_NAMETEST:
2436 case LYXP_TOKEN_NODETYPE:
2437 /* RelativeLocationPath */
aPiecekbf968d92021-05-27 14:35:05 +02002438 rc = reparse_relative_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002439 LY_CHECK_RET(rc);
2440 break;
aPiecekfba75362021-10-07 12:39:48 +02002441 case LYXP_TOKEN_VARREF:
2442 /* VariableReference */
2443 ++(*tok_idx);
2444 goto predicate;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002445 case LYXP_TOKEN_FUNCNAME:
2446 /* FunctionCall */
aPiecekbf968d92021-05-27 14:35:05 +02002447 rc = reparse_function_call(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002448 LY_CHECK_RET(rc);
2449 goto predicate;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002450 case LYXP_TOKEN_OPER_PATH:
2451 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +02002452 /* AbsoluteLocationPath */
aPiecekbf968d92021-05-27 14:35:05 +02002453 rc = reparse_absolute_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002454 LY_CHECK_RET(rc);
2455 break;
2456 case LYXP_TOKEN_LITERAL:
2457 /* Literal */
Michal Vasko004d3152020-06-11 19:59:22 +02002458 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002459 goto predicate;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002460 case LYXP_TOKEN_NUMBER:
2461 /* Number */
Michal Vasko004d3152020-06-11 19:59:22 +02002462 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002463 goto predicate;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002464 default:
Michal Vasko49fec8e2022-05-24 10:28:33 +02002465 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 +02002466 return LY_EVALID;
2467 }
2468
2469 return LY_SUCCESS;
2470
2471predicate:
2472 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002473 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
aPiecekbf968d92021-05-27 14:35:05 +02002474 rc = reparse_predicate(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002475 LY_CHECK_RET(rc);
2476 }
2477
2478 /* ('/' or '//') RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002479 if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002480
2481 /* '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02002482 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002483
aPiecekbf968d92021-05-27 14:35:05 +02002484 rc = reparse_relative_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002485 LY_CHECK_RET(rc);
2486 }
2487
2488 return LY_SUCCESS;
2489}
2490
2491/**
2492 * @brief Reparse UnaryExpr. Logs directly on error.
2493 *
2494 * [17] UnaryExpr ::= UnionExpr | '-' UnaryExpr
2495 * [18] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
2496 *
2497 * @param[in] ctx Context for logging.
2498 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002499 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002500 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002501 * @return LY_ERR
2502 */
2503static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02002504reparse_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 +02002505{
Michal Vaskodd528af2022-08-08 14:35:07 +02002506 uint32_t prev_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002507 LY_ERR rc;
2508
2509 /* ('-')* */
Michal Vasko004d3152020-06-11 19:59:22 +02002510 prev_exp = *tok_idx;
Michal Vasko69730152020-10-09 16:30:07 +02002511 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2512 (exp->expr[exp->tok_pos[*tok_idx]] == '-')) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002513 exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNARY);
Michal Vasko004d3152020-06-11 19:59:22 +02002514 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002515 }
2516
2517 /* PathExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002518 prev_exp = *tok_idx;
aPiecekbf968d92021-05-27 14:35:05 +02002519 rc = reparse_path_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002520 LY_CHECK_RET(rc);
2521
2522 /* ('|' PathExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002523 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_UNI)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002524 exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNION);
Michal Vasko004d3152020-06-11 19:59:22 +02002525 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002526
aPiecekbf968d92021-05-27 14:35:05 +02002527 rc = reparse_path_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002528 LY_CHECK_RET(rc);
2529 }
2530
2531 return LY_SUCCESS;
2532}
2533
2534/**
2535 * @brief Reparse AdditiveExpr. Logs directly on error.
2536 *
2537 * [15] AdditiveExpr ::= MultiplicativeExpr
2538 * | AdditiveExpr '+' MultiplicativeExpr
2539 * | AdditiveExpr '-' MultiplicativeExpr
2540 * [16] MultiplicativeExpr ::= UnaryExpr
2541 * | MultiplicativeExpr '*' UnaryExpr
2542 * | MultiplicativeExpr 'div' UnaryExpr
2543 * | MultiplicativeExpr 'mod' UnaryExpr
2544 *
2545 * @param[in] ctx Context for logging.
2546 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002547 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002548 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002549 * @return LY_ERR
2550 */
2551static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02002552reparse_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 +02002553{
Michal Vaskodd528af2022-08-08 14:35:07 +02002554 uint32_t prev_add_exp, prev_mul_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002555 LY_ERR rc;
2556
Michal Vasko004d3152020-06-11 19:59:22 +02002557 prev_add_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002558 goto reparse_multiplicative_expr;
2559
2560 /* ('+' / '-' MultiplicativeExpr)* */
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->expr[exp->tok_pos[*tok_idx]] == '-'))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002563 exp_repeat_push(exp, prev_add_exp, LYXP_EXPR_ADDITIVE);
Michal Vasko004d3152020-06-11 19:59:22 +02002564 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002565
2566reparse_multiplicative_expr:
2567 /* UnaryExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002568 prev_mul_exp = *tok_idx;
aPiecekbf968d92021-05-27 14:35:05 +02002569 rc = reparse_unary_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002570 LY_CHECK_RET(rc);
2571
2572 /* ('*' / 'div' / 'mod' UnaryExpr)* */
Michal Vasko69730152020-10-09 16:30:07 +02002573 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2574 ((exp->expr[exp->tok_pos[*tok_idx]] == '*') || (exp->tok_len[*tok_idx] == 3))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002575 exp_repeat_push(exp, prev_mul_exp, LYXP_EXPR_MULTIPLICATIVE);
Michal Vasko004d3152020-06-11 19:59:22 +02002576 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002577
aPiecekbf968d92021-05-27 14:35:05 +02002578 rc = reparse_unary_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002579 LY_CHECK_RET(rc);
2580 }
2581 }
2582
2583 return LY_SUCCESS;
2584}
2585
2586/**
2587 * @brief Reparse EqualityExpr. Logs directly on error.
2588 *
2589 * [13] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
2590 * | EqualityExpr '!=' RelationalExpr
2591 * [14] RelationalExpr ::= AdditiveExpr
2592 * | RelationalExpr '<' AdditiveExpr
2593 * | RelationalExpr '>' AdditiveExpr
2594 * | RelationalExpr '<=' AdditiveExpr
2595 * | RelationalExpr '>=' AdditiveExpr
2596 *
2597 * @param[in] ctx Context for logging.
2598 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002599 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002600 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002601 * @return LY_ERR
2602 */
2603static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02002604reparse_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 +02002605{
Michal Vaskodd528af2022-08-08 14:35:07 +02002606 uint32_t prev_eq_exp, prev_rel_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002607 LY_ERR rc;
2608
Michal Vasko004d3152020-06-11 19:59:22 +02002609 prev_eq_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002610 goto reparse_additive_expr;
2611
2612 /* ('=' / '!=' RelationalExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002613 while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_EQUAL, LYXP_TOKEN_OPER_NEQUAL)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002614 exp_repeat_push(exp, prev_eq_exp, LYXP_EXPR_EQUALITY);
Michal Vasko004d3152020-06-11 19:59:22 +02002615 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002616
2617reparse_additive_expr:
2618 /* AdditiveExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002619 prev_rel_exp = *tok_idx;
aPiecekbf968d92021-05-27 14:35:05 +02002620 rc = reparse_additive_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002621 LY_CHECK_RET(rc);
2622
2623 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002624 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_COMP)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002625 exp_repeat_push(exp, prev_rel_exp, LYXP_EXPR_RELATIONAL);
Michal Vasko004d3152020-06-11 19:59:22 +02002626 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002627
aPiecekbf968d92021-05-27 14:35:05 +02002628 rc = reparse_additive_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002629 LY_CHECK_RET(rc);
2630 }
2631 }
2632
2633 return LY_SUCCESS;
2634}
2635
2636/**
2637 * @brief Reparse OrExpr. Logs directly on error.
2638 *
2639 * [11] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
2640 * [12] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
2641 *
2642 * @param[in] ctx Context for logging.
2643 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002644 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002645 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002646 * @return LY_ERR
2647 */
2648static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02002649reparse_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 +02002650{
Michal Vaskodd528af2022-08-08 14:35:07 +02002651 uint32_t prev_or_exp, prev_and_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002652 LY_ERR rc;
2653
aPiecekbf968d92021-05-27 14:35:05 +02002654 ++depth;
2655 LY_CHECK_ERR_RET(depth > LYXP_MAX_BLOCK_DEPTH, LOGVAL(ctx, LY_VCODE_XP_DEPTH), LY_EINVAL);
2656
Michal Vasko004d3152020-06-11 19:59:22 +02002657 prev_or_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002658 goto reparse_equality_expr;
2659
2660 /* ('or' AndExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002661 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 +02002662 exp_repeat_push(exp, prev_or_exp, LYXP_EXPR_OR);
Michal Vasko004d3152020-06-11 19:59:22 +02002663 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002664
2665reparse_equality_expr:
2666 /* EqualityExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002667 prev_and_exp = *tok_idx;
aPiecekbf968d92021-05-27 14:35:05 +02002668 rc = reparse_equality_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002669 LY_CHECK_RET(rc);
2670
2671 /* ('and' EqualityExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002672 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 +02002673 exp_repeat_push(exp, prev_and_exp, LYXP_EXPR_AND);
Michal Vasko004d3152020-06-11 19:59:22 +02002674 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002675
aPiecekbf968d92021-05-27 14:35:05 +02002676 rc = reparse_equality_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002677 LY_CHECK_RET(rc);
2678 }
2679 }
2680
2681 return LY_SUCCESS;
2682}
Radek Krejcib1646a92018-11-02 16:08:26 +01002683
2684/**
2685 * @brief Parse NCName.
2686 *
2687 * @param[in] ncname Name to parse.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002688 * @return Length of @p ncname valid bytes.
Radek Krejcib1646a92018-11-02 16:08:26 +01002689 */
Michal Vasko49fec8e2022-05-24 10:28:33 +02002690static ssize_t
Radek Krejcib1646a92018-11-02 16:08:26 +01002691parse_ncname(const char *ncname)
2692{
Radek Krejci1deb5be2020-08-26 16:43:36 +02002693 uint32_t uc;
Radek Krejcid4270262019-01-07 15:07:25 +01002694 size_t size;
Michal Vasko49fec8e2022-05-24 10:28:33 +02002695 ssize_t len = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01002696
2697 LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), 0);
2698 if (!is_xmlqnamestartchar(uc) || (uc == ':')) {
2699 return len;
2700 }
2701
2702 do {
2703 len += size;
Radek Krejci9a564c92019-01-07 14:53:57 +01002704 if (!*ncname) {
2705 break;
2706 }
Radek Krejcid4270262019-01-07 15:07:25 +01002707 LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), -len);
Radek Krejcib1646a92018-11-02 16:08:26 +01002708 } while (is_xmlqnamechar(uc) && (uc != ':'));
2709
2710 return len;
2711}
2712
2713/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02002714 * @brief Add @p token into the expression @p exp.
Radek Krejcib1646a92018-11-02 16:08:26 +01002715 *
Michal Vasko03ff5a72019-09-11 13:49:33 +02002716 * @param[in] ctx Context for logging.
Radek Krejcib1646a92018-11-02 16:08:26 +01002717 * @param[in] exp Expression to use.
2718 * @param[in] token Token to add.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002719 * @param[in] tok_pos Token position in the XPath expression.
Radek Krejcib1646a92018-11-02 16:08:26 +01002720 * @param[in] tok_len Token length in the XPath expression.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002721 * @return LY_ERR
Radek Krejcib1646a92018-11-02 16:08:26 +01002722 */
2723static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02002724exp_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 +01002725{
2726 uint32_t prev;
2727
2728 if (exp->used == exp->size) {
2729 prev = exp->size;
2730 exp->size += LYXP_EXPR_SIZE_STEP;
2731 if (prev > exp->size) {
2732 LOGINT(ctx);
2733 return LY_EINT;
2734 }
2735
2736 exp->tokens = ly_realloc(exp->tokens, exp->size * sizeof *exp->tokens);
2737 LY_CHECK_ERR_RET(!exp->tokens, LOGMEM(ctx), LY_EMEM);
2738 exp->tok_pos = ly_realloc(exp->tok_pos, exp->size * sizeof *exp->tok_pos);
2739 LY_CHECK_ERR_RET(!exp->tok_pos, LOGMEM(ctx), LY_EMEM);
2740 exp->tok_len = ly_realloc(exp->tok_len, exp->size * sizeof *exp->tok_len);
2741 LY_CHECK_ERR_RET(!exp->tok_len, LOGMEM(ctx), LY_EMEM);
2742 }
2743
2744 exp->tokens[exp->used] = token;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002745 exp->tok_pos[exp->used] = tok_pos;
Radek Krejcib1646a92018-11-02 16:08:26 +01002746 exp->tok_len[exp->used] = tok_len;
2747 ++exp->used;
2748 return LY_SUCCESS;
2749}
2750
2751void
Michal Vasko14676352020-05-29 11:35:55 +02002752lyxp_expr_free(const struct ly_ctx *ctx, struct lyxp_expr *expr)
Radek Krejcib1646a92018-11-02 16:08:26 +01002753{
Michal Vaskodd528af2022-08-08 14:35:07 +02002754 uint32_t i;
Radek Krejcib1646a92018-11-02 16:08:26 +01002755
2756 if (!expr) {
2757 return;
2758 }
2759
2760 lydict_remove(ctx, expr->expr);
2761 free(expr->tokens);
2762 free(expr->tok_pos);
2763 free(expr->tok_len);
2764 if (expr->repeat) {
2765 for (i = 0; i < expr->used; ++i) {
2766 free(expr->repeat[i]);
2767 }
2768 }
2769 free(expr->repeat);
2770 free(expr);
2771}
2772
Michal Vasko49fec8e2022-05-24 10:28:33 +02002773/**
2774 * @brief Parse Axis name.
2775 *
2776 * @param[in] str String to parse.
2777 * @param[in] str_len Length of @p str.
2778 * @return LY_SUCCESS if an axis.
2779 * @return LY_ENOT otherwise.
2780 */
2781static LY_ERR
2782expr_parse_axis(const char *str, size_t str_len)
2783{
2784 switch (str_len) {
2785 case 4:
2786 if (!strncmp("self", str, str_len)) {
2787 return LY_SUCCESS;
2788 }
2789 break;
2790 case 5:
2791 if (!strncmp("child", str, str_len)) {
2792 return LY_SUCCESS;
2793 }
2794 break;
2795 case 6:
2796 if (!strncmp("parent", str, str_len)) {
2797 return LY_SUCCESS;
2798 }
2799 break;
2800 case 8:
2801 if (!strncmp("ancestor", str, str_len)) {
2802 return LY_SUCCESS;
2803 }
2804 break;
2805 case 9:
2806 if (!strncmp("attribute", str, str_len)) {
2807 return LY_SUCCESS;
2808 } else if (!strncmp("following", str, str_len)) {
2809 return LY_SUCCESS;
2810 } else if (!strncmp("namespace", str, str_len)) {
2811 LOGERR(NULL, LY_EINVAL, "Axis \"namespace\" not supported.");
2812 return LY_ENOT;
2813 } else if (!strncmp("preceding", str, str_len)) {
2814 return LY_SUCCESS;
2815 }
2816 break;
2817 case 10:
2818 if (!strncmp("descendant", str, str_len)) {
2819 return LY_SUCCESS;
2820 }
2821 break;
2822 case 16:
2823 if (!strncmp("ancestor-or-self", str, str_len)) {
2824 return LY_SUCCESS;
2825 }
2826 break;
2827 case 17:
2828 if (!strncmp("following-sibling", str, str_len)) {
2829 return LY_SUCCESS;
2830 } else if (!strncmp("preceding-sibling", str, str_len)) {
2831 return LY_SUCCESS;
2832 }
2833 break;
2834 case 18:
2835 if (!strncmp("descendant-or-self", str, str_len)) {
2836 return LY_SUCCESS;
2837 }
2838 break;
2839 }
2840
2841 return LY_ENOT;
2842}
2843
Radek Krejcif03a9e22020-09-18 20:09:31 +02002844LY_ERR
2845lyxp_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 +01002846{
Radek Krejcif03a9e22020-09-18 20:09:31 +02002847 LY_ERR ret = LY_SUCCESS;
2848 struct lyxp_expr *expr;
Radek Krejcid4270262019-01-07 15:07:25 +01002849 size_t parsed = 0, tok_len;
Radek Krejcib1646a92018-11-02 16:08:26 +01002850 enum lyxp_token tok_type;
Michal Vasko49fec8e2022-05-24 10:28:33 +02002851 ly_bool prev_func_check = 0, prev_ntype_check = 0, has_axis;
Michal Vaskodd528af2022-08-08 14:35:07 +02002852 uint32_t tok_idx = 0;
Michal Vasko49fec8e2022-05-24 10:28:33 +02002853 ssize_t ncname_len;
Radek Krejcib1646a92018-11-02 16:08:26 +01002854
Radek Krejcif03a9e22020-09-18 20:09:31 +02002855 assert(expr_p);
2856
2857 if (!expr_str[0]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002858 LOGVAL(ctx, LY_VCODE_XP_EOF);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002859 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02002860 }
2861
2862 if (!expr_len) {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002863 expr_len = strlen(expr_str);
Michal Vasko004d3152020-06-11 19:59:22 +02002864 }
Michal Vaskodd528af2022-08-08 14:35:07 +02002865 if (expr_len > UINT32_MAX) {
2866 LOGVAL(ctx, LYVE_XPATH, "XPath expression cannot be longer than %" PRIu32 " characters.", UINT32_MAX);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002867 return LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01002868 }
2869
2870 /* init lyxp_expr structure */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002871 expr = calloc(1, sizeof *expr);
2872 LY_CHECK_ERR_GOTO(!expr, LOGMEM(ctx); ret = LY_EMEM, error);
2873 LY_CHECK_GOTO(ret = lydict_insert(ctx, expr_str, expr_len, &expr->expr), error);
2874 expr->used = 0;
2875 expr->size = LYXP_EXPR_SIZE_START;
2876 expr->tokens = malloc(expr->size * sizeof *expr->tokens);
2877 LY_CHECK_ERR_GOTO(!expr->tokens, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002878
Radek Krejcif03a9e22020-09-18 20:09:31 +02002879 expr->tok_pos = malloc(expr->size * sizeof *expr->tok_pos);
2880 LY_CHECK_ERR_GOTO(!expr->tok_pos, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002881
Radek Krejcif03a9e22020-09-18 20:09:31 +02002882 expr->tok_len = malloc(expr->size * sizeof *expr->tok_len);
2883 LY_CHECK_ERR_GOTO(!expr->tok_len, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002884
Michal Vasko004d3152020-06-11 19:59:22 +02002885 /* make expr 0-terminated */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002886 expr_str = expr->expr;
Michal Vasko004d3152020-06-11 19:59:22 +02002887
Radek Krejcif03a9e22020-09-18 20:09:31 +02002888 while (is_xmlws(expr_str[parsed])) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002889 ++parsed;
2890 }
2891
2892 do {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002893 if (expr_str[parsed] == '(') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002894
2895 /* '(' */
2896 tok_len = 1;
2897 tok_type = LYXP_TOKEN_PAR1;
2898
Michal Vasko49fec8e2022-05-24 10:28:33 +02002899 if (prev_ntype_check && expr->used && (expr->tokens[expr->used - 1] == LYXP_TOKEN_NAMETEST) &&
2900 (((expr->tok_len[expr->used - 1] == 4) &&
2901 (!strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "node", 4) ||
2902 !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "text", 4))) ||
2903 ((expr->tok_len[expr->used - 1] == 7) &&
2904 !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "comment", 7)))) {
2905 /* it is NodeType after all */
2906 expr->tokens[expr->used - 1] = LYXP_TOKEN_NODETYPE;
2907
2908 prev_ntype_check = 0;
2909 prev_func_check = 0;
2910 } else if (prev_func_check && expr->used && (expr->tokens[expr->used - 1] == LYXP_TOKEN_NAMETEST)) {
2911 /* it is FunctionName after all */
2912 expr->tokens[expr->used - 1] = LYXP_TOKEN_FUNCNAME;
2913
2914 prev_ntype_check = 0;
2915 prev_func_check = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01002916 }
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_PAR2;
2923
Radek Krejcif03a9e22020-09-18 20:09:31 +02002924 } else if (expr_str[parsed] == '[') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002925
2926 /* '[' */
2927 tok_len = 1;
2928 tok_type = LYXP_TOKEN_BRACK1;
2929
Radek Krejcif03a9e22020-09-18 20:09:31 +02002930 } else if (expr_str[parsed] == ']') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002931
2932 /* ']' */
2933 tok_len = 1;
2934 tok_type = LYXP_TOKEN_BRACK2;
2935
Radek Krejcif03a9e22020-09-18 20:09:31 +02002936 } else if (!strncmp(&expr_str[parsed], "..", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002937
2938 /* '..' */
2939 tok_len = 2;
2940 tok_type = LYXP_TOKEN_DDOT;
2941
Radek Krejcif03a9e22020-09-18 20:09:31 +02002942 } else if ((expr_str[parsed] == '.') && (!isdigit(expr_str[parsed + 1]))) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002943
2944 /* '.' */
2945 tok_len = 1;
2946 tok_type = LYXP_TOKEN_DOT;
2947
Radek Krejcif03a9e22020-09-18 20:09:31 +02002948 } else if (expr_str[parsed] == '@') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002949
2950 /* '@' */
2951 tok_len = 1;
2952 tok_type = LYXP_TOKEN_AT;
2953
Radek Krejcif03a9e22020-09-18 20:09:31 +02002954 } else if (expr_str[parsed] == ',') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002955
2956 /* ',' */
2957 tok_len = 1;
2958 tok_type = LYXP_TOKEN_COMMA;
2959
Radek Krejcif03a9e22020-09-18 20:09:31 +02002960 } else if (expr_str[parsed] == '\'') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002961
2962 /* Literal with ' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002963 for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\''); ++tok_len) {}
2964 LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0',
Radek Krejci2efc45b2020-12-22 16:25:44 +01002965 LOGVAL(ctx, LY_VCODE_XP_EOE, expr_str[parsed], &expr_str[parsed]); ret = LY_EVALID,
Michal Vasko69730152020-10-09 16:30:07 +02002966 error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002967 ++tok_len;
2968 tok_type = LYXP_TOKEN_LITERAL;
2969
Radek Krejcif03a9e22020-09-18 20:09:31 +02002970 } else if (expr_str[parsed] == '\"') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002971
2972 /* Literal with " */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002973 for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\"'); ++tok_len) {}
2974 LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0',
Radek Krejci2efc45b2020-12-22 16:25:44 +01002975 LOGVAL(ctx, LY_VCODE_XP_EOE, expr_str[parsed], &expr_str[parsed]); ret = LY_EVALID,
Michal Vasko69730152020-10-09 16:30:07 +02002976 error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002977 ++tok_len;
2978 tok_type = LYXP_TOKEN_LITERAL;
2979
Radek Krejcif03a9e22020-09-18 20:09:31 +02002980 } else if ((expr_str[parsed] == '.') || (isdigit(expr_str[parsed]))) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002981
2982 /* Number */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002983 for (tok_len = 0; isdigit(expr_str[parsed + tok_len]); ++tok_len) {}
2984 if (expr_str[parsed + tok_len] == '.') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002985 ++tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02002986 for ( ; isdigit(expr_str[parsed + tok_len]); ++tok_len) {}
Radek Krejcib1646a92018-11-02 16:08:26 +01002987 }
2988 tok_type = LYXP_TOKEN_NUMBER;
2989
aPiecekfba75362021-10-07 12:39:48 +02002990 } else if (expr_str[parsed] == '$') {
2991
2992 /* VariableReference */
2993 parsed++;
Michal Vasko49fec8e2022-05-24 10:28:33 +02002994 ncname_len = parse_ncname(&expr_str[parsed]);
aPiecekfba75362021-10-07 12:39:48 +02002995 LY_CHECK_ERR_GOTO(ncname_len < 1, LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed - ncname_len],
2996 parsed - ncname_len + 1, expr_str); ret = LY_EVALID, error);
2997 tok_len = ncname_len;
2998 LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == ':',
2999 LOGVAL(ctx, LYVE_XPATH, "Variable with prefix is not supported."); ret = LY_EVALID,
3000 error);
3001 tok_type = LYXP_TOKEN_VARREF;
3002
Radek Krejcif03a9e22020-09-18 20:09:31 +02003003 } else if (expr_str[parsed] == '/') {
Radek Krejcib1646a92018-11-02 16:08:26 +01003004
3005 /* Operator '/', '//' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02003006 if (!strncmp(&expr_str[parsed], "//", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01003007 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003008 tok_type = LYXP_TOKEN_OPER_RPATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01003009 } else {
3010 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003011 tok_type = LYXP_TOKEN_OPER_PATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01003012 }
Radek Krejcib1646a92018-11-02 16:08:26 +01003013
Radek Krejcif03a9e22020-09-18 20:09:31 +02003014 } else if (!strncmp(&expr_str[parsed], "!=", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01003015
Michal Vasko3e48bf32020-06-01 08:39:07 +02003016 /* Operator '!=' */
Radek Krejcib1646a92018-11-02 16:08:26 +01003017 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003018 tok_type = LYXP_TOKEN_OPER_NEQUAL;
3019
Radek Krejcif03a9e22020-09-18 20:09:31 +02003020 } else if (!strncmp(&expr_str[parsed], "<=", 2) || !strncmp(&expr_str[parsed], ">=", 2)) {
Michal Vasko3e48bf32020-06-01 08:39:07 +02003021
3022 /* Operator '<=', '>=' */
3023 tok_len = 2;
3024 tok_type = LYXP_TOKEN_OPER_COMP;
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
3028 /* Operator '|' */
3029 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003030 tok_type = LYXP_TOKEN_OPER_UNI;
Radek Krejcib1646a92018-11-02 16:08:26 +01003031
Radek Krejcif03a9e22020-09-18 20:09:31 +02003032 } else if ((expr_str[parsed] == '+') || (expr_str[parsed] == '-')) {
Radek Krejcib1646a92018-11-02 16:08:26 +01003033
3034 /* Operator '+', '-' */
3035 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003036 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01003037
Radek Krejcif03a9e22020-09-18 20:09:31 +02003038 } else if (expr_str[parsed] == '=') {
Radek Krejcib1646a92018-11-02 16:08:26 +01003039
Michal Vasko3e48bf32020-06-01 08:39:07 +02003040 /* Operator '=' */
Radek Krejcib1646a92018-11-02 16:08:26 +01003041 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003042 tok_type = LYXP_TOKEN_OPER_EQUAL;
3043
Radek Krejcif03a9e22020-09-18 20:09:31 +02003044 } else if ((expr_str[parsed] == '<') || (expr_str[parsed] == '>')) {
Michal Vasko3e48bf32020-06-01 08:39:07 +02003045
3046 /* Operator '<', '>' */
3047 tok_len = 1;
3048 tok_type = LYXP_TOKEN_OPER_COMP;
Radek Krejcib1646a92018-11-02 16:08:26 +01003049
Michal Vasko69730152020-10-09 16:30:07 +02003050 } else if (expr->used && (expr->tokens[expr->used - 1] != LYXP_TOKEN_AT) &&
3051 (expr->tokens[expr->used - 1] != LYXP_TOKEN_PAR1) &&
3052 (expr->tokens[expr->used - 1] != LYXP_TOKEN_BRACK1) &&
3053 (expr->tokens[expr->used - 1] != LYXP_TOKEN_COMMA) &&
3054 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_LOG) &&
3055 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_EQUAL) &&
3056 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_NEQUAL) &&
3057 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_COMP) &&
3058 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_MATH) &&
3059 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_UNI) &&
3060 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_PATH) &&
3061 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_RPATH)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01003062
3063 /* Operator '*', 'or', 'and', 'mod', or 'div' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02003064 if (expr_str[parsed] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01003065 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003066 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01003067
Radek Krejcif03a9e22020-09-18 20:09:31 +02003068 } else if (!strncmp(&expr_str[parsed], "or", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01003069 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003070 tok_type = LYXP_TOKEN_OPER_LOG;
Radek Krejcib1646a92018-11-02 16:08:26 +01003071
Radek Krejcif03a9e22020-09-18 20:09:31 +02003072 } else if (!strncmp(&expr_str[parsed], "and", 3)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01003073 tok_len = 3;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003074 tok_type = LYXP_TOKEN_OPER_LOG;
Radek Krejcib1646a92018-11-02 16:08:26 +01003075
Radek Krejcif03a9e22020-09-18 20:09:31 +02003076 } else if (!strncmp(&expr_str[parsed], "mod", 3) || !strncmp(&expr_str[parsed], "div", 3)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01003077 tok_len = 3;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003078 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01003079
Michal Vasko49fec8e2022-05-24 10:28:33 +02003080 } else if (prev_ntype_check || prev_func_check) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003081 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 +02003082 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 +02003083 ret = LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01003084 goto error;
3085 } else {
Michal Vasko774ce402021-04-14 15:35:06 +02003086 LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed], parsed + 1, expr_str);
Radek Krejcif03a9e22020-09-18 20:09:31 +02003087 ret = LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01003088 goto error;
3089 }
Radek Krejcib1646a92018-11-02 16:08:26 +01003090 } else {
3091
Michal Vasko49fec8e2022-05-24 10:28:33 +02003092 /* (AxisName '::')? ((NCName ':')? '*' | QName) or NodeType/FunctionName */
3093 if (expr_str[parsed] == '*') {
3094 ncname_len = 1;
3095 } else {
3096 ncname_len = parse_ncname(&expr_str[parsed]);
3097 LY_CHECK_ERR_GOTO(ncname_len < 1, LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed - ncname_len],
3098 parsed - ncname_len + 1, expr_str); ret = LY_EVALID, error);
3099 }
Radek Krejcib1646a92018-11-02 16:08:26 +01003100 tok_len = ncname_len;
3101
Michal Vasko49fec8e2022-05-24 10:28:33 +02003102 has_axis = 0;
3103 if (!strncmp(&expr_str[parsed + tok_len], "::", 2)) {
3104 /* axis */
3105 LY_CHECK_ERR_GOTO(expr_parse_axis(&expr_str[parsed], ncname_len),
3106 LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed], parsed + 1, expr_str); ret = LY_EVALID, error);
3107 tok_type = LYXP_TOKEN_AXISNAME;
3108
3109 LY_CHECK_GOTO(ret = exp_add_token(ctx, expr, tok_type, parsed, tok_len), error);
3110 parsed += tok_len;
3111
3112 /* '::' */
3113 tok_len = 2;
3114 tok_type = LYXP_TOKEN_DCOLON;
3115
3116 LY_CHECK_GOTO(ret = exp_add_token(ctx, expr, tok_type, parsed, tok_len), error);
3117 parsed += tok_len;
3118
3119 if (expr_str[parsed] == '*') {
3120 ncname_len = 1;
3121 } else {
3122 ncname_len = parse_ncname(&expr_str[parsed]);
3123 LY_CHECK_ERR_GOTO(ncname_len < 1, LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed - ncname_len],
3124 parsed - ncname_len + 1, expr_str); ret = LY_EVALID, error);
3125 }
3126 tok_len = ncname_len;
3127
3128 has_axis = 1;
3129 }
3130
Radek Krejcif03a9e22020-09-18 20:09:31 +02003131 if (expr_str[parsed + tok_len] == ':') {
Radek Krejcib1646a92018-11-02 16:08:26 +01003132 ++tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02003133 if (expr_str[parsed + tok_len] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01003134 ++tok_len;
3135 } else {
Radek Krejcif03a9e22020-09-18 20:09:31 +02003136 ncname_len = parse_ncname(&expr_str[parsed + tok_len]);
Michal Vaskoe2be5462021-08-04 10:49:42 +02003137 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 +02003138 parsed - ncname_len + 1, expr_str); ret = LY_EVALID, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01003139 tok_len += ncname_len;
3140 }
Michal Vasko49fec8e2022-05-24 10:28:33 +02003141 /* remove old flags to prevent ambiguities */
3142 prev_ntype_check = 0;
3143 prev_func_check = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01003144 tok_type = LYXP_TOKEN_NAMETEST;
3145 } else {
Michal Vasko49fec8e2022-05-24 10:28:33 +02003146 /* if not '*', there is no prefix so it can still be NodeType/FunctionName, we can't finally decide now */
3147 prev_ntype_check = (expr_str[parsed] == '*') ? 0 : 1;
3148 prev_func_check = (prev_ntype_check && !has_axis) ? 1 : 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01003149 tok_type = LYXP_TOKEN_NAMETEST;
3150 }
3151 }
3152
3153 /* store the token, move on to the next one */
Radek Krejcif03a9e22020-09-18 20:09:31 +02003154 LY_CHECK_GOTO(ret = exp_add_token(ctx, expr, tok_type, parsed, tok_len), error);
Radek Krejcib1646a92018-11-02 16:08:26 +01003155 parsed += tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02003156 while (is_xmlws(expr_str[parsed])) {
Radek Krejcib1646a92018-11-02 16:08:26 +01003157 ++parsed;
3158 }
3159
Radek Krejcif03a9e22020-09-18 20:09:31 +02003160 } while (expr_str[parsed]);
Radek Krejcib1646a92018-11-02 16:08:26 +01003161
Michal Vasko004d3152020-06-11 19:59:22 +02003162 if (reparse) {
3163 /* prealloc repeat */
Radek Krejcif03a9e22020-09-18 20:09:31 +02003164 expr->repeat = calloc(expr->size, sizeof *expr->repeat);
3165 LY_CHECK_ERR_GOTO(!expr->repeat, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01003166
Michal Vasko004d3152020-06-11 19:59:22 +02003167 /* fill repeat */
aPiecekbf968d92021-05-27 14:35:05 +02003168 LY_CHECK_ERR_GOTO(reparse_or_expr(ctx, expr, &tok_idx, 0), ret = LY_EVALID, error);
Radek Krejcif03a9e22020-09-18 20:09:31 +02003169 if (expr->used > tok_idx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003170 LOGVAL(ctx, LYVE_XPATH, "Unparsed characters \"%s\" left at the end of an XPath expression.",
Michal Vasko69730152020-10-09 16:30:07 +02003171 &expr->expr[expr->tok_pos[tok_idx]]);
Radek Krejcif03a9e22020-09-18 20:09:31 +02003172 ret = LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02003173 goto error;
3174 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003175 }
3176
Radek Krejcif03a9e22020-09-18 20:09:31 +02003177 print_expr_struct_debug(expr);
3178 *expr_p = expr;
3179 return LY_SUCCESS;
Radek Krejcib1646a92018-11-02 16:08:26 +01003180
3181error:
Radek Krejcif03a9e22020-09-18 20:09:31 +02003182 lyxp_expr_free(ctx, expr);
3183 return ret;
Radek Krejcib1646a92018-11-02 16:08:26 +01003184}
3185
Michal Vasko1734be92020-09-22 08:55:10 +02003186LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02003187lyxp_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 +02003188 struct lyxp_expr **dup_p)
Michal Vasko004d3152020-06-11 19:59:22 +02003189{
Michal Vasko1734be92020-09-22 08:55:10 +02003190 LY_ERR ret = LY_SUCCESS;
3191 struct lyxp_expr *dup = NULL;
Michal Vaskod59039d2022-09-09 09:07:30 +02003192 uint32_t used = 0, i, j, expr_len;
Michal Vaskoe33134a2022-07-29 14:54:40 +02003193 const char *expr_start;
3194
3195 assert((!start_idx && !end_idx) || ((start_idx < exp->used) && (end_idx < exp->used) && (start_idx <= end_idx)));
Michal Vasko004d3152020-06-11 19:59:22 +02003196
Michal Vasko7f45cf22020-10-01 12:49:44 +02003197 if (!exp) {
3198 goto cleanup;
3199 }
3200
Michal Vaskoe33134a2022-07-29 14:54:40 +02003201 if (!start_idx && !end_idx) {
3202 end_idx = exp->used - 1;
3203 }
3204
3205 expr_start = exp->expr + exp->tok_pos[start_idx];
3206 expr_len = (exp->tok_pos[end_idx] + exp->tok_len[end_idx]) - exp->tok_pos[start_idx];
3207
Michal Vasko004d3152020-06-11 19:59:22 +02003208 dup = calloc(1, sizeof *dup);
Michal Vasko1734be92020-09-22 08:55:10 +02003209 LY_CHECK_ERR_GOTO(!dup, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02003210
Michal Vasko08e9b112021-06-11 15:41:17 +02003211 if (exp->used) {
Michal Vaskoe33134a2022-07-29 14:54:40 +02003212 used = (end_idx - start_idx) + 1;
3213
3214 dup->tokens = malloc(used * sizeof *dup->tokens);
Michal Vasko08e9b112021-06-11 15:41:17 +02003215 LY_CHECK_ERR_GOTO(!dup->tokens, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vaskoe33134a2022-07-29 14:54:40 +02003216 memcpy(dup->tokens, exp->tokens + start_idx, used * sizeof *dup->tokens);
Michal Vasko004d3152020-06-11 19:59:22 +02003217
Michal Vaskoe33134a2022-07-29 14:54:40 +02003218 dup->tok_pos = malloc(used * sizeof *dup->tok_pos);
Michal Vasko08e9b112021-06-11 15:41:17 +02003219 LY_CHECK_ERR_GOTO(!dup->tok_pos, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vaskoe33134a2022-07-29 14:54:40 +02003220 memcpy(dup->tok_pos, exp->tok_pos + start_idx, used * sizeof *dup->tok_pos);
Michal Vasko004d3152020-06-11 19:59:22 +02003221
Michal Vaskoe33134a2022-07-29 14:54:40 +02003222 if (start_idx) {
3223 /* fix the indices in the expression */
3224 for (i = 0; i < used; ++i) {
3225 dup->tok_pos[i] -= expr_start - exp->expr;
3226 }
3227 }
3228
3229 dup->tok_len = malloc(used * sizeof *dup->tok_len);
Michal Vasko08e9b112021-06-11 15:41:17 +02003230 LY_CHECK_ERR_GOTO(!dup->tok_len, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vaskoe33134a2022-07-29 14:54:40 +02003231 memcpy(dup->tok_len, exp->tok_len + start_idx, used * sizeof *dup->tok_len);
Michal Vasko004d3152020-06-11 19:59:22 +02003232
Michal Vasko79a7a872022-06-17 09:00:48 +02003233 if (exp->repeat) {
Michal Vaskoe33134a2022-07-29 14:54:40 +02003234 dup->repeat = malloc(used * sizeof *dup->repeat);
Michal Vasko79a7a872022-06-17 09:00:48 +02003235 LY_CHECK_ERR_GOTO(!dup->repeat, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vaskoe33134a2022-07-29 14:54:40 +02003236 for (i = start_idx; i <= end_idx; ++i) {
Michal Vasko79a7a872022-06-17 09:00:48 +02003237 if (!exp->repeat[i]) {
Michal Vaskoe33134a2022-07-29 14:54:40 +02003238 dup->repeat[i - start_idx] = NULL;
Michal Vasko79a7a872022-06-17 09:00:48 +02003239 } else {
3240 for (j = 0; exp->repeat[i][j]; ++j) {}
3241 /* the ending 0 as well */
3242 ++j;
Michal Vasko004d3152020-06-11 19:59:22 +02003243
Michal Vaskoe33134a2022-07-29 14:54:40 +02003244 dup->repeat[i - start_idx] = malloc(j * sizeof **dup->repeat);
3245 LY_CHECK_ERR_GOTO(!dup->repeat[i - start_idx], LOGMEM(ctx); ret = LY_EMEM, cleanup);
3246 memcpy(dup->repeat[i - start_idx], exp->repeat[i], j * sizeof **dup->repeat);
Michal Vasko79a7a872022-06-17 09:00:48 +02003247 }
Michal Vasko08e9b112021-06-11 15:41:17 +02003248 }
Michal Vasko004d3152020-06-11 19:59:22 +02003249 }
3250 }
3251
Michal Vaskoe33134a2022-07-29 14:54:40 +02003252 dup->used = used;
3253 dup->size = used;
3254
3255 /* copy only subexpression */
3256 LY_CHECK_GOTO(ret = lydict_insert(ctx, expr_start, expr_len, &dup->expr), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02003257
Michal Vasko1734be92020-09-22 08:55:10 +02003258cleanup:
3259 if (ret) {
3260 lyxp_expr_free(ctx, dup);
3261 } else {
3262 *dup_p = dup;
3263 }
3264 return ret;
Michal Vasko004d3152020-06-11 19:59:22 +02003265}
3266
Michal Vasko03ff5a72019-09-11 13:49:33 +02003267/**
3268 * @brief Get the last-added schema node that is currently in the context.
3269 *
3270 * @param[in] set Set to search in.
3271 * @return Last-added schema context node, NULL if no node is in context.
3272 */
3273static struct lysc_node *
3274warn_get_scnode_in_ctx(struct lyxp_set *set)
3275{
3276 uint32_t i;
3277
3278 if (!set || (set->type != LYXP_SET_SCNODE_SET)) {
3279 return NULL;
3280 }
3281
3282 i = set->used;
3283 do {
3284 --i;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003285 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003286 /* if there are more, simply return the first found (last added) */
3287 return set->val.scnodes[i].scnode;
3288 }
3289 } while (i);
3290
3291 return NULL;
3292}
3293
3294/**
3295 * @brief Test whether a type is numeric - integer type or decimal64.
3296 *
3297 * @param[in] type Type to test.
Radek Krejci857189e2020-09-01 13:26:36 +02003298 * @return Boolean value whether @p type is numeric type or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003299 */
Radek Krejci857189e2020-09-01 13:26:36 +02003300static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003301warn_is_numeric_type(struct lysc_type *type)
3302{
3303 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003304 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003305 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003306
3307 switch (type->basetype) {
3308 case LY_TYPE_DEC64:
3309 case LY_TYPE_INT8:
3310 case LY_TYPE_UINT8:
3311 case LY_TYPE_INT16:
3312 case LY_TYPE_UINT16:
3313 case LY_TYPE_INT32:
3314 case LY_TYPE_UINT32:
3315 case LY_TYPE_INT64:
3316 case LY_TYPE_UINT64:
3317 return 1;
3318 case LY_TYPE_UNION:
3319 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003320 LY_ARRAY_FOR(uni->types, u) {
3321 ret = warn_is_numeric_type(uni->types[u]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003322 if (ret) {
3323 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003324 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003325 }
3326 }
3327 /* did not find any suitable type */
3328 return 0;
3329 case LY_TYPE_LEAFREF:
3330 return warn_is_numeric_type(((struct lysc_type_leafref *)type)->realtype);
3331 default:
3332 return 0;
3333 }
3334}
3335
3336/**
3337 * @brief Test whether a type is string-like - no integers, decimal64 or binary.
3338 *
3339 * @param[in] type Type to test.
Radek Krejci857189e2020-09-01 13:26:36 +02003340 * @return Boolean value whether @p type's basetype is string type or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003341 */
Radek Krejci857189e2020-09-01 13:26:36 +02003342static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003343warn_is_string_type(struct lysc_type *type)
3344{
3345 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003346 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003347 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003348
3349 switch (type->basetype) {
3350 case LY_TYPE_BITS:
3351 case LY_TYPE_ENUM:
3352 case LY_TYPE_IDENT:
3353 case LY_TYPE_INST:
3354 case LY_TYPE_STRING:
3355 return 1;
3356 case LY_TYPE_UNION:
3357 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003358 LY_ARRAY_FOR(uni->types, u) {
3359 ret = warn_is_string_type(uni->types[u]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003360 if (ret) {
3361 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003362 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003363 }
3364 }
3365 /* did not find any suitable type */
3366 return 0;
3367 case LY_TYPE_LEAFREF:
3368 return warn_is_string_type(((struct lysc_type_leafref *)type)->realtype);
3369 default:
3370 return 0;
3371 }
3372}
3373
3374/**
3375 * @brief Test whether a type is one specific type.
3376 *
3377 * @param[in] type Type to test.
3378 * @param[in] base Expected type.
Radek Krejci857189e2020-09-01 13:26:36 +02003379 * @return Boolean value whether the given @p type is of the specific basetype @p base.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003380 */
Radek Krejci857189e2020-09-01 13:26:36 +02003381static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003382warn_is_specific_type(struct lysc_type *type, LY_DATA_TYPE base)
3383{
3384 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003385 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003386 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003387
3388 if (type->basetype == base) {
3389 return 1;
3390 } else if (type->basetype == LY_TYPE_UNION) {
3391 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003392 LY_ARRAY_FOR(uni->types, u) {
3393 ret = warn_is_specific_type(uni->types[u], base);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003394 if (ret) {
3395 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003396 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003397 }
3398 }
3399 /* did not find any suitable type */
3400 return 0;
3401 } else if (type->basetype == LY_TYPE_LEAFREF) {
3402 return warn_is_specific_type(((struct lysc_type_leafref *)type)->realtype, base);
3403 }
3404
3405 return 0;
3406}
3407
3408/**
3409 * @brief Get next type of a (union) type.
3410 *
3411 * @param[in] type Base type.
3412 * @param[in] prev_type Previously returned type.
3413 * @return Next type or NULL.
3414 */
3415static struct lysc_type *
3416warn_is_equal_type_next_type(struct lysc_type *type, struct lysc_type *prev_type)
3417{
3418 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003419 ly_bool found = 0;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003420 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003421
3422 switch (type->basetype) {
3423 case LY_TYPE_UNION:
3424 uni = (struct lysc_type_union *)type;
3425 if (!prev_type) {
3426 return uni->types[0];
3427 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003428 LY_ARRAY_FOR(uni->types, u) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003429 if (found) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003430 return uni->types[u];
Michal Vasko03ff5a72019-09-11 13:49:33 +02003431 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003432 if (prev_type == uni->types[u]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003433 found = 1;
3434 }
3435 }
3436 return NULL;
3437 default:
3438 if (prev_type) {
3439 assert(type == prev_type);
3440 return NULL;
3441 } else {
3442 return type;
3443 }
3444 }
3445}
3446
3447/**
3448 * @brief Test whether 2 types have a common type.
3449 *
3450 * @param[in] type1 First type.
3451 * @param[in] type2 Second type.
3452 * @return 1 if they do, 0 otherwise.
3453 */
3454static int
3455warn_is_equal_type(struct lysc_type *type1, struct lysc_type *type2)
3456{
3457 struct lysc_type *t1, *rt1, *t2, *rt2;
3458
3459 t1 = NULL;
3460 while ((t1 = warn_is_equal_type_next_type(type1, t1))) {
3461 if (t1->basetype == LY_TYPE_LEAFREF) {
3462 rt1 = ((struct lysc_type_leafref *)t1)->realtype;
3463 } else {
3464 rt1 = t1;
3465 }
3466
3467 t2 = NULL;
3468 while ((t2 = warn_is_equal_type_next_type(type2, t2))) {
3469 if (t2->basetype == LY_TYPE_LEAFREF) {
3470 rt2 = ((struct lysc_type_leafref *)t2)->realtype;
3471 } else {
3472 rt2 = t2;
3473 }
3474
3475 if (rt2->basetype == rt1->basetype) {
3476 /* match found */
3477 return 1;
3478 }
3479 }
3480 }
3481
3482 return 0;
3483}
3484
3485/**
Michal Vaskoaa956522021-11-11 10:45:34 +01003486 * @brief Print warning with information about the XPath subexpression that caused previous warning.
3487 *
3488 * @param[in] ctx Context for logging.
3489 * @param[in] tok_pos Index of the subexpression in the whole expression.
3490 * @param[in] subexpr Subexpression start.
3491 * @param[in] subexpr_len Length of @p subexpr to print.
3492 * @param[in] cur_scnode Expression context node.
3493 */
3494static void
Michal Vaskodd528af2022-08-08 14:35:07 +02003495warn_subexpr_log(const struct ly_ctx *ctx, uint32_t tok_pos, const char *subexpr, int subexpr_len,
Michal Vaskoaa956522021-11-11 10:45:34 +01003496 const struct lysc_node *cur_scnode)
3497{
3498 char *path;
3499
3500 path = lysc_path(cur_scnode, LYSC_PATH_LOG, NULL, 0);
Michal Vaskodd528af2022-08-08 14:35:07 +02003501 LOGWRN(ctx, "Previous warning generated by XPath subexpression[%" PRIu32 "] \"%.*s\" with context node \"%s\".",
Michal Vaskoaa956522021-11-11 10:45:34 +01003502 tok_pos, subexpr_len, subexpr, path);
3503 free(path);
3504}
3505
3506/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02003507 * @brief Check both operands of comparison operators.
3508 *
3509 * @param[in] ctx Context for errors.
3510 * @param[in] set1 First operand set.
3511 * @param[in] set2 Second operand set.
3512 * @param[in] numbers_only Whether accept only numbers or other types are fine too (for '=' and '!=').
3513 * @param[in] expr Start of the expression to print with the warning.
3514 * @param[in] tok_pos Token position.
3515 */
3516static void
Michal Vaskoaa956522021-11-11 10:45:34 +01003517warn_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 +02003518 uint32_t tok_pos)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003519{
3520 struct lysc_node_leaf *node1, *node2;
Radek Krejci857189e2020-09-01 13:26:36 +02003521 ly_bool leaves = 1, warning = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003522
3523 node1 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set1);
3524 node2 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set2);
3525
3526 if (!node1 && !node2) {
3527 /* no node-sets involved, nothing to do */
3528 return;
3529 }
3530
3531 if (node1) {
3532 if (!(node1->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3533 LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node1->nodetype), node1->name);
3534 warning = 1;
3535 leaves = 0;
3536 } else if (numbers_only && !warn_is_numeric_type(node1->type)) {
3537 LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node1->name);
3538 warning = 1;
3539 }
3540 }
3541
3542 if (node2) {
3543 if (!(node2->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3544 LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node2->nodetype), node2->name);
3545 warning = 1;
3546 leaves = 0;
3547 } else if (numbers_only && !warn_is_numeric_type(node2->type)) {
3548 LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node2->name);
3549 warning = 1;
3550 }
3551 }
3552
3553 if (node1 && node2 && leaves && !numbers_only) {
Michal Vasko69730152020-10-09 16:30:07 +02003554 if ((warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type)) ||
3555 (!warn_is_numeric_type(node1->type) && warn_is_numeric_type(node2->type)) ||
3556 (!warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type) &&
3557 !warn_is_equal_type(node1->type, node2->type))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003558 LOGWRN(ctx, "Incompatible types of operands \"%s\" and \"%s\" for comparison.", node1->name, node2->name);
3559 warning = 1;
3560 }
3561 }
3562
3563 if (warning) {
Michal Vaskoaa956522021-11-11 10:45:34 +01003564 warn_subexpr_log(ctx, tok_pos, expr + tok_pos, 20, set1->cur_scnode);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003565 }
3566}
3567
3568/**
3569 * @brief Check that a value is valid for a leaf. If not applicable, does nothing.
3570 *
3571 * @param[in] exp Parsed XPath expression.
3572 * @param[in] set Set with the leaf/leaf-list.
3573 * @param[in] val_exp Index of the value (literal/number) in @p exp.
3574 * @param[in] equal_exp Index of the start of the equality expression in @p exp.
3575 * @param[in] last_equal_exp Index of the end of the equality expression in @p exp.
3576 */
3577static void
Michal Vaskodd528af2022-08-08 14:35:07 +02003578warn_equality_value(const struct lyxp_expr *exp, struct lyxp_set *set, uint32_t val_exp, uint32_t equal_exp,
3579 uint32_t last_equal_exp)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003580{
3581 struct lysc_node *scnode;
3582 struct lysc_type *type;
3583 char *value;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003584 struct lyd_value storage;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003585 LY_ERR rc;
3586 struct ly_err_item *err = NULL;
3587
Michal Vasko69730152020-10-09 16:30:07 +02003588 if ((scnode = warn_get_scnode_in_ctx(set)) && (scnode->nodetype & (LYS_LEAF | LYS_LEAFLIST)) &&
3589 ((exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) || (exp->tokens[val_exp] == LYXP_TOKEN_NUMBER))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003590 /* check that the node can have the specified value */
3591 if (exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) {
3592 value = strndup(exp->expr + exp->tok_pos[val_exp] + 1, exp->tok_len[val_exp] - 2);
3593 } else {
3594 value = strndup(exp->expr + exp->tok_pos[val_exp], exp->tok_len[val_exp]);
3595 }
3596 if (!value) {
3597 LOGMEM(set->ctx);
3598 return;
3599 }
3600
3601 if ((((struct lysc_node_leaf *)scnode)->type->basetype == LY_TYPE_IDENT) && !strchr(value, ':')) {
3602 LOGWRN(set->ctx, "Identityref \"%s\" comparison with identity \"%s\" without prefix, consider adding"
Michal Vasko69730152020-10-09 16:30:07 +02003603 " a prefix or best using \"derived-from(-or-self)()\" functions.", scnode->name, value);
Michal Vaskoaa956522021-11-11 10:45:34 +01003604 warn_subexpr_log(set->ctx, exp->tok_pos[equal_exp], exp->expr + exp->tok_pos[equal_exp],
Radek Krejci0f969882020-08-21 16:56:47 +02003605 (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp],
Michal Vaskoaa956522021-11-11 10:45:34 +01003606 set->cur_scnode);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003607 }
3608
3609 type = ((struct lysc_node_leaf *)scnode)->type;
3610 if (type->basetype != LY_TYPE_IDENT) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003611 rc = type->plugin->store(set->ctx, type, value, strlen(value), 0, set->format, set->prefix_data,
Michal Vasko405cc9e2020-12-01 12:01:27 +01003612 LYD_HINT_DATA, scnode, &storage, NULL, &err);
Michal Vaskobf42e832020-11-23 16:59:42 +01003613 if (rc == LY_EINCOMPLETE) {
3614 rc = LY_SUCCESS;
3615 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003616
3617 if (err) {
3618 LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type (%s).", value, err->msg);
3619 ly_err_free(err);
3620 } else if (rc != LY_SUCCESS) {
3621 LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type.", value);
3622 }
3623 if (rc != LY_SUCCESS) {
Michal Vaskoaa956522021-11-11 10:45:34 +01003624 warn_subexpr_log(set->ctx, exp->tok_pos[equal_exp], exp->expr + exp->tok_pos[equal_exp],
Radek Krejci0f969882020-08-21 16:56:47 +02003625 (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp],
Michal Vaskoaa956522021-11-11 10:45:34 +01003626 set->cur_scnode);
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003627 } else {
3628 type->plugin->free(set->ctx, &storage);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003629 }
3630 }
3631 free(value);
3632 }
3633}
3634
3635/*
3636 * XPath functions
3637 */
3638
3639/**
3640 * @brief Execute the YANG 1.1 bit-is-set(node-set, string) function. Returns LYXP_SET_BOOLEAN
3641 * depending on whether the first node bit value from the second argument is set.
3642 *
3643 * @param[in] args Array of arguments.
3644 * @param[in] arg_count Count of elements in @p args.
3645 * @param[in,out] set Context and result set at the same time.
3646 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003647 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003648 */
3649static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02003650xpath_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 +02003651{
3652 struct lyd_node_term *leaf;
3653 struct lysc_node_leaf *sleaf;
Michal Vasko2588b952021-07-29 07:43:26 +02003654 struct lyd_value_bits *bits;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003655 LY_ERR rc = LY_SUCCESS;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003656 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003657
3658 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003659 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003660 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003661 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3662 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3663 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3664 sleaf->name);
3665 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_BITS)) {
3666 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"bits\".", __func__, sleaf->name);
3667 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003668 }
3669
3670 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3671 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003672 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3673 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003674 } else if (!warn_is_string_type(sleaf->type)) {
3675 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003676 }
3677 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003678 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003679 return rc;
3680 }
3681
Michal Vaskod3678892020-05-21 10:06:58 +02003682 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003683 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 +02003684 return LY_EVALID;
3685 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003686 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003687 LY_CHECK_RET(rc);
3688
3689 set_fill_boolean(set, 0);
Michal Vaskod3678892020-05-21 10:06:58 +02003690 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003691 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
Michal Vasko2588b952021-07-29 07:43:26 +02003692 if ((leaf->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (leaf->value.realtype->basetype == LY_TYPE_BITS)) {
3693 LYD_VALUE_GET(&leaf->value, bits);
3694 LY_ARRAY_FOR(bits->items, u) {
3695 if (!strcmp(bits->items[u]->name, args[1]->val.str)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003696 set_fill_boolean(set, 1);
3697 break;
3698 }
3699 }
3700 }
3701 }
3702
3703 return LY_SUCCESS;
3704}
3705
3706/**
3707 * @brief Execute the XPath boolean(object) function. Returns LYXP_SET_BOOLEAN
3708 * with the argument converted to boolean.
3709 *
3710 * @param[in] args Array of arguments.
3711 * @param[in] arg_count Count of elements in @p args.
3712 * @param[in,out] set Context and result set at the same time.
3713 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003714 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003715 */
3716static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02003717xpath_boolean(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003718{
3719 LY_ERR rc;
3720
3721 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02003722 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003723 return LY_SUCCESS;
3724 }
3725
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003726 rc = lyxp_set_cast(args[0], LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003727 LY_CHECK_RET(rc);
3728 set_fill_set(set, args[0]);
3729
3730 return LY_SUCCESS;
3731}
3732
3733/**
3734 * @brief Execute the XPath ceiling(number) function. Returns LYXP_SET_NUMBER
3735 * with the first argument rounded up to the nearest integer.
3736 *
3737 * @param[in] args Array of arguments.
3738 * @param[in] arg_count Count of elements in @p args.
3739 * @param[in,out] set Context and result set at the same time.
3740 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003741 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003742 */
3743static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02003744xpath_ceiling(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003745{
3746 struct lysc_node_leaf *sleaf;
3747 LY_ERR rc = LY_SUCCESS;
3748
3749 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003750 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003751 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003752 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3753 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3754 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3755 sleaf->name);
3756 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
3757 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
3758 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003759 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003760 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003761 return rc;
3762 }
3763
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003764 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003765 LY_CHECK_RET(rc);
3766 if ((long long)args[0]->val.num != args[0]->val.num) {
3767 set_fill_number(set, ((long long)args[0]->val.num) + 1);
3768 } else {
3769 set_fill_number(set, args[0]->val.num);
3770 }
3771
3772 return LY_SUCCESS;
3773}
3774
3775/**
3776 * @brief Execute the XPath concat(string, string, string*) function.
3777 * Returns LYXP_SET_STRING with the concatenation of all the arguments.
3778 *
3779 * @param[in] args Array of arguments.
3780 * @param[in] arg_count Count of elements in @p args.
3781 * @param[in,out] set Context and result set at the same time.
3782 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003783 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003784 */
3785static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02003786xpath_concat(struct lyxp_set **args, uint32_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003787{
Michal Vaskodd528af2022-08-08 14:35:07 +02003788 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003789 char *str = NULL;
3790 size_t used = 1;
3791 LY_ERR rc = LY_SUCCESS;
3792 struct lysc_node_leaf *sleaf;
3793
3794 if (options & LYXP_SCNODE_ALL) {
3795 for (i = 0; i < arg_count; ++i) {
3796 if ((args[i]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[i]))) {
3797 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3798 LOGWRN(set->ctx, "Argument #%u of %s is a %s node \"%s\".",
Michal Vasko69730152020-10-09 16:30:07 +02003799 i + 1, __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003800 } else if (!warn_is_string_type(sleaf->type)) {
Radek Krejci70124c82020-08-14 22:17:03 +02003801 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 +02003802 }
3803 }
3804 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003805 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003806 return rc;
3807 }
3808
3809 for (i = 0; i < arg_count; ++i) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003810 rc = lyxp_set_cast(args[i], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003811 if (rc != LY_SUCCESS) {
3812 free(str);
3813 return rc;
3814 }
3815
3816 str = ly_realloc(str, (used + strlen(args[i]->val.str)) * sizeof(char));
3817 LY_CHECK_ERR_RET(!str, LOGMEM(set->ctx), LY_EMEM);
3818 strcpy(str + used - 1, args[i]->val.str);
3819 used += strlen(args[i]->val.str);
3820 }
3821
3822 /* free, kind of */
Michal Vaskod3678892020-05-21 10:06:58 +02003823 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003824 set->type = LYXP_SET_STRING;
3825 set->val.str = str;
3826
3827 return LY_SUCCESS;
3828}
3829
3830/**
3831 * @brief Execute the XPath contains(string, string) function.
3832 * Returns LYXP_SET_BOOLEAN whether the second argument can
3833 * be found in the first or not.
3834 *
3835 * @param[in] args Array of arguments.
3836 * @param[in] arg_count Count of elements in @p args.
3837 * @param[in,out] set Context and result set at the same time.
3838 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003839 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003840 */
3841static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02003842xpath_contains(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003843{
3844 struct lysc_node_leaf *sleaf;
3845 LY_ERR rc = LY_SUCCESS;
3846
3847 if (options & LYXP_SCNODE_ALL) {
3848 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3849 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003850 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3851 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003852 } else if (!warn_is_string_type(sleaf->type)) {
3853 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003854 }
3855 }
3856
3857 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3858 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003859 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3860 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003861 } else if (!warn_is_string_type(sleaf->type)) {
3862 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003863 }
3864 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003865 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003866 return rc;
3867 }
3868
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003869 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003870 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003871 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003872 LY_CHECK_RET(rc);
3873
3874 if (strstr(args[0]->val.str, args[1]->val.str)) {
3875 set_fill_boolean(set, 1);
3876 } else {
3877 set_fill_boolean(set, 0);
3878 }
3879
3880 return LY_SUCCESS;
3881}
3882
3883/**
3884 * @brief Execute the XPath count(node-set) function. Returns LYXP_SET_NUMBER
3885 * with the size of the node-set from the argument.
3886 *
3887 * @param[in] args Array of arguments.
3888 * @param[in] arg_count Count of elements in @p args.
3889 * @param[in,out] set Context and result set at the same time.
3890 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003891 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003892 */
3893static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02003894xpath_count(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003895{
Michal Vasko03ff5a72019-09-11 13:49:33 +02003896 LY_ERR rc = LY_SUCCESS;
3897
3898 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003899 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003900 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003901 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003902 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003903 return rc;
3904 }
3905
Michal Vasko03ff5a72019-09-11 13:49:33 +02003906 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003907 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "count(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003908 return LY_EVALID;
3909 }
3910
3911 set_fill_number(set, args[0]->used);
3912 return LY_SUCCESS;
3913}
3914
3915/**
3916 * @brief Execute the XPath current() function. Returns LYXP_SET_NODE_SET
3917 * with the context with the intial node.
3918 *
3919 * @param[in] args Array of arguments.
3920 * @param[in] arg_count Count of elements in @p args.
3921 * @param[in,out] set Context and result set at the same time.
3922 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003923 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003924 */
3925static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02003926xpath_current(struct lyxp_set **args, uint32_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003927{
3928 if (arg_count || args) {
Radek Krejcie87c7dc2021-06-02 21:25:42 +02003929 LOGVAL(set->ctx, LY_VCODE_XP_INARGCOUNT, arg_count, LY_PRI_LENSTR("current()"));
Michal Vasko03ff5a72019-09-11 13:49:33 +02003930 return LY_EVALID;
3931 }
3932
3933 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02003934 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003935
Michal Vasko296dfaf2021-12-13 16:57:42 +01003936 if (set->cur_scnode) {
Michal Vasko7333cb32022-07-29 16:30:29 +02003937 LY_CHECK_RET(set_scnode_insert_node(set, set->cur_scnode, LYXP_NODE_ELEM, LYXP_AXIS_SELF, NULL));
Michal Vasko296dfaf2021-12-13 16:57:42 +01003938 } else {
3939 /* root node */
Michal Vasko7333cb32022-07-29 16:30:29 +02003940 LY_CHECK_RET(set_scnode_insert_node(set, NULL, set->root_type, LYXP_AXIS_SELF, NULL));
Michal Vasko296dfaf2021-12-13 16:57:42 +01003941 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003942 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02003943 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003944
Michal Vasko296dfaf2021-12-13 16:57:42 +01003945 if (set->cur_node) {
3946 /* position is filled later */
3947 set_insert_node(set, set->cur_node, 0, LYXP_NODE_ELEM, 0);
3948 } else {
3949 /* root node */
3950 set_insert_node(set, NULL, 0, set->root_type, 0);
3951 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003952 }
3953
3954 return LY_SUCCESS;
3955}
3956
3957/**
3958 * @brief Execute the YANG 1.1 deref(node-set) function. Returns LYXP_SET_NODE_SET with either
3959 * leafref or instance-identifier target node(s).
3960 *
3961 * @param[in] args Array of arguments.
3962 * @param[in] arg_count Count of elements in @p args.
3963 * @param[in,out] set Context and result set at the same time.
3964 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003965 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003966 */
3967static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02003968xpath_deref(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003969{
3970 struct lyd_node_term *leaf;
Michal Vasko42e497c2020-01-06 08:38:25 +01003971 struct lysc_node_leaf *sleaf = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02003972 struct lysc_type_leafref *lref;
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003973 const struct lysc_node *target;
Michal Vasko004d3152020-06-11 19:59:22 +02003974 struct ly_path *p;
3975 struct lyd_node *node;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003976 char *errmsg = NULL;
Michal Vasko00cbf532020-06-15 13:58:47 +02003977 uint8_t oper;
Michal Vasko741bb562021-06-24 11:59:50 +02003978 LY_ERR r;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003979
3980 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003981 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003982 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003983 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
Michal Vasko423ba3f2021-07-19 13:08:50 +02003984 if (!(sleaf->nodetype & LYD_NODE_TERM)) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003985 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3986 sleaf->name);
Michal Vaskoed725d72021-06-23 12:03:45 +02003987 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_LEAFREF) &&
3988 !warn_is_specific_type(sleaf->type, LY_TYPE_INST)) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003989 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"leafref\" nor \"instance-identifier\".",
3990 __func__, sleaf->name);
3991 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003992 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003993 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko423ba3f2021-07-19 13:08:50 +02003994 if (sleaf && (sleaf->nodetype & LYD_NODE_TERM) && (sleaf->type->basetype == LY_TYPE_LEAFREF)) {
Michal Vasko004d3152020-06-11 19:59:22 +02003995 lref = (struct lysc_type_leafref *)sleaf->type;
Michal Vaskod1e53b92021-01-28 13:11:06 +01003996 oper = (sleaf->flags & LYS_IS_OUTPUT) ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT;
Michal Vasko004d3152020-06-11 19:59:22 +02003997
3998 /* it was already evaluated on schema, it must succeed */
Michal Vasko741bb562021-06-24 11:59:50 +02003999 r = ly_path_compile_leafref(set->ctx, &sleaf->node, NULL, lref->path, oper, LY_PATH_TARGET_MANY,
Michal Vasko24fc4d12021-07-12 14:41:20 +02004000 LY_VALUE_SCHEMA_RESOLVED, lref->prefixes, &p);
Michal Vasko741bb562021-06-24 11:59:50 +02004001 if (!r) {
4002 /* get the target node */
4003 target = p[LY_ARRAY_COUNT(p) - 1].node;
4004 ly_path_free(set->ctx, p);
Michal Vasko004d3152020-06-11 19:59:22 +02004005
Michal Vasko7333cb32022-07-29 16:30:29 +02004006 LY_CHECK_RET(set_scnode_insert_node(set, target, LYXP_NODE_ELEM, LYXP_AXIS_SELF, NULL));
Michal Vasko741bb562021-06-24 11:59:50 +02004007 } /* else the target was found before but is disabled so it was removed */
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02004008 }
4009
Michal Vasko741bb562021-06-24 11:59:50 +02004010 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004011 }
4012
Michal Vaskod3678892020-05-21 10:06:58 +02004013 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004014 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "deref(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004015 return LY_EVALID;
4016 }
4017
Michal Vaskod3678892020-05-21 10:06:58 +02004018 lyxp_set_free_content(set);
4019 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004020 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
4021 sleaf = (struct lysc_node_leaf *)leaf->schema;
4022 if (sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
4023 if (sleaf->type->basetype == LY_TYPE_LEAFREF) {
4024 /* find leafref target */
Radek Krejci0b013302021-03-29 15:22:32 +02004025 if (lyplg_type_resolve_leafref((struct lysc_type_leafref *)sleaf->type, &leaf->node, &leaf->value, set->tree,
Michal Vasko9e685082021-01-29 14:49:09 +01004026 &node, &errmsg)) {
Michal Vasko004d3152020-06-11 19:59:22 +02004027 LOGERR(set->ctx, LY_EVALID, errmsg);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004028 free(errmsg);
Michal Vasko004d3152020-06-11 19:59:22 +02004029 return LY_EVALID;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004030 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004031 } else {
4032 assert(sleaf->type->basetype == LY_TYPE_INST);
Michal Vasko004d3152020-06-11 19:59:22 +02004033 if (ly_path_eval(leaf->value.target, set->tree, &node)) {
Michal Vaskoba99a3e2020-08-18 15:50:05 +02004034 LOGERR(set->ctx, LY_EVALID, "Invalid instance-identifier \"%s\" value - required instance not found.",
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02004035 lyd_get_value(&leaf->node));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004036 return LY_EVALID;
4037 }
4038 }
Michal Vasko004d3152020-06-11 19:59:22 +02004039
4040 /* insert it */
4041 set_insert_node(set, node, 0, LYXP_NODE_ELEM, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004042 }
4043 }
4044
4045 return LY_SUCCESS;
4046}
4047
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004048static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02004049xpath_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 +02004050{
Michal Vaskofe1af042022-07-29 14:58:59 +02004051 uint32_t i, id_len;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004052 LY_ARRAY_COUNT_TYPE u;
4053 struct lyd_node_term *leaf;
4054 struct lysc_node_leaf *sleaf;
4055 struct lyd_meta *meta;
Michal Vasko93923692021-05-07 15:28:02 +02004056 struct lyd_value *val;
4057 const struct lys_module *mod;
4058 const char *id_name;
Michal Vasko93923692021-05-07 15:28:02 +02004059 struct lysc_ident *id;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004060 LY_ERR rc = LY_SUCCESS;
Radek Krejci857189e2020-09-01 13:26:36 +02004061 ly_bool found;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004062
4063 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02004064 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004065 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", func);
Michal Vasko5676f4e2021-04-06 17:14:45 +02004066 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4067 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4068 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype),
4069 sleaf->name);
4070 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_IDENT)) {
4071 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"identityref\".", func, sleaf->name);
4072 }
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004073 }
4074
4075 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4076 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4077 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype),
Michal Vasko69730152020-10-09 16:30:07 +02004078 sleaf->name);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004079 } else if (!warn_is_string_type(sleaf->type)) {
4080 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", func, sleaf->name);
4081 }
4082 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004083 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004084 return rc;
4085 }
4086
4087 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004088 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 +02004089 return LY_EVALID;
4090 }
4091 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
4092 LY_CHECK_RET(rc);
4093
Michal Vasko93923692021-05-07 15:28:02 +02004094 /* parse the identity */
4095 id_name = args[1]->val.str;
4096 id_len = strlen(id_name);
4097 rc = moveto_resolve_model(&id_name, &id_len, set, set->cur_node ? set->cur_node->schema : NULL, &mod);
4098 LY_CHECK_RET(rc);
4099 if (!mod) {
4100 LOGVAL(set->ctx, LYVE_XPATH, "Identity \"%.*s\" without a prefix.", (int)id_len, id_name);
4101 return LY_EVALID;
4102 }
4103
4104 /* find the identity */
4105 found = 0;
4106 LY_ARRAY_FOR(mod->identities, u) {
4107 if (!ly_strncmp(mod->identities[u].name, id_name, id_len)) {
4108 /* we have match */
4109 found = 1;
4110 break;
4111 }
4112 }
4113 if (!found) {
4114 LOGVAL(set->ctx, LYVE_XPATH, "Identity \"%.*s\" not found in module \"%s\".", (int)id_len, id_name, mod->name);
4115 return LY_EVALID;
4116 }
4117 id = &mod->identities[u];
4118
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004119 set_fill_boolean(set, 0);
4120 found = 0;
4121 for (i = 0; i < args[0]->used; ++i) {
4122 if ((args[0]->val.nodes[i].type != LYXP_NODE_ELEM) && (args[0]->val.nodes[i].type != LYXP_NODE_META)) {
4123 continue;
4124 }
4125
4126 if (args[0]->val.nodes[i].type == LYXP_NODE_ELEM) {
4127 leaf = (struct lyd_node_term *)args[0]->val.nodes[i].node;
4128 sleaf = (struct lysc_node_leaf *)leaf->schema;
4129 val = &leaf->value;
4130 if (!(sleaf->nodetype & LYD_NODE_TERM) || (leaf->value.realtype->basetype != LY_TYPE_IDENT)) {
4131 /* uninteresting */
4132 continue;
4133 }
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004134 } else {
4135 meta = args[0]->val.meta[i].meta;
4136 val = &meta->value;
4137 if (val->realtype->basetype != LY_TYPE_IDENT) {
4138 /* uninteresting */
4139 continue;
4140 }
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004141 }
4142
Michal Vasko93923692021-05-07 15:28:02 +02004143 /* check the identity itself */
4144 if (self_match && (id == val->ident)) {
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004145 set_fill_boolean(set, 1);
4146 found = 1;
4147 }
Michal Vasko93923692021-05-07 15:28:02 +02004148 if (!found && !lyplg_type_identity_isderived(id, val->ident)) {
4149 set_fill_boolean(set, 1);
4150 found = 1;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004151 }
4152
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004153 if (found) {
4154 break;
4155 }
4156 }
4157
4158 return LY_SUCCESS;
4159}
4160
Michal Vasko03ff5a72019-09-11 13:49:33 +02004161/**
4162 * @brief Execute the YANG 1.1 derived-from(node-set, string) function. Returns LYXP_SET_BOOLEAN depending
4163 * on whether the first argument nodes contain a node of an identity derived from the second
4164 * argument identity.
4165 *
4166 * @param[in] args Array of arguments.
4167 * @param[in] arg_count Count of elements in @p args.
4168 * @param[in,out] set Context and result set at the same time.
4169 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004170 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004171 */
4172static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004173xpath_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 +02004174{
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004175 return xpath_derived_(args, set, options, 0, __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004176}
4177
4178/**
4179 * @brief Execute the YANG 1.1 derived-from-or-self(node-set, string) function. Returns LYXP_SET_BOOLEAN depending
4180 * on whether the first argument nodes contain a node of an identity that either is or is derived from
4181 * the second argument identity.
4182 *
4183 * @param[in] args Array of arguments.
4184 * @param[in] arg_count Count of elements in @p args.
4185 * @param[in,out] set Context and result set at the same time.
4186 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004187 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004188 */
4189static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004190xpath_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 +02004191{
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004192 return xpath_derived_(args, set, options, 1, __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004193}
4194
4195/**
4196 * @brief Execute the YANG 1.1 enum-value(node-set) function. Returns LYXP_SET_NUMBER
4197 * with the integer value of the first node's enum value, otherwise NaN.
4198 *
4199 * @param[in] args Array of arguments.
4200 * @param[in] arg_count Count of elements in @p args.
4201 * @param[in,out] set Context and result set at the same time.
4202 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004203 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004204 */
4205static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004206xpath_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 +02004207{
4208 struct lyd_node_term *leaf;
4209 struct lysc_node_leaf *sleaf;
4210 LY_ERR rc = LY_SUCCESS;
4211
4212 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02004213 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004214 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02004215 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4216 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4217 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
4218 sleaf->name);
4219 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_ENUM)) {
4220 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"enumeration\".", __func__, sleaf->name);
4221 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004222 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004223 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004224 return rc;
4225 }
4226
Michal Vaskod3678892020-05-21 10:06:58 +02004227 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004228 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "enum-value(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004229 return LY_EVALID;
4230 }
4231
4232 set_fill_number(set, NAN);
Michal Vaskod3678892020-05-21 10:06:58 +02004233 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004234 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
4235 sleaf = (struct lysc_node_leaf *)leaf->schema;
4236 if ((sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (sleaf->type->basetype == LY_TYPE_ENUM)) {
4237 set_fill_number(set, leaf->value.enum_item->value);
4238 }
4239 }
4240
4241 return LY_SUCCESS;
4242}
4243
4244/**
4245 * @brief Execute the XPath false() function. Returns LYXP_SET_BOOLEAN
4246 * with false value.
4247 *
4248 * @param[in] args Array of arguments.
4249 * @param[in] arg_count Count of elements in @p args.
4250 * @param[in,out] set Context and result set at the same time.
4251 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004252 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004253 */
4254static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004255xpath_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 +02004256{
4257 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004258 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004259 return LY_SUCCESS;
4260 }
4261
4262 set_fill_boolean(set, 0);
4263 return LY_SUCCESS;
4264}
4265
4266/**
4267 * @brief Execute the XPath floor(number) function. Returns LYXP_SET_NUMBER
4268 * with the first argument floored (truncated).
4269 *
4270 * @param[in] args Array of arguments.
4271 * @param[in] arg_count Count of elements in @p args.
4272 * @param[in,out] set Context and result set at the same time.
4273 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004274 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004275 */
4276static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004277xpath_floor(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t UNUSED(options))
Michal Vasko03ff5a72019-09-11 13:49:33 +02004278{
4279 LY_ERR rc;
4280
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004281 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004282 LY_CHECK_RET(rc);
4283 if (isfinite(args[0]->val.num)) {
4284 set_fill_number(set, (long long)args[0]->val.num);
4285 }
4286
4287 return LY_SUCCESS;
4288}
4289
4290/**
4291 * @brief Execute the XPath lang(string) function. Returns LYXP_SET_BOOLEAN
4292 * whether the language of the text matches the one from the argument.
4293 *
4294 * @param[in] args Array of arguments.
4295 * @param[in] arg_count Count of elements in @p args.
4296 * @param[in,out] set Context and result set at the same time.
4297 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004298 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004299 */
4300static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004301xpath_lang(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004302{
4303 const struct lyd_node *node;
4304 struct lysc_node_leaf *sleaf;
Michal Vasko9f96a052020-03-10 09:41:45 +01004305 struct lyd_meta *meta = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004306 const char *val;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004307 LY_ERR rc = LY_SUCCESS;
4308
4309 if (options & LYXP_SCNODE_ALL) {
4310 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4311 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004312 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
4313 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004314 } else if (!warn_is_string_type(sleaf->type)) {
4315 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004316 }
4317 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004318 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004319 return rc;
4320 }
4321
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004322 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004323 LY_CHECK_RET(rc);
4324
Michal Vasko03ff5a72019-09-11 13:49:33 +02004325 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004326 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "lang(string)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004327 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004328 } else if (!set->used) {
4329 set_fill_boolean(set, 0);
4330 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004331 }
4332
4333 switch (set->val.nodes[0].type) {
4334 case LYXP_NODE_ELEM:
4335 case LYXP_NODE_TEXT:
4336 node = set->val.nodes[0].node;
4337 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004338 case LYXP_NODE_META:
4339 node = set->val.meta[0].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004340 break;
4341 default:
4342 /* nothing to do with roots */
4343 set_fill_boolean(set, 0);
4344 return LY_SUCCESS;
4345 }
4346
Michal Vasko9f96a052020-03-10 09:41:45 +01004347 /* find lang metadata */
Michal Vasko9e685082021-01-29 14:49:09 +01004348 for ( ; node; node = lyd_parent(node)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01004349 for (meta = node->meta; meta; meta = meta->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004350 /* annotations */
Michal Vasko9f96a052020-03-10 09:41:45 +01004351 if (meta->name && !strcmp(meta->name, "lang") && !strcmp(meta->annotation->module->name, "xml")) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004352 break;
4353 }
4354 }
4355
Michal Vasko9f96a052020-03-10 09:41:45 +01004356 if (meta) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004357 break;
4358 }
4359 }
4360
4361 /* compare languages */
Michal Vasko9f96a052020-03-10 09:41:45 +01004362 if (!meta) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004363 set_fill_boolean(set, 0);
4364 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004365 uint64_t i;
4366
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02004367 val = lyd_get_meta_value(meta);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004368 for (i = 0; args[0]->val.str[i]; ++i) {
4369 if (tolower(args[0]->val.str[i]) != tolower(val[i])) {
4370 set_fill_boolean(set, 0);
4371 break;
4372 }
4373 }
4374 if (!args[0]->val.str[i]) {
4375 if (!val[i] || (val[i] == '-')) {
4376 set_fill_boolean(set, 1);
4377 } else {
4378 set_fill_boolean(set, 0);
4379 }
4380 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004381 }
4382
4383 return LY_SUCCESS;
4384}
4385
4386/**
4387 * @brief Execute the XPath last() function. Returns LYXP_SET_NUMBER
4388 * with the context size.
4389 *
4390 * @param[in] args Array of arguments.
4391 * @param[in] arg_count Count of elements in @p args.
4392 * @param[in,out] set Context and result set at the same time.
4393 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004394 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004395 */
4396static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004397xpath_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 +02004398{
4399 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004400 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004401 return LY_SUCCESS;
4402 }
4403
Michal Vasko03ff5a72019-09-11 13:49:33 +02004404 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004405 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "last()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004406 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004407 } else if (!set->used) {
4408 set_fill_number(set, 0);
4409 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004410 }
4411
4412 set_fill_number(set, set->ctx_size);
4413 return LY_SUCCESS;
4414}
4415
4416/**
4417 * @brief Execute the XPath local-name(node-set?) function. Returns LYXP_SET_STRING
4418 * with the node name without namespace from the argument or the context.
4419 *
4420 * @param[in] args Array of arguments.
4421 * @param[in] arg_count Count of elements in @p args.
4422 * @param[in,out] set Context and result set at the same time.
4423 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004424 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004425 */
4426static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004427xpath_local_name(struct lyxp_set **args, uint32_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004428{
4429 struct lyxp_set_node *item;
Michal Vasko69730152020-10-09 16:30:07 +02004430
Michal Vasko03ff5a72019-09-11 13:49:33 +02004431 /* suppress unused variable warning */
4432 (void)options;
4433
4434 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004435 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004436 return LY_SUCCESS;
4437 }
4438
4439 if (arg_count) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004440 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004441 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004442 "local-name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004443 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004444 } else if (!args[0]->used) {
4445 set_fill_string(set, "", 0);
4446 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004447 }
4448
4449 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004450 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004451
4452 item = &args[0]->val.nodes[0];
4453 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004454 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004455 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "local-name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004456 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004457 } else if (!set->used) {
4458 set_fill_string(set, "", 0);
4459 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004460 }
4461
4462 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004463 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004464
4465 item = &set->val.nodes[0];
4466 }
4467
4468 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004469 case LYXP_NODE_NONE:
4470 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004471 case LYXP_NODE_ROOT:
4472 case LYXP_NODE_ROOT_CONFIG:
4473 case LYXP_NODE_TEXT:
4474 set_fill_string(set, "", 0);
4475 break;
4476 case LYXP_NODE_ELEM:
4477 set_fill_string(set, item->node->schema->name, strlen(item->node->schema->name));
4478 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004479 case LYXP_NODE_META:
4480 set_fill_string(set, ((struct lyd_meta *)item->node)->name, strlen(((struct lyd_meta *)item->node)->name));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004481 break;
4482 }
4483
4484 return LY_SUCCESS;
4485}
4486
4487/**
4488 * @brief Execute the XPath name(node-set?) function. Returns LYXP_SET_STRING
4489 * with the node name fully qualified (with namespace) from the argument or the context.
4490 *
4491 * @param[in] args Array of arguments.
4492 * @param[in] arg_count Count of elements in @p args.
4493 * @param[in,out] set Context and result set at the same time.
4494 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004495 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004496 */
4497static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004498xpath_name(struct lyxp_set **args, uint32_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004499{
4500 struct lyxp_set_node *item;
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004501 struct lys_module *mod = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004502 char *str;
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004503 const char *name = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004504
4505 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004506 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004507 return LY_SUCCESS;
4508 }
4509
4510 if (arg_count) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004511 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004512 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004513 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004514 } else if (!args[0]->used) {
4515 set_fill_string(set, "", 0);
4516 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004517 }
4518
4519 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004520 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004521
4522 item = &args[0]->val.nodes[0];
4523 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004524 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004525 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004526 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004527 } else if (!set->used) {
4528 set_fill_string(set, "", 0);
4529 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004530 }
4531
4532 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004533 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004534
4535 item = &set->val.nodes[0];
4536 }
4537
4538 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004539 case LYXP_NODE_NONE:
4540 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004541 case LYXP_NODE_ROOT:
4542 case LYXP_NODE_ROOT_CONFIG:
4543 case LYXP_NODE_TEXT:
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004544 /* keep NULL */
Michal Vasko03ff5a72019-09-11 13:49:33 +02004545 break;
4546 case LYXP_NODE_ELEM:
4547 mod = item->node->schema->module;
4548 name = item->node->schema->name;
4549 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004550 case LYXP_NODE_META:
4551 mod = ((struct lyd_meta *)item->node)->annotation->module;
4552 name = ((struct lyd_meta *)item->node)->name;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004553 break;
4554 }
4555
4556 if (mod && name) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004557 int rc = asprintf(&str, "%s:%s", ly_get_prefix(mod, set->format, set->prefix_data), name);
Michal Vasko26bbb272022-08-02 14:54:33 +02004558
Michal Vasko03ff5a72019-09-11 13:49:33 +02004559 LY_CHECK_ERR_RET(rc == -1, LOGMEM(set->ctx), LY_EMEM);
4560 set_fill_string(set, str, strlen(str));
4561 free(str);
4562 } else {
4563 set_fill_string(set, "", 0);
4564 }
4565
4566 return LY_SUCCESS;
4567}
4568
4569/**
4570 * @brief Execute the XPath namespace-uri(node-set?) function. Returns LYXP_SET_STRING
4571 * with the namespace of the node from the argument or the context.
4572 *
4573 * @param[in] args Array of arguments.
4574 * @param[in] arg_count Count of elements in @p args.
4575 * @param[in,out] set Context and result set at the same time.
4576 * @param[in] options XPath options.
4577 * @return LY_ERR (LY_EINVAL for wrong arguments on schema)
4578 */
4579static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004580xpath_namespace_uri(struct lyxp_set **args, uint32_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004581{
4582 struct lyxp_set_node *item;
4583 struct lys_module *mod;
Michal Vasko69730152020-10-09 16:30:07 +02004584
Michal Vasko03ff5a72019-09-11 13:49:33 +02004585 /* suppress unused variable warning */
4586 (void)options;
4587
4588 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004589 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004590 return LY_SUCCESS;
4591 }
4592
4593 if (arg_count) {
Michal Vaskod3678892020-05-21 10:06:58 +02004594 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004595 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
Michal Vasko69730152020-10-09 16:30:07 +02004596 "namespace-uri(node-set?)");
Michal Vaskod3678892020-05-21 10:06:58 +02004597 return LY_EVALID;
4598 } else if (!args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004599 set_fill_string(set, "", 0);
4600 return LY_SUCCESS;
4601 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004602
4603 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004604 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004605
4606 item = &args[0]->val.nodes[0];
4607 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004608 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004609 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "namespace-uri(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004610 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004611 } else if (!set->used) {
4612 set_fill_string(set, "", 0);
4613 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004614 }
4615
4616 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004617 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004618
4619 item = &set->val.nodes[0];
4620 }
4621
4622 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004623 case LYXP_NODE_NONE:
4624 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004625 case LYXP_NODE_ROOT:
4626 case LYXP_NODE_ROOT_CONFIG:
4627 case LYXP_NODE_TEXT:
4628 set_fill_string(set, "", 0);
4629 break;
4630 case LYXP_NODE_ELEM:
Michal Vasko9f96a052020-03-10 09:41:45 +01004631 case LYXP_NODE_META:
Michal Vasko03ff5a72019-09-11 13:49:33 +02004632 if (item->type == LYXP_NODE_ELEM) {
4633 mod = item->node->schema->module;
Michal Vasko9f96a052020-03-10 09:41:45 +01004634 } else { /* LYXP_NODE_META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02004635 /* annotations */
Michal Vasko9f96a052020-03-10 09:41:45 +01004636 mod = ((struct lyd_meta *)item->node)->annotation->module;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004637 }
4638
4639 set_fill_string(set, mod->ns, strlen(mod->ns));
4640 break;
4641 }
4642
4643 return LY_SUCCESS;
4644}
4645
4646/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02004647 * @brief Execute the XPath normalize-space(string?) function. Returns LYXP_SET_STRING
4648 * with normalized value (no leading, trailing, double white spaces) of the node
4649 * from the argument or the context.
4650 *
4651 * @param[in] args Array of arguments.
4652 * @param[in] arg_count Count of elements in @p args.
4653 * @param[in,out] set Context and result set at the same time.
4654 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004655 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004656 */
4657static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004658xpath_normalize_space(struct lyxp_set **args, uint32_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004659{
Michal Vaskodd528af2022-08-08 14:35:07 +02004660 uint32_t i, new_used;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004661 char *new;
Radek Krejci857189e2020-09-01 13:26:36 +02004662 ly_bool have_spaces = 0, space_before = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004663 struct lysc_node_leaf *sleaf;
4664 LY_ERR rc = LY_SUCCESS;
4665
4666 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004667 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) &&
4668 (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004669 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004670 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
4671 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004672 } else if (!warn_is_string_type(sleaf->type)) {
4673 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004674 }
4675 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004676 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004677 return rc;
4678 }
4679
4680 if (arg_count) {
4681 set_fill_set(set, args[0]);
4682 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004683 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004684 LY_CHECK_RET(rc);
4685
4686 /* is there any normalization necessary? */
4687 for (i = 0; set->val.str[i]; ++i) {
4688 if (is_xmlws(set->val.str[i])) {
4689 if ((i == 0) || space_before || (!set->val.str[i + 1])) {
4690 have_spaces = 1;
4691 break;
4692 }
4693 space_before = 1;
4694 } else {
4695 space_before = 0;
4696 }
4697 }
4698
4699 /* yep, there is */
4700 if (have_spaces) {
4701 /* it's enough, at least one character will go, makes space for ending '\0' */
4702 new = malloc(strlen(set->val.str) * sizeof(char));
4703 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4704 new_used = 0;
4705
4706 space_before = 0;
4707 for (i = 0; set->val.str[i]; ++i) {
4708 if (is_xmlws(set->val.str[i])) {
4709 if ((i == 0) || space_before) {
4710 space_before = 1;
4711 continue;
4712 } else {
4713 space_before = 1;
4714 }
4715 } else {
4716 space_before = 0;
4717 }
4718
4719 new[new_used] = (space_before ? ' ' : set->val.str[i]);
4720 ++new_used;
4721 }
4722
4723 /* at worst there is one trailing space now */
4724 if (new_used && is_xmlws(new[new_used - 1])) {
4725 --new_used;
4726 }
4727
4728 new = ly_realloc(new, (new_used + 1) * sizeof(char));
4729 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4730 new[new_used] = '\0';
4731
4732 free(set->val.str);
4733 set->val.str = new;
4734 }
4735
4736 return LY_SUCCESS;
4737}
4738
4739/**
4740 * @brief Execute the XPath not(boolean) function. Returns LYXP_SET_BOOLEAN
4741 * with the argument converted to boolean and logically inverted.
4742 *
4743 * @param[in] args Array of arguments.
4744 * @param[in] arg_count Count of elements in @p args.
4745 * @param[in,out] set Context and result set at the same time.
4746 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004747 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004748 */
4749static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004750xpath_not(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004751{
4752 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004753 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004754 return LY_SUCCESS;
4755 }
4756
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004757 lyxp_set_cast(args[0], LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +02004758 if (args[0]->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004759 set_fill_boolean(set, 0);
4760 } else {
4761 set_fill_boolean(set, 1);
4762 }
4763
4764 return LY_SUCCESS;
4765}
4766
4767/**
4768 * @brief Execute the XPath number(object?) function. Returns LYXP_SET_NUMBER
4769 * with the number representation of either the argument or the context.
4770 *
4771 * @param[in] args Array of arguments.
4772 * @param[in] arg_count Count of elements in @p args.
4773 * @param[in,out] set Context and result set at the same time.
4774 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004775 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004776 */
4777static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004778xpath_number(struct lyxp_set **args, uint32_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004779{
4780 LY_ERR rc;
4781
4782 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004783 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004784 return LY_SUCCESS;
4785 }
4786
4787 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004788 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004789 LY_CHECK_RET(rc);
4790 set_fill_set(set, args[0]);
4791 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004792 rc = lyxp_set_cast(set, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004793 LY_CHECK_RET(rc);
4794 }
4795
4796 return LY_SUCCESS;
4797}
4798
4799/**
4800 * @brief Execute the XPath position() function. Returns LYXP_SET_NUMBER
4801 * with the context position.
4802 *
4803 * @param[in] args Array of arguments.
4804 * @param[in] arg_count Count of elements in @p args.
4805 * @param[in,out] set Context and result set at the same time.
4806 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004807 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004808 */
4809static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004810xpath_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 +02004811{
4812 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004813 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004814 return LY_SUCCESS;
4815 }
4816
Michal Vasko03ff5a72019-09-11 13:49:33 +02004817 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004818 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "position()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004819 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004820 } else if (!set->used) {
4821 set_fill_number(set, 0);
4822 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004823 }
4824
4825 set_fill_number(set, set->ctx_pos);
4826
4827 /* UNUSED in 'Release' build type */
4828 (void)options;
4829 return LY_SUCCESS;
4830}
4831
4832/**
4833 * @brief Execute the YANG 1.1 re-match(string, string) function. Returns LYXP_SET_BOOLEAN
4834 * depending on whether the second argument regex matches the first argument string. For details refer to
4835 * YANG 1.1 RFC section 10.2.1.
4836 *
4837 * @param[in] args Array of arguments.
4838 * @param[in] arg_count Count of elements in @p args.
4839 * @param[in,out] set Context and result set at the same time.
4840 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004841 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004842 */
4843static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004844xpath_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 +02004845{
4846 struct lysc_pattern **patterns = NULL, **pattern;
4847 struct lysc_node_leaf *sleaf;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004848 LY_ERR rc = LY_SUCCESS;
4849 struct ly_err_item *err;
4850
4851 if (options & LYXP_SCNODE_ALL) {
4852 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4853 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4854 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 +02004855 } else if (!warn_is_string_type(sleaf->type)) {
4856 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004857 }
4858 }
4859
4860 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4861 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4862 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 +02004863 } else if (!warn_is_string_type(sleaf->type)) {
4864 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004865 }
4866 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004867 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004868 return rc;
4869 }
4870
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004871 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004872 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004873 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004874 LY_CHECK_RET(rc);
4875
4876 LY_ARRAY_NEW_RET(set->ctx, patterns, pattern, LY_EMEM);
Radek Iša45802b52021-02-09 09:21:58 +01004877 *pattern = calloc(1, sizeof **pattern);
Radek Krejciddace2c2021-01-08 11:30:56 +01004878 LOG_LOCSET(NULL, set->cur_node, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01004879 rc = lys_compile_type_pattern_check(set->ctx, args[1]->val.str, &(*pattern)->code);
Michal Vasko4a7d4d62021-12-13 17:05:06 +01004880 if (set->cur_node) {
4881 LOG_LOCBACK(0, 1, 0, 0);
4882 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004883 if (rc != LY_SUCCESS) {
4884 LY_ARRAY_FREE(patterns);
4885 return rc;
4886 }
4887
Radek Krejci0b013302021-03-29 15:22:32 +02004888 rc = lyplg_type_validate_patterns(patterns, args[0]->val.str, strlen(args[0]->val.str), &err);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004889 pcre2_code_free((*pattern)->code);
4890 free(*pattern);
4891 LY_ARRAY_FREE(patterns);
4892 if (rc && (rc != LY_EVALID)) {
Michal Vasko177d0ed2020-11-23 16:43:03 +01004893 ly_err_print(set->ctx, err);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004894 ly_err_free(err);
4895 return rc;
4896 }
4897
4898 if (rc == LY_EVALID) {
4899 ly_err_free(err);
4900 set_fill_boolean(set, 0);
4901 } else {
4902 set_fill_boolean(set, 1);
4903 }
4904
4905 return LY_SUCCESS;
4906}
4907
4908/**
4909 * @brief Execute the XPath round(number) function. Returns LYXP_SET_NUMBER
4910 * with the rounded first argument. For details refer to
4911 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-round.
4912 *
4913 * @param[in] args Array of arguments.
4914 * @param[in] arg_count Count of elements in @p args.
4915 * @param[in,out] set Context and result set at the same time.
4916 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004917 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004918 */
4919static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004920xpath_round(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004921{
4922 struct lysc_node_leaf *sleaf;
4923 LY_ERR rc = LY_SUCCESS;
4924
4925 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02004926 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004927 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02004928 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4929 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4930 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
4931 sleaf->name);
4932 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
4933 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
4934 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004935 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004936 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004937 return rc;
4938 }
4939
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004940 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004941 LY_CHECK_RET(rc);
4942
4943 /* cover only the cases where floor can't be used */
4944 if ((args[0]->val.num == -0.0f) || ((args[0]->val.num < 0) && (args[0]->val.num >= -0.5))) {
4945 set_fill_number(set, -0.0f);
4946 } else {
4947 args[0]->val.num += 0.5;
4948 rc = xpath_floor(args, 1, args[0], options);
4949 LY_CHECK_RET(rc);
4950 set_fill_number(set, args[0]->val.num);
4951 }
4952
4953 return LY_SUCCESS;
4954}
4955
4956/**
4957 * @brief Execute the XPath starts-with(string, string) function.
4958 * Returns LYXP_SET_BOOLEAN whether the second argument is
4959 * the prefix of the first or not.
4960 *
4961 * @param[in] args Array of arguments.
4962 * @param[in] arg_count Count of elements in @p args.
4963 * @param[in,out] set Context and result set at the same time.
4964 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004965 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004966 */
4967static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004968xpath_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 +02004969{
4970 struct lysc_node_leaf *sleaf;
4971 LY_ERR rc = LY_SUCCESS;
4972
4973 if (options & LYXP_SCNODE_ALL) {
4974 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4975 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4976 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 +02004977 } else if (!warn_is_string_type(sleaf->type)) {
4978 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004979 }
4980 }
4981
4982 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4983 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4984 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 +02004985 } else if (!warn_is_string_type(sleaf->type)) {
4986 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004987 }
4988 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004989 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004990 return rc;
4991 }
4992
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004993 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004994 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004995 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004996 LY_CHECK_RET(rc);
4997
4998 if (strncmp(args[0]->val.str, args[1]->val.str, strlen(args[1]->val.str))) {
4999 set_fill_boolean(set, 0);
5000 } else {
5001 set_fill_boolean(set, 1);
5002 }
5003
5004 return LY_SUCCESS;
5005}
5006
5007/**
5008 * @brief Execute the XPath string(object?) function. Returns LYXP_SET_STRING
5009 * with the string representation of either the argument or the context.
5010 *
5011 * @param[in] args Array of arguments.
5012 * @param[in] arg_count Count of elements in @p args.
5013 * @param[in,out] set Context and result set at the same time.
5014 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005015 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005016 */
5017static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02005018xpath_string(struct lyxp_set **args, uint32_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005019{
5020 LY_ERR rc;
5021
5022 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02005023 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005024 return LY_SUCCESS;
5025 }
5026
5027 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005028 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005029 LY_CHECK_RET(rc);
5030 set_fill_set(set, args[0]);
5031 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005032 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005033 LY_CHECK_RET(rc);
5034 }
5035
5036 return LY_SUCCESS;
5037}
5038
5039/**
5040 * @brief Execute the XPath string-length(string?) function. Returns LYXP_SET_NUMBER
5041 * with the length of the string in either the argument or the context.
5042 *
5043 * @param[in] args Array of arguments.
5044 * @param[in] arg_count Count of elements in @p args.
5045 * @param[in,out] set Context and result set at the same time.
5046 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005047 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005048 */
5049static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02005050xpath_string_length(struct lyxp_set **args, uint32_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005051{
5052 struct lysc_node_leaf *sleaf;
5053 LY_ERR rc = LY_SUCCESS;
5054
5055 if (options & LYXP_SCNODE_ALL) {
5056 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5057 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5058 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 +02005059 } else if (!warn_is_string_type(sleaf->type)) {
5060 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005061 }
5062 }
5063 if (!arg_count && (set->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set))) {
5064 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5065 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 +02005066 } else if (!warn_is_string_type(sleaf->type)) {
5067 LOGWRN(set->ctx, "Argument #0 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005068 }
5069 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005070 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005071 return rc;
5072 }
5073
5074 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005075 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005076 LY_CHECK_RET(rc);
5077 set_fill_number(set, strlen(args[0]->val.str));
5078 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005079 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005080 LY_CHECK_RET(rc);
5081 set_fill_number(set, strlen(set->val.str));
5082 }
5083
5084 return LY_SUCCESS;
5085}
5086
5087/**
5088 * @brief Execute the XPath substring(string, number, number?) function.
5089 * Returns LYXP_SET_STRING substring of the first argument starting
5090 * on the second argument index ending on the third argument index,
5091 * indexed from 1. For exact definition refer to
5092 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-substring.
5093 *
5094 * @param[in] args Array of arguments.
5095 * @param[in] arg_count Count of elements in @p args.
5096 * @param[in,out] set Context and result set at the same time.
5097 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005098 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005099 */
5100static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02005101xpath_substring(struct lyxp_set **args, uint32_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005102{
Michal Vasko6db996e2022-07-28 10:28:04 +02005103 int64_t start;
5104 int32_t len;
Michal Vaskodd528af2022-08-08 14:35:07 +02005105 uint32_t str_start, str_len, pos;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005106 struct lysc_node_leaf *sleaf;
5107 LY_ERR rc = LY_SUCCESS;
5108
5109 if (options & LYXP_SCNODE_ALL) {
5110 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5111 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5112 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005113 } else if (!warn_is_string_type(sleaf->type)) {
5114 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005115 }
5116 }
5117
5118 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5119 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5120 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 +02005121 } else if (!warn_is_numeric_type(sleaf->type)) {
5122 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005123 }
5124 }
5125
Michal Vasko69730152020-10-09 16:30:07 +02005126 if ((arg_count == 3) && (args[2]->type == LYXP_SET_SCNODE_SET) &&
5127 (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005128 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5129 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 +02005130 } else if (!warn_is_numeric_type(sleaf->type)) {
5131 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005132 }
5133 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005134 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005135 return rc;
5136 }
5137
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005138 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005139 LY_CHECK_RET(rc);
5140
5141 /* start */
5142 if (xpath_round(&args[1], 1, args[1], options)) {
5143 return -1;
5144 }
5145 if (isfinite(args[1]->val.num)) {
5146 start = args[1]->val.num - 1;
5147 } else if (isinf(args[1]->val.num) && signbit(args[1]->val.num)) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02005148 start = INT32_MIN;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005149 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02005150 start = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005151 }
5152
5153 /* len */
5154 if (arg_count == 3) {
5155 rc = xpath_round(&args[2], 1, args[2], options);
5156 LY_CHECK_RET(rc);
Radek Krejci1deb5be2020-08-26 16:43:36 +02005157 if (isnan(args[2]->val.num) || signbit(args[2]->val.num)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005158 len = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005159 } else if (isfinite(args[2]->val.num)) {
5160 len = args[2]->val.num;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005161 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02005162 len = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005163 }
5164 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02005165 len = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005166 }
5167
5168 /* find matching character positions */
5169 str_start = 0;
5170 str_len = 0;
5171 for (pos = 0; args[0]->val.str[pos]; ++pos) {
5172 if (pos < start) {
5173 ++str_start;
5174 } else if (pos < start + len) {
5175 ++str_len;
5176 } else {
5177 break;
5178 }
5179 }
5180
5181 set_fill_string(set, args[0]->val.str + str_start, str_len);
5182 return LY_SUCCESS;
5183}
5184
5185/**
5186 * @brief Execute the XPath substring-after(string, string) function.
5187 * Returns LYXP_SET_STRING with the string succeeding the occurance
5188 * of the second argument in the first or an empty string.
5189 *
5190 * @param[in] args Array of arguments.
5191 * @param[in] arg_count Count of elements in @p args.
5192 * @param[in,out] set Context and result set at the same time.
5193 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005194 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005195 */
5196static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02005197xpath_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 +02005198{
5199 char *ptr;
5200 struct lysc_node_leaf *sleaf;
5201 LY_ERR rc = LY_SUCCESS;
5202
5203 if (options & LYXP_SCNODE_ALL) {
5204 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5205 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5206 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 +02005207 } else if (!warn_is_string_type(sleaf->type)) {
5208 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005209 }
5210 }
5211
5212 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5213 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5214 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 +02005215 } else if (!warn_is_string_type(sleaf->type)) {
5216 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005217 }
5218 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005219 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005220 return rc;
5221 }
5222
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005223 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005224 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005225 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005226 LY_CHECK_RET(rc);
5227
5228 ptr = strstr(args[0]->val.str, args[1]->val.str);
5229 if (ptr) {
5230 set_fill_string(set, ptr + strlen(args[1]->val.str), strlen(ptr + strlen(args[1]->val.str)));
5231 } else {
5232 set_fill_string(set, "", 0);
5233 }
5234
5235 return LY_SUCCESS;
5236}
5237
5238/**
5239 * @brief Execute the XPath substring-before(string, string) function.
5240 * Returns LYXP_SET_STRING with the string preceding the occurance
5241 * of the second argument in the first or an empty string.
5242 *
5243 * @param[in] args Array of arguments.
5244 * @param[in] arg_count Count of elements in @p args.
5245 * @param[in,out] set Context and result set at the same time.
5246 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005247 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005248 */
5249static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02005250xpath_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 +02005251{
5252 char *ptr;
5253 struct lysc_node_leaf *sleaf;
5254 LY_ERR rc = LY_SUCCESS;
5255
5256 if (options & LYXP_SCNODE_ALL) {
5257 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5258 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5259 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 +02005260 } else if (!warn_is_string_type(sleaf->type)) {
5261 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005262 }
5263 }
5264
5265 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5266 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5267 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 +02005268 } else if (!warn_is_string_type(sleaf->type)) {
5269 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005270 }
5271 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005272 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005273 return rc;
5274 }
5275
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005276 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005277 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005278 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005279 LY_CHECK_RET(rc);
5280
5281 ptr = strstr(args[0]->val.str, args[1]->val.str);
5282 if (ptr) {
5283 set_fill_string(set, args[0]->val.str, ptr - args[0]->val.str);
5284 } else {
5285 set_fill_string(set, "", 0);
5286 }
5287
5288 return LY_SUCCESS;
5289}
5290
5291/**
5292 * @brief Execute the XPath sum(node-set) function. Returns LYXP_SET_NUMBER
5293 * with the sum of all the nodes in the context.
5294 *
5295 * @param[in] args Array of arguments.
5296 * @param[in] arg_count Count of elements in @p args.
5297 * @param[in,out] set Context and result set at the same time.
5298 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005299 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005300 */
5301static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02005302xpath_sum(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005303{
5304 long double num;
5305 char *str;
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01005306 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005307 struct lyxp_set set_item;
5308 struct lysc_node_leaf *sleaf;
5309 LY_ERR rc = LY_SUCCESS;
5310
5311 if (options & LYXP_SCNODE_ALL) {
5312 if (args[0]->type == LYXP_SET_SCNODE_SET) {
5313 for (i = 0; i < args[0]->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005314 if (args[0]->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005315 sleaf = (struct lysc_node_leaf *)args[0]->val.scnodes[i].scnode;
5316 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5317 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__,
Michal Vasko69730152020-10-09 16:30:07 +02005318 lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005319 } else if (!warn_is_numeric_type(sleaf->type)) {
5320 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005321 }
5322 }
5323 }
5324 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005325 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005326 return rc;
5327 }
5328
5329 set_fill_number(set, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005330
5331 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005332 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "sum(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02005333 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02005334 } else if (!args[0]->used) {
5335 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005336 }
5337
Michal Vasko5c4e5892019-11-14 12:31:38 +01005338 set_init(&set_item, set);
5339
Michal Vasko03ff5a72019-09-11 13:49:33 +02005340 set_item.type = LYXP_SET_NODE_SET;
Michal Vasko41decbf2021-11-02 11:50:21 +01005341 set_item.val.nodes = calloc(1, sizeof *set_item.val.nodes);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005342 LY_CHECK_ERR_RET(!set_item.val.nodes, LOGMEM(set->ctx), LY_EMEM);
5343
5344 set_item.used = 1;
5345 set_item.size = 1;
5346
5347 for (i = 0; i < args[0]->used; ++i) {
5348 set_item.val.nodes[0] = args[0]->val.nodes[i];
5349
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005350 rc = cast_node_set_to_string(&set_item, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005351 LY_CHECK_RET(rc);
5352 num = cast_string_to_number(str);
5353 free(str);
5354 set->val.num += num;
5355 }
5356
5357 free(set_item.val.nodes);
5358
5359 return LY_SUCCESS;
5360}
5361
5362/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02005363 * @brief Execute the XPath translate(string, string, string) function.
5364 * Returns LYXP_SET_STRING with the first argument with the characters
5365 * from the second argument replaced by those on the corresponding
5366 * positions in the third argument.
5367 *
5368 * @param[in] args Array of arguments.
5369 * @param[in] arg_count Count of elements in @p args.
5370 * @param[in,out] set Context and result set at the same time.
5371 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005372 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005373 */
5374static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02005375xpath_translate(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005376{
Michal Vaskodd528af2022-08-08 14:35:07 +02005377 uint32_t i, j, new_used;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005378 char *new;
Radek Krejci857189e2020-09-01 13:26:36 +02005379 ly_bool have_removed;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005380 struct lysc_node_leaf *sleaf;
5381 LY_ERR rc = LY_SUCCESS;
5382
5383 if (options & LYXP_SCNODE_ALL) {
5384 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5385 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5386 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 +02005387 } else if (!warn_is_string_type(sleaf->type)) {
5388 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005389 }
5390 }
5391
5392 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5393 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5394 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 +02005395 } else if (!warn_is_string_type(sleaf->type)) {
5396 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005397 }
5398 }
5399
5400 if ((args[2]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
5401 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5402 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 +02005403 } else if (!warn_is_string_type(sleaf->type)) {
5404 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005405 }
5406 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005407 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005408 return rc;
5409 }
5410
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005411 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005412 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005413 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005414 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005415 rc = lyxp_set_cast(args[2], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005416 LY_CHECK_RET(rc);
5417
5418 new = malloc((strlen(args[0]->val.str) + 1) * sizeof(char));
5419 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5420 new_used = 0;
5421
5422 have_removed = 0;
5423 for (i = 0; args[0]->val.str[i]; ++i) {
Radek Krejci857189e2020-09-01 13:26:36 +02005424 ly_bool found = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005425
5426 for (j = 0; args[1]->val.str[j]; ++j) {
5427 if (args[0]->val.str[i] == args[1]->val.str[j]) {
5428 /* removing this char */
5429 if (j >= strlen(args[2]->val.str)) {
5430 have_removed = 1;
5431 found = 1;
5432 break;
5433 }
5434 /* replacing this char */
5435 new[new_used] = args[2]->val.str[j];
5436 ++new_used;
5437 found = 1;
5438 break;
5439 }
5440 }
5441
5442 /* copying this char */
5443 if (!found) {
5444 new[new_used] = args[0]->val.str[i];
5445 ++new_used;
5446 }
5447 }
5448
5449 if (have_removed) {
5450 new = ly_realloc(new, (new_used + 1) * sizeof(char));
5451 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5452 }
5453 new[new_used] = '\0';
5454
Michal Vaskod3678892020-05-21 10:06:58 +02005455 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005456 set->type = LYXP_SET_STRING;
5457 set->val.str = new;
5458
5459 return LY_SUCCESS;
5460}
5461
5462/**
5463 * @brief Execute the XPath true() function. Returns LYXP_SET_BOOLEAN
5464 * with true value.
5465 *
5466 * @param[in] args Array of arguments.
5467 * @param[in] arg_count Count of elements in @p args.
5468 * @param[in,out] set Context and result set at the same time.
5469 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005470 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005471 */
5472static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02005473xpath_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 +02005474{
5475 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02005476 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005477 return LY_SUCCESS;
5478 }
5479
5480 set_fill_boolean(set, 1);
5481 return LY_SUCCESS;
5482}
5483
Michal Vasko03ff5a72019-09-11 13:49:33 +02005484/**
Michal Vasko49fec8e2022-05-24 10:28:33 +02005485 * @brief Execute the XPath node() processing instruction (node type). Returns LYXP_SET_NODE_SET
5486 * with only nodes from the context.
5487 *
5488 * @param[in,out] set Context and result set at the same time.
5489 * @param[in] axis Axis to search on.
5490 * @param[in] options XPath options.
5491 * @return LY_ERR
5492 */
5493static LY_ERR
5494xpath_pi_node(struct lyxp_set *set, enum lyxp_axis axis, uint32_t options)
5495{
5496 if (options & LYXP_SCNODE_ALL) {
5497 return moveto_scnode(set, NULL, NULL, axis, options);
5498 }
5499
5500 if (set->type != LYXP_SET_NODE_SET) {
5501 lyxp_set_free_content(set);
5502 return LY_SUCCESS;
5503 }
5504
5505 /* just like moving to a node with no restrictions */
5506 return moveto_node(set, NULL, NULL, axis, options);
5507}
5508
5509/**
5510 * @brief Execute the XPath text() processing instruction (node type). Returns LYXP_SET_NODE_SET
5511 * with the text content of the nodes in the context.
5512 *
5513 * @param[in,out] set Context and result set at the same time.
5514 * @param[in] axis Axis to search on.
5515 * @param[in] options XPath options.
5516 * @return LY_ERR
5517 */
5518static LY_ERR
5519xpath_pi_text(struct lyxp_set *set, enum lyxp_axis axis, uint32_t options)
5520{
5521 uint32_t i;
5522
5523 if (options & LYXP_SCNODE_ALL) {
5524 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
5525 return LY_SUCCESS;
5526 }
5527
5528 if (set->type != LYXP_SET_NODE_SET) {
5529 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "text()");
5530 return LY_EVALID;
5531 }
5532
5533 if (axis != LYXP_AXIS_CHILD) {
5534 /* even following and preceding axescan return text nodes, but whatever */
5535 lyxp_set_free_content(set);
5536 return LY_SUCCESS;
5537 }
5538
5539 for (i = 0; i < set->used; ++i) {
5540 switch (set->val.nodes[i].type) {
5541 case LYXP_NODE_NONE:
5542 LOGINT_RET(set->ctx);
5543 case LYXP_NODE_ELEM:
5544 if (set->val.nodes[i].node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
5545 set->val.nodes[i].type = LYXP_NODE_TEXT;
5546 break;
5547 }
5548 /* fall through */
5549 case LYXP_NODE_ROOT:
5550 case LYXP_NODE_ROOT_CONFIG:
5551 case LYXP_NODE_TEXT:
5552 case LYXP_NODE_META:
5553 set_remove_node_none(set, i);
5554 break;
5555 }
5556 }
5557 set_remove_nodes_none(set);
5558
5559 return LY_SUCCESS;
5560}
5561
5562/**
Michal Vasko6346ece2019-09-24 13:12:53 +02005563 * @brief Skip prefix and return corresponding model if there is a prefix. Logs directly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005564 *
Michal Vasko2104e9f2020-03-06 08:23:25 +01005565 * XPath @p set is expected to be a (sc)node set!
5566 *
Michal Vasko6346ece2019-09-24 13:12:53 +02005567 * @param[in,out] qname Qualified node name. If includes prefix, it is skipped.
5568 * @param[in,out] qname_len Length of @p qname, is updated accordingly.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005569 * @param[in] set Set with general XPath context.
5570 * @param[in] ctx_scnode Context node to inherit module for unprefixed node for ::LY_PREF_JSON.
Michal Vasko6346ece2019-09-24 13:12:53 +02005571 * @param[out] moveto_mod Expected module of a matching node.
5572 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005573 */
Michal Vasko6346ece2019-09-24 13:12:53 +02005574static LY_ERR
Michal Vaskofe1af042022-07-29 14:58:59 +02005575moveto_resolve_model(const char **qname, uint32_t *qname_len, const struct lyxp_set *set,
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005576 const struct lysc_node *ctx_scnode, const struct lys_module **moveto_mod)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005577{
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02005578 const struct lys_module *mod = NULL;
Michal Vasko6346ece2019-09-24 13:12:53 +02005579 const char *ptr;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005580 size_t pref_len;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005581
Michal Vasko2104e9f2020-03-06 08:23:25 +01005582 assert((set->type == LYXP_SET_NODE_SET) || (set->type == LYXP_SET_SCNODE_SET));
5583
Michal Vasko6346ece2019-09-24 13:12:53 +02005584 if ((ptr = ly_strnchr(*qname, ':', *qname_len))) {
5585 /* specific module */
5586 pref_len = ptr - *qname;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005587 mod = ly_resolve_prefix(set->ctx, *qname, pref_len, set->format, set->prefix_data);
Michal Vasko6346ece2019-09-24 13:12:53 +02005588
Michal Vasko004d3152020-06-11 19:59:22 +02005589 /* check for errors and non-implemented modules, as they are not valid */
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005590 if (!mod || !mod->implemented) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005591 LOGVAL(set->ctx, LY_VCODE_XP_INMOD, pref_len, *qname);
Michal Vasko6346ece2019-09-24 13:12:53 +02005592 return LY_EVALID;
5593 }
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005594
Michal Vasko6346ece2019-09-24 13:12:53 +02005595 *qname += pref_len + 1;
5596 *qname_len -= pref_len + 1;
5597 } else if (((*qname)[0] == '*') && (*qname_len == 1)) {
5598 /* all modules - special case */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005599 mod = NULL;
Michal Vasko6346ece2019-09-24 13:12:53 +02005600 } else {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005601 switch (set->format) {
Radek Krejci8df109d2021-04-23 12:19:08 +02005602 case LY_VALUE_SCHEMA:
5603 case LY_VALUE_SCHEMA_RESOLVED:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005604 /* current module */
5605 mod = set->cur_mod;
5606 break;
Radek Krejci224d4b42021-04-23 13:54:59 +02005607 case LY_VALUE_CANON:
Radek Krejci8df109d2021-04-23 12:19:08 +02005608 case LY_VALUE_JSON:
Radek Krejcif9943642021-04-26 10:18:21 +02005609 case LY_VALUE_LYB:
Michal Vaskoddd76592022-01-17 13:34:48 +01005610 case LY_VALUE_STR_NS:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005611 /* inherit parent (context node) module */
5612 if (ctx_scnode) {
5613 mod = ctx_scnode->module;
5614 } else {
5615 mod = NULL;
5616 }
5617 break;
Radek Krejci8df109d2021-04-23 12:19:08 +02005618 case LY_VALUE_XML:
Michal Vasko52143d12021-04-14 15:36:39 +02005619 /* all nodes need to be prefixed */
5620 LOGVAL(set->ctx, LYVE_DATA, "Non-prefixed node \"%.*s\" in XML xpath found.", *qname_len, *qname);
5621 return LY_EVALID;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005622 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005623 }
5624
Michal Vasko6346ece2019-09-24 13:12:53 +02005625 *moveto_mod = mod;
5626 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005627}
5628
5629/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02005630 * @brief Move context @p set to the root. Handles absolute path.
5631 * Result is LYXP_SET_NODE_SET.
5632 *
5633 * @param[in,out] set Set to use.
5634 * @param[in] options Xpath options.
Michal Vaskob0099a92020-08-31 14:55:23 +02005635 * @return LY_ERR value.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005636 */
Michal Vaskob0099a92020-08-31 14:55:23 +02005637static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005638moveto_root(struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005639{
aPiecek8b0cc152021-05-31 16:40:31 +02005640 assert(!(options & LYXP_SKIP_EXPR));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005641
5642 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02005643 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko7333cb32022-07-29 16:30:29 +02005644 LY_CHECK_RET(set_scnode_insert_node(set, NULL, set->root_type, LYXP_AXIS_SELF, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005645 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005646 set->type = LYXP_SET_NODE_SET;
5647 set->used = 0;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005648 set_insert_node(set, NULL, 0, set->root_type, 0);
Michal Vasko306e2832022-07-25 09:15:17 +02005649 set->non_child_axis = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005650 }
Michal Vaskob0099a92020-08-31 14:55:23 +02005651
5652 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005653}
5654
5655/**
5656 * @brief Check @p node as a part of NameTest processing.
5657 *
5658 * @param[in] node Node to check.
Michal Vasko49fec8e2022-05-24 10:28:33 +02005659 * @param[in] node_type Node type of @p node.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005660 * @param[in] set Set to read general context from.
Michal Vaskod3678892020-05-21 10:06:58 +02005661 * @param[in] node_name Node name in the dictionary to move to, NULL for any node.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005662 * @param[in] moveto_mod Expected module of the node, NULL for no prefix.
Michal Vaskocdad7122020-11-09 21:04:44 +01005663 * @param[in] options XPath options.
Michal Vasko6346ece2019-09-24 13:12:53 +02005664 * @return LY_ERR (LY_ENOT if node does not match, LY_EINCOMPLETE on unresolved when,
Michal Vaskocd2c88a2022-06-07 10:54:34 +02005665 * LY_EINVAL if neither node nor any children match)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005666 */
5667static LY_ERR
Michal Vasko49fec8e2022-05-24 10:28:33 +02005668moveto_node_check(const struct lyd_node *node, enum lyxp_node_type node_type, const struct lyxp_set *set,
5669 const char *node_name, const struct lys_module *moveto_mod, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005670{
Michal Vasko49fec8e2022-05-24 10:28:33 +02005671 if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT)) {
5672 assert(node_type == set->root_type);
5673
5674 if (node_name || moveto_mod) {
5675 /* root will not match a specific node */
5676 return LY_ENOT;
5677 }
5678 return LY_SUCCESS;
5679 } else if (node_type != LYXP_NODE_ELEM) {
5680 /* other types will not match */
5681 return LY_ENOT;
5682 }
5683
Michal Vaskodca9f122021-07-16 13:56:22 +02005684 if (!node->schema) {
5685 /* opaque node never matches */
5686 return LY_ENOT;
5687 }
5688
Michal Vasko03ff5a72019-09-11 13:49:33 +02005689 /* module check */
Michal Vasko19089f02022-06-07 11:02:11 +02005690 if (moveto_mod) {
Michal Vasko0a1eb752022-09-29 11:00:34 +02005691 if ((set->ctx == LYD_CTX(node)) && (node->schema->module != moveto_mod)) {
Michal Vasko19089f02022-06-07 11:02:11 +02005692 return LY_ENOT;
Michal Vasko0a1eb752022-09-29 11:00:34 +02005693 } else if ((set->ctx != LYD_CTX(node)) && strcmp(node->schema->module->name, moveto_mod->name)) {
Michal Vasko19089f02022-06-07 11:02:11 +02005694 return LY_ENOT;
5695 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005696 }
5697
Michal Vasko5c4e5892019-11-14 12:31:38 +01005698 /* context check */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005699 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (node->schema->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005700 return LY_EINVAL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005701 } else if (set->context_op && (node->schema->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) &&
5702 (node->schema != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005703 return LY_EINVAL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005704 }
5705
5706 /* name check */
Michal Vasko19089f02022-06-07 11:02:11 +02005707 if (node_name) {
Michal Vasko0a1eb752022-09-29 11:00:34 +02005708 if ((set->ctx == LYD_CTX(node)) && (node->schema->name != node_name)) {
Michal Vasko19089f02022-06-07 11:02:11 +02005709 return LY_ENOT;
Michal Vasko0a1eb752022-09-29 11:00:34 +02005710 } else if ((set->ctx != LYD_CTX(node)) && strcmp(node->schema->name, node_name)) {
Michal Vasko19089f02022-06-07 11:02:11 +02005711 return LY_ENOT;
5712 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005713 }
5714
Michal Vasko6d657122022-10-19 14:38:35 +02005715 /* when check, accept the context node because it should only be the path ".", we have checked the when is valid before */
5716 if (!(options & LYXP_IGNORE_WHEN) && lysc_has_when(node->schema) && !(node->flags & LYD_WHEN_TRUE) &&
5717 (node != set->cur_node)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005718 return LY_EINCOMPLETE;
Michal Vaskoa1424542019-11-14 16:08:52 +01005719 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005720
5721 /* match */
5722 return LY_SUCCESS;
5723}
5724
5725/**
Michal Vasko49fec8e2022-05-24 10:28:33 +02005726 * @brief Get the next node in a forward DFS.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005727 *
Michal Vasko49fec8e2022-05-24 10:28:33 +02005728 * @param[in] iter Last returned node.
5729 * @param[in] stop Node to stop the search on and not return.
5730 * @return Next node, NULL if there are no more.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005731 */
Michal Vasko49fec8e2022-05-24 10:28:33 +02005732static const struct lyd_node *
5733moveto_axis_node_next_dfs_forward(const struct lyd_node *iter, const struct lyd_node *stop)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005734{
Michal Vasko49fec8e2022-05-24 10:28:33 +02005735 const struct lyd_node *next = NULL;
5736
5737 /* 1) child */
5738 next = lyd_child(iter);
5739 if (!next) {
5740 if (iter == stop) {
5741 /* reached stop, no more descendants */
5742 return NULL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005743 }
Michal Vasko49fec8e2022-05-24 10:28:33 +02005744 /* 2) child next sibling */
5745 next = iter->next;
5746 }
5747 while (!next) {
5748 iter = lyd_parent(iter);
5749 if ((!stop && !iter) || (stop && (lyd_parent(iter) == lyd_parent(stop)))) {
5750 return NULL;
5751 }
5752 next = iter->next;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005753 }
5754
Michal Vasko49fec8e2022-05-24 10:28:33 +02005755 return next;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005756}
5757
5758/**
Michal Vasko49fec8e2022-05-24 10:28:33 +02005759 * @brief Get the next node in a backward DFS.
5760 *
5761 * @param[in] iter Last returned node.
5762 * @param[in] stop Node to stop the search on and not return.
5763 * @return Next node, NULL if there are no more.
5764 */
5765static const struct lyd_node *
5766moveto_axis_node_next_dfs_backward(const struct lyd_node *iter, const struct lyd_node *stop)
5767{
5768 const struct lyd_node *next = NULL;
5769
5770 /* 1) previous sibling innermost last child */
5771 next = iter->prev->next ? iter->prev : NULL;
5772 while (next && lyd_child(next)) {
5773 next = lyd_child(next);
5774 next = next->prev;
5775 }
5776
5777 if (!next) {
5778 /* 2) parent */
5779 iter = lyd_parent(iter);
5780 if ((!stop && !iter) || (stop && (lyd_parent(iter) == lyd_parent(stop)))) {
5781 return NULL;
5782 }
5783 next = iter;
5784 }
5785
5786 return next;
5787}
5788
5789/**
5790 * @brief Get the first node on an axis for a context node.
5791 *
5792 * @param[in,out] iter NULL, updated to the next node.
5793 * @param[in,out] iter_type Node type 0 of @p iter, updated to the node type of the next node.
5794 * @param[in] node Context node.
5795 * @param[in] node_type Type of @p node.
5796 * @param[in] axis Axis to use.
5797 * @param[in] set XPath set with the general context.
5798 * @return LY_SUCCESS on success.
5799 * @return LY_ENOTFOUND if no next node found.
5800 */
5801static LY_ERR
5802moveto_axis_node_next_first(const struct lyd_node **iter, enum lyxp_node_type *iter_type, const struct lyd_node *node,
5803 enum lyxp_node_type node_type, enum lyxp_axis axis, struct lyxp_set *set)
5804{
5805 const struct lyd_node *next = NULL;
5806 enum lyxp_node_type next_type = 0;
5807
5808 assert(!*iter);
5809 assert(!*iter_type);
5810
5811 switch (axis) {
5812 case LYXP_AXIS_ANCESTOR_OR_SELF:
5813 case LYXP_AXIS_DESCENDANT_OR_SELF:
5814 case LYXP_AXIS_SELF:
5815 /* return the context node */
5816 next = node;
5817 next_type = node_type;
5818 break;
5819
5820 case LYXP_AXIS_ANCESTOR:
5821 case LYXP_AXIS_PARENT:
5822 if (node_type == LYXP_NODE_ELEM) {
5823 next = lyd_parent(node);
5824 next_type = next ? LYXP_NODE_ELEM : set->root_type;
5825 } else if (node_type == LYXP_NODE_TEXT) {
5826 next = node;
5827 next_type = LYXP_NODE_ELEM;
5828 } else if (node_type == LYXP_NODE_META) {
5829 next = ((struct lyd_meta *)node)->parent;
5830 next_type = LYXP_NODE_ELEM;
5831 } /* else root does not have a parent */
5832 break;
5833
5834 case LYXP_AXIS_CHILD:
5835 if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT)) {
5836 assert(!node);
5837
5838 /* search in all the trees */
5839 next = set->tree;
5840 next_type = next ? LYXP_NODE_ELEM : 0;
5841 } else {
5842 /* search in children */
5843 next = lyd_child(node);
5844 next_type = next ? LYXP_NODE_ELEM : 0;
5845 }
5846 break;
5847
5848 case LYXP_AXIS_DESCENDANT:
5849 if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT)) {
5850 /* top-level nodes */
5851 next = set->tree;
5852 next_type = LYXP_NODE_ELEM;
5853 } else if (node_type == LYXP_NODE_ELEM) {
5854 /* start from the context node */
5855 next = moveto_axis_node_next_dfs_forward(node, node);
5856 next_type = next ? LYXP_NODE_ELEM : 0;
5857 } /* else no children */
5858 break;
5859
5860 case LYXP_AXIS_FOLLOWING:
5861 case LYXP_AXIS_FOLLOWING_SIBLING:
5862 if (node_type == LYXP_NODE_ELEM) {
5863 /* first next sibling */
5864 next = node->next;
5865 next_type = next ? LYXP_NODE_ELEM : 0;
5866 } /* else no sibling */
5867 break;
5868
5869 case LYXP_AXIS_PRECEDING:
5870 if ((node_type == LYXP_NODE_ELEM) && node->prev->next) {
5871 /* skip ancestors */
5872 next = moveto_axis_node_next_dfs_backward(node, NULL);
5873 assert(next);
5874 next_type = LYXP_NODE_ELEM;
5875 } /* else no sibling */
5876 break;
5877
5878 case LYXP_AXIS_PRECEDING_SIBLING:
5879 if (node_type == LYXP_NODE_ELEM) {
5880 /* first previous sibling */
5881 next = node->prev->next ? node->prev : NULL;
5882 next_type = next ? LYXP_NODE_ELEM : 0;
5883 } /* else no sibling */
5884 break;
5885
5886 case LYXP_AXIS_ATTRIBUTE:
5887 /* handled specially */
5888 assert(0);
5889 LOGINT(set->ctx);
5890 break;
5891 }
5892
5893 *iter = next;
5894 *iter_type = next_type;
5895 return next_type ? LY_SUCCESS : LY_ENOTFOUND;
5896}
5897
5898/**
5899 * @brief Iterate over all nodes on an axis for a context node.
5900 *
5901 * @param[in,out] iter Last returned node, start with NULL, updated to the next node.
5902 * @param[in,out] iter_type Node type of @p iter, start with 0, updated to the node type of the next node.
5903 * @param[in] node Context node.
5904 * @param[in] node_type Type of @p node.
5905 * @param[in] axis Axis to use.
5906 * @param[in] set XPath set with the general context.
5907 * @return LY_SUCCESS on success.
5908 * @return LY_ENOTFOUND if no next node found.
5909 */
5910static LY_ERR
5911moveto_axis_node_next(const struct lyd_node **iter, enum lyxp_node_type *iter_type, const struct lyd_node *node,
5912 enum lyxp_node_type node_type, enum lyxp_axis axis, struct lyxp_set *set)
5913{
5914 const struct lyd_node *next = NULL;
5915 enum lyxp_node_type next_type = 0;
5916
5917 if (!*iter_type) {
5918 /* first returned node */
5919 return moveto_axis_node_next_first(iter, iter_type, node, node_type, axis, set);
5920 }
5921
5922 switch (axis) {
5923 case LYXP_AXIS_ANCESTOR_OR_SELF:
5924 if ((*iter == node) && (*iter_type == node_type)) {
5925 /* fake first ancestor, we returned self before */
5926 *iter = NULL;
5927 *iter_type = 0;
5928 return moveto_axis_node_next_first(iter, iter_type, node, node_type, LYXP_AXIS_ANCESTOR, set);
5929 } /* else continue ancestor */
5930
5931 /* fallthrough */
5932 case LYXP_AXIS_ANCESTOR:
5933 if (*iter_type == LYXP_NODE_ELEM) {
5934 /* iter parent */
5935 next = lyd_parent(*iter);
5936 next_type = next ? LYXP_NODE_ELEM : set->root_type;
5937 } /* else root, no ancestors */
5938 break;
5939
5940 case LYXP_AXIS_CHILD:
5941 assert(*iter_type == LYXP_NODE_ELEM);
5942
5943 /* next sibling (child) */
5944 next = (*iter)->next;
5945 next_type = next ? LYXP_NODE_ELEM : 0;
5946 break;
5947
5948 case LYXP_AXIS_DESCENDANT_OR_SELF:
5949 if ((*iter == node) && (*iter_type == node_type)) {
5950 /* fake first descendant, we returned self before */
5951 *iter = NULL;
5952 *iter_type = 0;
5953 return moveto_axis_node_next_first(iter, iter_type, node, node_type, LYXP_AXIS_DESCENDANT, set);
5954 } /* else continue descendant */
5955
5956 /* fallthrough */
5957 case LYXP_AXIS_DESCENDANT:
5958 assert(*iter_type == LYXP_NODE_ELEM);
5959 next = moveto_axis_node_next_dfs_forward(*iter, node);
5960 next_type = next ? LYXP_NODE_ELEM : 0;
5961 break;
5962
5963 case LYXP_AXIS_FOLLOWING:
5964 assert(*iter_type == LYXP_NODE_ELEM);
5965 next = moveto_axis_node_next_dfs_forward(*iter, NULL);
5966 next_type = next ? LYXP_NODE_ELEM : 0;
5967 break;
5968
5969 case LYXP_AXIS_FOLLOWING_SIBLING:
5970 assert(*iter_type == LYXP_NODE_ELEM);
5971
5972 /* next sibling */
5973 next = (*iter)->next;
5974 next_type = next ? LYXP_NODE_ELEM : 0;
5975 break;
5976
5977 case LYXP_AXIS_PARENT:
5978 case LYXP_AXIS_SELF:
5979 /* parent/self was returned before */
5980 break;
5981
5982 case LYXP_AXIS_PRECEDING:
5983 assert(*iter_type == LYXP_NODE_ELEM);
5984 next = moveto_axis_node_next_dfs_backward(*iter, NULL);
5985 next_type = next ? LYXP_NODE_ELEM : 0;
5986 break;
5987
5988 case LYXP_AXIS_PRECEDING_SIBLING:
5989 assert(*iter_type == LYXP_NODE_ELEM);
5990
5991 /* previous sibling */
5992 next = (*iter)->prev->next ? (*iter)->prev : NULL;
5993 next_type = next ? LYXP_NODE_ELEM : 0;
5994 break;
5995
5996 case LYXP_AXIS_ATTRIBUTE:
5997 /* handled specially */
5998 assert(0);
5999 LOGINT(set->ctx);
6000 break;
6001 }
6002
6003 *iter = next;
6004 *iter_type = next_type;
6005 return next_type ? LY_SUCCESS : LY_ENOTFOUND;
6006}
6007
6008/**
6009 * @brief Move context @p set to a node. Result is LYXP_SET_NODE_SET. Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006010 *
6011 * @param[in,out] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006012 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02006013 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko49fec8e2022-05-24 10:28:33 +02006014 * @param[in] axis Axis to search on.
Michal Vaskocdad7122020-11-09 21:04:44 +01006015 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006016 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6017 */
6018static LY_ERR
Michal Vasko49fec8e2022-05-24 10:28:33 +02006019moveto_node(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, enum lyxp_axis axis,
6020 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006021{
Michal Vasko230cf972021-12-02 12:31:00 +01006022 LY_ERR r, rc = LY_SUCCESS;
Michal Vasko49fec8e2022-05-24 10:28:33 +02006023 const struct lyd_node *iter;
6024 enum lyxp_node_type iter_type;
Michal Vasko230cf972021-12-02 12:31:00 +01006025 struct lyxp_set result;
Michal Vasko49fec8e2022-05-24 10:28:33 +02006026 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006027
aPiecek8b0cc152021-05-31 16:40:31 +02006028 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006029 return LY_SUCCESS;
6030 }
6031
6032 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006033 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006034 return LY_EVALID;
6035 }
6036
Michal Vasko230cf972021-12-02 12:31:00 +01006037 /* init result set */
6038 set_init(&result, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006039
Michal Vasko49fec8e2022-05-24 10:28:33 +02006040 for (i = 0; i < set->used; ++i) {
6041 /* iterate over all the nodes on the axis of the node */
6042 iter = NULL;
6043 iter_type = 0;
6044 while (!moveto_axis_node_next(&iter, &iter_type, set->val.nodes[i].node, set->val.nodes[i].type, axis, set)) {
6045 r = moveto_node_check(iter, iter_type, set, ncname, moveto_mod, options);
6046 if (r == LY_EINCOMPLETE) {
Michal Vasko230cf972021-12-02 12:31:00 +01006047 rc = r;
6048 goto cleanup;
Michal Vasko49fec8e2022-05-24 10:28:33 +02006049 } else if (r) {
6050 continue;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006051 }
Michal Vasko49fec8e2022-05-24 10:28:33 +02006052
6053 /* check for duplicates if they are possible */
6054 switch (axis) {
6055 case LYXP_AXIS_ANCESTOR:
6056 case LYXP_AXIS_ANCESTOR_OR_SELF:
6057 case LYXP_AXIS_DESCENDANT:
6058 case LYXP_AXIS_DESCENDANT_OR_SELF:
6059 case LYXP_AXIS_FOLLOWING:
6060 case LYXP_AXIS_FOLLOWING_SIBLING:
6061 case LYXP_AXIS_PARENT:
6062 case LYXP_AXIS_PRECEDING:
6063 case LYXP_AXIS_PRECEDING_SIBLING:
Michal Vasko306e2832022-07-25 09:15:17 +02006064 result.non_child_axis = 1;
Michal Vasko49fec8e2022-05-24 10:28:33 +02006065 if (set_dup_node_check(&result, iter, iter_type, -1)) {
6066 continue;
6067 }
6068 break;
6069 case LYXP_AXIS_CHILD:
6070 case LYXP_AXIS_SELF:
6071 break;
6072 case LYXP_AXIS_ATTRIBUTE:
6073 /* handled specially */
6074 assert(0);
6075 LOGINT(set->ctx);
6076 break;
6077 }
6078
6079 /* matching node */
6080 set_insert_node(&result, iter, 0, iter_type, result.used);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006081 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006082 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006083
Michal Vasko230cf972021-12-02 12:31:00 +01006084 /* move result to the set */
6085 lyxp_set_free_content(set);
6086 *set = result;
6087 result.type = LYXP_SET_NUMBER;
Michal Vasko49fec8e2022-05-24 10:28:33 +02006088
Michal Vasko306e2832022-07-25 09:15:17 +02006089 /* sort the final set if the document order could have been broken */
6090 if (set->non_child_axis) {
6091 set_sort(set);
6092 } else {
6093 assert(!set_sort(set));
6094 }
Michal Vasko230cf972021-12-02 12:31:00 +01006095
6096cleanup:
6097 lyxp_set_free_content(&result);
6098 return rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006099}
6100
6101/**
Michal Vasko49fec8e2022-05-24 10:28:33 +02006102 * @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 +02006103 *
6104 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02006105 * @param[in] scnode Matching node schema.
Michal Vasko004d3152020-06-11 19:59:22 +02006106 * @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 +01006107 * @param[in] options XPath options.
Michal Vaskod3678892020-05-21 10:06:58 +02006108 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6109 */
6110static LY_ERR
Michal Vasko49fec8e2022-05-24 10:28:33 +02006111moveto_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 +01006112 uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02006113{
Michal Vaskoddd76592022-01-17 13:34:48 +01006114 LY_ERR ret = LY_SUCCESS, r;
Michal Vaskod3678892020-05-21 10:06:58 +02006115 uint32_t i;
Michal Vaskod3678892020-05-21 10:06:58 +02006116 const struct lyd_node *siblings;
Michal Vasko230cf972021-12-02 12:31:00 +01006117 struct lyxp_set result;
Michal Vasko004d3152020-06-11 19:59:22 +02006118 struct lyd_node *sub, *inst = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02006119
Michal Vasko004d3152020-06-11 19:59:22 +02006120 assert(scnode && (!(scnode->nodetype & (LYS_LIST | LYS_LEAFLIST)) || predicates));
Michal Vaskod3678892020-05-21 10:06:58 +02006121
Michal Vasko50aaa072021-12-02 13:11:56 +01006122 /* init result set */
6123 set_init(&result, set);
6124
aPiecek8b0cc152021-05-31 16:40:31 +02006125 if (options & LYXP_SKIP_EXPR) {
Michal Vasko004d3152020-06-11 19:59:22 +02006126 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02006127 }
6128
6129 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006130 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko004d3152020-06-11 19:59:22 +02006131 ret = LY_EVALID;
6132 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02006133 }
6134
6135 /* context check for all the nodes since we have the schema node */
6136 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) {
6137 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02006138 goto cleanup;
Michal Vasko69730152020-10-09 16:30:07 +02006139 } else if (set->context_op && (scnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) &&
6140 (scnode != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02006141 lyxp_set_free_content(set);
6142 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +02006143 }
6144
6145 /* create specific data instance if needed */
6146 if (scnode->nodetype == LYS_LIST) {
6147 LY_CHECK_GOTO(ret = lyd_create_list(scnode, predicates, &inst), cleanup);
6148 } else if (scnode->nodetype == LYS_LEAFLIST) {
6149 LY_CHECK_GOTO(ret = lyd_create_term2(scnode, &predicates[0].value, &inst), cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02006150 }
6151
Michal Vasko230cf972021-12-02 12:31:00 +01006152 for (i = 0; i < set->used; ++i) {
Michal Vaskod3678892020-05-21 10:06:58 +02006153 siblings = NULL;
6154
6155 if ((set->val.nodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.nodes[i].type == LYXP_NODE_ROOT)) {
6156 assert(!set->val.nodes[i].node);
6157
6158 /* search in all the trees */
6159 siblings = set->tree;
6160 } else if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
6161 /* search in children */
Radek Krejcia1c1e542020-09-29 16:06:52 +02006162 siblings = lyd_child(set->val.nodes[i].node);
Michal Vaskod3678892020-05-21 10:06:58 +02006163 }
6164
6165 /* find the node using hashes */
Michal Vasko004d3152020-06-11 19:59:22 +02006166 if (inst) {
Michal Vaskoddd76592022-01-17 13:34:48 +01006167 r = lyd_find_sibling_first(siblings, inst, &sub);
Michal Vaskod3678892020-05-21 10:06:58 +02006168 } else {
Michal Vaskoddd76592022-01-17 13:34:48 +01006169 r = lyd_find_sibling_val(siblings, scnode, NULL, 0, &sub);
Michal Vaskod3678892020-05-21 10:06:58 +02006170 }
Michal Vaskoddd76592022-01-17 13:34:48 +01006171 LY_CHECK_ERR_GOTO(r && (r != LY_ENOTFOUND), ret = r, cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02006172
6173 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01006174 if (!(options & LYXP_IGNORE_WHEN) && sub && lysc_has_when(sub->schema) && !(sub->flags & LYD_WHEN_TRUE)) {
Michal Vasko004d3152020-06-11 19:59:22 +02006175 ret = LY_EINCOMPLETE;
6176 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02006177 }
6178
6179 if (sub) {
6180 /* pos filled later */
Michal Vasko230cf972021-12-02 12:31:00 +01006181 set_insert_node(&result, sub, 0, LYXP_NODE_ELEM, result.used);
Michal Vaskod3678892020-05-21 10:06:58 +02006182 }
6183 }
6184
Michal Vasko230cf972021-12-02 12:31:00 +01006185 /* move result to the set */
6186 lyxp_set_free_content(set);
6187 *set = result;
6188 result.type = LYXP_SET_NUMBER;
6189 assert(!set_sort(set));
6190
Michal Vasko004d3152020-06-11 19:59:22 +02006191cleanup:
Michal Vasko230cf972021-12-02 12:31:00 +01006192 lyxp_set_free_content(&result);
Michal Vasko004d3152020-06-11 19:59:22 +02006193 lyd_free_tree(inst);
6194 return ret;
Michal Vaskod3678892020-05-21 10:06:58 +02006195}
6196
6197/**
Michal Vasko49fec8e2022-05-24 10:28:33 +02006198 * @brief Check @p node as a part of schema NameTest processing.
6199 *
6200 * @param[in] node Schema node to check.
6201 * @param[in] ctx_scnode Context node.
6202 * @param[in] set Set to read general context from.
6203 * @param[in] node_name Node name in the dictionary to move to, NULL for any nodes.
6204 * @param[in] moveto_mod Expected module of the node, NULL for no prefix.
6205 * @return LY_ERR (LY_ENOT if node does not match, LY_EINVAL if neither node nor any children match)
6206 */
6207static LY_ERR
6208moveto_scnode_check(const struct lysc_node *node, const struct lysc_node *ctx_scnode, const struct lyxp_set *set,
6209 const char *node_name, const struct lys_module *moveto_mod)
6210{
6211 if (!moveto_mod && node_name) {
6212 switch (set->format) {
6213 case LY_VALUE_SCHEMA:
6214 case LY_VALUE_SCHEMA_RESOLVED:
6215 /* use current module */
6216 moveto_mod = set->cur_mod;
6217 break;
6218 case LY_VALUE_JSON:
6219 case LY_VALUE_LYB:
6220 case LY_VALUE_STR_NS:
6221 /* inherit module of the context node, if any */
6222 if (ctx_scnode) {
6223 moveto_mod = ctx_scnode->module;
6224 }
6225 break;
6226 case LY_VALUE_CANON:
6227 case LY_VALUE_XML:
6228 /* not defined */
6229 LOGINT(set->ctx);
6230 return LY_EINVAL;
6231 }
6232 }
6233
6234 if (!node) {
6235 /* root will not match a specific node */
6236 if (node_name || moveto_mod) {
6237 return LY_ENOT;
6238 }
6239 return LY_SUCCESS;
6240 }
6241
6242 /* module check */
6243 if (moveto_mod && (node->module != moveto_mod)) {
6244 return LY_ENOT;
6245 }
6246
6247 /* context check */
6248 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (node->flags & LYS_CONFIG_R)) {
6249 return LY_EINVAL;
6250 } else if (set->context_op && (node->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && (node != set->context_op)) {
6251 return LY_EINVAL;
6252 }
6253
6254 /* name check */
6255 if (node_name && (node->name != node_name)) {
6256 return LY_ENOT;
6257 }
6258
6259 /* match */
6260 return LY_SUCCESS;
6261}
6262
6263/**
6264 * @brief Get the next node in a forward schema node DFS.
6265 *
6266 * @param[in] iter Last returned node.
6267 * @param[in] stop Node to stop the search on and not return.
6268 * @param[in] getnext_opts Options for ::lys_getnext().
6269 * @return Next node, NULL if there are no more.
6270 */
6271static const struct lysc_node *
6272moveto_axis_scnode_next_dfs_forward(const struct lysc_node *iter, const struct lysc_node *stop, uint32_t getnext_opts)
6273{
6274 const struct lysc_node *next = NULL;
6275
6276 next = lysc_node_child(iter);
6277 if (!next) {
6278 /* no children, try siblings */
Michal Vasko34a22fe2022-06-15 07:58:55 +02006279 if ((iter == stop) || !lysc_data_parent(iter)) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02006280 /* we are done, no next element to process */
6281 return NULL;
6282 }
6283
6284 next = lys_getnext(iter, lysc_data_parent(iter), NULL, getnext_opts);
6285 }
6286 while (!next && iter) {
6287 /* parent is already processed, go to its sibling */
6288 iter = iter->parent;
Michal Vasko34a22fe2022-06-15 07:58:55 +02006289 if ((iter == stop) || !lysc_data_parent(iter)) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02006290 /* we are done, no next element to process */
6291 return NULL;
6292 }
6293 next = lys_getnext(iter, lysc_data_parent(iter), NULL, getnext_opts);
6294 }
6295
6296 return next;
6297}
6298
6299/**
6300 * @brief Consider schema node based on its in_ctx enum value.
6301 *
6302 * @param[in,out] in_ctx In_ctx enum of the schema node, may be updated.
6303 * @param[in] axis Axis to use.
6304 * @return LY_SUCCESS on success.
6305 * @return LY_ENOT if the node should not be returned.
6306 */
6307static LY_ERR
6308moveto_axis_scnode_next_in_ctx(int32_t *in_ctx, enum lyxp_axis axis)
6309{
6310 switch (axis) {
6311 case LYXP_AXIS_SELF:
6312 if ((*in_ctx == LYXP_SET_SCNODE_START) || (*in_ctx == LYXP_SET_SCNODE_ATOM_CTX)) {
6313 /* additionally put the start node into context */
6314 *in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
6315 return LY_SUCCESS;
6316 }
6317 break;
6318 case LYXP_AXIS_PARENT:
6319 case LYXP_AXIS_ANCESTOR_OR_SELF:
6320 case LYXP_AXIS_ANCESTOR:
6321 case LYXP_AXIS_DESCENDANT_OR_SELF:
6322 case LYXP_AXIS_DESCENDANT:
6323 case LYXP_AXIS_FOLLOWING:
6324 case LYXP_AXIS_FOLLOWING_SIBLING:
6325 case LYXP_AXIS_PRECEDING:
6326 case LYXP_AXIS_PRECEDING_SIBLING:
6327 case LYXP_AXIS_CHILD:
6328 if (*in_ctx == LYXP_SET_SCNODE_START) {
6329 /* remember that context node was used */
6330 *in_ctx = LYXP_SET_SCNODE_START_USED;
6331 return LY_SUCCESS;
6332 } else if (*in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
6333 /* traversed */
6334 *in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
6335 return LY_SUCCESS;
6336 }
6337 break;
6338 case LYXP_AXIS_ATTRIBUTE:
6339 /* unreachable */
6340 assert(0);
6341 LOGINT(NULL);
6342 break;
6343 }
6344
6345 return LY_ENOT;
6346}
6347
6348/**
6349 * @brief Get previous sibling for a schema node.
6350 *
6351 * @param[in] scnode Schema node.
6352 * @param[in] getnext_opts Options for ::lys_getnext().
6353 * @return Previous sibling, NULL if none.
6354 */
6355static const struct lysc_node *
6356moveto_axis_scnode_preceding_sibling(const struct lysc_node *scnode, uint32_t getnext_opts)
6357{
6358 const struct lysc_node *next = NULL, *prev = NULL;
6359
6360 while ((next = lys_getnext(next, lysc_data_parent(scnode), scnode->module->compiled, getnext_opts))) {
6361 if (next == scnode) {
6362 break;
6363 }
6364
6365 prev = next;
6366 }
6367
6368 return prev;
6369}
6370
6371/**
6372 * @brief Get the first schema node on an axis for a context node.
6373 *
6374 * @param[in,out] iter Last returned node, start with NULL, updated to the next node.
6375 * @param[in,out] iter_type Node type of @p iter, start with 0, updated to the node type of the next node.
6376 * @param[in,out] iter_mod Internal module iterator, do not change.
6377 * @param[in,out] iter_mod_idx Internal module index iterator, do not change.
6378 * @param[in] scnode Context node.
6379 * @param[in] node_type Type of @p scnode.
6380 * @param[in] in_ctx In_ctx enum of @p scnode.
6381 * @param[in] axis Axis to use.
6382 * @param[in] set XPath set with the general context.
6383 * @param[in] getnext_opts Options for ::lys_getnext().
6384 * @return LY_SUCCESS on success.
6385 * @return LY_ENOTFOUND if no next node found.
6386 */
6387static LY_ERR
6388moveto_axis_scnode_next_first(const struct lysc_node **iter, enum lyxp_node_type *iter_type, const struct lys_module **iter_mod,
6389 uint32_t *iter_mod_idx, const struct lysc_node *scnode, enum lyxp_node_type node_type, enum lyxp_axis axis,
6390 struct lyxp_set *set, uint32_t getnext_opts)
6391{
6392 const struct lysc_node *next = NULL;
6393 enum lyxp_node_type next_type = 0;
6394
6395 assert(!*iter);
6396 assert(!*iter_type);
6397
6398 *iter_mod = NULL;
6399 *iter_mod_idx = 0;
6400
6401 switch (axis) {
6402 case LYXP_AXIS_ANCESTOR_OR_SELF:
6403 case LYXP_AXIS_DESCENDANT_OR_SELF:
6404 case LYXP_AXIS_SELF:
6405 if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT) || (node_type == LYXP_NODE_ELEM)) {
6406 /* just return the node */
6407 next = scnode;
6408 next_type = node_type;
6409 }
6410 break;
6411
6412 case LYXP_AXIS_ANCESTOR:
6413 case LYXP_AXIS_PARENT:
6414 if (node_type == LYXP_NODE_ELEM) {
6415 next = lysc_data_parent(scnode);
6416 next_type = next ? LYXP_NODE_ELEM : set->root_type;
6417 } /* else no parent */
6418 break;
6419
6420 case LYXP_AXIS_DESCENDANT:
6421 case LYXP_AXIS_CHILD:
6422 if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT)) {
6423 /* it can actually be in any module, it's all <running>, and even if it's moveto_mod (if set),
6424 * it can be in a top-level augment */
6425 while ((*iter_mod = ly_ctx_get_module_iter(set->ctx, iter_mod_idx))) {
6426 /* module may not be implemented or not compiled yet */
6427 if (!(*iter_mod)->compiled) {
6428 continue;
6429 }
6430
6431 /* get next node */
6432 if ((next = lys_getnext(NULL, NULL, (*iter_mod)->compiled, getnext_opts))) {
6433 next_type = LYXP_NODE_ELEM;
6434 break;
6435 }
6436 }
6437 } else if (node_type == LYXP_NODE_ELEM) {
6438 /* get next node */
6439 next = lys_getnext(NULL, scnode, NULL, getnext_opts);
6440 next_type = next ? LYXP_NODE_ELEM : 0;
6441 }
6442 break;
6443
6444 case LYXP_AXIS_FOLLOWING:
6445 case LYXP_AXIS_FOLLOWING_SIBLING:
6446 if (node_type == LYXP_NODE_ELEM) {
6447 /* first next sibling */
6448 next = lys_getnext(scnode, lysc_data_parent(scnode), scnode->module->compiled, getnext_opts);
6449 next_type = next ? LYXP_NODE_ELEM : 0;
6450 } /* else no sibling */
6451 break;
6452
6453 case LYXP_AXIS_PRECEDING:
6454 case LYXP_AXIS_PRECEDING_SIBLING:
6455 if (node_type == LYXP_NODE_ELEM) {
6456 /* first parent sibling */
6457 next = lys_getnext(NULL, lysc_data_parent(scnode), scnode->module->compiled, getnext_opts);
6458 if (next == scnode) {
6459 /* no preceding sibling */
6460 next = NULL;
6461 }
6462 next_type = next ? LYXP_NODE_ELEM : 0;
6463 } /* else no sibling */
6464 break;
6465
6466 case LYXP_AXIS_ATTRIBUTE:
6467 /* unreachable */
6468 assert(0);
6469 LOGINT(set->ctx);
6470 break;
6471 }
6472
6473 *iter = next;
6474 *iter_type = next_type;
6475 return next_type ? LY_SUCCESS : LY_ENOTFOUND;
6476}
6477
6478/**
6479 * @brief Iterate over all schema nodes on an axis for a context node.
6480 *
6481 * @param[in,out] iter Last returned node, start with NULL, updated to the next node.
6482 * @param[in,out] iter_type Node type of @p iter, start with 0, updated to the node type of the next node.
6483 * @param[in,out] iter_mod Internal module iterator, do not change.
6484 * @param[in,out] iter_mod_idx Internal module index iterator, do not change.
6485 * @param[in] scnode Context node.
6486 * @param[in] node_type Type of @p scnode.
6487 * @param[in] axis Axis to use.
6488 * @param[in] set XPath set with the general context.
6489 * @param[in] getnext_opts Options for ::lys_getnext().
6490 * @return LY_SUCCESS on success.
6491 * @return LY_ENOTFOUND if no next node found.
6492 */
6493static LY_ERR
6494moveto_axis_scnode_next(const struct lysc_node **iter, enum lyxp_node_type *iter_type, const struct lys_module **iter_mod,
6495 uint32_t *iter_mod_idx, const struct lysc_node *scnode, enum lyxp_node_type node_type, enum lyxp_axis axis,
6496 struct lyxp_set *set, uint32_t getnext_opts)
6497{
6498 const struct lysc_node *next = NULL, *dfs_stop;
6499 enum lyxp_node_type next_type = 0;
6500
6501 if (!*iter_type) {
6502 /* first returned node */
6503 return moveto_axis_scnode_next_first(iter, iter_type, iter_mod, iter_mod_idx, scnode, node_type, axis, set,
6504 getnext_opts);
6505 }
6506
6507 switch (axis) {
6508 case LYXP_AXIS_PARENT:
6509 case LYXP_AXIS_SELF:
6510 /* parent/self was returned before */
6511 break;
6512
6513 case LYXP_AXIS_ANCESTOR_OR_SELF:
6514 if ((*iter == scnode) && (*iter_type == node_type)) {
6515 /* fake first ancestor, we returned self before */
6516 *iter = NULL;
6517 *iter_type = 0;
6518 return moveto_axis_scnode_next_first(iter, iter_type, iter_mod, iter_mod_idx, scnode, node_type,
6519 LYXP_AXIS_ANCESTOR, set, getnext_opts);
6520 } /* else continue ancestor */
6521
6522 /* fallthrough */
6523 case LYXP_AXIS_ANCESTOR:
6524 if (*iter_type == LYXP_NODE_ELEM) {
6525 next = lysc_data_parent(*iter);
6526 next_type = next ? LYXP_NODE_ELEM : set->root_type;
6527 } /* else no ancestor */
6528 break;
6529
6530 case LYXP_AXIS_DESCENDANT_OR_SELF:
6531 if ((*iter == scnode) && (*iter_type == node_type)) {
6532 /* fake first descendant, we returned self before */
6533 *iter = NULL;
6534 *iter_type = 0;
6535 return moveto_axis_scnode_next_first(iter, iter_type, iter_mod, iter_mod_idx, scnode, node_type,
6536 LYXP_AXIS_DESCENDANT, set, getnext_opts);
6537 } /* else DFS until context node */
6538 dfs_stop = scnode;
6539
6540 /* fallthrough */
6541 case LYXP_AXIS_DESCENDANT:
6542 if (axis == LYXP_AXIS_DESCENDANT) {
6543 /* DFS until the context node */
6544 dfs_stop = scnode;
6545 }
6546
6547 /* fallthrough */
6548 case LYXP_AXIS_PRECEDING:
6549 if (axis == LYXP_AXIS_PRECEDING) {
6550 /* DFS until the previous sibling */
6551 dfs_stop = moveto_axis_scnode_preceding_sibling(scnode, getnext_opts);
6552 assert(dfs_stop);
6553
6554 if (*iter == dfs_stop) {
6555 /* we are done */
6556 break;
6557 }
6558 }
6559
6560 /* fallthrough */
6561 case LYXP_AXIS_FOLLOWING:
6562 if (axis == LYXP_AXIS_FOLLOWING) {
6563 /* DFS through the whole module */
6564 dfs_stop = NULL;
6565 }
6566
6567 /* nested nodes */
6568 assert(*iter);
6569 next = moveto_axis_scnode_next_dfs_forward(*iter, dfs_stop, getnext_opts);
6570 if (next) {
6571 next_type = LYXP_NODE_ELEM;
6572 break;
6573 } /* else get next top-level node just like a child */
6574
6575 /* fallthrough */
6576 case LYXP_AXIS_CHILD:
6577 case LYXP_AXIS_FOLLOWING_SIBLING:
6578 if (!*iter_mod) {
6579 /* nodes from a single module */
6580 if ((next = lys_getnext(*iter, lysc_data_parent(*iter), (*iter)->module->compiled, getnext_opts))) {
6581 next_type = LYXP_NODE_ELEM;
6582 break;
6583 }
6584
6585 assert(scnode);
6586 if (!lysc_data_parent(scnode)) {
6587 /* iterating over top-level nodes, find next */
6588 while (lysc_data_parent(*iter)) {
6589 *iter = lysc_data_parent(*iter);
6590 }
6591 if ((next = lys_getnext(*iter, NULL, (*iter)->module->compiled, getnext_opts))) {
6592 next_type = LYXP_NODE_ELEM;
6593 break;
6594 }
6595 }
6596 }
6597
6598 while (*iter_mod) {
6599 /* module top-level nodes */
6600 if ((next = lys_getnext(*iter, NULL, (*iter_mod)->compiled, getnext_opts))) {
6601 next_type = LYXP_NODE_ELEM;
6602 break;
6603 }
6604
6605 /* get next module */
6606 while ((*iter_mod = ly_ctx_get_module_iter(set->ctx, iter_mod_idx))) {
6607 /* module may not be implemented or not compiled yet */
6608 if ((*iter_mod)->compiled) {
6609 break;
6610 }
6611 }
6612
6613 /* new module, start over */
6614 *iter = NULL;
6615 }
6616 break;
6617
6618 case LYXP_AXIS_PRECEDING_SIBLING:
6619 assert(*iter);
6620
6621 /* next parent sibling until scnode */
6622 next = lys_getnext(*iter, lysc_data_parent(*iter), (*iter)->module->compiled, getnext_opts);
6623 if (next == scnode) {
6624 /* no previous sibling */
6625 next = NULL;
6626 }
6627 next_type = next ? LYXP_NODE_ELEM : 0;
6628 break;
6629
6630 case LYXP_AXIS_ATTRIBUTE:
6631 /* unreachable */
6632 assert(0);
6633 LOGINT(set->ctx);
6634 break;
6635 }
6636
6637 *iter = next;
6638 *iter_type = next_type;
6639 return next_type ? LY_SUCCESS : LY_ENOTFOUND;
6640}
6641
6642/**
Michal Vaskod3678892020-05-21 10:06:58 +02006643 * @brief Move context @p set to a schema node. Result is LYXP_SET_SCNODE_SET (or LYXP_SET_EMPTY).
6644 *
6645 * @param[in,out] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006646 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02006647 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko49fec8e2022-05-24 10:28:33 +02006648 * @param[in] axis Axis to search on.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006649 * @param[in] options XPath options.
6650 * @return LY_ERR
6651 */
6652static LY_ERR
Michal Vasko49fec8e2022-05-24 10:28:33 +02006653moveto_scnode(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, enum lyxp_axis axis,
6654 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006655{
Radek Krejci857189e2020-09-01 13:26:36 +02006656 ly_bool temp_ctx = 0;
Michal Vasko49fec8e2022-05-24 10:28:33 +02006657 uint32_t getnext_opts, orig_used, i, mod_idx, idx;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006658 const struct lys_module *mod;
Michal Vasko49fec8e2022-05-24 10:28:33 +02006659 const struct lysc_node *iter;
6660 enum lyxp_node_type iter_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006661
aPiecek8b0cc152021-05-31 16:40:31 +02006662 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006663 return LY_SUCCESS;
6664 }
6665
6666 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006667 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006668 return LY_EVALID;
6669 }
6670
Michal Vaskocafad9d2019-11-07 15:20:03 +01006671 /* getnext opts */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01006672 getnext_opts = 0;
Michal Vaskocafad9d2019-11-07 15:20:03 +01006673 if (options & LYXP_SCNODE_OUTPUT) {
6674 getnext_opts |= LYS_GETNEXT_OUTPUT;
6675 }
6676
Michal Vasko03ff5a72019-09-11 13:49:33 +02006677 orig_used = set->used;
6678 for (i = 0; i < orig_used; ++i) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02006679 /* update in_ctx first */
6680 if (moveto_axis_scnode_next_in_ctx(&set->val.scnodes[i].in_ctx, axis)) {
6681 /* not usable, skip */
6682 continue;
6683 }
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006684
Michal Vasko49fec8e2022-05-24 10:28:33 +02006685 iter = NULL;
6686 iter_type = 0;
6687 while (!moveto_axis_scnode_next(&iter, &iter_type, &mod, &mod_idx, set->val.scnodes[i].scnode,
6688 set->val.scnodes[i].type, axis, set, getnext_opts)) {
6689 if (moveto_scnode_check(iter, NULL, set, ncname, moveto_mod)) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006690 continue;
6691 }
6692
Michal Vasko49fec8e2022-05-24 10:28:33 +02006693 /* insert */
Michal Vasko7333cb32022-07-29 16:30:29 +02006694 LY_CHECK_RET(set_scnode_insert_node(set, iter, iter_type, axis, &idx));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006695
Michal Vasko49fec8e2022-05-24 10:28:33 +02006696 /* we need to prevent these nodes from being considered in this moveto */
6697 if ((idx < orig_used) && (idx > i)) {
6698 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX;
6699 temp_ctx = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006700 }
6701 }
6702 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006703
6704 /* correct temporary in_ctx values */
6705 if (temp_ctx) {
6706 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006707 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_NEW_CTX) {
6708 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006709 }
6710 }
6711 }
6712
6713 return LY_SUCCESS;
6714}
6715
6716/**
Michal Vasko49fec8e2022-05-24 10:28:33 +02006717 * @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 +02006718 * Context position aware.
6719 *
6720 * @param[in] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006721 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02006722 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vaskocdad7122020-11-09 21:04:44 +01006723 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006724 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6725 */
6726static LY_ERR
Michal Vasko49fec8e2022-05-24 10:28:33 +02006727moveto_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 +02006728{
6729 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006730 const struct lyd_node *next, *elem, *start;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006731 struct lyxp_set ret_set;
6732 LY_ERR rc;
6733
aPiecek8b0cc152021-05-31 16:40:31 +02006734 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006735 return LY_SUCCESS;
6736 }
6737
6738 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006739 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006740 return LY_EVALID;
6741 }
6742
Michal Vasko9f96a052020-03-10 09:41:45 +01006743 /* 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 +02006744 rc = xpath_pi_node(set, LYXP_AXIS_CHILD, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006745 LY_CHECK_RET(rc);
6746
Michal Vasko6346ece2019-09-24 13:12:53 +02006747 /* this loop traverses all the nodes in the set and adds/keeps only those that match qname */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006748 set_init(&ret_set, set);
6749 for (i = 0; i < set->used; ++i) {
6750
6751 /* TREE DFS */
6752 start = set->val.nodes[i].node;
6753 for (elem = next = start; elem; elem = next) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02006754 rc = moveto_node_check(elem, LYXP_NODE_ELEM, set, ncname, moveto_mod, options);
Michal Vasko6346ece2019-09-24 13:12:53 +02006755 if (!rc) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006756 /* add matching node into result set */
6757 set_insert_node(&ret_set, elem, 0, LYXP_NODE_ELEM, ret_set.used);
6758 if (set_dup_node_check(set, elem, LYXP_NODE_ELEM, i)) {
6759 /* the node is a duplicate, we'll process it later in the set */
6760 goto skip_children;
6761 }
Michal Vasko6346ece2019-09-24 13:12:53 +02006762 } else if (rc == LY_EINCOMPLETE) {
Michal Vasko6346ece2019-09-24 13:12:53 +02006763 return rc;
6764 } else if (rc == LY_EINVAL) {
6765 goto skip_children;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006766 }
6767
6768 /* TREE DFS NEXT ELEM */
6769 /* select element for the next run - children first */
Radek Krejcia1c1e542020-09-29 16:06:52 +02006770 next = lyd_child(elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006771 if (!next) {
6772skip_children:
6773 /* no children, so try siblings, but only if it's not the start,
6774 * that is considered to be the root and it's siblings are not traversed */
6775 if (elem != start) {
6776 next = elem->next;
6777 } else {
6778 break;
6779 }
6780 }
6781 while (!next) {
6782 /* no siblings, go back through the parents */
Michal Vasko9e685082021-01-29 14:49:09 +01006783 if (lyd_parent(elem) == start) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006784 /* we are done, no next element to process */
6785 break;
6786 }
6787 /* parent is already processed, go to its sibling */
Michal Vasko9e685082021-01-29 14:49:09 +01006788 elem = lyd_parent(elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006789 next = elem->next;
6790 }
6791 }
6792 }
6793
6794 /* make the temporary set the current one */
6795 ret_set.ctx_pos = set->ctx_pos;
6796 ret_set.ctx_size = set->ctx_size;
Michal Vaskod3678892020-05-21 10:06:58 +02006797 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006798 memcpy(set, &ret_set, sizeof *set);
Michal Vasko306e2832022-07-25 09:15:17 +02006799 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006800
6801 return LY_SUCCESS;
6802}
6803
6804/**
Michal Vasko49fec8e2022-05-24 10:28:33 +02006805 * @brief Move context @p set to a child schema node and all its descendants starting from a node.
6806 * Result is LYXP_SET_NODE_SET.
6807 *
6808 * @param[in] set Set to use.
6809 * @param[in] start Start node whose subtree to add.
6810 * @param[in] start_idx Index of @p start in @p set.
6811 * @param[in] moveto_mod Matching node module, NULL for no prefix.
6812 * @param[in] ncname Matching node name in the dictionary, NULL for any.
6813 * @param[in] options XPath options.
6814 * @return LY_ERR value.
6815 */
6816static LY_ERR
6817moveto_scnode_dfs(struct lyxp_set *set, const struct lysc_node *start, uint32_t start_idx,
6818 const struct lys_module *moveto_mod, const char *ncname, uint32_t options)
6819{
6820 const struct lysc_node *next, *elem;
6821 uint32_t idx;
6822 LY_ERR rc;
6823
6824 /* TREE DFS */
6825 for (elem = next = start; elem; elem = next) {
6826 if ((elem == start) || (elem->nodetype & (LYS_CHOICE | LYS_CASE))) {
6827 /* schema-only nodes, skip root */
6828 goto next_iter;
6829 }
6830
6831 rc = moveto_scnode_check(elem, start, set, ncname, moveto_mod);
6832 if (!rc) {
6833 if (lyxp_set_scnode_contains(set, elem, LYXP_NODE_ELEM, start_idx, &idx)) {
6834 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
6835 if (idx > start_idx) {
6836 /* we will process it later in the set */
6837 goto skip_children;
6838 }
6839 } else {
Michal Vasko7333cb32022-07-29 16:30:29 +02006840 LY_CHECK_RET(set_scnode_insert_node(set, elem, LYXP_NODE_ELEM, LYXP_AXIS_DESCENDANT, NULL));
Michal Vasko49fec8e2022-05-24 10:28:33 +02006841 }
6842 } else if (rc == LY_EINVAL) {
6843 goto skip_children;
6844 }
6845
6846next_iter:
6847 /* TREE DFS NEXT ELEM */
6848 /* select element for the next run - children first */
6849 next = lysc_node_child(elem);
6850 if (next && (next->nodetype == LYS_INPUT) && (options & LYXP_SCNODE_OUTPUT)) {
6851 next = next->next;
6852 } else if (next && (next->nodetype == LYS_OUTPUT) && !(options & LYXP_SCNODE_OUTPUT)) {
6853 next = next->next;
6854 }
6855 if (!next) {
6856skip_children:
6857 /* no children, so try siblings, but only if it's not the start,
6858 * that is considered to be the root and it's siblings are not traversed */
6859 if (elem != start) {
6860 next = elem->next;
6861 } else {
6862 break;
6863 }
6864 }
6865 while (!next) {
6866 /* no siblings, go back through the parents */
6867 if (elem->parent == start) {
6868 /* we are done, no next element to process */
6869 break;
6870 }
6871 /* parent is already processed, go to its sibling */
6872 elem = elem->parent;
6873 next = elem->next;
6874 }
6875 }
6876
6877 return LY_SUCCESS;
6878}
6879
6880/**
6881 * @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 +02006882 *
6883 * @param[in] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006884 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02006885 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006886 * @param[in] options XPath options.
Michal Vasko49fec8e2022-05-24 10:28:33 +02006887 * @return LY_ERR value.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006888 */
6889static LY_ERR
Michal Vasko49fec8e2022-05-24 10:28:33 +02006890moveto_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 +02006891{
Michal Vasko49fec8e2022-05-24 10:28:33 +02006892 uint32_t i, orig_used, mod_idx;
6893 const struct lys_module *mod;
6894 const struct lysc_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006895
aPiecek8b0cc152021-05-31 16:40:31 +02006896 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006897 return LY_SUCCESS;
6898 }
6899
6900 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006901 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006902 return LY_EVALID;
6903 }
6904
Michal Vasko03ff5a72019-09-11 13:49:33 +02006905 orig_used = set->used;
6906 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006907 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
6908 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006909 continue;
6910 }
6911
6912 /* remember context node */
Radek Krejcif13b87b2020-12-01 22:02:17 +01006913 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vaskoec4df482019-12-16 10:02:18 +01006914 } else {
Michal Vasko1a09b212021-05-06 13:00:10 +02006915 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006916 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006917
Michal Vasko49fec8e2022-05-24 10:28:33 +02006918 if ((set->val.scnodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.scnodes[i].type == LYXP_NODE_ROOT)) {
6919 /* traverse all top-level nodes in all the modules */
6920 mod_idx = 0;
6921 while ((mod = ly_ctx_get_module_iter(set->ctx, &mod_idx))) {
6922 /* module may not be implemented or not compiled yet */
6923 if (!mod->compiled) {
6924 continue;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006925 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006926
Michal Vasko49fec8e2022-05-24 10:28:33 +02006927 root = NULL;
6928 /* no getnext opts needed */
6929 while ((root = lys_getnext(root, NULL, mod->compiled, 0))) {
6930 LY_CHECK_RET(moveto_scnode_dfs(set, root, i, moveto_mod, ncname, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006931 }
6932 }
Michal Vasko49fec8e2022-05-24 10:28:33 +02006933
6934 } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
6935 /* add all the descendants recursively */
6936 LY_CHECK_RET(moveto_scnode_dfs(set, set->val.scnodes[i].scnode, i, moveto_mod, ncname, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006937 }
6938 }
6939
6940 return LY_SUCCESS;
6941}
6942
6943/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02006944 * @brief Move context @p set to an attribute. Result is LYXP_SET_NODE_SET.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006945 * Indirectly context position aware.
6946 *
6947 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02006948 * @param[in] mod Matching metadata module, NULL for any.
6949 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
aPiecek8b0cc152021-05-31 16:40:31 +02006950 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006951 * @return LY_ERR
6952 */
6953static LY_ERR
aPiecek8b0cc152021-05-31 16:40:31 +02006954moveto_attr(struct lyxp_set *set, const struct lys_module *mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006955{
Michal Vasko9f96a052020-03-10 09:41:45 +01006956 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006957
aPiecek8b0cc152021-05-31 16:40:31 +02006958 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006959 return LY_SUCCESS;
6960 }
6961
6962 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006963 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006964 return LY_EVALID;
6965 }
6966
Radek Krejci1deb5be2020-08-26 16:43:36 +02006967 for (uint32_t i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02006968 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006969
6970 /* only attributes of an elem (not dummy) can be in the result, skip all the rest;
6971 * our attributes are always qualified */
Michal Vasko5c4e5892019-11-14 12:31:38 +01006972 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01006973 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006974
6975 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02006976 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006977 continue;
6978 }
6979
Michal Vaskod3678892020-05-21 10:06:58 +02006980 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006981 /* match */
6982 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01006983 set->val.meta[i].meta = sub;
6984 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006985 /* pos does not change */
6986 replaced = 1;
6987 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01006988 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 +02006989 }
6990 ++i;
6991 }
6992 }
6993 }
6994
6995 if (!replaced) {
6996 /* no match */
6997 set_remove_node(set, i);
6998 }
6999 }
7000
7001 return LY_SUCCESS;
7002}
7003
7004/**
7005 * @brief Move context @p set1 to union with @p set2. @p set2 is emptied afterwards.
Michal Vasko61ac2f62020-05-25 12:39:51 +02007006 * Result is LYXP_SET_NODE_SET. Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007007 *
7008 * @param[in,out] set1 Set to use for the result.
7009 * @param[in] set2 Set that is copied to @p set1.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007010 * @return LY_ERR
7011 */
7012static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007013moveto_union(struct lyxp_set *set1, struct lyxp_set *set2)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007014{
7015 LY_ERR rc;
7016
Michal Vaskod3678892020-05-21 10:06:58 +02007017 if ((set1->type != LYXP_SET_NODE_SET) || (set2->type != LYXP_SET_NODE_SET)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01007018 LOGVAL(set1->ctx, LY_VCODE_XP_INOP_2, "union", print_set_type(set1), print_set_type(set2));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007019 return LY_EVALID;
7020 }
7021
7022 /* set2 is empty or both set1 and set2 */
Michal Vaskod3678892020-05-21 10:06:58 +02007023 if (!set2->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007024 return LY_SUCCESS;
7025 }
7026
Michal Vaskod3678892020-05-21 10:06:58 +02007027 if (!set1->used) {
aPiecekadc1e4f2021-10-07 11:15:12 +02007028 /* release hidden allocated data (lyxp_set.size) */
7029 lyxp_set_free_content(set1);
7030 /* direct copying of the entire structure */
Michal Vasko03ff5a72019-09-11 13:49:33 +02007031 memcpy(set1, set2, sizeof *set1);
7032 /* dynamic memory belongs to set1 now, do not free */
Michal Vaskod3678892020-05-21 10:06:58 +02007033 memset(set2, 0, sizeof *set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007034 return LY_SUCCESS;
7035 }
7036
7037 /* we assume sets are sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007038 assert(!set_sort(set1) && !set_sort(set2));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007039
7040 /* sort, remove duplicates */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007041 rc = set_sorted_merge(set1, set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007042 LY_CHECK_RET(rc);
7043
7044 /* final set must be sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007045 assert(!set_sort(set1));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007046
7047 return LY_SUCCESS;
7048}
7049
7050/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02007051 * @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 +02007052 * Context position aware.
7053 *
7054 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02007055 * @param[in] mod Matching metadata module, NULL for any.
7056 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
Michal Vaskocdad7122020-11-09 21:04:44 +01007057 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007058 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7059 */
7060static int
Michal Vaskocdad7122020-11-09 21:04:44 +01007061moveto_attr_alldesc(struct lyxp_set *set, const struct lys_module *mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007062{
Michal Vasko9f96a052020-03-10 09:41:45 +01007063 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007064 struct lyxp_set *set_all_desc = NULL;
7065 LY_ERR rc;
7066
aPiecek8b0cc152021-05-31 16:40:31 +02007067 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007068 return LY_SUCCESS;
7069 }
7070
7071 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01007072 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007073 return LY_EVALID;
7074 }
7075
Michal Vasko03ff5a72019-09-11 13:49:33 +02007076 /* can be optimized similarly to moveto_node_alldesc() and save considerable amount of memory,
7077 * but it likely won't be used much, so it's a waste of time */
7078 /* copy the context */
7079 set_all_desc = set_copy(set);
7080 /* get all descendant nodes (the original context nodes are removed) */
Michal Vasko49fec8e2022-05-24 10:28:33 +02007081 rc = moveto_node_alldesc_child(set_all_desc, NULL, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007082 if (rc != LY_SUCCESS) {
7083 lyxp_set_free(set_all_desc);
7084 return rc;
7085 }
7086 /* prepend the original context nodes */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007087 rc = moveto_union(set, set_all_desc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007088 if (rc != LY_SUCCESS) {
7089 lyxp_set_free(set_all_desc);
7090 return rc;
7091 }
7092 lyxp_set_free(set_all_desc);
7093
Radek Krejci1deb5be2020-08-26 16:43:36 +02007094 for (uint32_t i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02007095 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007096
7097 /* only attributes of an elem can be in the result, skip all the rest,
7098 * we have all attributes qualified in lyd tree */
7099 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01007100 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007101 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02007102 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007103 continue;
7104 }
7105
Michal Vaskod3678892020-05-21 10:06:58 +02007106 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007107 /* match */
7108 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01007109 set->val.meta[i].meta = sub;
7110 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007111 /* pos does not change */
7112 replaced = 1;
7113 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01007114 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 +02007115 }
7116 ++i;
7117 }
7118 }
7119 }
7120
7121 if (!replaced) {
7122 /* no match */
7123 set_remove_node(set, i);
7124 }
7125 }
7126
7127 return LY_SUCCESS;
7128}
7129
7130/**
Michal Vasko8abcecc2022-07-28 09:55:01 +02007131 * @brief Move context @p set1 single item to the result of a comparison.
7132 *
7133 * @param[in] set1 First set with the item to compare.
7134 * @param[in] idx1 Index of the item in @p set1.
7135 * @param[in] set2 Second set.
7136 * @param[in] op Comparison operator to process.
7137 * @param[in] switch_operands Whether to switch sets as operands; whether it is `set1 op set2` or `set2 op set1`.
7138 * @param[out] result Result of the comparison.
7139 * @return LY_ERR value.
7140 */
7141static LY_ERR
7142moveto_op_comp_item(const struct lyxp_set *set1, uint32_t idx1, struct lyxp_set *set2, const char *op,
7143 ly_bool switch_operands, ly_bool *result)
7144{
7145 struct lyxp_set tmp1 = {0};
7146 LY_ERR rc = LY_SUCCESS;
7147
7148 assert(set1->type == LYXP_SET_NODE_SET);
7149
7150 /* cast set1 */
7151 switch (set2->type) {
7152 case LYXP_SET_NUMBER:
7153 rc = set_comp_cast(&tmp1, set1, LYXP_SET_NUMBER, idx1);
7154 break;
7155 case LYXP_SET_BOOLEAN:
7156 rc = set_comp_cast(&tmp1, set1, LYXP_SET_BOOLEAN, idx1);
7157 break;
7158 default:
7159 rc = set_comp_cast(&tmp1, set1, LYXP_SET_STRING, idx1);
7160 break;
7161 }
7162 LY_CHECK_GOTO(rc, cleanup);
7163
7164 /* canonize set2 */
7165 LY_CHECK_GOTO(rc = set_comp_canonize(set2, &set1->val.nodes[idx1]), cleanup);
7166
7167 /* compare recursively and store the result */
7168 if (switch_operands) {
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007169 LY_CHECK_GOTO(rc = moveto_op_comp(set2, &tmp1, op, result), cleanup);
Michal Vasko8abcecc2022-07-28 09:55:01 +02007170 } else {
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007171 LY_CHECK_GOTO(rc = moveto_op_comp(&tmp1, set2, op, result), cleanup);
Michal Vasko8abcecc2022-07-28 09:55:01 +02007172 }
7173
7174cleanup:
7175 lyxp_set_free_content(&tmp1);
7176 return rc;
7177}
7178
7179/**
7180 * @brief Move context @p set1 to the result of a comparison. Handles '=', '!=', '<=', '<', '>=', or '>'.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007181 * Result is LYXP_SET_BOOLEAN. Indirectly context position aware.
7182 *
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007183 * @param[in] set1 Set acting as the first operand for @p op.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007184 * @param[in] set2 Set acting as the second operand for @p op.
7185 * @param[in] op Comparison operator to process.
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007186 * @param[out] result Result of the comparison.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007187 * @return LY_ERR
7188 */
7189static LY_ERR
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007190moveto_op_comp(struct lyxp_set *set1, struct lyxp_set *set2, const char *op, ly_bool *result)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007191{
7192 /*
7193 * NODE SET + NODE SET = NODE SET + STRING /(1 NODE SET) 2 STRING
7194 * NODE SET + STRING = STRING + STRING /1 STRING (2 STRING)
7195 * NODE SET + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
7196 * NODE SET + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
7197 * STRING + NODE SET = STRING + STRING /(1 STRING) 2 STRING
7198 * NUMBER + NODE SET = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
7199 * BOOLEAN + NODE SET = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
7200 *
7201 * '=' or '!='
7202 * BOOLEAN + BOOLEAN
7203 * BOOLEAN + STRING = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
7204 * BOOLEAN + NUMBER = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
7205 * STRING + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
7206 * NUMBER + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
7207 * NUMBER + NUMBER
7208 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
7209 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
7210 * STRING + STRING
7211 *
7212 * '<=', '<', '>=', '>'
7213 * NUMBER + NUMBER
7214 * BOOLEAN + BOOLEAN = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
7215 * BOOLEAN + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
7216 * BOOLEAN + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
7217 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
7218 * STRING + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
7219 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
7220 * NUMBER + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
7221 * STRING + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
7222 */
Michal Vasko8abcecc2022-07-28 09:55:01 +02007223 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007224 LY_ERR rc;
7225
Michal Vasko03ff5a72019-09-11 13:49:33 +02007226 /* iterative evaluation with node-sets */
7227 if ((set1->type == LYXP_SET_NODE_SET) || (set2->type == LYXP_SET_NODE_SET)) {
7228 if (set1->type == LYXP_SET_NODE_SET) {
7229 for (i = 0; i < set1->used; ++i) {
Michal Vasko8abcecc2022-07-28 09:55:01 +02007230 /* evaluate for the single item */
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007231 LY_CHECK_RET(moveto_op_comp_item(set1, i, set2, op, 0, result));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007232
7233 /* lazy evaluation until true */
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007234 if (*result) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007235 return LY_SUCCESS;
7236 }
7237 }
7238 } else {
7239 for (i = 0; i < set2->used; ++i) {
Michal Vasko8abcecc2022-07-28 09:55:01 +02007240 /* evaluate for the single item */
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007241 LY_CHECK_RET(moveto_op_comp_item(set2, i, set1, op, 1, result));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007242
7243 /* lazy evaluation until true */
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007244 if (*result) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007245 return LY_SUCCESS;
7246 }
7247 }
7248 }
7249
Michal Vasko8abcecc2022-07-28 09:55:01 +02007250 /* false for all the nodes */
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007251 *result = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007252 return LY_SUCCESS;
7253 }
7254
7255 /* first convert properly */
7256 if ((op[0] == '=') || (op[0] == '!')) {
7257 if ((set1->type == LYXP_SET_BOOLEAN) || (set2->type == LYXP_SET_BOOLEAN)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007258 lyxp_set_cast(set1, LYXP_SET_BOOLEAN);
7259 lyxp_set_cast(set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007260 } else if ((set1->type == LYXP_SET_NUMBER) || (set2->type == LYXP_SET_NUMBER)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007261 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007262 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007263 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007264 LY_CHECK_RET(rc);
7265 } /* else we have 2 strings */
7266 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007267 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007268 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007269 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007270 LY_CHECK_RET(rc);
7271 }
7272
7273 assert(set1->type == set2->type);
7274
7275 /* compute result */
7276 if (op[0] == '=') {
7277 if (set1->type == LYXP_SET_BOOLEAN) {
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007278 *result = (set1->val.bln == set2->val.bln);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007279 } else if (set1->type == LYXP_SET_NUMBER) {
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007280 *result = (set1->val.num == set2->val.num);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007281 } else {
7282 assert(set1->type == LYXP_SET_STRING);
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007283 *result = strcmp(set1->val.str, set2->val.str) ? 0 : 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007284 }
7285 } else if (op[0] == '!') {
7286 if (set1->type == LYXP_SET_BOOLEAN) {
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007287 *result = (set1->val.bln != set2->val.bln);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007288 } else if (set1->type == LYXP_SET_NUMBER) {
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007289 *result = (set1->val.num != set2->val.num);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007290 } else {
7291 assert(set1->type == LYXP_SET_STRING);
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007292 *result = strcmp(set1->val.str, set2->val.str) ? 1 : 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007293 }
7294 } else {
7295 assert(set1->type == LYXP_SET_NUMBER);
7296 if (op[0] == '<') {
7297 if (op[1] == '=') {
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007298 *result = (set1->val.num <= set2->val.num);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007299 } else {
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007300 *result = (set1->val.num < set2->val.num);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007301 }
7302 } else {
7303 if (op[1] == '=') {
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007304 *result = (set1->val.num >= set2->val.num);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007305 } else {
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007306 *result = (set1->val.num > set2->val.num);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007307 }
7308 }
7309 }
7310
Michal Vasko03ff5a72019-09-11 13:49:33 +02007311 return LY_SUCCESS;
7312}
7313
7314/**
7315 * @brief Move context @p set to the result of a basic operation. Handles '+', '-', unary '-', '*', 'div',
7316 * or 'mod'. Result is LYXP_SET_NUMBER. Indirectly context position aware.
7317 *
7318 * @param[in,out] set1 Set to use for the result.
7319 * @param[in] set2 Set acting as the second operand for @p op.
7320 * @param[in] op Operator to process.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007321 * @return LY_ERR
7322 */
7323static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007324moveto_op_math(struct lyxp_set *set1, struct lyxp_set *set2, const char *op)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007325{
7326 LY_ERR rc;
7327
7328 /* unary '-' */
7329 if (!set2 && (op[0] == '-')) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007330 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007331 LY_CHECK_RET(rc);
7332 set1->val.num *= -1;
7333 lyxp_set_free(set2);
7334 return LY_SUCCESS;
7335 }
7336
7337 assert(set1 && set2);
7338
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007339 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007340 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007341 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007342 LY_CHECK_RET(rc);
7343
7344 switch (op[0]) {
7345 /* '+' */
7346 case '+':
7347 set1->val.num += set2->val.num;
7348 break;
7349
7350 /* '-' */
7351 case '-':
7352 set1->val.num -= set2->val.num;
7353 break;
7354
7355 /* '*' */
7356 case '*':
7357 set1->val.num *= set2->val.num;
7358 break;
7359
7360 /* 'div' */
7361 case 'd':
7362 set1->val.num /= set2->val.num;
7363 break;
7364
7365 /* 'mod' */
7366 case 'm':
7367 set1->val.num = ((long long)set1->val.num) % ((long long)set2->val.num);
7368 break;
7369
7370 default:
7371 LOGINT_RET(set1->ctx);
7372 }
7373
7374 return LY_SUCCESS;
7375}
7376
Michal Vasko03ff5a72019-09-11 13:49:33 +02007377/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02007378 * @brief Evaluate Predicate. Logs directly on error.
7379 *
Michal Vaskod3678892020-05-21 10:06:58 +02007380 * [9] Predicate ::= '[' Expr ']'
Michal Vasko03ff5a72019-09-11 13:49:33 +02007381 *
7382 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007383 * @param[in] tok_idx Position in the expression @p exp.
aPiecek8b0cc152021-05-31 16:40:31 +02007384 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007385 * @param[in] options XPath options.
Michal Vasko49fec8e2022-05-24 10:28:33 +02007386 * @param[in] axis Axis to search on.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007387 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7388 */
7389static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02007390eval_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 +02007391{
7392 LY_ERR rc;
Michal Vaskodd528af2022-08-08 14:35:07 +02007393 uint32_t i, orig_exp, orig_pos, orig_size;
Michal Vasko5c4e5892019-11-14 12:31:38 +01007394 int32_t pred_in_ctx;
Michal Vasko88a9e802022-05-24 10:50:28 +02007395 ly_bool reverse_axis = 0;
aPiecekfff4dca2021-10-07 10:59:53 +02007396 struct lyxp_set set2 = {0};
Michal Vasko03ff5a72019-09-11 13:49:33 +02007397
7398 /* '[' */
aPiecek8b0cc152021-05-31 16:40:31 +02007399 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02007400 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007401 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007402
aPiecek8b0cc152021-05-31 16:40:31 +02007403 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007404only_parse:
aPiecek8b0cc152021-05-31 16:40:31 +02007405 rc = eval_expr_select(exp, tok_idx, 0, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007406 LY_CHECK_RET(rc);
7407 } else if (set->type == LYXP_SET_NODE_SET) {
7408 /* 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 +01007409 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007410
7411 /* empty set, nothing to evaluate */
7412 if (!set->used) {
7413 goto only_parse;
7414 }
7415
Michal Vasko49fec8e2022-05-24 10:28:33 +02007416 /* decide forward or reverse axis */
7417 switch (axis) {
7418 case LYXP_AXIS_ANCESTOR:
7419 case LYXP_AXIS_ANCESTOR_OR_SELF:
7420 case LYXP_AXIS_PRECEDING:
7421 case LYXP_AXIS_PRECEDING_SIBLING:
7422 reverse_axis = 1;
7423 break;
7424 case LYXP_AXIS_DESCENDANT:
7425 case LYXP_AXIS_DESCENDANT_OR_SELF:
7426 case LYXP_AXIS_FOLLOWING:
7427 case LYXP_AXIS_FOLLOWING_SIBLING:
7428 case LYXP_AXIS_PARENT:
7429 case LYXP_AXIS_CHILD:
7430 case LYXP_AXIS_SELF:
7431 case LYXP_AXIS_ATTRIBUTE:
7432 reverse_axis = 0;
7433 break;
7434 }
7435
Michal Vasko004d3152020-06-11 19:59:22 +02007436 orig_exp = *tok_idx;
Michal Vasko49fec8e2022-05-24 10:28:33 +02007437 orig_pos = reverse_axis ? set->used + 1 : 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007438 orig_size = set->used;
Michal Vasko39dbf352020-05-21 10:08:59 +02007439 for (i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007440 set_init(&set2, set);
7441 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 +02007442
7443 /* remember the node context position for position() and context size for last() */
7444 orig_pos += reverse_axis ? -1 : 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007445
7446 set2.ctx_pos = orig_pos;
7447 set2.ctx_size = orig_size;
Michal Vasko004d3152020-06-11 19:59:22 +02007448 *tok_idx = orig_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007449
Michal Vasko004d3152020-06-11 19:59:22 +02007450 rc = eval_expr_select(exp, tok_idx, 0, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007451 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02007452 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007453 return rc;
7454 }
7455
Michal Vasko49fec8e2022-05-24 10:28:33 +02007456 /* number is a proximity position */
Michal Vasko03ff5a72019-09-11 13:49:33 +02007457 if (set2.type == LYXP_SET_NUMBER) {
7458 if ((long long)set2.val.num == orig_pos) {
7459 set2.val.num = 1;
7460 } else {
7461 set2.val.num = 0;
7462 }
7463 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007464 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007465
7466 /* predicate satisfied or not? */
Michal Vasko004d3152020-06-11 19:59:22 +02007467 if (!set2.val.bln) {
Michal Vasko2caefc12019-11-14 16:07:56 +01007468 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007469 }
7470 }
Michal Vasko2caefc12019-11-14 16:07:56 +01007471 set_remove_nodes_none(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007472
7473 } else if (set->type == LYXP_SET_SCNODE_SET) {
7474 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01007475 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007476 /* there is a currently-valid node */
7477 break;
7478 }
7479 }
7480 /* empty set, nothing to evaluate */
7481 if (i == set->used) {
7482 goto only_parse;
7483 }
7484
Michal Vasko004d3152020-06-11 19:59:22 +02007485 orig_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007486
Michal Vasko03ff5a72019-09-11 13:49:33 +02007487 /* set special in_ctx to all the valid snodes */
7488 pred_in_ctx = set_scnode_new_in_ctx(set);
7489
7490 /* use the valid snodes one-by-one */
7491 for (i = 0; i < set->used; ++i) {
7492 if (set->val.scnodes[i].in_ctx != pred_in_ctx) {
7493 continue;
7494 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01007495 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007496
Michal Vasko004d3152020-06-11 19:59:22 +02007497 *tok_idx = orig_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007498
Michal Vasko004d3152020-06-11 19:59:22 +02007499 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007500 LY_CHECK_RET(rc);
7501
7502 set->val.scnodes[i].in_ctx = pred_in_ctx;
7503 }
7504
7505 /* restore the state as it was before the predicate */
7506 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01007507 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007508 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007509 } else if (set->val.scnodes[i].in_ctx == pred_in_ctx) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01007510 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007511 }
7512 }
7513
7514 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02007515 set2.type = LYXP_SET_NODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007516 set_fill_set(&set2, set);
7517
Michal Vasko004d3152020-06-11 19:59:22 +02007518 rc = eval_expr_select(exp, tok_idx, 0, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007519 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02007520 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007521 return rc;
7522 }
7523
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007524 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +02007525 if (!set2.val.bln) {
Michal Vaskod3678892020-05-21 10:06:58 +02007526 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007527 }
Michal Vaskod3678892020-05-21 10:06:58 +02007528 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007529 }
7530
7531 /* ']' */
Michal Vasko004d3152020-06-11 19:59:22 +02007532 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_BRACK2);
aPiecek8b0cc152021-05-31 16:40:31 +02007533 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02007534 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007535 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007536
7537 return LY_SUCCESS;
7538}
7539
7540/**
Michal Vaskod3678892020-05-21 10:06:58 +02007541 * @brief Evaluate Literal. Logs directly on error.
7542 *
7543 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007544 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02007545 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7546 */
7547static void
Michal Vaskodd528af2022-08-08 14:35:07 +02007548eval_literal(const struct lyxp_expr *exp, uint32_t *tok_idx, struct lyxp_set *set)
Michal Vaskod3678892020-05-21 10:06:58 +02007549{
7550 if (set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007551 if (exp->tok_len[*tok_idx] == 2) {
Michal Vaskod3678892020-05-21 10:06:58 +02007552 set_fill_string(set, "", 0);
7553 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007554 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 +02007555 }
7556 }
7557 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02007558 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007559 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007560}
7561
7562/**
Michal Vasko3d969ff2022-07-29 15:02:08 +02007563 * @brief Check that a nametest in a predicate matches a key node.
7564 *
7565 * @param[in] nametest Nametest to check.
7566 * @param[in] len Length of @p nametest.
7567 * @param[in] ctx_scnode Found schema node as the context for the predicate.
7568 * @param[in] set Context set.
7569 * @param[in] key Expected key node.
7570 * @return LY_SUCCESS on success,
7571 * @return LY_ENOT if a predicate could not be compiled.
7572 * @return LY_ERR on any error.
7573 */
7574static LY_ERR
7575eval_name_test_try_compile_predicate_key(const char *nametest, uint32_t len, const struct lysc_node *ctx_scnode,
7576 const struct lyxp_set *set, const struct lysc_node *key)
7577{
7578 const struct lys_module *mod;
7579
7580 /* prefix (module) */
7581 LY_CHECK_RET(moveto_resolve_model(&nametest, &len, set, ctx_scnode, &mod));
7582 if (mod != key->module) {
7583 return LY_ENOT;
7584 }
7585
7586 /* node name */
7587 if (ly_strncmp(key->name, nametest, len)) {
7588 return LY_ENOT;
7589 }
7590
7591 return LY_SUCCESS;
7592}
7593
7594/**
7595 * @brief Append a simple predicate for the node.
7596 *
7597 * @param[in] exp Full parsed XPath expression.
7598 * @param[in] tok_idx Predicate start index in @p exp.
7599 * @param[in] end_tok_idx Predicate end index in @p exp.
7600 * @param[in] ctx_scnode Found schema node as the context for the predicate.
7601 * @param[in] set Context set.
7602 * @param[in] pred_node Node with the value referenced in the predicate.
7603 * @param[in,out] pred Predicate to append to.
7604 * @param[in,out] pred_len Length of @p pred, is updated.
7605 * @return LY_SUCCESS on success,
7606 * @return LY_ENOT if a predicate could not be compiled.
7607 * @return LY_ERR on any error.
7608 */
7609static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02007610eval_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 +02007611 const struct lysc_node *ctx_scnode, const struct lyxp_set *set, const struct lysc_node *pred_node, char **pred,
7612 uint32_t *pred_len)
7613{
7614 LY_ERR rc = LY_SUCCESS;
7615 uint32_t i;
7616 const struct lyd_node *siblings;
7617 struct lyd_node *ctx_node;
Michal Vasko5fb92a22022-08-02 09:21:37 +02007618 const struct lysc_node *sparent, *cur_scnode;
Michal Vasko3d969ff2022-07-29 15:02:08 +02007619 struct lyxp_expr *val_exp = NULL;
7620 struct lyxp_set set2 = {0};
7621 char quot;
7622
7623 /* duplicate the value expression */
7624 LY_CHECK_GOTO(rc = lyxp_expr_dup(set->ctx, exp, tok_idx, end_tok_idx, &val_exp), cleanup);
7625
7626 /* get its atoms */
Michal Vasko5fb92a22022-08-02 09:21:37 +02007627 cur_scnode = set->cur_node ? set->cur_node->schema : NULL;
7628 LY_CHECK_GOTO(rc = lyxp_atomize(set->ctx, val_exp, set->cur_mod, set->format, set->prefix_data, cur_scnode,
7629 ctx_scnode, &set2, LYXP_SCNODE), cleanup);
Michal Vasko3d969ff2022-07-29 15:02:08 +02007630
7631 /* check whether we can compile a single predicate (evaluation result value is always the same) */
7632 for (i = 0; i < set2.used; ++i) {
7633 if ((set2.val.scnodes[i].type != LYXP_NODE_ELEM) || (set2.val.scnodes[i].in_ctx < LYXP_SET_SCNODE_ATOM_NODE)) {
7634 /* skip root and context node */
7635 continue;
7636 }
7637
Michal Vasko5fb92a22022-08-02 09:21:37 +02007638 /* 1) context node descendants are traversed - do best-effort detection of the value dependency on the
7639 * context node instance */
7640 if ((set2.val.scnodes[i].axis == LYXP_AXIS_CHILD) && (set2.val.scnodes[i].scnode->parent == ctx_scnode)) {
7641 /* 1.1) context node child was accessed on the child axis, certain dependency */
Michal Vasko3d969ff2022-07-29 15:02:08 +02007642 rc = LY_ENOT;
7643 goto cleanup;
7644 }
Michal Vasko5fb92a22022-08-02 09:21:37 +02007645 if ((set2.val.scnodes[i].axis == LYXP_AXIS_DESCENDANT) || (set2.val.scnodes[i].axis == LYXP_AXIS_DESCENDANT_OR_SELF)) {
7646 for (sparent = set2.val.scnodes[i].scnode->parent; sparent && (sparent != ctx_scnode); sparent = sparent->parent) {}
7647 if (sparent) {
7648 /* 1.2) context node descendant was accessed on the descendant axis, probable dependency */
7649 rc = LY_ENOT;
7650 goto cleanup;
7651 }
7652 }
Michal Vasko3d969ff2022-07-29 15:02:08 +02007653
Michal Vasko5fb92a22022-08-02 09:21:37 +02007654 /* 2) multi-instance nodes (list or leaf-list) are traversed - all the instances need to be considered,
7655 * but the current node can be safely ignored, it is always the same data instance */
7656 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 +02007657 rc = LY_ENOT;
7658 goto cleanup;
7659 }
Michal Vasko3d969ff2022-07-29 15:02:08 +02007660 }
7661
7662 /* get any data instance of the context node, we checked it makes no difference */
7663 siblings = set->val.nodes[0].node ? lyd_child(set->val.nodes[0].node) : set->tree;
7664 LY_CHECK_GOTO(rc = lyd_find_sibling_schema(siblings, ctx_scnode, &ctx_node), cleanup);
7665
7666 /* evaluate the value subexpression with the root context node */
7667 lyxp_set_free_content(&set2);
7668 LY_CHECK_GOTO(rc = lyxp_eval(set->ctx, val_exp, set->cur_mod, set->format, set->prefix_data, set->cur_node,
7669 ctx_node, set->tree, NULL, &set2, 0), cleanup);
7670
7671 /* cast it into a string */
7672 LY_CHECK_GOTO(rc = lyxp_set_cast(&set2, LYXP_SET_STRING), cleanup);
7673
7674 /* append the JSON predicate */
7675 *pred = ly_realloc(*pred, *pred_len + 1 + strlen(pred_node->name) + 2 + strlen(set2.val.str) + 3);
7676 LY_CHECK_ERR_GOTO(!*pred, LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
7677 quot = strchr(set2.val.str, '\'') ? '\"' : '\'';
7678 *pred_len += sprintf(*pred + *pred_len, "[%s=%c%s%c]", pred_node->name, quot, set2.val.str, quot);
7679
7680cleanup:
7681 lyxp_expr_free(set->ctx, val_exp);
7682 lyxp_set_free_content(&set2);
7683 return rc;
7684}
7685
7686/**
Michal Vasko004d3152020-06-11 19:59:22 +02007687 * @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 +02007688 *
Michal Vasko004d3152020-06-11 19:59:22 +02007689 * @param[in] exp Full parsed XPath expression.
7690 * @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 +02007691 * @param[in] ctx_scnode Found schema node as the context for the predicate.
7692 * @param[in] set Context set.
Michal Vasko004d3152020-06-11 19:59:22 +02007693 * @param[out] predicates Parsed predicates.
7694 * @param[out] pred_type Type of @p predicates.
7695 * @return LY_SUCCESS on success,
Michal Vasko3d969ff2022-07-29 15:02:08 +02007696 * @return LY_ENOT if a predicate could not be compiled.
Michal Vasko004d3152020-06-11 19:59:22 +02007697 * @return LY_ERR on any error.
Michal Vaskod3678892020-05-21 10:06:58 +02007698 */
7699static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02007700eval_name_test_try_compile_predicates(const struct lyxp_expr *exp, uint32_t *tok_idx, const struct lysc_node *ctx_scnode,
Michal Vasko3d969ff2022-07-29 15:02:08 +02007701 const struct lyxp_set *set, struct ly_path_predicate **predicates, enum ly_path_pred_type *pred_type)
Michal Vaskod3678892020-05-21 10:06:58 +02007702{
Michal Vasko3d969ff2022-07-29 15:02:08 +02007703 LY_ERR rc = LY_SUCCESS;
Michal Vaskodd528af2022-08-08 14:35:07 +02007704 uint32_t e_idx, val_start_idx, pred_idx = 0, prev_lo, pred_len = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02007705 const struct lysc_node *key;
Michal Vasko3d969ff2022-07-29 15:02:08 +02007706 char *pred = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02007707 struct lyxp_expr *exp2 = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02007708
Michal Vasko3d969ff2022-07-29 15:02:08 +02007709 assert(ctx_scnode->nodetype & (LYS_LIST | LYS_LEAFLIST));
Michal Vaskod3678892020-05-21 10:06:58 +02007710
Michal Vasko3d969ff2022-07-29 15:02:08 +02007711 /* turn logging off */
7712 prev_lo = ly_log_options(0);
7713
7714 if (ctx_scnode->nodetype == LYS_LIST) {
7715 /* check for predicates "[key1=...][key2=...]..." */
7716
Michal Vasko004d3152020-06-11 19:59:22 +02007717 /* get key count */
Michal Vasko3d969ff2022-07-29 15:02:08 +02007718 if (ctx_scnode->flags & LYS_KEYLESS) {
7719 rc = LY_ENOT;
7720 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +02007721 }
Michal Vaskod3678892020-05-21 10:06:58 +02007722
Michal Vasko004d3152020-06-11 19:59:22 +02007723 /* learn where the predicates end */
7724 e_idx = *tok_idx;
Michal Vasko3d969ff2022-07-29 15:02:08 +02007725 for (key = lysc_node_child(ctx_scnode); key && (key->flags & LYS_KEY); key = key->next) {
Michal Vasko004d3152020-06-11 19:59:22 +02007726 /* '[' */
7727 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
Michal Vasko3d969ff2022-07-29 15:02:08 +02007728 rc = LY_ENOT;
7729 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +02007730 }
7731 ++e_idx;
7732
Michal Vasko3354d272021-04-06 09:40:06 +02007733 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_NAMETEST)) {
Michal Vasko3d969ff2022-07-29 15:02:08 +02007734 /* not a key */
7735 rc = LY_ENOT;
7736 goto cleanup;
Michal Vasko3354d272021-04-06 09:40:06 +02007737 }
7738
Michal Vasko3d969ff2022-07-29 15:02:08 +02007739 /* check key */
7740 LY_CHECK_GOTO(rc = eval_name_test_try_compile_predicate_key(exp->expr + exp->tok_pos[e_idx],
7741 exp->tok_len[e_idx], ctx_scnode, set, key), cleanup);
7742
7743 ++e_idx;
7744
7745 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_OPER_EQUAL)) {
7746 /* not '=' */
7747 rc = LY_ENOT;
7748 goto cleanup;
7749 }
7750 ++e_idx;
7751
7752 /* value start */
7753 val_start_idx = e_idx;
7754
Michal Vasko004d3152020-06-11 19:59:22 +02007755 /* ']' */
7756 while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) {
Michal Vaskob8ee3562022-08-02 10:43:17 +02007757 if (!lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_OPER_LOG)) {
7758 /* higher priority than '=' */
7759 rc = LY_ENOT;
7760 goto cleanup;
7761 }
Michal Vasko004d3152020-06-11 19:59:22 +02007762 ++e_idx;
7763 }
Michal Vasko004d3152020-06-11 19:59:22 +02007764
Michal Vasko3d969ff2022-07-29 15:02:08 +02007765 /* try to evaluate the value */
7766 LY_CHECK_GOTO(rc = eval_name_test_try_compile_predicate_append(exp, val_start_idx, e_idx - 1, ctx_scnode,
7767 set, key, &pred, &pred_len), cleanup);
7768
7769 ++e_idx;
Michal Vasko004d3152020-06-11 19:59:22 +02007770 }
Michal Vasko004d3152020-06-11 19:59:22 +02007771 } else {
Michal Vasko3d969ff2022-07-29 15:02:08 +02007772 /* check for predicate "[.=...]" */
7773
Michal Vasko004d3152020-06-11 19:59:22 +02007774 /* learn just where this single predicate ends */
7775 e_idx = *tok_idx;
7776
Michal Vaskod3678892020-05-21 10:06:58 +02007777 /* '[' */
Michal Vasko004d3152020-06-11 19:59:22 +02007778 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
Michal Vasko3d969ff2022-07-29 15:02:08 +02007779 rc = LY_ENOT;
7780 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +02007781 }
7782 ++e_idx;
Michal Vaskod3678892020-05-21 10:06:58 +02007783
Michal Vasko3354d272021-04-06 09:40:06 +02007784 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_DOT)) {
Michal Vasko3d969ff2022-07-29 15:02:08 +02007785 /* not the node value */
7786 rc = LY_ENOT;
7787 goto cleanup;
Michal Vasko3354d272021-04-06 09:40:06 +02007788 }
Michal Vasko3d969ff2022-07-29 15:02:08 +02007789 ++e_idx;
7790
7791 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_OPER_EQUAL)) {
7792 /* not '=' */
7793 rc = LY_ENOT;
7794 goto cleanup;
7795 }
7796 ++e_idx;
7797
7798 /* value start */
7799 val_start_idx = e_idx;
Michal Vasko3354d272021-04-06 09:40:06 +02007800
Michal Vaskod3678892020-05-21 10:06:58 +02007801 /* ']' */
Michal Vasko004d3152020-06-11 19:59:22 +02007802 while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) {
Michal Vaskob8ee3562022-08-02 10:43:17 +02007803 if (!lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_OPER_LOG)) {
7804 /* higher priority than '=' */
7805 rc = LY_ENOT;
7806 goto cleanup;
7807 }
Michal Vasko004d3152020-06-11 19:59:22 +02007808 ++e_idx;
7809 }
Michal Vasko3d969ff2022-07-29 15:02:08 +02007810
7811 /* try to evaluate the value */
7812 LY_CHECK_GOTO(rc = eval_name_test_try_compile_predicate_append(exp, val_start_idx, e_idx - 1, ctx_scnode, set,
7813 ctx_scnode, &pred, &pred_len), cleanup);
7814
Michal Vasko004d3152020-06-11 19:59:22 +02007815 ++e_idx;
Michal Vaskod3678892020-05-21 10:06:58 +02007816 }
7817
Michal Vasko004d3152020-06-11 19:59:22 +02007818 /* parse the predicate(s) */
Michal Vasko3d969ff2022-07-29 15:02:08 +02007819 LY_CHECK_GOTO(rc = ly_path_parse_predicate(set->ctx, ctx_scnode, pred, pred_len, LY_PATH_PREFIX_OPTIONAL,
7820 LY_PATH_PRED_SIMPLE, &exp2), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02007821
7822 /* compile */
Michal Vasko3d969ff2022-07-29 15:02:08 +02007823 rc = ly_path_compile_predicate(set->ctx, set->cur_node ? set->cur_node->schema : NULL, set->cur_mod, ctx_scnode, exp2,
7824 &pred_idx, LY_VALUE_JSON, NULL, predicates, pred_type);
7825 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02007826
7827 /* success, the predicate must include all the needed information for hash-based search */
7828 *tok_idx = e_idx;
7829
7830cleanup:
7831 ly_log_options(prev_lo);
Michal Vasko3d969ff2022-07-29 15:02:08 +02007832 lyxp_expr_free(set->ctx, exp2);
7833 free(pred);
7834 return rc;
Michal Vaskod3678892020-05-21 10:06:58 +02007835}
7836
7837/**
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007838 * @brief Search for/check the next schema node that could be the only matching schema node meaning the
7839 * data node(s) could be found using a single hash-based search.
7840 *
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007841 * @param[in] ctx libyang context.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007842 * @param[in] node Next context node to check.
7843 * @param[in] name Expected node name.
7844 * @param[in] name_len Length of @p name.
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007845 * @param[in] moveto_mod Expected node module, can be NULL for JSON format with no prefix.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007846 * @param[in] root_type XPath root type.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007847 * @param[in] format Prefix format.
7848 * @param[in,out] found Previously found node, is updated.
7849 * @return LY_SUCCESS on success,
7850 * @return LY_ENOT if the whole check failed and hashes cannot be used.
7851 */
7852static LY_ERR
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007853eval_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 +02007854 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 +02007855 const struct lysc_node **found)
7856{
7857 const struct lysc_node *scnode;
7858 const struct lys_module *mod;
7859 uint32_t idx = 0;
7860
Radek Krejci8df109d2021-04-23 12:19:08 +02007861 assert((format == LY_VALUE_JSON) || moveto_mod);
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007862
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007863continue_search:
Michal Vasko7d1d0912020-10-16 08:38:30 +02007864 scnode = NULL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007865 if (!node) {
Radek Krejci8df109d2021-04-23 12:19:08 +02007866 if ((format == LY_VALUE_JSON) && !moveto_mod) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007867 /* search all modules for a single match */
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007868 while ((mod = ly_ctx_get_module_iter(ctx, &idx))) {
Michal Vasko35a3b1d2021-07-14 09:34:16 +02007869 if (!mod->implemented) {
7870 continue;
7871 }
7872
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007873 scnode = lys_find_child(NULL, mod, name, name_len, 0, 0);
7874 if (scnode) {
7875 /* we have found a match */
7876 break;
7877 }
7878 }
7879
Michal Vasko7d1d0912020-10-16 08:38:30 +02007880 if (!scnode) {
7881 /* all modules searched */
7882 idx = 0;
7883 }
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007884 } else {
7885 /* search in top-level */
7886 scnode = lys_find_child(NULL, moveto_mod, name, name_len, 0, 0);
7887 }
7888 } else if (!*found || (lysc_data_parent(*found) != node->schema)) {
Radek Krejci8df109d2021-04-23 12:19:08 +02007889 if ((format == LY_VALUE_JSON) && !moveto_mod) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007890 /* we must adjust the module to inherit the one from the context node */
7891 moveto_mod = node->schema->module;
7892 }
7893
7894 /* search in children, do not repeat the same search */
7895 scnode = lys_find_child(node->schema, moveto_mod, name, name_len, 0, 0);
Michal Vasko7d1d0912020-10-16 08:38:30 +02007896 } /* else skip redundant search */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007897
7898 /* additional context check */
7899 if (scnode && (root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) {
7900 scnode = NULL;
7901 }
7902
7903 if (scnode) {
7904 if (*found) {
7905 /* we found a schema node with the same name but at different level, give up, too complicated
7906 * (more hash-based searches would be required, not supported) */
7907 return LY_ENOT;
7908 } else {
7909 /* remember the found schema node and continue to make sure it can be used */
7910 *found = scnode;
7911 }
7912 }
7913
7914 if (idx) {
7915 /* continue searching all the following models */
7916 goto continue_search;
7917 }
7918
7919 return LY_SUCCESS;
7920}
7921
7922/**
Michal Vasko4ad69e72021-10-26 16:25:55 +02007923 * @brief Generate message when no matching schema nodes were found for a path segment.
7924 *
7925 * @param[in] set XPath set.
7926 * @param[in] scparent Previous schema parent in the context, if only one.
7927 * @param[in] ncname XPath NCName being evaluated.
7928 * @param[in] ncname_len Length of @p ncname.
7929 * @param[in] expr Whole XPath expression.
7930 * @param[in] options XPath options.
7931 */
7932static void
7933eval_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 +02007934 uint32_t ncname_len, const char *expr, uint32_t options)
Michal Vasko4ad69e72021-10-26 16:25:55 +02007935{
7936 const char *format;
7937 char *path = NULL, *ppath = NULL;
7938
7939 path = lysc_path(set->cur_scnode, LYSC_PATH_LOG, NULL, 0);
7940 if (scparent) {
7941 /* generate path for the parent */
7942 if (scparent->type == LYXP_NODE_ELEM) {
7943 ppath = lysc_path(scparent->scnode, LYSC_PATH_LOG, NULL, 0);
7944 } else if (scparent->type == LYXP_NODE_ROOT) {
7945 ppath = strdup("<root>");
7946 } else if (scparent->type == LYXP_NODE_ROOT_CONFIG) {
7947 ppath = strdup("<config-root>");
7948 }
7949 }
7950 if (ppath) {
7951 format = "Schema node \"%.*s\" for parent \"%s\" not found; in expr \"%.*s\" with context node \"%s\".";
7952 if (options & LYXP_SCNODE_ERROR) {
7953 LOGERR(set->ctx, LY_EVALID, format, ncname_len, ncname, ppath, (ncname - expr) + ncname_len, expr, path);
7954 } else {
7955 LOGWRN(set->ctx, format, ncname_len, ncname, ppath, (ncname - expr) + ncname_len, expr, path);
7956 }
7957 } else {
7958 format = "Schema node \"%.*s\" not found; in expr \"%.*s\" with context node \"%s\".";
7959 if (options & LYXP_SCNODE_ERROR) {
7960 LOGERR(set->ctx, LY_EVALID, format, ncname_len, ncname, (ncname - expr) + ncname_len, expr, path);
7961 } else {
7962 LOGWRN(set->ctx, format, ncname_len, ncname, (ncname - expr) + ncname_len, expr, path);
7963 }
7964 }
7965 free(path);
7966 free(ppath);
7967}
7968
7969/**
Michal Vaskod3678892020-05-21 10:06:58 +02007970 * @brief Evaluate NameTest and any following Predicates. Logs directly on error.
7971 *
7972 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
7973 * [6] NodeTest ::= NameTest | NodeType '(' ')'
7974 * [7] NameTest ::= '*' | NCName ':' '*' | QName
7975 *
7976 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007977 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko49fec8e2022-05-24 10:28:33 +02007978 * @param[in] axis What axis to search on.
Michal Vaskod3678892020-05-21 10:06:58 +02007979 * @param[in] all_desc Whether to search all the descendants or children only.
aPiecek8b0cc152021-05-31 16:40:31 +02007980 * @param[in,out] set Context and result set.
Michal Vaskod3678892020-05-21 10:06:58 +02007981 * @param[in] options XPath options.
Michal Vaskoa036a6b2021-10-12 16:05:23 +02007982 * @return LY_ERR (LY_EINCOMPLETE on unresolved when, LY_ENOT for not found schema node)
Michal Vaskod3678892020-05-21 10:06:58 +02007983 */
7984static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02007985eval_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 +02007986 struct lyxp_set *set, uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02007987{
Michal Vaskoa036a6b2021-10-12 16:05:23 +02007988 LY_ERR rc = LY_SUCCESS, r;
Michal Vasko004d3152020-06-11 19:59:22 +02007989 const char *ncname, *ncname_dict = NULL;
Michal Vasko56c008c2022-07-29 14:57:47 +02007990 uint32_t i, ncname_len;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007991 const struct lys_module *moveto_mod = NULL;
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007992 const struct lysc_node *scnode = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02007993 struct ly_path_predicate *predicates = NULL;
7994 enum ly_path_pred_type pred_type = 0;
Michal Vaskoa036a6b2021-10-12 16:05:23 +02007995 int scnode_skip_pred = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02007996
aPiecek8b0cc152021-05-31 16:40:31 +02007997 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02007998 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007999 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02008000
aPiecek8b0cc152021-05-31 16:40:31 +02008001 if (options & LYXP_SKIP_EXPR) {
Michal Vaskod3678892020-05-21 10:06:58 +02008002 goto moveto;
8003 }
8004
Michal Vasko004d3152020-06-11 19:59:22 +02008005 ncname = &exp->expr[exp->tok_pos[*tok_idx - 1]];
8006 ncname_len = exp->tok_len[*tok_idx - 1];
Michal Vaskod3678892020-05-21 10:06:58 +02008007
Michal Vaskoc71c37b2021-01-11 13:40:02 +01008008 if ((ncname[0] == '*') && (ncname_len == 1)) {
8009 /* all nodes will match */
8010 goto moveto;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008011 }
8012
Michal Vaskoc71c37b2021-01-11 13:40:02 +01008013 /* parse (and skip) module name */
8014 rc = moveto_resolve_model(&ncname, &ncname_len, set, NULL, &moveto_mod);
Michal Vaskod3678892020-05-21 10:06:58 +02008015 LY_CHECK_GOTO(rc, cleanup);
8016
Michal Vasko49fec8e2022-05-24 10:28:33 +02008017 if ((ncname[0] == '*') && (ncname_len == 1)) {
8018 /* all nodes from the module will match */
8019 goto moveto;
8020 }
8021
8022 if (((set->format == LY_VALUE_JSON) || moveto_mod) && (axis == LYXP_AXIS_CHILD) && !all_desc &&
8023 (set->type == LYXP_SET_NODE_SET)) {
Michal Vaskod3678892020-05-21 10:06:58 +02008024 /* find the matching schema node in some parent in the context */
Michal Vasko56c008c2022-07-29 14:57:47 +02008025 for (i = 0; i < set->used; ++i) {
Michal Vaskoc71c37b2021-01-11 13:40:02 +01008026 if (eval_name_test_with_predicate_get_scnode(set->ctx, set->val.nodes[i].node, ncname, ncname_len,
8027 moveto_mod, set->root_type, set->format, &scnode)) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008028 /* check failed */
8029 scnode = NULL;
8030 break;
Michal Vaskod3678892020-05-21 10:06:58 +02008031 }
8032 }
8033
Michal Vasko004d3152020-06-11 19:59:22 +02008034 if (scnode && (scnode->nodetype & (LYS_LIST | LYS_LEAFLIST))) {
8035 /* try to create the predicates */
Michal Vasko3d969ff2022-07-29 15:02:08 +02008036 if (eval_name_test_try_compile_predicates(exp, tok_idx, scnode, set, &predicates, &pred_type)) {
Michal Vasko004d3152020-06-11 19:59:22 +02008037 /* hashes cannot be used */
Michal Vaskod3678892020-05-21 10:06:58 +02008038 scnode = NULL;
8039 }
8040 }
8041 }
8042
Michal Vaskoc71c37b2021-01-11 13:40:02 +01008043 if (!scnode) {
8044 /* we are not able to match based on a schema node and not all the modules match ("*"),
Michal Vasko004d3152020-06-11 19:59:22 +02008045 * use dictionary for efficient comparison */
Radek Krejci011e4aa2020-09-04 15:22:31 +02008046 LY_CHECK_GOTO(rc = lydict_insert(set->ctx, ncname, ncname_len, &ncname_dict), cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02008047 }
8048
8049moveto:
8050 /* move to the attribute(s), data node(s), or schema node(s) */
Michal Vasko49fec8e2022-05-24 10:28:33 +02008051 if (axis == LYXP_AXIS_ATTRIBUTE) {
aPiecek8b0cc152021-05-31 16:40:31 +02008052 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008053 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskod3678892020-05-21 10:06:58 +02008054 } else {
8055 if (all_desc) {
Michal Vaskocdad7122020-11-09 21:04:44 +01008056 rc = moveto_attr_alldesc(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02008057 } else {
aPiecek8b0cc152021-05-31 16:40:31 +02008058 rc = moveto_attr(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02008059 }
8060 LY_CHECK_GOTO(rc, cleanup);
8061 }
8062 } else {
aPiecek8b0cc152021-05-31 16:40:31 +02008063 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008064 const struct lyxp_set_scnode *scparent = NULL;
Michal Vasko56c008c2022-07-29 14:57:47 +02008065 ly_bool found = 0;
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008066
8067 /* remember parent if there is only one, to print in the warning */
8068 for (i = 0; i < set->used; ++i) {
8069 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
8070 if (!scparent) {
8071 /* remember the context node */
8072 scparent = &set->val.scnodes[i];
8073 } else {
8074 /* several context nodes, no reasonable error possible */
8075 scparent = NULL;
8076 break;
8077 }
8078 }
8079 }
Radek Krejci1deb5be2020-08-26 16:43:36 +02008080
Michal Vasko49fec8e2022-05-24 10:28:33 +02008081 if (all_desc && (axis == LYXP_AXIS_CHILD)) {
8082 /* efficient evaluation that does not add all the descendants into the set */
8083 rc = moveto_scnode_alldesc_child(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02008084 } else {
Michal Vasko49fec8e2022-05-24 10:28:33 +02008085 if (all_desc) {
8086 /* "//" == "/descendant-or-self::node()/" */
8087 rc = xpath_pi_node(set, LYXP_AXIS_DESCENDANT_OR_SELF, options);
8088 LY_CHECK_GOTO(rc, cleanup);
8089 }
8090 rc = moveto_scnode(set, moveto_mod, ncname_dict, axis, options);
Michal Vaskod3678892020-05-21 10:06:58 +02008091 }
8092 LY_CHECK_GOTO(rc, cleanup);
8093
Michal Vasko56c008c2022-07-29 14:57:47 +02008094 i = set->used;
8095 do {
8096 --i;
Michal Vasko1a09b212021-05-06 13:00:10 +02008097 if (set->val.scnodes[i].in_ctx > LYXP_SET_SCNODE_ATOM_NODE) {
Michal Vasko56c008c2022-07-29 14:57:47 +02008098 found = 1;
Michal Vaskod3678892020-05-21 10:06:58 +02008099 break;
8100 }
Michal Vasko56c008c2022-07-29 14:57:47 +02008101 } while (i);
8102 if (!found) {
Michal Vasko4ad69e72021-10-26 16:25:55 +02008103 /* generate message */
8104 eval_name_test_scnode_no_match_msg(set, scparent, ncname, ncname_len, exp->expr, options);
8105
8106 if (options & LYXP_SCNODE_ERROR) {
8107 /* error */
8108 rc = LY_EVALID;
8109 goto cleanup;
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008110 }
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008111
8112 /* skip the predicates and the rest of this path to not generate invalid warnings */
8113 rc = LY_ENOT;
8114 scnode_skip_pred = 1;
Michal Vaskod3678892020-05-21 10:06:58 +02008115 }
8116 } else {
Michal Vasko49fec8e2022-05-24 10:28:33 +02008117 if (all_desc && (axis == LYXP_AXIS_CHILD)) {
8118 /* efficient evaluation */
8119 rc = moveto_node_alldesc_child(set, moveto_mod, ncname_dict, options);
8120 } else if (scnode && (axis == LYXP_AXIS_CHILD)) {
8121 /* we can find the child nodes using hashes */
8122 rc = moveto_node_hash_child(set, scnode, predicates, options);
Michal Vaskod3678892020-05-21 10:06:58 +02008123 } else {
Michal Vasko49fec8e2022-05-24 10:28:33 +02008124 if (all_desc) {
8125 /* "//" == "/descendant-or-self::node()/" */
8126 rc = xpath_pi_node(set, LYXP_AXIS_DESCENDANT_OR_SELF, options);
8127 LY_CHECK_GOTO(rc, cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02008128 }
Michal Vasko49fec8e2022-05-24 10:28:33 +02008129 rc = moveto_node(set, moveto_mod, ncname_dict, axis, options);
Michal Vaskod3678892020-05-21 10:06:58 +02008130 }
8131 LY_CHECK_GOTO(rc, cleanup);
8132 }
8133 }
8134
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008135 if (scnode_skip_pred) {
8136 /* skip predicates */
8137 options |= LYXP_SKIP_EXPR;
8138 }
8139
Michal Vaskod3678892020-05-21 10:06:58 +02008140 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02008141 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02008142 r = eval_predicate(exp, tok_idx, set, options, axis);
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008143 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02008144 }
8145
8146cleanup:
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008147 if (scnode_skip_pred) {
8148 /* restore options */
8149 options &= ~LYXP_SKIP_EXPR;
8150 }
aPiecek8b0cc152021-05-31 16:40:31 +02008151 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko004d3152020-06-11 19:59:22 +02008152 lydict_remove(set->ctx, ncname_dict);
Michal Vaskof7e16e22020-10-21 09:24:39 +02008153 ly_path_predicates_free(set->ctx, pred_type, predicates);
Michal Vaskodb51a8d2020-05-27 15:22:29 +02008154 }
Michal Vaskod3678892020-05-21 10:06:58 +02008155 return rc;
8156}
8157
8158/**
8159 * @brief Evaluate NodeType and any following Predicates. Logs directly on error.
8160 *
8161 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
8162 * [6] NodeTest ::= NameTest | NodeType '(' ')'
8163 * [8] NodeType ::= 'text' | 'node'
8164 *
8165 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008166 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko49fec8e2022-05-24 10:28:33 +02008167 * @param[in] axis Axis to search on.
8168 * @param[in] all_desc Whether to search all the descendants or axis only.
aPiecek8b0cc152021-05-31 16:40:31 +02008169 * @param[in,out] set Context and result set.
Michal Vaskod3678892020-05-21 10:06:58 +02008170 * @param[in] options XPath options.
8171 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8172 */
8173static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02008174eval_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 +02008175 struct lyxp_set *set, uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02008176{
8177 LY_ERR rc;
8178
Michal Vaskod3678892020-05-21 10:06:58 +02008179 (void)all_desc;
8180
aPiecek8b0cc152021-05-31 16:40:31 +02008181 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko004d3152020-06-11 19:59:22 +02008182 assert(exp->tok_len[*tok_idx] == 4);
Michal Vasko49fec8e2022-05-24 10:28:33 +02008183 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "node", 4)) {
8184 rc = xpath_pi_node(set, axis, options);
Michal Vaskod3678892020-05-21 10:06:58 +02008185 } else {
Michal Vasko49fec8e2022-05-24 10:28:33 +02008186 assert(!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "text", 4));
8187 rc = xpath_pi_text(set, axis, options);
Michal Vaskod3678892020-05-21 10:06:58 +02008188 }
Michal Vasko49fec8e2022-05-24 10:28:33 +02008189 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02008190 }
aPiecek8b0cc152021-05-31 16:40:31 +02008191 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008192 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008193 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02008194
8195 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02008196 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
aPiecek8b0cc152021-05-31 16:40:31 +02008197 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008198 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008199 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02008200
8201 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02008202 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
aPiecek8b0cc152021-05-31 16:40:31 +02008203 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008204 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008205 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02008206
8207 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02008208 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02008209 rc = eval_predicate(exp, tok_idx, set, options, axis);
Michal Vaskod3678892020-05-21 10:06:58 +02008210 LY_CHECK_RET(rc);
8211 }
8212
8213 return LY_SUCCESS;
8214}
8215
8216/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02008217 * @brief Evaluate RelativeLocationPath. Logs directly on error.
8218 *
8219 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
8220 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
Michal Vaskod3678892020-05-21 10:06:58 +02008221 * [6] NodeTest ::= NameTest | NodeType '(' ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02008222 *
8223 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008224 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008225 * @param[in] all_desc Whether to search all the descendants or children only.
aPiecek8b0cc152021-05-31 16:40:31 +02008226 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008227 * @param[in] options XPath options.
8228 * @return LY_ERR (YL_EINCOMPLETE on unresolved when)
8229 */
8230static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02008231eval_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 +02008232 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008233{
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008234 LY_ERR rc = LY_SUCCESS;
Michal Vasko49fec8e2022-05-24 10:28:33 +02008235 enum lyxp_axis axis;
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008236 int scnode_skip_path = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008237
8238 goto step;
8239 do {
8240 /* evaluate '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02008241 if (exp->tok_len[*tok_idx] == 1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008242 all_desc = 0;
8243 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02008244 assert(exp->tok_len[*tok_idx] == 2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008245 all_desc = 1;
8246 }
aPiecek8b0cc152021-05-31 16:40:31 +02008247 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008248 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008249 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008250
8251step:
Michal Vasko49fec8e2022-05-24 10:28:33 +02008252 /* AxisSpecifier */
8253 if (exp->tokens[*tok_idx] == LYXP_TOKEN_AXISNAME) {
8254 axis = str2axis(exp->expr + exp->tok_pos[*tok_idx], exp->tok_len[*tok_idx]);
Michal Vaskod3678892020-05-21 10:06:58 +02008255
aPiecek8b0cc152021-05-31 16:40:31 +02008256 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008257 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
8258 ++(*tok_idx);
8259
8260 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_DCOLON);
8261 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
8262 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
8263 ++(*tok_idx);
8264 } else if (exp->tokens[*tok_idx] == LYXP_TOKEN_AT) {
8265 axis = LYXP_AXIS_ATTRIBUTE;
8266
8267 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
8268 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008269 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02008270 } else {
Michal Vasko49fec8e2022-05-24 10:28:33 +02008271 /* default */
8272 axis = LYXP_AXIS_CHILD;
Michal Vaskod3678892020-05-21 10:06:58 +02008273 }
8274
Michal Vasko49fec8e2022-05-24 10:28:33 +02008275 /* NodeTest Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02008276 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008277 case LYXP_TOKEN_DOT:
8278 /* evaluate '.' */
Michal Vasko49fec8e2022-05-24 10:28:33 +02008279 if (!(options & LYXP_SKIP_EXPR)) {
8280 if (((options & LYXP_SCNODE_ALL) && (set->type != LYXP_SET_SCNODE_SET)) ||
8281 (!(options & LYXP_SCNODE_ALL) && (set->type != LYXP_SET_NODE_SET))) {
8282 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
8283 rc = LY_EVALID;
8284 goto cleanup;
8285 }
8286
8287 if (all_desc) {
8288 rc = xpath_pi_node(set, LYXP_AXIS_DESCENDANT_OR_SELF, options);
8289 LY_CHECK_GOTO(rc, cleanup);
8290 }
8291 rc = xpath_pi_node(set, LYXP_AXIS_SELF, options);
8292 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008293 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008294 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008295 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008296 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008297 break;
8298
8299 case LYXP_TOKEN_DDOT:
8300 /* evaluate '..' */
Michal Vasko49fec8e2022-05-24 10:28:33 +02008301 if (!(options & LYXP_SKIP_EXPR)) {
8302 if (((options & LYXP_SCNODE_ALL) && (set->type != LYXP_SET_SCNODE_SET)) ||
8303 (!(options & LYXP_SCNODE_ALL) && (set->type != LYXP_SET_NODE_SET))) {
8304 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
8305 rc = LY_EVALID;
8306 goto cleanup;
8307 }
8308
8309 if (all_desc) {
8310 rc = xpath_pi_node(set, LYXP_AXIS_DESCENDANT_OR_SELF, options);
8311 LY_CHECK_GOTO(rc, cleanup);
8312 }
8313 rc = xpath_pi_node(set, LYXP_AXIS_PARENT, options);
8314 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008315 }
aPiecek8b0cc152021-05-31 16:40:31 +02008316 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008317 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008318 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008319 break;
8320
Michal Vasko03ff5a72019-09-11 13:49:33 +02008321 case LYXP_TOKEN_NAMETEST:
Michal Vaskod3678892020-05-21 10:06:58 +02008322 /* evaluate NameTest Predicate* */
Michal Vasko49fec8e2022-05-24 10:28:33 +02008323 rc = eval_name_test_with_predicate(exp, tok_idx, axis, all_desc, set, options);
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008324 if (rc == LY_ENOT) {
8325 assert(options & LYXP_SCNODE_ALL);
8326 /* skip the rest of this path */
8327 rc = LY_SUCCESS;
8328 scnode_skip_path = 1;
8329 options |= LYXP_SKIP_EXPR;
8330 }
8331 LY_CHECK_GOTO(rc, cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02008332 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008333
Michal Vaskod3678892020-05-21 10:06:58 +02008334 case LYXP_TOKEN_NODETYPE:
8335 /* evaluate NodeType Predicate* */
Michal Vasko49fec8e2022-05-24 10:28:33 +02008336 rc = eval_node_type_with_predicate(exp, tok_idx, axis, all_desc, set, options);
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008337 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008338 break;
8339
8340 default:
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008341 LOGINT(set->ctx);
8342 rc = LY_EINT;
8343 goto cleanup;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008344 }
Michal Vasko004d3152020-06-11 19:59:22 +02008345 } while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008346
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008347cleanup:
8348 if (scnode_skip_path) {
8349 options &= ~LYXP_SKIP_EXPR;
8350 }
8351 return rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008352}
8353
8354/**
8355 * @brief Evaluate AbsoluteLocationPath. Logs directly on error.
8356 *
8357 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
8358 *
8359 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008360 * @param[in] tok_idx Position in the expression @p exp.
aPiecek8b0cc152021-05-31 16:40:31 +02008361 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008362 * @param[in] options XPath options.
8363 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8364 */
8365static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02008366eval_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 +02008367{
Radek Krejci857189e2020-09-01 13:26:36 +02008368 ly_bool all_desc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008369
aPiecek8b0cc152021-05-31 16:40:31 +02008370 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008371 /* no matter what tokens follow, we need to be at the root */
Michal Vaskob0099a92020-08-31 14:55:23 +02008372 LY_CHECK_RET(moveto_root(set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008373 }
8374
8375 /* '/' RelativeLocationPath? */
Michal Vasko004d3152020-06-11 19:59:22 +02008376 if (exp->tok_len[*tok_idx] == 1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008377 /* evaluate '/' - deferred */
8378 all_desc = 0;
aPiecek8b0cc152021-05-31 16:40:31 +02008379 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008380 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008381 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008382
Michal Vasko004d3152020-06-11 19:59:22 +02008383 if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008384 return LY_SUCCESS;
8385 }
Michal Vasko004d3152020-06-11 19:59:22 +02008386 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008387 case LYXP_TOKEN_DOT:
8388 case LYXP_TOKEN_DDOT:
Michal Vasko49fec8e2022-05-24 10:28:33 +02008389 case LYXP_TOKEN_AXISNAME:
Michal Vasko03ff5a72019-09-11 13:49:33 +02008390 case LYXP_TOKEN_AT:
8391 case LYXP_TOKEN_NAMETEST:
8392 case LYXP_TOKEN_NODETYPE:
Michal Vaskob0099a92020-08-31 14:55:23 +02008393 LY_CHECK_RET(eval_relative_location_path(exp, tok_idx, all_desc, set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008394 break;
8395 default:
8396 break;
8397 }
8398
Michal Vasko03ff5a72019-09-11 13:49:33 +02008399 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02008400 /* '//' RelativeLocationPath */
Michal Vasko03ff5a72019-09-11 13:49:33 +02008401 /* evaluate '//' - deferred so as not to waste memory by remembering all the nodes */
8402 all_desc = 1;
aPiecek8b0cc152021-05-31 16:40:31 +02008403 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008404 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008405 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008406
Michal Vaskob0099a92020-08-31 14:55:23 +02008407 LY_CHECK_RET(eval_relative_location_path(exp, tok_idx, all_desc, set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008408 }
8409
8410 return LY_SUCCESS;
8411}
8412
8413/**
8414 * @brief Evaluate FunctionCall. Logs directly on error.
8415 *
Michal Vaskod3678892020-05-21 10:06:58 +02008416 * [11] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02008417 *
8418 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008419 * @param[in] tok_idx Position in the expression @p exp.
aPiecek8b0cc152021-05-31 16:40:31 +02008420 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008421 * @param[in] options XPath options.
8422 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8423 */
8424static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02008425eval_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 +02008426{
8427 LY_ERR rc;
Michal Vasko69730152020-10-09 16:30:07 +02008428
Michal Vaskodd528af2022-08-08 14:35:07 +02008429 LY_ERR (*xpath_func)(struct lyxp_set **, uint32_t, struct lyxp_set *, uint32_t) = NULL;
8430 uint32_t arg_count = 0, i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008431 struct lyxp_set **args = NULL, **args_aux;
8432
aPiecek8b0cc152021-05-31 16:40:31 +02008433 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008434 /* FunctionName */
Michal Vasko004d3152020-06-11 19:59:22 +02008435 switch (exp->tok_len[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008436 case 3:
Michal Vasko004d3152020-06-11 19:59:22 +02008437 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008438 xpath_func = &xpath_not;
Michal Vasko004d3152020-06-11 19:59:22 +02008439 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008440 xpath_func = &xpath_sum;
8441 }
8442 break;
8443 case 4:
Michal Vasko004d3152020-06-11 19:59:22 +02008444 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008445 xpath_func = &xpath_lang;
Michal Vasko004d3152020-06-11 19:59:22 +02008446 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008447 xpath_func = &xpath_last;
Michal Vasko004d3152020-06-11 19:59:22 +02008448 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008449 xpath_func = &xpath_name;
Michal Vasko004d3152020-06-11 19:59:22 +02008450 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008451 xpath_func = &xpath_true;
8452 }
8453 break;
8454 case 5:
Michal Vasko004d3152020-06-11 19:59:22 +02008455 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008456 xpath_func = &xpath_count;
Michal Vasko004d3152020-06-11 19:59:22 +02008457 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008458 xpath_func = &xpath_false;
Michal Vasko004d3152020-06-11 19:59:22 +02008459 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008460 xpath_func = &xpath_floor;
Michal Vasko004d3152020-06-11 19:59:22 +02008461 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008462 xpath_func = &xpath_round;
Michal Vasko004d3152020-06-11 19:59:22 +02008463 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008464 xpath_func = &xpath_deref;
8465 }
8466 break;
8467 case 6:
Michal Vasko004d3152020-06-11 19:59:22 +02008468 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008469 xpath_func = &xpath_concat;
Michal Vasko004d3152020-06-11 19:59:22 +02008470 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008471 xpath_func = &xpath_number;
Michal Vasko004d3152020-06-11 19:59:22 +02008472 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008473 xpath_func = &xpath_string;
8474 }
8475 break;
8476 case 7:
Michal Vasko004d3152020-06-11 19:59:22 +02008477 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008478 xpath_func = &xpath_boolean;
Michal Vasko004d3152020-06-11 19:59:22 +02008479 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008480 xpath_func = &xpath_ceiling;
Michal Vasko004d3152020-06-11 19:59:22 +02008481 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008482 xpath_func = &xpath_current;
8483 }
8484 break;
8485 case 8:
Michal Vasko004d3152020-06-11 19:59:22 +02008486 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008487 xpath_func = &xpath_contains;
Michal Vasko004d3152020-06-11 19:59:22 +02008488 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008489 xpath_func = &xpath_position;
Michal Vasko004d3152020-06-11 19:59:22 +02008490 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "re-match", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008491 xpath_func = &xpath_re_match;
8492 }
8493 break;
8494 case 9:
Michal Vasko004d3152020-06-11 19:59:22 +02008495 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008496 xpath_func = &xpath_substring;
Michal Vasko004d3152020-06-11 19:59:22 +02008497 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008498 xpath_func = &xpath_translate;
8499 }
8500 break;
8501 case 10:
Michal Vasko004d3152020-06-11 19:59:22 +02008502 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008503 xpath_func = &xpath_local_name;
Michal Vasko004d3152020-06-11 19:59:22 +02008504 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008505 xpath_func = &xpath_enum_value;
Michal Vasko004d3152020-06-11 19:59:22 +02008506 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008507 xpath_func = &xpath_bit_is_set;
8508 }
8509 break;
8510 case 11:
Michal Vasko004d3152020-06-11 19:59:22 +02008511 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008512 xpath_func = &xpath_starts_with;
8513 }
8514 break;
8515 case 12:
Michal Vasko004d3152020-06-11 19:59:22 +02008516 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008517 xpath_func = &xpath_derived_from;
8518 }
8519 break;
8520 case 13:
Michal Vasko004d3152020-06-11 19:59:22 +02008521 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008522 xpath_func = &xpath_namespace_uri;
Michal Vasko004d3152020-06-11 19:59:22 +02008523 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string-length", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008524 xpath_func = &xpath_string_length;
8525 }
8526 break;
8527 case 15:
Michal Vasko004d3152020-06-11 19:59:22 +02008528 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008529 xpath_func = &xpath_normalize_space;
Michal Vasko004d3152020-06-11 19:59:22 +02008530 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-after", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008531 xpath_func = &xpath_substring_after;
8532 }
8533 break;
8534 case 16:
Michal Vasko004d3152020-06-11 19:59:22 +02008535 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008536 xpath_func = &xpath_substring_before;
8537 }
8538 break;
8539 case 20:
Michal Vasko004d3152020-06-11 19:59:22 +02008540 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from-or-self", 20)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008541 xpath_func = &xpath_derived_from_or_self;
8542 }
8543 break;
8544 }
8545
8546 if (!xpath_func) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01008547 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 +02008548 return LY_EVALID;
8549 }
8550 }
8551
aPiecek8b0cc152021-05-31 16:40:31 +02008552 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008553 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008554 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008555
8556 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02008557 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
aPiecek8b0cc152021-05-31 16:40:31 +02008558 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008559 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008560 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008561
8562 /* ( Expr ( ',' Expr )* )? */
Michal Vasko004d3152020-06-11 19:59:22 +02008563 if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) {
aPiecek8b0cc152021-05-31 16:40:31 +02008564 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008565 args = malloc(sizeof *args);
8566 LY_CHECK_ERR_GOTO(!args, LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
8567 arg_count = 1;
8568 args[0] = set_copy(set);
8569 if (!args[0]) {
8570 rc = LY_EMEM;
8571 goto cleanup;
8572 }
8573
Michal Vasko004d3152020-06-11 19:59:22 +02008574 rc = eval_expr_select(exp, tok_idx, 0, args[0], options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008575 LY_CHECK_GOTO(rc, cleanup);
8576 } else {
aPiecek8b0cc152021-05-31 16:40:31 +02008577 rc = eval_expr_select(exp, tok_idx, 0, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008578 LY_CHECK_GOTO(rc, cleanup);
8579 }
8580 }
Michal Vasko004d3152020-06-11 19:59:22 +02008581 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) {
aPiecek8b0cc152021-05-31 16:40:31 +02008582 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008583 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008584 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008585
aPiecek8b0cc152021-05-31 16:40:31 +02008586 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008587 ++arg_count;
8588 args_aux = realloc(args, arg_count * sizeof *args);
8589 LY_CHECK_ERR_GOTO(!args_aux, arg_count--; LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
8590 args = args_aux;
8591 args[arg_count - 1] = set_copy(set);
8592 if (!args[arg_count - 1]) {
8593 rc = LY_EMEM;
8594 goto cleanup;
8595 }
8596
Michal Vasko004d3152020-06-11 19:59:22 +02008597 rc = eval_expr_select(exp, tok_idx, 0, args[arg_count - 1], options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008598 LY_CHECK_GOTO(rc, cleanup);
8599 } else {
aPiecek8b0cc152021-05-31 16:40:31 +02008600 rc = eval_expr_select(exp, tok_idx, 0, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008601 LY_CHECK_GOTO(rc, cleanup);
8602 }
8603 }
8604
8605 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02008606 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
aPiecek8b0cc152021-05-31 16:40:31 +02008607 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008608 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008609 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008610
aPiecek8b0cc152021-05-31 16:40:31 +02008611 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008612 /* evaluate function */
8613 rc = xpath_func(args, arg_count, set, options);
8614
8615 if (options & LYXP_SCNODE_ALL) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008616 /* merge all nodes from arg evaluations */
8617 for (i = 0; i < arg_count; ++i) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008618 set_scnode_clear_ctx(args[i], LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008619 lyxp_set_scnode_merge(set, args[i]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008620 }
8621 }
8622 } else {
8623 rc = LY_SUCCESS;
8624 }
8625
8626cleanup:
8627 for (i = 0; i < arg_count; ++i) {
8628 lyxp_set_free(args[i]);
8629 }
8630 free(args);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008631 return rc;
8632}
8633
8634/**
8635 * @brief Evaluate Number. Logs directly on error.
8636 *
8637 * @param[in] ctx Context for errors.
8638 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008639 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008640 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8641 * @return LY_ERR
8642 */
8643static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02008644eval_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 +02008645{
8646 long double num;
8647 char *endptr;
8648
8649 if (set) {
8650 errno = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02008651 num = strtold(&exp->expr[exp->tok_pos[*tok_idx]], &endptr);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008652 if (errno) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01008653 LOGVAL(ctx, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]);
8654 LOGVAL(ctx, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double (%s).",
Michal Vasko69730152020-10-09 16:30:07 +02008655 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]], strerror(errno));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008656 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02008657 } else if (endptr - &exp->expr[exp->tok_pos[*tok_idx]] != exp->tok_len[*tok_idx]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01008658 LOGVAL(ctx, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]);
8659 LOGVAL(ctx, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double.",
Michal Vasko69730152020-10-09 16:30:07 +02008660 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008661 return LY_EVALID;
8662 }
8663
8664 set_fill_number(set, num);
8665 }
8666
8667 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008668 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008669 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008670 return LY_SUCCESS;
8671}
8672
aPiecekdf23eee2021-10-07 12:21:50 +02008673LY_ERR
8674lyxp_vars_find(struct lyxp_var *vars, const char *name, size_t name_len, struct lyxp_var **var)
8675{
8676 LY_ERR ret = LY_ENOTFOUND;
8677 LY_ARRAY_COUNT_TYPE u;
8678
8679 assert(vars && name);
8680
8681 name_len = name_len ? name_len : strlen(name);
8682
8683 LY_ARRAY_FOR(vars, u) {
8684 if (!strncmp(vars[u].name, name, name_len)) {
8685 ret = LY_SUCCESS;
8686 break;
8687 }
8688 }
8689
8690 if (var && !ret) {
8691 *var = &vars[u];
8692 }
8693
8694 return ret;
8695}
8696
Michal Vasko03ff5a72019-09-11 13:49:33 +02008697/**
aPiecekfba75362021-10-07 12:39:48 +02008698 * @brief Evaluate VariableReference.
8699 *
8700 * @param[in] exp Parsed XPath expression.
8701 * @param[in] tok_idx Position in the expression @p exp.
8702 * @param[in] vars [Sized array](@ref sizedarrays) of XPath variables.
8703 * @param[in,out] set Context and result set.
8704 * @param[in] options XPath options.
8705 * @return LY_ERR value.
8706 */
8707static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02008708eval_variable_reference(const struct lyxp_expr *exp, uint32_t *tok_idx, struct lyxp_set *set, uint32_t options)
aPiecekfba75362021-10-07 12:39:48 +02008709{
8710 LY_ERR ret;
8711 const char *name;
8712 struct lyxp_var *var;
8713 const struct lyxp_var *vars;
8714 struct lyxp_expr *tokens = NULL;
Michal Vaskodd528af2022-08-08 14:35:07 +02008715 uint32_t token_index, name_len;
aPiecekfba75362021-10-07 12:39:48 +02008716
8717 vars = set->vars;
8718
Michal Vasko49fec8e2022-05-24 10:28:33 +02008719 /* find out the name and value of the variable */
aPiecekfba75362021-10-07 12:39:48 +02008720 name = &exp->expr[exp->tok_pos[*tok_idx]];
Michal Vaskoe68b5402022-07-29 14:58:15 +02008721 name_len = exp->tok_len[*tok_idx];
8722 ret = lyxp_vars_find((struct lyxp_var *)vars, name, name_len, &var);
8723 LY_CHECK_ERR_RET(ret, LOGERR(set->ctx, ret, "XPath variable \"%.*s\" not defined.", (int)name_len, name), ret);
aPiecekfba75362021-10-07 12:39:48 +02008724
Michal Vasko49fec8e2022-05-24 10:28:33 +02008725 /* parse value */
aPiecekfba75362021-10-07 12:39:48 +02008726 ret = lyxp_expr_parse(set->ctx, var->value, 0, 1, &tokens);
8727 LY_CHECK_GOTO(ret, cleanup);
8728
Michal Vasko49fec8e2022-05-24 10:28:33 +02008729 /* evaluate value */
aPiecekfba75362021-10-07 12:39:48 +02008730 token_index = 0;
8731 ret = eval_expr_select(tokens, &token_index, 0, set, options);
8732 LY_CHECK_GOTO(ret, cleanup);
8733
8734cleanup:
8735 lyxp_expr_free(set->ctx, tokens);
8736
8737 return ret;
8738}
8739
8740/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02008741 * @brief Evaluate PathExpr. Logs directly on error.
8742 *
Michal Vaskod3678892020-05-21 10:06:58 +02008743 * [12] PathExpr ::= LocationPath | PrimaryExpr Predicate*
Michal Vasko03ff5a72019-09-11 13:49:33 +02008744 * | PrimaryExpr Predicate* '/' RelativeLocationPath
8745 * | PrimaryExpr Predicate* '//' RelativeLocationPath
8746 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
aPiecekfba75362021-10-07 12:39:48 +02008747 * [10] PrimaryExpr ::= VariableReference | '(' Expr ')' | Literal | Number | FunctionCall
Michal Vasko03ff5a72019-09-11 13:49:33 +02008748 *
8749 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008750 * @param[in] tok_idx Position in the expression @p exp.
aPiecek8b0cc152021-05-31 16:40:31 +02008751 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008752 * @param[in] options XPath options.
8753 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8754 */
8755static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02008756eval_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 +02008757{
Michal Vasko49fec8e2022-05-24 10:28:33 +02008758 ly_bool all_desc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008759 LY_ERR rc;
8760
Michal Vasko004d3152020-06-11 19:59:22 +02008761 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008762 case LYXP_TOKEN_PAR1:
8763 /* '(' Expr ')' */
8764
8765 /* '(' */
aPiecek8b0cc152021-05-31 16:40:31 +02008766 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008767 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008768 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008769
8770 /* Expr */
Michal Vasko004d3152020-06-11 19:59:22 +02008771 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008772 LY_CHECK_RET(rc);
8773
8774 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02008775 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
aPiecek8b0cc152021-05-31 16:40:31 +02008776 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008777 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008778 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008779
Michal Vasko03ff5a72019-09-11 13:49:33 +02008780 goto predicate;
8781
8782 case LYXP_TOKEN_DOT:
8783 case LYXP_TOKEN_DDOT:
Michal Vasko49fec8e2022-05-24 10:28:33 +02008784 case LYXP_TOKEN_AXISNAME:
Michal Vasko03ff5a72019-09-11 13:49:33 +02008785 case LYXP_TOKEN_AT:
8786 case LYXP_TOKEN_NAMETEST:
8787 case LYXP_TOKEN_NODETYPE:
8788 /* RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02008789 rc = eval_relative_location_path(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008790 LY_CHECK_RET(rc);
8791 break;
8792
aPiecekfba75362021-10-07 12:39:48 +02008793 case LYXP_TOKEN_VARREF:
8794 /* VariableReference */
8795 rc = eval_variable_reference(exp, tok_idx, set, options);
8796 LY_CHECK_RET(rc);
8797 ++(*tok_idx);
8798
aPiecekfba75362021-10-07 12:39:48 +02008799 goto predicate;
8800
Michal Vasko03ff5a72019-09-11 13:49:33 +02008801 case LYXP_TOKEN_FUNCNAME:
8802 /* FunctionCall */
aPiecek8b0cc152021-05-31 16:40:31 +02008803 rc = eval_function_call(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008804 LY_CHECK_RET(rc);
8805
Michal Vasko03ff5a72019-09-11 13:49:33 +02008806 goto predicate;
8807
Michal Vasko3e48bf32020-06-01 08:39:07 +02008808 case LYXP_TOKEN_OPER_PATH:
8809 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +02008810 /* AbsoluteLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02008811 rc = eval_absolute_location_path(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008812 LY_CHECK_RET(rc);
8813 break;
8814
8815 case LYXP_TOKEN_LITERAL:
8816 /* Literal */
aPiecek8b0cc152021-05-31 16:40:31 +02008817 if ((options & LYXP_SKIP_EXPR) || (options & LYXP_SCNODE_ALL)) {
8818 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008819 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008820 }
Michal Vasko004d3152020-06-11 19:59:22 +02008821 eval_literal(exp, tok_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008822 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02008823 eval_literal(exp, tok_idx, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008824 }
8825
Michal Vasko03ff5a72019-09-11 13:49:33 +02008826 goto predicate;
8827
8828 case LYXP_TOKEN_NUMBER:
8829 /* Number */
aPiecek8b0cc152021-05-31 16:40:31 +02008830 if ((options & LYXP_SKIP_EXPR) || (options & LYXP_SCNODE_ALL)) {
8831 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008832 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008833 }
Michal Vasko004d3152020-06-11 19:59:22 +02008834 rc = eval_number(NULL, exp, tok_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008835 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02008836 rc = eval_number(set->ctx, exp, tok_idx, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008837 }
8838 LY_CHECK_RET(rc);
8839
Michal Vasko03ff5a72019-09-11 13:49:33 +02008840 goto predicate;
8841
8842 default:
Michal Vasko49fec8e2022-05-24 10:28:33 +02008843 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 +02008844 return LY_EVALID;
8845 }
8846
8847 return LY_SUCCESS;
8848
8849predicate:
8850 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02008851 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02008852 rc = eval_predicate(exp, tok_idx, set, options, LYXP_AXIS_CHILD);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008853 LY_CHECK_RET(rc);
8854 }
8855
8856 /* ('/' or '//') RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02008857 if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008858
8859 /* evaluate '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02008860 if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008861 all_desc = 0;
8862 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008863 all_desc = 1;
8864 }
8865
aPiecek8b0cc152021-05-31 16:40:31 +02008866 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008867 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008868 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008869
Michal Vasko004d3152020-06-11 19:59:22 +02008870 rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008871 LY_CHECK_RET(rc);
8872 }
8873
8874 return LY_SUCCESS;
8875}
8876
8877/**
8878 * @brief Evaluate UnionExpr. Logs directly on error.
8879 *
Michal Vaskod3678892020-05-21 10:06:58 +02008880 * [20] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008881 *
8882 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008883 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008884 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008885 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008886 * @param[in] options XPath options.
8887 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8888 */
8889static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02008890eval_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 +02008891{
8892 LY_ERR rc = LY_SUCCESS;
8893 struct lyxp_set orig_set, set2;
Michal Vaskodd528af2022-08-08 14:35:07 +02008894 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008895
8896 assert(repeat);
8897
8898 set_init(&orig_set, set);
8899 set_init(&set2, set);
8900
8901 set_fill_set(&orig_set, set);
8902
Michal Vasko004d3152020-06-11 19:59:22 +02008903 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008904 LY_CHECK_GOTO(rc, cleanup);
8905
8906 /* ('|' PathExpr)* */
8907 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008908 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_UNI);
aPiecek8b0cc152021-05-31 16:40:31 +02008909 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008910 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008911 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008912
aPiecek8b0cc152021-05-31 16:40:31 +02008913 if (options & LYXP_SKIP_EXPR) {
8914 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008915 LY_CHECK_GOTO(rc, cleanup);
8916 continue;
8917 }
8918
8919 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008920 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008921 LY_CHECK_GOTO(rc, cleanup);
8922
8923 /* eval */
8924 if (options & LYXP_SCNODE_ALL) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01008925 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008926 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008927 rc = moveto_union(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008928 LY_CHECK_GOTO(rc, cleanup);
8929 }
8930 }
8931
8932cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008933 lyxp_set_free_content(&orig_set);
8934 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008935 return rc;
8936}
8937
8938/**
8939 * @brief Evaluate UnaryExpr. Logs directly on error.
8940 *
Michal Vaskod3678892020-05-21 10:06:58 +02008941 * [19] UnaryExpr ::= UnionExpr | '-' UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008942 *
8943 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008944 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008945 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008946 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008947 * @param[in] options XPath options.
8948 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8949 */
8950static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02008951eval_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 +02008952{
8953 LY_ERR rc;
Michal Vaskodd528af2022-08-08 14:35:07 +02008954 uint32_t this_op, i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008955
8956 assert(repeat);
8957
8958 /* ('-')+ */
Michal Vasko004d3152020-06-11 19:59:22 +02008959 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008960 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008961 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 +02008962
aPiecek8b0cc152021-05-31 16:40:31 +02008963 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008964 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008965 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008966 }
8967
Michal Vasko004d3152020-06-11 19:59:22 +02008968 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNARY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008969 LY_CHECK_RET(rc);
8970
aPiecek8b0cc152021-05-31 16:40:31 +02008971 if (!(options & LYXP_SKIP_EXPR) && (repeat % 2)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008972 if (options & LYXP_SCNODE_ALL) {
8973 warn_operands(set->ctx, set, NULL, 1, exp->expr, exp->tok_pos[this_op]);
8974 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008975 rc = moveto_op_math(set, NULL, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008976 LY_CHECK_RET(rc);
8977 }
8978 }
8979
8980 return LY_SUCCESS;
8981}
8982
8983/**
8984 * @brief Evaluate MultiplicativeExpr. Logs directly on error.
8985 *
Michal Vaskod3678892020-05-21 10:06:58 +02008986 * [18] MultiplicativeExpr ::= UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008987 * | MultiplicativeExpr '*' UnaryExpr
8988 * | MultiplicativeExpr 'div' UnaryExpr
8989 * | MultiplicativeExpr 'mod' UnaryExpr
8990 *
8991 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008992 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008993 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008994 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008995 * @param[in] options XPath options.
8996 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8997 */
8998static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02008999eval_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 +02009000 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02009001{
9002 LY_ERR rc;
Michal Vaskodd528af2022-08-08 14:35:07 +02009003 uint32_t i, this_op;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009004 struct lyxp_set orig_set, set2;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009005
9006 assert(repeat);
9007
9008 set_init(&orig_set, set);
9009 set_init(&set2, set);
9010
9011 set_fill_set(&orig_set, set);
9012
Michal Vasko004d3152020-06-11 19:59:22 +02009013 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009014 LY_CHECK_GOTO(rc, cleanup);
9015
9016 /* ('*' / 'div' / 'mod' UnaryExpr)* */
9017 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02009018 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009019
Michal Vasko004d3152020-06-11 19:59:22 +02009020 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH);
aPiecek8b0cc152021-05-31 16:40:31 +02009021 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02009022 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02009023 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009024
aPiecek8b0cc152021-05-31 16:40:31 +02009025 if (options & LYXP_SKIP_EXPR) {
9026 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009027 LY_CHECK_GOTO(rc, cleanup);
9028 continue;
9029 }
9030
9031 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02009032 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009033 LY_CHECK_GOTO(rc, cleanup);
9034
9035 /* eval */
9036 if (options & LYXP_SCNODE_ALL) {
9037 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01009038 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02009039 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009040 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009041 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009042 LY_CHECK_GOTO(rc, cleanup);
9043 }
9044 }
9045
9046cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02009047 lyxp_set_free_content(&orig_set);
9048 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009049 return rc;
9050}
9051
9052/**
9053 * @brief Evaluate AdditiveExpr. Logs directly on error.
9054 *
Michal Vaskod3678892020-05-21 10:06:58 +02009055 * [17] AdditiveExpr ::= MultiplicativeExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02009056 * | AdditiveExpr '+' MultiplicativeExpr
9057 * | AdditiveExpr '-' MultiplicativeExpr
9058 *
9059 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02009060 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009061 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02009062 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009063 * @param[in] options XPath options.
9064 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
9065 */
9066static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02009067eval_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 +02009068{
9069 LY_ERR rc;
Michal Vaskodd528af2022-08-08 14:35:07 +02009070 uint32_t i, this_op;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009071 struct lyxp_set orig_set, set2;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009072
9073 assert(repeat);
9074
9075 set_init(&orig_set, set);
9076 set_init(&set2, set);
9077
9078 set_fill_set(&orig_set, set);
9079
Michal Vasko004d3152020-06-11 19:59:22 +02009080 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009081 LY_CHECK_GOTO(rc, cleanup);
9082
9083 /* ('+' / '-' MultiplicativeExpr)* */
9084 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02009085 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009086
Michal Vasko004d3152020-06-11 19:59:22 +02009087 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009088 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02009089 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02009090 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009091
aPiecek8b0cc152021-05-31 16:40:31 +02009092 if (options & LYXP_SKIP_EXPR) {
9093 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009094 LY_CHECK_GOTO(rc, cleanup);
9095 continue;
9096 }
9097
9098 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02009099 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009100 LY_CHECK_GOTO(rc, cleanup);
9101
9102 /* eval */
9103 if (options & LYXP_SCNODE_ALL) {
9104 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01009105 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02009106 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009107 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009108 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009109 LY_CHECK_GOTO(rc, cleanup);
9110 }
9111 }
9112
9113cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02009114 lyxp_set_free_content(&orig_set);
9115 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009116 return rc;
9117}
9118
9119/**
9120 * @brief Evaluate RelationalExpr. Logs directly on error.
9121 *
Michal Vaskod3678892020-05-21 10:06:58 +02009122 * [16] RelationalExpr ::= AdditiveExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02009123 * | RelationalExpr '<' AdditiveExpr
9124 * | RelationalExpr '>' AdditiveExpr
9125 * | RelationalExpr '<=' AdditiveExpr
9126 * | RelationalExpr '>=' AdditiveExpr
9127 *
9128 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02009129 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009130 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02009131 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009132 * @param[in] options XPath options.
9133 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
9134 */
9135static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02009136eval_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 +02009137{
9138 LY_ERR rc;
Michal Vaskodd528af2022-08-08 14:35:07 +02009139 uint32_t i, this_op;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009140 struct lyxp_set orig_set, set2;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009141
9142 assert(repeat);
9143
9144 set_init(&orig_set, set);
9145 set_init(&set2, set);
9146
9147 set_fill_set(&orig_set, set);
9148
Michal Vasko004d3152020-06-11 19:59:22 +02009149 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009150 LY_CHECK_GOTO(rc, cleanup);
9151
9152 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
9153 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02009154 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009155
Michal Vasko004d3152020-06-11 19:59:22 +02009156 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_COMP);
aPiecek8b0cc152021-05-31 16:40:31 +02009157 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02009158 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02009159 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009160
aPiecek8b0cc152021-05-31 16:40:31 +02009161 if (options & LYXP_SKIP_EXPR) {
9162 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009163 LY_CHECK_GOTO(rc, cleanup);
9164 continue;
9165 }
9166
9167 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02009168 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009169 LY_CHECK_GOTO(rc, cleanup);
9170
9171 /* eval */
9172 if (options & LYXP_SCNODE_ALL) {
9173 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01009174 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02009175 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009176 } else {
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02009177 ly_bool result;
9178
9179 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], &result);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009180 LY_CHECK_GOTO(rc, cleanup);
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02009181 set_fill_boolean(set, result);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009182 }
9183 }
9184
9185cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02009186 lyxp_set_free_content(&orig_set);
9187 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009188 return rc;
9189}
9190
9191/**
9192 * @brief Evaluate EqualityExpr. Logs directly on error.
9193 *
Michal Vaskod3678892020-05-21 10:06:58 +02009194 * [15] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02009195 * | EqualityExpr '!=' RelationalExpr
9196 *
9197 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02009198 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009199 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02009200 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009201 * @param[in] options XPath options.
9202 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
9203 */
9204static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02009205eval_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 +02009206{
9207 LY_ERR rc;
Michal Vaskodd528af2022-08-08 14:35:07 +02009208 uint32_t i, this_op;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009209 struct lyxp_set orig_set, set2;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009210
9211 assert(repeat);
9212
9213 set_init(&orig_set, set);
9214 set_init(&set2, set);
9215
9216 set_fill_set(&orig_set, set);
9217
Michal Vasko004d3152020-06-11 19:59:22 +02009218 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009219 LY_CHECK_GOTO(rc, cleanup);
9220
9221 /* ('=' / '!=' RelationalExpr)* */
9222 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02009223 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009224
Michal Vasko004d3152020-06-11 19:59:22 +02009225 assert((exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_EQUAL) || (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_NEQUAL));
aPiecek8b0cc152021-05-31 16:40:31 +02009226 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02009227 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02009228 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009229
aPiecek8b0cc152021-05-31 16:40:31 +02009230 if (options & LYXP_SKIP_EXPR) {
9231 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009232 LY_CHECK_GOTO(rc, cleanup);
9233 continue;
9234 }
9235
9236 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02009237 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009238 LY_CHECK_GOTO(rc, cleanup);
9239
9240 /* eval */
9241 if (options & LYXP_SCNODE_ALL) {
9242 warn_operands(set->ctx, set, &set2, 0, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vasko004d3152020-06-11 19:59:22 +02009243 warn_equality_value(exp, set, *tok_idx - 1, this_op - 1, *tok_idx - 1);
9244 warn_equality_value(exp, &set2, this_op - 1, this_op - 1, *tok_idx - 1);
Michal Vaskoecd62de2019-11-13 12:35:11 +01009245 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02009246 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009247 } else {
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02009248 ly_bool result;
9249
9250 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], &result);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009251 LY_CHECK_GOTO(rc, cleanup);
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02009252 set_fill_boolean(set, result);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009253 }
9254 }
9255
9256cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02009257 lyxp_set_free_content(&orig_set);
9258 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009259 return rc;
9260}
9261
9262/**
9263 * @brief Evaluate AndExpr. Logs directly on error.
9264 *
Michal Vaskod3678892020-05-21 10:06:58 +02009265 * [14] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02009266 *
9267 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02009268 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009269 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02009270 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009271 * @param[in] options XPath options.
9272 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
9273 */
9274static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02009275eval_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 +02009276{
9277 LY_ERR rc;
9278 struct lyxp_set orig_set, set2;
Michal Vaskodd528af2022-08-08 14:35:07 +02009279 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009280
9281 assert(repeat);
9282
9283 set_init(&orig_set, set);
9284 set_init(&set2, set);
9285
9286 set_fill_set(&orig_set, set);
9287
Michal Vasko004d3152020-06-11 19:59:22 +02009288 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009289 LY_CHECK_GOTO(rc, cleanup);
9290
9291 /* cast to boolean, we know that will be the final result */
aPiecek8b0cc152021-05-31 16:40:31 +02009292 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02009293 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009294 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009295 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009296 }
9297
9298 /* ('and' EqualityExpr)* */
9299 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02009300 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG);
aPiecek8b0cc152021-05-31 16:40:31 +02009301 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, ((options & LYXP_SKIP_EXPR) || !set->val.bln ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02009302 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02009303 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009304
9305 /* lazy evaluation */
aPiecek8b0cc152021-05-31 16:40:31 +02009306 if ((options & LYXP_SKIP_EXPR) || ((set->type == LYXP_SET_BOOLEAN) && !set->val.bln)) {
9307 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009308 LY_CHECK_GOTO(rc, cleanup);
9309 continue;
9310 }
9311
9312 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02009313 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009314 LY_CHECK_GOTO(rc, cleanup);
9315
9316 /* eval - just get boolean value actually */
9317 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vasko1a09b212021-05-06 13:00:10 +02009318 set_scnode_clear_ctx(&set2, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskoecd62de2019-11-13 12:35:11 +01009319 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009320 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009321 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009322 set_fill_set(set, &set2);
9323 }
9324 }
9325
9326cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02009327 lyxp_set_free_content(&orig_set);
9328 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009329 return rc;
9330}
9331
9332/**
9333 * @brief Evaluate OrExpr. Logs directly on error.
9334 *
Michal Vaskod3678892020-05-21 10:06:58 +02009335 * [13] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02009336 *
9337 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02009338 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009339 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02009340 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009341 * @param[in] options XPath options.
9342 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
9343 */
9344static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02009345eval_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 +02009346{
9347 LY_ERR rc;
9348 struct lyxp_set orig_set, set2;
Michal Vaskodd528af2022-08-08 14:35:07 +02009349 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009350
9351 assert(repeat);
9352
9353 set_init(&orig_set, set);
9354 set_init(&set2, set);
9355
9356 set_fill_set(&orig_set, set);
9357
Michal Vasko004d3152020-06-11 19:59:22 +02009358 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009359 LY_CHECK_GOTO(rc, cleanup);
9360
9361 /* cast to boolean, we know that will be the final result */
aPiecek8b0cc152021-05-31 16:40:31 +02009362 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02009363 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009364 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009365 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009366 }
9367
9368 /* ('or' AndExpr)* */
9369 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02009370 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG);
aPiecek8b0cc152021-05-31 16:40:31 +02009371 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, ((options & LYXP_SKIP_EXPR) || set->val.bln ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02009372 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02009373 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009374
9375 /* lazy evaluation */
aPiecek8b0cc152021-05-31 16:40:31 +02009376 if ((options & LYXP_SKIP_EXPR) || ((set->type == LYXP_SET_BOOLEAN) && set->val.bln)) {
9377 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009378 LY_CHECK_GOTO(rc, cleanup);
9379 continue;
9380 }
9381
9382 set_fill_set(&set2, &orig_set);
9383 /* expr_type cound have been LYXP_EXPR_NONE in all these later calls (except for the first one),
9384 * but it does not matter */
Michal Vasko004d3152020-06-11 19:59:22 +02009385 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009386 LY_CHECK_GOTO(rc, cleanup);
9387
9388 /* eval - just get boolean value actually */
9389 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vasko1a09b212021-05-06 13:00:10 +02009390 set_scnode_clear_ctx(&set2, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskoecd62de2019-11-13 12:35:11 +01009391 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009392 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009393 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009394 set_fill_set(set, &set2);
9395 }
9396 }
9397
9398cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02009399 lyxp_set_free_content(&orig_set);
9400 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009401 return rc;
9402}
9403
9404/**
Michal Vasko004d3152020-06-11 19:59:22 +02009405 * @brief Decide what expression is at the pointer @p tok_idx and evaluate it accordingly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009406 *
9407 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02009408 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009409 * @param[in] etype Expression type to evaluate.
aPiecek8b0cc152021-05-31 16:40:31 +02009410 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009411 * @param[in] options XPath options.
9412 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
9413 */
9414static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02009415eval_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 +02009416 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02009417{
Michal Vaskodd528af2022-08-08 14:35:07 +02009418 uint32_t i, count;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009419 enum lyxp_expr_type next_etype;
9420 LY_ERR rc;
9421
9422 /* process operator repeats */
Michal Vasko004d3152020-06-11 19:59:22 +02009423 if (!exp->repeat[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02009424 next_etype = LYXP_EXPR_NONE;
9425 } else {
9426 /* find etype repeat */
Radek Krejci1e008d22020-08-17 11:37:37 +02009427 for (i = 0; exp->repeat[*tok_idx][i] > etype; ++i) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02009428
9429 /* select one-priority lower because etype expression called us */
9430 if (i) {
Michal Vasko004d3152020-06-11 19:59:22 +02009431 next_etype = exp->repeat[*tok_idx][i - 1];
Michal Vasko03ff5a72019-09-11 13:49:33 +02009432 /* count repeats for that expression */
Radek Krejci1e008d22020-08-17 11:37:37 +02009433 for (count = 0; i && exp->repeat[*tok_idx][i - 1] == next_etype; ++count, --i) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02009434 } else {
9435 next_etype = LYXP_EXPR_NONE;
9436 }
9437 }
9438
9439 /* decide what expression are we parsing based on the repeat */
9440 switch (next_etype) {
9441 case LYXP_EXPR_OR:
Michal Vasko004d3152020-06-11 19:59:22 +02009442 rc = eval_or_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009443 break;
9444 case LYXP_EXPR_AND:
Michal Vasko004d3152020-06-11 19:59:22 +02009445 rc = eval_and_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009446 break;
9447 case LYXP_EXPR_EQUALITY:
Michal Vasko004d3152020-06-11 19:59:22 +02009448 rc = eval_equality_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009449 break;
9450 case LYXP_EXPR_RELATIONAL:
Michal Vasko004d3152020-06-11 19:59:22 +02009451 rc = eval_relational_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009452 break;
9453 case LYXP_EXPR_ADDITIVE:
Michal Vasko004d3152020-06-11 19:59:22 +02009454 rc = eval_additive_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009455 break;
9456 case LYXP_EXPR_MULTIPLICATIVE:
Michal Vasko004d3152020-06-11 19:59:22 +02009457 rc = eval_multiplicative_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009458 break;
9459 case LYXP_EXPR_UNARY:
Michal Vasko004d3152020-06-11 19:59:22 +02009460 rc = eval_unary_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009461 break;
9462 case LYXP_EXPR_UNION:
Michal Vasko004d3152020-06-11 19:59:22 +02009463 rc = eval_union_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009464 break;
9465 case LYXP_EXPR_NONE:
Michal Vasko004d3152020-06-11 19:59:22 +02009466 rc = eval_path_expr(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009467 break;
9468 default:
9469 LOGINT_RET(set->ctx);
9470 }
9471
9472 return rc;
9473}
9474
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009475/**
9476 * @brief Get root type.
9477 *
9478 * @param[in] ctx_node Context node.
9479 * @param[in] ctx_scnode Schema context node.
9480 * @param[in] options XPath options.
9481 * @return Root type.
9482 */
9483static enum lyxp_node_type
Radek Krejci1deb5be2020-08-26 16:43:36 +02009484lyxp_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 +01009485{
Michal Vasko6b26e742020-07-17 15:02:10 +02009486 const struct lysc_node *op;
9487
Michal Vaskoa27245c2022-05-02 09:01:35 +02009488 /* explicit */
9489 if (options & LYXP_ACCESS_TREE_ALL) {
9490 return LYXP_NODE_ROOT;
9491 } else if (options & LYXP_ACCESS_TREE_CONFIG) {
9492 return LYXP_NODE_ROOT_CONFIG;
9493 }
9494
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009495 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009496 /* schema */
Radek Krejci1e008d22020-08-17 11:37:37 +02009497 for (op = ctx_scnode; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent) {}
Michal Vasko6b26e742020-07-17 15:02:10 +02009498
9499 if (op || (options & LYXP_SCNODE)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009500 /* general root that can access everything */
9501 return LYXP_NODE_ROOT;
9502 } else if (!ctx_scnode || (ctx_scnode->flags & LYS_CONFIG_W)) {
9503 /* root context node can access only config data (because we said so, it is unspecified) */
9504 return LYXP_NODE_ROOT_CONFIG;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009505 }
Michal Vasko6b26e742020-07-17 15:02:10 +02009506 return LYXP_NODE_ROOT;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009507 }
9508
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009509 /* data */
Michal Vasko6b26e742020-07-17 15:02:10 +02009510 op = ctx_node ? ctx_node->schema : NULL;
Michal Vaskod989ba02020-08-24 10:59:24 +02009511 for ( ; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent) {}
Michal Vasko6b26e742020-07-17 15:02:10 +02009512
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009513 if (op || !(options & LYXP_SCHEMA)) {
9514 /* general root that can access everything */
9515 return LYXP_NODE_ROOT;
Christian Hoppsb6ecaea2021-02-06 09:45:38 -05009516 } else if (!ctx_node || !ctx_node->schema || (ctx_node->schema->flags & LYS_CONFIG_W)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009517 /* root context node can access only config data (because we said so, it is unspecified) */
9518 return LYXP_NODE_ROOT_CONFIG;
9519 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009520 return LYXP_NODE_ROOT;
9521}
9522
Michal Vasko03ff5a72019-09-11 13:49:33 +02009523LY_ERR
Michal Vasko400e9672021-01-11 13:39:17 +01009524lyxp_eval(const struct ly_ctx *ctx, const struct lyxp_expr *exp, const struct lys_module *cur_mod,
Michal Vaskoa3e92bc2022-07-29 14:56:23 +02009525 LY_VALUE_FORMAT format, void *prefix_data, const struct lyd_node *cur_node, const struct lyd_node *ctx_node,
9526 const struct lyd_node *tree, const struct lyxp_var *vars, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02009527{
Michal Vaskodd528af2022-08-08 14:35:07 +02009528 uint32_t tok_idx = 0;
Michal Vaskoddd76592022-01-17 13:34:48 +01009529 const struct lysc_node *snode;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009530 LY_ERR rc;
9531
Michal Vasko400e9672021-01-11 13:39:17 +01009532 LY_CHECK_ARG_RET(ctx, ctx, exp, set, LY_EINVAL);
Radek Krejci8df109d2021-04-23 12:19:08 +02009533 if (!cur_mod && ((format == LY_VALUE_SCHEMA) || (format == LY_VALUE_SCHEMA_RESOLVED))) {
Michal Vaskoddd76592022-01-17 13:34:48 +01009534 LOGERR(ctx, LY_EINVAL, "Current module must be set if schema format is used.");
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009535 return LY_EINVAL;
Michal Vasko004d3152020-06-11 19:59:22 +02009536 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02009537
Michal Vaskod3bb12f2020-12-04 14:33:09 +01009538 if (tree) {
9539 /* adjust the pointer to be the first top-level sibling */
9540 while (tree->parent) {
9541 tree = lyd_parent(tree);
9542 }
9543 tree = lyd_first_sibling(tree);
Michal Vaskoddd76592022-01-17 13:34:48 +01009544
9545 for (snode = tree->schema->parent; snode && (snode->nodetype & (LYS_CASE | LYS_CHOICE)); snode = snode->parent) {}
9546 if (snode) {
9547 /* unable to evaluate absolute paths */
9548 LOGERR(ctx, LY_EINVAL, "Data node \"%s\" has no parent but is not instance of a top-level schema node.",
9549 LYD_NAME(tree));
9550 return LY_EINVAL;
9551 }
Michal Vaskod3bb12f2020-12-04 14:33:09 +01009552 }
9553
Michal Vasko03ff5a72019-09-11 13:49:33 +02009554 /* prepare set for evaluation */
Michal Vasko03ff5a72019-09-11 13:49:33 +02009555 memset(set, 0, sizeof *set);
Michal Vaskod3678892020-05-21 10:06:58 +02009556 set->type = LYXP_SET_NODE_SET;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009557 set->root_type = lyxp_get_root_type(ctx_node, NULL, options);
9558 set_insert_node(set, (struct lyd_node *)ctx_node, 0, ctx_node ? LYXP_NODE_ELEM : set->root_type, 0);
9559
Michal Vasko400e9672021-01-11 13:39:17 +01009560 set->ctx = (struct ly_ctx *)ctx;
Michal Vaskoa3e92bc2022-07-29 14:56:23 +02009561 set->cur_node = cur_node;
9562 for (set->context_op = cur_node ? cur_node->schema : NULL;
Radek Krejci0f969882020-08-21 16:56:47 +02009563 set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF));
9564 set->context_op = set->context_op->parent) {}
Michal Vaskof03ed032020-03-04 13:31:44 +01009565 set->tree = tree;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009566 set->cur_mod = cur_mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009567 set->format = format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009568 set->prefix_data = prefix_data;
aPiecekfba75362021-10-07 12:39:48 +02009569 set->vars = vars;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009570
Radek Krejciddace2c2021-01-08 11:30:56 +01009571 LOG_LOCSET(NULL, set->cur_node, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01009572
Michal Vasko03ff5a72019-09-11 13:49:33 +02009573 /* evaluate */
Michal Vasko004d3152020-06-11 19:59:22 +02009574 rc = eval_expr_select(exp, &tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009575 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02009576 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009577 }
9578
Michal Vasko4a7d4d62021-12-13 17:05:06 +01009579 if (set->cur_node) {
9580 LOG_LOCBACK(0, 1, 0, 0);
9581 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02009582 return rc;
9583}
9584
9585#if 0
9586
9587/* full xml printing of set elements, not used currently */
9588
9589void
9590lyxp_set_print_xml(FILE *f, struct lyxp_set *set)
9591{
9592 uint32_t i;
9593 char *str_num;
9594 struct lyout out;
9595
9596 memset(&out, 0, sizeof out);
9597
9598 out.type = LYOUT_STREAM;
9599 out.method.f = f;
9600
9601 switch (set->type) {
9602 case LYXP_SET_EMPTY:
Michal Vasko5233e962020-08-14 14:26:20 +02009603 ly_print_(&out, "Empty XPath set\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02009604 break;
9605 case LYXP_SET_BOOLEAN:
Michal Vasko5233e962020-08-14 14:26:20 +02009606 ly_print_(&out, "Boolean XPath set:\n");
9607 ly_print_(&out, "%s\n\n", set->value.bool ? "true" : "false");
Michal Vasko03ff5a72019-09-11 13:49:33 +02009608 break;
9609 case LYXP_SET_STRING:
Michal Vasko5233e962020-08-14 14:26:20 +02009610 ly_print_(&out, "String XPath set:\n");
9611 ly_print_(&out, "\"%s\"\n\n", set->value.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009612 break;
9613 case LYXP_SET_NUMBER:
Michal Vasko5233e962020-08-14 14:26:20 +02009614 ly_print_(&out, "Number XPath set:\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02009615
9616 if (isnan(set->value.num)) {
9617 str_num = strdup("NaN");
9618 } else if ((set->value.num == 0) || (set->value.num == -0.0f)) {
9619 str_num = strdup("0");
9620 } else if (isinf(set->value.num) && !signbit(set->value.num)) {
9621 str_num = strdup("Infinity");
9622 } else if (isinf(set->value.num) && signbit(set->value.num)) {
9623 str_num = strdup("-Infinity");
9624 } else if ((long long)set->value.num == set->value.num) {
9625 if (asprintf(&str_num, "%lld", (long long)set->value.num) == -1) {
9626 str_num = NULL;
9627 }
9628 } else {
9629 if (asprintf(&str_num, "%03.1Lf", set->value.num) == -1) {
9630 str_num = NULL;
9631 }
9632 }
9633 if (!str_num) {
9634 LOGMEM;
9635 return;
9636 }
Michal Vasko5233e962020-08-14 14:26:20 +02009637 ly_print_(&out, "%s\n\n", str_num);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009638 free(str_num);
9639 break;
9640 case LYXP_SET_NODE_SET:
Michal Vasko5233e962020-08-14 14:26:20 +02009641 ly_print_(&out, "Node XPath set:\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02009642
9643 for (i = 0; i < set->used; ++i) {
Michal Vasko5233e962020-08-14 14:26:20 +02009644 ly_print_(&out, "%d. ", i + 1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009645 switch (set->node_type[i]) {
9646 case LYXP_NODE_ROOT_ALL:
Michal Vasko5233e962020-08-14 14:26:20 +02009647 ly_print_(&out, "ROOT all\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02009648 break;
9649 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko5233e962020-08-14 14:26:20 +02009650 ly_print_(&out, "ROOT config\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02009651 break;
9652 case LYXP_NODE_ROOT_STATE:
Michal Vasko5233e962020-08-14 14:26:20 +02009653 ly_print_(&out, "ROOT state\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02009654 break;
9655 case LYXP_NODE_ROOT_NOTIF:
Michal Vasko5233e962020-08-14 14:26:20 +02009656 ly_print_(&out, "ROOT notification \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009657 break;
9658 case LYXP_NODE_ROOT_RPC:
Michal Vasko5233e962020-08-14 14:26:20 +02009659 ly_print_(&out, "ROOT rpc \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009660 break;
9661 case LYXP_NODE_ROOT_OUTPUT:
Michal Vasko5233e962020-08-14 14:26:20 +02009662 ly_print_(&out, "ROOT output \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009663 break;
9664 case LYXP_NODE_ELEM:
Michal Vasko5233e962020-08-14 14:26:20 +02009665 ly_print_(&out, "ELEM \"%s\"\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009666 xml_print_node(&out, 1, set->value.nodes[i], 1, LYP_FORMAT);
Michal Vasko5233e962020-08-14 14:26:20 +02009667 ly_print_(&out, "\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02009668 break;
9669 case LYXP_NODE_TEXT:
Michal Vasko5233e962020-08-14 14:26:20 +02009670 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 +02009671 break;
9672 case LYXP_NODE_ATTR:
Michal Vasko5233e962020-08-14 14:26:20 +02009673 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 +02009674 break;
9675 }
9676 }
9677 break;
9678 }
9679}
9680
9681#endif
9682
9683LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009684lyxp_set_cast(struct lyxp_set *set, enum lyxp_set_type target)
Michal Vasko03ff5a72019-09-11 13:49:33 +02009685{
9686 long double num;
9687 char *str;
9688 LY_ERR rc;
9689
9690 if (!set || (set->type == target)) {
9691 return LY_SUCCESS;
9692 }
9693
9694 /* it's not possible to convert anything into a node set */
Michal Vaskod3678892020-05-21 10:06:58 +02009695 assert(target != LYXP_SET_NODE_SET);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009696
9697 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vaskod3678892020-05-21 10:06:58 +02009698 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009699 return LY_EINVAL;
9700 }
9701
9702 /* to STRING */
Michal Vaskod3678892020-05-21 10:06:58 +02009703 if ((target == LYXP_SET_STRING) || ((target == LYXP_SET_NUMBER) && (set->type == LYXP_SET_NODE_SET))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02009704 switch (set->type) {
9705 case LYXP_SET_NUMBER:
9706 if (isnan(set->val.num)) {
9707 set->val.str = strdup("NaN");
9708 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
9709 } else if ((set->val.num == 0) || (set->val.num == -0.0f)) {
9710 set->val.str = strdup("0");
9711 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
9712 } else if (isinf(set->val.num) && !signbit(set->val.num)) {
9713 set->val.str = strdup("Infinity");
9714 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
9715 } else if (isinf(set->val.num) && signbit(set->val.num)) {
9716 set->val.str = strdup("-Infinity");
9717 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
9718 } else if ((long long)set->val.num == set->val.num) {
9719 if (asprintf(&str, "%lld", (long long)set->val.num) == -1) {
9720 LOGMEM_RET(set->ctx);
9721 }
9722 set->val.str = str;
9723 } else {
9724 if (asprintf(&str, "%03.1Lf", set->val.num) == -1) {
9725 LOGMEM_RET(set->ctx);
9726 }
9727 set->val.str = str;
9728 }
9729 break;
9730 case LYXP_SET_BOOLEAN:
Michal Vasko004d3152020-06-11 19:59:22 +02009731 if (set->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02009732 set->val.str = strdup("true");
9733 } else {
9734 set->val.str = strdup("false");
9735 }
9736 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), LY_EMEM);
9737 break;
9738 case LYXP_SET_NODE_SET:
Michal Vasko03ff5a72019-09-11 13:49:33 +02009739 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009740 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02009741
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009742 rc = cast_node_set_to_string(set, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009743 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02009744 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009745 set->val.str = str;
9746 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009747 default:
9748 LOGINT_RET(set->ctx);
9749 }
9750 set->type = LYXP_SET_STRING;
9751 }
9752
9753 /* to NUMBER */
9754 if (target == LYXP_SET_NUMBER) {
9755 switch (set->type) {
9756 case LYXP_SET_STRING:
9757 num = cast_string_to_number(set->val.str);
Michal Vaskod3678892020-05-21 10:06:58 +02009758 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009759 set->val.num = num;
9760 break;
9761 case LYXP_SET_BOOLEAN:
Michal Vasko004d3152020-06-11 19:59:22 +02009762 if (set->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02009763 set->val.num = 1;
9764 } else {
9765 set->val.num = 0;
9766 }
9767 break;
9768 default:
9769 LOGINT_RET(set->ctx);
9770 }
9771 set->type = LYXP_SET_NUMBER;
9772 }
9773
9774 /* to BOOLEAN */
9775 if (target == LYXP_SET_BOOLEAN) {
9776 switch (set->type) {
9777 case LYXP_SET_NUMBER:
9778 if ((set->val.num == 0) || (set->val.num == -0.0f) || isnan(set->val.num)) {
Michal Vasko004d3152020-06-11 19:59:22 +02009779 set->val.bln = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009780 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02009781 set->val.bln = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009782 }
9783 break;
9784 case LYXP_SET_STRING:
9785 if (set->val.str[0]) {
Michal Vaskod3678892020-05-21 10:06:58 +02009786 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02009787 set->val.bln = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009788 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02009789 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02009790 set->val.bln = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009791 }
9792 break;
9793 case LYXP_SET_NODE_SET:
Michal Vaskod3678892020-05-21 10:06:58 +02009794 if (set->used) {
9795 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02009796 set->val.bln = 1;
Michal Vaskod3678892020-05-21 10:06:58 +02009797 } else {
9798 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02009799 set->val.bln = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02009800 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02009801 break;
9802 default:
9803 LOGINT_RET(set->ctx);
9804 }
9805 set->type = LYXP_SET_BOOLEAN;
9806 }
9807
Michal Vasko03ff5a72019-09-11 13:49:33 +02009808 return LY_SUCCESS;
9809}
9810
9811LY_ERR
Michal Vasko400e9672021-01-11 13:39:17 +01009812lyxp_atomize(const struct ly_ctx *ctx, const struct lyxp_expr *exp, const struct lys_module *cur_mod,
Michal Vaskoa3e92bc2022-07-29 14:56:23 +02009813 LY_VALUE_FORMAT format, void *prefix_data, const struct lysc_node *cur_scnode,
9814 const struct lysc_node *ctx_scnode, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02009815{
Radek Krejci2efc45b2020-12-22 16:25:44 +01009816 LY_ERR ret;
Michal Vaskodd528af2022-08-08 14:35:07 +02009817 uint32_t tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009818
Michal Vasko400e9672021-01-11 13:39:17 +01009819 LY_CHECK_ARG_RET(ctx, ctx, exp, set, LY_EINVAL);
Radek Krejci8df109d2021-04-23 12:19:08 +02009820 if (!cur_mod && ((format == LY_VALUE_SCHEMA) || (format == LY_VALUE_SCHEMA_RESOLVED))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009821 LOGARG(NULL, "Current module must be set if schema format is used.");
9822 return LY_EINVAL;
Michal Vasko004d3152020-06-11 19:59:22 +02009823 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02009824
9825 /* prepare set for evaluation */
Michal Vasko03ff5a72019-09-11 13:49:33 +02009826 memset(set, 0, sizeof *set);
9827 set->type = LYXP_SET_SCNODE_SET;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009828 set->root_type = lyxp_get_root_type(NULL, ctx_scnode, options);
Michal Vasko7333cb32022-07-29 16:30:29 +02009829 LY_CHECK_RET(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 +01009830 set->val.scnodes[0].in_ctx = LYXP_SET_SCNODE_START;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009831
Michal Vasko400e9672021-01-11 13:39:17 +01009832 set->ctx = (struct ly_ctx *)ctx;
Michal Vaskoa3e92bc2022-07-29 14:56:23 +02009833 set->cur_scnode = cur_scnode;
9834 for (set->context_op = cur_scnode;
Radek Krejci0f969882020-08-21 16:56:47 +02009835 set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF));
9836 set->context_op = set->context_op->parent) {}
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009837 set->cur_mod = cur_mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009838 set->format = format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009839 set->prefix_data = prefix_data;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009840
Radek Krejciddace2c2021-01-08 11:30:56 +01009841 LOG_LOCSET(set->cur_scnode, NULL, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01009842
Michal Vasko03ff5a72019-09-11 13:49:33 +02009843 /* evaluate */
Radek Krejci2efc45b2020-12-22 16:25:44 +01009844 ret = eval_expr_select(exp, &tok_idx, 0, set, options);
9845
Radek Krejciddace2c2021-01-08 11:30:56 +01009846 LOG_LOCBACK(1, 0, 0, 0);
Radek Krejci2efc45b2020-12-22 16:25:44 +01009847 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009848}
Michal Vaskod43d71a2020-08-07 14:54:58 +02009849
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01009850LIBYANG_API_DEF const char *
Michal Vaskod43d71a2020-08-07 14:54:58 +02009851lyxp_get_expr(const struct lyxp_expr *path)
9852{
9853 if (!path) {
9854 return NULL;
9855 }
9856
9857 return path->expr;
9858}