blob: 31843505af0290382abb6aea3f0609c9b8584534 [file] [log] [blame]
Radek Krejcib1646a92018-11-02 16:08:26 +01001/**
2 * @file xpath.c
3 * @author Michal Vasko <mvasko@cesnet.cz>
4 * @brief YANG XPath evaluation functions
5 *
Michal Vasko49fec8e2022-05-24 10:28:33 +02006 * Copyright (c) 2015 - 2022 CESNET, z.s.p.o.
Radek Krejcib1646a92018-11-02 16:08:26 +01007 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
Christian Hopps32874e12021-05-01 09:43:54 -040014#define _GNU_SOURCE /* asprintf, strdup */
Radek Krejcib1646a92018-11-02 16:08:26 +010015
Radek Krejci535ea9f2020-05-29 16:01:05 +020016#include "xpath.h"
Radek Krejcib1646a92018-11-02 16:08:26 +010017
Radek Krejci535ea9f2020-05-29 16:01:05 +020018#include <assert.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010019#include <ctype.h>
Radek Krejci535ea9f2020-05-29 16:01:05 +020020#include <errno.h>
Radek Krejci535ea9f2020-05-29 16:01:05 +020021#include <math.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020022#include <stdint.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010023#include <stdio.h>
24#include <stdlib.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010025#include <string.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010026
Radek Krejci535ea9f2020-05-29 16:01:05 +020027#include "common.h"
Michal Vasko5aa44c02020-06-29 11:47:02 +020028#include "compat.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020029#include "context.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020030#include "dict.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020031#include "hash_table.h"
Radek Krejci47fab892020-11-05 17:02:41 +010032#include "out.h"
Radek Krejci7931b192020-06-25 17:05:03 +020033#include "parser_data.h"
Michal Vasko004d3152020-06-11 19:59:22 +020034#include "path.h"
Michal Vaskob4750962022-10-06 15:33:35 +020035#include "plugins_exts/metadata.h"
Michal Vasko03ff5a72019-09-11 13:49:33 +020036#include "plugins_types.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020037#include "printer_data.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020038#include "schema_compile_node.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020039#include "tree.h"
Radek Krejci77114102021-03-10 15:21:57 +010040#include "tree_data.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020041#include "tree_data_internal.h"
Radek Krejci859a15a2021-03-05 20:56:59 +010042#include "tree_edit.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020043#include "tree_schema_internal.h"
44#include "xml.h"
Michal Vasko03ff5a72019-09-11 13:49:33 +020045
Michal Vasko7333cb32022-07-29 16:30:29 +020046static LY_ERR set_scnode_insert_node(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type,
47 enum lyxp_axis axis, uint32_t *index_p);
Michal Vaskodd528af2022-08-08 14:35:07 +020048static LY_ERR reparse_or_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint32_t *tok_idx, uint32_t depth);
49static 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 +020050 struct lyxp_set *set, uint32_t options);
Michal Vaskofe1af042022-07-29 14:58:59 +020051static LY_ERR moveto_resolve_model(const char **qname, uint32_t *qname_len, const struct lyxp_set *set,
Michal Vasko93923692021-05-07 15:28:02 +020052 const struct lysc_node *ctx_scnode, const struct lys_module **moveto_mod);
Michal Vasko47da6562022-07-14 15:43:15 +020053static LY_ERR moveto_axis_node_next(const struct lyd_node **iter, enum lyxp_node_type *iter_type,
54 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 +020055static LY_ERR moveto_node(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname,
56 enum lyxp_axis axis, uint32_t options);
57static LY_ERR moveto_scnode(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname,
58 enum lyxp_axis axis, uint32_t options);
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +020059static 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 +020060
aPiecek96dc1e32021-10-08 15:45:59 +020061/* Functions are divided into the following basic classes:
62 *
63 * (re)parse functions:
64 * Parse functions parse the expression into
65 * tokens (syntactic analysis).
66 * Reparse functions perform semantic analysis
67 * (do not save the result, just a check) of
68 * the expression and fill repeat indices.
69 *
70 * warn functions:
71 * Warn functions check specific reasonable conditions for schema XPath
72 * and print a warning if they are not satisfied.
73 *
74 * moveto functions:
75 * They and only they actually change the context (set).
76 *
77 * eval functions:
78 * They execute a parsed XPath expression on some data subtree.
79 */
80
Michal Vasko03ff5a72019-09-11 13:49:33 +020081/**
82 * @brief Print the type of an XPath \p set.
83 *
84 * @param[in] set Set to use.
85 * @return Set type string.
86 */
87static const char *
88print_set_type(struct lyxp_set *set)
89{
90 switch (set->type) {
Michal Vasko03ff5a72019-09-11 13:49:33 +020091 case LYXP_SET_NODE_SET:
92 return "node set";
93 case LYXP_SET_SCNODE_SET:
94 return "schema node set";
95 case LYXP_SET_BOOLEAN:
96 return "boolean";
97 case LYXP_SET_NUMBER:
98 return "number";
99 case LYXP_SET_STRING:
100 return "string";
101 }
102
103 return NULL;
104}
105
Michal Vasko24cddf82020-06-01 08:17:01 +0200106const char *
Michal Vasko49fec8e2022-05-24 10:28:33 +0200107lyxp_token2str(enum lyxp_token tok)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200108{
109 switch (tok) {
110 case LYXP_TOKEN_PAR1:
111 return "(";
112 case LYXP_TOKEN_PAR2:
113 return ")";
114 case LYXP_TOKEN_BRACK1:
115 return "[";
116 case LYXP_TOKEN_BRACK2:
117 return "]";
118 case LYXP_TOKEN_DOT:
119 return ".";
120 case LYXP_TOKEN_DDOT:
121 return "..";
122 case LYXP_TOKEN_AT:
123 return "@";
124 case LYXP_TOKEN_COMMA:
125 return ",";
Michal Vaskoe0a8ce72023-04-04 08:30:44 +0200126 case LYXP_TOKEN_DCOLON:
127 return "::";
Michal Vasko03ff5a72019-09-11 13:49:33 +0200128 case LYXP_TOKEN_NAMETEST:
129 return "NameTest";
130 case LYXP_TOKEN_NODETYPE:
131 return "NodeType";
aPiecekfba75362021-10-07 12:39:48 +0200132 case LYXP_TOKEN_VARREF:
133 return "VariableReference";
Michal Vasko03ff5a72019-09-11 13:49:33 +0200134 case LYXP_TOKEN_FUNCNAME:
135 return "FunctionName";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200136 case LYXP_TOKEN_OPER_LOG:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200137 return "Operator(Logic)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200138 case LYXP_TOKEN_OPER_EQUAL:
139 return "Operator(Equal)";
140 case LYXP_TOKEN_OPER_NEQUAL:
141 return "Operator(Non-equal)";
142 case LYXP_TOKEN_OPER_COMP:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200143 return "Operator(Comparison)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200144 case LYXP_TOKEN_OPER_MATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200145 return "Operator(Math)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200146 case LYXP_TOKEN_OPER_UNI:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200147 return "Operator(Union)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200148 case LYXP_TOKEN_OPER_PATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200149 return "Operator(Path)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200150 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko14676352020-05-29 11:35:55 +0200151 return "Operator(Recursive Path)";
Michal Vaskoe0a8ce72023-04-04 08:30:44 +0200152 case LYXP_TOKEN_AXISNAME:
153 return "AxisName";
Michal Vasko03ff5a72019-09-11 13:49:33 +0200154 case LYXP_TOKEN_LITERAL:
155 return "Literal";
156 case LYXP_TOKEN_NUMBER:
157 return "Number";
158 default:
159 LOGINT(NULL);
160 return "";
161 }
162}
163
164/**
Michal Vasko49fec8e2022-05-24 10:28:33 +0200165 * @brief Transform string into an axis.
166 *
167 * @param[in] str String to transform.
168 * @param[in] str_len Length of @p str.
169 * @return Transformed axis.
170 */
171static enum lyxp_axis
Michal Vaskodd528af2022-08-08 14:35:07 +0200172str2axis(const char *str, uint32_t str_len)
Michal Vasko49fec8e2022-05-24 10:28:33 +0200173{
174 switch (str_len) {
175 case 4:
176 assert(!strncmp("self", str, str_len));
177 return LYXP_AXIS_SELF;
178 case 5:
179 assert(!strncmp("child", str, str_len));
180 return LYXP_AXIS_CHILD;
181 case 6:
182 assert(!strncmp("parent", str, str_len));
183 return LYXP_AXIS_PARENT;
184 case 8:
185 assert(!strncmp("ancestor", str, str_len));
186 return LYXP_AXIS_ANCESTOR;
187 case 9:
188 if (str[0] == 'a') {
189 assert(!strncmp("attribute", str, str_len));
190 return LYXP_AXIS_ATTRIBUTE;
191 } else if (str[0] == 'f') {
192 assert(!strncmp("following", str, str_len));
193 return LYXP_AXIS_FOLLOWING;
194 } else {
195 assert(!strncmp("preceding", str, str_len));
196 return LYXP_AXIS_PRECEDING;
197 }
198 break;
199 case 10:
200 assert(!strncmp("descendant", str, str_len));
201 return LYXP_AXIS_DESCENDANT;
202 case 16:
203 assert(!strncmp("ancestor-or-self", str, str_len));
204 return LYXP_AXIS_ANCESTOR_OR_SELF;
205 case 17:
206 if (str[0] == 'f') {
207 assert(!strncmp("following-sibling", str, str_len));
208 return LYXP_AXIS_FOLLOWING_SIBLING;
209 } else {
210 assert(!strncmp("preceding-sibling", str, str_len));
211 return LYXP_AXIS_PRECEDING_SIBLING;
212 }
213 break;
214 case 18:
215 assert(!strncmp("descendant-or-self", str, str_len));
216 return LYXP_AXIS_DESCENDANT_OR_SELF;
217 }
218
219 LOGINT(NULL);
220 return 0;
221}
222
223/**
Michal Vasko03ff5a72019-09-11 13:49:33 +0200224 * @brief Print the whole expression \p exp to debug output.
225 *
226 * @param[in] exp Expression to use.
227 */
228static void
Michal Vasko40308e72020-10-20 16:38:40 +0200229print_expr_struct_debug(const struct lyxp_expr *exp)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200230{
Radek Krejcif13b87b2020-12-01 22:02:17 +0100231#define MSG_BUFFER_SIZE 128
232 char tmp[MSG_BUFFER_SIZE];
Michal Vasko1fdd8fa2021-01-08 09:21:45 +0100233 uint32_t i, j;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200234
Radek Krejci52b6d512020-10-12 12:33:17 +0200235 if (!exp || (ly_ll < LY_LLDBG)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200236 return;
237 }
238
239 LOGDBG(LY_LDGXPATH, "expression \"%s\":", exp->expr);
240 for (i = 0; i < exp->used; ++i) {
Michal Vasko49fec8e2022-05-24 10:28:33 +0200241 sprintf(tmp, "\ttoken %s, in expression \"%.*s\"", lyxp_token2str(exp->tokens[i]), exp->tok_len[i],
Michal Vasko69730152020-10-09 16:30:07 +0200242 &exp->expr[exp->tok_pos[i]]);
Michal Vasko23049552021-03-04 15:57:44 +0100243 if (exp->repeat && exp->repeat[i]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200244 sprintf(tmp + strlen(tmp), " (repeat %d", exp->repeat[i][0]);
245 for (j = 1; exp->repeat[i][j]; ++j) {
246 sprintf(tmp + strlen(tmp), ", %d", exp->repeat[i][j]);
247 }
248 strcat(tmp, ")");
249 }
250 LOGDBG(LY_LDGXPATH, tmp);
251 }
Radek Krejcif13b87b2020-12-01 22:02:17 +0100252#undef MSG_BUFFER_SIZE
Michal Vasko03ff5a72019-09-11 13:49:33 +0200253}
254
255#ifndef NDEBUG
256
257/**
258 * @brief Print XPath set content to debug output.
259 *
260 * @param[in] set Set to print.
261 */
262static void
263print_set_debug(struct lyxp_set *set)
264{
265 uint32_t i;
266 char *str;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200267 struct lyxp_set_node *item;
268 struct lyxp_set_scnode *sitem;
269
Radek Krejci52b6d512020-10-12 12:33:17 +0200270 if (ly_ll < LY_LLDBG) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200271 return;
272 }
273
274 switch (set->type) {
275 case LYXP_SET_NODE_SET:
276 LOGDBG(LY_LDGXPATH, "set NODE SET:");
277 for (i = 0; i < set->used; ++i) {
278 item = &set->val.nodes[i];
279
280 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +0100281 case LYXP_NODE_NONE:
282 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): NONE", i + 1, item->pos);
283 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200284 case LYXP_NODE_ROOT:
285 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT", i + 1, item->pos);
286 break;
287 case LYXP_NODE_ROOT_CONFIG:
288 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT CONFIG", i + 1, item->pos);
289 break;
290 case LYXP_NODE_ELEM:
Michal Vasko9e685082021-01-29 14:49:09 +0100291 if ((item->node->schema->nodetype == LYS_LIST) && (lyd_child(item->node)->schema->nodetype == LYS_LEAF)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200292 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (1st child val: %s)", i + 1, item->pos,
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200293 item->node->schema->name, lyd_get_value(lyd_child(item->node)));
Michal Vasko9e685082021-01-29 14:49:09 +0100294 } else if (item->node->schema->nodetype == LYS_LEAFLIST) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200295 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (val: %s)", i + 1, item->pos,
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200296 item->node->schema->name, lyd_get_value(item->node));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200297 } else {
298 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s", i + 1, item->pos, item->node->schema->name);
299 }
300 break;
301 case LYXP_NODE_TEXT:
302 if (item->node->schema->nodetype & LYS_ANYDATA) {
303 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): TEXT <%s>", i + 1, item->pos,
Michal Vasko69730152020-10-09 16:30:07 +0200304 item->node->schema->nodetype == LYS_ANYXML ? "anyxml" : "anydata");
Michal Vasko03ff5a72019-09-11 13:49:33 +0200305 } else {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200306 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 +0200307 }
308 break;
Michal Vasko9f96a052020-03-10 09:41:45 +0100309 case LYXP_NODE_META:
310 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 +0200311 set->val.meta[i].meta->value);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200312 break;
313 }
314 }
315 break;
316
317 case LYXP_SET_SCNODE_SET:
318 LOGDBG(LY_LDGXPATH, "set SCNODE SET:");
319 for (i = 0; i < set->used; ++i) {
320 sitem = &set->val.scnodes[i];
321
322 switch (sitem->type) {
323 case LYXP_NODE_ROOT:
324 LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT", i + 1, sitem->in_ctx);
325 break;
326 case LYXP_NODE_ROOT_CONFIG:
327 LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT CONFIG", i + 1, sitem->in_ctx);
328 break;
329 case LYXP_NODE_ELEM:
330 LOGDBG(LY_LDGXPATH, "\t%d (%u): ELEM %s", i + 1, sitem->in_ctx, sitem->scnode->name);
331 break;
332 default:
333 LOGINT(NULL);
334 break;
335 }
336 }
337 break;
338
Michal Vasko03ff5a72019-09-11 13:49:33 +0200339 case LYXP_SET_BOOLEAN:
340 LOGDBG(LY_LDGXPATH, "set BOOLEAN");
Michal Vasko004d3152020-06-11 19:59:22 +0200341 LOGDBG(LY_LDGXPATH, "\t%s", (set->val.bln ? "true" : "false"));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200342 break;
343
344 case LYXP_SET_STRING:
345 LOGDBG(LY_LDGXPATH, "set STRING");
346 LOGDBG(LY_LDGXPATH, "\t%s", set->val.str);
347 break;
348
349 case LYXP_SET_NUMBER:
350 LOGDBG(LY_LDGXPATH, "set NUMBER");
351
352 if (isnan(set->val.num)) {
353 str = strdup("NaN");
354 } else if ((set->val.num == 0) || (set->val.num == -0.0f)) {
355 str = strdup("0");
356 } else if (isinf(set->val.num) && !signbit(set->val.num)) {
357 str = strdup("Infinity");
358 } else if (isinf(set->val.num) && signbit(set->val.num)) {
359 str = strdup("-Infinity");
360 } else if ((long long)set->val.num == set->val.num) {
361 if (asprintf(&str, "%lld", (long long)set->val.num) == -1) {
362 str = NULL;
363 }
364 } else {
365 if (asprintf(&str, "%03.1Lf", set->val.num) == -1) {
366 str = NULL;
367 }
368 }
369 LY_CHECK_ERR_RET(!str, LOGMEM(NULL), );
370
371 LOGDBG(LY_LDGXPATH, "\t%s", str);
372 free(str);
373 }
374}
375
376#endif
377
378/**
379 * @brief Realloc the string \p str.
380 *
381 * @param[in] ctx libyang context for logging.
382 * @param[in] needed How much free space is required.
383 * @param[in,out] str Pointer to the string to use.
384 * @param[in,out] used Used bytes in \p str.
385 * @param[in,out] size Allocated bytes in \p str.
386 * @return LY_ERR
387 */
388static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +0200389cast_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 +0200390{
Michal Vasko2291ab92022-08-08 08:53:12 +0200391 if (*size - (unsigned)*used < needed) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200392 do {
Michal Vaskodd528af2022-08-08 14:35:07 +0200393 if ((UINT32_MAX - *size) < LYXP_STRING_CAST_SIZE_STEP) {
394 LOGERR(ctx, LY_EINVAL, "XPath string length limit (%" PRIu32 ") reached.", UINT32_MAX);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200395 return LY_EINVAL;
396 }
397 *size += LYXP_STRING_CAST_SIZE_STEP;
Michal Vasko2291ab92022-08-08 08:53:12 +0200398 } while (*size - (unsigned)*used < needed);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200399 *str = ly_realloc(*str, *size * sizeof(char));
400 LY_CHECK_ERR_RET(!(*str), LOGMEM(ctx), LY_EMEM);
401 }
402
403 return LY_SUCCESS;
404}
405
406/**
407 * @brief Cast nodes recursively to one string @p str.
408 *
Michal Vasko47da6562022-07-14 15:43:15 +0200409 * @param[in] node Node to cast, NULL if root.
410 * @param[in] set XPath set.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200411 * @param[in] indent Current indent.
412 * @param[in,out] str Resulting string.
413 * @param[in,out] used Used bytes in @p str.
414 * @param[in,out] size Allocated bytes in @p str.
Michal Vasko47da6562022-07-14 15:43:15 +0200415 * @return LY_ERR value.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200416 */
417static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +0200418cast_string_recursive(const struct lyd_node *node, struct lyxp_set *set, uint32_t indent, char **str, uint32_t *used,
419 uint32_t *size)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200420{
Radek Krejci7f769d72020-07-11 23:13:56 +0200421 char *buf, *line, *ptr = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200422 const char *value_str;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200423 const struct lyd_node *child;
Michal Vasko47da6562022-07-14 15:43:15 +0200424 enum lyxp_node_type child_type;
Michal Vasko60ea6352020-06-29 13:39:39 +0200425 struct lyd_node *tree;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200426 struct lyd_node_any *any;
427 LY_ERR rc;
428
Michal Vasko47da6562022-07-14 15:43:15 +0200429 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && node && (node->schema->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200430 return LY_SUCCESS;
431 }
432
Michal Vasko47da6562022-07-14 15:43:15 +0200433 if (!node) {
434 /* fake container */
435 LY_CHECK_RET(cast_string_realloc(set->ctx, 1, str, used, size));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200436 strcpy(*str + (*used - 1), "\n");
437 ++(*used);
438
439 ++indent;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200440
Michal Vasko47da6562022-07-14 15:43:15 +0200441 /* print all the top-level nodes */
442 child = NULL;
443 child_type = 0;
444 while (!moveto_axis_node_next(&child, &child_type, NULL, set->root_type, LYXP_AXIS_CHILD, set)) {
445 LY_CHECK_RET(cast_string_recursive(child, set, indent, str, used, size));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200446 }
447
Michal Vasko47da6562022-07-14 15:43:15 +0200448 /* end fake container */
449 LY_CHECK_RET(cast_string_realloc(set->ctx, 1, str, used, size));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200450 strcpy(*str + (*used - 1), "\n");
451 ++(*used);
452
453 --indent;
Michal Vasko47da6562022-07-14 15:43:15 +0200454 } else {
455 switch (node->schema->nodetype) {
456 case LYS_CONTAINER:
457 case LYS_LIST:
458 case LYS_RPC:
459 case LYS_NOTIF:
460 LY_CHECK_RET(cast_string_realloc(set->ctx, 1, str, used, size));
461 strcpy(*str + (*used - 1), "\n");
462 ++(*used);
463
464 for (child = lyd_child(node); child; child = child->next) {
465 LY_CHECK_RET(cast_string_recursive(child, set, indent + 1, str, used, size));
466 }
467
468 break;
469
470 case LYS_LEAF:
471 case LYS_LEAFLIST:
472 value_str = lyd_get_value(node);
473
474 /* print indent */
475 LY_CHECK_RET(cast_string_realloc(set->ctx, indent * 2 + strlen(value_str) + 1, str, used, size));
476 memset(*str + (*used - 1), ' ', indent * 2);
477 *used += indent * 2;
478
479 /* print value */
480 if (*used == 1) {
481 sprintf(*str + (*used - 1), "%s", value_str);
482 *used += strlen(value_str);
483 } else {
484 sprintf(*str + (*used - 1), "%s\n", value_str);
485 *used += strlen(value_str) + 1;
486 }
487
488 break;
489
490 case LYS_ANYXML:
491 case LYS_ANYDATA:
492 any = (struct lyd_node_any *)node;
493 if (!(void *)any->value.tree) {
494 /* no content */
495 buf = strdup("");
496 LY_CHECK_ERR_RET(!buf, LOGMEM(set->ctx), LY_EMEM);
497 } else {
498 struct ly_out *out;
499
500 if (any->value_type == LYD_ANYDATA_LYB) {
501 /* try to parse it into a data tree */
502 if (lyd_parse_data_mem((struct ly_ctx *)set->ctx, any->value.mem, LYD_LYB,
503 LYD_PARSE_ONLY | LYD_PARSE_STRICT, 0, &tree) == LY_SUCCESS) {
504 /* successfully parsed */
505 free(any->value.mem);
506 any->value.tree = tree;
507 any->value_type = LYD_ANYDATA_DATATREE;
508 }
509 /* error is covered by the following switch where LYD_ANYDATA_LYB causes failure */
510 }
511
512 switch (any->value_type) {
513 case LYD_ANYDATA_STRING:
514 case LYD_ANYDATA_XML:
515 case LYD_ANYDATA_JSON:
516 buf = strdup(any->value.json);
517 LY_CHECK_ERR_RET(!buf, LOGMEM(set->ctx), LY_EMEM);
518 break;
519 case LYD_ANYDATA_DATATREE:
520 LY_CHECK_RET(ly_out_new_memory(&buf, 0, &out));
521 rc = lyd_print_all(out, any->value.tree, LYD_XML, 0);
522 ly_out_free(out, NULL, 0);
523 LY_CHECK_RET(rc < 0, -rc);
524 break;
525 case LYD_ANYDATA_LYB:
526 LOGERR(set->ctx, LY_EINVAL, "Cannot convert LYB anydata into string.");
527 return LY_EINVAL;
528 }
529 }
530
531 line = strtok_r(buf, "\n", &ptr);
532 do {
533 rc = cast_string_realloc(set->ctx, indent * 2 + strlen(line) + 1, str, used, size);
534 if (rc != LY_SUCCESS) {
535 free(buf);
536 return rc;
537 }
538 memset(*str + (*used - 1), ' ', indent * 2);
539 *used += indent * 2;
540
541 strcpy(*str + (*used - 1), line);
542 *used += strlen(line);
543
544 strcpy(*str + (*used - 1), "\n");
545 *used += 1;
546 } while ((line = strtok_r(NULL, "\n", &ptr)));
547
548 free(buf);
549 break;
550
551 default:
552 LOGINT_RET(set->ctx);
553 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200554 }
555
556 return LY_SUCCESS;
557}
558
559/**
560 * @brief Cast an element into a string.
561 *
Michal Vasko47da6562022-07-14 15:43:15 +0200562 * @param[in] node Node to cast, NULL if root.
563 * @param[in] set XPath set.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200564 * @param[out] str Element cast to dynamically-allocated string.
565 * @return LY_ERR
566 */
567static LY_ERR
Michal Vasko47da6562022-07-14 15:43:15 +0200568cast_string_elem(const struct lyd_node *node, struct lyxp_set *set, char **str)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200569{
Michal Vaskodd528af2022-08-08 14:35:07 +0200570 uint32_t used, size;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200571 LY_ERR rc;
572
573 *str = malloc(LYXP_STRING_CAST_SIZE_START * sizeof(char));
Michal Vasko47da6562022-07-14 15:43:15 +0200574 LY_CHECK_ERR_RET(!*str, LOGMEM(set->ctx), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200575 (*str)[0] = '\0';
576 used = 1;
577 size = LYXP_STRING_CAST_SIZE_START;
578
Michal Vasko47da6562022-07-14 15:43:15 +0200579 rc = cast_string_recursive(node, set, 0, str, &used, &size);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200580 if (rc != LY_SUCCESS) {
581 free(*str);
582 return rc;
583 }
584
585 if (size > used) {
586 *str = ly_realloc(*str, used * sizeof(char));
Michal Vasko47da6562022-07-14 15:43:15 +0200587 LY_CHECK_ERR_RET(!*str, LOGMEM(set->ctx), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200588 }
589 return LY_SUCCESS;
590}
591
592/**
593 * @brief Cast a LYXP_SET_NODE_SET set into a string.
594 * Context position aware.
595 *
596 * @param[in] set Set to cast.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200597 * @param[out] str Cast dynamically-allocated string.
598 * @return LY_ERR
599 */
600static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100601cast_node_set_to_string(struct lyxp_set *set, char **str)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200602{
Michal Vaskoaa677062021-03-09 13:52:53 +0100603 if (!set->used) {
604 *str = strdup("");
605 if (!*str) {
606 LOGMEM_RET(set->ctx);
607 }
608 return LY_SUCCESS;
609 }
610
Michal Vasko03ff5a72019-09-11 13:49:33 +0200611 switch (set->val.nodes[0].type) {
Michal Vasko2caefc12019-11-14 16:07:56 +0100612 case LYXP_NODE_NONE:
613 /* invalid */
614 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200615 case LYXP_NODE_ROOT:
616 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200617 case LYXP_NODE_ELEM:
618 case LYXP_NODE_TEXT:
Michal Vasko47da6562022-07-14 15:43:15 +0200619 return cast_string_elem(set->val.nodes[0].node, set, str);
Michal Vasko9f96a052020-03-10 09:41:45 +0100620 case LYXP_NODE_META:
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200621 *str = strdup(lyd_get_meta_value(set->val.meta[0].meta));
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200622 if (!*str) {
623 LOGMEM_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200624 }
625 return LY_SUCCESS;
626 }
627
628 LOGINT_RET(set->ctx);
629}
630
631/**
632 * @brief Cast a string into an XPath number.
633 *
634 * @param[in] str String to use.
635 * @return Cast number.
636 */
637static long double
638cast_string_to_number(const char *str)
639{
640 long double num;
641 char *ptr;
642
643 errno = 0;
644 num = strtold(str, &ptr);
645 if (errno || *ptr) {
646 num = NAN;
647 }
648 return num;
649}
650
651/**
652 * @brief Callback for checking value equality.
653 *
Michal Vasko62524a92021-02-26 10:08:50 +0100654 * Implementation of ::lyht_value_equal_cb.
Radek Krejci857189e2020-09-01 13:26:36 +0200655 *
Michal Vasko03ff5a72019-09-11 13:49:33 +0200656 * @param[in] val1_p First value.
657 * @param[in] val2_p Second value.
658 * @param[in] mod Whether hash table is being modified.
659 * @param[in] cb_data Callback data.
Radek Krejci857189e2020-09-01 13:26:36 +0200660 * @return Boolean value whether values are equal or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200661 */
Radek Krejci857189e2020-09-01 13:26:36 +0200662static ly_bool
663set_values_equal_cb(void *val1_p, void *val2_p, ly_bool UNUSED(mod), void *UNUSED(cb_data))
Michal Vasko03ff5a72019-09-11 13:49:33 +0200664{
665 struct lyxp_set_hash_node *val1, *val2;
666
667 val1 = (struct lyxp_set_hash_node *)val1_p;
668 val2 = (struct lyxp_set_hash_node *)val2_p;
669
670 if ((val1->node == val2->node) && (val1->type == val2->type)) {
671 return 1;
672 }
673
674 return 0;
675}
676
677/**
678 * @brief Insert node and its hash into set.
679 *
680 * @param[in] set et to insert to.
681 * @param[in] node Node with hash.
682 * @param[in] type Node type.
683 */
684static void
685set_insert_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type)
686{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200687 LY_ERR r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200688 uint32_t i, hash;
689 struct lyxp_set_hash_node hnode;
690
691 if (!set->ht && (set->used >= LYD_HT_MIN_ITEMS)) {
692 /* create hash table and add all the nodes */
693 set->ht = lyht_new(1, sizeof(struct lyxp_set_hash_node), set_values_equal_cb, NULL, 1);
694 for (i = 0; i < set->used; ++i) {
695 hnode.node = set->val.nodes[i].node;
696 hnode.type = set->val.nodes[i].type;
697
Michal Vaskoae130f52023-04-20 14:25:16 +0200698 hash = lyht_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
699 hash = lyht_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
700 hash = lyht_hash_multi(hash, NULL, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200701
702 r = lyht_insert(set->ht, &hnode, hash, NULL);
703 assert(!r);
704 (void)r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200705
Michal Vasko9d6befd2019-12-11 14:56:56 +0100706 if (hnode.node == node) {
707 /* it was just added, do not add it twice */
708 node = NULL;
709 }
710 }
711 }
712
713 if (set->ht && node) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200714 /* add the new node into hash table */
715 hnode.node = node;
716 hnode.type = type;
717
Michal Vaskoae130f52023-04-20 14:25:16 +0200718 hash = lyht_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
719 hash = lyht_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
720 hash = lyht_hash_multi(hash, NULL, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200721
722 r = lyht_insert(set->ht, &hnode, hash, NULL);
723 assert(!r);
724 (void)r;
725 }
726}
727
728/**
729 * @brief Remove node and its hash from set.
730 *
731 * @param[in] set Set to remove from.
732 * @param[in] node Node to remove.
733 * @param[in] type Node type.
734 */
735static void
736set_remove_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type)
737{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200738 LY_ERR r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200739 struct lyxp_set_hash_node hnode;
740 uint32_t hash;
741
742 if (set->ht) {
743 hnode.node = node;
744 hnode.type = type;
745
Michal Vaskoae130f52023-04-20 14:25:16 +0200746 hash = lyht_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
747 hash = lyht_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
748 hash = lyht_hash_multi(hash, NULL, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200749
750 r = lyht_remove(set->ht, &hnode, hash);
751 assert(!r);
752 (void)r;
753
754 if (!set->ht->used) {
Michal Vasko77b7f90a2023-01-31 15:42:41 +0100755 lyht_free(set->ht, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200756 set->ht = NULL;
757 }
758 }
759}
760
761/**
762 * @brief Check whether node is in set based on its hash.
763 *
764 * @param[in] set Set to search in.
765 * @param[in] node Node to search for.
766 * @param[in] type Node type.
767 * @param[in] skip_idx Index in @p set to skip.
768 * @return LY_ERR
769 */
770static LY_ERR
771set_dup_node_hash_check(const struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type, int skip_idx)
772{
773 struct lyxp_set_hash_node hnode, *match_p;
774 uint32_t hash;
775
776 hnode.node = node;
777 hnode.type = type;
778
Michal Vaskoae130f52023-04-20 14:25:16 +0200779 hash = lyht_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
780 hash = lyht_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
781 hash = lyht_hash_multi(hash, NULL, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200782
783 if (!lyht_find(set->ht, &hnode, hash, (void **)&match_p)) {
784 if ((skip_idx > -1) && (set->val.nodes[skip_idx].node == match_p->node) && (set->val.nodes[skip_idx].type == match_p->type)) {
785 /* we found it on the index that should be skipped, find another */
786 hnode = *match_p;
787 if (lyht_find_next(set->ht, &hnode, hash, (void **)&match_p)) {
788 /* none other found */
789 return LY_SUCCESS;
790 }
791 }
792
793 return LY_EEXIST;
794 }
795
796 /* not found */
797 return LY_SUCCESS;
798}
799
Michal Vaskod3678892020-05-21 10:06:58 +0200800void
801lyxp_set_free_content(struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200802{
803 if (!set) {
804 return;
805 }
806
807 if (set->type == LYXP_SET_NODE_SET) {
808 free(set->val.nodes);
Michal Vasko77b7f90a2023-01-31 15:42:41 +0100809 lyht_free(set->ht, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200810 } else if (set->type == LYXP_SET_SCNODE_SET) {
811 free(set->val.scnodes);
Michal Vasko77b7f90a2023-01-31 15:42:41 +0100812 lyht_free(set->ht, NULL);
Michal Vaskod3678892020-05-21 10:06:58 +0200813 } else {
814 if (set->type == LYXP_SET_STRING) {
815 free(set->val.str);
816 }
817 set->type = LYXP_SET_NODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200818 }
Michal Vaskod3678892020-05-21 10:06:58 +0200819
820 set->val.nodes = NULL;
821 set->used = 0;
822 set->size = 0;
823 set->ht = NULL;
824 set->ctx_pos = 0;
aPiecek748da732021-06-01 09:56:43 +0200825 set->ctx_size = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200826}
827
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100828/**
829 * @brief Free dynamically-allocated set.
830 *
831 * @param[in] set Set to free.
832 */
833static void
Michal Vasko03ff5a72019-09-11 13:49:33 +0200834lyxp_set_free(struct lyxp_set *set)
835{
836 if (!set) {
837 return;
838 }
839
Michal Vaskod3678892020-05-21 10:06:58 +0200840 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200841 free(set);
842}
843
844/**
845 * @brief Initialize set context.
846 *
847 * @param[in] new Set to initialize.
848 * @param[in] set Arbitrary initialized set.
849 */
850static void
Michal Vasko4c7763f2020-07-27 17:40:37 +0200851set_init(struct lyxp_set *new, const struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200852{
853 memset(new, 0, sizeof *new);
Michal Vasko02a77382019-09-12 11:47:35 +0200854 if (set) {
Michal Vasko306e2832022-07-25 09:15:17 +0200855 new->non_child_axis = set->non_child_axis;
Michal Vasko02a77382019-09-12 11:47:35 +0200856 new->ctx = set->ctx;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200857 new->cur_node = set->cur_node;
Michal Vasko588112f2019-12-10 14:51:53 +0100858 new->root_type = set->root_type;
Michal Vasko6b26e742020-07-17 15:02:10 +0200859 new->context_op = set->context_op;
Michal Vaskof03ed032020-03-04 13:31:44 +0100860 new->tree = set->tree;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200861 new->cur_mod = set->cur_mod;
Michal Vasko02a77382019-09-12 11:47:35 +0200862 new->format = set->format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200863 new->prefix_data = set->prefix_data;
aPiecekfba75362021-10-07 12:39:48 +0200864 new->vars = set->vars;
Michal Vasko02a77382019-09-12 11:47:35 +0200865 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200866}
867
868/**
869 * @brief Create a deep copy of a set.
870 *
871 * @param[in] set Set to copy.
872 * @return Copy of @p set.
873 */
874static struct lyxp_set *
875set_copy(struct lyxp_set *set)
876{
877 struct lyxp_set *ret;
Michal Vasko1fdd8fa2021-01-08 09:21:45 +0100878 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200879
880 if (!set) {
881 return NULL;
882 }
883
884 ret = malloc(sizeof *ret);
885 LY_CHECK_ERR_RET(!ret, LOGMEM(set->ctx), NULL);
886 set_init(ret, set);
887
888 if (set->type == LYXP_SET_SCNODE_SET) {
889 ret->type = set->type;
890
891 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100892 if ((set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) ||
893 (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +0200894 uint32_t idx;
Michal Vasko26bbb272022-08-02 14:54:33 +0200895
Michal Vasko7333cb32022-07-29 16:30:29 +0200896 LY_CHECK_ERR_RET(set_scnode_insert_node(ret, set->val.scnodes[i].scnode, set->val.scnodes[i].type,
897 set->val.scnodes[i].axis, &idx), lyxp_set_free(ret), NULL);
Michal Vasko3f27c522020-01-06 08:37:49 +0100898 /* coverity seems to think scnodes can be NULL */
Radek Krejciaa6b53f2020-08-27 15:19:03 +0200899 if (!ret->val.scnodes) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200900 lyxp_set_free(ret);
901 return NULL;
902 }
Michal Vaskoba716542019-12-16 10:01:58 +0100903 ret->val.scnodes[idx].in_ctx = set->val.scnodes[i].in_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200904 }
905 }
906 } else if (set->type == LYXP_SET_NODE_SET) {
907 ret->type = set->type;
Michal Vasko08e9b112021-06-11 15:41:17 +0200908 if (set->used) {
909 ret->val.nodes = malloc(set->used * sizeof *ret->val.nodes);
910 LY_CHECK_ERR_RET(!ret->val.nodes, LOGMEM(set->ctx); free(ret), NULL);
911 memcpy(ret->val.nodes, set->val.nodes, set->used * sizeof *ret->val.nodes);
912 } else {
913 ret->val.nodes = NULL;
914 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200915
916 ret->used = ret->size = set->used;
917 ret->ctx_pos = set->ctx_pos;
918 ret->ctx_size = set->ctx_size;
Michal Vasko4a04e542021-02-01 08:58:30 +0100919 if (set->ht) {
920 ret->ht = lyht_dup(set->ht);
921 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200922 } else {
Radek Krejci0f969882020-08-21 16:56:47 +0200923 memcpy(ret, set, sizeof *ret);
924 if (set->type == LYXP_SET_STRING) {
925 ret->val.str = strdup(set->val.str);
926 LY_CHECK_ERR_RET(!ret->val.str, LOGMEM(set->ctx); free(ret), NULL);
927 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200928 }
929
930 return ret;
931}
932
933/**
934 * @brief Fill XPath set with a string. Any current data are disposed of.
935 *
936 * @param[in] set Set to fill.
937 * @param[in] string String to fill into \p set.
938 * @param[in] str_len Length of \p string. 0 is a valid value!
939 */
940static void
Michal Vaskodd528af2022-08-08 14:35:07 +0200941set_fill_string(struct lyxp_set *set, const char *string, uint32_t str_len)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200942{
Michal Vaskod3678892020-05-21 10:06:58 +0200943 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200944
945 set->type = LYXP_SET_STRING;
946 if ((str_len == 0) && (string[0] != '\0')) {
947 string = "";
948 }
949 set->val.str = strndup(string, str_len);
950}
951
952/**
953 * @brief Fill XPath set with a number. Any current data are disposed of.
954 *
955 * @param[in] set Set to fill.
956 * @param[in] number Number to fill into \p set.
957 */
958static void
959set_fill_number(struct lyxp_set *set, long double number)
960{
Michal Vaskod3678892020-05-21 10:06:58 +0200961 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200962
963 set->type = LYXP_SET_NUMBER;
964 set->val.num = number;
965}
966
967/**
968 * @brief Fill XPath set with a boolean. Any current data are disposed of.
969 *
970 * @param[in] set Set to fill.
971 * @param[in] boolean Boolean to fill into \p set.
972 */
973static void
Radek Krejci857189e2020-09-01 13:26:36 +0200974set_fill_boolean(struct lyxp_set *set, ly_bool boolean)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200975{
Michal Vaskod3678892020-05-21 10:06:58 +0200976 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200977
978 set->type = LYXP_SET_BOOLEAN;
Michal Vasko004d3152020-06-11 19:59:22 +0200979 set->val.bln = boolean;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200980}
981
982/**
983 * @brief Fill XPath set with the value from another set (deep assign).
984 * Any current data are disposed of.
985 *
986 * @param[in] trg Set to fill.
987 * @param[in] src Source set to copy into \p trg.
988 */
989static void
Michal Vasko4c7763f2020-07-27 17:40:37 +0200990set_fill_set(struct lyxp_set *trg, const struct lyxp_set *src)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200991{
992 if (!trg || !src) {
993 return;
994 }
995
996 if (trg->type == LYXP_SET_NODE_SET) {
997 free(trg->val.nodes);
998 } else if (trg->type == LYXP_SET_STRING) {
999 free(trg->val.str);
1000 }
1001 set_init(trg, src);
1002
1003 if (src->type == LYXP_SET_SCNODE_SET) {
1004 trg->type = LYXP_SET_SCNODE_SET;
1005 trg->used = src->used;
1006 trg->size = src->used;
1007
Michal Vasko08e9b112021-06-11 15:41:17 +02001008 if (trg->size) {
1009 trg->val.scnodes = ly_realloc(trg->val.scnodes, trg->size * sizeof *trg->val.scnodes);
1010 LY_CHECK_ERR_RET(!trg->val.scnodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), );
1011 memcpy(trg->val.scnodes, src->val.scnodes, src->used * sizeof *src->val.scnodes);
1012 } else {
1013 trg->val.scnodes = NULL;
1014 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001015 } else if (src->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +02001016 set_fill_boolean(trg, src->val.bln);
Michal Vasko44f3d2c2020-08-24 09:49:38 +02001017 } else if (src->type == LYXP_SET_NUMBER) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001018 set_fill_number(trg, src->val.num);
1019 } else if (src->type == LYXP_SET_STRING) {
1020 set_fill_string(trg, src->val.str, strlen(src->val.str));
1021 } else {
1022 if (trg->type == LYXP_SET_NODE_SET) {
1023 free(trg->val.nodes);
1024 } else if (trg->type == LYXP_SET_STRING) {
1025 free(trg->val.str);
1026 }
1027
Michal Vaskod3678892020-05-21 10:06:58 +02001028 assert(src->type == LYXP_SET_NODE_SET);
1029
1030 trg->type = LYXP_SET_NODE_SET;
1031 trg->used = src->used;
1032 trg->size = src->used;
1033 trg->ctx_pos = src->ctx_pos;
1034 trg->ctx_size = src->ctx_size;
1035
Michal Vasko08e9b112021-06-11 15:41:17 +02001036 if (trg->size) {
1037 trg->val.nodes = malloc(trg->size * sizeof *trg->val.nodes);
1038 LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), );
1039 memcpy(trg->val.nodes, src->val.nodes, src->used * sizeof *src->val.nodes);
1040 } else {
1041 trg->val.nodes = NULL;
1042 }
Michal Vaskod3678892020-05-21 10:06:58 +02001043 if (src->ht) {
1044 trg->ht = lyht_dup(src->ht);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001045 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02001046 trg->ht = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001047 }
1048 }
1049}
1050
1051/**
1052 * @brief Clear context of all schema nodes.
1053 *
1054 * @param[in] set Set to clear.
Michal Vasko1a09b212021-05-06 13:00:10 +02001055 * @param[in] new_ctx New context state for all the nodes currently in the context.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001056 */
1057static void
Michal Vasko1a09b212021-05-06 13:00:10 +02001058set_scnode_clear_ctx(struct lyxp_set *set, int32_t new_ctx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001059{
1060 uint32_t i;
1061
1062 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001063 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko1a09b212021-05-06 13:00:10 +02001064 set->val.scnodes[i].in_ctx = new_ctx;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001065 } else if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START) {
1066 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001067 }
1068 }
1069}
1070
1071/**
1072 * @brief Remove a node from a set. Removing last node changes
1073 * set into LYXP_SET_EMPTY. Context position aware.
1074 *
1075 * @param[in] set Set to use.
1076 * @param[in] idx Index from @p set of the node to be removed.
1077 */
1078static void
1079set_remove_node(struct lyxp_set *set, uint32_t idx)
1080{
1081 assert(set && (set->type == LYXP_SET_NODE_SET));
1082 assert(idx < set->used);
1083
1084 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
1085
1086 --set->used;
Michal Vasko08e9b112021-06-11 15:41:17 +02001087 if (idx < set->used) {
1088 memmove(&set->val.nodes[idx], &set->val.nodes[idx + 1], (set->used - idx) * sizeof *set->val.nodes);
1089 } else if (!set->used) {
Michal Vaskod3678892020-05-21 10:06:58 +02001090 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001091 }
1092}
1093
1094/**
Michal Vasko2caefc12019-11-14 16:07:56 +01001095 * @brief Remove a node from a set by setting its type to LYXP_NODE_NONE.
Michal Vasko57eab132019-09-24 11:46:26 +02001096 *
1097 * @param[in] set Set to use.
1098 * @param[in] idx Index from @p set of the node to be removed.
1099 */
1100static void
Michal Vasko2caefc12019-11-14 16:07:56 +01001101set_remove_node_none(struct lyxp_set *set, uint32_t idx)
Michal Vasko57eab132019-09-24 11:46:26 +02001102{
1103 assert(set && (set->type == LYXP_SET_NODE_SET));
1104 assert(idx < set->used);
1105
Michal Vasko2caefc12019-11-14 16:07:56 +01001106 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1107 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
1108 }
1109 set->val.nodes[idx].type = LYXP_NODE_NONE;
Michal Vasko57eab132019-09-24 11:46:26 +02001110}
1111
1112/**
Michal Vasko2caefc12019-11-14 16:07:56 +01001113 * @brief Remove all LYXP_NODE_NONE nodes from a set. Removing last node changes
Michal Vasko57eab132019-09-24 11:46:26 +02001114 * set into LYXP_SET_EMPTY. Context position aware.
1115 *
1116 * @param[in] set Set to consolidate.
1117 */
1118static void
Michal Vasko2caefc12019-11-14 16:07:56 +01001119set_remove_nodes_none(struct lyxp_set *set)
Michal Vasko57eab132019-09-24 11:46:26 +02001120{
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01001121 uint32_t i, orig_used, end = 0;
1122 int64_t start;
Michal Vasko57eab132019-09-24 11:46:26 +02001123
Michal Vaskod3678892020-05-21 10:06:58 +02001124 assert(set);
Michal Vasko57eab132019-09-24 11:46:26 +02001125
1126 orig_used = set->used;
1127 set->used = 0;
Michal Vaskod989ba02020-08-24 10:59:24 +02001128 for (i = 0; i < orig_used; ) {
Michal Vasko57eab132019-09-24 11:46:26 +02001129 start = -1;
1130 do {
Michal Vasko2caefc12019-11-14 16:07:56 +01001131 if ((set->val.nodes[i].type != LYXP_NODE_NONE) && (start == -1)) {
Michal Vasko57eab132019-09-24 11:46:26 +02001132 start = i;
Michal Vasko2caefc12019-11-14 16:07:56 +01001133 } else if ((start > -1) && (set->val.nodes[i].type == LYXP_NODE_NONE)) {
Michal Vasko57eab132019-09-24 11:46:26 +02001134 end = i;
1135 ++i;
1136 break;
1137 }
1138
1139 ++i;
1140 if (i == orig_used) {
1141 end = i;
1142 }
1143 } while (i < orig_used);
1144
1145 if (start > -1) {
1146 /* move the whole chunk of valid nodes together */
1147 if (set->used != (unsigned)start) {
1148 memmove(&set->val.nodes[set->used], &set->val.nodes[start], (end - start) * sizeof *set->val.nodes);
1149 }
1150 set->used += end - start;
1151 }
1152 }
Michal Vasko57eab132019-09-24 11:46:26 +02001153}
1154
1155/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02001156 * @brief Check for duplicates in a node set.
1157 *
1158 * @param[in] set Set to check.
1159 * @param[in] node Node to look for in @p set.
1160 * @param[in] node_type Type of @p node.
1161 * @param[in] skip_idx Index from @p set to skip.
1162 * @return LY_ERR
1163 */
1164static LY_ERR
1165set_dup_node_check(const struct lyxp_set *set, const struct lyd_node *node, enum lyxp_node_type node_type, int skip_idx)
1166{
1167 uint32_t i;
1168
Michal Vasko2caefc12019-11-14 16:07:56 +01001169 if (set->ht && node) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001170 return set_dup_node_hash_check(set, (struct lyd_node *)node, node_type, skip_idx);
1171 }
1172
1173 for (i = 0; i < set->used; ++i) {
1174 if ((skip_idx > -1) && (i == (unsigned)skip_idx)) {
1175 continue;
1176 }
1177
1178 if ((set->val.nodes[i].node == node) && (set->val.nodes[i].type == node_type)) {
1179 return LY_EEXIST;
1180 }
1181 }
1182
1183 return LY_SUCCESS;
1184}
1185
Radek Krejci857189e2020-09-01 13:26:36 +02001186ly_bool
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001187lyxp_set_scnode_contains(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type, int skip_idx,
1188 uint32_t *index_p)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001189{
1190 uint32_t i;
1191
1192 for (i = 0; i < set->used; ++i) {
1193 if ((skip_idx > -1) && (i == (unsigned)skip_idx)) {
1194 continue;
1195 }
1196
1197 if ((set->val.scnodes[i].scnode == node) && (set->val.scnodes[i].type == node_type)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001198 if (index_p) {
1199 *index_p = i;
1200 }
1201 return 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001202 }
1203 }
1204
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001205 return 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001206}
1207
Michal Vaskoecd62de2019-11-13 12:35:11 +01001208void
1209lyxp_set_scnode_merge(struct lyxp_set *set1, struct lyxp_set *set2)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001210{
1211 uint32_t orig_used, i, j;
1212
Michal Vaskod3678892020-05-21 10:06:58 +02001213 assert((set1->type == LYXP_SET_SCNODE_SET) && (set2->type == LYXP_SET_SCNODE_SET));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001214
Michal Vaskod3678892020-05-21 10:06:58 +02001215 if (!set2->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001216 return;
1217 }
1218
Michal Vaskod3678892020-05-21 10:06:58 +02001219 if (!set1->used) {
aPiecekadc1e4f2021-10-07 11:15:12 +02001220 /* release hidden allocated data (lyxp_set.size) */
1221 lyxp_set_free_content(set1);
1222 /* direct copying of the entire structure */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001223 memcpy(set1, set2, sizeof *set1);
1224 return;
1225 }
1226
1227 if (set1->used + set2->used > set1->size) {
1228 set1->size = set1->used + set2->used;
1229 set1->val.scnodes = ly_realloc(set1->val.scnodes, set1->size * sizeof *set1->val.scnodes);
1230 LY_CHECK_ERR_RET(!set1->val.scnodes, LOGMEM(set1->ctx), );
1231 }
1232
1233 orig_used = set1->used;
1234
1235 for (i = 0; i < set2->used; ++i) {
1236 for (j = 0; j < orig_used; ++j) {
1237 /* detect duplicities */
1238 if (set1->val.scnodes[j].scnode == set2->val.scnodes[i].scnode) {
1239 break;
1240 }
1241 }
1242
Michal Vasko0587bce2020-12-10 12:19:21 +01001243 if (j < orig_used) {
1244 /* node is there, but update its status if needed */
1245 if (set1->val.scnodes[j].in_ctx == LYXP_SET_SCNODE_START_USED) {
1246 set1->val.scnodes[j].in_ctx = set2->val.scnodes[i].in_ctx;
Michal Vasko1a09b212021-05-06 13:00:10 +02001247 } else if ((set1->val.scnodes[j].in_ctx == LYXP_SET_SCNODE_ATOM_NODE) &&
1248 (set2->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_VAL)) {
1249 set1->val.scnodes[j].in_ctx = set2->val.scnodes[i].in_ctx;
Michal Vasko0587bce2020-12-10 12:19:21 +01001250 }
1251 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001252 memcpy(&set1->val.scnodes[set1->used], &set2->val.scnodes[i], sizeof *set2->val.scnodes);
1253 ++set1->used;
1254 }
1255 }
1256
Michal Vaskod3678892020-05-21 10:06:58 +02001257 lyxp_set_free_content(set2);
1258 set2->type = LYXP_SET_SCNODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001259}
1260
1261/**
1262 * @brief Insert a node into a set. Context position aware.
1263 *
1264 * @param[in] set Set to use.
1265 * @param[in] node Node to insert to @p set.
1266 * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting.
1267 * @param[in] node_type Node type of @p node.
1268 * @param[in] idx Index in @p set to insert into.
1269 */
1270static void
1271set_insert_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx)
1272{
Michal Vaskod3678892020-05-21 10:06:58 +02001273 assert(set && (set->type == LYXP_SET_NODE_SET));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001274
Michal Vaskod3678892020-05-21 10:06:58 +02001275 if (!set->size) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001276 /* first item */
1277 if (idx) {
1278 /* no real harm done, but it is a bug */
1279 LOGINT(set->ctx);
1280 idx = 0;
1281 }
1282 set->val.nodes = malloc(LYXP_SET_SIZE_START * sizeof *set->val.nodes);
1283 LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), );
1284 set->type = LYXP_SET_NODE_SET;
1285 set->used = 0;
1286 set->size = LYXP_SET_SIZE_START;
1287 set->ctx_pos = 1;
1288 set->ctx_size = 1;
1289 set->ht = NULL;
1290 } else {
1291 /* not an empty set */
1292 if (set->used == set->size) {
1293
1294 /* set is full */
Michal Vasko871df522022-04-06 12:14:41 +02001295 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 +02001296 LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), );
Michal Vasko871df522022-04-06 12:14:41 +02001297 set->size *= LYXP_SET_SIZE_MUL_STEP;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001298 }
1299
1300 if (idx > set->used) {
1301 LOGINT(set->ctx);
1302 idx = set->used;
1303 }
1304
1305 /* make space for the new node */
1306 if (idx < set->used) {
1307 memmove(&set->val.nodes[idx + 1], &set->val.nodes[idx], (set->used - idx) * sizeof *set->val.nodes);
1308 }
1309 }
1310
1311 /* finally assign the value */
1312 set->val.nodes[idx].node = (struct lyd_node *)node;
1313 set->val.nodes[idx].type = node_type;
1314 set->val.nodes[idx].pos = pos;
1315 ++set->used;
1316
Michal Vasko2416fdd2021-10-01 11:07:10 +02001317 /* add into hash table */
1318 set_insert_node_hash(set, (struct lyd_node *)node, node_type);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001319}
1320
Michal Vasko7333cb32022-07-29 16:30:29 +02001321/**
1322 * @brief Insert schema node into set.
1323 *
1324 * @param[in] set Set to insert into.
1325 * @param[in] node Node to insert.
1326 * @param[in] node_type Node type of @p node.
1327 * @param[in] axis Axis that @p node was reached on.
1328 * @param[out] index_p Optional pointer to store index if the inserted @p node.
1329 * @return LY_SUCCESS on success.
1330 * @return LY_EMEM on memory allocation failure.
1331 */
1332static LY_ERR
1333set_scnode_insert_node(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type,
1334 enum lyxp_axis axis, uint32_t *index_p)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001335{
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001336 uint32_t index;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001337
1338 assert(set->type == LYXP_SET_SCNODE_SET);
1339
Michal Vasko871df522022-04-06 12:14:41 +02001340 if (!set->size) {
1341 /* first item */
1342 set->val.scnodes = malloc(LYXP_SET_SIZE_START * sizeof *set->val.scnodes);
1343 LY_CHECK_ERR_RET(!set->val.scnodes, LOGMEM(set->ctx), LY_EMEM);
1344 set->type = LYXP_SET_SCNODE_SET;
1345 set->used = 0;
1346 set->size = LYXP_SET_SIZE_START;
1347 set->ctx_pos = 1;
1348 set->ctx_size = 1;
1349 set->ht = NULL;
1350 }
1351
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001352 if (lyxp_set_scnode_contains(set, node, node_type, -1, &index)) {
Michal Vasko7333cb32022-07-29 16:30:29 +02001353 /* BUG if axes differs, this new one is thrown away */
Radek Krejcif13b87b2020-12-01 22:02:17 +01001354 set->val.scnodes[index].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001355 } else {
1356 if (set->used == set->size) {
Michal Vasko871df522022-04-06 12:14:41 +02001357 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 +02001358 LY_CHECK_ERR_RET(!set->val.scnodes, LOGMEM(set->ctx), LY_EMEM);
Michal Vasko871df522022-04-06 12:14:41 +02001359 set->size *= LYXP_SET_SIZE_MUL_STEP;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001360 }
1361
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001362 index = set->used;
1363 set->val.scnodes[index].scnode = (struct lysc_node *)node;
1364 set->val.scnodes[index].type = node_type;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001365 set->val.scnodes[index].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko7333cb32022-07-29 16:30:29 +02001366 set->val.scnodes[index].axis = axis;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001367 ++set->used;
1368 }
1369
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001370 if (index_p) {
1371 *index_p = index;
1372 }
1373
1374 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001375}
1376
1377/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02001378 * @brief Set all nodes with ctx 1 to a new unique context value.
1379 *
1380 * @param[in] set Set to modify.
1381 * @return New context value.
1382 */
Michal Vasko5c4e5892019-11-14 12:31:38 +01001383static int32_t
Michal Vasko03ff5a72019-09-11 13:49:33 +02001384set_scnode_new_in_ctx(struct lyxp_set *set)
1385{
Michal Vasko5c4e5892019-11-14 12:31:38 +01001386 uint32_t i;
1387 int32_t ret_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001388
1389 assert(set->type == LYXP_SET_SCNODE_SET);
1390
Radek Krejcif13b87b2020-12-01 22:02:17 +01001391 ret_ctx = LYXP_SET_SCNODE_ATOM_PRED_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001392retry:
1393 for (i = 0; i < set->used; ++i) {
1394 if (set->val.scnodes[i].in_ctx >= ret_ctx) {
1395 ret_ctx = set->val.scnodes[i].in_ctx + 1;
1396 goto retry;
1397 }
1398 }
1399 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001400 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001401 set->val.scnodes[i].in_ctx = ret_ctx;
1402 }
1403 }
1404
1405 return ret_ctx;
1406}
1407
1408/**
1409 * @brief Get unique @p node position in the data.
1410 *
1411 * @param[in] node Node to find.
1412 * @param[in] node_type Node type of @p node.
1413 * @param[in] root Root node.
1414 * @param[in] root_type Type of the XPath @p root node.
1415 * @param[in] prev Node that we think is before @p node in DFS from @p root. Can optionally
1416 * be used to increase efficiency and start the DFS from this node.
1417 * @param[in] prev_pos Node @p prev position. Optional, but must be set if @p prev is set.
1418 * @return Node position.
1419 */
1420static uint32_t
1421get_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 +02001422 enum lyxp_node_type root_type, const struct lyd_node **prev, uint32_t *prev_pos)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001423{
Michal Vasko56daf732020-08-10 10:57:18 +02001424 const struct lyd_node *elem = NULL, *top_sibling;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001425 uint32_t pos = 1;
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001426 ly_bool found = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001427
1428 assert(prev && prev_pos && !root->prev->next);
1429
1430 if ((node_type == LYXP_NODE_ROOT) || (node_type == LYXP_NODE_ROOT_CONFIG)) {
1431 return 0;
1432 }
1433
1434 if (*prev) {
1435 /* start from the previous element instead from the root */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001436 pos = *prev_pos;
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001437 for (top_sibling = *prev; top_sibling->parent; top_sibling = lyd_parent(top_sibling)) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001438 goto dfs_search;
1439 }
1440
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001441 LY_LIST_FOR(root, top_sibling) {
Michal Vasko56daf732020-08-10 10:57:18 +02001442 LYD_TREE_DFS_BEGIN(top_sibling, elem) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001443dfs_search:
Michal Vaskoa9309bb2021-07-09 09:31:55 +02001444 LYD_TREE_DFS_continue = 0;
1445
Michal Vasko56daf732020-08-10 10:57:18 +02001446 if (*prev && !elem) {
1447 /* resume previous DFS */
1448 elem = LYD_TREE_DFS_next = (struct lyd_node *)*prev;
1449 LYD_TREE_DFS_continue = 0;
1450 }
1451
Michal Vasko482a6822022-05-06 08:56:12 +02001452 if (!elem->schema || ((root_type == LYXP_NODE_ROOT_CONFIG) && (elem->schema->flags & LYS_CONFIG_R))) {
Michal Vasko56daf732020-08-10 10:57:18 +02001453 /* skip */
1454 LYD_TREE_DFS_continue = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001455 } else {
Michal Vasko56daf732020-08-10 10:57:18 +02001456 if (elem == node) {
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001457 found = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001458 break;
1459 }
Michal Vasko56daf732020-08-10 10:57:18 +02001460 ++pos;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001461 }
Michal Vasko56daf732020-08-10 10:57:18 +02001462
1463 LYD_TREE_DFS_END(top_sibling, elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001464 }
1465
1466 /* node found */
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001467 if (found) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001468 break;
1469 }
1470 }
1471
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001472 if (!found) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001473 if (!(*prev)) {
1474 /* we went from root and failed to find it, cannot be */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001475 LOGINT(LYD_CTX(node));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001476 return 0;
1477 } else {
Michal Vasko56daf732020-08-10 10:57:18 +02001478 /* start the search again from the beginning */
1479 *prev = root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001480
Michal Vasko56daf732020-08-10 10:57:18 +02001481 top_sibling = root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001482 pos = 1;
1483 goto dfs_search;
1484 }
1485 }
1486
1487 /* remember the last found node for next time */
1488 *prev = node;
1489 *prev_pos = pos;
1490
1491 return pos;
1492}
1493
1494/**
1495 * @brief Assign (fill) missing node positions.
1496 *
1497 * @param[in] set Set to fill positions in.
1498 * @param[in] root Context root node.
1499 * @param[in] root_type Context root type.
1500 * @return LY_ERR
1501 */
1502static LY_ERR
1503set_assign_pos(struct lyxp_set *set, const struct lyd_node *root, enum lyxp_node_type root_type)
1504{
1505 const struct lyd_node *prev = NULL, *tmp_node;
1506 uint32_t i, tmp_pos = 0;
1507
1508 for (i = 0; i < set->used; ++i) {
1509 if (!set->val.nodes[i].pos) {
1510 tmp_node = NULL;
1511 switch (set->val.nodes[i].type) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001512 case LYXP_NODE_META:
1513 tmp_node = set->val.meta[i].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001514 if (!tmp_node) {
1515 LOGINT_RET(root->schema->module->ctx);
1516 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01001517 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001518 case LYXP_NODE_ELEM:
1519 case LYXP_NODE_TEXT:
1520 if (!tmp_node) {
1521 tmp_node = set->val.nodes[i].node;
1522 }
1523 set->val.nodes[i].pos = get_node_pos(tmp_node, set->val.nodes[i].type, root, root_type, &prev, &tmp_pos);
1524 break;
1525 default:
1526 /* all roots have position 0 */
1527 break;
1528 }
1529 }
1530 }
1531
1532 return LY_SUCCESS;
1533}
1534
1535/**
Michal Vasko9f96a052020-03-10 09:41:45 +01001536 * @brief Get unique @p meta position in the parent metadata.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001537 *
Michal Vasko9f96a052020-03-10 09:41:45 +01001538 * @param[in] meta Metadata to use.
1539 * @return Metadata position.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001540 */
Michal Vaskodd528af2022-08-08 14:35:07 +02001541static uint32_t
Michal Vasko9f96a052020-03-10 09:41:45 +01001542get_meta_pos(struct lyd_meta *meta)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001543{
Michal Vaskodd528af2022-08-08 14:35:07 +02001544 uint32_t pos = 0;
Michal Vasko9f96a052020-03-10 09:41:45 +01001545 struct lyd_meta *meta2;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001546
Michal Vasko9f96a052020-03-10 09:41:45 +01001547 for (meta2 = meta->parent->meta; meta2 && (meta2 != meta); meta2 = meta2->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001548 ++pos;
1549 }
1550
Michal Vasko9f96a052020-03-10 09:41:45 +01001551 assert(meta2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001552 return pos;
1553}
1554
1555/**
1556 * @brief Compare 2 nodes in respect to XPath document order.
1557 *
1558 * @param[in] item1 1st node.
1559 * @param[in] item2 2nd node.
1560 * @return If 1st > 2nd returns 1, 1st == 2nd returns 0, and 1st < 2nd returns -1.
1561 */
1562static int
1563set_sort_compare(struct lyxp_set_node *item1, struct lyxp_set_node *item2)
1564{
Michal Vasko9f96a052020-03-10 09:41:45 +01001565 uint32_t meta_pos1 = 0, meta_pos2 = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001566
1567 if (item1->pos < item2->pos) {
1568 return -1;
1569 }
1570
1571 if (item1->pos > item2->pos) {
1572 return 1;
1573 }
1574
1575 /* node positions are equal, the fun case */
1576
1577 /* 1st ELEM - == - 2nd TEXT, 1st TEXT - == - 2nd ELEM */
1578 /* special case since text nodes are actually saved as their parents */
1579 if ((item1->node == item2->node) && (item1->type != item2->type)) {
1580 if (item1->type == LYXP_NODE_ELEM) {
1581 assert(item2->type == LYXP_NODE_TEXT);
1582 return -1;
1583 } else {
1584 assert((item1->type == LYXP_NODE_TEXT) && (item2->type == LYXP_NODE_ELEM));
1585 return 1;
1586 }
1587 }
1588
Michal Vasko9f96a052020-03-10 09:41:45 +01001589 /* we need meta positions now */
1590 if (item1->type == LYXP_NODE_META) {
1591 meta_pos1 = get_meta_pos((struct lyd_meta *)item1->node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001592 }
Michal Vasko9f96a052020-03-10 09:41:45 +01001593 if (item2->type == LYXP_NODE_META) {
1594 meta_pos2 = get_meta_pos((struct lyd_meta *)item2->node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001595 }
1596
Michal Vasko9f96a052020-03-10 09:41:45 +01001597 /* 1st ROOT - 2nd ROOT, 1st ELEM - 2nd ELEM, 1st TEXT - 2nd TEXT, 1st META - =pos= - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001598 /* check for duplicates */
1599 if (item1->node == item2->node) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001600 assert((item1->type == item2->type) && ((item1->type != LYXP_NODE_META) || (meta_pos1 == meta_pos2)));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001601 return 0;
1602 }
1603
Michal Vasko9f96a052020-03-10 09:41:45 +01001604 /* 1st ELEM - 2nd TEXT, 1st ELEM - any pos - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001605 /* elem is always first, 2nd node is after it */
1606 if (item1->type == LYXP_NODE_ELEM) {
1607 assert(item2->type != LYXP_NODE_ELEM);
1608 return -1;
1609 }
1610
Michal Vasko9f96a052020-03-10 09:41:45 +01001611 /* 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 +02001612 /* 2nd is before 1st */
Michal Vasko69730152020-10-09 16:30:07 +02001613 if (((item1->type == LYXP_NODE_TEXT) &&
1614 ((item2->type == LYXP_NODE_ELEM) || (item2->type == LYXP_NODE_META))) ||
1615 ((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_ELEM)) ||
1616 (((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_META)) &&
1617 (meta_pos1 > meta_pos2))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001618 return 1;
1619 }
1620
Michal Vasko9f96a052020-03-10 09:41:45 +01001621 /* 1st META - any pos - 2nd TEXT, 1st META <pos< - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001622 /* 2nd is after 1st */
1623 return -1;
1624}
1625
1626/**
1627 * @brief Set cast for comparisons.
1628 *
Michal Vasko8abcecc2022-07-28 09:55:01 +02001629 * @param[in,out] trg Target set to cast source into.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001630 * @param[in] src Source set.
1631 * @param[in] type Target set type.
1632 * @param[in] src_idx Source set node index.
Michal Vasko8abcecc2022-07-28 09:55:01 +02001633 * @return LY_SUCCESS on success.
1634 * @return LY_ERR value on error.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001635 */
1636static LY_ERR
Michal Vasko8abcecc2022-07-28 09:55:01 +02001637set_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 +02001638{
1639 assert(src->type == LYXP_SET_NODE_SET);
1640
1641 set_init(trg, src);
1642
1643 /* insert node into target set */
1644 set_insert_node(trg, src->val.nodes[src_idx].node, src->val.nodes[src_idx].pos, src->val.nodes[src_idx].type, 0);
1645
1646 /* cast target set appropriately */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001647 return lyxp_set_cast(trg, type);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001648}
1649
Michal Vasko4c7763f2020-07-27 17:40:37 +02001650/**
1651 * @brief Set content canonization for comparisons.
1652 *
Michal Vasko8abcecc2022-07-28 09:55:01 +02001653 * @param[in,out] set Set to canonize.
Michal Vasko4c7763f2020-07-27 17:40:37 +02001654 * @param[in] xp_node Source XPath node/meta to use for canonization.
Michal Vasko8abcecc2022-07-28 09:55:01 +02001655 * @return LY_SUCCESS on success.
1656 * @return LY_ERR value on error.
Michal Vasko4c7763f2020-07-27 17:40:37 +02001657 */
1658static LY_ERR
Michal Vasko8abcecc2022-07-28 09:55:01 +02001659set_comp_canonize(struct lyxp_set *set, const struct lyxp_set_node *xp_node)
Michal Vasko4c7763f2020-07-27 17:40:37 +02001660{
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001661 const struct lysc_type *type = NULL;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001662 struct lyd_value val;
1663 struct ly_err_item *err = NULL;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001664 LY_ERR rc;
1665
1666 /* is there anything to canonize even? */
Michal Vasko8abcecc2022-07-28 09:55:01 +02001667 if (set->type == LYXP_SET_STRING) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02001668 /* do we have a type to use for canonization? */
1669 if ((xp_node->type == LYXP_NODE_ELEM) && (xp_node->node->schema->nodetype & LYD_NODE_TERM)) {
1670 type = ((struct lyd_node_term *)xp_node->node)->value.realtype;
1671 } else if (xp_node->type == LYXP_NODE_META) {
1672 type = ((struct lyd_meta *)xp_node->node)->value.realtype;
1673 }
1674 }
1675 if (!type) {
Michal Vasko8abcecc2022-07-28 09:55:01 +02001676 /* no canonization needed/possible */
1677 return LY_SUCCESS;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001678 }
1679
Michal Vasko8abcecc2022-07-28 09:55:01 +02001680 /* check for built-in types without required canonization */
1681 if ((type->basetype == LY_TYPE_STRING) && (type->plugin->store == lyplg_type_store_string)) {
1682 /* string */
1683 return LY_SUCCESS;
1684 }
1685 if ((type->basetype == LY_TYPE_BOOL) && (type->plugin->store == lyplg_type_store_boolean)) {
1686 /* boolean */
1687 return LY_SUCCESS;
1688 }
1689 if ((type->basetype == LY_TYPE_ENUM) && (type->plugin->store == lyplg_type_store_enum)) {
1690 /* enumeration */
1691 return LY_SUCCESS;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001692 }
1693
Michal Vasko8abcecc2022-07-28 09:55:01 +02001694 /* print canonized string, ignore errors, the value may not satisfy schema constraints */
1695 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 +01001696 LYD_HINT_DATA, xp_node->node->schema, &val, NULL, &err);
Michal Vasko4c7763f2020-07-27 17:40:37 +02001697 ly_err_free(err);
1698 if (rc) {
Michal Vasko8abcecc2022-07-28 09:55:01 +02001699 /* invalid value, function store automaticaly dealloc value when fail */
1700 return LY_SUCCESS;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001701 }
1702
Michal Vasko8abcecc2022-07-28 09:55:01 +02001703 /* use the canonized string value */
1704 free(set->val.str);
1705 set->val.str = strdup(lyd_value_get_canonical(set->ctx, &val));
1706 type->plugin->free(set->ctx, &val);
1707 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), LY_EMEM);
Michal Vasko4c7763f2020-07-27 17:40:37 +02001708
Michal Vasko4c7763f2020-07-27 17:40:37 +02001709 return LY_SUCCESS;
1710}
1711
Michal Vasko03ff5a72019-09-11 13:49:33 +02001712/**
1713 * @brief Bubble sort @p set into XPath document order.
Michal Vasko49fec8e2022-05-24 10:28:33 +02001714 * Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001715 *
1716 * @param[in] set Set to sort.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001717 * @return How many times the whole set was traversed - 1 (if set was sorted, returns 0).
1718 */
1719static int
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001720set_sort(struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001721{
1722 uint32_t i, j;
Radek Krejci1deb5be2020-08-26 16:43:36 +02001723 int ret = 0, cmp;
Radek Krejci857189e2020-09-01 13:26:36 +02001724 ly_bool inverted, change;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001725 const struct lyd_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001726 struct lyxp_set_node item;
1727 struct lyxp_set_hash_node hnode;
1728 uint64_t hash;
1729
Michal Vasko3cf8fbf2020-05-27 15:21:21 +02001730 if ((set->type != LYXP_SET_NODE_SET) || (set->used < 2)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001731 return 0;
1732 }
1733
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001734 /* find first top-level node to be used as anchor for positions */
Michal Vasko4a7d4d62021-12-13 17:05:06 +01001735 for (root = set->tree; root->parent; root = lyd_parent(root)) {}
Michal Vaskod989ba02020-08-24 10:59:24 +02001736 for ( ; root->prev->next; root = root->prev) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001737
1738 /* fill positions */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001739 if (set_assign_pos(set, root, set->root_type)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001740 return -1;
1741 }
1742
Michal Vasko88a9e802022-05-24 10:50:28 +02001743#ifndef NDEBUG
Michal Vasko03ff5a72019-09-11 13:49:33 +02001744 LOGDBG(LY_LDGXPATH, "SORT BEGIN");
1745 print_set_debug(set);
Michal Vasko88a9e802022-05-24 10:50:28 +02001746#endif
Michal Vasko03ff5a72019-09-11 13:49:33 +02001747
1748 for (i = 0; i < set->used; ++i) {
1749 inverted = 0;
1750 change = 0;
1751
1752 for (j = 1; j < set->used - i; ++j) {
1753 /* compare node positions */
1754 if (inverted) {
1755 cmp = set_sort_compare(&set->val.nodes[j], &set->val.nodes[j - 1]);
1756 } else {
1757 cmp = set_sort_compare(&set->val.nodes[j - 1], &set->val.nodes[j]);
1758 }
1759
1760 /* swap if needed */
1761 if ((inverted && (cmp < 0)) || (!inverted && (cmp > 0))) {
1762 change = 1;
1763
1764 item = set->val.nodes[j - 1];
1765 set->val.nodes[j - 1] = set->val.nodes[j];
1766 set->val.nodes[j] = item;
1767 } else {
1768 /* whether node_pos1 should be smaller than node_pos2 or the other way around */
1769 inverted = !inverted;
1770 }
1771 }
1772
1773 ++ret;
1774
1775 if (!change) {
1776 break;
1777 }
1778 }
1779
Michal Vasko88a9e802022-05-24 10:50:28 +02001780#ifndef NDEBUG
Michal Vasko03ff5a72019-09-11 13:49:33 +02001781 LOGDBG(LY_LDGXPATH, "SORT END %d", ret);
1782 print_set_debug(set);
Michal Vasko88a9e802022-05-24 10:50:28 +02001783#endif
Michal Vasko03ff5a72019-09-11 13:49:33 +02001784
1785 /* check node hashes */
1786 if (set->used >= LYD_HT_MIN_ITEMS) {
1787 assert(set->ht);
1788 for (i = 0; i < set->used; ++i) {
1789 hnode.node = set->val.nodes[i].node;
1790 hnode.type = set->val.nodes[i].type;
1791
Michal Vaskoae130f52023-04-20 14:25:16 +02001792 hash = lyht_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
1793 hash = lyht_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
1794 hash = lyht_hash_multi(hash, NULL, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001795
1796 assert(!lyht_find(set->ht, &hnode, hash, NULL));
1797 }
1798 }
1799
1800 return ret - 1;
1801}
1802
Michal Vasko03ff5a72019-09-11 13:49:33 +02001803/**
1804 * @brief Merge 2 sorted sets into one.
1805 *
1806 * @param[in,out] trg Set to merge into. Duplicates are removed.
1807 * @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 +02001808 * @return LY_ERR
1809 */
1810static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001811set_sorted_merge(struct lyxp_set *trg, struct lyxp_set *src)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001812{
1813 uint32_t i, j, k, count, dup_count;
1814 int cmp;
1815 const struct lyd_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001816
Michal Vaskod3678892020-05-21 10:06:58 +02001817 if ((trg->type != LYXP_SET_NODE_SET) || (src->type != LYXP_SET_NODE_SET)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001818 return LY_EINVAL;
1819 }
1820
Michal Vaskod3678892020-05-21 10:06:58 +02001821 if (!src->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001822 return LY_SUCCESS;
Michal Vaskod3678892020-05-21 10:06:58 +02001823 } else if (!trg->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001824 set_fill_set(trg, src);
Michal Vaskod3678892020-05-21 10:06:58 +02001825 lyxp_set_free_content(src);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001826 return LY_SUCCESS;
1827 }
1828
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001829 /* find first top-level node to be used as anchor for positions */
Michal Vasko0143b682021-12-13 17:13:09 +01001830 for (root = trg->tree; root->parent; root = lyd_parent(root)) {}
Michal Vaskod989ba02020-08-24 10:59:24 +02001831 for ( ; root->prev->next; root = root->prev) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001832
1833 /* fill positions */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001834 if (set_assign_pos(trg, root, trg->root_type) || set_assign_pos(src, root, src->root_type)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001835 return LY_EINT;
1836 }
1837
1838#ifndef NDEBUG
1839 LOGDBG(LY_LDGXPATH, "MERGE target");
1840 print_set_debug(trg);
1841 LOGDBG(LY_LDGXPATH, "MERGE source");
1842 print_set_debug(src);
1843#endif
1844
1845 /* make memory for the merge (duplicates are not detected yet, so space
1846 * will likely be wasted on them, too bad) */
1847 if (trg->size - trg->used < src->used) {
1848 trg->size = trg->used + src->used;
1849
1850 trg->val.nodes = ly_realloc(trg->val.nodes, trg->size * sizeof *trg->val.nodes);
1851 LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx), LY_EMEM);
1852 }
1853
1854 i = 0;
1855 j = 0;
1856 count = 0;
1857 dup_count = 0;
1858 do {
1859 cmp = set_sort_compare(&src->val.nodes[i], &trg->val.nodes[j]);
1860 if (!cmp) {
1861 if (!count) {
1862 /* duplicate, just skip it */
1863 ++i;
1864 ++j;
1865 } else {
1866 /* we are copying something already, so let's copy the duplicate too,
1867 * we are hoping that afterwards there are some more nodes to
1868 * copy and this way we can copy them all together */
1869 ++count;
1870 ++dup_count;
1871 ++i;
1872 ++j;
1873 }
1874 } else if (cmp < 0) {
1875 /* inserting src node into trg, just remember it for now */
1876 ++count;
1877 ++i;
1878
1879 /* insert the hash now */
1880 set_insert_node_hash(trg, src->val.nodes[i - 1].node, src->val.nodes[i - 1].type);
1881 } else if (count) {
1882copy_nodes:
1883 /* time to actually copy the nodes, we have found the largest block of nodes */
1884 memmove(&trg->val.nodes[j + (count - dup_count)],
1885 &trg->val.nodes[j],
1886 (trg->used - j) * sizeof *trg->val.nodes);
1887 memcpy(&trg->val.nodes[j - dup_count], &src->val.nodes[i - count], count * sizeof *src->val.nodes);
1888
1889 trg->used += count - dup_count;
1890 /* do not change i, except the copying above, we are basically doing exactly what is in the else branch below */
1891 j += count - dup_count;
1892
1893 count = 0;
1894 dup_count = 0;
1895 } else {
1896 ++j;
1897 }
1898 } while ((i < src->used) && (j < trg->used));
1899
1900 if ((i < src->used) || count) {
1901 /* insert all the hashes first */
1902 for (k = i; k < src->used; ++k) {
1903 set_insert_node_hash(trg, src->val.nodes[k].node, src->val.nodes[k].type);
1904 }
1905
1906 /* loop ended, but we need to copy something at trg end */
1907 count += src->used - i;
1908 i = src->used;
1909 goto copy_nodes;
1910 }
1911
1912 /* we are inserting hashes before the actual node insert, which causes
1913 * situations when there were initially not enough items for a hash table,
1914 * but even after some were inserted, hash table was not created (during
1915 * insertion the number of items is not updated yet) */
1916 if (!trg->ht && (trg->used >= LYD_HT_MIN_ITEMS)) {
1917 set_insert_node_hash(trg, NULL, 0);
1918 }
1919
1920#ifndef NDEBUG
1921 LOGDBG(LY_LDGXPATH, "MERGE result");
1922 print_set_debug(trg);
1923#endif
1924
Michal Vaskod3678892020-05-21 10:06:58 +02001925 lyxp_set_free_content(src);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001926 return LY_SUCCESS;
1927}
1928
Michal Vasko14676352020-05-29 11:35:55 +02001929LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02001930lyxp_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 +02001931{
Michal Vasko004d3152020-06-11 19:59:22 +02001932 if (exp->used == tok_idx) {
Michal Vasko14676352020-05-29 11:35:55 +02001933 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001934 LOGVAL(ctx, LY_VCODE_XP_EOF);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001935 }
Michal Vasko14676352020-05-29 11:35:55 +02001936 return LY_EINCOMPLETE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001937 }
1938
Michal Vasko004d3152020-06-11 19:59:22 +02001939 if (want_tok && (exp->tokens[tok_idx] != want_tok)) {
Michal Vasko14676352020-05-29 11:35:55 +02001940 if (ctx) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02001941 LOGVAL(ctx, LY_VCODE_XP_INTOK2, lyxp_token2str(exp->tokens[tok_idx]),
1942 &exp->expr[exp->tok_pos[tok_idx]], lyxp_token2str(want_tok));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001943 }
Michal Vasko14676352020-05-29 11:35:55 +02001944 return LY_ENOT;
1945 }
1946
1947 return LY_SUCCESS;
1948}
1949
Michal Vasko004d3152020-06-11 19:59:22 +02001950LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02001951lyxp_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 +02001952{
1953 LY_CHECK_RET(lyxp_check_token(ctx, exp, *tok_idx, want_tok));
1954
1955 /* skip the token */
1956 ++(*tok_idx);
1957
1958 return LY_SUCCESS;
1959}
1960
Michal Vasko14676352020-05-29 11:35:55 +02001961/* just like lyxp_check_token() but tests for 2 tokens */
1962static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02001963exp_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 +02001964 enum lyxp_token want_tok2)
Michal Vasko14676352020-05-29 11:35:55 +02001965{
Michal Vasko004d3152020-06-11 19:59:22 +02001966 if (exp->used == tok_idx) {
Michal Vasko14676352020-05-29 11:35:55 +02001967 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001968 LOGVAL(ctx, LY_VCODE_XP_EOF);
Michal Vasko14676352020-05-29 11:35:55 +02001969 }
1970 return LY_EINCOMPLETE;
1971 }
1972
Michal Vasko004d3152020-06-11 19:59:22 +02001973 if ((exp->tokens[tok_idx] != want_tok1) && (exp->tokens[tok_idx] != want_tok2)) {
Michal Vasko14676352020-05-29 11:35:55 +02001974 if (ctx) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02001975 LOGVAL(ctx, LY_VCODE_XP_INTOK, lyxp_token2str(exp->tokens[tok_idx]),
Michal Vasko0b468e62020-10-19 09:33:04 +02001976 &exp->expr[exp->tok_pos[tok_idx]]);
Michal Vasko14676352020-05-29 11:35:55 +02001977 }
1978 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001979 }
1980
1981 return LY_SUCCESS;
1982}
1983
Michal Vasko4911eeb2021-06-28 11:23:05 +02001984LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02001985lyxp_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 +02001986 enum lyxp_token want_tok2)
1987{
1988 LY_CHECK_RET(exp_check_token2(ctx, exp, *tok_idx, want_tok1, want_tok2));
1989
1990 /* skip the token */
1991 ++(*tok_idx);
1992
1993 return LY_SUCCESS;
1994}
1995
Michal Vasko03ff5a72019-09-11 13:49:33 +02001996/**
1997 * @brief Stack operation push on the repeat array.
1998 *
1999 * @param[in] exp Expression to use.
Michal Vasko831e3bd2023-04-24 10:43:38 +02002000 * @param[in] tok_idx Position in the expresion @p exp.
2001 * @param[in] repeat_expr_type Repeated expression type, this value is pushed.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002002 */
2003static void
Michal Vasko831e3bd2023-04-24 10:43:38 +02002004exp_repeat_push(struct lyxp_expr *exp, uint32_t tok_idx, enum lyxp_expr_type repeat_expr_type)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002005{
Michal Vaskodd528af2022-08-08 14:35:07 +02002006 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002007
Michal Vasko004d3152020-06-11 19:59:22 +02002008 if (exp->repeat[tok_idx]) {
Radek Krejci1e008d22020-08-17 11:37:37 +02002009 for (i = 0; exp->repeat[tok_idx][i]; ++i) {}
Michal Vasko004d3152020-06-11 19:59:22 +02002010 exp->repeat[tok_idx] = realloc(exp->repeat[tok_idx], (i + 2) * sizeof *exp->repeat[tok_idx]);
2011 LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), );
Michal Vasko831e3bd2023-04-24 10:43:38 +02002012 exp->repeat[tok_idx][i] = repeat_expr_type;
Michal Vasko004d3152020-06-11 19:59:22 +02002013 exp->repeat[tok_idx][i + 1] = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002014 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02002015 exp->repeat[tok_idx] = calloc(2, sizeof *exp->repeat[tok_idx]);
2016 LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), );
Michal Vasko831e3bd2023-04-24 10:43:38 +02002017 exp->repeat[tok_idx][0] = repeat_expr_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002018 }
2019}
2020
2021/**
2022 * @brief Reparse Predicate. Logs directly on error.
2023 *
2024 * [7] Predicate ::= '[' Expr ']'
2025 *
2026 * @param[in] ctx Context for logging.
2027 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002028 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002029 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002030 * @return LY_ERR
2031 */
2032static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02002033reparse_predicate(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint32_t *tok_idx, uint32_t depth)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002034{
2035 LY_ERR rc;
2036
Michal Vasko004d3152020-06-11 19:59:22 +02002037 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002038 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002039 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002040
aPiecekbf968d92021-05-27 14:35:05 +02002041 rc = reparse_or_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002042 LY_CHECK_RET(rc);
2043
Michal Vasko004d3152020-06-11 19:59:22 +02002044 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002045 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002046 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002047
2048 return LY_SUCCESS;
2049}
2050
2051/**
2052 * @brief Reparse RelativeLocationPath. Logs directly on error.
2053 *
2054 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
2055 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
2056 * [6] NodeTest ::= NameTest | NodeType '(' ')'
2057 *
2058 * @param[in] ctx Context for logging.
2059 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002060 * @param[in] tok_idx Position in the expression \p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002061 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002062 * @return LY_ERR (LY_EINCOMPLETE on forward reference)
2063 */
2064static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02002065reparse_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 +02002066{
2067 LY_ERR rc;
2068
Michal Vasko004d3152020-06-11 19:59:22 +02002069 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002070 LY_CHECK_RET(rc);
2071
2072 goto step;
2073 do {
2074 /* '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02002075 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002076
Michal Vasko004d3152020-06-11 19:59:22 +02002077 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002078 LY_CHECK_RET(rc);
2079step:
2080 /* Step */
Michal Vasko004d3152020-06-11 19:59:22 +02002081 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002082 case LYXP_TOKEN_DOT:
Michal Vasko004d3152020-06-11 19:59:22 +02002083 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002084 break;
2085
2086 case LYXP_TOKEN_DDOT:
Michal Vasko004d3152020-06-11 19:59:22 +02002087 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002088 break;
2089
Michal Vasko49fec8e2022-05-24 10:28:33 +02002090 case LYXP_TOKEN_AXISNAME:
2091 ++(*tok_idx);
2092
2093 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_DCOLON);
2094 LY_CHECK_RET(rc);
2095
2096 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02002097 case LYXP_TOKEN_AT:
Michal Vasko004d3152020-06-11 19:59:22 +02002098 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002099
Michal Vasko004d3152020-06-11 19:59:22 +02002100 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002101 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002102 if ((exp->tokens[*tok_idx] != LYXP_TOKEN_NAMETEST) && (exp->tokens[*tok_idx] != LYXP_TOKEN_NODETYPE)) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02002103 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 +02002104 return LY_EVALID;
2105 }
Michal Vasko49fec8e2022-05-24 10:28:33 +02002106 if (exp->tokens[*tok_idx] == LYXP_TOKEN_NODETYPE) {
2107 goto reparse_nodetype;
2108 }
Radek Krejci0f969882020-08-21 16:56:47 +02002109 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02002110 case LYXP_TOKEN_NAMETEST:
Michal Vasko004d3152020-06-11 19:59:22 +02002111 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002112 goto reparse_predicate;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002113
2114 case LYXP_TOKEN_NODETYPE:
Michal Vasko49fec8e2022-05-24 10:28:33 +02002115reparse_nodetype:
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_PAR1);
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
2123 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02002124 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002125 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002126 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002127
2128reparse_predicate:
2129 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002130 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
aPiecekbf968d92021-05-27 14:35:05 +02002131 rc = reparse_predicate(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002132 LY_CHECK_RET(rc);
2133 }
2134 break;
2135 default:
Michal Vasko49fec8e2022-05-24 10:28:33 +02002136 LOGVAL(ctx, LY_VCODE_XP_INTOK, lyxp_token2str(exp->tokens[*tok_idx]), &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002137 return LY_EVALID;
2138 }
Michal Vasko004d3152020-06-11 19:59:22 +02002139 } while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02002140
2141 return LY_SUCCESS;
2142}
2143
2144/**
2145 * @brief Reparse AbsoluteLocationPath. Logs directly on error.
2146 *
2147 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
2148 *
2149 * @param[in] ctx Context for logging.
2150 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002151 * @param[in] tok_idx Position in the expression \p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002152 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002153 * @return LY_ERR
2154 */
2155static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02002156reparse_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 +02002157{
2158 LY_ERR rc;
2159
Michal Vasko004d3152020-06-11 19:59:22 +02002160 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 +02002161
2162 /* '/' RelativeLocationPath? */
Michal Vasko004d3152020-06-11 19:59:22 +02002163 if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002164 /* '/' */
Michal Vasko004d3152020-06-11 19:59:22 +02002165 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002166
Michal Vasko004d3152020-06-11 19:59:22 +02002167 if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002168 return LY_SUCCESS;
2169 }
Michal Vasko004d3152020-06-11 19:59:22 +02002170 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002171 case LYXP_TOKEN_DOT:
2172 case LYXP_TOKEN_DDOT:
Michal Vasko49fec8e2022-05-24 10:28:33 +02002173 case LYXP_TOKEN_AXISNAME:
Michal Vasko03ff5a72019-09-11 13:49:33 +02002174 case LYXP_TOKEN_AT:
2175 case LYXP_TOKEN_NAMETEST:
2176 case LYXP_TOKEN_NODETYPE:
aPiecekbf968d92021-05-27 14:35:05 +02002177 rc = reparse_relative_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002178 LY_CHECK_RET(rc);
Radek Krejci0f969882020-08-21 16:56:47 +02002179 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02002180 default:
2181 break;
2182 }
2183
Michal Vasko03ff5a72019-09-11 13:49:33 +02002184 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02002185 /* '//' RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002186 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002187
aPiecekbf968d92021-05-27 14:35:05 +02002188 rc = reparse_relative_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002189 LY_CHECK_RET(rc);
2190 }
2191
2192 return LY_SUCCESS;
2193}
2194
2195/**
2196 * @brief Reparse FunctionCall. Logs directly on error.
2197 *
2198 * [9] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
2199 *
2200 * @param[in] ctx Context for logging.
2201 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002202 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002203 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002204 * @return LY_ERR
2205 */
2206static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02002207reparse_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 +02002208{
Radek Krejci1deb5be2020-08-26 16:43:36 +02002209 int8_t min_arg_count = -1;
Michal Vaskodd528af2022-08-08 14:35:07 +02002210 uint32_t arg_count, max_arg_count = 0, func_tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002211 LY_ERR rc;
2212
Michal Vasko004d3152020-06-11 19:59:22 +02002213 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_FUNCNAME);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002214 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002215 func_tok_idx = *tok_idx;
2216 switch (exp->tok_len[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002217 case 3:
Michal Vasko004d3152020-06-11 19:59:22 +02002218 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002219 min_arg_count = 1;
2220 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002221 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002222 min_arg_count = 1;
2223 max_arg_count = 1;
2224 }
2225 break;
2226 case 4:
Michal Vasko004d3152020-06-11 19:59:22 +02002227 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002228 min_arg_count = 1;
2229 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002230 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002231 min_arg_count = 0;
2232 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002233 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002234 min_arg_count = 0;
2235 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002236 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002237 min_arg_count = 0;
2238 max_arg_count = 0;
2239 }
2240 break;
2241 case 5:
Michal Vasko004d3152020-06-11 19:59:22 +02002242 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002243 min_arg_count = 1;
2244 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002245 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002246 min_arg_count = 0;
2247 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002248 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002249 min_arg_count = 1;
2250 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002251 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002252 min_arg_count = 1;
2253 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002254 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002255 min_arg_count = 1;
2256 max_arg_count = 1;
2257 }
2258 break;
2259 case 6:
Michal Vasko004d3152020-06-11 19:59:22 +02002260 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002261 min_arg_count = 2;
Radek Krejci1deb5be2020-08-26 16:43:36 +02002262 max_arg_count = UINT32_MAX;
Michal Vasko004d3152020-06-11 19:59:22 +02002263 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002264 min_arg_count = 0;
2265 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002266 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002267 min_arg_count = 0;
2268 max_arg_count = 1;
2269 }
2270 break;
2271 case 7:
Michal Vasko004d3152020-06-11 19:59:22 +02002272 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002273 min_arg_count = 1;
2274 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002275 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002276 min_arg_count = 1;
2277 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002278 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002279 min_arg_count = 0;
2280 max_arg_count = 0;
2281 }
2282 break;
2283 case 8:
Michal Vasko004d3152020-06-11 19:59:22 +02002284 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002285 min_arg_count = 2;
2286 max_arg_count = 2;
Michal Vasko004d3152020-06-11 19:59:22 +02002287 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002288 min_arg_count = 0;
2289 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002290 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "re-match", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002291 min_arg_count = 2;
2292 max_arg_count = 2;
2293 }
2294 break;
2295 case 9:
Michal Vasko004d3152020-06-11 19:59:22 +02002296 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002297 min_arg_count = 2;
2298 max_arg_count = 3;
Michal Vasko004d3152020-06-11 19:59:22 +02002299 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002300 min_arg_count = 3;
2301 max_arg_count = 3;
2302 }
2303 break;
2304 case 10:
Michal Vasko004d3152020-06-11 19:59:22 +02002305 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002306 min_arg_count = 0;
2307 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002308 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002309 min_arg_count = 1;
2310 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002311 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002312 min_arg_count = 2;
2313 max_arg_count = 2;
2314 }
2315 break;
2316 case 11:
Michal Vasko004d3152020-06-11 19:59:22 +02002317 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002318 min_arg_count = 2;
2319 max_arg_count = 2;
2320 }
2321 break;
2322 case 12:
Michal Vasko004d3152020-06-11 19:59:22 +02002323 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002324 min_arg_count = 2;
2325 max_arg_count = 2;
2326 }
2327 break;
2328 case 13:
Michal Vasko004d3152020-06-11 19:59:22 +02002329 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002330 min_arg_count = 0;
2331 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002332 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string-length", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002333 min_arg_count = 0;
2334 max_arg_count = 1;
2335 }
2336 break;
2337 case 15:
Michal Vasko004d3152020-06-11 19:59:22 +02002338 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002339 min_arg_count = 0;
2340 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002341 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-after", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002342 min_arg_count = 2;
2343 max_arg_count = 2;
2344 }
2345 break;
2346 case 16:
Michal Vasko004d3152020-06-11 19:59:22 +02002347 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002348 min_arg_count = 2;
2349 max_arg_count = 2;
2350 }
2351 break;
2352 case 20:
Michal Vasko004d3152020-06-11 19:59:22 +02002353 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from-or-self", 20)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002354 min_arg_count = 2;
2355 max_arg_count = 2;
2356 }
2357 break;
2358 }
2359 if (min_arg_count == -1) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002360 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 +02002361 return LY_EINVAL;
2362 }
Michal Vasko004d3152020-06-11 19:59:22 +02002363 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002364
2365 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02002366 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002367 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002368 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002369
2370 /* ( Expr ( ',' Expr )* )? */
2371 arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002372 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002373 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002374 if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002375 ++arg_count;
aPiecekbf968d92021-05-27 14:35:05 +02002376 rc = reparse_or_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002377 LY_CHECK_RET(rc);
2378 }
Michal Vasko004d3152020-06-11 19:59:22 +02002379 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) {
2380 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002381
2382 ++arg_count;
aPiecekbf968d92021-05-27 14:35:05 +02002383 rc = reparse_or_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002384 LY_CHECK_RET(rc);
2385 }
2386
2387 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02002388 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002389 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002390 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002391
Radek Krejci857189e2020-09-01 13:26:36 +02002392 if ((arg_count < (uint32_t)min_arg_count) || (arg_count > max_arg_count)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002393 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 +02002394 return LY_EVALID;
2395 }
2396
2397 return LY_SUCCESS;
2398}
2399
2400/**
2401 * @brief Reparse PathExpr. Logs directly on error.
2402 *
2403 * [10] PathExpr ::= LocationPath | PrimaryExpr Predicate*
2404 * | PrimaryExpr Predicate* '/' RelativeLocationPath
2405 * | PrimaryExpr Predicate* '//' RelativeLocationPath
2406 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
aPiecekfba75362021-10-07 12:39:48 +02002407 * [8] PrimaryExpr ::= VariableReference | '(' Expr ')' | Literal | Number | FunctionCall
Michal Vasko03ff5a72019-09-11 13:49:33 +02002408 *
2409 * @param[in] ctx Context for logging.
2410 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002411 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002412 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002413 * @return LY_ERR
2414 */
2415static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02002416reparse_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 +02002417{
2418 LY_ERR rc;
2419
Michal Vasko004d3152020-06-11 19:59:22 +02002420 if (lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko14676352020-05-29 11:35:55 +02002421 return LY_EVALID;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002422 }
2423
Michal Vasko004d3152020-06-11 19:59:22 +02002424 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002425 case LYXP_TOKEN_PAR1:
2426 /* '(' Expr ')' Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002427 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002428
aPiecekbf968d92021-05-27 14:35:05 +02002429 rc = reparse_or_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002430 LY_CHECK_RET(rc);
2431
Michal Vasko004d3152020-06-11 19:59:22 +02002432 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002433 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002434 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002435 goto predicate;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002436 case LYXP_TOKEN_DOT:
2437 case LYXP_TOKEN_DDOT:
Michal Vasko49fec8e2022-05-24 10:28:33 +02002438 case LYXP_TOKEN_AXISNAME:
Michal Vasko03ff5a72019-09-11 13:49:33 +02002439 case LYXP_TOKEN_AT:
2440 case LYXP_TOKEN_NAMETEST:
2441 case LYXP_TOKEN_NODETYPE:
2442 /* RelativeLocationPath */
aPiecekbf968d92021-05-27 14:35:05 +02002443 rc = reparse_relative_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002444 LY_CHECK_RET(rc);
2445 break;
aPiecekfba75362021-10-07 12:39:48 +02002446 case LYXP_TOKEN_VARREF:
2447 /* VariableReference */
2448 ++(*tok_idx);
2449 goto predicate;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002450 case LYXP_TOKEN_FUNCNAME:
2451 /* FunctionCall */
aPiecekbf968d92021-05-27 14:35:05 +02002452 rc = reparse_function_call(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002453 LY_CHECK_RET(rc);
2454 goto predicate;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002455 case LYXP_TOKEN_OPER_PATH:
2456 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +02002457 /* AbsoluteLocationPath */
aPiecekbf968d92021-05-27 14:35:05 +02002458 rc = reparse_absolute_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002459 LY_CHECK_RET(rc);
2460 break;
2461 case LYXP_TOKEN_LITERAL:
2462 /* Literal */
Michal Vasko004d3152020-06-11 19:59:22 +02002463 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002464 goto predicate;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002465 case LYXP_TOKEN_NUMBER:
2466 /* Number */
Michal Vasko004d3152020-06-11 19:59:22 +02002467 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002468 goto predicate;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002469 default:
Michal Vasko49fec8e2022-05-24 10:28:33 +02002470 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 +02002471 return LY_EVALID;
2472 }
2473
2474 return LY_SUCCESS;
2475
2476predicate:
2477 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002478 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
aPiecekbf968d92021-05-27 14:35:05 +02002479 rc = reparse_predicate(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002480 LY_CHECK_RET(rc);
2481 }
2482
2483 /* ('/' or '//') RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002484 if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002485
2486 /* '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02002487 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002488
aPiecekbf968d92021-05-27 14:35:05 +02002489 rc = reparse_relative_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002490 LY_CHECK_RET(rc);
2491 }
2492
2493 return LY_SUCCESS;
2494}
2495
2496/**
2497 * @brief Reparse UnaryExpr. Logs directly on error.
2498 *
2499 * [17] UnaryExpr ::= UnionExpr | '-' UnaryExpr
2500 * [18] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
2501 *
2502 * @param[in] ctx Context for logging.
2503 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002504 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002505 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002506 * @return LY_ERR
2507 */
2508static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02002509reparse_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 +02002510{
Michal Vaskodd528af2022-08-08 14:35:07 +02002511 uint32_t prev_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002512 LY_ERR rc;
2513
2514 /* ('-')* */
Michal Vasko004d3152020-06-11 19:59:22 +02002515 prev_exp = *tok_idx;
Michal Vasko69730152020-10-09 16:30:07 +02002516 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2517 (exp->expr[exp->tok_pos[*tok_idx]] == '-')) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002518 exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNARY);
Michal Vasko004d3152020-06-11 19:59:22 +02002519 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002520 }
2521
2522 /* PathExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002523 prev_exp = *tok_idx;
aPiecekbf968d92021-05-27 14:35:05 +02002524 rc = reparse_path_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002525 LY_CHECK_RET(rc);
2526
2527 /* ('|' PathExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002528 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_UNI)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002529 exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNION);
Michal Vasko004d3152020-06-11 19:59:22 +02002530 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002531
aPiecekbf968d92021-05-27 14:35:05 +02002532 rc = reparse_path_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002533 LY_CHECK_RET(rc);
2534 }
2535
2536 return LY_SUCCESS;
2537}
2538
2539/**
2540 * @brief Reparse AdditiveExpr. Logs directly on error.
2541 *
2542 * [15] AdditiveExpr ::= MultiplicativeExpr
2543 * | AdditiveExpr '+' MultiplicativeExpr
2544 * | AdditiveExpr '-' MultiplicativeExpr
2545 * [16] MultiplicativeExpr ::= UnaryExpr
2546 * | MultiplicativeExpr '*' UnaryExpr
2547 * | MultiplicativeExpr 'div' UnaryExpr
2548 * | MultiplicativeExpr 'mod' UnaryExpr
2549 *
2550 * @param[in] ctx Context for logging.
2551 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002552 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002553 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002554 * @return LY_ERR
2555 */
2556static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02002557reparse_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 +02002558{
Michal Vaskodd528af2022-08-08 14:35:07 +02002559 uint32_t prev_add_exp, prev_mul_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002560 LY_ERR rc;
2561
Michal Vasko004d3152020-06-11 19:59:22 +02002562 prev_add_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002563 goto reparse_multiplicative_expr;
2564
2565 /* ('+' / '-' MultiplicativeExpr)* */
Michal Vasko69730152020-10-09 16:30:07 +02002566 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2567 ((exp->expr[exp->tok_pos[*tok_idx]] == '+') || (exp->expr[exp->tok_pos[*tok_idx]] == '-'))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002568 exp_repeat_push(exp, prev_add_exp, LYXP_EXPR_ADDITIVE);
Michal Vasko004d3152020-06-11 19:59:22 +02002569 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002570
2571reparse_multiplicative_expr:
2572 /* UnaryExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002573 prev_mul_exp = *tok_idx;
aPiecekbf968d92021-05-27 14:35:05 +02002574 rc = reparse_unary_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002575 LY_CHECK_RET(rc);
2576
2577 /* ('*' / 'div' / 'mod' UnaryExpr)* */
Michal Vasko69730152020-10-09 16:30:07 +02002578 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2579 ((exp->expr[exp->tok_pos[*tok_idx]] == '*') || (exp->tok_len[*tok_idx] == 3))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002580 exp_repeat_push(exp, prev_mul_exp, LYXP_EXPR_MULTIPLICATIVE);
Michal Vasko004d3152020-06-11 19:59:22 +02002581 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002582
aPiecekbf968d92021-05-27 14:35:05 +02002583 rc = reparse_unary_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002584 LY_CHECK_RET(rc);
2585 }
2586 }
2587
2588 return LY_SUCCESS;
2589}
2590
2591/**
2592 * @brief Reparse EqualityExpr. Logs directly on error.
2593 *
2594 * [13] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
2595 * | EqualityExpr '!=' RelationalExpr
2596 * [14] RelationalExpr ::= AdditiveExpr
2597 * | RelationalExpr '<' AdditiveExpr
2598 * | RelationalExpr '>' AdditiveExpr
2599 * | RelationalExpr '<=' AdditiveExpr
2600 * | RelationalExpr '>=' AdditiveExpr
2601 *
2602 * @param[in] ctx Context for logging.
2603 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002604 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002605 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002606 * @return LY_ERR
2607 */
2608static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02002609reparse_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 +02002610{
Michal Vaskodd528af2022-08-08 14:35:07 +02002611 uint32_t prev_eq_exp, prev_rel_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002612 LY_ERR rc;
2613
Michal Vasko004d3152020-06-11 19:59:22 +02002614 prev_eq_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002615 goto reparse_additive_expr;
2616
2617 /* ('=' / '!=' RelationalExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002618 while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_EQUAL, LYXP_TOKEN_OPER_NEQUAL)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002619 exp_repeat_push(exp, prev_eq_exp, LYXP_EXPR_EQUALITY);
Michal Vasko004d3152020-06-11 19:59:22 +02002620 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002621
2622reparse_additive_expr:
2623 /* AdditiveExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002624 prev_rel_exp = *tok_idx;
aPiecekbf968d92021-05-27 14:35:05 +02002625 rc = reparse_additive_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002626 LY_CHECK_RET(rc);
2627
2628 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002629 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_COMP)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002630 exp_repeat_push(exp, prev_rel_exp, LYXP_EXPR_RELATIONAL);
Michal Vasko004d3152020-06-11 19:59:22 +02002631 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002632
aPiecekbf968d92021-05-27 14:35:05 +02002633 rc = reparse_additive_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002634 LY_CHECK_RET(rc);
2635 }
2636 }
2637
2638 return LY_SUCCESS;
2639}
2640
2641/**
2642 * @brief Reparse OrExpr. Logs directly on error.
2643 *
2644 * [11] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
2645 * [12] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
2646 *
2647 * @param[in] ctx Context for logging.
2648 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002649 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002650 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002651 * @return LY_ERR
2652 */
2653static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02002654reparse_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 +02002655{
Michal Vaskodd528af2022-08-08 14:35:07 +02002656 uint32_t prev_or_exp, prev_and_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002657 LY_ERR rc;
2658
aPiecekbf968d92021-05-27 14:35:05 +02002659 ++depth;
2660 LY_CHECK_ERR_RET(depth > LYXP_MAX_BLOCK_DEPTH, LOGVAL(ctx, LY_VCODE_XP_DEPTH), LY_EINVAL);
2661
Michal Vasko004d3152020-06-11 19:59:22 +02002662 prev_or_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002663 goto reparse_equality_expr;
2664
2665 /* ('or' AndExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002666 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 +02002667 exp_repeat_push(exp, prev_or_exp, LYXP_EXPR_OR);
Michal Vasko004d3152020-06-11 19:59:22 +02002668 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002669
2670reparse_equality_expr:
2671 /* EqualityExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002672 prev_and_exp = *tok_idx;
aPiecekbf968d92021-05-27 14:35:05 +02002673 rc = reparse_equality_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002674 LY_CHECK_RET(rc);
2675
2676 /* ('and' EqualityExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002677 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 +02002678 exp_repeat_push(exp, prev_and_exp, LYXP_EXPR_AND);
Michal Vasko004d3152020-06-11 19:59:22 +02002679 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002680
aPiecekbf968d92021-05-27 14:35:05 +02002681 rc = reparse_equality_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002682 LY_CHECK_RET(rc);
2683 }
2684 }
2685
2686 return LY_SUCCESS;
2687}
Radek Krejcib1646a92018-11-02 16:08:26 +01002688
2689/**
2690 * @brief Parse NCName.
2691 *
2692 * @param[in] ncname Name to parse.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002693 * @return Length of @p ncname valid bytes.
Radek Krejcib1646a92018-11-02 16:08:26 +01002694 */
Michal Vasko49fec8e2022-05-24 10:28:33 +02002695static ssize_t
Radek Krejcib1646a92018-11-02 16:08:26 +01002696parse_ncname(const char *ncname)
2697{
Radek Krejci1deb5be2020-08-26 16:43:36 +02002698 uint32_t uc;
Radek Krejcid4270262019-01-07 15:07:25 +01002699 size_t size;
Michal Vasko49fec8e2022-05-24 10:28:33 +02002700 ssize_t len = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01002701
2702 LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), 0);
2703 if (!is_xmlqnamestartchar(uc) || (uc == ':')) {
2704 return len;
2705 }
2706
2707 do {
2708 len += size;
Radek Krejci9a564c92019-01-07 14:53:57 +01002709 if (!*ncname) {
2710 break;
2711 }
Radek Krejcid4270262019-01-07 15:07:25 +01002712 LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), -len);
Radek Krejcib1646a92018-11-02 16:08:26 +01002713 } while (is_xmlqnamechar(uc) && (uc != ':'));
2714
2715 return len;
2716}
2717
2718/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02002719 * @brief Add @p token into the expression @p exp.
Radek Krejcib1646a92018-11-02 16:08:26 +01002720 *
Michal Vasko03ff5a72019-09-11 13:49:33 +02002721 * @param[in] ctx Context for logging.
Radek Krejcib1646a92018-11-02 16:08:26 +01002722 * @param[in] exp Expression to use.
2723 * @param[in] token Token to add.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002724 * @param[in] tok_pos Token position in the XPath expression.
Radek Krejcib1646a92018-11-02 16:08:26 +01002725 * @param[in] tok_len Token length in the XPath expression.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002726 * @return LY_ERR
Radek Krejcib1646a92018-11-02 16:08:26 +01002727 */
2728static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02002729exp_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 +01002730{
2731 uint32_t prev;
2732
2733 if (exp->used == exp->size) {
2734 prev = exp->size;
2735 exp->size += LYXP_EXPR_SIZE_STEP;
2736 if (prev > exp->size) {
2737 LOGINT(ctx);
2738 return LY_EINT;
2739 }
2740
2741 exp->tokens = ly_realloc(exp->tokens, exp->size * sizeof *exp->tokens);
2742 LY_CHECK_ERR_RET(!exp->tokens, LOGMEM(ctx), LY_EMEM);
2743 exp->tok_pos = ly_realloc(exp->tok_pos, exp->size * sizeof *exp->tok_pos);
2744 LY_CHECK_ERR_RET(!exp->tok_pos, LOGMEM(ctx), LY_EMEM);
2745 exp->tok_len = ly_realloc(exp->tok_len, exp->size * sizeof *exp->tok_len);
2746 LY_CHECK_ERR_RET(!exp->tok_len, LOGMEM(ctx), LY_EMEM);
2747 }
2748
2749 exp->tokens[exp->used] = token;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002750 exp->tok_pos[exp->used] = tok_pos;
Radek Krejcib1646a92018-11-02 16:08:26 +01002751 exp->tok_len[exp->used] = tok_len;
2752 ++exp->used;
2753 return LY_SUCCESS;
2754}
2755
2756void
Michal Vasko14676352020-05-29 11:35:55 +02002757lyxp_expr_free(const struct ly_ctx *ctx, struct lyxp_expr *expr)
Radek Krejcib1646a92018-11-02 16:08:26 +01002758{
Michal Vaskodd528af2022-08-08 14:35:07 +02002759 uint32_t i;
Radek Krejcib1646a92018-11-02 16:08:26 +01002760
2761 if (!expr) {
2762 return;
2763 }
2764
2765 lydict_remove(ctx, expr->expr);
2766 free(expr->tokens);
2767 free(expr->tok_pos);
2768 free(expr->tok_len);
2769 if (expr->repeat) {
2770 for (i = 0; i < expr->used; ++i) {
2771 free(expr->repeat[i]);
2772 }
2773 }
2774 free(expr->repeat);
2775 free(expr);
2776}
2777
Michal Vasko49fec8e2022-05-24 10:28:33 +02002778/**
2779 * @brief Parse Axis name.
2780 *
2781 * @param[in] str String to parse.
2782 * @param[in] str_len Length of @p str.
2783 * @return LY_SUCCESS if an axis.
2784 * @return LY_ENOT otherwise.
2785 */
2786static LY_ERR
2787expr_parse_axis(const char *str, size_t str_len)
2788{
2789 switch (str_len) {
2790 case 4:
2791 if (!strncmp("self", str, str_len)) {
2792 return LY_SUCCESS;
2793 }
2794 break;
2795 case 5:
2796 if (!strncmp("child", str, str_len)) {
2797 return LY_SUCCESS;
2798 }
2799 break;
2800 case 6:
2801 if (!strncmp("parent", str, str_len)) {
2802 return LY_SUCCESS;
2803 }
2804 break;
2805 case 8:
2806 if (!strncmp("ancestor", str, str_len)) {
2807 return LY_SUCCESS;
2808 }
2809 break;
2810 case 9:
2811 if (!strncmp("attribute", str, str_len)) {
2812 return LY_SUCCESS;
2813 } else if (!strncmp("following", str, str_len)) {
2814 return LY_SUCCESS;
2815 } else if (!strncmp("namespace", str, str_len)) {
2816 LOGERR(NULL, LY_EINVAL, "Axis \"namespace\" not supported.");
2817 return LY_ENOT;
2818 } else if (!strncmp("preceding", str, str_len)) {
2819 return LY_SUCCESS;
2820 }
2821 break;
2822 case 10:
2823 if (!strncmp("descendant", str, str_len)) {
2824 return LY_SUCCESS;
2825 }
2826 break;
2827 case 16:
2828 if (!strncmp("ancestor-or-self", str, str_len)) {
2829 return LY_SUCCESS;
2830 }
2831 break;
2832 case 17:
2833 if (!strncmp("following-sibling", str, str_len)) {
2834 return LY_SUCCESS;
2835 } else if (!strncmp("preceding-sibling", str, str_len)) {
2836 return LY_SUCCESS;
2837 }
2838 break;
2839 case 18:
2840 if (!strncmp("descendant-or-self", str, str_len)) {
2841 return LY_SUCCESS;
2842 }
2843 break;
2844 }
2845
2846 return LY_ENOT;
2847}
2848
Radek Krejcif03a9e22020-09-18 20:09:31 +02002849LY_ERR
2850lyxp_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 +01002851{
Radek Krejcif03a9e22020-09-18 20:09:31 +02002852 LY_ERR ret = LY_SUCCESS;
2853 struct lyxp_expr *expr;
Radek Krejcid4270262019-01-07 15:07:25 +01002854 size_t parsed = 0, tok_len;
Radek Krejcib1646a92018-11-02 16:08:26 +01002855 enum lyxp_token tok_type;
Michal Vasko49fec8e2022-05-24 10:28:33 +02002856 ly_bool prev_func_check = 0, prev_ntype_check = 0, has_axis;
Michal Vaskodd528af2022-08-08 14:35:07 +02002857 uint32_t tok_idx = 0;
Michal Vasko49fec8e2022-05-24 10:28:33 +02002858 ssize_t ncname_len;
Radek Krejcib1646a92018-11-02 16:08:26 +01002859
Radek Krejcif03a9e22020-09-18 20:09:31 +02002860 assert(expr_p);
2861
2862 if (!expr_str[0]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002863 LOGVAL(ctx, LY_VCODE_XP_EOF);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002864 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02002865 }
2866
2867 if (!expr_len) {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002868 expr_len = strlen(expr_str);
Michal Vasko004d3152020-06-11 19:59:22 +02002869 }
Michal Vaskodd528af2022-08-08 14:35:07 +02002870 if (expr_len > UINT32_MAX) {
2871 LOGVAL(ctx, LYVE_XPATH, "XPath expression cannot be longer than %" PRIu32 " characters.", UINT32_MAX);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002872 return LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01002873 }
2874
2875 /* init lyxp_expr structure */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002876 expr = calloc(1, sizeof *expr);
2877 LY_CHECK_ERR_GOTO(!expr, LOGMEM(ctx); ret = LY_EMEM, error);
2878 LY_CHECK_GOTO(ret = lydict_insert(ctx, expr_str, expr_len, &expr->expr), error);
2879 expr->used = 0;
2880 expr->size = LYXP_EXPR_SIZE_START;
2881 expr->tokens = malloc(expr->size * sizeof *expr->tokens);
2882 LY_CHECK_ERR_GOTO(!expr->tokens, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002883
Radek Krejcif03a9e22020-09-18 20:09:31 +02002884 expr->tok_pos = malloc(expr->size * sizeof *expr->tok_pos);
2885 LY_CHECK_ERR_GOTO(!expr->tok_pos, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002886
Radek Krejcif03a9e22020-09-18 20:09:31 +02002887 expr->tok_len = malloc(expr->size * sizeof *expr->tok_len);
2888 LY_CHECK_ERR_GOTO(!expr->tok_len, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002889
Michal Vasko004d3152020-06-11 19:59:22 +02002890 /* make expr 0-terminated */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002891 expr_str = expr->expr;
Michal Vasko004d3152020-06-11 19:59:22 +02002892
Radek Krejcif03a9e22020-09-18 20:09:31 +02002893 while (is_xmlws(expr_str[parsed])) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002894 ++parsed;
2895 }
2896
2897 do {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002898 if (expr_str[parsed] == '(') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002899
2900 /* '(' */
2901 tok_len = 1;
2902 tok_type = LYXP_TOKEN_PAR1;
2903
Michal Vasko49fec8e2022-05-24 10:28:33 +02002904 if (prev_ntype_check && expr->used && (expr->tokens[expr->used - 1] == LYXP_TOKEN_NAMETEST) &&
2905 (((expr->tok_len[expr->used - 1] == 4) &&
2906 (!strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "node", 4) ||
2907 !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "text", 4))) ||
2908 ((expr->tok_len[expr->used - 1] == 7) &&
2909 !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "comment", 7)))) {
2910 /* it is NodeType after all */
2911 expr->tokens[expr->used - 1] = LYXP_TOKEN_NODETYPE;
2912
2913 prev_ntype_check = 0;
2914 prev_func_check = 0;
2915 } else if (prev_func_check && expr->used && (expr->tokens[expr->used - 1] == LYXP_TOKEN_NAMETEST)) {
2916 /* it is FunctionName after all */
2917 expr->tokens[expr->used - 1] = LYXP_TOKEN_FUNCNAME;
2918
2919 prev_ntype_check = 0;
2920 prev_func_check = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01002921 }
2922
Radek Krejcif03a9e22020-09-18 20:09:31 +02002923 } else if (expr_str[parsed] == ')') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002924
2925 /* ')' */
2926 tok_len = 1;
2927 tok_type = LYXP_TOKEN_PAR2;
2928
Radek Krejcif03a9e22020-09-18 20:09:31 +02002929 } else if (expr_str[parsed] == '[') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002930
2931 /* '[' */
2932 tok_len = 1;
2933 tok_type = LYXP_TOKEN_BRACK1;
2934
Radek Krejcif03a9e22020-09-18 20:09:31 +02002935 } else if (expr_str[parsed] == ']') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002936
2937 /* ']' */
2938 tok_len = 1;
2939 tok_type = LYXP_TOKEN_BRACK2;
2940
Radek Krejcif03a9e22020-09-18 20:09:31 +02002941 } else if (!strncmp(&expr_str[parsed], "..", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002942
2943 /* '..' */
2944 tok_len = 2;
2945 tok_type = LYXP_TOKEN_DDOT;
2946
Radek Krejcif03a9e22020-09-18 20:09:31 +02002947 } else if ((expr_str[parsed] == '.') && (!isdigit(expr_str[parsed + 1]))) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002948
2949 /* '.' */
2950 tok_len = 1;
2951 tok_type = LYXP_TOKEN_DOT;
2952
Radek Krejcif03a9e22020-09-18 20:09:31 +02002953 } else if (expr_str[parsed] == '@') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002954
2955 /* '@' */
2956 tok_len = 1;
2957 tok_type = LYXP_TOKEN_AT;
2958
Radek Krejcif03a9e22020-09-18 20:09:31 +02002959 } else if (expr_str[parsed] == ',') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002960
2961 /* ',' */
2962 tok_len = 1;
2963 tok_type = LYXP_TOKEN_COMMA;
2964
Radek Krejcif03a9e22020-09-18 20:09:31 +02002965 } else if (expr_str[parsed] == '\'') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002966
2967 /* Literal with ' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002968 for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\''); ++tok_len) {}
2969 LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0',
Radek Krejci2efc45b2020-12-22 16:25:44 +01002970 LOGVAL(ctx, LY_VCODE_XP_EOE, expr_str[parsed], &expr_str[parsed]); ret = LY_EVALID,
Michal Vasko69730152020-10-09 16:30:07 +02002971 error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002972 ++tok_len;
2973 tok_type = LYXP_TOKEN_LITERAL;
2974
Radek Krejcif03a9e22020-09-18 20:09:31 +02002975 } else if (expr_str[parsed] == '\"') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002976
2977 /* Literal with " */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002978 for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\"'); ++tok_len) {}
2979 LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0',
Radek Krejci2efc45b2020-12-22 16:25:44 +01002980 LOGVAL(ctx, LY_VCODE_XP_EOE, expr_str[parsed], &expr_str[parsed]); ret = LY_EVALID,
Michal Vasko69730152020-10-09 16:30:07 +02002981 error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002982 ++tok_len;
2983 tok_type = LYXP_TOKEN_LITERAL;
2984
Radek Krejcif03a9e22020-09-18 20:09:31 +02002985 } else if ((expr_str[parsed] == '.') || (isdigit(expr_str[parsed]))) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002986
2987 /* Number */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002988 for (tok_len = 0; isdigit(expr_str[parsed + tok_len]); ++tok_len) {}
2989 if (expr_str[parsed + tok_len] == '.') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002990 ++tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02002991 for ( ; isdigit(expr_str[parsed + tok_len]); ++tok_len) {}
Radek Krejcib1646a92018-11-02 16:08:26 +01002992 }
2993 tok_type = LYXP_TOKEN_NUMBER;
2994
aPiecekfba75362021-10-07 12:39:48 +02002995 } else if (expr_str[parsed] == '$') {
2996
2997 /* VariableReference */
2998 parsed++;
Michal Vasko49fec8e2022-05-24 10:28:33 +02002999 ncname_len = parse_ncname(&expr_str[parsed]);
aPiecekfba75362021-10-07 12:39:48 +02003000 LY_CHECK_ERR_GOTO(ncname_len < 1, LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed - ncname_len],
3001 parsed - ncname_len + 1, expr_str); ret = LY_EVALID, error);
3002 tok_len = ncname_len;
3003 LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == ':',
3004 LOGVAL(ctx, LYVE_XPATH, "Variable with prefix is not supported."); ret = LY_EVALID,
3005 error);
3006 tok_type = LYXP_TOKEN_VARREF;
3007
Radek Krejcif03a9e22020-09-18 20:09:31 +02003008 } else if (expr_str[parsed] == '/') {
Radek Krejcib1646a92018-11-02 16:08:26 +01003009
3010 /* Operator '/', '//' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02003011 if (!strncmp(&expr_str[parsed], "//", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01003012 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003013 tok_type = LYXP_TOKEN_OPER_RPATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01003014 } else {
3015 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003016 tok_type = LYXP_TOKEN_OPER_PATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01003017 }
Radek Krejcib1646a92018-11-02 16:08:26 +01003018
Radek Krejcif03a9e22020-09-18 20:09:31 +02003019 } else if (!strncmp(&expr_str[parsed], "!=", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01003020
Michal Vasko3e48bf32020-06-01 08:39:07 +02003021 /* Operator '!=' */
Radek Krejcib1646a92018-11-02 16:08:26 +01003022 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003023 tok_type = LYXP_TOKEN_OPER_NEQUAL;
3024
Radek Krejcif03a9e22020-09-18 20:09:31 +02003025 } else if (!strncmp(&expr_str[parsed], "<=", 2) || !strncmp(&expr_str[parsed], ">=", 2)) {
Michal Vasko3e48bf32020-06-01 08:39:07 +02003026
3027 /* Operator '<=', '>=' */
3028 tok_len = 2;
3029 tok_type = LYXP_TOKEN_OPER_COMP;
Radek Krejcib1646a92018-11-02 16:08:26 +01003030
Radek Krejcif03a9e22020-09-18 20:09:31 +02003031 } else if (expr_str[parsed] == '|') {
Radek Krejcib1646a92018-11-02 16:08:26 +01003032
3033 /* Operator '|' */
3034 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003035 tok_type = LYXP_TOKEN_OPER_UNI;
Radek Krejcib1646a92018-11-02 16:08:26 +01003036
Radek Krejcif03a9e22020-09-18 20:09:31 +02003037 } else if ((expr_str[parsed] == '+') || (expr_str[parsed] == '-')) {
Radek Krejcib1646a92018-11-02 16:08:26 +01003038
3039 /* Operator '+', '-' */
3040 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003041 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01003042
Radek Krejcif03a9e22020-09-18 20:09:31 +02003043 } else if (expr_str[parsed] == '=') {
Radek Krejcib1646a92018-11-02 16:08:26 +01003044
Michal Vasko3e48bf32020-06-01 08:39:07 +02003045 /* Operator '=' */
Radek Krejcib1646a92018-11-02 16:08:26 +01003046 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003047 tok_type = LYXP_TOKEN_OPER_EQUAL;
3048
Radek Krejcif03a9e22020-09-18 20:09:31 +02003049 } else if ((expr_str[parsed] == '<') || (expr_str[parsed] == '>')) {
Michal Vasko3e48bf32020-06-01 08:39:07 +02003050
3051 /* Operator '<', '>' */
3052 tok_len = 1;
3053 tok_type = LYXP_TOKEN_OPER_COMP;
Radek Krejcib1646a92018-11-02 16:08:26 +01003054
Michal Vasko69730152020-10-09 16:30:07 +02003055 } else if (expr->used && (expr->tokens[expr->used - 1] != LYXP_TOKEN_AT) &&
3056 (expr->tokens[expr->used - 1] != LYXP_TOKEN_PAR1) &&
3057 (expr->tokens[expr->used - 1] != LYXP_TOKEN_BRACK1) &&
3058 (expr->tokens[expr->used - 1] != LYXP_TOKEN_COMMA) &&
3059 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_LOG) &&
3060 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_EQUAL) &&
3061 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_NEQUAL) &&
3062 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_COMP) &&
3063 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_MATH) &&
3064 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_UNI) &&
3065 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_PATH) &&
3066 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_RPATH)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01003067
3068 /* Operator '*', 'or', 'and', 'mod', or 'div' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02003069 if (expr_str[parsed] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01003070 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003071 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01003072
Radek Krejcif03a9e22020-09-18 20:09:31 +02003073 } else if (!strncmp(&expr_str[parsed], "or", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01003074 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003075 tok_type = LYXP_TOKEN_OPER_LOG;
Radek Krejcib1646a92018-11-02 16:08:26 +01003076
Radek Krejcif03a9e22020-09-18 20:09:31 +02003077 } else if (!strncmp(&expr_str[parsed], "and", 3)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01003078 tok_len = 3;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003079 tok_type = LYXP_TOKEN_OPER_LOG;
Radek Krejcib1646a92018-11-02 16:08:26 +01003080
Radek Krejcif03a9e22020-09-18 20:09:31 +02003081 } else if (!strncmp(&expr_str[parsed], "mod", 3) || !strncmp(&expr_str[parsed], "div", 3)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01003082 tok_len = 3;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003083 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01003084
Michal Vasko49fec8e2022-05-24 10:28:33 +02003085 } else if (prev_ntype_check || prev_func_check) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003086 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 +02003087 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 +02003088 ret = LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01003089 goto error;
3090 } else {
Michal Vasko774ce402021-04-14 15:35:06 +02003091 LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed], parsed + 1, expr_str);
Radek Krejcif03a9e22020-09-18 20:09:31 +02003092 ret = LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01003093 goto error;
3094 }
Radek Krejcib1646a92018-11-02 16:08:26 +01003095 } else {
3096
Michal Vasko49fec8e2022-05-24 10:28:33 +02003097 /* (AxisName '::')? ((NCName ':')? '*' | QName) or NodeType/FunctionName */
3098 if (expr_str[parsed] == '*') {
3099 ncname_len = 1;
3100 } else {
3101 ncname_len = parse_ncname(&expr_str[parsed]);
3102 LY_CHECK_ERR_GOTO(ncname_len < 1, LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed - ncname_len],
3103 parsed - ncname_len + 1, expr_str); ret = LY_EVALID, error);
3104 }
Radek Krejcib1646a92018-11-02 16:08:26 +01003105 tok_len = ncname_len;
3106
Michal Vasko49fec8e2022-05-24 10:28:33 +02003107 has_axis = 0;
3108 if (!strncmp(&expr_str[parsed + tok_len], "::", 2)) {
3109 /* axis */
3110 LY_CHECK_ERR_GOTO(expr_parse_axis(&expr_str[parsed], ncname_len),
3111 LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed], parsed + 1, expr_str); ret = LY_EVALID, error);
3112 tok_type = LYXP_TOKEN_AXISNAME;
3113
3114 LY_CHECK_GOTO(ret = exp_add_token(ctx, expr, tok_type, parsed, tok_len), error);
3115 parsed += tok_len;
3116
3117 /* '::' */
3118 tok_len = 2;
3119 tok_type = LYXP_TOKEN_DCOLON;
3120
3121 LY_CHECK_GOTO(ret = exp_add_token(ctx, expr, tok_type, parsed, tok_len), error);
3122 parsed += tok_len;
3123
3124 if (expr_str[parsed] == '*') {
3125 ncname_len = 1;
3126 } else {
3127 ncname_len = parse_ncname(&expr_str[parsed]);
3128 LY_CHECK_ERR_GOTO(ncname_len < 1, LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed - ncname_len],
3129 parsed - ncname_len + 1, expr_str); ret = LY_EVALID, error);
3130 }
3131 tok_len = ncname_len;
3132
3133 has_axis = 1;
3134 }
3135
Radek Krejcif03a9e22020-09-18 20:09:31 +02003136 if (expr_str[parsed + tok_len] == ':') {
Radek Krejcib1646a92018-11-02 16:08:26 +01003137 ++tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02003138 if (expr_str[parsed + tok_len] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01003139 ++tok_len;
3140 } else {
Radek Krejcif03a9e22020-09-18 20:09:31 +02003141 ncname_len = parse_ncname(&expr_str[parsed + tok_len]);
Michal Vaskoe2be5462021-08-04 10:49:42 +02003142 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 +02003143 parsed - ncname_len + 1, expr_str); ret = LY_EVALID, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01003144 tok_len += ncname_len;
3145 }
Michal Vasko49fec8e2022-05-24 10:28:33 +02003146 /* remove old flags to prevent ambiguities */
3147 prev_ntype_check = 0;
3148 prev_func_check = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01003149 tok_type = LYXP_TOKEN_NAMETEST;
3150 } else {
Michal Vasko49fec8e2022-05-24 10:28:33 +02003151 /* if not '*', there is no prefix so it can still be NodeType/FunctionName, we can't finally decide now */
3152 prev_ntype_check = (expr_str[parsed] == '*') ? 0 : 1;
3153 prev_func_check = (prev_ntype_check && !has_axis) ? 1 : 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01003154 tok_type = LYXP_TOKEN_NAMETEST;
3155 }
3156 }
3157
3158 /* store the token, move on to the next one */
Radek Krejcif03a9e22020-09-18 20:09:31 +02003159 LY_CHECK_GOTO(ret = exp_add_token(ctx, expr, tok_type, parsed, tok_len), error);
Radek Krejcib1646a92018-11-02 16:08:26 +01003160 parsed += tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02003161 while (is_xmlws(expr_str[parsed])) {
Radek Krejcib1646a92018-11-02 16:08:26 +01003162 ++parsed;
3163 }
3164
Radek Krejcif03a9e22020-09-18 20:09:31 +02003165 } while (expr_str[parsed]);
Radek Krejcib1646a92018-11-02 16:08:26 +01003166
Michal Vasko004d3152020-06-11 19:59:22 +02003167 if (reparse) {
3168 /* prealloc repeat */
Radek Krejcif03a9e22020-09-18 20:09:31 +02003169 expr->repeat = calloc(expr->size, sizeof *expr->repeat);
3170 LY_CHECK_ERR_GOTO(!expr->repeat, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01003171
Michal Vasko004d3152020-06-11 19:59:22 +02003172 /* fill repeat */
aPiecekbf968d92021-05-27 14:35:05 +02003173 LY_CHECK_ERR_GOTO(reparse_or_expr(ctx, expr, &tok_idx, 0), ret = LY_EVALID, error);
Radek Krejcif03a9e22020-09-18 20:09:31 +02003174 if (expr->used > tok_idx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003175 LOGVAL(ctx, LYVE_XPATH, "Unparsed characters \"%s\" left at the end of an XPath expression.",
Michal Vasko69730152020-10-09 16:30:07 +02003176 &expr->expr[expr->tok_pos[tok_idx]]);
Radek Krejcif03a9e22020-09-18 20:09:31 +02003177 ret = LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02003178 goto error;
3179 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003180 }
3181
Radek Krejcif03a9e22020-09-18 20:09:31 +02003182 print_expr_struct_debug(expr);
3183 *expr_p = expr;
3184 return LY_SUCCESS;
Radek Krejcib1646a92018-11-02 16:08:26 +01003185
3186error:
Radek Krejcif03a9e22020-09-18 20:09:31 +02003187 lyxp_expr_free(ctx, expr);
3188 return ret;
Radek Krejcib1646a92018-11-02 16:08:26 +01003189}
3190
Michal Vasko1734be92020-09-22 08:55:10 +02003191LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02003192lyxp_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 +02003193 struct lyxp_expr **dup_p)
Michal Vasko004d3152020-06-11 19:59:22 +02003194{
Michal Vasko1734be92020-09-22 08:55:10 +02003195 LY_ERR ret = LY_SUCCESS;
3196 struct lyxp_expr *dup = NULL;
Michal Vaskod59039d2022-09-09 09:07:30 +02003197 uint32_t used = 0, i, j, expr_len;
Michal Vaskoe33134a2022-07-29 14:54:40 +02003198 const char *expr_start;
3199
3200 assert((!start_idx && !end_idx) || ((start_idx < exp->used) && (end_idx < exp->used) && (start_idx <= end_idx)));
Michal Vasko004d3152020-06-11 19:59:22 +02003201
Michal Vasko7f45cf22020-10-01 12:49:44 +02003202 if (!exp) {
3203 goto cleanup;
3204 }
3205
Michal Vaskoe33134a2022-07-29 14:54:40 +02003206 if (!start_idx && !end_idx) {
3207 end_idx = exp->used - 1;
3208 }
3209
3210 expr_start = exp->expr + exp->tok_pos[start_idx];
3211 expr_len = (exp->tok_pos[end_idx] + exp->tok_len[end_idx]) - exp->tok_pos[start_idx];
3212
Michal Vasko004d3152020-06-11 19:59:22 +02003213 dup = calloc(1, sizeof *dup);
Michal Vasko1734be92020-09-22 08:55:10 +02003214 LY_CHECK_ERR_GOTO(!dup, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02003215
Michal Vasko08e9b112021-06-11 15:41:17 +02003216 if (exp->used) {
Michal Vaskoe33134a2022-07-29 14:54:40 +02003217 used = (end_idx - start_idx) + 1;
3218
3219 dup->tokens = malloc(used * sizeof *dup->tokens);
Michal Vasko08e9b112021-06-11 15:41:17 +02003220 LY_CHECK_ERR_GOTO(!dup->tokens, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vaskoe33134a2022-07-29 14:54:40 +02003221 memcpy(dup->tokens, exp->tokens + start_idx, used * sizeof *dup->tokens);
Michal Vasko004d3152020-06-11 19:59:22 +02003222
Michal Vaskoe33134a2022-07-29 14:54:40 +02003223 dup->tok_pos = malloc(used * sizeof *dup->tok_pos);
Michal Vasko08e9b112021-06-11 15:41:17 +02003224 LY_CHECK_ERR_GOTO(!dup->tok_pos, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vaskoe33134a2022-07-29 14:54:40 +02003225 memcpy(dup->tok_pos, exp->tok_pos + start_idx, used * sizeof *dup->tok_pos);
Michal Vasko004d3152020-06-11 19:59:22 +02003226
Michal Vaskoe33134a2022-07-29 14:54:40 +02003227 if (start_idx) {
3228 /* fix the indices in the expression */
3229 for (i = 0; i < used; ++i) {
3230 dup->tok_pos[i] -= expr_start - exp->expr;
3231 }
3232 }
3233
3234 dup->tok_len = malloc(used * sizeof *dup->tok_len);
Michal Vasko08e9b112021-06-11 15:41:17 +02003235 LY_CHECK_ERR_GOTO(!dup->tok_len, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vaskoe33134a2022-07-29 14:54:40 +02003236 memcpy(dup->tok_len, exp->tok_len + start_idx, used * sizeof *dup->tok_len);
Michal Vasko004d3152020-06-11 19:59:22 +02003237
Michal Vasko79a7a872022-06-17 09:00:48 +02003238 if (exp->repeat) {
Michal Vaskoe33134a2022-07-29 14:54:40 +02003239 dup->repeat = malloc(used * sizeof *dup->repeat);
Michal Vasko79a7a872022-06-17 09:00:48 +02003240 LY_CHECK_ERR_GOTO(!dup->repeat, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vaskoe33134a2022-07-29 14:54:40 +02003241 for (i = start_idx; i <= end_idx; ++i) {
Michal Vasko79a7a872022-06-17 09:00:48 +02003242 if (!exp->repeat[i]) {
Michal Vaskoe33134a2022-07-29 14:54:40 +02003243 dup->repeat[i - start_idx] = NULL;
Michal Vasko79a7a872022-06-17 09:00:48 +02003244 } else {
3245 for (j = 0; exp->repeat[i][j]; ++j) {}
3246 /* the ending 0 as well */
3247 ++j;
Michal Vasko004d3152020-06-11 19:59:22 +02003248
Michal Vaskoe33134a2022-07-29 14:54:40 +02003249 dup->repeat[i - start_idx] = malloc(j * sizeof **dup->repeat);
3250 LY_CHECK_ERR_GOTO(!dup->repeat[i - start_idx], LOGMEM(ctx); ret = LY_EMEM, cleanup);
3251 memcpy(dup->repeat[i - start_idx], exp->repeat[i], j * sizeof **dup->repeat);
Michal Vasko79a7a872022-06-17 09:00:48 +02003252 }
Michal Vasko08e9b112021-06-11 15:41:17 +02003253 }
Michal Vasko004d3152020-06-11 19:59:22 +02003254 }
3255 }
3256
Michal Vaskoe33134a2022-07-29 14:54:40 +02003257 dup->used = used;
3258 dup->size = used;
3259
3260 /* copy only subexpression */
3261 LY_CHECK_GOTO(ret = lydict_insert(ctx, expr_start, expr_len, &dup->expr), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02003262
Michal Vasko1734be92020-09-22 08:55:10 +02003263cleanup:
3264 if (ret) {
3265 lyxp_expr_free(ctx, dup);
3266 } else {
3267 *dup_p = dup;
3268 }
3269 return ret;
Michal Vasko004d3152020-06-11 19:59:22 +02003270}
3271
Michal Vasko03ff5a72019-09-11 13:49:33 +02003272/**
3273 * @brief Get the last-added schema node that is currently in the context.
3274 *
3275 * @param[in] set Set to search in.
3276 * @return Last-added schema context node, NULL if no node is in context.
3277 */
3278static struct lysc_node *
3279warn_get_scnode_in_ctx(struct lyxp_set *set)
3280{
3281 uint32_t i;
3282
3283 if (!set || (set->type != LYXP_SET_SCNODE_SET)) {
3284 return NULL;
3285 }
3286
3287 i = set->used;
3288 do {
3289 --i;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003290 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003291 /* if there are more, simply return the first found (last added) */
3292 return set->val.scnodes[i].scnode;
3293 }
3294 } while (i);
3295
3296 return NULL;
3297}
3298
3299/**
3300 * @brief Test whether a type is numeric - integer type or decimal64.
3301 *
3302 * @param[in] type Type to test.
Radek Krejci857189e2020-09-01 13:26:36 +02003303 * @return Boolean value whether @p type is numeric type or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003304 */
Radek Krejci857189e2020-09-01 13:26:36 +02003305static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003306warn_is_numeric_type(struct lysc_type *type)
3307{
3308 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003309 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003310 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003311
3312 switch (type->basetype) {
3313 case LY_TYPE_DEC64:
3314 case LY_TYPE_INT8:
3315 case LY_TYPE_UINT8:
3316 case LY_TYPE_INT16:
3317 case LY_TYPE_UINT16:
3318 case LY_TYPE_INT32:
3319 case LY_TYPE_UINT32:
3320 case LY_TYPE_INT64:
3321 case LY_TYPE_UINT64:
3322 return 1;
3323 case LY_TYPE_UNION:
3324 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003325 LY_ARRAY_FOR(uni->types, u) {
3326 ret = warn_is_numeric_type(uni->types[u]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003327 if (ret) {
3328 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003329 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003330 }
3331 }
3332 /* did not find any suitable type */
3333 return 0;
3334 case LY_TYPE_LEAFREF:
3335 return warn_is_numeric_type(((struct lysc_type_leafref *)type)->realtype);
3336 default:
3337 return 0;
3338 }
3339}
3340
3341/**
3342 * @brief Test whether a type is string-like - no integers, decimal64 or binary.
3343 *
3344 * @param[in] type Type to test.
Radek Krejci857189e2020-09-01 13:26:36 +02003345 * @return Boolean value whether @p type's basetype is string type or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003346 */
Radek Krejci857189e2020-09-01 13:26:36 +02003347static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003348warn_is_string_type(struct lysc_type *type)
3349{
3350 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003351 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003352 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003353
3354 switch (type->basetype) {
3355 case LY_TYPE_BITS:
3356 case LY_TYPE_ENUM:
3357 case LY_TYPE_IDENT:
3358 case LY_TYPE_INST:
3359 case LY_TYPE_STRING:
3360 return 1;
3361 case LY_TYPE_UNION:
3362 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003363 LY_ARRAY_FOR(uni->types, u) {
3364 ret = warn_is_string_type(uni->types[u]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003365 if (ret) {
3366 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003367 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003368 }
3369 }
3370 /* did not find any suitable type */
3371 return 0;
3372 case LY_TYPE_LEAFREF:
3373 return warn_is_string_type(((struct lysc_type_leafref *)type)->realtype);
3374 default:
3375 return 0;
3376 }
3377}
3378
3379/**
3380 * @brief Test whether a type is one specific type.
3381 *
3382 * @param[in] type Type to test.
3383 * @param[in] base Expected type.
Radek Krejci857189e2020-09-01 13:26:36 +02003384 * @return Boolean value whether the given @p type is of the specific basetype @p base.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003385 */
Radek Krejci857189e2020-09-01 13:26:36 +02003386static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003387warn_is_specific_type(struct lysc_type *type, LY_DATA_TYPE base)
3388{
3389 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003390 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003391 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003392
3393 if (type->basetype == base) {
3394 return 1;
3395 } else if (type->basetype == LY_TYPE_UNION) {
3396 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003397 LY_ARRAY_FOR(uni->types, u) {
3398 ret = warn_is_specific_type(uni->types[u], base);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003399 if (ret) {
3400 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003401 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003402 }
3403 }
3404 /* did not find any suitable type */
3405 return 0;
3406 } else if (type->basetype == LY_TYPE_LEAFREF) {
3407 return warn_is_specific_type(((struct lysc_type_leafref *)type)->realtype, base);
3408 }
3409
3410 return 0;
3411}
3412
3413/**
3414 * @brief Get next type of a (union) type.
3415 *
3416 * @param[in] type Base type.
3417 * @param[in] prev_type Previously returned type.
3418 * @return Next type or NULL.
3419 */
3420static struct lysc_type *
3421warn_is_equal_type_next_type(struct lysc_type *type, struct lysc_type *prev_type)
3422{
3423 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003424 ly_bool found = 0;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003425 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003426
Michal Vasko4e55f5a2022-12-14 12:15:00 +01003427 if (type->basetype == LY_TYPE_UNION) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003428 uni = (struct lysc_type_union *)type;
3429 if (!prev_type) {
3430 return uni->types[0];
3431 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003432 LY_ARRAY_FOR(uni->types, u) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003433 if (found) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003434 return uni->types[u];
Michal Vasko03ff5a72019-09-11 13:49:33 +02003435 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003436 if (prev_type == uni->types[u]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003437 found = 1;
3438 }
3439 }
3440 return NULL;
Michal Vasko4e55f5a2022-12-14 12:15:00 +01003441 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003442 if (prev_type) {
3443 assert(type == prev_type);
3444 return NULL;
3445 } else {
3446 return type;
3447 }
3448 }
3449}
3450
3451/**
3452 * @brief Test whether 2 types have a common type.
3453 *
3454 * @param[in] type1 First type.
3455 * @param[in] type2 Second type.
3456 * @return 1 if they do, 0 otherwise.
3457 */
3458static int
3459warn_is_equal_type(struct lysc_type *type1, struct lysc_type *type2)
3460{
3461 struct lysc_type *t1, *rt1, *t2, *rt2;
3462
3463 t1 = NULL;
3464 while ((t1 = warn_is_equal_type_next_type(type1, t1))) {
3465 if (t1->basetype == LY_TYPE_LEAFREF) {
3466 rt1 = ((struct lysc_type_leafref *)t1)->realtype;
3467 } else {
3468 rt1 = t1;
3469 }
3470
3471 t2 = NULL;
3472 while ((t2 = warn_is_equal_type_next_type(type2, t2))) {
3473 if (t2->basetype == LY_TYPE_LEAFREF) {
3474 rt2 = ((struct lysc_type_leafref *)t2)->realtype;
3475 } else {
3476 rt2 = t2;
3477 }
3478
3479 if (rt2->basetype == rt1->basetype) {
3480 /* match found */
3481 return 1;
3482 }
3483 }
3484 }
3485
3486 return 0;
3487}
3488
3489/**
Michal Vaskoaa956522021-11-11 10:45:34 +01003490 * @brief Print warning with information about the XPath subexpression that caused previous warning.
3491 *
3492 * @param[in] ctx Context for logging.
3493 * @param[in] tok_pos Index of the subexpression in the whole expression.
3494 * @param[in] subexpr Subexpression start.
3495 * @param[in] subexpr_len Length of @p subexpr to print.
3496 * @param[in] cur_scnode Expression context node.
3497 */
3498static void
Michal Vaskodd528af2022-08-08 14:35:07 +02003499warn_subexpr_log(const struct ly_ctx *ctx, uint32_t tok_pos, const char *subexpr, int subexpr_len,
Michal Vaskoaa956522021-11-11 10:45:34 +01003500 const struct lysc_node *cur_scnode)
3501{
3502 char *path;
3503
3504 path = lysc_path(cur_scnode, LYSC_PATH_LOG, NULL, 0);
Michal Vaskodd528af2022-08-08 14:35:07 +02003505 LOGWRN(ctx, "Previous warning generated by XPath subexpression[%" PRIu32 "] \"%.*s\" with context node \"%s\".",
Michal Vaskoaa956522021-11-11 10:45:34 +01003506 tok_pos, subexpr_len, subexpr, path);
3507 free(path);
3508}
3509
3510/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02003511 * @brief Check both operands of comparison operators.
3512 *
3513 * @param[in] ctx Context for errors.
3514 * @param[in] set1 First operand set.
3515 * @param[in] set2 Second operand set.
3516 * @param[in] numbers_only Whether accept only numbers or other types are fine too (for '=' and '!=').
3517 * @param[in] expr Start of the expression to print with the warning.
3518 * @param[in] tok_pos Token position.
3519 */
3520static void
Michal Vaskoaa956522021-11-11 10:45:34 +01003521warn_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 +02003522 uint32_t tok_pos)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003523{
3524 struct lysc_node_leaf *node1, *node2;
Radek Krejci857189e2020-09-01 13:26:36 +02003525 ly_bool leaves = 1, warning = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003526
3527 node1 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set1);
3528 node2 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set2);
3529
3530 if (!node1 && !node2) {
3531 /* no node-sets involved, nothing to do */
3532 return;
3533 }
3534
3535 if (node1) {
3536 if (!(node1->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3537 LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node1->nodetype), node1->name);
3538 warning = 1;
3539 leaves = 0;
3540 } else if (numbers_only && !warn_is_numeric_type(node1->type)) {
3541 LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node1->name);
3542 warning = 1;
3543 }
3544 }
3545
3546 if (node2) {
3547 if (!(node2->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3548 LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node2->nodetype), node2->name);
3549 warning = 1;
3550 leaves = 0;
3551 } else if (numbers_only && !warn_is_numeric_type(node2->type)) {
3552 LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node2->name);
3553 warning = 1;
3554 }
3555 }
3556
3557 if (node1 && node2 && leaves && !numbers_only) {
Michal Vasko69730152020-10-09 16:30:07 +02003558 if ((warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type)) ||
3559 (!warn_is_numeric_type(node1->type) && warn_is_numeric_type(node2->type)) ||
3560 (!warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type) &&
3561 !warn_is_equal_type(node1->type, node2->type))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003562 LOGWRN(ctx, "Incompatible types of operands \"%s\" and \"%s\" for comparison.", node1->name, node2->name);
3563 warning = 1;
3564 }
3565 }
3566
3567 if (warning) {
Michal Vaskoaa956522021-11-11 10:45:34 +01003568 warn_subexpr_log(ctx, tok_pos, expr + tok_pos, 20, set1->cur_scnode);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003569 }
3570}
3571
3572/**
3573 * @brief Check that a value is valid for a leaf. If not applicable, does nothing.
3574 *
3575 * @param[in] exp Parsed XPath expression.
3576 * @param[in] set Set with the leaf/leaf-list.
3577 * @param[in] val_exp Index of the value (literal/number) in @p exp.
3578 * @param[in] equal_exp Index of the start of the equality expression in @p exp.
3579 * @param[in] last_equal_exp Index of the end of the equality expression in @p exp.
3580 */
3581static void
Michal Vaskodd528af2022-08-08 14:35:07 +02003582warn_equality_value(const struct lyxp_expr *exp, struct lyxp_set *set, uint32_t val_exp, uint32_t equal_exp,
3583 uint32_t last_equal_exp)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003584{
3585 struct lysc_node *scnode;
3586 struct lysc_type *type;
3587 char *value;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003588 struct lyd_value storage;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003589 LY_ERR rc;
3590 struct ly_err_item *err = NULL;
3591
Michal Vasko69730152020-10-09 16:30:07 +02003592 if ((scnode = warn_get_scnode_in_ctx(set)) && (scnode->nodetype & (LYS_LEAF | LYS_LEAFLIST)) &&
3593 ((exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) || (exp->tokens[val_exp] == LYXP_TOKEN_NUMBER))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003594 /* check that the node can have the specified value */
3595 if (exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) {
3596 value = strndup(exp->expr + exp->tok_pos[val_exp] + 1, exp->tok_len[val_exp] - 2);
3597 } else {
3598 value = strndup(exp->expr + exp->tok_pos[val_exp], exp->tok_len[val_exp]);
3599 }
3600 if (!value) {
3601 LOGMEM(set->ctx);
3602 return;
3603 }
3604
3605 if ((((struct lysc_node_leaf *)scnode)->type->basetype == LY_TYPE_IDENT) && !strchr(value, ':')) {
3606 LOGWRN(set->ctx, "Identityref \"%s\" comparison with identity \"%s\" without prefix, consider adding"
Michal Vasko69730152020-10-09 16:30:07 +02003607 " a prefix or best using \"derived-from(-or-self)()\" functions.", scnode->name, value);
Michal Vaskoaa956522021-11-11 10:45:34 +01003608 warn_subexpr_log(set->ctx, exp->tok_pos[equal_exp], exp->expr + exp->tok_pos[equal_exp],
Radek Krejci0f969882020-08-21 16:56:47 +02003609 (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp],
Michal Vaskoaa956522021-11-11 10:45:34 +01003610 set->cur_scnode);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003611 }
3612
3613 type = ((struct lysc_node_leaf *)scnode)->type;
3614 if (type->basetype != LY_TYPE_IDENT) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003615 rc = type->plugin->store(set->ctx, type, value, strlen(value), 0, set->format, set->prefix_data,
Michal Vasko405cc9e2020-12-01 12:01:27 +01003616 LYD_HINT_DATA, scnode, &storage, NULL, &err);
Michal Vaskobf42e832020-11-23 16:59:42 +01003617 if (rc == LY_EINCOMPLETE) {
3618 rc = LY_SUCCESS;
3619 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003620
3621 if (err) {
3622 LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type (%s).", value, err->msg);
3623 ly_err_free(err);
3624 } else if (rc != LY_SUCCESS) {
3625 LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type.", value);
3626 }
3627 if (rc != LY_SUCCESS) {
Michal Vaskoaa956522021-11-11 10:45:34 +01003628 warn_subexpr_log(set->ctx, exp->tok_pos[equal_exp], exp->expr + exp->tok_pos[equal_exp],
Radek Krejci0f969882020-08-21 16:56:47 +02003629 (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp],
Michal Vaskoaa956522021-11-11 10:45:34 +01003630 set->cur_scnode);
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003631 } else {
3632 type->plugin->free(set->ctx, &storage);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003633 }
3634 }
3635 free(value);
3636 }
3637}
3638
3639/*
3640 * XPath functions
3641 */
3642
3643/**
3644 * @brief Execute the YANG 1.1 bit-is-set(node-set, string) function. Returns LYXP_SET_BOOLEAN
3645 * depending on whether the first node bit value from the second argument is set.
3646 *
3647 * @param[in] args Array of arguments.
3648 * @param[in] arg_count Count of elements in @p args.
3649 * @param[in,out] set Context and result set at the same time.
3650 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003651 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003652 */
3653static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02003654xpath_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 +02003655{
3656 struct lyd_node_term *leaf;
3657 struct lysc_node_leaf *sleaf;
Michal Vasko2588b952021-07-29 07:43:26 +02003658 struct lyd_value_bits *bits;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003659 LY_ERR rc = LY_SUCCESS;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003660 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003661
3662 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003663 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003664 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003665 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3666 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3667 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3668 sleaf->name);
3669 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_BITS)) {
3670 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"bits\".", __func__, sleaf->name);
3671 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003672 }
3673
3674 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3675 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003676 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3677 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003678 } else if (!warn_is_string_type(sleaf->type)) {
3679 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003680 }
3681 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003682 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003683 return rc;
3684 }
3685
Michal Vaskod3678892020-05-21 10:06:58 +02003686 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003687 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 +02003688 return LY_EVALID;
3689 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003690 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003691 LY_CHECK_RET(rc);
3692
3693 set_fill_boolean(set, 0);
Michal Vaskod3678892020-05-21 10:06:58 +02003694 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003695 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
Michal Vasko2588b952021-07-29 07:43:26 +02003696 if ((leaf->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (leaf->value.realtype->basetype == LY_TYPE_BITS)) {
3697 LYD_VALUE_GET(&leaf->value, bits);
3698 LY_ARRAY_FOR(bits->items, u) {
3699 if (!strcmp(bits->items[u]->name, args[1]->val.str)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003700 set_fill_boolean(set, 1);
3701 break;
3702 }
3703 }
3704 }
3705 }
3706
3707 return LY_SUCCESS;
3708}
3709
3710/**
3711 * @brief Execute the XPath boolean(object) function. Returns LYXP_SET_BOOLEAN
3712 * with the argument converted to boolean.
3713 *
3714 * @param[in] args Array of arguments.
3715 * @param[in] arg_count Count of elements in @p args.
3716 * @param[in,out] set Context and result set at the same time.
3717 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003718 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003719 */
3720static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02003721xpath_boolean(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003722{
3723 LY_ERR rc;
3724
3725 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02003726 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003727 return LY_SUCCESS;
3728 }
3729
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003730 rc = lyxp_set_cast(args[0], LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003731 LY_CHECK_RET(rc);
3732 set_fill_set(set, args[0]);
3733
3734 return LY_SUCCESS;
3735}
3736
3737/**
3738 * @brief Execute the XPath ceiling(number) function. Returns LYXP_SET_NUMBER
3739 * with the first argument rounded up to the nearest integer.
3740 *
3741 * @param[in] args Array of arguments.
3742 * @param[in] arg_count Count of elements in @p args.
3743 * @param[in,out] set Context and result set at the same time.
3744 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003745 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003746 */
3747static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02003748xpath_ceiling(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003749{
3750 struct lysc_node_leaf *sleaf;
3751 LY_ERR rc = LY_SUCCESS;
3752
3753 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003754 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003755 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003756 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3757 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3758 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3759 sleaf->name);
3760 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
3761 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
3762 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003763 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003764 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003765 return rc;
3766 }
3767
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003768 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003769 LY_CHECK_RET(rc);
3770 if ((long long)args[0]->val.num != args[0]->val.num) {
3771 set_fill_number(set, ((long long)args[0]->val.num) + 1);
3772 } else {
3773 set_fill_number(set, args[0]->val.num);
3774 }
3775
3776 return LY_SUCCESS;
3777}
3778
3779/**
3780 * @brief Execute the XPath concat(string, string, string*) function.
3781 * Returns LYXP_SET_STRING with the concatenation of all the arguments.
3782 *
3783 * @param[in] args Array of arguments.
3784 * @param[in] arg_count Count of elements in @p args.
3785 * @param[in,out] set Context and result set at the same time.
3786 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003787 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003788 */
3789static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02003790xpath_concat(struct lyxp_set **args, uint32_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003791{
Michal Vaskodd528af2022-08-08 14:35:07 +02003792 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003793 char *str = NULL;
3794 size_t used = 1;
3795 LY_ERR rc = LY_SUCCESS;
3796 struct lysc_node_leaf *sleaf;
3797
3798 if (options & LYXP_SCNODE_ALL) {
3799 for (i = 0; i < arg_count; ++i) {
3800 if ((args[i]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[i]))) {
3801 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3802 LOGWRN(set->ctx, "Argument #%u of %s is a %s node \"%s\".",
Michal Vasko69730152020-10-09 16:30:07 +02003803 i + 1, __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003804 } else if (!warn_is_string_type(sleaf->type)) {
Radek Krejci70124c82020-08-14 22:17:03 +02003805 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 +02003806 }
3807 }
3808 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003809 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003810 return rc;
3811 }
3812
3813 for (i = 0; i < arg_count; ++i) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003814 rc = lyxp_set_cast(args[i], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003815 if (rc != LY_SUCCESS) {
3816 free(str);
3817 return rc;
3818 }
3819
3820 str = ly_realloc(str, (used + strlen(args[i]->val.str)) * sizeof(char));
3821 LY_CHECK_ERR_RET(!str, LOGMEM(set->ctx), LY_EMEM);
3822 strcpy(str + used - 1, args[i]->val.str);
3823 used += strlen(args[i]->val.str);
3824 }
3825
3826 /* free, kind of */
Michal Vaskod3678892020-05-21 10:06:58 +02003827 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003828 set->type = LYXP_SET_STRING;
3829 set->val.str = str;
3830
3831 return LY_SUCCESS;
3832}
3833
3834/**
3835 * @brief Execute the XPath contains(string, string) function.
3836 * Returns LYXP_SET_BOOLEAN whether the second argument can
3837 * be found in the first or not.
3838 *
3839 * @param[in] args Array of arguments.
3840 * @param[in] arg_count Count of elements in @p args.
3841 * @param[in,out] set Context and result set at the same time.
3842 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003843 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003844 */
3845static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02003846xpath_contains(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003847{
3848 struct lysc_node_leaf *sleaf;
3849 LY_ERR rc = LY_SUCCESS;
3850
3851 if (options & LYXP_SCNODE_ALL) {
3852 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3853 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003854 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3855 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003856 } else if (!warn_is_string_type(sleaf->type)) {
3857 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003858 }
3859 }
3860
3861 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3862 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003863 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3864 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003865 } else if (!warn_is_string_type(sleaf->type)) {
3866 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003867 }
3868 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003869 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003870 return rc;
3871 }
3872
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003873 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003874 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003875 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003876 LY_CHECK_RET(rc);
3877
3878 if (strstr(args[0]->val.str, args[1]->val.str)) {
3879 set_fill_boolean(set, 1);
3880 } else {
3881 set_fill_boolean(set, 0);
3882 }
3883
3884 return LY_SUCCESS;
3885}
3886
3887/**
3888 * @brief Execute the XPath count(node-set) function. Returns LYXP_SET_NUMBER
3889 * with the size of the node-set from the argument.
3890 *
3891 * @param[in] args Array of arguments.
3892 * @param[in] arg_count Count of elements in @p args.
3893 * @param[in,out] set Context and result set at the same time.
3894 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003895 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003896 */
3897static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02003898xpath_count(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003899{
Michal Vasko03ff5a72019-09-11 13:49:33 +02003900 LY_ERR rc = LY_SUCCESS;
3901
3902 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003903 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003904 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003905 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003906 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003907 return rc;
3908 }
3909
Michal Vasko03ff5a72019-09-11 13:49:33 +02003910 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003911 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "count(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003912 return LY_EVALID;
3913 }
3914
3915 set_fill_number(set, args[0]->used);
3916 return LY_SUCCESS;
3917}
3918
3919/**
3920 * @brief Execute the XPath current() function. Returns LYXP_SET_NODE_SET
3921 * with the context with the intial node.
3922 *
3923 * @param[in] args Array of arguments.
3924 * @param[in] arg_count Count of elements in @p args.
3925 * @param[in,out] set Context and result set at the same time.
3926 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003927 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003928 */
3929static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02003930xpath_current(struct lyxp_set **args, uint32_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003931{
3932 if (arg_count || args) {
Radek Krejcie87c7dc2021-06-02 21:25:42 +02003933 LOGVAL(set->ctx, LY_VCODE_XP_INARGCOUNT, arg_count, LY_PRI_LENSTR("current()"));
Michal Vasko03ff5a72019-09-11 13:49:33 +02003934 return LY_EVALID;
3935 }
3936
3937 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02003938 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003939
Michal Vasko296dfaf2021-12-13 16:57:42 +01003940 if (set->cur_scnode) {
Michal Vasko7333cb32022-07-29 16:30:29 +02003941 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 +01003942 } else {
3943 /* root node */
Michal Vasko7333cb32022-07-29 16:30:29 +02003944 LY_CHECK_RET(set_scnode_insert_node(set, NULL, set->root_type, LYXP_AXIS_SELF, NULL));
Michal Vasko296dfaf2021-12-13 16:57:42 +01003945 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003946 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02003947 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003948
Michal Vasko296dfaf2021-12-13 16:57:42 +01003949 if (set->cur_node) {
3950 /* position is filled later */
3951 set_insert_node(set, set->cur_node, 0, LYXP_NODE_ELEM, 0);
3952 } else {
3953 /* root node */
3954 set_insert_node(set, NULL, 0, set->root_type, 0);
3955 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003956 }
3957
3958 return LY_SUCCESS;
3959}
3960
3961/**
3962 * @brief Execute the YANG 1.1 deref(node-set) function. Returns LYXP_SET_NODE_SET with either
3963 * leafref or instance-identifier target node(s).
3964 *
3965 * @param[in] args Array of arguments.
3966 * @param[in] arg_count Count of elements in @p args.
3967 * @param[in,out] set Context and result set at the same time.
3968 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003969 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003970 */
3971static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02003972xpath_deref(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003973{
3974 struct lyd_node_term *leaf;
Michal Vasko42e497c2020-01-06 08:38:25 +01003975 struct lysc_node_leaf *sleaf = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02003976 struct lysc_type_leafref *lref;
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003977 const struct lysc_node *target;
Michal Vasko004d3152020-06-11 19:59:22 +02003978 struct ly_path *p;
3979 struct lyd_node *node;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003980 char *errmsg = NULL;
Michal Vasko00cbf532020-06-15 13:58:47 +02003981 uint8_t oper;
Michal Vasko741bb562021-06-24 11:59:50 +02003982 LY_ERR r;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003983
3984 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003985 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003986 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003987 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
Michal Vasko423ba3f2021-07-19 13:08:50 +02003988 if (!(sleaf->nodetype & LYD_NODE_TERM)) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003989 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3990 sleaf->name);
Michal Vaskoed725d72021-06-23 12:03:45 +02003991 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_LEAFREF) &&
3992 !warn_is_specific_type(sleaf->type, LY_TYPE_INST)) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003993 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"leafref\" nor \"instance-identifier\".",
3994 __func__, sleaf->name);
3995 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003996 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003997 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko423ba3f2021-07-19 13:08:50 +02003998 if (sleaf && (sleaf->nodetype & LYD_NODE_TERM) && (sleaf->type->basetype == LY_TYPE_LEAFREF)) {
Michal Vasko004d3152020-06-11 19:59:22 +02003999 lref = (struct lysc_type_leafref *)sleaf->type;
Michal Vaskod1e53b92021-01-28 13:11:06 +01004000 oper = (sleaf->flags & LYS_IS_OUTPUT) ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT;
Michal Vasko004d3152020-06-11 19:59:22 +02004001
4002 /* it was already evaluated on schema, it must succeed */
Michal Vasko741bb562021-06-24 11:59:50 +02004003 r = ly_path_compile_leafref(set->ctx, &sleaf->node, NULL, lref->path, oper, LY_PATH_TARGET_MANY,
Michal Vasko24fc4d12021-07-12 14:41:20 +02004004 LY_VALUE_SCHEMA_RESOLVED, lref->prefixes, &p);
Michal Vasko741bb562021-06-24 11:59:50 +02004005 if (!r) {
4006 /* get the target node */
4007 target = p[LY_ARRAY_COUNT(p) - 1].node;
4008 ly_path_free(set->ctx, p);
Michal Vasko004d3152020-06-11 19:59:22 +02004009
Michal Vasko7333cb32022-07-29 16:30:29 +02004010 LY_CHECK_RET(set_scnode_insert_node(set, target, LYXP_NODE_ELEM, LYXP_AXIS_SELF, NULL));
Michal Vasko741bb562021-06-24 11:59:50 +02004011 } /* else the target was found before but is disabled so it was removed */
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02004012 }
4013
Michal Vasko741bb562021-06-24 11:59:50 +02004014 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004015 }
4016
Michal Vaskod3678892020-05-21 10:06:58 +02004017 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004018 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "deref(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004019 return LY_EVALID;
4020 }
4021
Michal Vaskod3678892020-05-21 10:06:58 +02004022 lyxp_set_free_content(set);
4023 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004024 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
4025 sleaf = (struct lysc_node_leaf *)leaf->schema;
4026 if (sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
4027 if (sleaf->type->basetype == LY_TYPE_LEAFREF) {
4028 /* find leafref target */
Radek Krejci0b013302021-03-29 15:22:32 +02004029 if (lyplg_type_resolve_leafref((struct lysc_type_leafref *)sleaf->type, &leaf->node, &leaf->value, set->tree,
Michal Vasko9e685082021-01-29 14:49:09 +01004030 &node, &errmsg)) {
Michal Vasko1a2b8ee2022-12-05 10:41:55 +01004031 LOGERR(set->ctx, LY_EVALID, "%s", errmsg);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004032 free(errmsg);
Michal Vasko004d3152020-06-11 19:59:22 +02004033 return LY_EVALID;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004034 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004035 } else {
4036 assert(sleaf->type->basetype == LY_TYPE_INST);
Michal Vasko90189962023-02-28 12:10:34 +01004037 if (ly_path_eval(leaf->value.target, set->tree, NULL, &node)) {
Michal Vaskoba99a3e2020-08-18 15:50:05 +02004038 LOGERR(set->ctx, LY_EVALID, "Invalid instance-identifier \"%s\" value - required instance not found.",
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02004039 lyd_get_value(&leaf->node));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004040 return LY_EVALID;
4041 }
4042 }
Michal Vasko004d3152020-06-11 19:59:22 +02004043
4044 /* insert it */
4045 set_insert_node(set, node, 0, LYXP_NODE_ELEM, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004046 }
4047 }
4048
4049 return LY_SUCCESS;
4050}
4051
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004052static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02004053xpath_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 +02004054{
Michal Vaskofe1af042022-07-29 14:58:59 +02004055 uint32_t i, id_len;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004056 LY_ARRAY_COUNT_TYPE u;
4057 struct lyd_node_term *leaf;
4058 struct lysc_node_leaf *sleaf;
4059 struct lyd_meta *meta;
Michal Vasko93923692021-05-07 15:28:02 +02004060 struct lyd_value *val;
4061 const struct lys_module *mod;
4062 const char *id_name;
Michal Vasko93923692021-05-07 15:28:02 +02004063 struct lysc_ident *id;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004064 LY_ERR rc = LY_SUCCESS;
Radek Krejci857189e2020-09-01 13:26:36 +02004065 ly_bool found;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004066
4067 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02004068 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004069 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", func);
Michal Vasko5676f4e2021-04-06 17:14:45 +02004070 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4071 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4072 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype),
4073 sleaf->name);
4074 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_IDENT)) {
4075 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"identityref\".", func, sleaf->name);
4076 }
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004077 }
4078
4079 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4080 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4081 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype),
Michal Vasko69730152020-10-09 16:30:07 +02004082 sleaf->name);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004083 } else if (!warn_is_string_type(sleaf->type)) {
4084 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", func, sleaf->name);
4085 }
4086 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004087 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004088 return rc;
4089 }
4090
4091 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004092 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 +02004093 return LY_EVALID;
4094 }
4095 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
4096 LY_CHECK_RET(rc);
4097
Michal Vasko93923692021-05-07 15:28:02 +02004098 /* parse the identity */
4099 id_name = args[1]->val.str;
4100 id_len = strlen(id_name);
4101 rc = moveto_resolve_model(&id_name, &id_len, set, set->cur_node ? set->cur_node->schema : NULL, &mod);
4102 LY_CHECK_RET(rc);
4103 if (!mod) {
4104 LOGVAL(set->ctx, LYVE_XPATH, "Identity \"%.*s\" without a prefix.", (int)id_len, id_name);
4105 return LY_EVALID;
4106 }
4107
4108 /* find the identity */
4109 found = 0;
4110 LY_ARRAY_FOR(mod->identities, u) {
4111 if (!ly_strncmp(mod->identities[u].name, id_name, id_len)) {
4112 /* we have match */
4113 found = 1;
4114 break;
4115 }
4116 }
4117 if (!found) {
4118 LOGVAL(set->ctx, LYVE_XPATH, "Identity \"%.*s\" not found in module \"%s\".", (int)id_len, id_name, mod->name);
4119 return LY_EVALID;
4120 }
4121 id = &mod->identities[u];
4122
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004123 set_fill_boolean(set, 0);
4124 found = 0;
4125 for (i = 0; i < args[0]->used; ++i) {
4126 if ((args[0]->val.nodes[i].type != LYXP_NODE_ELEM) && (args[0]->val.nodes[i].type != LYXP_NODE_META)) {
4127 continue;
4128 }
4129
4130 if (args[0]->val.nodes[i].type == LYXP_NODE_ELEM) {
4131 leaf = (struct lyd_node_term *)args[0]->val.nodes[i].node;
4132 sleaf = (struct lysc_node_leaf *)leaf->schema;
4133 val = &leaf->value;
4134 if (!(sleaf->nodetype & LYD_NODE_TERM) || (leaf->value.realtype->basetype != LY_TYPE_IDENT)) {
4135 /* uninteresting */
4136 continue;
4137 }
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004138 } else {
4139 meta = args[0]->val.meta[i].meta;
4140 val = &meta->value;
4141 if (val->realtype->basetype != LY_TYPE_IDENT) {
4142 /* uninteresting */
4143 continue;
4144 }
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004145 }
4146
Michal Vasko93923692021-05-07 15:28:02 +02004147 /* check the identity itself */
4148 if (self_match && (id == val->ident)) {
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004149 set_fill_boolean(set, 1);
4150 found = 1;
4151 }
Michal Vasko93923692021-05-07 15:28:02 +02004152 if (!found && !lyplg_type_identity_isderived(id, val->ident)) {
4153 set_fill_boolean(set, 1);
4154 found = 1;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004155 }
4156
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004157 if (found) {
4158 break;
4159 }
4160 }
4161
4162 return LY_SUCCESS;
4163}
4164
Michal Vasko03ff5a72019-09-11 13:49:33 +02004165/**
4166 * @brief Execute the YANG 1.1 derived-from(node-set, string) function. Returns LYXP_SET_BOOLEAN depending
4167 * on whether the first argument nodes contain a node of an identity derived from the second
4168 * argument identity.
4169 *
4170 * @param[in] args Array of arguments.
4171 * @param[in] arg_count Count of elements in @p args.
4172 * @param[in,out] set Context and result set at the same time.
4173 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004174 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004175 */
4176static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004177xpath_derived_from(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004178{
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004179 return xpath_derived_(args, set, options, 0, __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004180}
4181
4182/**
4183 * @brief Execute the YANG 1.1 derived-from-or-self(node-set, string) function. Returns LYXP_SET_BOOLEAN depending
4184 * on whether the first argument nodes contain a node of an identity that either is or is derived from
4185 * the second argument identity.
4186 *
4187 * @param[in] args Array of arguments.
4188 * @param[in] arg_count Count of elements in @p args.
4189 * @param[in,out] set Context and result set at the same time.
4190 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004191 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004192 */
4193static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004194xpath_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 +02004195{
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004196 return xpath_derived_(args, set, options, 1, __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004197}
4198
4199/**
4200 * @brief Execute the YANG 1.1 enum-value(node-set) function. Returns LYXP_SET_NUMBER
4201 * with the integer value of the first node's enum value, otherwise NaN.
4202 *
4203 * @param[in] args Array of arguments.
4204 * @param[in] arg_count Count of elements in @p args.
4205 * @param[in,out] set Context and result set at the same time.
4206 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004207 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004208 */
4209static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004210xpath_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 +02004211{
4212 struct lyd_node_term *leaf;
4213 struct lysc_node_leaf *sleaf;
4214 LY_ERR rc = LY_SUCCESS;
4215
4216 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02004217 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004218 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02004219 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4220 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4221 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
4222 sleaf->name);
4223 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_ENUM)) {
4224 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"enumeration\".", __func__, sleaf->name);
4225 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004226 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004227 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004228 return rc;
4229 }
4230
Michal Vaskod3678892020-05-21 10:06:58 +02004231 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004232 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "enum-value(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004233 return LY_EVALID;
4234 }
4235
4236 set_fill_number(set, NAN);
Michal Vaskod3678892020-05-21 10:06:58 +02004237 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004238 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
4239 sleaf = (struct lysc_node_leaf *)leaf->schema;
4240 if ((sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (sleaf->type->basetype == LY_TYPE_ENUM)) {
4241 set_fill_number(set, leaf->value.enum_item->value);
4242 }
4243 }
4244
4245 return LY_SUCCESS;
4246}
4247
4248/**
4249 * @brief Execute the XPath false() function. Returns LYXP_SET_BOOLEAN
4250 * with false value.
4251 *
4252 * @param[in] args Array of arguments.
4253 * @param[in] arg_count Count of elements in @p args.
4254 * @param[in,out] set Context and result set at the same time.
4255 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004256 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004257 */
4258static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004259xpath_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 +02004260{
4261 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004262 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004263 return LY_SUCCESS;
4264 }
4265
4266 set_fill_boolean(set, 0);
4267 return LY_SUCCESS;
4268}
4269
4270/**
4271 * @brief Execute the XPath floor(number) function. Returns LYXP_SET_NUMBER
4272 * with the first argument floored (truncated).
4273 *
4274 * @param[in] args Array of arguments.
4275 * @param[in] arg_count Count of elements in @p args.
4276 * @param[in,out] set Context and result set at the same time.
4277 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004278 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004279 */
4280static LY_ERR
Michal Vasko0ff8d632023-04-06 12:31:17 +02004281xpath_floor(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004282{
Michal Vasko0ff8d632023-04-06 12:31:17 +02004283 struct lysc_node_leaf *sleaf;
4284 LY_ERR rc = LY_SUCCESS;
4285
4286 if (options & LYXP_SCNODE_ALL) {
4287 if (args[0]->type != LYXP_SET_SCNODE_SET) {
4288 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
4289 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4290 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4291 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
4292 sleaf->name);
4293 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
4294 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
4295 }
4296 }
4297 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
4298 return rc;
4299 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004300
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004301 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004302 LY_CHECK_RET(rc);
4303 if (isfinite(args[0]->val.num)) {
4304 set_fill_number(set, (long long)args[0]->val.num);
4305 }
4306
4307 return LY_SUCCESS;
4308}
4309
4310/**
4311 * @brief Execute the XPath lang(string) function. Returns LYXP_SET_BOOLEAN
4312 * whether the language of the text matches the one from the argument.
4313 *
4314 * @param[in] args Array of arguments.
4315 * @param[in] arg_count Count of elements in @p args.
4316 * @param[in,out] set Context and result set at the same time.
4317 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004318 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004319 */
4320static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004321xpath_lang(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004322{
4323 const struct lyd_node *node;
4324 struct lysc_node_leaf *sleaf;
Michal Vasko9f96a052020-03-10 09:41:45 +01004325 struct lyd_meta *meta = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004326 const char *val;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004327 LY_ERR rc = LY_SUCCESS;
4328
4329 if (options & LYXP_SCNODE_ALL) {
4330 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4331 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004332 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
4333 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004334 } else if (!warn_is_string_type(sleaf->type)) {
4335 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004336 }
4337 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004338 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004339 return rc;
4340 }
4341
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004342 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004343 LY_CHECK_RET(rc);
4344
Michal Vasko03ff5a72019-09-11 13:49:33 +02004345 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004346 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "lang(string)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004347 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004348 } else if (!set->used) {
4349 set_fill_boolean(set, 0);
4350 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004351 }
4352
4353 switch (set->val.nodes[0].type) {
4354 case LYXP_NODE_ELEM:
4355 case LYXP_NODE_TEXT:
4356 node = set->val.nodes[0].node;
4357 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004358 case LYXP_NODE_META:
4359 node = set->val.meta[0].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004360 break;
4361 default:
4362 /* nothing to do with roots */
4363 set_fill_boolean(set, 0);
4364 return LY_SUCCESS;
4365 }
4366
Michal Vasko9f96a052020-03-10 09:41:45 +01004367 /* find lang metadata */
Michal Vasko9e685082021-01-29 14:49:09 +01004368 for ( ; node; node = lyd_parent(node)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01004369 for (meta = node->meta; meta; meta = meta->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004370 /* annotations */
Michal Vasko9f96a052020-03-10 09:41:45 +01004371 if (meta->name && !strcmp(meta->name, "lang") && !strcmp(meta->annotation->module->name, "xml")) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004372 break;
4373 }
4374 }
4375
Michal Vasko9f96a052020-03-10 09:41:45 +01004376 if (meta) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004377 break;
4378 }
4379 }
4380
4381 /* compare languages */
Michal Vasko9f96a052020-03-10 09:41:45 +01004382 if (!meta) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004383 set_fill_boolean(set, 0);
4384 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004385 uint64_t i;
4386
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02004387 val = lyd_get_meta_value(meta);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004388 for (i = 0; args[0]->val.str[i]; ++i) {
4389 if (tolower(args[0]->val.str[i]) != tolower(val[i])) {
4390 set_fill_boolean(set, 0);
4391 break;
4392 }
4393 }
4394 if (!args[0]->val.str[i]) {
4395 if (!val[i] || (val[i] == '-')) {
4396 set_fill_boolean(set, 1);
4397 } else {
4398 set_fill_boolean(set, 0);
4399 }
4400 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004401 }
4402
4403 return LY_SUCCESS;
4404}
4405
4406/**
4407 * @brief Execute the XPath last() function. Returns LYXP_SET_NUMBER
4408 * with the context size.
4409 *
4410 * @param[in] args Array of arguments.
4411 * @param[in] arg_count Count of elements in @p args.
4412 * @param[in,out] set Context and result set at the same time.
4413 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004414 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004415 */
4416static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004417xpath_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 +02004418{
4419 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004420 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004421 return LY_SUCCESS;
4422 }
4423
Michal Vasko03ff5a72019-09-11 13:49:33 +02004424 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004425 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "last()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004426 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004427 } else if (!set->used) {
4428 set_fill_number(set, 0);
4429 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004430 }
4431
4432 set_fill_number(set, set->ctx_size);
4433 return LY_SUCCESS;
4434}
4435
4436/**
4437 * @brief Execute the XPath local-name(node-set?) function. Returns LYXP_SET_STRING
4438 * with the node name without namespace from the argument or the context.
4439 *
4440 * @param[in] args Array of arguments.
4441 * @param[in] arg_count Count of elements in @p args.
4442 * @param[in,out] set Context and result set at the same time.
4443 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004444 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004445 */
4446static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004447xpath_local_name(struct lyxp_set **args, uint32_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004448{
4449 struct lyxp_set_node *item;
Michal Vasko69730152020-10-09 16:30:07 +02004450
Michal Vasko03ff5a72019-09-11 13:49:33 +02004451 /* suppress unused variable warning */
4452 (void)options;
4453
4454 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004455 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004456 return LY_SUCCESS;
4457 }
4458
4459 if (arg_count) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004460 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004461 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004462 "local-name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004463 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004464 } else if (!args[0]->used) {
4465 set_fill_string(set, "", 0);
4466 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004467 }
4468
4469 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004470 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004471
4472 item = &args[0]->val.nodes[0];
4473 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004474 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004475 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "local-name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004476 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004477 } else if (!set->used) {
4478 set_fill_string(set, "", 0);
4479 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004480 }
4481
4482 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004483 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004484
4485 item = &set->val.nodes[0];
4486 }
4487
4488 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004489 case LYXP_NODE_NONE:
4490 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004491 case LYXP_NODE_ROOT:
4492 case LYXP_NODE_ROOT_CONFIG:
4493 case LYXP_NODE_TEXT:
4494 set_fill_string(set, "", 0);
4495 break;
4496 case LYXP_NODE_ELEM:
4497 set_fill_string(set, item->node->schema->name, strlen(item->node->schema->name));
4498 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004499 case LYXP_NODE_META:
4500 set_fill_string(set, ((struct lyd_meta *)item->node)->name, strlen(((struct lyd_meta *)item->node)->name));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004501 break;
4502 }
4503
4504 return LY_SUCCESS;
4505}
4506
4507/**
4508 * @brief Execute the XPath name(node-set?) function. Returns LYXP_SET_STRING
4509 * with the node name fully qualified (with namespace) from the argument or the context.
4510 *
4511 * @param[in] args Array of arguments.
4512 * @param[in] arg_count Count of elements in @p args.
4513 * @param[in,out] set Context and result set at the same time.
4514 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004515 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004516 */
4517static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004518xpath_name(struct lyxp_set **args, uint32_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004519{
4520 struct lyxp_set_node *item;
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004521 struct lys_module *mod = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004522 char *str;
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004523 const char *name = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004524
4525 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004526 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004527 return LY_SUCCESS;
4528 }
4529
4530 if (arg_count) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004531 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004532 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004533 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004534 } else if (!args[0]->used) {
4535 set_fill_string(set, "", 0);
4536 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004537 }
4538
4539 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004540 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004541
4542 item = &args[0]->val.nodes[0];
4543 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004544 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004545 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004546 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004547 } else if (!set->used) {
4548 set_fill_string(set, "", 0);
4549 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004550 }
4551
4552 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004553 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004554
4555 item = &set->val.nodes[0];
4556 }
4557
4558 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004559 case LYXP_NODE_NONE:
4560 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004561 case LYXP_NODE_ROOT:
4562 case LYXP_NODE_ROOT_CONFIG:
4563 case LYXP_NODE_TEXT:
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004564 /* keep NULL */
Michal Vasko03ff5a72019-09-11 13:49:33 +02004565 break;
4566 case LYXP_NODE_ELEM:
4567 mod = item->node->schema->module;
4568 name = item->node->schema->name;
4569 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004570 case LYXP_NODE_META:
4571 mod = ((struct lyd_meta *)item->node)->annotation->module;
4572 name = ((struct lyd_meta *)item->node)->name;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004573 break;
4574 }
4575
4576 if (mod && name) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004577 int rc = asprintf(&str, "%s:%s", ly_get_prefix(mod, set->format, set->prefix_data), name);
Michal Vasko26bbb272022-08-02 14:54:33 +02004578
Michal Vasko03ff5a72019-09-11 13:49:33 +02004579 LY_CHECK_ERR_RET(rc == -1, LOGMEM(set->ctx), LY_EMEM);
4580 set_fill_string(set, str, strlen(str));
4581 free(str);
4582 } else {
4583 set_fill_string(set, "", 0);
4584 }
4585
4586 return LY_SUCCESS;
4587}
4588
4589/**
4590 * @brief Execute the XPath namespace-uri(node-set?) function. Returns LYXP_SET_STRING
4591 * with the namespace of the node from the argument or the context.
4592 *
4593 * @param[in] args Array of arguments.
4594 * @param[in] arg_count Count of elements in @p args.
4595 * @param[in,out] set Context and result set at the same time.
4596 * @param[in] options XPath options.
4597 * @return LY_ERR (LY_EINVAL for wrong arguments on schema)
4598 */
4599static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004600xpath_namespace_uri(struct lyxp_set **args, uint32_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004601{
4602 struct lyxp_set_node *item;
4603 struct lys_module *mod;
Michal Vasko69730152020-10-09 16:30:07 +02004604
Michal Vasko03ff5a72019-09-11 13:49:33 +02004605 /* suppress unused variable warning */
4606 (void)options;
4607
4608 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004609 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004610 return LY_SUCCESS;
4611 }
4612
4613 if (arg_count) {
Michal Vaskod3678892020-05-21 10:06:58 +02004614 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004615 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
Michal Vasko69730152020-10-09 16:30:07 +02004616 "namespace-uri(node-set?)");
Michal Vaskod3678892020-05-21 10:06:58 +02004617 return LY_EVALID;
4618 } else if (!args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004619 set_fill_string(set, "", 0);
4620 return LY_SUCCESS;
4621 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004622
4623 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004624 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004625
4626 item = &args[0]->val.nodes[0];
4627 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004628 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004629 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "namespace-uri(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004630 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004631 } else if (!set->used) {
4632 set_fill_string(set, "", 0);
4633 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004634 }
4635
4636 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004637 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004638
4639 item = &set->val.nodes[0];
4640 }
4641
4642 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004643 case LYXP_NODE_NONE:
4644 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004645 case LYXP_NODE_ROOT:
4646 case LYXP_NODE_ROOT_CONFIG:
4647 case LYXP_NODE_TEXT:
4648 set_fill_string(set, "", 0);
4649 break;
4650 case LYXP_NODE_ELEM:
Michal Vasko9f96a052020-03-10 09:41:45 +01004651 case LYXP_NODE_META:
Michal Vasko03ff5a72019-09-11 13:49:33 +02004652 if (item->type == LYXP_NODE_ELEM) {
4653 mod = item->node->schema->module;
Michal Vasko9f96a052020-03-10 09:41:45 +01004654 } else { /* LYXP_NODE_META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02004655 /* annotations */
Michal Vasko9f96a052020-03-10 09:41:45 +01004656 mod = ((struct lyd_meta *)item->node)->annotation->module;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004657 }
4658
4659 set_fill_string(set, mod->ns, strlen(mod->ns));
4660 break;
4661 }
4662
4663 return LY_SUCCESS;
4664}
4665
4666/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02004667 * @brief Execute the XPath normalize-space(string?) function. Returns LYXP_SET_STRING
4668 * with normalized value (no leading, trailing, double white spaces) of the node
4669 * from the argument or the context.
4670 *
4671 * @param[in] args Array of arguments.
4672 * @param[in] arg_count Count of elements in @p args.
4673 * @param[in,out] set Context and result set at the same time.
4674 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004675 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004676 */
4677static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004678xpath_normalize_space(struct lyxp_set **args, uint32_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004679{
Michal Vaskodd528af2022-08-08 14:35:07 +02004680 uint32_t i, new_used;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004681 char *new;
Radek Krejci857189e2020-09-01 13:26:36 +02004682 ly_bool have_spaces = 0, space_before = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004683 struct lysc_node_leaf *sleaf;
4684 LY_ERR rc = LY_SUCCESS;
4685
4686 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004687 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) &&
4688 (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004689 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004690 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
4691 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004692 } else if (!warn_is_string_type(sleaf->type)) {
4693 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004694 }
4695 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004696 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004697 return rc;
4698 }
4699
4700 if (arg_count) {
4701 set_fill_set(set, args[0]);
4702 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004703 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004704 LY_CHECK_RET(rc);
4705
4706 /* is there any normalization necessary? */
4707 for (i = 0; set->val.str[i]; ++i) {
4708 if (is_xmlws(set->val.str[i])) {
4709 if ((i == 0) || space_before || (!set->val.str[i + 1])) {
4710 have_spaces = 1;
4711 break;
4712 }
4713 space_before = 1;
4714 } else {
4715 space_before = 0;
4716 }
4717 }
4718
4719 /* yep, there is */
4720 if (have_spaces) {
4721 /* it's enough, at least one character will go, makes space for ending '\0' */
4722 new = malloc(strlen(set->val.str) * sizeof(char));
4723 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4724 new_used = 0;
4725
4726 space_before = 0;
4727 for (i = 0; set->val.str[i]; ++i) {
4728 if (is_xmlws(set->val.str[i])) {
4729 if ((i == 0) || space_before) {
4730 space_before = 1;
4731 continue;
4732 } else {
4733 space_before = 1;
4734 }
4735 } else {
4736 space_before = 0;
4737 }
4738
4739 new[new_used] = (space_before ? ' ' : set->val.str[i]);
4740 ++new_used;
4741 }
4742
4743 /* at worst there is one trailing space now */
4744 if (new_used && is_xmlws(new[new_used - 1])) {
4745 --new_used;
4746 }
4747
4748 new = ly_realloc(new, (new_used + 1) * sizeof(char));
4749 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4750 new[new_used] = '\0';
4751
4752 free(set->val.str);
4753 set->val.str = new;
4754 }
4755
4756 return LY_SUCCESS;
4757}
4758
4759/**
4760 * @brief Execute the XPath not(boolean) function. Returns LYXP_SET_BOOLEAN
4761 * with the argument converted to boolean and logically inverted.
4762 *
4763 * @param[in] args Array of arguments.
4764 * @param[in] arg_count Count of elements in @p args.
4765 * @param[in,out] set Context and result set at the same time.
4766 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004767 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004768 */
4769static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004770xpath_not(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004771{
4772 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004773 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004774 return LY_SUCCESS;
4775 }
4776
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004777 lyxp_set_cast(args[0], LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +02004778 if (args[0]->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004779 set_fill_boolean(set, 0);
4780 } else {
4781 set_fill_boolean(set, 1);
4782 }
4783
4784 return LY_SUCCESS;
4785}
4786
4787/**
4788 * @brief Execute the XPath number(object?) function. Returns LYXP_SET_NUMBER
4789 * with the number representation of either the argument or the context.
4790 *
4791 * @param[in] args Array of arguments.
4792 * @param[in] arg_count Count of elements in @p args.
4793 * @param[in,out] set Context and result set at the same time.
4794 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004795 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004796 */
4797static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004798xpath_number(struct lyxp_set **args, uint32_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004799{
4800 LY_ERR rc;
4801
4802 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004803 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004804 return LY_SUCCESS;
4805 }
4806
4807 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004808 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004809 LY_CHECK_RET(rc);
4810 set_fill_set(set, args[0]);
4811 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004812 rc = lyxp_set_cast(set, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004813 LY_CHECK_RET(rc);
4814 }
4815
4816 return LY_SUCCESS;
4817}
4818
4819/**
4820 * @brief Execute the XPath position() function. Returns LYXP_SET_NUMBER
4821 * with the context position.
4822 *
4823 * @param[in] args Array of arguments.
4824 * @param[in] arg_count Count of elements in @p args.
4825 * @param[in,out] set Context and result set at the same time.
4826 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004827 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004828 */
4829static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004830xpath_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 +02004831{
4832 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004833 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004834 return LY_SUCCESS;
4835 }
4836
Michal Vasko03ff5a72019-09-11 13:49:33 +02004837 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004838 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "position()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004839 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004840 } else if (!set->used) {
4841 set_fill_number(set, 0);
4842 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004843 }
4844
4845 set_fill_number(set, set->ctx_pos);
4846
4847 /* UNUSED in 'Release' build type */
4848 (void)options;
4849 return LY_SUCCESS;
4850}
4851
4852/**
4853 * @brief Execute the YANG 1.1 re-match(string, string) function. Returns LYXP_SET_BOOLEAN
4854 * depending on whether the second argument regex matches the first argument string. For details refer to
4855 * YANG 1.1 RFC section 10.2.1.
4856 *
4857 * @param[in] args Array of arguments.
4858 * @param[in] arg_count Count of elements in @p args.
4859 * @param[in,out] set Context and result set at the same time.
4860 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004861 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004862 */
4863static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004864xpath_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 +02004865{
4866 struct lysc_pattern **patterns = NULL, **pattern;
4867 struct lysc_node_leaf *sleaf;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004868 LY_ERR rc = LY_SUCCESS;
4869 struct ly_err_item *err;
4870
4871 if (options & LYXP_SCNODE_ALL) {
4872 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4873 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4874 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 +02004875 } else if (!warn_is_string_type(sleaf->type)) {
4876 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004877 }
4878 }
4879
4880 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4881 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4882 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 +02004883 } else if (!warn_is_string_type(sleaf->type)) {
4884 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004885 }
4886 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004887 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004888 return rc;
4889 }
4890
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004891 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004892 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004893 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004894 LY_CHECK_RET(rc);
4895
4896 LY_ARRAY_NEW_RET(set->ctx, patterns, pattern, LY_EMEM);
Radek Iša45802b52021-02-09 09:21:58 +01004897 *pattern = calloc(1, sizeof **pattern);
Radek Krejciddace2c2021-01-08 11:30:56 +01004898 LOG_LOCSET(NULL, set->cur_node, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01004899 rc = lys_compile_type_pattern_check(set->ctx, args[1]->val.str, &(*pattern)->code);
Michal Vasko4a7d4d62021-12-13 17:05:06 +01004900 if (set->cur_node) {
4901 LOG_LOCBACK(0, 1, 0, 0);
4902 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004903 if (rc != LY_SUCCESS) {
4904 LY_ARRAY_FREE(patterns);
4905 return rc;
4906 }
4907
Radek Krejci0b013302021-03-29 15:22:32 +02004908 rc = lyplg_type_validate_patterns(patterns, args[0]->val.str, strlen(args[0]->val.str), &err);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004909 pcre2_code_free((*pattern)->code);
4910 free(*pattern);
4911 LY_ARRAY_FREE(patterns);
4912 if (rc && (rc != LY_EVALID)) {
Michal Vasko177d0ed2020-11-23 16:43:03 +01004913 ly_err_print(set->ctx, err);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004914 ly_err_free(err);
4915 return rc;
4916 }
4917
4918 if (rc == LY_EVALID) {
4919 ly_err_free(err);
4920 set_fill_boolean(set, 0);
4921 } else {
4922 set_fill_boolean(set, 1);
4923 }
4924
4925 return LY_SUCCESS;
4926}
4927
4928/**
4929 * @brief Execute the XPath round(number) function. Returns LYXP_SET_NUMBER
4930 * with the rounded first argument. For details refer to
4931 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-round.
4932 *
4933 * @param[in] args Array of arguments.
4934 * @param[in] arg_count Count of elements in @p args.
4935 * @param[in,out] set Context and result set at the same time.
4936 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004937 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004938 */
4939static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004940xpath_round(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004941{
4942 struct lysc_node_leaf *sleaf;
4943 LY_ERR rc = LY_SUCCESS;
4944
4945 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02004946 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004947 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02004948 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4949 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4950 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
4951 sleaf->name);
4952 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
4953 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
4954 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004955 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004956 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004957 return rc;
4958 }
4959
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004960 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004961 LY_CHECK_RET(rc);
4962
4963 /* cover only the cases where floor can't be used */
4964 if ((args[0]->val.num == -0.0f) || ((args[0]->val.num < 0) && (args[0]->val.num >= -0.5))) {
4965 set_fill_number(set, -0.0f);
4966 } else {
4967 args[0]->val.num += 0.5;
4968 rc = xpath_floor(args, 1, args[0], options);
4969 LY_CHECK_RET(rc);
4970 set_fill_number(set, args[0]->val.num);
4971 }
4972
4973 return LY_SUCCESS;
4974}
4975
4976/**
4977 * @brief Execute the XPath starts-with(string, string) function.
4978 * Returns LYXP_SET_BOOLEAN whether the second argument is
4979 * the prefix of the first or not.
4980 *
4981 * @param[in] args Array of arguments.
4982 * @param[in] arg_count Count of elements in @p args.
4983 * @param[in,out] set Context and result set at the same time.
4984 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004985 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004986 */
4987static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004988xpath_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 +02004989{
4990 struct lysc_node_leaf *sleaf;
4991 LY_ERR rc = LY_SUCCESS;
4992
4993 if (options & LYXP_SCNODE_ALL) {
4994 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4995 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4996 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 +02004997 } else if (!warn_is_string_type(sleaf->type)) {
4998 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004999 }
5000 }
5001
5002 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5003 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5004 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 +02005005 } else if (!warn_is_string_type(sleaf->type)) {
5006 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005007 }
5008 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005009 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005010 return rc;
5011 }
5012
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005013 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005014 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005015 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005016 LY_CHECK_RET(rc);
5017
5018 if (strncmp(args[0]->val.str, args[1]->val.str, strlen(args[1]->val.str))) {
5019 set_fill_boolean(set, 0);
5020 } else {
5021 set_fill_boolean(set, 1);
5022 }
5023
5024 return LY_SUCCESS;
5025}
5026
5027/**
5028 * @brief Execute the XPath string(object?) function. Returns LYXP_SET_STRING
5029 * with the string representation of either the argument or the context.
5030 *
5031 * @param[in] args Array of arguments.
5032 * @param[in] arg_count Count of elements in @p args.
5033 * @param[in,out] set Context and result set at the same time.
5034 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005035 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005036 */
5037static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02005038xpath_string(struct lyxp_set **args, uint32_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005039{
5040 LY_ERR rc;
5041
5042 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02005043 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005044 return LY_SUCCESS;
5045 }
5046
5047 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005048 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005049 LY_CHECK_RET(rc);
5050 set_fill_set(set, args[0]);
5051 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005052 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005053 LY_CHECK_RET(rc);
5054 }
5055
5056 return LY_SUCCESS;
5057}
5058
5059/**
5060 * @brief Execute the XPath string-length(string?) function. Returns LYXP_SET_NUMBER
5061 * with the length of the string in either the argument or the context.
5062 *
5063 * @param[in] args Array of arguments.
5064 * @param[in] arg_count Count of elements in @p args.
5065 * @param[in,out] set Context and result set at the same time.
5066 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005067 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005068 */
5069static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02005070xpath_string_length(struct lyxp_set **args, uint32_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005071{
5072 struct lysc_node_leaf *sleaf;
5073 LY_ERR rc = LY_SUCCESS;
5074
5075 if (options & LYXP_SCNODE_ALL) {
5076 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5077 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5078 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 +02005079 } else if (!warn_is_string_type(sleaf->type)) {
5080 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005081 }
5082 }
5083 if (!arg_count && (set->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set))) {
5084 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5085 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 +02005086 } else if (!warn_is_string_type(sleaf->type)) {
5087 LOGWRN(set->ctx, "Argument #0 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005088 }
5089 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005090 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005091 return rc;
5092 }
5093
5094 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005095 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005096 LY_CHECK_RET(rc);
5097 set_fill_number(set, strlen(args[0]->val.str));
5098 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005099 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005100 LY_CHECK_RET(rc);
5101 set_fill_number(set, strlen(set->val.str));
5102 }
5103
5104 return LY_SUCCESS;
5105}
5106
5107/**
5108 * @brief Execute the XPath substring(string, number, number?) function.
5109 * Returns LYXP_SET_STRING substring of the first argument starting
5110 * on the second argument index ending on the third argument index,
5111 * indexed from 1. For exact definition refer to
5112 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-substring.
5113 *
5114 * @param[in] args Array of arguments.
5115 * @param[in] arg_count Count of elements in @p args.
5116 * @param[in,out] set Context and result set at the same time.
5117 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005118 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005119 */
5120static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02005121xpath_substring(struct lyxp_set **args, uint32_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005122{
Michal Vasko6db996e2022-07-28 10:28:04 +02005123 int64_t start;
5124 int32_t len;
Michal Vaskodd528af2022-08-08 14:35:07 +02005125 uint32_t str_start, str_len, pos;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005126 struct lysc_node_leaf *sleaf;
5127 LY_ERR rc = LY_SUCCESS;
5128
5129 if (options & LYXP_SCNODE_ALL) {
5130 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5131 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5132 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 +02005133 } else if (!warn_is_string_type(sleaf->type)) {
5134 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005135 }
5136 }
5137
5138 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5139 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5140 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 +02005141 } else if (!warn_is_numeric_type(sleaf->type)) {
5142 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005143 }
5144 }
5145
Michal Vasko69730152020-10-09 16:30:07 +02005146 if ((arg_count == 3) && (args[2]->type == LYXP_SET_SCNODE_SET) &&
5147 (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005148 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5149 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 +02005150 } else if (!warn_is_numeric_type(sleaf->type)) {
5151 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005152 }
5153 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005154 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005155 return rc;
5156 }
5157
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005158 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005159 LY_CHECK_RET(rc);
5160
5161 /* start */
5162 if (xpath_round(&args[1], 1, args[1], options)) {
5163 return -1;
5164 }
5165 if (isfinite(args[1]->val.num)) {
5166 start = args[1]->val.num - 1;
5167 } else if (isinf(args[1]->val.num) && signbit(args[1]->val.num)) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02005168 start = INT32_MIN;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005169 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02005170 start = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005171 }
5172
5173 /* len */
5174 if (arg_count == 3) {
5175 rc = xpath_round(&args[2], 1, args[2], options);
5176 LY_CHECK_RET(rc);
Radek Krejci1deb5be2020-08-26 16:43:36 +02005177 if (isnan(args[2]->val.num) || signbit(args[2]->val.num)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005178 len = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005179 } else if (isfinite(args[2]->val.num)) {
5180 len = args[2]->val.num;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005181 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02005182 len = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005183 }
5184 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02005185 len = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005186 }
5187
5188 /* find matching character positions */
5189 str_start = 0;
5190 str_len = 0;
5191 for (pos = 0; args[0]->val.str[pos]; ++pos) {
5192 if (pos < start) {
5193 ++str_start;
5194 } else if (pos < start + len) {
5195 ++str_len;
5196 } else {
5197 break;
5198 }
5199 }
5200
5201 set_fill_string(set, args[0]->val.str + str_start, str_len);
5202 return LY_SUCCESS;
5203}
5204
5205/**
5206 * @brief Execute the XPath substring-after(string, string) function.
5207 * Returns LYXP_SET_STRING with the string succeeding the occurance
5208 * of the second argument in the first or an empty string.
5209 *
5210 * @param[in] args Array of arguments.
5211 * @param[in] arg_count Count of elements in @p args.
5212 * @param[in,out] set Context and result set at the same time.
5213 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005214 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005215 */
5216static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02005217xpath_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 +02005218{
5219 char *ptr;
5220 struct lysc_node_leaf *sleaf;
5221 LY_ERR rc = LY_SUCCESS;
5222
5223 if (options & LYXP_SCNODE_ALL) {
5224 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5225 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5226 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 +02005227 } else if (!warn_is_string_type(sleaf->type)) {
5228 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005229 }
5230 }
5231
5232 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5233 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5234 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 +02005235 } else if (!warn_is_string_type(sleaf->type)) {
5236 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005237 }
5238 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005239 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005240 return rc;
5241 }
5242
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005243 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005244 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005245 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005246 LY_CHECK_RET(rc);
5247
5248 ptr = strstr(args[0]->val.str, args[1]->val.str);
5249 if (ptr) {
5250 set_fill_string(set, ptr + strlen(args[1]->val.str), strlen(ptr + strlen(args[1]->val.str)));
5251 } else {
5252 set_fill_string(set, "", 0);
5253 }
5254
5255 return LY_SUCCESS;
5256}
5257
5258/**
5259 * @brief Execute the XPath substring-before(string, string) function.
5260 * Returns LYXP_SET_STRING with the string preceding the occurance
5261 * of the second argument in the first or an empty string.
5262 *
5263 * @param[in] args Array of arguments.
5264 * @param[in] arg_count Count of elements in @p args.
5265 * @param[in,out] set Context and result set at the same time.
5266 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005267 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005268 */
5269static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02005270xpath_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 +02005271{
5272 char *ptr;
5273 struct lysc_node_leaf *sleaf;
5274 LY_ERR rc = LY_SUCCESS;
5275
5276 if (options & LYXP_SCNODE_ALL) {
5277 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5278 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5279 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 +02005280 } else if (!warn_is_string_type(sleaf->type)) {
5281 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005282 }
5283 }
5284
5285 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5286 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5287 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 +02005288 } else if (!warn_is_string_type(sleaf->type)) {
5289 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005290 }
5291 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005292 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005293 return rc;
5294 }
5295
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005296 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005297 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005298 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005299 LY_CHECK_RET(rc);
5300
5301 ptr = strstr(args[0]->val.str, args[1]->val.str);
5302 if (ptr) {
5303 set_fill_string(set, args[0]->val.str, ptr - args[0]->val.str);
5304 } else {
5305 set_fill_string(set, "", 0);
5306 }
5307
5308 return LY_SUCCESS;
5309}
5310
5311/**
5312 * @brief Execute the XPath sum(node-set) function. Returns LYXP_SET_NUMBER
5313 * with the sum of all the nodes in the context.
5314 *
5315 * @param[in] args Array of arguments.
5316 * @param[in] arg_count Count of elements in @p args.
5317 * @param[in,out] set Context and result set at the same time.
5318 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005319 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005320 */
5321static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02005322xpath_sum(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005323{
5324 long double num;
5325 char *str;
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01005326 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005327 struct lyxp_set set_item;
5328 struct lysc_node_leaf *sleaf;
5329 LY_ERR rc = LY_SUCCESS;
5330
5331 if (options & LYXP_SCNODE_ALL) {
5332 if (args[0]->type == LYXP_SET_SCNODE_SET) {
5333 for (i = 0; i < args[0]->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005334 if (args[0]->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005335 sleaf = (struct lysc_node_leaf *)args[0]->val.scnodes[i].scnode;
5336 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5337 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__,
Michal Vasko69730152020-10-09 16:30:07 +02005338 lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005339 } else if (!warn_is_numeric_type(sleaf->type)) {
5340 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005341 }
5342 }
5343 }
5344 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005345 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005346 return rc;
5347 }
5348
5349 set_fill_number(set, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005350
5351 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005352 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "sum(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02005353 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02005354 } else if (!args[0]->used) {
5355 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005356 }
5357
Michal Vasko5c4e5892019-11-14 12:31:38 +01005358 set_init(&set_item, set);
5359
Michal Vasko03ff5a72019-09-11 13:49:33 +02005360 set_item.type = LYXP_SET_NODE_SET;
Michal Vasko41decbf2021-11-02 11:50:21 +01005361 set_item.val.nodes = calloc(1, sizeof *set_item.val.nodes);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005362 LY_CHECK_ERR_RET(!set_item.val.nodes, LOGMEM(set->ctx), LY_EMEM);
5363
5364 set_item.used = 1;
5365 set_item.size = 1;
5366
5367 for (i = 0; i < args[0]->used; ++i) {
5368 set_item.val.nodes[0] = args[0]->val.nodes[i];
5369
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005370 rc = cast_node_set_to_string(&set_item, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005371 LY_CHECK_RET(rc);
5372 num = cast_string_to_number(str);
5373 free(str);
5374 set->val.num += num;
5375 }
5376
5377 free(set_item.val.nodes);
5378
5379 return LY_SUCCESS;
5380}
5381
5382/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02005383 * @brief Execute the XPath translate(string, string, string) function.
5384 * Returns LYXP_SET_STRING with the first argument with the characters
5385 * from the second argument replaced by those on the corresponding
5386 * positions in the third argument.
5387 *
5388 * @param[in] args Array of arguments.
5389 * @param[in] arg_count Count of elements in @p args.
5390 * @param[in,out] set Context and result set at the same time.
5391 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005392 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005393 */
5394static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02005395xpath_translate(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005396{
Michal Vaskodd528af2022-08-08 14:35:07 +02005397 uint32_t i, j, new_used;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005398 char *new;
Radek Krejci857189e2020-09-01 13:26:36 +02005399 ly_bool have_removed;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005400 struct lysc_node_leaf *sleaf;
5401 LY_ERR rc = LY_SUCCESS;
5402
5403 if (options & LYXP_SCNODE_ALL) {
5404 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5405 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5406 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 +02005407 } else if (!warn_is_string_type(sleaf->type)) {
5408 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005409 }
5410 }
5411
5412 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5413 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5414 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 +02005415 } else if (!warn_is_string_type(sleaf->type)) {
5416 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005417 }
5418 }
5419
5420 if ((args[2]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
5421 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5422 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 +02005423 } else if (!warn_is_string_type(sleaf->type)) {
5424 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005425 }
5426 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005427 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005428 return rc;
5429 }
5430
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005431 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005432 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005433 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005434 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005435 rc = lyxp_set_cast(args[2], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005436 LY_CHECK_RET(rc);
5437
5438 new = malloc((strlen(args[0]->val.str) + 1) * sizeof(char));
5439 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5440 new_used = 0;
5441
5442 have_removed = 0;
5443 for (i = 0; args[0]->val.str[i]; ++i) {
Radek Krejci857189e2020-09-01 13:26:36 +02005444 ly_bool found = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005445
5446 for (j = 0; args[1]->val.str[j]; ++j) {
5447 if (args[0]->val.str[i] == args[1]->val.str[j]) {
5448 /* removing this char */
5449 if (j >= strlen(args[2]->val.str)) {
5450 have_removed = 1;
5451 found = 1;
5452 break;
5453 }
5454 /* replacing this char */
5455 new[new_used] = args[2]->val.str[j];
5456 ++new_used;
5457 found = 1;
5458 break;
5459 }
5460 }
5461
5462 /* copying this char */
5463 if (!found) {
5464 new[new_used] = args[0]->val.str[i];
5465 ++new_used;
5466 }
5467 }
5468
5469 if (have_removed) {
5470 new = ly_realloc(new, (new_used + 1) * sizeof(char));
5471 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5472 }
5473 new[new_used] = '\0';
5474
Michal Vaskod3678892020-05-21 10:06:58 +02005475 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005476 set->type = LYXP_SET_STRING;
5477 set->val.str = new;
5478
5479 return LY_SUCCESS;
5480}
5481
5482/**
5483 * @brief Execute the XPath true() function. Returns LYXP_SET_BOOLEAN
5484 * with true value.
5485 *
5486 * @param[in] args Array of arguments.
5487 * @param[in] arg_count Count of elements in @p args.
5488 * @param[in,out] set Context and result set at the same time.
5489 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005490 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005491 */
5492static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02005493xpath_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 +02005494{
5495 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02005496 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005497 return LY_SUCCESS;
5498 }
5499
5500 set_fill_boolean(set, 1);
5501 return LY_SUCCESS;
5502}
5503
Michal Vasko03ff5a72019-09-11 13:49:33 +02005504/**
Michal Vasko49fec8e2022-05-24 10:28:33 +02005505 * @brief Execute the XPath node() processing instruction (node type). Returns LYXP_SET_NODE_SET
5506 * with only nodes from the context.
5507 *
5508 * @param[in,out] set Context and result set at the same time.
5509 * @param[in] axis Axis to search on.
5510 * @param[in] options XPath options.
5511 * @return LY_ERR
5512 */
5513static LY_ERR
5514xpath_pi_node(struct lyxp_set *set, enum lyxp_axis axis, uint32_t options)
5515{
5516 if (options & LYXP_SCNODE_ALL) {
5517 return moveto_scnode(set, NULL, NULL, axis, options);
5518 }
5519
5520 if (set->type != LYXP_SET_NODE_SET) {
5521 lyxp_set_free_content(set);
5522 return LY_SUCCESS;
5523 }
5524
5525 /* just like moving to a node with no restrictions */
5526 return moveto_node(set, NULL, NULL, axis, options);
5527}
5528
5529/**
5530 * @brief Execute the XPath text() processing instruction (node type). Returns LYXP_SET_NODE_SET
5531 * with the text content of the nodes in the context.
5532 *
5533 * @param[in,out] set Context and result set at the same time.
5534 * @param[in] axis Axis to search on.
5535 * @param[in] options XPath options.
5536 * @return LY_ERR
5537 */
5538static LY_ERR
5539xpath_pi_text(struct lyxp_set *set, enum lyxp_axis axis, uint32_t options)
5540{
5541 uint32_t i;
5542
5543 if (options & LYXP_SCNODE_ALL) {
5544 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
5545 return LY_SUCCESS;
5546 }
5547
5548 if (set->type != LYXP_SET_NODE_SET) {
5549 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "text()");
5550 return LY_EVALID;
5551 }
5552
5553 if (axis != LYXP_AXIS_CHILD) {
5554 /* even following and preceding axescan return text nodes, but whatever */
5555 lyxp_set_free_content(set);
5556 return LY_SUCCESS;
5557 }
5558
5559 for (i = 0; i < set->used; ++i) {
5560 switch (set->val.nodes[i].type) {
5561 case LYXP_NODE_NONE:
5562 LOGINT_RET(set->ctx);
5563 case LYXP_NODE_ELEM:
5564 if (set->val.nodes[i].node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
5565 set->val.nodes[i].type = LYXP_NODE_TEXT;
5566 break;
5567 }
5568 /* fall through */
5569 case LYXP_NODE_ROOT:
5570 case LYXP_NODE_ROOT_CONFIG:
5571 case LYXP_NODE_TEXT:
5572 case LYXP_NODE_META:
5573 set_remove_node_none(set, i);
5574 break;
5575 }
5576 }
5577 set_remove_nodes_none(set);
5578
5579 return LY_SUCCESS;
5580}
5581
5582/**
Michal Vasko6346ece2019-09-24 13:12:53 +02005583 * @brief Skip prefix and return corresponding model if there is a prefix. Logs directly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005584 *
Michal Vasko2104e9f2020-03-06 08:23:25 +01005585 * XPath @p set is expected to be a (sc)node set!
5586 *
Michal Vasko6346ece2019-09-24 13:12:53 +02005587 * @param[in,out] qname Qualified node name. If includes prefix, it is skipped.
5588 * @param[in,out] qname_len Length of @p qname, is updated accordingly.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005589 * @param[in] set Set with general XPath context.
5590 * @param[in] ctx_scnode Context node to inherit module for unprefixed node for ::LY_PREF_JSON.
Michal Vasko6346ece2019-09-24 13:12:53 +02005591 * @param[out] moveto_mod Expected module of a matching node.
5592 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005593 */
Michal Vasko6346ece2019-09-24 13:12:53 +02005594static LY_ERR
Michal Vaskofe1af042022-07-29 14:58:59 +02005595moveto_resolve_model(const char **qname, uint32_t *qname_len, const struct lyxp_set *set,
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005596 const struct lysc_node *ctx_scnode, const struct lys_module **moveto_mod)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005597{
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02005598 const struct lys_module *mod = NULL;
Michal Vasko6346ece2019-09-24 13:12:53 +02005599 const char *ptr;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005600 size_t pref_len;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005601
Michal Vasko2104e9f2020-03-06 08:23:25 +01005602 assert((set->type == LYXP_SET_NODE_SET) || (set->type == LYXP_SET_SCNODE_SET));
5603
Michal Vasko6346ece2019-09-24 13:12:53 +02005604 if ((ptr = ly_strnchr(*qname, ':', *qname_len))) {
5605 /* specific module */
5606 pref_len = ptr - *qname;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005607 mod = ly_resolve_prefix(set->ctx, *qname, pref_len, set->format, set->prefix_data);
Michal Vasko6346ece2019-09-24 13:12:53 +02005608
Michal Vasko004d3152020-06-11 19:59:22 +02005609 /* check for errors and non-implemented modules, as they are not valid */
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005610 if (!mod || !mod->implemented) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005611 LOGVAL(set->ctx, LY_VCODE_XP_INMOD, pref_len, *qname);
Michal Vasko6346ece2019-09-24 13:12:53 +02005612 return LY_EVALID;
5613 }
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005614
Michal Vasko6346ece2019-09-24 13:12:53 +02005615 *qname += pref_len + 1;
5616 *qname_len -= pref_len + 1;
5617 } else if (((*qname)[0] == '*') && (*qname_len == 1)) {
5618 /* all modules - special case */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005619 mod = NULL;
Michal Vasko6346ece2019-09-24 13:12:53 +02005620 } else {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005621 switch (set->format) {
Radek Krejci8df109d2021-04-23 12:19:08 +02005622 case LY_VALUE_SCHEMA:
5623 case LY_VALUE_SCHEMA_RESOLVED:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005624 /* current module */
5625 mod = set->cur_mod;
5626 break;
Radek Krejci224d4b42021-04-23 13:54:59 +02005627 case LY_VALUE_CANON:
Radek Krejci8df109d2021-04-23 12:19:08 +02005628 case LY_VALUE_JSON:
Radek Krejcif9943642021-04-26 10:18:21 +02005629 case LY_VALUE_LYB:
Michal Vaskoddd76592022-01-17 13:34:48 +01005630 case LY_VALUE_STR_NS:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005631 /* inherit parent (context node) module */
5632 if (ctx_scnode) {
5633 mod = ctx_scnode->module;
5634 } else {
5635 mod = NULL;
5636 }
5637 break;
Radek Krejci8df109d2021-04-23 12:19:08 +02005638 case LY_VALUE_XML:
Michal Vasko52143d12021-04-14 15:36:39 +02005639 /* all nodes need to be prefixed */
5640 LOGVAL(set->ctx, LYVE_DATA, "Non-prefixed node \"%.*s\" in XML xpath found.", *qname_len, *qname);
5641 return LY_EVALID;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005642 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005643 }
5644
Michal Vasko6346ece2019-09-24 13:12:53 +02005645 *moveto_mod = mod;
5646 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005647}
5648
5649/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02005650 * @brief Move context @p set to the root. Handles absolute path.
5651 * Result is LYXP_SET_NODE_SET.
5652 *
5653 * @param[in,out] set Set to use.
5654 * @param[in] options Xpath options.
Michal Vaskob0099a92020-08-31 14:55:23 +02005655 * @return LY_ERR value.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005656 */
Michal Vaskob0099a92020-08-31 14:55:23 +02005657static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005658moveto_root(struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005659{
aPiecek8b0cc152021-05-31 16:40:31 +02005660 assert(!(options & LYXP_SKIP_EXPR));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005661
5662 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02005663 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko7333cb32022-07-29 16:30:29 +02005664 LY_CHECK_RET(set_scnode_insert_node(set, NULL, set->root_type, LYXP_AXIS_SELF, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005665 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005666 set->type = LYXP_SET_NODE_SET;
5667 set->used = 0;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005668 set_insert_node(set, NULL, 0, set->root_type, 0);
Michal Vasko306e2832022-07-25 09:15:17 +02005669 set->non_child_axis = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005670 }
Michal Vaskob0099a92020-08-31 14:55:23 +02005671
5672 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005673}
5674
5675/**
5676 * @brief Check @p node as a part of NameTest processing.
5677 *
5678 * @param[in] node Node to check.
Michal Vasko49fec8e2022-05-24 10:28:33 +02005679 * @param[in] node_type Node type of @p node.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005680 * @param[in] set Set to read general context from.
Michal Vaskod3678892020-05-21 10:06:58 +02005681 * @param[in] node_name Node name in the dictionary to move to, NULL for any node.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005682 * @param[in] moveto_mod Expected module of the node, NULL for no prefix.
Michal Vaskocdad7122020-11-09 21:04:44 +01005683 * @param[in] options XPath options.
Michal Vasko6346ece2019-09-24 13:12:53 +02005684 * @return LY_ERR (LY_ENOT if node does not match, LY_EINCOMPLETE on unresolved when,
Michal Vaskocd2c88a2022-06-07 10:54:34 +02005685 * LY_EINVAL if neither node nor any children match)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005686 */
5687static LY_ERR
Michal Vasko49fec8e2022-05-24 10:28:33 +02005688moveto_node_check(const struct lyd_node *node, enum lyxp_node_type node_type, const struct lyxp_set *set,
5689 const char *node_name, const struct lys_module *moveto_mod, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005690{
Michal Vasko49fec8e2022-05-24 10:28:33 +02005691 if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT)) {
5692 assert(node_type == set->root_type);
5693
5694 if (node_name || moveto_mod) {
5695 /* root will not match a specific node */
5696 return LY_ENOT;
5697 }
5698 return LY_SUCCESS;
5699 } else if (node_type != LYXP_NODE_ELEM) {
5700 /* other types will not match */
5701 return LY_ENOT;
5702 }
5703
Michal Vaskodca9f122021-07-16 13:56:22 +02005704 if (!node->schema) {
5705 /* opaque node never matches */
5706 return LY_ENOT;
5707 }
5708
Michal Vasko03ff5a72019-09-11 13:49:33 +02005709 /* module check */
Michal Vasko19089f02022-06-07 11:02:11 +02005710 if (moveto_mod) {
Michal Vasko0a1eb752022-09-29 11:00:34 +02005711 if ((set->ctx == LYD_CTX(node)) && (node->schema->module != moveto_mod)) {
Michal Vasko19089f02022-06-07 11:02:11 +02005712 return LY_ENOT;
Michal Vasko0a1eb752022-09-29 11:00:34 +02005713 } else if ((set->ctx != LYD_CTX(node)) && strcmp(node->schema->module->name, moveto_mod->name)) {
Michal Vasko19089f02022-06-07 11:02:11 +02005714 return LY_ENOT;
5715 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005716 }
5717
Michal Vasko5c4e5892019-11-14 12:31:38 +01005718 /* context check */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005719 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (node->schema->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005720 return LY_EINVAL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005721 } else if (set->context_op && (node->schema->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) &&
5722 (node->schema != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005723 return LY_EINVAL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005724 }
5725
5726 /* name check */
Michal Vasko19089f02022-06-07 11:02:11 +02005727 if (node_name) {
Michal Vasko0a1eb752022-09-29 11:00:34 +02005728 if ((set->ctx == LYD_CTX(node)) && (node->schema->name != node_name)) {
Michal Vasko19089f02022-06-07 11:02:11 +02005729 return LY_ENOT;
Michal Vasko0a1eb752022-09-29 11:00:34 +02005730 } else if ((set->ctx != LYD_CTX(node)) && strcmp(node->schema->name, node_name)) {
Michal Vasko19089f02022-06-07 11:02:11 +02005731 return LY_ENOT;
5732 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005733 }
5734
Michal Vasko6d657122022-10-19 14:38:35 +02005735 /* when check, accept the context node because it should only be the path ".", we have checked the when is valid before */
5736 if (!(options & LYXP_IGNORE_WHEN) && lysc_has_when(node->schema) && !(node->flags & LYD_WHEN_TRUE) &&
5737 (node != set->cur_node)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005738 return LY_EINCOMPLETE;
Michal Vaskoa1424542019-11-14 16:08:52 +01005739 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005740
5741 /* match */
5742 return LY_SUCCESS;
5743}
5744
5745/**
Michal Vasko49fec8e2022-05-24 10:28:33 +02005746 * @brief Get the next node in a forward DFS.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005747 *
Michal Vasko49fec8e2022-05-24 10:28:33 +02005748 * @param[in] iter Last returned node.
5749 * @param[in] stop Node to stop the search on and not return.
5750 * @return Next node, NULL if there are no more.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005751 */
Michal Vasko49fec8e2022-05-24 10:28:33 +02005752static const struct lyd_node *
5753moveto_axis_node_next_dfs_forward(const struct lyd_node *iter, const struct lyd_node *stop)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005754{
Michal Vasko49fec8e2022-05-24 10:28:33 +02005755 const struct lyd_node *next = NULL;
5756
5757 /* 1) child */
5758 next = lyd_child(iter);
5759 if (!next) {
5760 if (iter == stop) {
5761 /* reached stop, no more descendants */
5762 return NULL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005763 }
Michal Vasko49fec8e2022-05-24 10:28:33 +02005764 /* 2) child next sibling */
5765 next = iter->next;
5766 }
5767 while (!next) {
5768 iter = lyd_parent(iter);
5769 if ((!stop && !iter) || (stop && (lyd_parent(iter) == lyd_parent(stop)))) {
5770 return NULL;
5771 }
5772 next = iter->next;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005773 }
5774
Michal Vasko49fec8e2022-05-24 10:28:33 +02005775 return next;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005776}
5777
5778/**
Michal Vasko49fec8e2022-05-24 10:28:33 +02005779 * @brief Get the next node in a backward DFS.
5780 *
5781 * @param[in] iter Last returned node.
5782 * @param[in] stop Node to stop the search on and not return.
5783 * @return Next node, NULL if there are no more.
5784 */
5785static const struct lyd_node *
5786moveto_axis_node_next_dfs_backward(const struct lyd_node *iter, const struct lyd_node *stop)
5787{
5788 const struct lyd_node *next = NULL;
5789
5790 /* 1) previous sibling innermost last child */
5791 next = iter->prev->next ? iter->prev : NULL;
5792 while (next && lyd_child(next)) {
5793 next = lyd_child(next);
5794 next = next->prev;
5795 }
5796
5797 if (!next) {
5798 /* 2) parent */
5799 iter = lyd_parent(iter);
5800 if ((!stop && !iter) || (stop && (lyd_parent(iter) == lyd_parent(stop)))) {
5801 return NULL;
5802 }
5803 next = iter;
5804 }
5805
5806 return next;
5807}
5808
5809/**
5810 * @brief Get the first node on an axis for a context node.
5811 *
5812 * @param[in,out] iter NULL, updated to the next node.
5813 * @param[in,out] iter_type Node type 0 of @p iter, updated to the node type of the next node.
5814 * @param[in] node Context node.
5815 * @param[in] node_type Type of @p node.
5816 * @param[in] axis Axis to use.
5817 * @param[in] set XPath set with the general context.
5818 * @return LY_SUCCESS on success.
5819 * @return LY_ENOTFOUND if no next node found.
5820 */
5821static LY_ERR
5822moveto_axis_node_next_first(const struct lyd_node **iter, enum lyxp_node_type *iter_type, const struct lyd_node *node,
5823 enum lyxp_node_type node_type, enum lyxp_axis axis, struct lyxp_set *set)
5824{
5825 const struct lyd_node *next = NULL;
5826 enum lyxp_node_type next_type = 0;
5827
5828 assert(!*iter);
5829 assert(!*iter_type);
5830
5831 switch (axis) {
5832 case LYXP_AXIS_ANCESTOR_OR_SELF:
5833 case LYXP_AXIS_DESCENDANT_OR_SELF:
5834 case LYXP_AXIS_SELF:
5835 /* return the context node */
5836 next = node;
5837 next_type = node_type;
5838 break;
5839
5840 case LYXP_AXIS_ANCESTOR:
5841 case LYXP_AXIS_PARENT:
5842 if (node_type == LYXP_NODE_ELEM) {
5843 next = lyd_parent(node);
5844 next_type = next ? LYXP_NODE_ELEM : set->root_type;
5845 } else if (node_type == LYXP_NODE_TEXT) {
5846 next = node;
5847 next_type = LYXP_NODE_ELEM;
5848 } else if (node_type == LYXP_NODE_META) {
5849 next = ((struct lyd_meta *)node)->parent;
5850 next_type = LYXP_NODE_ELEM;
5851 } /* else root does not have a parent */
5852 break;
5853
5854 case LYXP_AXIS_CHILD:
5855 if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT)) {
5856 assert(!node);
5857
5858 /* search in all the trees */
5859 next = set->tree;
5860 next_type = next ? LYXP_NODE_ELEM : 0;
5861 } else {
5862 /* search in children */
5863 next = lyd_child(node);
5864 next_type = next ? LYXP_NODE_ELEM : 0;
5865 }
5866 break;
5867
5868 case LYXP_AXIS_DESCENDANT:
5869 if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT)) {
5870 /* top-level nodes */
5871 next = set->tree;
5872 next_type = LYXP_NODE_ELEM;
5873 } else if (node_type == LYXP_NODE_ELEM) {
5874 /* start from the context node */
5875 next = moveto_axis_node_next_dfs_forward(node, node);
5876 next_type = next ? LYXP_NODE_ELEM : 0;
5877 } /* else no children */
5878 break;
5879
5880 case LYXP_AXIS_FOLLOWING:
5881 case LYXP_AXIS_FOLLOWING_SIBLING:
5882 if (node_type == LYXP_NODE_ELEM) {
5883 /* first next sibling */
5884 next = node->next;
5885 next_type = next ? LYXP_NODE_ELEM : 0;
5886 } /* else no sibling */
5887 break;
5888
5889 case LYXP_AXIS_PRECEDING:
5890 if ((node_type == LYXP_NODE_ELEM) && node->prev->next) {
5891 /* skip ancestors */
5892 next = moveto_axis_node_next_dfs_backward(node, NULL);
5893 assert(next);
5894 next_type = LYXP_NODE_ELEM;
5895 } /* else no sibling */
5896 break;
5897
5898 case LYXP_AXIS_PRECEDING_SIBLING:
5899 if (node_type == LYXP_NODE_ELEM) {
5900 /* first previous sibling */
5901 next = node->prev->next ? node->prev : NULL;
5902 next_type = next ? LYXP_NODE_ELEM : 0;
5903 } /* else no sibling */
5904 break;
5905
5906 case LYXP_AXIS_ATTRIBUTE:
5907 /* handled specially */
5908 assert(0);
5909 LOGINT(set->ctx);
5910 break;
5911 }
5912
5913 *iter = next;
5914 *iter_type = next_type;
5915 return next_type ? LY_SUCCESS : LY_ENOTFOUND;
5916}
5917
5918/**
5919 * @brief Iterate over all nodes on an axis for a context node.
5920 *
5921 * @param[in,out] iter Last returned node, start with NULL, updated to the next node.
5922 * @param[in,out] iter_type Node type of @p iter, start with 0, updated to the node type of the next node.
5923 * @param[in] node Context node.
5924 * @param[in] node_type Type of @p node.
5925 * @param[in] axis Axis to use.
5926 * @param[in] set XPath set with the general context.
5927 * @return LY_SUCCESS on success.
5928 * @return LY_ENOTFOUND if no next node found.
5929 */
5930static LY_ERR
5931moveto_axis_node_next(const struct lyd_node **iter, enum lyxp_node_type *iter_type, const struct lyd_node *node,
5932 enum lyxp_node_type node_type, enum lyxp_axis axis, struct lyxp_set *set)
5933{
5934 const struct lyd_node *next = NULL;
5935 enum lyxp_node_type next_type = 0;
5936
5937 if (!*iter_type) {
5938 /* first returned node */
5939 return moveto_axis_node_next_first(iter, iter_type, node, node_type, axis, set);
5940 }
5941
5942 switch (axis) {
5943 case LYXP_AXIS_ANCESTOR_OR_SELF:
5944 if ((*iter == node) && (*iter_type == node_type)) {
5945 /* fake first ancestor, we returned self before */
5946 *iter = NULL;
5947 *iter_type = 0;
5948 return moveto_axis_node_next_first(iter, iter_type, node, node_type, LYXP_AXIS_ANCESTOR, set);
5949 } /* else continue ancestor */
5950
5951 /* fallthrough */
5952 case LYXP_AXIS_ANCESTOR:
5953 if (*iter_type == LYXP_NODE_ELEM) {
5954 /* iter parent */
5955 next = lyd_parent(*iter);
5956 next_type = next ? LYXP_NODE_ELEM : set->root_type;
5957 } /* else root, no ancestors */
5958 break;
5959
5960 case LYXP_AXIS_CHILD:
5961 assert(*iter_type == LYXP_NODE_ELEM);
5962
5963 /* next sibling (child) */
5964 next = (*iter)->next;
5965 next_type = next ? LYXP_NODE_ELEM : 0;
5966 break;
5967
5968 case LYXP_AXIS_DESCENDANT_OR_SELF:
5969 if ((*iter == node) && (*iter_type == node_type)) {
5970 /* fake first descendant, we returned self before */
5971 *iter = NULL;
5972 *iter_type = 0;
5973 return moveto_axis_node_next_first(iter, iter_type, node, node_type, LYXP_AXIS_DESCENDANT, set);
5974 } /* else continue descendant */
5975
5976 /* fallthrough */
5977 case LYXP_AXIS_DESCENDANT:
5978 assert(*iter_type == LYXP_NODE_ELEM);
5979 next = moveto_axis_node_next_dfs_forward(*iter, node);
5980 next_type = next ? LYXP_NODE_ELEM : 0;
5981 break;
5982
5983 case LYXP_AXIS_FOLLOWING:
5984 assert(*iter_type == LYXP_NODE_ELEM);
5985 next = moveto_axis_node_next_dfs_forward(*iter, NULL);
5986 next_type = next ? LYXP_NODE_ELEM : 0;
5987 break;
5988
5989 case LYXP_AXIS_FOLLOWING_SIBLING:
5990 assert(*iter_type == LYXP_NODE_ELEM);
5991
5992 /* next sibling */
5993 next = (*iter)->next;
5994 next_type = next ? LYXP_NODE_ELEM : 0;
5995 break;
5996
5997 case LYXP_AXIS_PARENT:
5998 case LYXP_AXIS_SELF:
5999 /* parent/self was returned before */
6000 break;
6001
6002 case LYXP_AXIS_PRECEDING:
6003 assert(*iter_type == LYXP_NODE_ELEM);
6004 next = moveto_axis_node_next_dfs_backward(*iter, NULL);
6005 next_type = next ? LYXP_NODE_ELEM : 0;
6006 break;
6007
6008 case LYXP_AXIS_PRECEDING_SIBLING:
6009 assert(*iter_type == LYXP_NODE_ELEM);
6010
6011 /* previous sibling */
6012 next = (*iter)->prev->next ? (*iter)->prev : NULL;
6013 next_type = next ? LYXP_NODE_ELEM : 0;
6014 break;
6015
6016 case LYXP_AXIS_ATTRIBUTE:
6017 /* handled specially */
6018 assert(0);
6019 LOGINT(set->ctx);
6020 break;
6021 }
6022
6023 *iter = next;
6024 *iter_type = next_type;
6025 return next_type ? LY_SUCCESS : LY_ENOTFOUND;
6026}
6027
6028/**
6029 * @brief Move context @p set to a node. Result is LYXP_SET_NODE_SET. Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006030 *
6031 * @param[in,out] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006032 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02006033 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko49fec8e2022-05-24 10:28:33 +02006034 * @param[in] axis Axis to search on.
Michal Vaskocdad7122020-11-09 21:04:44 +01006035 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006036 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6037 */
6038static LY_ERR
Michal Vasko49fec8e2022-05-24 10:28:33 +02006039moveto_node(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, enum lyxp_axis axis,
6040 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006041{
Michal Vasko230cf972021-12-02 12:31:00 +01006042 LY_ERR r, rc = LY_SUCCESS;
Michal Vasko49fec8e2022-05-24 10:28:33 +02006043 const struct lyd_node *iter;
6044 enum lyxp_node_type iter_type;
Michal Vasko230cf972021-12-02 12:31:00 +01006045 struct lyxp_set result;
Michal Vasko49fec8e2022-05-24 10:28:33 +02006046 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006047
aPiecek8b0cc152021-05-31 16:40:31 +02006048 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006049 return LY_SUCCESS;
6050 }
6051
6052 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006053 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006054 return LY_EVALID;
6055 }
6056
Michal Vasko230cf972021-12-02 12:31:00 +01006057 /* init result set */
6058 set_init(&result, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006059
Michal Vasko49fec8e2022-05-24 10:28:33 +02006060 for (i = 0; i < set->used; ++i) {
6061 /* iterate over all the nodes on the axis of the node */
6062 iter = NULL;
6063 iter_type = 0;
6064 while (!moveto_axis_node_next(&iter, &iter_type, set->val.nodes[i].node, set->val.nodes[i].type, axis, set)) {
6065 r = moveto_node_check(iter, iter_type, set, ncname, moveto_mod, options);
6066 if (r == LY_EINCOMPLETE) {
Michal Vasko230cf972021-12-02 12:31:00 +01006067 rc = r;
6068 goto cleanup;
Michal Vasko49fec8e2022-05-24 10:28:33 +02006069 } else if (r) {
6070 continue;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006071 }
Michal Vasko49fec8e2022-05-24 10:28:33 +02006072
6073 /* check for duplicates if they are possible */
6074 switch (axis) {
6075 case LYXP_AXIS_ANCESTOR:
6076 case LYXP_AXIS_ANCESTOR_OR_SELF:
6077 case LYXP_AXIS_DESCENDANT:
6078 case LYXP_AXIS_DESCENDANT_OR_SELF:
6079 case LYXP_AXIS_FOLLOWING:
6080 case LYXP_AXIS_FOLLOWING_SIBLING:
6081 case LYXP_AXIS_PARENT:
6082 case LYXP_AXIS_PRECEDING:
6083 case LYXP_AXIS_PRECEDING_SIBLING:
Michal Vasko306e2832022-07-25 09:15:17 +02006084 result.non_child_axis = 1;
Michal Vasko49fec8e2022-05-24 10:28:33 +02006085 if (set_dup_node_check(&result, iter, iter_type, -1)) {
6086 continue;
6087 }
6088 break;
6089 case LYXP_AXIS_CHILD:
6090 case LYXP_AXIS_SELF:
6091 break;
6092 case LYXP_AXIS_ATTRIBUTE:
6093 /* handled specially */
6094 assert(0);
6095 LOGINT(set->ctx);
6096 break;
6097 }
6098
6099 /* matching node */
6100 set_insert_node(&result, iter, 0, iter_type, result.used);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006101 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006102 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006103
Michal Vasko230cf972021-12-02 12:31:00 +01006104 /* move result to the set */
6105 lyxp_set_free_content(set);
6106 *set = result;
6107 result.type = LYXP_SET_NUMBER;
Michal Vasko49fec8e2022-05-24 10:28:33 +02006108
Michal Vasko306e2832022-07-25 09:15:17 +02006109 /* sort the final set if the document order could have been broken */
6110 if (set->non_child_axis) {
6111 set_sort(set);
6112 } else {
6113 assert(!set_sort(set));
6114 }
Michal Vasko230cf972021-12-02 12:31:00 +01006115
6116cleanup:
6117 lyxp_set_free_content(&result);
6118 return rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006119}
6120
6121/**
Michal Vasko49fec8e2022-05-24 10:28:33 +02006122 * @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 +02006123 *
6124 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02006125 * @param[in] scnode Matching node schema.
Michal Vasko004d3152020-06-11 19:59:22 +02006126 * @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 +01006127 * @param[in] options XPath options.
Michal Vaskod3678892020-05-21 10:06:58 +02006128 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6129 */
6130static LY_ERR
Michal Vasko49fec8e2022-05-24 10:28:33 +02006131moveto_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 +01006132 uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02006133{
Michal Vaskoddd76592022-01-17 13:34:48 +01006134 LY_ERR ret = LY_SUCCESS, r;
Michal Vaskod3678892020-05-21 10:06:58 +02006135 uint32_t i;
Michal Vaskod3678892020-05-21 10:06:58 +02006136 const struct lyd_node *siblings;
Michal Vasko230cf972021-12-02 12:31:00 +01006137 struct lyxp_set result;
Michal Vasko004d3152020-06-11 19:59:22 +02006138 struct lyd_node *sub, *inst = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02006139
Michal Vasko004d3152020-06-11 19:59:22 +02006140 assert(scnode && (!(scnode->nodetype & (LYS_LIST | LYS_LEAFLIST)) || predicates));
Michal Vaskod3678892020-05-21 10:06:58 +02006141
Michal Vasko50aaa072021-12-02 13:11:56 +01006142 /* init result set */
6143 set_init(&result, set);
6144
aPiecek8b0cc152021-05-31 16:40:31 +02006145 if (options & LYXP_SKIP_EXPR) {
Michal Vasko004d3152020-06-11 19:59:22 +02006146 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02006147 }
6148
6149 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006150 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko004d3152020-06-11 19:59:22 +02006151 ret = LY_EVALID;
6152 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02006153 }
6154
6155 /* context check for all the nodes since we have the schema node */
6156 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) {
6157 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02006158 goto cleanup;
Michal Vasko69730152020-10-09 16:30:07 +02006159 } else if (set->context_op && (scnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) &&
6160 (scnode != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02006161 lyxp_set_free_content(set);
6162 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +02006163 }
6164
6165 /* create specific data instance if needed */
6166 if (scnode->nodetype == LYS_LIST) {
Michal Vasko90189962023-02-28 12:10:34 +01006167 LY_CHECK_GOTO(ret = lyd_create_list(scnode, predicates, NULL, &inst), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02006168 } else if (scnode->nodetype == LYS_LEAFLIST) {
6169 LY_CHECK_GOTO(ret = lyd_create_term2(scnode, &predicates[0].value, &inst), cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02006170 }
6171
Michal Vasko230cf972021-12-02 12:31:00 +01006172 for (i = 0; i < set->used; ++i) {
Michal Vaskod3678892020-05-21 10:06:58 +02006173 siblings = NULL;
6174
6175 if ((set->val.nodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.nodes[i].type == LYXP_NODE_ROOT)) {
6176 assert(!set->val.nodes[i].node);
6177
6178 /* search in all the trees */
6179 siblings = set->tree;
6180 } else if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
6181 /* search in children */
Radek Krejcia1c1e542020-09-29 16:06:52 +02006182 siblings = lyd_child(set->val.nodes[i].node);
Michal Vaskod3678892020-05-21 10:06:58 +02006183 }
6184
6185 /* find the node using hashes */
Michal Vasko004d3152020-06-11 19:59:22 +02006186 if (inst) {
Michal Vaskoddd76592022-01-17 13:34:48 +01006187 r = lyd_find_sibling_first(siblings, inst, &sub);
Michal Vaskod3678892020-05-21 10:06:58 +02006188 } else {
Michal Vaskoddd76592022-01-17 13:34:48 +01006189 r = lyd_find_sibling_val(siblings, scnode, NULL, 0, &sub);
Michal Vaskod3678892020-05-21 10:06:58 +02006190 }
Michal Vaskoddd76592022-01-17 13:34:48 +01006191 LY_CHECK_ERR_GOTO(r && (r != LY_ENOTFOUND), ret = r, cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02006192
6193 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01006194 if (!(options & LYXP_IGNORE_WHEN) && sub && lysc_has_when(sub->schema) && !(sub->flags & LYD_WHEN_TRUE)) {
Michal Vasko004d3152020-06-11 19:59:22 +02006195 ret = LY_EINCOMPLETE;
6196 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02006197 }
6198
6199 if (sub) {
6200 /* pos filled later */
Michal Vasko230cf972021-12-02 12:31:00 +01006201 set_insert_node(&result, sub, 0, LYXP_NODE_ELEM, result.used);
Michal Vaskod3678892020-05-21 10:06:58 +02006202 }
6203 }
6204
Michal Vasko230cf972021-12-02 12:31:00 +01006205 /* move result to the set */
6206 lyxp_set_free_content(set);
6207 *set = result;
6208 result.type = LYXP_SET_NUMBER;
6209 assert(!set_sort(set));
6210
Michal Vasko004d3152020-06-11 19:59:22 +02006211cleanup:
Michal Vasko230cf972021-12-02 12:31:00 +01006212 lyxp_set_free_content(&result);
Michal Vasko004d3152020-06-11 19:59:22 +02006213 lyd_free_tree(inst);
6214 return ret;
Michal Vaskod3678892020-05-21 10:06:58 +02006215}
6216
6217/**
Michal Vasko49fec8e2022-05-24 10:28:33 +02006218 * @brief Check @p node as a part of schema NameTest processing.
6219 *
6220 * @param[in] node Schema node to check.
6221 * @param[in] ctx_scnode Context node.
6222 * @param[in] set Set to read general context from.
6223 * @param[in] node_name Node name in the dictionary to move to, NULL for any nodes.
6224 * @param[in] moveto_mod Expected module of the node, NULL for no prefix.
6225 * @return LY_ERR (LY_ENOT if node does not match, LY_EINVAL if neither node nor any children match)
6226 */
6227static LY_ERR
6228moveto_scnode_check(const struct lysc_node *node, const struct lysc_node *ctx_scnode, const struct lyxp_set *set,
6229 const char *node_name, const struct lys_module *moveto_mod)
6230{
6231 if (!moveto_mod && node_name) {
6232 switch (set->format) {
6233 case LY_VALUE_SCHEMA:
6234 case LY_VALUE_SCHEMA_RESOLVED:
6235 /* use current module */
6236 moveto_mod = set->cur_mod;
6237 break;
6238 case LY_VALUE_JSON:
6239 case LY_VALUE_LYB:
6240 case LY_VALUE_STR_NS:
6241 /* inherit module of the context node, if any */
6242 if (ctx_scnode) {
6243 moveto_mod = ctx_scnode->module;
6244 }
6245 break;
6246 case LY_VALUE_CANON:
6247 case LY_VALUE_XML:
6248 /* not defined */
6249 LOGINT(set->ctx);
6250 return LY_EINVAL;
6251 }
6252 }
6253
6254 if (!node) {
6255 /* root will not match a specific node */
6256 if (node_name || moveto_mod) {
6257 return LY_ENOT;
6258 }
6259 return LY_SUCCESS;
6260 }
6261
6262 /* module check */
6263 if (moveto_mod && (node->module != moveto_mod)) {
6264 return LY_ENOT;
6265 }
6266
6267 /* context check */
6268 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (node->flags & LYS_CONFIG_R)) {
6269 return LY_EINVAL;
6270 } else if (set->context_op && (node->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && (node != set->context_op)) {
6271 return LY_EINVAL;
6272 }
6273
6274 /* name check */
6275 if (node_name && (node->name != node_name)) {
6276 return LY_ENOT;
6277 }
6278
6279 /* match */
6280 return LY_SUCCESS;
6281}
6282
6283/**
6284 * @brief Get the next node in a forward schema node DFS.
6285 *
6286 * @param[in] iter Last returned node.
6287 * @param[in] stop Node to stop the search on and not return.
6288 * @param[in] getnext_opts Options for ::lys_getnext().
6289 * @return Next node, NULL if there are no more.
6290 */
6291static const struct lysc_node *
6292moveto_axis_scnode_next_dfs_forward(const struct lysc_node *iter, const struct lysc_node *stop, uint32_t getnext_opts)
6293{
6294 const struct lysc_node *next = NULL;
6295
6296 next = lysc_node_child(iter);
6297 if (!next) {
6298 /* no children, try siblings */
Michal Vasko34a22fe2022-06-15 07:58:55 +02006299 if ((iter == stop) || !lysc_data_parent(iter)) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02006300 /* we are done, no next element to process */
6301 return NULL;
6302 }
6303
6304 next = lys_getnext(iter, lysc_data_parent(iter), NULL, getnext_opts);
6305 }
6306 while (!next && iter) {
6307 /* parent is already processed, go to its sibling */
6308 iter = iter->parent;
Michal Vasko34a22fe2022-06-15 07:58:55 +02006309 if ((iter == stop) || !lysc_data_parent(iter)) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02006310 /* we are done, no next element to process */
6311 return NULL;
6312 }
6313 next = lys_getnext(iter, lysc_data_parent(iter), NULL, getnext_opts);
6314 }
6315
6316 return next;
6317}
6318
6319/**
6320 * @brief Consider schema node based on its in_ctx enum value.
6321 *
6322 * @param[in,out] in_ctx In_ctx enum of the schema node, may be updated.
6323 * @param[in] axis Axis to use.
6324 * @return LY_SUCCESS on success.
6325 * @return LY_ENOT if the node should not be returned.
6326 */
6327static LY_ERR
6328moveto_axis_scnode_next_in_ctx(int32_t *in_ctx, enum lyxp_axis axis)
6329{
6330 switch (axis) {
6331 case LYXP_AXIS_SELF:
6332 if ((*in_ctx == LYXP_SET_SCNODE_START) || (*in_ctx == LYXP_SET_SCNODE_ATOM_CTX)) {
6333 /* additionally put the start node into context */
6334 *in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
6335 return LY_SUCCESS;
6336 }
6337 break;
6338 case LYXP_AXIS_PARENT:
6339 case LYXP_AXIS_ANCESTOR_OR_SELF:
6340 case LYXP_AXIS_ANCESTOR:
6341 case LYXP_AXIS_DESCENDANT_OR_SELF:
6342 case LYXP_AXIS_DESCENDANT:
6343 case LYXP_AXIS_FOLLOWING:
6344 case LYXP_AXIS_FOLLOWING_SIBLING:
6345 case LYXP_AXIS_PRECEDING:
6346 case LYXP_AXIS_PRECEDING_SIBLING:
6347 case LYXP_AXIS_CHILD:
6348 if (*in_ctx == LYXP_SET_SCNODE_START) {
6349 /* remember that context node was used */
6350 *in_ctx = LYXP_SET_SCNODE_START_USED;
6351 return LY_SUCCESS;
6352 } else if (*in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
6353 /* traversed */
6354 *in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
6355 return LY_SUCCESS;
6356 }
6357 break;
6358 case LYXP_AXIS_ATTRIBUTE:
6359 /* unreachable */
6360 assert(0);
6361 LOGINT(NULL);
6362 break;
6363 }
6364
6365 return LY_ENOT;
6366}
6367
6368/**
6369 * @brief Get previous sibling for a schema node.
6370 *
6371 * @param[in] scnode Schema node.
6372 * @param[in] getnext_opts Options for ::lys_getnext().
6373 * @return Previous sibling, NULL if none.
6374 */
6375static const struct lysc_node *
6376moveto_axis_scnode_preceding_sibling(const struct lysc_node *scnode, uint32_t getnext_opts)
6377{
6378 const struct lysc_node *next = NULL, *prev = NULL;
6379
6380 while ((next = lys_getnext(next, lysc_data_parent(scnode), scnode->module->compiled, getnext_opts))) {
6381 if (next == scnode) {
6382 break;
6383 }
6384
6385 prev = next;
6386 }
6387
6388 return prev;
6389}
6390
6391/**
6392 * @brief Get the first schema node on an axis for a context node.
6393 *
6394 * @param[in,out] iter Last returned node, start with NULL, updated to the next node.
6395 * @param[in,out] iter_type Node type of @p iter, start with 0, updated to the node type of the next node.
6396 * @param[in,out] iter_mod Internal module iterator, do not change.
6397 * @param[in,out] iter_mod_idx Internal module index iterator, do not change.
6398 * @param[in] scnode Context node.
6399 * @param[in] node_type Type of @p scnode.
6400 * @param[in] in_ctx In_ctx enum of @p scnode.
6401 * @param[in] axis Axis to use.
6402 * @param[in] set XPath set with the general context.
6403 * @param[in] getnext_opts Options for ::lys_getnext().
6404 * @return LY_SUCCESS on success.
6405 * @return LY_ENOTFOUND if no next node found.
6406 */
6407static LY_ERR
6408moveto_axis_scnode_next_first(const struct lysc_node **iter, enum lyxp_node_type *iter_type, const struct lys_module **iter_mod,
6409 uint32_t *iter_mod_idx, const struct lysc_node *scnode, enum lyxp_node_type node_type, enum lyxp_axis axis,
6410 struct lyxp_set *set, uint32_t getnext_opts)
6411{
6412 const struct lysc_node *next = NULL;
6413 enum lyxp_node_type next_type = 0;
6414
6415 assert(!*iter);
6416 assert(!*iter_type);
6417
6418 *iter_mod = NULL;
6419 *iter_mod_idx = 0;
6420
6421 switch (axis) {
6422 case LYXP_AXIS_ANCESTOR_OR_SELF:
6423 case LYXP_AXIS_DESCENDANT_OR_SELF:
6424 case LYXP_AXIS_SELF:
6425 if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT) || (node_type == LYXP_NODE_ELEM)) {
6426 /* just return the node */
6427 next = scnode;
6428 next_type = node_type;
6429 }
6430 break;
6431
6432 case LYXP_AXIS_ANCESTOR:
6433 case LYXP_AXIS_PARENT:
6434 if (node_type == LYXP_NODE_ELEM) {
6435 next = lysc_data_parent(scnode);
6436 next_type = next ? LYXP_NODE_ELEM : set->root_type;
6437 } /* else no parent */
6438 break;
6439
6440 case LYXP_AXIS_DESCENDANT:
6441 case LYXP_AXIS_CHILD:
6442 if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT)) {
6443 /* it can actually be in any module, it's all <running>, and even if it's moveto_mod (if set),
6444 * it can be in a top-level augment */
6445 while ((*iter_mod = ly_ctx_get_module_iter(set->ctx, iter_mod_idx))) {
6446 /* module may not be implemented or not compiled yet */
6447 if (!(*iter_mod)->compiled) {
6448 continue;
6449 }
6450
6451 /* get next node */
6452 if ((next = lys_getnext(NULL, NULL, (*iter_mod)->compiled, getnext_opts))) {
6453 next_type = LYXP_NODE_ELEM;
6454 break;
6455 }
6456 }
6457 } else if (node_type == LYXP_NODE_ELEM) {
6458 /* get next node */
6459 next = lys_getnext(NULL, scnode, NULL, getnext_opts);
6460 next_type = next ? LYXP_NODE_ELEM : 0;
6461 }
6462 break;
6463
6464 case LYXP_AXIS_FOLLOWING:
6465 case LYXP_AXIS_FOLLOWING_SIBLING:
6466 if (node_type == LYXP_NODE_ELEM) {
6467 /* first next sibling */
6468 next = lys_getnext(scnode, lysc_data_parent(scnode), scnode->module->compiled, getnext_opts);
6469 next_type = next ? LYXP_NODE_ELEM : 0;
6470 } /* else no sibling */
6471 break;
6472
6473 case LYXP_AXIS_PRECEDING:
6474 case LYXP_AXIS_PRECEDING_SIBLING:
6475 if (node_type == LYXP_NODE_ELEM) {
6476 /* first parent sibling */
6477 next = lys_getnext(NULL, lysc_data_parent(scnode), scnode->module->compiled, getnext_opts);
6478 if (next == scnode) {
6479 /* no preceding sibling */
6480 next = NULL;
6481 }
6482 next_type = next ? LYXP_NODE_ELEM : 0;
6483 } /* else no sibling */
6484 break;
6485
6486 case LYXP_AXIS_ATTRIBUTE:
6487 /* unreachable */
6488 assert(0);
6489 LOGINT(set->ctx);
6490 break;
6491 }
6492
6493 *iter = next;
6494 *iter_type = next_type;
6495 return next_type ? LY_SUCCESS : LY_ENOTFOUND;
6496}
6497
6498/**
6499 * @brief Iterate over all schema nodes on an axis for a context node.
6500 *
6501 * @param[in,out] iter Last returned node, start with NULL, updated to the next node.
6502 * @param[in,out] iter_type Node type of @p iter, start with 0, updated to the node type of the next node.
6503 * @param[in,out] iter_mod Internal module iterator, do not change.
6504 * @param[in,out] iter_mod_idx Internal module index iterator, do not change.
6505 * @param[in] scnode Context node.
6506 * @param[in] node_type Type of @p scnode.
6507 * @param[in] axis Axis to use.
6508 * @param[in] set XPath set with the general context.
6509 * @param[in] getnext_opts Options for ::lys_getnext().
6510 * @return LY_SUCCESS on success.
6511 * @return LY_ENOTFOUND if no next node found.
6512 */
6513static LY_ERR
6514moveto_axis_scnode_next(const struct lysc_node **iter, enum lyxp_node_type *iter_type, const struct lys_module **iter_mod,
6515 uint32_t *iter_mod_idx, const struct lysc_node *scnode, enum lyxp_node_type node_type, enum lyxp_axis axis,
6516 struct lyxp_set *set, uint32_t getnext_opts)
6517{
6518 const struct lysc_node *next = NULL, *dfs_stop;
6519 enum lyxp_node_type next_type = 0;
6520
6521 if (!*iter_type) {
6522 /* first returned node */
6523 return moveto_axis_scnode_next_first(iter, iter_type, iter_mod, iter_mod_idx, scnode, node_type, axis, set,
6524 getnext_opts);
6525 }
6526
6527 switch (axis) {
6528 case LYXP_AXIS_PARENT:
6529 case LYXP_AXIS_SELF:
6530 /* parent/self was returned before */
6531 break;
6532
6533 case LYXP_AXIS_ANCESTOR_OR_SELF:
6534 if ((*iter == scnode) && (*iter_type == node_type)) {
6535 /* fake first ancestor, we returned self before */
6536 *iter = NULL;
6537 *iter_type = 0;
6538 return moveto_axis_scnode_next_first(iter, iter_type, iter_mod, iter_mod_idx, scnode, node_type,
6539 LYXP_AXIS_ANCESTOR, set, getnext_opts);
6540 } /* else continue ancestor */
6541
6542 /* fallthrough */
6543 case LYXP_AXIS_ANCESTOR:
6544 if (*iter_type == LYXP_NODE_ELEM) {
6545 next = lysc_data_parent(*iter);
6546 next_type = next ? LYXP_NODE_ELEM : set->root_type;
6547 } /* else no ancestor */
6548 break;
6549
6550 case LYXP_AXIS_DESCENDANT_OR_SELF:
6551 if ((*iter == scnode) && (*iter_type == node_type)) {
6552 /* fake first descendant, we returned self before */
6553 *iter = NULL;
6554 *iter_type = 0;
6555 return moveto_axis_scnode_next_first(iter, iter_type, iter_mod, iter_mod_idx, scnode, node_type,
6556 LYXP_AXIS_DESCENDANT, set, getnext_opts);
6557 } /* else DFS until context node */
6558 dfs_stop = scnode;
6559
6560 /* fallthrough */
6561 case LYXP_AXIS_DESCENDANT:
6562 if (axis == LYXP_AXIS_DESCENDANT) {
6563 /* DFS until the context node */
6564 dfs_stop = scnode;
6565 }
6566
6567 /* fallthrough */
6568 case LYXP_AXIS_PRECEDING:
6569 if (axis == LYXP_AXIS_PRECEDING) {
6570 /* DFS until the previous sibling */
6571 dfs_stop = moveto_axis_scnode_preceding_sibling(scnode, getnext_opts);
6572 assert(dfs_stop);
6573
6574 if (*iter == dfs_stop) {
6575 /* we are done */
6576 break;
6577 }
6578 }
6579
6580 /* fallthrough */
6581 case LYXP_AXIS_FOLLOWING:
6582 if (axis == LYXP_AXIS_FOLLOWING) {
6583 /* DFS through the whole module */
6584 dfs_stop = NULL;
6585 }
6586
6587 /* nested nodes */
6588 assert(*iter);
6589 next = moveto_axis_scnode_next_dfs_forward(*iter, dfs_stop, getnext_opts);
6590 if (next) {
6591 next_type = LYXP_NODE_ELEM;
6592 break;
6593 } /* else get next top-level node just like a child */
6594
6595 /* fallthrough */
6596 case LYXP_AXIS_CHILD:
6597 case LYXP_AXIS_FOLLOWING_SIBLING:
6598 if (!*iter_mod) {
6599 /* nodes from a single module */
6600 if ((next = lys_getnext(*iter, lysc_data_parent(*iter), (*iter)->module->compiled, getnext_opts))) {
6601 next_type = LYXP_NODE_ELEM;
6602 break;
6603 }
6604
6605 assert(scnode);
Michal Vaskoa353cce2022-11-14 10:09:55 +01006606 if ((axis != LYXP_AXIS_CHILD) && !lysc_data_parent(scnode)) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02006607 /* iterating over top-level nodes, find next */
6608 while (lysc_data_parent(*iter)) {
6609 *iter = lysc_data_parent(*iter);
6610 }
6611 if ((next = lys_getnext(*iter, NULL, (*iter)->module->compiled, getnext_opts))) {
6612 next_type = LYXP_NODE_ELEM;
6613 break;
6614 }
6615 }
6616 }
6617
6618 while (*iter_mod) {
6619 /* module top-level nodes */
6620 if ((next = lys_getnext(*iter, NULL, (*iter_mod)->compiled, getnext_opts))) {
6621 next_type = LYXP_NODE_ELEM;
6622 break;
6623 }
6624
6625 /* get next module */
6626 while ((*iter_mod = ly_ctx_get_module_iter(set->ctx, iter_mod_idx))) {
6627 /* module may not be implemented or not compiled yet */
6628 if ((*iter_mod)->compiled) {
6629 break;
6630 }
6631 }
6632
6633 /* new module, start over */
6634 *iter = NULL;
6635 }
6636 break;
6637
6638 case LYXP_AXIS_PRECEDING_SIBLING:
6639 assert(*iter);
6640
6641 /* next parent sibling until scnode */
6642 next = lys_getnext(*iter, lysc_data_parent(*iter), (*iter)->module->compiled, getnext_opts);
6643 if (next == scnode) {
6644 /* no previous sibling */
6645 next = NULL;
6646 }
6647 next_type = next ? LYXP_NODE_ELEM : 0;
6648 break;
6649
6650 case LYXP_AXIS_ATTRIBUTE:
6651 /* unreachable */
6652 assert(0);
6653 LOGINT(set->ctx);
6654 break;
6655 }
6656
6657 *iter = next;
6658 *iter_type = next_type;
6659 return next_type ? LY_SUCCESS : LY_ENOTFOUND;
6660}
6661
6662/**
Michal Vaskod3678892020-05-21 10:06:58 +02006663 * @brief Move context @p set to a schema node. Result is LYXP_SET_SCNODE_SET (or LYXP_SET_EMPTY).
6664 *
6665 * @param[in,out] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006666 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02006667 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko49fec8e2022-05-24 10:28:33 +02006668 * @param[in] axis Axis to search on.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006669 * @param[in] options XPath options.
6670 * @return LY_ERR
6671 */
6672static LY_ERR
Michal Vasko49fec8e2022-05-24 10:28:33 +02006673moveto_scnode(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, enum lyxp_axis axis,
6674 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006675{
Radek Krejci857189e2020-09-01 13:26:36 +02006676 ly_bool temp_ctx = 0;
Michal Vasko49fec8e2022-05-24 10:28:33 +02006677 uint32_t getnext_opts, orig_used, i, mod_idx, idx;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006678 const struct lys_module *mod;
Michal Vasko49fec8e2022-05-24 10:28:33 +02006679 const struct lysc_node *iter;
6680 enum lyxp_node_type iter_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006681
aPiecek8b0cc152021-05-31 16:40:31 +02006682 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006683 return LY_SUCCESS;
6684 }
6685
6686 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006687 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006688 return LY_EVALID;
6689 }
6690
Michal Vaskocafad9d2019-11-07 15:20:03 +01006691 /* getnext opts */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01006692 getnext_opts = 0;
Michal Vaskocafad9d2019-11-07 15:20:03 +01006693 if (options & LYXP_SCNODE_OUTPUT) {
6694 getnext_opts |= LYS_GETNEXT_OUTPUT;
6695 }
Michal Vaskoe482bb92023-02-01 11:41:41 +01006696 if (options & LYXP_SCNODE_SCHEMAMOUNT) {
6697 getnext_opts |= LYS_GETNEXT_WITHSCHEMAMOUNT;
6698 }
Michal Vaskocafad9d2019-11-07 15:20:03 +01006699
Michal Vasko03ff5a72019-09-11 13:49:33 +02006700 orig_used = set->used;
6701 for (i = 0; i < orig_used; ++i) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02006702 /* update in_ctx first */
6703 if (moveto_axis_scnode_next_in_ctx(&set->val.scnodes[i].in_ctx, axis)) {
6704 /* not usable, skip */
6705 continue;
6706 }
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006707
Michal Vasko49fec8e2022-05-24 10:28:33 +02006708 iter = NULL;
6709 iter_type = 0;
6710 while (!moveto_axis_scnode_next(&iter, &iter_type, &mod, &mod_idx, set->val.scnodes[i].scnode,
6711 set->val.scnodes[i].type, axis, set, getnext_opts)) {
6712 if (moveto_scnode_check(iter, NULL, set, ncname, moveto_mod)) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006713 continue;
6714 }
6715
Michal Vasko49fec8e2022-05-24 10:28:33 +02006716 /* insert */
Michal Vasko7333cb32022-07-29 16:30:29 +02006717 LY_CHECK_RET(set_scnode_insert_node(set, iter, iter_type, axis, &idx));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006718
Michal Vasko49fec8e2022-05-24 10:28:33 +02006719 /* we need to prevent these nodes from being considered in this moveto */
6720 if ((idx < orig_used) && (idx > i)) {
6721 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX;
6722 temp_ctx = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006723 }
6724 }
Michal Vasko66330fc2022-11-21 15:52:24 +01006725
6726 if (moveto_mod && ncname && ((axis == LYXP_AXIS_DESCENDANT) || (axis == LYXP_AXIS_CHILD)) &&
6727 (set->val.scnodes[i].type == LYXP_NODE_ELEM) && !ly_nested_ext_schema(NULL, set->val.scnodes[i].scnode,
6728 moveto_mod->name, strlen(moveto_mod->name), LY_VALUE_JSON, NULL, ncname, strlen(ncname), &iter, NULL)) {
6729 /* there is a matching node from an extension, use it */
6730 LY_CHECK_RET(set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, axis, &idx));
6731 if ((idx < orig_used) && (idx > i)) {
6732 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX;
6733 temp_ctx = 1;
6734 }
6735 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006736 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006737
6738 /* correct temporary in_ctx values */
6739 if (temp_ctx) {
6740 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006741 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_NEW_CTX) {
6742 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006743 }
6744 }
6745 }
6746
6747 return LY_SUCCESS;
6748}
6749
6750/**
Michal Vasko49fec8e2022-05-24 10:28:33 +02006751 * @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 +02006752 * Context position aware.
6753 *
6754 * @param[in] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006755 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02006756 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vaskocdad7122020-11-09 21:04:44 +01006757 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006758 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6759 */
6760static LY_ERR
Michal Vasko49fec8e2022-05-24 10:28:33 +02006761moveto_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 +02006762{
6763 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006764 const struct lyd_node *next, *elem, *start;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006765 struct lyxp_set ret_set;
6766 LY_ERR rc;
6767
aPiecek8b0cc152021-05-31 16:40:31 +02006768 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006769 return LY_SUCCESS;
6770 }
6771
6772 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006773 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006774 return LY_EVALID;
6775 }
6776
Michal Vasko9f96a052020-03-10 09:41:45 +01006777 /* 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 +02006778 rc = xpath_pi_node(set, LYXP_AXIS_CHILD, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006779 LY_CHECK_RET(rc);
6780
Michal Vasko6346ece2019-09-24 13:12:53 +02006781 /* this loop traverses all the nodes in the set and adds/keeps only those that match qname */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006782 set_init(&ret_set, set);
6783 for (i = 0; i < set->used; ++i) {
6784
6785 /* TREE DFS */
6786 start = set->val.nodes[i].node;
6787 for (elem = next = start; elem; elem = next) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02006788 rc = moveto_node_check(elem, LYXP_NODE_ELEM, set, ncname, moveto_mod, options);
Michal Vasko6346ece2019-09-24 13:12:53 +02006789 if (!rc) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006790 /* add matching node into result set */
6791 set_insert_node(&ret_set, elem, 0, LYXP_NODE_ELEM, ret_set.used);
6792 if (set_dup_node_check(set, elem, LYXP_NODE_ELEM, i)) {
6793 /* the node is a duplicate, we'll process it later in the set */
6794 goto skip_children;
6795 }
Michal Vasko6346ece2019-09-24 13:12:53 +02006796 } else if (rc == LY_EINCOMPLETE) {
Michal Vasko6346ece2019-09-24 13:12:53 +02006797 return rc;
6798 } else if (rc == LY_EINVAL) {
6799 goto skip_children;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006800 }
6801
6802 /* TREE DFS NEXT ELEM */
6803 /* select element for the next run - children first */
Radek Krejcia1c1e542020-09-29 16:06:52 +02006804 next = lyd_child(elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006805 if (!next) {
6806skip_children:
6807 /* no children, so try siblings, but only if it's not the start,
6808 * that is considered to be the root and it's siblings are not traversed */
6809 if (elem != start) {
6810 next = elem->next;
6811 } else {
6812 break;
6813 }
6814 }
6815 while (!next) {
6816 /* no siblings, go back through the parents */
Michal Vasko9e685082021-01-29 14:49:09 +01006817 if (lyd_parent(elem) == start) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006818 /* we are done, no next element to process */
6819 break;
6820 }
6821 /* parent is already processed, go to its sibling */
Michal Vasko9e685082021-01-29 14:49:09 +01006822 elem = lyd_parent(elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006823 next = elem->next;
6824 }
6825 }
6826 }
6827
6828 /* make the temporary set the current one */
6829 ret_set.ctx_pos = set->ctx_pos;
6830 ret_set.ctx_size = set->ctx_size;
Michal Vaskod3678892020-05-21 10:06:58 +02006831 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006832 memcpy(set, &ret_set, sizeof *set);
Michal Vasko306e2832022-07-25 09:15:17 +02006833 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006834
6835 return LY_SUCCESS;
6836}
6837
6838/**
Michal Vasko49fec8e2022-05-24 10:28:33 +02006839 * @brief Move context @p set to a child schema node and all its descendants starting from a node.
6840 * Result is LYXP_SET_NODE_SET.
6841 *
6842 * @param[in] set Set to use.
6843 * @param[in] start Start node whose subtree to add.
6844 * @param[in] start_idx Index of @p start in @p set.
6845 * @param[in] moveto_mod Matching node module, NULL for no prefix.
6846 * @param[in] ncname Matching node name in the dictionary, NULL for any.
6847 * @param[in] options XPath options.
6848 * @return LY_ERR value.
6849 */
6850static LY_ERR
6851moveto_scnode_dfs(struct lyxp_set *set, const struct lysc_node *start, uint32_t start_idx,
6852 const struct lys_module *moveto_mod, const char *ncname, uint32_t options)
6853{
6854 const struct lysc_node *next, *elem;
6855 uint32_t idx;
6856 LY_ERR rc;
6857
6858 /* TREE DFS */
6859 for (elem = next = start; elem; elem = next) {
6860 if ((elem == start) || (elem->nodetype & (LYS_CHOICE | LYS_CASE))) {
6861 /* schema-only nodes, skip root */
6862 goto next_iter;
6863 }
6864
6865 rc = moveto_scnode_check(elem, start, set, ncname, moveto_mod);
6866 if (!rc) {
6867 if (lyxp_set_scnode_contains(set, elem, LYXP_NODE_ELEM, start_idx, &idx)) {
6868 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
6869 if (idx > start_idx) {
6870 /* we will process it later in the set */
6871 goto skip_children;
6872 }
6873 } else {
Michal Vasko7333cb32022-07-29 16:30:29 +02006874 LY_CHECK_RET(set_scnode_insert_node(set, elem, LYXP_NODE_ELEM, LYXP_AXIS_DESCENDANT, NULL));
Michal Vasko49fec8e2022-05-24 10:28:33 +02006875 }
6876 } else if (rc == LY_EINVAL) {
6877 goto skip_children;
6878 }
6879
6880next_iter:
6881 /* TREE DFS NEXT ELEM */
6882 /* select element for the next run - children first */
6883 next = lysc_node_child(elem);
6884 if (next && (next->nodetype == LYS_INPUT) && (options & LYXP_SCNODE_OUTPUT)) {
6885 next = next->next;
6886 } else if (next && (next->nodetype == LYS_OUTPUT) && !(options & LYXP_SCNODE_OUTPUT)) {
6887 next = next->next;
6888 }
6889 if (!next) {
6890skip_children:
6891 /* no children, so try siblings, but only if it's not the start,
6892 * that is considered to be the root and it's siblings are not traversed */
6893 if (elem != start) {
6894 next = elem->next;
6895 } else {
6896 break;
6897 }
6898 }
6899 while (!next) {
6900 /* no siblings, go back through the parents */
6901 if (elem->parent == start) {
6902 /* we are done, no next element to process */
6903 break;
6904 }
6905 /* parent is already processed, go to its sibling */
6906 elem = elem->parent;
6907 next = elem->next;
6908 }
6909 }
6910
6911 return LY_SUCCESS;
6912}
6913
6914/**
6915 * @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 +02006916 *
6917 * @param[in] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006918 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02006919 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006920 * @param[in] options XPath options.
Michal Vasko49fec8e2022-05-24 10:28:33 +02006921 * @return LY_ERR value.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006922 */
6923static LY_ERR
Michal Vasko49fec8e2022-05-24 10:28:33 +02006924moveto_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 +02006925{
Michal Vasko49fec8e2022-05-24 10:28:33 +02006926 uint32_t i, orig_used, mod_idx;
6927 const struct lys_module *mod;
6928 const struct lysc_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006929
aPiecek8b0cc152021-05-31 16:40:31 +02006930 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006931 return LY_SUCCESS;
6932 }
6933
6934 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006935 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006936 return LY_EVALID;
6937 }
6938
Michal Vasko03ff5a72019-09-11 13:49:33 +02006939 orig_used = set->used;
6940 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006941 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
6942 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006943 continue;
6944 }
6945
6946 /* remember context node */
Radek Krejcif13b87b2020-12-01 22:02:17 +01006947 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vaskoec4df482019-12-16 10:02:18 +01006948 } else {
Michal Vasko1a09b212021-05-06 13:00:10 +02006949 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006950 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006951
Michal Vasko49fec8e2022-05-24 10:28:33 +02006952 if ((set->val.scnodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.scnodes[i].type == LYXP_NODE_ROOT)) {
6953 /* traverse all top-level nodes in all the modules */
6954 mod_idx = 0;
6955 while ((mod = ly_ctx_get_module_iter(set->ctx, &mod_idx))) {
6956 /* module may not be implemented or not compiled yet */
6957 if (!mod->compiled) {
6958 continue;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006959 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006960
Michal Vasko49fec8e2022-05-24 10:28:33 +02006961 root = NULL;
6962 /* no getnext opts needed */
6963 while ((root = lys_getnext(root, NULL, mod->compiled, 0))) {
6964 LY_CHECK_RET(moveto_scnode_dfs(set, root, i, moveto_mod, ncname, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006965 }
6966 }
Michal Vasko49fec8e2022-05-24 10:28:33 +02006967
6968 } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
6969 /* add all the descendants recursively */
6970 LY_CHECK_RET(moveto_scnode_dfs(set, set->val.scnodes[i].scnode, i, moveto_mod, ncname, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006971 }
6972 }
6973
6974 return LY_SUCCESS;
6975}
6976
6977/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02006978 * @brief Move context @p set to an attribute. Result is LYXP_SET_NODE_SET.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006979 * Indirectly context position aware.
6980 *
6981 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02006982 * @param[in] mod Matching metadata module, NULL for any.
6983 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
aPiecek8b0cc152021-05-31 16:40:31 +02006984 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006985 * @return LY_ERR
6986 */
6987static LY_ERR
aPiecek8b0cc152021-05-31 16:40:31 +02006988moveto_attr(struct lyxp_set *set, const struct lys_module *mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006989{
Michal Vasko9f96a052020-03-10 09:41:45 +01006990 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006991
aPiecek8b0cc152021-05-31 16:40:31 +02006992 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006993 return LY_SUCCESS;
6994 }
6995
6996 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006997 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006998 return LY_EVALID;
6999 }
7000
Radek Krejci1deb5be2020-08-26 16:43:36 +02007001 for (uint32_t i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02007002 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007003
7004 /* only attributes of an elem (not dummy) can be in the result, skip all the rest;
7005 * our attributes are always qualified */
Michal Vasko5c4e5892019-11-14 12:31:38 +01007006 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01007007 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007008
7009 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02007010 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007011 continue;
7012 }
7013
Michal Vaskod3678892020-05-21 10:06:58 +02007014 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007015 /* match */
7016 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01007017 set->val.meta[i].meta = sub;
7018 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007019 /* pos does not change */
7020 replaced = 1;
7021 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01007022 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 +02007023 }
7024 ++i;
7025 }
7026 }
7027 }
7028
7029 if (!replaced) {
7030 /* no match */
7031 set_remove_node(set, i);
7032 }
7033 }
7034
7035 return LY_SUCCESS;
7036}
7037
7038/**
7039 * @brief Move context @p set1 to union with @p set2. @p set2 is emptied afterwards.
Michal Vasko61ac2f62020-05-25 12:39:51 +02007040 * Result is LYXP_SET_NODE_SET. Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007041 *
7042 * @param[in,out] set1 Set to use for the result.
7043 * @param[in] set2 Set that is copied to @p set1.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007044 * @return LY_ERR
7045 */
7046static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007047moveto_union(struct lyxp_set *set1, struct lyxp_set *set2)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007048{
7049 LY_ERR rc;
7050
Michal Vaskod3678892020-05-21 10:06:58 +02007051 if ((set1->type != LYXP_SET_NODE_SET) || (set2->type != LYXP_SET_NODE_SET)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01007052 LOGVAL(set1->ctx, LY_VCODE_XP_INOP_2, "union", print_set_type(set1), print_set_type(set2));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007053 return LY_EVALID;
7054 }
7055
7056 /* set2 is empty or both set1 and set2 */
Michal Vaskod3678892020-05-21 10:06:58 +02007057 if (!set2->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007058 return LY_SUCCESS;
7059 }
7060
Michal Vaskod3678892020-05-21 10:06:58 +02007061 if (!set1->used) {
aPiecekadc1e4f2021-10-07 11:15:12 +02007062 /* release hidden allocated data (lyxp_set.size) */
7063 lyxp_set_free_content(set1);
7064 /* direct copying of the entire structure */
Michal Vasko03ff5a72019-09-11 13:49:33 +02007065 memcpy(set1, set2, sizeof *set1);
7066 /* dynamic memory belongs to set1 now, do not free */
Michal Vaskod3678892020-05-21 10:06:58 +02007067 memset(set2, 0, sizeof *set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007068 return LY_SUCCESS;
7069 }
7070
7071 /* we assume sets are sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007072 assert(!set_sort(set1) && !set_sort(set2));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007073
7074 /* sort, remove duplicates */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007075 rc = set_sorted_merge(set1, set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007076 LY_CHECK_RET(rc);
7077
7078 /* final set must be sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007079 assert(!set_sort(set1));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007080
7081 return LY_SUCCESS;
7082}
7083
7084/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02007085 * @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 +02007086 * Context position aware.
7087 *
7088 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02007089 * @param[in] mod Matching metadata module, NULL for any.
7090 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
Michal Vaskocdad7122020-11-09 21:04:44 +01007091 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007092 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7093 */
7094static int
Michal Vaskocdad7122020-11-09 21:04:44 +01007095moveto_attr_alldesc(struct lyxp_set *set, const struct lys_module *mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007096{
Michal Vasko9f96a052020-03-10 09:41:45 +01007097 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007098 struct lyxp_set *set_all_desc = NULL;
7099 LY_ERR rc;
7100
aPiecek8b0cc152021-05-31 16:40:31 +02007101 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007102 return LY_SUCCESS;
7103 }
7104
7105 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01007106 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007107 return LY_EVALID;
7108 }
7109
Michal Vasko03ff5a72019-09-11 13:49:33 +02007110 /* can be optimized similarly to moveto_node_alldesc() and save considerable amount of memory,
7111 * but it likely won't be used much, so it's a waste of time */
7112 /* copy the context */
7113 set_all_desc = set_copy(set);
7114 /* get all descendant nodes (the original context nodes are removed) */
Michal Vasko49fec8e2022-05-24 10:28:33 +02007115 rc = moveto_node_alldesc_child(set_all_desc, NULL, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007116 if (rc != LY_SUCCESS) {
7117 lyxp_set_free(set_all_desc);
7118 return rc;
7119 }
7120 /* prepend the original context nodes */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007121 rc = moveto_union(set, set_all_desc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007122 if (rc != LY_SUCCESS) {
7123 lyxp_set_free(set_all_desc);
7124 return rc;
7125 }
7126 lyxp_set_free(set_all_desc);
7127
Radek Krejci1deb5be2020-08-26 16:43:36 +02007128 for (uint32_t i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02007129 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007130
7131 /* only attributes of an elem can be in the result, skip all the rest,
7132 * we have all attributes qualified in lyd tree */
7133 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01007134 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007135 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02007136 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007137 continue;
7138 }
7139
Michal Vaskod3678892020-05-21 10:06:58 +02007140 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007141 /* match */
7142 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01007143 set->val.meta[i].meta = sub;
7144 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007145 /* pos does not change */
7146 replaced = 1;
7147 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01007148 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 +02007149 }
7150 ++i;
7151 }
7152 }
7153 }
7154
7155 if (!replaced) {
7156 /* no match */
7157 set_remove_node(set, i);
7158 }
7159 }
7160
7161 return LY_SUCCESS;
7162}
7163
7164/**
Michal Vasko8abcecc2022-07-28 09:55:01 +02007165 * @brief Move context @p set1 single item to the result of a comparison.
7166 *
7167 * @param[in] set1 First set with the item to compare.
7168 * @param[in] idx1 Index of the item in @p set1.
7169 * @param[in] set2 Second set.
7170 * @param[in] op Comparison operator to process.
7171 * @param[in] switch_operands Whether to switch sets as operands; whether it is `set1 op set2` or `set2 op set1`.
7172 * @param[out] result Result of the comparison.
7173 * @return LY_ERR value.
7174 */
7175static LY_ERR
7176moveto_op_comp_item(const struct lyxp_set *set1, uint32_t idx1, struct lyxp_set *set2, const char *op,
7177 ly_bool switch_operands, ly_bool *result)
7178{
7179 struct lyxp_set tmp1 = {0};
7180 LY_ERR rc = LY_SUCCESS;
7181
7182 assert(set1->type == LYXP_SET_NODE_SET);
7183
7184 /* cast set1 */
7185 switch (set2->type) {
7186 case LYXP_SET_NUMBER:
7187 rc = set_comp_cast(&tmp1, set1, LYXP_SET_NUMBER, idx1);
7188 break;
7189 case LYXP_SET_BOOLEAN:
7190 rc = set_comp_cast(&tmp1, set1, LYXP_SET_BOOLEAN, idx1);
7191 break;
7192 default:
7193 rc = set_comp_cast(&tmp1, set1, LYXP_SET_STRING, idx1);
7194 break;
7195 }
7196 LY_CHECK_GOTO(rc, cleanup);
7197
7198 /* canonize set2 */
7199 LY_CHECK_GOTO(rc = set_comp_canonize(set2, &set1->val.nodes[idx1]), cleanup);
7200
7201 /* compare recursively and store the result */
7202 if (switch_operands) {
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007203 LY_CHECK_GOTO(rc = moveto_op_comp(set2, &tmp1, op, result), cleanup);
Michal Vasko8abcecc2022-07-28 09:55:01 +02007204 } else {
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007205 LY_CHECK_GOTO(rc = moveto_op_comp(&tmp1, set2, op, result), cleanup);
Michal Vasko8abcecc2022-07-28 09:55:01 +02007206 }
7207
7208cleanup:
7209 lyxp_set_free_content(&tmp1);
7210 return rc;
7211}
7212
7213/**
7214 * @brief Move context @p set1 to the result of a comparison. Handles '=', '!=', '<=', '<', '>=', or '>'.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007215 * Result is LYXP_SET_BOOLEAN. Indirectly context position aware.
7216 *
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007217 * @param[in] set1 Set acting as the first operand for @p op.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007218 * @param[in] set2 Set acting as the second operand for @p op.
7219 * @param[in] op Comparison operator to process.
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007220 * @param[out] result Result of the comparison.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007221 * @return LY_ERR
7222 */
7223static LY_ERR
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007224moveto_op_comp(struct lyxp_set *set1, struct lyxp_set *set2, const char *op, ly_bool *result)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007225{
7226 /*
7227 * NODE SET + NODE SET = NODE SET + STRING /(1 NODE SET) 2 STRING
7228 * NODE SET + STRING = STRING + STRING /1 STRING (2 STRING)
7229 * NODE SET + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
7230 * NODE SET + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
7231 * STRING + NODE SET = STRING + STRING /(1 STRING) 2 STRING
7232 * NUMBER + NODE SET = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
7233 * BOOLEAN + NODE SET = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
7234 *
7235 * '=' or '!='
7236 * BOOLEAN + BOOLEAN
7237 * BOOLEAN + STRING = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
7238 * BOOLEAN + NUMBER = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
7239 * STRING + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
7240 * NUMBER + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
7241 * NUMBER + NUMBER
7242 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
7243 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
7244 * STRING + STRING
7245 *
7246 * '<=', '<', '>=', '>'
7247 * NUMBER + NUMBER
7248 * BOOLEAN + BOOLEAN = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
7249 * BOOLEAN + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
7250 * BOOLEAN + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
7251 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
7252 * STRING + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
7253 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
7254 * NUMBER + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
7255 * STRING + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
7256 */
Michal Vasko8abcecc2022-07-28 09:55:01 +02007257 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007258 LY_ERR rc;
7259
Michal Vasko03ff5a72019-09-11 13:49:33 +02007260 /* iterative evaluation with node-sets */
7261 if ((set1->type == LYXP_SET_NODE_SET) || (set2->type == LYXP_SET_NODE_SET)) {
7262 if (set1->type == LYXP_SET_NODE_SET) {
7263 for (i = 0; i < set1->used; ++i) {
Michal Vasko8abcecc2022-07-28 09:55:01 +02007264 /* evaluate for the single item */
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007265 LY_CHECK_RET(moveto_op_comp_item(set1, i, set2, op, 0, result));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007266
7267 /* lazy evaluation until true */
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007268 if (*result) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007269 return LY_SUCCESS;
7270 }
7271 }
7272 } else {
7273 for (i = 0; i < set2->used; ++i) {
Michal Vasko8abcecc2022-07-28 09:55:01 +02007274 /* evaluate for the single item */
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007275 LY_CHECK_RET(moveto_op_comp_item(set2, i, set1, op, 1, result));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007276
7277 /* lazy evaluation until true */
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007278 if (*result) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007279 return LY_SUCCESS;
7280 }
7281 }
7282 }
7283
Michal Vasko8abcecc2022-07-28 09:55:01 +02007284 /* false for all the nodes */
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007285 *result = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007286 return LY_SUCCESS;
7287 }
7288
7289 /* first convert properly */
7290 if ((op[0] == '=') || (op[0] == '!')) {
7291 if ((set1->type == LYXP_SET_BOOLEAN) || (set2->type == LYXP_SET_BOOLEAN)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007292 lyxp_set_cast(set1, LYXP_SET_BOOLEAN);
7293 lyxp_set_cast(set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007294 } else if ((set1->type == LYXP_SET_NUMBER) || (set2->type == LYXP_SET_NUMBER)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007295 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007296 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007297 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007298 LY_CHECK_RET(rc);
7299 } /* else we have 2 strings */
7300 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007301 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007302 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007303 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007304 LY_CHECK_RET(rc);
7305 }
7306
7307 assert(set1->type == set2->type);
7308
7309 /* compute result */
7310 if (op[0] == '=') {
7311 if (set1->type == LYXP_SET_BOOLEAN) {
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007312 *result = (set1->val.bln == set2->val.bln);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007313 } else if (set1->type == LYXP_SET_NUMBER) {
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007314 *result = (set1->val.num == set2->val.num);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007315 } else {
7316 assert(set1->type == LYXP_SET_STRING);
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007317 *result = strcmp(set1->val.str, set2->val.str) ? 0 : 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007318 }
7319 } else if (op[0] == '!') {
7320 if (set1->type == LYXP_SET_BOOLEAN) {
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007321 *result = (set1->val.bln != set2->val.bln);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007322 } else if (set1->type == LYXP_SET_NUMBER) {
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007323 *result = (set1->val.num != set2->val.num);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007324 } else {
7325 assert(set1->type == LYXP_SET_STRING);
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007326 *result = strcmp(set1->val.str, set2->val.str) ? 1 : 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007327 }
7328 } else {
7329 assert(set1->type == LYXP_SET_NUMBER);
7330 if (op[0] == '<') {
7331 if (op[1] == '=') {
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007332 *result = (set1->val.num <= set2->val.num);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007333 } else {
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007334 *result = (set1->val.num < set2->val.num);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007335 }
7336 } else {
7337 if (op[1] == '=') {
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007338 *result = (set1->val.num >= set2->val.num);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007339 } else {
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007340 *result = (set1->val.num > set2->val.num);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007341 }
7342 }
7343 }
7344
Michal Vasko03ff5a72019-09-11 13:49:33 +02007345 return LY_SUCCESS;
7346}
7347
7348/**
7349 * @brief Move context @p set to the result of a basic operation. Handles '+', '-', unary '-', '*', 'div',
7350 * or 'mod'. Result is LYXP_SET_NUMBER. Indirectly context position aware.
7351 *
7352 * @param[in,out] set1 Set to use for the result.
7353 * @param[in] set2 Set acting as the second operand for @p op.
7354 * @param[in] op Operator to process.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007355 * @return LY_ERR
7356 */
7357static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007358moveto_op_math(struct lyxp_set *set1, struct lyxp_set *set2, const char *op)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007359{
7360 LY_ERR rc;
7361
7362 /* unary '-' */
7363 if (!set2 && (op[0] == '-')) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007364 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007365 LY_CHECK_RET(rc);
7366 set1->val.num *= -1;
7367 lyxp_set_free(set2);
7368 return LY_SUCCESS;
7369 }
7370
7371 assert(set1 && set2);
7372
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007373 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007374 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007375 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007376 LY_CHECK_RET(rc);
7377
7378 switch (op[0]) {
7379 /* '+' */
7380 case '+':
7381 set1->val.num += set2->val.num;
7382 break;
7383
7384 /* '-' */
7385 case '-':
7386 set1->val.num -= set2->val.num;
7387 break;
7388
7389 /* '*' */
7390 case '*':
7391 set1->val.num *= set2->val.num;
7392 break;
7393
7394 /* 'div' */
7395 case 'd':
7396 set1->val.num /= set2->val.num;
7397 break;
7398
7399 /* 'mod' */
7400 case 'm':
7401 set1->val.num = ((long long)set1->val.num) % ((long long)set2->val.num);
7402 break;
7403
7404 default:
7405 LOGINT_RET(set1->ctx);
7406 }
7407
7408 return LY_SUCCESS;
7409}
7410
Michal Vasko03ff5a72019-09-11 13:49:33 +02007411/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02007412 * @brief Evaluate Predicate. Logs directly on error.
7413 *
Michal Vaskod3678892020-05-21 10:06:58 +02007414 * [9] Predicate ::= '[' Expr ']'
Michal Vasko03ff5a72019-09-11 13:49:33 +02007415 *
7416 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007417 * @param[in] tok_idx Position in the expression @p exp.
aPiecek8b0cc152021-05-31 16:40:31 +02007418 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007419 * @param[in] options XPath options.
Michal Vasko49fec8e2022-05-24 10:28:33 +02007420 * @param[in] axis Axis to search on.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007421 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7422 */
7423static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02007424eval_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 +02007425{
7426 LY_ERR rc;
Michal Vaskodd528af2022-08-08 14:35:07 +02007427 uint32_t i, orig_exp, orig_pos, orig_size;
Michal Vasko5c4e5892019-11-14 12:31:38 +01007428 int32_t pred_in_ctx;
Michal Vasko88a9e802022-05-24 10:50:28 +02007429 ly_bool reverse_axis = 0;
aPiecekfff4dca2021-10-07 10:59:53 +02007430 struct lyxp_set set2 = {0};
Michal Vasko03ff5a72019-09-11 13:49:33 +02007431
7432 /* '[' */
aPiecek8b0cc152021-05-31 16:40:31 +02007433 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02007434 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007435 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007436
aPiecek8b0cc152021-05-31 16:40:31 +02007437 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007438only_parse:
aPiecek8b0cc152021-05-31 16:40:31 +02007439 rc = eval_expr_select(exp, tok_idx, 0, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007440 LY_CHECK_RET(rc);
7441 } else if (set->type == LYXP_SET_NODE_SET) {
7442 /* 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 +01007443 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007444
7445 /* empty set, nothing to evaluate */
7446 if (!set->used) {
7447 goto only_parse;
7448 }
7449
Michal Vasko49fec8e2022-05-24 10:28:33 +02007450 /* decide forward or reverse axis */
7451 switch (axis) {
7452 case LYXP_AXIS_ANCESTOR:
7453 case LYXP_AXIS_ANCESTOR_OR_SELF:
7454 case LYXP_AXIS_PRECEDING:
7455 case LYXP_AXIS_PRECEDING_SIBLING:
7456 reverse_axis = 1;
7457 break;
7458 case LYXP_AXIS_DESCENDANT:
7459 case LYXP_AXIS_DESCENDANT_OR_SELF:
7460 case LYXP_AXIS_FOLLOWING:
7461 case LYXP_AXIS_FOLLOWING_SIBLING:
7462 case LYXP_AXIS_PARENT:
7463 case LYXP_AXIS_CHILD:
7464 case LYXP_AXIS_SELF:
7465 case LYXP_AXIS_ATTRIBUTE:
7466 reverse_axis = 0;
7467 break;
7468 }
7469
Michal Vasko004d3152020-06-11 19:59:22 +02007470 orig_exp = *tok_idx;
Michal Vasko49fec8e2022-05-24 10:28:33 +02007471 orig_pos = reverse_axis ? set->used + 1 : 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007472 orig_size = set->used;
Michal Vasko39dbf352020-05-21 10:08:59 +02007473 for (i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007474 set_init(&set2, set);
7475 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 +02007476
7477 /* remember the node context position for position() and context size for last() */
7478 orig_pos += reverse_axis ? -1 : 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007479
7480 set2.ctx_pos = orig_pos;
7481 set2.ctx_size = orig_size;
Michal Vasko004d3152020-06-11 19:59:22 +02007482 *tok_idx = orig_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007483
Michal Vasko004d3152020-06-11 19:59:22 +02007484 rc = eval_expr_select(exp, tok_idx, 0, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007485 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02007486 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007487 return rc;
7488 }
7489
Michal Vasko49fec8e2022-05-24 10:28:33 +02007490 /* number is a proximity position */
Michal Vasko03ff5a72019-09-11 13:49:33 +02007491 if (set2.type == LYXP_SET_NUMBER) {
7492 if ((long long)set2.val.num == orig_pos) {
7493 set2.val.num = 1;
7494 } else {
7495 set2.val.num = 0;
7496 }
7497 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007498 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007499
7500 /* predicate satisfied or not? */
Michal Vasko004d3152020-06-11 19:59:22 +02007501 if (!set2.val.bln) {
Michal Vasko2caefc12019-11-14 16:07:56 +01007502 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007503 }
7504 }
Michal Vasko2caefc12019-11-14 16:07:56 +01007505 set_remove_nodes_none(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007506
7507 } else if (set->type == LYXP_SET_SCNODE_SET) {
7508 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01007509 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007510 /* there is a currently-valid node */
7511 break;
7512 }
7513 }
7514 /* empty set, nothing to evaluate */
7515 if (i == set->used) {
7516 goto only_parse;
7517 }
7518
Michal Vasko004d3152020-06-11 19:59:22 +02007519 orig_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007520
Michal Vasko03ff5a72019-09-11 13:49:33 +02007521 /* set special in_ctx to all the valid snodes */
7522 pred_in_ctx = set_scnode_new_in_ctx(set);
7523
7524 /* use the valid snodes one-by-one */
7525 for (i = 0; i < set->used; ++i) {
7526 if (set->val.scnodes[i].in_ctx != pred_in_ctx) {
7527 continue;
7528 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01007529 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007530
Michal Vasko004d3152020-06-11 19:59:22 +02007531 *tok_idx = orig_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007532
Michal Vasko004d3152020-06-11 19:59:22 +02007533 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007534 LY_CHECK_RET(rc);
7535
7536 set->val.scnodes[i].in_ctx = pred_in_ctx;
7537 }
7538
7539 /* restore the state as it was before the predicate */
7540 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01007541 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007542 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007543 } else if (set->val.scnodes[i].in_ctx == pred_in_ctx) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01007544 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007545 }
7546 }
7547
7548 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02007549 set2.type = LYXP_SET_NODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007550 set_fill_set(&set2, set);
7551
Michal Vasko004d3152020-06-11 19:59:22 +02007552 rc = eval_expr_select(exp, tok_idx, 0, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007553 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02007554 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007555 return rc;
7556 }
7557
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007558 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +02007559 if (!set2.val.bln) {
Michal Vaskod3678892020-05-21 10:06:58 +02007560 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007561 }
Michal Vaskod3678892020-05-21 10:06:58 +02007562 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007563 }
7564
7565 /* ']' */
Michal Vasko004d3152020-06-11 19:59:22 +02007566 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_BRACK2);
aPiecek8b0cc152021-05-31 16:40:31 +02007567 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02007568 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007569 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007570
7571 return LY_SUCCESS;
7572}
7573
7574/**
Michal Vaskod3678892020-05-21 10:06:58 +02007575 * @brief Evaluate Literal. Logs directly on error.
7576 *
7577 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007578 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02007579 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7580 */
7581static void
Michal Vaskodd528af2022-08-08 14:35:07 +02007582eval_literal(const struct lyxp_expr *exp, uint32_t *tok_idx, struct lyxp_set *set)
Michal Vaskod3678892020-05-21 10:06:58 +02007583{
7584 if (set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007585 if (exp->tok_len[*tok_idx] == 2) {
Michal Vaskod3678892020-05-21 10:06:58 +02007586 set_fill_string(set, "", 0);
7587 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007588 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 +02007589 }
7590 }
7591 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02007592 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007593 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007594}
7595
7596/**
Michal Vasko3d969ff2022-07-29 15:02:08 +02007597 * @brief Check that a nametest in a predicate matches a key node.
7598 *
7599 * @param[in] nametest Nametest to check.
7600 * @param[in] len Length of @p nametest.
7601 * @param[in] ctx_scnode Found schema node as the context for the predicate.
7602 * @param[in] set Context set.
7603 * @param[in] key Expected key node.
7604 * @return LY_SUCCESS on success,
7605 * @return LY_ENOT if a predicate could not be compiled.
7606 * @return LY_ERR on any error.
7607 */
7608static LY_ERR
7609eval_name_test_try_compile_predicate_key(const char *nametest, uint32_t len, const struct lysc_node *ctx_scnode,
7610 const struct lyxp_set *set, const struct lysc_node *key)
7611{
7612 const struct lys_module *mod;
7613
7614 /* prefix (module) */
7615 LY_CHECK_RET(moveto_resolve_model(&nametest, &len, set, ctx_scnode, &mod));
7616 if (mod != key->module) {
7617 return LY_ENOT;
7618 }
7619
7620 /* node name */
7621 if (ly_strncmp(key->name, nametest, len)) {
7622 return LY_ENOT;
7623 }
7624
7625 return LY_SUCCESS;
7626}
7627
7628/**
7629 * @brief Append a simple predicate for the node.
7630 *
7631 * @param[in] exp Full parsed XPath expression.
7632 * @param[in] tok_idx Predicate start index in @p exp.
7633 * @param[in] end_tok_idx Predicate end index in @p exp.
7634 * @param[in] ctx_scnode Found schema node as the context for the predicate.
7635 * @param[in] set Context set.
7636 * @param[in] pred_node Node with the value referenced in the predicate.
7637 * @param[in,out] pred Predicate to append to.
7638 * @param[in,out] pred_len Length of @p pred, is updated.
7639 * @return LY_SUCCESS on success,
7640 * @return LY_ENOT if a predicate could not be compiled.
7641 * @return LY_ERR on any error.
7642 */
7643static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02007644eval_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 +02007645 const struct lysc_node *ctx_scnode, const struct lyxp_set *set, const struct lysc_node *pred_node, char **pred,
7646 uint32_t *pred_len)
7647{
7648 LY_ERR rc = LY_SUCCESS;
7649 uint32_t i;
7650 const struct lyd_node *siblings;
7651 struct lyd_node *ctx_node;
Michal Vasko5fb92a22022-08-02 09:21:37 +02007652 const struct lysc_node *sparent, *cur_scnode;
Michal Vasko3d969ff2022-07-29 15:02:08 +02007653 struct lyxp_expr *val_exp = NULL;
7654 struct lyxp_set set2 = {0};
7655 char quot;
7656
7657 /* duplicate the value expression */
7658 LY_CHECK_GOTO(rc = lyxp_expr_dup(set->ctx, exp, tok_idx, end_tok_idx, &val_exp), cleanup);
7659
7660 /* get its atoms */
Michal Vasko5fb92a22022-08-02 09:21:37 +02007661 cur_scnode = set->cur_node ? set->cur_node->schema : NULL;
7662 LY_CHECK_GOTO(rc = lyxp_atomize(set->ctx, val_exp, set->cur_mod, set->format, set->prefix_data, cur_scnode,
7663 ctx_scnode, &set2, LYXP_SCNODE), cleanup);
Michal Vasko3d969ff2022-07-29 15:02:08 +02007664
7665 /* check whether we can compile a single predicate (evaluation result value is always the same) */
7666 for (i = 0; i < set2.used; ++i) {
7667 if ((set2.val.scnodes[i].type != LYXP_NODE_ELEM) || (set2.val.scnodes[i].in_ctx < LYXP_SET_SCNODE_ATOM_NODE)) {
7668 /* skip root and context node */
7669 continue;
7670 }
7671
Michal Vasko5fb92a22022-08-02 09:21:37 +02007672 /* 1) context node descendants are traversed - do best-effort detection of the value dependency on the
7673 * context node instance */
7674 if ((set2.val.scnodes[i].axis == LYXP_AXIS_CHILD) && (set2.val.scnodes[i].scnode->parent == ctx_scnode)) {
7675 /* 1.1) context node child was accessed on the child axis, certain dependency */
Michal Vasko3d969ff2022-07-29 15:02:08 +02007676 rc = LY_ENOT;
7677 goto cleanup;
7678 }
Michal Vasko5fb92a22022-08-02 09:21:37 +02007679 if ((set2.val.scnodes[i].axis == LYXP_AXIS_DESCENDANT) || (set2.val.scnodes[i].axis == LYXP_AXIS_DESCENDANT_OR_SELF)) {
7680 for (sparent = set2.val.scnodes[i].scnode->parent; sparent && (sparent != ctx_scnode); sparent = sparent->parent) {}
7681 if (sparent) {
7682 /* 1.2) context node descendant was accessed on the descendant axis, probable dependency */
7683 rc = LY_ENOT;
7684 goto cleanup;
7685 }
7686 }
Michal Vasko3d969ff2022-07-29 15:02:08 +02007687
Michal Vasko5fb92a22022-08-02 09:21:37 +02007688 /* 2) multi-instance nodes (list or leaf-list) are traversed - all the instances need to be considered,
7689 * but the current node can be safely ignored, it is always the same data instance */
7690 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 +02007691 rc = LY_ENOT;
7692 goto cleanup;
7693 }
Michal Vasko3d969ff2022-07-29 15:02:08 +02007694 }
7695
7696 /* get any data instance of the context node, we checked it makes no difference */
7697 siblings = set->val.nodes[0].node ? lyd_child(set->val.nodes[0].node) : set->tree;
7698 LY_CHECK_GOTO(rc = lyd_find_sibling_schema(siblings, ctx_scnode, &ctx_node), cleanup);
7699
7700 /* evaluate the value subexpression with the root context node */
7701 lyxp_set_free_content(&set2);
7702 LY_CHECK_GOTO(rc = lyxp_eval(set->ctx, val_exp, set->cur_mod, set->format, set->prefix_data, set->cur_node,
7703 ctx_node, set->tree, NULL, &set2, 0), cleanup);
7704
7705 /* cast it into a string */
7706 LY_CHECK_GOTO(rc = lyxp_set_cast(&set2, LYXP_SET_STRING), cleanup);
7707
7708 /* append the JSON predicate */
7709 *pred = ly_realloc(*pred, *pred_len + 1 + strlen(pred_node->name) + 2 + strlen(set2.val.str) + 3);
7710 LY_CHECK_ERR_GOTO(!*pred, LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
7711 quot = strchr(set2.val.str, '\'') ? '\"' : '\'';
7712 *pred_len += sprintf(*pred + *pred_len, "[%s=%c%s%c]", pred_node->name, quot, set2.val.str, quot);
7713
7714cleanup:
7715 lyxp_expr_free(set->ctx, val_exp);
7716 lyxp_set_free_content(&set2);
7717 return rc;
7718}
7719
7720/**
Michal Vasko004d3152020-06-11 19:59:22 +02007721 * @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 +02007722 *
Michal Vasko004d3152020-06-11 19:59:22 +02007723 * @param[in] exp Full parsed XPath expression.
7724 * @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 +02007725 * @param[in] ctx_scnode Found schema node as the context for the predicate.
7726 * @param[in] set Context set.
Michal Vasko004d3152020-06-11 19:59:22 +02007727 * @param[out] predicates Parsed predicates.
Michal Vasko004d3152020-06-11 19:59:22 +02007728 * @return LY_SUCCESS on success,
Michal Vasko3d969ff2022-07-29 15:02:08 +02007729 * @return LY_ENOT if a predicate could not be compiled.
Michal Vasko004d3152020-06-11 19:59:22 +02007730 * @return LY_ERR on any error.
Michal Vaskod3678892020-05-21 10:06:58 +02007731 */
7732static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02007733eval_name_test_try_compile_predicates(const struct lyxp_expr *exp, uint32_t *tok_idx, const struct lysc_node *ctx_scnode,
Michal Vasko90189962023-02-28 12:10:34 +01007734 const struct lyxp_set *set, struct ly_path_predicate **predicates)
Michal Vaskod3678892020-05-21 10:06:58 +02007735{
Michal Vasko3d969ff2022-07-29 15:02:08 +02007736 LY_ERR rc = LY_SUCCESS;
Michal Vaskod4a6d042022-12-08 08:34:29 +01007737 uint32_t e_idx, val_start_idx, pred_idx = 0, temp_lo = 0, pred_len = 0, nested_pred;
Michal Vaskod3678892020-05-21 10:06:58 +02007738 const struct lysc_node *key;
Michal Vasko3d969ff2022-07-29 15:02:08 +02007739 char *pred = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02007740 struct lyxp_expr *exp2 = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02007741
Michal Vasko3d969ff2022-07-29 15:02:08 +02007742 assert(ctx_scnode->nodetype & (LYS_LIST | LYS_LEAFLIST));
Michal Vaskod3678892020-05-21 10:06:58 +02007743
Michal Vasko3d969ff2022-07-29 15:02:08 +02007744 /* turn logging off */
Michal Vaskod4a6d042022-12-08 08:34:29 +01007745 ly_temp_log_options(&temp_lo);
Michal Vasko3d969ff2022-07-29 15:02:08 +02007746
7747 if (ctx_scnode->nodetype == LYS_LIST) {
7748 /* check for predicates "[key1=...][key2=...]..." */
7749
Michal Vasko004d3152020-06-11 19:59:22 +02007750 /* get key count */
Michal Vasko3d969ff2022-07-29 15:02:08 +02007751 if (ctx_scnode->flags & LYS_KEYLESS) {
7752 rc = LY_ENOT;
7753 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +02007754 }
Michal Vaskod3678892020-05-21 10:06:58 +02007755
Michal Vasko004d3152020-06-11 19:59:22 +02007756 /* learn where the predicates end */
7757 e_idx = *tok_idx;
Michal Vasko3d969ff2022-07-29 15:02:08 +02007758 for (key = lysc_node_child(ctx_scnode); key && (key->flags & LYS_KEY); key = key->next) {
Michal Vasko004d3152020-06-11 19:59:22 +02007759 /* '[' */
7760 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
Michal Vasko3d969ff2022-07-29 15:02:08 +02007761 rc = LY_ENOT;
7762 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +02007763 }
7764 ++e_idx;
7765
Michal Vasko3354d272021-04-06 09:40:06 +02007766 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_NAMETEST)) {
Michal Vasko3d969ff2022-07-29 15:02:08 +02007767 /* not a key */
7768 rc = LY_ENOT;
7769 goto cleanup;
Michal Vasko3354d272021-04-06 09:40:06 +02007770 }
7771
Michal Vasko3d969ff2022-07-29 15:02:08 +02007772 /* check key */
7773 LY_CHECK_GOTO(rc = eval_name_test_try_compile_predicate_key(exp->expr + exp->tok_pos[e_idx],
7774 exp->tok_len[e_idx], ctx_scnode, set, key), cleanup);
7775
7776 ++e_idx;
7777
7778 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_OPER_EQUAL)) {
7779 /* not '=' */
7780 rc = LY_ENOT;
7781 goto cleanup;
7782 }
7783 ++e_idx;
7784
7785 /* value start */
7786 val_start_idx = e_idx;
7787
Michal Vasko004d3152020-06-11 19:59:22 +02007788 /* ']' */
Michal Vasko3ef7e252022-11-16 08:29:49 +01007789 nested_pred = 1;
7790 do {
7791 ++e_idx;
7792
7793 if ((nested_pred == 1) && !lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_OPER_LOG)) {
Michal Vaskob8ee3562022-08-02 10:43:17 +02007794 /* higher priority than '=' */
7795 rc = LY_ENOT;
7796 goto cleanup;
Michal Vasko3ef7e252022-11-16 08:29:49 +01007797 } else if (!lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
7798 /* nested predicate */
7799 ++nested_pred;
7800 } else if (!lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) {
7801 /* predicate end */
7802 --nested_pred;
Michal Vaskob8ee3562022-08-02 10:43:17 +02007803 }
Michal Vasko3ef7e252022-11-16 08:29:49 +01007804 } while (nested_pred);
Michal Vasko004d3152020-06-11 19:59:22 +02007805
Michal Vasko3d969ff2022-07-29 15:02:08 +02007806 /* try to evaluate the value */
7807 LY_CHECK_GOTO(rc = eval_name_test_try_compile_predicate_append(exp, val_start_idx, e_idx - 1, ctx_scnode,
7808 set, key, &pred, &pred_len), cleanup);
7809
7810 ++e_idx;
Michal Vasko004d3152020-06-11 19:59:22 +02007811 }
Michal Vasko004d3152020-06-11 19:59:22 +02007812 } else {
Michal Vasko3d969ff2022-07-29 15:02:08 +02007813 /* check for predicate "[.=...]" */
7814
Michal Vasko004d3152020-06-11 19:59:22 +02007815 /* learn just where this single predicate ends */
7816 e_idx = *tok_idx;
7817
Michal Vaskod3678892020-05-21 10:06:58 +02007818 /* '[' */
Michal Vasko004d3152020-06-11 19:59:22 +02007819 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
Michal Vasko3d969ff2022-07-29 15:02:08 +02007820 rc = LY_ENOT;
7821 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +02007822 }
7823 ++e_idx;
Michal Vaskod3678892020-05-21 10:06:58 +02007824
Michal Vasko3354d272021-04-06 09:40:06 +02007825 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_DOT)) {
Michal Vasko3d969ff2022-07-29 15:02:08 +02007826 /* not the node value */
7827 rc = LY_ENOT;
7828 goto cleanup;
Michal Vasko3354d272021-04-06 09:40:06 +02007829 }
Michal Vasko3d969ff2022-07-29 15:02:08 +02007830 ++e_idx;
7831
7832 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_OPER_EQUAL)) {
7833 /* not '=' */
7834 rc = LY_ENOT;
7835 goto cleanup;
7836 }
7837 ++e_idx;
7838
7839 /* value start */
7840 val_start_idx = e_idx;
Michal Vasko3354d272021-04-06 09:40:06 +02007841
Michal Vaskod3678892020-05-21 10:06:58 +02007842 /* ']' */
Michal Vasko31f19632022-11-16 09:38:40 +01007843 nested_pred = 1;
7844 do {
7845 ++e_idx;
7846
7847 if ((nested_pred == 1) && !lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_OPER_LOG)) {
Michal Vaskob8ee3562022-08-02 10:43:17 +02007848 /* higher priority than '=' */
7849 rc = LY_ENOT;
7850 goto cleanup;
Michal Vasko31f19632022-11-16 09:38:40 +01007851 } else if (!lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
7852 /* nested predicate */
7853 ++nested_pred;
7854 } else if (!lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) {
7855 /* predicate end */
7856 --nested_pred;
Michal Vaskob8ee3562022-08-02 10:43:17 +02007857 }
Michal Vasko31f19632022-11-16 09:38:40 +01007858 } while (nested_pred);
Michal Vasko3d969ff2022-07-29 15:02:08 +02007859
7860 /* try to evaluate the value */
7861 LY_CHECK_GOTO(rc = eval_name_test_try_compile_predicate_append(exp, val_start_idx, e_idx - 1, ctx_scnode, set,
7862 ctx_scnode, &pred, &pred_len), cleanup);
7863
Michal Vasko004d3152020-06-11 19:59:22 +02007864 ++e_idx;
Michal Vaskod3678892020-05-21 10:06:58 +02007865 }
7866
Michal Vasko004d3152020-06-11 19:59:22 +02007867 /* parse the predicate(s) */
Michal Vasko3d969ff2022-07-29 15:02:08 +02007868 LY_CHECK_GOTO(rc = ly_path_parse_predicate(set->ctx, ctx_scnode, pred, pred_len, LY_PATH_PREFIX_OPTIONAL,
7869 LY_PATH_PRED_SIMPLE, &exp2), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02007870
7871 /* compile */
Michal Vasko3d969ff2022-07-29 15:02:08 +02007872 rc = ly_path_compile_predicate(set->ctx, set->cur_node ? set->cur_node->schema : NULL, set->cur_mod, ctx_scnode, exp2,
Michal Vasko90189962023-02-28 12:10:34 +01007873 &pred_idx, LY_VALUE_JSON, NULL, predicates);
Michal Vasko3d969ff2022-07-29 15:02:08 +02007874 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02007875
7876 /* success, the predicate must include all the needed information for hash-based search */
7877 *tok_idx = e_idx;
7878
7879cleanup:
Michal Vaskod4a6d042022-12-08 08:34:29 +01007880 ly_temp_log_options(NULL);
Michal Vasko3d969ff2022-07-29 15:02:08 +02007881 lyxp_expr_free(set->ctx, exp2);
7882 free(pred);
7883 return rc;
Michal Vaskod3678892020-05-21 10:06:58 +02007884}
7885
7886/**
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007887 * @brief Search for/check the next schema node that could be the only matching schema node meaning the
7888 * data node(s) could be found using a single hash-based search.
7889 *
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007890 * @param[in] ctx libyang context.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007891 * @param[in] node Next context node to check.
7892 * @param[in] name Expected node name.
7893 * @param[in] name_len Length of @p name.
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007894 * @param[in] moveto_mod Expected node module, can be NULL for JSON format with no prefix.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007895 * @param[in] root_type XPath root type.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007896 * @param[in] format Prefix format.
7897 * @param[in,out] found Previously found node, is updated.
7898 * @return LY_SUCCESS on success,
7899 * @return LY_ENOT if the whole check failed and hashes cannot be used.
7900 */
7901static LY_ERR
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007902eval_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 +02007903 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 +02007904 const struct lysc_node **found)
7905{
7906 const struct lysc_node *scnode;
7907 const struct lys_module *mod;
7908 uint32_t idx = 0;
7909
Radek Krejci8df109d2021-04-23 12:19:08 +02007910 assert((format == LY_VALUE_JSON) || moveto_mod);
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007911
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007912continue_search:
Michal Vasko7d1d0912020-10-16 08:38:30 +02007913 scnode = NULL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007914 if (!node) {
Radek Krejci8df109d2021-04-23 12:19:08 +02007915 if ((format == LY_VALUE_JSON) && !moveto_mod) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007916 /* search all modules for a single match */
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007917 while ((mod = ly_ctx_get_module_iter(ctx, &idx))) {
Michal Vasko35a3b1d2021-07-14 09:34:16 +02007918 if (!mod->implemented) {
7919 continue;
7920 }
7921
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007922 scnode = lys_find_child(NULL, mod, name, name_len, 0, 0);
7923 if (scnode) {
7924 /* we have found a match */
7925 break;
7926 }
7927 }
7928
Michal Vasko7d1d0912020-10-16 08:38:30 +02007929 if (!scnode) {
7930 /* all modules searched */
7931 idx = 0;
7932 }
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007933 } else {
7934 /* search in top-level */
7935 scnode = lys_find_child(NULL, moveto_mod, name, name_len, 0, 0);
7936 }
7937 } else if (!*found || (lysc_data_parent(*found) != node->schema)) {
Radek Krejci8df109d2021-04-23 12:19:08 +02007938 if ((format == LY_VALUE_JSON) && !moveto_mod) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007939 /* we must adjust the module to inherit the one from the context node */
7940 moveto_mod = node->schema->module;
7941 }
7942
7943 /* search in children, do not repeat the same search */
7944 scnode = lys_find_child(node->schema, moveto_mod, name, name_len, 0, 0);
Michal Vasko7d1d0912020-10-16 08:38:30 +02007945 } /* else skip redundant search */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007946
7947 /* additional context check */
7948 if (scnode && (root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) {
7949 scnode = NULL;
7950 }
7951
7952 if (scnode) {
7953 if (*found) {
7954 /* we found a schema node with the same name but at different level, give up, too complicated
7955 * (more hash-based searches would be required, not supported) */
7956 return LY_ENOT;
7957 } else {
7958 /* remember the found schema node and continue to make sure it can be used */
7959 *found = scnode;
7960 }
7961 }
7962
7963 if (idx) {
7964 /* continue searching all the following models */
7965 goto continue_search;
7966 }
7967
7968 return LY_SUCCESS;
7969}
7970
7971/**
Michal Vasko4ad69e72021-10-26 16:25:55 +02007972 * @brief Generate message when no matching schema nodes were found for a path segment.
7973 *
7974 * @param[in] set XPath set.
7975 * @param[in] scparent Previous schema parent in the context, if only one.
7976 * @param[in] ncname XPath NCName being evaluated.
7977 * @param[in] ncname_len Length of @p ncname.
7978 * @param[in] expr Whole XPath expression.
7979 * @param[in] options XPath options.
7980 */
7981static void
7982eval_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 +02007983 uint32_t ncname_len, const char *expr, uint32_t options)
Michal Vasko4ad69e72021-10-26 16:25:55 +02007984{
7985 const char *format;
7986 char *path = NULL, *ppath = NULL;
7987
7988 path = lysc_path(set->cur_scnode, LYSC_PATH_LOG, NULL, 0);
7989 if (scparent) {
7990 /* generate path for the parent */
7991 if (scparent->type == LYXP_NODE_ELEM) {
7992 ppath = lysc_path(scparent->scnode, LYSC_PATH_LOG, NULL, 0);
7993 } else if (scparent->type == LYXP_NODE_ROOT) {
7994 ppath = strdup("<root>");
7995 } else if (scparent->type == LYXP_NODE_ROOT_CONFIG) {
7996 ppath = strdup("<config-root>");
7997 }
7998 }
7999 if (ppath) {
8000 format = "Schema node \"%.*s\" for parent \"%s\" not found; in expr \"%.*s\" with context node \"%s\".";
8001 if (options & LYXP_SCNODE_ERROR) {
8002 LOGERR(set->ctx, LY_EVALID, format, ncname_len, ncname, ppath, (ncname - expr) + ncname_len, expr, path);
8003 } else {
8004 LOGWRN(set->ctx, format, ncname_len, ncname, ppath, (ncname - expr) + ncname_len, expr, path);
8005 }
8006 } else {
8007 format = "Schema node \"%.*s\" not found; in expr \"%.*s\" with context node \"%s\".";
8008 if (options & LYXP_SCNODE_ERROR) {
8009 LOGERR(set->ctx, LY_EVALID, format, ncname_len, ncname, (ncname - expr) + ncname_len, expr, path);
8010 } else {
8011 LOGWRN(set->ctx, format, ncname_len, ncname, (ncname - expr) + ncname_len, expr, path);
8012 }
8013 }
8014 free(path);
8015 free(ppath);
8016}
8017
8018/**
Michal Vaskod3678892020-05-21 10:06:58 +02008019 * @brief Evaluate NameTest and any following Predicates. Logs directly on error.
8020 *
8021 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
8022 * [6] NodeTest ::= NameTest | NodeType '(' ')'
8023 * [7] NameTest ::= '*' | NCName ':' '*' | QName
8024 *
8025 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008026 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko49fec8e2022-05-24 10:28:33 +02008027 * @param[in] axis What axis to search on.
Michal Vaskod3678892020-05-21 10:06:58 +02008028 * @param[in] all_desc Whether to search all the descendants or children only.
aPiecek8b0cc152021-05-31 16:40:31 +02008029 * @param[in,out] set Context and result set.
Michal Vaskod3678892020-05-21 10:06:58 +02008030 * @param[in] options XPath options.
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008031 * @return LY_ERR (LY_EINCOMPLETE on unresolved when, LY_ENOT for not found schema node)
Michal Vaskod3678892020-05-21 10:06:58 +02008032 */
8033static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02008034eval_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 +02008035 struct lyxp_set *set, uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02008036{
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008037 LY_ERR rc = LY_SUCCESS, r;
Michal Vasko004d3152020-06-11 19:59:22 +02008038 const char *ncname, *ncname_dict = NULL;
Michal Vasko56c008c2022-07-29 14:57:47 +02008039 uint32_t i, ncname_len;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008040 const struct lys_module *moveto_mod = NULL;
Michal Vaskoc71c37b2021-01-11 13:40:02 +01008041 const struct lysc_node *scnode = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02008042 struct ly_path_predicate *predicates = NULL;
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008043 int scnode_skip_pred = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02008044
aPiecek8b0cc152021-05-31 16:40:31 +02008045 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008046 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008047 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02008048
aPiecek8b0cc152021-05-31 16:40:31 +02008049 if (options & LYXP_SKIP_EXPR) {
Michal Vaskod3678892020-05-21 10:06:58 +02008050 goto moveto;
8051 }
8052
Michal Vasko004d3152020-06-11 19:59:22 +02008053 ncname = &exp->expr[exp->tok_pos[*tok_idx - 1]];
8054 ncname_len = exp->tok_len[*tok_idx - 1];
Michal Vaskod3678892020-05-21 10:06:58 +02008055
Michal Vaskoc71c37b2021-01-11 13:40:02 +01008056 if ((ncname[0] == '*') && (ncname_len == 1)) {
8057 /* all nodes will match */
8058 goto moveto;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008059 }
8060
Michal Vaskoc71c37b2021-01-11 13:40:02 +01008061 /* parse (and skip) module name */
8062 rc = moveto_resolve_model(&ncname, &ncname_len, set, NULL, &moveto_mod);
Michal Vaskod3678892020-05-21 10:06:58 +02008063 LY_CHECK_GOTO(rc, cleanup);
8064
Michal Vasko49fec8e2022-05-24 10:28:33 +02008065 if ((ncname[0] == '*') && (ncname_len == 1)) {
8066 /* all nodes from the module will match */
8067 goto moveto;
8068 }
8069
8070 if (((set->format == LY_VALUE_JSON) || moveto_mod) && (axis == LYXP_AXIS_CHILD) && !all_desc &&
8071 (set->type == LYXP_SET_NODE_SET)) {
Michal Vaskod3678892020-05-21 10:06:58 +02008072 /* find the matching schema node in some parent in the context */
Michal Vasko56c008c2022-07-29 14:57:47 +02008073 for (i = 0; i < set->used; ++i) {
Michal Vaskoc71c37b2021-01-11 13:40:02 +01008074 if (eval_name_test_with_predicate_get_scnode(set->ctx, set->val.nodes[i].node, ncname, ncname_len,
8075 moveto_mod, set->root_type, set->format, &scnode)) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008076 /* check failed */
8077 scnode = NULL;
8078 break;
Michal Vaskod3678892020-05-21 10:06:58 +02008079 }
8080 }
8081
Michal Vasko004d3152020-06-11 19:59:22 +02008082 if (scnode && (scnode->nodetype & (LYS_LIST | LYS_LEAFLIST))) {
8083 /* try to create the predicates */
Michal Vasko90189962023-02-28 12:10:34 +01008084 if (eval_name_test_try_compile_predicates(exp, tok_idx, scnode, set, &predicates)) {
Michal Vasko004d3152020-06-11 19:59:22 +02008085 /* hashes cannot be used */
Michal Vaskod3678892020-05-21 10:06:58 +02008086 scnode = NULL;
8087 }
8088 }
8089 }
8090
Michal Vaskoc71c37b2021-01-11 13:40:02 +01008091 if (!scnode) {
8092 /* we are not able to match based on a schema node and not all the modules match ("*"),
Michal Vasko004d3152020-06-11 19:59:22 +02008093 * use dictionary for efficient comparison */
Radek Krejci011e4aa2020-09-04 15:22:31 +02008094 LY_CHECK_GOTO(rc = lydict_insert(set->ctx, ncname, ncname_len, &ncname_dict), cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02008095 }
8096
8097moveto:
8098 /* move to the attribute(s), data node(s), or schema node(s) */
Michal Vasko49fec8e2022-05-24 10:28:33 +02008099 if (axis == LYXP_AXIS_ATTRIBUTE) {
aPiecek8b0cc152021-05-31 16:40:31 +02008100 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008101 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskod3678892020-05-21 10:06:58 +02008102 } else {
8103 if (all_desc) {
Michal Vaskocdad7122020-11-09 21:04:44 +01008104 rc = moveto_attr_alldesc(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02008105 } else {
aPiecek8b0cc152021-05-31 16:40:31 +02008106 rc = moveto_attr(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02008107 }
8108 LY_CHECK_GOTO(rc, cleanup);
8109 }
8110 } else {
aPiecek8b0cc152021-05-31 16:40:31 +02008111 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008112 const struct lyxp_set_scnode *scparent = NULL;
Michal Vasko56c008c2022-07-29 14:57:47 +02008113 ly_bool found = 0;
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008114
8115 /* remember parent if there is only one, to print in the warning */
8116 for (i = 0; i < set->used; ++i) {
8117 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
8118 if (!scparent) {
8119 /* remember the context node */
8120 scparent = &set->val.scnodes[i];
8121 } else {
8122 /* several context nodes, no reasonable error possible */
8123 scparent = NULL;
8124 break;
8125 }
8126 }
8127 }
Radek Krejci1deb5be2020-08-26 16:43:36 +02008128
Michal Vasko49fec8e2022-05-24 10:28:33 +02008129 if (all_desc && (axis == LYXP_AXIS_CHILD)) {
8130 /* efficient evaluation that does not add all the descendants into the set */
8131 rc = moveto_scnode_alldesc_child(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02008132 } else {
Michal Vasko49fec8e2022-05-24 10:28:33 +02008133 if (all_desc) {
8134 /* "//" == "/descendant-or-self::node()/" */
8135 rc = xpath_pi_node(set, LYXP_AXIS_DESCENDANT_OR_SELF, options);
8136 LY_CHECK_GOTO(rc, cleanup);
8137 }
8138 rc = moveto_scnode(set, moveto_mod, ncname_dict, axis, options);
Michal Vaskod3678892020-05-21 10:06:58 +02008139 }
8140 LY_CHECK_GOTO(rc, cleanup);
8141
Michal Vasko56c008c2022-07-29 14:57:47 +02008142 i = set->used;
8143 do {
8144 --i;
Michal Vasko1a09b212021-05-06 13:00:10 +02008145 if (set->val.scnodes[i].in_ctx > LYXP_SET_SCNODE_ATOM_NODE) {
Michal Vasko56c008c2022-07-29 14:57:47 +02008146 found = 1;
Michal Vaskod3678892020-05-21 10:06:58 +02008147 break;
8148 }
Michal Vasko56c008c2022-07-29 14:57:47 +02008149 } while (i);
8150 if (!found) {
Michal Vasko4ad69e72021-10-26 16:25:55 +02008151 /* generate message */
8152 eval_name_test_scnode_no_match_msg(set, scparent, ncname, ncname_len, exp->expr, options);
8153
8154 if (options & LYXP_SCNODE_ERROR) {
8155 /* error */
8156 rc = LY_EVALID;
8157 goto cleanup;
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008158 }
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008159
8160 /* skip the predicates and the rest of this path to not generate invalid warnings */
8161 rc = LY_ENOT;
8162 scnode_skip_pred = 1;
Michal Vaskod3678892020-05-21 10:06:58 +02008163 }
8164 } else {
Michal Vasko49fec8e2022-05-24 10:28:33 +02008165 if (all_desc && (axis == LYXP_AXIS_CHILD)) {
8166 /* efficient evaluation */
8167 rc = moveto_node_alldesc_child(set, moveto_mod, ncname_dict, options);
8168 } else if (scnode && (axis == LYXP_AXIS_CHILD)) {
8169 /* we can find the child nodes using hashes */
8170 rc = moveto_node_hash_child(set, scnode, predicates, options);
Michal Vaskod3678892020-05-21 10:06:58 +02008171 } else {
Michal Vasko49fec8e2022-05-24 10:28:33 +02008172 if (all_desc) {
8173 /* "//" == "/descendant-or-self::node()/" */
8174 rc = xpath_pi_node(set, LYXP_AXIS_DESCENDANT_OR_SELF, options);
8175 LY_CHECK_GOTO(rc, cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02008176 }
Michal Vasko49fec8e2022-05-24 10:28:33 +02008177 rc = moveto_node(set, moveto_mod, ncname_dict, axis, options);
Michal Vaskod3678892020-05-21 10:06:58 +02008178 }
8179 LY_CHECK_GOTO(rc, cleanup);
8180 }
8181 }
8182
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008183 if (scnode_skip_pred) {
8184 /* skip predicates */
8185 options |= LYXP_SKIP_EXPR;
8186 }
8187
Michal Vaskod3678892020-05-21 10:06:58 +02008188 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02008189 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02008190 r = eval_predicate(exp, tok_idx, set, options, axis);
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008191 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02008192 }
8193
8194cleanup:
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008195 if (scnode_skip_pred) {
8196 /* restore options */
8197 options &= ~LYXP_SKIP_EXPR;
8198 }
aPiecek8b0cc152021-05-31 16:40:31 +02008199 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko004d3152020-06-11 19:59:22 +02008200 lydict_remove(set->ctx, ncname_dict);
Michal Vasko90189962023-02-28 12:10:34 +01008201 ly_path_predicates_free(set->ctx, predicates);
Michal Vaskodb51a8d2020-05-27 15:22:29 +02008202 }
Michal Vaskod3678892020-05-21 10:06:58 +02008203 return rc;
8204}
8205
8206/**
8207 * @brief Evaluate NodeType and any following Predicates. Logs directly on error.
8208 *
8209 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
8210 * [6] NodeTest ::= NameTest | NodeType '(' ')'
8211 * [8] NodeType ::= 'text' | 'node'
8212 *
8213 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008214 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko49fec8e2022-05-24 10:28:33 +02008215 * @param[in] axis Axis to search on.
8216 * @param[in] all_desc Whether to search all the descendants or axis only.
aPiecek8b0cc152021-05-31 16:40:31 +02008217 * @param[in,out] set Context and result set.
Michal Vaskod3678892020-05-21 10:06:58 +02008218 * @param[in] options XPath options.
8219 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8220 */
8221static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02008222eval_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 +02008223 struct lyxp_set *set, uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02008224{
8225 LY_ERR rc;
8226
Michal Vaskod3678892020-05-21 10:06:58 +02008227 (void)all_desc;
8228
aPiecek8b0cc152021-05-31 16:40:31 +02008229 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko004d3152020-06-11 19:59:22 +02008230 assert(exp->tok_len[*tok_idx] == 4);
Michal Vasko49fec8e2022-05-24 10:28:33 +02008231 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "node", 4)) {
8232 rc = xpath_pi_node(set, axis, options);
Michal Vaskod3678892020-05-21 10:06:58 +02008233 } else {
Michal Vasko49fec8e2022-05-24 10:28:33 +02008234 assert(!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "text", 4));
8235 rc = xpath_pi_text(set, axis, options);
Michal Vaskod3678892020-05-21 10:06:58 +02008236 }
Michal Vasko49fec8e2022-05-24 10:28:33 +02008237 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02008238 }
aPiecek8b0cc152021-05-31 16:40:31 +02008239 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008240 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008241 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02008242
8243 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02008244 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
aPiecek8b0cc152021-05-31 16:40:31 +02008245 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008246 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008247 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02008248
8249 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02008250 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
aPiecek8b0cc152021-05-31 16:40:31 +02008251 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008252 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008253 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02008254
8255 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02008256 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02008257 rc = eval_predicate(exp, tok_idx, set, options, axis);
Michal Vaskod3678892020-05-21 10:06:58 +02008258 LY_CHECK_RET(rc);
8259 }
8260
8261 return LY_SUCCESS;
8262}
8263
8264/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02008265 * @brief Evaluate RelativeLocationPath. Logs directly on error.
8266 *
8267 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
8268 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
Michal Vaskod3678892020-05-21 10:06:58 +02008269 * [6] NodeTest ::= NameTest | NodeType '(' ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02008270 *
8271 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008272 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008273 * @param[in] all_desc Whether to search all the descendants or children only.
aPiecek8b0cc152021-05-31 16:40:31 +02008274 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008275 * @param[in] options XPath options.
8276 * @return LY_ERR (YL_EINCOMPLETE on unresolved when)
8277 */
8278static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02008279eval_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 +02008280 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008281{
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008282 LY_ERR rc = LY_SUCCESS;
Michal Vasko49fec8e2022-05-24 10:28:33 +02008283 enum lyxp_axis axis;
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008284 int scnode_skip_path = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008285
8286 goto step;
8287 do {
8288 /* evaluate '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02008289 if (exp->tok_len[*tok_idx] == 1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008290 all_desc = 0;
8291 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02008292 assert(exp->tok_len[*tok_idx] == 2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008293 all_desc = 1;
8294 }
aPiecek8b0cc152021-05-31 16:40:31 +02008295 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008296 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008297 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008298
8299step:
Michal Vasko49fec8e2022-05-24 10:28:33 +02008300 /* AxisSpecifier */
8301 if (exp->tokens[*tok_idx] == LYXP_TOKEN_AXISNAME) {
8302 axis = str2axis(exp->expr + exp->tok_pos[*tok_idx], exp->tok_len[*tok_idx]);
Michal Vaskod3678892020-05-21 10:06:58 +02008303
aPiecek8b0cc152021-05-31 16:40:31 +02008304 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008305 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
8306 ++(*tok_idx);
8307
8308 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_DCOLON);
8309 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
8310 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
8311 ++(*tok_idx);
8312 } else if (exp->tokens[*tok_idx] == LYXP_TOKEN_AT) {
8313 axis = LYXP_AXIS_ATTRIBUTE;
8314
8315 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
8316 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008317 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02008318 } else {
Michal Vasko49fec8e2022-05-24 10:28:33 +02008319 /* default */
8320 axis = LYXP_AXIS_CHILD;
Michal Vaskod3678892020-05-21 10:06:58 +02008321 }
8322
Michal Vasko49fec8e2022-05-24 10:28:33 +02008323 /* NodeTest Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02008324 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008325 case LYXP_TOKEN_DOT:
8326 /* evaluate '.' */
Michal Vasko49fec8e2022-05-24 10:28:33 +02008327 if (!(options & LYXP_SKIP_EXPR)) {
8328 if (((options & LYXP_SCNODE_ALL) && (set->type != LYXP_SET_SCNODE_SET)) ||
8329 (!(options & LYXP_SCNODE_ALL) && (set->type != LYXP_SET_NODE_SET))) {
8330 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
8331 rc = LY_EVALID;
8332 goto cleanup;
8333 }
8334
8335 if (all_desc) {
8336 rc = xpath_pi_node(set, LYXP_AXIS_DESCENDANT_OR_SELF, options);
8337 LY_CHECK_GOTO(rc, cleanup);
8338 }
8339 rc = xpath_pi_node(set, LYXP_AXIS_SELF, options);
8340 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008341 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008342 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008343 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008344 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008345 break;
8346
8347 case LYXP_TOKEN_DDOT:
8348 /* evaluate '..' */
Michal Vasko49fec8e2022-05-24 10:28:33 +02008349 if (!(options & LYXP_SKIP_EXPR)) {
8350 if (((options & LYXP_SCNODE_ALL) && (set->type != LYXP_SET_SCNODE_SET)) ||
8351 (!(options & LYXP_SCNODE_ALL) && (set->type != LYXP_SET_NODE_SET))) {
8352 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
8353 rc = LY_EVALID;
8354 goto cleanup;
8355 }
8356
8357 if (all_desc) {
8358 rc = xpath_pi_node(set, LYXP_AXIS_DESCENDANT_OR_SELF, options);
8359 LY_CHECK_GOTO(rc, cleanup);
8360 }
8361 rc = xpath_pi_node(set, LYXP_AXIS_PARENT, options);
8362 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008363 }
aPiecek8b0cc152021-05-31 16:40:31 +02008364 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008365 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008366 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008367 break;
8368
Michal Vasko03ff5a72019-09-11 13:49:33 +02008369 case LYXP_TOKEN_NAMETEST:
Michal Vaskod3678892020-05-21 10:06:58 +02008370 /* evaluate NameTest Predicate* */
Michal Vasko49fec8e2022-05-24 10:28:33 +02008371 rc = eval_name_test_with_predicate(exp, tok_idx, axis, all_desc, set, options);
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008372 if (rc == LY_ENOT) {
8373 assert(options & LYXP_SCNODE_ALL);
8374 /* skip the rest of this path */
8375 rc = LY_SUCCESS;
8376 scnode_skip_path = 1;
8377 options |= LYXP_SKIP_EXPR;
8378 }
8379 LY_CHECK_GOTO(rc, cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02008380 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008381
Michal Vaskod3678892020-05-21 10:06:58 +02008382 case LYXP_TOKEN_NODETYPE:
8383 /* evaluate NodeType Predicate* */
Michal Vasko49fec8e2022-05-24 10:28:33 +02008384 rc = eval_node_type_with_predicate(exp, tok_idx, axis, all_desc, set, options);
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008385 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008386 break;
8387
8388 default:
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008389 LOGINT(set->ctx);
8390 rc = LY_EINT;
8391 goto cleanup;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008392 }
Michal Vasko004d3152020-06-11 19:59:22 +02008393 } while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008394
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008395cleanup:
8396 if (scnode_skip_path) {
8397 options &= ~LYXP_SKIP_EXPR;
8398 }
8399 return rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008400}
8401
8402/**
8403 * @brief Evaluate AbsoluteLocationPath. Logs directly on error.
8404 *
8405 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
8406 *
8407 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008408 * @param[in] tok_idx Position in the expression @p exp.
aPiecek8b0cc152021-05-31 16:40:31 +02008409 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008410 * @param[in] options XPath options.
8411 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8412 */
8413static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02008414eval_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 +02008415{
Radek Krejci857189e2020-09-01 13:26:36 +02008416 ly_bool all_desc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008417
aPiecek8b0cc152021-05-31 16:40:31 +02008418 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008419 /* no matter what tokens follow, we need to be at the root */
Michal Vaskob0099a92020-08-31 14:55:23 +02008420 LY_CHECK_RET(moveto_root(set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008421 }
8422
8423 /* '/' RelativeLocationPath? */
Michal Vasko004d3152020-06-11 19:59:22 +02008424 if (exp->tok_len[*tok_idx] == 1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008425 /* evaluate '/' - deferred */
8426 all_desc = 0;
aPiecek8b0cc152021-05-31 16:40:31 +02008427 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008428 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008429 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008430
Michal Vasko004d3152020-06-11 19:59:22 +02008431 if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008432 return LY_SUCCESS;
8433 }
Michal Vasko004d3152020-06-11 19:59:22 +02008434 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008435 case LYXP_TOKEN_DOT:
8436 case LYXP_TOKEN_DDOT:
Michal Vasko49fec8e2022-05-24 10:28:33 +02008437 case LYXP_TOKEN_AXISNAME:
Michal Vasko03ff5a72019-09-11 13:49:33 +02008438 case LYXP_TOKEN_AT:
8439 case LYXP_TOKEN_NAMETEST:
8440 case LYXP_TOKEN_NODETYPE:
Michal Vaskob0099a92020-08-31 14:55:23 +02008441 LY_CHECK_RET(eval_relative_location_path(exp, tok_idx, all_desc, set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008442 break;
8443 default:
8444 break;
8445 }
8446
Michal Vasko03ff5a72019-09-11 13:49:33 +02008447 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02008448 /* '//' RelativeLocationPath */
Michal Vasko03ff5a72019-09-11 13:49:33 +02008449 /* evaluate '//' - deferred so as not to waste memory by remembering all the nodes */
8450 all_desc = 1;
aPiecek8b0cc152021-05-31 16:40:31 +02008451 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008452 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008453 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008454
Michal Vaskob0099a92020-08-31 14:55:23 +02008455 LY_CHECK_RET(eval_relative_location_path(exp, tok_idx, all_desc, set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008456 }
8457
8458 return LY_SUCCESS;
8459}
8460
8461/**
8462 * @brief Evaluate FunctionCall. Logs directly on error.
8463 *
Michal Vaskod3678892020-05-21 10:06:58 +02008464 * [11] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02008465 *
8466 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008467 * @param[in] tok_idx Position in the expression @p exp.
aPiecek8b0cc152021-05-31 16:40:31 +02008468 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008469 * @param[in] options XPath options.
8470 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8471 */
8472static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02008473eval_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 +02008474{
8475 LY_ERR rc;
Michal Vasko69730152020-10-09 16:30:07 +02008476
Michal Vaskodd528af2022-08-08 14:35:07 +02008477 LY_ERR (*xpath_func)(struct lyxp_set **, uint32_t, struct lyxp_set *, uint32_t) = NULL;
8478 uint32_t arg_count = 0, i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008479 struct lyxp_set **args = NULL, **args_aux;
8480
aPiecek8b0cc152021-05-31 16:40:31 +02008481 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008482 /* FunctionName */
Michal Vasko004d3152020-06-11 19:59:22 +02008483 switch (exp->tok_len[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008484 case 3:
Michal Vasko004d3152020-06-11 19:59:22 +02008485 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008486 xpath_func = &xpath_not;
Michal Vasko004d3152020-06-11 19:59:22 +02008487 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008488 xpath_func = &xpath_sum;
8489 }
8490 break;
8491 case 4:
Michal Vasko004d3152020-06-11 19:59:22 +02008492 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008493 xpath_func = &xpath_lang;
Michal Vasko004d3152020-06-11 19:59:22 +02008494 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008495 xpath_func = &xpath_last;
Michal Vasko004d3152020-06-11 19:59:22 +02008496 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008497 xpath_func = &xpath_name;
Michal Vasko004d3152020-06-11 19:59:22 +02008498 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008499 xpath_func = &xpath_true;
8500 }
8501 break;
8502 case 5:
Michal Vasko004d3152020-06-11 19:59:22 +02008503 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008504 xpath_func = &xpath_count;
Michal Vasko004d3152020-06-11 19:59:22 +02008505 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008506 xpath_func = &xpath_false;
Michal Vasko004d3152020-06-11 19:59:22 +02008507 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008508 xpath_func = &xpath_floor;
Michal Vasko004d3152020-06-11 19:59:22 +02008509 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008510 xpath_func = &xpath_round;
Michal Vasko004d3152020-06-11 19:59:22 +02008511 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008512 xpath_func = &xpath_deref;
8513 }
8514 break;
8515 case 6:
Michal Vasko004d3152020-06-11 19:59:22 +02008516 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008517 xpath_func = &xpath_concat;
Michal Vasko004d3152020-06-11 19:59:22 +02008518 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008519 xpath_func = &xpath_number;
Michal Vasko004d3152020-06-11 19:59:22 +02008520 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008521 xpath_func = &xpath_string;
8522 }
8523 break;
8524 case 7:
Michal Vasko004d3152020-06-11 19:59:22 +02008525 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008526 xpath_func = &xpath_boolean;
Michal Vasko004d3152020-06-11 19:59:22 +02008527 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008528 xpath_func = &xpath_ceiling;
Michal Vasko004d3152020-06-11 19:59:22 +02008529 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008530 xpath_func = &xpath_current;
8531 }
8532 break;
8533 case 8:
Michal Vasko004d3152020-06-11 19:59:22 +02008534 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008535 xpath_func = &xpath_contains;
Michal Vasko004d3152020-06-11 19:59:22 +02008536 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008537 xpath_func = &xpath_position;
Michal Vasko004d3152020-06-11 19:59:22 +02008538 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "re-match", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008539 xpath_func = &xpath_re_match;
8540 }
8541 break;
8542 case 9:
Michal Vasko004d3152020-06-11 19:59:22 +02008543 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008544 xpath_func = &xpath_substring;
Michal Vasko004d3152020-06-11 19:59:22 +02008545 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008546 xpath_func = &xpath_translate;
8547 }
8548 break;
8549 case 10:
Michal Vasko004d3152020-06-11 19:59:22 +02008550 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008551 xpath_func = &xpath_local_name;
Michal Vasko004d3152020-06-11 19:59:22 +02008552 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008553 xpath_func = &xpath_enum_value;
Michal Vasko004d3152020-06-11 19:59:22 +02008554 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008555 xpath_func = &xpath_bit_is_set;
8556 }
8557 break;
8558 case 11:
Michal Vasko004d3152020-06-11 19:59:22 +02008559 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008560 xpath_func = &xpath_starts_with;
8561 }
8562 break;
8563 case 12:
Michal Vasko004d3152020-06-11 19:59:22 +02008564 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008565 xpath_func = &xpath_derived_from;
8566 }
8567 break;
8568 case 13:
Michal Vasko004d3152020-06-11 19:59:22 +02008569 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008570 xpath_func = &xpath_namespace_uri;
Michal Vasko004d3152020-06-11 19:59:22 +02008571 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string-length", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008572 xpath_func = &xpath_string_length;
8573 }
8574 break;
8575 case 15:
Michal Vasko004d3152020-06-11 19:59:22 +02008576 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008577 xpath_func = &xpath_normalize_space;
Michal Vasko004d3152020-06-11 19:59:22 +02008578 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-after", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008579 xpath_func = &xpath_substring_after;
8580 }
8581 break;
8582 case 16:
Michal Vasko004d3152020-06-11 19:59:22 +02008583 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008584 xpath_func = &xpath_substring_before;
8585 }
8586 break;
8587 case 20:
Michal Vasko004d3152020-06-11 19:59:22 +02008588 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from-or-self", 20)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008589 xpath_func = &xpath_derived_from_or_self;
8590 }
8591 break;
8592 }
8593
8594 if (!xpath_func) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01008595 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 +02008596 return LY_EVALID;
8597 }
8598 }
8599
aPiecek8b0cc152021-05-31 16:40:31 +02008600 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008601 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008602 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008603
8604 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02008605 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
aPiecek8b0cc152021-05-31 16:40:31 +02008606 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008607 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008608 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008609
8610 /* ( Expr ( ',' Expr )* )? */
Michal Vasko004d3152020-06-11 19:59:22 +02008611 if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) {
aPiecek8b0cc152021-05-31 16:40:31 +02008612 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008613 args = malloc(sizeof *args);
8614 LY_CHECK_ERR_GOTO(!args, LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
8615 arg_count = 1;
8616 args[0] = set_copy(set);
8617 if (!args[0]) {
8618 rc = LY_EMEM;
8619 goto cleanup;
8620 }
8621
Michal Vasko004d3152020-06-11 19:59:22 +02008622 rc = eval_expr_select(exp, tok_idx, 0, args[0], options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008623 LY_CHECK_GOTO(rc, cleanup);
8624 } else {
aPiecek8b0cc152021-05-31 16:40:31 +02008625 rc = eval_expr_select(exp, tok_idx, 0, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008626 LY_CHECK_GOTO(rc, cleanup);
8627 }
8628 }
Michal Vasko004d3152020-06-11 19:59:22 +02008629 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) {
aPiecek8b0cc152021-05-31 16:40:31 +02008630 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008631 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008632 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008633
aPiecek8b0cc152021-05-31 16:40:31 +02008634 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008635 ++arg_count;
8636 args_aux = realloc(args, arg_count * sizeof *args);
8637 LY_CHECK_ERR_GOTO(!args_aux, arg_count--; LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
8638 args = args_aux;
8639 args[arg_count - 1] = set_copy(set);
8640 if (!args[arg_count - 1]) {
8641 rc = LY_EMEM;
8642 goto cleanup;
8643 }
8644
Michal Vasko004d3152020-06-11 19:59:22 +02008645 rc = eval_expr_select(exp, tok_idx, 0, args[arg_count - 1], options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008646 LY_CHECK_GOTO(rc, cleanup);
8647 } else {
aPiecek8b0cc152021-05-31 16:40:31 +02008648 rc = eval_expr_select(exp, tok_idx, 0, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008649 LY_CHECK_GOTO(rc, cleanup);
8650 }
8651 }
8652
8653 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02008654 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
aPiecek8b0cc152021-05-31 16:40:31 +02008655 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008656 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008657 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008658
aPiecek8b0cc152021-05-31 16:40:31 +02008659 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008660 /* evaluate function */
8661 rc = xpath_func(args, arg_count, set, options);
8662
8663 if (options & LYXP_SCNODE_ALL) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008664 /* merge all nodes from arg evaluations */
8665 for (i = 0; i < arg_count; ++i) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008666 set_scnode_clear_ctx(args[i], LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008667 lyxp_set_scnode_merge(set, args[i]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008668 }
8669 }
8670 } else {
8671 rc = LY_SUCCESS;
8672 }
8673
8674cleanup:
8675 for (i = 0; i < arg_count; ++i) {
8676 lyxp_set_free(args[i]);
8677 }
8678 free(args);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008679 return rc;
8680}
8681
8682/**
8683 * @brief Evaluate Number. Logs directly on error.
8684 *
8685 * @param[in] ctx Context for errors.
8686 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008687 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008688 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8689 * @return LY_ERR
8690 */
8691static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02008692eval_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 +02008693{
8694 long double num;
8695 char *endptr;
8696
8697 if (set) {
8698 errno = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02008699 num = strtold(&exp->expr[exp->tok_pos[*tok_idx]], &endptr);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008700 if (errno) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01008701 LOGVAL(ctx, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]);
8702 LOGVAL(ctx, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double (%s).",
Michal Vasko69730152020-10-09 16:30:07 +02008703 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]], strerror(errno));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008704 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02008705 } else if (endptr - &exp->expr[exp->tok_pos[*tok_idx]] != exp->tok_len[*tok_idx]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01008706 LOGVAL(ctx, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]);
8707 LOGVAL(ctx, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double.",
Michal Vasko69730152020-10-09 16:30:07 +02008708 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008709 return LY_EVALID;
8710 }
8711
8712 set_fill_number(set, num);
8713 }
8714
8715 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008716 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008717 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008718 return LY_SUCCESS;
8719}
8720
aPiecekdf23eee2021-10-07 12:21:50 +02008721LY_ERR
Michal Vasko90189962023-02-28 12:10:34 +01008722lyxp_vars_find(const struct ly_ctx *ctx, const struct lyxp_var *vars, const char *name, size_t name_len,
8723 struct lyxp_var **var)
aPiecekdf23eee2021-10-07 12:21:50 +02008724{
aPiecekdf23eee2021-10-07 12:21:50 +02008725 LY_ARRAY_COUNT_TYPE u;
8726
Michal Vasko90189962023-02-28 12:10:34 +01008727 assert(name);
aPiecekdf23eee2021-10-07 12:21:50 +02008728
Michal Vasko90189962023-02-28 12:10:34 +01008729 if (!name_len) {
8730 name_len = strlen(name);
8731 }
aPiecekdf23eee2021-10-07 12:21:50 +02008732
8733 LY_ARRAY_FOR(vars, u) {
8734 if (!strncmp(vars[u].name, name, name_len)) {
Michal Vasko90189962023-02-28 12:10:34 +01008735 if (var) {
8736 *var = (struct lyxp_var *)&vars[u];
8737 }
8738 return LY_SUCCESS;
aPiecekdf23eee2021-10-07 12:21:50 +02008739 }
8740 }
8741
Michal Vasko90189962023-02-28 12:10:34 +01008742 if (ctx) {
8743 LOGERR(ctx, LY_ENOTFOUND, "Variable \"%.*s\" not defined.", (int)name_len, name);
aPiecekdf23eee2021-10-07 12:21:50 +02008744 }
Michal Vasko90189962023-02-28 12:10:34 +01008745 return LY_ENOTFOUND;
aPiecekdf23eee2021-10-07 12:21:50 +02008746}
8747
Michal Vasko03ff5a72019-09-11 13:49:33 +02008748/**
aPiecekfba75362021-10-07 12:39:48 +02008749 * @brief Evaluate VariableReference.
8750 *
8751 * @param[in] exp Parsed XPath expression.
8752 * @param[in] tok_idx Position in the expression @p exp.
8753 * @param[in] vars [Sized array](@ref sizedarrays) of XPath variables.
8754 * @param[in,out] set Context and result set.
8755 * @param[in] options XPath options.
8756 * @return LY_ERR value.
8757 */
8758static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02008759eval_variable_reference(const struct lyxp_expr *exp, uint32_t *tok_idx, struct lyxp_set *set, uint32_t options)
aPiecekfba75362021-10-07 12:39:48 +02008760{
8761 LY_ERR ret;
8762 const char *name;
8763 struct lyxp_var *var;
aPiecekfba75362021-10-07 12:39:48 +02008764 struct lyxp_expr *tokens = NULL;
Michal Vaskodd528af2022-08-08 14:35:07 +02008765 uint32_t token_index, name_len;
aPiecekfba75362021-10-07 12:39:48 +02008766
Michal Vasko49fec8e2022-05-24 10:28:33 +02008767 /* find out the name and value of the variable */
aPiecekfba75362021-10-07 12:39:48 +02008768 name = &exp->expr[exp->tok_pos[*tok_idx]];
Michal Vaskoe68b5402022-07-29 14:58:15 +02008769 name_len = exp->tok_len[*tok_idx];
Michal Vasko90189962023-02-28 12:10:34 +01008770 ret = lyxp_vars_find(set->ctx, set->vars, name, name_len, &var);
8771 LY_CHECK_RET(ret);
aPiecekfba75362021-10-07 12:39:48 +02008772
Michal Vasko49fec8e2022-05-24 10:28:33 +02008773 /* parse value */
aPiecekfba75362021-10-07 12:39:48 +02008774 ret = lyxp_expr_parse(set->ctx, var->value, 0, 1, &tokens);
8775 LY_CHECK_GOTO(ret, cleanup);
8776
Michal Vasko49fec8e2022-05-24 10:28:33 +02008777 /* evaluate value */
aPiecekfba75362021-10-07 12:39:48 +02008778 token_index = 0;
8779 ret = eval_expr_select(tokens, &token_index, 0, set, options);
8780 LY_CHECK_GOTO(ret, cleanup);
8781
8782cleanup:
8783 lyxp_expr_free(set->ctx, tokens);
8784
8785 return ret;
8786}
8787
8788/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02008789 * @brief Evaluate PathExpr. Logs directly on error.
8790 *
Michal Vaskod3678892020-05-21 10:06:58 +02008791 * [12] PathExpr ::= LocationPath | PrimaryExpr Predicate*
Michal Vasko03ff5a72019-09-11 13:49:33 +02008792 * | PrimaryExpr Predicate* '/' RelativeLocationPath
8793 * | PrimaryExpr Predicate* '//' RelativeLocationPath
8794 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
aPiecekfba75362021-10-07 12:39:48 +02008795 * [10] PrimaryExpr ::= VariableReference | '(' Expr ')' | Literal | Number | FunctionCall
Michal Vasko03ff5a72019-09-11 13:49:33 +02008796 *
8797 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008798 * @param[in] tok_idx Position in the expression @p exp.
aPiecek8b0cc152021-05-31 16:40:31 +02008799 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008800 * @param[in] options XPath options.
8801 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8802 */
8803static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02008804eval_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 +02008805{
Michal Vasko49fec8e2022-05-24 10:28:33 +02008806 ly_bool all_desc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008807 LY_ERR rc;
8808
Michal Vasko004d3152020-06-11 19:59:22 +02008809 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008810 case LYXP_TOKEN_PAR1:
8811 /* '(' Expr ')' */
8812
8813 /* '(' */
aPiecek8b0cc152021-05-31 16:40:31 +02008814 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008815 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008816 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008817
8818 /* Expr */
Michal Vasko004d3152020-06-11 19:59:22 +02008819 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008820 LY_CHECK_RET(rc);
8821
8822 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02008823 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
aPiecek8b0cc152021-05-31 16:40:31 +02008824 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008825 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008826 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008827
Michal Vasko03ff5a72019-09-11 13:49:33 +02008828 goto predicate;
8829
8830 case LYXP_TOKEN_DOT:
8831 case LYXP_TOKEN_DDOT:
Michal Vasko49fec8e2022-05-24 10:28:33 +02008832 case LYXP_TOKEN_AXISNAME:
Michal Vasko03ff5a72019-09-11 13:49:33 +02008833 case LYXP_TOKEN_AT:
8834 case LYXP_TOKEN_NAMETEST:
8835 case LYXP_TOKEN_NODETYPE:
8836 /* RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02008837 rc = eval_relative_location_path(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008838 LY_CHECK_RET(rc);
8839 break;
8840
aPiecekfba75362021-10-07 12:39:48 +02008841 case LYXP_TOKEN_VARREF:
8842 /* VariableReference */
8843 rc = eval_variable_reference(exp, tok_idx, set, options);
8844 LY_CHECK_RET(rc);
8845 ++(*tok_idx);
8846
aPiecekfba75362021-10-07 12:39:48 +02008847 goto predicate;
8848
Michal Vasko03ff5a72019-09-11 13:49:33 +02008849 case LYXP_TOKEN_FUNCNAME:
8850 /* FunctionCall */
aPiecek8b0cc152021-05-31 16:40:31 +02008851 rc = eval_function_call(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008852 LY_CHECK_RET(rc);
8853
Michal Vasko03ff5a72019-09-11 13:49:33 +02008854 goto predicate;
8855
Michal Vasko3e48bf32020-06-01 08:39:07 +02008856 case LYXP_TOKEN_OPER_PATH:
8857 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +02008858 /* AbsoluteLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02008859 rc = eval_absolute_location_path(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008860 LY_CHECK_RET(rc);
8861 break;
8862
8863 case LYXP_TOKEN_LITERAL:
8864 /* Literal */
aPiecek8b0cc152021-05-31 16:40:31 +02008865 if ((options & LYXP_SKIP_EXPR) || (options & LYXP_SCNODE_ALL)) {
8866 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008867 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008868 }
Michal Vasko004d3152020-06-11 19:59:22 +02008869 eval_literal(exp, tok_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008870 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02008871 eval_literal(exp, tok_idx, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008872 }
8873
Michal Vasko03ff5a72019-09-11 13:49:33 +02008874 goto predicate;
8875
8876 case LYXP_TOKEN_NUMBER:
8877 /* Number */
aPiecek8b0cc152021-05-31 16:40:31 +02008878 if ((options & LYXP_SKIP_EXPR) || (options & LYXP_SCNODE_ALL)) {
8879 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008880 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008881 }
Michal Vasko004d3152020-06-11 19:59:22 +02008882 rc = eval_number(NULL, exp, tok_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008883 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02008884 rc = eval_number(set->ctx, exp, tok_idx, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008885 }
8886 LY_CHECK_RET(rc);
8887
Michal Vasko03ff5a72019-09-11 13:49:33 +02008888 goto predicate;
8889
8890 default:
Michal Vasko49fec8e2022-05-24 10:28:33 +02008891 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 +02008892 return LY_EVALID;
8893 }
8894
8895 return LY_SUCCESS;
8896
8897predicate:
8898 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02008899 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02008900 rc = eval_predicate(exp, tok_idx, set, options, LYXP_AXIS_CHILD);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008901 LY_CHECK_RET(rc);
8902 }
8903
8904 /* ('/' or '//') RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02008905 if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008906
8907 /* evaluate '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02008908 if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008909 all_desc = 0;
8910 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008911 all_desc = 1;
8912 }
8913
aPiecek8b0cc152021-05-31 16:40:31 +02008914 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008915 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008916 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008917
Michal Vasko004d3152020-06-11 19:59:22 +02008918 rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008919 LY_CHECK_RET(rc);
8920 }
8921
8922 return LY_SUCCESS;
8923}
8924
8925/**
8926 * @brief Evaluate UnionExpr. Logs directly on error.
8927 *
Michal Vaskod3678892020-05-21 10:06:58 +02008928 * [20] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008929 *
8930 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008931 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008932 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008933 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008934 * @param[in] options XPath options.
8935 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8936 */
8937static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02008938eval_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 +02008939{
8940 LY_ERR rc = LY_SUCCESS;
8941 struct lyxp_set orig_set, set2;
Michal Vaskodd528af2022-08-08 14:35:07 +02008942 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008943
8944 assert(repeat);
8945
8946 set_init(&orig_set, set);
8947 set_init(&set2, set);
8948
8949 set_fill_set(&orig_set, set);
8950
Michal Vasko004d3152020-06-11 19:59:22 +02008951 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008952 LY_CHECK_GOTO(rc, cleanup);
8953
8954 /* ('|' PathExpr)* */
8955 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008956 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_UNI);
aPiecek8b0cc152021-05-31 16:40:31 +02008957 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008958 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008959 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008960
aPiecek8b0cc152021-05-31 16:40:31 +02008961 if (options & LYXP_SKIP_EXPR) {
8962 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008963 LY_CHECK_GOTO(rc, cleanup);
8964 continue;
8965 }
8966
8967 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008968 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008969 LY_CHECK_GOTO(rc, cleanup);
8970
8971 /* eval */
8972 if (options & LYXP_SCNODE_ALL) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01008973 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008974 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008975 rc = moveto_union(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008976 LY_CHECK_GOTO(rc, cleanup);
8977 }
8978 }
8979
8980cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008981 lyxp_set_free_content(&orig_set);
8982 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008983 return rc;
8984}
8985
8986/**
8987 * @brief Evaluate UnaryExpr. Logs directly on error.
8988 *
Michal Vaskod3678892020-05-21 10:06:58 +02008989 * [19] UnaryExpr ::= UnionExpr | '-' UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008990 *
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_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 +02009000{
9001 LY_ERR rc;
Michal Vaskodd528af2022-08-08 14:35:07 +02009002 uint32_t this_op, i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009003
9004 assert(repeat);
9005
9006 /* ('-')+ */
Michal Vasko004d3152020-06-11 19:59:22 +02009007 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009008 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02009009 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 +02009010
aPiecek8b0cc152021-05-31 16:40:31 +02009011 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02009012 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02009013 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009014 }
9015
Michal Vasko004d3152020-06-11 19:59:22 +02009016 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNARY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009017 LY_CHECK_RET(rc);
9018
aPiecek8b0cc152021-05-31 16:40:31 +02009019 if (!(options & LYXP_SKIP_EXPR) && (repeat % 2)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02009020 if (options & LYXP_SCNODE_ALL) {
9021 warn_operands(set->ctx, set, NULL, 1, exp->expr, exp->tok_pos[this_op]);
9022 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009023 rc = moveto_op_math(set, NULL, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009024 LY_CHECK_RET(rc);
9025 }
9026 }
9027
9028 return LY_SUCCESS;
9029}
9030
9031/**
9032 * @brief Evaluate MultiplicativeExpr. Logs directly on error.
9033 *
Michal Vaskod3678892020-05-21 10:06:58 +02009034 * [18] MultiplicativeExpr ::= UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02009035 * | MultiplicativeExpr '*' UnaryExpr
9036 * | MultiplicativeExpr 'div' UnaryExpr
9037 * | MultiplicativeExpr 'mod' UnaryExpr
9038 *
9039 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02009040 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009041 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02009042 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009043 * @param[in] options XPath options.
9044 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
9045 */
9046static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02009047eval_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 +02009048 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02009049{
9050 LY_ERR rc;
Michal Vaskodd528af2022-08-08 14:35:07 +02009051 uint32_t i, this_op;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009052 struct lyxp_set orig_set, set2;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009053
9054 assert(repeat);
9055
9056 set_init(&orig_set, set);
9057 set_init(&set2, set);
9058
9059 set_fill_set(&orig_set, set);
9060
Michal Vasko004d3152020-06-11 19:59:22 +02009061 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009062 LY_CHECK_GOTO(rc, cleanup);
9063
9064 /* ('*' / 'div' / 'mod' UnaryExpr)* */
9065 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02009066 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009067
Michal Vasko004d3152020-06-11 19:59:22 +02009068 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH);
aPiecek8b0cc152021-05-31 16:40:31 +02009069 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02009070 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02009071 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009072
aPiecek8b0cc152021-05-31 16:40:31 +02009073 if (options & LYXP_SKIP_EXPR) {
9074 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009075 LY_CHECK_GOTO(rc, cleanup);
9076 continue;
9077 }
9078
9079 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02009080 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009081 LY_CHECK_GOTO(rc, cleanup);
9082
9083 /* eval */
9084 if (options & LYXP_SCNODE_ALL) {
9085 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01009086 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02009087 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009088 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009089 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009090 LY_CHECK_GOTO(rc, cleanup);
9091 }
9092 }
9093
9094cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02009095 lyxp_set_free_content(&orig_set);
9096 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009097 return rc;
9098}
9099
9100/**
9101 * @brief Evaluate AdditiveExpr. Logs directly on error.
9102 *
Michal Vaskod3678892020-05-21 10:06:58 +02009103 * [17] AdditiveExpr ::= MultiplicativeExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02009104 * | AdditiveExpr '+' MultiplicativeExpr
9105 * | AdditiveExpr '-' MultiplicativeExpr
9106 *
9107 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02009108 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009109 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02009110 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009111 * @param[in] options XPath options.
9112 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
9113 */
9114static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02009115eval_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 +02009116{
9117 LY_ERR rc;
Michal Vaskodd528af2022-08-08 14:35:07 +02009118 uint32_t i, this_op;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009119 struct lyxp_set orig_set, set2;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009120
9121 assert(repeat);
9122
9123 set_init(&orig_set, set);
9124 set_init(&set2, set);
9125
9126 set_fill_set(&orig_set, set);
9127
Michal Vasko004d3152020-06-11 19:59:22 +02009128 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009129 LY_CHECK_GOTO(rc, cleanup);
9130
9131 /* ('+' / '-' MultiplicativeExpr)* */
9132 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02009133 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009134
Michal Vasko004d3152020-06-11 19:59:22 +02009135 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009136 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02009137 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02009138 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009139
aPiecek8b0cc152021-05-31 16:40:31 +02009140 if (options & LYXP_SKIP_EXPR) {
9141 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009142 LY_CHECK_GOTO(rc, cleanup);
9143 continue;
9144 }
9145
9146 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02009147 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009148 LY_CHECK_GOTO(rc, cleanup);
9149
9150 /* eval */
9151 if (options & LYXP_SCNODE_ALL) {
9152 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01009153 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02009154 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009155 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009156 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009157 LY_CHECK_GOTO(rc, cleanup);
9158 }
9159 }
9160
9161cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02009162 lyxp_set_free_content(&orig_set);
9163 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009164 return rc;
9165}
9166
9167/**
9168 * @brief Evaluate RelationalExpr. Logs directly on error.
9169 *
Michal Vaskod3678892020-05-21 10:06:58 +02009170 * [16] RelationalExpr ::= AdditiveExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02009171 * | RelationalExpr '<' AdditiveExpr
9172 * | RelationalExpr '>' AdditiveExpr
9173 * | RelationalExpr '<=' AdditiveExpr
9174 * | RelationalExpr '>=' AdditiveExpr
9175 *
9176 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02009177 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009178 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02009179 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009180 * @param[in] options XPath options.
9181 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
9182 */
9183static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02009184eval_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 +02009185{
9186 LY_ERR rc;
Michal Vaskodd528af2022-08-08 14:35:07 +02009187 uint32_t i, this_op;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009188 struct lyxp_set orig_set, set2;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009189
9190 assert(repeat);
9191
9192 set_init(&orig_set, set);
9193 set_init(&set2, set);
9194
9195 set_fill_set(&orig_set, set);
9196
Michal Vasko004d3152020-06-11 19:59:22 +02009197 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009198 LY_CHECK_GOTO(rc, cleanup);
9199
9200 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
9201 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02009202 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009203
Michal Vasko004d3152020-06-11 19:59:22 +02009204 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_COMP);
aPiecek8b0cc152021-05-31 16:40:31 +02009205 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02009206 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02009207 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009208
aPiecek8b0cc152021-05-31 16:40:31 +02009209 if (options & LYXP_SKIP_EXPR) {
9210 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009211 LY_CHECK_GOTO(rc, cleanup);
9212 continue;
9213 }
9214
9215 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02009216 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009217 LY_CHECK_GOTO(rc, cleanup);
9218
9219 /* eval */
9220 if (options & LYXP_SCNODE_ALL) {
9221 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01009222 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02009223 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009224 } else {
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02009225 ly_bool result;
9226
9227 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], &result);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009228 LY_CHECK_GOTO(rc, cleanup);
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02009229 set_fill_boolean(set, result);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009230 }
9231 }
9232
9233cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02009234 lyxp_set_free_content(&orig_set);
9235 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009236 return rc;
9237}
9238
9239/**
9240 * @brief Evaluate EqualityExpr. Logs directly on error.
9241 *
Michal Vaskod3678892020-05-21 10:06:58 +02009242 * [15] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02009243 * | EqualityExpr '!=' RelationalExpr
9244 *
9245 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02009246 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009247 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02009248 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009249 * @param[in] options XPath options.
9250 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
9251 */
9252static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02009253eval_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 +02009254{
9255 LY_ERR rc;
Michal Vaskodd528af2022-08-08 14:35:07 +02009256 uint32_t i, this_op;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009257 struct lyxp_set orig_set, set2;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009258
9259 assert(repeat);
9260
9261 set_init(&orig_set, set);
9262 set_init(&set2, set);
9263
9264 set_fill_set(&orig_set, set);
9265
Michal Vasko004d3152020-06-11 19:59:22 +02009266 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009267 LY_CHECK_GOTO(rc, cleanup);
9268
9269 /* ('=' / '!=' RelationalExpr)* */
9270 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02009271 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009272
Michal Vasko004d3152020-06-11 19:59:22 +02009273 assert((exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_EQUAL) || (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_NEQUAL));
aPiecek8b0cc152021-05-31 16:40:31 +02009274 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02009275 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02009276 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009277
aPiecek8b0cc152021-05-31 16:40:31 +02009278 if (options & LYXP_SKIP_EXPR) {
9279 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009280 LY_CHECK_GOTO(rc, cleanup);
9281 continue;
9282 }
9283
9284 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02009285 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009286 LY_CHECK_GOTO(rc, cleanup);
9287
9288 /* eval */
9289 if (options & LYXP_SCNODE_ALL) {
9290 warn_operands(set->ctx, set, &set2, 0, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vasko004d3152020-06-11 19:59:22 +02009291 warn_equality_value(exp, set, *tok_idx - 1, this_op - 1, *tok_idx - 1);
9292 warn_equality_value(exp, &set2, this_op - 1, this_op - 1, *tok_idx - 1);
Michal Vaskoecd62de2019-11-13 12:35:11 +01009293 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02009294 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009295 } else {
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02009296 ly_bool result;
9297
9298 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], &result);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009299 LY_CHECK_GOTO(rc, cleanup);
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02009300 set_fill_boolean(set, result);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009301 }
9302 }
9303
9304cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02009305 lyxp_set_free_content(&orig_set);
9306 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009307 return rc;
9308}
9309
9310/**
9311 * @brief Evaluate AndExpr. Logs directly on error.
9312 *
Michal Vaskod3678892020-05-21 10:06:58 +02009313 * [14] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02009314 *
9315 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02009316 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009317 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02009318 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009319 * @param[in] options XPath options.
9320 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
9321 */
9322static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02009323eval_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 +02009324{
9325 LY_ERR rc;
9326 struct lyxp_set orig_set, set2;
Michal Vaskodd528af2022-08-08 14:35:07 +02009327 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009328
9329 assert(repeat);
9330
9331 set_init(&orig_set, set);
9332 set_init(&set2, set);
9333
9334 set_fill_set(&orig_set, set);
9335
Michal Vasko004d3152020-06-11 19:59:22 +02009336 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009337 LY_CHECK_GOTO(rc, cleanup);
9338
Michal Vasko3ff82dd2023-05-09 14:09:21 +02009339 if (!(options & LYXP_SKIP_EXPR)) {
9340 if (options & LYXP_SCNODE_ALL) {
9341 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
9342 } else {
9343 /* cast to boolean, we know that will be the final result */
9344 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
9345 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02009346 }
9347
9348 /* ('and' EqualityExpr)* */
9349 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02009350 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG);
aPiecek8b0cc152021-05-31 16:40:31 +02009351 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, ((options & LYXP_SKIP_EXPR) || !set->val.bln ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02009352 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02009353 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009354
9355 /* lazy evaluation */
aPiecek8b0cc152021-05-31 16:40:31 +02009356 if ((options & LYXP_SKIP_EXPR) || ((set->type == LYXP_SET_BOOLEAN) && !set->val.bln)) {
9357 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009358 LY_CHECK_GOTO(rc, cleanup);
9359 continue;
9360 }
9361
9362 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02009363 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009364 LY_CHECK_GOTO(rc, cleanup);
9365
9366 /* eval - just get boolean value actually */
9367 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vasko1a09b212021-05-06 13:00:10 +02009368 set_scnode_clear_ctx(&set2, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskoecd62de2019-11-13 12:35:11 +01009369 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009370 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009371 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009372 set_fill_set(set, &set2);
9373 }
9374 }
9375
9376cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02009377 lyxp_set_free_content(&orig_set);
9378 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009379 return rc;
9380}
9381
9382/**
9383 * @brief Evaluate OrExpr. Logs directly on error.
9384 *
Michal Vaskod3678892020-05-21 10:06:58 +02009385 * [13] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02009386 *
9387 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02009388 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009389 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02009390 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009391 * @param[in] options XPath options.
9392 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
9393 */
9394static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02009395eval_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 +02009396{
9397 LY_ERR rc;
9398 struct lyxp_set orig_set, set2;
Michal Vaskodd528af2022-08-08 14:35:07 +02009399 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009400
9401 assert(repeat);
9402
9403 set_init(&orig_set, set);
9404 set_init(&set2, set);
9405
9406 set_fill_set(&orig_set, set);
9407
Michal Vasko004d3152020-06-11 19:59:22 +02009408 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009409 LY_CHECK_GOTO(rc, cleanup);
9410
Michal Vasko3ff82dd2023-05-09 14:09:21 +02009411 if (!(options & LYXP_SKIP_EXPR)) {
9412 if (options & LYXP_SCNODE_ALL) {
9413 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
9414 } else {
9415 /* cast to boolean, we know that will be the final result */
9416 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
9417 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02009418 }
9419
9420 /* ('or' AndExpr)* */
9421 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02009422 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG);
aPiecek8b0cc152021-05-31 16:40:31 +02009423 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, ((options & LYXP_SKIP_EXPR) || set->val.bln ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02009424 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02009425 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009426
9427 /* lazy evaluation */
aPiecek8b0cc152021-05-31 16:40:31 +02009428 if ((options & LYXP_SKIP_EXPR) || ((set->type == LYXP_SET_BOOLEAN) && set->val.bln)) {
9429 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009430 LY_CHECK_GOTO(rc, cleanup);
9431 continue;
9432 }
9433
9434 set_fill_set(&set2, &orig_set);
9435 /* expr_type cound have been LYXP_EXPR_NONE in all these later calls (except for the first one),
9436 * but it does not matter */
Michal Vasko004d3152020-06-11 19:59:22 +02009437 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009438 LY_CHECK_GOTO(rc, cleanup);
9439
9440 /* eval - just get boolean value actually */
9441 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vasko1a09b212021-05-06 13:00:10 +02009442 set_scnode_clear_ctx(&set2, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskoecd62de2019-11-13 12:35:11 +01009443 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009444 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009445 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009446 set_fill_set(set, &set2);
9447 }
9448 }
9449
9450cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02009451 lyxp_set_free_content(&orig_set);
9452 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009453 return rc;
9454}
9455
9456/**
Michal Vasko004d3152020-06-11 19:59:22 +02009457 * @brief Decide what expression is at the pointer @p tok_idx and evaluate it accordingly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009458 *
9459 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02009460 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009461 * @param[in] etype Expression type to evaluate.
aPiecek8b0cc152021-05-31 16:40:31 +02009462 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009463 * @param[in] options XPath options.
9464 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
9465 */
9466static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02009467eval_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 +02009468 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02009469{
Michal Vaskodd528af2022-08-08 14:35:07 +02009470 uint32_t i, count;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009471 enum lyxp_expr_type next_etype;
9472 LY_ERR rc;
9473
9474 /* process operator repeats */
Michal Vasko004d3152020-06-11 19:59:22 +02009475 if (!exp->repeat[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02009476 next_etype = LYXP_EXPR_NONE;
9477 } else {
9478 /* find etype repeat */
Radek Krejci1e008d22020-08-17 11:37:37 +02009479 for (i = 0; exp->repeat[*tok_idx][i] > etype; ++i) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02009480
9481 /* select one-priority lower because etype expression called us */
9482 if (i) {
Michal Vasko004d3152020-06-11 19:59:22 +02009483 next_etype = exp->repeat[*tok_idx][i - 1];
Michal Vasko03ff5a72019-09-11 13:49:33 +02009484 /* count repeats for that expression */
Radek Krejci1e008d22020-08-17 11:37:37 +02009485 for (count = 0; i && exp->repeat[*tok_idx][i - 1] == next_etype; ++count, --i) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02009486 } else {
9487 next_etype = LYXP_EXPR_NONE;
9488 }
9489 }
9490
9491 /* decide what expression are we parsing based on the repeat */
9492 switch (next_etype) {
9493 case LYXP_EXPR_OR:
Michal Vasko004d3152020-06-11 19:59:22 +02009494 rc = eval_or_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009495 break;
9496 case LYXP_EXPR_AND:
Michal Vasko004d3152020-06-11 19:59:22 +02009497 rc = eval_and_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009498 break;
9499 case LYXP_EXPR_EQUALITY:
Michal Vasko004d3152020-06-11 19:59:22 +02009500 rc = eval_equality_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009501 break;
9502 case LYXP_EXPR_RELATIONAL:
Michal Vasko004d3152020-06-11 19:59:22 +02009503 rc = eval_relational_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009504 break;
9505 case LYXP_EXPR_ADDITIVE:
Michal Vasko004d3152020-06-11 19:59:22 +02009506 rc = eval_additive_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009507 break;
9508 case LYXP_EXPR_MULTIPLICATIVE:
Michal Vasko004d3152020-06-11 19:59:22 +02009509 rc = eval_multiplicative_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009510 break;
9511 case LYXP_EXPR_UNARY:
Michal Vasko004d3152020-06-11 19:59:22 +02009512 rc = eval_unary_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009513 break;
9514 case LYXP_EXPR_UNION:
Michal Vasko004d3152020-06-11 19:59:22 +02009515 rc = eval_union_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009516 break;
9517 case LYXP_EXPR_NONE:
Michal Vasko004d3152020-06-11 19:59:22 +02009518 rc = eval_path_expr(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009519 break;
9520 default:
9521 LOGINT_RET(set->ctx);
9522 }
9523
9524 return rc;
9525}
9526
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009527/**
9528 * @brief Get root type.
9529 *
9530 * @param[in] ctx_node Context node.
9531 * @param[in] ctx_scnode Schema context node.
9532 * @param[in] options XPath options.
9533 * @return Root type.
9534 */
9535static enum lyxp_node_type
Radek Krejci1deb5be2020-08-26 16:43:36 +02009536lyxp_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 +01009537{
Michal Vasko6b26e742020-07-17 15:02:10 +02009538 const struct lysc_node *op;
9539
Michal Vaskoa27245c2022-05-02 09:01:35 +02009540 /* explicit */
9541 if (options & LYXP_ACCESS_TREE_ALL) {
9542 return LYXP_NODE_ROOT;
9543 } else if (options & LYXP_ACCESS_TREE_CONFIG) {
9544 return LYXP_NODE_ROOT_CONFIG;
9545 }
9546
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009547 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009548 /* schema */
Radek Krejci1e008d22020-08-17 11:37:37 +02009549 for (op = ctx_scnode; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent) {}
Michal Vasko6b26e742020-07-17 15:02:10 +02009550
9551 if (op || (options & LYXP_SCNODE)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009552 /* general root that can access everything */
9553 return LYXP_NODE_ROOT;
9554 } else if (!ctx_scnode || (ctx_scnode->flags & LYS_CONFIG_W)) {
9555 /* root context node can access only config data (because we said so, it is unspecified) */
9556 return LYXP_NODE_ROOT_CONFIG;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009557 }
Michal Vasko6b26e742020-07-17 15:02:10 +02009558 return LYXP_NODE_ROOT;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009559 }
9560
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009561 /* data */
Michal Vasko6b26e742020-07-17 15:02:10 +02009562 op = ctx_node ? ctx_node->schema : NULL;
Michal Vaskod989ba02020-08-24 10:59:24 +02009563 for ( ; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent) {}
Michal Vasko6b26e742020-07-17 15:02:10 +02009564
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009565 if (op || !(options & LYXP_SCHEMA)) {
9566 /* general root that can access everything */
9567 return LYXP_NODE_ROOT;
Christian Hoppsb6ecaea2021-02-06 09:45:38 -05009568 } else if (!ctx_node || !ctx_node->schema || (ctx_node->schema->flags & LYS_CONFIG_W)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009569 /* root context node can access only config data (because we said so, it is unspecified) */
9570 return LYXP_NODE_ROOT_CONFIG;
9571 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009572 return LYXP_NODE_ROOT;
9573}
9574
Michal Vasko03ff5a72019-09-11 13:49:33 +02009575LY_ERR
Michal Vasko400e9672021-01-11 13:39:17 +01009576lyxp_eval(const struct ly_ctx *ctx, const struct lyxp_expr *exp, const struct lys_module *cur_mod,
Michal Vaskoa3e92bc2022-07-29 14:56:23 +02009577 LY_VALUE_FORMAT format, void *prefix_data, const struct lyd_node *cur_node, const struct lyd_node *ctx_node,
9578 const struct lyd_node *tree, const struct lyxp_var *vars, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02009579{
Michal Vaskodd528af2022-08-08 14:35:07 +02009580 uint32_t tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009581 LY_ERR rc;
9582
Michal Vasko400e9672021-01-11 13:39:17 +01009583 LY_CHECK_ARG_RET(ctx, ctx, exp, set, LY_EINVAL);
Radek Krejci8df109d2021-04-23 12:19:08 +02009584 if (!cur_mod && ((format == LY_VALUE_SCHEMA) || (format == LY_VALUE_SCHEMA_RESOLVED))) {
Michal Vaskoddd76592022-01-17 13:34:48 +01009585 LOGERR(ctx, LY_EINVAL, "Current module must be set if schema format is used.");
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009586 return LY_EINVAL;
Michal Vasko004d3152020-06-11 19:59:22 +02009587 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02009588
Michal Vaskod3bb12f2020-12-04 14:33:09 +01009589 if (tree) {
9590 /* adjust the pointer to be the first top-level sibling */
9591 while (tree->parent) {
9592 tree = lyd_parent(tree);
9593 }
9594 tree = lyd_first_sibling(tree);
Michal Vaskoddd76592022-01-17 13:34:48 +01009595
Michal Vasko6f78a772022-11-10 08:13:52 +01009596 if (lysc_data_parent(tree->schema)) {
Michal Vaskoddd76592022-01-17 13:34:48 +01009597 /* unable to evaluate absolute paths */
9598 LOGERR(ctx, LY_EINVAL, "Data node \"%s\" has no parent but is not instance of a top-level schema node.",
9599 LYD_NAME(tree));
9600 return LY_EINVAL;
9601 }
Michal Vaskod3bb12f2020-12-04 14:33:09 +01009602 }
9603
Michal Vasko03ff5a72019-09-11 13:49:33 +02009604 /* prepare set for evaluation */
Michal Vasko03ff5a72019-09-11 13:49:33 +02009605 memset(set, 0, sizeof *set);
Michal Vaskod3678892020-05-21 10:06:58 +02009606 set->type = LYXP_SET_NODE_SET;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009607 set->root_type = lyxp_get_root_type(ctx_node, NULL, options);
9608 set_insert_node(set, (struct lyd_node *)ctx_node, 0, ctx_node ? LYXP_NODE_ELEM : set->root_type, 0);
9609
Michal Vasko400e9672021-01-11 13:39:17 +01009610 set->ctx = (struct ly_ctx *)ctx;
Michal Vaskoa3e92bc2022-07-29 14:56:23 +02009611 set->cur_node = cur_node;
9612 for (set->context_op = cur_node ? cur_node->schema : NULL;
Radek Krejci0f969882020-08-21 16:56:47 +02009613 set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF));
9614 set->context_op = set->context_op->parent) {}
Michal Vaskof03ed032020-03-04 13:31:44 +01009615 set->tree = tree;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009616 set->cur_mod = cur_mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009617 set->format = format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009618 set->prefix_data = prefix_data;
aPiecekfba75362021-10-07 12:39:48 +02009619 set->vars = vars;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009620
Radek Krejciddace2c2021-01-08 11:30:56 +01009621 LOG_LOCSET(NULL, set->cur_node, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01009622
Michal Vasko03ff5a72019-09-11 13:49:33 +02009623 /* evaluate */
Michal Vasko004d3152020-06-11 19:59:22 +02009624 rc = eval_expr_select(exp, &tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009625 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02009626 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009627 }
9628
Michal Vasko4a7d4d62021-12-13 17:05:06 +01009629 if (set->cur_node) {
9630 LOG_LOCBACK(0, 1, 0, 0);
9631 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02009632 return rc;
9633}
9634
9635#if 0
9636
9637/* full xml printing of set elements, not used currently */
9638
9639void
9640lyxp_set_print_xml(FILE *f, struct lyxp_set *set)
9641{
9642 uint32_t i;
9643 char *str_num;
9644 struct lyout out;
9645
9646 memset(&out, 0, sizeof out);
9647
9648 out.type = LYOUT_STREAM;
9649 out.method.f = f;
9650
9651 switch (set->type) {
9652 case LYXP_SET_EMPTY:
Michal Vasko5233e962020-08-14 14:26:20 +02009653 ly_print_(&out, "Empty XPath set\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02009654 break;
9655 case LYXP_SET_BOOLEAN:
Michal Vasko5233e962020-08-14 14:26:20 +02009656 ly_print_(&out, "Boolean XPath set:\n");
9657 ly_print_(&out, "%s\n\n", set->value.bool ? "true" : "false");
Michal Vasko03ff5a72019-09-11 13:49:33 +02009658 break;
9659 case LYXP_SET_STRING:
Michal Vasko5233e962020-08-14 14:26:20 +02009660 ly_print_(&out, "String XPath set:\n");
9661 ly_print_(&out, "\"%s\"\n\n", set->value.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009662 break;
9663 case LYXP_SET_NUMBER:
Michal Vasko5233e962020-08-14 14:26:20 +02009664 ly_print_(&out, "Number XPath set:\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02009665
9666 if (isnan(set->value.num)) {
9667 str_num = strdup("NaN");
9668 } else if ((set->value.num == 0) || (set->value.num == -0.0f)) {
9669 str_num = strdup("0");
9670 } else if (isinf(set->value.num) && !signbit(set->value.num)) {
9671 str_num = strdup("Infinity");
9672 } else if (isinf(set->value.num) && signbit(set->value.num)) {
9673 str_num = strdup("-Infinity");
9674 } else if ((long long)set->value.num == set->value.num) {
9675 if (asprintf(&str_num, "%lld", (long long)set->value.num) == -1) {
9676 str_num = NULL;
9677 }
9678 } else {
9679 if (asprintf(&str_num, "%03.1Lf", set->value.num) == -1) {
9680 str_num = NULL;
9681 }
9682 }
9683 if (!str_num) {
9684 LOGMEM;
9685 return;
9686 }
Michal Vasko5233e962020-08-14 14:26:20 +02009687 ly_print_(&out, "%s\n\n", str_num);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009688 free(str_num);
9689 break;
9690 case LYXP_SET_NODE_SET:
Michal Vasko5233e962020-08-14 14:26:20 +02009691 ly_print_(&out, "Node XPath set:\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02009692
9693 for (i = 0; i < set->used; ++i) {
Michal Vasko5233e962020-08-14 14:26:20 +02009694 ly_print_(&out, "%d. ", i + 1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009695 switch (set->node_type[i]) {
9696 case LYXP_NODE_ROOT_ALL:
Michal Vasko5233e962020-08-14 14:26:20 +02009697 ly_print_(&out, "ROOT all\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02009698 break;
9699 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko5233e962020-08-14 14:26:20 +02009700 ly_print_(&out, "ROOT config\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02009701 break;
9702 case LYXP_NODE_ROOT_STATE:
Michal Vasko5233e962020-08-14 14:26:20 +02009703 ly_print_(&out, "ROOT state\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02009704 break;
9705 case LYXP_NODE_ROOT_NOTIF:
Michal Vasko5233e962020-08-14 14:26:20 +02009706 ly_print_(&out, "ROOT notification \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009707 break;
9708 case LYXP_NODE_ROOT_RPC:
Michal Vasko5233e962020-08-14 14:26:20 +02009709 ly_print_(&out, "ROOT rpc \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009710 break;
9711 case LYXP_NODE_ROOT_OUTPUT:
Michal Vasko5233e962020-08-14 14:26:20 +02009712 ly_print_(&out, "ROOT output \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009713 break;
9714 case LYXP_NODE_ELEM:
Michal Vasko5233e962020-08-14 14:26:20 +02009715 ly_print_(&out, "ELEM \"%s\"\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009716 xml_print_node(&out, 1, set->value.nodes[i], 1, LYP_FORMAT);
Michal Vasko5233e962020-08-14 14:26:20 +02009717 ly_print_(&out, "\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02009718 break;
9719 case LYXP_NODE_TEXT:
Michal Vasko5233e962020-08-14 14:26:20 +02009720 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 +02009721 break;
9722 case LYXP_NODE_ATTR:
Michal Vasko5233e962020-08-14 14:26:20 +02009723 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 +02009724 break;
9725 }
9726 }
9727 break;
9728 }
9729}
9730
9731#endif
9732
9733LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009734lyxp_set_cast(struct lyxp_set *set, enum lyxp_set_type target)
Michal Vasko03ff5a72019-09-11 13:49:33 +02009735{
9736 long double num;
9737 char *str;
9738 LY_ERR rc;
9739
9740 if (!set || (set->type == target)) {
9741 return LY_SUCCESS;
9742 }
9743
9744 /* it's not possible to convert anything into a node set */
Michal Vaskod3678892020-05-21 10:06:58 +02009745 assert(target != LYXP_SET_NODE_SET);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009746
9747 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vaskod3678892020-05-21 10:06:58 +02009748 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009749 return LY_EINVAL;
9750 }
9751
9752 /* to STRING */
Michal Vaskod3678892020-05-21 10:06:58 +02009753 if ((target == LYXP_SET_STRING) || ((target == LYXP_SET_NUMBER) && (set->type == LYXP_SET_NODE_SET))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02009754 switch (set->type) {
9755 case LYXP_SET_NUMBER:
9756 if (isnan(set->val.num)) {
9757 set->val.str = strdup("NaN");
9758 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
9759 } else if ((set->val.num == 0) || (set->val.num == -0.0f)) {
9760 set->val.str = strdup("0");
9761 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
9762 } else if (isinf(set->val.num) && !signbit(set->val.num)) {
9763 set->val.str = strdup("Infinity");
9764 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
9765 } else if (isinf(set->val.num) && signbit(set->val.num)) {
9766 set->val.str = strdup("-Infinity");
9767 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
9768 } else if ((long long)set->val.num == set->val.num) {
9769 if (asprintf(&str, "%lld", (long long)set->val.num) == -1) {
9770 LOGMEM_RET(set->ctx);
9771 }
9772 set->val.str = str;
9773 } else {
9774 if (asprintf(&str, "%03.1Lf", set->val.num) == -1) {
9775 LOGMEM_RET(set->ctx);
9776 }
9777 set->val.str = str;
9778 }
9779 break;
9780 case LYXP_SET_BOOLEAN:
Michal Vasko004d3152020-06-11 19:59:22 +02009781 if (set->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02009782 set->val.str = strdup("true");
9783 } else {
9784 set->val.str = strdup("false");
9785 }
9786 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), LY_EMEM);
9787 break;
9788 case LYXP_SET_NODE_SET:
Michal Vasko03ff5a72019-09-11 13:49:33 +02009789 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009790 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02009791
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009792 rc = cast_node_set_to_string(set, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009793 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02009794 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009795 set->val.str = str;
9796 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009797 default:
9798 LOGINT_RET(set->ctx);
9799 }
9800 set->type = LYXP_SET_STRING;
9801 }
9802
9803 /* to NUMBER */
9804 if (target == LYXP_SET_NUMBER) {
9805 switch (set->type) {
9806 case LYXP_SET_STRING:
9807 num = cast_string_to_number(set->val.str);
Michal Vaskod3678892020-05-21 10:06:58 +02009808 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009809 set->val.num = num;
9810 break;
9811 case LYXP_SET_BOOLEAN:
Michal Vasko004d3152020-06-11 19:59:22 +02009812 if (set->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02009813 set->val.num = 1;
9814 } else {
9815 set->val.num = 0;
9816 }
9817 break;
9818 default:
9819 LOGINT_RET(set->ctx);
9820 }
9821 set->type = LYXP_SET_NUMBER;
9822 }
9823
9824 /* to BOOLEAN */
9825 if (target == LYXP_SET_BOOLEAN) {
9826 switch (set->type) {
9827 case LYXP_SET_NUMBER:
9828 if ((set->val.num == 0) || (set->val.num == -0.0f) || isnan(set->val.num)) {
Michal Vasko004d3152020-06-11 19:59:22 +02009829 set->val.bln = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009830 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02009831 set->val.bln = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009832 }
9833 break;
9834 case LYXP_SET_STRING:
9835 if (set->val.str[0]) {
Michal Vaskod3678892020-05-21 10:06:58 +02009836 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02009837 set->val.bln = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009838 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02009839 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02009840 set->val.bln = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009841 }
9842 break;
9843 case LYXP_SET_NODE_SET:
Michal Vaskod3678892020-05-21 10:06:58 +02009844 if (set->used) {
9845 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02009846 set->val.bln = 1;
Michal Vaskod3678892020-05-21 10:06:58 +02009847 } else {
9848 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02009849 set->val.bln = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02009850 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02009851 break;
9852 default:
9853 LOGINT_RET(set->ctx);
9854 }
9855 set->type = LYXP_SET_BOOLEAN;
9856 }
9857
Michal Vasko03ff5a72019-09-11 13:49:33 +02009858 return LY_SUCCESS;
9859}
9860
9861LY_ERR
Michal Vasko400e9672021-01-11 13:39:17 +01009862lyxp_atomize(const struct ly_ctx *ctx, const struct lyxp_expr *exp, const struct lys_module *cur_mod,
Michal Vaskoa3e92bc2022-07-29 14:56:23 +02009863 LY_VALUE_FORMAT format, void *prefix_data, const struct lysc_node *cur_scnode,
9864 const struct lysc_node *ctx_scnode, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02009865{
Radek Krejci2efc45b2020-12-22 16:25:44 +01009866 LY_ERR ret;
Michal Vaskodd528af2022-08-08 14:35:07 +02009867 uint32_t tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009868
Michal Vasko400e9672021-01-11 13:39:17 +01009869 LY_CHECK_ARG_RET(ctx, ctx, exp, set, LY_EINVAL);
Radek Krejci8df109d2021-04-23 12:19:08 +02009870 if (!cur_mod && ((format == LY_VALUE_SCHEMA) || (format == LY_VALUE_SCHEMA_RESOLVED))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009871 LOGARG(NULL, "Current module must be set if schema format is used.");
9872 return LY_EINVAL;
Michal Vasko004d3152020-06-11 19:59:22 +02009873 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02009874
9875 /* prepare set for evaluation */
Michal Vasko03ff5a72019-09-11 13:49:33 +02009876 memset(set, 0, sizeof *set);
9877 set->type = LYXP_SET_SCNODE_SET;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009878 set->root_type = lyxp_get_root_type(NULL, ctx_scnode, options);
Michal Vasko7333cb32022-07-29 16:30:29 +02009879 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 +01009880 set->val.scnodes[0].in_ctx = LYXP_SET_SCNODE_START;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009881
Michal Vasko400e9672021-01-11 13:39:17 +01009882 set->ctx = (struct ly_ctx *)ctx;
Michal Vaskoa3e92bc2022-07-29 14:56:23 +02009883 set->cur_scnode = cur_scnode;
9884 for (set->context_op = cur_scnode;
Radek Krejci0f969882020-08-21 16:56:47 +02009885 set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF));
9886 set->context_op = set->context_op->parent) {}
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009887 set->cur_mod = cur_mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009888 set->format = format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009889 set->prefix_data = prefix_data;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009890
Radek Krejciddace2c2021-01-08 11:30:56 +01009891 LOG_LOCSET(set->cur_scnode, NULL, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01009892
Michal Vasko03ff5a72019-09-11 13:49:33 +02009893 /* evaluate */
Radek Krejci2efc45b2020-12-22 16:25:44 +01009894 ret = eval_expr_select(exp, &tok_idx, 0, set, options);
9895
Michal Vaskof69e4552022-12-02 10:27:42 +01009896 LOG_LOCBACK(set->cur_scnode ? 1 : 0, 0, 0, 0);
Radek Krejci2efc45b2020-12-22 16:25:44 +01009897 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009898}
Michal Vaskod43d71a2020-08-07 14:54:58 +02009899
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01009900LIBYANG_API_DEF const char *
Michal Vaskod43d71a2020-08-07 14:54:58 +02009901lyxp_get_expr(const struct lyxp_expr *path)
9902{
9903 if (!path) {
9904 return NULL;
9905 }
9906
9907 return path->expr;
9908}