blob: 478153887c77e792577ffa0f07dc7114729bc001 [file] [log] [blame]
Radek Krejcib1646a92018-11-02 16:08:26 +01001/**
2 * @file xpath.c
3 * @author Michal Vasko <mvasko@cesnet.cz>
4 * @brief YANG XPath evaluation functions
5 *
Michal Vasko49fec8e2022-05-24 10:28:33 +02006 * Copyright (c) 2015 - 2022 CESNET, z.s.p.o.
Radek Krejcib1646a92018-11-02 16:08:26 +01007 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
Christian Hopps32874e12021-05-01 09:43:54 -040014#define _GNU_SOURCE /* asprintf, strdup */
Radek Krejcib1646a92018-11-02 16:08:26 +010015
Radek Krejci535ea9f2020-05-29 16:01:05 +020016#include "xpath.h"
Radek Krejcib1646a92018-11-02 16:08:26 +010017
Radek Krejci535ea9f2020-05-29 16:01:05 +020018#include <assert.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010019#include <ctype.h>
Radek Krejci535ea9f2020-05-29 16:01:05 +020020#include <errno.h>
Radek Krejci535ea9f2020-05-29 16:01:05 +020021#include <math.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020022#include <stdint.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010023#include <stdio.h>
24#include <stdlib.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010025#include <string.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010026
Radek Krejci535ea9f2020-05-29 16:01:05 +020027#include "common.h"
Michal Vasko5aa44c02020-06-29 11:47:02 +020028#include "compat.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020029#include "context.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020030#include "dict.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020031#include "hash_table.h"
Radek Krejci47fab892020-11-05 17:02:41 +010032#include "out.h"
Radek Krejci7931b192020-06-25 17:05:03 +020033#include "parser_data.h"
Michal Vasko004d3152020-06-11 19:59:22 +020034#include "path.h"
Michal Vasko03ff5a72019-09-11 13:49:33 +020035#include "plugins_types.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020036#include "printer_data.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020037#include "schema_compile_node.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020038#include "tree.h"
Radek Krejci77114102021-03-10 15:21:57 +010039#include "tree_data.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020040#include "tree_data_internal.h"
Radek Krejci859a15a2021-03-05 20:56:59 +010041#include "tree_edit.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020042#include "tree_schema_internal.h"
43#include "xml.h"
Michal Vasko03ff5a72019-09-11 13:49:33 +020044
Michal Vasko7333cb32022-07-29 16:30:29 +020045static LY_ERR set_scnode_insert_node(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type,
46 enum lyxp_axis axis, uint32_t *index_p);
aPiecekbf968d92021-05-27 14:35:05 +020047static LY_ERR reparse_or_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t depth);
Michal Vasko40308e72020-10-20 16:38:40 +020048static LY_ERR eval_expr_select(const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_expr_type etype,
49 struct lyxp_set *set, uint32_t options);
Michal Vaskofe1af042022-07-29 14:58:59 +020050static LY_ERR moveto_resolve_model(const char **qname, uint32_t *qname_len, const struct lyxp_set *set,
Michal Vasko93923692021-05-07 15:28:02 +020051 const struct lysc_node *ctx_scnode, const struct lys_module **moveto_mod);
Michal Vasko47da6562022-07-14 15:43:15 +020052static LY_ERR moveto_axis_node_next(const struct lyd_node **iter, enum lyxp_node_type *iter_type,
53 const struct lyd_node *node, enum lyxp_node_type node_type, enum lyxp_axis axis, struct lyxp_set *set);
Michal Vasko49fec8e2022-05-24 10:28:33 +020054static LY_ERR moveto_node(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname,
55 enum lyxp_axis axis, uint32_t options);
56static LY_ERR moveto_scnode(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname,
57 enum lyxp_axis axis, uint32_t options);
Michal Vasko8abcecc2022-07-28 09:55:01 +020058static LY_ERR moveto_op_comp(struct lyxp_set *set1, struct lyxp_set *set2, const char *op);
Michal Vasko03ff5a72019-09-11 13:49:33 +020059
aPiecek96dc1e32021-10-08 15:45:59 +020060/* Functions are divided into the following basic classes:
61 *
62 * (re)parse functions:
63 * Parse functions parse the expression into
64 * tokens (syntactic analysis).
65 * Reparse functions perform semantic analysis
66 * (do not save the result, just a check) of
67 * the expression and fill repeat indices.
68 *
69 * warn functions:
70 * Warn functions check specific reasonable conditions for schema XPath
71 * and print a warning if they are not satisfied.
72 *
73 * moveto functions:
74 * They and only they actually change the context (set).
75 *
76 * eval functions:
77 * They execute a parsed XPath expression on some data subtree.
78 */
79
Michal Vasko03ff5a72019-09-11 13:49:33 +020080/**
81 * @brief Print the type of an XPath \p set.
82 *
83 * @param[in] set Set to use.
84 * @return Set type string.
85 */
86static const char *
87print_set_type(struct lyxp_set *set)
88{
89 switch (set->type) {
Michal Vasko03ff5a72019-09-11 13:49:33 +020090 case LYXP_SET_NODE_SET:
91 return "node set";
92 case LYXP_SET_SCNODE_SET:
93 return "schema node set";
94 case LYXP_SET_BOOLEAN:
95 return "boolean";
96 case LYXP_SET_NUMBER:
97 return "number";
98 case LYXP_SET_STRING:
99 return "string";
100 }
101
102 return NULL;
103}
104
Michal Vasko24cddf82020-06-01 08:17:01 +0200105const char *
Michal Vasko49fec8e2022-05-24 10:28:33 +0200106lyxp_token2str(enum lyxp_token tok)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200107{
108 switch (tok) {
109 case LYXP_TOKEN_PAR1:
110 return "(";
111 case LYXP_TOKEN_PAR2:
112 return ")";
113 case LYXP_TOKEN_BRACK1:
114 return "[";
115 case LYXP_TOKEN_BRACK2:
116 return "]";
117 case LYXP_TOKEN_DOT:
118 return ".";
119 case LYXP_TOKEN_DDOT:
120 return "..";
121 case LYXP_TOKEN_AT:
122 return "@";
123 case LYXP_TOKEN_COMMA:
124 return ",";
125 case LYXP_TOKEN_NAMETEST:
126 return "NameTest";
127 case LYXP_TOKEN_NODETYPE:
128 return "NodeType";
aPiecekfba75362021-10-07 12:39:48 +0200129 case LYXP_TOKEN_VARREF:
130 return "VariableReference";
Michal Vasko03ff5a72019-09-11 13:49:33 +0200131 case LYXP_TOKEN_FUNCNAME:
132 return "FunctionName";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200133 case LYXP_TOKEN_OPER_LOG:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200134 return "Operator(Logic)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200135 case LYXP_TOKEN_OPER_EQUAL:
136 return "Operator(Equal)";
137 case LYXP_TOKEN_OPER_NEQUAL:
138 return "Operator(Non-equal)";
139 case LYXP_TOKEN_OPER_COMP:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200140 return "Operator(Comparison)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200141 case LYXP_TOKEN_OPER_MATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200142 return "Operator(Math)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200143 case LYXP_TOKEN_OPER_UNI:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200144 return "Operator(Union)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200145 case LYXP_TOKEN_OPER_PATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200146 return "Operator(Path)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200147 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko14676352020-05-29 11:35:55 +0200148 return "Operator(Recursive Path)";
Michal Vasko03ff5a72019-09-11 13:49:33 +0200149 case LYXP_TOKEN_LITERAL:
150 return "Literal";
151 case LYXP_TOKEN_NUMBER:
152 return "Number";
153 default:
154 LOGINT(NULL);
155 return "";
156 }
157}
158
159/**
Michal Vasko49fec8e2022-05-24 10:28:33 +0200160 * @brief Transform string into an axis.
161 *
162 * @param[in] str String to transform.
163 * @param[in] str_len Length of @p str.
164 * @return Transformed axis.
165 */
166static enum lyxp_axis
167str2axis(const char *str, uint16_t str_len)
168{
169 switch (str_len) {
170 case 4:
171 assert(!strncmp("self", str, str_len));
172 return LYXP_AXIS_SELF;
173 case 5:
174 assert(!strncmp("child", str, str_len));
175 return LYXP_AXIS_CHILD;
176 case 6:
177 assert(!strncmp("parent", str, str_len));
178 return LYXP_AXIS_PARENT;
179 case 8:
180 assert(!strncmp("ancestor", str, str_len));
181 return LYXP_AXIS_ANCESTOR;
182 case 9:
183 if (str[0] == 'a') {
184 assert(!strncmp("attribute", str, str_len));
185 return LYXP_AXIS_ATTRIBUTE;
186 } else if (str[0] == 'f') {
187 assert(!strncmp("following", str, str_len));
188 return LYXP_AXIS_FOLLOWING;
189 } else {
190 assert(!strncmp("preceding", str, str_len));
191 return LYXP_AXIS_PRECEDING;
192 }
193 break;
194 case 10:
195 assert(!strncmp("descendant", str, str_len));
196 return LYXP_AXIS_DESCENDANT;
197 case 16:
198 assert(!strncmp("ancestor-or-self", str, str_len));
199 return LYXP_AXIS_ANCESTOR_OR_SELF;
200 case 17:
201 if (str[0] == 'f') {
202 assert(!strncmp("following-sibling", str, str_len));
203 return LYXP_AXIS_FOLLOWING_SIBLING;
204 } else {
205 assert(!strncmp("preceding-sibling", str, str_len));
206 return LYXP_AXIS_PRECEDING_SIBLING;
207 }
208 break;
209 case 18:
210 assert(!strncmp("descendant-or-self", str, str_len));
211 return LYXP_AXIS_DESCENDANT_OR_SELF;
212 }
213
214 LOGINT(NULL);
215 return 0;
216}
217
218/**
Michal Vasko03ff5a72019-09-11 13:49:33 +0200219 * @brief Print the whole expression \p exp to debug output.
220 *
221 * @param[in] exp Expression to use.
222 */
223static void
Michal Vasko40308e72020-10-20 16:38:40 +0200224print_expr_struct_debug(const struct lyxp_expr *exp)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200225{
Radek Krejcif13b87b2020-12-01 22:02:17 +0100226#define MSG_BUFFER_SIZE 128
227 char tmp[MSG_BUFFER_SIZE];
Michal Vasko1fdd8fa2021-01-08 09:21:45 +0100228 uint32_t i, j;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200229
Radek Krejci52b6d512020-10-12 12:33:17 +0200230 if (!exp || (ly_ll < LY_LLDBG)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200231 return;
232 }
233
234 LOGDBG(LY_LDGXPATH, "expression \"%s\":", exp->expr);
235 for (i = 0; i < exp->used; ++i) {
Michal Vasko49fec8e2022-05-24 10:28:33 +0200236 sprintf(tmp, "\ttoken %s, in expression \"%.*s\"", lyxp_token2str(exp->tokens[i]), exp->tok_len[i],
Michal Vasko69730152020-10-09 16:30:07 +0200237 &exp->expr[exp->tok_pos[i]]);
Michal Vasko23049552021-03-04 15:57:44 +0100238 if (exp->repeat && exp->repeat[i]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200239 sprintf(tmp + strlen(tmp), " (repeat %d", exp->repeat[i][0]);
240 for (j = 1; exp->repeat[i][j]; ++j) {
241 sprintf(tmp + strlen(tmp), ", %d", exp->repeat[i][j]);
242 }
243 strcat(tmp, ")");
244 }
245 LOGDBG(LY_LDGXPATH, tmp);
246 }
Radek Krejcif13b87b2020-12-01 22:02:17 +0100247#undef MSG_BUFFER_SIZE
Michal Vasko03ff5a72019-09-11 13:49:33 +0200248}
249
250#ifndef NDEBUG
251
252/**
253 * @brief Print XPath set content to debug output.
254 *
255 * @param[in] set Set to print.
256 */
257static void
258print_set_debug(struct lyxp_set *set)
259{
260 uint32_t i;
261 char *str;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200262 struct lyxp_set_node *item;
263 struct lyxp_set_scnode *sitem;
264
Radek Krejci52b6d512020-10-12 12:33:17 +0200265 if (ly_ll < LY_LLDBG) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200266 return;
267 }
268
269 switch (set->type) {
270 case LYXP_SET_NODE_SET:
271 LOGDBG(LY_LDGXPATH, "set NODE SET:");
272 for (i = 0; i < set->used; ++i) {
273 item = &set->val.nodes[i];
274
275 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +0100276 case LYXP_NODE_NONE:
277 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): NONE", i + 1, item->pos);
278 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200279 case LYXP_NODE_ROOT:
280 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT", i + 1, item->pos);
281 break;
282 case LYXP_NODE_ROOT_CONFIG:
283 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT CONFIG", i + 1, item->pos);
284 break;
285 case LYXP_NODE_ELEM:
Michal Vasko9e685082021-01-29 14:49:09 +0100286 if ((item->node->schema->nodetype == LYS_LIST) && (lyd_child(item->node)->schema->nodetype == LYS_LEAF)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200287 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (1st child val: %s)", i + 1, item->pos,
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200288 item->node->schema->name, lyd_get_value(lyd_child(item->node)));
Michal Vasko9e685082021-01-29 14:49:09 +0100289 } else if (item->node->schema->nodetype == LYS_LEAFLIST) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200290 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (val: %s)", i + 1, item->pos,
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200291 item->node->schema->name, lyd_get_value(item->node));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200292 } else {
293 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s", i + 1, item->pos, item->node->schema->name);
294 }
295 break;
296 case LYXP_NODE_TEXT:
297 if (item->node->schema->nodetype & LYS_ANYDATA) {
298 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): TEXT <%s>", i + 1, item->pos,
Michal Vasko69730152020-10-09 16:30:07 +0200299 item->node->schema->nodetype == LYS_ANYXML ? "anyxml" : "anydata");
Michal Vasko03ff5a72019-09-11 13:49:33 +0200300 } else {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200301 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): TEXT %s", i + 1, item->pos, lyd_get_value(item->node));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200302 }
303 break;
Michal Vasko9f96a052020-03-10 09:41:45 +0100304 case LYXP_NODE_META:
305 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): META %s = %s", i + 1, item->pos, set->val.meta[i].meta->name,
Michal Vasko69730152020-10-09 16:30:07 +0200306 set->val.meta[i].meta->value);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200307 break;
308 }
309 }
310 break;
311
312 case LYXP_SET_SCNODE_SET:
313 LOGDBG(LY_LDGXPATH, "set SCNODE SET:");
314 for (i = 0; i < set->used; ++i) {
315 sitem = &set->val.scnodes[i];
316
317 switch (sitem->type) {
318 case LYXP_NODE_ROOT:
319 LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT", i + 1, sitem->in_ctx);
320 break;
321 case LYXP_NODE_ROOT_CONFIG:
322 LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT CONFIG", i + 1, sitem->in_ctx);
323 break;
324 case LYXP_NODE_ELEM:
325 LOGDBG(LY_LDGXPATH, "\t%d (%u): ELEM %s", i + 1, sitem->in_ctx, sitem->scnode->name);
326 break;
327 default:
328 LOGINT(NULL);
329 break;
330 }
331 }
332 break;
333
Michal Vasko03ff5a72019-09-11 13:49:33 +0200334 case LYXP_SET_BOOLEAN:
335 LOGDBG(LY_LDGXPATH, "set BOOLEAN");
Michal Vasko004d3152020-06-11 19:59:22 +0200336 LOGDBG(LY_LDGXPATH, "\t%s", (set->val.bln ? "true" : "false"));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200337 break;
338
339 case LYXP_SET_STRING:
340 LOGDBG(LY_LDGXPATH, "set STRING");
341 LOGDBG(LY_LDGXPATH, "\t%s", set->val.str);
342 break;
343
344 case LYXP_SET_NUMBER:
345 LOGDBG(LY_LDGXPATH, "set NUMBER");
346
347 if (isnan(set->val.num)) {
348 str = strdup("NaN");
349 } else if ((set->val.num == 0) || (set->val.num == -0.0f)) {
350 str = strdup("0");
351 } else if (isinf(set->val.num) && !signbit(set->val.num)) {
352 str = strdup("Infinity");
353 } else if (isinf(set->val.num) && signbit(set->val.num)) {
354 str = strdup("-Infinity");
355 } else if ((long long)set->val.num == set->val.num) {
356 if (asprintf(&str, "%lld", (long long)set->val.num) == -1) {
357 str = NULL;
358 }
359 } else {
360 if (asprintf(&str, "%03.1Lf", set->val.num) == -1) {
361 str = NULL;
362 }
363 }
364 LY_CHECK_ERR_RET(!str, LOGMEM(NULL), );
365
366 LOGDBG(LY_LDGXPATH, "\t%s", str);
367 free(str);
368 }
369}
370
371#endif
372
373/**
374 * @brief Realloc the string \p str.
375 *
376 * @param[in] ctx libyang context for logging.
377 * @param[in] needed How much free space is required.
378 * @param[in,out] str Pointer to the string to use.
379 * @param[in,out] used Used bytes in \p str.
380 * @param[in,out] size Allocated bytes in \p str.
381 * @return LY_ERR
382 */
383static LY_ERR
Michal Vasko52927e22020-03-16 17:26:14 +0100384cast_string_realloc(const struct ly_ctx *ctx, uint16_t needed, char **str, uint16_t *used, uint16_t *size)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200385{
386 if (*size - *used < needed) {
387 do {
388 if ((UINT16_MAX - *size) < LYXP_STRING_CAST_SIZE_STEP) {
389 LOGERR(ctx, LY_EINVAL, "XPath string length limit (%u) reached.", UINT16_MAX);
390 return LY_EINVAL;
391 }
392 *size += LYXP_STRING_CAST_SIZE_STEP;
393 } while (*size - *used < needed);
394 *str = ly_realloc(*str, *size * sizeof(char));
395 LY_CHECK_ERR_RET(!(*str), LOGMEM(ctx), LY_EMEM);
396 }
397
398 return LY_SUCCESS;
399}
400
401/**
402 * @brief Cast nodes recursively to one string @p str.
403 *
Michal Vasko47da6562022-07-14 15:43:15 +0200404 * @param[in] node Node to cast, NULL if root.
405 * @param[in] set XPath set.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200406 * @param[in] indent Current indent.
407 * @param[in,out] str Resulting string.
408 * @param[in,out] used Used bytes in @p str.
409 * @param[in,out] size Allocated bytes in @p str.
Michal Vasko47da6562022-07-14 15:43:15 +0200410 * @return LY_ERR value.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200411 */
412static LY_ERR
Michal Vasko47da6562022-07-14 15:43:15 +0200413cast_string_recursive(const struct lyd_node *node, struct lyxp_set *set, uint16_t indent, char **str, uint16_t *used,
414 uint16_t *size)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200415{
Radek Krejci7f769d72020-07-11 23:13:56 +0200416 char *buf, *line, *ptr = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200417 const char *value_str;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200418 const struct lyd_node *child;
Michal Vasko47da6562022-07-14 15:43:15 +0200419 enum lyxp_node_type child_type;
Michal Vasko60ea6352020-06-29 13:39:39 +0200420 struct lyd_node *tree;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200421 struct lyd_node_any *any;
422 LY_ERR rc;
423
Michal Vasko47da6562022-07-14 15:43:15 +0200424 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && node && (node->schema->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200425 return LY_SUCCESS;
426 }
427
Michal Vasko47da6562022-07-14 15:43:15 +0200428 if (!node) {
429 /* fake container */
430 LY_CHECK_RET(cast_string_realloc(set->ctx, 1, str, used, size));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200431 strcpy(*str + (*used - 1), "\n");
432 ++(*used);
433
434 ++indent;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200435
Michal Vasko47da6562022-07-14 15:43:15 +0200436 /* print all the top-level nodes */
437 child = NULL;
438 child_type = 0;
439 while (!moveto_axis_node_next(&child, &child_type, NULL, set->root_type, LYXP_AXIS_CHILD, set)) {
440 LY_CHECK_RET(cast_string_recursive(child, set, indent, str, used, size));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200441 }
442
Michal Vasko47da6562022-07-14 15:43:15 +0200443 /* end fake container */
444 LY_CHECK_RET(cast_string_realloc(set->ctx, 1, str, used, size));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200445 strcpy(*str + (*used - 1), "\n");
446 ++(*used);
447
448 --indent;
Michal Vasko47da6562022-07-14 15:43:15 +0200449 } else {
450 switch (node->schema->nodetype) {
451 case LYS_CONTAINER:
452 case LYS_LIST:
453 case LYS_RPC:
454 case LYS_NOTIF:
455 LY_CHECK_RET(cast_string_realloc(set->ctx, 1, str, used, size));
456 strcpy(*str + (*used - 1), "\n");
457 ++(*used);
458
459 for (child = lyd_child(node); child; child = child->next) {
460 LY_CHECK_RET(cast_string_recursive(child, set, indent + 1, str, used, size));
461 }
462
463 break;
464
465 case LYS_LEAF:
466 case LYS_LEAFLIST:
467 value_str = lyd_get_value(node);
468
469 /* print indent */
470 LY_CHECK_RET(cast_string_realloc(set->ctx, indent * 2 + strlen(value_str) + 1, str, used, size));
471 memset(*str + (*used - 1), ' ', indent * 2);
472 *used += indent * 2;
473
474 /* print value */
475 if (*used == 1) {
476 sprintf(*str + (*used - 1), "%s", value_str);
477 *used += strlen(value_str);
478 } else {
479 sprintf(*str + (*used - 1), "%s\n", value_str);
480 *used += strlen(value_str) + 1;
481 }
482
483 break;
484
485 case LYS_ANYXML:
486 case LYS_ANYDATA:
487 any = (struct lyd_node_any *)node;
488 if (!(void *)any->value.tree) {
489 /* no content */
490 buf = strdup("");
491 LY_CHECK_ERR_RET(!buf, LOGMEM(set->ctx), LY_EMEM);
492 } else {
493 struct ly_out *out;
494
495 if (any->value_type == LYD_ANYDATA_LYB) {
496 /* try to parse it into a data tree */
497 if (lyd_parse_data_mem((struct ly_ctx *)set->ctx, any->value.mem, LYD_LYB,
498 LYD_PARSE_ONLY | LYD_PARSE_STRICT, 0, &tree) == LY_SUCCESS) {
499 /* successfully parsed */
500 free(any->value.mem);
501 any->value.tree = tree;
502 any->value_type = LYD_ANYDATA_DATATREE;
503 }
504 /* error is covered by the following switch where LYD_ANYDATA_LYB causes failure */
505 }
506
507 switch (any->value_type) {
508 case LYD_ANYDATA_STRING:
509 case LYD_ANYDATA_XML:
510 case LYD_ANYDATA_JSON:
511 buf = strdup(any->value.json);
512 LY_CHECK_ERR_RET(!buf, LOGMEM(set->ctx), LY_EMEM);
513 break;
514 case LYD_ANYDATA_DATATREE:
515 LY_CHECK_RET(ly_out_new_memory(&buf, 0, &out));
516 rc = lyd_print_all(out, any->value.tree, LYD_XML, 0);
517 ly_out_free(out, NULL, 0);
518 LY_CHECK_RET(rc < 0, -rc);
519 break;
520 case LYD_ANYDATA_LYB:
521 LOGERR(set->ctx, LY_EINVAL, "Cannot convert LYB anydata into string.");
522 return LY_EINVAL;
523 }
524 }
525
526 line = strtok_r(buf, "\n", &ptr);
527 do {
528 rc = cast_string_realloc(set->ctx, indent * 2 + strlen(line) + 1, str, used, size);
529 if (rc != LY_SUCCESS) {
530 free(buf);
531 return rc;
532 }
533 memset(*str + (*used - 1), ' ', indent * 2);
534 *used += indent * 2;
535
536 strcpy(*str + (*used - 1), line);
537 *used += strlen(line);
538
539 strcpy(*str + (*used - 1), "\n");
540 *used += 1;
541 } while ((line = strtok_r(NULL, "\n", &ptr)));
542
543 free(buf);
544 break;
545
546 default:
547 LOGINT_RET(set->ctx);
548 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200549 }
550
551 return LY_SUCCESS;
552}
553
554/**
555 * @brief Cast an element into a string.
556 *
Michal Vasko47da6562022-07-14 15:43:15 +0200557 * @param[in] node Node to cast, NULL if root.
558 * @param[in] set XPath set.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200559 * @param[out] str Element cast to dynamically-allocated string.
560 * @return LY_ERR
561 */
562static LY_ERR
Michal Vasko47da6562022-07-14 15:43:15 +0200563cast_string_elem(const struct lyd_node *node, struct lyxp_set *set, char **str)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200564{
565 uint16_t used, size;
566 LY_ERR rc;
567
568 *str = malloc(LYXP_STRING_CAST_SIZE_START * sizeof(char));
Michal Vasko47da6562022-07-14 15:43:15 +0200569 LY_CHECK_ERR_RET(!*str, LOGMEM(set->ctx), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200570 (*str)[0] = '\0';
571 used = 1;
572 size = LYXP_STRING_CAST_SIZE_START;
573
Michal Vasko47da6562022-07-14 15:43:15 +0200574 rc = cast_string_recursive(node, set, 0, str, &used, &size);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200575 if (rc != LY_SUCCESS) {
576 free(*str);
577 return rc;
578 }
579
580 if (size > used) {
581 *str = ly_realloc(*str, used * sizeof(char));
Michal Vasko47da6562022-07-14 15:43:15 +0200582 LY_CHECK_ERR_RET(!*str, LOGMEM(set->ctx), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200583 }
584 return LY_SUCCESS;
585}
586
587/**
588 * @brief Cast a LYXP_SET_NODE_SET set into a string.
589 * Context position aware.
590 *
591 * @param[in] set Set to cast.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200592 * @param[out] str Cast dynamically-allocated string.
593 * @return LY_ERR
594 */
595static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100596cast_node_set_to_string(struct lyxp_set *set, char **str)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200597{
Michal Vaskoaa677062021-03-09 13:52:53 +0100598 if (!set->used) {
599 *str = strdup("");
600 if (!*str) {
601 LOGMEM_RET(set->ctx);
602 }
603 return LY_SUCCESS;
604 }
605
Michal Vasko03ff5a72019-09-11 13:49:33 +0200606 switch (set->val.nodes[0].type) {
Michal Vasko2caefc12019-11-14 16:07:56 +0100607 case LYXP_NODE_NONE:
608 /* invalid */
609 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200610 case LYXP_NODE_ROOT:
611 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200612 case LYXP_NODE_ELEM:
613 case LYXP_NODE_TEXT:
Michal Vasko47da6562022-07-14 15:43:15 +0200614 return cast_string_elem(set->val.nodes[0].node, set, str);
Michal Vasko9f96a052020-03-10 09:41:45 +0100615 case LYXP_NODE_META:
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200616 *str = strdup(lyd_get_meta_value(set->val.meta[0].meta));
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200617 if (!*str) {
618 LOGMEM_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200619 }
620 return LY_SUCCESS;
621 }
622
623 LOGINT_RET(set->ctx);
624}
625
626/**
627 * @brief Cast a string into an XPath number.
628 *
629 * @param[in] str String to use.
630 * @return Cast number.
631 */
632static long double
633cast_string_to_number(const char *str)
634{
635 long double num;
636 char *ptr;
637
638 errno = 0;
639 num = strtold(str, &ptr);
640 if (errno || *ptr) {
641 num = NAN;
642 }
643 return num;
644}
645
646/**
647 * @brief Callback for checking value equality.
648 *
Michal Vasko62524a92021-02-26 10:08:50 +0100649 * Implementation of ::lyht_value_equal_cb.
Radek Krejci857189e2020-09-01 13:26:36 +0200650 *
Michal Vasko03ff5a72019-09-11 13:49:33 +0200651 * @param[in] val1_p First value.
652 * @param[in] val2_p Second value.
653 * @param[in] mod Whether hash table is being modified.
654 * @param[in] cb_data Callback data.
Radek Krejci857189e2020-09-01 13:26:36 +0200655 * @return Boolean value whether values are equal or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200656 */
Radek Krejci857189e2020-09-01 13:26:36 +0200657static ly_bool
658set_values_equal_cb(void *val1_p, void *val2_p, ly_bool UNUSED(mod), void *UNUSED(cb_data))
Michal Vasko03ff5a72019-09-11 13:49:33 +0200659{
660 struct lyxp_set_hash_node *val1, *val2;
661
662 val1 = (struct lyxp_set_hash_node *)val1_p;
663 val2 = (struct lyxp_set_hash_node *)val2_p;
664
665 if ((val1->node == val2->node) && (val1->type == val2->type)) {
666 return 1;
667 }
668
669 return 0;
670}
671
672/**
673 * @brief Insert node and its hash into set.
674 *
675 * @param[in] set et to insert to.
676 * @param[in] node Node with hash.
677 * @param[in] type Node type.
678 */
679static void
680set_insert_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type)
681{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200682 LY_ERR r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200683 uint32_t i, hash;
684 struct lyxp_set_hash_node hnode;
685
686 if (!set->ht && (set->used >= LYD_HT_MIN_ITEMS)) {
687 /* create hash table and add all the nodes */
688 set->ht = lyht_new(1, sizeof(struct lyxp_set_hash_node), set_values_equal_cb, NULL, 1);
689 for (i = 0; i < set->used; ++i) {
690 hnode.node = set->val.nodes[i].node;
691 hnode.type = set->val.nodes[i].type;
692
693 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
694 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
695 hash = dict_hash_multi(hash, NULL, 0);
696
697 r = lyht_insert(set->ht, &hnode, hash, NULL);
698 assert(!r);
699 (void)r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200700
Michal Vasko9d6befd2019-12-11 14:56:56 +0100701 if (hnode.node == node) {
702 /* it was just added, do not add it twice */
703 node = NULL;
704 }
705 }
706 }
707
708 if (set->ht && node) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200709 /* add the new node into hash table */
710 hnode.node = node;
711 hnode.type = type;
712
713 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
714 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
715 hash = dict_hash_multi(hash, NULL, 0);
716
717 r = lyht_insert(set->ht, &hnode, hash, NULL);
718 assert(!r);
719 (void)r;
720 }
721}
722
723/**
724 * @brief Remove node and its hash from set.
725 *
726 * @param[in] set Set to remove from.
727 * @param[in] node Node to remove.
728 * @param[in] type Node type.
729 */
730static void
731set_remove_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type)
732{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200733 LY_ERR r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200734 struct lyxp_set_hash_node hnode;
735 uint32_t hash;
736
737 if (set->ht) {
738 hnode.node = node;
739 hnode.type = type;
740
741 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
742 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
743 hash = dict_hash_multi(hash, NULL, 0);
744
745 r = lyht_remove(set->ht, &hnode, hash);
746 assert(!r);
747 (void)r;
748
749 if (!set->ht->used) {
750 lyht_free(set->ht);
751 set->ht = NULL;
752 }
753 }
754}
755
756/**
757 * @brief Check whether node is in set based on its hash.
758 *
759 * @param[in] set Set to search in.
760 * @param[in] node Node to search for.
761 * @param[in] type Node type.
762 * @param[in] skip_idx Index in @p set to skip.
763 * @return LY_ERR
764 */
765static LY_ERR
766set_dup_node_hash_check(const struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type, int skip_idx)
767{
768 struct lyxp_set_hash_node hnode, *match_p;
769 uint32_t hash;
770
771 hnode.node = node;
772 hnode.type = type;
773
774 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
775 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
776 hash = dict_hash_multi(hash, NULL, 0);
777
778 if (!lyht_find(set->ht, &hnode, hash, (void **)&match_p)) {
779 if ((skip_idx > -1) && (set->val.nodes[skip_idx].node == match_p->node) && (set->val.nodes[skip_idx].type == match_p->type)) {
780 /* we found it on the index that should be skipped, find another */
781 hnode = *match_p;
782 if (lyht_find_next(set->ht, &hnode, hash, (void **)&match_p)) {
783 /* none other found */
784 return LY_SUCCESS;
785 }
786 }
787
788 return LY_EEXIST;
789 }
790
791 /* not found */
792 return LY_SUCCESS;
793}
794
Michal Vaskod3678892020-05-21 10:06:58 +0200795void
796lyxp_set_free_content(struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200797{
798 if (!set) {
799 return;
800 }
801
802 if (set->type == LYXP_SET_NODE_SET) {
803 free(set->val.nodes);
804 lyht_free(set->ht);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200805 } else if (set->type == LYXP_SET_SCNODE_SET) {
806 free(set->val.scnodes);
Michal Vaskod3678892020-05-21 10:06:58 +0200807 lyht_free(set->ht);
808 } else {
809 if (set->type == LYXP_SET_STRING) {
810 free(set->val.str);
811 }
812 set->type = LYXP_SET_NODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200813 }
Michal Vaskod3678892020-05-21 10:06:58 +0200814
815 set->val.nodes = NULL;
816 set->used = 0;
817 set->size = 0;
818 set->ht = NULL;
819 set->ctx_pos = 0;
aPiecek748da732021-06-01 09:56:43 +0200820 set->ctx_size = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200821}
822
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100823/**
824 * @brief Free dynamically-allocated set.
825 *
826 * @param[in] set Set to free.
827 */
828static void
Michal Vasko03ff5a72019-09-11 13:49:33 +0200829lyxp_set_free(struct lyxp_set *set)
830{
831 if (!set) {
832 return;
833 }
834
Michal Vaskod3678892020-05-21 10:06:58 +0200835 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200836 free(set);
837}
838
839/**
840 * @brief Initialize set context.
841 *
842 * @param[in] new Set to initialize.
843 * @param[in] set Arbitrary initialized set.
844 */
845static void
Michal Vasko4c7763f2020-07-27 17:40:37 +0200846set_init(struct lyxp_set *new, const struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200847{
848 memset(new, 0, sizeof *new);
Michal Vasko02a77382019-09-12 11:47:35 +0200849 if (set) {
Michal Vasko306e2832022-07-25 09:15:17 +0200850 new->non_child_axis = set->non_child_axis;
Michal Vasko02a77382019-09-12 11:47:35 +0200851 new->ctx = set->ctx;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200852 new->cur_node = set->cur_node;
Michal Vasko588112f2019-12-10 14:51:53 +0100853 new->root_type = set->root_type;
Michal Vasko6b26e742020-07-17 15:02:10 +0200854 new->context_op = set->context_op;
Michal Vaskof03ed032020-03-04 13:31:44 +0100855 new->tree = set->tree;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200856 new->cur_mod = set->cur_mod;
Michal Vasko02a77382019-09-12 11:47:35 +0200857 new->format = set->format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200858 new->prefix_data = set->prefix_data;
aPiecekfba75362021-10-07 12:39:48 +0200859 new->vars = set->vars;
Michal Vasko02a77382019-09-12 11:47:35 +0200860 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200861}
862
863/**
864 * @brief Create a deep copy of a set.
865 *
866 * @param[in] set Set to copy.
867 * @return Copy of @p set.
868 */
869static struct lyxp_set *
870set_copy(struct lyxp_set *set)
871{
872 struct lyxp_set *ret;
Michal Vasko1fdd8fa2021-01-08 09:21:45 +0100873 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200874
875 if (!set) {
876 return NULL;
877 }
878
879 ret = malloc(sizeof *ret);
880 LY_CHECK_ERR_RET(!ret, LOGMEM(set->ctx), NULL);
881 set_init(ret, set);
882
883 if (set->type == LYXP_SET_SCNODE_SET) {
884 ret->type = set->type;
885
886 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100887 if ((set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) ||
888 (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +0200889 uint32_t idx;
Michal Vasko7333cb32022-07-29 16:30:29 +0200890 LY_CHECK_ERR_RET(set_scnode_insert_node(ret, set->val.scnodes[i].scnode, set->val.scnodes[i].type,
891 set->val.scnodes[i].axis, &idx), lyxp_set_free(ret), NULL);
Michal Vasko3f27c522020-01-06 08:37:49 +0100892 /* coverity seems to think scnodes can be NULL */
Radek Krejciaa6b53f2020-08-27 15:19:03 +0200893 if (!ret->val.scnodes) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200894 lyxp_set_free(ret);
895 return NULL;
896 }
Michal Vaskoba716542019-12-16 10:01:58 +0100897 ret->val.scnodes[idx].in_ctx = set->val.scnodes[i].in_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200898 }
899 }
900 } else if (set->type == LYXP_SET_NODE_SET) {
901 ret->type = set->type;
Michal Vasko08e9b112021-06-11 15:41:17 +0200902 if (set->used) {
903 ret->val.nodes = malloc(set->used * sizeof *ret->val.nodes);
904 LY_CHECK_ERR_RET(!ret->val.nodes, LOGMEM(set->ctx); free(ret), NULL);
905 memcpy(ret->val.nodes, set->val.nodes, set->used * sizeof *ret->val.nodes);
906 } else {
907 ret->val.nodes = NULL;
908 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200909
910 ret->used = ret->size = set->used;
911 ret->ctx_pos = set->ctx_pos;
912 ret->ctx_size = set->ctx_size;
Michal Vasko4a04e542021-02-01 08:58:30 +0100913 if (set->ht) {
914 ret->ht = lyht_dup(set->ht);
915 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200916 } else {
Radek Krejci0f969882020-08-21 16:56:47 +0200917 memcpy(ret, set, sizeof *ret);
918 if (set->type == LYXP_SET_STRING) {
919 ret->val.str = strdup(set->val.str);
920 LY_CHECK_ERR_RET(!ret->val.str, LOGMEM(set->ctx); free(ret), NULL);
921 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200922 }
923
924 return ret;
925}
926
927/**
928 * @brief Fill XPath set with a string. Any current data are disposed of.
929 *
930 * @param[in] set Set to fill.
931 * @param[in] string String to fill into \p set.
932 * @param[in] str_len Length of \p string. 0 is a valid value!
933 */
934static void
935set_fill_string(struct lyxp_set *set, const char *string, uint16_t str_len)
936{
Michal Vaskod3678892020-05-21 10:06:58 +0200937 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200938
939 set->type = LYXP_SET_STRING;
940 if ((str_len == 0) && (string[0] != '\0')) {
941 string = "";
942 }
943 set->val.str = strndup(string, str_len);
944}
945
946/**
947 * @brief Fill XPath set with a number. Any current data are disposed of.
948 *
949 * @param[in] set Set to fill.
950 * @param[in] number Number to fill into \p set.
951 */
952static void
953set_fill_number(struct lyxp_set *set, long double number)
954{
Michal Vaskod3678892020-05-21 10:06:58 +0200955 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200956
957 set->type = LYXP_SET_NUMBER;
958 set->val.num = number;
959}
960
961/**
962 * @brief Fill XPath set with a boolean. Any current data are disposed of.
963 *
964 * @param[in] set Set to fill.
965 * @param[in] boolean Boolean to fill into \p set.
966 */
967static void
Radek Krejci857189e2020-09-01 13:26:36 +0200968set_fill_boolean(struct lyxp_set *set, ly_bool boolean)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200969{
Michal Vaskod3678892020-05-21 10:06:58 +0200970 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200971
972 set->type = LYXP_SET_BOOLEAN;
Michal Vasko004d3152020-06-11 19:59:22 +0200973 set->val.bln = boolean;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200974}
975
976/**
977 * @brief Fill XPath set with the value from another set (deep assign).
978 * Any current data are disposed of.
979 *
980 * @param[in] trg Set to fill.
981 * @param[in] src Source set to copy into \p trg.
982 */
983static void
Michal Vasko4c7763f2020-07-27 17:40:37 +0200984set_fill_set(struct lyxp_set *trg, const struct lyxp_set *src)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200985{
986 if (!trg || !src) {
987 return;
988 }
989
990 if (trg->type == LYXP_SET_NODE_SET) {
991 free(trg->val.nodes);
992 } else if (trg->type == LYXP_SET_STRING) {
993 free(trg->val.str);
994 }
995 set_init(trg, src);
996
997 if (src->type == LYXP_SET_SCNODE_SET) {
998 trg->type = LYXP_SET_SCNODE_SET;
999 trg->used = src->used;
1000 trg->size = src->used;
1001
Michal Vasko08e9b112021-06-11 15:41:17 +02001002 if (trg->size) {
1003 trg->val.scnodes = ly_realloc(trg->val.scnodes, trg->size * sizeof *trg->val.scnodes);
1004 LY_CHECK_ERR_RET(!trg->val.scnodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), );
1005 memcpy(trg->val.scnodes, src->val.scnodes, src->used * sizeof *src->val.scnodes);
1006 } else {
1007 trg->val.scnodes = NULL;
1008 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001009 } else if (src->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +02001010 set_fill_boolean(trg, src->val.bln);
Michal Vasko44f3d2c2020-08-24 09:49:38 +02001011 } else if (src->type == LYXP_SET_NUMBER) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001012 set_fill_number(trg, src->val.num);
1013 } else if (src->type == LYXP_SET_STRING) {
1014 set_fill_string(trg, src->val.str, strlen(src->val.str));
1015 } else {
1016 if (trg->type == LYXP_SET_NODE_SET) {
1017 free(trg->val.nodes);
1018 } else if (trg->type == LYXP_SET_STRING) {
1019 free(trg->val.str);
1020 }
1021
Michal Vaskod3678892020-05-21 10:06:58 +02001022 assert(src->type == LYXP_SET_NODE_SET);
1023
1024 trg->type = LYXP_SET_NODE_SET;
1025 trg->used = src->used;
1026 trg->size = src->used;
1027 trg->ctx_pos = src->ctx_pos;
1028 trg->ctx_size = src->ctx_size;
1029
Michal Vasko08e9b112021-06-11 15:41:17 +02001030 if (trg->size) {
1031 trg->val.nodes = malloc(trg->size * sizeof *trg->val.nodes);
1032 LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), );
1033 memcpy(trg->val.nodes, src->val.nodes, src->used * sizeof *src->val.nodes);
1034 } else {
1035 trg->val.nodes = NULL;
1036 }
Michal Vaskod3678892020-05-21 10:06:58 +02001037 if (src->ht) {
1038 trg->ht = lyht_dup(src->ht);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001039 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02001040 trg->ht = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001041 }
1042 }
1043}
1044
1045/**
1046 * @brief Clear context of all schema nodes.
1047 *
1048 * @param[in] set Set to clear.
Michal Vasko1a09b212021-05-06 13:00:10 +02001049 * @param[in] new_ctx New context state for all the nodes currently in the context.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001050 */
1051static void
Michal Vasko1a09b212021-05-06 13:00:10 +02001052set_scnode_clear_ctx(struct lyxp_set *set, int32_t new_ctx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001053{
1054 uint32_t i;
1055
1056 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001057 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko1a09b212021-05-06 13:00:10 +02001058 set->val.scnodes[i].in_ctx = new_ctx;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001059 } else if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START) {
1060 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001061 }
1062 }
1063}
1064
1065/**
1066 * @brief Remove a node from a set. Removing last node changes
1067 * set into LYXP_SET_EMPTY. Context position aware.
1068 *
1069 * @param[in] set Set to use.
1070 * @param[in] idx Index from @p set of the node to be removed.
1071 */
1072static void
1073set_remove_node(struct lyxp_set *set, uint32_t idx)
1074{
1075 assert(set && (set->type == LYXP_SET_NODE_SET));
1076 assert(idx < set->used);
1077
1078 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
1079
1080 --set->used;
Michal Vasko08e9b112021-06-11 15:41:17 +02001081 if (idx < set->used) {
1082 memmove(&set->val.nodes[idx], &set->val.nodes[idx + 1], (set->used - idx) * sizeof *set->val.nodes);
1083 } else if (!set->used) {
Michal Vaskod3678892020-05-21 10:06:58 +02001084 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001085 }
1086}
1087
1088/**
Michal Vasko2caefc12019-11-14 16:07:56 +01001089 * @brief Remove a node from a set by setting its type to LYXP_NODE_NONE.
Michal Vasko57eab132019-09-24 11:46:26 +02001090 *
1091 * @param[in] set Set to use.
1092 * @param[in] idx Index from @p set of the node to be removed.
1093 */
1094static void
Michal Vasko2caefc12019-11-14 16:07:56 +01001095set_remove_node_none(struct lyxp_set *set, uint32_t idx)
Michal Vasko57eab132019-09-24 11:46:26 +02001096{
1097 assert(set && (set->type == LYXP_SET_NODE_SET));
1098 assert(idx < set->used);
1099
Michal Vasko2caefc12019-11-14 16:07:56 +01001100 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1101 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
1102 }
1103 set->val.nodes[idx].type = LYXP_NODE_NONE;
Michal Vasko57eab132019-09-24 11:46:26 +02001104}
1105
1106/**
Michal Vasko2caefc12019-11-14 16:07:56 +01001107 * @brief Remove all LYXP_NODE_NONE nodes from a set. Removing last node changes
Michal Vasko57eab132019-09-24 11:46:26 +02001108 * set into LYXP_SET_EMPTY. Context position aware.
1109 *
1110 * @param[in] set Set to consolidate.
1111 */
1112static void
Michal Vasko2caefc12019-11-14 16:07:56 +01001113set_remove_nodes_none(struct lyxp_set *set)
Michal Vasko57eab132019-09-24 11:46:26 +02001114{
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01001115 uint32_t i, orig_used, end = 0;
1116 int64_t start;
Michal Vasko57eab132019-09-24 11:46:26 +02001117
Michal Vaskod3678892020-05-21 10:06:58 +02001118 assert(set);
Michal Vasko57eab132019-09-24 11:46:26 +02001119
1120 orig_used = set->used;
1121 set->used = 0;
Michal Vaskod989ba02020-08-24 10:59:24 +02001122 for (i = 0; i < orig_used; ) {
Michal Vasko57eab132019-09-24 11:46:26 +02001123 start = -1;
1124 do {
Michal Vasko2caefc12019-11-14 16:07:56 +01001125 if ((set->val.nodes[i].type != LYXP_NODE_NONE) && (start == -1)) {
Michal Vasko57eab132019-09-24 11:46:26 +02001126 start = i;
Michal Vasko2caefc12019-11-14 16:07:56 +01001127 } else if ((start > -1) && (set->val.nodes[i].type == LYXP_NODE_NONE)) {
Michal Vasko57eab132019-09-24 11:46:26 +02001128 end = i;
1129 ++i;
1130 break;
1131 }
1132
1133 ++i;
1134 if (i == orig_used) {
1135 end = i;
1136 }
1137 } while (i < orig_used);
1138
1139 if (start > -1) {
1140 /* move the whole chunk of valid nodes together */
1141 if (set->used != (unsigned)start) {
1142 memmove(&set->val.nodes[set->used], &set->val.nodes[start], (end - start) * sizeof *set->val.nodes);
1143 }
1144 set->used += end - start;
1145 }
1146 }
Michal Vasko57eab132019-09-24 11:46:26 +02001147}
1148
1149/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02001150 * @brief Check for duplicates in a node set.
1151 *
1152 * @param[in] set Set to check.
1153 * @param[in] node Node to look for in @p set.
1154 * @param[in] node_type Type of @p node.
1155 * @param[in] skip_idx Index from @p set to skip.
1156 * @return LY_ERR
1157 */
1158static LY_ERR
1159set_dup_node_check(const struct lyxp_set *set, const struct lyd_node *node, enum lyxp_node_type node_type, int skip_idx)
1160{
1161 uint32_t i;
1162
Michal Vasko2caefc12019-11-14 16:07:56 +01001163 if (set->ht && node) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001164 return set_dup_node_hash_check(set, (struct lyd_node *)node, node_type, skip_idx);
1165 }
1166
1167 for (i = 0; i < set->used; ++i) {
1168 if ((skip_idx > -1) && (i == (unsigned)skip_idx)) {
1169 continue;
1170 }
1171
1172 if ((set->val.nodes[i].node == node) && (set->val.nodes[i].type == node_type)) {
1173 return LY_EEXIST;
1174 }
1175 }
1176
1177 return LY_SUCCESS;
1178}
1179
Radek Krejci857189e2020-09-01 13:26:36 +02001180ly_bool
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001181lyxp_set_scnode_contains(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type, int skip_idx,
1182 uint32_t *index_p)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001183{
1184 uint32_t i;
1185
1186 for (i = 0; i < set->used; ++i) {
1187 if ((skip_idx > -1) && (i == (unsigned)skip_idx)) {
1188 continue;
1189 }
1190
1191 if ((set->val.scnodes[i].scnode == node) && (set->val.scnodes[i].type == node_type)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001192 if (index_p) {
1193 *index_p = i;
1194 }
1195 return 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001196 }
1197 }
1198
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001199 return 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001200}
1201
Michal Vaskoecd62de2019-11-13 12:35:11 +01001202void
1203lyxp_set_scnode_merge(struct lyxp_set *set1, struct lyxp_set *set2)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001204{
1205 uint32_t orig_used, i, j;
1206
Michal Vaskod3678892020-05-21 10:06:58 +02001207 assert((set1->type == LYXP_SET_SCNODE_SET) && (set2->type == LYXP_SET_SCNODE_SET));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001208
Michal Vaskod3678892020-05-21 10:06:58 +02001209 if (!set2->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001210 return;
1211 }
1212
Michal Vaskod3678892020-05-21 10:06:58 +02001213 if (!set1->used) {
aPiecekadc1e4f2021-10-07 11:15:12 +02001214 /* release hidden allocated data (lyxp_set.size) */
1215 lyxp_set_free_content(set1);
1216 /* direct copying of the entire structure */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001217 memcpy(set1, set2, sizeof *set1);
1218 return;
1219 }
1220
1221 if (set1->used + set2->used > set1->size) {
1222 set1->size = set1->used + set2->used;
1223 set1->val.scnodes = ly_realloc(set1->val.scnodes, set1->size * sizeof *set1->val.scnodes);
1224 LY_CHECK_ERR_RET(!set1->val.scnodes, LOGMEM(set1->ctx), );
1225 }
1226
1227 orig_used = set1->used;
1228
1229 for (i = 0; i < set2->used; ++i) {
1230 for (j = 0; j < orig_used; ++j) {
1231 /* detect duplicities */
1232 if (set1->val.scnodes[j].scnode == set2->val.scnodes[i].scnode) {
1233 break;
1234 }
1235 }
1236
Michal Vasko0587bce2020-12-10 12:19:21 +01001237 if (j < orig_used) {
1238 /* node is there, but update its status if needed */
1239 if (set1->val.scnodes[j].in_ctx == LYXP_SET_SCNODE_START_USED) {
1240 set1->val.scnodes[j].in_ctx = set2->val.scnodes[i].in_ctx;
Michal Vasko1a09b212021-05-06 13:00:10 +02001241 } else if ((set1->val.scnodes[j].in_ctx == LYXP_SET_SCNODE_ATOM_NODE) &&
1242 (set2->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_VAL)) {
1243 set1->val.scnodes[j].in_ctx = set2->val.scnodes[i].in_ctx;
Michal Vasko0587bce2020-12-10 12:19:21 +01001244 }
1245 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001246 memcpy(&set1->val.scnodes[set1->used], &set2->val.scnodes[i], sizeof *set2->val.scnodes);
1247 ++set1->used;
1248 }
1249 }
1250
Michal Vaskod3678892020-05-21 10:06:58 +02001251 lyxp_set_free_content(set2);
1252 set2->type = LYXP_SET_SCNODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001253}
1254
1255/**
1256 * @brief Insert a node into a set. Context position aware.
1257 *
1258 * @param[in] set Set to use.
1259 * @param[in] node Node to insert to @p set.
1260 * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting.
1261 * @param[in] node_type Node type of @p node.
1262 * @param[in] idx Index in @p set to insert into.
1263 */
1264static void
1265set_insert_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx)
1266{
Michal Vaskod3678892020-05-21 10:06:58 +02001267 assert(set && (set->type == LYXP_SET_NODE_SET));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001268
Michal Vaskod3678892020-05-21 10:06:58 +02001269 if (!set->size) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001270 /* first item */
1271 if (idx) {
1272 /* no real harm done, but it is a bug */
1273 LOGINT(set->ctx);
1274 idx = 0;
1275 }
1276 set->val.nodes = malloc(LYXP_SET_SIZE_START * sizeof *set->val.nodes);
1277 LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), );
1278 set->type = LYXP_SET_NODE_SET;
1279 set->used = 0;
1280 set->size = LYXP_SET_SIZE_START;
1281 set->ctx_pos = 1;
1282 set->ctx_size = 1;
1283 set->ht = NULL;
1284 } else {
1285 /* not an empty set */
1286 if (set->used == set->size) {
1287
1288 /* set is full */
Michal Vasko871df522022-04-06 12:14:41 +02001289 set->val.nodes = ly_realloc(set->val.nodes, (set->size * LYXP_SET_SIZE_MUL_STEP) * sizeof *set->val.nodes);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001290 LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), );
Michal Vasko871df522022-04-06 12:14:41 +02001291 set->size *= LYXP_SET_SIZE_MUL_STEP;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001292 }
1293
1294 if (idx > set->used) {
1295 LOGINT(set->ctx);
1296 idx = set->used;
1297 }
1298
1299 /* make space for the new node */
1300 if (idx < set->used) {
1301 memmove(&set->val.nodes[idx + 1], &set->val.nodes[idx], (set->used - idx) * sizeof *set->val.nodes);
1302 }
1303 }
1304
1305 /* finally assign the value */
1306 set->val.nodes[idx].node = (struct lyd_node *)node;
1307 set->val.nodes[idx].type = node_type;
1308 set->val.nodes[idx].pos = pos;
1309 ++set->used;
1310
Michal Vasko2416fdd2021-10-01 11:07:10 +02001311 /* add into hash table */
1312 set_insert_node_hash(set, (struct lyd_node *)node, node_type);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001313}
1314
Michal Vasko7333cb32022-07-29 16:30:29 +02001315/**
1316 * @brief Insert schema node into set.
1317 *
1318 * @param[in] set Set to insert into.
1319 * @param[in] node Node to insert.
1320 * @param[in] node_type Node type of @p node.
1321 * @param[in] axis Axis that @p node was reached on.
1322 * @param[out] index_p Optional pointer to store index if the inserted @p node.
1323 * @return LY_SUCCESS on success.
1324 * @return LY_EMEM on memory allocation failure.
1325 */
1326static LY_ERR
1327set_scnode_insert_node(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type,
1328 enum lyxp_axis axis, uint32_t *index_p)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001329{
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001330 uint32_t index;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001331
1332 assert(set->type == LYXP_SET_SCNODE_SET);
1333
Michal Vasko871df522022-04-06 12:14:41 +02001334 if (!set->size) {
1335 /* first item */
1336 set->val.scnodes = malloc(LYXP_SET_SIZE_START * sizeof *set->val.scnodes);
1337 LY_CHECK_ERR_RET(!set->val.scnodes, LOGMEM(set->ctx), LY_EMEM);
1338 set->type = LYXP_SET_SCNODE_SET;
1339 set->used = 0;
1340 set->size = LYXP_SET_SIZE_START;
1341 set->ctx_pos = 1;
1342 set->ctx_size = 1;
1343 set->ht = NULL;
1344 }
1345
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001346 if (lyxp_set_scnode_contains(set, node, node_type, -1, &index)) {
Michal Vasko7333cb32022-07-29 16:30:29 +02001347 /* BUG if axes differs, this new one is thrown away */
Radek Krejcif13b87b2020-12-01 22:02:17 +01001348 set->val.scnodes[index].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001349 } else {
1350 if (set->used == set->size) {
Michal Vasko871df522022-04-06 12:14:41 +02001351 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 +02001352 LY_CHECK_ERR_RET(!set->val.scnodes, LOGMEM(set->ctx), LY_EMEM);
Michal Vasko871df522022-04-06 12:14:41 +02001353 set->size *= LYXP_SET_SIZE_MUL_STEP;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001354 }
1355
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001356 index = set->used;
1357 set->val.scnodes[index].scnode = (struct lysc_node *)node;
1358 set->val.scnodes[index].type = node_type;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001359 set->val.scnodes[index].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko7333cb32022-07-29 16:30:29 +02001360 set->val.scnodes[index].axis = axis;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001361 ++set->used;
1362 }
1363
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001364 if (index_p) {
1365 *index_p = index;
1366 }
1367
1368 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001369}
1370
1371/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02001372 * @brief Set all nodes with ctx 1 to a new unique context value.
1373 *
1374 * @param[in] set Set to modify.
1375 * @return New context value.
1376 */
Michal Vasko5c4e5892019-11-14 12:31:38 +01001377static int32_t
Michal Vasko03ff5a72019-09-11 13:49:33 +02001378set_scnode_new_in_ctx(struct lyxp_set *set)
1379{
Michal Vasko5c4e5892019-11-14 12:31:38 +01001380 uint32_t i;
1381 int32_t ret_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001382
1383 assert(set->type == LYXP_SET_SCNODE_SET);
1384
Radek Krejcif13b87b2020-12-01 22:02:17 +01001385 ret_ctx = LYXP_SET_SCNODE_ATOM_PRED_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001386retry:
1387 for (i = 0; i < set->used; ++i) {
1388 if (set->val.scnodes[i].in_ctx >= ret_ctx) {
1389 ret_ctx = set->val.scnodes[i].in_ctx + 1;
1390 goto retry;
1391 }
1392 }
1393 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001394 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001395 set->val.scnodes[i].in_ctx = ret_ctx;
1396 }
1397 }
1398
1399 return ret_ctx;
1400}
1401
1402/**
1403 * @brief Get unique @p node position in the data.
1404 *
1405 * @param[in] node Node to find.
1406 * @param[in] node_type Node type of @p node.
1407 * @param[in] root Root node.
1408 * @param[in] root_type Type of the XPath @p root node.
1409 * @param[in] prev Node that we think is before @p node in DFS from @p root. Can optionally
1410 * be used to increase efficiency and start the DFS from this node.
1411 * @param[in] prev_pos Node @p prev position. Optional, but must be set if @p prev is set.
1412 * @return Node position.
1413 */
1414static uint32_t
1415get_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 +02001416 enum lyxp_node_type root_type, const struct lyd_node **prev, uint32_t *prev_pos)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001417{
Michal Vasko56daf732020-08-10 10:57:18 +02001418 const struct lyd_node *elem = NULL, *top_sibling;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001419 uint32_t pos = 1;
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001420 ly_bool found = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001421
1422 assert(prev && prev_pos && !root->prev->next);
1423
1424 if ((node_type == LYXP_NODE_ROOT) || (node_type == LYXP_NODE_ROOT_CONFIG)) {
1425 return 0;
1426 }
1427
1428 if (*prev) {
1429 /* start from the previous element instead from the root */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001430 pos = *prev_pos;
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001431 for (top_sibling = *prev; top_sibling->parent; top_sibling = lyd_parent(top_sibling)) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001432 goto dfs_search;
1433 }
1434
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001435 LY_LIST_FOR(root, top_sibling) {
Michal Vasko56daf732020-08-10 10:57:18 +02001436 LYD_TREE_DFS_BEGIN(top_sibling, elem) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001437dfs_search:
Michal Vaskoa9309bb2021-07-09 09:31:55 +02001438 LYD_TREE_DFS_continue = 0;
1439
Michal Vasko56daf732020-08-10 10:57:18 +02001440 if (*prev && !elem) {
1441 /* resume previous DFS */
1442 elem = LYD_TREE_DFS_next = (struct lyd_node *)*prev;
1443 LYD_TREE_DFS_continue = 0;
1444 }
1445
Michal Vasko482a6822022-05-06 08:56:12 +02001446 if (!elem->schema || ((root_type == LYXP_NODE_ROOT_CONFIG) && (elem->schema->flags & LYS_CONFIG_R))) {
Michal Vasko56daf732020-08-10 10:57:18 +02001447 /* skip */
1448 LYD_TREE_DFS_continue = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001449 } else {
Michal Vasko56daf732020-08-10 10:57:18 +02001450 if (elem == node) {
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001451 found = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001452 break;
1453 }
Michal Vasko56daf732020-08-10 10:57:18 +02001454 ++pos;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001455 }
Michal Vasko56daf732020-08-10 10:57:18 +02001456
1457 LYD_TREE_DFS_END(top_sibling, elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001458 }
1459
1460 /* node found */
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001461 if (found) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001462 break;
1463 }
1464 }
1465
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001466 if (!found) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001467 if (!(*prev)) {
1468 /* we went from root and failed to find it, cannot be */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001469 LOGINT(LYD_CTX(node));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001470 return 0;
1471 } else {
Michal Vasko56daf732020-08-10 10:57:18 +02001472 /* start the search again from the beginning */
1473 *prev = root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001474
Michal Vasko56daf732020-08-10 10:57:18 +02001475 top_sibling = root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001476 pos = 1;
1477 goto dfs_search;
1478 }
1479 }
1480
1481 /* remember the last found node for next time */
1482 *prev = node;
1483 *prev_pos = pos;
1484
1485 return pos;
1486}
1487
1488/**
1489 * @brief Assign (fill) missing node positions.
1490 *
1491 * @param[in] set Set to fill positions in.
1492 * @param[in] root Context root node.
1493 * @param[in] root_type Context root type.
1494 * @return LY_ERR
1495 */
1496static LY_ERR
1497set_assign_pos(struct lyxp_set *set, const struct lyd_node *root, enum lyxp_node_type root_type)
1498{
1499 const struct lyd_node *prev = NULL, *tmp_node;
1500 uint32_t i, tmp_pos = 0;
1501
1502 for (i = 0; i < set->used; ++i) {
1503 if (!set->val.nodes[i].pos) {
1504 tmp_node = NULL;
1505 switch (set->val.nodes[i].type) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001506 case LYXP_NODE_META:
1507 tmp_node = set->val.meta[i].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001508 if (!tmp_node) {
1509 LOGINT_RET(root->schema->module->ctx);
1510 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01001511 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001512 case LYXP_NODE_ELEM:
1513 case LYXP_NODE_TEXT:
1514 if (!tmp_node) {
1515 tmp_node = set->val.nodes[i].node;
1516 }
1517 set->val.nodes[i].pos = get_node_pos(tmp_node, set->val.nodes[i].type, root, root_type, &prev, &tmp_pos);
1518 break;
1519 default:
1520 /* all roots have position 0 */
1521 break;
1522 }
1523 }
1524 }
1525
1526 return LY_SUCCESS;
1527}
1528
1529/**
Michal Vasko9f96a052020-03-10 09:41:45 +01001530 * @brief Get unique @p meta position in the parent metadata.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001531 *
Michal Vasko9f96a052020-03-10 09:41:45 +01001532 * @param[in] meta Metadata to use.
1533 * @return Metadata position.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001534 */
1535static uint16_t
Michal Vasko9f96a052020-03-10 09:41:45 +01001536get_meta_pos(struct lyd_meta *meta)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001537{
1538 uint16_t pos = 0;
Michal Vasko9f96a052020-03-10 09:41:45 +01001539 struct lyd_meta *meta2;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001540
Michal Vasko9f96a052020-03-10 09:41:45 +01001541 for (meta2 = meta->parent->meta; meta2 && (meta2 != meta); meta2 = meta2->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001542 ++pos;
1543 }
1544
Michal Vasko9f96a052020-03-10 09:41:45 +01001545 assert(meta2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001546 return pos;
1547}
1548
1549/**
1550 * @brief Compare 2 nodes in respect to XPath document order.
1551 *
1552 * @param[in] item1 1st node.
1553 * @param[in] item2 2nd node.
1554 * @return If 1st > 2nd returns 1, 1st == 2nd returns 0, and 1st < 2nd returns -1.
1555 */
1556static int
1557set_sort_compare(struct lyxp_set_node *item1, struct lyxp_set_node *item2)
1558{
Michal Vasko9f96a052020-03-10 09:41:45 +01001559 uint32_t meta_pos1 = 0, meta_pos2 = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001560
1561 if (item1->pos < item2->pos) {
1562 return -1;
1563 }
1564
1565 if (item1->pos > item2->pos) {
1566 return 1;
1567 }
1568
1569 /* node positions are equal, the fun case */
1570
1571 /* 1st ELEM - == - 2nd TEXT, 1st TEXT - == - 2nd ELEM */
1572 /* special case since text nodes are actually saved as their parents */
1573 if ((item1->node == item2->node) && (item1->type != item2->type)) {
1574 if (item1->type == LYXP_NODE_ELEM) {
1575 assert(item2->type == LYXP_NODE_TEXT);
1576 return -1;
1577 } else {
1578 assert((item1->type == LYXP_NODE_TEXT) && (item2->type == LYXP_NODE_ELEM));
1579 return 1;
1580 }
1581 }
1582
Michal Vasko9f96a052020-03-10 09:41:45 +01001583 /* we need meta positions now */
1584 if (item1->type == LYXP_NODE_META) {
1585 meta_pos1 = get_meta_pos((struct lyd_meta *)item1->node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001586 }
Michal Vasko9f96a052020-03-10 09:41:45 +01001587 if (item2->type == LYXP_NODE_META) {
1588 meta_pos2 = get_meta_pos((struct lyd_meta *)item2->node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001589 }
1590
Michal Vasko9f96a052020-03-10 09:41:45 +01001591 /* 1st ROOT - 2nd ROOT, 1st ELEM - 2nd ELEM, 1st TEXT - 2nd TEXT, 1st META - =pos= - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001592 /* check for duplicates */
1593 if (item1->node == item2->node) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001594 assert((item1->type == item2->type) && ((item1->type != LYXP_NODE_META) || (meta_pos1 == meta_pos2)));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001595 return 0;
1596 }
1597
Michal Vasko9f96a052020-03-10 09:41:45 +01001598 /* 1st ELEM - 2nd TEXT, 1st ELEM - any pos - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001599 /* elem is always first, 2nd node is after it */
1600 if (item1->type == LYXP_NODE_ELEM) {
1601 assert(item2->type != LYXP_NODE_ELEM);
1602 return -1;
1603 }
1604
Michal Vasko9f96a052020-03-10 09:41:45 +01001605 /* 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 +02001606 /* 2nd is before 1st */
Michal Vasko69730152020-10-09 16:30:07 +02001607 if (((item1->type == LYXP_NODE_TEXT) &&
1608 ((item2->type == LYXP_NODE_ELEM) || (item2->type == LYXP_NODE_META))) ||
1609 ((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_ELEM)) ||
1610 (((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_META)) &&
1611 (meta_pos1 > meta_pos2))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001612 return 1;
1613 }
1614
Michal Vasko9f96a052020-03-10 09:41:45 +01001615 /* 1st META - any pos - 2nd TEXT, 1st META <pos< - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001616 /* 2nd is after 1st */
1617 return -1;
1618}
1619
1620/**
1621 * @brief Set cast for comparisons.
1622 *
Michal Vasko8abcecc2022-07-28 09:55:01 +02001623 * @param[in,out] trg Target set to cast source into.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001624 * @param[in] src Source set.
1625 * @param[in] type Target set type.
1626 * @param[in] src_idx Source set node index.
Michal Vasko8abcecc2022-07-28 09:55:01 +02001627 * @return LY_SUCCESS on success.
1628 * @return LY_ERR value on error.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001629 */
1630static LY_ERR
Michal Vasko8abcecc2022-07-28 09:55:01 +02001631set_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 +02001632{
1633 assert(src->type == LYXP_SET_NODE_SET);
1634
1635 set_init(trg, src);
1636
1637 /* insert node into target set */
1638 set_insert_node(trg, src->val.nodes[src_idx].node, src->val.nodes[src_idx].pos, src->val.nodes[src_idx].type, 0);
1639
1640 /* cast target set appropriately */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001641 return lyxp_set_cast(trg, type);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001642}
1643
Michal Vasko4c7763f2020-07-27 17:40:37 +02001644/**
1645 * @brief Set content canonization for comparisons.
1646 *
Michal Vasko8abcecc2022-07-28 09:55:01 +02001647 * @param[in,out] set Set to canonize.
Michal Vasko4c7763f2020-07-27 17:40:37 +02001648 * @param[in] xp_node Source XPath node/meta to use for canonization.
Michal Vasko8abcecc2022-07-28 09:55:01 +02001649 * @return LY_SUCCESS on success.
1650 * @return LY_ERR value on error.
Michal Vasko4c7763f2020-07-27 17:40:37 +02001651 */
1652static LY_ERR
Michal Vasko8abcecc2022-07-28 09:55:01 +02001653set_comp_canonize(struct lyxp_set *set, const struct lyxp_set_node *xp_node)
Michal Vasko4c7763f2020-07-27 17:40:37 +02001654{
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001655 const struct lysc_type *type = NULL;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001656 struct lyd_value val;
1657 struct ly_err_item *err = NULL;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001658 LY_ERR rc;
1659
1660 /* is there anything to canonize even? */
Michal Vasko8abcecc2022-07-28 09:55:01 +02001661 if (set->type == LYXP_SET_STRING) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02001662 /* do we have a type to use for canonization? */
1663 if ((xp_node->type == LYXP_NODE_ELEM) && (xp_node->node->schema->nodetype & LYD_NODE_TERM)) {
1664 type = ((struct lyd_node_term *)xp_node->node)->value.realtype;
1665 } else if (xp_node->type == LYXP_NODE_META) {
1666 type = ((struct lyd_meta *)xp_node->node)->value.realtype;
1667 }
1668 }
1669 if (!type) {
Michal Vasko8abcecc2022-07-28 09:55:01 +02001670 /* no canonization needed/possible */
1671 return LY_SUCCESS;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001672 }
1673
Michal Vasko8abcecc2022-07-28 09:55:01 +02001674 /* check for built-in types without required canonization */
1675 if ((type->basetype == LY_TYPE_STRING) && (type->plugin->store == lyplg_type_store_string)) {
1676 /* string */
1677 return LY_SUCCESS;
1678 }
1679 if ((type->basetype == LY_TYPE_BOOL) && (type->plugin->store == lyplg_type_store_boolean)) {
1680 /* boolean */
1681 return LY_SUCCESS;
1682 }
1683 if ((type->basetype == LY_TYPE_ENUM) && (type->plugin->store == lyplg_type_store_enum)) {
1684 /* enumeration */
1685 return LY_SUCCESS;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001686 }
1687
Michal Vasko8abcecc2022-07-28 09:55:01 +02001688 /* print canonized string, ignore errors, the value may not satisfy schema constraints */
1689 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 +01001690 LYD_HINT_DATA, xp_node->node->schema, &val, NULL, &err);
Michal Vasko4c7763f2020-07-27 17:40:37 +02001691 ly_err_free(err);
1692 if (rc) {
Michal Vasko8abcecc2022-07-28 09:55:01 +02001693 /* invalid value, function store automaticaly dealloc value when fail */
1694 return LY_SUCCESS;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001695 }
1696
Michal Vasko8abcecc2022-07-28 09:55:01 +02001697 /* use the canonized string value */
1698 free(set->val.str);
1699 set->val.str = strdup(lyd_value_get_canonical(set->ctx, &val));
1700 type->plugin->free(set->ctx, &val);
1701 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), LY_EMEM);
Michal Vasko4c7763f2020-07-27 17:40:37 +02001702
Michal Vasko4c7763f2020-07-27 17:40:37 +02001703 return LY_SUCCESS;
1704}
1705
Michal Vasko03ff5a72019-09-11 13:49:33 +02001706/**
1707 * @brief Bubble sort @p set into XPath document order.
Michal Vasko49fec8e2022-05-24 10:28:33 +02001708 * Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001709 *
1710 * @param[in] set Set to sort.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001711 * @return How many times the whole set was traversed - 1 (if set was sorted, returns 0).
1712 */
1713static int
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001714set_sort(struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001715{
1716 uint32_t i, j;
Radek Krejci1deb5be2020-08-26 16:43:36 +02001717 int ret = 0, cmp;
Radek Krejci857189e2020-09-01 13:26:36 +02001718 ly_bool inverted, change;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001719 const struct lyd_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001720 struct lyxp_set_node item;
1721 struct lyxp_set_hash_node hnode;
1722 uint64_t hash;
1723
Michal Vasko3cf8fbf2020-05-27 15:21:21 +02001724 if ((set->type != LYXP_SET_NODE_SET) || (set->used < 2)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001725 return 0;
1726 }
1727
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001728 /* find first top-level node to be used as anchor for positions */
Michal Vasko4a7d4d62021-12-13 17:05:06 +01001729 for (root = set->tree; root->parent; root = lyd_parent(root)) {}
Michal Vaskod989ba02020-08-24 10:59:24 +02001730 for ( ; root->prev->next; root = root->prev) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001731
1732 /* fill positions */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001733 if (set_assign_pos(set, root, set->root_type)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001734 return -1;
1735 }
1736
Michal Vasko88a9e802022-05-24 10:50:28 +02001737#ifndef NDEBUG
Michal Vasko03ff5a72019-09-11 13:49:33 +02001738 LOGDBG(LY_LDGXPATH, "SORT BEGIN");
1739 print_set_debug(set);
Michal Vasko88a9e802022-05-24 10:50:28 +02001740#endif
Michal Vasko03ff5a72019-09-11 13:49:33 +02001741
1742 for (i = 0; i < set->used; ++i) {
1743 inverted = 0;
1744 change = 0;
1745
1746 for (j = 1; j < set->used - i; ++j) {
1747 /* compare node positions */
1748 if (inverted) {
1749 cmp = set_sort_compare(&set->val.nodes[j], &set->val.nodes[j - 1]);
1750 } else {
1751 cmp = set_sort_compare(&set->val.nodes[j - 1], &set->val.nodes[j]);
1752 }
1753
1754 /* swap if needed */
1755 if ((inverted && (cmp < 0)) || (!inverted && (cmp > 0))) {
1756 change = 1;
1757
1758 item = set->val.nodes[j - 1];
1759 set->val.nodes[j - 1] = set->val.nodes[j];
1760 set->val.nodes[j] = item;
1761 } else {
1762 /* whether node_pos1 should be smaller than node_pos2 or the other way around */
1763 inverted = !inverted;
1764 }
1765 }
1766
1767 ++ret;
1768
1769 if (!change) {
1770 break;
1771 }
1772 }
1773
Michal Vasko88a9e802022-05-24 10:50:28 +02001774#ifndef NDEBUG
Michal Vasko03ff5a72019-09-11 13:49:33 +02001775 LOGDBG(LY_LDGXPATH, "SORT END %d", ret);
1776 print_set_debug(set);
Michal Vasko88a9e802022-05-24 10:50:28 +02001777#endif
Michal Vasko03ff5a72019-09-11 13:49:33 +02001778
1779 /* check node hashes */
1780 if (set->used >= LYD_HT_MIN_ITEMS) {
1781 assert(set->ht);
1782 for (i = 0; i < set->used; ++i) {
1783 hnode.node = set->val.nodes[i].node;
1784 hnode.type = set->val.nodes[i].type;
1785
1786 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
1787 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
1788 hash = dict_hash_multi(hash, NULL, 0);
1789
1790 assert(!lyht_find(set->ht, &hnode, hash, NULL));
1791 }
1792 }
1793
1794 return ret - 1;
1795}
1796
Michal Vasko03ff5a72019-09-11 13:49:33 +02001797/**
1798 * @brief Merge 2 sorted sets into one.
1799 *
1800 * @param[in,out] trg Set to merge into. Duplicates are removed.
1801 * @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 +02001802 * @return LY_ERR
1803 */
1804static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001805set_sorted_merge(struct lyxp_set *trg, struct lyxp_set *src)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001806{
1807 uint32_t i, j, k, count, dup_count;
1808 int cmp;
1809 const struct lyd_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001810
Michal Vaskod3678892020-05-21 10:06:58 +02001811 if ((trg->type != LYXP_SET_NODE_SET) || (src->type != LYXP_SET_NODE_SET)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001812 return LY_EINVAL;
1813 }
1814
Michal Vaskod3678892020-05-21 10:06:58 +02001815 if (!src->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001816 return LY_SUCCESS;
Michal Vaskod3678892020-05-21 10:06:58 +02001817 } else if (!trg->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001818 set_fill_set(trg, src);
Michal Vaskod3678892020-05-21 10:06:58 +02001819 lyxp_set_free_content(src);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001820 return LY_SUCCESS;
1821 }
1822
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001823 /* find first top-level node to be used as anchor for positions */
Michal Vasko0143b682021-12-13 17:13:09 +01001824 for (root = trg->tree; root->parent; root = lyd_parent(root)) {}
Michal Vaskod989ba02020-08-24 10:59:24 +02001825 for ( ; root->prev->next; root = root->prev) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001826
1827 /* fill positions */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001828 if (set_assign_pos(trg, root, trg->root_type) || set_assign_pos(src, root, src->root_type)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001829 return LY_EINT;
1830 }
1831
1832#ifndef NDEBUG
1833 LOGDBG(LY_LDGXPATH, "MERGE target");
1834 print_set_debug(trg);
1835 LOGDBG(LY_LDGXPATH, "MERGE source");
1836 print_set_debug(src);
1837#endif
1838
1839 /* make memory for the merge (duplicates are not detected yet, so space
1840 * will likely be wasted on them, too bad) */
1841 if (trg->size - trg->used < src->used) {
1842 trg->size = trg->used + src->used;
1843
1844 trg->val.nodes = ly_realloc(trg->val.nodes, trg->size * sizeof *trg->val.nodes);
1845 LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx), LY_EMEM);
1846 }
1847
1848 i = 0;
1849 j = 0;
1850 count = 0;
1851 dup_count = 0;
1852 do {
1853 cmp = set_sort_compare(&src->val.nodes[i], &trg->val.nodes[j]);
1854 if (!cmp) {
1855 if (!count) {
1856 /* duplicate, just skip it */
1857 ++i;
1858 ++j;
1859 } else {
1860 /* we are copying something already, so let's copy the duplicate too,
1861 * we are hoping that afterwards there are some more nodes to
1862 * copy and this way we can copy them all together */
1863 ++count;
1864 ++dup_count;
1865 ++i;
1866 ++j;
1867 }
1868 } else if (cmp < 0) {
1869 /* inserting src node into trg, just remember it for now */
1870 ++count;
1871 ++i;
1872
1873 /* insert the hash now */
1874 set_insert_node_hash(trg, src->val.nodes[i - 1].node, src->val.nodes[i - 1].type);
1875 } else if (count) {
1876copy_nodes:
1877 /* time to actually copy the nodes, we have found the largest block of nodes */
1878 memmove(&trg->val.nodes[j + (count - dup_count)],
1879 &trg->val.nodes[j],
1880 (trg->used - j) * sizeof *trg->val.nodes);
1881 memcpy(&trg->val.nodes[j - dup_count], &src->val.nodes[i - count], count * sizeof *src->val.nodes);
1882
1883 trg->used += count - dup_count;
1884 /* do not change i, except the copying above, we are basically doing exactly what is in the else branch below */
1885 j += count - dup_count;
1886
1887 count = 0;
1888 dup_count = 0;
1889 } else {
1890 ++j;
1891 }
1892 } while ((i < src->used) && (j < trg->used));
1893
1894 if ((i < src->used) || count) {
1895 /* insert all the hashes first */
1896 for (k = i; k < src->used; ++k) {
1897 set_insert_node_hash(trg, src->val.nodes[k].node, src->val.nodes[k].type);
1898 }
1899
1900 /* loop ended, but we need to copy something at trg end */
1901 count += src->used - i;
1902 i = src->used;
1903 goto copy_nodes;
1904 }
1905
1906 /* we are inserting hashes before the actual node insert, which causes
1907 * situations when there were initially not enough items for a hash table,
1908 * but even after some were inserted, hash table was not created (during
1909 * insertion the number of items is not updated yet) */
1910 if (!trg->ht && (trg->used >= LYD_HT_MIN_ITEMS)) {
1911 set_insert_node_hash(trg, NULL, 0);
1912 }
1913
1914#ifndef NDEBUG
1915 LOGDBG(LY_LDGXPATH, "MERGE result");
1916 print_set_debug(trg);
1917#endif
1918
Michal Vaskod3678892020-05-21 10:06:58 +02001919 lyxp_set_free_content(src);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001920 return LY_SUCCESS;
1921}
1922
Michal Vasko14676352020-05-29 11:35:55 +02001923LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02001924lyxp_check_token(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t tok_idx, enum lyxp_token want_tok)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001925{
Michal Vasko004d3152020-06-11 19:59:22 +02001926 if (exp->used == tok_idx) {
Michal Vasko14676352020-05-29 11:35:55 +02001927 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001928 LOGVAL(ctx, LY_VCODE_XP_EOF);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001929 }
Michal Vasko14676352020-05-29 11:35:55 +02001930 return LY_EINCOMPLETE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001931 }
1932
Michal Vasko004d3152020-06-11 19:59:22 +02001933 if (want_tok && (exp->tokens[tok_idx] != want_tok)) {
Michal Vasko14676352020-05-29 11:35:55 +02001934 if (ctx) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02001935 LOGVAL(ctx, LY_VCODE_XP_INTOK2, lyxp_token2str(exp->tokens[tok_idx]),
1936 &exp->expr[exp->tok_pos[tok_idx]], lyxp_token2str(want_tok));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001937 }
Michal Vasko14676352020-05-29 11:35:55 +02001938 return LY_ENOT;
1939 }
1940
1941 return LY_SUCCESS;
1942}
1943
Michal Vasko004d3152020-06-11 19:59:22 +02001944LY_ERR
1945lyxp_next_token(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_token want_tok)
1946{
1947 LY_CHECK_RET(lyxp_check_token(ctx, exp, *tok_idx, want_tok));
1948
1949 /* skip the token */
1950 ++(*tok_idx);
1951
1952 return LY_SUCCESS;
1953}
1954
Michal Vasko14676352020-05-29 11:35:55 +02001955/* just like lyxp_check_token() but tests for 2 tokens */
1956static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02001957exp_check_token2(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t tok_idx, enum lyxp_token want_tok1,
Radek Krejci0f969882020-08-21 16:56:47 +02001958 enum lyxp_token want_tok2)
Michal Vasko14676352020-05-29 11:35:55 +02001959{
Michal Vasko004d3152020-06-11 19:59:22 +02001960 if (exp->used == tok_idx) {
Michal Vasko14676352020-05-29 11:35:55 +02001961 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001962 LOGVAL(ctx, LY_VCODE_XP_EOF);
Michal Vasko14676352020-05-29 11:35:55 +02001963 }
1964 return LY_EINCOMPLETE;
1965 }
1966
Michal Vasko004d3152020-06-11 19:59:22 +02001967 if ((exp->tokens[tok_idx] != want_tok1) && (exp->tokens[tok_idx] != want_tok2)) {
Michal Vasko14676352020-05-29 11:35:55 +02001968 if (ctx) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02001969 LOGVAL(ctx, LY_VCODE_XP_INTOK, lyxp_token2str(exp->tokens[tok_idx]),
Michal Vasko0b468e62020-10-19 09:33:04 +02001970 &exp->expr[exp->tok_pos[tok_idx]]);
Michal Vasko14676352020-05-29 11:35:55 +02001971 }
1972 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001973 }
1974
1975 return LY_SUCCESS;
1976}
1977
Michal Vasko4911eeb2021-06-28 11:23:05 +02001978LY_ERR
1979lyxp_next_token2(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_token want_tok1,
1980 enum lyxp_token want_tok2)
1981{
1982 LY_CHECK_RET(exp_check_token2(ctx, exp, *tok_idx, want_tok1, want_tok2));
1983
1984 /* skip the token */
1985 ++(*tok_idx);
1986
1987 return LY_SUCCESS;
1988}
1989
Michal Vasko03ff5a72019-09-11 13:49:33 +02001990/**
1991 * @brief Stack operation push on the repeat array.
1992 *
1993 * @param[in] exp Expression to use.
Michal Vasko004d3152020-06-11 19:59:22 +02001994 * @param[in] tok_idx Position in the expresion \p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001995 * @param[in] repeat_op_idx Index from \p exp of the operator token. This value is pushed.
1996 */
1997static void
Michal Vasko004d3152020-06-11 19:59:22 +02001998exp_repeat_push(struct lyxp_expr *exp, uint16_t tok_idx, uint16_t repeat_op_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001999{
2000 uint16_t i;
2001
Michal Vasko004d3152020-06-11 19:59:22 +02002002 if (exp->repeat[tok_idx]) {
Radek Krejci1e008d22020-08-17 11:37:37 +02002003 for (i = 0; exp->repeat[tok_idx][i]; ++i) {}
Michal Vasko004d3152020-06-11 19:59:22 +02002004 exp->repeat[tok_idx] = realloc(exp->repeat[tok_idx], (i + 2) * sizeof *exp->repeat[tok_idx]);
2005 LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), );
2006 exp->repeat[tok_idx][i] = repeat_op_idx;
2007 exp->repeat[tok_idx][i + 1] = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002008 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02002009 exp->repeat[tok_idx] = calloc(2, sizeof *exp->repeat[tok_idx]);
2010 LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), );
2011 exp->repeat[tok_idx][0] = repeat_op_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002012 }
2013}
2014
2015/**
2016 * @brief Reparse Predicate. Logs directly on error.
2017 *
2018 * [7] Predicate ::= '[' Expr ']'
2019 *
2020 * @param[in] ctx Context for logging.
2021 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002022 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002023 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002024 * @return LY_ERR
2025 */
2026static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002027reparse_predicate(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t depth)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002028{
2029 LY_ERR rc;
2030
Michal Vasko004d3152020-06-11 19:59:22 +02002031 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002032 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002033 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002034
aPiecekbf968d92021-05-27 14:35:05 +02002035 rc = reparse_or_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002036 LY_CHECK_RET(rc);
2037
Michal Vasko004d3152020-06-11 19:59:22 +02002038 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002039 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002040 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002041
2042 return LY_SUCCESS;
2043}
2044
2045/**
2046 * @brief Reparse RelativeLocationPath. Logs directly on error.
2047 *
2048 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
2049 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
2050 * [6] NodeTest ::= NameTest | NodeType '(' ')'
2051 *
2052 * @param[in] ctx Context for logging.
2053 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002054 * @param[in] tok_idx Position in the expression \p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002055 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002056 * @return LY_ERR (LY_EINCOMPLETE on forward reference)
2057 */
2058static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002059reparse_relative_location_path(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t depth)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002060{
2061 LY_ERR rc;
2062
Michal Vasko004d3152020-06-11 19:59:22 +02002063 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002064 LY_CHECK_RET(rc);
2065
2066 goto step;
2067 do {
2068 /* '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02002069 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002070
Michal Vasko004d3152020-06-11 19:59:22 +02002071 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002072 LY_CHECK_RET(rc);
2073step:
2074 /* Step */
Michal Vasko004d3152020-06-11 19:59:22 +02002075 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002076 case LYXP_TOKEN_DOT:
Michal Vasko004d3152020-06-11 19:59:22 +02002077 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002078 break;
2079
2080 case LYXP_TOKEN_DDOT:
Michal Vasko004d3152020-06-11 19:59:22 +02002081 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002082 break;
2083
Michal Vasko49fec8e2022-05-24 10:28:33 +02002084 case LYXP_TOKEN_AXISNAME:
2085 ++(*tok_idx);
2086
2087 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_DCOLON);
2088 LY_CHECK_RET(rc);
2089
2090 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02002091 case LYXP_TOKEN_AT:
Michal Vasko004d3152020-06-11 19:59:22 +02002092 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002093
Michal Vasko004d3152020-06-11 19:59:22 +02002094 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002095 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002096 if ((exp->tokens[*tok_idx] != LYXP_TOKEN_NAMETEST) && (exp->tokens[*tok_idx] != LYXP_TOKEN_NODETYPE)) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02002097 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 +02002098 return LY_EVALID;
2099 }
Michal Vasko49fec8e2022-05-24 10:28:33 +02002100 if (exp->tokens[*tok_idx] == LYXP_TOKEN_NODETYPE) {
2101 goto reparse_nodetype;
2102 }
Radek Krejci0f969882020-08-21 16:56:47 +02002103 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02002104 case LYXP_TOKEN_NAMETEST:
Michal Vasko004d3152020-06-11 19:59:22 +02002105 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002106 goto reparse_predicate;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002107
2108 case LYXP_TOKEN_NODETYPE:
Michal Vasko49fec8e2022-05-24 10:28:33 +02002109reparse_nodetype:
Michal Vasko004d3152020-06-11 19:59:22 +02002110 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002111
2112 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02002113 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002114 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002115 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002116
2117 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02002118 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002119 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002120 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002121
2122reparse_predicate:
2123 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002124 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
aPiecekbf968d92021-05-27 14:35:05 +02002125 rc = reparse_predicate(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002126 LY_CHECK_RET(rc);
2127 }
2128 break;
2129 default:
Michal Vasko49fec8e2022-05-24 10:28:33 +02002130 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 +02002131 return LY_EVALID;
2132 }
Michal Vasko004d3152020-06-11 19:59:22 +02002133 } while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02002134
2135 return LY_SUCCESS;
2136}
2137
2138/**
2139 * @brief Reparse AbsoluteLocationPath. Logs directly on error.
2140 *
2141 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
2142 *
2143 * @param[in] ctx Context for logging.
2144 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002145 * @param[in] tok_idx Position in the expression \p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002146 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002147 * @return LY_ERR
2148 */
2149static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002150reparse_absolute_location_path(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t depth)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002151{
2152 LY_ERR rc;
2153
Michal Vasko004d3152020-06-11 19:59:22 +02002154 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 +02002155
2156 /* '/' RelativeLocationPath? */
Michal Vasko004d3152020-06-11 19:59:22 +02002157 if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002158 /* '/' */
Michal Vasko004d3152020-06-11 19:59:22 +02002159 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002160
Michal Vasko004d3152020-06-11 19:59:22 +02002161 if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002162 return LY_SUCCESS;
2163 }
Michal Vasko004d3152020-06-11 19:59:22 +02002164 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002165 case LYXP_TOKEN_DOT:
2166 case LYXP_TOKEN_DDOT:
Michal Vasko49fec8e2022-05-24 10:28:33 +02002167 case LYXP_TOKEN_AXISNAME:
Michal Vasko03ff5a72019-09-11 13:49:33 +02002168 case LYXP_TOKEN_AT:
2169 case LYXP_TOKEN_NAMETEST:
2170 case LYXP_TOKEN_NODETYPE:
aPiecekbf968d92021-05-27 14:35:05 +02002171 rc = reparse_relative_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002172 LY_CHECK_RET(rc);
Radek Krejci0f969882020-08-21 16:56:47 +02002173 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02002174 default:
2175 break;
2176 }
2177
Michal Vasko03ff5a72019-09-11 13:49:33 +02002178 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02002179 /* '//' RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002180 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002181
aPiecekbf968d92021-05-27 14:35:05 +02002182 rc = reparse_relative_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002183 LY_CHECK_RET(rc);
2184 }
2185
2186 return LY_SUCCESS;
2187}
2188
2189/**
2190 * @brief Reparse FunctionCall. Logs directly on error.
2191 *
2192 * [9] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
2193 *
2194 * @param[in] ctx Context for logging.
2195 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002196 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002197 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002198 * @return LY_ERR
2199 */
2200static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002201reparse_function_call(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t depth)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002202{
Radek Krejci1deb5be2020-08-26 16:43:36 +02002203 int8_t min_arg_count = -1;
2204 uint32_t arg_count, max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002205 uint16_t func_tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002206 LY_ERR rc;
2207
Michal Vasko004d3152020-06-11 19:59:22 +02002208 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_FUNCNAME);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002209 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002210 func_tok_idx = *tok_idx;
2211 switch (exp->tok_len[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002212 case 3:
Michal Vasko004d3152020-06-11 19:59:22 +02002213 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002214 min_arg_count = 1;
2215 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002216 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002217 min_arg_count = 1;
2218 max_arg_count = 1;
2219 }
2220 break;
2221 case 4:
Michal Vasko004d3152020-06-11 19:59:22 +02002222 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002223 min_arg_count = 1;
2224 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002225 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002226 min_arg_count = 0;
2227 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002228 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002229 min_arg_count = 0;
2230 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002231 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002232 min_arg_count = 0;
2233 max_arg_count = 0;
2234 }
2235 break;
2236 case 5:
Michal Vasko004d3152020-06-11 19:59:22 +02002237 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002238 min_arg_count = 1;
2239 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002240 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002241 min_arg_count = 0;
2242 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002243 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002244 min_arg_count = 1;
2245 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002246 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002247 min_arg_count = 1;
2248 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002249 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002250 min_arg_count = 1;
2251 max_arg_count = 1;
2252 }
2253 break;
2254 case 6:
Michal Vasko004d3152020-06-11 19:59:22 +02002255 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002256 min_arg_count = 2;
Radek Krejci1deb5be2020-08-26 16:43:36 +02002257 max_arg_count = UINT32_MAX;
Michal Vasko004d3152020-06-11 19:59:22 +02002258 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002259 min_arg_count = 0;
2260 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002261 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002262 min_arg_count = 0;
2263 max_arg_count = 1;
2264 }
2265 break;
2266 case 7:
Michal Vasko004d3152020-06-11 19:59:22 +02002267 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002268 min_arg_count = 1;
2269 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002270 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002271 min_arg_count = 1;
2272 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002273 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002274 min_arg_count = 0;
2275 max_arg_count = 0;
2276 }
2277 break;
2278 case 8:
Michal Vasko004d3152020-06-11 19:59:22 +02002279 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002280 min_arg_count = 2;
2281 max_arg_count = 2;
Michal Vasko004d3152020-06-11 19:59:22 +02002282 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002283 min_arg_count = 0;
2284 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002285 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "re-match", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002286 min_arg_count = 2;
2287 max_arg_count = 2;
2288 }
2289 break;
2290 case 9:
Michal Vasko004d3152020-06-11 19:59:22 +02002291 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002292 min_arg_count = 2;
2293 max_arg_count = 3;
Michal Vasko004d3152020-06-11 19:59:22 +02002294 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002295 min_arg_count = 3;
2296 max_arg_count = 3;
2297 }
2298 break;
2299 case 10:
Michal Vasko004d3152020-06-11 19:59:22 +02002300 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002301 min_arg_count = 0;
2302 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002303 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002304 min_arg_count = 1;
2305 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002306 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002307 min_arg_count = 2;
2308 max_arg_count = 2;
2309 }
2310 break;
2311 case 11:
Michal Vasko004d3152020-06-11 19:59:22 +02002312 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002313 min_arg_count = 2;
2314 max_arg_count = 2;
2315 }
2316 break;
2317 case 12:
Michal Vasko004d3152020-06-11 19:59:22 +02002318 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002319 min_arg_count = 2;
2320 max_arg_count = 2;
2321 }
2322 break;
2323 case 13:
Michal Vasko004d3152020-06-11 19:59:22 +02002324 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002325 min_arg_count = 0;
2326 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002327 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string-length", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002328 min_arg_count = 0;
2329 max_arg_count = 1;
2330 }
2331 break;
2332 case 15:
Michal Vasko004d3152020-06-11 19:59:22 +02002333 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002334 min_arg_count = 0;
2335 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002336 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-after", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002337 min_arg_count = 2;
2338 max_arg_count = 2;
2339 }
2340 break;
2341 case 16:
Michal Vasko004d3152020-06-11 19:59:22 +02002342 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002343 min_arg_count = 2;
2344 max_arg_count = 2;
2345 }
2346 break;
2347 case 20:
Michal Vasko004d3152020-06-11 19:59:22 +02002348 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from-or-self", 20)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002349 min_arg_count = 2;
2350 max_arg_count = 2;
2351 }
2352 break;
2353 }
2354 if (min_arg_count == -1) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002355 LOGVAL(ctx, LY_VCODE_XP_INFUNC, exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002356 return LY_EINVAL;
2357 }
Michal Vasko004d3152020-06-11 19:59:22 +02002358 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002359
2360 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02002361 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002362 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002363 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002364
2365 /* ( Expr ( ',' Expr )* )? */
2366 arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002367 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002368 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002369 if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002370 ++arg_count;
aPiecekbf968d92021-05-27 14:35:05 +02002371 rc = reparse_or_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002372 LY_CHECK_RET(rc);
2373 }
Michal Vasko004d3152020-06-11 19:59:22 +02002374 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) {
2375 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002376
2377 ++arg_count;
aPiecekbf968d92021-05-27 14:35:05 +02002378 rc = reparse_or_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002379 LY_CHECK_RET(rc);
2380 }
2381
2382 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02002383 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002384 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002385 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002386
Radek Krejci857189e2020-09-01 13:26:36 +02002387 if ((arg_count < (uint32_t)min_arg_count) || (arg_count > max_arg_count)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002388 LOGVAL(ctx, LY_VCODE_XP_INARGCOUNT, arg_count, exp->tok_len[func_tok_idx], &exp->expr[exp->tok_pos[func_tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002389 return LY_EVALID;
2390 }
2391
2392 return LY_SUCCESS;
2393}
2394
2395/**
2396 * @brief Reparse PathExpr. Logs directly on error.
2397 *
2398 * [10] PathExpr ::= LocationPath | PrimaryExpr Predicate*
2399 * | PrimaryExpr Predicate* '/' RelativeLocationPath
2400 * | PrimaryExpr Predicate* '//' RelativeLocationPath
2401 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
aPiecekfba75362021-10-07 12:39:48 +02002402 * [8] PrimaryExpr ::= VariableReference | '(' Expr ')' | Literal | Number | FunctionCall
Michal Vasko03ff5a72019-09-11 13:49:33 +02002403 *
2404 * @param[in] ctx Context for logging.
2405 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002406 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002407 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002408 * @return LY_ERR
2409 */
2410static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002411reparse_path_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t depth)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002412{
2413 LY_ERR rc;
2414
Michal Vasko004d3152020-06-11 19:59:22 +02002415 if (lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko14676352020-05-29 11:35:55 +02002416 return LY_EVALID;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002417 }
2418
Michal Vasko004d3152020-06-11 19:59:22 +02002419 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002420 case LYXP_TOKEN_PAR1:
2421 /* '(' Expr ')' Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002422 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002423
aPiecekbf968d92021-05-27 14:35:05 +02002424 rc = reparse_or_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002425 LY_CHECK_RET(rc);
2426
Michal Vasko004d3152020-06-11 19:59:22 +02002427 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002428 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002429 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002430 goto predicate;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002431 case LYXP_TOKEN_DOT:
2432 case LYXP_TOKEN_DDOT:
Michal Vasko49fec8e2022-05-24 10:28:33 +02002433 case LYXP_TOKEN_AXISNAME:
Michal Vasko03ff5a72019-09-11 13:49:33 +02002434 case LYXP_TOKEN_AT:
2435 case LYXP_TOKEN_NAMETEST:
2436 case LYXP_TOKEN_NODETYPE:
2437 /* RelativeLocationPath */
aPiecekbf968d92021-05-27 14:35:05 +02002438 rc = reparse_relative_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002439 LY_CHECK_RET(rc);
2440 break;
aPiecekfba75362021-10-07 12:39:48 +02002441 case LYXP_TOKEN_VARREF:
2442 /* VariableReference */
2443 ++(*tok_idx);
2444 goto predicate;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002445 case LYXP_TOKEN_FUNCNAME:
2446 /* FunctionCall */
aPiecekbf968d92021-05-27 14:35:05 +02002447 rc = reparse_function_call(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002448 LY_CHECK_RET(rc);
2449 goto predicate;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002450 case LYXP_TOKEN_OPER_PATH:
2451 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +02002452 /* AbsoluteLocationPath */
aPiecekbf968d92021-05-27 14:35:05 +02002453 rc = reparse_absolute_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002454 LY_CHECK_RET(rc);
2455 break;
2456 case LYXP_TOKEN_LITERAL:
2457 /* Literal */
Michal Vasko004d3152020-06-11 19:59:22 +02002458 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002459 goto predicate;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002460 case LYXP_TOKEN_NUMBER:
2461 /* Number */
Michal Vasko004d3152020-06-11 19:59:22 +02002462 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002463 goto predicate;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002464 default:
Michal Vasko49fec8e2022-05-24 10:28:33 +02002465 LOGVAL(ctx, LY_VCODE_XP_INTOK, lyxp_token2str(exp->tokens[*tok_idx]), &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002466 return LY_EVALID;
2467 }
2468
2469 return LY_SUCCESS;
2470
2471predicate:
2472 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002473 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
aPiecekbf968d92021-05-27 14:35:05 +02002474 rc = reparse_predicate(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002475 LY_CHECK_RET(rc);
2476 }
2477
2478 /* ('/' or '//') RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002479 if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002480
2481 /* '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02002482 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002483
aPiecekbf968d92021-05-27 14:35:05 +02002484 rc = reparse_relative_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002485 LY_CHECK_RET(rc);
2486 }
2487
2488 return LY_SUCCESS;
2489}
2490
2491/**
2492 * @brief Reparse UnaryExpr. Logs directly on error.
2493 *
2494 * [17] UnaryExpr ::= UnionExpr | '-' UnaryExpr
2495 * [18] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
2496 *
2497 * @param[in] ctx Context for logging.
2498 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002499 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002500 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002501 * @return LY_ERR
2502 */
2503static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002504reparse_unary_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t depth)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002505{
2506 uint16_t prev_exp;
2507 LY_ERR rc;
2508
2509 /* ('-')* */
Michal Vasko004d3152020-06-11 19:59:22 +02002510 prev_exp = *tok_idx;
Michal Vasko69730152020-10-09 16:30:07 +02002511 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2512 (exp->expr[exp->tok_pos[*tok_idx]] == '-')) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002513 exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNARY);
Michal Vasko004d3152020-06-11 19:59:22 +02002514 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002515 }
2516
2517 /* PathExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002518 prev_exp = *tok_idx;
aPiecekbf968d92021-05-27 14:35:05 +02002519 rc = reparse_path_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002520 LY_CHECK_RET(rc);
2521
2522 /* ('|' PathExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002523 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_UNI)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002524 exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNION);
Michal Vasko004d3152020-06-11 19:59:22 +02002525 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002526
aPiecekbf968d92021-05-27 14:35:05 +02002527 rc = reparse_path_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002528 LY_CHECK_RET(rc);
2529 }
2530
2531 return LY_SUCCESS;
2532}
2533
2534/**
2535 * @brief Reparse AdditiveExpr. Logs directly on error.
2536 *
2537 * [15] AdditiveExpr ::= MultiplicativeExpr
2538 * | AdditiveExpr '+' MultiplicativeExpr
2539 * | AdditiveExpr '-' MultiplicativeExpr
2540 * [16] MultiplicativeExpr ::= UnaryExpr
2541 * | MultiplicativeExpr '*' UnaryExpr
2542 * | MultiplicativeExpr 'div' UnaryExpr
2543 * | MultiplicativeExpr 'mod' UnaryExpr
2544 *
2545 * @param[in] ctx Context for logging.
2546 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002547 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002548 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002549 * @return LY_ERR
2550 */
2551static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002552reparse_additive_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t depth)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002553{
2554 uint16_t prev_add_exp, prev_mul_exp;
2555 LY_ERR rc;
2556
Michal Vasko004d3152020-06-11 19:59:22 +02002557 prev_add_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002558 goto reparse_multiplicative_expr;
2559
2560 /* ('+' / '-' MultiplicativeExpr)* */
Michal Vasko69730152020-10-09 16:30:07 +02002561 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2562 ((exp->expr[exp->tok_pos[*tok_idx]] == '+') || (exp->expr[exp->tok_pos[*tok_idx]] == '-'))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002563 exp_repeat_push(exp, prev_add_exp, LYXP_EXPR_ADDITIVE);
Michal Vasko004d3152020-06-11 19:59:22 +02002564 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002565
2566reparse_multiplicative_expr:
2567 /* UnaryExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002568 prev_mul_exp = *tok_idx;
aPiecekbf968d92021-05-27 14:35:05 +02002569 rc = reparse_unary_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002570 LY_CHECK_RET(rc);
2571
2572 /* ('*' / 'div' / 'mod' UnaryExpr)* */
Michal Vasko69730152020-10-09 16:30:07 +02002573 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2574 ((exp->expr[exp->tok_pos[*tok_idx]] == '*') || (exp->tok_len[*tok_idx] == 3))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002575 exp_repeat_push(exp, prev_mul_exp, LYXP_EXPR_MULTIPLICATIVE);
Michal Vasko004d3152020-06-11 19:59:22 +02002576 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002577
aPiecekbf968d92021-05-27 14:35:05 +02002578 rc = reparse_unary_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002579 LY_CHECK_RET(rc);
2580 }
2581 }
2582
2583 return LY_SUCCESS;
2584}
2585
2586/**
2587 * @brief Reparse EqualityExpr. Logs directly on error.
2588 *
2589 * [13] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
2590 * | EqualityExpr '!=' RelationalExpr
2591 * [14] RelationalExpr ::= AdditiveExpr
2592 * | RelationalExpr '<' AdditiveExpr
2593 * | RelationalExpr '>' AdditiveExpr
2594 * | RelationalExpr '<=' AdditiveExpr
2595 * | RelationalExpr '>=' AdditiveExpr
2596 *
2597 * @param[in] ctx Context for logging.
2598 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002599 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002600 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002601 * @return LY_ERR
2602 */
2603static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002604reparse_equality_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t depth)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002605{
2606 uint16_t prev_eq_exp, prev_rel_exp;
2607 LY_ERR rc;
2608
Michal Vasko004d3152020-06-11 19:59:22 +02002609 prev_eq_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002610 goto reparse_additive_expr;
2611
2612 /* ('=' / '!=' RelationalExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002613 while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_EQUAL, LYXP_TOKEN_OPER_NEQUAL)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002614 exp_repeat_push(exp, prev_eq_exp, LYXP_EXPR_EQUALITY);
Michal Vasko004d3152020-06-11 19:59:22 +02002615 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002616
2617reparse_additive_expr:
2618 /* AdditiveExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002619 prev_rel_exp = *tok_idx;
aPiecekbf968d92021-05-27 14:35:05 +02002620 rc = reparse_additive_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002621 LY_CHECK_RET(rc);
2622
2623 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002624 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_COMP)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002625 exp_repeat_push(exp, prev_rel_exp, LYXP_EXPR_RELATIONAL);
Michal Vasko004d3152020-06-11 19:59:22 +02002626 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002627
aPiecekbf968d92021-05-27 14:35:05 +02002628 rc = reparse_additive_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002629 LY_CHECK_RET(rc);
2630 }
2631 }
2632
2633 return LY_SUCCESS;
2634}
2635
2636/**
2637 * @brief Reparse OrExpr. Logs directly on error.
2638 *
2639 * [11] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
2640 * [12] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
2641 *
2642 * @param[in] ctx Context for logging.
2643 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002644 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002645 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002646 * @return LY_ERR
2647 */
2648static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002649reparse_or_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t depth)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002650{
2651 uint16_t prev_or_exp, prev_and_exp;
2652 LY_ERR rc;
2653
aPiecekbf968d92021-05-27 14:35:05 +02002654 ++depth;
2655 LY_CHECK_ERR_RET(depth > LYXP_MAX_BLOCK_DEPTH, LOGVAL(ctx, LY_VCODE_XP_DEPTH), LY_EINVAL);
2656
Michal Vasko004d3152020-06-11 19:59:22 +02002657 prev_or_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002658 goto reparse_equality_expr;
2659
2660 /* ('or' AndExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002661 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_LOG) && (exp->tok_len[*tok_idx] == 2)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002662 exp_repeat_push(exp, prev_or_exp, LYXP_EXPR_OR);
Michal Vasko004d3152020-06-11 19:59:22 +02002663 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002664
2665reparse_equality_expr:
2666 /* EqualityExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002667 prev_and_exp = *tok_idx;
aPiecekbf968d92021-05-27 14:35:05 +02002668 rc = reparse_equality_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002669 LY_CHECK_RET(rc);
2670
2671 /* ('and' EqualityExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002672 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_LOG) && (exp->tok_len[*tok_idx] == 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002673 exp_repeat_push(exp, prev_and_exp, LYXP_EXPR_AND);
Michal Vasko004d3152020-06-11 19:59:22 +02002674 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002675
aPiecekbf968d92021-05-27 14:35:05 +02002676 rc = reparse_equality_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002677 LY_CHECK_RET(rc);
2678 }
2679 }
2680
2681 return LY_SUCCESS;
2682}
Radek Krejcib1646a92018-11-02 16:08:26 +01002683
2684/**
2685 * @brief Parse NCName.
2686 *
2687 * @param[in] ncname Name to parse.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002688 * @return Length of @p ncname valid bytes.
Radek Krejcib1646a92018-11-02 16:08:26 +01002689 */
Michal Vasko49fec8e2022-05-24 10:28:33 +02002690static ssize_t
Radek Krejcib1646a92018-11-02 16:08:26 +01002691parse_ncname(const char *ncname)
2692{
Radek Krejci1deb5be2020-08-26 16:43:36 +02002693 uint32_t uc;
Radek Krejcid4270262019-01-07 15:07:25 +01002694 size_t size;
Michal Vasko49fec8e2022-05-24 10:28:33 +02002695 ssize_t len = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01002696
2697 LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), 0);
2698 if (!is_xmlqnamestartchar(uc) || (uc == ':')) {
2699 return len;
2700 }
2701
2702 do {
2703 len += size;
Radek Krejci9a564c92019-01-07 14:53:57 +01002704 if (!*ncname) {
2705 break;
2706 }
Radek Krejcid4270262019-01-07 15:07:25 +01002707 LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), -len);
Radek Krejcib1646a92018-11-02 16:08:26 +01002708 } while (is_xmlqnamechar(uc) && (uc != ':'));
2709
2710 return len;
2711}
2712
2713/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02002714 * @brief Add @p token into the expression @p exp.
Radek Krejcib1646a92018-11-02 16:08:26 +01002715 *
Michal Vasko03ff5a72019-09-11 13:49:33 +02002716 * @param[in] ctx Context for logging.
Radek Krejcib1646a92018-11-02 16:08:26 +01002717 * @param[in] exp Expression to use.
2718 * @param[in] token Token to add.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002719 * @param[in] tok_pos Token position in the XPath expression.
Radek Krejcib1646a92018-11-02 16:08:26 +01002720 * @param[in] tok_len Token length in the XPath expression.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002721 * @return LY_ERR
Radek Krejcib1646a92018-11-02 16:08:26 +01002722 */
2723static LY_ERR
Michal Vasko14676352020-05-29 11:35:55 +02002724exp_add_token(const struct ly_ctx *ctx, struct lyxp_expr *exp, enum lyxp_token token, uint16_t tok_pos, uint16_t tok_len)
Radek Krejcib1646a92018-11-02 16:08:26 +01002725{
2726 uint32_t prev;
2727
2728 if (exp->used == exp->size) {
2729 prev = exp->size;
2730 exp->size += LYXP_EXPR_SIZE_STEP;
2731 if (prev > exp->size) {
2732 LOGINT(ctx);
2733 return LY_EINT;
2734 }
2735
2736 exp->tokens = ly_realloc(exp->tokens, exp->size * sizeof *exp->tokens);
2737 LY_CHECK_ERR_RET(!exp->tokens, LOGMEM(ctx), LY_EMEM);
2738 exp->tok_pos = ly_realloc(exp->tok_pos, exp->size * sizeof *exp->tok_pos);
2739 LY_CHECK_ERR_RET(!exp->tok_pos, LOGMEM(ctx), LY_EMEM);
2740 exp->tok_len = ly_realloc(exp->tok_len, exp->size * sizeof *exp->tok_len);
2741 LY_CHECK_ERR_RET(!exp->tok_len, LOGMEM(ctx), LY_EMEM);
2742 }
2743
2744 exp->tokens[exp->used] = token;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002745 exp->tok_pos[exp->used] = tok_pos;
Radek Krejcib1646a92018-11-02 16:08:26 +01002746 exp->tok_len[exp->used] = tok_len;
2747 ++exp->used;
2748 return LY_SUCCESS;
2749}
2750
2751void
Michal Vasko14676352020-05-29 11:35:55 +02002752lyxp_expr_free(const struct ly_ctx *ctx, struct lyxp_expr *expr)
Radek Krejcib1646a92018-11-02 16:08:26 +01002753{
2754 uint16_t i;
2755
2756 if (!expr) {
2757 return;
2758 }
2759
2760 lydict_remove(ctx, expr->expr);
2761 free(expr->tokens);
2762 free(expr->tok_pos);
2763 free(expr->tok_len);
2764 if (expr->repeat) {
2765 for (i = 0; i < expr->used; ++i) {
2766 free(expr->repeat[i]);
2767 }
2768 }
2769 free(expr->repeat);
2770 free(expr);
2771}
2772
Michal Vasko49fec8e2022-05-24 10:28:33 +02002773/**
2774 * @brief Parse Axis name.
2775 *
2776 * @param[in] str String to parse.
2777 * @param[in] str_len Length of @p str.
2778 * @return LY_SUCCESS if an axis.
2779 * @return LY_ENOT otherwise.
2780 */
2781static LY_ERR
2782expr_parse_axis(const char *str, size_t str_len)
2783{
2784 switch (str_len) {
2785 case 4:
2786 if (!strncmp("self", str, str_len)) {
2787 return LY_SUCCESS;
2788 }
2789 break;
2790 case 5:
2791 if (!strncmp("child", str, str_len)) {
2792 return LY_SUCCESS;
2793 }
2794 break;
2795 case 6:
2796 if (!strncmp("parent", str, str_len)) {
2797 return LY_SUCCESS;
2798 }
2799 break;
2800 case 8:
2801 if (!strncmp("ancestor", str, str_len)) {
2802 return LY_SUCCESS;
2803 }
2804 break;
2805 case 9:
2806 if (!strncmp("attribute", str, str_len)) {
2807 return LY_SUCCESS;
2808 } else if (!strncmp("following", str, str_len)) {
2809 return LY_SUCCESS;
2810 } else if (!strncmp("namespace", str, str_len)) {
2811 LOGERR(NULL, LY_EINVAL, "Axis \"namespace\" not supported.");
2812 return LY_ENOT;
2813 } else if (!strncmp("preceding", str, str_len)) {
2814 return LY_SUCCESS;
2815 }
2816 break;
2817 case 10:
2818 if (!strncmp("descendant", str, str_len)) {
2819 return LY_SUCCESS;
2820 }
2821 break;
2822 case 16:
2823 if (!strncmp("ancestor-or-self", str, str_len)) {
2824 return LY_SUCCESS;
2825 }
2826 break;
2827 case 17:
2828 if (!strncmp("following-sibling", str, str_len)) {
2829 return LY_SUCCESS;
2830 } else if (!strncmp("preceding-sibling", str, str_len)) {
2831 return LY_SUCCESS;
2832 }
2833 break;
2834 case 18:
2835 if (!strncmp("descendant-or-self", str, str_len)) {
2836 return LY_SUCCESS;
2837 }
2838 break;
2839 }
2840
2841 return LY_ENOT;
2842}
2843
Radek Krejcif03a9e22020-09-18 20:09:31 +02002844LY_ERR
2845lyxp_expr_parse(const struct ly_ctx *ctx, const char *expr_str, size_t expr_len, ly_bool reparse, struct lyxp_expr **expr_p)
Radek Krejcib1646a92018-11-02 16:08:26 +01002846{
Radek Krejcif03a9e22020-09-18 20:09:31 +02002847 LY_ERR ret = LY_SUCCESS;
2848 struct lyxp_expr *expr;
Radek Krejcid4270262019-01-07 15:07:25 +01002849 size_t parsed = 0, tok_len;
Radek Krejcib1646a92018-11-02 16:08:26 +01002850 enum lyxp_token tok_type;
Michal Vasko49fec8e2022-05-24 10:28:33 +02002851 ly_bool prev_func_check = 0, prev_ntype_check = 0, has_axis;
Michal Vasko004d3152020-06-11 19:59:22 +02002852 uint16_t tok_idx = 0;
Michal Vasko49fec8e2022-05-24 10:28:33 +02002853 ssize_t ncname_len;
Radek Krejcib1646a92018-11-02 16:08:26 +01002854
Radek Krejcif03a9e22020-09-18 20:09:31 +02002855 assert(expr_p);
2856
2857 if (!expr_str[0]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002858 LOGVAL(ctx, LY_VCODE_XP_EOF);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002859 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02002860 }
2861
2862 if (!expr_len) {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002863 expr_len = strlen(expr_str);
Michal Vasko004d3152020-06-11 19:59:22 +02002864 }
2865 if (expr_len > UINT16_MAX) {
Michal Vasko623ac7b2021-04-09 12:44:23 +02002866 LOGVAL(ctx, LYVE_XPATH, "XPath expression cannot be longer than %u characters.", UINT16_MAX);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002867 return LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01002868 }
2869
2870 /* init lyxp_expr structure */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002871 expr = calloc(1, sizeof *expr);
2872 LY_CHECK_ERR_GOTO(!expr, LOGMEM(ctx); ret = LY_EMEM, error);
2873 LY_CHECK_GOTO(ret = lydict_insert(ctx, expr_str, expr_len, &expr->expr), error);
2874 expr->used = 0;
2875 expr->size = LYXP_EXPR_SIZE_START;
2876 expr->tokens = malloc(expr->size * sizeof *expr->tokens);
2877 LY_CHECK_ERR_GOTO(!expr->tokens, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002878
Radek Krejcif03a9e22020-09-18 20:09:31 +02002879 expr->tok_pos = malloc(expr->size * sizeof *expr->tok_pos);
2880 LY_CHECK_ERR_GOTO(!expr->tok_pos, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002881
Radek Krejcif03a9e22020-09-18 20:09:31 +02002882 expr->tok_len = malloc(expr->size * sizeof *expr->tok_len);
2883 LY_CHECK_ERR_GOTO(!expr->tok_len, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002884
Michal Vasko004d3152020-06-11 19:59:22 +02002885 /* make expr 0-terminated */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002886 expr_str = expr->expr;
Michal Vasko004d3152020-06-11 19:59:22 +02002887
Radek Krejcif03a9e22020-09-18 20:09:31 +02002888 while (is_xmlws(expr_str[parsed])) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002889 ++parsed;
2890 }
2891
2892 do {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002893 if (expr_str[parsed] == '(') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002894
2895 /* '(' */
2896 tok_len = 1;
2897 tok_type = LYXP_TOKEN_PAR1;
2898
Michal Vasko49fec8e2022-05-24 10:28:33 +02002899 if (prev_ntype_check && expr->used && (expr->tokens[expr->used - 1] == LYXP_TOKEN_NAMETEST) &&
2900 (((expr->tok_len[expr->used - 1] == 4) &&
2901 (!strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "node", 4) ||
2902 !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "text", 4))) ||
2903 ((expr->tok_len[expr->used - 1] == 7) &&
2904 !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "comment", 7)))) {
2905 /* it is NodeType after all */
2906 expr->tokens[expr->used - 1] = LYXP_TOKEN_NODETYPE;
2907
2908 prev_ntype_check = 0;
2909 prev_func_check = 0;
2910 } else if (prev_func_check && expr->used && (expr->tokens[expr->used - 1] == LYXP_TOKEN_NAMETEST)) {
2911 /* it is FunctionName after all */
2912 expr->tokens[expr->used - 1] = LYXP_TOKEN_FUNCNAME;
2913
2914 prev_ntype_check = 0;
2915 prev_func_check = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01002916 }
2917
Radek Krejcif03a9e22020-09-18 20:09:31 +02002918 } else if (expr_str[parsed] == ')') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002919
2920 /* ')' */
2921 tok_len = 1;
2922 tok_type = LYXP_TOKEN_PAR2;
2923
Radek Krejcif03a9e22020-09-18 20:09:31 +02002924 } else if (expr_str[parsed] == '[') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002925
2926 /* '[' */
2927 tok_len = 1;
2928 tok_type = LYXP_TOKEN_BRACK1;
2929
Radek Krejcif03a9e22020-09-18 20:09:31 +02002930 } else if (expr_str[parsed] == ']') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002931
2932 /* ']' */
2933 tok_len = 1;
2934 tok_type = LYXP_TOKEN_BRACK2;
2935
Radek Krejcif03a9e22020-09-18 20:09:31 +02002936 } else if (!strncmp(&expr_str[parsed], "..", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002937
2938 /* '..' */
2939 tok_len = 2;
2940 tok_type = LYXP_TOKEN_DDOT;
2941
Radek Krejcif03a9e22020-09-18 20:09:31 +02002942 } else if ((expr_str[parsed] == '.') && (!isdigit(expr_str[parsed + 1]))) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002943
2944 /* '.' */
2945 tok_len = 1;
2946 tok_type = LYXP_TOKEN_DOT;
2947
Radek Krejcif03a9e22020-09-18 20:09:31 +02002948 } else if (expr_str[parsed] == '@') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002949
2950 /* '@' */
2951 tok_len = 1;
2952 tok_type = LYXP_TOKEN_AT;
2953
Radek Krejcif03a9e22020-09-18 20:09:31 +02002954 } else if (expr_str[parsed] == ',') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002955
2956 /* ',' */
2957 tok_len = 1;
2958 tok_type = LYXP_TOKEN_COMMA;
2959
Radek Krejcif03a9e22020-09-18 20:09:31 +02002960 } else if (expr_str[parsed] == '\'') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002961
2962 /* Literal with ' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002963 for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\''); ++tok_len) {}
2964 LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0',
Radek Krejci2efc45b2020-12-22 16:25:44 +01002965 LOGVAL(ctx, LY_VCODE_XP_EOE, expr_str[parsed], &expr_str[parsed]); ret = LY_EVALID,
Michal Vasko69730152020-10-09 16:30:07 +02002966 error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002967 ++tok_len;
2968 tok_type = LYXP_TOKEN_LITERAL;
2969
Radek Krejcif03a9e22020-09-18 20:09:31 +02002970 } else if (expr_str[parsed] == '\"') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002971
2972 /* Literal with " */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002973 for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\"'); ++tok_len) {}
2974 LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0',
Radek Krejci2efc45b2020-12-22 16:25:44 +01002975 LOGVAL(ctx, LY_VCODE_XP_EOE, expr_str[parsed], &expr_str[parsed]); ret = LY_EVALID,
Michal Vasko69730152020-10-09 16:30:07 +02002976 error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002977 ++tok_len;
2978 tok_type = LYXP_TOKEN_LITERAL;
2979
Radek Krejcif03a9e22020-09-18 20:09:31 +02002980 } else if ((expr_str[parsed] == '.') || (isdigit(expr_str[parsed]))) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002981
2982 /* Number */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002983 for (tok_len = 0; isdigit(expr_str[parsed + tok_len]); ++tok_len) {}
2984 if (expr_str[parsed + tok_len] == '.') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002985 ++tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02002986 for ( ; isdigit(expr_str[parsed + tok_len]); ++tok_len) {}
Radek Krejcib1646a92018-11-02 16:08:26 +01002987 }
2988 tok_type = LYXP_TOKEN_NUMBER;
2989
aPiecekfba75362021-10-07 12:39:48 +02002990 } else if (expr_str[parsed] == '$') {
2991
2992 /* VariableReference */
2993 parsed++;
Michal Vasko49fec8e2022-05-24 10:28:33 +02002994 ncname_len = parse_ncname(&expr_str[parsed]);
aPiecekfba75362021-10-07 12:39:48 +02002995 LY_CHECK_ERR_GOTO(ncname_len < 1, LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed - ncname_len],
2996 parsed - ncname_len + 1, expr_str); ret = LY_EVALID, error);
2997 tok_len = ncname_len;
2998 LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == ':',
2999 LOGVAL(ctx, LYVE_XPATH, "Variable with prefix is not supported."); ret = LY_EVALID,
3000 error);
3001 tok_type = LYXP_TOKEN_VARREF;
3002
Radek Krejcif03a9e22020-09-18 20:09:31 +02003003 } else if (expr_str[parsed] == '/') {
Radek Krejcib1646a92018-11-02 16:08:26 +01003004
3005 /* Operator '/', '//' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02003006 if (!strncmp(&expr_str[parsed], "//", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01003007 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003008 tok_type = LYXP_TOKEN_OPER_RPATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01003009 } else {
3010 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003011 tok_type = LYXP_TOKEN_OPER_PATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01003012 }
Radek Krejcib1646a92018-11-02 16:08:26 +01003013
Radek Krejcif03a9e22020-09-18 20:09:31 +02003014 } else if (!strncmp(&expr_str[parsed], "!=", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01003015
Michal Vasko3e48bf32020-06-01 08:39:07 +02003016 /* Operator '!=' */
Radek Krejcib1646a92018-11-02 16:08:26 +01003017 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003018 tok_type = LYXP_TOKEN_OPER_NEQUAL;
3019
Radek Krejcif03a9e22020-09-18 20:09:31 +02003020 } else if (!strncmp(&expr_str[parsed], "<=", 2) || !strncmp(&expr_str[parsed], ">=", 2)) {
Michal Vasko3e48bf32020-06-01 08:39:07 +02003021
3022 /* Operator '<=', '>=' */
3023 tok_len = 2;
3024 tok_type = LYXP_TOKEN_OPER_COMP;
Radek Krejcib1646a92018-11-02 16:08:26 +01003025
Radek Krejcif03a9e22020-09-18 20:09:31 +02003026 } else if (expr_str[parsed] == '|') {
Radek Krejcib1646a92018-11-02 16:08:26 +01003027
3028 /* Operator '|' */
3029 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003030 tok_type = LYXP_TOKEN_OPER_UNI;
Radek Krejcib1646a92018-11-02 16:08:26 +01003031
Radek Krejcif03a9e22020-09-18 20:09:31 +02003032 } else if ((expr_str[parsed] == '+') || (expr_str[parsed] == '-')) {
Radek Krejcib1646a92018-11-02 16:08:26 +01003033
3034 /* Operator '+', '-' */
3035 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003036 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01003037
Radek Krejcif03a9e22020-09-18 20:09:31 +02003038 } else if (expr_str[parsed] == '=') {
Radek Krejcib1646a92018-11-02 16:08:26 +01003039
Michal Vasko3e48bf32020-06-01 08:39:07 +02003040 /* Operator '=' */
Radek Krejcib1646a92018-11-02 16:08:26 +01003041 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003042 tok_type = LYXP_TOKEN_OPER_EQUAL;
3043
Radek Krejcif03a9e22020-09-18 20:09:31 +02003044 } else if ((expr_str[parsed] == '<') || (expr_str[parsed] == '>')) {
Michal Vasko3e48bf32020-06-01 08:39:07 +02003045
3046 /* Operator '<', '>' */
3047 tok_len = 1;
3048 tok_type = LYXP_TOKEN_OPER_COMP;
Radek Krejcib1646a92018-11-02 16:08:26 +01003049
Michal Vasko69730152020-10-09 16:30:07 +02003050 } else if (expr->used && (expr->tokens[expr->used - 1] != LYXP_TOKEN_AT) &&
3051 (expr->tokens[expr->used - 1] != LYXP_TOKEN_PAR1) &&
3052 (expr->tokens[expr->used - 1] != LYXP_TOKEN_BRACK1) &&
3053 (expr->tokens[expr->used - 1] != LYXP_TOKEN_COMMA) &&
3054 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_LOG) &&
3055 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_EQUAL) &&
3056 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_NEQUAL) &&
3057 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_COMP) &&
3058 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_MATH) &&
3059 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_UNI) &&
3060 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_PATH) &&
3061 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_RPATH)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01003062
3063 /* Operator '*', 'or', 'and', 'mod', or 'div' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02003064 if (expr_str[parsed] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01003065 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003066 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01003067
Radek Krejcif03a9e22020-09-18 20:09:31 +02003068 } else if (!strncmp(&expr_str[parsed], "or", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01003069 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003070 tok_type = LYXP_TOKEN_OPER_LOG;
Radek Krejcib1646a92018-11-02 16:08:26 +01003071
Radek Krejcif03a9e22020-09-18 20:09:31 +02003072 } else if (!strncmp(&expr_str[parsed], "and", 3)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01003073 tok_len = 3;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003074 tok_type = LYXP_TOKEN_OPER_LOG;
Radek Krejcib1646a92018-11-02 16:08:26 +01003075
Radek Krejcif03a9e22020-09-18 20:09:31 +02003076 } else if (!strncmp(&expr_str[parsed], "mod", 3) || !strncmp(&expr_str[parsed], "div", 3)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01003077 tok_len = 3;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003078 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01003079
Michal Vasko49fec8e2022-05-24 10:28:33 +02003080 } else if (prev_ntype_check || prev_func_check) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003081 LOGVAL(ctx, LYVE_XPATH, "Invalid character 0x%x ('%c'), perhaps \"%.*s\" is supposed to be a function call.",
Michal Vasko69730152020-10-09 16:30:07 +02003082 expr_str[parsed], expr_str[parsed], expr->tok_len[expr->used - 1], &expr->expr[expr->tok_pos[expr->used - 1]]);
Radek Krejcif03a9e22020-09-18 20:09:31 +02003083 ret = LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01003084 goto error;
3085 } else {
Michal Vasko774ce402021-04-14 15:35:06 +02003086 LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed], parsed + 1, expr_str);
Radek Krejcif03a9e22020-09-18 20:09:31 +02003087 ret = LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01003088 goto error;
3089 }
Radek Krejcib1646a92018-11-02 16:08:26 +01003090 } else {
3091
Michal Vasko49fec8e2022-05-24 10:28:33 +02003092 /* (AxisName '::')? ((NCName ':')? '*' | QName) or NodeType/FunctionName */
3093 if (expr_str[parsed] == '*') {
3094 ncname_len = 1;
3095 } else {
3096 ncname_len = parse_ncname(&expr_str[parsed]);
3097 LY_CHECK_ERR_GOTO(ncname_len < 1, LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed - ncname_len],
3098 parsed - ncname_len + 1, expr_str); ret = LY_EVALID, error);
3099 }
Radek Krejcib1646a92018-11-02 16:08:26 +01003100 tok_len = ncname_len;
3101
Michal Vasko49fec8e2022-05-24 10:28:33 +02003102 has_axis = 0;
3103 if (!strncmp(&expr_str[parsed + tok_len], "::", 2)) {
3104 /* axis */
3105 LY_CHECK_ERR_GOTO(expr_parse_axis(&expr_str[parsed], ncname_len),
3106 LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed], parsed + 1, expr_str); ret = LY_EVALID, error);
3107 tok_type = LYXP_TOKEN_AXISNAME;
3108
3109 LY_CHECK_GOTO(ret = exp_add_token(ctx, expr, tok_type, parsed, tok_len), error);
3110 parsed += tok_len;
3111
3112 /* '::' */
3113 tok_len = 2;
3114 tok_type = LYXP_TOKEN_DCOLON;
3115
3116 LY_CHECK_GOTO(ret = exp_add_token(ctx, expr, tok_type, parsed, tok_len), error);
3117 parsed += tok_len;
3118
3119 if (expr_str[parsed] == '*') {
3120 ncname_len = 1;
3121 } else {
3122 ncname_len = parse_ncname(&expr_str[parsed]);
3123 LY_CHECK_ERR_GOTO(ncname_len < 1, LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed - ncname_len],
3124 parsed - ncname_len + 1, expr_str); ret = LY_EVALID, error);
3125 }
3126 tok_len = ncname_len;
3127
3128 has_axis = 1;
3129 }
3130
Radek Krejcif03a9e22020-09-18 20:09:31 +02003131 if (expr_str[parsed + tok_len] == ':') {
Radek Krejcib1646a92018-11-02 16:08:26 +01003132 ++tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02003133 if (expr_str[parsed + tok_len] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01003134 ++tok_len;
3135 } else {
Radek Krejcif03a9e22020-09-18 20:09:31 +02003136 ncname_len = parse_ncname(&expr_str[parsed + tok_len]);
Michal Vaskoe2be5462021-08-04 10:49:42 +02003137 LY_CHECK_ERR_GOTO(ncname_len < 1, LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed - ncname_len],
Michal Vasko774ce402021-04-14 15:35:06 +02003138 parsed - ncname_len + 1, expr_str); ret = LY_EVALID, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01003139 tok_len += ncname_len;
3140 }
Michal Vasko49fec8e2022-05-24 10:28:33 +02003141 /* remove old flags to prevent ambiguities */
3142 prev_ntype_check = 0;
3143 prev_func_check = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01003144 tok_type = LYXP_TOKEN_NAMETEST;
3145 } else {
Michal Vasko49fec8e2022-05-24 10:28:33 +02003146 /* if not '*', there is no prefix so it can still be NodeType/FunctionName, we can't finally decide now */
3147 prev_ntype_check = (expr_str[parsed] == '*') ? 0 : 1;
3148 prev_func_check = (prev_ntype_check && !has_axis) ? 1 : 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01003149 tok_type = LYXP_TOKEN_NAMETEST;
3150 }
3151 }
3152
3153 /* store the token, move on to the next one */
Radek Krejcif03a9e22020-09-18 20:09:31 +02003154 LY_CHECK_GOTO(ret = exp_add_token(ctx, expr, tok_type, parsed, tok_len), error);
Radek Krejcib1646a92018-11-02 16:08:26 +01003155 parsed += tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02003156 while (is_xmlws(expr_str[parsed])) {
Radek Krejcib1646a92018-11-02 16:08:26 +01003157 ++parsed;
3158 }
3159
Radek Krejcif03a9e22020-09-18 20:09:31 +02003160 } while (expr_str[parsed]);
Radek Krejcib1646a92018-11-02 16:08:26 +01003161
Michal Vasko004d3152020-06-11 19:59:22 +02003162 if (reparse) {
3163 /* prealloc repeat */
Radek Krejcif03a9e22020-09-18 20:09:31 +02003164 expr->repeat = calloc(expr->size, sizeof *expr->repeat);
3165 LY_CHECK_ERR_GOTO(!expr->repeat, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01003166
Michal Vasko004d3152020-06-11 19:59:22 +02003167 /* fill repeat */
aPiecekbf968d92021-05-27 14:35:05 +02003168 LY_CHECK_ERR_GOTO(reparse_or_expr(ctx, expr, &tok_idx, 0), ret = LY_EVALID, error);
Radek Krejcif03a9e22020-09-18 20:09:31 +02003169 if (expr->used > tok_idx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003170 LOGVAL(ctx, LYVE_XPATH, "Unparsed characters \"%s\" left at the end of an XPath expression.",
Michal Vasko69730152020-10-09 16:30:07 +02003171 &expr->expr[expr->tok_pos[tok_idx]]);
Radek Krejcif03a9e22020-09-18 20:09:31 +02003172 ret = LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02003173 goto error;
3174 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003175 }
3176
Radek Krejcif03a9e22020-09-18 20:09:31 +02003177 print_expr_struct_debug(expr);
3178 *expr_p = expr;
3179 return LY_SUCCESS;
Radek Krejcib1646a92018-11-02 16:08:26 +01003180
3181error:
Radek Krejcif03a9e22020-09-18 20:09:31 +02003182 lyxp_expr_free(ctx, expr);
3183 return ret;
Radek Krejcib1646a92018-11-02 16:08:26 +01003184}
3185
Michal Vasko1734be92020-09-22 08:55:10 +02003186LY_ERR
Michal Vaskoe33134a2022-07-29 14:54:40 +02003187lyxp_expr_dup(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t start_idx, uint16_t end_idx,
3188 struct lyxp_expr **dup_p)
Michal Vasko004d3152020-06-11 19:59:22 +02003189{
Michal Vasko1734be92020-09-22 08:55:10 +02003190 LY_ERR ret = LY_SUCCESS;
3191 struct lyxp_expr *dup = NULL;
Michal Vaskoe33134a2022-07-29 14:54:40 +02003192 uint16_t used = 0, i, j, k, expr_len;
3193 const char *expr_start;
3194
3195 assert((!start_idx && !end_idx) || ((start_idx < exp->used) && (end_idx < exp->used) && (start_idx <= end_idx)));
Michal Vasko004d3152020-06-11 19:59:22 +02003196
Michal Vasko7f45cf22020-10-01 12:49:44 +02003197 if (!exp) {
3198 goto cleanup;
3199 }
3200
Michal Vaskoe33134a2022-07-29 14:54:40 +02003201 if (!start_idx && !end_idx) {
3202 end_idx = exp->used - 1;
3203 }
3204
3205 expr_start = exp->expr + exp->tok_pos[start_idx];
3206 expr_len = (exp->tok_pos[end_idx] + exp->tok_len[end_idx]) - exp->tok_pos[start_idx];
3207
Michal Vasko004d3152020-06-11 19:59:22 +02003208 dup = calloc(1, sizeof *dup);
Michal Vasko1734be92020-09-22 08:55:10 +02003209 LY_CHECK_ERR_GOTO(!dup, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02003210
Michal Vasko08e9b112021-06-11 15:41:17 +02003211 if (exp->used) {
Michal Vaskoe33134a2022-07-29 14:54:40 +02003212 used = (end_idx - start_idx) + 1;
3213
3214 dup->tokens = malloc(used * sizeof *dup->tokens);
Michal Vasko08e9b112021-06-11 15:41:17 +02003215 LY_CHECK_ERR_GOTO(!dup->tokens, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vaskoe33134a2022-07-29 14:54:40 +02003216 memcpy(dup->tokens, exp->tokens + start_idx, used * sizeof *dup->tokens);
Michal Vasko004d3152020-06-11 19:59:22 +02003217
Michal Vaskoe33134a2022-07-29 14:54:40 +02003218 dup->tok_pos = malloc(used * sizeof *dup->tok_pos);
Michal Vasko08e9b112021-06-11 15:41:17 +02003219 LY_CHECK_ERR_GOTO(!dup->tok_pos, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vaskoe33134a2022-07-29 14:54:40 +02003220 memcpy(dup->tok_pos, exp->tok_pos + start_idx, used * sizeof *dup->tok_pos);
Michal Vasko004d3152020-06-11 19:59:22 +02003221
Michal Vaskoe33134a2022-07-29 14:54:40 +02003222 if (start_idx) {
3223 /* fix the indices in the expression */
3224 for (i = 0; i < used; ++i) {
3225 dup->tok_pos[i] -= expr_start - exp->expr;
3226 }
3227 }
3228
3229 dup->tok_len = malloc(used * sizeof *dup->tok_len);
Michal Vasko08e9b112021-06-11 15:41:17 +02003230 LY_CHECK_ERR_GOTO(!dup->tok_len, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vaskoe33134a2022-07-29 14:54:40 +02003231 memcpy(dup->tok_len, exp->tok_len + start_idx, used * sizeof *dup->tok_len);
Michal Vasko004d3152020-06-11 19:59:22 +02003232
Michal Vasko79a7a872022-06-17 09:00:48 +02003233 if (exp->repeat) {
Michal Vaskoe33134a2022-07-29 14:54:40 +02003234 dup->repeat = malloc(used * sizeof *dup->repeat);
Michal Vasko79a7a872022-06-17 09:00:48 +02003235 LY_CHECK_ERR_GOTO(!dup->repeat, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vaskoe33134a2022-07-29 14:54:40 +02003236 for (i = start_idx; i <= end_idx; ++i) {
Michal Vasko79a7a872022-06-17 09:00:48 +02003237 if (!exp->repeat[i]) {
Michal Vaskoe33134a2022-07-29 14:54:40 +02003238 dup->repeat[i - start_idx] = NULL;
Michal Vasko79a7a872022-06-17 09:00:48 +02003239 } else {
3240 for (j = 0; exp->repeat[i][j]; ++j) {}
3241 /* the ending 0 as well */
3242 ++j;
Michal Vasko004d3152020-06-11 19:59:22 +02003243
Michal Vaskoe33134a2022-07-29 14:54:40 +02003244 dup->repeat[i - start_idx] = malloc(j * sizeof **dup->repeat);
3245 LY_CHECK_ERR_GOTO(!dup->repeat[i - start_idx], LOGMEM(ctx); ret = LY_EMEM, cleanup);
3246 memcpy(dup->repeat[i - start_idx], exp->repeat[i], j * sizeof **dup->repeat);
3247 dup->repeat[i - start_idx][j - 1] = 0;
3248
3249 if (start_idx) {
3250 /* fix the indices in the tokens */
3251 for (k = 0; k < j; ++k) {
3252 dup->repeat[i - start_idx][k] -= start_idx;
3253 }
3254 }
Michal Vasko79a7a872022-06-17 09:00:48 +02003255 }
Michal Vasko08e9b112021-06-11 15:41:17 +02003256 }
Michal Vasko004d3152020-06-11 19:59:22 +02003257 }
3258 }
3259
Michal Vaskoe33134a2022-07-29 14:54:40 +02003260 dup->used = used;
3261 dup->size = used;
3262
3263 /* copy only subexpression */
3264 LY_CHECK_GOTO(ret = lydict_insert(ctx, expr_start, expr_len, &dup->expr), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02003265
Michal Vasko1734be92020-09-22 08:55:10 +02003266cleanup:
3267 if (ret) {
3268 lyxp_expr_free(ctx, dup);
3269 } else {
3270 *dup_p = dup;
3271 }
3272 return ret;
Michal Vasko004d3152020-06-11 19:59:22 +02003273}
3274
Michal Vasko03ff5a72019-09-11 13:49:33 +02003275/**
3276 * @brief Get the last-added schema node that is currently in the context.
3277 *
3278 * @param[in] set Set to search in.
3279 * @return Last-added schema context node, NULL if no node is in context.
3280 */
3281static struct lysc_node *
3282warn_get_scnode_in_ctx(struct lyxp_set *set)
3283{
3284 uint32_t i;
3285
3286 if (!set || (set->type != LYXP_SET_SCNODE_SET)) {
3287 return NULL;
3288 }
3289
3290 i = set->used;
3291 do {
3292 --i;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003293 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003294 /* if there are more, simply return the first found (last added) */
3295 return set->val.scnodes[i].scnode;
3296 }
3297 } while (i);
3298
3299 return NULL;
3300}
3301
3302/**
3303 * @brief Test whether a type is numeric - integer type or decimal64.
3304 *
3305 * @param[in] type Type to test.
Radek Krejci857189e2020-09-01 13:26:36 +02003306 * @return Boolean value whether @p type is numeric type or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003307 */
Radek Krejci857189e2020-09-01 13:26:36 +02003308static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003309warn_is_numeric_type(struct lysc_type *type)
3310{
3311 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003312 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003313 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003314
3315 switch (type->basetype) {
3316 case LY_TYPE_DEC64:
3317 case LY_TYPE_INT8:
3318 case LY_TYPE_UINT8:
3319 case LY_TYPE_INT16:
3320 case LY_TYPE_UINT16:
3321 case LY_TYPE_INT32:
3322 case LY_TYPE_UINT32:
3323 case LY_TYPE_INT64:
3324 case LY_TYPE_UINT64:
3325 return 1;
3326 case LY_TYPE_UNION:
3327 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003328 LY_ARRAY_FOR(uni->types, u) {
3329 ret = warn_is_numeric_type(uni->types[u]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003330 if (ret) {
3331 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003332 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003333 }
3334 }
3335 /* did not find any suitable type */
3336 return 0;
3337 case LY_TYPE_LEAFREF:
3338 return warn_is_numeric_type(((struct lysc_type_leafref *)type)->realtype);
3339 default:
3340 return 0;
3341 }
3342}
3343
3344/**
3345 * @brief Test whether a type is string-like - no integers, decimal64 or binary.
3346 *
3347 * @param[in] type Type to test.
Radek Krejci857189e2020-09-01 13:26:36 +02003348 * @return Boolean value whether @p type's basetype is string type or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003349 */
Radek Krejci857189e2020-09-01 13:26:36 +02003350static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003351warn_is_string_type(struct lysc_type *type)
3352{
3353 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003354 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003355 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003356
3357 switch (type->basetype) {
3358 case LY_TYPE_BITS:
3359 case LY_TYPE_ENUM:
3360 case LY_TYPE_IDENT:
3361 case LY_TYPE_INST:
3362 case LY_TYPE_STRING:
3363 return 1;
3364 case LY_TYPE_UNION:
3365 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003366 LY_ARRAY_FOR(uni->types, u) {
3367 ret = warn_is_string_type(uni->types[u]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003368 if (ret) {
3369 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003370 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003371 }
3372 }
3373 /* did not find any suitable type */
3374 return 0;
3375 case LY_TYPE_LEAFREF:
3376 return warn_is_string_type(((struct lysc_type_leafref *)type)->realtype);
3377 default:
3378 return 0;
3379 }
3380}
3381
3382/**
3383 * @brief Test whether a type is one specific type.
3384 *
3385 * @param[in] type Type to test.
3386 * @param[in] base Expected type.
Radek Krejci857189e2020-09-01 13:26:36 +02003387 * @return Boolean value whether the given @p type is of the specific basetype @p base.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003388 */
Radek Krejci857189e2020-09-01 13:26:36 +02003389static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003390warn_is_specific_type(struct lysc_type *type, LY_DATA_TYPE base)
3391{
3392 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003393 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003394 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003395
3396 if (type->basetype == base) {
3397 return 1;
3398 } else if (type->basetype == LY_TYPE_UNION) {
3399 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003400 LY_ARRAY_FOR(uni->types, u) {
3401 ret = warn_is_specific_type(uni->types[u], base);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003402 if (ret) {
3403 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003404 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003405 }
3406 }
3407 /* did not find any suitable type */
3408 return 0;
3409 } else if (type->basetype == LY_TYPE_LEAFREF) {
3410 return warn_is_specific_type(((struct lysc_type_leafref *)type)->realtype, base);
3411 }
3412
3413 return 0;
3414}
3415
3416/**
3417 * @brief Get next type of a (union) type.
3418 *
3419 * @param[in] type Base type.
3420 * @param[in] prev_type Previously returned type.
3421 * @return Next type or NULL.
3422 */
3423static struct lysc_type *
3424warn_is_equal_type_next_type(struct lysc_type *type, struct lysc_type *prev_type)
3425{
3426 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003427 ly_bool found = 0;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003428 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003429
3430 switch (type->basetype) {
3431 case LY_TYPE_UNION:
3432 uni = (struct lysc_type_union *)type;
3433 if (!prev_type) {
3434 return uni->types[0];
3435 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003436 LY_ARRAY_FOR(uni->types, u) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003437 if (found) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003438 return uni->types[u];
Michal Vasko03ff5a72019-09-11 13:49:33 +02003439 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003440 if (prev_type == uni->types[u]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003441 found = 1;
3442 }
3443 }
3444 return NULL;
3445 default:
3446 if (prev_type) {
3447 assert(type == prev_type);
3448 return NULL;
3449 } else {
3450 return type;
3451 }
3452 }
3453}
3454
3455/**
3456 * @brief Test whether 2 types have a common type.
3457 *
3458 * @param[in] type1 First type.
3459 * @param[in] type2 Second type.
3460 * @return 1 if they do, 0 otherwise.
3461 */
3462static int
3463warn_is_equal_type(struct lysc_type *type1, struct lysc_type *type2)
3464{
3465 struct lysc_type *t1, *rt1, *t2, *rt2;
3466
3467 t1 = NULL;
3468 while ((t1 = warn_is_equal_type_next_type(type1, t1))) {
3469 if (t1->basetype == LY_TYPE_LEAFREF) {
3470 rt1 = ((struct lysc_type_leafref *)t1)->realtype;
3471 } else {
3472 rt1 = t1;
3473 }
3474
3475 t2 = NULL;
3476 while ((t2 = warn_is_equal_type_next_type(type2, t2))) {
3477 if (t2->basetype == LY_TYPE_LEAFREF) {
3478 rt2 = ((struct lysc_type_leafref *)t2)->realtype;
3479 } else {
3480 rt2 = t2;
3481 }
3482
3483 if (rt2->basetype == rt1->basetype) {
3484 /* match found */
3485 return 1;
3486 }
3487 }
3488 }
3489
3490 return 0;
3491}
3492
3493/**
Michal Vaskoaa956522021-11-11 10:45:34 +01003494 * @brief Print warning with information about the XPath subexpression that caused previous warning.
3495 *
3496 * @param[in] ctx Context for logging.
3497 * @param[in] tok_pos Index of the subexpression in the whole expression.
3498 * @param[in] subexpr Subexpression start.
3499 * @param[in] subexpr_len Length of @p subexpr to print.
3500 * @param[in] cur_scnode Expression context node.
3501 */
3502static void
3503warn_subexpr_log(const struct ly_ctx *ctx, uint16_t tok_pos, const char *subexpr, int subexpr_len,
3504 const struct lysc_node *cur_scnode)
3505{
3506 char *path;
3507
3508 path = lysc_path(cur_scnode, LYSC_PATH_LOG, NULL, 0);
3509 LOGWRN(ctx, "Previous warning generated by XPath subexpression[%" PRIu16 "] \"%.*s\" with context node \"%s\".",
3510 tok_pos, subexpr_len, subexpr, path);
3511 free(path);
3512}
3513
3514/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02003515 * @brief Check both operands of comparison operators.
3516 *
3517 * @param[in] ctx Context for errors.
3518 * @param[in] set1 First operand set.
3519 * @param[in] set2 Second operand set.
3520 * @param[in] numbers_only Whether accept only numbers or other types are fine too (for '=' and '!=').
3521 * @param[in] expr Start of the expression to print with the warning.
3522 * @param[in] tok_pos Token position.
3523 */
3524static void
Michal Vaskoaa956522021-11-11 10:45:34 +01003525warn_operands(struct ly_ctx *ctx, struct lyxp_set *set1, struct lyxp_set *set2, ly_bool numbers_only, const char *expr,
3526 uint16_t tok_pos)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003527{
3528 struct lysc_node_leaf *node1, *node2;
Radek Krejci857189e2020-09-01 13:26:36 +02003529 ly_bool leaves = 1, warning = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003530
3531 node1 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set1);
3532 node2 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set2);
3533
3534 if (!node1 && !node2) {
3535 /* no node-sets involved, nothing to do */
3536 return;
3537 }
3538
3539 if (node1) {
3540 if (!(node1->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3541 LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node1->nodetype), node1->name);
3542 warning = 1;
3543 leaves = 0;
3544 } else if (numbers_only && !warn_is_numeric_type(node1->type)) {
3545 LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node1->name);
3546 warning = 1;
3547 }
3548 }
3549
3550 if (node2) {
3551 if (!(node2->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3552 LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node2->nodetype), node2->name);
3553 warning = 1;
3554 leaves = 0;
3555 } else if (numbers_only && !warn_is_numeric_type(node2->type)) {
3556 LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node2->name);
3557 warning = 1;
3558 }
3559 }
3560
3561 if (node1 && node2 && leaves && !numbers_only) {
Michal Vasko69730152020-10-09 16:30:07 +02003562 if ((warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type)) ||
3563 (!warn_is_numeric_type(node1->type) && warn_is_numeric_type(node2->type)) ||
3564 (!warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type) &&
3565 !warn_is_equal_type(node1->type, node2->type))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003566 LOGWRN(ctx, "Incompatible types of operands \"%s\" and \"%s\" for comparison.", node1->name, node2->name);
3567 warning = 1;
3568 }
3569 }
3570
3571 if (warning) {
Michal Vaskoaa956522021-11-11 10:45:34 +01003572 warn_subexpr_log(ctx, tok_pos, expr + tok_pos, 20, set1->cur_scnode);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003573 }
3574}
3575
3576/**
3577 * @brief Check that a value is valid for a leaf. If not applicable, does nothing.
3578 *
3579 * @param[in] exp Parsed XPath expression.
3580 * @param[in] set Set with the leaf/leaf-list.
3581 * @param[in] val_exp Index of the value (literal/number) in @p exp.
3582 * @param[in] equal_exp Index of the start of the equality expression in @p exp.
3583 * @param[in] last_equal_exp Index of the end of the equality expression in @p exp.
3584 */
3585static void
Michal Vasko40308e72020-10-20 16:38:40 +02003586warn_equality_value(const struct lyxp_expr *exp, struct lyxp_set *set, uint16_t val_exp, uint16_t equal_exp,
3587 uint16_t last_equal_exp)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003588{
3589 struct lysc_node *scnode;
3590 struct lysc_type *type;
3591 char *value;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003592 struct lyd_value storage;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003593 LY_ERR rc;
3594 struct ly_err_item *err = NULL;
3595
Michal Vasko69730152020-10-09 16:30:07 +02003596 if ((scnode = warn_get_scnode_in_ctx(set)) && (scnode->nodetype & (LYS_LEAF | LYS_LEAFLIST)) &&
3597 ((exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) || (exp->tokens[val_exp] == LYXP_TOKEN_NUMBER))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003598 /* check that the node can have the specified value */
3599 if (exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) {
3600 value = strndup(exp->expr + exp->tok_pos[val_exp] + 1, exp->tok_len[val_exp] - 2);
3601 } else {
3602 value = strndup(exp->expr + exp->tok_pos[val_exp], exp->tok_len[val_exp]);
3603 }
3604 if (!value) {
3605 LOGMEM(set->ctx);
3606 return;
3607 }
3608
3609 if ((((struct lysc_node_leaf *)scnode)->type->basetype == LY_TYPE_IDENT) && !strchr(value, ':')) {
3610 LOGWRN(set->ctx, "Identityref \"%s\" comparison with identity \"%s\" without prefix, consider adding"
Michal Vasko69730152020-10-09 16:30:07 +02003611 " a prefix or best using \"derived-from(-or-self)()\" functions.", scnode->name, value);
Michal Vaskoaa956522021-11-11 10:45:34 +01003612 warn_subexpr_log(set->ctx, exp->tok_pos[equal_exp], exp->expr + exp->tok_pos[equal_exp],
Radek Krejci0f969882020-08-21 16:56:47 +02003613 (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp],
Michal Vaskoaa956522021-11-11 10:45:34 +01003614 set->cur_scnode);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003615 }
3616
3617 type = ((struct lysc_node_leaf *)scnode)->type;
3618 if (type->basetype != LY_TYPE_IDENT) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003619 rc = type->plugin->store(set->ctx, type, value, strlen(value), 0, set->format, set->prefix_data,
Michal Vasko405cc9e2020-12-01 12:01:27 +01003620 LYD_HINT_DATA, scnode, &storage, NULL, &err);
Michal Vaskobf42e832020-11-23 16:59:42 +01003621 if (rc == LY_EINCOMPLETE) {
3622 rc = LY_SUCCESS;
3623 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003624
3625 if (err) {
3626 LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type (%s).", value, err->msg);
3627 ly_err_free(err);
3628 } else if (rc != LY_SUCCESS) {
3629 LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type.", value);
3630 }
3631 if (rc != LY_SUCCESS) {
Michal Vaskoaa956522021-11-11 10:45:34 +01003632 warn_subexpr_log(set->ctx, exp->tok_pos[equal_exp], exp->expr + exp->tok_pos[equal_exp],
Radek Krejci0f969882020-08-21 16:56:47 +02003633 (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp],
Michal Vaskoaa956522021-11-11 10:45:34 +01003634 set->cur_scnode);
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003635 } else {
3636 type->plugin->free(set->ctx, &storage);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003637 }
3638 }
3639 free(value);
3640 }
3641}
3642
3643/*
3644 * XPath functions
3645 */
3646
3647/**
3648 * @brief Execute the YANG 1.1 bit-is-set(node-set, string) function. Returns LYXP_SET_BOOLEAN
3649 * depending on whether the first node bit value from the second argument is set.
3650 *
3651 * @param[in] args Array of arguments.
3652 * @param[in] arg_count Count of elements in @p args.
3653 * @param[in,out] set Context and result set at the same time.
3654 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003655 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003656 */
3657static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003658xpath_bit_is_set(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003659{
3660 struct lyd_node_term *leaf;
3661 struct lysc_node_leaf *sleaf;
Michal Vasko2588b952021-07-29 07:43:26 +02003662 struct lyd_value_bits *bits;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003663 LY_ERR rc = LY_SUCCESS;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003664 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003665
3666 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003667 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003668 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003669 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3670 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3671 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3672 sleaf->name);
3673 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_BITS)) {
3674 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"bits\".", __func__, sleaf->name);
3675 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003676 }
3677
3678 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3679 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003680 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3681 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003682 } else if (!warn_is_string_type(sleaf->type)) {
3683 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003684 }
3685 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003686 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003687 return rc;
3688 }
3689
Michal Vaskod3678892020-05-21 10:06:58 +02003690 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003691 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 +02003692 return LY_EVALID;
3693 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003694 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003695 LY_CHECK_RET(rc);
3696
3697 set_fill_boolean(set, 0);
Michal Vaskod3678892020-05-21 10:06:58 +02003698 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003699 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
Michal Vasko2588b952021-07-29 07:43:26 +02003700 if ((leaf->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (leaf->value.realtype->basetype == LY_TYPE_BITS)) {
3701 LYD_VALUE_GET(&leaf->value, bits);
3702 LY_ARRAY_FOR(bits->items, u) {
3703 if (!strcmp(bits->items[u]->name, args[1]->val.str)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003704 set_fill_boolean(set, 1);
3705 break;
3706 }
3707 }
3708 }
3709 }
3710
3711 return LY_SUCCESS;
3712}
3713
3714/**
3715 * @brief Execute the XPath boolean(object) function. Returns LYXP_SET_BOOLEAN
3716 * with the argument converted to boolean.
3717 *
3718 * @param[in] args Array of arguments.
3719 * @param[in] arg_count Count of elements in @p args.
3720 * @param[in,out] set Context and result set at the same time.
3721 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003722 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003723 */
3724static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003725xpath_boolean(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003726{
3727 LY_ERR rc;
3728
3729 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02003730 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003731 return LY_SUCCESS;
3732 }
3733
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003734 rc = lyxp_set_cast(args[0], LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003735 LY_CHECK_RET(rc);
3736 set_fill_set(set, args[0]);
3737
3738 return LY_SUCCESS;
3739}
3740
3741/**
3742 * @brief Execute the XPath ceiling(number) function. Returns LYXP_SET_NUMBER
3743 * with the first argument rounded up to the nearest integer.
3744 *
3745 * @param[in] args Array of arguments.
3746 * @param[in] arg_count Count of elements in @p args.
3747 * @param[in,out] set Context and result set at the same time.
3748 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003749 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003750 */
3751static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003752xpath_ceiling(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003753{
3754 struct lysc_node_leaf *sleaf;
3755 LY_ERR rc = LY_SUCCESS;
3756
3757 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003758 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003759 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003760 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3761 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3762 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3763 sleaf->name);
3764 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
3765 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
3766 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003767 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003768 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003769 return rc;
3770 }
3771
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003772 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003773 LY_CHECK_RET(rc);
3774 if ((long long)args[0]->val.num != args[0]->val.num) {
3775 set_fill_number(set, ((long long)args[0]->val.num) + 1);
3776 } else {
3777 set_fill_number(set, args[0]->val.num);
3778 }
3779
3780 return LY_SUCCESS;
3781}
3782
3783/**
3784 * @brief Execute the XPath concat(string, string, string*) function.
3785 * Returns LYXP_SET_STRING with the concatenation of all the arguments.
3786 *
3787 * @param[in] args Array of arguments.
3788 * @param[in] arg_count Count of elements in @p args.
3789 * @param[in,out] set Context and result set at the same time.
3790 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003791 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003792 */
3793static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003794xpath_concat(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003795{
3796 uint16_t i;
3797 char *str = NULL;
3798 size_t used = 1;
3799 LY_ERR rc = LY_SUCCESS;
3800 struct lysc_node_leaf *sleaf;
3801
3802 if (options & LYXP_SCNODE_ALL) {
3803 for (i = 0; i < arg_count; ++i) {
3804 if ((args[i]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[i]))) {
3805 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3806 LOGWRN(set->ctx, "Argument #%u of %s is a %s node \"%s\".",
Michal Vasko69730152020-10-09 16:30:07 +02003807 i + 1, __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003808 } else if (!warn_is_string_type(sleaf->type)) {
Radek Krejci70124c82020-08-14 22:17:03 +02003809 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 +02003810 }
3811 }
3812 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003813 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003814 return rc;
3815 }
3816
3817 for (i = 0; i < arg_count; ++i) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003818 rc = lyxp_set_cast(args[i], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003819 if (rc != LY_SUCCESS) {
3820 free(str);
3821 return rc;
3822 }
3823
3824 str = ly_realloc(str, (used + strlen(args[i]->val.str)) * sizeof(char));
3825 LY_CHECK_ERR_RET(!str, LOGMEM(set->ctx), LY_EMEM);
3826 strcpy(str + used - 1, args[i]->val.str);
3827 used += strlen(args[i]->val.str);
3828 }
3829
3830 /* free, kind of */
Michal Vaskod3678892020-05-21 10:06:58 +02003831 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003832 set->type = LYXP_SET_STRING;
3833 set->val.str = str;
3834
3835 return LY_SUCCESS;
3836}
3837
3838/**
3839 * @brief Execute the XPath contains(string, string) function.
3840 * Returns LYXP_SET_BOOLEAN whether the second argument can
3841 * be found in the first or not.
3842 *
3843 * @param[in] args Array of arguments.
3844 * @param[in] arg_count Count of elements in @p args.
3845 * @param[in,out] set Context and result set at the same time.
3846 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003847 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003848 */
3849static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003850xpath_contains(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003851{
3852 struct lysc_node_leaf *sleaf;
3853 LY_ERR rc = LY_SUCCESS;
3854
3855 if (options & LYXP_SCNODE_ALL) {
3856 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3857 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003858 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3859 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003860 } else if (!warn_is_string_type(sleaf->type)) {
3861 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003862 }
3863 }
3864
3865 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3866 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003867 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3868 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003869 } else if (!warn_is_string_type(sleaf->type)) {
3870 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003871 }
3872 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003873 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003874 return rc;
3875 }
3876
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003877 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003878 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003879 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003880 LY_CHECK_RET(rc);
3881
3882 if (strstr(args[0]->val.str, args[1]->val.str)) {
3883 set_fill_boolean(set, 1);
3884 } else {
3885 set_fill_boolean(set, 0);
3886 }
3887
3888 return LY_SUCCESS;
3889}
3890
3891/**
3892 * @brief Execute the XPath count(node-set) function. Returns LYXP_SET_NUMBER
3893 * with the size of the node-set from the argument.
3894 *
3895 * @param[in] args Array of arguments.
3896 * @param[in] arg_count Count of elements in @p args.
3897 * @param[in,out] set Context and result set at the same time.
3898 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003899 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003900 */
3901static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003902xpath_count(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003903{
Michal Vasko03ff5a72019-09-11 13:49:33 +02003904 LY_ERR rc = LY_SUCCESS;
3905
3906 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003907 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003908 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003909 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003910 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003911 return rc;
3912 }
3913
Michal Vasko03ff5a72019-09-11 13:49:33 +02003914 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003915 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "count(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003916 return LY_EVALID;
3917 }
3918
3919 set_fill_number(set, args[0]->used);
3920 return LY_SUCCESS;
3921}
3922
3923/**
3924 * @brief Execute the XPath current() function. Returns LYXP_SET_NODE_SET
3925 * with the context with the intial node.
3926 *
3927 * @param[in] args Array of arguments.
3928 * @param[in] arg_count Count of elements in @p args.
3929 * @param[in,out] set Context and result set at the same time.
3930 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003931 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003932 */
3933static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003934xpath_current(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003935{
3936 if (arg_count || args) {
Radek Krejcie87c7dc2021-06-02 21:25:42 +02003937 LOGVAL(set->ctx, LY_VCODE_XP_INARGCOUNT, arg_count, LY_PRI_LENSTR("current()"));
Michal Vasko03ff5a72019-09-11 13:49:33 +02003938 return LY_EVALID;
3939 }
3940
3941 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02003942 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003943
Michal Vasko296dfaf2021-12-13 16:57:42 +01003944 if (set->cur_scnode) {
Michal Vasko7333cb32022-07-29 16:30:29 +02003945 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 +01003946 } else {
3947 /* root node */
Michal Vasko7333cb32022-07-29 16:30:29 +02003948 LY_CHECK_RET(set_scnode_insert_node(set, NULL, set->root_type, LYXP_AXIS_SELF, NULL));
Michal Vasko296dfaf2021-12-13 16:57:42 +01003949 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003950 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02003951 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003952
Michal Vasko296dfaf2021-12-13 16:57:42 +01003953 if (set->cur_node) {
3954 /* position is filled later */
3955 set_insert_node(set, set->cur_node, 0, LYXP_NODE_ELEM, 0);
3956 } else {
3957 /* root node */
3958 set_insert_node(set, NULL, 0, set->root_type, 0);
3959 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003960 }
3961
3962 return LY_SUCCESS;
3963}
3964
3965/**
3966 * @brief Execute the YANG 1.1 deref(node-set) function. Returns LYXP_SET_NODE_SET with either
3967 * leafref or instance-identifier target node(s).
3968 *
3969 * @param[in] args Array of arguments.
3970 * @param[in] arg_count Count of elements in @p args.
3971 * @param[in,out] set Context and result set at the same time.
3972 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003973 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003974 */
3975static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003976xpath_deref(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003977{
3978 struct lyd_node_term *leaf;
Michal Vasko42e497c2020-01-06 08:38:25 +01003979 struct lysc_node_leaf *sleaf = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02003980 struct lysc_type_leafref *lref;
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003981 const struct lysc_node *target;
Michal Vasko004d3152020-06-11 19:59:22 +02003982 struct ly_path *p;
3983 struct lyd_node *node;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003984 char *errmsg = NULL;
Michal Vasko00cbf532020-06-15 13:58:47 +02003985 uint8_t oper;
Michal Vasko741bb562021-06-24 11:59:50 +02003986 LY_ERR r;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003987
3988 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003989 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003990 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003991 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
Michal Vasko423ba3f2021-07-19 13:08:50 +02003992 if (!(sleaf->nodetype & LYD_NODE_TERM)) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003993 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3994 sleaf->name);
Michal Vaskoed725d72021-06-23 12:03:45 +02003995 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_LEAFREF) &&
3996 !warn_is_specific_type(sleaf->type, LY_TYPE_INST)) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003997 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"leafref\" nor \"instance-identifier\".",
3998 __func__, sleaf->name);
3999 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004000 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004001 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko423ba3f2021-07-19 13:08:50 +02004002 if (sleaf && (sleaf->nodetype & LYD_NODE_TERM) && (sleaf->type->basetype == LY_TYPE_LEAFREF)) {
Michal Vasko004d3152020-06-11 19:59:22 +02004003 lref = (struct lysc_type_leafref *)sleaf->type;
Michal Vaskod1e53b92021-01-28 13:11:06 +01004004 oper = (sleaf->flags & LYS_IS_OUTPUT) ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT;
Michal Vasko004d3152020-06-11 19:59:22 +02004005
4006 /* it was already evaluated on schema, it must succeed */
Michal Vasko741bb562021-06-24 11:59:50 +02004007 r = ly_path_compile_leafref(set->ctx, &sleaf->node, NULL, lref->path, oper, LY_PATH_TARGET_MANY,
Michal Vasko24fc4d12021-07-12 14:41:20 +02004008 LY_VALUE_SCHEMA_RESOLVED, lref->prefixes, &p);
Michal Vasko741bb562021-06-24 11:59:50 +02004009 if (!r) {
4010 /* get the target node */
4011 target = p[LY_ARRAY_COUNT(p) - 1].node;
4012 ly_path_free(set->ctx, p);
Michal Vasko004d3152020-06-11 19:59:22 +02004013
Michal Vasko7333cb32022-07-29 16:30:29 +02004014 LY_CHECK_RET(set_scnode_insert_node(set, target, LYXP_NODE_ELEM, LYXP_AXIS_SELF, NULL));
Michal Vasko741bb562021-06-24 11:59:50 +02004015 } /* else the target was found before but is disabled so it was removed */
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02004016 }
4017
Michal Vasko741bb562021-06-24 11:59:50 +02004018 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004019 }
4020
Michal Vaskod3678892020-05-21 10:06:58 +02004021 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004022 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "deref(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004023 return LY_EVALID;
4024 }
4025
Michal Vaskod3678892020-05-21 10:06:58 +02004026 lyxp_set_free_content(set);
4027 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004028 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
4029 sleaf = (struct lysc_node_leaf *)leaf->schema;
4030 if (sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
4031 if (sleaf->type->basetype == LY_TYPE_LEAFREF) {
4032 /* find leafref target */
Radek Krejci0b013302021-03-29 15:22:32 +02004033 if (lyplg_type_resolve_leafref((struct lysc_type_leafref *)sleaf->type, &leaf->node, &leaf->value, set->tree,
Michal Vasko9e685082021-01-29 14:49:09 +01004034 &node, &errmsg)) {
Michal Vasko004d3152020-06-11 19:59:22 +02004035 LOGERR(set->ctx, LY_EVALID, errmsg);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004036 free(errmsg);
Michal Vasko004d3152020-06-11 19:59:22 +02004037 return LY_EVALID;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004038 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004039 } else {
4040 assert(sleaf->type->basetype == LY_TYPE_INST);
Michal Vasko004d3152020-06-11 19:59:22 +02004041 if (ly_path_eval(leaf->value.target, set->tree, &node)) {
Michal Vaskoba99a3e2020-08-18 15:50:05 +02004042 LOGERR(set->ctx, LY_EVALID, "Invalid instance-identifier \"%s\" value - required instance not found.",
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02004043 lyd_get_value(&leaf->node));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004044 return LY_EVALID;
4045 }
4046 }
Michal Vasko004d3152020-06-11 19:59:22 +02004047
4048 /* insert it */
4049 set_insert_node(set, node, 0, LYXP_NODE_ELEM, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004050 }
4051 }
4052
4053 return LY_SUCCESS;
4054}
4055
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004056static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02004057xpath_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 +02004058{
Michal Vaskofe1af042022-07-29 14:58:59 +02004059 uint32_t i, id_len;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004060 LY_ARRAY_COUNT_TYPE u;
4061 struct lyd_node_term *leaf;
4062 struct lysc_node_leaf *sleaf;
4063 struct lyd_meta *meta;
Michal Vasko93923692021-05-07 15:28:02 +02004064 struct lyd_value *val;
4065 const struct lys_module *mod;
4066 const char *id_name;
Michal Vasko93923692021-05-07 15:28:02 +02004067 struct lysc_ident *id;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004068 LY_ERR rc = LY_SUCCESS;
Radek Krejci857189e2020-09-01 13:26:36 +02004069 ly_bool found;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004070
4071 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02004072 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004073 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", func);
Michal Vasko5676f4e2021-04-06 17:14:45 +02004074 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4075 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4076 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype),
4077 sleaf->name);
4078 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_IDENT)) {
4079 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"identityref\".", func, sleaf->name);
4080 }
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004081 }
4082
4083 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4084 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4085 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype),
Michal Vasko69730152020-10-09 16:30:07 +02004086 sleaf->name);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004087 } else if (!warn_is_string_type(sleaf->type)) {
4088 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", func, sleaf->name);
4089 }
4090 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004091 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004092 return rc;
4093 }
4094
4095 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004096 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 +02004097 return LY_EVALID;
4098 }
4099 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
4100 LY_CHECK_RET(rc);
4101
Michal Vasko93923692021-05-07 15:28:02 +02004102 /* parse the identity */
4103 id_name = args[1]->val.str;
4104 id_len = strlen(id_name);
4105 rc = moveto_resolve_model(&id_name, &id_len, set, set->cur_node ? set->cur_node->schema : NULL, &mod);
4106 LY_CHECK_RET(rc);
4107 if (!mod) {
4108 LOGVAL(set->ctx, LYVE_XPATH, "Identity \"%.*s\" without a prefix.", (int)id_len, id_name);
4109 return LY_EVALID;
4110 }
4111
4112 /* find the identity */
4113 found = 0;
4114 LY_ARRAY_FOR(mod->identities, u) {
4115 if (!ly_strncmp(mod->identities[u].name, id_name, id_len)) {
4116 /* we have match */
4117 found = 1;
4118 break;
4119 }
4120 }
4121 if (!found) {
4122 LOGVAL(set->ctx, LYVE_XPATH, "Identity \"%.*s\" not found in module \"%s\".", (int)id_len, id_name, mod->name);
4123 return LY_EVALID;
4124 }
4125 id = &mod->identities[u];
4126
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004127 set_fill_boolean(set, 0);
4128 found = 0;
4129 for (i = 0; i < args[0]->used; ++i) {
4130 if ((args[0]->val.nodes[i].type != LYXP_NODE_ELEM) && (args[0]->val.nodes[i].type != LYXP_NODE_META)) {
4131 continue;
4132 }
4133
4134 if (args[0]->val.nodes[i].type == LYXP_NODE_ELEM) {
4135 leaf = (struct lyd_node_term *)args[0]->val.nodes[i].node;
4136 sleaf = (struct lysc_node_leaf *)leaf->schema;
4137 val = &leaf->value;
4138 if (!(sleaf->nodetype & LYD_NODE_TERM) || (leaf->value.realtype->basetype != LY_TYPE_IDENT)) {
4139 /* uninteresting */
4140 continue;
4141 }
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004142 } else {
4143 meta = args[0]->val.meta[i].meta;
4144 val = &meta->value;
4145 if (val->realtype->basetype != LY_TYPE_IDENT) {
4146 /* uninteresting */
4147 continue;
4148 }
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004149 }
4150
Michal Vasko93923692021-05-07 15:28:02 +02004151 /* check the identity itself */
4152 if (self_match && (id == val->ident)) {
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004153 set_fill_boolean(set, 1);
4154 found = 1;
4155 }
Michal Vasko93923692021-05-07 15:28:02 +02004156 if (!found && !lyplg_type_identity_isderived(id, val->ident)) {
4157 set_fill_boolean(set, 1);
4158 found = 1;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004159 }
4160
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004161 if (found) {
4162 break;
4163 }
4164 }
4165
4166 return LY_SUCCESS;
4167}
4168
Michal Vasko03ff5a72019-09-11 13:49:33 +02004169/**
4170 * @brief Execute the YANG 1.1 derived-from(node-set, string) function. Returns LYXP_SET_BOOLEAN depending
4171 * on whether the first argument nodes contain a node of an identity derived from the second
4172 * argument identity.
4173 *
4174 * @param[in] args Array of arguments.
4175 * @param[in] arg_count Count of elements in @p args.
4176 * @param[in,out] set Context and result set at the same time.
4177 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004178 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004179 */
4180static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004181xpath_derived_from(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004182{
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004183 return xpath_derived_(args, set, options, 0, __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004184}
4185
4186/**
4187 * @brief Execute the YANG 1.1 derived-from-or-self(node-set, string) function. Returns LYXP_SET_BOOLEAN depending
4188 * on whether the first argument nodes contain a node of an identity that either is or is derived from
4189 * the second argument identity.
4190 *
4191 * @param[in] args Array of arguments.
4192 * @param[in] arg_count Count of elements in @p args.
4193 * @param[in,out] set Context and result set at the same time.
4194 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004195 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004196 */
4197static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004198xpath_derived_from_or_self(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004199{
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004200 return xpath_derived_(args, set, options, 1, __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004201}
4202
4203/**
4204 * @brief Execute the YANG 1.1 enum-value(node-set) function. Returns LYXP_SET_NUMBER
4205 * with the integer value of the first node's enum value, otherwise NaN.
4206 *
4207 * @param[in] args Array of arguments.
4208 * @param[in] arg_count Count of elements in @p args.
4209 * @param[in,out] set Context and result set at the same time.
4210 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004211 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004212 */
4213static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004214xpath_enum_value(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004215{
4216 struct lyd_node_term *leaf;
4217 struct lysc_node_leaf *sleaf;
4218 LY_ERR rc = LY_SUCCESS;
4219
4220 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02004221 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004222 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02004223 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4224 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4225 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
4226 sleaf->name);
4227 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_ENUM)) {
4228 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"enumeration\".", __func__, sleaf->name);
4229 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004230 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004231 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004232 return rc;
4233 }
4234
Michal Vaskod3678892020-05-21 10:06:58 +02004235 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004236 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "enum-value(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004237 return LY_EVALID;
4238 }
4239
4240 set_fill_number(set, NAN);
Michal Vaskod3678892020-05-21 10:06:58 +02004241 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004242 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
4243 sleaf = (struct lysc_node_leaf *)leaf->schema;
4244 if ((sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (sleaf->type->basetype == LY_TYPE_ENUM)) {
4245 set_fill_number(set, leaf->value.enum_item->value);
4246 }
4247 }
4248
4249 return LY_SUCCESS;
4250}
4251
4252/**
4253 * @brief Execute the XPath false() function. Returns LYXP_SET_BOOLEAN
4254 * with false value.
4255 *
4256 * @param[in] args Array of arguments.
4257 * @param[in] arg_count Count of elements in @p args.
4258 * @param[in,out] set Context and result set at the same time.
4259 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004260 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004261 */
4262static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004263xpath_false(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004264{
4265 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004266 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004267 return LY_SUCCESS;
4268 }
4269
4270 set_fill_boolean(set, 0);
4271 return LY_SUCCESS;
4272}
4273
4274/**
4275 * @brief Execute the XPath floor(number) function. Returns LYXP_SET_NUMBER
4276 * with the first argument floored (truncated).
4277 *
4278 * @param[in] args Array of arguments.
4279 * @param[in] arg_count Count of elements in @p args.
4280 * @param[in,out] set Context and result set at the same time.
4281 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004282 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004283 */
4284static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004285xpath_floor(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t UNUSED(options))
Michal Vasko03ff5a72019-09-11 13:49:33 +02004286{
4287 LY_ERR rc;
4288
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004289 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004290 LY_CHECK_RET(rc);
4291 if (isfinite(args[0]->val.num)) {
4292 set_fill_number(set, (long long)args[0]->val.num);
4293 }
4294
4295 return LY_SUCCESS;
4296}
4297
4298/**
4299 * @brief Execute the XPath lang(string) function. Returns LYXP_SET_BOOLEAN
4300 * whether the language of the text matches the one from the argument.
4301 *
4302 * @param[in] args Array of arguments.
4303 * @param[in] arg_count Count of elements in @p args.
4304 * @param[in,out] set Context and result set at the same time.
4305 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004306 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004307 */
4308static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004309xpath_lang(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004310{
4311 const struct lyd_node *node;
4312 struct lysc_node_leaf *sleaf;
Michal Vasko9f96a052020-03-10 09:41:45 +01004313 struct lyd_meta *meta = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004314 const char *val;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004315 LY_ERR rc = LY_SUCCESS;
4316
4317 if (options & LYXP_SCNODE_ALL) {
4318 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4319 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004320 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
4321 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004322 } else if (!warn_is_string_type(sleaf->type)) {
4323 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004324 }
4325 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004326 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004327 return rc;
4328 }
4329
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004330 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004331 LY_CHECK_RET(rc);
4332
Michal Vasko03ff5a72019-09-11 13:49:33 +02004333 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004334 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "lang(string)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004335 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004336 } else if (!set->used) {
4337 set_fill_boolean(set, 0);
4338 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004339 }
4340
4341 switch (set->val.nodes[0].type) {
4342 case LYXP_NODE_ELEM:
4343 case LYXP_NODE_TEXT:
4344 node = set->val.nodes[0].node;
4345 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004346 case LYXP_NODE_META:
4347 node = set->val.meta[0].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004348 break;
4349 default:
4350 /* nothing to do with roots */
4351 set_fill_boolean(set, 0);
4352 return LY_SUCCESS;
4353 }
4354
Michal Vasko9f96a052020-03-10 09:41:45 +01004355 /* find lang metadata */
Michal Vasko9e685082021-01-29 14:49:09 +01004356 for ( ; node; node = lyd_parent(node)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01004357 for (meta = node->meta; meta; meta = meta->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004358 /* annotations */
Michal Vasko9f96a052020-03-10 09:41:45 +01004359 if (meta->name && !strcmp(meta->name, "lang") && !strcmp(meta->annotation->module->name, "xml")) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004360 break;
4361 }
4362 }
4363
Michal Vasko9f96a052020-03-10 09:41:45 +01004364 if (meta) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004365 break;
4366 }
4367 }
4368
4369 /* compare languages */
Michal Vasko9f96a052020-03-10 09:41:45 +01004370 if (!meta) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004371 set_fill_boolean(set, 0);
4372 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004373 uint64_t i;
4374
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02004375 val = lyd_get_meta_value(meta);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004376 for (i = 0; args[0]->val.str[i]; ++i) {
4377 if (tolower(args[0]->val.str[i]) != tolower(val[i])) {
4378 set_fill_boolean(set, 0);
4379 break;
4380 }
4381 }
4382 if (!args[0]->val.str[i]) {
4383 if (!val[i] || (val[i] == '-')) {
4384 set_fill_boolean(set, 1);
4385 } else {
4386 set_fill_boolean(set, 0);
4387 }
4388 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004389 }
4390
4391 return LY_SUCCESS;
4392}
4393
4394/**
4395 * @brief Execute the XPath last() function. Returns LYXP_SET_NUMBER
4396 * with the context size.
4397 *
4398 * @param[in] args Array of arguments.
4399 * @param[in] arg_count Count of elements in @p args.
4400 * @param[in,out] set Context and result set at the same time.
4401 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004402 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004403 */
4404static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004405xpath_last(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004406{
4407 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004408 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004409 return LY_SUCCESS;
4410 }
4411
Michal Vasko03ff5a72019-09-11 13:49:33 +02004412 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004413 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "last()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004414 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004415 } else if (!set->used) {
4416 set_fill_number(set, 0);
4417 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004418 }
4419
4420 set_fill_number(set, set->ctx_size);
4421 return LY_SUCCESS;
4422}
4423
4424/**
4425 * @brief Execute the XPath local-name(node-set?) function. Returns LYXP_SET_STRING
4426 * with the node name without namespace from the argument or the context.
4427 *
4428 * @param[in] args Array of arguments.
4429 * @param[in] arg_count Count of elements in @p args.
4430 * @param[in,out] set Context and result set at the same time.
4431 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004432 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004433 */
4434static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004435xpath_local_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004436{
4437 struct lyxp_set_node *item;
Michal Vasko69730152020-10-09 16:30:07 +02004438
Michal Vasko03ff5a72019-09-11 13:49:33 +02004439 /* suppress unused variable warning */
4440 (void)options;
4441
4442 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004443 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004444 return LY_SUCCESS;
4445 }
4446
4447 if (arg_count) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004448 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004449 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004450 "local-name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004451 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004452 } else if (!args[0]->used) {
4453 set_fill_string(set, "", 0);
4454 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004455 }
4456
4457 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004458 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004459
4460 item = &args[0]->val.nodes[0];
4461 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004462 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004463 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "local-name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004464 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004465 } else if (!set->used) {
4466 set_fill_string(set, "", 0);
4467 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004468 }
4469
4470 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004471 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004472
4473 item = &set->val.nodes[0];
4474 }
4475
4476 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004477 case LYXP_NODE_NONE:
4478 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004479 case LYXP_NODE_ROOT:
4480 case LYXP_NODE_ROOT_CONFIG:
4481 case LYXP_NODE_TEXT:
4482 set_fill_string(set, "", 0);
4483 break;
4484 case LYXP_NODE_ELEM:
4485 set_fill_string(set, item->node->schema->name, strlen(item->node->schema->name));
4486 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004487 case LYXP_NODE_META:
4488 set_fill_string(set, ((struct lyd_meta *)item->node)->name, strlen(((struct lyd_meta *)item->node)->name));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004489 break;
4490 }
4491
4492 return LY_SUCCESS;
4493}
4494
4495/**
4496 * @brief Execute the XPath name(node-set?) function. Returns LYXP_SET_STRING
4497 * with the node name fully qualified (with namespace) from the argument or the context.
4498 *
4499 * @param[in] args Array of arguments.
4500 * @param[in] arg_count Count of elements in @p args.
4501 * @param[in,out] set Context and result set at the same time.
4502 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004503 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004504 */
4505static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004506xpath_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004507{
4508 struct lyxp_set_node *item;
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004509 struct lys_module *mod = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004510 char *str;
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004511 const char *name = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004512
4513 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004514 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004515 return LY_SUCCESS;
4516 }
4517
4518 if (arg_count) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004519 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004520 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004521 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004522 } else if (!args[0]->used) {
4523 set_fill_string(set, "", 0);
4524 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004525 }
4526
4527 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004528 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004529
4530 item = &args[0]->val.nodes[0];
4531 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004532 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004533 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004534 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004535 } else if (!set->used) {
4536 set_fill_string(set, "", 0);
4537 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004538 }
4539
4540 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004541 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004542
4543 item = &set->val.nodes[0];
4544 }
4545
4546 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004547 case LYXP_NODE_NONE:
4548 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004549 case LYXP_NODE_ROOT:
4550 case LYXP_NODE_ROOT_CONFIG:
4551 case LYXP_NODE_TEXT:
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004552 /* keep NULL */
Michal Vasko03ff5a72019-09-11 13:49:33 +02004553 break;
4554 case LYXP_NODE_ELEM:
4555 mod = item->node->schema->module;
4556 name = item->node->schema->name;
4557 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004558 case LYXP_NODE_META:
4559 mod = ((struct lyd_meta *)item->node)->annotation->module;
4560 name = ((struct lyd_meta *)item->node)->name;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004561 break;
4562 }
4563
4564 if (mod && name) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004565 int rc = asprintf(&str, "%s:%s", ly_get_prefix(mod, set->format, set->prefix_data), name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004566 LY_CHECK_ERR_RET(rc == -1, LOGMEM(set->ctx), LY_EMEM);
4567 set_fill_string(set, str, strlen(str));
4568 free(str);
4569 } else {
4570 set_fill_string(set, "", 0);
4571 }
4572
4573 return LY_SUCCESS;
4574}
4575
4576/**
4577 * @brief Execute the XPath namespace-uri(node-set?) function. Returns LYXP_SET_STRING
4578 * with the namespace of the node from the argument or the context.
4579 *
4580 * @param[in] args Array of arguments.
4581 * @param[in] arg_count Count of elements in @p args.
4582 * @param[in,out] set Context and result set at the same time.
4583 * @param[in] options XPath options.
4584 * @return LY_ERR (LY_EINVAL for wrong arguments on schema)
4585 */
4586static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004587xpath_namespace_uri(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004588{
4589 struct lyxp_set_node *item;
4590 struct lys_module *mod;
Michal Vasko69730152020-10-09 16:30:07 +02004591
Michal Vasko03ff5a72019-09-11 13:49:33 +02004592 /* suppress unused variable warning */
4593 (void)options;
4594
4595 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004596 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004597 return LY_SUCCESS;
4598 }
4599
4600 if (arg_count) {
Michal Vaskod3678892020-05-21 10:06:58 +02004601 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004602 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
Michal Vasko69730152020-10-09 16:30:07 +02004603 "namespace-uri(node-set?)");
Michal Vaskod3678892020-05-21 10:06:58 +02004604 return LY_EVALID;
4605 } else if (!args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004606 set_fill_string(set, "", 0);
4607 return LY_SUCCESS;
4608 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004609
4610 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004611 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004612
4613 item = &args[0]->val.nodes[0];
4614 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004615 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004616 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "namespace-uri(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004617 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004618 } else if (!set->used) {
4619 set_fill_string(set, "", 0);
4620 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004621 }
4622
4623 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004624 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004625
4626 item = &set->val.nodes[0];
4627 }
4628
4629 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004630 case LYXP_NODE_NONE:
4631 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004632 case LYXP_NODE_ROOT:
4633 case LYXP_NODE_ROOT_CONFIG:
4634 case LYXP_NODE_TEXT:
4635 set_fill_string(set, "", 0);
4636 break;
4637 case LYXP_NODE_ELEM:
Michal Vasko9f96a052020-03-10 09:41:45 +01004638 case LYXP_NODE_META:
Michal Vasko03ff5a72019-09-11 13:49:33 +02004639 if (item->type == LYXP_NODE_ELEM) {
4640 mod = item->node->schema->module;
Michal Vasko9f96a052020-03-10 09:41:45 +01004641 } else { /* LYXP_NODE_META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02004642 /* annotations */
Michal Vasko9f96a052020-03-10 09:41:45 +01004643 mod = ((struct lyd_meta *)item->node)->annotation->module;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004644 }
4645
4646 set_fill_string(set, mod->ns, strlen(mod->ns));
4647 break;
4648 }
4649
4650 return LY_SUCCESS;
4651}
4652
4653/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02004654 * @brief Execute the XPath normalize-space(string?) function. Returns LYXP_SET_STRING
4655 * with normalized value (no leading, trailing, double white spaces) of the node
4656 * from the argument or the context.
4657 *
4658 * @param[in] args Array of arguments.
4659 * @param[in] arg_count Count of elements in @p args.
4660 * @param[in,out] set Context and result set at the same time.
4661 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004662 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004663 */
4664static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004665xpath_normalize_space(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004666{
4667 uint16_t i, new_used;
4668 char *new;
Radek Krejci857189e2020-09-01 13:26:36 +02004669 ly_bool have_spaces = 0, space_before = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004670 struct lysc_node_leaf *sleaf;
4671 LY_ERR rc = LY_SUCCESS;
4672
4673 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004674 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) &&
4675 (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004676 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004677 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
4678 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004679 } else if (!warn_is_string_type(sleaf->type)) {
4680 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004681 }
4682 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004683 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004684 return rc;
4685 }
4686
4687 if (arg_count) {
4688 set_fill_set(set, args[0]);
4689 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004690 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004691 LY_CHECK_RET(rc);
4692
4693 /* is there any normalization necessary? */
4694 for (i = 0; set->val.str[i]; ++i) {
4695 if (is_xmlws(set->val.str[i])) {
4696 if ((i == 0) || space_before || (!set->val.str[i + 1])) {
4697 have_spaces = 1;
4698 break;
4699 }
4700 space_before = 1;
4701 } else {
4702 space_before = 0;
4703 }
4704 }
4705
4706 /* yep, there is */
4707 if (have_spaces) {
4708 /* it's enough, at least one character will go, makes space for ending '\0' */
4709 new = malloc(strlen(set->val.str) * sizeof(char));
4710 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4711 new_used = 0;
4712
4713 space_before = 0;
4714 for (i = 0; set->val.str[i]; ++i) {
4715 if (is_xmlws(set->val.str[i])) {
4716 if ((i == 0) || space_before) {
4717 space_before = 1;
4718 continue;
4719 } else {
4720 space_before = 1;
4721 }
4722 } else {
4723 space_before = 0;
4724 }
4725
4726 new[new_used] = (space_before ? ' ' : set->val.str[i]);
4727 ++new_used;
4728 }
4729
4730 /* at worst there is one trailing space now */
4731 if (new_used && is_xmlws(new[new_used - 1])) {
4732 --new_used;
4733 }
4734
4735 new = ly_realloc(new, (new_used + 1) * sizeof(char));
4736 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4737 new[new_used] = '\0';
4738
4739 free(set->val.str);
4740 set->val.str = new;
4741 }
4742
4743 return LY_SUCCESS;
4744}
4745
4746/**
4747 * @brief Execute the XPath not(boolean) function. Returns LYXP_SET_BOOLEAN
4748 * with the argument converted to boolean and logically inverted.
4749 *
4750 * @param[in] args Array of arguments.
4751 * @param[in] arg_count Count of elements in @p args.
4752 * @param[in,out] set Context and result set at the same time.
4753 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004754 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004755 */
4756static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004757xpath_not(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004758{
4759 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004760 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004761 return LY_SUCCESS;
4762 }
4763
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004764 lyxp_set_cast(args[0], LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +02004765 if (args[0]->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004766 set_fill_boolean(set, 0);
4767 } else {
4768 set_fill_boolean(set, 1);
4769 }
4770
4771 return LY_SUCCESS;
4772}
4773
4774/**
4775 * @brief Execute the XPath number(object?) function. Returns LYXP_SET_NUMBER
4776 * with the number representation of either the argument or the context.
4777 *
4778 * @param[in] args Array of arguments.
4779 * @param[in] arg_count Count of elements in @p args.
4780 * @param[in,out] set Context and result set at the same time.
4781 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004782 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004783 */
4784static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004785xpath_number(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004786{
4787 LY_ERR rc;
4788
4789 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004790 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004791 return LY_SUCCESS;
4792 }
4793
4794 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004795 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004796 LY_CHECK_RET(rc);
4797 set_fill_set(set, args[0]);
4798 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004799 rc = lyxp_set_cast(set, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004800 LY_CHECK_RET(rc);
4801 }
4802
4803 return LY_SUCCESS;
4804}
4805
4806/**
4807 * @brief Execute the XPath position() function. Returns LYXP_SET_NUMBER
4808 * with the context position.
4809 *
4810 * @param[in] args Array of arguments.
4811 * @param[in] arg_count Count of elements in @p args.
4812 * @param[in,out] set Context and result set at the same time.
4813 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004814 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004815 */
4816static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004817xpath_position(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004818{
4819 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004820 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004821 return LY_SUCCESS;
4822 }
4823
Michal Vasko03ff5a72019-09-11 13:49:33 +02004824 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004825 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "position()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004826 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004827 } else if (!set->used) {
4828 set_fill_number(set, 0);
4829 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004830 }
4831
4832 set_fill_number(set, set->ctx_pos);
4833
4834 /* UNUSED in 'Release' build type */
4835 (void)options;
4836 return LY_SUCCESS;
4837}
4838
4839/**
4840 * @brief Execute the YANG 1.1 re-match(string, string) function. Returns LYXP_SET_BOOLEAN
4841 * depending on whether the second argument regex matches the first argument string. For details refer to
4842 * YANG 1.1 RFC section 10.2.1.
4843 *
4844 * @param[in] args Array of arguments.
4845 * @param[in] arg_count Count of elements in @p args.
4846 * @param[in,out] set Context and result set at the same time.
4847 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004848 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004849 */
4850static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004851xpath_re_match(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004852{
4853 struct lysc_pattern **patterns = NULL, **pattern;
4854 struct lysc_node_leaf *sleaf;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004855 LY_ERR rc = LY_SUCCESS;
4856 struct ly_err_item *err;
4857
4858 if (options & LYXP_SCNODE_ALL) {
4859 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4860 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4861 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 +02004862 } else if (!warn_is_string_type(sleaf->type)) {
4863 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004864 }
4865 }
4866
4867 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4868 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4869 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 +02004870 } else if (!warn_is_string_type(sleaf->type)) {
4871 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004872 }
4873 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004874 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004875 return rc;
4876 }
4877
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004878 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004879 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004880 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004881 LY_CHECK_RET(rc);
4882
4883 LY_ARRAY_NEW_RET(set->ctx, patterns, pattern, LY_EMEM);
Radek Iša45802b52021-02-09 09:21:58 +01004884 *pattern = calloc(1, sizeof **pattern);
Radek Krejciddace2c2021-01-08 11:30:56 +01004885 LOG_LOCSET(NULL, set->cur_node, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01004886 rc = lys_compile_type_pattern_check(set->ctx, args[1]->val.str, &(*pattern)->code);
Michal Vasko4a7d4d62021-12-13 17:05:06 +01004887 if (set->cur_node) {
4888 LOG_LOCBACK(0, 1, 0, 0);
4889 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004890 if (rc != LY_SUCCESS) {
4891 LY_ARRAY_FREE(patterns);
4892 return rc;
4893 }
4894
Radek Krejci0b013302021-03-29 15:22:32 +02004895 rc = lyplg_type_validate_patterns(patterns, args[0]->val.str, strlen(args[0]->val.str), &err);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004896 pcre2_code_free((*pattern)->code);
4897 free(*pattern);
4898 LY_ARRAY_FREE(patterns);
4899 if (rc && (rc != LY_EVALID)) {
Michal Vasko177d0ed2020-11-23 16:43:03 +01004900 ly_err_print(set->ctx, err);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004901 ly_err_free(err);
4902 return rc;
4903 }
4904
4905 if (rc == LY_EVALID) {
4906 ly_err_free(err);
4907 set_fill_boolean(set, 0);
4908 } else {
4909 set_fill_boolean(set, 1);
4910 }
4911
4912 return LY_SUCCESS;
4913}
4914
4915/**
4916 * @brief Execute the XPath round(number) function. Returns LYXP_SET_NUMBER
4917 * with the rounded first argument. For details refer to
4918 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-round.
4919 *
4920 * @param[in] args Array of arguments.
4921 * @param[in] arg_count Count of elements in @p args.
4922 * @param[in,out] set Context and result set at the same time.
4923 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004924 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004925 */
4926static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004927xpath_round(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004928{
4929 struct lysc_node_leaf *sleaf;
4930 LY_ERR rc = LY_SUCCESS;
4931
4932 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02004933 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004934 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02004935 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4936 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4937 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
4938 sleaf->name);
4939 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
4940 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
4941 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004942 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004943 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004944 return rc;
4945 }
4946
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004947 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004948 LY_CHECK_RET(rc);
4949
4950 /* cover only the cases where floor can't be used */
4951 if ((args[0]->val.num == -0.0f) || ((args[0]->val.num < 0) && (args[0]->val.num >= -0.5))) {
4952 set_fill_number(set, -0.0f);
4953 } else {
4954 args[0]->val.num += 0.5;
4955 rc = xpath_floor(args, 1, args[0], options);
4956 LY_CHECK_RET(rc);
4957 set_fill_number(set, args[0]->val.num);
4958 }
4959
4960 return LY_SUCCESS;
4961}
4962
4963/**
4964 * @brief Execute the XPath starts-with(string, string) function.
4965 * Returns LYXP_SET_BOOLEAN whether the second argument is
4966 * the prefix of the first or not.
4967 *
4968 * @param[in] args Array of arguments.
4969 * @param[in] arg_count Count of elements in @p args.
4970 * @param[in,out] set Context and result set at the same time.
4971 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004972 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004973 */
4974static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004975xpath_starts_with(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004976{
4977 struct lysc_node_leaf *sleaf;
4978 LY_ERR rc = LY_SUCCESS;
4979
4980 if (options & LYXP_SCNODE_ALL) {
4981 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4982 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4983 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 +02004984 } else if (!warn_is_string_type(sleaf->type)) {
4985 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004986 }
4987 }
4988
4989 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4990 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4991 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 +02004992 } else if (!warn_is_string_type(sleaf->type)) {
4993 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004994 }
4995 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004996 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004997 return rc;
4998 }
4999
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005000 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005001 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005002 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005003 LY_CHECK_RET(rc);
5004
5005 if (strncmp(args[0]->val.str, args[1]->val.str, strlen(args[1]->val.str))) {
5006 set_fill_boolean(set, 0);
5007 } else {
5008 set_fill_boolean(set, 1);
5009 }
5010
5011 return LY_SUCCESS;
5012}
5013
5014/**
5015 * @brief Execute the XPath string(object?) function. Returns LYXP_SET_STRING
5016 * with the string representation of either the argument or the context.
5017 *
5018 * @param[in] args Array of arguments.
5019 * @param[in] arg_count Count of elements in @p args.
5020 * @param[in,out] set Context and result set at the same time.
5021 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005022 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005023 */
5024static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005025xpath_string(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005026{
5027 LY_ERR rc;
5028
5029 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02005030 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005031 return LY_SUCCESS;
5032 }
5033
5034 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005035 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005036 LY_CHECK_RET(rc);
5037 set_fill_set(set, args[0]);
5038 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005039 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005040 LY_CHECK_RET(rc);
5041 }
5042
5043 return LY_SUCCESS;
5044}
5045
5046/**
5047 * @brief Execute the XPath string-length(string?) function. Returns LYXP_SET_NUMBER
5048 * with the length of the string in either the argument or the context.
5049 *
5050 * @param[in] args Array of arguments.
5051 * @param[in] arg_count Count of elements in @p args.
5052 * @param[in,out] set Context and result set at the same time.
5053 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005054 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005055 */
5056static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005057xpath_string_length(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005058{
5059 struct lysc_node_leaf *sleaf;
5060 LY_ERR rc = LY_SUCCESS;
5061
5062 if (options & LYXP_SCNODE_ALL) {
5063 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5064 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5065 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 +02005066 } else if (!warn_is_string_type(sleaf->type)) {
5067 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005068 }
5069 }
5070 if (!arg_count && (set->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set))) {
5071 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5072 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 +02005073 } else if (!warn_is_string_type(sleaf->type)) {
5074 LOGWRN(set->ctx, "Argument #0 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005075 }
5076 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005077 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005078 return rc;
5079 }
5080
5081 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005082 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005083 LY_CHECK_RET(rc);
5084 set_fill_number(set, strlen(args[0]->val.str));
5085 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005086 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005087 LY_CHECK_RET(rc);
5088 set_fill_number(set, strlen(set->val.str));
5089 }
5090
5091 return LY_SUCCESS;
5092}
5093
5094/**
5095 * @brief Execute the XPath substring(string, number, number?) function.
5096 * Returns LYXP_SET_STRING substring of the first argument starting
5097 * on the second argument index ending on the third argument index,
5098 * indexed from 1. For exact definition refer to
5099 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-substring.
5100 *
5101 * @param[in] args Array of arguments.
5102 * @param[in] arg_count Count of elements in @p args.
5103 * @param[in,out] set Context and result set at the same time.
5104 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005105 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005106 */
5107static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005108xpath_substring(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005109{
Michal Vasko6db996e2022-07-28 10:28:04 +02005110 int64_t start;
5111 int32_t len;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005112 uint16_t str_start, str_len, pos;
5113 struct lysc_node_leaf *sleaf;
5114 LY_ERR rc = LY_SUCCESS;
5115
5116 if (options & LYXP_SCNODE_ALL) {
5117 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5118 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5119 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 +02005120 } else if (!warn_is_string_type(sleaf->type)) {
5121 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005122 }
5123 }
5124
5125 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5126 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5127 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 +02005128 } else if (!warn_is_numeric_type(sleaf->type)) {
5129 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005130 }
5131 }
5132
Michal Vasko69730152020-10-09 16:30:07 +02005133 if ((arg_count == 3) && (args[2]->type == LYXP_SET_SCNODE_SET) &&
5134 (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005135 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5136 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 +02005137 } else if (!warn_is_numeric_type(sleaf->type)) {
5138 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005139 }
5140 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005141 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005142 return rc;
5143 }
5144
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005145 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005146 LY_CHECK_RET(rc);
5147
5148 /* start */
5149 if (xpath_round(&args[1], 1, args[1], options)) {
5150 return -1;
5151 }
5152 if (isfinite(args[1]->val.num)) {
5153 start = args[1]->val.num - 1;
5154 } else if (isinf(args[1]->val.num) && signbit(args[1]->val.num)) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02005155 start = INT32_MIN;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005156 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02005157 start = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005158 }
5159
5160 /* len */
5161 if (arg_count == 3) {
5162 rc = xpath_round(&args[2], 1, args[2], options);
5163 LY_CHECK_RET(rc);
Radek Krejci1deb5be2020-08-26 16:43:36 +02005164 if (isnan(args[2]->val.num) || signbit(args[2]->val.num)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005165 len = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005166 } else if (isfinite(args[2]->val.num)) {
5167 len = args[2]->val.num;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005168 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02005169 len = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005170 }
5171 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02005172 len = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005173 }
5174
5175 /* find matching character positions */
5176 str_start = 0;
5177 str_len = 0;
5178 for (pos = 0; args[0]->val.str[pos]; ++pos) {
5179 if (pos < start) {
5180 ++str_start;
5181 } else if (pos < start + len) {
5182 ++str_len;
5183 } else {
5184 break;
5185 }
5186 }
5187
5188 set_fill_string(set, args[0]->val.str + str_start, str_len);
5189 return LY_SUCCESS;
5190}
5191
5192/**
5193 * @brief Execute the XPath substring-after(string, string) function.
5194 * Returns LYXP_SET_STRING with the string succeeding the occurance
5195 * of the second argument in the first or an empty string.
5196 *
5197 * @param[in] args Array of arguments.
5198 * @param[in] arg_count Count of elements in @p args.
5199 * @param[in,out] set Context and result set at the same time.
5200 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005201 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005202 */
5203static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005204xpath_substring_after(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005205{
5206 char *ptr;
5207 struct lysc_node_leaf *sleaf;
5208 LY_ERR rc = LY_SUCCESS;
5209
5210 if (options & LYXP_SCNODE_ALL) {
5211 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5212 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5213 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 +02005214 } else if (!warn_is_string_type(sleaf->type)) {
5215 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005216 }
5217 }
5218
5219 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5220 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5221 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 +02005222 } else if (!warn_is_string_type(sleaf->type)) {
5223 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005224 }
5225 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005226 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005227 return rc;
5228 }
5229
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005230 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005231 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005232 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005233 LY_CHECK_RET(rc);
5234
5235 ptr = strstr(args[0]->val.str, args[1]->val.str);
5236 if (ptr) {
5237 set_fill_string(set, ptr + strlen(args[1]->val.str), strlen(ptr + strlen(args[1]->val.str)));
5238 } else {
5239 set_fill_string(set, "", 0);
5240 }
5241
5242 return LY_SUCCESS;
5243}
5244
5245/**
5246 * @brief Execute the XPath substring-before(string, string) function.
5247 * Returns LYXP_SET_STRING with the string preceding the occurance
5248 * of the second argument in the first or an empty string.
5249 *
5250 * @param[in] args Array of arguments.
5251 * @param[in] arg_count Count of elements in @p args.
5252 * @param[in,out] set Context and result set at the same time.
5253 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005254 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005255 */
5256static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005257xpath_substring_before(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005258{
5259 char *ptr;
5260 struct lysc_node_leaf *sleaf;
5261 LY_ERR rc = LY_SUCCESS;
5262
5263 if (options & LYXP_SCNODE_ALL) {
5264 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5265 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5266 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 +02005267 } else if (!warn_is_string_type(sleaf->type)) {
5268 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005269 }
5270 }
5271
5272 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5273 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5274 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 +02005275 } else if (!warn_is_string_type(sleaf->type)) {
5276 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005277 }
5278 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005279 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005280 return rc;
5281 }
5282
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005283 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005284 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005285 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005286 LY_CHECK_RET(rc);
5287
5288 ptr = strstr(args[0]->val.str, args[1]->val.str);
5289 if (ptr) {
5290 set_fill_string(set, args[0]->val.str, ptr - args[0]->val.str);
5291 } else {
5292 set_fill_string(set, "", 0);
5293 }
5294
5295 return LY_SUCCESS;
5296}
5297
5298/**
5299 * @brief Execute the XPath sum(node-set) function. Returns LYXP_SET_NUMBER
5300 * with the sum of all the nodes in the context.
5301 *
5302 * @param[in] args Array of arguments.
5303 * @param[in] arg_count Count of elements in @p args.
5304 * @param[in,out] set Context and result set at the same time.
5305 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005306 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005307 */
5308static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005309xpath_sum(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005310{
5311 long double num;
5312 char *str;
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01005313 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005314 struct lyxp_set set_item;
5315 struct lysc_node_leaf *sleaf;
5316 LY_ERR rc = LY_SUCCESS;
5317
5318 if (options & LYXP_SCNODE_ALL) {
5319 if (args[0]->type == LYXP_SET_SCNODE_SET) {
5320 for (i = 0; i < args[0]->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005321 if (args[0]->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005322 sleaf = (struct lysc_node_leaf *)args[0]->val.scnodes[i].scnode;
5323 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5324 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__,
Michal Vasko69730152020-10-09 16:30:07 +02005325 lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005326 } else if (!warn_is_numeric_type(sleaf->type)) {
5327 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005328 }
5329 }
5330 }
5331 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005332 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005333 return rc;
5334 }
5335
5336 set_fill_number(set, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005337
5338 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005339 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "sum(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02005340 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02005341 } else if (!args[0]->used) {
5342 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005343 }
5344
Michal Vasko5c4e5892019-11-14 12:31:38 +01005345 set_init(&set_item, set);
5346
Michal Vasko03ff5a72019-09-11 13:49:33 +02005347 set_item.type = LYXP_SET_NODE_SET;
Michal Vasko41decbf2021-11-02 11:50:21 +01005348 set_item.val.nodes = calloc(1, sizeof *set_item.val.nodes);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005349 LY_CHECK_ERR_RET(!set_item.val.nodes, LOGMEM(set->ctx), LY_EMEM);
5350
5351 set_item.used = 1;
5352 set_item.size = 1;
5353
5354 for (i = 0; i < args[0]->used; ++i) {
5355 set_item.val.nodes[0] = args[0]->val.nodes[i];
5356
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005357 rc = cast_node_set_to_string(&set_item, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005358 LY_CHECK_RET(rc);
5359 num = cast_string_to_number(str);
5360 free(str);
5361 set->val.num += num;
5362 }
5363
5364 free(set_item.val.nodes);
5365
5366 return LY_SUCCESS;
5367}
5368
5369/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02005370 * @brief Execute the XPath translate(string, string, string) function.
5371 * Returns LYXP_SET_STRING with the first argument with the characters
5372 * from the second argument replaced by those on the corresponding
5373 * positions in the third argument.
5374 *
5375 * @param[in] args Array of arguments.
5376 * @param[in] arg_count Count of elements in @p args.
5377 * @param[in,out] set Context and result set at the same time.
5378 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005379 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005380 */
5381static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005382xpath_translate(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005383{
5384 uint16_t i, j, new_used;
5385 char *new;
Radek Krejci857189e2020-09-01 13:26:36 +02005386 ly_bool have_removed;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005387 struct lysc_node_leaf *sleaf;
5388 LY_ERR rc = LY_SUCCESS;
5389
5390 if (options & LYXP_SCNODE_ALL) {
5391 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5392 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5393 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 +02005394 } else if (!warn_is_string_type(sleaf->type)) {
5395 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005396 }
5397 }
5398
5399 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5400 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5401 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 +02005402 } else if (!warn_is_string_type(sleaf->type)) {
5403 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005404 }
5405 }
5406
5407 if ((args[2]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
5408 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5409 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 +02005410 } else if (!warn_is_string_type(sleaf->type)) {
5411 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005412 }
5413 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005414 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005415 return rc;
5416 }
5417
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005418 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005419 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005420 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005421 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005422 rc = lyxp_set_cast(args[2], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005423 LY_CHECK_RET(rc);
5424
5425 new = malloc((strlen(args[0]->val.str) + 1) * sizeof(char));
5426 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5427 new_used = 0;
5428
5429 have_removed = 0;
5430 for (i = 0; args[0]->val.str[i]; ++i) {
Radek Krejci857189e2020-09-01 13:26:36 +02005431 ly_bool found = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005432
5433 for (j = 0; args[1]->val.str[j]; ++j) {
5434 if (args[0]->val.str[i] == args[1]->val.str[j]) {
5435 /* removing this char */
5436 if (j >= strlen(args[2]->val.str)) {
5437 have_removed = 1;
5438 found = 1;
5439 break;
5440 }
5441 /* replacing this char */
5442 new[new_used] = args[2]->val.str[j];
5443 ++new_used;
5444 found = 1;
5445 break;
5446 }
5447 }
5448
5449 /* copying this char */
5450 if (!found) {
5451 new[new_used] = args[0]->val.str[i];
5452 ++new_used;
5453 }
5454 }
5455
5456 if (have_removed) {
5457 new = ly_realloc(new, (new_used + 1) * sizeof(char));
5458 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5459 }
5460 new[new_used] = '\0';
5461
Michal Vaskod3678892020-05-21 10:06:58 +02005462 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005463 set->type = LYXP_SET_STRING;
5464 set->val.str = new;
5465
5466 return LY_SUCCESS;
5467}
5468
5469/**
5470 * @brief Execute the XPath true() function. Returns LYXP_SET_BOOLEAN
5471 * with true value.
5472 *
5473 * @param[in] args Array of arguments.
5474 * @param[in] arg_count Count of elements in @p args.
5475 * @param[in,out] set Context and result set at the same time.
5476 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005477 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005478 */
5479static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005480xpath_true(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005481{
5482 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02005483 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005484 return LY_SUCCESS;
5485 }
5486
5487 set_fill_boolean(set, 1);
5488 return LY_SUCCESS;
5489}
5490
Michal Vasko03ff5a72019-09-11 13:49:33 +02005491/**
Michal Vasko49fec8e2022-05-24 10:28:33 +02005492 * @brief Execute the XPath node() processing instruction (node type). Returns LYXP_SET_NODE_SET
5493 * with only nodes from the context.
5494 *
5495 * @param[in,out] set Context and result set at the same time.
5496 * @param[in] axis Axis to search on.
5497 * @param[in] options XPath options.
5498 * @return LY_ERR
5499 */
5500static LY_ERR
5501xpath_pi_node(struct lyxp_set *set, enum lyxp_axis axis, uint32_t options)
5502{
5503 if (options & LYXP_SCNODE_ALL) {
5504 return moveto_scnode(set, NULL, NULL, axis, options);
5505 }
5506
5507 if (set->type != LYXP_SET_NODE_SET) {
5508 lyxp_set_free_content(set);
5509 return LY_SUCCESS;
5510 }
5511
5512 /* just like moving to a node with no restrictions */
5513 return moveto_node(set, NULL, NULL, axis, options);
5514}
5515
5516/**
5517 * @brief Execute the XPath text() processing instruction (node type). Returns LYXP_SET_NODE_SET
5518 * with the text content of the nodes in the context.
5519 *
5520 * @param[in,out] set Context and result set at the same time.
5521 * @param[in] axis Axis to search on.
5522 * @param[in] options XPath options.
5523 * @return LY_ERR
5524 */
5525static LY_ERR
5526xpath_pi_text(struct lyxp_set *set, enum lyxp_axis axis, uint32_t options)
5527{
5528 uint32_t i;
5529
5530 if (options & LYXP_SCNODE_ALL) {
5531 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
5532 return LY_SUCCESS;
5533 }
5534
5535 if (set->type != LYXP_SET_NODE_SET) {
5536 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "text()");
5537 return LY_EVALID;
5538 }
5539
5540 if (axis != LYXP_AXIS_CHILD) {
5541 /* even following and preceding axescan return text nodes, but whatever */
5542 lyxp_set_free_content(set);
5543 return LY_SUCCESS;
5544 }
5545
5546 for (i = 0; i < set->used; ++i) {
5547 switch (set->val.nodes[i].type) {
5548 case LYXP_NODE_NONE:
5549 LOGINT_RET(set->ctx);
5550 case LYXP_NODE_ELEM:
5551 if (set->val.nodes[i].node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
5552 set->val.nodes[i].type = LYXP_NODE_TEXT;
5553 break;
5554 }
5555 /* fall through */
5556 case LYXP_NODE_ROOT:
5557 case LYXP_NODE_ROOT_CONFIG:
5558 case LYXP_NODE_TEXT:
5559 case LYXP_NODE_META:
5560 set_remove_node_none(set, i);
5561 break;
5562 }
5563 }
5564 set_remove_nodes_none(set);
5565
5566 return LY_SUCCESS;
5567}
5568
5569/**
Michal Vasko6346ece2019-09-24 13:12:53 +02005570 * @brief Skip prefix and return corresponding model if there is a prefix. Logs directly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005571 *
Michal Vasko2104e9f2020-03-06 08:23:25 +01005572 * XPath @p set is expected to be a (sc)node set!
5573 *
Michal Vasko6346ece2019-09-24 13:12:53 +02005574 * @param[in,out] qname Qualified node name. If includes prefix, it is skipped.
5575 * @param[in,out] qname_len Length of @p qname, is updated accordingly.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005576 * @param[in] set Set with general XPath context.
5577 * @param[in] ctx_scnode Context node to inherit module for unprefixed node for ::LY_PREF_JSON.
Michal Vasko6346ece2019-09-24 13:12:53 +02005578 * @param[out] moveto_mod Expected module of a matching node.
5579 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005580 */
Michal Vasko6346ece2019-09-24 13:12:53 +02005581static LY_ERR
Michal Vaskofe1af042022-07-29 14:58:59 +02005582moveto_resolve_model(const char **qname, uint32_t *qname_len, const struct lyxp_set *set,
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005583 const struct lysc_node *ctx_scnode, const struct lys_module **moveto_mod)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005584{
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02005585 const struct lys_module *mod = NULL;
Michal Vasko6346ece2019-09-24 13:12:53 +02005586 const char *ptr;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005587 size_t pref_len;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005588
Michal Vasko2104e9f2020-03-06 08:23:25 +01005589 assert((set->type == LYXP_SET_NODE_SET) || (set->type == LYXP_SET_SCNODE_SET));
5590
Michal Vasko6346ece2019-09-24 13:12:53 +02005591 if ((ptr = ly_strnchr(*qname, ':', *qname_len))) {
5592 /* specific module */
5593 pref_len = ptr - *qname;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005594 mod = ly_resolve_prefix(set->ctx, *qname, pref_len, set->format, set->prefix_data);
Michal Vasko6346ece2019-09-24 13:12:53 +02005595
Michal Vasko004d3152020-06-11 19:59:22 +02005596 /* check for errors and non-implemented modules, as they are not valid */
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005597 if (!mod || !mod->implemented) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005598 LOGVAL(set->ctx, LY_VCODE_XP_INMOD, pref_len, *qname);
Michal Vasko6346ece2019-09-24 13:12:53 +02005599 return LY_EVALID;
5600 }
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005601
Michal Vasko6346ece2019-09-24 13:12:53 +02005602 *qname += pref_len + 1;
5603 *qname_len -= pref_len + 1;
5604 } else if (((*qname)[0] == '*') && (*qname_len == 1)) {
5605 /* all modules - special case */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005606 mod = NULL;
Michal Vasko6346ece2019-09-24 13:12:53 +02005607 } else {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005608 switch (set->format) {
Radek Krejci8df109d2021-04-23 12:19:08 +02005609 case LY_VALUE_SCHEMA:
5610 case LY_VALUE_SCHEMA_RESOLVED:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005611 /* current module */
5612 mod = set->cur_mod;
5613 break;
Radek Krejci224d4b42021-04-23 13:54:59 +02005614 case LY_VALUE_CANON:
Radek Krejci8df109d2021-04-23 12:19:08 +02005615 case LY_VALUE_JSON:
Radek Krejcif9943642021-04-26 10:18:21 +02005616 case LY_VALUE_LYB:
Michal Vaskoddd76592022-01-17 13:34:48 +01005617 case LY_VALUE_STR_NS:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005618 /* inherit parent (context node) module */
5619 if (ctx_scnode) {
5620 mod = ctx_scnode->module;
5621 } else {
5622 mod = NULL;
5623 }
5624 break;
Radek Krejci8df109d2021-04-23 12:19:08 +02005625 case LY_VALUE_XML:
Michal Vasko52143d12021-04-14 15:36:39 +02005626 /* all nodes need to be prefixed */
5627 LOGVAL(set->ctx, LYVE_DATA, "Non-prefixed node \"%.*s\" in XML xpath found.", *qname_len, *qname);
5628 return LY_EVALID;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005629 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005630 }
5631
Michal Vasko6346ece2019-09-24 13:12:53 +02005632 *moveto_mod = mod;
5633 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005634}
5635
5636/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02005637 * @brief Move context @p set to the root. Handles absolute path.
5638 * Result is LYXP_SET_NODE_SET.
5639 *
5640 * @param[in,out] set Set to use.
5641 * @param[in] options Xpath options.
Michal Vaskob0099a92020-08-31 14:55:23 +02005642 * @return LY_ERR value.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005643 */
Michal Vaskob0099a92020-08-31 14:55:23 +02005644static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005645moveto_root(struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005646{
aPiecek8b0cc152021-05-31 16:40:31 +02005647 assert(!(options & LYXP_SKIP_EXPR));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005648
5649 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02005650 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko7333cb32022-07-29 16:30:29 +02005651 LY_CHECK_RET(set_scnode_insert_node(set, NULL, set->root_type, LYXP_AXIS_SELF, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005652 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005653 set->type = LYXP_SET_NODE_SET;
5654 set->used = 0;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005655 set_insert_node(set, NULL, 0, set->root_type, 0);
Michal Vasko306e2832022-07-25 09:15:17 +02005656 set->non_child_axis = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005657 }
Michal Vaskob0099a92020-08-31 14:55:23 +02005658
5659 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005660}
5661
5662/**
5663 * @brief Check @p node as a part of NameTest processing.
5664 *
5665 * @param[in] node Node to check.
Michal Vasko49fec8e2022-05-24 10:28:33 +02005666 * @param[in] node_type Node type of @p node.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005667 * @param[in] set Set to read general context from.
Michal Vaskod3678892020-05-21 10:06:58 +02005668 * @param[in] node_name Node name in the dictionary to move to, NULL for any node.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005669 * @param[in] moveto_mod Expected module of the node, NULL for no prefix.
Michal Vaskocdad7122020-11-09 21:04:44 +01005670 * @param[in] options XPath options.
Michal Vasko6346ece2019-09-24 13:12:53 +02005671 * @return LY_ERR (LY_ENOT if node does not match, LY_EINCOMPLETE on unresolved when,
Michal Vaskocd2c88a2022-06-07 10:54:34 +02005672 * LY_EINVAL if neither node nor any children match)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005673 */
5674static LY_ERR
Michal Vasko49fec8e2022-05-24 10:28:33 +02005675moveto_node_check(const struct lyd_node *node, enum lyxp_node_type node_type, const struct lyxp_set *set,
5676 const char *node_name, const struct lys_module *moveto_mod, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005677{
Michal Vasko49fec8e2022-05-24 10:28:33 +02005678 if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT)) {
5679 assert(node_type == set->root_type);
5680
5681 if (node_name || moveto_mod) {
5682 /* root will not match a specific node */
5683 return LY_ENOT;
5684 }
5685 return LY_SUCCESS;
5686 } else if (node_type != LYXP_NODE_ELEM) {
5687 /* other types will not match */
5688 return LY_ENOT;
5689 }
5690
Michal Vaskodca9f122021-07-16 13:56:22 +02005691 if (!node->schema) {
5692 /* opaque node never matches */
5693 return LY_ENOT;
5694 }
5695
Michal Vasko03ff5a72019-09-11 13:49:33 +02005696 /* module check */
Michal Vasko19089f02022-06-07 11:02:11 +02005697 if (moveto_mod) {
5698 if (!(node->flags & LYD_EXT) && (node->schema->module != moveto_mod)) {
5699 return LY_ENOT;
5700 } else if ((node->flags & LYD_EXT) && strcmp(node->schema->module->name, moveto_mod->name)) {
5701 return LY_ENOT;
5702 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005703 }
5704
Michal Vasko5c4e5892019-11-14 12:31:38 +01005705 /* context check */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005706 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (node->schema->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005707 return LY_EINVAL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005708 } else if (set->context_op && (node->schema->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) &&
5709 (node->schema != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005710 return LY_EINVAL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005711 }
5712
5713 /* name check */
Michal Vasko19089f02022-06-07 11:02:11 +02005714 if (node_name) {
5715 if (!(node->flags & LYD_EXT) && (node->schema->name != node_name)) {
5716 return LY_ENOT;
5717 } else if ((node->flags & LYD_EXT) && strcmp(node->schema->name, node_name)) {
5718 return LY_ENOT;
5719 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005720 }
5721
Michal Vaskoa1424542019-11-14 16:08:52 +01005722 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01005723 if (!(options & LYXP_IGNORE_WHEN) && lysc_has_when(node->schema) && !(node->flags & LYD_WHEN_TRUE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005724 return LY_EINCOMPLETE;
Michal Vaskoa1424542019-11-14 16:08:52 +01005725 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005726
5727 /* match */
5728 return LY_SUCCESS;
5729}
5730
5731/**
Michal Vasko49fec8e2022-05-24 10:28:33 +02005732 * @brief Get the next node in a forward DFS.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005733 *
Michal Vasko49fec8e2022-05-24 10:28:33 +02005734 * @param[in] iter Last returned node.
5735 * @param[in] stop Node to stop the search on and not return.
5736 * @return Next node, NULL if there are no more.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005737 */
Michal Vasko49fec8e2022-05-24 10:28:33 +02005738static const struct lyd_node *
5739moveto_axis_node_next_dfs_forward(const struct lyd_node *iter, const struct lyd_node *stop)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005740{
Michal Vasko49fec8e2022-05-24 10:28:33 +02005741 const struct lyd_node *next = NULL;
5742
5743 /* 1) child */
5744 next = lyd_child(iter);
5745 if (!next) {
5746 if (iter == stop) {
5747 /* reached stop, no more descendants */
5748 return NULL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005749 }
Michal Vasko49fec8e2022-05-24 10:28:33 +02005750 /* 2) child next sibling */
5751 next = iter->next;
5752 }
5753 while (!next) {
5754 iter = lyd_parent(iter);
5755 if ((!stop && !iter) || (stop && (lyd_parent(iter) == lyd_parent(stop)))) {
5756 return NULL;
5757 }
5758 next = iter->next;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005759 }
5760
Michal Vasko49fec8e2022-05-24 10:28:33 +02005761 return next;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005762}
5763
5764/**
Michal Vasko49fec8e2022-05-24 10:28:33 +02005765 * @brief Get the next node in a backward DFS.
5766 *
5767 * @param[in] iter Last returned node.
5768 * @param[in] stop Node to stop the search on and not return.
5769 * @return Next node, NULL if there are no more.
5770 */
5771static const struct lyd_node *
5772moveto_axis_node_next_dfs_backward(const struct lyd_node *iter, const struct lyd_node *stop)
5773{
5774 const struct lyd_node *next = NULL;
5775
5776 /* 1) previous sibling innermost last child */
5777 next = iter->prev->next ? iter->prev : NULL;
5778 while (next && lyd_child(next)) {
5779 next = lyd_child(next);
5780 next = next->prev;
5781 }
5782
5783 if (!next) {
5784 /* 2) parent */
5785 iter = lyd_parent(iter);
5786 if ((!stop && !iter) || (stop && (lyd_parent(iter) == lyd_parent(stop)))) {
5787 return NULL;
5788 }
5789 next = iter;
5790 }
5791
5792 return next;
5793}
5794
5795/**
5796 * @brief Get the first node on an axis for a context node.
5797 *
5798 * @param[in,out] iter NULL, updated to the next node.
5799 * @param[in,out] iter_type Node type 0 of @p iter, updated to the node type of the next node.
5800 * @param[in] node Context node.
5801 * @param[in] node_type Type of @p node.
5802 * @param[in] axis Axis to use.
5803 * @param[in] set XPath set with the general context.
5804 * @return LY_SUCCESS on success.
5805 * @return LY_ENOTFOUND if no next node found.
5806 */
5807static LY_ERR
5808moveto_axis_node_next_first(const struct lyd_node **iter, enum lyxp_node_type *iter_type, const struct lyd_node *node,
5809 enum lyxp_node_type node_type, enum lyxp_axis axis, struct lyxp_set *set)
5810{
5811 const struct lyd_node *next = NULL;
5812 enum lyxp_node_type next_type = 0;
5813
5814 assert(!*iter);
5815 assert(!*iter_type);
5816
5817 switch (axis) {
5818 case LYXP_AXIS_ANCESTOR_OR_SELF:
5819 case LYXP_AXIS_DESCENDANT_OR_SELF:
5820 case LYXP_AXIS_SELF:
5821 /* return the context node */
5822 next = node;
5823 next_type = node_type;
5824 break;
5825
5826 case LYXP_AXIS_ANCESTOR:
5827 case LYXP_AXIS_PARENT:
5828 if (node_type == LYXP_NODE_ELEM) {
5829 next = lyd_parent(node);
5830 next_type = next ? LYXP_NODE_ELEM : set->root_type;
5831 } else if (node_type == LYXP_NODE_TEXT) {
5832 next = node;
5833 next_type = LYXP_NODE_ELEM;
5834 } else if (node_type == LYXP_NODE_META) {
5835 next = ((struct lyd_meta *)node)->parent;
5836 next_type = LYXP_NODE_ELEM;
5837 } /* else root does not have a parent */
5838 break;
5839
5840 case LYXP_AXIS_CHILD:
5841 if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT)) {
5842 assert(!node);
5843
5844 /* search in all the trees */
5845 next = set->tree;
5846 next_type = next ? LYXP_NODE_ELEM : 0;
5847 } else {
5848 /* search in children */
5849 next = lyd_child(node);
5850 next_type = next ? LYXP_NODE_ELEM : 0;
5851 }
5852 break;
5853
5854 case LYXP_AXIS_DESCENDANT:
5855 if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT)) {
5856 /* top-level nodes */
5857 next = set->tree;
5858 next_type = LYXP_NODE_ELEM;
5859 } else if (node_type == LYXP_NODE_ELEM) {
5860 /* start from the context node */
5861 next = moveto_axis_node_next_dfs_forward(node, node);
5862 next_type = next ? LYXP_NODE_ELEM : 0;
5863 } /* else no children */
5864 break;
5865
5866 case LYXP_AXIS_FOLLOWING:
5867 case LYXP_AXIS_FOLLOWING_SIBLING:
5868 if (node_type == LYXP_NODE_ELEM) {
5869 /* first next sibling */
5870 next = node->next;
5871 next_type = next ? LYXP_NODE_ELEM : 0;
5872 } /* else no sibling */
5873 break;
5874
5875 case LYXP_AXIS_PRECEDING:
5876 if ((node_type == LYXP_NODE_ELEM) && node->prev->next) {
5877 /* skip ancestors */
5878 next = moveto_axis_node_next_dfs_backward(node, NULL);
5879 assert(next);
5880 next_type = LYXP_NODE_ELEM;
5881 } /* else no sibling */
5882 break;
5883
5884 case LYXP_AXIS_PRECEDING_SIBLING:
5885 if (node_type == LYXP_NODE_ELEM) {
5886 /* first previous sibling */
5887 next = node->prev->next ? node->prev : NULL;
5888 next_type = next ? LYXP_NODE_ELEM : 0;
5889 } /* else no sibling */
5890 break;
5891
5892 case LYXP_AXIS_ATTRIBUTE:
5893 /* handled specially */
5894 assert(0);
5895 LOGINT(set->ctx);
5896 break;
5897 }
5898
5899 *iter = next;
5900 *iter_type = next_type;
5901 return next_type ? LY_SUCCESS : LY_ENOTFOUND;
5902}
5903
5904/**
5905 * @brief Iterate over all nodes on an axis for a context node.
5906 *
5907 * @param[in,out] iter Last returned node, start with NULL, updated to the next node.
5908 * @param[in,out] iter_type Node type of @p iter, start with 0, updated to the node type of the next node.
5909 * @param[in] node Context node.
5910 * @param[in] node_type Type of @p node.
5911 * @param[in] axis Axis to use.
5912 * @param[in] set XPath set with the general context.
5913 * @return LY_SUCCESS on success.
5914 * @return LY_ENOTFOUND if no next node found.
5915 */
5916static LY_ERR
5917moveto_axis_node_next(const struct lyd_node **iter, enum lyxp_node_type *iter_type, const struct lyd_node *node,
5918 enum lyxp_node_type node_type, enum lyxp_axis axis, struct lyxp_set *set)
5919{
5920 const struct lyd_node *next = NULL;
5921 enum lyxp_node_type next_type = 0;
5922
5923 if (!*iter_type) {
5924 /* first returned node */
5925 return moveto_axis_node_next_first(iter, iter_type, node, node_type, axis, set);
5926 }
5927
5928 switch (axis) {
5929 case LYXP_AXIS_ANCESTOR_OR_SELF:
5930 if ((*iter == node) && (*iter_type == node_type)) {
5931 /* fake first ancestor, we returned self before */
5932 *iter = NULL;
5933 *iter_type = 0;
5934 return moveto_axis_node_next_first(iter, iter_type, node, node_type, LYXP_AXIS_ANCESTOR, set);
5935 } /* else continue ancestor */
5936
5937 /* fallthrough */
5938 case LYXP_AXIS_ANCESTOR:
5939 if (*iter_type == LYXP_NODE_ELEM) {
5940 /* iter parent */
5941 next = lyd_parent(*iter);
5942 next_type = next ? LYXP_NODE_ELEM : set->root_type;
5943 } /* else root, no ancestors */
5944 break;
5945
5946 case LYXP_AXIS_CHILD:
5947 assert(*iter_type == LYXP_NODE_ELEM);
5948
5949 /* next sibling (child) */
5950 next = (*iter)->next;
5951 next_type = next ? LYXP_NODE_ELEM : 0;
5952 break;
5953
5954 case LYXP_AXIS_DESCENDANT_OR_SELF:
5955 if ((*iter == node) && (*iter_type == node_type)) {
5956 /* fake first descendant, we returned self before */
5957 *iter = NULL;
5958 *iter_type = 0;
5959 return moveto_axis_node_next_first(iter, iter_type, node, node_type, LYXP_AXIS_DESCENDANT, set);
5960 } /* else continue descendant */
5961
5962 /* fallthrough */
5963 case LYXP_AXIS_DESCENDANT:
5964 assert(*iter_type == LYXP_NODE_ELEM);
5965 next = moveto_axis_node_next_dfs_forward(*iter, node);
5966 next_type = next ? LYXP_NODE_ELEM : 0;
5967 break;
5968
5969 case LYXP_AXIS_FOLLOWING:
5970 assert(*iter_type == LYXP_NODE_ELEM);
5971 next = moveto_axis_node_next_dfs_forward(*iter, NULL);
5972 next_type = next ? LYXP_NODE_ELEM : 0;
5973 break;
5974
5975 case LYXP_AXIS_FOLLOWING_SIBLING:
5976 assert(*iter_type == LYXP_NODE_ELEM);
5977
5978 /* next sibling */
5979 next = (*iter)->next;
5980 next_type = next ? LYXP_NODE_ELEM : 0;
5981 break;
5982
5983 case LYXP_AXIS_PARENT:
5984 case LYXP_AXIS_SELF:
5985 /* parent/self was returned before */
5986 break;
5987
5988 case LYXP_AXIS_PRECEDING:
5989 assert(*iter_type == LYXP_NODE_ELEM);
5990 next = moveto_axis_node_next_dfs_backward(*iter, NULL);
5991 next_type = next ? LYXP_NODE_ELEM : 0;
5992 break;
5993
5994 case LYXP_AXIS_PRECEDING_SIBLING:
5995 assert(*iter_type == LYXP_NODE_ELEM);
5996
5997 /* previous sibling */
5998 next = (*iter)->prev->next ? (*iter)->prev : NULL;
5999 next_type = next ? LYXP_NODE_ELEM : 0;
6000 break;
6001
6002 case LYXP_AXIS_ATTRIBUTE:
6003 /* handled specially */
6004 assert(0);
6005 LOGINT(set->ctx);
6006 break;
6007 }
6008
6009 *iter = next;
6010 *iter_type = next_type;
6011 return next_type ? LY_SUCCESS : LY_ENOTFOUND;
6012}
6013
6014/**
6015 * @brief Move context @p set to a node. Result is LYXP_SET_NODE_SET. Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006016 *
6017 * @param[in,out] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006018 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02006019 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko49fec8e2022-05-24 10:28:33 +02006020 * @param[in] axis Axis to search on.
Michal Vaskocdad7122020-11-09 21:04:44 +01006021 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006022 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6023 */
6024static LY_ERR
Michal Vasko49fec8e2022-05-24 10:28:33 +02006025moveto_node(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, enum lyxp_axis axis,
6026 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006027{
Michal Vasko230cf972021-12-02 12:31:00 +01006028 LY_ERR r, rc = LY_SUCCESS;
Michal Vasko49fec8e2022-05-24 10:28:33 +02006029 const struct lyd_node *iter;
6030 enum lyxp_node_type iter_type;
Michal Vasko230cf972021-12-02 12:31:00 +01006031 struct lyxp_set result;
Michal Vasko49fec8e2022-05-24 10:28:33 +02006032 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006033
aPiecek8b0cc152021-05-31 16:40:31 +02006034 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006035 return LY_SUCCESS;
6036 }
6037
6038 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006039 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006040 return LY_EVALID;
6041 }
6042
Michal Vasko230cf972021-12-02 12:31:00 +01006043 /* init result set */
6044 set_init(&result, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006045
Michal Vasko49fec8e2022-05-24 10:28:33 +02006046 for (i = 0; i < set->used; ++i) {
6047 /* iterate over all the nodes on the axis of the node */
6048 iter = NULL;
6049 iter_type = 0;
6050 while (!moveto_axis_node_next(&iter, &iter_type, set->val.nodes[i].node, set->val.nodes[i].type, axis, set)) {
6051 r = moveto_node_check(iter, iter_type, set, ncname, moveto_mod, options);
6052 if (r == LY_EINCOMPLETE) {
Michal Vasko230cf972021-12-02 12:31:00 +01006053 rc = r;
6054 goto cleanup;
Michal Vasko49fec8e2022-05-24 10:28:33 +02006055 } else if (r) {
6056 continue;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006057 }
Michal Vasko49fec8e2022-05-24 10:28:33 +02006058
6059 /* check for duplicates if they are possible */
6060 switch (axis) {
6061 case LYXP_AXIS_ANCESTOR:
6062 case LYXP_AXIS_ANCESTOR_OR_SELF:
6063 case LYXP_AXIS_DESCENDANT:
6064 case LYXP_AXIS_DESCENDANT_OR_SELF:
6065 case LYXP_AXIS_FOLLOWING:
6066 case LYXP_AXIS_FOLLOWING_SIBLING:
6067 case LYXP_AXIS_PARENT:
6068 case LYXP_AXIS_PRECEDING:
6069 case LYXP_AXIS_PRECEDING_SIBLING:
Michal Vasko306e2832022-07-25 09:15:17 +02006070 result.non_child_axis = 1;
Michal Vasko49fec8e2022-05-24 10:28:33 +02006071 if (set_dup_node_check(&result, iter, iter_type, -1)) {
6072 continue;
6073 }
6074 break;
6075 case LYXP_AXIS_CHILD:
6076 case LYXP_AXIS_SELF:
6077 break;
6078 case LYXP_AXIS_ATTRIBUTE:
6079 /* handled specially */
6080 assert(0);
6081 LOGINT(set->ctx);
6082 break;
6083 }
6084
6085 /* matching node */
6086 set_insert_node(&result, iter, 0, iter_type, result.used);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006087 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006088 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006089
Michal Vasko230cf972021-12-02 12:31:00 +01006090 /* move result to the set */
6091 lyxp_set_free_content(set);
6092 *set = result;
6093 result.type = LYXP_SET_NUMBER;
Michal Vasko49fec8e2022-05-24 10:28:33 +02006094
Michal Vasko306e2832022-07-25 09:15:17 +02006095 /* sort the final set if the document order could have been broken */
6096 if (set->non_child_axis) {
6097 set_sort(set);
6098 } else {
6099 assert(!set_sort(set));
6100 }
Michal Vasko230cf972021-12-02 12:31:00 +01006101
6102cleanup:
6103 lyxp_set_free_content(&result);
6104 return rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006105}
6106
6107/**
Michal Vasko49fec8e2022-05-24 10:28:33 +02006108 * @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 +02006109 *
6110 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02006111 * @param[in] scnode Matching node schema.
Michal Vasko004d3152020-06-11 19:59:22 +02006112 * @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 +01006113 * @param[in] options XPath options.
Michal Vaskod3678892020-05-21 10:06:58 +02006114 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6115 */
6116static LY_ERR
Michal Vasko49fec8e2022-05-24 10:28:33 +02006117moveto_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 +01006118 uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02006119{
Michal Vaskoddd76592022-01-17 13:34:48 +01006120 LY_ERR ret = LY_SUCCESS, r;
Michal Vaskod3678892020-05-21 10:06:58 +02006121 uint32_t i;
Michal Vaskod3678892020-05-21 10:06:58 +02006122 const struct lyd_node *siblings;
Michal Vasko230cf972021-12-02 12:31:00 +01006123 struct lyxp_set result;
Michal Vasko004d3152020-06-11 19:59:22 +02006124 struct lyd_node *sub, *inst = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02006125
Michal Vasko004d3152020-06-11 19:59:22 +02006126 assert(scnode && (!(scnode->nodetype & (LYS_LIST | LYS_LEAFLIST)) || predicates));
Michal Vaskod3678892020-05-21 10:06:58 +02006127
Michal Vasko50aaa072021-12-02 13:11:56 +01006128 /* init result set */
6129 set_init(&result, set);
6130
aPiecek8b0cc152021-05-31 16:40:31 +02006131 if (options & LYXP_SKIP_EXPR) {
Michal Vasko004d3152020-06-11 19:59:22 +02006132 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02006133 }
6134
6135 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006136 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko004d3152020-06-11 19:59:22 +02006137 ret = LY_EVALID;
6138 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02006139 }
6140
6141 /* context check for all the nodes since we have the schema node */
6142 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) {
6143 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02006144 goto cleanup;
Michal Vasko69730152020-10-09 16:30:07 +02006145 } else if (set->context_op && (scnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) &&
6146 (scnode != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02006147 lyxp_set_free_content(set);
6148 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +02006149 }
6150
6151 /* create specific data instance if needed */
6152 if (scnode->nodetype == LYS_LIST) {
6153 LY_CHECK_GOTO(ret = lyd_create_list(scnode, predicates, &inst), cleanup);
6154 } else if (scnode->nodetype == LYS_LEAFLIST) {
6155 LY_CHECK_GOTO(ret = lyd_create_term2(scnode, &predicates[0].value, &inst), cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02006156 }
6157
Michal Vasko230cf972021-12-02 12:31:00 +01006158 for (i = 0; i < set->used; ++i) {
Michal Vaskod3678892020-05-21 10:06:58 +02006159 siblings = NULL;
6160
6161 if ((set->val.nodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.nodes[i].type == LYXP_NODE_ROOT)) {
6162 assert(!set->val.nodes[i].node);
6163
6164 /* search in all the trees */
6165 siblings = set->tree;
6166 } else if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
6167 /* search in children */
Radek Krejcia1c1e542020-09-29 16:06:52 +02006168 siblings = lyd_child(set->val.nodes[i].node);
Michal Vaskod3678892020-05-21 10:06:58 +02006169 }
6170
6171 /* find the node using hashes */
Michal Vasko004d3152020-06-11 19:59:22 +02006172 if (inst) {
Michal Vaskoddd76592022-01-17 13:34:48 +01006173 r = lyd_find_sibling_first(siblings, inst, &sub);
Michal Vaskod3678892020-05-21 10:06:58 +02006174 } else {
Michal Vaskoddd76592022-01-17 13:34:48 +01006175 r = lyd_find_sibling_val(siblings, scnode, NULL, 0, &sub);
Michal Vaskod3678892020-05-21 10:06:58 +02006176 }
Michal Vaskoddd76592022-01-17 13:34:48 +01006177 LY_CHECK_ERR_GOTO(r && (r != LY_ENOTFOUND), ret = r, cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02006178
6179 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01006180 if (!(options & LYXP_IGNORE_WHEN) && sub && lysc_has_when(sub->schema) && !(sub->flags & LYD_WHEN_TRUE)) {
Michal Vasko004d3152020-06-11 19:59:22 +02006181 ret = LY_EINCOMPLETE;
6182 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02006183 }
6184
6185 if (sub) {
6186 /* pos filled later */
Michal Vasko230cf972021-12-02 12:31:00 +01006187 set_insert_node(&result, sub, 0, LYXP_NODE_ELEM, result.used);
Michal Vaskod3678892020-05-21 10:06:58 +02006188 }
6189 }
6190
Michal Vasko230cf972021-12-02 12:31:00 +01006191 /* move result to the set */
6192 lyxp_set_free_content(set);
6193 *set = result;
6194 result.type = LYXP_SET_NUMBER;
6195 assert(!set_sort(set));
6196
Michal Vasko004d3152020-06-11 19:59:22 +02006197cleanup:
Michal Vasko230cf972021-12-02 12:31:00 +01006198 lyxp_set_free_content(&result);
Michal Vasko004d3152020-06-11 19:59:22 +02006199 lyd_free_tree(inst);
6200 return ret;
Michal Vaskod3678892020-05-21 10:06:58 +02006201}
6202
6203/**
Michal Vasko49fec8e2022-05-24 10:28:33 +02006204 * @brief Check @p node as a part of schema NameTest processing.
6205 *
6206 * @param[in] node Schema node to check.
6207 * @param[in] ctx_scnode Context node.
6208 * @param[in] set Set to read general context from.
6209 * @param[in] node_name Node name in the dictionary to move to, NULL for any nodes.
6210 * @param[in] moveto_mod Expected module of the node, NULL for no prefix.
6211 * @return LY_ERR (LY_ENOT if node does not match, LY_EINVAL if neither node nor any children match)
6212 */
6213static LY_ERR
6214moveto_scnode_check(const struct lysc_node *node, const struct lysc_node *ctx_scnode, const struct lyxp_set *set,
6215 const char *node_name, const struct lys_module *moveto_mod)
6216{
6217 if (!moveto_mod && node_name) {
6218 switch (set->format) {
6219 case LY_VALUE_SCHEMA:
6220 case LY_VALUE_SCHEMA_RESOLVED:
6221 /* use current module */
6222 moveto_mod = set->cur_mod;
6223 break;
6224 case LY_VALUE_JSON:
6225 case LY_VALUE_LYB:
6226 case LY_VALUE_STR_NS:
6227 /* inherit module of the context node, if any */
6228 if (ctx_scnode) {
6229 moveto_mod = ctx_scnode->module;
6230 }
6231 break;
6232 case LY_VALUE_CANON:
6233 case LY_VALUE_XML:
6234 /* not defined */
6235 LOGINT(set->ctx);
6236 return LY_EINVAL;
6237 }
6238 }
6239
6240 if (!node) {
6241 /* root will not match a specific node */
6242 if (node_name || moveto_mod) {
6243 return LY_ENOT;
6244 }
6245 return LY_SUCCESS;
6246 }
6247
6248 /* module check */
6249 if (moveto_mod && (node->module != moveto_mod)) {
6250 return LY_ENOT;
6251 }
6252
6253 /* context check */
6254 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (node->flags & LYS_CONFIG_R)) {
6255 return LY_EINVAL;
6256 } else if (set->context_op && (node->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && (node != set->context_op)) {
6257 return LY_EINVAL;
6258 }
6259
6260 /* name check */
6261 if (node_name && (node->name != node_name)) {
6262 return LY_ENOT;
6263 }
6264
6265 /* match */
6266 return LY_SUCCESS;
6267}
6268
6269/**
6270 * @brief Get the next node in a forward schema node DFS.
6271 *
6272 * @param[in] iter Last returned node.
6273 * @param[in] stop Node to stop the search on and not return.
6274 * @param[in] getnext_opts Options for ::lys_getnext().
6275 * @return Next node, NULL if there are no more.
6276 */
6277static const struct lysc_node *
6278moveto_axis_scnode_next_dfs_forward(const struct lysc_node *iter, const struct lysc_node *stop, uint32_t getnext_opts)
6279{
6280 const struct lysc_node *next = NULL;
6281
6282 next = lysc_node_child(iter);
6283 if (!next) {
6284 /* no children, try siblings */
Michal Vasko34a22fe2022-06-15 07:58:55 +02006285 if ((iter == stop) || !lysc_data_parent(iter)) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02006286 /* we are done, no next element to process */
6287 return NULL;
6288 }
6289
6290 next = lys_getnext(iter, lysc_data_parent(iter), NULL, getnext_opts);
6291 }
6292 while (!next && iter) {
6293 /* parent is already processed, go to its sibling */
6294 iter = iter->parent;
Michal Vasko34a22fe2022-06-15 07:58:55 +02006295 if ((iter == stop) || !lysc_data_parent(iter)) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02006296 /* we are done, no next element to process */
6297 return NULL;
6298 }
6299 next = lys_getnext(iter, lysc_data_parent(iter), NULL, getnext_opts);
6300 }
6301
6302 return next;
6303}
6304
6305/**
6306 * @brief Consider schema node based on its in_ctx enum value.
6307 *
6308 * @param[in,out] in_ctx In_ctx enum of the schema node, may be updated.
6309 * @param[in] axis Axis to use.
6310 * @return LY_SUCCESS on success.
6311 * @return LY_ENOT if the node should not be returned.
6312 */
6313static LY_ERR
6314moveto_axis_scnode_next_in_ctx(int32_t *in_ctx, enum lyxp_axis axis)
6315{
6316 switch (axis) {
6317 case LYXP_AXIS_SELF:
6318 if ((*in_ctx == LYXP_SET_SCNODE_START) || (*in_ctx == LYXP_SET_SCNODE_ATOM_CTX)) {
6319 /* additionally put the start node into context */
6320 *in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
6321 return LY_SUCCESS;
6322 }
6323 break;
6324 case LYXP_AXIS_PARENT:
6325 case LYXP_AXIS_ANCESTOR_OR_SELF:
6326 case LYXP_AXIS_ANCESTOR:
6327 case LYXP_AXIS_DESCENDANT_OR_SELF:
6328 case LYXP_AXIS_DESCENDANT:
6329 case LYXP_AXIS_FOLLOWING:
6330 case LYXP_AXIS_FOLLOWING_SIBLING:
6331 case LYXP_AXIS_PRECEDING:
6332 case LYXP_AXIS_PRECEDING_SIBLING:
6333 case LYXP_AXIS_CHILD:
6334 if (*in_ctx == LYXP_SET_SCNODE_START) {
6335 /* remember that context node was used */
6336 *in_ctx = LYXP_SET_SCNODE_START_USED;
6337 return LY_SUCCESS;
6338 } else if (*in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
6339 /* traversed */
6340 *in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
6341 return LY_SUCCESS;
6342 }
6343 break;
6344 case LYXP_AXIS_ATTRIBUTE:
6345 /* unreachable */
6346 assert(0);
6347 LOGINT(NULL);
6348 break;
6349 }
6350
6351 return LY_ENOT;
6352}
6353
6354/**
6355 * @brief Get previous sibling for a schema node.
6356 *
6357 * @param[in] scnode Schema node.
6358 * @param[in] getnext_opts Options for ::lys_getnext().
6359 * @return Previous sibling, NULL if none.
6360 */
6361static const struct lysc_node *
6362moveto_axis_scnode_preceding_sibling(const struct lysc_node *scnode, uint32_t getnext_opts)
6363{
6364 const struct lysc_node *next = NULL, *prev = NULL;
6365
6366 while ((next = lys_getnext(next, lysc_data_parent(scnode), scnode->module->compiled, getnext_opts))) {
6367 if (next == scnode) {
6368 break;
6369 }
6370
6371 prev = next;
6372 }
6373
6374 return prev;
6375}
6376
6377/**
6378 * @brief Get the first schema node on an axis for a context node.
6379 *
6380 * @param[in,out] iter Last returned node, start with NULL, updated to the next node.
6381 * @param[in,out] iter_type Node type of @p iter, start with 0, updated to the node type of the next node.
6382 * @param[in,out] iter_mod Internal module iterator, do not change.
6383 * @param[in,out] iter_mod_idx Internal module index iterator, do not change.
6384 * @param[in] scnode Context node.
6385 * @param[in] node_type Type of @p scnode.
6386 * @param[in] in_ctx In_ctx enum of @p scnode.
6387 * @param[in] axis Axis to use.
6388 * @param[in] set XPath set with the general context.
6389 * @param[in] getnext_opts Options for ::lys_getnext().
6390 * @return LY_SUCCESS on success.
6391 * @return LY_ENOTFOUND if no next node found.
6392 */
6393static LY_ERR
6394moveto_axis_scnode_next_first(const struct lysc_node **iter, enum lyxp_node_type *iter_type, const struct lys_module **iter_mod,
6395 uint32_t *iter_mod_idx, const struct lysc_node *scnode, enum lyxp_node_type node_type, enum lyxp_axis axis,
6396 struct lyxp_set *set, uint32_t getnext_opts)
6397{
6398 const struct lysc_node *next = NULL;
6399 enum lyxp_node_type next_type = 0;
6400
6401 assert(!*iter);
6402 assert(!*iter_type);
6403
6404 *iter_mod = NULL;
6405 *iter_mod_idx = 0;
6406
6407 switch (axis) {
6408 case LYXP_AXIS_ANCESTOR_OR_SELF:
6409 case LYXP_AXIS_DESCENDANT_OR_SELF:
6410 case LYXP_AXIS_SELF:
6411 if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT) || (node_type == LYXP_NODE_ELEM)) {
6412 /* just return the node */
6413 next = scnode;
6414 next_type = node_type;
6415 }
6416 break;
6417
6418 case LYXP_AXIS_ANCESTOR:
6419 case LYXP_AXIS_PARENT:
6420 if (node_type == LYXP_NODE_ELEM) {
6421 next = lysc_data_parent(scnode);
6422 next_type = next ? LYXP_NODE_ELEM : set->root_type;
6423 } /* else no parent */
6424 break;
6425
6426 case LYXP_AXIS_DESCENDANT:
6427 case LYXP_AXIS_CHILD:
6428 if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT)) {
6429 /* it can actually be in any module, it's all <running>, and even if it's moveto_mod (if set),
6430 * it can be in a top-level augment */
6431 while ((*iter_mod = ly_ctx_get_module_iter(set->ctx, iter_mod_idx))) {
6432 /* module may not be implemented or not compiled yet */
6433 if (!(*iter_mod)->compiled) {
6434 continue;
6435 }
6436
6437 /* get next node */
6438 if ((next = lys_getnext(NULL, NULL, (*iter_mod)->compiled, getnext_opts))) {
6439 next_type = LYXP_NODE_ELEM;
6440 break;
6441 }
6442 }
6443 } else if (node_type == LYXP_NODE_ELEM) {
6444 /* get next node */
6445 next = lys_getnext(NULL, scnode, NULL, getnext_opts);
6446 next_type = next ? LYXP_NODE_ELEM : 0;
6447 }
6448 break;
6449
6450 case LYXP_AXIS_FOLLOWING:
6451 case LYXP_AXIS_FOLLOWING_SIBLING:
6452 if (node_type == LYXP_NODE_ELEM) {
6453 /* first next sibling */
6454 next = lys_getnext(scnode, lysc_data_parent(scnode), scnode->module->compiled, getnext_opts);
6455 next_type = next ? LYXP_NODE_ELEM : 0;
6456 } /* else no sibling */
6457 break;
6458
6459 case LYXP_AXIS_PRECEDING:
6460 case LYXP_AXIS_PRECEDING_SIBLING:
6461 if (node_type == LYXP_NODE_ELEM) {
6462 /* first parent sibling */
6463 next = lys_getnext(NULL, lysc_data_parent(scnode), scnode->module->compiled, getnext_opts);
6464 if (next == scnode) {
6465 /* no preceding sibling */
6466 next = NULL;
6467 }
6468 next_type = next ? LYXP_NODE_ELEM : 0;
6469 } /* else no sibling */
6470 break;
6471
6472 case LYXP_AXIS_ATTRIBUTE:
6473 /* unreachable */
6474 assert(0);
6475 LOGINT(set->ctx);
6476 break;
6477 }
6478
6479 *iter = next;
6480 *iter_type = next_type;
6481 return next_type ? LY_SUCCESS : LY_ENOTFOUND;
6482}
6483
6484/**
6485 * @brief Iterate over all schema nodes on an axis for a context node.
6486 *
6487 * @param[in,out] iter Last returned node, start with NULL, updated to the next node.
6488 * @param[in,out] iter_type Node type of @p iter, start with 0, updated to the node type of the next node.
6489 * @param[in,out] iter_mod Internal module iterator, do not change.
6490 * @param[in,out] iter_mod_idx Internal module index iterator, do not change.
6491 * @param[in] scnode Context node.
6492 * @param[in] node_type Type of @p scnode.
6493 * @param[in] axis Axis to use.
6494 * @param[in] set XPath set with the general context.
6495 * @param[in] getnext_opts Options for ::lys_getnext().
6496 * @return LY_SUCCESS on success.
6497 * @return LY_ENOTFOUND if no next node found.
6498 */
6499static LY_ERR
6500moveto_axis_scnode_next(const struct lysc_node **iter, enum lyxp_node_type *iter_type, const struct lys_module **iter_mod,
6501 uint32_t *iter_mod_idx, const struct lysc_node *scnode, enum lyxp_node_type node_type, enum lyxp_axis axis,
6502 struct lyxp_set *set, uint32_t getnext_opts)
6503{
6504 const struct lysc_node *next = NULL, *dfs_stop;
6505 enum lyxp_node_type next_type = 0;
6506
6507 if (!*iter_type) {
6508 /* first returned node */
6509 return moveto_axis_scnode_next_first(iter, iter_type, iter_mod, iter_mod_idx, scnode, node_type, axis, set,
6510 getnext_opts);
6511 }
6512
6513 switch (axis) {
6514 case LYXP_AXIS_PARENT:
6515 case LYXP_AXIS_SELF:
6516 /* parent/self was returned before */
6517 break;
6518
6519 case LYXP_AXIS_ANCESTOR_OR_SELF:
6520 if ((*iter == scnode) && (*iter_type == node_type)) {
6521 /* fake first ancestor, we returned self before */
6522 *iter = NULL;
6523 *iter_type = 0;
6524 return moveto_axis_scnode_next_first(iter, iter_type, iter_mod, iter_mod_idx, scnode, node_type,
6525 LYXP_AXIS_ANCESTOR, set, getnext_opts);
6526 } /* else continue ancestor */
6527
6528 /* fallthrough */
6529 case LYXP_AXIS_ANCESTOR:
6530 if (*iter_type == LYXP_NODE_ELEM) {
6531 next = lysc_data_parent(*iter);
6532 next_type = next ? LYXP_NODE_ELEM : set->root_type;
6533 } /* else no ancestor */
6534 break;
6535
6536 case LYXP_AXIS_DESCENDANT_OR_SELF:
6537 if ((*iter == scnode) && (*iter_type == node_type)) {
6538 /* fake first descendant, we returned self before */
6539 *iter = NULL;
6540 *iter_type = 0;
6541 return moveto_axis_scnode_next_first(iter, iter_type, iter_mod, iter_mod_idx, scnode, node_type,
6542 LYXP_AXIS_DESCENDANT, set, getnext_opts);
6543 } /* else DFS until context node */
6544 dfs_stop = scnode;
6545
6546 /* fallthrough */
6547 case LYXP_AXIS_DESCENDANT:
6548 if (axis == LYXP_AXIS_DESCENDANT) {
6549 /* DFS until the context node */
6550 dfs_stop = scnode;
6551 }
6552
6553 /* fallthrough */
6554 case LYXP_AXIS_PRECEDING:
6555 if (axis == LYXP_AXIS_PRECEDING) {
6556 /* DFS until the previous sibling */
6557 dfs_stop = moveto_axis_scnode_preceding_sibling(scnode, getnext_opts);
6558 assert(dfs_stop);
6559
6560 if (*iter == dfs_stop) {
6561 /* we are done */
6562 break;
6563 }
6564 }
6565
6566 /* fallthrough */
6567 case LYXP_AXIS_FOLLOWING:
6568 if (axis == LYXP_AXIS_FOLLOWING) {
6569 /* DFS through the whole module */
6570 dfs_stop = NULL;
6571 }
6572
6573 /* nested nodes */
6574 assert(*iter);
6575 next = moveto_axis_scnode_next_dfs_forward(*iter, dfs_stop, getnext_opts);
6576 if (next) {
6577 next_type = LYXP_NODE_ELEM;
6578 break;
6579 } /* else get next top-level node just like a child */
6580
6581 /* fallthrough */
6582 case LYXP_AXIS_CHILD:
6583 case LYXP_AXIS_FOLLOWING_SIBLING:
6584 if (!*iter_mod) {
6585 /* nodes from a single module */
6586 if ((next = lys_getnext(*iter, lysc_data_parent(*iter), (*iter)->module->compiled, getnext_opts))) {
6587 next_type = LYXP_NODE_ELEM;
6588 break;
6589 }
6590
6591 assert(scnode);
6592 if (!lysc_data_parent(scnode)) {
6593 /* iterating over top-level nodes, find next */
6594 while (lysc_data_parent(*iter)) {
6595 *iter = lysc_data_parent(*iter);
6596 }
6597 if ((next = lys_getnext(*iter, NULL, (*iter)->module->compiled, getnext_opts))) {
6598 next_type = LYXP_NODE_ELEM;
6599 break;
6600 }
6601 }
6602 }
6603
6604 while (*iter_mod) {
6605 /* module top-level nodes */
6606 if ((next = lys_getnext(*iter, NULL, (*iter_mod)->compiled, getnext_opts))) {
6607 next_type = LYXP_NODE_ELEM;
6608 break;
6609 }
6610
6611 /* get next module */
6612 while ((*iter_mod = ly_ctx_get_module_iter(set->ctx, iter_mod_idx))) {
6613 /* module may not be implemented or not compiled yet */
6614 if ((*iter_mod)->compiled) {
6615 break;
6616 }
6617 }
6618
6619 /* new module, start over */
6620 *iter = NULL;
6621 }
6622 break;
6623
6624 case LYXP_AXIS_PRECEDING_SIBLING:
6625 assert(*iter);
6626
6627 /* next parent sibling until scnode */
6628 next = lys_getnext(*iter, lysc_data_parent(*iter), (*iter)->module->compiled, getnext_opts);
6629 if (next == scnode) {
6630 /* no previous sibling */
6631 next = NULL;
6632 }
6633 next_type = next ? LYXP_NODE_ELEM : 0;
6634 break;
6635
6636 case LYXP_AXIS_ATTRIBUTE:
6637 /* unreachable */
6638 assert(0);
6639 LOGINT(set->ctx);
6640 break;
6641 }
6642
6643 *iter = next;
6644 *iter_type = next_type;
6645 return next_type ? LY_SUCCESS : LY_ENOTFOUND;
6646}
6647
6648/**
Michal Vaskod3678892020-05-21 10:06:58 +02006649 * @brief Move context @p set to a schema node. Result is LYXP_SET_SCNODE_SET (or LYXP_SET_EMPTY).
6650 *
6651 * @param[in,out] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006652 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02006653 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko49fec8e2022-05-24 10:28:33 +02006654 * @param[in] axis Axis to search on.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006655 * @param[in] options XPath options.
6656 * @return LY_ERR
6657 */
6658static LY_ERR
Michal Vasko49fec8e2022-05-24 10:28:33 +02006659moveto_scnode(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, enum lyxp_axis axis,
6660 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006661{
Radek Krejci857189e2020-09-01 13:26:36 +02006662 ly_bool temp_ctx = 0;
Michal Vasko49fec8e2022-05-24 10:28:33 +02006663 uint32_t getnext_opts, orig_used, i, mod_idx, idx;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006664 const struct lys_module *mod;
Michal Vasko49fec8e2022-05-24 10:28:33 +02006665 const struct lysc_node *iter;
6666 enum lyxp_node_type iter_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006667
aPiecek8b0cc152021-05-31 16:40:31 +02006668 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006669 return LY_SUCCESS;
6670 }
6671
6672 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006673 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006674 return LY_EVALID;
6675 }
6676
Michal Vaskocafad9d2019-11-07 15:20:03 +01006677 /* getnext opts */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01006678 getnext_opts = 0;
Michal Vaskocafad9d2019-11-07 15:20:03 +01006679 if (options & LYXP_SCNODE_OUTPUT) {
6680 getnext_opts |= LYS_GETNEXT_OUTPUT;
6681 }
6682
Michal Vasko03ff5a72019-09-11 13:49:33 +02006683 orig_used = set->used;
6684 for (i = 0; i < orig_used; ++i) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02006685 /* update in_ctx first */
6686 if (moveto_axis_scnode_next_in_ctx(&set->val.scnodes[i].in_ctx, axis)) {
6687 /* not usable, skip */
6688 continue;
6689 }
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006690
Michal Vasko49fec8e2022-05-24 10:28:33 +02006691 iter = NULL;
6692 iter_type = 0;
6693 while (!moveto_axis_scnode_next(&iter, &iter_type, &mod, &mod_idx, set->val.scnodes[i].scnode,
6694 set->val.scnodes[i].type, axis, set, getnext_opts)) {
6695 if (moveto_scnode_check(iter, NULL, set, ncname, moveto_mod)) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006696 continue;
6697 }
6698
Michal Vasko49fec8e2022-05-24 10:28:33 +02006699 /* insert */
Michal Vasko7333cb32022-07-29 16:30:29 +02006700 LY_CHECK_RET(set_scnode_insert_node(set, iter, iter_type, axis, &idx));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006701
Michal Vasko49fec8e2022-05-24 10:28:33 +02006702 /* we need to prevent these nodes from being considered in this moveto */
6703 if ((idx < orig_used) && (idx > i)) {
6704 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX;
6705 temp_ctx = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006706 }
6707 }
6708 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006709
6710 /* correct temporary in_ctx values */
6711 if (temp_ctx) {
6712 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006713 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_NEW_CTX) {
6714 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006715 }
6716 }
6717 }
6718
6719 return LY_SUCCESS;
6720}
6721
6722/**
Michal Vasko49fec8e2022-05-24 10:28:33 +02006723 * @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 +02006724 * Context position aware.
6725 *
6726 * @param[in] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006727 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02006728 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vaskocdad7122020-11-09 21:04:44 +01006729 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006730 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6731 */
6732static LY_ERR
Michal Vasko49fec8e2022-05-24 10:28:33 +02006733moveto_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 +02006734{
6735 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006736 const struct lyd_node *next, *elem, *start;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006737 struct lyxp_set ret_set;
6738 LY_ERR rc;
6739
aPiecek8b0cc152021-05-31 16:40:31 +02006740 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006741 return LY_SUCCESS;
6742 }
6743
6744 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006745 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006746 return LY_EVALID;
6747 }
6748
Michal Vasko9f96a052020-03-10 09:41:45 +01006749 /* 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 +02006750 rc = xpath_pi_node(set, LYXP_AXIS_CHILD, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006751 LY_CHECK_RET(rc);
6752
Michal Vasko6346ece2019-09-24 13:12:53 +02006753 /* this loop traverses all the nodes in the set and adds/keeps only those that match qname */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006754 set_init(&ret_set, set);
6755 for (i = 0; i < set->used; ++i) {
6756
6757 /* TREE DFS */
6758 start = set->val.nodes[i].node;
6759 for (elem = next = start; elem; elem = next) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02006760 rc = moveto_node_check(elem, LYXP_NODE_ELEM, set, ncname, moveto_mod, options);
Michal Vasko6346ece2019-09-24 13:12:53 +02006761 if (!rc) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006762 /* add matching node into result set */
6763 set_insert_node(&ret_set, elem, 0, LYXP_NODE_ELEM, ret_set.used);
6764 if (set_dup_node_check(set, elem, LYXP_NODE_ELEM, i)) {
6765 /* the node is a duplicate, we'll process it later in the set */
6766 goto skip_children;
6767 }
Michal Vasko6346ece2019-09-24 13:12:53 +02006768 } else if (rc == LY_EINCOMPLETE) {
Michal Vasko6346ece2019-09-24 13:12:53 +02006769 return rc;
6770 } else if (rc == LY_EINVAL) {
6771 goto skip_children;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006772 }
6773
6774 /* TREE DFS NEXT ELEM */
6775 /* select element for the next run - children first */
Radek Krejcia1c1e542020-09-29 16:06:52 +02006776 next = lyd_child(elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006777 if (!next) {
6778skip_children:
6779 /* no children, so try siblings, but only if it's not the start,
6780 * that is considered to be the root and it's siblings are not traversed */
6781 if (elem != start) {
6782 next = elem->next;
6783 } else {
6784 break;
6785 }
6786 }
6787 while (!next) {
6788 /* no siblings, go back through the parents */
Michal Vasko9e685082021-01-29 14:49:09 +01006789 if (lyd_parent(elem) == start) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006790 /* we are done, no next element to process */
6791 break;
6792 }
6793 /* parent is already processed, go to its sibling */
Michal Vasko9e685082021-01-29 14:49:09 +01006794 elem = lyd_parent(elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006795 next = elem->next;
6796 }
6797 }
6798 }
6799
6800 /* make the temporary set the current one */
6801 ret_set.ctx_pos = set->ctx_pos;
6802 ret_set.ctx_size = set->ctx_size;
Michal Vaskod3678892020-05-21 10:06:58 +02006803 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006804 memcpy(set, &ret_set, sizeof *set);
Michal Vasko306e2832022-07-25 09:15:17 +02006805 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006806
6807 return LY_SUCCESS;
6808}
6809
6810/**
Michal Vasko49fec8e2022-05-24 10:28:33 +02006811 * @brief Move context @p set to a child schema node and all its descendants starting from a node.
6812 * Result is LYXP_SET_NODE_SET.
6813 *
6814 * @param[in] set Set to use.
6815 * @param[in] start Start node whose subtree to add.
6816 * @param[in] start_idx Index of @p start in @p set.
6817 * @param[in] moveto_mod Matching node module, NULL for no prefix.
6818 * @param[in] ncname Matching node name in the dictionary, NULL for any.
6819 * @param[in] options XPath options.
6820 * @return LY_ERR value.
6821 */
6822static LY_ERR
6823moveto_scnode_dfs(struct lyxp_set *set, const struct lysc_node *start, uint32_t start_idx,
6824 const struct lys_module *moveto_mod, const char *ncname, uint32_t options)
6825{
6826 const struct lysc_node *next, *elem;
6827 uint32_t idx;
6828 LY_ERR rc;
6829
6830 /* TREE DFS */
6831 for (elem = next = start; elem; elem = next) {
6832 if ((elem == start) || (elem->nodetype & (LYS_CHOICE | LYS_CASE))) {
6833 /* schema-only nodes, skip root */
6834 goto next_iter;
6835 }
6836
6837 rc = moveto_scnode_check(elem, start, set, ncname, moveto_mod);
6838 if (!rc) {
6839 if (lyxp_set_scnode_contains(set, elem, LYXP_NODE_ELEM, start_idx, &idx)) {
6840 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
6841 if (idx > start_idx) {
6842 /* we will process it later in the set */
6843 goto skip_children;
6844 }
6845 } else {
Michal Vasko7333cb32022-07-29 16:30:29 +02006846 LY_CHECK_RET(set_scnode_insert_node(set, elem, LYXP_NODE_ELEM, LYXP_AXIS_DESCENDANT, NULL));
Michal Vasko49fec8e2022-05-24 10:28:33 +02006847 }
6848 } else if (rc == LY_EINVAL) {
6849 goto skip_children;
6850 }
6851
6852next_iter:
6853 /* TREE DFS NEXT ELEM */
6854 /* select element for the next run - children first */
6855 next = lysc_node_child(elem);
6856 if (next && (next->nodetype == LYS_INPUT) && (options & LYXP_SCNODE_OUTPUT)) {
6857 next = next->next;
6858 } else if (next && (next->nodetype == LYS_OUTPUT) && !(options & LYXP_SCNODE_OUTPUT)) {
6859 next = next->next;
6860 }
6861 if (!next) {
6862skip_children:
6863 /* no children, so try siblings, but only if it's not the start,
6864 * that is considered to be the root and it's siblings are not traversed */
6865 if (elem != start) {
6866 next = elem->next;
6867 } else {
6868 break;
6869 }
6870 }
6871 while (!next) {
6872 /* no siblings, go back through the parents */
6873 if (elem->parent == start) {
6874 /* we are done, no next element to process */
6875 break;
6876 }
6877 /* parent is already processed, go to its sibling */
6878 elem = elem->parent;
6879 next = elem->next;
6880 }
6881 }
6882
6883 return LY_SUCCESS;
6884}
6885
6886/**
6887 * @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 +02006888 *
6889 * @param[in] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006890 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02006891 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006892 * @param[in] options XPath options.
Michal Vasko49fec8e2022-05-24 10:28:33 +02006893 * @return LY_ERR value.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006894 */
6895static LY_ERR
Michal Vasko49fec8e2022-05-24 10:28:33 +02006896moveto_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 +02006897{
Michal Vasko49fec8e2022-05-24 10:28:33 +02006898 uint32_t i, orig_used, mod_idx;
6899 const struct lys_module *mod;
6900 const struct lysc_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006901
aPiecek8b0cc152021-05-31 16:40:31 +02006902 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006903 return LY_SUCCESS;
6904 }
6905
6906 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006907 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006908 return LY_EVALID;
6909 }
6910
Michal Vasko03ff5a72019-09-11 13:49:33 +02006911 orig_used = set->used;
6912 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006913 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
6914 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006915 continue;
6916 }
6917
6918 /* remember context node */
Radek Krejcif13b87b2020-12-01 22:02:17 +01006919 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vaskoec4df482019-12-16 10:02:18 +01006920 } else {
Michal Vasko1a09b212021-05-06 13:00:10 +02006921 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006922 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006923
Michal Vasko49fec8e2022-05-24 10:28:33 +02006924 if ((set->val.scnodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.scnodes[i].type == LYXP_NODE_ROOT)) {
6925 /* traverse all top-level nodes in all the modules */
6926 mod_idx = 0;
6927 while ((mod = ly_ctx_get_module_iter(set->ctx, &mod_idx))) {
6928 /* module may not be implemented or not compiled yet */
6929 if (!mod->compiled) {
6930 continue;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006931 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006932
Michal Vasko49fec8e2022-05-24 10:28:33 +02006933 root = NULL;
6934 /* no getnext opts needed */
6935 while ((root = lys_getnext(root, NULL, mod->compiled, 0))) {
6936 LY_CHECK_RET(moveto_scnode_dfs(set, root, i, moveto_mod, ncname, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006937 }
6938 }
Michal Vasko49fec8e2022-05-24 10:28:33 +02006939
6940 } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
6941 /* add all the descendants recursively */
6942 LY_CHECK_RET(moveto_scnode_dfs(set, set->val.scnodes[i].scnode, i, moveto_mod, ncname, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006943 }
6944 }
6945
6946 return LY_SUCCESS;
6947}
6948
6949/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02006950 * @brief Move context @p set to an attribute. Result is LYXP_SET_NODE_SET.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006951 * Indirectly context position aware.
6952 *
6953 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02006954 * @param[in] mod Matching metadata module, NULL for any.
6955 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
aPiecek8b0cc152021-05-31 16:40:31 +02006956 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006957 * @return LY_ERR
6958 */
6959static LY_ERR
aPiecek8b0cc152021-05-31 16:40:31 +02006960moveto_attr(struct lyxp_set *set, const struct lys_module *mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006961{
Michal Vasko9f96a052020-03-10 09:41:45 +01006962 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006963
aPiecek8b0cc152021-05-31 16:40:31 +02006964 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006965 return LY_SUCCESS;
6966 }
6967
6968 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006969 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006970 return LY_EVALID;
6971 }
6972
Radek Krejci1deb5be2020-08-26 16:43:36 +02006973 for (uint32_t i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02006974 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006975
6976 /* only attributes of an elem (not dummy) can be in the result, skip all the rest;
6977 * our attributes are always qualified */
Michal Vasko5c4e5892019-11-14 12:31:38 +01006978 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01006979 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006980
6981 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02006982 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006983 continue;
6984 }
6985
Michal Vaskod3678892020-05-21 10:06:58 +02006986 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006987 /* match */
6988 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01006989 set->val.meta[i].meta = sub;
6990 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006991 /* pos does not change */
6992 replaced = 1;
6993 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01006994 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 +02006995 }
6996 ++i;
6997 }
6998 }
6999 }
7000
7001 if (!replaced) {
7002 /* no match */
7003 set_remove_node(set, i);
7004 }
7005 }
7006
7007 return LY_SUCCESS;
7008}
7009
7010/**
7011 * @brief Move context @p set1 to union with @p set2. @p set2 is emptied afterwards.
Michal Vasko61ac2f62020-05-25 12:39:51 +02007012 * Result is LYXP_SET_NODE_SET. Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007013 *
7014 * @param[in,out] set1 Set to use for the result.
7015 * @param[in] set2 Set that is copied to @p set1.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007016 * @return LY_ERR
7017 */
7018static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007019moveto_union(struct lyxp_set *set1, struct lyxp_set *set2)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007020{
7021 LY_ERR rc;
7022
Michal Vaskod3678892020-05-21 10:06:58 +02007023 if ((set1->type != LYXP_SET_NODE_SET) || (set2->type != LYXP_SET_NODE_SET)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01007024 LOGVAL(set1->ctx, LY_VCODE_XP_INOP_2, "union", print_set_type(set1), print_set_type(set2));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007025 return LY_EVALID;
7026 }
7027
7028 /* set2 is empty or both set1 and set2 */
Michal Vaskod3678892020-05-21 10:06:58 +02007029 if (!set2->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007030 return LY_SUCCESS;
7031 }
7032
Michal Vaskod3678892020-05-21 10:06:58 +02007033 if (!set1->used) {
aPiecekadc1e4f2021-10-07 11:15:12 +02007034 /* release hidden allocated data (lyxp_set.size) */
7035 lyxp_set_free_content(set1);
7036 /* direct copying of the entire structure */
Michal Vasko03ff5a72019-09-11 13:49:33 +02007037 memcpy(set1, set2, sizeof *set1);
7038 /* dynamic memory belongs to set1 now, do not free */
Michal Vaskod3678892020-05-21 10:06:58 +02007039 memset(set2, 0, sizeof *set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007040 return LY_SUCCESS;
7041 }
7042
7043 /* we assume sets are sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007044 assert(!set_sort(set1) && !set_sort(set2));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007045
7046 /* sort, remove duplicates */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007047 rc = set_sorted_merge(set1, set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007048 LY_CHECK_RET(rc);
7049
7050 /* final set must be sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007051 assert(!set_sort(set1));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007052
7053 return LY_SUCCESS;
7054}
7055
7056/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02007057 * @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 +02007058 * Context position aware.
7059 *
7060 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02007061 * @param[in] mod Matching metadata module, NULL for any.
7062 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
Michal Vaskocdad7122020-11-09 21:04:44 +01007063 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007064 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7065 */
7066static int
Michal Vaskocdad7122020-11-09 21:04:44 +01007067moveto_attr_alldesc(struct lyxp_set *set, const struct lys_module *mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007068{
Michal Vasko9f96a052020-03-10 09:41:45 +01007069 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007070 struct lyxp_set *set_all_desc = NULL;
7071 LY_ERR rc;
7072
aPiecek8b0cc152021-05-31 16:40:31 +02007073 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007074 return LY_SUCCESS;
7075 }
7076
7077 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01007078 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007079 return LY_EVALID;
7080 }
7081
Michal Vasko03ff5a72019-09-11 13:49:33 +02007082 /* can be optimized similarly to moveto_node_alldesc() and save considerable amount of memory,
7083 * but it likely won't be used much, so it's a waste of time */
7084 /* copy the context */
7085 set_all_desc = set_copy(set);
7086 /* get all descendant nodes (the original context nodes are removed) */
Michal Vasko49fec8e2022-05-24 10:28:33 +02007087 rc = moveto_node_alldesc_child(set_all_desc, NULL, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007088 if (rc != LY_SUCCESS) {
7089 lyxp_set_free(set_all_desc);
7090 return rc;
7091 }
7092 /* prepend the original context nodes */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007093 rc = moveto_union(set, set_all_desc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007094 if (rc != LY_SUCCESS) {
7095 lyxp_set_free(set_all_desc);
7096 return rc;
7097 }
7098 lyxp_set_free(set_all_desc);
7099
Radek Krejci1deb5be2020-08-26 16:43:36 +02007100 for (uint32_t i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02007101 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007102
7103 /* only attributes of an elem can be in the result, skip all the rest,
7104 * we have all attributes qualified in lyd tree */
7105 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01007106 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007107 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02007108 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007109 continue;
7110 }
7111
Michal Vaskod3678892020-05-21 10:06:58 +02007112 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007113 /* match */
7114 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01007115 set->val.meta[i].meta = sub;
7116 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007117 /* pos does not change */
7118 replaced = 1;
7119 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01007120 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 +02007121 }
7122 ++i;
7123 }
7124 }
7125 }
7126
7127 if (!replaced) {
7128 /* no match */
7129 set_remove_node(set, i);
7130 }
7131 }
7132
7133 return LY_SUCCESS;
7134}
7135
7136/**
Michal Vasko8abcecc2022-07-28 09:55:01 +02007137 * @brief Move context @p set1 single item to the result of a comparison.
7138 *
7139 * @param[in] set1 First set with the item to compare.
7140 * @param[in] idx1 Index of the item in @p set1.
7141 * @param[in] set2 Second set.
7142 * @param[in] op Comparison operator to process.
7143 * @param[in] switch_operands Whether to switch sets as operands; whether it is `set1 op set2` or `set2 op set1`.
7144 * @param[out] result Result of the comparison.
7145 * @return LY_ERR value.
7146 */
7147static LY_ERR
7148moveto_op_comp_item(const struct lyxp_set *set1, uint32_t idx1, struct lyxp_set *set2, const char *op,
7149 ly_bool switch_operands, ly_bool *result)
7150{
7151 struct lyxp_set tmp1 = {0};
7152 LY_ERR rc = LY_SUCCESS;
7153
7154 assert(set1->type == LYXP_SET_NODE_SET);
7155
7156 /* cast set1 */
7157 switch (set2->type) {
7158 case LYXP_SET_NUMBER:
7159 rc = set_comp_cast(&tmp1, set1, LYXP_SET_NUMBER, idx1);
7160 break;
7161 case LYXP_SET_BOOLEAN:
7162 rc = set_comp_cast(&tmp1, set1, LYXP_SET_BOOLEAN, idx1);
7163 break;
7164 default:
7165 rc = set_comp_cast(&tmp1, set1, LYXP_SET_STRING, idx1);
7166 break;
7167 }
7168 LY_CHECK_GOTO(rc, cleanup);
7169
7170 /* canonize set2 */
7171 LY_CHECK_GOTO(rc = set_comp_canonize(set2, &set1->val.nodes[idx1]), cleanup);
7172
7173 /* compare recursively and store the result */
7174 if (switch_operands) {
7175 LY_CHECK_GOTO(rc = moveto_op_comp(set2, &tmp1, op), cleanup);
7176 *result = set2->val.bln;
7177 } else {
7178 LY_CHECK_GOTO(rc = moveto_op_comp(&tmp1, set2, op), cleanup);
7179 *result = tmp1.val.bln;
7180 }
7181
7182cleanup:
7183 lyxp_set_free_content(&tmp1);
7184 return rc;
7185}
7186
7187/**
7188 * @brief Move context @p set1 to the result of a comparison. Handles '=', '!=', '<=', '<', '>=', or '>'.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007189 * Result is LYXP_SET_BOOLEAN. Indirectly context position aware.
7190 *
7191 * @param[in,out] set1 Set to use for the result.
7192 * @param[in] set2 Set acting as the second operand for @p op.
7193 * @param[in] op Comparison operator to process.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007194 * @return LY_ERR
7195 */
7196static LY_ERR
Michal Vasko8abcecc2022-07-28 09:55:01 +02007197moveto_op_comp(struct lyxp_set *set1, struct lyxp_set *set2, const char *op)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007198{
7199 /*
7200 * NODE SET + NODE SET = NODE SET + STRING /(1 NODE SET) 2 STRING
7201 * NODE SET + STRING = STRING + STRING /1 STRING (2 STRING)
7202 * NODE SET + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
7203 * NODE SET + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
7204 * STRING + NODE SET = STRING + STRING /(1 STRING) 2 STRING
7205 * NUMBER + NODE SET = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
7206 * BOOLEAN + NODE SET = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
7207 *
7208 * '=' or '!='
7209 * BOOLEAN + BOOLEAN
7210 * BOOLEAN + STRING = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
7211 * BOOLEAN + NUMBER = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
7212 * STRING + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
7213 * NUMBER + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
7214 * NUMBER + NUMBER
7215 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
7216 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
7217 * STRING + STRING
7218 *
7219 * '<=', '<', '>=', '>'
7220 * NUMBER + NUMBER
7221 * BOOLEAN + BOOLEAN = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
7222 * BOOLEAN + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
7223 * BOOLEAN + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
7224 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
7225 * STRING + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
7226 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
7227 * NUMBER + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
7228 * STRING + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
7229 */
Michal Vasko8abcecc2022-07-28 09:55:01 +02007230 ly_bool result;
7231 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007232 LY_ERR rc;
7233
Michal Vasko03ff5a72019-09-11 13:49:33 +02007234 /* iterative evaluation with node-sets */
7235 if ((set1->type == LYXP_SET_NODE_SET) || (set2->type == LYXP_SET_NODE_SET)) {
7236 if (set1->type == LYXP_SET_NODE_SET) {
7237 for (i = 0; i < set1->used; ++i) {
Michal Vasko8abcecc2022-07-28 09:55:01 +02007238 /* evaluate for the single item */
7239 LY_CHECK_RET(moveto_op_comp_item(set1, i, set2, op, 0, &result));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007240
7241 /* lazy evaluation until true */
Michal Vasko8abcecc2022-07-28 09:55:01 +02007242 if (result) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007243 set_fill_boolean(set1, 1);
7244 return LY_SUCCESS;
7245 }
7246 }
7247 } else {
7248 for (i = 0; i < set2->used; ++i) {
Michal Vasko8abcecc2022-07-28 09:55:01 +02007249 /* evaluate for the single item */
7250 LY_CHECK_RET(moveto_op_comp_item(set2, i, set1, op, 1, &result));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007251
7252 /* lazy evaluation until true */
Michal Vasko8abcecc2022-07-28 09:55:01 +02007253 if (result) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007254 set_fill_boolean(set1, 1);
7255 return LY_SUCCESS;
7256 }
7257 }
7258 }
7259
Michal Vasko8abcecc2022-07-28 09:55:01 +02007260 /* false for all the nodes */
Michal Vasko03ff5a72019-09-11 13:49:33 +02007261 set_fill_boolean(set1, 0);
7262 return LY_SUCCESS;
7263 }
7264
7265 /* first convert properly */
7266 if ((op[0] == '=') || (op[0] == '!')) {
7267 if ((set1->type == LYXP_SET_BOOLEAN) || (set2->type == LYXP_SET_BOOLEAN)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007268 lyxp_set_cast(set1, LYXP_SET_BOOLEAN);
7269 lyxp_set_cast(set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007270 } else if ((set1->type == LYXP_SET_NUMBER) || (set2->type == LYXP_SET_NUMBER)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007271 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007272 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007273 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007274 LY_CHECK_RET(rc);
7275 } /* else we have 2 strings */
7276 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007277 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007278 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007279 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007280 LY_CHECK_RET(rc);
7281 }
7282
7283 assert(set1->type == set2->type);
7284
7285 /* compute result */
7286 if (op[0] == '=') {
7287 if (set1->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +02007288 result = (set1->val.bln == set2->val.bln);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007289 } else if (set1->type == LYXP_SET_NUMBER) {
7290 result = (set1->val.num == set2->val.num);
7291 } else {
7292 assert(set1->type == LYXP_SET_STRING);
Michal Vaskoac6c72f2019-11-14 16:09:34 +01007293 result = !strcmp(set1->val.str, set2->val.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007294 }
7295 } else if (op[0] == '!') {
7296 if (set1->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +02007297 result = (set1->val.bln != set2->val.bln);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007298 } else if (set1->type == LYXP_SET_NUMBER) {
7299 result = (set1->val.num != set2->val.num);
7300 } else {
7301 assert(set1->type == LYXP_SET_STRING);
Michal Vaskoc2058432020-11-06 17:26:21 +01007302 result = strcmp(set1->val.str, set2->val.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007303 }
7304 } else {
7305 assert(set1->type == LYXP_SET_NUMBER);
7306 if (op[0] == '<') {
7307 if (op[1] == '=') {
7308 result = (set1->val.num <= set2->val.num);
7309 } else {
7310 result = (set1->val.num < set2->val.num);
7311 }
7312 } else {
7313 if (op[1] == '=') {
7314 result = (set1->val.num >= set2->val.num);
7315 } else {
7316 result = (set1->val.num > set2->val.num);
7317 }
7318 }
7319 }
7320
7321 /* assign result */
7322 if (result) {
7323 set_fill_boolean(set1, 1);
7324 } else {
7325 set_fill_boolean(set1, 0);
7326 }
7327
7328 return LY_SUCCESS;
7329}
7330
7331/**
7332 * @brief Move context @p set to the result of a basic operation. Handles '+', '-', unary '-', '*', 'div',
7333 * or 'mod'. Result is LYXP_SET_NUMBER. Indirectly context position aware.
7334 *
7335 * @param[in,out] set1 Set to use for the result.
7336 * @param[in] set2 Set acting as the second operand for @p op.
7337 * @param[in] op Operator to process.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007338 * @return LY_ERR
7339 */
7340static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007341moveto_op_math(struct lyxp_set *set1, struct lyxp_set *set2, const char *op)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007342{
7343 LY_ERR rc;
7344
7345 /* unary '-' */
7346 if (!set2 && (op[0] == '-')) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007347 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007348 LY_CHECK_RET(rc);
7349 set1->val.num *= -1;
7350 lyxp_set_free(set2);
7351 return LY_SUCCESS;
7352 }
7353
7354 assert(set1 && set2);
7355
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007356 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007357 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007358 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007359 LY_CHECK_RET(rc);
7360
7361 switch (op[0]) {
7362 /* '+' */
7363 case '+':
7364 set1->val.num += set2->val.num;
7365 break;
7366
7367 /* '-' */
7368 case '-':
7369 set1->val.num -= set2->val.num;
7370 break;
7371
7372 /* '*' */
7373 case '*':
7374 set1->val.num *= set2->val.num;
7375 break;
7376
7377 /* 'div' */
7378 case 'd':
7379 set1->val.num /= set2->val.num;
7380 break;
7381
7382 /* 'mod' */
7383 case 'm':
7384 set1->val.num = ((long long)set1->val.num) % ((long long)set2->val.num);
7385 break;
7386
7387 default:
7388 LOGINT_RET(set1->ctx);
7389 }
7390
7391 return LY_SUCCESS;
7392}
7393
Michal Vasko03ff5a72019-09-11 13:49:33 +02007394/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02007395 * @brief Evaluate Predicate. Logs directly on error.
7396 *
Michal Vaskod3678892020-05-21 10:06:58 +02007397 * [9] Predicate ::= '[' Expr ']'
Michal Vasko03ff5a72019-09-11 13:49:33 +02007398 *
7399 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007400 * @param[in] tok_idx Position in the expression @p exp.
aPiecek8b0cc152021-05-31 16:40:31 +02007401 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007402 * @param[in] options XPath options.
Michal Vasko49fec8e2022-05-24 10:28:33 +02007403 * @param[in] axis Axis to search on.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007404 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7405 */
7406static LY_ERR
Michal Vasko49fec8e2022-05-24 10:28:33 +02007407eval_predicate(const struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set, uint32_t options, enum lyxp_axis axis)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007408{
7409 LY_ERR rc;
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01007410 uint16_t orig_exp;
7411 uint32_t i, orig_pos, orig_size;
Michal Vasko5c4e5892019-11-14 12:31:38 +01007412 int32_t pred_in_ctx;
Michal Vasko88a9e802022-05-24 10:50:28 +02007413 ly_bool reverse_axis = 0;
aPiecekfff4dca2021-10-07 10:59:53 +02007414 struct lyxp_set set2 = {0};
Michal Vasko03ff5a72019-09-11 13:49:33 +02007415
7416 /* '[' */
aPiecek8b0cc152021-05-31 16:40:31 +02007417 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02007418 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007419 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007420
aPiecek8b0cc152021-05-31 16:40:31 +02007421 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007422only_parse:
aPiecek8b0cc152021-05-31 16:40:31 +02007423 rc = eval_expr_select(exp, tok_idx, 0, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007424 LY_CHECK_RET(rc);
7425 } else if (set->type == LYXP_SET_NODE_SET) {
7426 /* 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 +01007427 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007428
7429 /* empty set, nothing to evaluate */
7430 if (!set->used) {
7431 goto only_parse;
7432 }
7433
Michal Vasko49fec8e2022-05-24 10:28:33 +02007434 /* decide forward or reverse axis */
7435 switch (axis) {
7436 case LYXP_AXIS_ANCESTOR:
7437 case LYXP_AXIS_ANCESTOR_OR_SELF:
7438 case LYXP_AXIS_PRECEDING:
7439 case LYXP_AXIS_PRECEDING_SIBLING:
7440 reverse_axis = 1;
7441 break;
7442 case LYXP_AXIS_DESCENDANT:
7443 case LYXP_AXIS_DESCENDANT_OR_SELF:
7444 case LYXP_AXIS_FOLLOWING:
7445 case LYXP_AXIS_FOLLOWING_SIBLING:
7446 case LYXP_AXIS_PARENT:
7447 case LYXP_AXIS_CHILD:
7448 case LYXP_AXIS_SELF:
7449 case LYXP_AXIS_ATTRIBUTE:
7450 reverse_axis = 0;
7451 break;
7452 }
7453
Michal Vasko004d3152020-06-11 19:59:22 +02007454 orig_exp = *tok_idx;
Michal Vasko49fec8e2022-05-24 10:28:33 +02007455 orig_pos = reverse_axis ? set->used + 1 : 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007456 orig_size = set->used;
Michal Vasko39dbf352020-05-21 10:08:59 +02007457 for (i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007458 set_init(&set2, set);
7459 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 +02007460
7461 /* remember the node context position for position() and context size for last() */
7462 orig_pos += reverse_axis ? -1 : 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007463
7464 set2.ctx_pos = orig_pos;
7465 set2.ctx_size = orig_size;
Michal Vasko004d3152020-06-11 19:59:22 +02007466 *tok_idx = orig_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007467
Michal Vasko004d3152020-06-11 19:59:22 +02007468 rc = eval_expr_select(exp, tok_idx, 0, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007469 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02007470 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007471 return rc;
7472 }
7473
Michal Vasko49fec8e2022-05-24 10:28:33 +02007474 /* number is a proximity position */
Michal Vasko03ff5a72019-09-11 13:49:33 +02007475 if (set2.type == LYXP_SET_NUMBER) {
7476 if ((long long)set2.val.num == orig_pos) {
7477 set2.val.num = 1;
7478 } else {
7479 set2.val.num = 0;
7480 }
7481 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007482 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007483
7484 /* predicate satisfied or not? */
Michal Vasko004d3152020-06-11 19:59:22 +02007485 if (!set2.val.bln) {
Michal Vasko2caefc12019-11-14 16:07:56 +01007486 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007487 }
7488 }
Michal Vasko2caefc12019-11-14 16:07:56 +01007489 set_remove_nodes_none(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007490
7491 } else if (set->type == LYXP_SET_SCNODE_SET) {
7492 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01007493 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007494 /* there is a currently-valid node */
7495 break;
7496 }
7497 }
7498 /* empty set, nothing to evaluate */
7499 if (i == set->used) {
7500 goto only_parse;
7501 }
7502
Michal Vasko004d3152020-06-11 19:59:22 +02007503 orig_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007504
Michal Vasko03ff5a72019-09-11 13:49:33 +02007505 /* set special in_ctx to all the valid snodes */
7506 pred_in_ctx = set_scnode_new_in_ctx(set);
7507
7508 /* use the valid snodes one-by-one */
7509 for (i = 0; i < set->used; ++i) {
7510 if (set->val.scnodes[i].in_ctx != pred_in_ctx) {
7511 continue;
7512 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01007513 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007514
Michal Vasko004d3152020-06-11 19:59:22 +02007515 *tok_idx = orig_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007516
Michal Vasko004d3152020-06-11 19:59:22 +02007517 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007518 LY_CHECK_RET(rc);
7519
7520 set->val.scnodes[i].in_ctx = pred_in_ctx;
7521 }
7522
7523 /* restore the state as it was before the predicate */
7524 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01007525 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007526 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007527 } else if (set->val.scnodes[i].in_ctx == pred_in_ctx) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01007528 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007529 }
7530 }
7531
7532 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02007533 set2.type = LYXP_SET_NODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007534 set_fill_set(&set2, set);
7535
Michal Vasko004d3152020-06-11 19:59:22 +02007536 rc = eval_expr_select(exp, tok_idx, 0, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007537 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02007538 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007539 return rc;
7540 }
7541
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007542 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +02007543 if (!set2.val.bln) {
Michal Vaskod3678892020-05-21 10:06:58 +02007544 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007545 }
Michal Vaskod3678892020-05-21 10:06:58 +02007546 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007547 }
7548
7549 /* ']' */
Michal Vasko004d3152020-06-11 19:59:22 +02007550 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_BRACK2);
aPiecek8b0cc152021-05-31 16:40:31 +02007551 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02007552 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007553 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007554
7555 return LY_SUCCESS;
7556}
7557
7558/**
Michal Vaskod3678892020-05-21 10:06:58 +02007559 * @brief Evaluate Literal. Logs directly on error.
7560 *
7561 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007562 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02007563 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7564 */
7565static void
Michal Vasko40308e72020-10-20 16:38:40 +02007566eval_literal(const struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set)
Michal Vaskod3678892020-05-21 10:06:58 +02007567{
7568 if (set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007569 if (exp->tok_len[*tok_idx] == 2) {
Michal Vaskod3678892020-05-21 10:06:58 +02007570 set_fill_string(set, "", 0);
7571 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007572 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 +02007573 }
7574 }
7575 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02007576 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007577 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007578}
7579
7580/**
Michal Vasko3d969ff2022-07-29 15:02:08 +02007581 * @brief Check that a nametest in a predicate matches a key node.
7582 *
7583 * @param[in] nametest Nametest to check.
7584 * @param[in] len Length of @p nametest.
7585 * @param[in] ctx_scnode Found schema node as the context for the predicate.
7586 * @param[in] set Context set.
7587 * @param[in] key Expected key node.
7588 * @return LY_SUCCESS on success,
7589 * @return LY_ENOT if a predicate could not be compiled.
7590 * @return LY_ERR on any error.
7591 */
7592static LY_ERR
7593eval_name_test_try_compile_predicate_key(const char *nametest, uint32_t len, const struct lysc_node *ctx_scnode,
7594 const struct lyxp_set *set, const struct lysc_node *key)
7595{
7596 const struct lys_module *mod;
7597
7598 /* prefix (module) */
7599 LY_CHECK_RET(moveto_resolve_model(&nametest, &len, set, ctx_scnode, &mod));
7600 if (mod != key->module) {
7601 return LY_ENOT;
7602 }
7603
7604 /* node name */
7605 if (ly_strncmp(key->name, nametest, len)) {
7606 return LY_ENOT;
7607 }
7608
7609 return LY_SUCCESS;
7610}
7611
7612/**
7613 * @brief Append a simple predicate for the node.
7614 *
7615 * @param[in] exp Full parsed XPath expression.
7616 * @param[in] tok_idx Predicate start index in @p exp.
7617 * @param[in] end_tok_idx Predicate end index in @p exp.
7618 * @param[in] ctx_scnode Found schema node as the context for the predicate.
7619 * @param[in] set Context set.
7620 * @param[in] pred_node Node with the value referenced in the predicate.
7621 * @param[in,out] pred Predicate to append to.
7622 * @param[in,out] pred_len Length of @p pred, is updated.
7623 * @return LY_SUCCESS on success,
7624 * @return LY_ENOT if a predicate could not be compiled.
7625 * @return LY_ERR on any error.
7626 */
7627static LY_ERR
7628eval_name_test_try_compile_predicate_append(const struct lyxp_expr *exp, uint16_t tok_idx, uint16_t end_tok_idx,
7629 const struct lysc_node *ctx_scnode, const struct lyxp_set *set, const struct lysc_node *pred_node, char **pred,
7630 uint32_t *pred_len)
7631{
7632 LY_ERR rc = LY_SUCCESS;
7633 uint32_t i;
7634 const struct lyd_node *siblings;
7635 struct lyd_node *ctx_node;
Michal Vasko5fb92a22022-08-02 09:21:37 +02007636 const struct lysc_node *sparent, *cur_scnode;
Michal Vasko3d969ff2022-07-29 15:02:08 +02007637 struct lyxp_expr *val_exp = NULL;
7638 struct lyxp_set set2 = {0};
7639 char quot;
7640
7641 /* duplicate the value expression */
7642 LY_CHECK_GOTO(rc = lyxp_expr_dup(set->ctx, exp, tok_idx, end_tok_idx, &val_exp), cleanup);
7643
7644 /* get its atoms */
Michal Vasko5fb92a22022-08-02 09:21:37 +02007645 cur_scnode = set->cur_node ? set->cur_node->schema : NULL;
7646 LY_CHECK_GOTO(rc = lyxp_atomize(set->ctx, val_exp, set->cur_mod, set->format, set->prefix_data, cur_scnode,
7647 ctx_scnode, &set2, LYXP_SCNODE), cleanup);
Michal Vasko3d969ff2022-07-29 15:02:08 +02007648
7649 /* check whether we can compile a single predicate (evaluation result value is always the same) */
7650 for (i = 0; i < set2.used; ++i) {
7651 if ((set2.val.scnodes[i].type != LYXP_NODE_ELEM) || (set2.val.scnodes[i].in_ctx < LYXP_SET_SCNODE_ATOM_NODE)) {
7652 /* skip root and context node */
7653 continue;
7654 }
7655
Michal Vasko5fb92a22022-08-02 09:21:37 +02007656 /* 1) context node descendants are traversed - do best-effort detection of the value dependency on the
7657 * context node instance */
7658 if ((set2.val.scnodes[i].axis == LYXP_AXIS_CHILD) && (set2.val.scnodes[i].scnode->parent == ctx_scnode)) {
7659 /* 1.1) context node child was accessed on the child axis, certain dependency */
Michal Vasko3d969ff2022-07-29 15:02:08 +02007660 rc = LY_ENOT;
7661 goto cleanup;
7662 }
Michal Vasko5fb92a22022-08-02 09:21:37 +02007663 if ((set2.val.scnodes[i].axis == LYXP_AXIS_DESCENDANT) || (set2.val.scnodes[i].axis == LYXP_AXIS_DESCENDANT_OR_SELF)) {
7664 for (sparent = set2.val.scnodes[i].scnode->parent; sparent && (sparent != ctx_scnode); sparent = sparent->parent) {}
7665 if (sparent) {
7666 /* 1.2) context node descendant was accessed on the descendant axis, probable dependency */
7667 rc = LY_ENOT;
7668 goto cleanup;
7669 }
7670 }
Michal Vasko3d969ff2022-07-29 15:02:08 +02007671
Michal Vasko5fb92a22022-08-02 09:21:37 +02007672 /* 2) multi-instance nodes (list or leaf-list) are traversed - all the instances need to be considered,
7673 * but the current node can be safely ignored, it is always the same data instance */
7674 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 +02007675 rc = LY_ENOT;
7676 goto cleanup;
7677 }
Michal Vasko3d969ff2022-07-29 15:02:08 +02007678 }
7679
7680 /* get any data instance of the context node, we checked it makes no difference */
7681 siblings = set->val.nodes[0].node ? lyd_child(set->val.nodes[0].node) : set->tree;
7682 LY_CHECK_GOTO(rc = lyd_find_sibling_schema(siblings, ctx_scnode, &ctx_node), cleanup);
7683
7684 /* evaluate the value subexpression with the root context node */
7685 lyxp_set_free_content(&set2);
7686 LY_CHECK_GOTO(rc = lyxp_eval(set->ctx, val_exp, set->cur_mod, set->format, set->prefix_data, set->cur_node,
7687 ctx_node, set->tree, NULL, &set2, 0), cleanup);
7688
7689 /* cast it into a string */
7690 LY_CHECK_GOTO(rc = lyxp_set_cast(&set2, LYXP_SET_STRING), cleanup);
7691
7692 /* append the JSON predicate */
7693 *pred = ly_realloc(*pred, *pred_len + 1 + strlen(pred_node->name) + 2 + strlen(set2.val.str) + 3);
7694 LY_CHECK_ERR_GOTO(!*pred, LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
7695 quot = strchr(set2.val.str, '\'') ? '\"' : '\'';
7696 *pred_len += sprintf(*pred + *pred_len, "[%s=%c%s%c]", pred_node->name, quot, set2.val.str, quot);
7697
7698cleanup:
7699 lyxp_expr_free(set->ctx, val_exp);
7700 lyxp_set_free_content(&set2);
7701 return rc;
7702}
7703
7704/**
Michal Vasko004d3152020-06-11 19:59:22 +02007705 * @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 +02007706 *
Michal Vasko004d3152020-06-11 19:59:22 +02007707 * @param[in] exp Full parsed XPath expression.
7708 * @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 +02007709 * @param[in] ctx_scnode Found schema node as the context for the predicate.
7710 * @param[in] set Context set.
Michal Vasko004d3152020-06-11 19:59:22 +02007711 * @param[out] predicates Parsed predicates.
7712 * @param[out] pred_type Type of @p predicates.
7713 * @return LY_SUCCESS on success,
Michal Vasko3d969ff2022-07-29 15:02:08 +02007714 * @return LY_ENOT if a predicate could not be compiled.
Michal Vasko004d3152020-06-11 19:59:22 +02007715 * @return LY_ERR on any error.
Michal Vaskod3678892020-05-21 10:06:58 +02007716 */
7717static LY_ERR
Michal Vasko3d969ff2022-07-29 15:02:08 +02007718eval_name_test_try_compile_predicates(const struct lyxp_expr *exp, uint16_t *tok_idx, const struct lysc_node *ctx_scnode,
7719 const struct lyxp_set *set, struct ly_path_predicate **predicates, enum ly_path_pred_type *pred_type)
Michal Vaskod3678892020-05-21 10:06:58 +02007720{
Michal Vasko3d969ff2022-07-29 15:02:08 +02007721 LY_ERR rc = LY_SUCCESS;
7722 uint16_t e_idx, val_start_idx, pred_idx = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02007723 const struct lysc_node *key;
Michal Vasko3d969ff2022-07-29 15:02:08 +02007724 uint32_t prev_lo, pred_len = 0;
7725 char *pred = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02007726 struct lyxp_expr *exp2 = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02007727
Michal Vasko3d969ff2022-07-29 15:02:08 +02007728 assert(ctx_scnode->nodetype & (LYS_LIST | LYS_LEAFLIST));
Michal Vaskod3678892020-05-21 10:06:58 +02007729
Michal Vasko3d969ff2022-07-29 15:02:08 +02007730 /* turn logging off */
7731 prev_lo = ly_log_options(0);
7732
7733 if (ctx_scnode->nodetype == LYS_LIST) {
7734 /* check for predicates "[key1=...][key2=...]..." */
7735
Michal Vasko004d3152020-06-11 19:59:22 +02007736 /* get key count */
Michal Vasko3d969ff2022-07-29 15:02:08 +02007737 if (ctx_scnode->flags & LYS_KEYLESS) {
7738 rc = LY_ENOT;
7739 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +02007740 }
Michal Vaskod3678892020-05-21 10:06:58 +02007741
Michal Vasko004d3152020-06-11 19:59:22 +02007742 /* learn where the predicates end */
7743 e_idx = *tok_idx;
Michal Vasko3d969ff2022-07-29 15:02:08 +02007744 for (key = lysc_node_child(ctx_scnode); key && (key->flags & LYS_KEY); key = key->next) {
Michal Vasko004d3152020-06-11 19:59:22 +02007745 /* '[' */
7746 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
Michal Vasko3d969ff2022-07-29 15:02:08 +02007747 rc = LY_ENOT;
7748 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +02007749 }
7750 ++e_idx;
7751
Michal Vasko3354d272021-04-06 09:40:06 +02007752 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_NAMETEST)) {
Michal Vasko3d969ff2022-07-29 15:02:08 +02007753 /* not a key */
7754 rc = LY_ENOT;
7755 goto cleanup;
Michal Vasko3354d272021-04-06 09:40:06 +02007756 }
7757
Michal Vasko3d969ff2022-07-29 15:02:08 +02007758 /* check key */
7759 LY_CHECK_GOTO(rc = eval_name_test_try_compile_predicate_key(exp->expr + exp->tok_pos[e_idx],
7760 exp->tok_len[e_idx], ctx_scnode, set, key), cleanup);
7761
7762 ++e_idx;
7763
7764 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_OPER_EQUAL)) {
7765 /* not '=' */
7766 rc = LY_ENOT;
7767 goto cleanup;
7768 }
7769 ++e_idx;
7770
7771 /* value start */
7772 val_start_idx = e_idx;
7773
Michal Vasko004d3152020-06-11 19:59:22 +02007774 /* ']' */
7775 while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) {
Michal Vaskob8ee3562022-08-02 10:43:17 +02007776 if (!lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_OPER_LOG)) {
7777 /* higher priority than '=' */
7778 rc = LY_ENOT;
7779 goto cleanup;
7780 }
Michal Vasko004d3152020-06-11 19:59:22 +02007781 ++e_idx;
7782 }
Michal Vasko004d3152020-06-11 19:59:22 +02007783
Michal Vasko3d969ff2022-07-29 15:02:08 +02007784 /* try to evaluate the value */
7785 LY_CHECK_GOTO(rc = eval_name_test_try_compile_predicate_append(exp, val_start_idx, e_idx - 1, ctx_scnode,
7786 set, key, &pred, &pred_len), cleanup);
7787
7788 ++e_idx;
Michal Vasko004d3152020-06-11 19:59:22 +02007789 }
Michal Vasko004d3152020-06-11 19:59:22 +02007790 } else {
Michal Vasko3d969ff2022-07-29 15:02:08 +02007791 /* check for predicate "[.=...]" */
7792
Michal Vasko004d3152020-06-11 19:59:22 +02007793 /* learn just where this single predicate ends */
7794 e_idx = *tok_idx;
7795
Michal Vaskod3678892020-05-21 10:06:58 +02007796 /* '[' */
Michal Vasko004d3152020-06-11 19:59:22 +02007797 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
Michal Vasko3d969ff2022-07-29 15:02:08 +02007798 rc = LY_ENOT;
7799 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +02007800 }
7801 ++e_idx;
Michal Vaskod3678892020-05-21 10:06:58 +02007802
Michal Vasko3354d272021-04-06 09:40:06 +02007803 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_DOT)) {
Michal Vasko3d969ff2022-07-29 15:02:08 +02007804 /* not the node value */
7805 rc = LY_ENOT;
7806 goto cleanup;
Michal Vasko3354d272021-04-06 09:40:06 +02007807 }
Michal Vasko3d969ff2022-07-29 15:02:08 +02007808 ++e_idx;
7809
7810 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_OPER_EQUAL)) {
7811 /* not '=' */
7812 rc = LY_ENOT;
7813 goto cleanup;
7814 }
7815 ++e_idx;
7816
7817 /* value start */
7818 val_start_idx = e_idx;
Michal Vasko3354d272021-04-06 09:40:06 +02007819
Michal Vaskod3678892020-05-21 10:06:58 +02007820 /* ']' */
Michal Vasko004d3152020-06-11 19:59:22 +02007821 while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) {
Michal Vaskob8ee3562022-08-02 10:43:17 +02007822 if (!lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_OPER_LOG)) {
7823 /* higher priority than '=' */
7824 rc = LY_ENOT;
7825 goto cleanup;
7826 }
Michal Vasko004d3152020-06-11 19:59:22 +02007827 ++e_idx;
7828 }
Michal Vasko3d969ff2022-07-29 15:02:08 +02007829
7830 /* try to evaluate the value */
7831 LY_CHECK_GOTO(rc = eval_name_test_try_compile_predicate_append(exp, val_start_idx, e_idx - 1, ctx_scnode, set,
7832 ctx_scnode, &pred, &pred_len), cleanup);
7833
Michal Vasko004d3152020-06-11 19:59:22 +02007834 ++e_idx;
Michal Vaskod3678892020-05-21 10:06:58 +02007835 }
7836
Michal Vasko004d3152020-06-11 19:59:22 +02007837 /* parse the predicate(s) */
Michal Vasko3d969ff2022-07-29 15:02:08 +02007838 LY_CHECK_GOTO(rc = ly_path_parse_predicate(set->ctx, ctx_scnode, pred, pred_len, LY_PATH_PREFIX_OPTIONAL,
7839 LY_PATH_PRED_SIMPLE, &exp2), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02007840
7841 /* compile */
Michal Vasko3d969ff2022-07-29 15:02:08 +02007842 rc = ly_path_compile_predicate(set->ctx, set->cur_node ? set->cur_node->schema : NULL, set->cur_mod, ctx_scnode, exp2,
7843 &pred_idx, LY_VALUE_JSON, NULL, predicates, pred_type);
7844 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02007845
7846 /* success, the predicate must include all the needed information for hash-based search */
7847 *tok_idx = e_idx;
7848
7849cleanup:
7850 ly_log_options(prev_lo);
Michal Vasko3d969ff2022-07-29 15:02:08 +02007851 lyxp_expr_free(set->ctx, exp2);
7852 free(pred);
7853 return rc;
Michal Vaskod3678892020-05-21 10:06:58 +02007854}
7855
7856/**
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007857 * @brief Search for/check the next schema node that could be the only matching schema node meaning the
7858 * data node(s) could be found using a single hash-based search.
7859 *
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007860 * @param[in] ctx libyang context.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007861 * @param[in] node Next context node to check.
7862 * @param[in] name Expected node name.
7863 * @param[in] name_len Length of @p name.
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007864 * @param[in] moveto_mod Expected node module, can be NULL for JSON format with no prefix.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007865 * @param[in] root_type XPath root type.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007866 * @param[in] format Prefix format.
7867 * @param[in,out] found Previously found node, is updated.
7868 * @return LY_SUCCESS on success,
7869 * @return LY_ENOT if the whole check failed and hashes cannot be used.
7870 */
7871static LY_ERR
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007872eval_name_test_with_predicate_get_scnode(const struct ly_ctx *ctx, const struct lyd_node *node, const char *name,
Radek Krejci8df109d2021-04-23 12:19:08 +02007873 uint16_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 +02007874 const struct lysc_node **found)
7875{
7876 const struct lysc_node *scnode;
7877 const struct lys_module *mod;
7878 uint32_t idx = 0;
7879
Radek Krejci8df109d2021-04-23 12:19:08 +02007880 assert((format == LY_VALUE_JSON) || moveto_mod);
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007881
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007882continue_search:
Michal Vasko7d1d0912020-10-16 08:38:30 +02007883 scnode = NULL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007884 if (!node) {
Radek Krejci8df109d2021-04-23 12:19:08 +02007885 if ((format == LY_VALUE_JSON) && !moveto_mod) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007886 /* search all modules for a single match */
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007887 while ((mod = ly_ctx_get_module_iter(ctx, &idx))) {
Michal Vasko35a3b1d2021-07-14 09:34:16 +02007888 if (!mod->implemented) {
7889 continue;
7890 }
7891
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007892 scnode = lys_find_child(NULL, mod, name, name_len, 0, 0);
7893 if (scnode) {
7894 /* we have found a match */
7895 break;
7896 }
7897 }
7898
Michal Vasko7d1d0912020-10-16 08:38:30 +02007899 if (!scnode) {
7900 /* all modules searched */
7901 idx = 0;
7902 }
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007903 } else {
7904 /* search in top-level */
7905 scnode = lys_find_child(NULL, moveto_mod, name, name_len, 0, 0);
7906 }
7907 } else if (!*found || (lysc_data_parent(*found) != node->schema)) {
Radek Krejci8df109d2021-04-23 12:19:08 +02007908 if ((format == LY_VALUE_JSON) && !moveto_mod) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007909 /* we must adjust the module to inherit the one from the context node */
7910 moveto_mod = node->schema->module;
7911 }
7912
7913 /* search in children, do not repeat the same search */
7914 scnode = lys_find_child(node->schema, moveto_mod, name, name_len, 0, 0);
Michal Vasko7d1d0912020-10-16 08:38:30 +02007915 } /* else skip redundant search */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007916
7917 /* additional context check */
7918 if (scnode && (root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) {
7919 scnode = NULL;
7920 }
7921
7922 if (scnode) {
7923 if (*found) {
7924 /* we found a schema node with the same name but at different level, give up, too complicated
7925 * (more hash-based searches would be required, not supported) */
7926 return LY_ENOT;
7927 } else {
7928 /* remember the found schema node and continue to make sure it can be used */
7929 *found = scnode;
7930 }
7931 }
7932
7933 if (idx) {
7934 /* continue searching all the following models */
7935 goto continue_search;
7936 }
7937
7938 return LY_SUCCESS;
7939}
7940
7941/**
Michal Vasko4ad69e72021-10-26 16:25:55 +02007942 * @brief Generate message when no matching schema nodes were found for a path segment.
7943 *
7944 * @param[in] set XPath set.
7945 * @param[in] scparent Previous schema parent in the context, if only one.
7946 * @param[in] ncname XPath NCName being evaluated.
7947 * @param[in] ncname_len Length of @p ncname.
7948 * @param[in] expr Whole XPath expression.
7949 * @param[in] options XPath options.
7950 */
7951static void
7952eval_name_test_scnode_no_match_msg(struct lyxp_set *set, const struct lyxp_set_scnode *scparent, const char *ncname,
7953 uint16_t ncname_len, const char *expr, uint32_t options)
7954{
7955 const char *format;
7956 char *path = NULL, *ppath = NULL;
7957
7958 path = lysc_path(set->cur_scnode, LYSC_PATH_LOG, NULL, 0);
7959 if (scparent) {
7960 /* generate path for the parent */
7961 if (scparent->type == LYXP_NODE_ELEM) {
7962 ppath = lysc_path(scparent->scnode, LYSC_PATH_LOG, NULL, 0);
7963 } else if (scparent->type == LYXP_NODE_ROOT) {
7964 ppath = strdup("<root>");
7965 } else if (scparent->type == LYXP_NODE_ROOT_CONFIG) {
7966 ppath = strdup("<config-root>");
7967 }
7968 }
7969 if (ppath) {
7970 format = "Schema node \"%.*s\" for parent \"%s\" not found; in expr \"%.*s\" with context node \"%s\".";
7971 if (options & LYXP_SCNODE_ERROR) {
7972 LOGERR(set->ctx, LY_EVALID, format, ncname_len, ncname, ppath, (ncname - expr) + ncname_len, expr, path);
7973 } else {
7974 LOGWRN(set->ctx, format, ncname_len, ncname, ppath, (ncname - expr) + ncname_len, expr, path);
7975 }
7976 } else {
7977 format = "Schema node \"%.*s\" not found; in expr \"%.*s\" with context node \"%s\".";
7978 if (options & LYXP_SCNODE_ERROR) {
7979 LOGERR(set->ctx, LY_EVALID, format, ncname_len, ncname, (ncname - expr) + ncname_len, expr, path);
7980 } else {
7981 LOGWRN(set->ctx, format, ncname_len, ncname, (ncname - expr) + ncname_len, expr, path);
7982 }
7983 }
7984 free(path);
7985 free(ppath);
7986}
7987
7988/**
Michal Vaskod3678892020-05-21 10:06:58 +02007989 * @brief Evaluate NameTest and any following Predicates. Logs directly on error.
7990 *
7991 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
7992 * [6] NodeTest ::= NameTest | NodeType '(' ')'
7993 * [7] NameTest ::= '*' | NCName ':' '*' | QName
7994 *
7995 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007996 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko49fec8e2022-05-24 10:28:33 +02007997 * @param[in] axis What axis to search on.
Michal Vaskod3678892020-05-21 10:06:58 +02007998 * @param[in] all_desc Whether to search all the descendants or children only.
aPiecek8b0cc152021-05-31 16:40:31 +02007999 * @param[in,out] set Context and result set.
Michal Vaskod3678892020-05-21 10:06:58 +02008000 * @param[in] options XPath options.
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008001 * @return LY_ERR (LY_EINCOMPLETE on unresolved when, LY_ENOT for not found schema node)
Michal Vaskod3678892020-05-21 10:06:58 +02008002 */
8003static LY_ERR
Michal Vasko49fec8e2022-05-24 10:28:33 +02008004eval_name_test_with_predicate(const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_axis axis, ly_bool all_desc,
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008005 struct lyxp_set *set, uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02008006{
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008007 LY_ERR rc = LY_SUCCESS, r;
Michal Vasko004d3152020-06-11 19:59:22 +02008008 const char *ncname, *ncname_dict = NULL;
Michal Vasko56c008c2022-07-29 14:57:47 +02008009 uint32_t i, ncname_len;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008010 const struct lys_module *moveto_mod = NULL;
Michal Vaskoc71c37b2021-01-11 13:40:02 +01008011 const struct lysc_node *scnode = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02008012 struct ly_path_predicate *predicates = NULL;
8013 enum ly_path_pred_type pred_type = 0;
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008014 int scnode_skip_pred = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02008015
aPiecek8b0cc152021-05-31 16:40:31 +02008016 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008017 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008018 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02008019
aPiecek8b0cc152021-05-31 16:40:31 +02008020 if (options & LYXP_SKIP_EXPR) {
Michal Vaskod3678892020-05-21 10:06:58 +02008021 goto moveto;
8022 }
8023
Michal Vasko004d3152020-06-11 19:59:22 +02008024 ncname = &exp->expr[exp->tok_pos[*tok_idx - 1]];
8025 ncname_len = exp->tok_len[*tok_idx - 1];
Michal Vaskod3678892020-05-21 10:06:58 +02008026
Michal Vaskoc71c37b2021-01-11 13:40:02 +01008027 if ((ncname[0] == '*') && (ncname_len == 1)) {
8028 /* all nodes will match */
8029 goto moveto;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008030 }
8031
Michal Vaskoc71c37b2021-01-11 13:40:02 +01008032 /* parse (and skip) module name */
8033 rc = moveto_resolve_model(&ncname, &ncname_len, set, NULL, &moveto_mod);
Michal Vaskod3678892020-05-21 10:06:58 +02008034 LY_CHECK_GOTO(rc, cleanup);
8035
Michal Vasko49fec8e2022-05-24 10:28:33 +02008036 if ((ncname[0] == '*') && (ncname_len == 1)) {
8037 /* all nodes from the module will match */
8038 goto moveto;
8039 }
8040
8041 if (((set->format == LY_VALUE_JSON) || moveto_mod) && (axis == LYXP_AXIS_CHILD) && !all_desc &&
8042 (set->type == LYXP_SET_NODE_SET)) {
Michal Vaskod3678892020-05-21 10:06:58 +02008043 /* find the matching schema node in some parent in the context */
Michal Vasko56c008c2022-07-29 14:57:47 +02008044 for (i = 0; i < set->used; ++i) {
Michal Vaskoc71c37b2021-01-11 13:40:02 +01008045 if (eval_name_test_with_predicate_get_scnode(set->ctx, set->val.nodes[i].node, ncname, ncname_len,
8046 moveto_mod, set->root_type, set->format, &scnode)) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008047 /* check failed */
8048 scnode = NULL;
8049 break;
Michal Vaskod3678892020-05-21 10:06:58 +02008050 }
8051 }
8052
Michal Vasko004d3152020-06-11 19:59:22 +02008053 if (scnode && (scnode->nodetype & (LYS_LIST | LYS_LEAFLIST))) {
8054 /* try to create the predicates */
Michal Vasko3d969ff2022-07-29 15:02:08 +02008055 if (eval_name_test_try_compile_predicates(exp, tok_idx, scnode, set, &predicates, &pred_type)) {
Michal Vasko004d3152020-06-11 19:59:22 +02008056 /* hashes cannot be used */
Michal Vaskod3678892020-05-21 10:06:58 +02008057 scnode = NULL;
8058 }
8059 }
8060 }
8061
Michal Vaskoc71c37b2021-01-11 13:40:02 +01008062 if (!scnode) {
8063 /* we are not able to match based on a schema node and not all the modules match ("*"),
Michal Vasko004d3152020-06-11 19:59:22 +02008064 * use dictionary for efficient comparison */
Radek Krejci011e4aa2020-09-04 15:22:31 +02008065 LY_CHECK_GOTO(rc = lydict_insert(set->ctx, ncname, ncname_len, &ncname_dict), cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02008066 }
8067
8068moveto:
8069 /* move to the attribute(s), data node(s), or schema node(s) */
Michal Vasko49fec8e2022-05-24 10:28:33 +02008070 if (axis == LYXP_AXIS_ATTRIBUTE) {
aPiecek8b0cc152021-05-31 16:40:31 +02008071 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008072 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskod3678892020-05-21 10:06:58 +02008073 } else {
8074 if (all_desc) {
Michal Vaskocdad7122020-11-09 21:04:44 +01008075 rc = moveto_attr_alldesc(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02008076 } else {
aPiecek8b0cc152021-05-31 16:40:31 +02008077 rc = moveto_attr(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02008078 }
8079 LY_CHECK_GOTO(rc, cleanup);
8080 }
8081 } else {
aPiecek8b0cc152021-05-31 16:40:31 +02008082 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008083 const struct lyxp_set_scnode *scparent = NULL;
Michal Vasko56c008c2022-07-29 14:57:47 +02008084 ly_bool found = 0;
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008085
8086 /* remember parent if there is only one, to print in the warning */
8087 for (i = 0; i < set->used; ++i) {
8088 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
8089 if (!scparent) {
8090 /* remember the context node */
8091 scparent = &set->val.scnodes[i];
8092 } else {
8093 /* several context nodes, no reasonable error possible */
8094 scparent = NULL;
8095 break;
8096 }
8097 }
8098 }
Radek Krejci1deb5be2020-08-26 16:43:36 +02008099
Michal Vasko49fec8e2022-05-24 10:28:33 +02008100 if (all_desc && (axis == LYXP_AXIS_CHILD)) {
8101 /* efficient evaluation that does not add all the descendants into the set */
8102 rc = moveto_scnode_alldesc_child(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02008103 } else {
Michal Vasko49fec8e2022-05-24 10:28:33 +02008104 if (all_desc) {
8105 /* "//" == "/descendant-or-self::node()/" */
8106 rc = xpath_pi_node(set, LYXP_AXIS_DESCENDANT_OR_SELF, options);
8107 LY_CHECK_GOTO(rc, cleanup);
8108 }
8109 rc = moveto_scnode(set, moveto_mod, ncname_dict, axis, options);
Michal Vaskod3678892020-05-21 10:06:58 +02008110 }
8111 LY_CHECK_GOTO(rc, cleanup);
8112
Michal Vasko56c008c2022-07-29 14:57:47 +02008113 i = set->used;
8114 do {
8115 --i;
Michal Vasko1a09b212021-05-06 13:00:10 +02008116 if (set->val.scnodes[i].in_ctx > LYXP_SET_SCNODE_ATOM_NODE) {
Michal Vasko56c008c2022-07-29 14:57:47 +02008117 found = 1;
Michal Vaskod3678892020-05-21 10:06:58 +02008118 break;
8119 }
Michal Vasko56c008c2022-07-29 14:57:47 +02008120 } while (i);
8121 if (!found) {
Michal Vasko4ad69e72021-10-26 16:25:55 +02008122 /* generate message */
8123 eval_name_test_scnode_no_match_msg(set, scparent, ncname, ncname_len, exp->expr, options);
8124
8125 if (options & LYXP_SCNODE_ERROR) {
8126 /* error */
8127 rc = LY_EVALID;
8128 goto cleanup;
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008129 }
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008130
8131 /* skip the predicates and the rest of this path to not generate invalid warnings */
8132 rc = LY_ENOT;
8133 scnode_skip_pred = 1;
Michal Vaskod3678892020-05-21 10:06:58 +02008134 }
8135 } else {
Michal Vasko49fec8e2022-05-24 10:28:33 +02008136 if (all_desc && (axis == LYXP_AXIS_CHILD)) {
8137 /* efficient evaluation */
8138 rc = moveto_node_alldesc_child(set, moveto_mod, ncname_dict, options);
8139 } else if (scnode && (axis == LYXP_AXIS_CHILD)) {
8140 /* we can find the child nodes using hashes */
8141 rc = moveto_node_hash_child(set, scnode, predicates, options);
Michal Vaskod3678892020-05-21 10:06:58 +02008142 } else {
Michal Vasko49fec8e2022-05-24 10:28:33 +02008143 if (all_desc) {
8144 /* "//" == "/descendant-or-self::node()/" */
8145 rc = xpath_pi_node(set, LYXP_AXIS_DESCENDANT_OR_SELF, options);
8146 LY_CHECK_GOTO(rc, cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02008147 }
Michal Vasko49fec8e2022-05-24 10:28:33 +02008148 rc = moveto_node(set, moveto_mod, ncname_dict, axis, options);
Michal Vaskod3678892020-05-21 10:06:58 +02008149 }
8150 LY_CHECK_GOTO(rc, cleanup);
8151 }
8152 }
8153
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008154 if (scnode_skip_pred) {
8155 /* skip predicates */
8156 options |= LYXP_SKIP_EXPR;
8157 }
8158
Michal Vaskod3678892020-05-21 10:06:58 +02008159 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02008160 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02008161 r = eval_predicate(exp, tok_idx, set, options, axis);
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008162 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02008163 }
8164
8165cleanup:
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008166 if (scnode_skip_pred) {
8167 /* restore options */
8168 options &= ~LYXP_SKIP_EXPR;
8169 }
aPiecek8b0cc152021-05-31 16:40:31 +02008170 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko004d3152020-06-11 19:59:22 +02008171 lydict_remove(set->ctx, ncname_dict);
Michal Vaskof7e16e22020-10-21 09:24:39 +02008172 ly_path_predicates_free(set->ctx, pred_type, predicates);
Michal Vaskodb51a8d2020-05-27 15:22:29 +02008173 }
Michal Vaskod3678892020-05-21 10:06:58 +02008174 return rc;
8175}
8176
8177/**
8178 * @brief Evaluate NodeType and any following Predicates. Logs directly on error.
8179 *
8180 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
8181 * [6] NodeTest ::= NameTest | NodeType '(' ')'
8182 * [8] NodeType ::= 'text' | 'node'
8183 *
8184 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008185 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko49fec8e2022-05-24 10:28:33 +02008186 * @param[in] axis Axis to search on.
8187 * @param[in] all_desc Whether to search all the descendants or axis only.
aPiecek8b0cc152021-05-31 16:40:31 +02008188 * @param[in,out] set Context and result set.
Michal Vaskod3678892020-05-21 10:06:58 +02008189 * @param[in] options XPath options.
8190 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8191 */
8192static LY_ERR
Michal Vasko49fec8e2022-05-24 10:28:33 +02008193eval_node_type_with_predicate(const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_axis axis, ly_bool all_desc,
Radek Krejci1deb5be2020-08-26 16:43:36 +02008194 struct lyxp_set *set, uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02008195{
8196 LY_ERR rc;
8197
Michal Vaskod3678892020-05-21 10:06:58 +02008198 (void)all_desc;
8199
aPiecek8b0cc152021-05-31 16:40:31 +02008200 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko004d3152020-06-11 19:59:22 +02008201 assert(exp->tok_len[*tok_idx] == 4);
Michal Vasko49fec8e2022-05-24 10:28:33 +02008202 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "node", 4)) {
8203 rc = xpath_pi_node(set, axis, options);
Michal Vaskod3678892020-05-21 10:06:58 +02008204 } else {
Michal Vasko49fec8e2022-05-24 10:28:33 +02008205 assert(!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "text", 4));
8206 rc = xpath_pi_text(set, axis, options);
Michal Vaskod3678892020-05-21 10:06:58 +02008207 }
Michal Vasko49fec8e2022-05-24 10:28:33 +02008208 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02008209 }
aPiecek8b0cc152021-05-31 16:40:31 +02008210 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008211 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008212 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02008213
8214 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02008215 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
aPiecek8b0cc152021-05-31 16:40:31 +02008216 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008217 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008218 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02008219
8220 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02008221 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
aPiecek8b0cc152021-05-31 16:40:31 +02008222 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008223 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008224 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02008225
8226 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02008227 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02008228 rc = eval_predicate(exp, tok_idx, set, options, axis);
Michal Vaskod3678892020-05-21 10:06:58 +02008229 LY_CHECK_RET(rc);
8230 }
8231
8232 return LY_SUCCESS;
8233}
8234
8235/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02008236 * @brief Evaluate RelativeLocationPath. Logs directly on error.
8237 *
8238 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
8239 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
Michal Vaskod3678892020-05-21 10:06:58 +02008240 * [6] NodeTest ::= NameTest | NodeType '(' ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02008241 *
8242 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008243 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008244 * @param[in] all_desc Whether to search all the descendants or children only.
aPiecek8b0cc152021-05-31 16:40:31 +02008245 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008246 * @param[in] options XPath options.
8247 * @return LY_ERR (YL_EINCOMPLETE on unresolved when)
8248 */
8249static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008250eval_relative_location_path(const struct lyxp_expr *exp, uint16_t *tok_idx, ly_bool all_desc, struct lyxp_set *set,
8251 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008252{
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008253 LY_ERR rc = LY_SUCCESS;
Michal Vasko49fec8e2022-05-24 10:28:33 +02008254 enum lyxp_axis axis;
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008255 int scnode_skip_path = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008256
8257 goto step;
8258 do {
8259 /* evaluate '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02008260 if (exp->tok_len[*tok_idx] == 1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008261 all_desc = 0;
8262 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02008263 assert(exp->tok_len[*tok_idx] == 2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008264 all_desc = 1;
8265 }
aPiecek8b0cc152021-05-31 16:40:31 +02008266 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008267 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008268 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008269
8270step:
Michal Vasko49fec8e2022-05-24 10:28:33 +02008271 /* AxisSpecifier */
8272 if (exp->tokens[*tok_idx] == LYXP_TOKEN_AXISNAME) {
8273 axis = str2axis(exp->expr + exp->tok_pos[*tok_idx], exp->tok_len[*tok_idx]);
Michal Vaskod3678892020-05-21 10:06:58 +02008274
aPiecek8b0cc152021-05-31 16:40:31 +02008275 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008276 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
8277 ++(*tok_idx);
8278
8279 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_DCOLON);
8280 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
8281 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
8282 ++(*tok_idx);
8283 } else if (exp->tokens[*tok_idx] == LYXP_TOKEN_AT) {
8284 axis = LYXP_AXIS_ATTRIBUTE;
8285
8286 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
8287 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008288 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02008289 } else {
Michal Vasko49fec8e2022-05-24 10:28:33 +02008290 /* default */
8291 axis = LYXP_AXIS_CHILD;
Michal Vaskod3678892020-05-21 10:06:58 +02008292 }
8293
Michal Vasko49fec8e2022-05-24 10:28:33 +02008294 /* NodeTest Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02008295 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008296 case LYXP_TOKEN_DOT:
8297 /* evaluate '.' */
Michal Vasko49fec8e2022-05-24 10:28:33 +02008298 if (!(options & LYXP_SKIP_EXPR)) {
8299 if (((options & LYXP_SCNODE_ALL) && (set->type != LYXP_SET_SCNODE_SET)) ||
8300 (!(options & LYXP_SCNODE_ALL) && (set->type != LYXP_SET_NODE_SET))) {
8301 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
8302 rc = LY_EVALID;
8303 goto cleanup;
8304 }
8305
8306 if (all_desc) {
8307 rc = xpath_pi_node(set, LYXP_AXIS_DESCENDANT_OR_SELF, options);
8308 LY_CHECK_GOTO(rc, cleanup);
8309 }
8310 rc = xpath_pi_node(set, LYXP_AXIS_SELF, options);
8311 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008312 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008313 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008314 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008315 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008316 break;
8317
8318 case LYXP_TOKEN_DDOT:
8319 /* evaluate '..' */
Michal Vasko49fec8e2022-05-24 10:28:33 +02008320 if (!(options & LYXP_SKIP_EXPR)) {
8321 if (((options & LYXP_SCNODE_ALL) && (set->type != LYXP_SET_SCNODE_SET)) ||
8322 (!(options & LYXP_SCNODE_ALL) && (set->type != LYXP_SET_NODE_SET))) {
8323 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
8324 rc = LY_EVALID;
8325 goto cleanup;
8326 }
8327
8328 if (all_desc) {
8329 rc = xpath_pi_node(set, LYXP_AXIS_DESCENDANT_OR_SELF, options);
8330 LY_CHECK_GOTO(rc, cleanup);
8331 }
8332 rc = xpath_pi_node(set, LYXP_AXIS_PARENT, options);
8333 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008334 }
aPiecek8b0cc152021-05-31 16:40:31 +02008335 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008336 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008337 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008338 break;
8339
Michal Vasko03ff5a72019-09-11 13:49:33 +02008340 case LYXP_TOKEN_NAMETEST:
Michal Vaskod3678892020-05-21 10:06:58 +02008341 /* evaluate NameTest Predicate* */
Michal Vasko49fec8e2022-05-24 10:28:33 +02008342 rc = eval_name_test_with_predicate(exp, tok_idx, axis, all_desc, set, options);
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008343 if (rc == LY_ENOT) {
8344 assert(options & LYXP_SCNODE_ALL);
8345 /* skip the rest of this path */
8346 rc = LY_SUCCESS;
8347 scnode_skip_path = 1;
8348 options |= LYXP_SKIP_EXPR;
8349 }
8350 LY_CHECK_GOTO(rc, cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02008351 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008352
Michal Vaskod3678892020-05-21 10:06:58 +02008353 case LYXP_TOKEN_NODETYPE:
8354 /* evaluate NodeType Predicate* */
Michal Vasko49fec8e2022-05-24 10:28:33 +02008355 rc = eval_node_type_with_predicate(exp, tok_idx, axis, all_desc, set, options);
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008356 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008357 break;
8358
8359 default:
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008360 LOGINT(set->ctx);
8361 rc = LY_EINT;
8362 goto cleanup;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008363 }
Michal Vasko004d3152020-06-11 19:59:22 +02008364 } while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008365
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008366cleanup:
8367 if (scnode_skip_path) {
8368 options &= ~LYXP_SKIP_EXPR;
8369 }
8370 return rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008371}
8372
8373/**
8374 * @brief Evaluate AbsoluteLocationPath. Logs directly on error.
8375 *
8376 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
8377 *
8378 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008379 * @param[in] tok_idx Position in the expression @p exp.
aPiecek8b0cc152021-05-31 16:40:31 +02008380 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008381 * @param[in] options XPath options.
8382 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8383 */
8384static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008385eval_absolute_location_path(const struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008386{
Radek Krejci857189e2020-09-01 13:26:36 +02008387 ly_bool all_desc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008388
aPiecek8b0cc152021-05-31 16:40:31 +02008389 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008390 /* no matter what tokens follow, we need to be at the root */
Michal Vaskob0099a92020-08-31 14:55:23 +02008391 LY_CHECK_RET(moveto_root(set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008392 }
8393
8394 /* '/' RelativeLocationPath? */
Michal Vasko004d3152020-06-11 19:59:22 +02008395 if (exp->tok_len[*tok_idx] == 1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008396 /* evaluate '/' - deferred */
8397 all_desc = 0;
aPiecek8b0cc152021-05-31 16:40:31 +02008398 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008399 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008400 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008401
Michal Vasko004d3152020-06-11 19:59:22 +02008402 if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008403 return LY_SUCCESS;
8404 }
Michal Vasko004d3152020-06-11 19:59:22 +02008405 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008406 case LYXP_TOKEN_DOT:
8407 case LYXP_TOKEN_DDOT:
Michal Vasko49fec8e2022-05-24 10:28:33 +02008408 case LYXP_TOKEN_AXISNAME:
Michal Vasko03ff5a72019-09-11 13:49:33 +02008409 case LYXP_TOKEN_AT:
8410 case LYXP_TOKEN_NAMETEST:
8411 case LYXP_TOKEN_NODETYPE:
Michal Vaskob0099a92020-08-31 14:55:23 +02008412 LY_CHECK_RET(eval_relative_location_path(exp, tok_idx, all_desc, set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008413 break;
8414 default:
8415 break;
8416 }
8417
Michal Vasko03ff5a72019-09-11 13:49:33 +02008418 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02008419 /* '//' RelativeLocationPath */
Michal Vasko03ff5a72019-09-11 13:49:33 +02008420 /* evaluate '//' - deferred so as not to waste memory by remembering all the nodes */
8421 all_desc = 1;
aPiecek8b0cc152021-05-31 16:40:31 +02008422 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008423 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008424 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008425
Michal Vaskob0099a92020-08-31 14:55:23 +02008426 LY_CHECK_RET(eval_relative_location_path(exp, tok_idx, all_desc, set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008427 }
8428
8429 return LY_SUCCESS;
8430}
8431
8432/**
8433 * @brief Evaluate FunctionCall. Logs directly on error.
8434 *
Michal Vaskod3678892020-05-21 10:06:58 +02008435 * [11] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02008436 *
8437 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008438 * @param[in] tok_idx Position in the expression @p exp.
aPiecek8b0cc152021-05-31 16:40:31 +02008439 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008440 * @param[in] options XPath options.
8441 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8442 */
8443static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008444eval_function_call(const struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008445{
8446 LY_ERR rc;
Michal Vasko69730152020-10-09 16:30:07 +02008447
Radek Krejci1deb5be2020-08-26 16:43:36 +02008448 LY_ERR (*xpath_func)(struct lyxp_set **, uint16_t, struct lyxp_set *, uint32_t) = NULL;
Michal Vasko0cbf54f2019-12-16 10:01:06 +01008449 uint16_t arg_count = 0, i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008450 struct lyxp_set **args = NULL, **args_aux;
8451
aPiecek8b0cc152021-05-31 16:40:31 +02008452 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008453 /* FunctionName */
Michal Vasko004d3152020-06-11 19:59:22 +02008454 switch (exp->tok_len[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008455 case 3:
Michal Vasko004d3152020-06-11 19:59:22 +02008456 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008457 xpath_func = &xpath_not;
Michal Vasko004d3152020-06-11 19:59:22 +02008458 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008459 xpath_func = &xpath_sum;
8460 }
8461 break;
8462 case 4:
Michal Vasko004d3152020-06-11 19:59:22 +02008463 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008464 xpath_func = &xpath_lang;
Michal Vasko004d3152020-06-11 19:59:22 +02008465 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008466 xpath_func = &xpath_last;
Michal Vasko004d3152020-06-11 19:59:22 +02008467 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008468 xpath_func = &xpath_name;
Michal Vasko004d3152020-06-11 19:59:22 +02008469 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008470 xpath_func = &xpath_true;
8471 }
8472 break;
8473 case 5:
Michal Vasko004d3152020-06-11 19:59:22 +02008474 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008475 xpath_func = &xpath_count;
Michal Vasko004d3152020-06-11 19:59:22 +02008476 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008477 xpath_func = &xpath_false;
Michal Vasko004d3152020-06-11 19:59:22 +02008478 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008479 xpath_func = &xpath_floor;
Michal Vasko004d3152020-06-11 19:59:22 +02008480 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008481 xpath_func = &xpath_round;
Michal Vasko004d3152020-06-11 19:59:22 +02008482 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008483 xpath_func = &xpath_deref;
8484 }
8485 break;
8486 case 6:
Michal Vasko004d3152020-06-11 19:59:22 +02008487 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008488 xpath_func = &xpath_concat;
Michal Vasko004d3152020-06-11 19:59:22 +02008489 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008490 xpath_func = &xpath_number;
Michal Vasko004d3152020-06-11 19:59:22 +02008491 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008492 xpath_func = &xpath_string;
8493 }
8494 break;
8495 case 7:
Michal Vasko004d3152020-06-11 19:59:22 +02008496 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008497 xpath_func = &xpath_boolean;
Michal Vasko004d3152020-06-11 19:59:22 +02008498 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008499 xpath_func = &xpath_ceiling;
Michal Vasko004d3152020-06-11 19:59:22 +02008500 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008501 xpath_func = &xpath_current;
8502 }
8503 break;
8504 case 8:
Michal Vasko004d3152020-06-11 19:59:22 +02008505 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008506 xpath_func = &xpath_contains;
Michal Vasko004d3152020-06-11 19:59:22 +02008507 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008508 xpath_func = &xpath_position;
Michal Vasko004d3152020-06-11 19:59:22 +02008509 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "re-match", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008510 xpath_func = &xpath_re_match;
8511 }
8512 break;
8513 case 9:
Michal Vasko004d3152020-06-11 19:59:22 +02008514 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008515 xpath_func = &xpath_substring;
Michal Vasko004d3152020-06-11 19:59:22 +02008516 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008517 xpath_func = &xpath_translate;
8518 }
8519 break;
8520 case 10:
Michal Vasko004d3152020-06-11 19:59:22 +02008521 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008522 xpath_func = &xpath_local_name;
Michal Vasko004d3152020-06-11 19:59:22 +02008523 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008524 xpath_func = &xpath_enum_value;
Michal Vasko004d3152020-06-11 19:59:22 +02008525 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008526 xpath_func = &xpath_bit_is_set;
8527 }
8528 break;
8529 case 11:
Michal Vasko004d3152020-06-11 19:59:22 +02008530 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008531 xpath_func = &xpath_starts_with;
8532 }
8533 break;
8534 case 12:
Michal Vasko004d3152020-06-11 19:59:22 +02008535 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008536 xpath_func = &xpath_derived_from;
8537 }
8538 break;
8539 case 13:
Michal Vasko004d3152020-06-11 19:59:22 +02008540 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008541 xpath_func = &xpath_namespace_uri;
Michal Vasko004d3152020-06-11 19:59:22 +02008542 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string-length", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008543 xpath_func = &xpath_string_length;
8544 }
8545 break;
8546 case 15:
Michal Vasko004d3152020-06-11 19:59:22 +02008547 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008548 xpath_func = &xpath_normalize_space;
Michal Vasko004d3152020-06-11 19:59:22 +02008549 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-after", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008550 xpath_func = &xpath_substring_after;
8551 }
8552 break;
8553 case 16:
Michal Vasko004d3152020-06-11 19:59:22 +02008554 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008555 xpath_func = &xpath_substring_before;
8556 }
8557 break;
8558 case 20:
Michal Vasko004d3152020-06-11 19:59:22 +02008559 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from-or-self", 20)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008560 xpath_func = &xpath_derived_from_or_self;
8561 }
8562 break;
8563 }
8564
8565 if (!xpath_func) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01008566 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 +02008567 return LY_EVALID;
8568 }
8569 }
8570
aPiecek8b0cc152021-05-31 16:40:31 +02008571 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008572 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008573 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008574
8575 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02008576 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
aPiecek8b0cc152021-05-31 16:40:31 +02008577 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008578 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008579 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008580
8581 /* ( Expr ( ',' Expr )* )? */
Michal Vasko004d3152020-06-11 19:59:22 +02008582 if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) {
aPiecek8b0cc152021-05-31 16:40:31 +02008583 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008584 args = malloc(sizeof *args);
8585 LY_CHECK_ERR_GOTO(!args, LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
8586 arg_count = 1;
8587 args[0] = set_copy(set);
8588 if (!args[0]) {
8589 rc = LY_EMEM;
8590 goto cleanup;
8591 }
8592
Michal Vasko004d3152020-06-11 19:59:22 +02008593 rc = eval_expr_select(exp, tok_idx, 0, args[0], options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008594 LY_CHECK_GOTO(rc, cleanup);
8595 } else {
aPiecek8b0cc152021-05-31 16:40:31 +02008596 rc = eval_expr_select(exp, tok_idx, 0, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008597 LY_CHECK_GOTO(rc, cleanup);
8598 }
8599 }
Michal Vasko004d3152020-06-11 19:59:22 +02008600 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) {
aPiecek8b0cc152021-05-31 16:40:31 +02008601 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008602 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008603 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008604
aPiecek8b0cc152021-05-31 16:40:31 +02008605 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008606 ++arg_count;
8607 args_aux = realloc(args, arg_count * sizeof *args);
8608 LY_CHECK_ERR_GOTO(!args_aux, arg_count--; LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
8609 args = args_aux;
8610 args[arg_count - 1] = set_copy(set);
8611 if (!args[arg_count - 1]) {
8612 rc = LY_EMEM;
8613 goto cleanup;
8614 }
8615
Michal Vasko004d3152020-06-11 19:59:22 +02008616 rc = eval_expr_select(exp, tok_idx, 0, args[arg_count - 1], options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008617 LY_CHECK_GOTO(rc, cleanup);
8618 } else {
aPiecek8b0cc152021-05-31 16:40:31 +02008619 rc = eval_expr_select(exp, tok_idx, 0, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008620 LY_CHECK_GOTO(rc, cleanup);
8621 }
8622 }
8623
8624 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02008625 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
aPiecek8b0cc152021-05-31 16:40:31 +02008626 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008627 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008628 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008629
aPiecek8b0cc152021-05-31 16:40:31 +02008630 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008631 /* evaluate function */
8632 rc = xpath_func(args, arg_count, set, options);
8633
8634 if (options & LYXP_SCNODE_ALL) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008635 /* merge all nodes from arg evaluations */
8636 for (i = 0; i < arg_count; ++i) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008637 set_scnode_clear_ctx(args[i], LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008638 lyxp_set_scnode_merge(set, args[i]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008639 }
8640 }
8641 } else {
8642 rc = LY_SUCCESS;
8643 }
8644
8645cleanup:
8646 for (i = 0; i < arg_count; ++i) {
8647 lyxp_set_free(args[i]);
8648 }
8649 free(args);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008650 return rc;
8651}
8652
8653/**
8654 * @brief Evaluate Number. Logs directly on error.
8655 *
8656 * @param[in] ctx Context for errors.
8657 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008658 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008659 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8660 * @return LY_ERR
8661 */
8662static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008663eval_number(struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008664{
8665 long double num;
8666 char *endptr;
8667
8668 if (set) {
8669 errno = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02008670 num = strtold(&exp->expr[exp->tok_pos[*tok_idx]], &endptr);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008671 if (errno) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01008672 LOGVAL(ctx, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]);
8673 LOGVAL(ctx, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double (%s).",
Michal Vasko69730152020-10-09 16:30:07 +02008674 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]], strerror(errno));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008675 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02008676 } else if (endptr - &exp->expr[exp->tok_pos[*tok_idx]] != exp->tok_len[*tok_idx]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01008677 LOGVAL(ctx, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]);
8678 LOGVAL(ctx, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double.",
Michal Vasko69730152020-10-09 16:30:07 +02008679 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008680 return LY_EVALID;
8681 }
8682
8683 set_fill_number(set, num);
8684 }
8685
8686 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008687 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008688 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008689 return LY_SUCCESS;
8690}
8691
aPiecekdf23eee2021-10-07 12:21:50 +02008692LY_ERR
8693lyxp_vars_find(struct lyxp_var *vars, const char *name, size_t name_len, struct lyxp_var **var)
8694{
8695 LY_ERR ret = LY_ENOTFOUND;
8696 LY_ARRAY_COUNT_TYPE u;
8697
8698 assert(vars && name);
8699
8700 name_len = name_len ? name_len : strlen(name);
8701
8702 LY_ARRAY_FOR(vars, u) {
8703 if (!strncmp(vars[u].name, name, name_len)) {
8704 ret = LY_SUCCESS;
8705 break;
8706 }
8707 }
8708
8709 if (var && !ret) {
8710 *var = &vars[u];
8711 }
8712
8713 return ret;
8714}
8715
Michal Vasko03ff5a72019-09-11 13:49:33 +02008716/**
aPiecekfba75362021-10-07 12:39:48 +02008717 * @brief Evaluate VariableReference.
8718 *
8719 * @param[in] exp Parsed XPath expression.
8720 * @param[in] tok_idx Position in the expression @p exp.
8721 * @param[in] vars [Sized array](@ref sizedarrays) of XPath variables.
8722 * @param[in,out] set Context and result set.
8723 * @param[in] options XPath options.
8724 * @return LY_ERR value.
8725 */
8726static LY_ERR
8727eval_variable_reference(const struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set, uint32_t options)
8728{
8729 LY_ERR ret;
8730 const char *name;
8731 struct lyxp_var *var;
8732 const struct lyxp_var *vars;
8733 struct lyxp_expr *tokens = NULL;
Michal Vaskoe68b5402022-07-29 14:58:15 +02008734 uint16_t token_index, name_len;
aPiecekfba75362021-10-07 12:39:48 +02008735
8736 vars = set->vars;
8737
Michal Vasko49fec8e2022-05-24 10:28:33 +02008738 /* find out the name and value of the variable */
aPiecekfba75362021-10-07 12:39:48 +02008739 name = &exp->expr[exp->tok_pos[*tok_idx]];
Michal Vaskoe68b5402022-07-29 14:58:15 +02008740 name_len = exp->tok_len[*tok_idx];
8741 ret = lyxp_vars_find((struct lyxp_var *)vars, name, name_len, &var);
8742 LY_CHECK_ERR_RET(ret, LOGERR(set->ctx, ret, "XPath variable \"%.*s\" not defined.", (int)name_len, name), ret);
aPiecekfba75362021-10-07 12:39:48 +02008743
Michal Vasko49fec8e2022-05-24 10:28:33 +02008744 /* parse value */
aPiecekfba75362021-10-07 12:39:48 +02008745 ret = lyxp_expr_parse(set->ctx, var->value, 0, 1, &tokens);
8746 LY_CHECK_GOTO(ret, cleanup);
8747
Michal Vasko49fec8e2022-05-24 10:28:33 +02008748 /* evaluate value */
aPiecekfba75362021-10-07 12:39:48 +02008749 token_index = 0;
8750 ret = eval_expr_select(tokens, &token_index, 0, set, options);
8751 LY_CHECK_GOTO(ret, cleanup);
8752
8753cleanup:
8754 lyxp_expr_free(set->ctx, tokens);
8755
8756 return ret;
8757}
8758
8759/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02008760 * @brief Evaluate PathExpr. Logs directly on error.
8761 *
Michal Vaskod3678892020-05-21 10:06:58 +02008762 * [12] PathExpr ::= LocationPath | PrimaryExpr Predicate*
Michal Vasko03ff5a72019-09-11 13:49:33 +02008763 * | PrimaryExpr Predicate* '/' RelativeLocationPath
8764 * | PrimaryExpr Predicate* '//' RelativeLocationPath
8765 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
aPiecekfba75362021-10-07 12:39:48 +02008766 * [10] PrimaryExpr ::= VariableReference | '(' Expr ')' | Literal | Number | FunctionCall
Michal Vasko03ff5a72019-09-11 13:49:33 +02008767 *
8768 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008769 * @param[in] tok_idx Position in the expression @p exp.
aPiecek8b0cc152021-05-31 16:40:31 +02008770 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008771 * @param[in] options XPath options.
8772 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8773 */
8774static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008775eval_path_expr(const struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008776{
Michal Vasko49fec8e2022-05-24 10:28:33 +02008777 ly_bool all_desc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008778 LY_ERR rc;
8779
Michal Vasko004d3152020-06-11 19:59:22 +02008780 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008781 case LYXP_TOKEN_PAR1:
8782 /* '(' Expr ')' */
8783
8784 /* '(' */
aPiecek8b0cc152021-05-31 16:40:31 +02008785 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008786 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008787 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008788
8789 /* Expr */
Michal Vasko004d3152020-06-11 19:59:22 +02008790 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008791 LY_CHECK_RET(rc);
8792
8793 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02008794 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
aPiecek8b0cc152021-05-31 16:40:31 +02008795 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008796 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008797 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008798
Michal Vasko03ff5a72019-09-11 13:49:33 +02008799 goto predicate;
8800
8801 case LYXP_TOKEN_DOT:
8802 case LYXP_TOKEN_DDOT:
Michal Vasko49fec8e2022-05-24 10:28:33 +02008803 case LYXP_TOKEN_AXISNAME:
Michal Vasko03ff5a72019-09-11 13:49:33 +02008804 case LYXP_TOKEN_AT:
8805 case LYXP_TOKEN_NAMETEST:
8806 case LYXP_TOKEN_NODETYPE:
8807 /* RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02008808 rc = eval_relative_location_path(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008809 LY_CHECK_RET(rc);
8810 break;
8811
aPiecekfba75362021-10-07 12:39:48 +02008812 case LYXP_TOKEN_VARREF:
8813 /* VariableReference */
8814 rc = eval_variable_reference(exp, tok_idx, set, options);
8815 LY_CHECK_RET(rc);
8816 ++(*tok_idx);
8817
aPiecekfba75362021-10-07 12:39:48 +02008818 goto predicate;
8819
Michal Vasko03ff5a72019-09-11 13:49:33 +02008820 case LYXP_TOKEN_FUNCNAME:
8821 /* FunctionCall */
aPiecek8b0cc152021-05-31 16:40:31 +02008822 rc = eval_function_call(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008823 LY_CHECK_RET(rc);
8824
Michal Vasko03ff5a72019-09-11 13:49:33 +02008825 goto predicate;
8826
Michal Vasko3e48bf32020-06-01 08:39:07 +02008827 case LYXP_TOKEN_OPER_PATH:
8828 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +02008829 /* AbsoluteLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02008830 rc = eval_absolute_location_path(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008831 LY_CHECK_RET(rc);
8832 break;
8833
8834 case LYXP_TOKEN_LITERAL:
8835 /* Literal */
aPiecek8b0cc152021-05-31 16:40:31 +02008836 if ((options & LYXP_SKIP_EXPR) || (options & LYXP_SCNODE_ALL)) {
8837 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008838 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008839 }
Michal Vasko004d3152020-06-11 19:59:22 +02008840 eval_literal(exp, tok_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008841 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02008842 eval_literal(exp, tok_idx, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008843 }
8844
Michal Vasko03ff5a72019-09-11 13:49:33 +02008845 goto predicate;
8846
8847 case LYXP_TOKEN_NUMBER:
8848 /* Number */
aPiecek8b0cc152021-05-31 16:40:31 +02008849 if ((options & LYXP_SKIP_EXPR) || (options & LYXP_SCNODE_ALL)) {
8850 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008851 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008852 }
Michal Vasko004d3152020-06-11 19:59:22 +02008853 rc = eval_number(NULL, exp, tok_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008854 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02008855 rc = eval_number(set->ctx, exp, tok_idx, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008856 }
8857 LY_CHECK_RET(rc);
8858
Michal Vasko03ff5a72019-09-11 13:49:33 +02008859 goto predicate;
8860
8861 default:
Michal Vasko49fec8e2022-05-24 10:28:33 +02008862 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 +02008863 return LY_EVALID;
8864 }
8865
8866 return LY_SUCCESS;
8867
8868predicate:
8869 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02008870 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02008871 rc = eval_predicate(exp, tok_idx, set, options, LYXP_AXIS_CHILD);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008872 LY_CHECK_RET(rc);
8873 }
8874
8875 /* ('/' or '//') RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02008876 if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008877
8878 /* evaluate '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02008879 if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008880 all_desc = 0;
8881 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008882 all_desc = 1;
8883 }
8884
aPiecek8b0cc152021-05-31 16:40:31 +02008885 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008886 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008887 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008888
Michal Vasko004d3152020-06-11 19:59:22 +02008889 rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008890 LY_CHECK_RET(rc);
8891 }
8892
8893 return LY_SUCCESS;
8894}
8895
8896/**
8897 * @brief Evaluate UnionExpr. Logs directly on error.
8898 *
Michal Vaskod3678892020-05-21 10:06:58 +02008899 * [20] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008900 *
8901 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008902 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008903 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008904 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008905 * @param[in] options XPath options.
8906 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8907 */
8908static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008909eval_union_expr(const struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008910{
8911 LY_ERR rc = LY_SUCCESS;
8912 struct lyxp_set orig_set, set2;
8913 uint16_t i;
8914
8915 assert(repeat);
8916
8917 set_init(&orig_set, set);
8918 set_init(&set2, set);
8919
8920 set_fill_set(&orig_set, set);
8921
Michal Vasko004d3152020-06-11 19:59:22 +02008922 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008923 LY_CHECK_GOTO(rc, cleanup);
8924
8925 /* ('|' PathExpr)* */
8926 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008927 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_UNI);
aPiecek8b0cc152021-05-31 16:40:31 +02008928 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008929 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008930 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008931
aPiecek8b0cc152021-05-31 16:40:31 +02008932 if (options & LYXP_SKIP_EXPR) {
8933 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008934 LY_CHECK_GOTO(rc, cleanup);
8935 continue;
8936 }
8937
8938 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008939 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008940 LY_CHECK_GOTO(rc, cleanup);
8941
8942 /* eval */
8943 if (options & LYXP_SCNODE_ALL) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01008944 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008945 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008946 rc = moveto_union(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008947 LY_CHECK_GOTO(rc, cleanup);
8948 }
8949 }
8950
8951cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008952 lyxp_set_free_content(&orig_set);
8953 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008954 return rc;
8955}
8956
8957/**
8958 * @brief Evaluate UnaryExpr. Logs directly on error.
8959 *
Michal Vaskod3678892020-05-21 10:06:58 +02008960 * [19] UnaryExpr ::= UnionExpr | '-' UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008961 *
8962 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008963 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008964 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008965 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008966 * @param[in] options XPath options.
8967 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8968 */
8969static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008970eval_unary_expr(const struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008971{
8972 LY_ERR rc;
8973 uint16_t this_op, i;
8974
8975 assert(repeat);
8976
8977 /* ('-')+ */
Michal Vasko004d3152020-06-11 19:59:22 +02008978 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008979 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008980 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 +02008981
aPiecek8b0cc152021-05-31 16:40:31 +02008982 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008983 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008984 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008985 }
8986
Michal Vasko004d3152020-06-11 19:59:22 +02008987 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNARY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008988 LY_CHECK_RET(rc);
8989
aPiecek8b0cc152021-05-31 16:40:31 +02008990 if (!(options & LYXP_SKIP_EXPR) && (repeat % 2)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008991 if (options & LYXP_SCNODE_ALL) {
8992 warn_operands(set->ctx, set, NULL, 1, exp->expr, exp->tok_pos[this_op]);
8993 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008994 rc = moveto_op_math(set, NULL, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008995 LY_CHECK_RET(rc);
8996 }
8997 }
8998
8999 return LY_SUCCESS;
9000}
9001
9002/**
9003 * @brief Evaluate MultiplicativeExpr. Logs directly on error.
9004 *
Michal Vaskod3678892020-05-21 10:06:58 +02009005 * [18] MultiplicativeExpr ::= UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02009006 * | MultiplicativeExpr '*' UnaryExpr
9007 * | MultiplicativeExpr 'div' UnaryExpr
9008 * | MultiplicativeExpr 'mod' UnaryExpr
9009 *
9010 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02009011 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009012 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02009013 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009014 * @param[in] options XPath options.
9015 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
9016 */
9017static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02009018eval_multiplicative_expr(const struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set,
9019 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02009020{
9021 LY_ERR rc;
9022 uint16_t this_op;
9023 struct lyxp_set orig_set, set2;
9024 uint16_t i;
9025
9026 assert(repeat);
9027
9028 set_init(&orig_set, set);
9029 set_init(&set2, set);
9030
9031 set_fill_set(&orig_set, set);
9032
Michal Vasko004d3152020-06-11 19:59:22 +02009033 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009034 LY_CHECK_GOTO(rc, cleanup);
9035
9036 /* ('*' / 'div' / 'mod' UnaryExpr)* */
9037 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02009038 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009039
Michal Vasko004d3152020-06-11 19:59:22 +02009040 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH);
aPiecek8b0cc152021-05-31 16:40:31 +02009041 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02009042 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02009043 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009044
aPiecek8b0cc152021-05-31 16:40:31 +02009045 if (options & LYXP_SKIP_EXPR) {
9046 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009047 LY_CHECK_GOTO(rc, cleanup);
9048 continue;
9049 }
9050
9051 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02009052 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009053 LY_CHECK_GOTO(rc, cleanup);
9054
9055 /* eval */
9056 if (options & LYXP_SCNODE_ALL) {
9057 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01009058 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02009059 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009060 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009061 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009062 LY_CHECK_GOTO(rc, cleanup);
9063 }
9064 }
9065
9066cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02009067 lyxp_set_free_content(&orig_set);
9068 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009069 return rc;
9070}
9071
9072/**
9073 * @brief Evaluate AdditiveExpr. Logs directly on error.
9074 *
Michal Vaskod3678892020-05-21 10:06:58 +02009075 * [17] AdditiveExpr ::= MultiplicativeExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02009076 * | AdditiveExpr '+' MultiplicativeExpr
9077 * | AdditiveExpr '-' MultiplicativeExpr
9078 *
9079 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02009080 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009081 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02009082 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009083 * @param[in] options XPath options.
9084 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
9085 */
9086static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02009087eval_additive_expr(const struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02009088{
9089 LY_ERR rc;
9090 uint16_t this_op;
9091 struct lyxp_set orig_set, set2;
9092 uint16_t i;
9093
9094 assert(repeat);
9095
9096 set_init(&orig_set, set);
9097 set_init(&set2, set);
9098
9099 set_fill_set(&orig_set, set);
9100
Michal Vasko004d3152020-06-11 19:59:22 +02009101 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009102 LY_CHECK_GOTO(rc, cleanup);
9103
9104 /* ('+' / '-' MultiplicativeExpr)* */
9105 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02009106 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009107
Michal Vasko004d3152020-06-11 19:59:22 +02009108 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009109 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02009110 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02009111 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009112
aPiecek8b0cc152021-05-31 16:40:31 +02009113 if (options & LYXP_SKIP_EXPR) {
9114 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009115 LY_CHECK_GOTO(rc, cleanup);
9116 continue;
9117 }
9118
9119 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02009120 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009121 LY_CHECK_GOTO(rc, cleanup);
9122
9123 /* eval */
9124 if (options & LYXP_SCNODE_ALL) {
9125 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01009126 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02009127 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009128 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009129 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009130 LY_CHECK_GOTO(rc, cleanup);
9131 }
9132 }
9133
9134cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02009135 lyxp_set_free_content(&orig_set);
9136 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009137 return rc;
9138}
9139
9140/**
9141 * @brief Evaluate RelationalExpr. Logs directly on error.
9142 *
Michal Vaskod3678892020-05-21 10:06:58 +02009143 * [16] RelationalExpr ::= AdditiveExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02009144 * | RelationalExpr '<' AdditiveExpr
9145 * | RelationalExpr '>' AdditiveExpr
9146 * | RelationalExpr '<=' AdditiveExpr
9147 * | RelationalExpr '>=' AdditiveExpr
9148 *
9149 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02009150 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009151 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02009152 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009153 * @param[in] options XPath options.
9154 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
9155 */
9156static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02009157eval_relational_expr(const struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02009158{
9159 LY_ERR rc;
9160 uint16_t this_op;
9161 struct lyxp_set orig_set, set2;
9162 uint16_t i;
9163
9164 assert(repeat);
9165
9166 set_init(&orig_set, set);
9167 set_init(&set2, set);
9168
9169 set_fill_set(&orig_set, set);
9170
Michal Vasko004d3152020-06-11 19:59:22 +02009171 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009172 LY_CHECK_GOTO(rc, cleanup);
9173
9174 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
9175 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02009176 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009177
Michal Vasko004d3152020-06-11 19:59:22 +02009178 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_COMP);
aPiecek8b0cc152021-05-31 16:40:31 +02009179 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02009180 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02009181 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009182
aPiecek8b0cc152021-05-31 16:40:31 +02009183 if (options & LYXP_SKIP_EXPR) {
9184 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009185 LY_CHECK_GOTO(rc, cleanup);
9186 continue;
9187 }
9188
9189 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02009190 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009191 LY_CHECK_GOTO(rc, cleanup);
9192
9193 /* eval */
9194 if (options & LYXP_SCNODE_ALL) {
9195 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01009196 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02009197 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009198 } else {
Michal Vasko8abcecc2022-07-28 09:55:01 +02009199 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009200 LY_CHECK_GOTO(rc, cleanup);
9201 }
9202 }
9203
9204cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02009205 lyxp_set_free_content(&orig_set);
9206 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009207 return rc;
9208}
9209
9210/**
9211 * @brief Evaluate EqualityExpr. Logs directly on error.
9212 *
Michal Vaskod3678892020-05-21 10:06:58 +02009213 * [15] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02009214 * | EqualityExpr '!=' RelationalExpr
9215 *
9216 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02009217 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009218 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02009219 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009220 * @param[in] options XPath options.
9221 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
9222 */
9223static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02009224eval_equality_expr(const struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02009225{
9226 LY_ERR rc;
9227 uint16_t this_op;
9228 struct lyxp_set orig_set, set2;
9229 uint16_t i;
9230
9231 assert(repeat);
9232
9233 set_init(&orig_set, set);
9234 set_init(&set2, set);
9235
9236 set_fill_set(&orig_set, set);
9237
Michal Vasko004d3152020-06-11 19:59:22 +02009238 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009239 LY_CHECK_GOTO(rc, cleanup);
9240
9241 /* ('=' / '!=' RelationalExpr)* */
9242 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02009243 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009244
Michal Vasko004d3152020-06-11 19:59:22 +02009245 assert((exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_EQUAL) || (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_NEQUAL));
aPiecek8b0cc152021-05-31 16:40:31 +02009246 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02009247 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02009248 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009249
aPiecek8b0cc152021-05-31 16:40:31 +02009250 if (options & LYXP_SKIP_EXPR) {
9251 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009252 LY_CHECK_GOTO(rc, cleanup);
9253 continue;
9254 }
9255
9256 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02009257 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009258 LY_CHECK_GOTO(rc, cleanup);
9259
9260 /* eval */
9261 if (options & LYXP_SCNODE_ALL) {
9262 warn_operands(set->ctx, set, &set2, 0, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vasko004d3152020-06-11 19:59:22 +02009263 warn_equality_value(exp, set, *tok_idx - 1, this_op - 1, *tok_idx - 1);
9264 warn_equality_value(exp, &set2, this_op - 1, this_op - 1, *tok_idx - 1);
Michal Vaskoecd62de2019-11-13 12:35:11 +01009265 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02009266 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009267 } else {
Michal Vasko8abcecc2022-07-28 09:55:01 +02009268 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009269 LY_CHECK_GOTO(rc, cleanup);
9270 }
9271 }
9272
9273cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02009274 lyxp_set_free_content(&orig_set);
9275 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009276 return rc;
9277}
9278
9279/**
9280 * @brief Evaluate AndExpr. Logs directly on error.
9281 *
Michal Vaskod3678892020-05-21 10:06:58 +02009282 * [14] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02009283 *
9284 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02009285 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009286 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02009287 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009288 * @param[in] options XPath options.
9289 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
9290 */
9291static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02009292eval_and_expr(const struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02009293{
9294 LY_ERR rc;
9295 struct lyxp_set orig_set, set2;
9296 uint16_t i;
9297
9298 assert(repeat);
9299
9300 set_init(&orig_set, set);
9301 set_init(&set2, set);
9302
9303 set_fill_set(&orig_set, set);
9304
Michal Vasko004d3152020-06-11 19:59:22 +02009305 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009306 LY_CHECK_GOTO(rc, cleanup);
9307
9308 /* cast to boolean, we know that will be the final result */
aPiecek8b0cc152021-05-31 16:40:31 +02009309 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02009310 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009311 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009312 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009313 }
9314
9315 /* ('and' EqualityExpr)* */
9316 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02009317 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG);
aPiecek8b0cc152021-05-31 16:40:31 +02009318 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, ((options & LYXP_SKIP_EXPR) || !set->val.bln ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02009319 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02009320 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009321
9322 /* lazy evaluation */
aPiecek8b0cc152021-05-31 16:40:31 +02009323 if ((options & LYXP_SKIP_EXPR) || ((set->type == LYXP_SET_BOOLEAN) && !set->val.bln)) {
9324 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009325 LY_CHECK_GOTO(rc, cleanup);
9326 continue;
9327 }
9328
9329 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02009330 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009331 LY_CHECK_GOTO(rc, cleanup);
9332
9333 /* eval - just get boolean value actually */
9334 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vasko1a09b212021-05-06 13:00:10 +02009335 set_scnode_clear_ctx(&set2, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskoecd62de2019-11-13 12:35:11 +01009336 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009337 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009338 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009339 set_fill_set(set, &set2);
9340 }
9341 }
9342
9343cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02009344 lyxp_set_free_content(&orig_set);
9345 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009346 return rc;
9347}
9348
9349/**
9350 * @brief Evaluate OrExpr. Logs directly on error.
9351 *
Michal Vaskod3678892020-05-21 10:06:58 +02009352 * [13] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02009353 *
9354 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02009355 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009356 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02009357 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009358 * @param[in] options XPath options.
9359 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
9360 */
9361static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02009362eval_or_expr(const struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02009363{
9364 LY_ERR rc;
9365 struct lyxp_set orig_set, set2;
9366 uint16_t i;
9367
9368 assert(repeat);
9369
9370 set_init(&orig_set, set);
9371 set_init(&set2, set);
9372
9373 set_fill_set(&orig_set, set);
9374
Michal Vasko004d3152020-06-11 19:59:22 +02009375 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009376 LY_CHECK_GOTO(rc, cleanup);
9377
9378 /* cast to boolean, we know that will be the final result */
aPiecek8b0cc152021-05-31 16:40:31 +02009379 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02009380 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009381 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009382 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009383 }
9384
9385 /* ('or' AndExpr)* */
9386 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02009387 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG);
aPiecek8b0cc152021-05-31 16:40:31 +02009388 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, ((options & LYXP_SKIP_EXPR) || set->val.bln ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02009389 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02009390 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009391
9392 /* lazy evaluation */
aPiecek8b0cc152021-05-31 16:40:31 +02009393 if ((options & LYXP_SKIP_EXPR) || ((set->type == LYXP_SET_BOOLEAN) && set->val.bln)) {
9394 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009395 LY_CHECK_GOTO(rc, cleanup);
9396 continue;
9397 }
9398
9399 set_fill_set(&set2, &orig_set);
9400 /* expr_type cound have been LYXP_EXPR_NONE in all these later calls (except for the first one),
9401 * but it does not matter */
Michal Vasko004d3152020-06-11 19:59:22 +02009402 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009403 LY_CHECK_GOTO(rc, cleanup);
9404
9405 /* eval - just get boolean value actually */
9406 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vasko1a09b212021-05-06 13:00:10 +02009407 set_scnode_clear_ctx(&set2, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskoecd62de2019-11-13 12:35:11 +01009408 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009409 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009410 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009411 set_fill_set(set, &set2);
9412 }
9413 }
9414
9415cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02009416 lyxp_set_free_content(&orig_set);
9417 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009418 return rc;
9419}
9420
9421/**
Michal Vasko004d3152020-06-11 19:59:22 +02009422 * @brief Decide what expression is at the pointer @p tok_idx and evaluate it accordingly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009423 *
9424 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02009425 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009426 * @param[in] etype Expression type to evaluate.
aPiecek8b0cc152021-05-31 16:40:31 +02009427 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009428 * @param[in] options XPath options.
9429 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
9430 */
9431static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02009432eval_expr_select(const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_expr_type etype, struct lyxp_set *set,
9433 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02009434{
9435 uint16_t i, count;
9436 enum lyxp_expr_type next_etype;
9437 LY_ERR rc;
9438
9439 /* process operator repeats */
Michal Vasko004d3152020-06-11 19:59:22 +02009440 if (!exp->repeat[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02009441 next_etype = LYXP_EXPR_NONE;
9442 } else {
9443 /* find etype repeat */
Radek Krejci1e008d22020-08-17 11:37:37 +02009444 for (i = 0; exp->repeat[*tok_idx][i] > etype; ++i) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02009445
9446 /* select one-priority lower because etype expression called us */
9447 if (i) {
Michal Vasko004d3152020-06-11 19:59:22 +02009448 next_etype = exp->repeat[*tok_idx][i - 1];
Michal Vasko03ff5a72019-09-11 13:49:33 +02009449 /* count repeats for that expression */
Radek Krejci1e008d22020-08-17 11:37:37 +02009450 for (count = 0; i && exp->repeat[*tok_idx][i - 1] == next_etype; ++count, --i) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02009451 } else {
9452 next_etype = LYXP_EXPR_NONE;
9453 }
9454 }
9455
9456 /* decide what expression are we parsing based on the repeat */
9457 switch (next_etype) {
9458 case LYXP_EXPR_OR:
Michal Vasko004d3152020-06-11 19:59:22 +02009459 rc = eval_or_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009460 break;
9461 case LYXP_EXPR_AND:
Michal Vasko004d3152020-06-11 19:59:22 +02009462 rc = eval_and_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009463 break;
9464 case LYXP_EXPR_EQUALITY:
Michal Vasko004d3152020-06-11 19:59:22 +02009465 rc = eval_equality_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009466 break;
9467 case LYXP_EXPR_RELATIONAL:
Michal Vasko004d3152020-06-11 19:59:22 +02009468 rc = eval_relational_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009469 break;
9470 case LYXP_EXPR_ADDITIVE:
Michal Vasko004d3152020-06-11 19:59:22 +02009471 rc = eval_additive_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009472 break;
9473 case LYXP_EXPR_MULTIPLICATIVE:
Michal Vasko004d3152020-06-11 19:59:22 +02009474 rc = eval_multiplicative_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009475 break;
9476 case LYXP_EXPR_UNARY:
Michal Vasko004d3152020-06-11 19:59:22 +02009477 rc = eval_unary_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009478 break;
9479 case LYXP_EXPR_UNION:
Michal Vasko004d3152020-06-11 19:59:22 +02009480 rc = eval_union_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009481 break;
9482 case LYXP_EXPR_NONE:
Michal Vasko004d3152020-06-11 19:59:22 +02009483 rc = eval_path_expr(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009484 break;
9485 default:
9486 LOGINT_RET(set->ctx);
9487 }
9488
9489 return rc;
9490}
9491
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009492/**
9493 * @brief Get root type.
9494 *
9495 * @param[in] ctx_node Context node.
9496 * @param[in] ctx_scnode Schema context node.
9497 * @param[in] options XPath options.
9498 * @return Root type.
9499 */
9500static enum lyxp_node_type
Radek Krejci1deb5be2020-08-26 16:43:36 +02009501lyxp_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 +01009502{
Michal Vasko6b26e742020-07-17 15:02:10 +02009503 const struct lysc_node *op;
9504
Michal Vaskoa27245c2022-05-02 09:01:35 +02009505 /* explicit */
9506 if (options & LYXP_ACCESS_TREE_ALL) {
9507 return LYXP_NODE_ROOT;
9508 } else if (options & LYXP_ACCESS_TREE_CONFIG) {
9509 return LYXP_NODE_ROOT_CONFIG;
9510 }
9511
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009512 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009513 /* schema */
Radek Krejci1e008d22020-08-17 11:37:37 +02009514 for (op = ctx_scnode; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent) {}
Michal Vasko6b26e742020-07-17 15:02:10 +02009515
9516 if (op || (options & LYXP_SCNODE)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009517 /* general root that can access everything */
9518 return LYXP_NODE_ROOT;
9519 } else if (!ctx_scnode || (ctx_scnode->flags & LYS_CONFIG_W)) {
9520 /* root context node can access only config data (because we said so, it is unspecified) */
9521 return LYXP_NODE_ROOT_CONFIG;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009522 }
Michal Vasko6b26e742020-07-17 15:02:10 +02009523 return LYXP_NODE_ROOT;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009524 }
9525
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009526 /* data */
Michal Vasko6b26e742020-07-17 15:02:10 +02009527 op = ctx_node ? ctx_node->schema : NULL;
Michal Vaskod989ba02020-08-24 10:59:24 +02009528 for ( ; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent) {}
Michal Vasko6b26e742020-07-17 15:02:10 +02009529
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009530 if (op || !(options & LYXP_SCHEMA)) {
9531 /* general root that can access everything */
9532 return LYXP_NODE_ROOT;
Christian Hoppsb6ecaea2021-02-06 09:45:38 -05009533 } else if (!ctx_node || !ctx_node->schema || (ctx_node->schema->flags & LYS_CONFIG_W)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009534 /* root context node can access only config data (because we said so, it is unspecified) */
9535 return LYXP_NODE_ROOT_CONFIG;
9536 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009537 return LYXP_NODE_ROOT;
9538}
9539
Michal Vasko03ff5a72019-09-11 13:49:33 +02009540LY_ERR
Michal Vasko400e9672021-01-11 13:39:17 +01009541lyxp_eval(const struct ly_ctx *ctx, const struct lyxp_expr *exp, const struct lys_module *cur_mod,
Michal Vaskoa3e92bc2022-07-29 14:56:23 +02009542 LY_VALUE_FORMAT format, void *prefix_data, const struct lyd_node *cur_node, const struct lyd_node *ctx_node,
9543 const struct lyd_node *tree, const struct lyxp_var *vars, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02009544{
Michal Vasko004d3152020-06-11 19:59:22 +02009545 uint16_t tok_idx = 0;
Michal Vaskoddd76592022-01-17 13:34:48 +01009546 const struct lysc_node *snode;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009547 LY_ERR rc;
9548
Michal Vasko400e9672021-01-11 13:39:17 +01009549 LY_CHECK_ARG_RET(ctx, ctx, exp, set, LY_EINVAL);
Radek Krejci8df109d2021-04-23 12:19:08 +02009550 if (!cur_mod && ((format == LY_VALUE_SCHEMA) || (format == LY_VALUE_SCHEMA_RESOLVED))) {
Michal Vaskoddd76592022-01-17 13:34:48 +01009551 LOGERR(ctx, LY_EINVAL, "Current module must be set if schema format is used.");
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009552 return LY_EINVAL;
Michal Vasko004d3152020-06-11 19:59:22 +02009553 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02009554
Michal Vaskod3bb12f2020-12-04 14:33:09 +01009555 if (tree) {
9556 /* adjust the pointer to be the first top-level sibling */
9557 while (tree->parent) {
9558 tree = lyd_parent(tree);
9559 }
9560 tree = lyd_first_sibling(tree);
Michal Vaskoddd76592022-01-17 13:34:48 +01009561
9562 for (snode = tree->schema->parent; snode && (snode->nodetype & (LYS_CASE | LYS_CHOICE)); snode = snode->parent) {}
9563 if (snode) {
9564 /* unable to evaluate absolute paths */
9565 LOGERR(ctx, LY_EINVAL, "Data node \"%s\" has no parent but is not instance of a top-level schema node.",
9566 LYD_NAME(tree));
9567 return LY_EINVAL;
9568 }
Michal Vaskod3bb12f2020-12-04 14:33:09 +01009569 }
9570
Michal Vasko03ff5a72019-09-11 13:49:33 +02009571 /* prepare set for evaluation */
Michal Vasko03ff5a72019-09-11 13:49:33 +02009572 memset(set, 0, sizeof *set);
Michal Vaskod3678892020-05-21 10:06:58 +02009573 set->type = LYXP_SET_NODE_SET;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009574 set->root_type = lyxp_get_root_type(ctx_node, NULL, options);
9575 set_insert_node(set, (struct lyd_node *)ctx_node, 0, ctx_node ? LYXP_NODE_ELEM : set->root_type, 0);
9576
Michal Vasko400e9672021-01-11 13:39:17 +01009577 set->ctx = (struct ly_ctx *)ctx;
Michal Vaskoa3e92bc2022-07-29 14:56:23 +02009578 set->cur_node = cur_node;
9579 for (set->context_op = cur_node ? cur_node->schema : NULL;
Radek Krejci0f969882020-08-21 16:56:47 +02009580 set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF));
9581 set->context_op = set->context_op->parent) {}
Michal Vaskof03ed032020-03-04 13:31:44 +01009582 set->tree = tree;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009583 set->cur_mod = cur_mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009584 set->format = format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009585 set->prefix_data = prefix_data;
aPiecekfba75362021-10-07 12:39:48 +02009586 set->vars = vars;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009587
Radek Krejciddace2c2021-01-08 11:30:56 +01009588 LOG_LOCSET(NULL, set->cur_node, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01009589
Michal Vasko03ff5a72019-09-11 13:49:33 +02009590 /* evaluate */
Michal Vasko004d3152020-06-11 19:59:22 +02009591 rc = eval_expr_select(exp, &tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009592 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02009593 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009594 }
9595
Michal Vasko4a7d4d62021-12-13 17:05:06 +01009596 if (set->cur_node) {
9597 LOG_LOCBACK(0, 1, 0, 0);
9598 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02009599 return rc;
9600}
9601
9602#if 0
9603
9604/* full xml printing of set elements, not used currently */
9605
9606void
9607lyxp_set_print_xml(FILE *f, struct lyxp_set *set)
9608{
9609 uint32_t i;
9610 char *str_num;
9611 struct lyout out;
9612
9613 memset(&out, 0, sizeof out);
9614
9615 out.type = LYOUT_STREAM;
9616 out.method.f = f;
9617
9618 switch (set->type) {
9619 case LYXP_SET_EMPTY:
Michal Vasko5233e962020-08-14 14:26:20 +02009620 ly_print_(&out, "Empty XPath set\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02009621 break;
9622 case LYXP_SET_BOOLEAN:
Michal Vasko5233e962020-08-14 14:26:20 +02009623 ly_print_(&out, "Boolean XPath set:\n");
9624 ly_print_(&out, "%s\n\n", set->value.bool ? "true" : "false");
Michal Vasko03ff5a72019-09-11 13:49:33 +02009625 break;
9626 case LYXP_SET_STRING:
Michal Vasko5233e962020-08-14 14:26:20 +02009627 ly_print_(&out, "String XPath set:\n");
9628 ly_print_(&out, "\"%s\"\n\n", set->value.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009629 break;
9630 case LYXP_SET_NUMBER:
Michal Vasko5233e962020-08-14 14:26:20 +02009631 ly_print_(&out, "Number XPath set:\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02009632
9633 if (isnan(set->value.num)) {
9634 str_num = strdup("NaN");
9635 } else if ((set->value.num == 0) || (set->value.num == -0.0f)) {
9636 str_num = strdup("0");
9637 } else if (isinf(set->value.num) && !signbit(set->value.num)) {
9638 str_num = strdup("Infinity");
9639 } else if (isinf(set->value.num) && signbit(set->value.num)) {
9640 str_num = strdup("-Infinity");
9641 } else if ((long long)set->value.num == set->value.num) {
9642 if (asprintf(&str_num, "%lld", (long long)set->value.num) == -1) {
9643 str_num = NULL;
9644 }
9645 } else {
9646 if (asprintf(&str_num, "%03.1Lf", set->value.num) == -1) {
9647 str_num = NULL;
9648 }
9649 }
9650 if (!str_num) {
9651 LOGMEM;
9652 return;
9653 }
Michal Vasko5233e962020-08-14 14:26:20 +02009654 ly_print_(&out, "%s\n\n", str_num);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009655 free(str_num);
9656 break;
9657 case LYXP_SET_NODE_SET:
Michal Vasko5233e962020-08-14 14:26:20 +02009658 ly_print_(&out, "Node XPath set:\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02009659
9660 for (i = 0; i < set->used; ++i) {
Michal Vasko5233e962020-08-14 14:26:20 +02009661 ly_print_(&out, "%d. ", i + 1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009662 switch (set->node_type[i]) {
9663 case LYXP_NODE_ROOT_ALL:
Michal Vasko5233e962020-08-14 14:26:20 +02009664 ly_print_(&out, "ROOT all\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02009665 break;
9666 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko5233e962020-08-14 14:26:20 +02009667 ly_print_(&out, "ROOT config\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02009668 break;
9669 case LYXP_NODE_ROOT_STATE:
Michal Vasko5233e962020-08-14 14:26:20 +02009670 ly_print_(&out, "ROOT state\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02009671 break;
9672 case LYXP_NODE_ROOT_NOTIF:
Michal Vasko5233e962020-08-14 14:26:20 +02009673 ly_print_(&out, "ROOT notification \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009674 break;
9675 case LYXP_NODE_ROOT_RPC:
Michal Vasko5233e962020-08-14 14:26:20 +02009676 ly_print_(&out, "ROOT rpc \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009677 break;
9678 case LYXP_NODE_ROOT_OUTPUT:
Michal Vasko5233e962020-08-14 14:26:20 +02009679 ly_print_(&out, "ROOT output \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009680 break;
9681 case LYXP_NODE_ELEM:
Michal Vasko5233e962020-08-14 14:26:20 +02009682 ly_print_(&out, "ELEM \"%s\"\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009683 xml_print_node(&out, 1, set->value.nodes[i], 1, LYP_FORMAT);
Michal Vasko5233e962020-08-14 14:26:20 +02009684 ly_print_(&out, "\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02009685 break;
9686 case LYXP_NODE_TEXT:
Michal Vasko5233e962020-08-14 14:26:20 +02009687 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 +02009688 break;
9689 case LYXP_NODE_ATTR:
Michal Vasko5233e962020-08-14 14:26:20 +02009690 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 +02009691 break;
9692 }
9693 }
9694 break;
9695 }
9696}
9697
9698#endif
9699
9700LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009701lyxp_set_cast(struct lyxp_set *set, enum lyxp_set_type target)
Michal Vasko03ff5a72019-09-11 13:49:33 +02009702{
9703 long double num;
9704 char *str;
9705 LY_ERR rc;
9706
9707 if (!set || (set->type == target)) {
9708 return LY_SUCCESS;
9709 }
9710
9711 /* it's not possible to convert anything into a node set */
Michal Vaskod3678892020-05-21 10:06:58 +02009712 assert(target != LYXP_SET_NODE_SET);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009713
9714 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vaskod3678892020-05-21 10:06:58 +02009715 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009716 return LY_EINVAL;
9717 }
9718
9719 /* to STRING */
Michal Vaskod3678892020-05-21 10:06:58 +02009720 if ((target == LYXP_SET_STRING) || ((target == LYXP_SET_NUMBER) && (set->type == LYXP_SET_NODE_SET))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02009721 switch (set->type) {
9722 case LYXP_SET_NUMBER:
9723 if (isnan(set->val.num)) {
9724 set->val.str = strdup("NaN");
9725 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
9726 } else if ((set->val.num == 0) || (set->val.num == -0.0f)) {
9727 set->val.str = strdup("0");
9728 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
9729 } else if (isinf(set->val.num) && !signbit(set->val.num)) {
9730 set->val.str = strdup("Infinity");
9731 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
9732 } else if (isinf(set->val.num) && signbit(set->val.num)) {
9733 set->val.str = strdup("-Infinity");
9734 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
9735 } else if ((long long)set->val.num == set->val.num) {
9736 if (asprintf(&str, "%lld", (long long)set->val.num) == -1) {
9737 LOGMEM_RET(set->ctx);
9738 }
9739 set->val.str = str;
9740 } else {
9741 if (asprintf(&str, "%03.1Lf", set->val.num) == -1) {
9742 LOGMEM_RET(set->ctx);
9743 }
9744 set->val.str = str;
9745 }
9746 break;
9747 case LYXP_SET_BOOLEAN:
Michal Vasko004d3152020-06-11 19:59:22 +02009748 if (set->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02009749 set->val.str = strdup("true");
9750 } else {
9751 set->val.str = strdup("false");
9752 }
9753 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), LY_EMEM);
9754 break;
9755 case LYXP_SET_NODE_SET:
Michal Vasko03ff5a72019-09-11 13:49:33 +02009756 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009757 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02009758
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009759 rc = cast_node_set_to_string(set, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009760 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02009761 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009762 set->val.str = str;
9763 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009764 default:
9765 LOGINT_RET(set->ctx);
9766 }
9767 set->type = LYXP_SET_STRING;
9768 }
9769
9770 /* to NUMBER */
9771 if (target == LYXP_SET_NUMBER) {
9772 switch (set->type) {
9773 case LYXP_SET_STRING:
9774 num = cast_string_to_number(set->val.str);
Michal Vaskod3678892020-05-21 10:06:58 +02009775 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009776 set->val.num = num;
9777 break;
9778 case LYXP_SET_BOOLEAN:
Michal Vasko004d3152020-06-11 19:59:22 +02009779 if (set->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02009780 set->val.num = 1;
9781 } else {
9782 set->val.num = 0;
9783 }
9784 break;
9785 default:
9786 LOGINT_RET(set->ctx);
9787 }
9788 set->type = LYXP_SET_NUMBER;
9789 }
9790
9791 /* to BOOLEAN */
9792 if (target == LYXP_SET_BOOLEAN) {
9793 switch (set->type) {
9794 case LYXP_SET_NUMBER:
9795 if ((set->val.num == 0) || (set->val.num == -0.0f) || isnan(set->val.num)) {
Michal Vasko004d3152020-06-11 19:59:22 +02009796 set->val.bln = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009797 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02009798 set->val.bln = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009799 }
9800 break;
9801 case LYXP_SET_STRING:
9802 if (set->val.str[0]) {
Michal Vaskod3678892020-05-21 10:06:58 +02009803 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02009804 set->val.bln = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009805 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02009806 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02009807 set->val.bln = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009808 }
9809 break;
9810 case LYXP_SET_NODE_SET:
Michal Vaskod3678892020-05-21 10:06:58 +02009811 if (set->used) {
9812 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02009813 set->val.bln = 1;
Michal Vaskod3678892020-05-21 10:06:58 +02009814 } else {
9815 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02009816 set->val.bln = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02009817 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02009818 break;
9819 default:
9820 LOGINT_RET(set->ctx);
9821 }
9822 set->type = LYXP_SET_BOOLEAN;
9823 }
9824
Michal Vasko03ff5a72019-09-11 13:49:33 +02009825 return LY_SUCCESS;
9826}
9827
9828LY_ERR
Michal Vasko400e9672021-01-11 13:39:17 +01009829lyxp_atomize(const struct ly_ctx *ctx, const struct lyxp_expr *exp, const struct lys_module *cur_mod,
Michal Vaskoa3e92bc2022-07-29 14:56:23 +02009830 LY_VALUE_FORMAT format, void *prefix_data, const struct lysc_node *cur_scnode,
9831 const struct lysc_node *ctx_scnode, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02009832{
Radek Krejci2efc45b2020-12-22 16:25:44 +01009833 LY_ERR ret;
Michal Vasko004d3152020-06-11 19:59:22 +02009834 uint16_t tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009835
Michal Vasko400e9672021-01-11 13:39:17 +01009836 LY_CHECK_ARG_RET(ctx, ctx, exp, set, LY_EINVAL);
Radek Krejci8df109d2021-04-23 12:19:08 +02009837 if (!cur_mod && ((format == LY_VALUE_SCHEMA) || (format == LY_VALUE_SCHEMA_RESOLVED))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009838 LOGARG(NULL, "Current module must be set if schema format is used.");
9839 return LY_EINVAL;
Michal Vasko004d3152020-06-11 19:59:22 +02009840 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02009841
9842 /* prepare set for evaluation */
Michal Vasko03ff5a72019-09-11 13:49:33 +02009843 memset(set, 0, sizeof *set);
9844 set->type = LYXP_SET_SCNODE_SET;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009845 set->root_type = lyxp_get_root_type(NULL, ctx_scnode, options);
Michal Vasko7333cb32022-07-29 16:30:29 +02009846 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 +01009847 set->val.scnodes[0].in_ctx = LYXP_SET_SCNODE_START;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009848
Michal Vasko400e9672021-01-11 13:39:17 +01009849 set->ctx = (struct ly_ctx *)ctx;
Michal Vaskoa3e92bc2022-07-29 14:56:23 +02009850 set->cur_scnode = cur_scnode;
9851 for (set->context_op = cur_scnode;
Radek Krejci0f969882020-08-21 16:56:47 +02009852 set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF));
9853 set->context_op = set->context_op->parent) {}
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009854 set->cur_mod = cur_mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009855 set->format = format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009856 set->prefix_data = prefix_data;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009857
Radek Krejciddace2c2021-01-08 11:30:56 +01009858 LOG_LOCSET(set->cur_scnode, NULL, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01009859
Michal Vasko03ff5a72019-09-11 13:49:33 +02009860 /* evaluate */
Radek Krejci2efc45b2020-12-22 16:25:44 +01009861 ret = eval_expr_select(exp, &tok_idx, 0, set, options);
9862
Radek Krejciddace2c2021-01-08 11:30:56 +01009863 LOG_LOCBACK(1, 0, 0, 0);
Radek Krejci2efc45b2020-12-22 16:25:44 +01009864 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009865}
Michal Vaskod43d71a2020-08-07 14:54:58 +02009866
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01009867LIBYANG_API_DEF const char *
Michal Vaskod43d71a2020-08-07 14:54:58 +02009868lyxp_get_expr(const struct lyxp_expr *path)
9869{
9870 if (!path) {
9871 return NULL;
9872 }
9873
9874 return path->expr;
9875}