blob: 3c192304638691f227d96aca4302f85230e4ad45 [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 Vasko26bbb272022-08-02 14:54:33 +0200890
Michal Vasko7333cb32022-07-29 16:30:29 +0200891 LY_CHECK_ERR_RET(set_scnode_insert_node(ret, set->val.scnodes[i].scnode, set->val.scnodes[i].type,
892 set->val.scnodes[i].axis, &idx), lyxp_set_free(ret), NULL);
Michal Vasko3f27c522020-01-06 08:37:49 +0100893 /* coverity seems to think scnodes can be NULL */
Radek Krejciaa6b53f2020-08-27 15:19:03 +0200894 if (!ret->val.scnodes) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200895 lyxp_set_free(ret);
896 return NULL;
897 }
Michal Vaskoba716542019-12-16 10:01:58 +0100898 ret->val.scnodes[idx].in_ctx = set->val.scnodes[i].in_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200899 }
900 }
901 } else if (set->type == LYXP_SET_NODE_SET) {
902 ret->type = set->type;
Michal Vasko08e9b112021-06-11 15:41:17 +0200903 if (set->used) {
904 ret->val.nodes = malloc(set->used * sizeof *ret->val.nodes);
905 LY_CHECK_ERR_RET(!ret->val.nodes, LOGMEM(set->ctx); free(ret), NULL);
906 memcpy(ret->val.nodes, set->val.nodes, set->used * sizeof *ret->val.nodes);
907 } else {
908 ret->val.nodes = NULL;
909 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200910
911 ret->used = ret->size = set->used;
912 ret->ctx_pos = set->ctx_pos;
913 ret->ctx_size = set->ctx_size;
Michal Vasko4a04e542021-02-01 08:58:30 +0100914 if (set->ht) {
915 ret->ht = lyht_dup(set->ht);
916 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200917 } else {
Radek Krejci0f969882020-08-21 16:56:47 +0200918 memcpy(ret, set, sizeof *ret);
919 if (set->type == LYXP_SET_STRING) {
920 ret->val.str = strdup(set->val.str);
921 LY_CHECK_ERR_RET(!ret->val.str, LOGMEM(set->ctx); free(ret), NULL);
922 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200923 }
924
925 return ret;
926}
927
928/**
929 * @brief Fill XPath set with a string. Any current data are disposed of.
930 *
931 * @param[in] set Set to fill.
932 * @param[in] string String to fill into \p set.
933 * @param[in] str_len Length of \p string. 0 is a valid value!
934 */
935static void
936set_fill_string(struct lyxp_set *set, const char *string, uint16_t str_len)
937{
Michal Vaskod3678892020-05-21 10:06:58 +0200938 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200939
940 set->type = LYXP_SET_STRING;
941 if ((str_len == 0) && (string[0] != '\0')) {
942 string = "";
943 }
944 set->val.str = strndup(string, str_len);
945}
946
947/**
948 * @brief Fill XPath set with a number. Any current data are disposed of.
949 *
950 * @param[in] set Set to fill.
951 * @param[in] number Number to fill into \p set.
952 */
953static void
954set_fill_number(struct lyxp_set *set, long double number)
955{
Michal Vaskod3678892020-05-21 10:06:58 +0200956 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200957
958 set->type = LYXP_SET_NUMBER;
959 set->val.num = number;
960}
961
962/**
963 * @brief Fill XPath set with a boolean. Any current data are disposed of.
964 *
965 * @param[in] set Set to fill.
966 * @param[in] boolean Boolean to fill into \p set.
967 */
968static void
Radek Krejci857189e2020-09-01 13:26:36 +0200969set_fill_boolean(struct lyxp_set *set, ly_bool boolean)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200970{
Michal Vaskod3678892020-05-21 10:06:58 +0200971 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200972
973 set->type = LYXP_SET_BOOLEAN;
Michal Vasko004d3152020-06-11 19:59:22 +0200974 set->val.bln = boolean;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200975}
976
977/**
978 * @brief Fill XPath set with the value from another set (deep assign).
979 * Any current data are disposed of.
980 *
981 * @param[in] trg Set to fill.
982 * @param[in] src Source set to copy into \p trg.
983 */
984static void
Michal Vasko4c7763f2020-07-27 17:40:37 +0200985set_fill_set(struct lyxp_set *trg, const struct lyxp_set *src)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200986{
987 if (!trg || !src) {
988 return;
989 }
990
991 if (trg->type == LYXP_SET_NODE_SET) {
992 free(trg->val.nodes);
993 } else if (trg->type == LYXP_SET_STRING) {
994 free(trg->val.str);
995 }
996 set_init(trg, src);
997
998 if (src->type == LYXP_SET_SCNODE_SET) {
999 trg->type = LYXP_SET_SCNODE_SET;
1000 trg->used = src->used;
1001 trg->size = src->used;
1002
Michal Vasko08e9b112021-06-11 15:41:17 +02001003 if (trg->size) {
1004 trg->val.scnodes = ly_realloc(trg->val.scnodes, trg->size * sizeof *trg->val.scnodes);
1005 LY_CHECK_ERR_RET(!trg->val.scnodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), );
1006 memcpy(trg->val.scnodes, src->val.scnodes, src->used * sizeof *src->val.scnodes);
1007 } else {
1008 trg->val.scnodes = NULL;
1009 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001010 } else if (src->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +02001011 set_fill_boolean(trg, src->val.bln);
Michal Vasko44f3d2c2020-08-24 09:49:38 +02001012 } else if (src->type == LYXP_SET_NUMBER) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001013 set_fill_number(trg, src->val.num);
1014 } else if (src->type == LYXP_SET_STRING) {
1015 set_fill_string(trg, src->val.str, strlen(src->val.str));
1016 } else {
1017 if (trg->type == LYXP_SET_NODE_SET) {
1018 free(trg->val.nodes);
1019 } else if (trg->type == LYXP_SET_STRING) {
1020 free(trg->val.str);
1021 }
1022
Michal Vaskod3678892020-05-21 10:06:58 +02001023 assert(src->type == LYXP_SET_NODE_SET);
1024
1025 trg->type = LYXP_SET_NODE_SET;
1026 trg->used = src->used;
1027 trg->size = src->used;
1028 trg->ctx_pos = src->ctx_pos;
1029 trg->ctx_size = src->ctx_size;
1030
Michal Vasko08e9b112021-06-11 15:41:17 +02001031 if (trg->size) {
1032 trg->val.nodes = malloc(trg->size * sizeof *trg->val.nodes);
1033 LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), );
1034 memcpy(trg->val.nodes, src->val.nodes, src->used * sizeof *src->val.nodes);
1035 } else {
1036 trg->val.nodes = NULL;
1037 }
Michal Vaskod3678892020-05-21 10:06:58 +02001038 if (src->ht) {
1039 trg->ht = lyht_dup(src->ht);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001040 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02001041 trg->ht = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001042 }
1043 }
1044}
1045
1046/**
1047 * @brief Clear context of all schema nodes.
1048 *
1049 * @param[in] set Set to clear.
Michal Vasko1a09b212021-05-06 13:00:10 +02001050 * @param[in] new_ctx New context state for all the nodes currently in the context.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001051 */
1052static void
Michal Vasko1a09b212021-05-06 13:00:10 +02001053set_scnode_clear_ctx(struct lyxp_set *set, int32_t new_ctx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001054{
1055 uint32_t i;
1056
1057 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001058 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko1a09b212021-05-06 13:00:10 +02001059 set->val.scnodes[i].in_ctx = new_ctx;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001060 } else if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START) {
1061 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001062 }
1063 }
1064}
1065
1066/**
1067 * @brief Remove a node from a set. Removing last node changes
1068 * set into LYXP_SET_EMPTY. Context position aware.
1069 *
1070 * @param[in] set Set to use.
1071 * @param[in] idx Index from @p set of the node to be removed.
1072 */
1073static void
1074set_remove_node(struct lyxp_set *set, uint32_t idx)
1075{
1076 assert(set && (set->type == LYXP_SET_NODE_SET));
1077 assert(idx < set->used);
1078
1079 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
1080
1081 --set->used;
Michal Vasko08e9b112021-06-11 15:41:17 +02001082 if (idx < set->used) {
1083 memmove(&set->val.nodes[idx], &set->val.nodes[idx + 1], (set->used - idx) * sizeof *set->val.nodes);
1084 } else if (!set->used) {
Michal Vaskod3678892020-05-21 10:06:58 +02001085 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001086 }
1087}
1088
1089/**
Michal Vasko2caefc12019-11-14 16:07:56 +01001090 * @brief Remove a node from a set by setting its type to LYXP_NODE_NONE.
Michal Vasko57eab132019-09-24 11:46:26 +02001091 *
1092 * @param[in] set Set to use.
1093 * @param[in] idx Index from @p set of the node to be removed.
1094 */
1095static void
Michal Vasko2caefc12019-11-14 16:07:56 +01001096set_remove_node_none(struct lyxp_set *set, uint32_t idx)
Michal Vasko57eab132019-09-24 11:46:26 +02001097{
1098 assert(set && (set->type == LYXP_SET_NODE_SET));
1099 assert(idx < set->used);
1100
Michal Vasko2caefc12019-11-14 16:07:56 +01001101 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1102 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
1103 }
1104 set->val.nodes[idx].type = LYXP_NODE_NONE;
Michal Vasko57eab132019-09-24 11:46:26 +02001105}
1106
1107/**
Michal Vasko2caefc12019-11-14 16:07:56 +01001108 * @brief Remove all LYXP_NODE_NONE nodes from a set. Removing last node changes
Michal Vasko57eab132019-09-24 11:46:26 +02001109 * set into LYXP_SET_EMPTY. Context position aware.
1110 *
1111 * @param[in] set Set to consolidate.
1112 */
1113static void
Michal Vasko2caefc12019-11-14 16:07:56 +01001114set_remove_nodes_none(struct lyxp_set *set)
Michal Vasko57eab132019-09-24 11:46:26 +02001115{
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01001116 uint32_t i, orig_used, end = 0;
1117 int64_t start;
Michal Vasko57eab132019-09-24 11:46:26 +02001118
Michal Vaskod3678892020-05-21 10:06:58 +02001119 assert(set);
Michal Vasko57eab132019-09-24 11:46:26 +02001120
1121 orig_used = set->used;
1122 set->used = 0;
Michal Vaskod989ba02020-08-24 10:59:24 +02001123 for (i = 0; i < orig_used; ) {
Michal Vasko57eab132019-09-24 11:46:26 +02001124 start = -1;
1125 do {
Michal Vasko2caefc12019-11-14 16:07:56 +01001126 if ((set->val.nodes[i].type != LYXP_NODE_NONE) && (start == -1)) {
Michal Vasko57eab132019-09-24 11:46:26 +02001127 start = i;
Michal Vasko2caefc12019-11-14 16:07:56 +01001128 } else if ((start > -1) && (set->val.nodes[i].type == LYXP_NODE_NONE)) {
Michal Vasko57eab132019-09-24 11:46:26 +02001129 end = i;
1130 ++i;
1131 break;
1132 }
1133
1134 ++i;
1135 if (i == orig_used) {
1136 end = i;
1137 }
1138 } while (i < orig_used);
1139
1140 if (start > -1) {
1141 /* move the whole chunk of valid nodes together */
1142 if (set->used != (unsigned)start) {
1143 memmove(&set->val.nodes[set->used], &set->val.nodes[start], (end - start) * sizeof *set->val.nodes);
1144 }
1145 set->used += end - start;
1146 }
1147 }
Michal Vasko57eab132019-09-24 11:46:26 +02001148}
1149
1150/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02001151 * @brief Check for duplicates in a node set.
1152 *
1153 * @param[in] set Set to check.
1154 * @param[in] node Node to look for in @p set.
1155 * @param[in] node_type Type of @p node.
1156 * @param[in] skip_idx Index from @p set to skip.
1157 * @return LY_ERR
1158 */
1159static LY_ERR
1160set_dup_node_check(const struct lyxp_set *set, const struct lyd_node *node, enum lyxp_node_type node_type, int skip_idx)
1161{
1162 uint32_t i;
1163
Michal Vasko2caefc12019-11-14 16:07:56 +01001164 if (set->ht && node) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001165 return set_dup_node_hash_check(set, (struct lyd_node *)node, node_type, skip_idx);
1166 }
1167
1168 for (i = 0; i < set->used; ++i) {
1169 if ((skip_idx > -1) && (i == (unsigned)skip_idx)) {
1170 continue;
1171 }
1172
1173 if ((set->val.nodes[i].node == node) && (set->val.nodes[i].type == node_type)) {
1174 return LY_EEXIST;
1175 }
1176 }
1177
1178 return LY_SUCCESS;
1179}
1180
Radek Krejci857189e2020-09-01 13:26:36 +02001181ly_bool
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001182lyxp_set_scnode_contains(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type, int skip_idx,
1183 uint32_t *index_p)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001184{
1185 uint32_t i;
1186
1187 for (i = 0; i < set->used; ++i) {
1188 if ((skip_idx > -1) && (i == (unsigned)skip_idx)) {
1189 continue;
1190 }
1191
1192 if ((set->val.scnodes[i].scnode == node) && (set->val.scnodes[i].type == node_type)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001193 if (index_p) {
1194 *index_p = i;
1195 }
1196 return 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001197 }
1198 }
1199
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001200 return 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001201}
1202
Michal Vaskoecd62de2019-11-13 12:35:11 +01001203void
1204lyxp_set_scnode_merge(struct lyxp_set *set1, struct lyxp_set *set2)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001205{
1206 uint32_t orig_used, i, j;
1207
Michal Vaskod3678892020-05-21 10:06:58 +02001208 assert((set1->type == LYXP_SET_SCNODE_SET) && (set2->type == LYXP_SET_SCNODE_SET));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001209
Michal Vaskod3678892020-05-21 10:06:58 +02001210 if (!set2->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001211 return;
1212 }
1213
Michal Vaskod3678892020-05-21 10:06:58 +02001214 if (!set1->used) {
aPiecekadc1e4f2021-10-07 11:15:12 +02001215 /* release hidden allocated data (lyxp_set.size) */
1216 lyxp_set_free_content(set1);
1217 /* direct copying of the entire structure */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001218 memcpy(set1, set2, sizeof *set1);
1219 return;
1220 }
1221
1222 if (set1->used + set2->used > set1->size) {
1223 set1->size = set1->used + set2->used;
1224 set1->val.scnodes = ly_realloc(set1->val.scnodes, set1->size * sizeof *set1->val.scnodes);
1225 LY_CHECK_ERR_RET(!set1->val.scnodes, LOGMEM(set1->ctx), );
1226 }
1227
1228 orig_used = set1->used;
1229
1230 for (i = 0; i < set2->used; ++i) {
1231 for (j = 0; j < orig_used; ++j) {
1232 /* detect duplicities */
1233 if (set1->val.scnodes[j].scnode == set2->val.scnodes[i].scnode) {
1234 break;
1235 }
1236 }
1237
Michal Vasko0587bce2020-12-10 12:19:21 +01001238 if (j < orig_used) {
1239 /* node is there, but update its status if needed */
1240 if (set1->val.scnodes[j].in_ctx == LYXP_SET_SCNODE_START_USED) {
1241 set1->val.scnodes[j].in_ctx = set2->val.scnodes[i].in_ctx;
Michal Vasko1a09b212021-05-06 13:00:10 +02001242 } else if ((set1->val.scnodes[j].in_ctx == LYXP_SET_SCNODE_ATOM_NODE) &&
1243 (set2->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_VAL)) {
1244 set1->val.scnodes[j].in_ctx = set2->val.scnodes[i].in_ctx;
Michal Vasko0587bce2020-12-10 12:19:21 +01001245 }
1246 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001247 memcpy(&set1->val.scnodes[set1->used], &set2->val.scnodes[i], sizeof *set2->val.scnodes);
1248 ++set1->used;
1249 }
1250 }
1251
Michal Vaskod3678892020-05-21 10:06:58 +02001252 lyxp_set_free_content(set2);
1253 set2->type = LYXP_SET_SCNODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001254}
1255
1256/**
1257 * @brief Insert a node into a set. Context position aware.
1258 *
1259 * @param[in] set Set to use.
1260 * @param[in] node Node to insert to @p set.
1261 * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting.
1262 * @param[in] node_type Node type of @p node.
1263 * @param[in] idx Index in @p set to insert into.
1264 */
1265static void
1266set_insert_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx)
1267{
Michal Vaskod3678892020-05-21 10:06:58 +02001268 assert(set && (set->type == LYXP_SET_NODE_SET));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001269
Michal Vaskod3678892020-05-21 10:06:58 +02001270 if (!set->size) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001271 /* first item */
1272 if (idx) {
1273 /* no real harm done, but it is a bug */
1274 LOGINT(set->ctx);
1275 idx = 0;
1276 }
1277 set->val.nodes = malloc(LYXP_SET_SIZE_START * sizeof *set->val.nodes);
1278 LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), );
1279 set->type = LYXP_SET_NODE_SET;
1280 set->used = 0;
1281 set->size = LYXP_SET_SIZE_START;
1282 set->ctx_pos = 1;
1283 set->ctx_size = 1;
1284 set->ht = NULL;
1285 } else {
1286 /* not an empty set */
1287 if (set->used == set->size) {
1288
1289 /* set is full */
Michal Vasko871df522022-04-06 12:14:41 +02001290 set->val.nodes = ly_realloc(set->val.nodes, (set->size * LYXP_SET_SIZE_MUL_STEP) * sizeof *set->val.nodes);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001291 LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), );
Michal Vasko871df522022-04-06 12:14:41 +02001292 set->size *= LYXP_SET_SIZE_MUL_STEP;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001293 }
1294
1295 if (idx > set->used) {
1296 LOGINT(set->ctx);
1297 idx = set->used;
1298 }
1299
1300 /* make space for the new node */
1301 if (idx < set->used) {
1302 memmove(&set->val.nodes[idx + 1], &set->val.nodes[idx], (set->used - idx) * sizeof *set->val.nodes);
1303 }
1304 }
1305
1306 /* finally assign the value */
1307 set->val.nodes[idx].node = (struct lyd_node *)node;
1308 set->val.nodes[idx].type = node_type;
1309 set->val.nodes[idx].pos = pos;
1310 ++set->used;
1311
Michal Vasko2416fdd2021-10-01 11:07:10 +02001312 /* add into hash table */
1313 set_insert_node_hash(set, (struct lyd_node *)node, node_type);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001314}
1315
Michal Vasko7333cb32022-07-29 16:30:29 +02001316/**
1317 * @brief Insert schema node into set.
1318 *
1319 * @param[in] set Set to insert into.
1320 * @param[in] node Node to insert.
1321 * @param[in] node_type Node type of @p node.
1322 * @param[in] axis Axis that @p node was reached on.
1323 * @param[out] index_p Optional pointer to store index if the inserted @p node.
1324 * @return LY_SUCCESS on success.
1325 * @return LY_EMEM on memory allocation failure.
1326 */
1327static LY_ERR
1328set_scnode_insert_node(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type,
1329 enum lyxp_axis axis, uint32_t *index_p)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001330{
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001331 uint32_t index;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001332
1333 assert(set->type == LYXP_SET_SCNODE_SET);
1334
Michal Vasko871df522022-04-06 12:14:41 +02001335 if (!set->size) {
1336 /* first item */
1337 set->val.scnodes = malloc(LYXP_SET_SIZE_START * sizeof *set->val.scnodes);
1338 LY_CHECK_ERR_RET(!set->val.scnodes, LOGMEM(set->ctx), LY_EMEM);
1339 set->type = LYXP_SET_SCNODE_SET;
1340 set->used = 0;
1341 set->size = LYXP_SET_SIZE_START;
1342 set->ctx_pos = 1;
1343 set->ctx_size = 1;
1344 set->ht = NULL;
1345 }
1346
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001347 if (lyxp_set_scnode_contains(set, node, node_type, -1, &index)) {
Michal Vasko7333cb32022-07-29 16:30:29 +02001348 /* BUG if axes differs, this new one is thrown away */
Radek Krejcif13b87b2020-12-01 22:02:17 +01001349 set->val.scnodes[index].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001350 } else {
1351 if (set->used == set->size) {
Michal Vasko871df522022-04-06 12:14:41 +02001352 set->val.scnodes = ly_realloc(set->val.scnodes, (set->size * LYXP_SET_SIZE_MUL_STEP) * sizeof *set->val.scnodes);
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001353 LY_CHECK_ERR_RET(!set->val.scnodes, LOGMEM(set->ctx), LY_EMEM);
Michal Vasko871df522022-04-06 12:14:41 +02001354 set->size *= LYXP_SET_SIZE_MUL_STEP;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001355 }
1356
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001357 index = set->used;
1358 set->val.scnodes[index].scnode = (struct lysc_node *)node;
1359 set->val.scnodes[index].type = node_type;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001360 set->val.scnodes[index].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko7333cb32022-07-29 16:30:29 +02001361 set->val.scnodes[index].axis = axis;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001362 ++set->used;
1363 }
1364
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001365 if (index_p) {
1366 *index_p = index;
1367 }
1368
1369 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001370}
1371
1372/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02001373 * @brief Set all nodes with ctx 1 to a new unique context value.
1374 *
1375 * @param[in] set Set to modify.
1376 * @return New context value.
1377 */
Michal Vasko5c4e5892019-11-14 12:31:38 +01001378static int32_t
Michal Vasko03ff5a72019-09-11 13:49:33 +02001379set_scnode_new_in_ctx(struct lyxp_set *set)
1380{
Michal Vasko5c4e5892019-11-14 12:31:38 +01001381 uint32_t i;
1382 int32_t ret_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001383
1384 assert(set->type == LYXP_SET_SCNODE_SET);
1385
Radek Krejcif13b87b2020-12-01 22:02:17 +01001386 ret_ctx = LYXP_SET_SCNODE_ATOM_PRED_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001387retry:
1388 for (i = 0; i < set->used; ++i) {
1389 if (set->val.scnodes[i].in_ctx >= ret_ctx) {
1390 ret_ctx = set->val.scnodes[i].in_ctx + 1;
1391 goto retry;
1392 }
1393 }
1394 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001395 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001396 set->val.scnodes[i].in_ctx = ret_ctx;
1397 }
1398 }
1399
1400 return ret_ctx;
1401}
1402
1403/**
1404 * @brief Get unique @p node position in the data.
1405 *
1406 * @param[in] node Node to find.
1407 * @param[in] node_type Node type of @p node.
1408 * @param[in] root Root node.
1409 * @param[in] root_type Type of the XPath @p root node.
1410 * @param[in] prev Node that we think is before @p node in DFS from @p root. Can optionally
1411 * be used to increase efficiency and start the DFS from this node.
1412 * @param[in] prev_pos Node @p prev position. Optional, but must be set if @p prev is set.
1413 * @return Node position.
1414 */
1415static uint32_t
1416get_node_pos(const struct lyd_node *node, enum lyxp_node_type node_type, const struct lyd_node *root,
Radek Krejci0f969882020-08-21 16:56:47 +02001417 enum lyxp_node_type root_type, const struct lyd_node **prev, uint32_t *prev_pos)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001418{
Michal Vasko56daf732020-08-10 10:57:18 +02001419 const struct lyd_node *elem = NULL, *top_sibling;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001420 uint32_t pos = 1;
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001421 ly_bool found = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001422
1423 assert(prev && prev_pos && !root->prev->next);
1424
1425 if ((node_type == LYXP_NODE_ROOT) || (node_type == LYXP_NODE_ROOT_CONFIG)) {
1426 return 0;
1427 }
1428
1429 if (*prev) {
1430 /* start from the previous element instead from the root */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001431 pos = *prev_pos;
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001432 for (top_sibling = *prev; top_sibling->parent; top_sibling = lyd_parent(top_sibling)) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001433 goto dfs_search;
1434 }
1435
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001436 LY_LIST_FOR(root, top_sibling) {
Michal Vasko56daf732020-08-10 10:57:18 +02001437 LYD_TREE_DFS_BEGIN(top_sibling, elem) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001438dfs_search:
Michal Vaskoa9309bb2021-07-09 09:31:55 +02001439 LYD_TREE_DFS_continue = 0;
1440
Michal Vasko56daf732020-08-10 10:57:18 +02001441 if (*prev && !elem) {
1442 /* resume previous DFS */
1443 elem = LYD_TREE_DFS_next = (struct lyd_node *)*prev;
1444 LYD_TREE_DFS_continue = 0;
1445 }
1446
Michal Vasko482a6822022-05-06 08:56:12 +02001447 if (!elem->schema || ((root_type == LYXP_NODE_ROOT_CONFIG) && (elem->schema->flags & LYS_CONFIG_R))) {
Michal Vasko56daf732020-08-10 10:57:18 +02001448 /* skip */
1449 LYD_TREE_DFS_continue = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001450 } else {
Michal Vasko56daf732020-08-10 10:57:18 +02001451 if (elem == node) {
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001452 found = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001453 break;
1454 }
Michal Vasko56daf732020-08-10 10:57:18 +02001455 ++pos;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001456 }
Michal Vasko56daf732020-08-10 10:57:18 +02001457
1458 LYD_TREE_DFS_END(top_sibling, elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001459 }
1460
1461 /* node found */
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001462 if (found) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001463 break;
1464 }
1465 }
1466
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001467 if (!found) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001468 if (!(*prev)) {
1469 /* we went from root and failed to find it, cannot be */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001470 LOGINT(LYD_CTX(node));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001471 return 0;
1472 } else {
Michal Vasko56daf732020-08-10 10:57:18 +02001473 /* start the search again from the beginning */
1474 *prev = root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001475
Michal Vasko56daf732020-08-10 10:57:18 +02001476 top_sibling = root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001477 pos = 1;
1478 goto dfs_search;
1479 }
1480 }
1481
1482 /* remember the last found node for next time */
1483 *prev = node;
1484 *prev_pos = pos;
1485
1486 return pos;
1487}
1488
1489/**
1490 * @brief Assign (fill) missing node positions.
1491 *
1492 * @param[in] set Set to fill positions in.
1493 * @param[in] root Context root node.
1494 * @param[in] root_type Context root type.
1495 * @return LY_ERR
1496 */
1497static LY_ERR
1498set_assign_pos(struct lyxp_set *set, const struct lyd_node *root, enum lyxp_node_type root_type)
1499{
1500 const struct lyd_node *prev = NULL, *tmp_node;
1501 uint32_t i, tmp_pos = 0;
1502
1503 for (i = 0; i < set->used; ++i) {
1504 if (!set->val.nodes[i].pos) {
1505 tmp_node = NULL;
1506 switch (set->val.nodes[i].type) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001507 case LYXP_NODE_META:
1508 tmp_node = set->val.meta[i].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001509 if (!tmp_node) {
1510 LOGINT_RET(root->schema->module->ctx);
1511 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01001512 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001513 case LYXP_NODE_ELEM:
1514 case LYXP_NODE_TEXT:
1515 if (!tmp_node) {
1516 tmp_node = set->val.nodes[i].node;
1517 }
1518 set->val.nodes[i].pos = get_node_pos(tmp_node, set->val.nodes[i].type, root, root_type, &prev, &tmp_pos);
1519 break;
1520 default:
1521 /* all roots have position 0 */
1522 break;
1523 }
1524 }
1525 }
1526
1527 return LY_SUCCESS;
1528}
1529
1530/**
Michal Vasko9f96a052020-03-10 09:41:45 +01001531 * @brief Get unique @p meta position in the parent metadata.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001532 *
Michal Vasko9f96a052020-03-10 09:41:45 +01001533 * @param[in] meta Metadata to use.
1534 * @return Metadata position.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001535 */
1536static uint16_t
Michal Vasko9f96a052020-03-10 09:41:45 +01001537get_meta_pos(struct lyd_meta *meta)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001538{
1539 uint16_t pos = 0;
Michal Vasko9f96a052020-03-10 09:41:45 +01001540 struct lyd_meta *meta2;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001541
Michal Vasko9f96a052020-03-10 09:41:45 +01001542 for (meta2 = meta->parent->meta; meta2 && (meta2 != meta); meta2 = meta2->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001543 ++pos;
1544 }
1545
Michal Vasko9f96a052020-03-10 09:41:45 +01001546 assert(meta2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001547 return pos;
1548}
1549
1550/**
1551 * @brief Compare 2 nodes in respect to XPath document order.
1552 *
1553 * @param[in] item1 1st node.
1554 * @param[in] item2 2nd node.
1555 * @return If 1st > 2nd returns 1, 1st == 2nd returns 0, and 1st < 2nd returns -1.
1556 */
1557static int
1558set_sort_compare(struct lyxp_set_node *item1, struct lyxp_set_node *item2)
1559{
Michal Vasko9f96a052020-03-10 09:41:45 +01001560 uint32_t meta_pos1 = 0, meta_pos2 = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001561
1562 if (item1->pos < item2->pos) {
1563 return -1;
1564 }
1565
1566 if (item1->pos > item2->pos) {
1567 return 1;
1568 }
1569
1570 /* node positions are equal, the fun case */
1571
1572 /* 1st ELEM - == - 2nd TEXT, 1st TEXT - == - 2nd ELEM */
1573 /* special case since text nodes are actually saved as their parents */
1574 if ((item1->node == item2->node) && (item1->type != item2->type)) {
1575 if (item1->type == LYXP_NODE_ELEM) {
1576 assert(item2->type == LYXP_NODE_TEXT);
1577 return -1;
1578 } else {
1579 assert((item1->type == LYXP_NODE_TEXT) && (item2->type == LYXP_NODE_ELEM));
1580 return 1;
1581 }
1582 }
1583
Michal Vasko9f96a052020-03-10 09:41:45 +01001584 /* we need meta positions now */
1585 if (item1->type == LYXP_NODE_META) {
1586 meta_pos1 = get_meta_pos((struct lyd_meta *)item1->node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001587 }
Michal Vasko9f96a052020-03-10 09:41:45 +01001588 if (item2->type == LYXP_NODE_META) {
1589 meta_pos2 = get_meta_pos((struct lyd_meta *)item2->node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001590 }
1591
Michal Vasko9f96a052020-03-10 09:41:45 +01001592 /* 1st ROOT - 2nd ROOT, 1st ELEM - 2nd ELEM, 1st TEXT - 2nd TEXT, 1st META - =pos= - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001593 /* check for duplicates */
1594 if (item1->node == item2->node) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001595 assert((item1->type == item2->type) && ((item1->type != LYXP_NODE_META) || (meta_pos1 == meta_pos2)));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001596 return 0;
1597 }
1598
Michal Vasko9f96a052020-03-10 09:41:45 +01001599 /* 1st ELEM - 2nd TEXT, 1st ELEM - any pos - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001600 /* elem is always first, 2nd node is after it */
1601 if (item1->type == LYXP_NODE_ELEM) {
1602 assert(item2->type != LYXP_NODE_ELEM);
1603 return -1;
1604 }
1605
Michal Vasko9f96a052020-03-10 09:41:45 +01001606 /* 1st TEXT - 2nd ELEM, 1st TEXT - any pos - 2nd META, 1st META - any pos - 2nd ELEM, 1st META - >pos> - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001607 /* 2nd is before 1st */
Michal Vasko69730152020-10-09 16:30:07 +02001608 if (((item1->type == LYXP_NODE_TEXT) &&
1609 ((item2->type == LYXP_NODE_ELEM) || (item2->type == LYXP_NODE_META))) ||
1610 ((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_ELEM)) ||
1611 (((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_META)) &&
1612 (meta_pos1 > meta_pos2))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001613 return 1;
1614 }
1615
Michal Vasko9f96a052020-03-10 09:41:45 +01001616 /* 1st META - any pos - 2nd TEXT, 1st META <pos< - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001617 /* 2nd is after 1st */
1618 return -1;
1619}
1620
1621/**
1622 * @brief Set cast for comparisons.
1623 *
Michal Vasko8abcecc2022-07-28 09:55:01 +02001624 * @param[in,out] trg Target set to cast source into.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001625 * @param[in] src Source set.
1626 * @param[in] type Target set type.
1627 * @param[in] src_idx Source set node index.
Michal Vasko8abcecc2022-07-28 09:55:01 +02001628 * @return LY_SUCCESS on success.
1629 * @return LY_ERR value on error.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001630 */
1631static LY_ERR
Michal Vasko8abcecc2022-07-28 09:55:01 +02001632set_comp_cast(struct lyxp_set *trg, const struct lyxp_set *src, enum lyxp_set_type type, uint32_t src_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001633{
1634 assert(src->type == LYXP_SET_NODE_SET);
1635
1636 set_init(trg, src);
1637
1638 /* insert node into target set */
1639 set_insert_node(trg, src->val.nodes[src_idx].node, src->val.nodes[src_idx].pos, src->val.nodes[src_idx].type, 0);
1640
1641 /* cast target set appropriately */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001642 return lyxp_set_cast(trg, type);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001643}
1644
Michal Vasko4c7763f2020-07-27 17:40:37 +02001645/**
1646 * @brief Set content canonization for comparisons.
1647 *
Michal Vasko8abcecc2022-07-28 09:55:01 +02001648 * @param[in,out] set Set to canonize.
Michal Vasko4c7763f2020-07-27 17:40:37 +02001649 * @param[in] xp_node Source XPath node/meta to use for canonization.
Michal Vasko8abcecc2022-07-28 09:55:01 +02001650 * @return LY_SUCCESS on success.
1651 * @return LY_ERR value on error.
Michal Vasko4c7763f2020-07-27 17:40:37 +02001652 */
1653static LY_ERR
Michal Vasko8abcecc2022-07-28 09:55:01 +02001654set_comp_canonize(struct lyxp_set *set, const struct lyxp_set_node *xp_node)
Michal Vasko4c7763f2020-07-27 17:40:37 +02001655{
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001656 const struct lysc_type *type = NULL;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001657 struct lyd_value val;
1658 struct ly_err_item *err = NULL;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001659 LY_ERR rc;
1660
1661 /* is there anything to canonize even? */
Michal Vasko8abcecc2022-07-28 09:55:01 +02001662 if (set->type == LYXP_SET_STRING) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02001663 /* do we have a type to use for canonization? */
1664 if ((xp_node->type == LYXP_NODE_ELEM) && (xp_node->node->schema->nodetype & LYD_NODE_TERM)) {
1665 type = ((struct lyd_node_term *)xp_node->node)->value.realtype;
1666 } else if (xp_node->type == LYXP_NODE_META) {
1667 type = ((struct lyd_meta *)xp_node->node)->value.realtype;
1668 }
1669 }
1670 if (!type) {
Michal Vasko8abcecc2022-07-28 09:55:01 +02001671 /* no canonization needed/possible */
1672 return LY_SUCCESS;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001673 }
1674
Michal Vasko8abcecc2022-07-28 09:55:01 +02001675 /* check for built-in types without required canonization */
1676 if ((type->basetype == LY_TYPE_STRING) && (type->plugin->store == lyplg_type_store_string)) {
1677 /* string */
1678 return LY_SUCCESS;
1679 }
1680 if ((type->basetype == LY_TYPE_BOOL) && (type->plugin->store == lyplg_type_store_boolean)) {
1681 /* boolean */
1682 return LY_SUCCESS;
1683 }
1684 if ((type->basetype == LY_TYPE_ENUM) && (type->plugin->store == lyplg_type_store_enum)) {
1685 /* enumeration */
1686 return LY_SUCCESS;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001687 }
1688
Michal Vasko8abcecc2022-07-28 09:55:01 +02001689 /* print canonized string, ignore errors, the value may not satisfy schema constraints */
1690 rc = type->plugin->store(set->ctx, type, set->val.str, strlen(set->val.str), 0, set->format, set->prefix_data,
Michal Vasko405cc9e2020-12-01 12:01:27 +01001691 LYD_HINT_DATA, xp_node->node->schema, &val, NULL, &err);
Michal Vasko4c7763f2020-07-27 17:40:37 +02001692 ly_err_free(err);
1693 if (rc) {
Michal Vasko8abcecc2022-07-28 09:55:01 +02001694 /* invalid value, function store automaticaly dealloc value when fail */
1695 return LY_SUCCESS;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001696 }
1697
Michal Vasko8abcecc2022-07-28 09:55:01 +02001698 /* use the canonized string value */
1699 free(set->val.str);
1700 set->val.str = strdup(lyd_value_get_canonical(set->ctx, &val));
1701 type->plugin->free(set->ctx, &val);
1702 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), LY_EMEM);
Michal Vasko4c7763f2020-07-27 17:40:37 +02001703
Michal Vasko4c7763f2020-07-27 17:40:37 +02001704 return LY_SUCCESS;
1705}
1706
Michal Vasko03ff5a72019-09-11 13:49:33 +02001707/**
1708 * @brief Bubble sort @p set into XPath document order.
Michal Vasko49fec8e2022-05-24 10:28:33 +02001709 * Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001710 *
1711 * @param[in] set Set to sort.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001712 * @return How many times the whole set was traversed - 1 (if set was sorted, returns 0).
1713 */
1714static int
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001715set_sort(struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001716{
1717 uint32_t i, j;
Radek Krejci1deb5be2020-08-26 16:43:36 +02001718 int ret = 0, cmp;
Radek Krejci857189e2020-09-01 13:26:36 +02001719 ly_bool inverted, change;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001720 const struct lyd_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001721 struct lyxp_set_node item;
1722 struct lyxp_set_hash_node hnode;
1723 uint64_t hash;
1724
Michal Vasko3cf8fbf2020-05-27 15:21:21 +02001725 if ((set->type != LYXP_SET_NODE_SET) || (set->used < 2)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001726 return 0;
1727 }
1728
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001729 /* find first top-level node to be used as anchor for positions */
Michal Vasko4a7d4d62021-12-13 17:05:06 +01001730 for (root = set->tree; root->parent; root = lyd_parent(root)) {}
Michal Vaskod989ba02020-08-24 10:59:24 +02001731 for ( ; root->prev->next; root = root->prev) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001732
1733 /* fill positions */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001734 if (set_assign_pos(set, root, set->root_type)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001735 return -1;
1736 }
1737
Michal Vasko88a9e802022-05-24 10:50:28 +02001738#ifndef NDEBUG
Michal Vasko03ff5a72019-09-11 13:49:33 +02001739 LOGDBG(LY_LDGXPATH, "SORT BEGIN");
1740 print_set_debug(set);
Michal Vasko88a9e802022-05-24 10:50:28 +02001741#endif
Michal Vasko03ff5a72019-09-11 13:49:33 +02001742
1743 for (i = 0; i < set->used; ++i) {
1744 inverted = 0;
1745 change = 0;
1746
1747 for (j = 1; j < set->used - i; ++j) {
1748 /* compare node positions */
1749 if (inverted) {
1750 cmp = set_sort_compare(&set->val.nodes[j], &set->val.nodes[j - 1]);
1751 } else {
1752 cmp = set_sort_compare(&set->val.nodes[j - 1], &set->val.nodes[j]);
1753 }
1754
1755 /* swap if needed */
1756 if ((inverted && (cmp < 0)) || (!inverted && (cmp > 0))) {
1757 change = 1;
1758
1759 item = set->val.nodes[j - 1];
1760 set->val.nodes[j - 1] = set->val.nodes[j];
1761 set->val.nodes[j] = item;
1762 } else {
1763 /* whether node_pos1 should be smaller than node_pos2 or the other way around */
1764 inverted = !inverted;
1765 }
1766 }
1767
1768 ++ret;
1769
1770 if (!change) {
1771 break;
1772 }
1773 }
1774
Michal Vasko88a9e802022-05-24 10:50:28 +02001775#ifndef NDEBUG
Michal Vasko03ff5a72019-09-11 13:49:33 +02001776 LOGDBG(LY_LDGXPATH, "SORT END %d", ret);
1777 print_set_debug(set);
Michal Vasko88a9e802022-05-24 10:50:28 +02001778#endif
Michal Vasko03ff5a72019-09-11 13:49:33 +02001779
1780 /* check node hashes */
1781 if (set->used >= LYD_HT_MIN_ITEMS) {
1782 assert(set->ht);
1783 for (i = 0; i < set->used; ++i) {
1784 hnode.node = set->val.nodes[i].node;
1785 hnode.type = set->val.nodes[i].type;
1786
1787 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
1788 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
1789 hash = dict_hash_multi(hash, NULL, 0);
1790
1791 assert(!lyht_find(set->ht, &hnode, hash, NULL));
1792 }
1793 }
1794
1795 return ret - 1;
1796}
1797
Michal Vasko03ff5a72019-09-11 13:49:33 +02001798/**
1799 * @brief Merge 2 sorted sets into one.
1800 *
1801 * @param[in,out] trg Set to merge into. Duplicates are removed.
1802 * @param[in] src Set to be merged into @p trg. It is cast to #LYXP_SET_EMPTY on success.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001803 * @return LY_ERR
1804 */
1805static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001806set_sorted_merge(struct lyxp_set *trg, struct lyxp_set *src)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001807{
1808 uint32_t i, j, k, count, dup_count;
1809 int cmp;
1810 const struct lyd_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001811
Michal Vaskod3678892020-05-21 10:06:58 +02001812 if ((trg->type != LYXP_SET_NODE_SET) || (src->type != LYXP_SET_NODE_SET)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001813 return LY_EINVAL;
1814 }
1815
Michal Vaskod3678892020-05-21 10:06:58 +02001816 if (!src->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001817 return LY_SUCCESS;
Michal Vaskod3678892020-05-21 10:06:58 +02001818 } else if (!trg->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001819 set_fill_set(trg, src);
Michal Vaskod3678892020-05-21 10:06:58 +02001820 lyxp_set_free_content(src);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001821 return LY_SUCCESS;
1822 }
1823
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001824 /* find first top-level node to be used as anchor for positions */
Michal Vasko0143b682021-12-13 17:13:09 +01001825 for (root = trg->tree; root->parent; root = lyd_parent(root)) {}
Michal Vaskod989ba02020-08-24 10:59:24 +02001826 for ( ; root->prev->next; root = root->prev) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001827
1828 /* fill positions */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001829 if (set_assign_pos(trg, root, trg->root_type) || set_assign_pos(src, root, src->root_type)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001830 return LY_EINT;
1831 }
1832
1833#ifndef NDEBUG
1834 LOGDBG(LY_LDGXPATH, "MERGE target");
1835 print_set_debug(trg);
1836 LOGDBG(LY_LDGXPATH, "MERGE source");
1837 print_set_debug(src);
1838#endif
1839
1840 /* make memory for the merge (duplicates are not detected yet, so space
1841 * will likely be wasted on them, too bad) */
1842 if (trg->size - trg->used < src->used) {
1843 trg->size = trg->used + src->used;
1844
1845 trg->val.nodes = ly_realloc(trg->val.nodes, trg->size * sizeof *trg->val.nodes);
1846 LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx), LY_EMEM);
1847 }
1848
1849 i = 0;
1850 j = 0;
1851 count = 0;
1852 dup_count = 0;
1853 do {
1854 cmp = set_sort_compare(&src->val.nodes[i], &trg->val.nodes[j]);
1855 if (!cmp) {
1856 if (!count) {
1857 /* duplicate, just skip it */
1858 ++i;
1859 ++j;
1860 } else {
1861 /* we are copying something already, so let's copy the duplicate too,
1862 * we are hoping that afterwards there are some more nodes to
1863 * copy and this way we can copy them all together */
1864 ++count;
1865 ++dup_count;
1866 ++i;
1867 ++j;
1868 }
1869 } else if (cmp < 0) {
1870 /* inserting src node into trg, just remember it for now */
1871 ++count;
1872 ++i;
1873
1874 /* insert the hash now */
1875 set_insert_node_hash(trg, src->val.nodes[i - 1].node, src->val.nodes[i - 1].type);
1876 } else if (count) {
1877copy_nodes:
1878 /* time to actually copy the nodes, we have found the largest block of nodes */
1879 memmove(&trg->val.nodes[j + (count - dup_count)],
1880 &trg->val.nodes[j],
1881 (trg->used - j) * sizeof *trg->val.nodes);
1882 memcpy(&trg->val.nodes[j - dup_count], &src->val.nodes[i - count], count * sizeof *src->val.nodes);
1883
1884 trg->used += count - dup_count;
1885 /* do not change i, except the copying above, we are basically doing exactly what is in the else branch below */
1886 j += count - dup_count;
1887
1888 count = 0;
1889 dup_count = 0;
1890 } else {
1891 ++j;
1892 }
1893 } while ((i < src->used) && (j < trg->used));
1894
1895 if ((i < src->used) || count) {
1896 /* insert all the hashes first */
1897 for (k = i; k < src->used; ++k) {
1898 set_insert_node_hash(trg, src->val.nodes[k].node, src->val.nodes[k].type);
1899 }
1900
1901 /* loop ended, but we need to copy something at trg end */
1902 count += src->used - i;
1903 i = src->used;
1904 goto copy_nodes;
1905 }
1906
1907 /* we are inserting hashes before the actual node insert, which causes
1908 * situations when there were initially not enough items for a hash table,
1909 * but even after some were inserted, hash table was not created (during
1910 * insertion the number of items is not updated yet) */
1911 if (!trg->ht && (trg->used >= LYD_HT_MIN_ITEMS)) {
1912 set_insert_node_hash(trg, NULL, 0);
1913 }
1914
1915#ifndef NDEBUG
1916 LOGDBG(LY_LDGXPATH, "MERGE result");
1917 print_set_debug(trg);
1918#endif
1919
Michal Vaskod3678892020-05-21 10:06:58 +02001920 lyxp_set_free_content(src);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001921 return LY_SUCCESS;
1922}
1923
Michal Vasko14676352020-05-29 11:35:55 +02001924LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02001925lyxp_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 +02001926{
Michal Vasko004d3152020-06-11 19:59:22 +02001927 if (exp->used == tok_idx) {
Michal Vasko14676352020-05-29 11:35:55 +02001928 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001929 LOGVAL(ctx, LY_VCODE_XP_EOF);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001930 }
Michal Vasko14676352020-05-29 11:35:55 +02001931 return LY_EINCOMPLETE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001932 }
1933
Michal Vasko004d3152020-06-11 19:59:22 +02001934 if (want_tok && (exp->tokens[tok_idx] != want_tok)) {
Michal Vasko14676352020-05-29 11:35:55 +02001935 if (ctx) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02001936 LOGVAL(ctx, LY_VCODE_XP_INTOK2, lyxp_token2str(exp->tokens[tok_idx]),
1937 &exp->expr[exp->tok_pos[tok_idx]], lyxp_token2str(want_tok));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001938 }
Michal Vasko14676352020-05-29 11:35:55 +02001939 return LY_ENOT;
1940 }
1941
1942 return LY_SUCCESS;
1943}
1944
Michal Vasko004d3152020-06-11 19:59:22 +02001945LY_ERR
1946lyxp_next_token(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_token want_tok)
1947{
1948 LY_CHECK_RET(lyxp_check_token(ctx, exp, *tok_idx, want_tok));
1949
1950 /* skip the token */
1951 ++(*tok_idx);
1952
1953 return LY_SUCCESS;
1954}
1955
Michal Vasko14676352020-05-29 11:35:55 +02001956/* just like lyxp_check_token() but tests for 2 tokens */
1957static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02001958exp_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 +02001959 enum lyxp_token want_tok2)
Michal Vasko14676352020-05-29 11:35:55 +02001960{
Michal Vasko004d3152020-06-11 19:59:22 +02001961 if (exp->used == tok_idx) {
Michal Vasko14676352020-05-29 11:35:55 +02001962 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001963 LOGVAL(ctx, LY_VCODE_XP_EOF);
Michal Vasko14676352020-05-29 11:35:55 +02001964 }
1965 return LY_EINCOMPLETE;
1966 }
1967
Michal Vasko004d3152020-06-11 19:59:22 +02001968 if ((exp->tokens[tok_idx] != want_tok1) && (exp->tokens[tok_idx] != want_tok2)) {
Michal Vasko14676352020-05-29 11:35:55 +02001969 if (ctx) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02001970 LOGVAL(ctx, LY_VCODE_XP_INTOK, lyxp_token2str(exp->tokens[tok_idx]),
Michal Vasko0b468e62020-10-19 09:33:04 +02001971 &exp->expr[exp->tok_pos[tok_idx]]);
Michal Vasko14676352020-05-29 11:35:55 +02001972 }
1973 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001974 }
1975
1976 return LY_SUCCESS;
1977}
1978
Michal Vasko4911eeb2021-06-28 11:23:05 +02001979LY_ERR
1980lyxp_next_token2(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_token want_tok1,
1981 enum lyxp_token want_tok2)
1982{
1983 LY_CHECK_RET(exp_check_token2(ctx, exp, *tok_idx, want_tok1, want_tok2));
1984
1985 /* skip the token */
1986 ++(*tok_idx);
1987
1988 return LY_SUCCESS;
1989}
1990
Michal Vasko03ff5a72019-09-11 13:49:33 +02001991/**
1992 * @brief Stack operation push on the repeat array.
1993 *
1994 * @param[in] exp Expression to use.
Michal Vasko004d3152020-06-11 19:59:22 +02001995 * @param[in] tok_idx Position in the expresion \p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001996 * @param[in] repeat_op_idx Index from \p exp of the operator token. This value is pushed.
1997 */
1998static void
Michal Vasko004d3152020-06-11 19:59:22 +02001999exp_repeat_push(struct lyxp_expr *exp, uint16_t tok_idx, uint16_t repeat_op_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002000{
2001 uint16_t i;
2002
Michal Vasko004d3152020-06-11 19:59:22 +02002003 if (exp->repeat[tok_idx]) {
Radek Krejci1e008d22020-08-17 11:37:37 +02002004 for (i = 0; exp->repeat[tok_idx][i]; ++i) {}
Michal Vasko004d3152020-06-11 19:59:22 +02002005 exp->repeat[tok_idx] = realloc(exp->repeat[tok_idx], (i + 2) * sizeof *exp->repeat[tok_idx]);
2006 LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), );
2007 exp->repeat[tok_idx][i] = repeat_op_idx;
2008 exp->repeat[tok_idx][i + 1] = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002009 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02002010 exp->repeat[tok_idx] = calloc(2, sizeof *exp->repeat[tok_idx]);
2011 LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), );
2012 exp->repeat[tok_idx][0] = repeat_op_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002013 }
2014}
2015
2016/**
2017 * @brief Reparse Predicate. Logs directly on error.
2018 *
2019 * [7] Predicate ::= '[' Expr ']'
2020 *
2021 * @param[in] ctx Context for logging.
2022 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002023 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002024 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002025 * @return LY_ERR
2026 */
2027static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002028reparse_predicate(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t depth)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002029{
2030 LY_ERR rc;
2031
Michal Vasko004d3152020-06-11 19:59:22 +02002032 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002033 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002034 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002035
aPiecekbf968d92021-05-27 14:35:05 +02002036 rc = reparse_or_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002037 LY_CHECK_RET(rc);
2038
Michal Vasko004d3152020-06-11 19:59:22 +02002039 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002040 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002041 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002042
2043 return LY_SUCCESS;
2044}
2045
2046/**
2047 * @brief Reparse RelativeLocationPath. Logs directly on error.
2048 *
2049 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
2050 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
2051 * [6] NodeTest ::= NameTest | NodeType '(' ')'
2052 *
2053 * @param[in] ctx Context for logging.
2054 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002055 * @param[in] tok_idx Position in the expression \p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002056 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002057 * @return LY_ERR (LY_EINCOMPLETE on forward reference)
2058 */
2059static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002060reparse_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 +02002061{
2062 LY_ERR rc;
2063
Michal Vasko004d3152020-06-11 19:59:22 +02002064 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002065 LY_CHECK_RET(rc);
2066
2067 goto step;
2068 do {
2069 /* '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02002070 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002071
Michal Vasko004d3152020-06-11 19:59:22 +02002072 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002073 LY_CHECK_RET(rc);
2074step:
2075 /* Step */
Michal Vasko004d3152020-06-11 19:59:22 +02002076 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002077 case LYXP_TOKEN_DOT:
Michal Vasko004d3152020-06-11 19:59:22 +02002078 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002079 break;
2080
2081 case LYXP_TOKEN_DDOT:
Michal Vasko004d3152020-06-11 19:59:22 +02002082 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002083 break;
2084
Michal Vasko49fec8e2022-05-24 10:28:33 +02002085 case LYXP_TOKEN_AXISNAME:
2086 ++(*tok_idx);
2087
2088 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_DCOLON);
2089 LY_CHECK_RET(rc);
2090
2091 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02002092 case LYXP_TOKEN_AT:
Michal Vasko004d3152020-06-11 19:59:22 +02002093 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002094
Michal Vasko004d3152020-06-11 19:59:22 +02002095 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002096 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002097 if ((exp->tokens[*tok_idx] != LYXP_TOKEN_NAMETEST) && (exp->tokens[*tok_idx] != LYXP_TOKEN_NODETYPE)) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02002098 LOGVAL(ctx, LY_VCODE_XP_INTOK, lyxp_token2str(exp->tokens[*tok_idx]), &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002099 return LY_EVALID;
2100 }
Michal Vasko49fec8e2022-05-24 10:28:33 +02002101 if (exp->tokens[*tok_idx] == LYXP_TOKEN_NODETYPE) {
2102 goto reparse_nodetype;
2103 }
Radek Krejci0f969882020-08-21 16:56:47 +02002104 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02002105 case LYXP_TOKEN_NAMETEST:
Michal Vasko004d3152020-06-11 19:59:22 +02002106 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002107 goto reparse_predicate;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002108
2109 case LYXP_TOKEN_NODETYPE:
Michal Vasko49fec8e2022-05-24 10:28:33 +02002110reparse_nodetype:
Michal Vasko004d3152020-06-11 19:59:22 +02002111 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002112
2113 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02002114 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002115 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002116 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002117
2118 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02002119 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002120 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002121 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002122
2123reparse_predicate:
2124 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002125 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
aPiecekbf968d92021-05-27 14:35:05 +02002126 rc = reparse_predicate(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002127 LY_CHECK_RET(rc);
2128 }
2129 break;
2130 default:
Michal Vasko49fec8e2022-05-24 10:28:33 +02002131 LOGVAL(ctx, LY_VCODE_XP_INTOK, lyxp_token2str(exp->tokens[*tok_idx]), &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002132 return LY_EVALID;
2133 }
Michal Vasko004d3152020-06-11 19:59:22 +02002134 } while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02002135
2136 return LY_SUCCESS;
2137}
2138
2139/**
2140 * @brief Reparse AbsoluteLocationPath. Logs directly on error.
2141 *
2142 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
2143 *
2144 * @param[in] ctx Context for logging.
2145 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002146 * @param[in] tok_idx Position in the expression \p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002147 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002148 * @return LY_ERR
2149 */
2150static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002151reparse_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 +02002152{
2153 LY_ERR rc;
2154
Michal Vasko004d3152020-06-11 19:59:22 +02002155 LY_CHECK_RET(exp_check_token2(ctx, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02002156
2157 /* '/' RelativeLocationPath? */
Michal Vasko004d3152020-06-11 19:59:22 +02002158 if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002159 /* '/' */
Michal Vasko004d3152020-06-11 19:59:22 +02002160 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002161
Michal Vasko004d3152020-06-11 19:59:22 +02002162 if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002163 return LY_SUCCESS;
2164 }
Michal Vasko004d3152020-06-11 19:59:22 +02002165 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002166 case LYXP_TOKEN_DOT:
2167 case LYXP_TOKEN_DDOT:
Michal Vasko49fec8e2022-05-24 10:28:33 +02002168 case LYXP_TOKEN_AXISNAME:
Michal Vasko03ff5a72019-09-11 13:49:33 +02002169 case LYXP_TOKEN_AT:
2170 case LYXP_TOKEN_NAMETEST:
2171 case LYXP_TOKEN_NODETYPE:
aPiecekbf968d92021-05-27 14:35:05 +02002172 rc = reparse_relative_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002173 LY_CHECK_RET(rc);
Radek Krejci0f969882020-08-21 16:56:47 +02002174 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02002175 default:
2176 break;
2177 }
2178
Michal Vasko03ff5a72019-09-11 13:49:33 +02002179 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02002180 /* '//' RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002181 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002182
aPiecekbf968d92021-05-27 14:35:05 +02002183 rc = reparse_relative_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002184 LY_CHECK_RET(rc);
2185 }
2186
2187 return LY_SUCCESS;
2188}
2189
2190/**
2191 * @brief Reparse FunctionCall. Logs directly on error.
2192 *
2193 * [9] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
2194 *
2195 * @param[in] ctx Context for logging.
2196 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002197 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002198 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002199 * @return LY_ERR
2200 */
2201static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002202reparse_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 +02002203{
Radek Krejci1deb5be2020-08-26 16:43:36 +02002204 int8_t min_arg_count = -1;
2205 uint32_t arg_count, max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002206 uint16_t func_tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002207 LY_ERR rc;
2208
Michal Vasko004d3152020-06-11 19:59:22 +02002209 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_FUNCNAME);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002210 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002211 func_tok_idx = *tok_idx;
2212 switch (exp->tok_len[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002213 case 3:
Michal Vasko004d3152020-06-11 19:59:22 +02002214 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002215 min_arg_count = 1;
2216 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002217 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002218 min_arg_count = 1;
2219 max_arg_count = 1;
2220 }
2221 break;
2222 case 4:
Michal Vasko004d3152020-06-11 19:59:22 +02002223 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002224 min_arg_count = 1;
2225 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002226 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002227 min_arg_count = 0;
2228 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002229 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002230 min_arg_count = 0;
2231 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002232 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002233 min_arg_count = 0;
2234 max_arg_count = 0;
2235 }
2236 break;
2237 case 5:
Michal Vasko004d3152020-06-11 19:59:22 +02002238 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002239 min_arg_count = 1;
2240 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002241 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002242 min_arg_count = 0;
2243 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002244 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002245 min_arg_count = 1;
2246 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002247 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002248 min_arg_count = 1;
2249 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002250 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002251 min_arg_count = 1;
2252 max_arg_count = 1;
2253 }
2254 break;
2255 case 6:
Michal Vasko004d3152020-06-11 19:59:22 +02002256 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002257 min_arg_count = 2;
Radek Krejci1deb5be2020-08-26 16:43:36 +02002258 max_arg_count = UINT32_MAX;
Michal Vasko004d3152020-06-11 19:59:22 +02002259 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002260 min_arg_count = 0;
2261 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002262 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002263 min_arg_count = 0;
2264 max_arg_count = 1;
2265 }
2266 break;
2267 case 7:
Michal Vasko004d3152020-06-11 19:59:22 +02002268 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002269 min_arg_count = 1;
2270 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002271 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002272 min_arg_count = 1;
2273 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002274 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002275 min_arg_count = 0;
2276 max_arg_count = 0;
2277 }
2278 break;
2279 case 8:
Michal Vasko004d3152020-06-11 19:59:22 +02002280 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002281 min_arg_count = 2;
2282 max_arg_count = 2;
Michal Vasko004d3152020-06-11 19:59:22 +02002283 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002284 min_arg_count = 0;
2285 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002286 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "re-match", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002287 min_arg_count = 2;
2288 max_arg_count = 2;
2289 }
2290 break;
2291 case 9:
Michal Vasko004d3152020-06-11 19:59:22 +02002292 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002293 min_arg_count = 2;
2294 max_arg_count = 3;
Michal Vasko004d3152020-06-11 19:59:22 +02002295 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002296 min_arg_count = 3;
2297 max_arg_count = 3;
2298 }
2299 break;
2300 case 10:
Michal Vasko004d3152020-06-11 19:59:22 +02002301 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002302 min_arg_count = 0;
2303 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002304 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002305 min_arg_count = 1;
2306 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002307 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002308 min_arg_count = 2;
2309 max_arg_count = 2;
2310 }
2311 break;
2312 case 11:
Michal Vasko004d3152020-06-11 19:59:22 +02002313 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002314 min_arg_count = 2;
2315 max_arg_count = 2;
2316 }
2317 break;
2318 case 12:
Michal Vasko004d3152020-06-11 19:59:22 +02002319 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002320 min_arg_count = 2;
2321 max_arg_count = 2;
2322 }
2323 break;
2324 case 13:
Michal Vasko004d3152020-06-11 19:59:22 +02002325 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002326 min_arg_count = 0;
2327 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002328 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string-length", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002329 min_arg_count = 0;
2330 max_arg_count = 1;
2331 }
2332 break;
2333 case 15:
Michal Vasko004d3152020-06-11 19:59:22 +02002334 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002335 min_arg_count = 0;
2336 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002337 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-after", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002338 min_arg_count = 2;
2339 max_arg_count = 2;
2340 }
2341 break;
2342 case 16:
Michal Vasko004d3152020-06-11 19:59:22 +02002343 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002344 min_arg_count = 2;
2345 max_arg_count = 2;
2346 }
2347 break;
2348 case 20:
Michal Vasko004d3152020-06-11 19:59:22 +02002349 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from-or-self", 20)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002350 min_arg_count = 2;
2351 max_arg_count = 2;
2352 }
2353 break;
2354 }
2355 if (min_arg_count == -1) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002356 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 +02002357 return LY_EINVAL;
2358 }
Michal Vasko004d3152020-06-11 19:59:22 +02002359 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002360
2361 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02002362 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002363 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002364 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002365
2366 /* ( Expr ( ',' Expr )* )? */
2367 arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002368 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002369 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002370 if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002371 ++arg_count;
aPiecekbf968d92021-05-27 14:35:05 +02002372 rc = reparse_or_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002373 LY_CHECK_RET(rc);
2374 }
Michal Vasko004d3152020-06-11 19:59:22 +02002375 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) {
2376 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002377
2378 ++arg_count;
aPiecekbf968d92021-05-27 14:35:05 +02002379 rc = reparse_or_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002380 LY_CHECK_RET(rc);
2381 }
2382
2383 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02002384 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002385 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002386 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002387
Radek Krejci857189e2020-09-01 13:26:36 +02002388 if ((arg_count < (uint32_t)min_arg_count) || (arg_count > max_arg_count)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002389 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 +02002390 return LY_EVALID;
2391 }
2392
2393 return LY_SUCCESS;
2394}
2395
2396/**
2397 * @brief Reparse PathExpr. Logs directly on error.
2398 *
2399 * [10] PathExpr ::= LocationPath | PrimaryExpr Predicate*
2400 * | PrimaryExpr Predicate* '/' RelativeLocationPath
2401 * | PrimaryExpr Predicate* '//' RelativeLocationPath
2402 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
aPiecekfba75362021-10-07 12:39:48 +02002403 * [8] PrimaryExpr ::= VariableReference | '(' Expr ')' | Literal | Number | FunctionCall
Michal Vasko03ff5a72019-09-11 13:49:33 +02002404 *
2405 * @param[in] ctx Context for logging.
2406 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002407 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002408 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002409 * @return LY_ERR
2410 */
2411static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002412reparse_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 +02002413{
2414 LY_ERR rc;
2415
Michal Vasko004d3152020-06-11 19:59:22 +02002416 if (lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko14676352020-05-29 11:35:55 +02002417 return LY_EVALID;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002418 }
2419
Michal Vasko004d3152020-06-11 19:59:22 +02002420 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002421 case LYXP_TOKEN_PAR1:
2422 /* '(' Expr ')' Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002423 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002424
aPiecekbf968d92021-05-27 14:35:05 +02002425 rc = reparse_or_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002426 LY_CHECK_RET(rc);
2427
Michal Vasko004d3152020-06-11 19:59:22 +02002428 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002429 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002430 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002431 goto predicate;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002432 case LYXP_TOKEN_DOT:
2433 case LYXP_TOKEN_DDOT:
Michal Vasko49fec8e2022-05-24 10:28:33 +02002434 case LYXP_TOKEN_AXISNAME:
Michal Vasko03ff5a72019-09-11 13:49:33 +02002435 case LYXP_TOKEN_AT:
2436 case LYXP_TOKEN_NAMETEST:
2437 case LYXP_TOKEN_NODETYPE:
2438 /* RelativeLocationPath */
aPiecekbf968d92021-05-27 14:35:05 +02002439 rc = reparse_relative_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002440 LY_CHECK_RET(rc);
2441 break;
aPiecekfba75362021-10-07 12:39:48 +02002442 case LYXP_TOKEN_VARREF:
2443 /* VariableReference */
2444 ++(*tok_idx);
2445 goto predicate;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002446 case LYXP_TOKEN_FUNCNAME:
2447 /* FunctionCall */
aPiecekbf968d92021-05-27 14:35:05 +02002448 rc = reparse_function_call(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002449 LY_CHECK_RET(rc);
2450 goto predicate;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002451 case LYXP_TOKEN_OPER_PATH:
2452 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +02002453 /* AbsoluteLocationPath */
aPiecekbf968d92021-05-27 14:35:05 +02002454 rc = reparse_absolute_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002455 LY_CHECK_RET(rc);
2456 break;
2457 case LYXP_TOKEN_LITERAL:
2458 /* Literal */
Michal Vasko004d3152020-06-11 19:59:22 +02002459 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002460 goto predicate;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002461 case LYXP_TOKEN_NUMBER:
2462 /* Number */
Michal Vasko004d3152020-06-11 19:59:22 +02002463 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002464 goto predicate;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002465 default:
Michal Vasko49fec8e2022-05-24 10:28:33 +02002466 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 +02002467 return LY_EVALID;
2468 }
2469
2470 return LY_SUCCESS;
2471
2472predicate:
2473 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002474 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
aPiecekbf968d92021-05-27 14:35:05 +02002475 rc = reparse_predicate(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002476 LY_CHECK_RET(rc);
2477 }
2478
2479 /* ('/' or '//') RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002480 if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002481
2482 /* '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02002483 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002484
aPiecekbf968d92021-05-27 14:35:05 +02002485 rc = reparse_relative_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002486 LY_CHECK_RET(rc);
2487 }
2488
2489 return LY_SUCCESS;
2490}
2491
2492/**
2493 * @brief Reparse UnaryExpr. Logs directly on error.
2494 *
2495 * [17] UnaryExpr ::= UnionExpr | '-' UnaryExpr
2496 * [18] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
2497 *
2498 * @param[in] ctx Context for logging.
2499 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002500 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002501 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002502 * @return LY_ERR
2503 */
2504static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002505reparse_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 +02002506{
2507 uint16_t prev_exp;
2508 LY_ERR rc;
2509
2510 /* ('-')* */
Michal Vasko004d3152020-06-11 19:59:22 +02002511 prev_exp = *tok_idx;
Michal Vasko69730152020-10-09 16:30:07 +02002512 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2513 (exp->expr[exp->tok_pos[*tok_idx]] == '-')) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002514 exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNARY);
Michal Vasko004d3152020-06-11 19:59:22 +02002515 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002516 }
2517
2518 /* PathExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002519 prev_exp = *tok_idx;
aPiecekbf968d92021-05-27 14:35:05 +02002520 rc = reparse_path_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002521 LY_CHECK_RET(rc);
2522
2523 /* ('|' PathExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002524 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_UNI)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002525 exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNION);
Michal Vasko004d3152020-06-11 19:59:22 +02002526 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002527
aPiecekbf968d92021-05-27 14:35:05 +02002528 rc = reparse_path_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002529 LY_CHECK_RET(rc);
2530 }
2531
2532 return LY_SUCCESS;
2533}
2534
2535/**
2536 * @brief Reparse AdditiveExpr. Logs directly on error.
2537 *
2538 * [15] AdditiveExpr ::= MultiplicativeExpr
2539 * | AdditiveExpr '+' MultiplicativeExpr
2540 * | AdditiveExpr '-' MultiplicativeExpr
2541 * [16] MultiplicativeExpr ::= UnaryExpr
2542 * | MultiplicativeExpr '*' UnaryExpr
2543 * | MultiplicativeExpr 'div' UnaryExpr
2544 * | MultiplicativeExpr 'mod' UnaryExpr
2545 *
2546 * @param[in] ctx Context for logging.
2547 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002548 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002549 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002550 * @return LY_ERR
2551 */
2552static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002553reparse_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 +02002554{
2555 uint16_t prev_add_exp, prev_mul_exp;
2556 LY_ERR rc;
2557
Michal Vasko004d3152020-06-11 19:59:22 +02002558 prev_add_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002559 goto reparse_multiplicative_expr;
2560
2561 /* ('+' / '-' MultiplicativeExpr)* */
Michal Vasko69730152020-10-09 16:30:07 +02002562 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2563 ((exp->expr[exp->tok_pos[*tok_idx]] == '+') || (exp->expr[exp->tok_pos[*tok_idx]] == '-'))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002564 exp_repeat_push(exp, prev_add_exp, LYXP_EXPR_ADDITIVE);
Michal Vasko004d3152020-06-11 19:59:22 +02002565 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002566
2567reparse_multiplicative_expr:
2568 /* UnaryExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002569 prev_mul_exp = *tok_idx;
aPiecekbf968d92021-05-27 14:35:05 +02002570 rc = reparse_unary_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002571 LY_CHECK_RET(rc);
2572
2573 /* ('*' / 'div' / 'mod' UnaryExpr)* */
Michal Vasko69730152020-10-09 16:30:07 +02002574 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2575 ((exp->expr[exp->tok_pos[*tok_idx]] == '*') || (exp->tok_len[*tok_idx] == 3))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002576 exp_repeat_push(exp, prev_mul_exp, LYXP_EXPR_MULTIPLICATIVE);
Michal Vasko004d3152020-06-11 19:59:22 +02002577 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002578
aPiecekbf968d92021-05-27 14:35:05 +02002579 rc = reparse_unary_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002580 LY_CHECK_RET(rc);
2581 }
2582 }
2583
2584 return LY_SUCCESS;
2585}
2586
2587/**
2588 * @brief Reparse EqualityExpr. Logs directly on error.
2589 *
2590 * [13] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
2591 * | EqualityExpr '!=' RelationalExpr
2592 * [14] RelationalExpr ::= AdditiveExpr
2593 * | RelationalExpr '<' AdditiveExpr
2594 * | RelationalExpr '>' AdditiveExpr
2595 * | RelationalExpr '<=' AdditiveExpr
2596 * | RelationalExpr '>=' AdditiveExpr
2597 *
2598 * @param[in] ctx Context for logging.
2599 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002600 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002601 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002602 * @return LY_ERR
2603 */
2604static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002605reparse_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 +02002606{
2607 uint16_t prev_eq_exp, prev_rel_exp;
2608 LY_ERR rc;
2609
Michal Vasko004d3152020-06-11 19:59:22 +02002610 prev_eq_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002611 goto reparse_additive_expr;
2612
2613 /* ('=' / '!=' RelationalExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002614 while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_EQUAL, LYXP_TOKEN_OPER_NEQUAL)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002615 exp_repeat_push(exp, prev_eq_exp, LYXP_EXPR_EQUALITY);
Michal Vasko004d3152020-06-11 19:59:22 +02002616 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002617
2618reparse_additive_expr:
2619 /* AdditiveExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002620 prev_rel_exp = *tok_idx;
aPiecekbf968d92021-05-27 14:35:05 +02002621 rc = reparse_additive_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002622 LY_CHECK_RET(rc);
2623
2624 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002625 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_COMP)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002626 exp_repeat_push(exp, prev_rel_exp, LYXP_EXPR_RELATIONAL);
Michal Vasko004d3152020-06-11 19:59:22 +02002627 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002628
aPiecekbf968d92021-05-27 14:35:05 +02002629 rc = reparse_additive_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002630 LY_CHECK_RET(rc);
2631 }
2632 }
2633
2634 return LY_SUCCESS;
2635}
2636
2637/**
2638 * @brief Reparse OrExpr. Logs directly on error.
2639 *
2640 * [11] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
2641 * [12] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
2642 *
2643 * @param[in] ctx Context for logging.
2644 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002645 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002646 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002647 * @return LY_ERR
2648 */
2649static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002650reparse_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 +02002651{
2652 uint16_t prev_or_exp, prev_and_exp;
2653 LY_ERR rc;
2654
aPiecekbf968d92021-05-27 14:35:05 +02002655 ++depth;
2656 LY_CHECK_ERR_RET(depth > LYXP_MAX_BLOCK_DEPTH, LOGVAL(ctx, LY_VCODE_XP_DEPTH), LY_EINVAL);
2657
Michal Vasko004d3152020-06-11 19:59:22 +02002658 prev_or_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002659 goto reparse_equality_expr;
2660
2661 /* ('or' AndExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002662 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 +02002663 exp_repeat_push(exp, prev_or_exp, LYXP_EXPR_OR);
Michal Vasko004d3152020-06-11 19:59:22 +02002664 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002665
2666reparse_equality_expr:
2667 /* EqualityExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002668 prev_and_exp = *tok_idx;
aPiecekbf968d92021-05-27 14:35:05 +02002669 rc = reparse_equality_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002670 LY_CHECK_RET(rc);
2671
2672 /* ('and' EqualityExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002673 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 +02002674 exp_repeat_push(exp, prev_and_exp, LYXP_EXPR_AND);
Michal Vasko004d3152020-06-11 19:59:22 +02002675 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002676
aPiecekbf968d92021-05-27 14:35:05 +02002677 rc = reparse_equality_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002678 LY_CHECK_RET(rc);
2679 }
2680 }
2681
2682 return LY_SUCCESS;
2683}
Radek Krejcib1646a92018-11-02 16:08:26 +01002684
2685/**
2686 * @brief Parse NCName.
2687 *
2688 * @param[in] ncname Name to parse.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002689 * @return Length of @p ncname valid bytes.
Radek Krejcib1646a92018-11-02 16:08:26 +01002690 */
Michal Vasko49fec8e2022-05-24 10:28:33 +02002691static ssize_t
Radek Krejcib1646a92018-11-02 16:08:26 +01002692parse_ncname(const char *ncname)
2693{
Radek Krejci1deb5be2020-08-26 16:43:36 +02002694 uint32_t uc;
Radek Krejcid4270262019-01-07 15:07:25 +01002695 size_t size;
Michal Vasko49fec8e2022-05-24 10:28:33 +02002696 ssize_t len = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01002697
2698 LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), 0);
2699 if (!is_xmlqnamestartchar(uc) || (uc == ':')) {
2700 return len;
2701 }
2702
2703 do {
2704 len += size;
Radek Krejci9a564c92019-01-07 14:53:57 +01002705 if (!*ncname) {
2706 break;
2707 }
Radek Krejcid4270262019-01-07 15:07:25 +01002708 LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), -len);
Radek Krejcib1646a92018-11-02 16:08:26 +01002709 } while (is_xmlqnamechar(uc) && (uc != ':'));
2710
2711 return len;
2712}
2713
2714/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02002715 * @brief Add @p token into the expression @p exp.
Radek Krejcib1646a92018-11-02 16:08:26 +01002716 *
Michal Vasko03ff5a72019-09-11 13:49:33 +02002717 * @param[in] ctx Context for logging.
Radek Krejcib1646a92018-11-02 16:08:26 +01002718 * @param[in] exp Expression to use.
2719 * @param[in] token Token to add.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002720 * @param[in] tok_pos Token position in the XPath expression.
Radek Krejcib1646a92018-11-02 16:08:26 +01002721 * @param[in] tok_len Token length in the XPath expression.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002722 * @return LY_ERR
Radek Krejcib1646a92018-11-02 16:08:26 +01002723 */
2724static LY_ERR
Michal Vasko14676352020-05-29 11:35:55 +02002725exp_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 +01002726{
2727 uint32_t prev;
2728
2729 if (exp->used == exp->size) {
2730 prev = exp->size;
2731 exp->size += LYXP_EXPR_SIZE_STEP;
2732 if (prev > exp->size) {
2733 LOGINT(ctx);
2734 return LY_EINT;
2735 }
2736
2737 exp->tokens = ly_realloc(exp->tokens, exp->size * sizeof *exp->tokens);
2738 LY_CHECK_ERR_RET(!exp->tokens, LOGMEM(ctx), LY_EMEM);
2739 exp->tok_pos = ly_realloc(exp->tok_pos, exp->size * sizeof *exp->tok_pos);
2740 LY_CHECK_ERR_RET(!exp->tok_pos, LOGMEM(ctx), LY_EMEM);
2741 exp->tok_len = ly_realloc(exp->tok_len, exp->size * sizeof *exp->tok_len);
2742 LY_CHECK_ERR_RET(!exp->tok_len, LOGMEM(ctx), LY_EMEM);
2743 }
2744
2745 exp->tokens[exp->used] = token;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002746 exp->tok_pos[exp->used] = tok_pos;
Radek Krejcib1646a92018-11-02 16:08:26 +01002747 exp->tok_len[exp->used] = tok_len;
2748 ++exp->used;
2749 return LY_SUCCESS;
2750}
2751
2752void
Michal Vasko14676352020-05-29 11:35:55 +02002753lyxp_expr_free(const struct ly_ctx *ctx, struct lyxp_expr *expr)
Radek Krejcib1646a92018-11-02 16:08:26 +01002754{
2755 uint16_t i;
2756
2757 if (!expr) {
2758 return;
2759 }
2760
2761 lydict_remove(ctx, expr->expr);
2762 free(expr->tokens);
2763 free(expr->tok_pos);
2764 free(expr->tok_len);
2765 if (expr->repeat) {
2766 for (i = 0; i < expr->used; ++i) {
2767 free(expr->repeat[i]);
2768 }
2769 }
2770 free(expr->repeat);
2771 free(expr);
2772}
2773
Michal Vasko49fec8e2022-05-24 10:28:33 +02002774/**
2775 * @brief Parse Axis name.
2776 *
2777 * @param[in] str String to parse.
2778 * @param[in] str_len Length of @p str.
2779 * @return LY_SUCCESS if an axis.
2780 * @return LY_ENOT otherwise.
2781 */
2782static LY_ERR
2783expr_parse_axis(const char *str, size_t str_len)
2784{
2785 switch (str_len) {
2786 case 4:
2787 if (!strncmp("self", str, str_len)) {
2788 return LY_SUCCESS;
2789 }
2790 break;
2791 case 5:
2792 if (!strncmp("child", str, str_len)) {
2793 return LY_SUCCESS;
2794 }
2795 break;
2796 case 6:
2797 if (!strncmp("parent", str, str_len)) {
2798 return LY_SUCCESS;
2799 }
2800 break;
2801 case 8:
2802 if (!strncmp("ancestor", str, str_len)) {
2803 return LY_SUCCESS;
2804 }
2805 break;
2806 case 9:
2807 if (!strncmp("attribute", str, str_len)) {
2808 return LY_SUCCESS;
2809 } else if (!strncmp("following", str, str_len)) {
2810 return LY_SUCCESS;
2811 } else if (!strncmp("namespace", str, str_len)) {
2812 LOGERR(NULL, LY_EINVAL, "Axis \"namespace\" not supported.");
2813 return LY_ENOT;
2814 } else if (!strncmp("preceding", str, str_len)) {
2815 return LY_SUCCESS;
2816 }
2817 break;
2818 case 10:
2819 if (!strncmp("descendant", str, str_len)) {
2820 return LY_SUCCESS;
2821 }
2822 break;
2823 case 16:
2824 if (!strncmp("ancestor-or-self", str, str_len)) {
2825 return LY_SUCCESS;
2826 }
2827 break;
2828 case 17:
2829 if (!strncmp("following-sibling", str, str_len)) {
2830 return LY_SUCCESS;
2831 } else if (!strncmp("preceding-sibling", str, str_len)) {
2832 return LY_SUCCESS;
2833 }
2834 break;
2835 case 18:
2836 if (!strncmp("descendant-or-self", str, str_len)) {
2837 return LY_SUCCESS;
2838 }
2839 break;
2840 }
2841
2842 return LY_ENOT;
2843}
2844
Radek Krejcif03a9e22020-09-18 20:09:31 +02002845LY_ERR
2846lyxp_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 +01002847{
Radek Krejcif03a9e22020-09-18 20:09:31 +02002848 LY_ERR ret = LY_SUCCESS;
2849 struct lyxp_expr *expr;
Radek Krejcid4270262019-01-07 15:07:25 +01002850 size_t parsed = 0, tok_len;
Radek Krejcib1646a92018-11-02 16:08:26 +01002851 enum lyxp_token tok_type;
Michal Vasko49fec8e2022-05-24 10:28:33 +02002852 ly_bool prev_func_check = 0, prev_ntype_check = 0, has_axis;
Michal Vasko004d3152020-06-11 19:59:22 +02002853 uint16_t tok_idx = 0;
Michal Vasko49fec8e2022-05-24 10:28:33 +02002854 ssize_t ncname_len;
Radek Krejcib1646a92018-11-02 16:08:26 +01002855
Radek Krejcif03a9e22020-09-18 20:09:31 +02002856 assert(expr_p);
2857
2858 if (!expr_str[0]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002859 LOGVAL(ctx, LY_VCODE_XP_EOF);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002860 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02002861 }
2862
2863 if (!expr_len) {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002864 expr_len = strlen(expr_str);
Michal Vasko004d3152020-06-11 19:59:22 +02002865 }
2866 if (expr_len > UINT16_MAX) {
Michal Vasko623ac7b2021-04-09 12:44:23 +02002867 LOGVAL(ctx, LYVE_XPATH, "XPath expression cannot be longer than %u characters.", UINT16_MAX);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002868 return LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01002869 }
2870
2871 /* init lyxp_expr structure */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002872 expr = calloc(1, sizeof *expr);
2873 LY_CHECK_ERR_GOTO(!expr, LOGMEM(ctx); ret = LY_EMEM, error);
2874 LY_CHECK_GOTO(ret = lydict_insert(ctx, expr_str, expr_len, &expr->expr), error);
2875 expr->used = 0;
2876 expr->size = LYXP_EXPR_SIZE_START;
2877 expr->tokens = malloc(expr->size * sizeof *expr->tokens);
2878 LY_CHECK_ERR_GOTO(!expr->tokens, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002879
Radek Krejcif03a9e22020-09-18 20:09:31 +02002880 expr->tok_pos = malloc(expr->size * sizeof *expr->tok_pos);
2881 LY_CHECK_ERR_GOTO(!expr->tok_pos, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002882
Radek Krejcif03a9e22020-09-18 20:09:31 +02002883 expr->tok_len = malloc(expr->size * sizeof *expr->tok_len);
2884 LY_CHECK_ERR_GOTO(!expr->tok_len, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002885
Michal Vasko004d3152020-06-11 19:59:22 +02002886 /* make expr 0-terminated */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002887 expr_str = expr->expr;
Michal Vasko004d3152020-06-11 19:59:22 +02002888
Radek Krejcif03a9e22020-09-18 20:09:31 +02002889 while (is_xmlws(expr_str[parsed])) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002890 ++parsed;
2891 }
2892
2893 do {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002894 if (expr_str[parsed] == '(') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002895
2896 /* '(' */
2897 tok_len = 1;
2898 tok_type = LYXP_TOKEN_PAR1;
2899
Michal Vasko49fec8e2022-05-24 10:28:33 +02002900 if (prev_ntype_check && expr->used && (expr->tokens[expr->used - 1] == LYXP_TOKEN_NAMETEST) &&
2901 (((expr->tok_len[expr->used - 1] == 4) &&
2902 (!strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "node", 4) ||
2903 !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "text", 4))) ||
2904 ((expr->tok_len[expr->used - 1] == 7) &&
2905 !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "comment", 7)))) {
2906 /* it is NodeType after all */
2907 expr->tokens[expr->used - 1] = LYXP_TOKEN_NODETYPE;
2908
2909 prev_ntype_check = 0;
2910 prev_func_check = 0;
2911 } else if (prev_func_check && expr->used && (expr->tokens[expr->used - 1] == LYXP_TOKEN_NAMETEST)) {
2912 /* it is FunctionName after all */
2913 expr->tokens[expr->used - 1] = LYXP_TOKEN_FUNCNAME;
2914
2915 prev_ntype_check = 0;
2916 prev_func_check = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01002917 }
2918
Radek Krejcif03a9e22020-09-18 20:09:31 +02002919 } else if (expr_str[parsed] == ')') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002920
2921 /* ')' */
2922 tok_len = 1;
2923 tok_type = LYXP_TOKEN_PAR2;
2924
Radek Krejcif03a9e22020-09-18 20:09:31 +02002925 } else if (expr_str[parsed] == '[') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002926
2927 /* '[' */
2928 tok_len = 1;
2929 tok_type = LYXP_TOKEN_BRACK1;
2930
Radek Krejcif03a9e22020-09-18 20:09:31 +02002931 } else if (expr_str[parsed] == ']') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002932
2933 /* ']' */
2934 tok_len = 1;
2935 tok_type = LYXP_TOKEN_BRACK2;
2936
Radek Krejcif03a9e22020-09-18 20:09:31 +02002937 } else if (!strncmp(&expr_str[parsed], "..", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002938
2939 /* '..' */
2940 tok_len = 2;
2941 tok_type = LYXP_TOKEN_DDOT;
2942
Radek Krejcif03a9e22020-09-18 20:09:31 +02002943 } else if ((expr_str[parsed] == '.') && (!isdigit(expr_str[parsed + 1]))) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002944
2945 /* '.' */
2946 tok_len = 1;
2947 tok_type = LYXP_TOKEN_DOT;
2948
Radek Krejcif03a9e22020-09-18 20:09:31 +02002949 } else if (expr_str[parsed] == '@') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002950
2951 /* '@' */
2952 tok_len = 1;
2953 tok_type = LYXP_TOKEN_AT;
2954
Radek Krejcif03a9e22020-09-18 20:09:31 +02002955 } else if (expr_str[parsed] == ',') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002956
2957 /* ',' */
2958 tok_len = 1;
2959 tok_type = LYXP_TOKEN_COMMA;
2960
Radek Krejcif03a9e22020-09-18 20:09:31 +02002961 } else if (expr_str[parsed] == '\'') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002962
2963 /* Literal with ' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002964 for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\''); ++tok_len) {}
2965 LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0',
Radek Krejci2efc45b2020-12-22 16:25:44 +01002966 LOGVAL(ctx, LY_VCODE_XP_EOE, expr_str[parsed], &expr_str[parsed]); ret = LY_EVALID,
Michal Vasko69730152020-10-09 16:30:07 +02002967 error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002968 ++tok_len;
2969 tok_type = LYXP_TOKEN_LITERAL;
2970
Radek Krejcif03a9e22020-09-18 20:09:31 +02002971 } else if (expr_str[parsed] == '\"') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002972
2973 /* Literal with " */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002974 for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\"'); ++tok_len) {}
2975 LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0',
Radek Krejci2efc45b2020-12-22 16:25:44 +01002976 LOGVAL(ctx, LY_VCODE_XP_EOE, expr_str[parsed], &expr_str[parsed]); ret = LY_EVALID,
Michal Vasko69730152020-10-09 16:30:07 +02002977 error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002978 ++tok_len;
2979 tok_type = LYXP_TOKEN_LITERAL;
2980
Radek Krejcif03a9e22020-09-18 20:09:31 +02002981 } else if ((expr_str[parsed] == '.') || (isdigit(expr_str[parsed]))) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002982
2983 /* Number */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002984 for (tok_len = 0; isdigit(expr_str[parsed + tok_len]); ++tok_len) {}
2985 if (expr_str[parsed + tok_len] == '.') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002986 ++tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02002987 for ( ; isdigit(expr_str[parsed + tok_len]); ++tok_len) {}
Radek Krejcib1646a92018-11-02 16:08:26 +01002988 }
2989 tok_type = LYXP_TOKEN_NUMBER;
2990
aPiecekfba75362021-10-07 12:39:48 +02002991 } else if (expr_str[parsed] == '$') {
2992
2993 /* VariableReference */
2994 parsed++;
Michal Vasko49fec8e2022-05-24 10:28:33 +02002995 ncname_len = parse_ncname(&expr_str[parsed]);
aPiecekfba75362021-10-07 12:39:48 +02002996 LY_CHECK_ERR_GOTO(ncname_len < 1, LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed - ncname_len],
2997 parsed - ncname_len + 1, expr_str); ret = LY_EVALID, error);
2998 tok_len = ncname_len;
2999 LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == ':',
3000 LOGVAL(ctx, LYVE_XPATH, "Variable with prefix is not supported."); ret = LY_EVALID,
3001 error);
3002 tok_type = LYXP_TOKEN_VARREF;
3003
Radek Krejcif03a9e22020-09-18 20:09:31 +02003004 } else if (expr_str[parsed] == '/') {
Radek Krejcib1646a92018-11-02 16:08:26 +01003005
3006 /* Operator '/', '//' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02003007 if (!strncmp(&expr_str[parsed], "//", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01003008 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003009 tok_type = LYXP_TOKEN_OPER_RPATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01003010 } else {
3011 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003012 tok_type = LYXP_TOKEN_OPER_PATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01003013 }
Radek Krejcib1646a92018-11-02 16:08:26 +01003014
Radek Krejcif03a9e22020-09-18 20:09:31 +02003015 } else if (!strncmp(&expr_str[parsed], "!=", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01003016
Michal Vasko3e48bf32020-06-01 08:39:07 +02003017 /* Operator '!=' */
Radek Krejcib1646a92018-11-02 16:08:26 +01003018 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003019 tok_type = LYXP_TOKEN_OPER_NEQUAL;
3020
Radek Krejcif03a9e22020-09-18 20:09:31 +02003021 } else if (!strncmp(&expr_str[parsed], "<=", 2) || !strncmp(&expr_str[parsed], ">=", 2)) {
Michal Vasko3e48bf32020-06-01 08:39:07 +02003022
3023 /* Operator '<=', '>=' */
3024 tok_len = 2;
3025 tok_type = LYXP_TOKEN_OPER_COMP;
Radek Krejcib1646a92018-11-02 16:08:26 +01003026
Radek Krejcif03a9e22020-09-18 20:09:31 +02003027 } else if (expr_str[parsed] == '|') {
Radek Krejcib1646a92018-11-02 16:08:26 +01003028
3029 /* Operator '|' */
3030 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003031 tok_type = LYXP_TOKEN_OPER_UNI;
Radek Krejcib1646a92018-11-02 16:08:26 +01003032
Radek Krejcif03a9e22020-09-18 20:09:31 +02003033 } else if ((expr_str[parsed] == '+') || (expr_str[parsed] == '-')) {
Radek Krejcib1646a92018-11-02 16:08:26 +01003034
3035 /* Operator '+', '-' */
3036 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003037 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01003038
Radek Krejcif03a9e22020-09-18 20:09:31 +02003039 } else if (expr_str[parsed] == '=') {
Radek Krejcib1646a92018-11-02 16:08:26 +01003040
Michal Vasko3e48bf32020-06-01 08:39:07 +02003041 /* Operator '=' */
Radek Krejcib1646a92018-11-02 16:08:26 +01003042 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003043 tok_type = LYXP_TOKEN_OPER_EQUAL;
3044
Radek Krejcif03a9e22020-09-18 20:09:31 +02003045 } else if ((expr_str[parsed] == '<') || (expr_str[parsed] == '>')) {
Michal Vasko3e48bf32020-06-01 08:39:07 +02003046
3047 /* Operator '<', '>' */
3048 tok_len = 1;
3049 tok_type = LYXP_TOKEN_OPER_COMP;
Radek Krejcib1646a92018-11-02 16:08:26 +01003050
Michal Vasko69730152020-10-09 16:30:07 +02003051 } else if (expr->used && (expr->tokens[expr->used - 1] != LYXP_TOKEN_AT) &&
3052 (expr->tokens[expr->used - 1] != LYXP_TOKEN_PAR1) &&
3053 (expr->tokens[expr->used - 1] != LYXP_TOKEN_BRACK1) &&
3054 (expr->tokens[expr->used - 1] != LYXP_TOKEN_COMMA) &&
3055 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_LOG) &&
3056 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_EQUAL) &&
3057 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_NEQUAL) &&
3058 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_COMP) &&
3059 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_MATH) &&
3060 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_UNI) &&
3061 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_PATH) &&
3062 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_RPATH)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01003063
3064 /* Operator '*', 'or', 'and', 'mod', or 'div' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02003065 if (expr_str[parsed] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01003066 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003067 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01003068
Radek Krejcif03a9e22020-09-18 20:09:31 +02003069 } else if (!strncmp(&expr_str[parsed], "or", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01003070 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003071 tok_type = LYXP_TOKEN_OPER_LOG;
Radek Krejcib1646a92018-11-02 16:08:26 +01003072
Radek Krejcif03a9e22020-09-18 20:09:31 +02003073 } else if (!strncmp(&expr_str[parsed], "and", 3)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01003074 tok_len = 3;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003075 tok_type = LYXP_TOKEN_OPER_LOG;
Radek Krejcib1646a92018-11-02 16:08:26 +01003076
Radek Krejcif03a9e22020-09-18 20:09:31 +02003077 } else if (!strncmp(&expr_str[parsed], "mod", 3) || !strncmp(&expr_str[parsed], "div", 3)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01003078 tok_len = 3;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003079 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01003080
Michal Vasko49fec8e2022-05-24 10:28:33 +02003081 } else if (prev_ntype_check || prev_func_check) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003082 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 +02003083 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 +02003084 ret = LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01003085 goto error;
3086 } else {
Michal Vasko774ce402021-04-14 15:35:06 +02003087 LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed], parsed + 1, expr_str);
Radek Krejcif03a9e22020-09-18 20:09:31 +02003088 ret = LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01003089 goto error;
3090 }
Radek Krejcib1646a92018-11-02 16:08:26 +01003091 } else {
3092
Michal Vasko49fec8e2022-05-24 10:28:33 +02003093 /* (AxisName '::')? ((NCName ':')? '*' | QName) or NodeType/FunctionName */
3094 if (expr_str[parsed] == '*') {
3095 ncname_len = 1;
3096 } else {
3097 ncname_len = parse_ncname(&expr_str[parsed]);
3098 LY_CHECK_ERR_GOTO(ncname_len < 1, LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed - ncname_len],
3099 parsed - ncname_len + 1, expr_str); ret = LY_EVALID, error);
3100 }
Radek Krejcib1646a92018-11-02 16:08:26 +01003101 tok_len = ncname_len;
3102
Michal Vasko49fec8e2022-05-24 10:28:33 +02003103 has_axis = 0;
3104 if (!strncmp(&expr_str[parsed + tok_len], "::", 2)) {
3105 /* axis */
3106 LY_CHECK_ERR_GOTO(expr_parse_axis(&expr_str[parsed], ncname_len),
3107 LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed], parsed + 1, expr_str); ret = LY_EVALID, error);
3108 tok_type = LYXP_TOKEN_AXISNAME;
3109
3110 LY_CHECK_GOTO(ret = exp_add_token(ctx, expr, tok_type, parsed, tok_len), error);
3111 parsed += tok_len;
3112
3113 /* '::' */
3114 tok_len = 2;
3115 tok_type = LYXP_TOKEN_DCOLON;
3116
3117 LY_CHECK_GOTO(ret = exp_add_token(ctx, expr, tok_type, parsed, tok_len), error);
3118 parsed += tok_len;
3119
3120 if (expr_str[parsed] == '*') {
3121 ncname_len = 1;
3122 } else {
3123 ncname_len = parse_ncname(&expr_str[parsed]);
3124 LY_CHECK_ERR_GOTO(ncname_len < 1, LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed - ncname_len],
3125 parsed - ncname_len + 1, expr_str); ret = LY_EVALID, error);
3126 }
3127 tok_len = ncname_len;
3128
3129 has_axis = 1;
3130 }
3131
Radek Krejcif03a9e22020-09-18 20:09:31 +02003132 if (expr_str[parsed + tok_len] == ':') {
Radek Krejcib1646a92018-11-02 16:08:26 +01003133 ++tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02003134 if (expr_str[parsed + tok_len] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01003135 ++tok_len;
3136 } else {
Radek Krejcif03a9e22020-09-18 20:09:31 +02003137 ncname_len = parse_ncname(&expr_str[parsed + tok_len]);
Michal Vaskoe2be5462021-08-04 10:49:42 +02003138 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 +02003139 parsed - ncname_len + 1, expr_str); ret = LY_EVALID, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01003140 tok_len += ncname_len;
3141 }
Michal Vasko49fec8e2022-05-24 10:28:33 +02003142 /* remove old flags to prevent ambiguities */
3143 prev_ntype_check = 0;
3144 prev_func_check = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01003145 tok_type = LYXP_TOKEN_NAMETEST;
3146 } else {
Michal Vasko49fec8e2022-05-24 10:28:33 +02003147 /* if not '*', there is no prefix so it can still be NodeType/FunctionName, we can't finally decide now */
3148 prev_ntype_check = (expr_str[parsed] == '*') ? 0 : 1;
3149 prev_func_check = (prev_ntype_check && !has_axis) ? 1 : 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01003150 tok_type = LYXP_TOKEN_NAMETEST;
3151 }
3152 }
3153
3154 /* store the token, move on to the next one */
Radek Krejcif03a9e22020-09-18 20:09:31 +02003155 LY_CHECK_GOTO(ret = exp_add_token(ctx, expr, tok_type, parsed, tok_len), error);
Radek Krejcib1646a92018-11-02 16:08:26 +01003156 parsed += tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02003157 while (is_xmlws(expr_str[parsed])) {
Radek Krejcib1646a92018-11-02 16:08:26 +01003158 ++parsed;
3159 }
3160
Radek Krejcif03a9e22020-09-18 20:09:31 +02003161 } while (expr_str[parsed]);
Radek Krejcib1646a92018-11-02 16:08:26 +01003162
Michal Vasko004d3152020-06-11 19:59:22 +02003163 if (reparse) {
3164 /* prealloc repeat */
Radek Krejcif03a9e22020-09-18 20:09:31 +02003165 expr->repeat = calloc(expr->size, sizeof *expr->repeat);
3166 LY_CHECK_ERR_GOTO(!expr->repeat, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01003167
Michal Vasko004d3152020-06-11 19:59:22 +02003168 /* fill repeat */
aPiecekbf968d92021-05-27 14:35:05 +02003169 LY_CHECK_ERR_GOTO(reparse_or_expr(ctx, expr, &tok_idx, 0), ret = LY_EVALID, error);
Radek Krejcif03a9e22020-09-18 20:09:31 +02003170 if (expr->used > tok_idx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003171 LOGVAL(ctx, LYVE_XPATH, "Unparsed characters \"%s\" left at the end of an XPath expression.",
Michal Vasko69730152020-10-09 16:30:07 +02003172 &expr->expr[expr->tok_pos[tok_idx]]);
Radek Krejcif03a9e22020-09-18 20:09:31 +02003173 ret = LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02003174 goto error;
3175 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003176 }
3177
Radek Krejcif03a9e22020-09-18 20:09:31 +02003178 print_expr_struct_debug(expr);
3179 *expr_p = expr;
3180 return LY_SUCCESS;
Radek Krejcib1646a92018-11-02 16:08:26 +01003181
3182error:
Radek Krejcif03a9e22020-09-18 20:09:31 +02003183 lyxp_expr_free(ctx, expr);
3184 return ret;
Radek Krejcib1646a92018-11-02 16:08:26 +01003185}
3186
Michal Vasko1734be92020-09-22 08:55:10 +02003187LY_ERR
Michal Vaskoe33134a2022-07-29 14:54:40 +02003188lyxp_expr_dup(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t start_idx, uint16_t end_idx,
3189 struct lyxp_expr **dup_p)
Michal Vasko004d3152020-06-11 19:59:22 +02003190{
Michal Vasko1734be92020-09-22 08:55:10 +02003191 LY_ERR ret = LY_SUCCESS;
3192 struct lyxp_expr *dup = NULL;
Michal Vaskoe33134a2022-07-29 14:54:40 +02003193 uint16_t used = 0, i, j, k, expr_len;
3194 const char *expr_start;
3195
3196 assert((!start_idx && !end_idx) || ((start_idx < exp->used) && (end_idx < exp->used) && (start_idx <= end_idx)));
Michal Vasko004d3152020-06-11 19:59:22 +02003197
Michal Vasko7f45cf22020-10-01 12:49:44 +02003198 if (!exp) {
3199 goto cleanup;
3200 }
3201
Michal Vaskoe33134a2022-07-29 14:54:40 +02003202 if (!start_idx && !end_idx) {
3203 end_idx = exp->used - 1;
3204 }
3205
3206 expr_start = exp->expr + exp->tok_pos[start_idx];
3207 expr_len = (exp->tok_pos[end_idx] + exp->tok_len[end_idx]) - exp->tok_pos[start_idx];
3208
Michal Vasko004d3152020-06-11 19:59:22 +02003209 dup = calloc(1, sizeof *dup);
Michal Vasko1734be92020-09-22 08:55:10 +02003210 LY_CHECK_ERR_GOTO(!dup, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02003211
Michal Vasko08e9b112021-06-11 15:41:17 +02003212 if (exp->used) {
Michal Vaskoe33134a2022-07-29 14:54:40 +02003213 used = (end_idx - start_idx) + 1;
3214
3215 dup->tokens = malloc(used * sizeof *dup->tokens);
Michal Vasko08e9b112021-06-11 15:41:17 +02003216 LY_CHECK_ERR_GOTO(!dup->tokens, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vaskoe33134a2022-07-29 14:54:40 +02003217 memcpy(dup->tokens, exp->tokens + start_idx, used * sizeof *dup->tokens);
Michal Vasko004d3152020-06-11 19:59:22 +02003218
Michal Vaskoe33134a2022-07-29 14:54:40 +02003219 dup->tok_pos = malloc(used * sizeof *dup->tok_pos);
Michal Vasko08e9b112021-06-11 15:41:17 +02003220 LY_CHECK_ERR_GOTO(!dup->tok_pos, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vaskoe33134a2022-07-29 14:54:40 +02003221 memcpy(dup->tok_pos, exp->tok_pos + start_idx, used * sizeof *dup->tok_pos);
Michal Vasko004d3152020-06-11 19:59:22 +02003222
Michal Vaskoe33134a2022-07-29 14:54:40 +02003223 if (start_idx) {
3224 /* fix the indices in the expression */
3225 for (i = 0; i < used; ++i) {
3226 dup->tok_pos[i] -= expr_start - exp->expr;
3227 }
3228 }
3229
3230 dup->tok_len = malloc(used * sizeof *dup->tok_len);
Michal Vasko08e9b112021-06-11 15:41:17 +02003231 LY_CHECK_ERR_GOTO(!dup->tok_len, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vaskoe33134a2022-07-29 14:54:40 +02003232 memcpy(dup->tok_len, exp->tok_len + start_idx, used * sizeof *dup->tok_len);
Michal Vasko004d3152020-06-11 19:59:22 +02003233
Michal Vasko79a7a872022-06-17 09:00:48 +02003234 if (exp->repeat) {
Michal Vaskoe33134a2022-07-29 14:54:40 +02003235 dup->repeat = malloc(used * sizeof *dup->repeat);
Michal Vasko79a7a872022-06-17 09:00:48 +02003236 LY_CHECK_ERR_GOTO(!dup->repeat, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vaskoe33134a2022-07-29 14:54:40 +02003237 for (i = start_idx; i <= end_idx; ++i) {
Michal Vasko79a7a872022-06-17 09:00:48 +02003238 if (!exp->repeat[i]) {
Michal Vaskoe33134a2022-07-29 14:54:40 +02003239 dup->repeat[i - start_idx] = NULL;
Michal Vasko79a7a872022-06-17 09:00:48 +02003240 } else {
3241 for (j = 0; exp->repeat[i][j]; ++j) {}
3242 /* the ending 0 as well */
3243 ++j;
Michal Vasko004d3152020-06-11 19:59:22 +02003244
Michal Vaskoe33134a2022-07-29 14:54:40 +02003245 dup->repeat[i - start_idx] = malloc(j * sizeof **dup->repeat);
3246 LY_CHECK_ERR_GOTO(!dup->repeat[i - start_idx], LOGMEM(ctx); ret = LY_EMEM, cleanup);
3247 memcpy(dup->repeat[i - start_idx], exp->repeat[i], j * sizeof **dup->repeat);
3248 dup->repeat[i - start_idx][j - 1] = 0;
3249
3250 if (start_idx) {
3251 /* fix the indices in the tokens */
3252 for (k = 0; k < j; ++k) {
3253 dup->repeat[i - start_idx][k] -= start_idx;
3254 }
3255 }
Michal Vasko79a7a872022-06-17 09:00:48 +02003256 }
Michal Vasko08e9b112021-06-11 15:41:17 +02003257 }
Michal Vasko004d3152020-06-11 19:59:22 +02003258 }
3259 }
3260
Michal Vaskoe33134a2022-07-29 14:54:40 +02003261 dup->used = used;
3262 dup->size = used;
3263
3264 /* copy only subexpression */
3265 LY_CHECK_GOTO(ret = lydict_insert(ctx, expr_start, expr_len, &dup->expr), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02003266
Michal Vasko1734be92020-09-22 08:55:10 +02003267cleanup:
3268 if (ret) {
3269 lyxp_expr_free(ctx, dup);
3270 } else {
3271 *dup_p = dup;
3272 }
3273 return ret;
Michal Vasko004d3152020-06-11 19:59:22 +02003274}
3275
Michal Vasko03ff5a72019-09-11 13:49:33 +02003276/**
3277 * @brief Get the last-added schema node that is currently in the context.
3278 *
3279 * @param[in] set Set to search in.
3280 * @return Last-added schema context node, NULL if no node is in context.
3281 */
3282static struct lysc_node *
3283warn_get_scnode_in_ctx(struct lyxp_set *set)
3284{
3285 uint32_t i;
3286
3287 if (!set || (set->type != LYXP_SET_SCNODE_SET)) {
3288 return NULL;
3289 }
3290
3291 i = set->used;
3292 do {
3293 --i;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003294 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003295 /* if there are more, simply return the first found (last added) */
3296 return set->val.scnodes[i].scnode;
3297 }
3298 } while (i);
3299
3300 return NULL;
3301}
3302
3303/**
3304 * @brief Test whether a type is numeric - integer type or decimal64.
3305 *
3306 * @param[in] type Type to test.
Radek Krejci857189e2020-09-01 13:26:36 +02003307 * @return Boolean value whether @p type is numeric type or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003308 */
Radek Krejci857189e2020-09-01 13:26:36 +02003309static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003310warn_is_numeric_type(struct lysc_type *type)
3311{
3312 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003313 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003314 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003315
3316 switch (type->basetype) {
3317 case LY_TYPE_DEC64:
3318 case LY_TYPE_INT8:
3319 case LY_TYPE_UINT8:
3320 case LY_TYPE_INT16:
3321 case LY_TYPE_UINT16:
3322 case LY_TYPE_INT32:
3323 case LY_TYPE_UINT32:
3324 case LY_TYPE_INT64:
3325 case LY_TYPE_UINT64:
3326 return 1;
3327 case LY_TYPE_UNION:
3328 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003329 LY_ARRAY_FOR(uni->types, u) {
3330 ret = warn_is_numeric_type(uni->types[u]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003331 if (ret) {
3332 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003333 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003334 }
3335 }
3336 /* did not find any suitable type */
3337 return 0;
3338 case LY_TYPE_LEAFREF:
3339 return warn_is_numeric_type(((struct lysc_type_leafref *)type)->realtype);
3340 default:
3341 return 0;
3342 }
3343}
3344
3345/**
3346 * @brief Test whether a type is string-like - no integers, decimal64 or binary.
3347 *
3348 * @param[in] type Type to test.
Radek Krejci857189e2020-09-01 13:26:36 +02003349 * @return Boolean value whether @p type's basetype is string type or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003350 */
Radek Krejci857189e2020-09-01 13:26:36 +02003351static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003352warn_is_string_type(struct lysc_type *type)
3353{
3354 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003355 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003356 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003357
3358 switch (type->basetype) {
3359 case LY_TYPE_BITS:
3360 case LY_TYPE_ENUM:
3361 case LY_TYPE_IDENT:
3362 case LY_TYPE_INST:
3363 case LY_TYPE_STRING:
3364 return 1;
3365 case LY_TYPE_UNION:
3366 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003367 LY_ARRAY_FOR(uni->types, u) {
3368 ret = warn_is_string_type(uni->types[u]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003369 if (ret) {
3370 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003371 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003372 }
3373 }
3374 /* did not find any suitable type */
3375 return 0;
3376 case LY_TYPE_LEAFREF:
3377 return warn_is_string_type(((struct lysc_type_leafref *)type)->realtype);
3378 default:
3379 return 0;
3380 }
3381}
3382
3383/**
3384 * @brief Test whether a type is one specific type.
3385 *
3386 * @param[in] type Type to test.
3387 * @param[in] base Expected type.
Radek Krejci857189e2020-09-01 13:26:36 +02003388 * @return Boolean value whether the given @p type is of the specific basetype @p base.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003389 */
Radek Krejci857189e2020-09-01 13:26:36 +02003390static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003391warn_is_specific_type(struct lysc_type *type, LY_DATA_TYPE base)
3392{
3393 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003394 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003395 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003396
3397 if (type->basetype == base) {
3398 return 1;
3399 } else if (type->basetype == LY_TYPE_UNION) {
3400 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003401 LY_ARRAY_FOR(uni->types, u) {
3402 ret = warn_is_specific_type(uni->types[u], base);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003403 if (ret) {
3404 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003405 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003406 }
3407 }
3408 /* did not find any suitable type */
3409 return 0;
3410 } else if (type->basetype == LY_TYPE_LEAFREF) {
3411 return warn_is_specific_type(((struct lysc_type_leafref *)type)->realtype, base);
3412 }
3413
3414 return 0;
3415}
3416
3417/**
3418 * @brief Get next type of a (union) type.
3419 *
3420 * @param[in] type Base type.
3421 * @param[in] prev_type Previously returned type.
3422 * @return Next type or NULL.
3423 */
3424static struct lysc_type *
3425warn_is_equal_type_next_type(struct lysc_type *type, struct lysc_type *prev_type)
3426{
3427 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003428 ly_bool found = 0;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003429 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003430
3431 switch (type->basetype) {
3432 case LY_TYPE_UNION:
3433 uni = (struct lysc_type_union *)type;
3434 if (!prev_type) {
3435 return uni->types[0];
3436 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003437 LY_ARRAY_FOR(uni->types, u) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003438 if (found) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003439 return uni->types[u];
Michal Vasko03ff5a72019-09-11 13:49:33 +02003440 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003441 if (prev_type == uni->types[u]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003442 found = 1;
3443 }
3444 }
3445 return NULL;
3446 default:
3447 if (prev_type) {
3448 assert(type == prev_type);
3449 return NULL;
3450 } else {
3451 return type;
3452 }
3453 }
3454}
3455
3456/**
3457 * @brief Test whether 2 types have a common type.
3458 *
3459 * @param[in] type1 First type.
3460 * @param[in] type2 Second type.
3461 * @return 1 if they do, 0 otherwise.
3462 */
3463static int
3464warn_is_equal_type(struct lysc_type *type1, struct lysc_type *type2)
3465{
3466 struct lysc_type *t1, *rt1, *t2, *rt2;
3467
3468 t1 = NULL;
3469 while ((t1 = warn_is_equal_type_next_type(type1, t1))) {
3470 if (t1->basetype == LY_TYPE_LEAFREF) {
3471 rt1 = ((struct lysc_type_leafref *)t1)->realtype;
3472 } else {
3473 rt1 = t1;
3474 }
3475
3476 t2 = NULL;
3477 while ((t2 = warn_is_equal_type_next_type(type2, t2))) {
3478 if (t2->basetype == LY_TYPE_LEAFREF) {
3479 rt2 = ((struct lysc_type_leafref *)t2)->realtype;
3480 } else {
3481 rt2 = t2;
3482 }
3483
3484 if (rt2->basetype == rt1->basetype) {
3485 /* match found */
3486 return 1;
3487 }
3488 }
3489 }
3490
3491 return 0;
3492}
3493
3494/**
Michal Vaskoaa956522021-11-11 10:45:34 +01003495 * @brief Print warning with information about the XPath subexpression that caused previous warning.
3496 *
3497 * @param[in] ctx Context for logging.
3498 * @param[in] tok_pos Index of the subexpression in the whole expression.
3499 * @param[in] subexpr Subexpression start.
3500 * @param[in] subexpr_len Length of @p subexpr to print.
3501 * @param[in] cur_scnode Expression context node.
3502 */
3503static void
3504warn_subexpr_log(const struct ly_ctx *ctx, uint16_t tok_pos, const char *subexpr, int subexpr_len,
3505 const struct lysc_node *cur_scnode)
3506{
3507 char *path;
3508
3509 path = lysc_path(cur_scnode, LYSC_PATH_LOG, NULL, 0);
3510 LOGWRN(ctx, "Previous warning generated by XPath subexpression[%" PRIu16 "] \"%.*s\" with context node \"%s\".",
3511 tok_pos, subexpr_len, subexpr, path);
3512 free(path);
3513}
3514
3515/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02003516 * @brief Check both operands of comparison operators.
3517 *
3518 * @param[in] ctx Context for errors.
3519 * @param[in] set1 First operand set.
3520 * @param[in] set2 Second operand set.
3521 * @param[in] numbers_only Whether accept only numbers or other types are fine too (for '=' and '!=').
3522 * @param[in] expr Start of the expression to print with the warning.
3523 * @param[in] tok_pos Token position.
3524 */
3525static void
Michal Vaskoaa956522021-11-11 10:45:34 +01003526warn_operands(struct ly_ctx *ctx, struct lyxp_set *set1, struct lyxp_set *set2, ly_bool numbers_only, const char *expr,
3527 uint16_t tok_pos)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003528{
3529 struct lysc_node_leaf *node1, *node2;
Radek Krejci857189e2020-09-01 13:26:36 +02003530 ly_bool leaves = 1, warning = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003531
3532 node1 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set1);
3533 node2 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set2);
3534
3535 if (!node1 && !node2) {
3536 /* no node-sets involved, nothing to do */
3537 return;
3538 }
3539
3540 if (node1) {
3541 if (!(node1->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3542 LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node1->nodetype), node1->name);
3543 warning = 1;
3544 leaves = 0;
3545 } else if (numbers_only && !warn_is_numeric_type(node1->type)) {
3546 LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node1->name);
3547 warning = 1;
3548 }
3549 }
3550
3551 if (node2) {
3552 if (!(node2->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3553 LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node2->nodetype), node2->name);
3554 warning = 1;
3555 leaves = 0;
3556 } else if (numbers_only && !warn_is_numeric_type(node2->type)) {
3557 LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node2->name);
3558 warning = 1;
3559 }
3560 }
3561
3562 if (node1 && node2 && leaves && !numbers_only) {
Michal Vasko69730152020-10-09 16:30:07 +02003563 if ((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_numeric_type(node1->type) && !warn_is_numeric_type(node2->type) &&
3566 !warn_is_equal_type(node1->type, node2->type))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003567 LOGWRN(ctx, "Incompatible types of operands \"%s\" and \"%s\" for comparison.", node1->name, node2->name);
3568 warning = 1;
3569 }
3570 }
3571
3572 if (warning) {
Michal Vaskoaa956522021-11-11 10:45:34 +01003573 warn_subexpr_log(ctx, tok_pos, expr + tok_pos, 20, set1->cur_scnode);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003574 }
3575}
3576
3577/**
3578 * @brief Check that a value is valid for a leaf. If not applicable, does nothing.
3579 *
3580 * @param[in] exp Parsed XPath expression.
3581 * @param[in] set Set with the leaf/leaf-list.
3582 * @param[in] val_exp Index of the value (literal/number) in @p exp.
3583 * @param[in] equal_exp Index of the start of the equality expression in @p exp.
3584 * @param[in] last_equal_exp Index of the end of the equality expression in @p exp.
3585 */
3586static void
Michal Vasko40308e72020-10-20 16:38:40 +02003587warn_equality_value(const struct lyxp_expr *exp, struct lyxp_set *set, uint16_t val_exp, uint16_t equal_exp,
3588 uint16_t last_equal_exp)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003589{
3590 struct lysc_node *scnode;
3591 struct lysc_type *type;
3592 char *value;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003593 struct lyd_value storage;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003594 LY_ERR rc;
3595 struct ly_err_item *err = NULL;
3596
Michal Vasko69730152020-10-09 16:30:07 +02003597 if ((scnode = warn_get_scnode_in_ctx(set)) && (scnode->nodetype & (LYS_LEAF | LYS_LEAFLIST)) &&
3598 ((exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) || (exp->tokens[val_exp] == LYXP_TOKEN_NUMBER))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003599 /* check that the node can have the specified value */
3600 if (exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) {
3601 value = strndup(exp->expr + exp->tok_pos[val_exp] + 1, exp->tok_len[val_exp] - 2);
3602 } else {
3603 value = strndup(exp->expr + exp->tok_pos[val_exp], exp->tok_len[val_exp]);
3604 }
3605 if (!value) {
3606 LOGMEM(set->ctx);
3607 return;
3608 }
3609
3610 if ((((struct lysc_node_leaf *)scnode)->type->basetype == LY_TYPE_IDENT) && !strchr(value, ':')) {
3611 LOGWRN(set->ctx, "Identityref \"%s\" comparison with identity \"%s\" without prefix, consider adding"
Michal Vasko69730152020-10-09 16:30:07 +02003612 " a prefix or best using \"derived-from(-or-self)()\" functions.", scnode->name, value);
Michal Vaskoaa956522021-11-11 10:45:34 +01003613 warn_subexpr_log(set->ctx, exp->tok_pos[equal_exp], exp->expr + exp->tok_pos[equal_exp],
Radek Krejci0f969882020-08-21 16:56:47 +02003614 (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp],
Michal Vaskoaa956522021-11-11 10:45:34 +01003615 set->cur_scnode);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003616 }
3617
3618 type = ((struct lysc_node_leaf *)scnode)->type;
3619 if (type->basetype != LY_TYPE_IDENT) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003620 rc = type->plugin->store(set->ctx, type, value, strlen(value), 0, set->format, set->prefix_data,
Michal Vasko405cc9e2020-12-01 12:01:27 +01003621 LYD_HINT_DATA, scnode, &storage, NULL, &err);
Michal Vaskobf42e832020-11-23 16:59:42 +01003622 if (rc == LY_EINCOMPLETE) {
3623 rc = LY_SUCCESS;
3624 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003625
3626 if (err) {
3627 LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type (%s).", value, err->msg);
3628 ly_err_free(err);
3629 } else if (rc != LY_SUCCESS) {
3630 LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type.", value);
3631 }
3632 if (rc != LY_SUCCESS) {
Michal Vaskoaa956522021-11-11 10:45:34 +01003633 warn_subexpr_log(set->ctx, exp->tok_pos[equal_exp], exp->expr + exp->tok_pos[equal_exp],
Radek Krejci0f969882020-08-21 16:56:47 +02003634 (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp],
Michal Vaskoaa956522021-11-11 10:45:34 +01003635 set->cur_scnode);
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003636 } else {
3637 type->plugin->free(set->ctx, &storage);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003638 }
3639 }
3640 free(value);
3641 }
3642}
3643
3644/*
3645 * XPath functions
3646 */
3647
3648/**
3649 * @brief Execute the YANG 1.1 bit-is-set(node-set, string) function. Returns LYXP_SET_BOOLEAN
3650 * depending on whether the first node bit value from the second argument is set.
3651 *
3652 * @param[in] args Array of arguments.
3653 * @param[in] arg_count Count of elements in @p args.
3654 * @param[in,out] set Context and result set at the same time.
3655 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003656 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003657 */
3658static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003659xpath_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 +02003660{
3661 struct lyd_node_term *leaf;
3662 struct lysc_node_leaf *sleaf;
Michal Vasko2588b952021-07-29 07:43:26 +02003663 struct lyd_value_bits *bits;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003664 LY_ERR rc = LY_SUCCESS;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003665 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003666
3667 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003668 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003669 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003670 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3671 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3672 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3673 sleaf->name);
3674 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_BITS)) {
3675 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"bits\".", __func__, sleaf->name);
3676 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003677 }
3678
3679 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3680 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003681 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3682 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003683 } else if (!warn_is_string_type(sleaf->type)) {
3684 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003685 }
3686 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003687 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003688 return rc;
3689 }
3690
Michal Vaskod3678892020-05-21 10:06:58 +02003691 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003692 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 +02003693 return LY_EVALID;
3694 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003695 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003696 LY_CHECK_RET(rc);
3697
3698 set_fill_boolean(set, 0);
Michal Vaskod3678892020-05-21 10:06:58 +02003699 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003700 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
Michal Vasko2588b952021-07-29 07:43:26 +02003701 if ((leaf->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (leaf->value.realtype->basetype == LY_TYPE_BITS)) {
3702 LYD_VALUE_GET(&leaf->value, bits);
3703 LY_ARRAY_FOR(bits->items, u) {
3704 if (!strcmp(bits->items[u]->name, args[1]->val.str)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003705 set_fill_boolean(set, 1);
3706 break;
3707 }
3708 }
3709 }
3710 }
3711
3712 return LY_SUCCESS;
3713}
3714
3715/**
3716 * @brief Execute the XPath boolean(object) function. Returns LYXP_SET_BOOLEAN
3717 * with the argument converted to boolean.
3718 *
3719 * @param[in] args Array of arguments.
3720 * @param[in] arg_count Count of elements in @p args.
3721 * @param[in,out] set Context and result set at the same time.
3722 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003723 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003724 */
3725static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003726xpath_boolean(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003727{
3728 LY_ERR rc;
3729
3730 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02003731 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003732 return LY_SUCCESS;
3733 }
3734
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003735 rc = lyxp_set_cast(args[0], LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003736 LY_CHECK_RET(rc);
3737 set_fill_set(set, args[0]);
3738
3739 return LY_SUCCESS;
3740}
3741
3742/**
3743 * @brief Execute the XPath ceiling(number) function. Returns LYXP_SET_NUMBER
3744 * with the first argument rounded up to the nearest integer.
3745 *
3746 * @param[in] args Array of arguments.
3747 * @param[in] arg_count Count of elements in @p args.
3748 * @param[in,out] set Context and result set at the same time.
3749 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003750 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003751 */
3752static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003753xpath_ceiling(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003754{
3755 struct lysc_node_leaf *sleaf;
3756 LY_ERR rc = LY_SUCCESS;
3757
3758 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003759 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003760 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003761 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3762 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3763 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3764 sleaf->name);
3765 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
3766 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
3767 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003768 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003769 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003770 return rc;
3771 }
3772
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003773 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003774 LY_CHECK_RET(rc);
3775 if ((long long)args[0]->val.num != args[0]->val.num) {
3776 set_fill_number(set, ((long long)args[0]->val.num) + 1);
3777 } else {
3778 set_fill_number(set, args[0]->val.num);
3779 }
3780
3781 return LY_SUCCESS;
3782}
3783
3784/**
3785 * @brief Execute the XPath concat(string, string, string*) function.
3786 * Returns LYXP_SET_STRING with the concatenation of all the arguments.
3787 *
3788 * @param[in] args Array of arguments.
3789 * @param[in] arg_count Count of elements in @p args.
3790 * @param[in,out] set Context and result set at the same time.
3791 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003792 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003793 */
3794static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003795xpath_concat(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003796{
3797 uint16_t i;
3798 char *str = NULL;
3799 size_t used = 1;
3800 LY_ERR rc = LY_SUCCESS;
3801 struct lysc_node_leaf *sleaf;
3802
3803 if (options & LYXP_SCNODE_ALL) {
3804 for (i = 0; i < arg_count; ++i) {
3805 if ((args[i]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[i]))) {
3806 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3807 LOGWRN(set->ctx, "Argument #%u of %s is a %s node \"%s\".",
Michal Vasko69730152020-10-09 16:30:07 +02003808 i + 1, __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003809 } else if (!warn_is_string_type(sleaf->type)) {
Radek Krejci70124c82020-08-14 22:17:03 +02003810 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 +02003811 }
3812 }
3813 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003814 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003815 return rc;
3816 }
3817
3818 for (i = 0; i < arg_count; ++i) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003819 rc = lyxp_set_cast(args[i], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003820 if (rc != LY_SUCCESS) {
3821 free(str);
3822 return rc;
3823 }
3824
3825 str = ly_realloc(str, (used + strlen(args[i]->val.str)) * sizeof(char));
3826 LY_CHECK_ERR_RET(!str, LOGMEM(set->ctx), LY_EMEM);
3827 strcpy(str + used - 1, args[i]->val.str);
3828 used += strlen(args[i]->val.str);
3829 }
3830
3831 /* free, kind of */
Michal Vaskod3678892020-05-21 10:06:58 +02003832 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003833 set->type = LYXP_SET_STRING;
3834 set->val.str = str;
3835
3836 return LY_SUCCESS;
3837}
3838
3839/**
3840 * @brief Execute the XPath contains(string, string) function.
3841 * Returns LYXP_SET_BOOLEAN whether the second argument can
3842 * be found in the first or not.
3843 *
3844 * @param[in] args Array of arguments.
3845 * @param[in] arg_count Count of elements in @p args.
3846 * @param[in,out] set Context and result set at the same time.
3847 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003848 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003849 */
3850static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003851xpath_contains(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003852{
3853 struct lysc_node_leaf *sleaf;
3854 LY_ERR rc = LY_SUCCESS;
3855
3856 if (options & LYXP_SCNODE_ALL) {
3857 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3858 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003859 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3860 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003861 } else if (!warn_is_string_type(sleaf->type)) {
3862 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003863 }
3864 }
3865
3866 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3867 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003868 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3869 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003870 } else if (!warn_is_string_type(sleaf->type)) {
3871 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003872 }
3873 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003874 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003875 return rc;
3876 }
3877
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003878 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003879 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003880 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003881 LY_CHECK_RET(rc);
3882
3883 if (strstr(args[0]->val.str, args[1]->val.str)) {
3884 set_fill_boolean(set, 1);
3885 } else {
3886 set_fill_boolean(set, 0);
3887 }
3888
3889 return LY_SUCCESS;
3890}
3891
3892/**
3893 * @brief Execute the XPath count(node-set) function. Returns LYXP_SET_NUMBER
3894 * with the size of the node-set from the argument.
3895 *
3896 * @param[in] args Array of arguments.
3897 * @param[in] arg_count Count of elements in @p args.
3898 * @param[in,out] set Context and result set at the same time.
3899 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003900 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003901 */
3902static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003903xpath_count(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003904{
Michal Vasko03ff5a72019-09-11 13:49:33 +02003905 LY_ERR rc = LY_SUCCESS;
3906
3907 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003908 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003909 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003910 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003911 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003912 return rc;
3913 }
3914
Michal Vasko03ff5a72019-09-11 13:49:33 +02003915 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003916 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "count(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003917 return LY_EVALID;
3918 }
3919
3920 set_fill_number(set, args[0]->used);
3921 return LY_SUCCESS;
3922}
3923
3924/**
3925 * @brief Execute the XPath current() function. Returns LYXP_SET_NODE_SET
3926 * with the context with the intial node.
3927 *
3928 * @param[in] args Array of arguments.
3929 * @param[in] arg_count Count of elements in @p args.
3930 * @param[in,out] set Context and result set at the same time.
3931 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003932 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003933 */
3934static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003935xpath_current(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003936{
3937 if (arg_count || args) {
Radek Krejcie87c7dc2021-06-02 21:25:42 +02003938 LOGVAL(set->ctx, LY_VCODE_XP_INARGCOUNT, arg_count, LY_PRI_LENSTR("current()"));
Michal Vasko03ff5a72019-09-11 13:49:33 +02003939 return LY_EVALID;
3940 }
3941
3942 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02003943 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003944
Michal Vasko296dfaf2021-12-13 16:57:42 +01003945 if (set->cur_scnode) {
Michal Vasko7333cb32022-07-29 16:30:29 +02003946 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 +01003947 } else {
3948 /* root node */
Michal Vasko7333cb32022-07-29 16:30:29 +02003949 LY_CHECK_RET(set_scnode_insert_node(set, NULL, set->root_type, LYXP_AXIS_SELF, NULL));
Michal Vasko296dfaf2021-12-13 16:57:42 +01003950 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003951 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02003952 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003953
Michal Vasko296dfaf2021-12-13 16:57:42 +01003954 if (set->cur_node) {
3955 /* position is filled later */
3956 set_insert_node(set, set->cur_node, 0, LYXP_NODE_ELEM, 0);
3957 } else {
3958 /* root node */
3959 set_insert_node(set, NULL, 0, set->root_type, 0);
3960 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003961 }
3962
3963 return LY_SUCCESS;
3964}
3965
3966/**
3967 * @brief Execute the YANG 1.1 deref(node-set) function. Returns LYXP_SET_NODE_SET with either
3968 * leafref or instance-identifier target node(s).
3969 *
3970 * @param[in] args Array of arguments.
3971 * @param[in] arg_count Count of elements in @p args.
3972 * @param[in,out] set Context and result set at the same time.
3973 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003974 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003975 */
3976static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003977xpath_deref(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003978{
3979 struct lyd_node_term *leaf;
Michal Vasko42e497c2020-01-06 08:38:25 +01003980 struct lysc_node_leaf *sleaf = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02003981 struct lysc_type_leafref *lref;
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003982 const struct lysc_node *target;
Michal Vasko004d3152020-06-11 19:59:22 +02003983 struct ly_path *p;
3984 struct lyd_node *node;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003985 char *errmsg = NULL;
Michal Vasko00cbf532020-06-15 13:58:47 +02003986 uint8_t oper;
Michal Vasko741bb562021-06-24 11:59:50 +02003987 LY_ERR r;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003988
3989 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003990 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003991 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003992 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
Michal Vasko423ba3f2021-07-19 13:08:50 +02003993 if (!(sleaf->nodetype & LYD_NODE_TERM)) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003994 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3995 sleaf->name);
Michal Vaskoed725d72021-06-23 12:03:45 +02003996 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_LEAFREF) &&
3997 !warn_is_specific_type(sleaf->type, LY_TYPE_INST)) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003998 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"leafref\" nor \"instance-identifier\".",
3999 __func__, sleaf->name);
4000 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004001 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004002 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko423ba3f2021-07-19 13:08:50 +02004003 if (sleaf && (sleaf->nodetype & LYD_NODE_TERM) && (sleaf->type->basetype == LY_TYPE_LEAFREF)) {
Michal Vasko004d3152020-06-11 19:59:22 +02004004 lref = (struct lysc_type_leafref *)sleaf->type;
Michal Vaskod1e53b92021-01-28 13:11:06 +01004005 oper = (sleaf->flags & LYS_IS_OUTPUT) ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT;
Michal Vasko004d3152020-06-11 19:59:22 +02004006
4007 /* it was already evaluated on schema, it must succeed */
Michal Vasko741bb562021-06-24 11:59:50 +02004008 r = ly_path_compile_leafref(set->ctx, &sleaf->node, NULL, lref->path, oper, LY_PATH_TARGET_MANY,
Michal Vasko24fc4d12021-07-12 14:41:20 +02004009 LY_VALUE_SCHEMA_RESOLVED, lref->prefixes, &p);
Michal Vasko741bb562021-06-24 11:59:50 +02004010 if (!r) {
4011 /* get the target node */
4012 target = p[LY_ARRAY_COUNT(p) - 1].node;
4013 ly_path_free(set->ctx, p);
Michal Vasko004d3152020-06-11 19:59:22 +02004014
Michal Vasko7333cb32022-07-29 16:30:29 +02004015 LY_CHECK_RET(set_scnode_insert_node(set, target, LYXP_NODE_ELEM, LYXP_AXIS_SELF, NULL));
Michal Vasko741bb562021-06-24 11:59:50 +02004016 } /* else the target was found before but is disabled so it was removed */
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02004017 }
4018
Michal Vasko741bb562021-06-24 11:59:50 +02004019 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004020 }
4021
Michal Vaskod3678892020-05-21 10:06:58 +02004022 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004023 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "deref(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004024 return LY_EVALID;
4025 }
4026
Michal Vaskod3678892020-05-21 10:06:58 +02004027 lyxp_set_free_content(set);
4028 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004029 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
4030 sleaf = (struct lysc_node_leaf *)leaf->schema;
4031 if (sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
4032 if (sleaf->type->basetype == LY_TYPE_LEAFREF) {
4033 /* find leafref target */
Radek Krejci0b013302021-03-29 15:22:32 +02004034 if (lyplg_type_resolve_leafref((struct lysc_type_leafref *)sleaf->type, &leaf->node, &leaf->value, set->tree,
Michal Vasko9e685082021-01-29 14:49:09 +01004035 &node, &errmsg)) {
Michal Vasko004d3152020-06-11 19:59:22 +02004036 LOGERR(set->ctx, LY_EVALID, errmsg);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004037 free(errmsg);
Michal Vasko004d3152020-06-11 19:59:22 +02004038 return LY_EVALID;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004039 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004040 } else {
4041 assert(sleaf->type->basetype == LY_TYPE_INST);
Michal Vasko004d3152020-06-11 19:59:22 +02004042 if (ly_path_eval(leaf->value.target, set->tree, &node)) {
Michal Vaskoba99a3e2020-08-18 15:50:05 +02004043 LOGERR(set->ctx, LY_EVALID, "Invalid instance-identifier \"%s\" value - required instance not found.",
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02004044 lyd_get_value(&leaf->node));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004045 return LY_EVALID;
4046 }
4047 }
Michal Vasko004d3152020-06-11 19:59:22 +02004048
4049 /* insert it */
4050 set_insert_node(set, node, 0, LYXP_NODE_ELEM, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004051 }
4052 }
4053
4054 return LY_SUCCESS;
4055}
4056
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004057static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02004058xpath_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 +02004059{
Michal Vaskofe1af042022-07-29 14:58:59 +02004060 uint32_t i, id_len;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004061 LY_ARRAY_COUNT_TYPE u;
4062 struct lyd_node_term *leaf;
4063 struct lysc_node_leaf *sleaf;
4064 struct lyd_meta *meta;
Michal Vasko93923692021-05-07 15:28:02 +02004065 struct lyd_value *val;
4066 const struct lys_module *mod;
4067 const char *id_name;
Michal Vasko93923692021-05-07 15:28:02 +02004068 struct lysc_ident *id;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004069 LY_ERR rc = LY_SUCCESS;
Radek Krejci857189e2020-09-01 13:26:36 +02004070 ly_bool found;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004071
4072 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02004073 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004074 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", func);
Michal Vasko5676f4e2021-04-06 17:14:45 +02004075 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4076 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4077 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype),
4078 sleaf->name);
4079 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_IDENT)) {
4080 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"identityref\".", func, sleaf->name);
4081 }
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004082 }
4083
4084 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4085 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4086 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype),
Michal Vasko69730152020-10-09 16:30:07 +02004087 sleaf->name);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004088 } else if (!warn_is_string_type(sleaf->type)) {
4089 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", func, sleaf->name);
4090 }
4091 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004092 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004093 return rc;
4094 }
4095
4096 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004097 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 +02004098 return LY_EVALID;
4099 }
4100 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
4101 LY_CHECK_RET(rc);
4102
Michal Vasko93923692021-05-07 15:28:02 +02004103 /* parse the identity */
4104 id_name = args[1]->val.str;
4105 id_len = strlen(id_name);
4106 rc = moveto_resolve_model(&id_name, &id_len, set, set->cur_node ? set->cur_node->schema : NULL, &mod);
4107 LY_CHECK_RET(rc);
4108 if (!mod) {
4109 LOGVAL(set->ctx, LYVE_XPATH, "Identity \"%.*s\" without a prefix.", (int)id_len, id_name);
4110 return LY_EVALID;
4111 }
4112
4113 /* find the identity */
4114 found = 0;
4115 LY_ARRAY_FOR(mod->identities, u) {
4116 if (!ly_strncmp(mod->identities[u].name, id_name, id_len)) {
4117 /* we have match */
4118 found = 1;
4119 break;
4120 }
4121 }
4122 if (!found) {
4123 LOGVAL(set->ctx, LYVE_XPATH, "Identity \"%.*s\" not found in module \"%s\".", (int)id_len, id_name, mod->name);
4124 return LY_EVALID;
4125 }
4126 id = &mod->identities[u];
4127
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004128 set_fill_boolean(set, 0);
4129 found = 0;
4130 for (i = 0; i < args[0]->used; ++i) {
4131 if ((args[0]->val.nodes[i].type != LYXP_NODE_ELEM) && (args[0]->val.nodes[i].type != LYXP_NODE_META)) {
4132 continue;
4133 }
4134
4135 if (args[0]->val.nodes[i].type == LYXP_NODE_ELEM) {
4136 leaf = (struct lyd_node_term *)args[0]->val.nodes[i].node;
4137 sleaf = (struct lysc_node_leaf *)leaf->schema;
4138 val = &leaf->value;
4139 if (!(sleaf->nodetype & LYD_NODE_TERM) || (leaf->value.realtype->basetype != LY_TYPE_IDENT)) {
4140 /* uninteresting */
4141 continue;
4142 }
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004143 } else {
4144 meta = args[0]->val.meta[i].meta;
4145 val = &meta->value;
4146 if (val->realtype->basetype != LY_TYPE_IDENT) {
4147 /* uninteresting */
4148 continue;
4149 }
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004150 }
4151
Michal Vasko93923692021-05-07 15:28:02 +02004152 /* check the identity itself */
4153 if (self_match && (id == val->ident)) {
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004154 set_fill_boolean(set, 1);
4155 found = 1;
4156 }
Michal Vasko93923692021-05-07 15:28:02 +02004157 if (!found && !lyplg_type_identity_isderived(id, val->ident)) {
4158 set_fill_boolean(set, 1);
4159 found = 1;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004160 }
4161
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004162 if (found) {
4163 break;
4164 }
4165 }
4166
4167 return LY_SUCCESS;
4168}
4169
Michal Vasko03ff5a72019-09-11 13:49:33 +02004170/**
4171 * @brief Execute the YANG 1.1 derived-from(node-set, string) function. Returns LYXP_SET_BOOLEAN depending
4172 * on whether the first argument nodes contain a node of an identity derived from the second
4173 * argument identity.
4174 *
4175 * @param[in] args Array of arguments.
4176 * @param[in] arg_count Count of elements in @p args.
4177 * @param[in,out] set Context and result set at the same time.
4178 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004179 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004180 */
4181static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004182xpath_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 +02004183{
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004184 return xpath_derived_(args, set, options, 0, __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004185}
4186
4187/**
4188 * @brief Execute the YANG 1.1 derived-from-or-self(node-set, string) function. Returns LYXP_SET_BOOLEAN depending
4189 * on whether the first argument nodes contain a node of an identity that either is or is derived from
4190 * the second argument identity.
4191 *
4192 * @param[in] args Array of arguments.
4193 * @param[in] arg_count Count of elements in @p args.
4194 * @param[in,out] set Context and result set at the same time.
4195 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004196 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004197 */
4198static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004199xpath_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 +02004200{
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004201 return xpath_derived_(args, set, options, 1, __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004202}
4203
4204/**
4205 * @brief Execute the YANG 1.1 enum-value(node-set) function. Returns LYXP_SET_NUMBER
4206 * with the integer value of the first node's enum value, otherwise NaN.
4207 *
4208 * @param[in] args Array of arguments.
4209 * @param[in] arg_count Count of elements in @p args.
4210 * @param[in,out] set Context and result set at the same time.
4211 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004212 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004213 */
4214static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004215xpath_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 +02004216{
4217 struct lyd_node_term *leaf;
4218 struct lysc_node_leaf *sleaf;
4219 LY_ERR rc = LY_SUCCESS;
4220
4221 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02004222 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004223 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02004224 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4225 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4226 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
4227 sleaf->name);
4228 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_ENUM)) {
4229 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"enumeration\".", __func__, sleaf->name);
4230 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004231 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004232 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004233 return rc;
4234 }
4235
Michal Vaskod3678892020-05-21 10:06:58 +02004236 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004237 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "enum-value(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004238 return LY_EVALID;
4239 }
4240
4241 set_fill_number(set, NAN);
Michal Vaskod3678892020-05-21 10:06:58 +02004242 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004243 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
4244 sleaf = (struct lysc_node_leaf *)leaf->schema;
4245 if ((sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (sleaf->type->basetype == LY_TYPE_ENUM)) {
4246 set_fill_number(set, leaf->value.enum_item->value);
4247 }
4248 }
4249
4250 return LY_SUCCESS;
4251}
4252
4253/**
4254 * @brief Execute the XPath false() function. Returns LYXP_SET_BOOLEAN
4255 * with false value.
4256 *
4257 * @param[in] args Array of arguments.
4258 * @param[in] arg_count Count of elements in @p args.
4259 * @param[in,out] set Context and result set at the same time.
4260 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004261 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004262 */
4263static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004264xpath_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 +02004265{
4266 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004267 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004268 return LY_SUCCESS;
4269 }
4270
4271 set_fill_boolean(set, 0);
4272 return LY_SUCCESS;
4273}
4274
4275/**
4276 * @brief Execute the XPath floor(number) function. Returns LYXP_SET_NUMBER
4277 * with the first argument floored (truncated).
4278 *
4279 * @param[in] args Array of arguments.
4280 * @param[in] arg_count Count of elements in @p args.
4281 * @param[in,out] set Context and result set at the same time.
4282 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004283 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004284 */
4285static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004286xpath_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 +02004287{
4288 LY_ERR rc;
4289
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004290 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004291 LY_CHECK_RET(rc);
4292 if (isfinite(args[0]->val.num)) {
4293 set_fill_number(set, (long long)args[0]->val.num);
4294 }
4295
4296 return LY_SUCCESS;
4297}
4298
4299/**
4300 * @brief Execute the XPath lang(string) function. Returns LYXP_SET_BOOLEAN
4301 * whether the language of the text matches the one from the argument.
4302 *
4303 * @param[in] args Array of arguments.
4304 * @param[in] arg_count Count of elements in @p args.
4305 * @param[in,out] set Context and result set at the same time.
4306 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004307 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004308 */
4309static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004310xpath_lang(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004311{
4312 const struct lyd_node *node;
4313 struct lysc_node_leaf *sleaf;
Michal Vasko9f96a052020-03-10 09:41:45 +01004314 struct lyd_meta *meta = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004315 const char *val;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004316 LY_ERR rc = LY_SUCCESS;
4317
4318 if (options & LYXP_SCNODE_ALL) {
4319 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4320 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004321 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
4322 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004323 } else if (!warn_is_string_type(sleaf->type)) {
4324 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004325 }
4326 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004327 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004328 return rc;
4329 }
4330
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004331 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004332 LY_CHECK_RET(rc);
4333
Michal Vasko03ff5a72019-09-11 13:49:33 +02004334 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004335 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "lang(string)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004336 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004337 } else if (!set->used) {
4338 set_fill_boolean(set, 0);
4339 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004340 }
4341
4342 switch (set->val.nodes[0].type) {
4343 case LYXP_NODE_ELEM:
4344 case LYXP_NODE_TEXT:
4345 node = set->val.nodes[0].node;
4346 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004347 case LYXP_NODE_META:
4348 node = set->val.meta[0].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004349 break;
4350 default:
4351 /* nothing to do with roots */
4352 set_fill_boolean(set, 0);
4353 return LY_SUCCESS;
4354 }
4355
Michal Vasko9f96a052020-03-10 09:41:45 +01004356 /* find lang metadata */
Michal Vasko9e685082021-01-29 14:49:09 +01004357 for ( ; node; node = lyd_parent(node)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01004358 for (meta = node->meta; meta; meta = meta->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004359 /* annotations */
Michal Vasko9f96a052020-03-10 09:41:45 +01004360 if (meta->name && !strcmp(meta->name, "lang") && !strcmp(meta->annotation->module->name, "xml")) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004361 break;
4362 }
4363 }
4364
Michal Vasko9f96a052020-03-10 09:41:45 +01004365 if (meta) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004366 break;
4367 }
4368 }
4369
4370 /* compare languages */
Michal Vasko9f96a052020-03-10 09:41:45 +01004371 if (!meta) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004372 set_fill_boolean(set, 0);
4373 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004374 uint64_t i;
4375
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02004376 val = lyd_get_meta_value(meta);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004377 for (i = 0; args[0]->val.str[i]; ++i) {
4378 if (tolower(args[0]->val.str[i]) != tolower(val[i])) {
4379 set_fill_boolean(set, 0);
4380 break;
4381 }
4382 }
4383 if (!args[0]->val.str[i]) {
4384 if (!val[i] || (val[i] == '-')) {
4385 set_fill_boolean(set, 1);
4386 } else {
4387 set_fill_boolean(set, 0);
4388 }
4389 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004390 }
4391
4392 return LY_SUCCESS;
4393}
4394
4395/**
4396 * @brief Execute the XPath last() function. Returns LYXP_SET_NUMBER
4397 * with the context size.
4398 *
4399 * @param[in] args Array of arguments.
4400 * @param[in] arg_count Count of elements in @p args.
4401 * @param[in,out] set Context and result set at the same time.
4402 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004403 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004404 */
4405static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004406xpath_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 +02004407{
4408 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004409 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004410 return LY_SUCCESS;
4411 }
4412
Michal Vasko03ff5a72019-09-11 13:49:33 +02004413 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004414 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "last()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004415 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004416 } else if (!set->used) {
4417 set_fill_number(set, 0);
4418 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004419 }
4420
4421 set_fill_number(set, set->ctx_size);
4422 return LY_SUCCESS;
4423}
4424
4425/**
4426 * @brief Execute the XPath local-name(node-set?) function. Returns LYXP_SET_STRING
4427 * with the node name without namespace from the argument or the context.
4428 *
4429 * @param[in] args Array of arguments.
4430 * @param[in] arg_count Count of elements in @p args.
4431 * @param[in,out] set Context and result set at the same time.
4432 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004433 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004434 */
4435static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004436xpath_local_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004437{
4438 struct lyxp_set_node *item;
Michal Vasko69730152020-10-09 16:30:07 +02004439
Michal Vasko03ff5a72019-09-11 13:49:33 +02004440 /* suppress unused variable warning */
4441 (void)options;
4442
4443 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004444 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004445 return LY_SUCCESS;
4446 }
4447
4448 if (arg_count) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004449 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004450 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004451 "local-name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004452 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004453 } else if (!args[0]->used) {
4454 set_fill_string(set, "", 0);
4455 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004456 }
4457
4458 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004459 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004460
4461 item = &args[0]->val.nodes[0];
4462 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004463 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004464 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "local-name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004465 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004466 } else if (!set->used) {
4467 set_fill_string(set, "", 0);
4468 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004469 }
4470
4471 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004472 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004473
4474 item = &set->val.nodes[0];
4475 }
4476
4477 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004478 case LYXP_NODE_NONE:
4479 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004480 case LYXP_NODE_ROOT:
4481 case LYXP_NODE_ROOT_CONFIG:
4482 case LYXP_NODE_TEXT:
4483 set_fill_string(set, "", 0);
4484 break;
4485 case LYXP_NODE_ELEM:
4486 set_fill_string(set, item->node->schema->name, strlen(item->node->schema->name));
4487 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004488 case LYXP_NODE_META:
4489 set_fill_string(set, ((struct lyd_meta *)item->node)->name, strlen(((struct lyd_meta *)item->node)->name));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004490 break;
4491 }
4492
4493 return LY_SUCCESS;
4494}
4495
4496/**
4497 * @brief Execute the XPath name(node-set?) function. Returns LYXP_SET_STRING
4498 * with the node name fully qualified (with namespace) from the argument or the context.
4499 *
4500 * @param[in] args Array of arguments.
4501 * @param[in] arg_count Count of elements in @p args.
4502 * @param[in,out] set Context and result set at the same time.
4503 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004504 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004505 */
4506static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004507xpath_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004508{
4509 struct lyxp_set_node *item;
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004510 struct lys_module *mod = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004511 char *str;
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004512 const char *name = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004513
4514 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004515 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004516 return LY_SUCCESS;
4517 }
4518
4519 if (arg_count) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004520 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004521 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004522 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004523 } else if (!args[0]->used) {
4524 set_fill_string(set, "", 0);
4525 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004526 }
4527
4528 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004529 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004530
4531 item = &args[0]->val.nodes[0];
4532 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004533 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004534 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004535 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004536 } else if (!set->used) {
4537 set_fill_string(set, "", 0);
4538 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004539 }
4540
4541 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004542 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004543
4544 item = &set->val.nodes[0];
4545 }
4546
4547 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004548 case LYXP_NODE_NONE:
4549 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004550 case LYXP_NODE_ROOT:
4551 case LYXP_NODE_ROOT_CONFIG:
4552 case LYXP_NODE_TEXT:
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004553 /* keep NULL */
Michal Vasko03ff5a72019-09-11 13:49:33 +02004554 break;
4555 case LYXP_NODE_ELEM:
4556 mod = item->node->schema->module;
4557 name = item->node->schema->name;
4558 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004559 case LYXP_NODE_META:
4560 mod = ((struct lyd_meta *)item->node)->annotation->module;
4561 name = ((struct lyd_meta *)item->node)->name;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004562 break;
4563 }
4564
4565 if (mod && name) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004566 int rc = asprintf(&str, "%s:%s", ly_get_prefix(mod, set->format, set->prefix_data), name);
Michal Vasko26bbb272022-08-02 14:54:33 +02004567
Michal Vasko03ff5a72019-09-11 13:49:33 +02004568 LY_CHECK_ERR_RET(rc == -1, LOGMEM(set->ctx), LY_EMEM);
4569 set_fill_string(set, str, strlen(str));
4570 free(str);
4571 } else {
4572 set_fill_string(set, "", 0);
4573 }
4574
4575 return LY_SUCCESS;
4576}
4577
4578/**
4579 * @brief Execute the XPath namespace-uri(node-set?) function. Returns LYXP_SET_STRING
4580 * with the namespace of the node from the argument or the context.
4581 *
4582 * @param[in] args Array of arguments.
4583 * @param[in] arg_count Count of elements in @p args.
4584 * @param[in,out] set Context and result set at the same time.
4585 * @param[in] options XPath options.
4586 * @return LY_ERR (LY_EINVAL for wrong arguments on schema)
4587 */
4588static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004589xpath_namespace_uri(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004590{
4591 struct lyxp_set_node *item;
4592 struct lys_module *mod;
Michal Vasko69730152020-10-09 16:30:07 +02004593
Michal Vasko03ff5a72019-09-11 13:49:33 +02004594 /* suppress unused variable warning */
4595 (void)options;
4596
4597 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004598 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004599 return LY_SUCCESS;
4600 }
4601
4602 if (arg_count) {
Michal Vaskod3678892020-05-21 10:06:58 +02004603 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004604 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
Michal Vasko69730152020-10-09 16:30:07 +02004605 "namespace-uri(node-set?)");
Michal Vaskod3678892020-05-21 10:06:58 +02004606 return LY_EVALID;
4607 } else if (!args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004608 set_fill_string(set, "", 0);
4609 return LY_SUCCESS;
4610 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004611
4612 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004613 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004614
4615 item = &args[0]->val.nodes[0];
4616 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004617 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004618 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "namespace-uri(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004619 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004620 } else if (!set->used) {
4621 set_fill_string(set, "", 0);
4622 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004623 }
4624
4625 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004626 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004627
4628 item = &set->val.nodes[0];
4629 }
4630
4631 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004632 case LYXP_NODE_NONE:
4633 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004634 case LYXP_NODE_ROOT:
4635 case LYXP_NODE_ROOT_CONFIG:
4636 case LYXP_NODE_TEXT:
4637 set_fill_string(set, "", 0);
4638 break;
4639 case LYXP_NODE_ELEM:
Michal Vasko9f96a052020-03-10 09:41:45 +01004640 case LYXP_NODE_META:
Michal Vasko03ff5a72019-09-11 13:49:33 +02004641 if (item->type == LYXP_NODE_ELEM) {
4642 mod = item->node->schema->module;
Michal Vasko9f96a052020-03-10 09:41:45 +01004643 } else { /* LYXP_NODE_META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02004644 /* annotations */
Michal Vasko9f96a052020-03-10 09:41:45 +01004645 mod = ((struct lyd_meta *)item->node)->annotation->module;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004646 }
4647
4648 set_fill_string(set, mod->ns, strlen(mod->ns));
4649 break;
4650 }
4651
4652 return LY_SUCCESS;
4653}
4654
4655/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02004656 * @brief Execute the XPath normalize-space(string?) function. Returns LYXP_SET_STRING
4657 * with normalized value (no leading, trailing, double white spaces) of the node
4658 * from the argument or the context.
4659 *
4660 * @param[in] args Array of arguments.
4661 * @param[in] arg_count Count of elements in @p args.
4662 * @param[in,out] set Context and result set at the same time.
4663 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004664 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004665 */
4666static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004667xpath_normalize_space(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004668{
4669 uint16_t i, new_used;
4670 char *new;
Radek Krejci857189e2020-09-01 13:26:36 +02004671 ly_bool have_spaces = 0, space_before = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004672 struct lysc_node_leaf *sleaf;
4673 LY_ERR rc = LY_SUCCESS;
4674
4675 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004676 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) &&
4677 (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004678 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004679 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
4680 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004681 } else if (!warn_is_string_type(sleaf->type)) {
4682 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004683 }
4684 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004685 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004686 return rc;
4687 }
4688
4689 if (arg_count) {
4690 set_fill_set(set, args[0]);
4691 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004692 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004693 LY_CHECK_RET(rc);
4694
4695 /* is there any normalization necessary? */
4696 for (i = 0; set->val.str[i]; ++i) {
4697 if (is_xmlws(set->val.str[i])) {
4698 if ((i == 0) || space_before || (!set->val.str[i + 1])) {
4699 have_spaces = 1;
4700 break;
4701 }
4702 space_before = 1;
4703 } else {
4704 space_before = 0;
4705 }
4706 }
4707
4708 /* yep, there is */
4709 if (have_spaces) {
4710 /* it's enough, at least one character will go, makes space for ending '\0' */
4711 new = malloc(strlen(set->val.str) * sizeof(char));
4712 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4713 new_used = 0;
4714
4715 space_before = 0;
4716 for (i = 0; set->val.str[i]; ++i) {
4717 if (is_xmlws(set->val.str[i])) {
4718 if ((i == 0) || space_before) {
4719 space_before = 1;
4720 continue;
4721 } else {
4722 space_before = 1;
4723 }
4724 } else {
4725 space_before = 0;
4726 }
4727
4728 new[new_used] = (space_before ? ' ' : set->val.str[i]);
4729 ++new_used;
4730 }
4731
4732 /* at worst there is one trailing space now */
4733 if (new_used && is_xmlws(new[new_used - 1])) {
4734 --new_used;
4735 }
4736
4737 new = ly_realloc(new, (new_used + 1) * sizeof(char));
4738 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4739 new[new_used] = '\0';
4740
4741 free(set->val.str);
4742 set->val.str = new;
4743 }
4744
4745 return LY_SUCCESS;
4746}
4747
4748/**
4749 * @brief Execute the XPath not(boolean) function. Returns LYXP_SET_BOOLEAN
4750 * with the argument converted to boolean and logically inverted.
4751 *
4752 * @param[in] args Array of arguments.
4753 * @param[in] arg_count Count of elements in @p args.
4754 * @param[in,out] set Context and result set at the same time.
4755 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004756 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004757 */
4758static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004759xpath_not(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004760{
4761 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004762 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004763 return LY_SUCCESS;
4764 }
4765
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004766 lyxp_set_cast(args[0], LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +02004767 if (args[0]->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004768 set_fill_boolean(set, 0);
4769 } else {
4770 set_fill_boolean(set, 1);
4771 }
4772
4773 return LY_SUCCESS;
4774}
4775
4776/**
4777 * @brief Execute the XPath number(object?) function. Returns LYXP_SET_NUMBER
4778 * with the number representation of either the argument or the context.
4779 *
4780 * @param[in] args Array of arguments.
4781 * @param[in] arg_count Count of elements in @p args.
4782 * @param[in,out] set Context and result set at the same time.
4783 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004784 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004785 */
4786static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004787xpath_number(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004788{
4789 LY_ERR rc;
4790
4791 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004792 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004793 return LY_SUCCESS;
4794 }
4795
4796 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004797 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004798 LY_CHECK_RET(rc);
4799 set_fill_set(set, args[0]);
4800 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004801 rc = lyxp_set_cast(set, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004802 LY_CHECK_RET(rc);
4803 }
4804
4805 return LY_SUCCESS;
4806}
4807
4808/**
4809 * @brief Execute the XPath position() function. Returns LYXP_SET_NUMBER
4810 * with the context position.
4811 *
4812 * @param[in] args Array of arguments.
4813 * @param[in] arg_count Count of elements in @p args.
4814 * @param[in,out] set Context and result set at the same time.
4815 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004816 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004817 */
4818static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004819xpath_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 +02004820{
4821 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004822 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004823 return LY_SUCCESS;
4824 }
4825
Michal Vasko03ff5a72019-09-11 13:49:33 +02004826 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004827 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "position()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004828 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004829 } else if (!set->used) {
4830 set_fill_number(set, 0);
4831 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004832 }
4833
4834 set_fill_number(set, set->ctx_pos);
4835
4836 /* UNUSED in 'Release' build type */
4837 (void)options;
4838 return LY_SUCCESS;
4839}
4840
4841/**
4842 * @brief Execute the YANG 1.1 re-match(string, string) function. Returns LYXP_SET_BOOLEAN
4843 * depending on whether the second argument regex matches the first argument string. For details refer to
4844 * YANG 1.1 RFC section 10.2.1.
4845 *
4846 * @param[in] args Array of arguments.
4847 * @param[in] arg_count Count of elements in @p args.
4848 * @param[in,out] set Context and result set at the same time.
4849 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004850 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004851 */
4852static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004853xpath_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 +02004854{
4855 struct lysc_pattern **patterns = NULL, **pattern;
4856 struct lysc_node_leaf *sleaf;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004857 LY_ERR rc = LY_SUCCESS;
4858 struct ly_err_item *err;
4859
4860 if (options & LYXP_SCNODE_ALL) {
4861 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4862 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4863 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 +02004864 } else if (!warn_is_string_type(sleaf->type)) {
4865 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004866 }
4867 }
4868
4869 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4870 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4871 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 +02004872 } else if (!warn_is_string_type(sleaf->type)) {
4873 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004874 }
4875 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004876 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004877 return rc;
4878 }
4879
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004880 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004881 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004882 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004883 LY_CHECK_RET(rc);
4884
4885 LY_ARRAY_NEW_RET(set->ctx, patterns, pattern, LY_EMEM);
Radek Iša45802b52021-02-09 09:21:58 +01004886 *pattern = calloc(1, sizeof **pattern);
Radek Krejciddace2c2021-01-08 11:30:56 +01004887 LOG_LOCSET(NULL, set->cur_node, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01004888 rc = lys_compile_type_pattern_check(set->ctx, args[1]->val.str, &(*pattern)->code);
Michal Vasko4a7d4d62021-12-13 17:05:06 +01004889 if (set->cur_node) {
4890 LOG_LOCBACK(0, 1, 0, 0);
4891 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004892 if (rc != LY_SUCCESS) {
4893 LY_ARRAY_FREE(patterns);
4894 return rc;
4895 }
4896
Radek Krejci0b013302021-03-29 15:22:32 +02004897 rc = lyplg_type_validate_patterns(patterns, args[0]->val.str, strlen(args[0]->val.str), &err);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004898 pcre2_code_free((*pattern)->code);
4899 free(*pattern);
4900 LY_ARRAY_FREE(patterns);
4901 if (rc && (rc != LY_EVALID)) {
Michal Vasko177d0ed2020-11-23 16:43:03 +01004902 ly_err_print(set->ctx, err);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004903 ly_err_free(err);
4904 return rc;
4905 }
4906
4907 if (rc == LY_EVALID) {
4908 ly_err_free(err);
4909 set_fill_boolean(set, 0);
4910 } else {
4911 set_fill_boolean(set, 1);
4912 }
4913
4914 return LY_SUCCESS;
4915}
4916
4917/**
4918 * @brief Execute the XPath round(number) function. Returns LYXP_SET_NUMBER
4919 * with the rounded first argument. For details refer to
4920 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-round.
4921 *
4922 * @param[in] args Array of arguments.
4923 * @param[in] arg_count Count of elements in @p args.
4924 * @param[in,out] set Context and result set at the same time.
4925 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004926 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004927 */
4928static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004929xpath_round(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004930{
4931 struct lysc_node_leaf *sleaf;
4932 LY_ERR rc = LY_SUCCESS;
4933
4934 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02004935 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004936 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02004937 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4938 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4939 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
4940 sleaf->name);
4941 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
4942 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
4943 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004944 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004945 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004946 return rc;
4947 }
4948
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004949 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004950 LY_CHECK_RET(rc);
4951
4952 /* cover only the cases where floor can't be used */
4953 if ((args[0]->val.num == -0.0f) || ((args[0]->val.num < 0) && (args[0]->val.num >= -0.5))) {
4954 set_fill_number(set, -0.0f);
4955 } else {
4956 args[0]->val.num += 0.5;
4957 rc = xpath_floor(args, 1, args[0], options);
4958 LY_CHECK_RET(rc);
4959 set_fill_number(set, args[0]->val.num);
4960 }
4961
4962 return LY_SUCCESS;
4963}
4964
4965/**
4966 * @brief Execute the XPath starts-with(string, string) function.
4967 * Returns LYXP_SET_BOOLEAN whether the second argument is
4968 * the prefix of the first or not.
4969 *
4970 * @param[in] args Array of arguments.
4971 * @param[in] arg_count Count of elements in @p args.
4972 * @param[in,out] set Context and result set at the same time.
4973 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004974 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004975 */
4976static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004977xpath_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 +02004978{
4979 struct lysc_node_leaf *sleaf;
4980 LY_ERR rc = LY_SUCCESS;
4981
4982 if (options & LYXP_SCNODE_ALL) {
4983 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4984 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4985 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 +02004986 } else if (!warn_is_string_type(sleaf->type)) {
4987 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004988 }
4989 }
4990
4991 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4992 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4993 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 +02004994 } else if (!warn_is_string_type(sleaf->type)) {
4995 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004996 }
4997 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004998 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004999 return rc;
5000 }
5001
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005002 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005003 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005004 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005005 LY_CHECK_RET(rc);
5006
5007 if (strncmp(args[0]->val.str, args[1]->val.str, strlen(args[1]->val.str))) {
5008 set_fill_boolean(set, 0);
5009 } else {
5010 set_fill_boolean(set, 1);
5011 }
5012
5013 return LY_SUCCESS;
5014}
5015
5016/**
5017 * @brief Execute the XPath string(object?) function. Returns LYXP_SET_STRING
5018 * with the string representation of either the argument or the context.
5019 *
5020 * @param[in] args Array of arguments.
5021 * @param[in] arg_count Count of elements in @p args.
5022 * @param[in,out] set Context and result set at the same time.
5023 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005024 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005025 */
5026static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005027xpath_string(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005028{
5029 LY_ERR rc;
5030
5031 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02005032 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005033 return LY_SUCCESS;
5034 }
5035
5036 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005037 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005038 LY_CHECK_RET(rc);
5039 set_fill_set(set, args[0]);
5040 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005041 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005042 LY_CHECK_RET(rc);
5043 }
5044
5045 return LY_SUCCESS;
5046}
5047
5048/**
5049 * @brief Execute the XPath string-length(string?) function. Returns LYXP_SET_NUMBER
5050 * with the length of the string in either the argument or the context.
5051 *
5052 * @param[in] args Array of arguments.
5053 * @param[in] arg_count Count of elements in @p args.
5054 * @param[in,out] set Context and result set at the same time.
5055 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005056 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005057 */
5058static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005059xpath_string_length(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005060{
5061 struct lysc_node_leaf *sleaf;
5062 LY_ERR rc = LY_SUCCESS;
5063
5064 if (options & LYXP_SCNODE_ALL) {
5065 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5066 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5067 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 +02005068 } else if (!warn_is_string_type(sleaf->type)) {
5069 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005070 }
5071 }
5072 if (!arg_count && (set->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set))) {
5073 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5074 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 +02005075 } else if (!warn_is_string_type(sleaf->type)) {
5076 LOGWRN(set->ctx, "Argument #0 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005077 }
5078 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005079 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005080 return rc;
5081 }
5082
5083 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005084 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005085 LY_CHECK_RET(rc);
5086 set_fill_number(set, strlen(args[0]->val.str));
5087 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005088 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005089 LY_CHECK_RET(rc);
5090 set_fill_number(set, strlen(set->val.str));
5091 }
5092
5093 return LY_SUCCESS;
5094}
5095
5096/**
5097 * @brief Execute the XPath substring(string, number, number?) function.
5098 * Returns LYXP_SET_STRING substring of the first argument starting
5099 * on the second argument index ending on the third argument index,
5100 * indexed from 1. For exact definition refer to
5101 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-substring.
5102 *
5103 * @param[in] args Array of arguments.
5104 * @param[in] arg_count Count of elements in @p args.
5105 * @param[in,out] set Context and result set at the same time.
5106 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005107 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005108 */
5109static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005110xpath_substring(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005111{
Michal Vasko6db996e2022-07-28 10:28:04 +02005112 int64_t start;
5113 int32_t len;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005114 uint16_t str_start, str_len, pos;
5115 struct lysc_node_leaf *sleaf;
5116 LY_ERR rc = LY_SUCCESS;
5117
5118 if (options & LYXP_SCNODE_ALL) {
5119 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5120 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5121 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 +02005122 } else if (!warn_is_string_type(sleaf->type)) {
5123 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005124 }
5125 }
5126
5127 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5128 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5129 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 +02005130 } else if (!warn_is_numeric_type(sleaf->type)) {
5131 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005132 }
5133 }
5134
Michal Vasko69730152020-10-09 16:30:07 +02005135 if ((arg_count == 3) && (args[2]->type == LYXP_SET_SCNODE_SET) &&
5136 (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005137 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5138 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 +02005139 } else if (!warn_is_numeric_type(sleaf->type)) {
5140 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005141 }
5142 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005143 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005144 return rc;
5145 }
5146
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005147 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005148 LY_CHECK_RET(rc);
5149
5150 /* start */
5151 if (xpath_round(&args[1], 1, args[1], options)) {
5152 return -1;
5153 }
5154 if (isfinite(args[1]->val.num)) {
5155 start = args[1]->val.num - 1;
5156 } else if (isinf(args[1]->val.num) && signbit(args[1]->val.num)) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02005157 start = INT32_MIN;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005158 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02005159 start = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005160 }
5161
5162 /* len */
5163 if (arg_count == 3) {
5164 rc = xpath_round(&args[2], 1, args[2], options);
5165 LY_CHECK_RET(rc);
Radek Krejci1deb5be2020-08-26 16:43:36 +02005166 if (isnan(args[2]->val.num) || signbit(args[2]->val.num)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005167 len = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005168 } else if (isfinite(args[2]->val.num)) {
5169 len = args[2]->val.num;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005170 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02005171 len = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005172 }
5173 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02005174 len = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005175 }
5176
5177 /* find matching character positions */
5178 str_start = 0;
5179 str_len = 0;
5180 for (pos = 0; args[0]->val.str[pos]; ++pos) {
5181 if (pos < start) {
5182 ++str_start;
5183 } else if (pos < start + len) {
5184 ++str_len;
5185 } else {
5186 break;
5187 }
5188 }
5189
5190 set_fill_string(set, args[0]->val.str + str_start, str_len);
5191 return LY_SUCCESS;
5192}
5193
5194/**
5195 * @brief Execute the XPath substring-after(string, string) function.
5196 * Returns LYXP_SET_STRING with the string succeeding the occurance
5197 * of the second argument in the first or an empty string.
5198 *
5199 * @param[in] args Array of arguments.
5200 * @param[in] arg_count Count of elements in @p args.
5201 * @param[in,out] set Context and result set at the same time.
5202 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005203 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005204 */
5205static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005206xpath_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 +02005207{
5208 char *ptr;
5209 struct lysc_node_leaf *sleaf;
5210 LY_ERR rc = LY_SUCCESS;
5211
5212 if (options & LYXP_SCNODE_ALL) {
5213 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5214 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5215 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 +02005216 } else if (!warn_is_string_type(sleaf->type)) {
5217 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005218 }
5219 }
5220
5221 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5222 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5223 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 +02005224 } else if (!warn_is_string_type(sleaf->type)) {
5225 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005226 }
5227 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005228 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005229 return rc;
5230 }
5231
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005232 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005233 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005234 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005235 LY_CHECK_RET(rc);
5236
5237 ptr = strstr(args[0]->val.str, args[1]->val.str);
5238 if (ptr) {
5239 set_fill_string(set, ptr + strlen(args[1]->val.str), strlen(ptr + strlen(args[1]->val.str)));
5240 } else {
5241 set_fill_string(set, "", 0);
5242 }
5243
5244 return LY_SUCCESS;
5245}
5246
5247/**
5248 * @brief Execute the XPath substring-before(string, string) function.
5249 * Returns LYXP_SET_STRING with the string preceding the occurance
5250 * of the second argument in the first or an empty string.
5251 *
5252 * @param[in] args Array of arguments.
5253 * @param[in] arg_count Count of elements in @p args.
5254 * @param[in,out] set Context and result set at the same time.
5255 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005256 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005257 */
5258static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005259xpath_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 +02005260{
5261 char *ptr;
5262 struct lysc_node_leaf *sleaf;
5263 LY_ERR rc = LY_SUCCESS;
5264
5265 if (options & LYXP_SCNODE_ALL) {
5266 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5267 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5268 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 +02005269 } else if (!warn_is_string_type(sleaf->type)) {
5270 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005271 }
5272 }
5273
5274 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5275 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5276 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 +02005277 } else if (!warn_is_string_type(sleaf->type)) {
5278 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005279 }
5280 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005281 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005282 return rc;
5283 }
5284
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005285 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005286 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005287 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005288 LY_CHECK_RET(rc);
5289
5290 ptr = strstr(args[0]->val.str, args[1]->val.str);
5291 if (ptr) {
5292 set_fill_string(set, args[0]->val.str, ptr - args[0]->val.str);
5293 } else {
5294 set_fill_string(set, "", 0);
5295 }
5296
5297 return LY_SUCCESS;
5298}
5299
5300/**
5301 * @brief Execute the XPath sum(node-set) function. Returns LYXP_SET_NUMBER
5302 * with the sum of all the nodes in the context.
5303 *
5304 * @param[in] args Array of arguments.
5305 * @param[in] arg_count Count of elements in @p args.
5306 * @param[in,out] set Context and result set at the same time.
5307 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005308 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005309 */
5310static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005311xpath_sum(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005312{
5313 long double num;
5314 char *str;
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01005315 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005316 struct lyxp_set set_item;
5317 struct lysc_node_leaf *sleaf;
5318 LY_ERR rc = LY_SUCCESS;
5319
5320 if (options & LYXP_SCNODE_ALL) {
5321 if (args[0]->type == LYXP_SET_SCNODE_SET) {
5322 for (i = 0; i < args[0]->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005323 if (args[0]->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005324 sleaf = (struct lysc_node_leaf *)args[0]->val.scnodes[i].scnode;
5325 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5326 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__,
Michal Vasko69730152020-10-09 16:30:07 +02005327 lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005328 } else if (!warn_is_numeric_type(sleaf->type)) {
5329 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005330 }
5331 }
5332 }
5333 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005334 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005335 return rc;
5336 }
5337
5338 set_fill_number(set, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005339
5340 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005341 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "sum(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02005342 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02005343 } else if (!args[0]->used) {
5344 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005345 }
5346
Michal Vasko5c4e5892019-11-14 12:31:38 +01005347 set_init(&set_item, set);
5348
Michal Vasko03ff5a72019-09-11 13:49:33 +02005349 set_item.type = LYXP_SET_NODE_SET;
Michal Vasko41decbf2021-11-02 11:50:21 +01005350 set_item.val.nodes = calloc(1, sizeof *set_item.val.nodes);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005351 LY_CHECK_ERR_RET(!set_item.val.nodes, LOGMEM(set->ctx), LY_EMEM);
5352
5353 set_item.used = 1;
5354 set_item.size = 1;
5355
5356 for (i = 0; i < args[0]->used; ++i) {
5357 set_item.val.nodes[0] = args[0]->val.nodes[i];
5358
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005359 rc = cast_node_set_to_string(&set_item, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005360 LY_CHECK_RET(rc);
5361 num = cast_string_to_number(str);
5362 free(str);
5363 set->val.num += num;
5364 }
5365
5366 free(set_item.val.nodes);
5367
5368 return LY_SUCCESS;
5369}
5370
5371/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02005372 * @brief Execute the XPath translate(string, string, string) function.
5373 * Returns LYXP_SET_STRING with the first argument with the characters
5374 * from the second argument replaced by those on the corresponding
5375 * positions in the third argument.
5376 *
5377 * @param[in] args Array of arguments.
5378 * @param[in] arg_count Count of elements in @p args.
5379 * @param[in,out] set Context and result set at the same time.
5380 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005381 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005382 */
5383static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005384xpath_translate(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005385{
5386 uint16_t i, j, new_used;
5387 char *new;
Radek Krejci857189e2020-09-01 13:26:36 +02005388 ly_bool have_removed;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005389 struct lysc_node_leaf *sleaf;
5390 LY_ERR rc = LY_SUCCESS;
5391
5392 if (options & LYXP_SCNODE_ALL) {
5393 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5394 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5395 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 +02005396 } else if (!warn_is_string_type(sleaf->type)) {
5397 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005398 }
5399 }
5400
5401 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5402 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5403 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 +02005404 } else if (!warn_is_string_type(sleaf->type)) {
5405 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005406 }
5407 }
5408
5409 if ((args[2]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
5410 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5411 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 +02005412 } else if (!warn_is_string_type(sleaf->type)) {
5413 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005414 }
5415 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005416 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005417 return rc;
5418 }
5419
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005420 rc = lyxp_set_cast(args[0], 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[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005423 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005424 rc = lyxp_set_cast(args[2], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005425 LY_CHECK_RET(rc);
5426
5427 new = malloc((strlen(args[0]->val.str) + 1) * sizeof(char));
5428 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5429 new_used = 0;
5430
5431 have_removed = 0;
5432 for (i = 0; args[0]->val.str[i]; ++i) {
Radek Krejci857189e2020-09-01 13:26:36 +02005433 ly_bool found = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005434
5435 for (j = 0; args[1]->val.str[j]; ++j) {
5436 if (args[0]->val.str[i] == args[1]->val.str[j]) {
5437 /* removing this char */
5438 if (j >= strlen(args[2]->val.str)) {
5439 have_removed = 1;
5440 found = 1;
5441 break;
5442 }
5443 /* replacing this char */
5444 new[new_used] = args[2]->val.str[j];
5445 ++new_used;
5446 found = 1;
5447 break;
5448 }
5449 }
5450
5451 /* copying this char */
5452 if (!found) {
5453 new[new_used] = args[0]->val.str[i];
5454 ++new_used;
5455 }
5456 }
5457
5458 if (have_removed) {
5459 new = ly_realloc(new, (new_used + 1) * sizeof(char));
5460 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5461 }
5462 new[new_used] = '\0';
5463
Michal Vaskod3678892020-05-21 10:06:58 +02005464 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005465 set->type = LYXP_SET_STRING;
5466 set->val.str = new;
5467
5468 return LY_SUCCESS;
5469}
5470
5471/**
5472 * @brief Execute the XPath true() function. Returns LYXP_SET_BOOLEAN
5473 * with true value.
5474 *
5475 * @param[in] args Array of arguments.
5476 * @param[in] arg_count Count of elements in @p args.
5477 * @param[in,out] set Context and result set at the same time.
5478 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005479 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005480 */
5481static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005482xpath_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 +02005483{
5484 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02005485 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005486 return LY_SUCCESS;
5487 }
5488
5489 set_fill_boolean(set, 1);
5490 return LY_SUCCESS;
5491}
5492
Michal Vasko03ff5a72019-09-11 13:49:33 +02005493/**
Michal Vasko49fec8e2022-05-24 10:28:33 +02005494 * @brief Execute the XPath node() processing instruction (node type). Returns LYXP_SET_NODE_SET
5495 * with only nodes from the context.
5496 *
5497 * @param[in,out] set Context and result set at the same time.
5498 * @param[in] axis Axis to search on.
5499 * @param[in] options XPath options.
5500 * @return LY_ERR
5501 */
5502static LY_ERR
5503xpath_pi_node(struct lyxp_set *set, enum lyxp_axis axis, uint32_t options)
5504{
5505 if (options & LYXP_SCNODE_ALL) {
5506 return moveto_scnode(set, NULL, NULL, axis, options);
5507 }
5508
5509 if (set->type != LYXP_SET_NODE_SET) {
5510 lyxp_set_free_content(set);
5511 return LY_SUCCESS;
5512 }
5513
5514 /* just like moving to a node with no restrictions */
5515 return moveto_node(set, NULL, NULL, axis, options);
5516}
5517
5518/**
5519 * @brief Execute the XPath text() processing instruction (node type). Returns LYXP_SET_NODE_SET
5520 * with the text content of the nodes in the context.
5521 *
5522 * @param[in,out] set Context and result set at the same time.
5523 * @param[in] axis Axis to search on.
5524 * @param[in] options XPath options.
5525 * @return LY_ERR
5526 */
5527static LY_ERR
5528xpath_pi_text(struct lyxp_set *set, enum lyxp_axis axis, uint32_t options)
5529{
5530 uint32_t i;
5531
5532 if (options & LYXP_SCNODE_ALL) {
5533 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
5534 return LY_SUCCESS;
5535 }
5536
5537 if (set->type != LYXP_SET_NODE_SET) {
5538 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "text()");
5539 return LY_EVALID;
5540 }
5541
5542 if (axis != LYXP_AXIS_CHILD) {
5543 /* even following and preceding axescan return text nodes, but whatever */
5544 lyxp_set_free_content(set);
5545 return LY_SUCCESS;
5546 }
5547
5548 for (i = 0; i < set->used; ++i) {
5549 switch (set->val.nodes[i].type) {
5550 case LYXP_NODE_NONE:
5551 LOGINT_RET(set->ctx);
5552 case LYXP_NODE_ELEM:
5553 if (set->val.nodes[i].node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
5554 set->val.nodes[i].type = LYXP_NODE_TEXT;
5555 break;
5556 }
5557 /* fall through */
5558 case LYXP_NODE_ROOT:
5559 case LYXP_NODE_ROOT_CONFIG:
5560 case LYXP_NODE_TEXT:
5561 case LYXP_NODE_META:
5562 set_remove_node_none(set, i);
5563 break;
5564 }
5565 }
5566 set_remove_nodes_none(set);
5567
5568 return LY_SUCCESS;
5569}
5570
5571/**
Michal Vasko6346ece2019-09-24 13:12:53 +02005572 * @brief Skip prefix and return corresponding model if there is a prefix. Logs directly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005573 *
Michal Vasko2104e9f2020-03-06 08:23:25 +01005574 * XPath @p set is expected to be a (sc)node set!
5575 *
Michal Vasko6346ece2019-09-24 13:12:53 +02005576 * @param[in,out] qname Qualified node name. If includes prefix, it is skipped.
5577 * @param[in,out] qname_len Length of @p qname, is updated accordingly.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005578 * @param[in] set Set with general XPath context.
5579 * @param[in] ctx_scnode Context node to inherit module for unprefixed node for ::LY_PREF_JSON.
Michal Vasko6346ece2019-09-24 13:12:53 +02005580 * @param[out] moveto_mod Expected module of a matching node.
5581 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005582 */
Michal Vasko6346ece2019-09-24 13:12:53 +02005583static LY_ERR
Michal Vaskofe1af042022-07-29 14:58:59 +02005584moveto_resolve_model(const char **qname, uint32_t *qname_len, const struct lyxp_set *set,
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005585 const struct lysc_node *ctx_scnode, const struct lys_module **moveto_mod)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005586{
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02005587 const struct lys_module *mod = NULL;
Michal Vasko6346ece2019-09-24 13:12:53 +02005588 const char *ptr;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005589 size_t pref_len;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005590
Michal Vasko2104e9f2020-03-06 08:23:25 +01005591 assert((set->type == LYXP_SET_NODE_SET) || (set->type == LYXP_SET_SCNODE_SET));
5592
Michal Vasko6346ece2019-09-24 13:12:53 +02005593 if ((ptr = ly_strnchr(*qname, ':', *qname_len))) {
5594 /* specific module */
5595 pref_len = ptr - *qname;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005596 mod = ly_resolve_prefix(set->ctx, *qname, pref_len, set->format, set->prefix_data);
Michal Vasko6346ece2019-09-24 13:12:53 +02005597
Michal Vasko004d3152020-06-11 19:59:22 +02005598 /* check for errors and non-implemented modules, as they are not valid */
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005599 if (!mod || !mod->implemented) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005600 LOGVAL(set->ctx, LY_VCODE_XP_INMOD, pref_len, *qname);
Michal Vasko6346ece2019-09-24 13:12:53 +02005601 return LY_EVALID;
5602 }
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005603
Michal Vasko6346ece2019-09-24 13:12:53 +02005604 *qname += pref_len + 1;
5605 *qname_len -= pref_len + 1;
5606 } else if (((*qname)[0] == '*') && (*qname_len == 1)) {
5607 /* all modules - special case */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005608 mod = NULL;
Michal Vasko6346ece2019-09-24 13:12:53 +02005609 } else {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005610 switch (set->format) {
Radek Krejci8df109d2021-04-23 12:19:08 +02005611 case LY_VALUE_SCHEMA:
5612 case LY_VALUE_SCHEMA_RESOLVED:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005613 /* current module */
5614 mod = set->cur_mod;
5615 break;
Radek Krejci224d4b42021-04-23 13:54:59 +02005616 case LY_VALUE_CANON:
Radek Krejci8df109d2021-04-23 12:19:08 +02005617 case LY_VALUE_JSON:
Radek Krejcif9943642021-04-26 10:18:21 +02005618 case LY_VALUE_LYB:
Michal Vaskoddd76592022-01-17 13:34:48 +01005619 case LY_VALUE_STR_NS:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005620 /* inherit parent (context node) module */
5621 if (ctx_scnode) {
5622 mod = ctx_scnode->module;
5623 } else {
5624 mod = NULL;
5625 }
5626 break;
Radek Krejci8df109d2021-04-23 12:19:08 +02005627 case LY_VALUE_XML:
Michal Vasko52143d12021-04-14 15:36:39 +02005628 /* all nodes need to be prefixed */
5629 LOGVAL(set->ctx, LYVE_DATA, "Non-prefixed node \"%.*s\" in XML xpath found.", *qname_len, *qname);
5630 return LY_EVALID;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005631 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005632 }
5633
Michal Vasko6346ece2019-09-24 13:12:53 +02005634 *moveto_mod = mod;
5635 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005636}
5637
5638/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02005639 * @brief Move context @p set to the root. Handles absolute path.
5640 * Result is LYXP_SET_NODE_SET.
5641 *
5642 * @param[in,out] set Set to use.
5643 * @param[in] options Xpath options.
Michal Vaskob0099a92020-08-31 14:55:23 +02005644 * @return LY_ERR value.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005645 */
Michal Vaskob0099a92020-08-31 14:55:23 +02005646static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005647moveto_root(struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005648{
aPiecek8b0cc152021-05-31 16:40:31 +02005649 assert(!(options & LYXP_SKIP_EXPR));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005650
5651 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02005652 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko7333cb32022-07-29 16:30:29 +02005653 LY_CHECK_RET(set_scnode_insert_node(set, NULL, set->root_type, LYXP_AXIS_SELF, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005654 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005655 set->type = LYXP_SET_NODE_SET;
5656 set->used = 0;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005657 set_insert_node(set, NULL, 0, set->root_type, 0);
Michal Vasko306e2832022-07-25 09:15:17 +02005658 set->non_child_axis = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005659 }
Michal Vaskob0099a92020-08-31 14:55:23 +02005660
5661 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005662}
5663
5664/**
5665 * @brief Check @p node as a part of NameTest processing.
5666 *
5667 * @param[in] node Node to check.
Michal Vasko49fec8e2022-05-24 10:28:33 +02005668 * @param[in] node_type Node type of @p node.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005669 * @param[in] set Set to read general context from.
Michal Vaskod3678892020-05-21 10:06:58 +02005670 * @param[in] node_name Node name in the dictionary to move to, NULL for any node.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005671 * @param[in] moveto_mod Expected module of the node, NULL for no prefix.
Michal Vaskocdad7122020-11-09 21:04:44 +01005672 * @param[in] options XPath options.
Michal Vasko6346ece2019-09-24 13:12:53 +02005673 * @return LY_ERR (LY_ENOT if node does not match, LY_EINCOMPLETE on unresolved when,
Michal Vaskocd2c88a2022-06-07 10:54:34 +02005674 * LY_EINVAL if neither node nor any children match)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005675 */
5676static LY_ERR
Michal Vasko49fec8e2022-05-24 10:28:33 +02005677moveto_node_check(const struct lyd_node *node, enum lyxp_node_type node_type, const struct lyxp_set *set,
5678 const char *node_name, const struct lys_module *moveto_mod, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005679{
Michal Vasko49fec8e2022-05-24 10:28:33 +02005680 if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT)) {
5681 assert(node_type == set->root_type);
5682
5683 if (node_name || moveto_mod) {
5684 /* root will not match a specific node */
5685 return LY_ENOT;
5686 }
5687 return LY_SUCCESS;
5688 } else if (node_type != LYXP_NODE_ELEM) {
5689 /* other types will not match */
5690 return LY_ENOT;
5691 }
5692
Michal Vaskodca9f122021-07-16 13:56:22 +02005693 if (!node->schema) {
5694 /* opaque node never matches */
5695 return LY_ENOT;
5696 }
5697
Michal Vasko03ff5a72019-09-11 13:49:33 +02005698 /* module check */
Michal Vasko19089f02022-06-07 11:02:11 +02005699 if (moveto_mod) {
5700 if (!(node->flags & LYD_EXT) && (node->schema->module != moveto_mod)) {
5701 return LY_ENOT;
5702 } else if ((node->flags & LYD_EXT) && strcmp(node->schema->module->name, moveto_mod->name)) {
5703 return LY_ENOT;
5704 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005705 }
5706
Michal Vasko5c4e5892019-11-14 12:31:38 +01005707 /* context check */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005708 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (node->schema->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005709 return LY_EINVAL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005710 } else if (set->context_op && (node->schema->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) &&
5711 (node->schema != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005712 return LY_EINVAL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005713 }
5714
5715 /* name check */
Michal Vasko19089f02022-06-07 11:02:11 +02005716 if (node_name) {
5717 if (!(node->flags & LYD_EXT) && (node->schema->name != node_name)) {
5718 return LY_ENOT;
5719 } else if ((node->flags & LYD_EXT) && strcmp(node->schema->name, node_name)) {
5720 return LY_ENOT;
5721 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005722 }
5723
Michal Vaskoa1424542019-11-14 16:08:52 +01005724 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01005725 if (!(options & LYXP_IGNORE_WHEN) && lysc_has_when(node->schema) && !(node->flags & LYD_WHEN_TRUE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005726 return LY_EINCOMPLETE;
Michal Vaskoa1424542019-11-14 16:08:52 +01005727 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005728
5729 /* match */
5730 return LY_SUCCESS;
5731}
5732
5733/**
Michal Vasko49fec8e2022-05-24 10:28:33 +02005734 * @brief Get the next node in a forward DFS.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005735 *
Michal Vasko49fec8e2022-05-24 10:28:33 +02005736 * @param[in] iter Last returned node.
5737 * @param[in] stop Node to stop the search on and not return.
5738 * @return Next node, NULL if there are no more.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005739 */
Michal Vasko49fec8e2022-05-24 10:28:33 +02005740static const struct lyd_node *
5741moveto_axis_node_next_dfs_forward(const struct lyd_node *iter, const struct lyd_node *stop)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005742{
Michal Vasko49fec8e2022-05-24 10:28:33 +02005743 const struct lyd_node *next = NULL;
5744
5745 /* 1) child */
5746 next = lyd_child(iter);
5747 if (!next) {
5748 if (iter == stop) {
5749 /* reached stop, no more descendants */
5750 return NULL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005751 }
Michal Vasko49fec8e2022-05-24 10:28:33 +02005752 /* 2) child next sibling */
5753 next = iter->next;
5754 }
5755 while (!next) {
5756 iter = lyd_parent(iter);
5757 if ((!stop && !iter) || (stop && (lyd_parent(iter) == lyd_parent(stop)))) {
5758 return NULL;
5759 }
5760 next = iter->next;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005761 }
5762
Michal Vasko49fec8e2022-05-24 10:28:33 +02005763 return next;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005764}
5765
5766/**
Michal Vasko49fec8e2022-05-24 10:28:33 +02005767 * @brief Get the next node in a backward DFS.
5768 *
5769 * @param[in] iter Last returned node.
5770 * @param[in] stop Node to stop the search on and not return.
5771 * @return Next node, NULL if there are no more.
5772 */
5773static const struct lyd_node *
5774moveto_axis_node_next_dfs_backward(const struct lyd_node *iter, const struct lyd_node *stop)
5775{
5776 const struct lyd_node *next = NULL;
5777
5778 /* 1) previous sibling innermost last child */
5779 next = iter->prev->next ? iter->prev : NULL;
5780 while (next && lyd_child(next)) {
5781 next = lyd_child(next);
5782 next = next->prev;
5783 }
5784
5785 if (!next) {
5786 /* 2) parent */
5787 iter = lyd_parent(iter);
5788 if ((!stop && !iter) || (stop && (lyd_parent(iter) == lyd_parent(stop)))) {
5789 return NULL;
5790 }
5791 next = iter;
5792 }
5793
5794 return next;
5795}
5796
5797/**
5798 * @brief Get the first node on an axis for a context node.
5799 *
5800 * @param[in,out] iter NULL, updated to the next node.
5801 * @param[in,out] iter_type Node type 0 of @p iter, updated to the node type of the next node.
5802 * @param[in] node Context node.
5803 * @param[in] node_type Type of @p node.
5804 * @param[in] axis Axis to use.
5805 * @param[in] set XPath set with the general context.
5806 * @return LY_SUCCESS on success.
5807 * @return LY_ENOTFOUND if no next node found.
5808 */
5809static LY_ERR
5810moveto_axis_node_next_first(const struct lyd_node **iter, enum lyxp_node_type *iter_type, const struct lyd_node *node,
5811 enum lyxp_node_type node_type, enum lyxp_axis axis, struct lyxp_set *set)
5812{
5813 const struct lyd_node *next = NULL;
5814 enum lyxp_node_type next_type = 0;
5815
5816 assert(!*iter);
5817 assert(!*iter_type);
5818
5819 switch (axis) {
5820 case LYXP_AXIS_ANCESTOR_OR_SELF:
5821 case LYXP_AXIS_DESCENDANT_OR_SELF:
5822 case LYXP_AXIS_SELF:
5823 /* return the context node */
5824 next = node;
5825 next_type = node_type;
5826 break;
5827
5828 case LYXP_AXIS_ANCESTOR:
5829 case LYXP_AXIS_PARENT:
5830 if (node_type == LYXP_NODE_ELEM) {
5831 next = lyd_parent(node);
5832 next_type = next ? LYXP_NODE_ELEM : set->root_type;
5833 } else if (node_type == LYXP_NODE_TEXT) {
5834 next = node;
5835 next_type = LYXP_NODE_ELEM;
5836 } else if (node_type == LYXP_NODE_META) {
5837 next = ((struct lyd_meta *)node)->parent;
5838 next_type = LYXP_NODE_ELEM;
5839 } /* else root does not have a parent */
5840 break;
5841
5842 case LYXP_AXIS_CHILD:
5843 if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT)) {
5844 assert(!node);
5845
5846 /* search in all the trees */
5847 next = set->tree;
5848 next_type = next ? LYXP_NODE_ELEM : 0;
5849 } else {
5850 /* search in children */
5851 next = lyd_child(node);
5852 next_type = next ? LYXP_NODE_ELEM : 0;
5853 }
5854 break;
5855
5856 case LYXP_AXIS_DESCENDANT:
5857 if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT)) {
5858 /* top-level nodes */
5859 next = set->tree;
5860 next_type = LYXP_NODE_ELEM;
5861 } else if (node_type == LYXP_NODE_ELEM) {
5862 /* start from the context node */
5863 next = moveto_axis_node_next_dfs_forward(node, node);
5864 next_type = next ? LYXP_NODE_ELEM : 0;
5865 } /* else no children */
5866 break;
5867
5868 case LYXP_AXIS_FOLLOWING:
5869 case LYXP_AXIS_FOLLOWING_SIBLING:
5870 if (node_type == LYXP_NODE_ELEM) {
5871 /* first next sibling */
5872 next = node->next;
5873 next_type = next ? LYXP_NODE_ELEM : 0;
5874 } /* else no sibling */
5875 break;
5876
5877 case LYXP_AXIS_PRECEDING:
5878 if ((node_type == LYXP_NODE_ELEM) && node->prev->next) {
5879 /* skip ancestors */
5880 next = moveto_axis_node_next_dfs_backward(node, NULL);
5881 assert(next);
5882 next_type = LYXP_NODE_ELEM;
5883 } /* else no sibling */
5884 break;
5885
5886 case LYXP_AXIS_PRECEDING_SIBLING:
5887 if (node_type == LYXP_NODE_ELEM) {
5888 /* first previous sibling */
5889 next = node->prev->next ? node->prev : NULL;
5890 next_type = next ? LYXP_NODE_ELEM : 0;
5891 } /* else no sibling */
5892 break;
5893
5894 case LYXP_AXIS_ATTRIBUTE:
5895 /* handled specially */
5896 assert(0);
5897 LOGINT(set->ctx);
5898 break;
5899 }
5900
5901 *iter = next;
5902 *iter_type = next_type;
5903 return next_type ? LY_SUCCESS : LY_ENOTFOUND;
5904}
5905
5906/**
5907 * @brief Iterate over all nodes on an axis for a context node.
5908 *
5909 * @param[in,out] iter Last returned node, start with NULL, updated to the next node.
5910 * @param[in,out] iter_type Node type of @p iter, start with 0, updated to the node type of the next node.
5911 * @param[in] node Context node.
5912 * @param[in] node_type Type of @p node.
5913 * @param[in] axis Axis to use.
5914 * @param[in] set XPath set with the general context.
5915 * @return LY_SUCCESS on success.
5916 * @return LY_ENOTFOUND if no next node found.
5917 */
5918static LY_ERR
5919moveto_axis_node_next(const struct lyd_node **iter, enum lyxp_node_type *iter_type, const struct lyd_node *node,
5920 enum lyxp_node_type node_type, enum lyxp_axis axis, struct lyxp_set *set)
5921{
5922 const struct lyd_node *next = NULL;
5923 enum lyxp_node_type next_type = 0;
5924
5925 if (!*iter_type) {
5926 /* first returned node */
5927 return moveto_axis_node_next_first(iter, iter_type, node, node_type, axis, set);
5928 }
5929
5930 switch (axis) {
5931 case LYXP_AXIS_ANCESTOR_OR_SELF:
5932 if ((*iter == node) && (*iter_type == node_type)) {
5933 /* fake first ancestor, we returned self before */
5934 *iter = NULL;
5935 *iter_type = 0;
5936 return moveto_axis_node_next_first(iter, iter_type, node, node_type, LYXP_AXIS_ANCESTOR, set);
5937 } /* else continue ancestor */
5938
5939 /* fallthrough */
5940 case LYXP_AXIS_ANCESTOR:
5941 if (*iter_type == LYXP_NODE_ELEM) {
5942 /* iter parent */
5943 next = lyd_parent(*iter);
5944 next_type = next ? LYXP_NODE_ELEM : set->root_type;
5945 } /* else root, no ancestors */
5946 break;
5947
5948 case LYXP_AXIS_CHILD:
5949 assert(*iter_type == LYXP_NODE_ELEM);
5950
5951 /* next sibling (child) */
5952 next = (*iter)->next;
5953 next_type = next ? LYXP_NODE_ELEM : 0;
5954 break;
5955
5956 case LYXP_AXIS_DESCENDANT_OR_SELF:
5957 if ((*iter == node) && (*iter_type == node_type)) {
5958 /* fake first descendant, we returned self before */
5959 *iter = NULL;
5960 *iter_type = 0;
5961 return moveto_axis_node_next_first(iter, iter_type, node, node_type, LYXP_AXIS_DESCENDANT, set);
5962 } /* else continue descendant */
5963
5964 /* fallthrough */
5965 case LYXP_AXIS_DESCENDANT:
5966 assert(*iter_type == LYXP_NODE_ELEM);
5967 next = moveto_axis_node_next_dfs_forward(*iter, node);
5968 next_type = next ? LYXP_NODE_ELEM : 0;
5969 break;
5970
5971 case LYXP_AXIS_FOLLOWING:
5972 assert(*iter_type == LYXP_NODE_ELEM);
5973 next = moveto_axis_node_next_dfs_forward(*iter, NULL);
5974 next_type = next ? LYXP_NODE_ELEM : 0;
5975 break;
5976
5977 case LYXP_AXIS_FOLLOWING_SIBLING:
5978 assert(*iter_type == LYXP_NODE_ELEM);
5979
5980 /* next sibling */
5981 next = (*iter)->next;
5982 next_type = next ? LYXP_NODE_ELEM : 0;
5983 break;
5984
5985 case LYXP_AXIS_PARENT:
5986 case LYXP_AXIS_SELF:
5987 /* parent/self was returned before */
5988 break;
5989
5990 case LYXP_AXIS_PRECEDING:
5991 assert(*iter_type == LYXP_NODE_ELEM);
5992 next = moveto_axis_node_next_dfs_backward(*iter, NULL);
5993 next_type = next ? LYXP_NODE_ELEM : 0;
5994 break;
5995
5996 case LYXP_AXIS_PRECEDING_SIBLING:
5997 assert(*iter_type == LYXP_NODE_ELEM);
5998
5999 /* previous sibling */
6000 next = (*iter)->prev->next ? (*iter)->prev : NULL;
6001 next_type = next ? LYXP_NODE_ELEM : 0;
6002 break;
6003
6004 case LYXP_AXIS_ATTRIBUTE:
6005 /* handled specially */
6006 assert(0);
6007 LOGINT(set->ctx);
6008 break;
6009 }
6010
6011 *iter = next;
6012 *iter_type = next_type;
6013 return next_type ? LY_SUCCESS : LY_ENOTFOUND;
6014}
6015
6016/**
6017 * @brief Move context @p set to a node. Result is LYXP_SET_NODE_SET. Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006018 *
6019 * @param[in,out] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006020 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02006021 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko49fec8e2022-05-24 10:28:33 +02006022 * @param[in] axis Axis to search on.
Michal Vaskocdad7122020-11-09 21:04:44 +01006023 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006024 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6025 */
6026static LY_ERR
Michal Vasko49fec8e2022-05-24 10:28:33 +02006027moveto_node(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, enum lyxp_axis axis,
6028 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006029{
Michal Vasko230cf972021-12-02 12:31:00 +01006030 LY_ERR r, rc = LY_SUCCESS;
Michal Vasko49fec8e2022-05-24 10:28:33 +02006031 const struct lyd_node *iter;
6032 enum lyxp_node_type iter_type;
Michal Vasko230cf972021-12-02 12:31:00 +01006033 struct lyxp_set result;
Michal Vasko49fec8e2022-05-24 10:28:33 +02006034 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006035
aPiecek8b0cc152021-05-31 16:40:31 +02006036 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006037 return LY_SUCCESS;
6038 }
6039
6040 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006041 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006042 return LY_EVALID;
6043 }
6044
Michal Vasko230cf972021-12-02 12:31:00 +01006045 /* init result set */
6046 set_init(&result, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006047
Michal Vasko49fec8e2022-05-24 10:28:33 +02006048 for (i = 0; i < set->used; ++i) {
6049 /* iterate over all the nodes on the axis of the node */
6050 iter = NULL;
6051 iter_type = 0;
6052 while (!moveto_axis_node_next(&iter, &iter_type, set->val.nodes[i].node, set->val.nodes[i].type, axis, set)) {
6053 r = moveto_node_check(iter, iter_type, set, ncname, moveto_mod, options);
6054 if (r == LY_EINCOMPLETE) {
Michal Vasko230cf972021-12-02 12:31:00 +01006055 rc = r;
6056 goto cleanup;
Michal Vasko49fec8e2022-05-24 10:28:33 +02006057 } else if (r) {
6058 continue;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006059 }
Michal Vasko49fec8e2022-05-24 10:28:33 +02006060
6061 /* check for duplicates if they are possible */
6062 switch (axis) {
6063 case LYXP_AXIS_ANCESTOR:
6064 case LYXP_AXIS_ANCESTOR_OR_SELF:
6065 case LYXP_AXIS_DESCENDANT:
6066 case LYXP_AXIS_DESCENDANT_OR_SELF:
6067 case LYXP_AXIS_FOLLOWING:
6068 case LYXP_AXIS_FOLLOWING_SIBLING:
6069 case LYXP_AXIS_PARENT:
6070 case LYXP_AXIS_PRECEDING:
6071 case LYXP_AXIS_PRECEDING_SIBLING:
Michal Vasko306e2832022-07-25 09:15:17 +02006072 result.non_child_axis = 1;
Michal Vasko49fec8e2022-05-24 10:28:33 +02006073 if (set_dup_node_check(&result, iter, iter_type, -1)) {
6074 continue;
6075 }
6076 break;
6077 case LYXP_AXIS_CHILD:
6078 case LYXP_AXIS_SELF:
6079 break;
6080 case LYXP_AXIS_ATTRIBUTE:
6081 /* handled specially */
6082 assert(0);
6083 LOGINT(set->ctx);
6084 break;
6085 }
6086
6087 /* matching node */
6088 set_insert_node(&result, iter, 0, iter_type, result.used);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006089 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006090 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006091
Michal Vasko230cf972021-12-02 12:31:00 +01006092 /* move result to the set */
6093 lyxp_set_free_content(set);
6094 *set = result;
6095 result.type = LYXP_SET_NUMBER;
Michal Vasko49fec8e2022-05-24 10:28:33 +02006096
Michal Vasko306e2832022-07-25 09:15:17 +02006097 /* sort the final set if the document order could have been broken */
6098 if (set->non_child_axis) {
6099 set_sort(set);
6100 } else {
6101 assert(!set_sort(set));
6102 }
Michal Vasko230cf972021-12-02 12:31:00 +01006103
6104cleanup:
6105 lyxp_set_free_content(&result);
6106 return rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006107}
6108
6109/**
Michal Vasko49fec8e2022-05-24 10:28:33 +02006110 * @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 +02006111 *
6112 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02006113 * @param[in] scnode Matching node schema.
Michal Vasko004d3152020-06-11 19:59:22 +02006114 * @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 +01006115 * @param[in] options XPath options.
Michal Vaskod3678892020-05-21 10:06:58 +02006116 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6117 */
6118static LY_ERR
Michal Vasko49fec8e2022-05-24 10:28:33 +02006119moveto_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 +01006120 uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02006121{
Michal Vaskoddd76592022-01-17 13:34:48 +01006122 LY_ERR ret = LY_SUCCESS, r;
Michal Vaskod3678892020-05-21 10:06:58 +02006123 uint32_t i;
Michal Vaskod3678892020-05-21 10:06:58 +02006124 const struct lyd_node *siblings;
Michal Vasko230cf972021-12-02 12:31:00 +01006125 struct lyxp_set result;
Michal Vasko004d3152020-06-11 19:59:22 +02006126 struct lyd_node *sub, *inst = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02006127
Michal Vasko004d3152020-06-11 19:59:22 +02006128 assert(scnode && (!(scnode->nodetype & (LYS_LIST | LYS_LEAFLIST)) || predicates));
Michal Vaskod3678892020-05-21 10:06:58 +02006129
Michal Vasko50aaa072021-12-02 13:11:56 +01006130 /* init result set */
6131 set_init(&result, set);
6132
aPiecek8b0cc152021-05-31 16:40:31 +02006133 if (options & LYXP_SKIP_EXPR) {
Michal Vasko004d3152020-06-11 19:59:22 +02006134 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02006135 }
6136
6137 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006138 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko004d3152020-06-11 19:59:22 +02006139 ret = LY_EVALID;
6140 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02006141 }
6142
6143 /* context check for all the nodes since we have the schema node */
6144 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) {
6145 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02006146 goto cleanup;
Michal Vasko69730152020-10-09 16:30:07 +02006147 } else if (set->context_op && (scnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) &&
6148 (scnode != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02006149 lyxp_set_free_content(set);
6150 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +02006151 }
6152
6153 /* create specific data instance if needed */
6154 if (scnode->nodetype == LYS_LIST) {
6155 LY_CHECK_GOTO(ret = lyd_create_list(scnode, predicates, &inst), cleanup);
6156 } else if (scnode->nodetype == LYS_LEAFLIST) {
6157 LY_CHECK_GOTO(ret = lyd_create_term2(scnode, &predicates[0].value, &inst), cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02006158 }
6159
Michal Vasko230cf972021-12-02 12:31:00 +01006160 for (i = 0; i < set->used; ++i) {
Michal Vaskod3678892020-05-21 10:06:58 +02006161 siblings = NULL;
6162
6163 if ((set->val.nodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.nodes[i].type == LYXP_NODE_ROOT)) {
6164 assert(!set->val.nodes[i].node);
6165
6166 /* search in all the trees */
6167 siblings = set->tree;
6168 } else if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
6169 /* search in children */
Radek Krejcia1c1e542020-09-29 16:06:52 +02006170 siblings = lyd_child(set->val.nodes[i].node);
Michal Vaskod3678892020-05-21 10:06:58 +02006171 }
6172
6173 /* find the node using hashes */
Michal Vasko004d3152020-06-11 19:59:22 +02006174 if (inst) {
Michal Vaskoddd76592022-01-17 13:34:48 +01006175 r = lyd_find_sibling_first(siblings, inst, &sub);
Michal Vaskod3678892020-05-21 10:06:58 +02006176 } else {
Michal Vaskoddd76592022-01-17 13:34:48 +01006177 r = lyd_find_sibling_val(siblings, scnode, NULL, 0, &sub);
Michal Vaskod3678892020-05-21 10:06:58 +02006178 }
Michal Vaskoddd76592022-01-17 13:34:48 +01006179 LY_CHECK_ERR_GOTO(r && (r != LY_ENOTFOUND), ret = r, cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02006180
6181 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01006182 if (!(options & LYXP_IGNORE_WHEN) && sub && lysc_has_when(sub->schema) && !(sub->flags & LYD_WHEN_TRUE)) {
Michal Vasko004d3152020-06-11 19:59:22 +02006183 ret = LY_EINCOMPLETE;
6184 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02006185 }
6186
6187 if (sub) {
6188 /* pos filled later */
Michal Vasko230cf972021-12-02 12:31:00 +01006189 set_insert_node(&result, sub, 0, LYXP_NODE_ELEM, result.used);
Michal Vaskod3678892020-05-21 10:06:58 +02006190 }
6191 }
6192
Michal Vasko230cf972021-12-02 12:31:00 +01006193 /* move result to the set */
6194 lyxp_set_free_content(set);
6195 *set = result;
6196 result.type = LYXP_SET_NUMBER;
6197 assert(!set_sort(set));
6198
Michal Vasko004d3152020-06-11 19:59:22 +02006199cleanup:
Michal Vasko230cf972021-12-02 12:31:00 +01006200 lyxp_set_free_content(&result);
Michal Vasko004d3152020-06-11 19:59:22 +02006201 lyd_free_tree(inst);
6202 return ret;
Michal Vaskod3678892020-05-21 10:06:58 +02006203}
6204
6205/**
Michal Vasko49fec8e2022-05-24 10:28:33 +02006206 * @brief Check @p node as a part of schema NameTest processing.
6207 *
6208 * @param[in] node Schema node to check.
6209 * @param[in] ctx_scnode Context node.
6210 * @param[in] set Set to read general context from.
6211 * @param[in] node_name Node name in the dictionary to move to, NULL for any nodes.
6212 * @param[in] moveto_mod Expected module of the node, NULL for no prefix.
6213 * @return LY_ERR (LY_ENOT if node does not match, LY_EINVAL if neither node nor any children match)
6214 */
6215static LY_ERR
6216moveto_scnode_check(const struct lysc_node *node, const struct lysc_node *ctx_scnode, const struct lyxp_set *set,
6217 const char *node_name, const struct lys_module *moveto_mod)
6218{
6219 if (!moveto_mod && node_name) {
6220 switch (set->format) {
6221 case LY_VALUE_SCHEMA:
6222 case LY_VALUE_SCHEMA_RESOLVED:
6223 /* use current module */
6224 moveto_mod = set->cur_mod;
6225 break;
6226 case LY_VALUE_JSON:
6227 case LY_VALUE_LYB:
6228 case LY_VALUE_STR_NS:
6229 /* inherit module of the context node, if any */
6230 if (ctx_scnode) {
6231 moveto_mod = ctx_scnode->module;
6232 }
6233 break;
6234 case LY_VALUE_CANON:
6235 case LY_VALUE_XML:
6236 /* not defined */
6237 LOGINT(set->ctx);
6238 return LY_EINVAL;
6239 }
6240 }
6241
6242 if (!node) {
6243 /* root will not match a specific node */
6244 if (node_name || moveto_mod) {
6245 return LY_ENOT;
6246 }
6247 return LY_SUCCESS;
6248 }
6249
6250 /* module check */
6251 if (moveto_mod && (node->module != moveto_mod)) {
6252 return LY_ENOT;
6253 }
6254
6255 /* context check */
6256 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (node->flags & LYS_CONFIG_R)) {
6257 return LY_EINVAL;
6258 } else if (set->context_op && (node->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && (node != set->context_op)) {
6259 return LY_EINVAL;
6260 }
6261
6262 /* name check */
6263 if (node_name && (node->name != node_name)) {
6264 return LY_ENOT;
6265 }
6266
6267 /* match */
6268 return LY_SUCCESS;
6269}
6270
6271/**
6272 * @brief Get the next node in a forward schema node DFS.
6273 *
6274 * @param[in] iter Last returned node.
6275 * @param[in] stop Node to stop the search on and not return.
6276 * @param[in] getnext_opts Options for ::lys_getnext().
6277 * @return Next node, NULL if there are no more.
6278 */
6279static const struct lysc_node *
6280moveto_axis_scnode_next_dfs_forward(const struct lysc_node *iter, const struct lysc_node *stop, uint32_t getnext_opts)
6281{
6282 const struct lysc_node *next = NULL;
6283
6284 next = lysc_node_child(iter);
6285 if (!next) {
6286 /* no children, try siblings */
Michal Vasko34a22fe2022-06-15 07:58:55 +02006287 if ((iter == stop) || !lysc_data_parent(iter)) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02006288 /* we are done, no next element to process */
6289 return NULL;
6290 }
6291
6292 next = lys_getnext(iter, lysc_data_parent(iter), NULL, getnext_opts);
6293 }
6294 while (!next && iter) {
6295 /* parent is already processed, go to its sibling */
6296 iter = iter->parent;
Michal Vasko34a22fe2022-06-15 07:58:55 +02006297 if ((iter == stop) || !lysc_data_parent(iter)) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02006298 /* we are done, no next element to process */
6299 return NULL;
6300 }
6301 next = lys_getnext(iter, lysc_data_parent(iter), NULL, getnext_opts);
6302 }
6303
6304 return next;
6305}
6306
6307/**
6308 * @brief Consider schema node based on its in_ctx enum value.
6309 *
6310 * @param[in,out] in_ctx In_ctx enum of the schema node, may be updated.
6311 * @param[in] axis Axis to use.
6312 * @return LY_SUCCESS on success.
6313 * @return LY_ENOT if the node should not be returned.
6314 */
6315static LY_ERR
6316moveto_axis_scnode_next_in_ctx(int32_t *in_ctx, enum lyxp_axis axis)
6317{
6318 switch (axis) {
6319 case LYXP_AXIS_SELF:
6320 if ((*in_ctx == LYXP_SET_SCNODE_START) || (*in_ctx == LYXP_SET_SCNODE_ATOM_CTX)) {
6321 /* additionally put the start node into context */
6322 *in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
6323 return LY_SUCCESS;
6324 }
6325 break;
6326 case LYXP_AXIS_PARENT:
6327 case LYXP_AXIS_ANCESTOR_OR_SELF:
6328 case LYXP_AXIS_ANCESTOR:
6329 case LYXP_AXIS_DESCENDANT_OR_SELF:
6330 case LYXP_AXIS_DESCENDANT:
6331 case LYXP_AXIS_FOLLOWING:
6332 case LYXP_AXIS_FOLLOWING_SIBLING:
6333 case LYXP_AXIS_PRECEDING:
6334 case LYXP_AXIS_PRECEDING_SIBLING:
6335 case LYXP_AXIS_CHILD:
6336 if (*in_ctx == LYXP_SET_SCNODE_START) {
6337 /* remember that context node was used */
6338 *in_ctx = LYXP_SET_SCNODE_START_USED;
6339 return LY_SUCCESS;
6340 } else if (*in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
6341 /* traversed */
6342 *in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
6343 return LY_SUCCESS;
6344 }
6345 break;
6346 case LYXP_AXIS_ATTRIBUTE:
6347 /* unreachable */
6348 assert(0);
6349 LOGINT(NULL);
6350 break;
6351 }
6352
6353 return LY_ENOT;
6354}
6355
6356/**
6357 * @brief Get previous sibling for a schema node.
6358 *
6359 * @param[in] scnode Schema node.
6360 * @param[in] getnext_opts Options for ::lys_getnext().
6361 * @return Previous sibling, NULL if none.
6362 */
6363static const struct lysc_node *
6364moveto_axis_scnode_preceding_sibling(const struct lysc_node *scnode, uint32_t getnext_opts)
6365{
6366 const struct lysc_node *next = NULL, *prev = NULL;
6367
6368 while ((next = lys_getnext(next, lysc_data_parent(scnode), scnode->module->compiled, getnext_opts))) {
6369 if (next == scnode) {
6370 break;
6371 }
6372
6373 prev = next;
6374 }
6375
6376 return prev;
6377}
6378
6379/**
6380 * @brief Get the first schema node on an axis for a context node.
6381 *
6382 * @param[in,out] iter Last returned node, start with NULL, updated to the next node.
6383 * @param[in,out] iter_type Node type of @p iter, start with 0, updated to the node type of the next node.
6384 * @param[in,out] iter_mod Internal module iterator, do not change.
6385 * @param[in,out] iter_mod_idx Internal module index iterator, do not change.
6386 * @param[in] scnode Context node.
6387 * @param[in] node_type Type of @p scnode.
6388 * @param[in] in_ctx In_ctx enum of @p scnode.
6389 * @param[in] axis Axis to use.
6390 * @param[in] set XPath set with the general context.
6391 * @param[in] getnext_opts Options for ::lys_getnext().
6392 * @return LY_SUCCESS on success.
6393 * @return LY_ENOTFOUND if no next node found.
6394 */
6395static LY_ERR
6396moveto_axis_scnode_next_first(const struct lysc_node **iter, enum lyxp_node_type *iter_type, const struct lys_module **iter_mod,
6397 uint32_t *iter_mod_idx, const struct lysc_node *scnode, enum lyxp_node_type node_type, enum lyxp_axis axis,
6398 struct lyxp_set *set, uint32_t getnext_opts)
6399{
6400 const struct lysc_node *next = NULL;
6401 enum lyxp_node_type next_type = 0;
6402
6403 assert(!*iter);
6404 assert(!*iter_type);
6405
6406 *iter_mod = NULL;
6407 *iter_mod_idx = 0;
6408
6409 switch (axis) {
6410 case LYXP_AXIS_ANCESTOR_OR_SELF:
6411 case LYXP_AXIS_DESCENDANT_OR_SELF:
6412 case LYXP_AXIS_SELF:
6413 if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT) || (node_type == LYXP_NODE_ELEM)) {
6414 /* just return the node */
6415 next = scnode;
6416 next_type = node_type;
6417 }
6418 break;
6419
6420 case LYXP_AXIS_ANCESTOR:
6421 case LYXP_AXIS_PARENT:
6422 if (node_type == LYXP_NODE_ELEM) {
6423 next = lysc_data_parent(scnode);
6424 next_type = next ? LYXP_NODE_ELEM : set->root_type;
6425 } /* else no parent */
6426 break;
6427
6428 case LYXP_AXIS_DESCENDANT:
6429 case LYXP_AXIS_CHILD:
6430 if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT)) {
6431 /* it can actually be in any module, it's all <running>, and even if it's moveto_mod (if set),
6432 * it can be in a top-level augment */
6433 while ((*iter_mod = ly_ctx_get_module_iter(set->ctx, iter_mod_idx))) {
6434 /* module may not be implemented or not compiled yet */
6435 if (!(*iter_mod)->compiled) {
6436 continue;
6437 }
6438
6439 /* get next node */
6440 if ((next = lys_getnext(NULL, NULL, (*iter_mod)->compiled, getnext_opts))) {
6441 next_type = LYXP_NODE_ELEM;
6442 break;
6443 }
6444 }
6445 } else if (node_type == LYXP_NODE_ELEM) {
6446 /* get next node */
6447 next = lys_getnext(NULL, scnode, NULL, getnext_opts);
6448 next_type = next ? LYXP_NODE_ELEM : 0;
6449 }
6450 break;
6451
6452 case LYXP_AXIS_FOLLOWING:
6453 case LYXP_AXIS_FOLLOWING_SIBLING:
6454 if (node_type == LYXP_NODE_ELEM) {
6455 /* first next sibling */
6456 next = lys_getnext(scnode, lysc_data_parent(scnode), scnode->module->compiled, getnext_opts);
6457 next_type = next ? LYXP_NODE_ELEM : 0;
6458 } /* else no sibling */
6459 break;
6460
6461 case LYXP_AXIS_PRECEDING:
6462 case LYXP_AXIS_PRECEDING_SIBLING:
6463 if (node_type == LYXP_NODE_ELEM) {
6464 /* first parent sibling */
6465 next = lys_getnext(NULL, lysc_data_parent(scnode), scnode->module->compiled, getnext_opts);
6466 if (next == scnode) {
6467 /* no preceding sibling */
6468 next = NULL;
6469 }
6470 next_type = next ? LYXP_NODE_ELEM : 0;
6471 } /* else no sibling */
6472 break;
6473
6474 case LYXP_AXIS_ATTRIBUTE:
6475 /* unreachable */
6476 assert(0);
6477 LOGINT(set->ctx);
6478 break;
6479 }
6480
6481 *iter = next;
6482 *iter_type = next_type;
6483 return next_type ? LY_SUCCESS : LY_ENOTFOUND;
6484}
6485
6486/**
6487 * @brief Iterate over all schema nodes on an axis for a context node.
6488 *
6489 * @param[in,out] iter Last returned node, start with NULL, updated to the next node.
6490 * @param[in,out] iter_type Node type of @p iter, start with 0, updated to the node type of the next node.
6491 * @param[in,out] iter_mod Internal module iterator, do not change.
6492 * @param[in,out] iter_mod_idx Internal module index iterator, do not change.
6493 * @param[in] scnode Context node.
6494 * @param[in] node_type Type of @p scnode.
6495 * @param[in] axis Axis to use.
6496 * @param[in] set XPath set with the general context.
6497 * @param[in] getnext_opts Options for ::lys_getnext().
6498 * @return LY_SUCCESS on success.
6499 * @return LY_ENOTFOUND if no next node found.
6500 */
6501static LY_ERR
6502moveto_axis_scnode_next(const struct lysc_node **iter, enum lyxp_node_type *iter_type, const struct lys_module **iter_mod,
6503 uint32_t *iter_mod_idx, const struct lysc_node *scnode, enum lyxp_node_type node_type, enum lyxp_axis axis,
6504 struct lyxp_set *set, uint32_t getnext_opts)
6505{
6506 const struct lysc_node *next = NULL, *dfs_stop;
6507 enum lyxp_node_type next_type = 0;
6508
6509 if (!*iter_type) {
6510 /* first returned node */
6511 return moveto_axis_scnode_next_first(iter, iter_type, iter_mod, iter_mod_idx, scnode, node_type, axis, set,
6512 getnext_opts);
6513 }
6514
6515 switch (axis) {
6516 case LYXP_AXIS_PARENT:
6517 case LYXP_AXIS_SELF:
6518 /* parent/self was returned before */
6519 break;
6520
6521 case LYXP_AXIS_ANCESTOR_OR_SELF:
6522 if ((*iter == scnode) && (*iter_type == node_type)) {
6523 /* fake first ancestor, we returned self before */
6524 *iter = NULL;
6525 *iter_type = 0;
6526 return moveto_axis_scnode_next_first(iter, iter_type, iter_mod, iter_mod_idx, scnode, node_type,
6527 LYXP_AXIS_ANCESTOR, set, getnext_opts);
6528 } /* else continue ancestor */
6529
6530 /* fallthrough */
6531 case LYXP_AXIS_ANCESTOR:
6532 if (*iter_type == LYXP_NODE_ELEM) {
6533 next = lysc_data_parent(*iter);
6534 next_type = next ? LYXP_NODE_ELEM : set->root_type;
6535 } /* else no ancestor */
6536 break;
6537
6538 case LYXP_AXIS_DESCENDANT_OR_SELF:
6539 if ((*iter == scnode) && (*iter_type == node_type)) {
6540 /* fake first descendant, we returned self before */
6541 *iter = NULL;
6542 *iter_type = 0;
6543 return moveto_axis_scnode_next_first(iter, iter_type, iter_mod, iter_mod_idx, scnode, node_type,
6544 LYXP_AXIS_DESCENDANT, set, getnext_opts);
6545 } /* else DFS until context node */
6546 dfs_stop = scnode;
6547
6548 /* fallthrough */
6549 case LYXP_AXIS_DESCENDANT:
6550 if (axis == LYXP_AXIS_DESCENDANT) {
6551 /* DFS until the context node */
6552 dfs_stop = scnode;
6553 }
6554
6555 /* fallthrough */
6556 case LYXP_AXIS_PRECEDING:
6557 if (axis == LYXP_AXIS_PRECEDING) {
6558 /* DFS until the previous sibling */
6559 dfs_stop = moveto_axis_scnode_preceding_sibling(scnode, getnext_opts);
6560 assert(dfs_stop);
6561
6562 if (*iter == dfs_stop) {
6563 /* we are done */
6564 break;
6565 }
6566 }
6567
6568 /* fallthrough */
6569 case LYXP_AXIS_FOLLOWING:
6570 if (axis == LYXP_AXIS_FOLLOWING) {
6571 /* DFS through the whole module */
6572 dfs_stop = NULL;
6573 }
6574
6575 /* nested nodes */
6576 assert(*iter);
6577 next = moveto_axis_scnode_next_dfs_forward(*iter, dfs_stop, getnext_opts);
6578 if (next) {
6579 next_type = LYXP_NODE_ELEM;
6580 break;
6581 } /* else get next top-level node just like a child */
6582
6583 /* fallthrough */
6584 case LYXP_AXIS_CHILD:
6585 case LYXP_AXIS_FOLLOWING_SIBLING:
6586 if (!*iter_mod) {
6587 /* nodes from a single module */
6588 if ((next = lys_getnext(*iter, lysc_data_parent(*iter), (*iter)->module->compiled, getnext_opts))) {
6589 next_type = LYXP_NODE_ELEM;
6590 break;
6591 }
6592
6593 assert(scnode);
6594 if (!lysc_data_parent(scnode)) {
6595 /* iterating over top-level nodes, find next */
6596 while (lysc_data_parent(*iter)) {
6597 *iter = lysc_data_parent(*iter);
6598 }
6599 if ((next = lys_getnext(*iter, NULL, (*iter)->module->compiled, getnext_opts))) {
6600 next_type = LYXP_NODE_ELEM;
6601 break;
6602 }
6603 }
6604 }
6605
6606 while (*iter_mod) {
6607 /* module top-level nodes */
6608 if ((next = lys_getnext(*iter, NULL, (*iter_mod)->compiled, getnext_opts))) {
6609 next_type = LYXP_NODE_ELEM;
6610 break;
6611 }
6612
6613 /* get next module */
6614 while ((*iter_mod = ly_ctx_get_module_iter(set->ctx, iter_mod_idx))) {
6615 /* module may not be implemented or not compiled yet */
6616 if ((*iter_mod)->compiled) {
6617 break;
6618 }
6619 }
6620
6621 /* new module, start over */
6622 *iter = NULL;
6623 }
6624 break;
6625
6626 case LYXP_AXIS_PRECEDING_SIBLING:
6627 assert(*iter);
6628
6629 /* next parent sibling until scnode */
6630 next = lys_getnext(*iter, lysc_data_parent(*iter), (*iter)->module->compiled, getnext_opts);
6631 if (next == scnode) {
6632 /* no previous sibling */
6633 next = NULL;
6634 }
6635 next_type = next ? LYXP_NODE_ELEM : 0;
6636 break;
6637
6638 case LYXP_AXIS_ATTRIBUTE:
6639 /* unreachable */
6640 assert(0);
6641 LOGINT(set->ctx);
6642 break;
6643 }
6644
6645 *iter = next;
6646 *iter_type = next_type;
6647 return next_type ? LY_SUCCESS : LY_ENOTFOUND;
6648}
6649
6650/**
Michal Vaskod3678892020-05-21 10:06:58 +02006651 * @brief Move context @p set to a schema node. Result is LYXP_SET_SCNODE_SET (or LYXP_SET_EMPTY).
6652 *
6653 * @param[in,out] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006654 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02006655 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko49fec8e2022-05-24 10:28:33 +02006656 * @param[in] axis Axis to search on.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006657 * @param[in] options XPath options.
6658 * @return LY_ERR
6659 */
6660static LY_ERR
Michal Vasko49fec8e2022-05-24 10:28:33 +02006661moveto_scnode(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, enum lyxp_axis axis,
6662 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006663{
Radek Krejci857189e2020-09-01 13:26:36 +02006664 ly_bool temp_ctx = 0;
Michal Vasko49fec8e2022-05-24 10:28:33 +02006665 uint32_t getnext_opts, orig_used, i, mod_idx, idx;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006666 const struct lys_module *mod;
Michal Vasko49fec8e2022-05-24 10:28:33 +02006667 const struct lysc_node *iter;
6668 enum lyxp_node_type iter_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006669
aPiecek8b0cc152021-05-31 16:40:31 +02006670 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006671 return LY_SUCCESS;
6672 }
6673
6674 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006675 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006676 return LY_EVALID;
6677 }
6678
Michal Vaskocafad9d2019-11-07 15:20:03 +01006679 /* getnext opts */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01006680 getnext_opts = 0;
Michal Vaskocafad9d2019-11-07 15:20:03 +01006681 if (options & LYXP_SCNODE_OUTPUT) {
6682 getnext_opts |= LYS_GETNEXT_OUTPUT;
6683 }
6684
Michal Vasko03ff5a72019-09-11 13:49:33 +02006685 orig_used = set->used;
6686 for (i = 0; i < orig_used; ++i) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02006687 /* update in_ctx first */
6688 if (moveto_axis_scnode_next_in_ctx(&set->val.scnodes[i].in_ctx, axis)) {
6689 /* not usable, skip */
6690 continue;
6691 }
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006692
Michal Vasko49fec8e2022-05-24 10:28:33 +02006693 iter = NULL;
6694 iter_type = 0;
6695 while (!moveto_axis_scnode_next(&iter, &iter_type, &mod, &mod_idx, set->val.scnodes[i].scnode,
6696 set->val.scnodes[i].type, axis, set, getnext_opts)) {
6697 if (moveto_scnode_check(iter, NULL, set, ncname, moveto_mod)) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006698 continue;
6699 }
6700
Michal Vasko49fec8e2022-05-24 10:28:33 +02006701 /* insert */
Michal Vasko7333cb32022-07-29 16:30:29 +02006702 LY_CHECK_RET(set_scnode_insert_node(set, iter, iter_type, axis, &idx));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006703
Michal Vasko49fec8e2022-05-24 10:28:33 +02006704 /* we need to prevent these nodes from being considered in this moveto */
6705 if ((idx < orig_used) && (idx > i)) {
6706 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX;
6707 temp_ctx = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006708 }
6709 }
6710 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006711
6712 /* correct temporary in_ctx values */
6713 if (temp_ctx) {
6714 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006715 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_NEW_CTX) {
6716 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006717 }
6718 }
6719 }
6720
6721 return LY_SUCCESS;
6722}
6723
6724/**
Michal Vasko49fec8e2022-05-24 10:28:33 +02006725 * @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 +02006726 * Context position aware.
6727 *
6728 * @param[in] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006729 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02006730 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vaskocdad7122020-11-09 21:04:44 +01006731 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006732 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6733 */
6734static LY_ERR
Michal Vasko49fec8e2022-05-24 10:28:33 +02006735moveto_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 +02006736{
6737 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006738 const struct lyd_node *next, *elem, *start;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006739 struct lyxp_set ret_set;
6740 LY_ERR rc;
6741
aPiecek8b0cc152021-05-31 16:40:31 +02006742 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006743 return LY_SUCCESS;
6744 }
6745
6746 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006747 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006748 return LY_EVALID;
6749 }
6750
Michal Vasko9f96a052020-03-10 09:41:45 +01006751 /* 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 +02006752 rc = xpath_pi_node(set, LYXP_AXIS_CHILD, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006753 LY_CHECK_RET(rc);
6754
Michal Vasko6346ece2019-09-24 13:12:53 +02006755 /* this loop traverses all the nodes in the set and adds/keeps only those that match qname */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006756 set_init(&ret_set, set);
6757 for (i = 0; i < set->used; ++i) {
6758
6759 /* TREE DFS */
6760 start = set->val.nodes[i].node;
6761 for (elem = next = start; elem; elem = next) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02006762 rc = moveto_node_check(elem, LYXP_NODE_ELEM, set, ncname, moveto_mod, options);
Michal Vasko6346ece2019-09-24 13:12:53 +02006763 if (!rc) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006764 /* add matching node into result set */
6765 set_insert_node(&ret_set, elem, 0, LYXP_NODE_ELEM, ret_set.used);
6766 if (set_dup_node_check(set, elem, LYXP_NODE_ELEM, i)) {
6767 /* the node is a duplicate, we'll process it later in the set */
6768 goto skip_children;
6769 }
Michal Vasko6346ece2019-09-24 13:12:53 +02006770 } else if (rc == LY_EINCOMPLETE) {
Michal Vasko6346ece2019-09-24 13:12:53 +02006771 return rc;
6772 } else if (rc == LY_EINVAL) {
6773 goto skip_children;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006774 }
6775
6776 /* TREE DFS NEXT ELEM */
6777 /* select element for the next run - children first */
Radek Krejcia1c1e542020-09-29 16:06:52 +02006778 next = lyd_child(elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006779 if (!next) {
6780skip_children:
6781 /* no children, so try siblings, but only if it's not the start,
6782 * that is considered to be the root and it's siblings are not traversed */
6783 if (elem != start) {
6784 next = elem->next;
6785 } else {
6786 break;
6787 }
6788 }
6789 while (!next) {
6790 /* no siblings, go back through the parents */
Michal Vasko9e685082021-01-29 14:49:09 +01006791 if (lyd_parent(elem) == start) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006792 /* we are done, no next element to process */
6793 break;
6794 }
6795 /* parent is already processed, go to its sibling */
Michal Vasko9e685082021-01-29 14:49:09 +01006796 elem = lyd_parent(elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006797 next = elem->next;
6798 }
6799 }
6800 }
6801
6802 /* make the temporary set the current one */
6803 ret_set.ctx_pos = set->ctx_pos;
6804 ret_set.ctx_size = set->ctx_size;
Michal Vaskod3678892020-05-21 10:06:58 +02006805 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006806 memcpy(set, &ret_set, sizeof *set);
Michal Vasko306e2832022-07-25 09:15:17 +02006807 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006808
6809 return LY_SUCCESS;
6810}
6811
6812/**
Michal Vasko49fec8e2022-05-24 10:28:33 +02006813 * @brief Move context @p set to a child schema node and all its descendants starting from a node.
6814 * Result is LYXP_SET_NODE_SET.
6815 *
6816 * @param[in] set Set to use.
6817 * @param[in] start Start node whose subtree to add.
6818 * @param[in] start_idx Index of @p start in @p set.
6819 * @param[in] moveto_mod Matching node module, NULL for no prefix.
6820 * @param[in] ncname Matching node name in the dictionary, NULL for any.
6821 * @param[in] options XPath options.
6822 * @return LY_ERR value.
6823 */
6824static LY_ERR
6825moveto_scnode_dfs(struct lyxp_set *set, const struct lysc_node *start, uint32_t start_idx,
6826 const struct lys_module *moveto_mod, const char *ncname, uint32_t options)
6827{
6828 const struct lysc_node *next, *elem;
6829 uint32_t idx;
6830 LY_ERR rc;
6831
6832 /* TREE DFS */
6833 for (elem = next = start; elem; elem = next) {
6834 if ((elem == start) || (elem->nodetype & (LYS_CHOICE | LYS_CASE))) {
6835 /* schema-only nodes, skip root */
6836 goto next_iter;
6837 }
6838
6839 rc = moveto_scnode_check(elem, start, set, ncname, moveto_mod);
6840 if (!rc) {
6841 if (lyxp_set_scnode_contains(set, elem, LYXP_NODE_ELEM, start_idx, &idx)) {
6842 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
6843 if (idx > start_idx) {
6844 /* we will process it later in the set */
6845 goto skip_children;
6846 }
6847 } else {
Michal Vasko7333cb32022-07-29 16:30:29 +02006848 LY_CHECK_RET(set_scnode_insert_node(set, elem, LYXP_NODE_ELEM, LYXP_AXIS_DESCENDANT, NULL));
Michal Vasko49fec8e2022-05-24 10:28:33 +02006849 }
6850 } else if (rc == LY_EINVAL) {
6851 goto skip_children;
6852 }
6853
6854next_iter:
6855 /* TREE DFS NEXT ELEM */
6856 /* select element for the next run - children first */
6857 next = lysc_node_child(elem);
6858 if (next && (next->nodetype == LYS_INPUT) && (options & LYXP_SCNODE_OUTPUT)) {
6859 next = next->next;
6860 } else if (next && (next->nodetype == LYS_OUTPUT) && !(options & LYXP_SCNODE_OUTPUT)) {
6861 next = next->next;
6862 }
6863 if (!next) {
6864skip_children:
6865 /* no children, so try siblings, but only if it's not the start,
6866 * that is considered to be the root and it's siblings are not traversed */
6867 if (elem != start) {
6868 next = elem->next;
6869 } else {
6870 break;
6871 }
6872 }
6873 while (!next) {
6874 /* no siblings, go back through the parents */
6875 if (elem->parent == start) {
6876 /* we are done, no next element to process */
6877 break;
6878 }
6879 /* parent is already processed, go to its sibling */
6880 elem = elem->parent;
6881 next = elem->next;
6882 }
6883 }
6884
6885 return LY_SUCCESS;
6886}
6887
6888/**
6889 * @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 +02006890 *
6891 * @param[in] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006892 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02006893 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006894 * @param[in] options XPath options.
Michal Vasko49fec8e2022-05-24 10:28:33 +02006895 * @return LY_ERR value.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006896 */
6897static LY_ERR
Michal Vasko49fec8e2022-05-24 10:28:33 +02006898moveto_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 +02006899{
Michal Vasko49fec8e2022-05-24 10:28:33 +02006900 uint32_t i, orig_used, mod_idx;
6901 const struct lys_module *mod;
6902 const struct lysc_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006903
aPiecek8b0cc152021-05-31 16:40:31 +02006904 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006905 return LY_SUCCESS;
6906 }
6907
6908 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006909 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006910 return LY_EVALID;
6911 }
6912
Michal Vasko03ff5a72019-09-11 13:49:33 +02006913 orig_used = set->used;
6914 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006915 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
6916 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006917 continue;
6918 }
6919
6920 /* remember context node */
Radek Krejcif13b87b2020-12-01 22:02:17 +01006921 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vaskoec4df482019-12-16 10:02:18 +01006922 } else {
Michal Vasko1a09b212021-05-06 13:00:10 +02006923 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006924 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006925
Michal Vasko49fec8e2022-05-24 10:28:33 +02006926 if ((set->val.scnodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.scnodes[i].type == LYXP_NODE_ROOT)) {
6927 /* traverse all top-level nodes in all the modules */
6928 mod_idx = 0;
6929 while ((mod = ly_ctx_get_module_iter(set->ctx, &mod_idx))) {
6930 /* module may not be implemented or not compiled yet */
6931 if (!mod->compiled) {
6932 continue;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006933 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006934
Michal Vasko49fec8e2022-05-24 10:28:33 +02006935 root = NULL;
6936 /* no getnext opts needed */
6937 while ((root = lys_getnext(root, NULL, mod->compiled, 0))) {
6938 LY_CHECK_RET(moveto_scnode_dfs(set, root, i, moveto_mod, ncname, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006939 }
6940 }
Michal Vasko49fec8e2022-05-24 10:28:33 +02006941
6942 } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
6943 /* add all the descendants recursively */
6944 LY_CHECK_RET(moveto_scnode_dfs(set, set->val.scnodes[i].scnode, i, moveto_mod, ncname, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006945 }
6946 }
6947
6948 return LY_SUCCESS;
6949}
6950
6951/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02006952 * @brief Move context @p set to an attribute. Result is LYXP_SET_NODE_SET.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006953 * Indirectly context position aware.
6954 *
6955 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02006956 * @param[in] mod Matching metadata module, NULL for any.
6957 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
aPiecek8b0cc152021-05-31 16:40:31 +02006958 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006959 * @return LY_ERR
6960 */
6961static LY_ERR
aPiecek8b0cc152021-05-31 16:40:31 +02006962moveto_attr(struct lyxp_set *set, const struct lys_module *mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006963{
Michal Vasko9f96a052020-03-10 09:41:45 +01006964 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006965
aPiecek8b0cc152021-05-31 16:40:31 +02006966 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006967 return LY_SUCCESS;
6968 }
6969
6970 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006971 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006972 return LY_EVALID;
6973 }
6974
Radek Krejci1deb5be2020-08-26 16:43:36 +02006975 for (uint32_t i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02006976 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006977
6978 /* only attributes of an elem (not dummy) can be in the result, skip all the rest;
6979 * our attributes are always qualified */
Michal Vasko5c4e5892019-11-14 12:31:38 +01006980 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01006981 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006982
6983 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02006984 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006985 continue;
6986 }
6987
Michal Vaskod3678892020-05-21 10:06:58 +02006988 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006989 /* match */
6990 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01006991 set->val.meta[i].meta = sub;
6992 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006993 /* pos does not change */
6994 replaced = 1;
6995 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01006996 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 +02006997 }
6998 ++i;
6999 }
7000 }
7001 }
7002
7003 if (!replaced) {
7004 /* no match */
7005 set_remove_node(set, i);
7006 }
7007 }
7008
7009 return LY_SUCCESS;
7010}
7011
7012/**
7013 * @brief Move context @p set1 to union with @p set2. @p set2 is emptied afterwards.
Michal Vasko61ac2f62020-05-25 12:39:51 +02007014 * Result is LYXP_SET_NODE_SET. Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007015 *
7016 * @param[in,out] set1 Set to use for the result.
7017 * @param[in] set2 Set that is copied to @p set1.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007018 * @return LY_ERR
7019 */
7020static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007021moveto_union(struct lyxp_set *set1, struct lyxp_set *set2)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007022{
7023 LY_ERR rc;
7024
Michal Vaskod3678892020-05-21 10:06:58 +02007025 if ((set1->type != LYXP_SET_NODE_SET) || (set2->type != LYXP_SET_NODE_SET)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01007026 LOGVAL(set1->ctx, LY_VCODE_XP_INOP_2, "union", print_set_type(set1), print_set_type(set2));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007027 return LY_EVALID;
7028 }
7029
7030 /* set2 is empty or both set1 and set2 */
Michal Vaskod3678892020-05-21 10:06:58 +02007031 if (!set2->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007032 return LY_SUCCESS;
7033 }
7034
Michal Vaskod3678892020-05-21 10:06:58 +02007035 if (!set1->used) {
aPiecekadc1e4f2021-10-07 11:15:12 +02007036 /* release hidden allocated data (lyxp_set.size) */
7037 lyxp_set_free_content(set1);
7038 /* direct copying of the entire structure */
Michal Vasko03ff5a72019-09-11 13:49:33 +02007039 memcpy(set1, set2, sizeof *set1);
7040 /* dynamic memory belongs to set1 now, do not free */
Michal Vaskod3678892020-05-21 10:06:58 +02007041 memset(set2, 0, sizeof *set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007042 return LY_SUCCESS;
7043 }
7044
7045 /* we assume sets are sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007046 assert(!set_sort(set1) && !set_sort(set2));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007047
7048 /* sort, remove duplicates */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007049 rc = set_sorted_merge(set1, set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007050 LY_CHECK_RET(rc);
7051
7052 /* final set must be sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007053 assert(!set_sort(set1));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007054
7055 return LY_SUCCESS;
7056}
7057
7058/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02007059 * @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 +02007060 * Context position aware.
7061 *
7062 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02007063 * @param[in] mod Matching metadata module, NULL for any.
7064 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
Michal Vaskocdad7122020-11-09 21:04:44 +01007065 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007066 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7067 */
7068static int
Michal Vaskocdad7122020-11-09 21:04:44 +01007069moveto_attr_alldesc(struct lyxp_set *set, const struct lys_module *mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007070{
Michal Vasko9f96a052020-03-10 09:41:45 +01007071 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007072 struct lyxp_set *set_all_desc = NULL;
7073 LY_ERR rc;
7074
aPiecek8b0cc152021-05-31 16:40:31 +02007075 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007076 return LY_SUCCESS;
7077 }
7078
7079 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01007080 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007081 return LY_EVALID;
7082 }
7083
Michal Vasko03ff5a72019-09-11 13:49:33 +02007084 /* can be optimized similarly to moveto_node_alldesc() and save considerable amount of memory,
7085 * but it likely won't be used much, so it's a waste of time */
7086 /* copy the context */
7087 set_all_desc = set_copy(set);
7088 /* get all descendant nodes (the original context nodes are removed) */
Michal Vasko49fec8e2022-05-24 10:28:33 +02007089 rc = moveto_node_alldesc_child(set_all_desc, NULL, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007090 if (rc != LY_SUCCESS) {
7091 lyxp_set_free(set_all_desc);
7092 return rc;
7093 }
7094 /* prepend the original context nodes */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007095 rc = moveto_union(set, set_all_desc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007096 if (rc != LY_SUCCESS) {
7097 lyxp_set_free(set_all_desc);
7098 return rc;
7099 }
7100 lyxp_set_free(set_all_desc);
7101
Radek Krejci1deb5be2020-08-26 16:43:36 +02007102 for (uint32_t i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02007103 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007104
7105 /* only attributes of an elem can be in the result, skip all the rest,
7106 * we have all attributes qualified in lyd tree */
7107 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01007108 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007109 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02007110 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007111 continue;
7112 }
7113
Michal Vaskod3678892020-05-21 10:06:58 +02007114 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007115 /* match */
7116 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01007117 set->val.meta[i].meta = sub;
7118 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007119 /* pos does not change */
7120 replaced = 1;
7121 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01007122 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 +02007123 }
7124 ++i;
7125 }
7126 }
7127 }
7128
7129 if (!replaced) {
7130 /* no match */
7131 set_remove_node(set, i);
7132 }
7133 }
7134
7135 return LY_SUCCESS;
7136}
7137
7138/**
Michal Vasko8abcecc2022-07-28 09:55:01 +02007139 * @brief Move context @p set1 single item to the result of a comparison.
7140 *
7141 * @param[in] set1 First set with the item to compare.
7142 * @param[in] idx1 Index of the item in @p set1.
7143 * @param[in] set2 Second set.
7144 * @param[in] op Comparison operator to process.
7145 * @param[in] switch_operands Whether to switch sets as operands; whether it is `set1 op set2` or `set2 op set1`.
7146 * @param[out] result Result of the comparison.
7147 * @return LY_ERR value.
7148 */
7149static LY_ERR
7150moveto_op_comp_item(const struct lyxp_set *set1, uint32_t idx1, struct lyxp_set *set2, const char *op,
7151 ly_bool switch_operands, ly_bool *result)
7152{
7153 struct lyxp_set tmp1 = {0};
7154 LY_ERR rc = LY_SUCCESS;
7155
7156 assert(set1->type == LYXP_SET_NODE_SET);
7157
7158 /* cast set1 */
7159 switch (set2->type) {
7160 case LYXP_SET_NUMBER:
7161 rc = set_comp_cast(&tmp1, set1, LYXP_SET_NUMBER, idx1);
7162 break;
7163 case LYXP_SET_BOOLEAN:
7164 rc = set_comp_cast(&tmp1, set1, LYXP_SET_BOOLEAN, idx1);
7165 break;
7166 default:
7167 rc = set_comp_cast(&tmp1, set1, LYXP_SET_STRING, idx1);
7168 break;
7169 }
7170 LY_CHECK_GOTO(rc, cleanup);
7171
7172 /* canonize set2 */
7173 LY_CHECK_GOTO(rc = set_comp_canonize(set2, &set1->val.nodes[idx1]), cleanup);
7174
7175 /* compare recursively and store the result */
7176 if (switch_operands) {
7177 LY_CHECK_GOTO(rc = moveto_op_comp(set2, &tmp1, op), cleanup);
7178 *result = set2->val.bln;
7179 } else {
7180 LY_CHECK_GOTO(rc = moveto_op_comp(&tmp1, set2, op), cleanup);
7181 *result = tmp1.val.bln;
7182 }
7183
7184cleanup:
7185 lyxp_set_free_content(&tmp1);
7186 return rc;
7187}
7188
7189/**
7190 * @brief Move context @p set1 to the result of a comparison. Handles '=', '!=', '<=', '<', '>=', or '>'.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007191 * Result is LYXP_SET_BOOLEAN. Indirectly context position aware.
7192 *
7193 * @param[in,out] set1 Set to use for the result.
7194 * @param[in] set2 Set acting as the second operand for @p op.
7195 * @param[in] op Comparison operator to process.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007196 * @return LY_ERR
7197 */
7198static LY_ERR
Michal Vasko8abcecc2022-07-28 09:55:01 +02007199moveto_op_comp(struct lyxp_set *set1, struct lyxp_set *set2, const char *op)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007200{
7201 /*
7202 * NODE SET + NODE SET = NODE SET + STRING /(1 NODE SET) 2 STRING
7203 * NODE SET + STRING = STRING + STRING /1 STRING (2 STRING)
7204 * NODE SET + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
7205 * NODE SET + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
7206 * STRING + NODE SET = STRING + STRING /(1 STRING) 2 STRING
7207 * NUMBER + NODE SET = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
7208 * BOOLEAN + NODE SET = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
7209 *
7210 * '=' or '!='
7211 * BOOLEAN + BOOLEAN
7212 * BOOLEAN + STRING = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
7213 * BOOLEAN + NUMBER = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
7214 * STRING + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
7215 * NUMBER + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
7216 * NUMBER + NUMBER
7217 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
7218 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
7219 * STRING + STRING
7220 *
7221 * '<=', '<', '>=', '>'
7222 * NUMBER + NUMBER
7223 * BOOLEAN + BOOLEAN = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
7224 * BOOLEAN + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
7225 * BOOLEAN + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
7226 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
7227 * STRING + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
7228 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
7229 * NUMBER + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
7230 * STRING + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
7231 */
Michal Vasko8abcecc2022-07-28 09:55:01 +02007232 ly_bool result;
7233 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007234 LY_ERR rc;
7235
Michal Vasko03ff5a72019-09-11 13:49:33 +02007236 /* iterative evaluation with node-sets */
7237 if ((set1->type == LYXP_SET_NODE_SET) || (set2->type == LYXP_SET_NODE_SET)) {
7238 if (set1->type == LYXP_SET_NODE_SET) {
7239 for (i = 0; i < set1->used; ++i) {
Michal Vasko8abcecc2022-07-28 09:55:01 +02007240 /* evaluate for the single item */
7241 LY_CHECK_RET(moveto_op_comp_item(set1, i, set2, op, 0, &result));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007242
7243 /* lazy evaluation until true */
Michal Vasko8abcecc2022-07-28 09:55:01 +02007244 if (result) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007245 set_fill_boolean(set1, 1);
7246 return LY_SUCCESS;
7247 }
7248 }
7249 } else {
7250 for (i = 0; i < set2->used; ++i) {
Michal Vasko8abcecc2022-07-28 09:55:01 +02007251 /* evaluate for the single item */
7252 LY_CHECK_RET(moveto_op_comp_item(set2, i, set1, op, 1, &result));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007253
7254 /* lazy evaluation until true */
Michal Vasko8abcecc2022-07-28 09:55:01 +02007255 if (result) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007256 set_fill_boolean(set1, 1);
7257 return LY_SUCCESS;
7258 }
7259 }
7260 }
7261
Michal Vasko8abcecc2022-07-28 09:55:01 +02007262 /* false for all the nodes */
Michal Vasko03ff5a72019-09-11 13:49:33 +02007263 set_fill_boolean(set1, 0);
7264 return LY_SUCCESS;
7265 }
7266
7267 /* first convert properly */
7268 if ((op[0] == '=') || (op[0] == '!')) {
7269 if ((set1->type == LYXP_SET_BOOLEAN) || (set2->type == LYXP_SET_BOOLEAN)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007270 lyxp_set_cast(set1, LYXP_SET_BOOLEAN);
7271 lyxp_set_cast(set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007272 } else if ((set1->type == LYXP_SET_NUMBER) || (set2->type == LYXP_SET_NUMBER)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007273 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007274 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007275 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007276 LY_CHECK_RET(rc);
7277 } /* else we have 2 strings */
7278 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007279 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007280 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007281 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007282 LY_CHECK_RET(rc);
7283 }
7284
7285 assert(set1->type == set2->type);
7286
7287 /* compute result */
7288 if (op[0] == '=') {
7289 if (set1->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +02007290 result = (set1->val.bln == set2->val.bln);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007291 } else if (set1->type == LYXP_SET_NUMBER) {
7292 result = (set1->val.num == set2->val.num);
7293 } else {
7294 assert(set1->type == LYXP_SET_STRING);
Michal Vaskof60d1782022-08-02 11:00:32 +02007295 result = strcmp(set1->val.str, set2->val.str) ? 0 : 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007296 }
7297 } else if (op[0] == '!') {
7298 if (set1->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +02007299 result = (set1->val.bln != set2->val.bln);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007300 } else if (set1->type == LYXP_SET_NUMBER) {
7301 result = (set1->val.num != set2->val.num);
7302 } else {
7303 assert(set1->type == LYXP_SET_STRING);
Michal Vaskof60d1782022-08-02 11:00:32 +02007304 result = strcmp(set1->val.str, set2->val.str) ? 1 : 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007305 }
7306 } else {
7307 assert(set1->type == LYXP_SET_NUMBER);
7308 if (op[0] == '<') {
7309 if (op[1] == '=') {
7310 result = (set1->val.num <= set2->val.num);
7311 } else {
7312 result = (set1->val.num < set2->val.num);
7313 }
7314 } else {
7315 if (op[1] == '=') {
7316 result = (set1->val.num >= set2->val.num);
7317 } else {
7318 result = (set1->val.num > set2->val.num);
7319 }
7320 }
7321 }
7322
7323 /* assign result */
7324 if (result) {
7325 set_fill_boolean(set1, 1);
7326 } else {
7327 set_fill_boolean(set1, 0);
7328 }
7329
7330 return LY_SUCCESS;
7331}
7332
7333/**
7334 * @brief Move context @p set to the result of a basic operation. Handles '+', '-', unary '-', '*', 'div',
7335 * or 'mod'. Result is LYXP_SET_NUMBER. Indirectly context position aware.
7336 *
7337 * @param[in,out] set1 Set to use for the result.
7338 * @param[in] set2 Set acting as the second operand for @p op.
7339 * @param[in] op Operator to process.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007340 * @return LY_ERR
7341 */
7342static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007343moveto_op_math(struct lyxp_set *set1, struct lyxp_set *set2, const char *op)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007344{
7345 LY_ERR rc;
7346
7347 /* unary '-' */
7348 if (!set2 && (op[0] == '-')) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007349 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007350 LY_CHECK_RET(rc);
7351 set1->val.num *= -1;
7352 lyxp_set_free(set2);
7353 return LY_SUCCESS;
7354 }
7355
7356 assert(set1 && set2);
7357
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007358 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007359 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007360 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007361 LY_CHECK_RET(rc);
7362
7363 switch (op[0]) {
7364 /* '+' */
7365 case '+':
7366 set1->val.num += set2->val.num;
7367 break;
7368
7369 /* '-' */
7370 case '-':
7371 set1->val.num -= set2->val.num;
7372 break;
7373
7374 /* '*' */
7375 case '*':
7376 set1->val.num *= set2->val.num;
7377 break;
7378
7379 /* 'div' */
7380 case 'd':
7381 set1->val.num /= set2->val.num;
7382 break;
7383
7384 /* 'mod' */
7385 case 'm':
7386 set1->val.num = ((long long)set1->val.num) % ((long long)set2->val.num);
7387 break;
7388
7389 default:
7390 LOGINT_RET(set1->ctx);
7391 }
7392
7393 return LY_SUCCESS;
7394}
7395
Michal Vasko03ff5a72019-09-11 13:49:33 +02007396/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02007397 * @brief Evaluate Predicate. Logs directly on error.
7398 *
Michal Vaskod3678892020-05-21 10:06:58 +02007399 * [9] Predicate ::= '[' Expr ']'
Michal Vasko03ff5a72019-09-11 13:49:33 +02007400 *
7401 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007402 * @param[in] tok_idx Position in the expression @p exp.
aPiecek8b0cc152021-05-31 16:40:31 +02007403 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007404 * @param[in] options XPath options.
Michal Vasko49fec8e2022-05-24 10:28:33 +02007405 * @param[in] axis Axis to search on.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007406 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7407 */
7408static LY_ERR
Michal Vasko49fec8e2022-05-24 10:28:33 +02007409eval_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 +02007410{
7411 LY_ERR rc;
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01007412 uint16_t orig_exp;
7413 uint32_t i, orig_pos, orig_size;
Michal Vasko5c4e5892019-11-14 12:31:38 +01007414 int32_t pred_in_ctx;
Michal Vasko88a9e802022-05-24 10:50:28 +02007415 ly_bool reverse_axis = 0;
aPiecekfff4dca2021-10-07 10:59:53 +02007416 struct lyxp_set set2 = {0};
Michal Vasko03ff5a72019-09-11 13:49:33 +02007417
7418 /* '[' */
aPiecek8b0cc152021-05-31 16:40:31 +02007419 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02007420 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007421 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007422
aPiecek8b0cc152021-05-31 16:40:31 +02007423 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007424only_parse:
aPiecek8b0cc152021-05-31 16:40:31 +02007425 rc = eval_expr_select(exp, tok_idx, 0, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007426 LY_CHECK_RET(rc);
7427 } else if (set->type == LYXP_SET_NODE_SET) {
7428 /* 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 +01007429 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007430
7431 /* empty set, nothing to evaluate */
7432 if (!set->used) {
7433 goto only_parse;
7434 }
7435
Michal Vasko49fec8e2022-05-24 10:28:33 +02007436 /* decide forward or reverse axis */
7437 switch (axis) {
7438 case LYXP_AXIS_ANCESTOR:
7439 case LYXP_AXIS_ANCESTOR_OR_SELF:
7440 case LYXP_AXIS_PRECEDING:
7441 case LYXP_AXIS_PRECEDING_SIBLING:
7442 reverse_axis = 1;
7443 break;
7444 case LYXP_AXIS_DESCENDANT:
7445 case LYXP_AXIS_DESCENDANT_OR_SELF:
7446 case LYXP_AXIS_FOLLOWING:
7447 case LYXP_AXIS_FOLLOWING_SIBLING:
7448 case LYXP_AXIS_PARENT:
7449 case LYXP_AXIS_CHILD:
7450 case LYXP_AXIS_SELF:
7451 case LYXP_AXIS_ATTRIBUTE:
7452 reverse_axis = 0;
7453 break;
7454 }
7455
Michal Vasko004d3152020-06-11 19:59:22 +02007456 orig_exp = *tok_idx;
Michal Vasko49fec8e2022-05-24 10:28:33 +02007457 orig_pos = reverse_axis ? set->used + 1 : 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007458 orig_size = set->used;
Michal Vasko39dbf352020-05-21 10:08:59 +02007459 for (i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007460 set_init(&set2, set);
7461 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 +02007462
7463 /* remember the node context position for position() and context size for last() */
7464 orig_pos += reverse_axis ? -1 : 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007465
7466 set2.ctx_pos = orig_pos;
7467 set2.ctx_size = orig_size;
Michal Vasko004d3152020-06-11 19:59:22 +02007468 *tok_idx = orig_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007469
Michal Vasko004d3152020-06-11 19:59:22 +02007470 rc = eval_expr_select(exp, tok_idx, 0, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007471 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02007472 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007473 return rc;
7474 }
7475
Michal Vasko49fec8e2022-05-24 10:28:33 +02007476 /* number is a proximity position */
Michal Vasko03ff5a72019-09-11 13:49:33 +02007477 if (set2.type == LYXP_SET_NUMBER) {
7478 if ((long long)set2.val.num == orig_pos) {
7479 set2.val.num = 1;
7480 } else {
7481 set2.val.num = 0;
7482 }
7483 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007484 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007485
7486 /* predicate satisfied or not? */
Michal Vasko004d3152020-06-11 19:59:22 +02007487 if (!set2.val.bln) {
Michal Vasko2caefc12019-11-14 16:07:56 +01007488 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007489 }
7490 }
Michal Vasko2caefc12019-11-14 16:07:56 +01007491 set_remove_nodes_none(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007492
7493 } else if (set->type == LYXP_SET_SCNODE_SET) {
7494 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01007495 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007496 /* there is a currently-valid node */
7497 break;
7498 }
7499 }
7500 /* empty set, nothing to evaluate */
7501 if (i == set->used) {
7502 goto only_parse;
7503 }
7504
Michal Vasko004d3152020-06-11 19:59:22 +02007505 orig_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007506
Michal Vasko03ff5a72019-09-11 13:49:33 +02007507 /* set special in_ctx to all the valid snodes */
7508 pred_in_ctx = set_scnode_new_in_ctx(set);
7509
7510 /* use the valid snodes one-by-one */
7511 for (i = 0; i < set->used; ++i) {
7512 if (set->val.scnodes[i].in_ctx != pred_in_ctx) {
7513 continue;
7514 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01007515 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007516
Michal Vasko004d3152020-06-11 19:59:22 +02007517 *tok_idx = orig_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007518
Michal Vasko004d3152020-06-11 19:59:22 +02007519 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007520 LY_CHECK_RET(rc);
7521
7522 set->val.scnodes[i].in_ctx = pred_in_ctx;
7523 }
7524
7525 /* restore the state as it was before the predicate */
7526 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01007527 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007528 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007529 } else if (set->val.scnodes[i].in_ctx == pred_in_ctx) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01007530 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007531 }
7532 }
7533
7534 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02007535 set2.type = LYXP_SET_NODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007536 set_fill_set(&set2, set);
7537
Michal Vasko004d3152020-06-11 19:59:22 +02007538 rc = eval_expr_select(exp, tok_idx, 0, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007539 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02007540 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007541 return rc;
7542 }
7543
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007544 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +02007545 if (!set2.val.bln) {
Michal Vaskod3678892020-05-21 10:06:58 +02007546 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007547 }
Michal Vaskod3678892020-05-21 10:06:58 +02007548 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007549 }
7550
7551 /* ']' */
Michal Vasko004d3152020-06-11 19:59:22 +02007552 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_BRACK2);
aPiecek8b0cc152021-05-31 16:40:31 +02007553 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02007554 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007555 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007556
7557 return LY_SUCCESS;
7558}
7559
7560/**
Michal Vaskod3678892020-05-21 10:06:58 +02007561 * @brief Evaluate Literal. Logs directly on error.
7562 *
7563 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007564 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02007565 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7566 */
7567static void
Michal Vasko40308e72020-10-20 16:38:40 +02007568eval_literal(const struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set)
Michal Vaskod3678892020-05-21 10:06:58 +02007569{
7570 if (set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007571 if (exp->tok_len[*tok_idx] == 2) {
Michal Vaskod3678892020-05-21 10:06:58 +02007572 set_fill_string(set, "", 0);
7573 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007574 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 +02007575 }
7576 }
7577 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02007578 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007579 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007580}
7581
7582/**
Michal Vasko3d969ff2022-07-29 15:02:08 +02007583 * @brief Check that a nametest in a predicate matches a key node.
7584 *
7585 * @param[in] nametest Nametest to check.
7586 * @param[in] len Length of @p nametest.
7587 * @param[in] ctx_scnode Found schema node as the context for the predicate.
7588 * @param[in] set Context set.
7589 * @param[in] key Expected key node.
7590 * @return LY_SUCCESS on success,
7591 * @return LY_ENOT if a predicate could not be compiled.
7592 * @return LY_ERR on any error.
7593 */
7594static LY_ERR
7595eval_name_test_try_compile_predicate_key(const char *nametest, uint32_t len, const struct lysc_node *ctx_scnode,
7596 const struct lyxp_set *set, const struct lysc_node *key)
7597{
7598 const struct lys_module *mod;
7599
7600 /* prefix (module) */
7601 LY_CHECK_RET(moveto_resolve_model(&nametest, &len, set, ctx_scnode, &mod));
7602 if (mod != key->module) {
7603 return LY_ENOT;
7604 }
7605
7606 /* node name */
7607 if (ly_strncmp(key->name, nametest, len)) {
7608 return LY_ENOT;
7609 }
7610
7611 return LY_SUCCESS;
7612}
7613
7614/**
7615 * @brief Append a simple predicate for the node.
7616 *
7617 * @param[in] exp Full parsed XPath expression.
7618 * @param[in] tok_idx Predicate start index in @p exp.
7619 * @param[in] end_tok_idx Predicate end index in @p exp.
7620 * @param[in] ctx_scnode Found schema node as the context for the predicate.
7621 * @param[in] set Context set.
7622 * @param[in] pred_node Node with the value referenced in the predicate.
7623 * @param[in,out] pred Predicate to append to.
7624 * @param[in,out] pred_len Length of @p pred, is updated.
7625 * @return LY_SUCCESS on success,
7626 * @return LY_ENOT if a predicate could not be compiled.
7627 * @return LY_ERR on any error.
7628 */
7629static LY_ERR
7630eval_name_test_try_compile_predicate_append(const struct lyxp_expr *exp, uint16_t tok_idx, uint16_t end_tok_idx,
7631 const struct lysc_node *ctx_scnode, const struct lyxp_set *set, const struct lysc_node *pred_node, char **pred,
7632 uint32_t *pred_len)
7633{
7634 LY_ERR rc = LY_SUCCESS;
7635 uint32_t i;
7636 const struct lyd_node *siblings;
7637 struct lyd_node *ctx_node;
Michal Vasko5fb92a22022-08-02 09:21:37 +02007638 const struct lysc_node *sparent, *cur_scnode;
Michal Vasko3d969ff2022-07-29 15:02:08 +02007639 struct lyxp_expr *val_exp = NULL;
7640 struct lyxp_set set2 = {0};
7641 char quot;
7642
7643 /* duplicate the value expression */
7644 LY_CHECK_GOTO(rc = lyxp_expr_dup(set->ctx, exp, tok_idx, end_tok_idx, &val_exp), cleanup);
7645
7646 /* get its atoms */
Michal Vasko5fb92a22022-08-02 09:21:37 +02007647 cur_scnode = set->cur_node ? set->cur_node->schema : NULL;
7648 LY_CHECK_GOTO(rc = lyxp_atomize(set->ctx, val_exp, set->cur_mod, set->format, set->prefix_data, cur_scnode,
7649 ctx_scnode, &set2, LYXP_SCNODE), cleanup);
Michal Vasko3d969ff2022-07-29 15:02:08 +02007650
7651 /* check whether we can compile a single predicate (evaluation result value is always the same) */
7652 for (i = 0; i < set2.used; ++i) {
7653 if ((set2.val.scnodes[i].type != LYXP_NODE_ELEM) || (set2.val.scnodes[i].in_ctx < LYXP_SET_SCNODE_ATOM_NODE)) {
7654 /* skip root and context node */
7655 continue;
7656 }
7657
Michal Vasko5fb92a22022-08-02 09:21:37 +02007658 /* 1) context node descendants are traversed - do best-effort detection of the value dependency on the
7659 * context node instance */
7660 if ((set2.val.scnodes[i].axis == LYXP_AXIS_CHILD) && (set2.val.scnodes[i].scnode->parent == ctx_scnode)) {
7661 /* 1.1) context node child was accessed on the child axis, certain dependency */
Michal Vasko3d969ff2022-07-29 15:02:08 +02007662 rc = LY_ENOT;
7663 goto cleanup;
7664 }
Michal Vasko5fb92a22022-08-02 09:21:37 +02007665 if ((set2.val.scnodes[i].axis == LYXP_AXIS_DESCENDANT) || (set2.val.scnodes[i].axis == LYXP_AXIS_DESCENDANT_OR_SELF)) {
7666 for (sparent = set2.val.scnodes[i].scnode->parent; sparent && (sparent != ctx_scnode); sparent = sparent->parent) {}
7667 if (sparent) {
7668 /* 1.2) context node descendant was accessed on the descendant axis, probable dependency */
7669 rc = LY_ENOT;
7670 goto cleanup;
7671 }
7672 }
Michal Vasko3d969ff2022-07-29 15:02:08 +02007673
Michal Vasko5fb92a22022-08-02 09:21:37 +02007674 /* 2) multi-instance nodes (list or leaf-list) are traversed - all the instances need to be considered,
7675 * but the current node can be safely ignored, it is always the same data instance */
7676 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 +02007677 rc = LY_ENOT;
7678 goto cleanup;
7679 }
Michal Vasko3d969ff2022-07-29 15:02:08 +02007680 }
7681
7682 /* get any data instance of the context node, we checked it makes no difference */
7683 siblings = set->val.nodes[0].node ? lyd_child(set->val.nodes[0].node) : set->tree;
7684 LY_CHECK_GOTO(rc = lyd_find_sibling_schema(siblings, ctx_scnode, &ctx_node), cleanup);
7685
7686 /* evaluate the value subexpression with the root context node */
7687 lyxp_set_free_content(&set2);
7688 LY_CHECK_GOTO(rc = lyxp_eval(set->ctx, val_exp, set->cur_mod, set->format, set->prefix_data, set->cur_node,
7689 ctx_node, set->tree, NULL, &set2, 0), cleanup);
7690
7691 /* cast it into a string */
7692 LY_CHECK_GOTO(rc = lyxp_set_cast(&set2, LYXP_SET_STRING), cleanup);
7693
7694 /* append the JSON predicate */
7695 *pred = ly_realloc(*pred, *pred_len + 1 + strlen(pred_node->name) + 2 + strlen(set2.val.str) + 3);
7696 LY_CHECK_ERR_GOTO(!*pred, LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
7697 quot = strchr(set2.val.str, '\'') ? '\"' : '\'';
7698 *pred_len += sprintf(*pred + *pred_len, "[%s=%c%s%c]", pred_node->name, quot, set2.val.str, quot);
7699
7700cleanup:
7701 lyxp_expr_free(set->ctx, val_exp);
7702 lyxp_set_free_content(&set2);
7703 return rc;
7704}
7705
7706/**
Michal Vasko004d3152020-06-11 19:59:22 +02007707 * @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 +02007708 *
Michal Vasko004d3152020-06-11 19:59:22 +02007709 * @param[in] exp Full parsed XPath expression.
7710 * @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 +02007711 * @param[in] ctx_scnode Found schema node as the context for the predicate.
7712 * @param[in] set Context set.
Michal Vasko004d3152020-06-11 19:59:22 +02007713 * @param[out] predicates Parsed predicates.
7714 * @param[out] pred_type Type of @p predicates.
7715 * @return LY_SUCCESS on success,
Michal Vasko3d969ff2022-07-29 15:02:08 +02007716 * @return LY_ENOT if a predicate could not be compiled.
Michal Vasko004d3152020-06-11 19:59:22 +02007717 * @return LY_ERR on any error.
Michal Vaskod3678892020-05-21 10:06:58 +02007718 */
7719static LY_ERR
Michal Vasko3d969ff2022-07-29 15:02:08 +02007720eval_name_test_try_compile_predicates(const struct lyxp_expr *exp, uint16_t *tok_idx, const struct lysc_node *ctx_scnode,
7721 const struct lyxp_set *set, struct ly_path_predicate **predicates, enum ly_path_pred_type *pred_type)
Michal Vaskod3678892020-05-21 10:06:58 +02007722{
Michal Vasko3d969ff2022-07-29 15:02:08 +02007723 LY_ERR rc = LY_SUCCESS;
7724 uint16_t e_idx, val_start_idx, pred_idx = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02007725 const struct lysc_node *key;
Michal Vasko3d969ff2022-07-29 15:02:08 +02007726 uint32_t prev_lo, pred_len = 0;
7727 char *pred = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02007728 struct lyxp_expr *exp2 = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02007729
Michal Vasko3d969ff2022-07-29 15:02:08 +02007730 assert(ctx_scnode->nodetype & (LYS_LIST | LYS_LEAFLIST));
Michal Vaskod3678892020-05-21 10:06:58 +02007731
Michal Vasko3d969ff2022-07-29 15:02:08 +02007732 /* turn logging off */
7733 prev_lo = ly_log_options(0);
7734
7735 if (ctx_scnode->nodetype == LYS_LIST) {
7736 /* check for predicates "[key1=...][key2=...]..." */
7737
Michal Vasko004d3152020-06-11 19:59:22 +02007738 /* get key count */
Michal Vasko3d969ff2022-07-29 15:02:08 +02007739 if (ctx_scnode->flags & LYS_KEYLESS) {
7740 rc = LY_ENOT;
7741 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +02007742 }
Michal Vaskod3678892020-05-21 10:06:58 +02007743
Michal Vasko004d3152020-06-11 19:59:22 +02007744 /* learn where the predicates end */
7745 e_idx = *tok_idx;
Michal Vasko3d969ff2022-07-29 15:02:08 +02007746 for (key = lysc_node_child(ctx_scnode); key && (key->flags & LYS_KEY); key = key->next) {
Michal Vasko004d3152020-06-11 19:59:22 +02007747 /* '[' */
7748 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
Michal Vasko3d969ff2022-07-29 15:02:08 +02007749 rc = LY_ENOT;
7750 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +02007751 }
7752 ++e_idx;
7753
Michal Vasko3354d272021-04-06 09:40:06 +02007754 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_NAMETEST)) {
Michal Vasko3d969ff2022-07-29 15:02:08 +02007755 /* not a key */
7756 rc = LY_ENOT;
7757 goto cleanup;
Michal Vasko3354d272021-04-06 09:40:06 +02007758 }
7759
Michal Vasko3d969ff2022-07-29 15:02:08 +02007760 /* check key */
7761 LY_CHECK_GOTO(rc = eval_name_test_try_compile_predicate_key(exp->expr + exp->tok_pos[e_idx],
7762 exp->tok_len[e_idx], ctx_scnode, set, key), cleanup);
7763
7764 ++e_idx;
7765
7766 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_OPER_EQUAL)) {
7767 /* not '=' */
7768 rc = LY_ENOT;
7769 goto cleanup;
7770 }
7771 ++e_idx;
7772
7773 /* value start */
7774 val_start_idx = e_idx;
7775
Michal Vasko004d3152020-06-11 19:59:22 +02007776 /* ']' */
7777 while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) {
Michal Vaskob8ee3562022-08-02 10:43:17 +02007778 if (!lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_OPER_LOG)) {
7779 /* higher priority than '=' */
7780 rc = LY_ENOT;
7781 goto cleanup;
7782 }
Michal Vasko004d3152020-06-11 19:59:22 +02007783 ++e_idx;
7784 }
Michal Vasko004d3152020-06-11 19:59:22 +02007785
Michal Vasko3d969ff2022-07-29 15:02:08 +02007786 /* try to evaluate the value */
7787 LY_CHECK_GOTO(rc = eval_name_test_try_compile_predicate_append(exp, val_start_idx, e_idx - 1, ctx_scnode,
7788 set, key, &pred, &pred_len), cleanup);
7789
7790 ++e_idx;
Michal Vasko004d3152020-06-11 19:59:22 +02007791 }
Michal Vasko004d3152020-06-11 19:59:22 +02007792 } else {
Michal Vasko3d969ff2022-07-29 15:02:08 +02007793 /* check for predicate "[.=...]" */
7794
Michal Vasko004d3152020-06-11 19:59:22 +02007795 /* learn just where this single predicate ends */
7796 e_idx = *tok_idx;
7797
Michal Vaskod3678892020-05-21 10:06:58 +02007798 /* '[' */
Michal Vasko004d3152020-06-11 19:59:22 +02007799 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
Michal Vasko3d969ff2022-07-29 15:02:08 +02007800 rc = LY_ENOT;
7801 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +02007802 }
7803 ++e_idx;
Michal Vaskod3678892020-05-21 10:06:58 +02007804
Michal Vasko3354d272021-04-06 09:40:06 +02007805 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_DOT)) {
Michal Vasko3d969ff2022-07-29 15:02:08 +02007806 /* not the node value */
7807 rc = LY_ENOT;
7808 goto cleanup;
Michal Vasko3354d272021-04-06 09:40:06 +02007809 }
Michal Vasko3d969ff2022-07-29 15:02:08 +02007810 ++e_idx;
7811
7812 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_OPER_EQUAL)) {
7813 /* not '=' */
7814 rc = LY_ENOT;
7815 goto cleanup;
7816 }
7817 ++e_idx;
7818
7819 /* value start */
7820 val_start_idx = e_idx;
Michal Vasko3354d272021-04-06 09:40:06 +02007821
Michal Vaskod3678892020-05-21 10:06:58 +02007822 /* ']' */
Michal Vasko004d3152020-06-11 19:59:22 +02007823 while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) {
Michal Vaskob8ee3562022-08-02 10:43:17 +02007824 if (!lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_OPER_LOG)) {
7825 /* higher priority than '=' */
7826 rc = LY_ENOT;
7827 goto cleanup;
7828 }
Michal Vasko004d3152020-06-11 19:59:22 +02007829 ++e_idx;
7830 }
Michal Vasko3d969ff2022-07-29 15:02:08 +02007831
7832 /* try to evaluate the value */
7833 LY_CHECK_GOTO(rc = eval_name_test_try_compile_predicate_append(exp, val_start_idx, e_idx - 1, ctx_scnode, set,
7834 ctx_scnode, &pred, &pred_len), cleanup);
7835
Michal Vasko004d3152020-06-11 19:59:22 +02007836 ++e_idx;
Michal Vaskod3678892020-05-21 10:06:58 +02007837 }
7838
Michal Vasko004d3152020-06-11 19:59:22 +02007839 /* parse the predicate(s) */
Michal Vasko3d969ff2022-07-29 15:02:08 +02007840 LY_CHECK_GOTO(rc = ly_path_parse_predicate(set->ctx, ctx_scnode, pred, pred_len, LY_PATH_PREFIX_OPTIONAL,
7841 LY_PATH_PRED_SIMPLE, &exp2), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02007842
7843 /* compile */
Michal Vasko3d969ff2022-07-29 15:02:08 +02007844 rc = ly_path_compile_predicate(set->ctx, set->cur_node ? set->cur_node->schema : NULL, set->cur_mod, ctx_scnode, exp2,
7845 &pred_idx, LY_VALUE_JSON, NULL, predicates, pred_type);
7846 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02007847
7848 /* success, the predicate must include all the needed information for hash-based search */
7849 *tok_idx = e_idx;
7850
7851cleanup:
7852 ly_log_options(prev_lo);
Michal Vasko3d969ff2022-07-29 15:02:08 +02007853 lyxp_expr_free(set->ctx, exp2);
7854 free(pred);
7855 return rc;
Michal Vaskod3678892020-05-21 10:06:58 +02007856}
7857
7858/**
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007859 * @brief Search for/check the next schema node that could be the only matching schema node meaning the
7860 * data node(s) could be found using a single hash-based search.
7861 *
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007862 * @param[in] ctx libyang context.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007863 * @param[in] node Next context node to check.
7864 * @param[in] name Expected node name.
7865 * @param[in] name_len Length of @p name.
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007866 * @param[in] moveto_mod Expected node module, can be NULL for JSON format with no prefix.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007867 * @param[in] root_type XPath root type.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007868 * @param[in] format Prefix format.
7869 * @param[in,out] found Previously found node, is updated.
7870 * @return LY_SUCCESS on success,
7871 * @return LY_ENOT if the whole check failed and hashes cannot be used.
7872 */
7873static LY_ERR
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007874eval_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 +02007875 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 +02007876 const struct lysc_node **found)
7877{
7878 const struct lysc_node *scnode;
7879 const struct lys_module *mod;
7880 uint32_t idx = 0;
7881
Radek Krejci8df109d2021-04-23 12:19:08 +02007882 assert((format == LY_VALUE_JSON) || moveto_mod);
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007883
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007884continue_search:
Michal Vasko7d1d0912020-10-16 08:38:30 +02007885 scnode = NULL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007886 if (!node) {
Radek Krejci8df109d2021-04-23 12:19:08 +02007887 if ((format == LY_VALUE_JSON) && !moveto_mod) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007888 /* search all modules for a single match */
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007889 while ((mod = ly_ctx_get_module_iter(ctx, &idx))) {
Michal Vasko35a3b1d2021-07-14 09:34:16 +02007890 if (!mod->implemented) {
7891 continue;
7892 }
7893
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007894 scnode = lys_find_child(NULL, mod, name, name_len, 0, 0);
7895 if (scnode) {
7896 /* we have found a match */
7897 break;
7898 }
7899 }
7900
Michal Vasko7d1d0912020-10-16 08:38:30 +02007901 if (!scnode) {
7902 /* all modules searched */
7903 idx = 0;
7904 }
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007905 } else {
7906 /* search in top-level */
7907 scnode = lys_find_child(NULL, moveto_mod, name, name_len, 0, 0);
7908 }
7909 } else if (!*found || (lysc_data_parent(*found) != node->schema)) {
Radek Krejci8df109d2021-04-23 12:19:08 +02007910 if ((format == LY_VALUE_JSON) && !moveto_mod) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007911 /* we must adjust the module to inherit the one from the context node */
7912 moveto_mod = node->schema->module;
7913 }
7914
7915 /* search in children, do not repeat the same search */
7916 scnode = lys_find_child(node->schema, moveto_mod, name, name_len, 0, 0);
Michal Vasko7d1d0912020-10-16 08:38:30 +02007917 } /* else skip redundant search */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007918
7919 /* additional context check */
7920 if (scnode && (root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) {
7921 scnode = NULL;
7922 }
7923
7924 if (scnode) {
7925 if (*found) {
7926 /* we found a schema node with the same name but at different level, give up, too complicated
7927 * (more hash-based searches would be required, not supported) */
7928 return LY_ENOT;
7929 } else {
7930 /* remember the found schema node and continue to make sure it can be used */
7931 *found = scnode;
7932 }
7933 }
7934
7935 if (idx) {
7936 /* continue searching all the following models */
7937 goto continue_search;
7938 }
7939
7940 return LY_SUCCESS;
7941}
7942
7943/**
Michal Vasko4ad69e72021-10-26 16:25:55 +02007944 * @brief Generate message when no matching schema nodes were found for a path segment.
7945 *
7946 * @param[in] set XPath set.
7947 * @param[in] scparent Previous schema parent in the context, if only one.
7948 * @param[in] ncname XPath NCName being evaluated.
7949 * @param[in] ncname_len Length of @p ncname.
7950 * @param[in] expr Whole XPath expression.
7951 * @param[in] options XPath options.
7952 */
7953static void
7954eval_name_test_scnode_no_match_msg(struct lyxp_set *set, const struct lyxp_set_scnode *scparent, const char *ncname,
7955 uint16_t ncname_len, const char *expr, uint32_t options)
7956{
7957 const char *format;
7958 char *path = NULL, *ppath = NULL;
7959
7960 path = lysc_path(set->cur_scnode, LYSC_PATH_LOG, NULL, 0);
7961 if (scparent) {
7962 /* generate path for the parent */
7963 if (scparent->type == LYXP_NODE_ELEM) {
7964 ppath = lysc_path(scparent->scnode, LYSC_PATH_LOG, NULL, 0);
7965 } else if (scparent->type == LYXP_NODE_ROOT) {
7966 ppath = strdup("<root>");
7967 } else if (scparent->type == LYXP_NODE_ROOT_CONFIG) {
7968 ppath = strdup("<config-root>");
7969 }
7970 }
7971 if (ppath) {
7972 format = "Schema node \"%.*s\" for parent \"%s\" not found; in expr \"%.*s\" with context node \"%s\".";
7973 if (options & LYXP_SCNODE_ERROR) {
7974 LOGERR(set->ctx, LY_EVALID, format, ncname_len, ncname, ppath, (ncname - expr) + ncname_len, expr, path);
7975 } else {
7976 LOGWRN(set->ctx, format, ncname_len, ncname, ppath, (ncname - expr) + ncname_len, expr, path);
7977 }
7978 } else {
7979 format = "Schema node \"%.*s\" not found; in expr \"%.*s\" with context node \"%s\".";
7980 if (options & LYXP_SCNODE_ERROR) {
7981 LOGERR(set->ctx, LY_EVALID, format, ncname_len, ncname, (ncname - expr) + ncname_len, expr, path);
7982 } else {
7983 LOGWRN(set->ctx, format, ncname_len, ncname, (ncname - expr) + ncname_len, expr, path);
7984 }
7985 }
7986 free(path);
7987 free(ppath);
7988}
7989
7990/**
Michal Vaskod3678892020-05-21 10:06:58 +02007991 * @brief Evaluate NameTest and any following Predicates. Logs directly on error.
7992 *
7993 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
7994 * [6] NodeTest ::= NameTest | NodeType '(' ')'
7995 * [7] NameTest ::= '*' | NCName ':' '*' | QName
7996 *
7997 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007998 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko49fec8e2022-05-24 10:28:33 +02007999 * @param[in] axis What axis to search on.
Michal Vaskod3678892020-05-21 10:06:58 +02008000 * @param[in] all_desc Whether to search all the descendants or children only.
aPiecek8b0cc152021-05-31 16:40:31 +02008001 * @param[in,out] set Context and result set.
Michal Vaskod3678892020-05-21 10:06:58 +02008002 * @param[in] options XPath options.
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008003 * @return LY_ERR (LY_EINCOMPLETE on unresolved when, LY_ENOT for not found schema node)
Michal Vaskod3678892020-05-21 10:06:58 +02008004 */
8005static LY_ERR
Michal Vasko49fec8e2022-05-24 10:28:33 +02008006eval_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 +02008007 struct lyxp_set *set, uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02008008{
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008009 LY_ERR rc = LY_SUCCESS, r;
Michal Vasko004d3152020-06-11 19:59:22 +02008010 const char *ncname, *ncname_dict = NULL;
Michal Vasko56c008c2022-07-29 14:57:47 +02008011 uint32_t i, ncname_len;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008012 const struct lys_module *moveto_mod = NULL;
Michal Vaskoc71c37b2021-01-11 13:40:02 +01008013 const struct lysc_node *scnode = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02008014 struct ly_path_predicate *predicates = NULL;
8015 enum ly_path_pred_type pred_type = 0;
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008016 int scnode_skip_pred = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02008017
aPiecek8b0cc152021-05-31 16:40:31 +02008018 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008019 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008020 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02008021
aPiecek8b0cc152021-05-31 16:40:31 +02008022 if (options & LYXP_SKIP_EXPR) {
Michal Vaskod3678892020-05-21 10:06:58 +02008023 goto moveto;
8024 }
8025
Michal Vasko004d3152020-06-11 19:59:22 +02008026 ncname = &exp->expr[exp->tok_pos[*tok_idx - 1]];
8027 ncname_len = exp->tok_len[*tok_idx - 1];
Michal Vaskod3678892020-05-21 10:06:58 +02008028
Michal Vaskoc71c37b2021-01-11 13:40:02 +01008029 if ((ncname[0] == '*') && (ncname_len == 1)) {
8030 /* all nodes will match */
8031 goto moveto;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008032 }
8033
Michal Vaskoc71c37b2021-01-11 13:40:02 +01008034 /* parse (and skip) module name */
8035 rc = moveto_resolve_model(&ncname, &ncname_len, set, NULL, &moveto_mod);
Michal Vaskod3678892020-05-21 10:06:58 +02008036 LY_CHECK_GOTO(rc, cleanup);
8037
Michal Vasko49fec8e2022-05-24 10:28:33 +02008038 if ((ncname[0] == '*') && (ncname_len == 1)) {
8039 /* all nodes from the module will match */
8040 goto moveto;
8041 }
8042
8043 if (((set->format == LY_VALUE_JSON) || moveto_mod) && (axis == LYXP_AXIS_CHILD) && !all_desc &&
8044 (set->type == LYXP_SET_NODE_SET)) {
Michal Vaskod3678892020-05-21 10:06:58 +02008045 /* find the matching schema node in some parent in the context */
Michal Vasko56c008c2022-07-29 14:57:47 +02008046 for (i = 0; i < set->used; ++i) {
Michal Vaskoc71c37b2021-01-11 13:40:02 +01008047 if (eval_name_test_with_predicate_get_scnode(set->ctx, set->val.nodes[i].node, ncname, ncname_len,
8048 moveto_mod, set->root_type, set->format, &scnode)) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008049 /* check failed */
8050 scnode = NULL;
8051 break;
Michal Vaskod3678892020-05-21 10:06:58 +02008052 }
8053 }
8054
Michal Vasko004d3152020-06-11 19:59:22 +02008055 if (scnode && (scnode->nodetype & (LYS_LIST | LYS_LEAFLIST))) {
8056 /* try to create the predicates */
Michal Vasko3d969ff2022-07-29 15:02:08 +02008057 if (eval_name_test_try_compile_predicates(exp, tok_idx, scnode, set, &predicates, &pred_type)) {
Michal Vasko004d3152020-06-11 19:59:22 +02008058 /* hashes cannot be used */
Michal Vaskod3678892020-05-21 10:06:58 +02008059 scnode = NULL;
8060 }
8061 }
8062 }
8063
Michal Vaskoc71c37b2021-01-11 13:40:02 +01008064 if (!scnode) {
8065 /* we are not able to match based on a schema node and not all the modules match ("*"),
Michal Vasko004d3152020-06-11 19:59:22 +02008066 * use dictionary for efficient comparison */
Radek Krejci011e4aa2020-09-04 15:22:31 +02008067 LY_CHECK_GOTO(rc = lydict_insert(set->ctx, ncname, ncname_len, &ncname_dict), cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02008068 }
8069
8070moveto:
8071 /* move to the attribute(s), data node(s), or schema node(s) */
Michal Vasko49fec8e2022-05-24 10:28:33 +02008072 if (axis == LYXP_AXIS_ATTRIBUTE) {
aPiecek8b0cc152021-05-31 16:40:31 +02008073 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008074 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskod3678892020-05-21 10:06:58 +02008075 } else {
8076 if (all_desc) {
Michal Vaskocdad7122020-11-09 21:04:44 +01008077 rc = moveto_attr_alldesc(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02008078 } else {
aPiecek8b0cc152021-05-31 16:40:31 +02008079 rc = moveto_attr(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02008080 }
8081 LY_CHECK_GOTO(rc, cleanup);
8082 }
8083 } else {
aPiecek8b0cc152021-05-31 16:40:31 +02008084 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008085 const struct lyxp_set_scnode *scparent = NULL;
Michal Vasko56c008c2022-07-29 14:57:47 +02008086 ly_bool found = 0;
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008087
8088 /* remember parent if there is only one, to print in the warning */
8089 for (i = 0; i < set->used; ++i) {
8090 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
8091 if (!scparent) {
8092 /* remember the context node */
8093 scparent = &set->val.scnodes[i];
8094 } else {
8095 /* several context nodes, no reasonable error possible */
8096 scparent = NULL;
8097 break;
8098 }
8099 }
8100 }
Radek Krejci1deb5be2020-08-26 16:43:36 +02008101
Michal Vasko49fec8e2022-05-24 10:28:33 +02008102 if (all_desc && (axis == LYXP_AXIS_CHILD)) {
8103 /* efficient evaluation that does not add all the descendants into the set */
8104 rc = moveto_scnode_alldesc_child(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02008105 } else {
Michal Vasko49fec8e2022-05-24 10:28:33 +02008106 if (all_desc) {
8107 /* "//" == "/descendant-or-self::node()/" */
8108 rc = xpath_pi_node(set, LYXP_AXIS_DESCENDANT_OR_SELF, options);
8109 LY_CHECK_GOTO(rc, cleanup);
8110 }
8111 rc = moveto_scnode(set, moveto_mod, ncname_dict, axis, options);
Michal Vaskod3678892020-05-21 10:06:58 +02008112 }
8113 LY_CHECK_GOTO(rc, cleanup);
8114
Michal Vasko56c008c2022-07-29 14:57:47 +02008115 i = set->used;
8116 do {
8117 --i;
Michal Vasko1a09b212021-05-06 13:00:10 +02008118 if (set->val.scnodes[i].in_ctx > LYXP_SET_SCNODE_ATOM_NODE) {
Michal Vasko56c008c2022-07-29 14:57:47 +02008119 found = 1;
Michal Vaskod3678892020-05-21 10:06:58 +02008120 break;
8121 }
Michal Vasko56c008c2022-07-29 14:57:47 +02008122 } while (i);
8123 if (!found) {
Michal Vasko4ad69e72021-10-26 16:25:55 +02008124 /* generate message */
8125 eval_name_test_scnode_no_match_msg(set, scparent, ncname, ncname_len, exp->expr, options);
8126
8127 if (options & LYXP_SCNODE_ERROR) {
8128 /* error */
8129 rc = LY_EVALID;
8130 goto cleanup;
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008131 }
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008132
8133 /* skip the predicates and the rest of this path to not generate invalid warnings */
8134 rc = LY_ENOT;
8135 scnode_skip_pred = 1;
Michal Vaskod3678892020-05-21 10:06:58 +02008136 }
8137 } else {
Michal Vasko49fec8e2022-05-24 10:28:33 +02008138 if (all_desc && (axis == LYXP_AXIS_CHILD)) {
8139 /* efficient evaluation */
8140 rc = moveto_node_alldesc_child(set, moveto_mod, ncname_dict, options);
8141 } else if (scnode && (axis == LYXP_AXIS_CHILD)) {
8142 /* we can find the child nodes using hashes */
8143 rc = moveto_node_hash_child(set, scnode, predicates, options);
Michal Vaskod3678892020-05-21 10:06:58 +02008144 } else {
Michal Vasko49fec8e2022-05-24 10:28:33 +02008145 if (all_desc) {
8146 /* "//" == "/descendant-or-self::node()/" */
8147 rc = xpath_pi_node(set, LYXP_AXIS_DESCENDANT_OR_SELF, options);
8148 LY_CHECK_GOTO(rc, cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02008149 }
Michal Vasko49fec8e2022-05-24 10:28:33 +02008150 rc = moveto_node(set, moveto_mod, ncname_dict, axis, options);
Michal Vaskod3678892020-05-21 10:06:58 +02008151 }
8152 LY_CHECK_GOTO(rc, cleanup);
8153 }
8154 }
8155
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008156 if (scnode_skip_pred) {
8157 /* skip predicates */
8158 options |= LYXP_SKIP_EXPR;
8159 }
8160
Michal Vaskod3678892020-05-21 10:06:58 +02008161 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02008162 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02008163 r = eval_predicate(exp, tok_idx, set, options, axis);
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008164 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02008165 }
8166
8167cleanup:
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008168 if (scnode_skip_pred) {
8169 /* restore options */
8170 options &= ~LYXP_SKIP_EXPR;
8171 }
aPiecek8b0cc152021-05-31 16:40:31 +02008172 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko004d3152020-06-11 19:59:22 +02008173 lydict_remove(set->ctx, ncname_dict);
Michal Vaskof7e16e22020-10-21 09:24:39 +02008174 ly_path_predicates_free(set->ctx, pred_type, predicates);
Michal Vaskodb51a8d2020-05-27 15:22:29 +02008175 }
Michal Vaskod3678892020-05-21 10:06:58 +02008176 return rc;
8177}
8178
8179/**
8180 * @brief Evaluate NodeType and any following Predicates. Logs directly on error.
8181 *
8182 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
8183 * [6] NodeTest ::= NameTest | NodeType '(' ')'
8184 * [8] NodeType ::= 'text' | 'node'
8185 *
8186 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008187 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko49fec8e2022-05-24 10:28:33 +02008188 * @param[in] axis Axis to search on.
8189 * @param[in] all_desc Whether to search all the descendants or axis only.
aPiecek8b0cc152021-05-31 16:40:31 +02008190 * @param[in,out] set Context and result set.
Michal Vaskod3678892020-05-21 10:06:58 +02008191 * @param[in] options XPath options.
8192 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8193 */
8194static LY_ERR
Michal Vasko49fec8e2022-05-24 10:28:33 +02008195eval_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 +02008196 struct lyxp_set *set, uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02008197{
8198 LY_ERR rc;
8199
Michal Vaskod3678892020-05-21 10:06:58 +02008200 (void)all_desc;
8201
aPiecek8b0cc152021-05-31 16:40:31 +02008202 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko004d3152020-06-11 19:59:22 +02008203 assert(exp->tok_len[*tok_idx] == 4);
Michal Vasko49fec8e2022-05-24 10:28:33 +02008204 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "node", 4)) {
8205 rc = xpath_pi_node(set, axis, options);
Michal Vaskod3678892020-05-21 10:06:58 +02008206 } else {
Michal Vasko49fec8e2022-05-24 10:28:33 +02008207 assert(!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "text", 4));
8208 rc = xpath_pi_text(set, axis, options);
Michal Vaskod3678892020-05-21 10:06:58 +02008209 }
Michal Vasko49fec8e2022-05-24 10:28:33 +02008210 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02008211 }
aPiecek8b0cc152021-05-31 16:40:31 +02008212 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008213 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008214 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02008215
8216 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02008217 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
aPiecek8b0cc152021-05-31 16:40:31 +02008218 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008219 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008220 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02008221
8222 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02008223 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
aPiecek8b0cc152021-05-31 16:40:31 +02008224 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008225 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008226 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02008227
8228 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02008229 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02008230 rc = eval_predicate(exp, tok_idx, set, options, axis);
Michal Vaskod3678892020-05-21 10:06:58 +02008231 LY_CHECK_RET(rc);
8232 }
8233
8234 return LY_SUCCESS;
8235}
8236
8237/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02008238 * @brief Evaluate RelativeLocationPath. Logs directly on error.
8239 *
8240 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
8241 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
Michal Vaskod3678892020-05-21 10:06:58 +02008242 * [6] NodeTest ::= NameTest | NodeType '(' ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02008243 *
8244 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008245 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008246 * @param[in] all_desc Whether to search all the descendants or children only.
aPiecek8b0cc152021-05-31 16:40:31 +02008247 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008248 * @param[in] options XPath options.
8249 * @return LY_ERR (YL_EINCOMPLETE on unresolved when)
8250 */
8251static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008252eval_relative_location_path(const struct lyxp_expr *exp, uint16_t *tok_idx, ly_bool all_desc, struct lyxp_set *set,
8253 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008254{
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008255 LY_ERR rc = LY_SUCCESS;
Michal Vasko49fec8e2022-05-24 10:28:33 +02008256 enum lyxp_axis axis;
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008257 int scnode_skip_path = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008258
8259 goto step;
8260 do {
8261 /* evaluate '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02008262 if (exp->tok_len[*tok_idx] == 1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008263 all_desc = 0;
8264 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02008265 assert(exp->tok_len[*tok_idx] == 2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008266 all_desc = 1;
8267 }
aPiecek8b0cc152021-05-31 16:40:31 +02008268 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008269 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008270 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008271
8272step:
Michal Vasko49fec8e2022-05-24 10:28:33 +02008273 /* AxisSpecifier */
8274 if (exp->tokens[*tok_idx] == LYXP_TOKEN_AXISNAME) {
8275 axis = str2axis(exp->expr + exp->tok_pos[*tok_idx], exp->tok_len[*tok_idx]);
Michal Vaskod3678892020-05-21 10:06:58 +02008276
aPiecek8b0cc152021-05-31 16:40:31 +02008277 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008278 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
8279 ++(*tok_idx);
8280
8281 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_DCOLON);
8282 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
8283 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
8284 ++(*tok_idx);
8285 } else if (exp->tokens[*tok_idx] == LYXP_TOKEN_AT) {
8286 axis = LYXP_AXIS_ATTRIBUTE;
8287
8288 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
8289 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008290 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02008291 } else {
Michal Vasko49fec8e2022-05-24 10:28:33 +02008292 /* default */
8293 axis = LYXP_AXIS_CHILD;
Michal Vaskod3678892020-05-21 10:06:58 +02008294 }
8295
Michal Vasko49fec8e2022-05-24 10:28:33 +02008296 /* NodeTest Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02008297 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008298 case LYXP_TOKEN_DOT:
8299 /* evaluate '.' */
Michal Vasko49fec8e2022-05-24 10:28:33 +02008300 if (!(options & LYXP_SKIP_EXPR)) {
8301 if (((options & LYXP_SCNODE_ALL) && (set->type != LYXP_SET_SCNODE_SET)) ||
8302 (!(options & LYXP_SCNODE_ALL) && (set->type != LYXP_SET_NODE_SET))) {
8303 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
8304 rc = LY_EVALID;
8305 goto cleanup;
8306 }
8307
8308 if (all_desc) {
8309 rc = xpath_pi_node(set, LYXP_AXIS_DESCENDANT_OR_SELF, options);
8310 LY_CHECK_GOTO(rc, cleanup);
8311 }
8312 rc = xpath_pi_node(set, LYXP_AXIS_SELF, options);
8313 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008314 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008315 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008316 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008317 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008318 break;
8319
8320 case LYXP_TOKEN_DDOT:
8321 /* evaluate '..' */
Michal Vasko49fec8e2022-05-24 10:28:33 +02008322 if (!(options & LYXP_SKIP_EXPR)) {
8323 if (((options & LYXP_SCNODE_ALL) && (set->type != LYXP_SET_SCNODE_SET)) ||
8324 (!(options & LYXP_SCNODE_ALL) && (set->type != LYXP_SET_NODE_SET))) {
8325 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
8326 rc = LY_EVALID;
8327 goto cleanup;
8328 }
8329
8330 if (all_desc) {
8331 rc = xpath_pi_node(set, LYXP_AXIS_DESCENDANT_OR_SELF, options);
8332 LY_CHECK_GOTO(rc, cleanup);
8333 }
8334 rc = xpath_pi_node(set, LYXP_AXIS_PARENT, options);
8335 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008336 }
aPiecek8b0cc152021-05-31 16:40:31 +02008337 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008338 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008339 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008340 break;
8341
Michal Vasko03ff5a72019-09-11 13:49:33 +02008342 case LYXP_TOKEN_NAMETEST:
Michal Vaskod3678892020-05-21 10:06:58 +02008343 /* evaluate NameTest Predicate* */
Michal Vasko49fec8e2022-05-24 10:28:33 +02008344 rc = eval_name_test_with_predicate(exp, tok_idx, axis, all_desc, set, options);
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008345 if (rc == LY_ENOT) {
8346 assert(options & LYXP_SCNODE_ALL);
8347 /* skip the rest of this path */
8348 rc = LY_SUCCESS;
8349 scnode_skip_path = 1;
8350 options |= LYXP_SKIP_EXPR;
8351 }
8352 LY_CHECK_GOTO(rc, cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02008353 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008354
Michal Vaskod3678892020-05-21 10:06:58 +02008355 case LYXP_TOKEN_NODETYPE:
8356 /* evaluate NodeType Predicate* */
Michal Vasko49fec8e2022-05-24 10:28:33 +02008357 rc = eval_node_type_with_predicate(exp, tok_idx, axis, all_desc, set, options);
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008358 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008359 break;
8360
8361 default:
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008362 LOGINT(set->ctx);
8363 rc = LY_EINT;
8364 goto cleanup;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008365 }
Michal Vasko004d3152020-06-11 19:59:22 +02008366 } while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008367
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008368cleanup:
8369 if (scnode_skip_path) {
8370 options &= ~LYXP_SKIP_EXPR;
8371 }
8372 return rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008373}
8374
8375/**
8376 * @brief Evaluate AbsoluteLocationPath. Logs directly on error.
8377 *
8378 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
8379 *
8380 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008381 * @param[in] tok_idx Position in the expression @p exp.
aPiecek8b0cc152021-05-31 16:40:31 +02008382 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008383 * @param[in] options XPath options.
8384 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8385 */
8386static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008387eval_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 +02008388{
Radek Krejci857189e2020-09-01 13:26:36 +02008389 ly_bool all_desc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008390
aPiecek8b0cc152021-05-31 16:40:31 +02008391 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008392 /* no matter what tokens follow, we need to be at the root */
Michal Vaskob0099a92020-08-31 14:55:23 +02008393 LY_CHECK_RET(moveto_root(set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008394 }
8395
8396 /* '/' RelativeLocationPath? */
Michal Vasko004d3152020-06-11 19:59:22 +02008397 if (exp->tok_len[*tok_idx] == 1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008398 /* evaluate '/' - deferred */
8399 all_desc = 0;
aPiecek8b0cc152021-05-31 16:40:31 +02008400 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008401 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008402 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008403
Michal Vasko004d3152020-06-11 19:59:22 +02008404 if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008405 return LY_SUCCESS;
8406 }
Michal Vasko004d3152020-06-11 19:59:22 +02008407 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008408 case LYXP_TOKEN_DOT:
8409 case LYXP_TOKEN_DDOT:
Michal Vasko49fec8e2022-05-24 10:28:33 +02008410 case LYXP_TOKEN_AXISNAME:
Michal Vasko03ff5a72019-09-11 13:49:33 +02008411 case LYXP_TOKEN_AT:
8412 case LYXP_TOKEN_NAMETEST:
8413 case LYXP_TOKEN_NODETYPE:
Michal Vaskob0099a92020-08-31 14:55:23 +02008414 LY_CHECK_RET(eval_relative_location_path(exp, tok_idx, all_desc, set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008415 break;
8416 default:
8417 break;
8418 }
8419
Michal Vasko03ff5a72019-09-11 13:49:33 +02008420 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02008421 /* '//' RelativeLocationPath */
Michal Vasko03ff5a72019-09-11 13:49:33 +02008422 /* evaluate '//' - deferred so as not to waste memory by remembering all the nodes */
8423 all_desc = 1;
aPiecek8b0cc152021-05-31 16:40:31 +02008424 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008425 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008426 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008427
Michal Vaskob0099a92020-08-31 14:55:23 +02008428 LY_CHECK_RET(eval_relative_location_path(exp, tok_idx, all_desc, set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008429 }
8430
8431 return LY_SUCCESS;
8432}
8433
8434/**
8435 * @brief Evaluate FunctionCall. Logs directly on error.
8436 *
Michal Vaskod3678892020-05-21 10:06:58 +02008437 * [11] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02008438 *
8439 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008440 * @param[in] tok_idx Position in the expression @p exp.
aPiecek8b0cc152021-05-31 16:40:31 +02008441 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008442 * @param[in] options XPath options.
8443 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8444 */
8445static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008446eval_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 +02008447{
8448 LY_ERR rc;
Michal Vasko69730152020-10-09 16:30:07 +02008449
Radek Krejci1deb5be2020-08-26 16:43:36 +02008450 LY_ERR (*xpath_func)(struct lyxp_set **, uint16_t, struct lyxp_set *, uint32_t) = NULL;
Michal Vasko0cbf54f2019-12-16 10:01:06 +01008451 uint16_t arg_count = 0, i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008452 struct lyxp_set **args = NULL, **args_aux;
8453
aPiecek8b0cc152021-05-31 16:40:31 +02008454 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008455 /* FunctionName */
Michal Vasko004d3152020-06-11 19:59:22 +02008456 switch (exp->tok_len[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008457 case 3:
Michal Vasko004d3152020-06-11 19:59:22 +02008458 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008459 xpath_func = &xpath_not;
Michal Vasko004d3152020-06-11 19:59:22 +02008460 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008461 xpath_func = &xpath_sum;
8462 }
8463 break;
8464 case 4:
Michal Vasko004d3152020-06-11 19:59:22 +02008465 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008466 xpath_func = &xpath_lang;
Michal Vasko004d3152020-06-11 19:59:22 +02008467 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008468 xpath_func = &xpath_last;
Michal Vasko004d3152020-06-11 19:59:22 +02008469 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008470 xpath_func = &xpath_name;
Michal Vasko004d3152020-06-11 19:59:22 +02008471 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008472 xpath_func = &xpath_true;
8473 }
8474 break;
8475 case 5:
Michal Vasko004d3152020-06-11 19:59:22 +02008476 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008477 xpath_func = &xpath_count;
Michal Vasko004d3152020-06-11 19:59:22 +02008478 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008479 xpath_func = &xpath_false;
Michal Vasko004d3152020-06-11 19:59:22 +02008480 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008481 xpath_func = &xpath_floor;
Michal Vasko004d3152020-06-11 19:59:22 +02008482 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008483 xpath_func = &xpath_round;
Michal Vasko004d3152020-06-11 19:59:22 +02008484 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008485 xpath_func = &xpath_deref;
8486 }
8487 break;
8488 case 6:
Michal Vasko004d3152020-06-11 19:59:22 +02008489 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008490 xpath_func = &xpath_concat;
Michal Vasko004d3152020-06-11 19:59:22 +02008491 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008492 xpath_func = &xpath_number;
Michal Vasko004d3152020-06-11 19:59:22 +02008493 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008494 xpath_func = &xpath_string;
8495 }
8496 break;
8497 case 7:
Michal Vasko004d3152020-06-11 19:59:22 +02008498 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008499 xpath_func = &xpath_boolean;
Michal Vasko004d3152020-06-11 19:59:22 +02008500 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008501 xpath_func = &xpath_ceiling;
Michal Vasko004d3152020-06-11 19:59:22 +02008502 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008503 xpath_func = &xpath_current;
8504 }
8505 break;
8506 case 8:
Michal Vasko004d3152020-06-11 19:59:22 +02008507 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008508 xpath_func = &xpath_contains;
Michal Vasko004d3152020-06-11 19:59:22 +02008509 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008510 xpath_func = &xpath_position;
Michal Vasko004d3152020-06-11 19:59:22 +02008511 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "re-match", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008512 xpath_func = &xpath_re_match;
8513 }
8514 break;
8515 case 9:
Michal Vasko004d3152020-06-11 19:59:22 +02008516 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008517 xpath_func = &xpath_substring;
Michal Vasko004d3152020-06-11 19:59:22 +02008518 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008519 xpath_func = &xpath_translate;
8520 }
8521 break;
8522 case 10:
Michal Vasko004d3152020-06-11 19:59:22 +02008523 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008524 xpath_func = &xpath_local_name;
Michal Vasko004d3152020-06-11 19:59:22 +02008525 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008526 xpath_func = &xpath_enum_value;
Michal Vasko004d3152020-06-11 19:59:22 +02008527 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008528 xpath_func = &xpath_bit_is_set;
8529 }
8530 break;
8531 case 11:
Michal Vasko004d3152020-06-11 19:59:22 +02008532 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008533 xpath_func = &xpath_starts_with;
8534 }
8535 break;
8536 case 12:
Michal Vasko004d3152020-06-11 19:59:22 +02008537 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008538 xpath_func = &xpath_derived_from;
8539 }
8540 break;
8541 case 13:
Michal Vasko004d3152020-06-11 19:59:22 +02008542 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008543 xpath_func = &xpath_namespace_uri;
Michal Vasko004d3152020-06-11 19:59:22 +02008544 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string-length", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008545 xpath_func = &xpath_string_length;
8546 }
8547 break;
8548 case 15:
Michal Vasko004d3152020-06-11 19:59:22 +02008549 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008550 xpath_func = &xpath_normalize_space;
Michal Vasko004d3152020-06-11 19:59:22 +02008551 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-after", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008552 xpath_func = &xpath_substring_after;
8553 }
8554 break;
8555 case 16:
Michal Vasko004d3152020-06-11 19:59:22 +02008556 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008557 xpath_func = &xpath_substring_before;
8558 }
8559 break;
8560 case 20:
Michal Vasko004d3152020-06-11 19:59:22 +02008561 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from-or-self", 20)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008562 xpath_func = &xpath_derived_from_or_self;
8563 }
8564 break;
8565 }
8566
8567 if (!xpath_func) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01008568 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 +02008569 return LY_EVALID;
8570 }
8571 }
8572
aPiecek8b0cc152021-05-31 16:40:31 +02008573 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008574 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008575 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008576
8577 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02008578 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
aPiecek8b0cc152021-05-31 16:40:31 +02008579 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008580 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008581 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008582
8583 /* ( Expr ( ',' Expr )* )? */
Michal Vasko004d3152020-06-11 19:59:22 +02008584 if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) {
aPiecek8b0cc152021-05-31 16:40:31 +02008585 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008586 args = malloc(sizeof *args);
8587 LY_CHECK_ERR_GOTO(!args, LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
8588 arg_count = 1;
8589 args[0] = set_copy(set);
8590 if (!args[0]) {
8591 rc = LY_EMEM;
8592 goto cleanup;
8593 }
8594
Michal Vasko004d3152020-06-11 19:59:22 +02008595 rc = eval_expr_select(exp, tok_idx, 0, args[0], options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008596 LY_CHECK_GOTO(rc, cleanup);
8597 } else {
aPiecek8b0cc152021-05-31 16:40:31 +02008598 rc = eval_expr_select(exp, tok_idx, 0, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008599 LY_CHECK_GOTO(rc, cleanup);
8600 }
8601 }
Michal Vasko004d3152020-06-11 19:59:22 +02008602 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) {
aPiecek8b0cc152021-05-31 16:40:31 +02008603 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008604 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008605 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008606
aPiecek8b0cc152021-05-31 16:40:31 +02008607 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008608 ++arg_count;
8609 args_aux = realloc(args, arg_count * sizeof *args);
8610 LY_CHECK_ERR_GOTO(!args_aux, arg_count--; LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
8611 args = args_aux;
8612 args[arg_count - 1] = set_copy(set);
8613 if (!args[arg_count - 1]) {
8614 rc = LY_EMEM;
8615 goto cleanup;
8616 }
8617
Michal Vasko004d3152020-06-11 19:59:22 +02008618 rc = eval_expr_select(exp, tok_idx, 0, args[arg_count - 1], options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008619 LY_CHECK_GOTO(rc, cleanup);
8620 } else {
aPiecek8b0cc152021-05-31 16:40:31 +02008621 rc = eval_expr_select(exp, tok_idx, 0, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008622 LY_CHECK_GOTO(rc, cleanup);
8623 }
8624 }
8625
8626 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02008627 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
aPiecek8b0cc152021-05-31 16:40:31 +02008628 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008629 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008630 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008631
aPiecek8b0cc152021-05-31 16:40:31 +02008632 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008633 /* evaluate function */
8634 rc = xpath_func(args, arg_count, set, options);
8635
8636 if (options & LYXP_SCNODE_ALL) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008637 /* merge all nodes from arg evaluations */
8638 for (i = 0; i < arg_count; ++i) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008639 set_scnode_clear_ctx(args[i], LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008640 lyxp_set_scnode_merge(set, args[i]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008641 }
8642 }
8643 } else {
8644 rc = LY_SUCCESS;
8645 }
8646
8647cleanup:
8648 for (i = 0; i < arg_count; ++i) {
8649 lyxp_set_free(args[i]);
8650 }
8651 free(args);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008652 return rc;
8653}
8654
8655/**
8656 * @brief Evaluate Number. Logs directly on error.
8657 *
8658 * @param[in] ctx Context for errors.
8659 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008660 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008661 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8662 * @return LY_ERR
8663 */
8664static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008665eval_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 +02008666{
8667 long double num;
8668 char *endptr;
8669
8670 if (set) {
8671 errno = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02008672 num = strtold(&exp->expr[exp->tok_pos[*tok_idx]], &endptr);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008673 if (errno) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01008674 LOGVAL(ctx, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]);
8675 LOGVAL(ctx, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double (%s).",
Michal Vasko69730152020-10-09 16:30:07 +02008676 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]], strerror(errno));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008677 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02008678 } else if (endptr - &exp->expr[exp->tok_pos[*tok_idx]] != exp->tok_len[*tok_idx]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01008679 LOGVAL(ctx, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]);
8680 LOGVAL(ctx, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double.",
Michal Vasko69730152020-10-09 16:30:07 +02008681 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008682 return LY_EVALID;
8683 }
8684
8685 set_fill_number(set, num);
8686 }
8687
8688 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008689 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008690 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008691 return LY_SUCCESS;
8692}
8693
aPiecekdf23eee2021-10-07 12:21:50 +02008694LY_ERR
8695lyxp_vars_find(struct lyxp_var *vars, const char *name, size_t name_len, struct lyxp_var **var)
8696{
8697 LY_ERR ret = LY_ENOTFOUND;
8698 LY_ARRAY_COUNT_TYPE u;
8699
8700 assert(vars && name);
8701
8702 name_len = name_len ? name_len : strlen(name);
8703
8704 LY_ARRAY_FOR(vars, u) {
8705 if (!strncmp(vars[u].name, name, name_len)) {
8706 ret = LY_SUCCESS;
8707 break;
8708 }
8709 }
8710
8711 if (var && !ret) {
8712 *var = &vars[u];
8713 }
8714
8715 return ret;
8716}
8717
Michal Vasko03ff5a72019-09-11 13:49:33 +02008718/**
aPiecekfba75362021-10-07 12:39:48 +02008719 * @brief Evaluate VariableReference.
8720 *
8721 * @param[in] exp Parsed XPath expression.
8722 * @param[in] tok_idx Position in the expression @p exp.
8723 * @param[in] vars [Sized array](@ref sizedarrays) of XPath variables.
8724 * @param[in,out] set Context and result set.
8725 * @param[in] options XPath options.
8726 * @return LY_ERR value.
8727 */
8728static LY_ERR
8729eval_variable_reference(const struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set, uint32_t options)
8730{
8731 LY_ERR ret;
8732 const char *name;
8733 struct lyxp_var *var;
8734 const struct lyxp_var *vars;
8735 struct lyxp_expr *tokens = NULL;
Michal Vaskoe68b5402022-07-29 14:58:15 +02008736 uint16_t token_index, name_len;
aPiecekfba75362021-10-07 12:39:48 +02008737
8738 vars = set->vars;
8739
Michal Vasko49fec8e2022-05-24 10:28:33 +02008740 /* find out the name and value of the variable */
aPiecekfba75362021-10-07 12:39:48 +02008741 name = &exp->expr[exp->tok_pos[*tok_idx]];
Michal Vaskoe68b5402022-07-29 14:58:15 +02008742 name_len = exp->tok_len[*tok_idx];
8743 ret = lyxp_vars_find((struct lyxp_var *)vars, name, name_len, &var);
8744 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 +02008745
Michal Vasko49fec8e2022-05-24 10:28:33 +02008746 /* parse value */
aPiecekfba75362021-10-07 12:39:48 +02008747 ret = lyxp_expr_parse(set->ctx, var->value, 0, 1, &tokens);
8748 LY_CHECK_GOTO(ret, cleanup);
8749
Michal Vasko49fec8e2022-05-24 10:28:33 +02008750 /* evaluate value */
aPiecekfba75362021-10-07 12:39:48 +02008751 token_index = 0;
8752 ret = eval_expr_select(tokens, &token_index, 0, set, options);
8753 LY_CHECK_GOTO(ret, cleanup);
8754
8755cleanup:
8756 lyxp_expr_free(set->ctx, tokens);
8757
8758 return ret;
8759}
8760
8761/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02008762 * @brief Evaluate PathExpr. Logs directly on error.
8763 *
Michal Vaskod3678892020-05-21 10:06:58 +02008764 * [12] PathExpr ::= LocationPath | PrimaryExpr Predicate*
Michal Vasko03ff5a72019-09-11 13:49:33 +02008765 * | PrimaryExpr Predicate* '/' RelativeLocationPath
8766 * | PrimaryExpr Predicate* '//' RelativeLocationPath
8767 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
aPiecekfba75362021-10-07 12:39:48 +02008768 * [10] PrimaryExpr ::= VariableReference | '(' Expr ')' | Literal | Number | FunctionCall
Michal Vasko03ff5a72019-09-11 13:49:33 +02008769 *
8770 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008771 * @param[in] tok_idx Position in the expression @p exp.
aPiecek8b0cc152021-05-31 16:40:31 +02008772 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008773 * @param[in] options XPath options.
8774 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8775 */
8776static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008777eval_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 +02008778{
Michal Vasko49fec8e2022-05-24 10:28:33 +02008779 ly_bool all_desc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008780 LY_ERR rc;
8781
Michal Vasko004d3152020-06-11 19:59:22 +02008782 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008783 case LYXP_TOKEN_PAR1:
8784 /* '(' Expr ')' */
8785
8786 /* '(' */
aPiecek8b0cc152021-05-31 16:40:31 +02008787 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008788 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008789 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008790
8791 /* Expr */
Michal Vasko004d3152020-06-11 19:59:22 +02008792 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008793 LY_CHECK_RET(rc);
8794
8795 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02008796 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
aPiecek8b0cc152021-05-31 16:40:31 +02008797 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008798 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008799 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008800
Michal Vasko03ff5a72019-09-11 13:49:33 +02008801 goto predicate;
8802
8803 case LYXP_TOKEN_DOT:
8804 case LYXP_TOKEN_DDOT:
Michal Vasko49fec8e2022-05-24 10:28:33 +02008805 case LYXP_TOKEN_AXISNAME:
Michal Vasko03ff5a72019-09-11 13:49:33 +02008806 case LYXP_TOKEN_AT:
8807 case LYXP_TOKEN_NAMETEST:
8808 case LYXP_TOKEN_NODETYPE:
8809 /* RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02008810 rc = eval_relative_location_path(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008811 LY_CHECK_RET(rc);
8812 break;
8813
aPiecekfba75362021-10-07 12:39:48 +02008814 case LYXP_TOKEN_VARREF:
8815 /* VariableReference */
8816 rc = eval_variable_reference(exp, tok_idx, set, options);
8817 LY_CHECK_RET(rc);
8818 ++(*tok_idx);
8819
aPiecekfba75362021-10-07 12:39:48 +02008820 goto predicate;
8821
Michal Vasko03ff5a72019-09-11 13:49:33 +02008822 case LYXP_TOKEN_FUNCNAME:
8823 /* FunctionCall */
aPiecek8b0cc152021-05-31 16:40:31 +02008824 rc = eval_function_call(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008825 LY_CHECK_RET(rc);
8826
Michal Vasko03ff5a72019-09-11 13:49:33 +02008827 goto predicate;
8828
Michal Vasko3e48bf32020-06-01 08:39:07 +02008829 case LYXP_TOKEN_OPER_PATH:
8830 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +02008831 /* AbsoluteLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02008832 rc = eval_absolute_location_path(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008833 LY_CHECK_RET(rc);
8834 break;
8835
8836 case LYXP_TOKEN_LITERAL:
8837 /* Literal */
aPiecek8b0cc152021-05-31 16:40:31 +02008838 if ((options & LYXP_SKIP_EXPR) || (options & LYXP_SCNODE_ALL)) {
8839 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008840 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008841 }
Michal Vasko004d3152020-06-11 19:59:22 +02008842 eval_literal(exp, tok_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008843 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02008844 eval_literal(exp, tok_idx, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008845 }
8846
Michal Vasko03ff5a72019-09-11 13:49:33 +02008847 goto predicate;
8848
8849 case LYXP_TOKEN_NUMBER:
8850 /* Number */
aPiecek8b0cc152021-05-31 16:40:31 +02008851 if ((options & LYXP_SKIP_EXPR) || (options & LYXP_SCNODE_ALL)) {
8852 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008853 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008854 }
Michal Vasko004d3152020-06-11 19:59:22 +02008855 rc = eval_number(NULL, exp, tok_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008856 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02008857 rc = eval_number(set->ctx, exp, tok_idx, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008858 }
8859 LY_CHECK_RET(rc);
8860
Michal Vasko03ff5a72019-09-11 13:49:33 +02008861 goto predicate;
8862
8863 default:
Michal Vasko49fec8e2022-05-24 10:28:33 +02008864 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 +02008865 return LY_EVALID;
8866 }
8867
8868 return LY_SUCCESS;
8869
8870predicate:
8871 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02008872 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02008873 rc = eval_predicate(exp, tok_idx, set, options, LYXP_AXIS_CHILD);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008874 LY_CHECK_RET(rc);
8875 }
8876
8877 /* ('/' or '//') RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02008878 if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008879
8880 /* evaluate '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02008881 if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008882 all_desc = 0;
8883 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008884 all_desc = 1;
8885 }
8886
aPiecek8b0cc152021-05-31 16:40:31 +02008887 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008888 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008889 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008890
Michal Vasko004d3152020-06-11 19:59:22 +02008891 rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008892 LY_CHECK_RET(rc);
8893 }
8894
8895 return LY_SUCCESS;
8896}
8897
8898/**
8899 * @brief Evaluate UnionExpr. Logs directly on error.
8900 *
Michal Vaskod3678892020-05-21 10:06:58 +02008901 * [20] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008902 *
8903 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008904 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008905 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008906 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008907 * @param[in] options XPath options.
8908 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8909 */
8910static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008911eval_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 +02008912{
8913 LY_ERR rc = LY_SUCCESS;
8914 struct lyxp_set orig_set, set2;
8915 uint16_t i;
8916
8917 assert(repeat);
8918
8919 set_init(&orig_set, set);
8920 set_init(&set2, set);
8921
8922 set_fill_set(&orig_set, set);
8923
Michal Vasko004d3152020-06-11 19:59:22 +02008924 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008925 LY_CHECK_GOTO(rc, cleanup);
8926
8927 /* ('|' PathExpr)* */
8928 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008929 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_UNI);
aPiecek8b0cc152021-05-31 16:40:31 +02008930 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008931 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008932 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008933
aPiecek8b0cc152021-05-31 16:40:31 +02008934 if (options & LYXP_SKIP_EXPR) {
8935 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008936 LY_CHECK_GOTO(rc, cleanup);
8937 continue;
8938 }
8939
8940 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008941 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008942 LY_CHECK_GOTO(rc, cleanup);
8943
8944 /* eval */
8945 if (options & LYXP_SCNODE_ALL) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01008946 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008947 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008948 rc = moveto_union(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008949 LY_CHECK_GOTO(rc, cleanup);
8950 }
8951 }
8952
8953cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008954 lyxp_set_free_content(&orig_set);
8955 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008956 return rc;
8957}
8958
8959/**
8960 * @brief Evaluate UnaryExpr. Logs directly on error.
8961 *
Michal Vaskod3678892020-05-21 10:06:58 +02008962 * [19] UnaryExpr ::= UnionExpr | '-' UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008963 *
8964 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008965 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008966 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008967 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008968 * @param[in] options XPath options.
8969 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8970 */
8971static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008972eval_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 +02008973{
8974 LY_ERR rc;
8975 uint16_t this_op, i;
8976
8977 assert(repeat);
8978
8979 /* ('-')+ */
Michal Vasko004d3152020-06-11 19:59:22 +02008980 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008981 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008982 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 +02008983
aPiecek8b0cc152021-05-31 16:40:31 +02008984 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008985 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008986 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008987 }
8988
Michal Vasko004d3152020-06-11 19:59:22 +02008989 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNARY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008990 LY_CHECK_RET(rc);
8991
aPiecek8b0cc152021-05-31 16:40:31 +02008992 if (!(options & LYXP_SKIP_EXPR) && (repeat % 2)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008993 if (options & LYXP_SCNODE_ALL) {
8994 warn_operands(set->ctx, set, NULL, 1, exp->expr, exp->tok_pos[this_op]);
8995 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008996 rc = moveto_op_math(set, NULL, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008997 LY_CHECK_RET(rc);
8998 }
8999 }
9000
9001 return LY_SUCCESS;
9002}
9003
9004/**
9005 * @brief Evaluate MultiplicativeExpr. Logs directly on error.
9006 *
Michal Vaskod3678892020-05-21 10:06:58 +02009007 * [18] MultiplicativeExpr ::= UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02009008 * | MultiplicativeExpr '*' UnaryExpr
9009 * | MultiplicativeExpr 'div' UnaryExpr
9010 * | MultiplicativeExpr 'mod' UnaryExpr
9011 *
9012 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02009013 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009014 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02009015 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009016 * @param[in] options XPath options.
9017 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
9018 */
9019static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02009020eval_multiplicative_expr(const struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set,
9021 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02009022{
9023 LY_ERR rc;
9024 uint16_t this_op;
9025 struct lyxp_set orig_set, set2;
9026 uint16_t i;
9027
9028 assert(repeat);
9029
9030 set_init(&orig_set, set);
9031 set_init(&set2, set);
9032
9033 set_fill_set(&orig_set, set);
9034
Michal Vasko004d3152020-06-11 19:59:22 +02009035 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009036 LY_CHECK_GOTO(rc, cleanup);
9037
9038 /* ('*' / 'div' / 'mod' UnaryExpr)* */
9039 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02009040 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009041
Michal Vasko004d3152020-06-11 19:59:22 +02009042 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH);
aPiecek8b0cc152021-05-31 16:40:31 +02009043 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02009044 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02009045 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009046
aPiecek8b0cc152021-05-31 16:40:31 +02009047 if (options & LYXP_SKIP_EXPR) {
9048 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009049 LY_CHECK_GOTO(rc, cleanup);
9050 continue;
9051 }
9052
9053 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02009054 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009055 LY_CHECK_GOTO(rc, cleanup);
9056
9057 /* eval */
9058 if (options & LYXP_SCNODE_ALL) {
9059 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01009060 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02009061 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009062 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009063 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009064 LY_CHECK_GOTO(rc, cleanup);
9065 }
9066 }
9067
9068cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02009069 lyxp_set_free_content(&orig_set);
9070 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009071 return rc;
9072}
9073
9074/**
9075 * @brief Evaluate AdditiveExpr. Logs directly on error.
9076 *
Michal Vaskod3678892020-05-21 10:06:58 +02009077 * [17] AdditiveExpr ::= MultiplicativeExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02009078 * | AdditiveExpr '+' MultiplicativeExpr
9079 * | AdditiveExpr '-' MultiplicativeExpr
9080 *
9081 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02009082 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009083 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02009084 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009085 * @param[in] options XPath options.
9086 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
9087 */
9088static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02009089eval_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 +02009090{
9091 LY_ERR rc;
9092 uint16_t this_op;
9093 struct lyxp_set orig_set, set2;
9094 uint16_t i;
9095
9096 assert(repeat);
9097
9098 set_init(&orig_set, set);
9099 set_init(&set2, set);
9100
9101 set_fill_set(&orig_set, set);
9102
Michal Vasko004d3152020-06-11 19:59:22 +02009103 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009104 LY_CHECK_GOTO(rc, cleanup);
9105
9106 /* ('+' / '-' MultiplicativeExpr)* */
9107 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02009108 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009109
Michal Vasko004d3152020-06-11 19:59:22 +02009110 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009111 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02009112 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02009113 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009114
aPiecek8b0cc152021-05-31 16:40:31 +02009115 if (options & LYXP_SKIP_EXPR) {
9116 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009117 LY_CHECK_GOTO(rc, cleanup);
9118 continue;
9119 }
9120
9121 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02009122 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009123 LY_CHECK_GOTO(rc, cleanup);
9124
9125 /* eval */
9126 if (options & LYXP_SCNODE_ALL) {
9127 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01009128 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02009129 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009130 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009131 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009132 LY_CHECK_GOTO(rc, cleanup);
9133 }
9134 }
9135
9136cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02009137 lyxp_set_free_content(&orig_set);
9138 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009139 return rc;
9140}
9141
9142/**
9143 * @brief Evaluate RelationalExpr. Logs directly on error.
9144 *
Michal Vaskod3678892020-05-21 10:06:58 +02009145 * [16] RelationalExpr ::= AdditiveExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02009146 * | RelationalExpr '<' AdditiveExpr
9147 * | RelationalExpr '>' AdditiveExpr
9148 * | RelationalExpr '<=' AdditiveExpr
9149 * | RelationalExpr '>=' AdditiveExpr
9150 *
9151 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02009152 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009153 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02009154 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009155 * @param[in] options XPath options.
9156 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
9157 */
9158static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02009159eval_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 +02009160{
9161 LY_ERR rc;
9162 uint16_t this_op;
9163 struct lyxp_set orig_set, set2;
9164 uint16_t i;
9165
9166 assert(repeat);
9167
9168 set_init(&orig_set, set);
9169 set_init(&set2, set);
9170
9171 set_fill_set(&orig_set, set);
9172
Michal Vasko004d3152020-06-11 19:59:22 +02009173 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009174 LY_CHECK_GOTO(rc, cleanup);
9175
9176 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
9177 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02009178 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009179
Michal Vasko004d3152020-06-11 19:59:22 +02009180 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_COMP);
aPiecek8b0cc152021-05-31 16:40:31 +02009181 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02009182 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02009183 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009184
aPiecek8b0cc152021-05-31 16:40:31 +02009185 if (options & LYXP_SKIP_EXPR) {
9186 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009187 LY_CHECK_GOTO(rc, cleanup);
9188 continue;
9189 }
9190
9191 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02009192 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009193 LY_CHECK_GOTO(rc, cleanup);
9194
9195 /* eval */
9196 if (options & LYXP_SCNODE_ALL) {
9197 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01009198 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02009199 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009200 } else {
Michal Vasko8abcecc2022-07-28 09:55:01 +02009201 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009202 LY_CHECK_GOTO(rc, cleanup);
9203 }
9204 }
9205
9206cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02009207 lyxp_set_free_content(&orig_set);
9208 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009209 return rc;
9210}
9211
9212/**
9213 * @brief Evaluate EqualityExpr. Logs directly on error.
9214 *
Michal Vaskod3678892020-05-21 10:06:58 +02009215 * [15] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02009216 * | EqualityExpr '!=' RelationalExpr
9217 *
9218 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02009219 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009220 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02009221 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009222 * @param[in] options XPath options.
9223 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
9224 */
9225static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02009226eval_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 +02009227{
9228 LY_ERR rc;
9229 uint16_t this_op;
9230 struct lyxp_set orig_set, set2;
9231 uint16_t i;
9232
9233 assert(repeat);
9234
9235 set_init(&orig_set, set);
9236 set_init(&set2, set);
9237
9238 set_fill_set(&orig_set, set);
9239
Michal Vasko004d3152020-06-11 19:59:22 +02009240 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009241 LY_CHECK_GOTO(rc, cleanup);
9242
9243 /* ('=' / '!=' RelationalExpr)* */
9244 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02009245 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009246
Michal Vasko004d3152020-06-11 19:59:22 +02009247 assert((exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_EQUAL) || (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_NEQUAL));
aPiecek8b0cc152021-05-31 16:40:31 +02009248 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02009249 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02009250 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009251
aPiecek8b0cc152021-05-31 16:40:31 +02009252 if (options & LYXP_SKIP_EXPR) {
9253 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009254 LY_CHECK_GOTO(rc, cleanup);
9255 continue;
9256 }
9257
9258 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02009259 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009260 LY_CHECK_GOTO(rc, cleanup);
9261
9262 /* eval */
9263 if (options & LYXP_SCNODE_ALL) {
9264 warn_operands(set->ctx, set, &set2, 0, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vasko004d3152020-06-11 19:59:22 +02009265 warn_equality_value(exp, set, *tok_idx - 1, this_op - 1, *tok_idx - 1);
9266 warn_equality_value(exp, &set2, this_op - 1, this_op - 1, *tok_idx - 1);
Michal Vaskoecd62de2019-11-13 12:35:11 +01009267 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02009268 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009269 } else {
Michal Vasko8abcecc2022-07-28 09:55:01 +02009270 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009271 LY_CHECK_GOTO(rc, cleanup);
9272 }
9273 }
9274
9275cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02009276 lyxp_set_free_content(&orig_set);
9277 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009278 return rc;
9279}
9280
9281/**
9282 * @brief Evaluate AndExpr. Logs directly on error.
9283 *
Michal Vaskod3678892020-05-21 10:06:58 +02009284 * [14] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02009285 *
9286 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02009287 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009288 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02009289 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009290 * @param[in] options XPath options.
9291 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
9292 */
9293static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02009294eval_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 +02009295{
9296 LY_ERR rc;
9297 struct lyxp_set orig_set, set2;
9298 uint16_t i;
9299
9300 assert(repeat);
9301
9302 set_init(&orig_set, set);
9303 set_init(&set2, set);
9304
9305 set_fill_set(&orig_set, set);
9306
Michal Vasko004d3152020-06-11 19:59:22 +02009307 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009308 LY_CHECK_GOTO(rc, cleanup);
9309
9310 /* cast to boolean, we know that will be the final result */
aPiecek8b0cc152021-05-31 16:40:31 +02009311 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02009312 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009313 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009314 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009315 }
9316
9317 /* ('and' EqualityExpr)* */
9318 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02009319 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG);
aPiecek8b0cc152021-05-31 16:40:31 +02009320 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, ((options & LYXP_SKIP_EXPR) || !set->val.bln ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02009321 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02009322 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009323
9324 /* lazy evaluation */
aPiecek8b0cc152021-05-31 16:40:31 +02009325 if ((options & LYXP_SKIP_EXPR) || ((set->type == LYXP_SET_BOOLEAN) && !set->val.bln)) {
9326 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009327 LY_CHECK_GOTO(rc, cleanup);
9328 continue;
9329 }
9330
9331 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02009332 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009333 LY_CHECK_GOTO(rc, cleanup);
9334
9335 /* eval - just get boolean value actually */
9336 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vasko1a09b212021-05-06 13:00:10 +02009337 set_scnode_clear_ctx(&set2, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskoecd62de2019-11-13 12:35:11 +01009338 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009339 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009340 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009341 set_fill_set(set, &set2);
9342 }
9343 }
9344
9345cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02009346 lyxp_set_free_content(&orig_set);
9347 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009348 return rc;
9349}
9350
9351/**
9352 * @brief Evaluate OrExpr. Logs directly on error.
9353 *
Michal Vaskod3678892020-05-21 10:06:58 +02009354 * [13] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02009355 *
9356 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02009357 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009358 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02009359 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009360 * @param[in] options XPath options.
9361 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
9362 */
9363static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02009364eval_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 +02009365{
9366 LY_ERR rc;
9367 struct lyxp_set orig_set, set2;
9368 uint16_t i;
9369
9370 assert(repeat);
9371
9372 set_init(&orig_set, set);
9373 set_init(&set2, set);
9374
9375 set_fill_set(&orig_set, set);
9376
Michal Vasko004d3152020-06-11 19:59:22 +02009377 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009378 LY_CHECK_GOTO(rc, cleanup);
9379
9380 /* cast to boolean, we know that will be the final result */
aPiecek8b0cc152021-05-31 16:40:31 +02009381 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02009382 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009383 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009384 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009385 }
9386
9387 /* ('or' AndExpr)* */
9388 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02009389 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG);
aPiecek8b0cc152021-05-31 16:40:31 +02009390 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, ((options & LYXP_SKIP_EXPR) || set->val.bln ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02009391 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02009392 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009393
9394 /* lazy evaluation */
aPiecek8b0cc152021-05-31 16:40:31 +02009395 if ((options & LYXP_SKIP_EXPR) || ((set->type == LYXP_SET_BOOLEAN) && set->val.bln)) {
9396 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009397 LY_CHECK_GOTO(rc, cleanup);
9398 continue;
9399 }
9400
9401 set_fill_set(&set2, &orig_set);
9402 /* expr_type cound have been LYXP_EXPR_NONE in all these later calls (except for the first one),
9403 * but it does not matter */
Michal Vasko004d3152020-06-11 19:59:22 +02009404 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009405 LY_CHECK_GOTO(rc, cleanup);
9406
9407 /* eval - just get boolean value actually */
9408 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vasko1a09b212021-05-06 13:00:10 +02009409 set_scnode_clear_ctx(&set2, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskoecd62de2019-11-13 12:35:11 +01009410 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009411 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009412 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009413 set_fill_set(set, &set2);
9414 }
9415 }
9416
9417cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02009418 lyxp_set_free_content(&orig_set);
9419 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009420 return rc;
9421}
9422
9423/**
Michal Vasko004d3152020-06-11 19:59:22 +02009424 * @brief Decide what expression is at the pointer @p tok_idx and evaluate it accordingly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009425 *
9426 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02009427 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009428 * @param[in] etype Expression type to evaluate.
aPiecek8b0cc152021-05-31 16:40:31 +02009429 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009430 * @param[in] options XPath options.
9431 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
9432 */
9433static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02009434eval_expr_select(const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_expr_type etype, struct lyxp_set *set,
9435 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02009436{
9437 uint16_t i, count;
9438 enum lyxp_expr_type next_etype;
9439 LY_ERR rc;
9440
9441 /* process operator repeats */
Michal Vasko004d3152020-06-11 19:59:22 +02009442 if (!exp->repeat[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02009443 next_etype = LYXP_EXPR_NONE;
9444 } else {
9445 /* find etype repeat */
Radek Krejci1e008d22020-08-17 11:37:37 +02009446 for (i = 0; exp->repeat[*tok_idx][i] > etype; ++i) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02009447
9448 /* select one-priority lower because etype expression called us */
9449 if (i) {
Michal Vasko004d3152020-06-11 19:59:22 +02009450 next_etype = exp->repeat[*tok_idx][i - 1];
Michal Vasko03ff5a72019-09-11 13:49:33 +02009451 /* count repeats for that expression */
Radek Krejci1e008d22020-08-17 11:37:37 +02009452 for (count = 0; i && exp->repeat[*tok_idx][i - 1] == next_etype; ++count, --i) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02009453 } else {
9454 next_etype = LYXP_EXPR_NONE;
9455 }
9456 }
9457
9458 /* decide what expression are we parsing based on the repeat */
9459 switch (next_etype) {
9460 case LYXP_EXPR_OR:
Michal Vasko004d3152020-06-11 19:59:22 +02009461 rc = eval_or_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009462 break;
9463 case LYXP_EXPR_AND:
Michal Vasko004d3152020-06-11 19:59:22 +02009464 rc = eval_and_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009465 break;
9466 case LYXP_EXPR_EQUALITY:
Michal Vasko004d3152020-06-11 19:59:22 +02009467 rc = eval_equality_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009468 break;
9469 case LYXP_EXPR_RELATIONAL:
Michal Vasko004d3152020-06-11 19:59:22 +02009470 rc = eval_relational_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009471 break;
9472 case LYXP_EXPR_ADDITIVE:
Michal Vasko004d3152020-06-11 19:59:22 +02009473 rc = eval_additive_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009474 break;
9475 case LYXP_EXPR_MULTIPLICATIVE:
Michal Vasko004d3152020-06-11 19:59:22 +02009476 rc = eval_multiplicative_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009477 break;
9478 case LYXP_EXPR_UNARY:
Michal Vasko004d3152020-06-11 19:59:22 +02009479 rc = eval_unary_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009480 break;
9481 case LYXP_EXPR_UNION:
Michal Vasko004d3152020-06-11 19:59:22 +02009482 rc = eval_union_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009483 break;
9484 case LYXP_EXPR_NONE:
Michal Vasko004d3152020-06-11 19:59:22 +02009485 rc = eval_path_expr(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009486 break;
9487 default:
9488 LOGINT_RET(set->ctx);
9489 }
9490
9491 return rc;
9492}
9493
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009494/**
9495 * @brief Get root type.
9496 *
9497 * @param[in] ctx_node Context node.
9498 * @param[in] ctx_scnode Schema context node.
9499 * @param[in] options XPath options.
9500 * @return Root type.
9501 */
9502static enum lyxp_node_type
Radek Krejci1deb5be2020-08-26 16:43:36 +02009503lyxp_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 +01009504{
Michal Vasko6b26e742020-07-17 15:02:10 +02009505 const struct lysc_node *op;
9506
Michal Vaskoa27245c2022-05-02 09:01:35 +02009507 /* explicit */
9508 if (options & LYXP_ACCESS_TREE_ALL) {
9509 return LYXP_NODE_ROOT;
9510 } else if (options & LYXP_ACCESS_TREE_CONFIG) {
9511 return LYXP_NODE_ROOT_CONFIG;
9512 }
9513
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009514 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009515 /* schema */
Radek Krejci1e008d22020-08-17 11:37:37 +02009516 for (op = ctx_scnode; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent) {}
Michal Vasko6b26e742020-07-17 15:02:10 +02009517
9518 if (op || (options & LYXP_SCNODE)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009519 /* general root that can access everything */
9520 return LYXP_NODE_ROOT;
9521 } else if (!ctx_scnode || (ctx_scnode->flags & LYS_CONFIG_W)) {
9522 /* root context node can access only config data (because we said so, it is unspecified) */
9523 return LYXP_NODE_ROOT_CONFIG;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009524 }
Michal Vasko6b26e742020-07-17 15:02:10 +02009525 return LYXP_NODE_ROOT;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009526 }
9527
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009528 /* data */
Michal Vasko6b26e742020-07-17 15:02:10 +02009529 op = ctx_node ? ctx_node->schema : NULL;
Michal Vaskod989ba02020-08-24 10:59:24 +02009530 for ( ; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent) {}
Michal Vasko6b26e742020-07-17 15:02:10 +02009531
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009532 if (op || !(options & LYXP_SCHEMA)) {
9533 /* general root that can access everything */
9534 return LYXP_NODE_ROOT;
Christian Hoppsb6ecaea2021-02-06 09:45:38 -05009535 } else if (!ctx_node || !ctx_node->schema || (ctx_node->schema->flags & LYS_CONFIG_W)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009536 /* root context node can access only config data (because we said so, it is unspecified) */
9537 return LYXP_NODE_ROOT_CONFIG;
9538 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009539 return LYXP_NODE_ROOT;
9540}
9541
Michal Vasko03ff5a72019-09-11 13:49:33 +02009542LY_ERR
Michal Vasko400e9672021-01-11 13:39:17 +01009543lyxp_eval(const struct ly_ctx *ctx, const struct lyxp_expr *exp, const struct lys_module *cur_mod,
Michal Vaskoa3e92bc2022-07-29 14:56:23 +02009544 LY_VALUE_FORMAT format, void *prefix_data, const struct lyd_node *cur_node, const struct lyd_node *ctx_node,
9545 const struct lyd_node *tree, const struct lyxp_var *vars, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02009546{
Michal Vasko004d3152020-06-11 19:59:22 +02009547 uint16_t tok_idx = 0;
Michal Vaskoddd76592022-01-17 13:34:48 +01009548 const struct lysc_node *snode;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009549 LY_ERR rc;
9550
Michal Vasko400e9672021-01-11 13:39:17 +01009551 LY_CHECK_ARG_RET(ctx, ctx, exp, set, LY_EINVAL);
Radek Krejci8df109d2021-04-23 12:19:08 +02009552 if (!cur_mod && ((format == LY_VALUE_SCHEMA) || (format == LY_VALUE_SCHEMA_RESOLVED))) {
Michal Vaskoddd76592022-01-17 13:34:48 +01009553 LOGERR(ctx, LY_EINVAL, "Current module must be set if schema format is used.");
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009554 return LY_EINVAL;
Michal Vasko004d3152020-06-11 19:59:22 +02009555 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02009556
Michal Vaskod3bb12f2020-12-04 14:33:09 +01009557 if (tree) {
9558 /* adjust the pointer to be the first top-level sibling */
9559 while (tree->parent) {
9560 tree = lyd_parent(tree);
9561 }
9562 tree = lyd_first_sibling(tree);
Michal Vaskoddd76592022-01-17 13:34:48 +01009563
9564 for (snode = tree->schema->parent; snode && (snode->nodetype & (LYS_CASE | LYS_CHOICE)); snode = snode->parent) {}
9565 if (snode) {
9566 /* unable to evaluate absolute paths */
9567 LOGERR(ctx, LY_EINVAL, "Data node \"%s\" has no parent but is not instance of a top-level schema node.",
9568 LYD_NAME(tree));
9569 return LY_EINVAL;
9570 }
Michal Vaskod3bb12f2020-12-04 14:33:09 +01009571 }
9572
Michal Vasko03ff5a72019-09-11 13:49:33 +02009573 /* prepare set for evaluation */
Michal Vasko03ff5a72019-09-11 13:49:33 +02009574 memset(set, 0, sizeof *set);
Michal Vaskod3678892020-05-21 10:06:58 +02009575 set->type = LYXP_SET_NODE_SET;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009576 set->root_type = lyxp_get_root_type(ctx_node, NULL, options);
9577 set_insert_node(set, (struct lyd_node *)ctx_node, 0, ctx_node ? LYXP_NODE_ELEM : set->root_type, 0);
9578
Michal Vasko400e9672021-01-11 13:39:17 +01009579 set->ctx = (struct ly_ctx *)ctx;
Michal Vaskoa3e92bc2022-07-29 14:56:23 +02009580 set->cur_node = cur_node;
9581 for (set->context_op = cur_node ? cur_node->schema : NULL;
Radek Krejci0f969882020-08-21 16:56:47 +02009582 set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF));
9583 set->context_op = set->context_op->parent) {}
Michal Vaskof03ed032020-03-04 13:31:44 +01009584 set->tree = tree;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009585 set->cur_mod = cur_mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009586 set->format = format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009587 set->prefix_data = prefix_data;
aPiecekfba75362021-10-07 12:39:48 +02009588 set->vars = vars;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009589
Radek Krejciddace2c2021-01-08 11:30:56 +01009590 LOG_LOCSET(NULL, set->cur_node, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01009591
Michal Vasko03ff5a72019-09-11 13:49:33 +02009592 /* evaluate */
Michal Vasko004d3152020-06-11 19:59:22 +02009593 rc = eval_expr_select(exp, &tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009594 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02009595 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009596 }
9597
Michal Vasko4a7d4d62021-12-13 17:05:06 +01009598 if (set->cur_node) {
9599 LOG_LOCBACK(0, 1, 0, 0);
9600 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02009601 return rc;
9602}
9603
9604#if 0
9605
9606/* full xml printing of set elements, not used currently */
9607
9608void
9609lyxp_set_print_xml(FILE *f, struct lyxp_set *set)
9610{
9611 uint32_t i;
9612 char *str_num;
9613 struct lyout out;
9614
9615 memset(&out, 0, sizeof out);
9616
9617 out.type = LYOUT_STREAM;
9618 out.method.f = f;
9619
9620 switch (set->type) {
9621 case LYXP_SET_EMPTY:
Michal Vasko5233e962020-08-14 14:26:20 +02009622 ly_print_(&out, "Empty XPath set\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02009623 break;
9624 case LYXP_SET_BOOLEAN:
Michal Vasko5233e962020-08-14 14:26:20 +02009625 ly_print_(&out, "Boolean XPath set:\n");
9626 ly_print_(&out, "%s\n\n", set->value.bool ? "true" : "false");
Michal Vasko03ff5a72019-09-11 13:49:33 +02009627 break;
9628 case LYXP_SET_STRING:
Michal Vasko5233e962020-08-14 14:26:20 +02009629 ly_print_(&out, "String XPath set:\n");
9630 ly_print_(&out, "\"%s\"\n\n", set->value.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009631 break;
9632 case LYXP_SET_NUMBER:
Michal Vasko5233e962020-08-14 14:26:20 +02009633 ly_print_(&out, "Number XPath set:\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02009634
9635 if (isnan(set->value.num)) {
9636 str_num = strdup("NaN");
9637 } else if ((set->value.num == 0) || (set->value.num == -0.0f)) {
9638 str_num = strdup("0");
9639 } else if (isinf(set->value.num) && !signbit(set->value.num)) {
9640 str_num = strdup("Infinity");
9641 } else if (isinf(set->value.num) && signbit(set->value.num)) {
9642 str_num = strdup("-Infinity");
9643 } else if ((long long)set->value.num == set->value.num) {
9644 if (asprintf(&str_num, "%lld", (long long)set->value.num) == -1) {
9645 str_num = NULL;
9646 }
9647 } else {
9648 if (asprintf(&str_num, "%03.1Lf", set->value.num) == -1) {
9649 str_num = NULL;
9650 }
9651 }
9652 if (!str_num) {
9653 LOGMEM;
9654 return;
9655 }
Michal Vasko5233e962020-08-14 14:26:20 +02009656 ly_print_(&out, "%s\n\n", str_num);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009657 free(str_num);
9658 break;
9659 case LYXP_SET_NODE_SET:
Michal Vasko5233e962020-08-14 14:26:20 +02009660 ly_print_(&out, "Node XPath set:\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02009661
9662 for (i = 0; i < set->used; ++i) {
Michal Vasko5233e962020-08-14 14:26:20 +02009663 ly_print_(&out, "%d. ", i + 1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009664 switch (set->node_type[i]) {
9665 case LYXP_NODE_ROOT_ALL:
Michal Vasko5233e962020-08-14 14:26:20 +02009666 ly_print_(&out, "ROOT all\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02009667 break;
9668 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko5233e962020-08-14 14:26:20 +02009669 ly_print_(&out, "ROOT config\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02009670 break;
9671 case LYXP_NODE_ROOT_STATE:
Michal Vasko5233e962020-08-14 14:26:20 +02009672 ly_print_(&out, "ROOT state\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02009673 break;
9674 case LYXP_NODE_ROOT_NOTIF:
Michal Vasko5233e962020-08-14 14:26:20 +02009675 ly_print_(&out, "ROOT notification \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009676 break;
9677 case LYXP_NODE_ROOT_RPC:
Michal Vasko5233e962020-08-14 14:26:20 +02009678 ly_print_(&out, "ROOT rpc \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009679 break;
9680 case LYXP_NODE_ROOT_OUTPUT:
Michal Vasko5233e962020-08-14 14:26:20 +02009681 ly_print_(&out, "ROOT output \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009682 break;
9683 case LYXP_NODE_ELEM:
Michal Vasko5233e962020-08-14 14:26:20 +02009684 ly_print_(&out, "ELEM \"%s\"\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009685 xml_print_node(&out, 1, set->value.nodes[i], 1, LYP_FORMAT);
Michal Vasko5233e962020-08-14 14:26:20 +02009686 ly_print_(&out, "\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02009687 break;
9688 case LYXP_NODE_TEXT:
Michal Vasko5233e962020-08-14 14:26:20 +02009689 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 +02009690 break;
9691 case LYXP_NODE_ATTR:
Michal Vasko5233e962020-08-14 14:26:20 +02009692 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 +02009693 break;
9694 }
9695 }
9696 break;
9697 }
9698}
9699
9700#endif
9701
9702LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009703lyxp_set_cast(struct lyxp_set *set, enum lyxp_set_type target)
Michal Vasko03ff5a72019-09-11 13:49:33 +02009704{
9705 long double num;
9706 char *str;
9707 LY_ERR rc;
9708
9709 if (!set || (set->type == target)) {
9710 return LY_SUCCESS;
9711 }
9712
9713 /* it's not possible to convert anything into a node set */
Michal Vaskod3678892020-05-21 10:06:58 +02009714 assert(target != LYXP_SET_NODE_SET);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009715
9716 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vaskod3678892020-05-21 10:06:58 +02009717 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009718 return LY_EINVAL;
9719 }
9720
9721 /* to STRING */
Michal Vaskod3678892020-05-21 10:06:58 +02009722 if ((target == LYXP_SET_STRING) || ((target == LYXP_SET_NUMBER) && (set->type == LYXP_SET_NODE_SET))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02009723 switch (set->type) {
9724 case LYXP_SET_NUMBER:
9725 if (isnan(set->val.num)) {
9726 set->val.str = strdup("NaN");
9727 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
9728 } else if ((set->val.num == 0) || (set->val.num == -0.0f)) {
9729 set->val.str = strdup("0");
9730 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
9731 } else if (isinf(set->val.num) && !signbit(set->val.num)) {
9732 set->val.str = strdup("Infinity");
9733 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
9734 } else if (isinf(set->val.num) && signbit(set->val.num)) {
9735 set->val.str = strdup("-Infinity");
9736 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
9737 } else if ((long long)set->val.num == set->val.num) {
9738 if (asprintf(&str, "%lld", (long long)set->val.num) == -1) {
9739 LOGMEM_RET(set->ctx);
9740 }
9741 set->val.str = str;
9742 } else {
9743 if (asprintf(&str, "%03.1Lf", set->val.num) == -1) {
9744 LOGMEM_RET(set->ctx);
9745 }
9746 set->val.str = str;
9747 }
9748 break;
9749 case LYXP_SET_BOOLEAN:
Michal Vasko004d3152020-06-11 19:59:22 +02009750 if (set->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02009751 set->val.str = strdup("true");
9752 } else {
9753 set->val.str = strdup("false");
9754 }
9755 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), LY_EMEM);
9756 break;
9757 case LYXP_SET_NODE_SET:
Michal Vasko03ff5a72019-09-11 13:49:33 +02009758 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009759 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02009760
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009761 rc = cast_node_set_to_string(set, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009762 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02009763 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009764 set->val.str = str;
9765 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009766 default:
9767 LOGINT_RET(set->ctx);
9768 }
9769 set->type = LYXP_SET_STRING;
9770 }
9771
9772 /* to NUMBER */
9773 if (target == LYXP_SET_NUMBER) {
9774 switch (set->type) {
9775 case LYXP_SET_STRING:
9776 num = cast_string_to_number(set->val.str);
Michal Vaskod3678892020-05-21 10:06:58 +02009777 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009778 set->val.num = num;
9779 break;
9780 case LYXP_SET_BOOLEAN:
Michal Vasko004d3152020-06-11 19:59:22 +02009781 if (set->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02009782 set->val.num = 1;
9783 } else {
9784 set->val.num = 0;
9785 }
9786 break;
9787 default:
9788 LOGINT_RET(set->ctx);
9789 }
9790 set->type = LYXP_SET_NUMBER;
9791 }
9792
9793 /* to BOOLEAN */
9794 if (target == LYXP_SET_BOOLEAN) {
9795 switch (set->type) {
9796 case LYXP_SET_NUMBER:
9797 if ((set->val.num == 0) || (set->val.num == -0.0f) || isnan(set->val.num)) {
Michal Vasko004d3152020-06-11 19:59:22 +02009798 set->val.bln = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009799 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02009800 set->val.bln = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009801 }
9802 break;
9803 case LYXP_SET_STRING:
9804 if (set->val.str[0]) {
Michal Vaskod3678892020-05-21 10:06:58 +02009805 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02009806 set->val.bln = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009807 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02009808 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02009809 set->val.bln = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009810 }
9811 break;
9812 case LYXP_SET_NODE_SET:
Michal Vaskod3678892020-05-21 10:06:58 +02009813 if (set->used) {
9814 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02009815 set->val.bln = 1;
Michal Vaskod3678892020-05-21 10:06:58 +02009816 } else {
9817 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02009818 set->val.bln = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02009819 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02009820 break;
9821 default:
9822 LOGINT_RET(set->ctx);
9823 }
9824 set->type = LYXP_SET_BOOLEAN;
9825 }
9826
Michal Vasko03ff5a72019-09-11 13:49:33 +02009827 return LY_SUCCESS;
9828}
9829
9830LY_ERR
Michal Vasko400e9672021-01-11 13:39:17 +01009831lyxp_atomize(const struct ly_ctx *ctx, const struct lyxp_expr *exp, const struct lys_module *cur_mod,
Michal Vaskoa3e92bc2022-07-29 14:56:23 +02009832 LY_VALUE_FORMAT format, void *prefix_data, const struct lysc_node *cur_scnode,
9833 const struct lysc_node *ctx_scnode, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02009834{
Radek Krejci2efc45b2020-12-22 16:25:44 +01009835 LY_ERR ret;
Michal Vasko004d3152020-06-11 19:59:22 +02009836 uint16_t tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009837
Michal Vasko400e9672021-01-11 13:39:17 +01009838 LY_CHECK_ARG_RET(ctx, ctx, exp, set, LY_EINVAL);
Radek Krejci8df109d2021-04-23 12:19:08 +02009839 if (!cur_mod && ((format == LY_VALUE_SCHEMA) || (format == LY_VALUE_SCHEMA_RESOLVED))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009840 LOGARG(NULL, "Current module must be set if schema format is used.");
9841 return LY_EINVAL;
Michal Vasko004d3152020-06-11 19:59:22 +02009842 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02009843
9844 /* prepare set for evaluation */
Michal Vasko03ff5a72019-09-11 13:49:33 +02009845 memset(set, 0, sizeof *set);
9846 set->type = LYXP_SET_SCNODE_SET;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009847 set->root_type = lyxp_get_root_type(NULL, ctx_scnode, options);
Michal Vasko7333cb32022-07-29 16:30:29 +02009848 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 +01009849 set->val.scnodes[0].in_ctx = LYXP_SET_SCNODE_START;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009850
Michal Vasko400e9672021-01-11 13:39:17 +01009851 set->ctx = (struct ly_ctx *)ctx;
Michal Vaskoa3e92bc2022-07-29 14:56:23 +02009852 set->cur_scnode = cur_scnode;
9853 for (set->context_op = cur_scnode;
Radek Krejci0f969882020-08-21 16:56:47 +02009854 set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF));
9855 set->context_op = set->context_op->parent) {}
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009856 set->cur_mod = cur_mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009857 set->format = format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009858 set->prefix_data = prefix_data;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009859
Radek Krejciddace2c2021-01-08 11:30:56 +01009860 LOG_LOCSET(set->cur_scnode, NULL, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01009861
Michal Vasko03ff5a72019-09-11 13:49:33 +02009862 /* evaluate */
Radek Krejci2efc45b2020-12-22 16:25:44 +01009863 ret = eval_expr_select(exp, &tok_idx, 0, set, options);
9864
Radek Krejciddace2c2021-01-08 11:30:56 +01009865 LOG_LOCBACK(1, 0, 0, 0);
Radek Krejci2efc45b2020-12-22 16:25:44 +01009866 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009867}
Michal Vaskod43d71a2020-08-07 14:54:58 +02009868
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01009869LIBYANG_API_DEF const char *
Michal Vaskod43d71a2020-08-07 14:54:58 +02009870lyxp_get_expr(const struct lyxp_expr *path)
9871{
9872 if (!path) {
9873 return NULL;
9874 }
9875
9876 return path->expr;
9877}