blob: 6b3ea51a1935eab46a0864dc92b60822dbde95b1 [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
aPiecekbf968d92021-05-27 14:35:05 +020045static 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 +020046static LY_ERR eval_expr_select(const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_expr_type etype,
47 struct lyxp_set *set, uint32_t options);
Michal Vasko93923692021-05-07 15:28:02 +020048static LY_ERR moveto_resolve_model(const char **qname, uint16_t *qname_len, const struct lyxp_set *set,
49 const struct lysc_node *ctx_scnode, const struct lys_module **moveto_mod);
Michal Vasko47da6562022-07-14 15:43:15 +020050static LY_ERR moveto_axis_node_next(const struct lyd_node **iter, enum lyxp_node_type *iter_type,
51 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 +020052static LY_ERR moveto_node(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname,
53 enum lyxp_axis axis, uint32_t options);
54static LY_ERR moveto_scnode(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname,
55 enum lyxp_axis axis, uint32_t options);
Michal Vasko8abcecc2022-07-28 09:55:01 +020056static LY_ERR moveto_op_comp(struct lyxp_set *set1, struct lyxp_set *set2, const char *op);
Michal Vasko03ff5a72019-09-11 13:49:33 +020057
aPiecek96dc1e32021-10-08 15:45:59 +020058/* Functions are divided into the following basic classes:
59 *
60 * (re)parse functions:
61 * Parse functions parse the expression into
62 * tokens (syntactic analysis).
63 * Reparse functions perform semantic analysis
64 * (do not save the result, just a check) of
65 * the expression and fill repeat indices.
66 *
67 * warn functions:
68 * Warn functions check specific reasonable conditions for schema XPath
69 * and print a warning if they are not satisfied.
70 *
71 * moveto functions:
72 * They and only they actually change the context (set).
73 *
74 * eval functions:
75 * They execute a parsed XPath expression on some data subtree.
76 */
77
Michal Vasko03ff5a72019-09-11 13:49:33 +020078/**
79 * @brief Print the type of an XPath \p set.
80 *
81 * @param[in] set Set to use.
82 * @return Set type string.
83 */
84static const char *
85print_set_type(struct lyxp_set *set)
86{
87 switch (set->type) {
Michal Vasko03ff5a72019-09-11 13:49:33 +020088 case LYXP_SET_NODE_SET:
89 return "node set";
90 case LYXP_SET_SCNODE_SET:
91 return "schema node set";
92 case LYXP_SET_BOOLEAN:
93 return "boolean";
94 case LYXP_SET_NUMBER:
95 return "number";
96 case LYXP_SET_STRING:
97 return "string";
98 }
99
100 return NULL;
101}
102
Michal Vasko24cddf82020-06-01 08:17:01 +0200103const char *
Michal Vasko49fec8e2022-05-24 10:28:33 +0200104lyxp_token2str(enum lyxp_token tok)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200105{
106 switch (tok) {
107 case LYXP_TOKEN_PAR1:
108 return "(";
109 case LYXP_TOKEN_PAR2:
110 return ")";
111 case LYXP_TOKEN_BRACK1:
112 return "[";
113 case LYXP_TOKEN_BRACK2:
114 return "]";
115 case LYXP_TOKEN_DOT:
116 return ".";
117 case LYXP_TOKEN_DDOT:
118 return "..";
119 case LYXP_TOKEN_AT:
120 return "@";
121 case LYXP_TOKEN_COMMA:
122 return ",";
123 case LYXP_TOKEN_NAMETEST:
124 return "NameTest";
125 case LYXP_TOKEN_NODETYPE:
126 return "NodeType";
aPiecekfba75362021-10-07 12:39:48 +0200127 case LYXP_TOKEN_VARREF:
128 return "VariableReference";
Michal Vasko03ff5a72019-09-11 13:49:33 +0200129 case LYXP_TOKEN_FUNCNAME:
130 return "FunctionName";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200131 case LYXP_TOKEN_OPER_LOG:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200132 return "Operator(Logic)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200133 case LYXP_TOKEN_OPER_EQUAL:
134 return "Operator(Equal)";
135 case LYXP_TOKEN_OPER_NEQUAL:
136 return "Operator(Non-equal)";
137 case LYXP_TOKEN_OPER_COMP:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200138 return "Operator(Comparison)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200139 case LYXP_TOKEN_OPER_MATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200140 return "Operator(Math)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200141 case LYXP_TOKEN_OPER_UNI:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200142 return "Operator(Union)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200143 case LYXP_TOKEN_OPER_PATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200144 return "Operator(Path)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200145 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko14676352020-05-29 11:35:55 +0200146 return "Operator(Recursive Path)";
Michal Vasko03ff5a72019-09-11 13:49:33 +0200147 case LYXP_TOKEN_LITERAL:
148 return "Literal";
149 case LYXP_TOKEN_NUMBER:
150 return "Number";
151 default:
152 LOGINT(NULL);
153 return "";
154 }
155}
156
157/**
Michal Vasko49fec8e2022-05-24 10:28:33 +0200158 * @brief Transform string into an axis.
159 *
160 * @param[in] str String to transform.
161 * @param[in] str_len Length of @p str.
162 * @return Transformed axis.
163 */
164static enum lyxp_axis
165str2axis(const char *str, uint16_t str_len)
166{
167 switch (str_len) {
168 case 4:
169 assert(!strncmp("self", str, str_len));
170 return LYXP_AXIS_SELF;
171 case 5:
172 assert(!strncmp("child", str, str_len));
173 return LYXP_AXIS_CHILD;
174 case 6:
175 assert(!strncmp("parent", str, str_len));
176 return LYXP_AXIS_PARENT;
177 case 8:
178 assert(!strncmp("ancestor", str, str_len));
179 return LYXP_AXIS_ANCESTOR;
180 case 9:
181 if (str[0] == 'a') {
182 assert(!strncmp("attribute", str, str_len));
183 return LYXP_AXIS_ATTRIBUTE;
184 } else if (str[0] == 'f') {
185 assert(!strncmp("following", str, str_len));
186 return LYXP_AXIS_FOLLOWING;
187 } else {
188 assert(!strncmp("preceding", str, str_len));
189 return LYXP_AXIS_PRECEDING;
190 }
191 break;
192 case 10:
193 assert(!strncmp("descendant", str, str_len));
194 return LYXP_AXIS_DESCENDANT;
195 case 16:
196 assert(!strncmp("ancestor-or-self", str, str_len));
197 return LYXP_AXIS_ANCESTOR_OR_SELF;
198 case 17:
199 if (str[0] == 'f') {
200 assert(!strncmp("following-sibling", str, str_len));
201 return LYXP_AXIS_FOLLOWING_SIBLING;
202 } else {
203 assert(!strncmp("preceding-sibling", str, str_len));
204 return LYXP_AXIS_PRECEDING_SIBLING;
205 }
206 break;
207 case 18:
208 assert(!strncmp("descendant-or-self", str, str_len));
209 return LYXP_AXIS_DESCENDANT_OR_SELF;
210 }
211
212 LOGINT(NULL);
213 return 0;
214}
215
216/**
Michal Vasko03ff5a72019-09-11 13:49:33 +0200217 * @brief Print the whole expression \p exp to debug output.
218 *
219 * @param[in] exp Expression to use.
220 */
221static void
Michal Vasko40308e72020-10-20 16:38:40 +0200222print_expr_struct_debug(const struct lyxp_expr *exp)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200223{
Radek Krejcif13b87b2020-12-01 22:02:17 +0100224#define MSG_BUFFER_SIZE 128
225 char tmp[MSG_BUFFER_SIZE];
Michal Vasko1fdd8fa2021-01-08 09:21:45 +0100226 uint32_t i, j;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200227
Radek Krejci52b6d512020-10-12 12:33:17 +0200228 if (!exp || (ly_ll < LY_LLDBG)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200229 return;
230 }
231
232 LOGDBG(LY_LDGXPATH, "expression \"%s\":", exp->expr);
233 for (i = 0; i < exp->used; ++i) {
Michal Vasko49fec8e2022-05-24 10:28:33 +0200234 sprintf(tmp, "\ttoken %s, in expression \"%.*s\"", lyxp_token2str(exp->tokens[i]), exp->tok_len[i],
Michal Vasko69730152020-10-09 16:30:07 +0200235 &exp->expr[exp->tok_pos[i]]);
Michal Vasko23049552021-03-04 15:57:44 +0100236 if (exp->repeat && exp->repeat[i]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200237 sprintf(tmp + strlen(tmp), " (repeat %d", exp->repeat[i][0]);
238 for (j = 1; exp->repeat[i][j]; ++j) {
239 sprintf(tmp + strlen(tmp), ", %d", exp->repeat[i][j]);
240 }
241 strcat(tmp, ")");
242 }
243 LOGDBG(LY_LDGXPATH, tmp);
244 }
Radek Krejcif13b87b2020-12-01 22:02:17 +0100245#undef MSG_BUFFER_SIZE
Michal Vasko03ff5a72019-09-11 13:49:33 +0200246}
247
248#ifndef NDEBUG
249
250/**
251 * @brief Print XPath set content to debug output.
252 *
253 * @param[in] set Set to print.
254 */
255static void
256print_set_debug(struct lyxp_set *set)
257{
258 uint32_t i;
259 char *str;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200260 struct lyxp_set_node *item;
261 struct lyxp_set_scnode *sitem;
262
Radek Krejci52b6d512020-10-12 12:33:17 +0200263 if (ly_ll < LY_LLDBG) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200264 return;
265 }
266
267 switch (set->type) {
268 case LYXP_SET_NODE_SET:
269 LOGDBG(LY_LDGXPATH, "set NODE SET:");
270 for (i = 0; i < set->used; ++i) {
271 item = &set->val.nodes[i];
272
273 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +0100274 case LYXP_NODE_NONE:
275 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): NONE", i + 1, item->pos);
276 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200277 case LYXP_NODE_ROOT:
278 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT", i + 1, item->pos);
279 break;
280 case LYXP_NODE_ROOT_CONFIG:
281 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT CONFIG", i + 1, item->pos);
282 break;
283 case LYXP_NODE_ELEM:
Michal Vasko9e685082021-01-29 14:49:09 +0100284 if ((item->node->schema->nodetype == LYS_LIST) && (lyd_child(item->node)->schema->nodetype == LYS_LEAF)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200285 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (1st child val: %s)", i + 1, item->pos,
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200286 item->node->schema->name, lyd_get_value(lyd_child(item->node)));
Michal Vasko9e685082021-01-29 14:49:09 +0100287 } else if (item->node->schema->nodetype == LYS_LEAFLIST) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200288 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (val: %s)", i + 1, item->pos,
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200289 item->node->schema->name, lyd_get_value(item->node));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200290 } else {
291 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s", i + 1, item->pos, item->node->schema->name);
292 }
293 break;
294 case LYXP_NODE_TEXT:
295 if (item->node->schema->nodetype & LYS_ANYDATA) {
296 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): TEXT <%s>", i + 1, item->pos,
Michal Vasko69730152020-10-09 16:30:07 +0200297 item->node->schema->nodetype == LYS_ANYXML ? "anyxml" : "anydata");
Michal Vasko03ff5a72019-09-11 13:49:33 +0200298 } else {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200299 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 +0200300 }
301 break;
Michal Vasko9f96a052020-03-10 09:41:45 +0100302 case LYXP_NODE_META:
303 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 +0200304 set->val.meta[i].meta->value);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200305 break;
306 }
307 }
308 break;
309
310 case LYXP_SET_SCNODE_SET:
311 LOGDBG(LY_LDGXPATH, "set SCNODE SET:");
312 for (i = 0; i < set->used; ++i) {
313 sitem = &set->val.scnodes[i];
314
315 switch (sitem->type) {
316 case LYXP_NODE_ROOT:
317 LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT", i + 1, sitem->in_ctx);
318 break;
319 case LYXP_NODE_ROOT_CONFIG:
320 LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT CONFIG", i + 1, sitem->in_ctx);
321 break;
322 case LYXP_NODE_ELEM:
323 LOGDBG(LY_LDGXPATH, "\t%d (%u): ELEM %s", i + 1, sitem->in_ctx, sitem->scnode->name);
324 break;
325 default:
326 LOGINT(NULL);
327 break;
328 }
329 }
330 break;
331
Michal Vasko03ff5a72019-09-11 13:49:33 +0200332 case LYXP_SET_BOOLEAN:
333 LOGDBG(LY_LDGXPATH, "set BOOLEAN");
Michal Vasko004d3152020-06-11 19:59:22 +0200334 LOGDBG(LY_LDGXPATH, "\t%s", (set->val.bln ? "true" : "false"));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200335 break;
336
337 case LYXP_SET_STRING:
338 LOGDBG(LY_LDGXPATH, "set STRING");
339 LOGDBG(LY_LDGXPATH, "\t%s", set->val.str);
340 break;
341
342 case LYXP_SET_NUMBER:
343 LOGDBG(LY_LDGXPATH, "set NUMBER");
344
345 if (isnan(set->val.num)) {
346 str = strdup("NaN");
347 } else if ((set->val.num == 0) || (set->val.num == -0.0f)) {
348 str = strdup("0");
349 } else if (isinf(set->val.num) && !signbit(set->val.num)) {
350 str = strdup("Infinity");
351 } else if (isinf(set->val.num) && signbit(set->val.num)) {
352 str = strdup("-Infinity");
353 } else if ((long long)set->val.num == set->val.num) {
354 if (asprintf(&str, "%lld", (long long)set->val.num) == -1) {
355 str = NULL;
356 }
357 } else {
358 if (asprintf(&str, "%03.1Lf", set->val.num) == -1) {
359 str = NULL;
360 }
361 }
362 LY_CHECK_ERR_RET(!str, LOGMEM(NULL), );
363
364 LOGDBG(LY_LDGXPATH, "\t%s", str);
365 free(str);
366 }
367}
368
369#endif
370
371/**
372 * @brief Realloc the string \p str.
373 *
374 * @param[in] ctx libyang context for logging.
375 * @param[in] needed How much free space is required.
376 * @param[in,out] str Pointer to the string to use.
377 * @param[in,out] used Used bytes in \p str.
378 * @param[in,out] size Allocated bytes in \p str.
379 * @return LY_ERR
380 */
381static LY_ERR
Michal Vasko52927e22020-03-16 17:26:14 +0100382cast_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 +0200383{
384 if (*size - *used < needed) {
385 do {
386 if ((UINT16_MAX - *size) < LYXP_STRING_CAST_SIZE_STEP) {
387 LOGERR(ctx, LY_EINVAL, "XPath string length limit (%u) reached.", UINT16_MAX);
388 return LY_EINVAL;
389 }
390 *size += LYXP_STRING_CAST_SIZE_STEP;
391 } while (*size - *used < needed);
392 *str = ly_realloc(*str, *size * sizeof(char));
393 LY_CHECK_ERR_RET(!(*str), LOGMEM(ctx), LY_EMEM);
394 }
395
396 return LY_SUCCESS;
397}
398
399/**
400 * @brief Cast nodes recursively to one string @p str.
401 *
Michal Vasko47da6562022-07-14 15:43:15 +0200402 * @param[in] node Node to cast, NULL if root.
403 * @param[in] set XPath set.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200404 * @param[in] indent Current indent.
405 * @param[in,out] str Resulting string.
406 * @param[in,out] used Used bytes in @p str.
407 * @param[in,out] size Allocated bytes in @p str.
Michal Vasko47da6562022-07-14 15:43:15 +0200408 * @return LY_ERR value.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200409 */
410static LY_ERR
Michal Vasko47da6562022-07-14 15:43:15 +0200411cast_string_recursive(const struct lyd_node *node, struct lyxp_set *set, uint16_t indent, char **str, uint16_t *used,
412 uint16_t *size)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200413{
Radek Krejci7f769d72020-07-11 23:13:56 +0200414 char *buf, *line, *ptr = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200415 const char *value_str;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200416 const struct lyd_node *child;
Michal Vasko47da6562022-07-14 15:43:15 +0200417 enum lyxp_node_type child_type;
Michal Vasko60ea6352020-06-29 13:39:39 +0200418 struct lyd_node *tree;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200419 struct lyd_node_any *any;
420 LY_ERR rc;
421
Michal Vasko47da6562022-07-14 15:43:15 +0200422 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && node && (node->schema->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200423 return LY_SUCCESS;
424 }
425
Michal Vasko47da6562022-07-14 15:43:15 +0200426 if (!node) {
427 /* fake container */
428 LY_CHECK_RET(cast_string_realloc(set->ctx, 1, str, used, size));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200429 strcpy(*str + (*used - 1), "\n");
430 ++(*used);
431
432 ++indent;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200433
Michal Vasko47da6562022-07-14 15:43:15 +0200434 /* print all the top-level nodes */
435 child = NULL;
436 child_type = 0;
437 while (!moveto_axis_node_next(&child, &child_type, NULL, set->root_type, LYXP_AXIS_CHILD, set)) {
438 LY_CHECK_RET(cast_string_recursive(child, set, indent, str, used, size));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200439 }
440
Michal Vasko47da6562022-07-14 15:43:15 +0200441 /* end fake container */
442 LY_CHECK_RET(cast_string_realloc(set->ctx, 1, str, used, size));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200443 strcpy(*str + (*used - 1), "\n");
444 ++(*used);
445
446 --indent;
Michal Vasko47da6562022-07-14 15:43:15 +0200447 } else {
448 switch (node->schema->nodetype) {
449 case LYS_CONTAINER:
450 case LYS_LIST:
451 case LYS_RPC:
452 case LYS_NOTIF:
453 LY_CHECK_RET(cast_string_realloc(set->ctx, 1, str, used, size));
454 strcpy(*str + (*used - 1), "\n");
455 ++(*used);
456
457 for (child = lyd_child(node); child; child = child->next) {
458 LY_CHECK_RET(cast_string_recursive(child, set, indent + 1, str, used, size));
459 }
460
461 break;
462
463 case LYS_LEAF:
464 case LYS_LEAFLIST:
465 value_str = lyd_get_value(node);
466
467 /* print indent */
468 LY_CHECK_RET(cast_string_realloc(set->ctx, indent * 2 + strlen(value_str) + 1, str, used, size));
469 memset(*str + (*used - 1), ' ', indent * 2);
470 *used += indent * 2;
471
472 /* print value */
473 if (*used == 1) {
474 sprintf(*str + (*used - 1), "%s", value_str);
475 *used += strlen(value_str);
476 } else {
477 sprintf(*str + (*used - 1), "%s\n", value_str);
478 *used += strlen(value_str) + 1;
479 }
480
481 break;
482
483 case LYS_ANYXML:
484 case LYS_ANYDATA:
485 any = (struct lyd_node_any *)node;
486 if (!(void *)any->value.tree) {
487 /* no content */
488 buf = strdup("");
489 LY_CHECK_ERR_RET(!buf, LOGMEM(set->ctx), LY_EMEM);
490 } else {
491 struct ly_out *out;
492
493 if (any->value_type == LYD_ANYDATA_LYB) {
494 /* try to parse it into a data tree */
495 if (lyd_parse_data_mem((struct ly_ctx *)set->ctx, any->value.mem, LYD_LYB,
496 LYD_PARSE_ONLY | LYD_PARSE_STRICT, 0, &tree) == LY_SUCCESS) {
497 /* successfully parsed */
498 free(any->value.mem);
499 any->value.tree = tree;
500 any->value_type = LYD_ANYDATA_DATATREE;
501 }
502 /* error is covered by the following switch where LYD_ANYDATA_LYB causes failure */
503 }
504
505 switch (any->value_type) {
506 case LYD_ANYDATA_STRING:
507 case LYD_ANYDATA_XML:
508 case LYD_ANYDATA_JSON:
509 buf = strdup(any->value.json);
510 LY_CHECK_ERR_RET(!buf, LOGMEM(set->ctx), LY_EMEM);
511 break;
512 case LYD_ANYDATA_DATATREE:
513 LY_CHECK_RET(ly_out_new_memory(&buf, 0, &out));
514 rc = lyd_print_all(out, any->value.tree, LYD_XML, 0);
515 ly_out_free(out, NULL, 0);
516 LY_CHECK_RET(rc < 0, -rc);
517 break;
518 case LYD_ANYDATA_LYB:
519 LOGERR(set->ctx, LY_EINVAL, "Cannot convert LYB anydata into string.");
520 return LY_EINVAL;
521 }
522 }
523
524 line = strtok_r(buf, "\n", &ptr);
525 do {
526 rc = cast_string_realloc(set->ctx, indent * 2 + strlen(line) + 1, str, used, size);
527 if (rc != LY_SUCCESS) {
528 free(buf);
529 return rc;
530 }
531 memset(*str + (*used - 1), ' ', indent * 2);
532 *used += indent * 2;
533
534 strcpy(*str + (*used - 1), line);
535 *used += strlen(line);
536
537 strcpy(*str + (*used - 1), "\n");
538 *used += 1;
539 } while ((line = strtok_r(NULL, "\n", &ptr)));
540
541 free(buf);
542 break;
543
544 default:
545 LOGINT_RET(set->ctx);
546 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200547 }
548
549 return LY_SUCCESS;
550}
551
552/**
553 * @brief Cast an element into a string.
554 *
Michal Vasko47da6562022-07-14 15:43:15 +0200555 * @param[in] node Node to cast, NULL if root.
556 * @param[in] set XPath set.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200557 * @param[out] str Element cast to dynamically-allocated string.
558 * @return LY_ERR
559 */
560static LY_ERR
Michal Vasko47da6562022-07-14 15:43:15 +0200561cast_string_elem(const struct lyd_node *node, struct lyxp_set *set, char **str)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200562{
563 uint16_t used, size;
564 LY_ERR rc;
565
566 *str = malloc(LYXP_STRING_CAST_SIZE_START * sizeof(char));
Michal Vasko47da6562022-07-14 15:43:15 +0200567 LY_CHECK_ERR_RET(!*str, LOGMEM(set->ctx), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200568 (*str)[0] = '\0';
569 used = 1;
570 size = LYXP_STRING_CAST_SIZE_START;
571
Michal Vasko47da6562022-07-14 15:43:15 +0200572 rc = cast_string_recursive(node, set, 0, str, &used, &size);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200573 if (rc != LY_SUCCESS) {
574 free(*str);
575 return rc;
576 }
577
578 if (size > used) {
579 *str = ly_realloc(*str, used * sizeof(char));
Michal Vasko47da6562022-07-14 15:43:15 +0200580 LY_CHECK_ERR_RET(!*str, LOGMEM(set->ctx), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200581 }
582 return LY_SUCCESS;
583}
584
585/**
586 * @brief Cast a LYXP_SET_NODE_SET set into a string.
587 * Context position aware.
588 *
589 * @param[in] set Set to cast.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200590 * @param[out] str Cast dynamically-allocated string.
591 * @return LY_ERR
592 */
593static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100594cast_node_set_to_string(struct lyxp_set *set, char **str)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200595{
Michal Vaskoaa677062021-03-09 13:52:53 +0100596 if (!set->used) {
597 *str = strdup("");
598 if (!*str) {
599 LOGMEM_RET(set->ctx);
600 }
601 return LY_SUCCESS;
602 }
603
Michal Vasko03ff5a72019-09-11 13:49:33 +0200604 switch (set->val.nodes[0].type) {
Michal Vasko2caefc12019-11-14 16:07:56 +0100605 case LYXP_NODE_NONE:
606 /* invalid */
607 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200608 case LYXP_NODE_ROOT:
609 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200610 case LYXP_NODE_ELEM:
611 case LYXP_NODE_TEXT:
Michal Vasko47da6562022-07-14 15:43:15 +0200612 return cast_string_elem(set->val.nodes[0].node, set, str);
Michal Vasko9f96a052020-03-10 09:41:45 +0100613 case LYXP_NODE_META:
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200614 *str = strdup(lyd_get_meta_value(set->val.meta[0].meta));
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200615 if (!*str) {
616 LOGMEM_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200617 }
618 return LY_SUCCESS;
619 }
620
621 LOGINT_RET(set->ctx);
622}
623
624/**
625 * @brief Cast a string into an XPath number.
626 *
627 * @param[in] str String to use.
628 * @return Cast number.
629 */
630static long double
631cast_string_to_number(const char *str)
632{
633 long double num;
634 char *ptr;
635
636 errno = 0;
637 num = strtold(str, &ptr);
638 if (errno || *ptr) {
639 num = NAN;
640 }
641 return num;
642}
643
644/**
645 * @brief Callback for checking value equality.
646 *
Michal Vasko62524a92021-02-26 10:08:50 +0100647 * Implementation of ::lyht_value_equal_cb.
Radek Krejci857189e2020-09-01 13:26:36 +0200648 *
Michal Vasko03ff5a72019-09-11 13:49:33 +0200649 * @param[in] val1_p First value.
650 * @param[in] val2_p Second value.
651 * @param[in] mod Whether hash table is being modified.
652 * @param[in] cb_data Callback data.
Radek Krejci857189e2020-09-01 13:26:36 +0200653 * @return Boolean value whether values are equal or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200654 */
Radek Krejci857189e2020-09-01 13:26:36 +0200655static ly_bool
656set_values_equal_cb(void *val1_p, void *val2_p, ly_bool UNUSED(mod), void *UNUSED(cb_data))
Michal Vasko03ff5a72019-09-11 13:49:33 +0200657{
658 struct lyxp_set_hash_node *val1, *val2;
659
660 val1 = (struct lyxp_set_hash_node *)val1_p;
661 val2 = (struct lyxp_set_hash_node *)val2_p;
662
663 if ((val1->node == val2->node) && (val1->type == val2->type)) {
664 return 1;
665 }
666
667 return 0;
668}
669
670/**
671 * @brief Insert node and its hash into set.
672 *
673 * @param[in] set et to insert to.
674 * @param[in] node Node with hash.
675 * @param[in] type Node type.
676 */
677static void
678set_insert_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type)
679{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200680 LY_ERR r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200681 uint32_t i, hash;
682 struct lyxp_set_hash_node hnode;
683
684 if (!set->ht && (set->used >= LYD_HT_MIN_ITEMS)) {
685 /* create hash table and add all the nodes */
686 set->ht = lyht_new(1, sizeof(struct lyxp_set_hash_node), set_values_equal_cb, NULL, 1);
687 for (i = 0; i < set->used; ++i) {
688 hnode.node = set->val.nodes[i].node;
689 hnode.type = set->val.nodes[i].type;
690
691 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
692 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
693 hash = dict_hash_multi(hash, NULL, 0);
694
695 r = lyht_insert(set->ht, &hnode, hash, NULL);
696 assert(!r);
697 (void)r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200698
Michal Vasko9d6befd2019-12-11 14:56:56 +0100699 if (hnode.node == node) {
700 /* it was just added, do not add it twice */
701 node = NULL;
702 }
703 }
704 }
705
706 if (set->ht && node) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200707 /* add the new node into hash table */
708 hnode.node = node;
709 hnode.type = type;
710
711 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
712 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
713 hash = dict_hash_multi(hash, NULL, 0);
714
715 r = lyht_insert(set->ht, &hnode, hash, NULL);
716 assert(!r);
717 (void)r;
718 }
719}
720
721/**
722 * @brief Remove node and its hash from set.
723 *
724 * @param[in] set Set to remove from.
725 * @param[in] node Node to remove.
726 * @param[in] type Node type.
727 */
728static void
729set_remove_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type)
730{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200731 LY_ERR r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200732 struct lyxp_set_hash_node hnode;
733 uint32_t hash;
734
735 if (set->ht) {
736 hnode.node = node;
737 hnode.type = type;
738
739 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
740 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
741 hash = dict_hash_multi(hash, NULL, 0);
742
743 r = lyht_remove(set->ht, &hnode, hash);
744 assert(!r);
745 (void)r;
746
747 if (!set->ht->used) {
748 lyht_free(set->ht);
749 set->ht = NULL;
750 }
751 }
752}
753
754/**
755 * @brief Check whether node is in set based on its hash.
756 *
757 * @param[in] set Set to search in.
758 * @param[in] node Node to search for.
759 * @param[in] type Node type.
760 * @param[in] skip_idx Index in @p set to skip.
761 * @return LY_ERR
762 */
763static LY_ERR
764set_dup_node_hash_check(const struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type, int skip_idx)
765{
766 struct lyxp_set_hash_node hnode, *match_p;
767 uint32_t hash;
768
769 hnode.node = node;
770 hnode.type = type;
771
772 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
773 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
774 hash = dict_hash_multi(hash, NULL, 0);
775
776 if (!lyht_find(set->ht, &hnode, hash, (void **)&match_p)) {
777 if ((skip_idx > -1) && (set->val.nodes[skip_idx].node == match_p->node) && (set->val.nodes[skip_idx].type == match_p->type)) {
778 /* we found it on the index that should be skipped, find another */
779 hnode = *match_p;
780 if (lyht_find_next(set->ht, &hnode, hash, (void **)&match_p)) {
781 /* none other found */
782 return LY_SUCCESS;
783 }
784 }
785
786 return LY_EEXIST;
787 }
788
789 /* not found */
790 return LY_SUCCESS;
791}
792
Michal Vaskod3678892020-05-21 10:06:58 +0200793void
794lyxp_set_free_content(struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200795{
796 if (!set) {
797 return;
798 }
799
800 if (set->type == LYXP_SET_NODE_SET) {
801 free(set->val.nodes);
802 lyht_free(set->ht);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200803 } else if (set->type == LYXP_SET_SCNODE_SET) {
804 free(set->val.scnodes);
Michal Vaskod3678892020-05-21 10:06:58 +0200805 lyht_free(set->ht);
806 } else {
807 if (set->type == LYXP_SET_STRING) {
808 free(set->val.str);
809 }
810 set->type = LYXP_SET_NODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200811 }
Michal Vaskod3678892020-05-21 10:06:58 +0200812
813 set->val.nodes = NULL;
814 set->used = 0;
815 set->size = 0;
816 set->ht = NULL;
817 set->ctx_pos = 0;
aPiecek748da732021-06-01 09:56:43 +0200818 set->ctx_size = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200819}
820
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100821/**
822 * @brief Free dynamically-allocated set.
823 *
824 * @param[in] set Set to free.
825 */
826static void
Michal Vasko03ff5a72019-09-11 13:49:33 +0200827lyxp_set_free(struct lyxp_set *set)
828{
829 if (!set) {
830 return;
831 }
832
Michal Vaskod3678892020-05-21 10:06:58 +0200833 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200834 free(set);
835}
836
837/**
838 * @brief Initialize set context.
839 *
840 * @param[in] new Set to initialize.
841 * @param[in] set Arbitrary initialized set.
842 */
843static void
Michal Vasko4c7763f2020-07-27 17:40:37 +0200844set_init(struct lyxp_set *new, const struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200845{
846 memset(new, 0, sizeof *new);
Michal Vasko02a77382019-09-12 11:47:35 +0200847 if (set) {
Michal Vasko306e2832022-07-25 09:15:17 +0200848 new->non_child_axis = set->non_child_axis;
Michal Vasko02a77382019-09-12 11:47:35 +0200849 new->ctx = set->ctx;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200850 new->cur_node = set->cur_node;
Michal Vasko588112f2019-12-10 14:51:53 +0100851 new->root_type = set->root_type;
Michal Vasko6b26e742020-07-17 15:02:10 +0200852 new->context_op = set->context_op;
Michal Vaskof03ed032020-03-04 13:31:44 +0100853 new->tree = set->tree;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200854 new->cur_mod = set->cur_mod;
Michal Vasko02a77382019-09-12 11:47:35 +0200855 new->format = set->format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200856 new->prefix_data = set->prefix_data;
aPiecekfba75362021-10-07 12:39:48 +0200857 new->vars = set->vars;
Michal Vasko02a77382019-09-12 11:47:35 +0200858 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200859}
860
861/**
862 * @brief Create a deep copy of a set.
863 *
864 * @param[in] set Set to copy.
865 * @return Copy of @p set.
866 */
867static struct lyxp_set *
868set_copy(struct lyxp_set *set)
869{
870 struct lyxp_set *ret;
Michal Vasko1fdd8fa2021-01-08 09:21:45 +0100871 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200872
873 if (!set) {
874 return NULL;
875 }
876
877 ret = malloc(sizeof *ret);
878 LY_CHECK_ERR_RET(!ret, LOGMEM(set->ctx), NULL);
879 set_init(ret, set);
880
881 if (set->type == LYXP_SET_SCNODE_SET) {
882 ret->type = set->type;
883
884 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100885 if ((set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) ||
886 (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +0200887 uint32_t idx;
888 LY_CHECK_ERR_RET(lyxp_set_scnode_insert_node(ret, set->val.scnodes[i].scnode, set->val.scnodes[i].type, &idx),
889 lyxp_set_free(ret), NULL);
Michal Vasko3f27c522020-01-06 08:37:49 +0100890 /* coverity seems to think scnodes can be NULL */
Radek Krejciaa6b53f2020-08-27 15:19:03 +0200891 if (!ret->val.scnodes) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200892 lyxp_set_free(ret);
893 return NULL;
894 }
Michal Vaskoba716542019-12-16 10:01:58 +0100895 ret->val.scnodes[idx].in_ctx = set->val.scnodes[i].in_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200896 }
897 }
898 } else if (set->type == LYXP_SET_NODE_SET) {
899 ret->type = set->type;
Michal Vasko08e9b112021-06-11 15:41:17 +0200900 if (set->used) {
901 ret->val.nodes = malloc(set->used * sizeof *ret->val.nodes);
902 LY_CHECK_ERR_RET(!ret->val.nodes, LOGMEM(set->ctx); free(ret), NULL);
903 memcpy(ret->val.nodes, set->val.nodes, set->used * sizeof *ret->val.nodes);
904 } else {
905 ret->val.nodes = NULL;
906 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200907
908 ret->used = ret->size = set->used;
909 ret->ctx_pos = set->ctx_pos;
910 ret->ctx_size = set->ctx_size;
Michal Vasko4a04e542021-02-01 08:58:30 +0100911 if (set->ht) {
912 ret->ht = lyht_dup(set->ht);
913 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200914 } else {
Radek Krejci0f969882020-08-21 16:56:47 +0200915 memcpy(ret, set, sizeof *ret);
916 if (set->type == LYXP_SET_STRING) {
917 ret->val.str = strdup(set->val.str);
918 LY_CHECK_ERR_RET(!ret->val.str, LOGMEM(set->ctx); free(ret), NULL);
919 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200920 }
921
922 return ret;
923}
924
925/**
926 * @brief Fill XPath set with a string. Any current data are disposed of.
927 *
928 * @param[in] set Set to fill.
929 * @param[in] string String to fill into \p set.
930 * @param[in] str_len Length of \p string. 0 is a valid value!
931 */
932static void
933set_fill_string(struct lyxp_set *set, const char *string, uint16_t str_len)
934{
Michal Vaskod3678892020-05-21 10:06:58 +0200935 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200936
937 set->type = LYXP_SET_STRING;
938 if ((str_len == 0) && (string[0] != '\0')) {
939 string = "";
940 }
941 set->val.str = strndup(string, str_len);
942}
943
944/**
945 * @brief Fill XPath set with a number. Any current data are disposed of.
946 *
947 * @param[in] set Set to fill.
948 * @param[in] number Number to fill into \p set.
949 */
950static void
951set_fill_number(struct lyxp_set *set, long double number)
952{
Michal Vaskod3678892020-05-21 10:06:58 +0200953 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200954
955 set->type = LYXP_SET_NUMBER;
956 set->val.num = number;
957}
958
959/**
960 * @brief Fill XPath set with a boolean. Any current data are disposed of.
961 *
962 * @param[in] set Set to fill.
963 * @param[in] boolean Boolean to fill into \p set.
964 */
965static void
Radek Krejci857189e2020-09-01 13:26:36 +0200966set_fill_boolean(struct lyxp_set *set, ly_bool boolean)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200967{
Michal Vaskod3678892020-05-21 10:06:58 +0200968 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200969
970 set->type = LYXP_SET_BOOLEAN;
Michal Vasko004d3152020-06-11 19:59:22 +0200971 set->val.bln = boolean;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200972}
973
974/**
975 * @brief Fill XPath set with the value from another set (deep assign).
976 * Any current data are disposed of.
977 *
978 * @param[in] trg Set to fill.
979 * @param[in] src Source set to copy into \p trg.
980 */
981static void
Michal Vasko4c7763f2020-07-27 17:40:37 +0200982set_fill_set(struct lyxp_set *trg, const struct lyxp_set *src)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200983{
984 if (!trg || !src) {
985 return;
986 }
987
988 if (trg->type == LYXP_SET_NODE_SET) {
989 free(trg->val.nodes);
990 } else if (trg->type == LYXP_SET_STRING) {
991 free(trg->val.str);
992 }
993 set_init(trg, src);
994
995 if (src->type == LYXP_SET_SCNODE_SET) {
996 trg->type = LYXP_SET_SCNODE_SET;
997 trg->used = src->used;
998 trg->size = src->used;
999
Michal Vasko08e9b112021-06-11 15:41:17 +02001000 if (trg->size) {
1001 trg->val.scnodes = ly_realloc(trg->val.scnodes, trg->size * sizeof *trg->val.scnodes);
1002 LY_CHECK_ERR_RET(!trg->val.scnodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), );
1003 memcpy(trg->val.scnodes, src->val.scnodes, src->used * sizeof *src->val.scnodes);
1004 } else {
1005 trg->val.scnodes = NULL;
1006 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001007 } else if (src->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +02001008 set_fill_boolean(trg, src->val.bln);
Michal Vasko44f3d2c2020-08-24 09:49:38 +02001009 } else if (src->type == LYXP_SET_NUMBER) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001010 set_fill_number(trg, src->val.num);
1011 } else if (src->type == LYXP_SET_STRING) {
1012 set_fill_string(trg, src->val.str, strlen(src->val.str));
1013 } else {
1014 if (trg->type == LYXP_SET_NODE_SET) {
1015 free(trg->val.nodes);
1016 } else if (trg->type == LYXP_SET_STRING) {
1017 free(trg->val.str);
1018 }
1019
Michal Vaskod3678892020-05-21 10:06:58 +02001020 assert(src->type == LYXP_SET_NODE_SET);
1021
1022 trg->type = LYXP_SET_NODE_SET;
1023 trg->used = src->used;
1024 trg->size = src->used;
1025 trg->ctx_pos = src->ctx_pos;
1026 trg->ctx_size = src->ctx_size;
1027
Michal Vasko08e9b112021-06-11 15:41:17 +02001028 if (trg->size) {
1029 trg->val.nodes = malloc(trg->size * sizeof *trg->val.nodes);
1030 LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), );
1031 memcpy(trg->val.nodes, src->val.nodes, src->used * sizeof *src->val.nodes);
1032 } else {
1033 trg->val.nodes = NULL;
1034 }
Michal Vaskod3678892020-05-21 10:06:58 +02001035 if (src->ht) {
1036 trg->ht = lyht_dup(src->ht);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001037 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02001038 trg->ht = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001039 }
1040 }
1041}
1042
1043/**
1044 * @brief Clear context of all schema nodes.
1045 *
1046 * @param[in] set Set to clear.
Michal Vasko1a09b212021-05-06 13:00:10 +02001047 * @param[in] new_ctx New context state for all the nodes currently in the context.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001048 */
1049static void
Michal Vasko1a09b212021-05-06 13:00:10 +02001050set_scnode_clear_ctx(struct lyxp_set *set, int32_t new_ctx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001051{
1052 uint32_t i;
1053
1054 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001055 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko1a09b212021-05-06 13:00:10 +02001056 set->val.scnodes[i].in_ctx = new_ctx;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001057 } else if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START) {
1058 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001059 }
1060 }
1061}
1062
1063/**
1064 * @brief Remove a node from a set. Removing last node changes
1065 * set into LYXP_SET_EMPTY. Context position aware.
1066 *
1067 * @param[in] set Set to use.
1068 * @param[in] idx Index from @p set of the node to be removed.
1069 */
1070static void
1071set_remove_node(struct lyxp_set *set, uint32_t idx)
1072{
1073 assert(set && (set->type == LYXP_SET_NODE_SET));
1074 assert(idx < set->used);
1075
1076 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
1077
1078 --set->used;
Michal Vasko08e9b112021-06-11 15:41:17 +02001079 if (idx < set->used) {
1080 memmove(&set->val.nodes[idx], &set->val.nodes[idx + 1], (set->used - idx) * sizeof *set->val.nodes);
1081 } else if (!set->used) {
Michal Vaskod3678892020-05-21 10:06:58 +02001082 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001083 }
1084}
1085
1086/**
Michal Vasko2caefc12019-11-14 16:07:56 +01001087 * @brief Remove a node from a set by setting its type to LYXP_NODE_NONE.
Michal Vasko57eab132019-09-24 11:46:26 +02001088 *
1089 * @param[in] set Set to use.
1090 * @param[in] idx Index from @p set of the node to be removed.
1091 */
1092static void
Michal Vasko2caefc12019-11-14 16:07:56 +01001093set_remove_node_none(struct lyxp_set *set, uint32_t idx)
Michal Vasko57eab132019-09-24 11:46:26 +02001094{
1095 assert(set && (set->type == LYXP_SET_NODE_SET));
1096 assert(idx < set->used);
1097
Michal Vasko2caefc12019-11-14 16:07:56 +01001098 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1099 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
1100 }
1101 set->val.nodes[idx].type = LYXP_NODE_NONE;
Michal Vasko57eab132019-09-24 11:46:26 +02001102}
1103
1104/**
Michal Vasko2caefc12019-11-14 16:07:56 +01001105 * @brief Remove all LYXP_NODE_NONE nodes from a set. Removing last node changes
Michal Vasko57eab132019-09-24 11:46:26 +02001106 * set into LYXP_SET_EMPTY. Context position aware.
1107 *
1108 * @param[in] set Set to consolidate.
1109 */
1110static void
Michal Vasko2caefc12019-11-14 16:07:56 +01001111set_remove_nodes_none(struct lyxp_set *set)
Michal Vasko57eab132019-09-24 11:46:26 +02001112{
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01001113 uint32_t i, orig_used, end = 0;
1114 int64_t start;
Michal Vasko57eab132019-09-24 11:46:26 +02001115
Michal Vaskod3678892020-05-21 10:06:58 +02001116 assert(set);
Michal Vasko57eab132019-09-24 11:46:26 +02001117
1118 orig_used = set->used;
1119 set->used = 0;
Michal Vaskod989ba02020-08-24 10:59:24 +02001120 for (i = 0; i < orig_used; ) {
Michal Vasko57eab132019-09-24 11:46:26 +02001121 start = -1;
1122 do {
Michal Vasko2caefc12019-11-14 16:07:56 +01001123 if ((set->val.nodes[i].type != LYXP_NODE_NONE) && (start == -1)) {
Michal Vasko57eab132019-09-24 11:46:26 +02001124 start = i;
Michal Vasko2caefc12019-11-14 16:07:56 +01001125 } else if ((start > -1) && (set->val.nodes[i].type == LYXP_NODE_NONE)) {
Michal Vasko57eab132019-09-24 11:46:26 +02001126 end = i;
1127 ++i;
1128 break;
1129 }
1130
1131 ++i;
1132 if (i == orig_used) {
1133 end = i;
1134 }
1135 } while (i < orig_used);
1136
1137 if (start > -1) {
1138 /* move the whole chunk of valid nodes together */
1139 if (set->used != (unsigned)start) {
1140 memmove(&set->val.nodes[set->used], &set->val.nodes[start], (end - start) * sizeof *set->val.nodes);
1141 }
1142 set->used += end - start;
1143 }
1144 }
Michal Vasko57eab132019-09-24 11:46:26 +02001145}
1146
1147/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02001148 * @brief Check for duplicates in a node set.
1149 *
1150 * @param[in] set Set to check.
1151 * @param[in] node Node to look for in @p set.
1152 * @param[in] node_type Type of @p node.
1153 * @param[in] skip_idx Index from @p set to skip.
1154 * @return LY_ERR
1155 */
1156static LY_ERR
1157set_dup_node_check(const struct lyxp_set *set, const struct lyd_node *node, enum lyxp_node_type node_type, int skip_idx)
1158{
1159 uint32_t i;
1160
Michal Vasko2caefc12019-11-14 16:07:56 +01001161 if (set->ht && node) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001162 return set_dup_node_hash_check(set, (struct lyd_node *)node, node_type, skip_idx);
1163 }
1164
1165 for (i = 0; i < set->used; ++i) {
1166 if ((skip_idx > -1) && (i == (unsigned)skip_idx)) {
1167 continue;
1168 }
1169
1170 if ((set->val.nodes[i].node == node) && (set->val.nodes[i].type == node_type)) {
1171 return LY_EEXIST;
1172 }
1173 }
1174
1175 return LY_SUCCESS;
1176}
1177
Radek Krejci857189e2020-09-01 13:26:36 +02001178ly_bool
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001179lyxp_set_scnode_contains(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type, int skip_idx,
1180 uint32_t *index_p)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001181{
1182 uint32_t i;
1183
1184 for (i = 0; i < set->used; ++i) {
1185 if ((skip_idx > -1) && (i == (unsigned)skip_idx)) {
1186 continue;
1187 }
1188
1189 if ((set->val.scnodes[i].scnode == node) && (set->val.scnodes[i].type == node_type)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001190 if (index_p) {
1191 *index_p = i;
1192 }
1193 return 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001194 }
1195 }
1196
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001197 return 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001198}
1199
Michal Vaskoecd62de2019-11-13 12:35:11 +01001200void
1201lyxp_set_scnode_merge(struct lyxp_set *set1, struct lyxp_set *set2)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001202{
1203 uint32_t orig_used, i, j;
1204
Michal Vaskod3678892020-05-21 10:06:58 +02001205 assert((set1->type == LYXP_SET_SCNODE_SET) && (set2->type == LYXP_SET_SCNODE_SET));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001206
Michal Vaskod3678892020-05-21 10:06:58 +02001207 if (!set2->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001208 return;
1209 }
1210
Michal Vaskod3678892020-05-21 10:06:58 +02001211 if (!set1->used) {
aPiecekadc1e4f2021-10-07 11:15:12 +02001212 /* release hidden allocated data (lyxp_set.size) */
1213 lyxp_set_free_content(set1);
1214 /* direct copying of the entire structure */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001215 memcpy(set1, set2, sizeof *set1);
1216 return;
1217 }
1218
1219 if (set1->used + set2->used > set1->size) {
1220 set1->size = set1->used + set2->used;
1221 set1->val.scnodes = ly_realloc(set1->val.scnodes, set1->size * sizeof *set1->val.scnodes);
1222 LY_CHECK_ERR_RET(!set1->val.scnodes, LOGMEM(set1->ctx), );
1223 }
1224
1225 orig_used = set1->used;
1226
1227 for (i = 0; i < set2->used; ++i) {
1228 for (j = 0; j < orig_used; ++j) {
1229 /* detect duplicities */
1230 if (set1->val.scnodes[j].scnode == set2->val.scnodes[i].scnode) {
1231 break;
1232 }
1233 }
1234
Michal Vasko0587bce2020-12-10 12:19:21 +01001235 if (j < orig_used) {
1236 /* node is there, but update its status if needed */
1237 if (set1->val.scnodes[j].in_ctx == LYXP_SET_SCNODE_START_USED) {
1238 set1->val.scnodes[j].in_ctx = set2->val.scnodes[i].in_ctx;
Michal Vasko1a09b212021-05-06 13:00:10 +02001239 } else if ((set1->val.scnodes[j].in_ctx == LYXP_SET_SCNODE_ATOM_NODE) &&
1240 (set2->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_VAL)) {
1241 set1->val.scnodes[j].in_ctx = set2->val.scnodes[i].in_ctx;
Michal Vasko0587bce2020-12-10 12:19:21 +01001242 }
1243 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001244 memcpy(&set1->val.scnodes[set1->used], &set2->val.scnodes[i], sizeof *set2->val.scnodes);
1245 ++set1->used;
1246 }
1247 }
1248
Michal Vaskod3678892020-05-21 10:06:58 +02001249 lyxp_set_free_content(set2);
1250 set2->type = LYXP_SET_SCNODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001251}
1252
1253/**
1254 * @brief Insert a node into a set. Context position aware.
1255 *
1256 * @param[in] set Set to use.
1257 * @param[in] node Node to insert to @p set.
1258 * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting.
1259 * @param[in] node_type Node type of @p node.
1260 * @param[in] idx Index in @p set to insert into.
1261 */
1262static void
1263set_insert_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx)
1264{
Michal Vaskod3678892020-05-21 10:06:58 +02001265 assert(set && (set->type == LYXP_SET_NODE_SET));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001266
Michal Vaskod3678892020-05-21 10:06:58 +02001267 if (!set->size) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001268 /* first item */
1269 if (idx) {
1270 /* no real harm done, but it is a bug */
1271 LOGINT(set->ctx);
1272 idx = 0;
1273 }
1274 set->val.nodes = malloc(LYXP_SET_SIZE_START * sizeof *set->val.nodes);
1275 LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), );
1276 set->type = LYXP_SET_NODE_SET;
1277 set->used = 0;
1278 set->size = LYXP_SET_SIZE_START;
1279 set->ctx_pos = 1;
1280 set->ctx_size = 1;
1281 set->ht = NULL;
1282 } else {
1283 /* not an empty set */
1284 if (set->used == set->size) {
1285
1286 /* set is full */
Michal Vasko871df522022-04-06 12:14:41 +02001287 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 +02001288 LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), );
Michal Vasko871df522022-04-06 12:14:41 +02001289 set->size *= LYXP_SET_SIZE_MUL_STEP;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001290 }
1291
1292 if (idx > set->used) {
1293 LOGINT(set->ctx);
1294 idx = set->used;
1295 }
1296
1297 /* make space for the new node */
1298 if (idx < set->used) {
1299 memmove(&set->val.nodes[idx + 1], &set->val.nodes[idx], (set->used - idx) * sizeof *set->val.nodes);
1300 }
1301 }
1302
1303 /* finally assign the value */
1304 set->val.nodes[idx].node = (struct lyd_node *)node;
1305 set->val.nodes[idx].type = node_type;
1306 set->val.nodes[idx].pos = pos;
1307 ++set->used;
1308
Michal Vasko2416fdd2021-10-01 11:07:10 +02001309 /* add into hash table */
1310 set_insert_node_hash(set, (struct lyd_node *)node, node_type);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001311}
1312
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001313LY_ERR
Michal Vaskoddd76592022-01-17 13:34:48 +01001314lyxp_set_scnode_insert_node(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type,
1315 uint32_t *index_p)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001316{
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001317 uint32_t index;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001318
1319 assert(set->type == LYXP_SET_SCNODE_SET);
1320
Michal Vasko871df522022-04-06 12:14:41 +02001321 if (!set->size) {
1322 /* first item */
1323 set->val.scnodes = malloc(LYXP_SET_SIZE_START * sizeof *set->val.scnodes);
1324 LY_CHECK_ERR_RET(!set->val.scnodes, LOGMEM(set->ctx), LY_EMEM);
1325 set->type = LYXP_SET_SCNODE_SET;
1326 set->used = 0;
1327 set->size = LYXP_SET_SIZE_START;
1328 set->ctx_pos = 1;
1329 set->ctx_size = 1;
1330 set->ht = NULL;
1331 }
1332
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001333 if (lyxp_set_scnode_contains(set, node, node_type, -1, &index)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001334 set->val.scnodes[index].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001335 } else {
1336 if (set->used == set->size) {
Michal Vasko871df522022-04-06 12:14:41 +02001337 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 +02001338 LY_CHECK_ERR_RET(!set->val.scnodes, LOGMEM(set->ctx), LY_EMEM);
Michal Vasko871df522022-04-06 12:14:41 +02001339 set->size *= LYXP_SET_SIZE_MUL_STEP;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001340 }
1341
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001342 index = set->used;
1343 set->val.scnodes[index].scnode = (struct lysc_node *)node;
1344 set->val.scnodes[index].type = node_type;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001345 set->val.scnodes[index].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001346 ++set->used;
1347 }
1348
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001349 if (index_p) {
1350 *index_p = index;
1351 }
1352
1353 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001354}
1355
1356/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02001357 * @brief Set all nodes with ctx 1 to a new unique context value.
1358 *
1359 * @param[in] set Set to modify.
1360 * @return New context value.
1361 */
Michal Vasko5c4e5892019-11-14 12:31:38 +01001362static int32_t
Michal Vasko03ff5a72019-09-11 13:49:33 +02001363set_scnode_new_in_ctx(struct lyxp_set *set)
1364{
Michal Vasko5c4e5892019-11-14 12:31:38 +01001365 uint32_t i;
1366 int32_t ret_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001367
1368 assert(set->type == LYXP_SET_SCNODE_SET);
1369
Radek Krejcif13b87b2020-12-01 22:02:17 +01001370 ret_ctx = LYXP_SET_SCNODE_ATOM_PRED_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001371retry:
1372 for (i = 0; i < set->used; ++i) {
1373 if (set->val.scnodes[i].in_ctx >= ret_ctx) {
1374 ret_ctx = set->val.scnodes[i].in_ctx + 1;
1375 goto retry;
1376 }
1377 }
1378 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001379 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001380 set->val.scnodes[i].in_ctx = ret_ctx;
1381 }
1382 }
1383
1384 return ret_ctx;
1385}
1386
1387/**
1388 * @brief Get unique @p node position in the data.
1389 *
1390 * @param[in] node Node to find.
1391 * @param[in] node_type Node type of @p node.
1392 * @param[in] root Root node.
1393 * @param[in] root_type Type of the XPath @p root node.
1394 * @param[in] prev Node that we think is before @p node in DFS from @p root. Can optionally
1395 * be used to increase efficiency and start the DFS from this node.
1396 * @param[in] prev_pos Node @p prev position. Optional, but must be set if @p prev is set.
1397 * @return Node position.
1398 */
1399static uint32_t
1400get_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 +02001401 enum lyxp_node_type root_type, const struct lyd_node **prev, uint32_t *prev_pos)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001402{
Michal Vasko56daf732020-08-10 10:57:18 +02001403 const struct lyd_node *elem = NULL, *top_sibling;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001404 uint32_t pos = 1;
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001405 ly_bool found = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001406
1407 assert(prev && prev_pos && !root->prev->next);
1408
1409 if ((node_type == LYXP_NODE_ROOT) || (node_type == LYXP_NODE_ROOT_CONFIG)) {
1410 return 0;
1411 }
1412
1413 if (*prev) {
1414 /* start from the previous element instead from the root */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001415 pos = *prev_pos;
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001416 for (top_sibling = *prev; top_sibling->parent; top_sibling = lyd_parent(top_sibling)) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001417 goto dfs_search;
1418 }
1419
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001420 LY_LIST_FOR(root, top_sibling) {
Michal Vasko56daf732020-08-10 10:57:18 +02001421 LYD_TREE_DFS_BEGIN(top_sibling, elem) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001422dfs_search:
Michal Vaskoa9309bb2021-07-09 09:31:55 +02001423 LYD_TREE_DFS_continue = 0;
1424
Michal Vasko56daf732020-08-10 10:57:18 +02001425 if (*prev && !elem) {
1426 /* resume previous DFS */
1427 elem = LYD_TREE_DFS_next = (struct lyd_node *)*prev;
1428 LYD_TREE_DFS_continue = 0;
1429 }
1430
Michal Vasko482a6822022-05-06 08:56:12 +02001431 if (!elem->schema || ((root_type == LYXP_NODE_ROOT_CONFIG) && (elem->schema->flags & LYS_CONFIG_R))) {
Michal Vasko56daf732020-08-10 10:57:18 +02001432 /* skip */
1433 LYD_TREE_DFS_continue = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001434 } else {
Michal Vasko56daf732020-08-10 10:57:18 +02001435 if (elem == node) {
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001436 found = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001437 break;
1438 }
Michal Vasko56daf732020-08-10 10:57:18 +02001439 ++pos;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001440 }
Michal Vasko56daf732020-08-10 10:57:18 +02001441
1442 LYD_TREE_DFS_END(top_sibling, elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001443 }
1444
1445 /* node found */
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001446 if (found) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001447 break;
1448 }
1449 }
1450
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001451 if (!found) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001452 if (!(*prev)) {
1453 /* we went from root and failed to find it, cannot be */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001454 LOGINT(LYD_CTX(node));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001455 return 0;
1456 } else {
Michal Vasko56daf732020-08-10 10:57:18 +02001457 /* start the search again from the beginning */
1458 *prev = root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001459
Michal Vasko56daf732020-08-10 10:57:18 +02001460 top_sibling = root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001461 pos = 1;
1462 goto dfs_search;
1463 }
1464 }
1465
1466 /* remember the last found node for next time */
1467 *prev = node;
1468 *prev_pos = pos;
1469
1470 return pos;
1471}
1472
1473/**
1474 * @brief Assign (fill) missing node positions.
1475 *
1476 * @param[in] set Set to fill positions in.
1477 * @param[in] root Context root node.
1478 * @param[in] root_type Context root type.
1479 * @return LY_ERR
1480 */
1481static LY_ERR
1482set_assign_pos(struct lyxp_set *set, const struct lyd_node *root, enum lyxp_node_type root_type)
1483{
1484 const struct lyd_node *prev = NULL, *tmp_node;
1485 uint32_t i, tmp_pos = 0;
1486
1487 for (i = 0; i < set->used; ++i) {
1488 if (!set->val.nodes[i].pos) {
1489 tmp_node = NULL;
1490 switch (set->val.nodes[i].type) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001491 case LYXP_NODE_META:
1492 tmp_node = set->val.meta[i].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001493 if (!tmp_node) {
1494 LOGINT_RET(root->schema->module->ctx);
1495 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01001496 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001497 case LYXP_NODE_ELEM:
1498 case LYXP_NODE_TEXT:
1499 if (!tmp_node) {
1500 tmp_node = set->val.nodes[i].node;
1501 }
1502 set->val.nodes[i].pos = get_node_pos(tmp_node, set->val.nodes[i].type, root, root_type, &prev, &tmp_pos);
1503 break;
1504 default:
1505 /* all roots have position 0 */
1506 break;
1507 }
1508 }
1509 }
1510
1511 return LY_SUCCESS;
1512}
1513
1514/**
Michal Vasko9f96a052020-03-10 09:41:45 +01001515 * @brief Get unique @p meta position in the parent metadata.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001516 *
Michal Vasko9f96a052020-03-10 09:41:45 +01001517 * @param[in] meta Metadata to use.
1518 * @return Metadata position.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001519 */
1520static uint16_t
Michal Vasko9f96a052020-03-10 09:41:45 +01001521get_meta_pos(struct lyd_meta *meta)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001522{
1523 uint16_t pos = 0;
Michal Vasko9f96a052020-03-10 09:41:45 +01001524 struct lyd_meta *meta2;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001525
Michal Vasko9f96a052020-03-10 09:41:45 +01001526 for (meta2 = meta->parent->meta; meta2 && (meta2 != meta); meta2 = meta2->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001527 ++pos;
1528 }
1529
Michal Vasko9f96a052020-03-10 09:41:45 +01001530 assert(meta2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001531 return pos;
1532}
1533
1534/**
1535 * @brief Compare 2 nodes in respect to XPath document order.
1536 *
1537 * @param[in] item1 1st node.
1538 * @param[in] item2 2nd node.
1539 * @return If 1st > 2nd returns 1, 1st == 2nd returns 0, and 1st < 2nd returns -1.
1540 */
1541static int
1542set_sort_compare(struct lyxp_set_node *item1, struct lyxp_set_node *item2)
1543{
Michal Vasko9f96a052020-03-10 09:41:45 +01001544 uint32_t meta_pos1 = 0, meta_pos2 = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001545
1546 if (item1->pos < item2->pos) {
1547 return -1;
1548 }
1549
1550 if (item1->pos > item2->pos) {
1551 return 1;
1552 }
1553
1554 /* node positions are equal, the fun case */
1555
1556 /* 1st ELEM - == - 2nd TEXT, 1st TEXT - == - 2nd ELEM */
1557 /* special case since text nodes are actually saved as their parents */
1558 if ((item1->node == item2->node) && (item1->type != item2->type)) {
1559 if (item1->type == LYXP_NODE_ELEM) {
1560 assert(item2->type == LYXP_NODE_TEXT);
1561 return -1;
1562 } else {
1563 assert((item1->type == LYXP_NODE_TEXT) && (item2->type == LYXP_NODE_ELEM));
1564 return 1;
1565 }
1566 }
1567
Michal Vasko9f96a052020-03-10 09:41:45 +01001568 /* we need meta positions now */
1569 if (item1->type == LYXP_NODE_META) {
1570 meta_pos1 = get_meta_pos((struct lyd_meta *)item1->node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001571 }
Michal Vasko9f96a052020-03-10 09:41:45 +01001572 if (item2->type == LYXP_NODE_META) {
1573 meta_pos2 = get_meta_pos((struct lyd_meta *)item2->node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001574 }
1575
Michal Vasko9f96a052020-03-10 09:41:45 +01001576 /* 1st ROOT - 2nd ROOT, 1st ELEM - 2nd ELEM, 1st TEXT - 2nd TEXT, 1st META - =pos= - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001577 /* check for duplicates */
1578 if (item1->node == item2->node) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001579 assert((item1->type == item2->type) && ((item1->type != LYXP_NODE_META) || (meta_pos1 == meta_pos2)));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001580 return 0;
1581 }
1582
Michal Vasko9f96a052020-03-10 09:41:45 +01001583 /* 1st ELEM - 2nd TEXT, 1st ELEM - any pos - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001584 /* elem is always first, 2nd node is after it */
1585 if (item1->type == LYXP_NODE_ELEM) {
1586 assert(item2->type != LYXP_NODE_ELEM);
1587 return -1;
1588 }
1589
Michal Vasko9f96a052020-03-10 09:41:45 +01001590 /* 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 +02001591 /* 2nd is before 1st */
Michal Vasko69730152020-10-09 16:30:07 +02001592 if (((item1->type == LYXP_NODE_TEXT) &&
1593 ((item2->type == LYXP_NODE_ELEM) || (item2->type == LYXP_NODE_META))) ||
1594 ((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_ELEM)) ||
1595 (((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_META)) &&
1596 (meta_pos1 > meta_pos2))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001597 return 1;
1598 }
1599
Michal Vasko9f96a052020-03-10 09:41:45 +01001600 /* 1st META - any pos - 2nd TEXT, 1st META <pos< - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001601 /* 2nd is after 1st */
1602 return -1;
1603}
1604
1605/**
1606 * @brief Set cast for comparisons.
1607 *
Michal Vasko8abcecc2022-07-28 09:55:01 +02001608 * @param[in,out] trg Target set to cast source into.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001609 * @param[in] src Source set.
1610 * @param[in] type Target set type.
1611 * @param[in] src_idx Source set node index.
Michal Vasko8abcecc2022-07-28 09:55:01 +02001612 * @return LY_SUCCESS on success.
1613 * @return LY_ERR value on error.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001614 */
1615static LY_ERR
Michal Vasko8abcecc2022-07-28 09:55:01 +02001616set_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 +02001617{
1618 assert(src->type == LYXP_SET_NODE_SET);
1619
1620 set_init(trg, src);
1621
1622 /* insert node into target set */
1623 set_insert_node(trg, src->val.nodes[src_idx].node, src->val.nodes[src_idx].pos, src->val.nodes[src_idx].type, 0);
1624
1625 /* cast target set appropriately */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001626 return lyxp_set_cast(trg, type);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001627}
1628
Michal Vasko4c7763f2020-07-27 17:40:37 +02001629/**
1630 * @brief Set content canonization for comparisons.
1631 *
Michal Vasko8abcecc2022-07-28 09:55:01 +02001632 * @param[in,out] set Set to canonize.
Michal Vasko4c7763f2020-07-27 17:40:37 +02001633 * @param[in] xp_node Source XPath node/meta to use for canonization.
Michal Vasko8abcecc2022-07-28 09:55:01 +02001634 * @return LY_SUCCESS on success.
1635 * @return LY_ERR value on error.
Michal Vasko4c7763f2020-07-27 17:40:37 +02001636 */
1637static LY_ERR
Michal Vasko8abcecc2022-07-28 09:55:01 +02001638set_comp_canonize(struct lyxp_set *set, const struct lyxp_set_node *xp_node)
Michal Vasko4c7763f2020-07-27 17:40:37 +02001639{
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001640 const struct lysc_type *type = NULL;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001641 struct lyd_value val;
1642 struct ly_err_item *err = NULL;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001643 LY_ERR rc;
1644
1645 /* is there anything to canonize even? */
Michal Vasko8abcecc2022-07-28 09:55:01 +02001646 if (set->type == LYXP_SET_STRING) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02001647 /* do we have a type to use for canonization? */
1648 if ((xp_node->type == LYXP_NODE_ELEM) && (xp_node->node->schema->nodetype & LYD_NODE_TERM)) {
1649 type = ((struct lyd_node_term *)xp_node->node)->value.realtype;
1650 } else if (xp_node->type == LYXP_NODE_META) {
1651 type = ((struct lyd_meta *)xp_node->node)->value.realtype;
1652 }
1653 }
1654 if (!type) {
Michal Vasko8abcecc2022-07-28 09:55:01 +02001655 /* no canonization needed/possible */
1656 return LY_SUCCESS;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001657 }
1658
Michal Vasko8abcecc2022-07-28 09:55:01 +02001659 /* check for built-in types without required canonization */
1660 if ((type->basetype == LY_TYPE_STRING) && (type->plugin->store == lyplg_type_store_string)) {
1661 /* string */
1662 return LY_SUCCESS;
1663 }
1664 if ((type->basetype == LY_TYPE_BOOL) && (type->plugin->store == lyplg_type_store_boolean)) {
1665 /* boolean */
1666 return LY_SUCCESS;
1667 }
1668 if ((type->basetype == LY_TYPE_ENUM) && (type->plugin->store == lyplg_type_store_enum)) {
1669 /* enumeration */
1670 return LY_SUCCESS;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001671 }
1672
Michal Vasko8abcecc2022-07-28 09:55:01 +02001673 /* print canonized string, ignore errors, the value may not satisfy schema constraints */
1674 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 +01001675 LYD_HINT_DATA, xp_node->node->schema, &val, NULL, &err);
Michal Vasko4c7763f2020-07-27 17:40:37 +02001676 ly_err_free(err);
1677 if (rc) {
Michal Vasko8abcecc2022-07-28 09:55:01 +02001678 /* invalid value, function store automaticaly dealloc value when fail */
1679 return LY_SUCCESS;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001680 }
1681
Michal Vasko8abcecc2022-07-28 09:55:01 +02001682 /* use the canonized string value */
1683 free(set->val.str);
1684 set->val.str = strdup(lyd_value_get_canonical(set->ctx, &val));
1685 type->plugin->free(set->ctx, &val);
1686 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), LY_EMEM);
Michal Vasko4c7763f2020-07-27 17:40:37 +02001687
Michal Vasko4c7763f2020-07-27 17:40:37 +02001688 return LY_SUCCESS;
1689}
1690
Michal Vasko03ff5a72019-09-11 13:49:33 +02001691/**
1692 * @brief Bubble sort @p set into XPath document order.
Michal Vasko49fec8e2022-05-24 10:28:33 +02001693 * Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001694 *
1695 * @param[in] set Set to sort.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001696 * @return How many times the whole set was traversed - 1 (if set was sorted, returns 0).
1697 */
1698static int
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001699set_sort(struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001700{
1701 uint32_t i, j;
Radek Krejci1deb5be2020-08-26 16:43:36 +02001702 int ret = 0, cmp;
Radek Krejci857189e2020-09-01 13:26:36 +02001703 ly_bool inverted, change;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001704 const struct lyd_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001705 struct lyxp_set_node item;
1706 struct lyxp_set_hash_node hnode;
1707 uint64_t hash;
1708
Michal Vasko3cf8fbf2020-05-27 15:21:21 +02001709 if ((set->type != LYXP_SET_NODE_SET) || (set->used < 2)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001710 return 0;
1711 }
1712
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001713 /* find first top-level node to be used as anchor for positions */
Michal Vasko4a7d4d62021-12-13 17:05:06 +01001714 for (root = set->tree; root->parent; root = lyd_parent(root)) {}
Michal Vaskod989ba02020-08-24 10:59:24 +02001715 for ( ; root->prev->next; root = root->prev) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001716
1717 /* fill positions */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001718 if (set_assign_pos(set, root, set->root_type)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001719 return -1;
1720 }
1721
Michal Vasko88a9e802022-05-24 10:50:28 +02001722#ifndef NDEBUG
Michal Vasko03ff5a72019-09-11 13:49:33 +02001723 LOGDBG(LY_LDGXPATH, "SORT BEGIN");
1724 print_set_debug(set);
Michal Vasko88a9e802022-05-24 10:50:28 +02001725#endif
Michal Vasko03ff5a72019-09-11 13:49:33 +02001726
1727 for (i = 0; i < set->used; ++i) {
1728 inverted = 0;
1729 change = 0;
1730
1731 for (j = 1; j < set->used - i; ++j) {
1732 /* compare node positions */
1733 if (inverted) {
1734 cmp = set_sort_compare(&set->val.nodes[j], &set->val.nodes[j - 1]);
1735 } else {
1736 cmp = set_sort_compare(&set->val.nodes[j - 1], &set->val.nodes[j]);
1737 }
1738
1739 /* swap if needed */
1740 if ((inverted && (cmp < 0)) || (!inverted && (cmp > 0))) {
1741 change = 1;
1742
1743 item = set->val.nodes[j - 1];
1744 set->val.nodes[j - 1] = set->val.nodes[j];
1745 set->val.nodes[j] = item;
1746 } else {
1747 /* whether node_pos1 should be smaller than node_pos2 or the other way around */
1748 inverted = !inverted;
1749 }
1750 }
1751
1752 ++ret;
1753
1754 if (!change) {
1755 break;
1756 }
1757 }
1758
Michal Vasko88a9e802022-05-24 10:50:28 +02001759#ifndef NDEBUG
Michal Vasko03ff5a72019-09-11 13:49:33 +02001760 LOGDBG(LY_LDGXPATH, "SORT END %d", ret);
1761 print_set_debug(set);
Michal Vasko88a9e802022-05-24 10:50:28 +02001762#endif
Michal Vasko03ff5a72019-09-11 13:49:33 +02001763
1764 /* check node hashes */
1765 if (set->used >= LYD_HT_MIN_ITEMS) {
1766 assert(set->ht);
1767 for (i = 0; i < set->used; ++i) {
1768 hnode.node = set->val.nodes[i].node;
1769 hnode.type = set->val.nodes[i].type;
1770
1771 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
1772 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
1773 hash = dict_hash_multi(hash, NULL, 0);
1774
1775 assert(!lyht_find(set->ht, &hnode, hash, NULL));
1776 }
1777 }
1778
1779 return ret - 1;
1780}
1781
Michal Vasko03ff5a72019-09-11 13:49:33 +02001782/**
1783 * @brief Merge 2 sorted sets into one.
1784 *
1785 * @param[in,out] trg Set to merge into. Duplicates are removed.
1786 * @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 +02001787 * @return LY_ERR
1788 */
1789static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001790set_sorted_merge(struct lyxp_set *trg, struct lyxp_set *src)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001791{
1792 uint32_t i, j, k, count, dup_count;
1793 int cmp;
1794 const struct lyd_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001795
Michal Vaskod3678892020-05-21 10:06:58 +02001796 if ((trg->type != LYXP_SET_NODE_SET) || (src->type != LYXP_SET_NODE_SET)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001797 return LY_EINVAL;
1798 }
1799
Michal Vaskod3678892020-05-21 10:06:58 +02001800 if (!src->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001801 return LY_SUCCESS;
Michal Vaskod3678892020-05-21 10:06:58 +02001802 } else if (!trg->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001803 set_fill_set(trg, src);
Michal Vaskod3678892020-05-21 10:06:58 +02001804 lyxp_set_free_content(src);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001805 return LY_SUCCESS;
1806 }
1807
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001808 /* find first top-level node to be used as anchor for positions */
Michal Vasko0143b682021-12-13 17:13:09 +01001809 for (root = trg->tree; root->parent; root = lyd_parent(root)) {}
Michal Vaskod989ba02020-08-24 10:59:24 +02001810 for ( ; root->prev->next; root = root->prev) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001811
1812 /* fill positions */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001813 if (set_assign_pos(trg, root, trg->root_type) || set_assign_pos(src, root, src->root_type)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001814 return LY_EINT;
1815 }
1816
1817#ifndef NDEBUG
1818 LOGDBG(LY_LDGXPATH, "MERGE target");
1819 print_set_debug(trg);
1820 LOGDBG(LY_LDGXPATH, "MERGE source");
1821 print_set_debug(src);
1822#endif
1823
1824 /* make memory for the merge (duplicates are not detected yet, so space
1825 * will likely be wasted on them, too bad) */
1826 if (trg->size - trg->used < src->used) {
1827 trg->size = trg->used + src->used;
1828
1829 trg->val.nodes = ly_realloc(trg->val.nodes, trg->size * sizeof *trg->val.nodes);
1830 LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx), LY_EMEM);
1831 }
1832
1833 i = 0;
1834 j = 0;
1835 count = 0;
1836 dup_count = 0;
1837 do {
1838 cmp = set_sort_compare(&src->val.nodes[i], &trg->val.nodes[j]);
1839 if (!cmp) {
1840 if (!count) {
1841 /* duplicate, just skip it */
1842 ++i;
1843 ++j;
1844 } else {
1845 /* we are copying something already, so let's copy the duplicate too,
1846 * we are hoping that afterwards there are some more nodes to
1847 * copy and this way we can copy them all together */
1848 ++count;
1849 ++dup_count;
1850 ++i;
1851 ++j;
1852 }
1853 } else if (cmp < 0) {
1854 /* inserting src node into trg, just remember it for now */
1855 ++count;
1856 ++i;
1857
1858 /* insert the hash now */
1859 set_insert_node_hash(trg, src->val.nodes[i - 1].node, src->val.nodes[i - 1].type);
1860 } else if (count) {
1861copy_nodes:
1862 /* time to actually copy the nodes, we have found the largest block of nodes */
1863 memmove(&trg->val.nodes[j + (count - dup_count)],
1864 &trg->val.nodes[j],
1865 (trg->used - j) * sizeof *trg->val.nodes);
1866 memcpy(&trg->val.nodes[j - dup_count], &src->val.nodes[i - count], count * sizeof *src->val.nodes);
1867
1868 trg->used += count - dup_count;
1869 /* do not change i, except the copying above, we are basically doing exactly what is in the else branch below */
1870 j += count - dup_count;
1871
1872 count = 0;
1873 dup_count = 0;
1874 } else {
1875 ++j;
1876 }
1877 } while ((i < src->used) && (j < trg->used));
1878
1879 if ((i < src->used) || count) {
1880 /* insert all the hashes first */
1881 for (k = i; k < src->used; ++k) {
1882 set_insert_node_hash(trg, src->val.nodes[k].node, src->val.nodes[k].type);
1883 }
1884
1885 /* loop ended, but we need to copy something at trg end */
1886 count += src->used - i;
1887 i = src->used;
1888 goto copy_nodes;
1889 }
1890
1891 /* we are inserting hashes before the actual node insert, which causes
1892 * situations when there were initially not enough items for a hash table,
1893 * but even after some were inserted, hash table was not created (during
1894 * insertion the number of items is not updated yet) */
1895 if (!trg->ht && (trg->used >= LYD_HT_MIN_ITEMS)) {
1896 set_insert_node_hash(trg, NULL, 0);
1897 }
1898
1899#ifndef NDEBUG
1900 LOGDBG(LY_LDGXPATH, "MERGE result");
1901 print_set_debug(trg);
1902#endif
1903
Michal Vaskod3678892020-05-21 10:06:58 +02001904 lyxp_set_free_content(src);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001905 return LY_SUCCESS;
1906}
1907
Michal Vasko14676352020-05-29 11:35:55 +02001908LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02001909lyxp_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 +02001910{
Michal Vasko004d3152020-06-11 19:59:22 +02001911 if (exp->used == tok_idx) {
Michal Vasko14676352020-05-29 11:35:55 +02001912 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001913 LOGVAL(ctx, LY_VCODE_XP_EOF);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001914 }
Michal Vasko14676352020-05-29 11:35:55 +02001915 return LY_EINCOMPLETE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001916 }
1917
Michal Vasko004d3152020-06-11 19:59:22 +02001918 if (want_tok && (exp->tokens[tok_idx] != want_tok)) {
Michal Vasko14676352020-05-29 11:35:55 +02001919 if (ctx) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02001920 LOGVAL(ctx, LY_VCODE_XP_INTOK2, lyxp_token2str(exp->tokens[tok_idx]),
1921 &exp->expr[exp->tok_pos[tok_idx]], lyxp_token2str(want_tok));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001922 }
Michal Vasko14676352020-05-29 11:35:55 +02001923 return LY_ENOT;
1924 }
1925
1926 return LY_SUCCESS;
1927}
1928
Michal Vasko004d3152020-06-11 19:59:22 +02001929LY_ERR
1930lyxp_next_token(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_token want_tok)
1931{
1932 LY_CHECK_RET(lyxp_check_token(ctx, exp, *tok_idx, want_tok));
1933
1934 /* skip the token */
1935 ++(*tok_idx);
1936
1937 return LY_SUCCESS;
1938}
1939
Michal Vasko14676352020-05-29 11:35:55 +02001940/* just like lyxp_check_token() but tests for 2 tokens */
1941static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02001942exp_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 +02001943 enum lyxp_token want_tok2)
Michal Vasko14676352020-05-29 11:35:55 +02001944{
Michal Vasko004d3152020-06-11 19:59:22 +02001945 if (exp->used == tok_idx) {
Michal Vasko14676352020-05-29 11:35:55 +02001946 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001947 LOGVAL(ctx, LY_VCODE_XP_EOF);
Michal Vasko14676352020-05-29 11:35:55 +02001948 }
1949 return LY_EINCOMPLETE;
1950 }
1951
Michal Vasko004d3152020-06-11 19:59:22 +02001952 if ((exp->tokens[tok_idx] != want_tok1) && (exp->tokens[tok_idx] != want_tok2)) {
Michal Vasko14676352020-05-29 11:35:55 +02001953 if (ctx) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02001954 LOGVAL(ctx, LY_VCODE_XP_INTOK, lyxp_token2str(exp->tokens[tok_idx]),
Michal Vasko0b468e62020-10-19 09:33:04 +02001955 &exp->expr[exp->tok_pos[tok_idx]]);
Michal Vasko14676352020-05-29 11:35:55 +02001956 }
1957 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001958 }
1959
1960 return LY_SUCCESS;
1961}
1962
Michal Vasko4911eeb2021-06-28 11:23:05 +02001963LY_ERR
1964lyxp_next_token2(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_token want_tok1,
1965 enum lyxp_token want_tok2)
1966{
1967 LY_CHECK_RET(exp_check_token2(ctx, exp, *tok_idx, want_tok1, want_tok2));
1968
1969 /* skip the token */
1970 ++(*tok_idx);
1971
1972 return LY_SUCCESS;
1973}
1974
Michal Vasko03ff5a72019-09-11 13:49:33 +02001975/**
1976 * @brief Stack operation push on the repeat array.
1977 *
1978 * @param[in] exp Expression to use.
Michal Vasko004d3152020-06-11 19:59:22 +02001979 * @param[in] tok_idx Position in the expresion \p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001980 * @param[in] repeat_op_idx Index from \p exp of the operator token. This value is pushed.
1981 */
1982static void
Michal Vasko004d3152020-06-11 19:59:22 +02001983exp_repeat_push(struct lyxp_expr *exp, uint16_t tok_idx, uint16_t repeat_op_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001984{
1985 uint16_t i;
1986
Michal Vasko004d3152020-06-11 19:59:22 +02001987 if (exp->repeat[tok_idx]) {
Radek Krejci1e008d22020-08-17 11:37:37 +02001988 for (i = 0; exp->repeat[tok_idx][i]; ++i) {}
Michal Vasko004d3152020-06-11 19:59:22 +02001989 exp->repeat[tok_idx] = realloc(exp->repeat[tok_idx], (i + 2) * sizeof *exp->repeat[tok_idx]);
1990 LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), );
1991 exp->repeat[tok_idx][i] = repeat_op_idx;
1992 exp->repeat[tok_idx][i + 1] = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001993 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02001994 exp->repeat[tok_idx] = calloc(2, sizeof *exp->repeat[tok_idx]);
1995 LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), );
1996 exp->repeat[tok_idx][0] = repeat_op_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001997 }
1998}
1999
2000/**
2001 * @brief Reparse Predicate. Logs directly on error.
2002 *
2003 * [7] Predicate ::= '[' Expr ']'
2004 *
2005 * @param[in] ctx Context for logging.
2006 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002007 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002008 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002009 * @return LY_ERR
2010 */
2011static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002012reparse_predicate(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t depth)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002013{
2014 LY_ERR rc;
2015
Michal Vasko004d3152020-06-11 19:59:22 +02002016 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002017 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002018 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002019
aPiecekbf968d92021-05-27 14:35:05 +02002020 rc = reparse_or_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002021 LY_CHECK_RET(rc);
2022
Michal Vasko004d3152020-06-11 19:59:22 +02002023 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002024 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002025 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002026
2027 return LY_SUCCESS;
2028}
2029
2030/**
2031 * @brief Reparse RelativeLocationPath. Logs directly on error.
2032 *
2033 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
2034 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
2035 * [6] NodeTest ::= NameTest | NodeType '(' ')'
2036 *
2037 * @param[in] ctx Context for logging.
2038 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002039 * @param[in] tok_idx Position in the expression \p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002040 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002041 * @return LY_ERR (LY_EINCOMPLETE on forward reference)
2042 */
2043static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002044reparse_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 +02002045{
2046 LY_ERR rc;
2047
Michal Vasko004d3152020-06-11 19:59:22 +02002048 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002049 LY_CHECK_RET(rc);
2050
2051 goto step;
2052 do {
2053 /* '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02002054 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002055
Michal Vasko004d3152020-06-11 19:59:22 +02002056 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002057 LY_CHECK_RET(rc);
2058step:
2059 /* Step */
Michal Vasko004d3152020-06-11 19:59:22 +02002060 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002061 case LYXP_TOKEN_DOT:
Michal Vasko004d3152020-06-11 19:59:22 +02002062 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002063 break;
2064
2065 case LYXP_TOKEN_DDOT:
Michal Vasko004d3152020-06-11 19:59:22 +02002066 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002067 break;
2068
Michal Vasko49fec8e2022-05-24 10:28:33 +02002069 case LYXP_TOKEN_AXISNAME:
2070 ++(*tok_idx);
2071
2072 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_DCOLON);
2073 LY_CHECK_RET(rc);
2074
2075 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02002076 case LYXP_TOKEN_AT:
Michal Vasko004d3152020-06-11 19:59:22 +02002077 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002078
Michal Vasko004d3152020-06-11 19:59:22 +02002079 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002080 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002081 if ((exp->tokens[*tok_idx] != LYXP_TOKEN_NAMETEST) && (exp->tokens[*tok_idx] != LYXP_TOKEN_NODETYPE)) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02002082 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 +02002083 return LY_EVALID;
2084 }
Michal Vasko49fec8e2022-05-24 10:28:33 +02002085 if (exp->tokens[*tok_idx] == LYXP_TOKEN_NODETYPE) {
2086 goto reparse_nodetype;
2087 }
Radek Krejci0f969882020-08-21 16:56:47 +02002088 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02002089 case LYXP_TOKEN_NAMETEST:
Michal Vasko004d3152020-06-11 19:59:22 +02002090 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002091 goto reparse_predicate;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002092
2093 case LYXP_TOKEN_NODETYPE:
Michal Vasko49fec8e2022-05-24 10:28:33 +02002094reparse_nodetype:
Michal Vasko004d3152020-06-11 19:59:22 +02002095 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002096
2097 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02002098 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002099 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002100 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002101
2102 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02002103 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002104 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002105 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002106
2107reparse_predicate:
2108 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002109 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
aPiecekbf968d92021-05-27 14:35:05 +02002110 rc = reparse_predicate(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002111 LY_CHECK_RET(rc);
2112 }
2113 break;
2114 default:
Michal Vasko49fec8e2022-05-24 10:28:33 +02002115 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 +02002116 return LY_EVALID;
2117 }
Michal Vasko004d3152020-06-11 19:59:22 +02002118 } while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02002119
2120 return LY_SUCCESS;
2121}
2122
2123/**
2124 * @brief Reparse AbsoluteLocationPath. Logs directly on error.
2125 *
2126 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
2127 *
2128 * @param[in] ctx Context for logging.
2129 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002130 * @param[in] tok_idx Position in the expression \p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002131 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002132 * @return LY_ERR
2133 */
2134static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002135reparse_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 +02002136{
2137 LY_ERR rc;
2138
Michal Vasko004d3152020-06-11 19:59:22 +02002139 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 +02002140
2141 /* '/' RelativeLocationPath? */
Michal Vasko004d3152020-06-11 19:59:22 +02002142 if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002143 /* '/' */
Michal Vasko004d3152020-06-11 19:59:22 +02002144 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002145
Michal Vasko004d3152020-06-11 19:59:22 +02002146 if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002147 return LY_SUCCESS;
2148 }
Michal Vasko004d3152020-06-11 19:59:22 +02002149 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002150 case LYXP_TOKEN_DOT:
2151 case LYXP_TOKEN_DDOT:
Michal Vasko49fec8e2022-05-24 10:28:33 +02002152 case LYXP_TOKEN_AXISNAME:
Michal Vasko03ff5a72019-09-11 13:49:33 +02002153 case LYXP_TOKEN_AT:
2154 case LYXP_TOKEN_NAMETEST:
2155 case LYXP_TOKEN_NODETYPE:
aPiecekbf968d92021-05-27 14:35:05 +02002156 rc = reparse_relative_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002157 LY_CHECK_RET(rc);
Radek Krejci0f969882020-08-21 16:56:47 +02002158 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02002159 default:
2160 break;
2161 }
2162
Michal Vasko03ff5a72019-09-11 13:49:33 +02002163 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02002164 /* '//' RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002165 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002166
aPiecekbf968d92021-05-27 14:35:05 +02002167 rc = reparse_relative_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002168 LY_CHECK_RET(rc);
2169 }
2170
2171 return LY_SUCCESS;
2172}
2173
2174/**
2175 * @brief Reparse FunctionCall. Logs directly on error.
2176 *
2177 * [9] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
2178 *
2179 * @param[in] ctx Context for logging.
2180 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002181 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002182 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002183 * @return LY_ERR
2184 */
2185static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002186reparse_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 +02002187{
Radek Krejci1deb5be2020-08-26 16:43:36 +02002188 int8_t min_arg_count = -1;
2189 uint32_t arg_count, max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002190 uint16_t func_tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002191 LY_ERR rc;
2192
Michal Vasko004d3152020-06-11 19:59:22 +02002193 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_FUNCNAME);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002194 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002195 func_tok_idx = *tok_idx;
2196 switch (exp->tok_len[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002197 case 3:
Michal Vasko004d3152020-06-11 19:59:22 +02002198 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002199 min_arg_count = 1;
2200 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002201 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002202 min_arg_count = 1;
2203 max_arg_count = 1;
2204 }
2205 break;
2206 case 4:
Michal Vasko004d3152020-06-11 19:59:22 +02002207 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002208 min_arg_count = 1;
2209 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002210 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002211 min_arg_count = 0;
2212 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002213 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002214 min_arg_count = 0;
2215 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002216 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002217 min_arg_count = 0;
2218 max_arg_count = 0;
2219 }
2220 break;
2221 case 5:
Michal Vasko004d3152020-06-11 19:59:22 +02002222 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002223 min_arg_count = 1;
2224 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002225 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002226 min_arg_count = 0;
2227 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002228 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002229 min_arg_count = 1;
2230 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002231 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002232 min_arg_count = 1;
2233 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002234 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002235 min_arg_count = 1;
2236 max_arg_count = 1;
2237 }
2238 break;
2239 case 6:
Michal Vasko004d3152020-06-11 19:59:22 +02002240 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002241 min_arg_count = 2;
Radek Krejci1deb5be2020-08-26 16:43:36 +02002242 max_arg_count = UINT32_MAX;
Michal Vasko004d3152020-06-11 19:59:22 +02002243 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002244 min_arg_count = 0;
2245 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002246 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002247 min_arg_count = 0;
2248 max_arg_count = 1;
2249 }
2250 break;
2251 case 7:
Michal Vasko004d3152020-06-11 19:59:22 +02002252 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002253 min_arg_count = 1;
2254 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002255 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002256 min_arg_count = 1;
2257 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002258 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002259 min_arg_count = 0;
2260 max_arg_count = 0;
2261 }
2262 break;
2263 case 8:
Michal Vasko004d3152020-06-11 19:59:22 +02002264 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002265 min_arg_count = 2;
2266 max_arg_count = 2;
Michal Vasko004d3152020-06-11 19:59:22 +02002267 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002268 min_arg_count = 0;
2269 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002270 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "re-match", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002271 min_arg_count = 2;
2272 max_arg_count = 2;
2273 }
2274 break;
2275 case 9:
Michal Vasko004d3152020-06-11 19:59:22 +02002276 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002277 min_arg_count = 2;
2278 max_arg_count = 3;
Michal Vasko004d3152020-06-11 19:59:22 +02002279 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002280 min_arg_count = 3;
2281 max_arg_count = 3;
2282 }
2283 break;
2284 case 10:
Michal Vasko004d3152020-06-11 19:59:22 +02002285 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002286 min_arg_count = 0;
2287 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002288 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002289 min_arg_count = 1;
2290 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002291 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002292 min_arg_count = 2;
2293 max_arg_count = 2;
2294 }
2295 break;
2296 case 11:
Michal Vasko004d3152020-06-11 19:59:22 +02002297 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002298 min_arg_count = 2;
2299 max_arg_count = 2;
2300 }
2301 break;
2302 case 12:
Michal Vasko004d3152020-06-11 19:59:22 +02002303 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002304 min_arg_count = 2;
2305 max_arg_count = 2;
2306 }
2307 break;
2308 case 13:
Michal Vasko004d3152020-06-11 19:59:22 +02002309 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002310 min_arg_count = 0;
2311 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002312 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string-length", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002313 min_arg_count = 0;
2314 max_arg_count = 1;
2315 }
2316 break;
2317 case 15:
Michal Vasko004d3152020-06-11 19:59:22 +02002318 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002319 min_arg_count = 0;
2320 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002321 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-after", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002322 min_arg_count = 2;
2323 max_arg_count = 2;
2324 }
2325 break;
2326 case 16:
Michal Vasko004d3152020-06-11 19:59:22 +02002327 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002328 min_arg_count = 2;
2329 max_arg_count = 2;
2330 }
2331 break;
2332 case 20:
Michal Vasko004d3152020-06-11 19:59:22 +02002333 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from-or-self", 20)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002334 min_arg_count = 2;
2335 max_arg_count = 2;
2336 }
2337 break;
2338 }
2339 if (min_arg_count == -1) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002340 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 +02002341 return LY_EINVAL;
2342 }
Michal Vasko004d3152020-06-11 19:59:22 +02002343 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002344
2345 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02002346 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002347 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002348 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002349
2350 /* ( Expr ( ',' Expr )* )? */
2351 arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002352 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002353 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002354 if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002355 ++arg_count;
aPiecekbf968d92021-05-27 14:35:05 +02002356 rc = reparse_or_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002357 LY_CHECK_RET(rc);
2358 }
Michal Vasko004d3152020-06-11 19:59:22 +02002359 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) {
2360 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002361
2362 ++arg_count;
aPiecekbf968d92021-05-27 14:35:05 +02002363 rc = reparse_or_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002364 LY_CHECK_RET(rc);
2365 }
2366
2367 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02002368 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002369 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002370 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002371
Radek Krejci857189e2020-09-01 13:26:36 +02002372 if ((arg_count < (uint32_t)min_arg_count) || (arg_count > max_arg_count)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002373 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 +02002374 return LY_EVALID;
2375 }
2376
2377 return LY_SUCCESS;
2378}
2379
2380/**
2381 * @brief Reparse PathExpr. Logs directly on error.
2382 *
2383 * [10] PathExpr ::= LocationPath | PrimaryExpr Predicate*
2384 * | PrimaryExpr Predicate* '/' RelativeLocationPath
2385 * | PrimaryExpr Predicate* '//' RelativeLocationPath
2386 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
aPiecekfba75362021-10-07 12:39:48 +02002387 * [8] PrimaryExpr ::= VariableReference | '(' Expr ')' | Literal | Number | FunctionCall
Michal Vasko03ff5a72019-09-11 13:49:33 +02002388 *
2389 * @param[in] ctx Context for logging.
2390 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002391 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002392 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002393 * @return LY_ERR
2394 */
2395static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002396reparse_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 +02002397{
2398 LY_ERR rc;
2399
Michal Vasko004d3152020-06-11 19:59:22 +02002400 if (lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko14676352020-05-29 11:35:55 +02002401 return LY_EVALID;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002402 }
2403
Michal Vasko004d3152020-06-11 19:59:22 +02002404 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002405 case LYXP_TOKEN_PAR1:
2406 /* '(' Expr ')' Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002407 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002408
aPiecekbf968d92021-05-27 14:35:05 +02002409 rc = reparse_or_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002410 LY_CHECK_RET(rc);
2411
Michal Vasko004d3152020-06-11 19:59:22 +02002412 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002413 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002414 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002415 goto predicate;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002416 case LYXP_TOKEN_DOT:
2417 case LYXP_TOKEN_DDOT:
Michal Vasko49fec8e2022-05-24 10:28:33 +02002418 case LYXP_TOKEN_AXISNAME:
Michal Vasko03ff5a72019-09-11 13:49:33 +02002419 case LYXP_TOKEN_AT:
2420 case LYXP_TOKEN_NAMETEST:
2421 case LYXP_TOKEN_NODETYPE:
2422 /* RelativeLocationPath */
aPiecekbf968d92021-05-27 14:35:05 +02002423 rc = reparse_relative_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002424 LY_CHECK_RET(rc);
2425 break;
aPiecekfba75362021-10-07 12:39:48 +02002426 case LYXP_TOKEN_VARREF:
2427 /* VariableReference */
2428 ++(*tok_idx);
2429 goto predicate;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002430 case LYXP_TOKEN_FUNCNAME:
2431 /* FunctionCall */
aPiecekbf968d92021-05-27 14:35:05 +02002432 rc = reparse_function_call(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002433 LY_CHECK_RET(rc);
2434 goto predicate;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002435 case LYXP_TOKEN_OPER_PATH:
2436 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +02002437 /* AbsoluteLocationPath */
aPiecekbf968d92021-05-27 14:35:05 +02002438 rc = reparse_absolute_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002439 LY_CHECK_RET(rc);
2440 break;
2441 case LYXP_TOKEN_LITERAL:
2442 /* Literal */
Michal Vasko004d3152020-06-11 19:59:22 +02002443 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002444 goto predicate;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002445 case LYXP_TOKEN_NUMBER:
2446 /* Number */
Michal Vasko004d3152020-06-11 19:59:22 +02002447 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002448 goto predicate;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002449 default:
Michal Vasko49fec8e2022-05-24 10:28:33 +02002450 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 +02002451 return LY_EVALID;
2452 }
2453
2454 return LY_SUCCESS;
2455
2456predicate:
2457 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002458 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
aPiecekbf968d92021-05-27 14:35:05 +02002459 rc = reparse_predicate(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002460 LY_CHECK_RET(rc);
2461 }
2462
2463 /* ('/' or '//') RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002464 if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002465
2466 /* '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02002467 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002468
aPiecekbf968d92021-05-27 14:35:05 +02002469 rc = reparse_relative_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002470 LY_CHECK_RET(rc);
2471 }
2472
2473 return LY_SUCCESS;
2474}
2475
2476/**
2477 * @brief Reparse UnaryExpr. Logs directly on error.
2478 *
2479 * [17] UnaryExpr ::= UnionExpr | '-' UnaryExpr
2480 * [18] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
2481 *
2482 * @param[in] ctx Context for logging.
2483 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002484 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002485 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002486 * @return LY_ERR
2487 */
2488static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002489reparse_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 +02002490{
2491 uint16_t prev_exp;
2492 LY_ERR rc;
2493
2494 /* ('-')* */
Michal Vasko004d3152020-06-11 19:59:22 +02002495 prev_exp = *tok_idx;
Michal Vasko69730152020-10-09 16:30:07 +02002496 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2497 (exp->expr[exp->tok_pos[*tok_idx]] == '-')) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002498 exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNARY);
Michal Vasko004d3152020-06-11 19:59:22 +02002499 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002500 }
2501
2502 /* PathExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002503 prev_exp = *tok_idx;
aPiecekbf968d92021-05-27 14:35:05 +02002504 rc = reparse_path_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002505 LY_CHECK_RET(rc);
2506
2507 /* ('|' PathExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002508 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_UNI)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002509 exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNION);
Michal Vasko004d3152020-06-11 19:59:22 +02002510 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002511
aPiecekbf968d92021-05-27 14:35:05 +02002512 rc = reparse_path_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002513 LY_CHECK_RET(rc);
2514 }
2515
2516 return LY_SUCCESS;
2517}
2518
2519/**
2520 * @brief Reparse AdditiveExpr. Logs directly on error.
2521 *
2522 * [15] AdditiveExpr ::= MultiplicativeExpr
2523 * | AdditiveExpr '+' MultiplicativeExpr
2524 * | AdditiveExpr '-' MultiplicativeExpr
2525 * [16] MultiplicativeExpr ::= UnaryExpr
2526 * | MultiplicativeExpr '*' UnaryExpr
2527 * | MultiplicativeExpr 'div' UnaryExpr
2528 * | MultiplicativeExpr 'mod' UnaryExpr
2529 *
2530 * @param[in] ctx Context for logging.
2531 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002532 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002533 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002534 * @return LY_ERR
2535 */
2536static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002537reparse_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 +02002538{
2539 uint16_t prev_add_exp, prev_mul_exp;
2540 LY_ERR rc;
2541
Michal Vasko004d3152020-06-11 19:59:22 +02002542 prev_add_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002543 goto reparse_multiplicative_expr;
2544
2545 /* ('+' / '-' MultiplicativeExpr)* */
Michal Vasko69730152020-10-09 16:30:07 +02002546 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2547 ((exp->expr[exp->tok_pos[*tok_idx]] == '+') || (exp->expr[exp->tok_pos[*tok_idx]] == '-'))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002548 exp_repeat_push(exp, prev_add_exp, LYXP_EXPR_ADDITIVE);
Michal Vasko004d3152020-06-11 19:59:22 +02002549 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002550
2551reparse_multiplicative_expr:
2552 /* UnaryExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002553 prev_mul_exp = *tok_idx;
aPiecekbf968d92021-05-27 14:35:05 +02002554 rc = reparse_unary_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002555 LY_CHECK_RET(rc);
2556
2557 /* ('*' / 'div' / 'mod' UnaryExpr)* */
Michal Vasko69730152020-10-09 16:30:07 +02002558 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2559 ((exp->expr[exp->tok_pos[*tok_idx]] == '*') || (exp->tok_len[*tok_idx] == 3))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002560 exp_repeat_push(exp, prev_mul_exp, LYXP_EXPR_MULTIPLICATIVE);
Michal Vasko004d3152020-06-11 19:59:22 +02002561 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002562
aPiecekbf968d92021-05-27 14:35:05 +02002563 rc = reparse_unary_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002564 LY_CHECK_RET(rc);
2565 }
2566 }
2567
2568 return LY_SUCCESS;
2569}
2570
2571/**
2572 * @brief Reparse EqualityExpr. Logs directly on error.
2573 *
2574 * [13] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
2575 * | EqualityExpr '!=' RelationalExpr
2576 * [14] RelationalExpr ::= AdditiveExpr
2577 * | RelationalExpr '<' AdditiveExpr
2578 * | RelationalExpr '>' AdditiveExpr
2579 * | RelationalExpr '<=' AdditiveExpr
2580 * | RelationalExpr '>=' AdditiveExpr
2581 *
2582 * @param[in] ctx Context for logging.
2583 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002584 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002585 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002586 * @return LY_ERR
2587 */
2588static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002589reparse_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 +02002590{
2591 uint16_t prev_eq_exp, prev_rel_exp;
2592 LY_ERR rc;
2593
Michal Vasko004d3152020-06-11 19:59:22 +02002594 prev_eq_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002595 goto reparse_additive_expr;
2596
2597 /* ('=' / '!=' RelationalExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002598 while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_EQUAL, LYXP_TOKEN_OPER_NEQUAL)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002599 exp_repeat_push(exp, prev_eq_exp, LYXP_EXPR_EQUALITY);
Michal Vasko004d3152020-06-11 19:59:22 +02002600 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002601
2602reparse_additive_expr:
2603 /* AdditiveExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002604 prev_rel_exp = *tok_idx;
aPiecekbf968d92021-05-27 14:35:05 +02002605 rc = reparse_additive_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002606 LY_CHECK_RET(rc);
2607
2608 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002609 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_COMP)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002610 exp_repeat_push(exp, prev_rel_exp, LYXP_EXPR_RELATIONAL);
Michal Vasko004d3152020-06-11 19:59:22 +02002611 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002612
aPiecekbf968d92021-05-27 14:35:05 +02002613 rc = reparse_additive_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002614 LY_CHECK_RET(rc);
2615 }
2616 }
2617
2618 return LY_SUCCESS;
2619}
2620
2621/**
2622 * @brief Reparse OrExpr. Logs directly on error.
2623 *
2624 * [11] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
2625 * [12] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
2626 *
2627 * @param[in] ctx Context for logging.
2628 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002629 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002630 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002631 * @return LY_ERR
2632 */
2633static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002634reparse_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 +02002635{
2636 uint16_t prev_or_exp, prev_and_exp;
2637 LY_ERR rc;
2638
aPiecekbf968d92021-05-27 14:35:05 +02002639 ++depth;
2640 LY_CHECK_ERR_RET(depth > LYXP_MAX_BLOCK_DEPTH, LOGVAL(ctx, LY_VCODE_XP_DEPTH), LY_EINVAL);
2641
Michal Vasko004d3152020-06-11 19:59:22 +02002642 prev_or_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002643 goto reparse_equality_expr;
2644
2645 /* ('or' AndExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002646 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 +02002647 exp_repeat_push(exp, prev_or_exp, LYXP_EXPR_OR);
Michal Vasko004d3152020-06-11 19:59:22 +02002648 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002649
2650reparse_equality_expr:
2651 /* EqualityExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002652 prev_and_exp = *tok_idx;
aPiecekbf968d92021-05-27 14:35:05 +02002653 rc = reparse_equality_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002654 LY_CHECK_RET(rc);
2655
2656 /* ('and' EqualityExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002657 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 +02002658 exp_repeat_push(exp, prev_and_exp, LYXP_EXPR_AND);
Michal Vasko004d3152020-06-11 19:59:22 +02002659 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002660
aPiecekbf968d92021-05-27 14:35:05 +02002661 rc = reparse_equality_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002662 LY_CHECK_RET(rc);
2663 }
2664 }
2665
2666 return LY_SUCCESS;
2667}
Radek Krejcib1646a92018-11-02 16:08:26 +01002668
2669/**
2670 * @brief Parse NCName.
2671 *
2672 * @param[in] ncname Name to parse.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002673 * @return Length of @p ncname valid bytes.
Radek Krejcib1646a92018-11-02 16:08:26 +01002674 */
Michal Vasko49fec8e2022-05-24 10:28:33 +02002675static ssize_t
Radek Krejcib1646a92018-11-02 16:08:26 +01002676parse_ncname(const char *ncname)
2677{
Radek Krejci1deb5be2020-08-26 16:43:36 +02002678 uint32_t uc;
Radek Krejcid4270262019-01-07 15:07:25 +01002679 size_t size;
Michal Vasko49fec8e2022-05-24 10:28:33 +02002680 ssize_t len = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01002681
2682 LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), 0);
2683 if (!is_xmlqnamestartchar(uc) || (uc == ':')) {
2684 return len;
2685 }
2686
2687 do {
2688 len += size;
Radek Krejci9a564c92019-01-07 14:53:57 +01002689 if (!*ncname) {
2690 break;
2691 }
Radek Krejcid4270262019-01-07 15:07:25 +01002692 LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), -len);
Radek Krejcib1646a92018-11-02 16:08:26 +01002693 } while (is_xmlqnamechar(uc) && (uc != ':'));
2694
2695 return len;
2696}
2697
2698/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02002699 * @brief Add @p token into the expression @p exp.
Radek Krejcib1646a92018-11-02 16:08:26 +01002700 *
Michal Vasko03ff5a72019-09-11 13:49:33 +02002701 * @param[in] ctx Context for logging.
Radek Krejcib1646a92018-11-02 16:08:26 +01002702 * @param[in] exp Expression to use.
2703 * @param[in] token Token to add.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002704 * @param[in] tok_pos Token position in the XPath expression.
Radek Krejcib1646a92018-11-02 16:08:26 +01002705 * @param[in] tok_len Token length in the XPath expression.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002706 * @return LY_ERR
Radek Krejcib1646a92018-11-02 16:08:26 +01002707 */
2708static LY_ERR
Michal Vasko14676352020-05-29 11:35:55 +02002709exp_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 +01002710{
2711 uint32_t prev;
2712
2713 if (exp->used == exp->size) {
2714 prev = exp->size;
2715 exp->size += LYXP_EXPR_SIZE_STEP;
2716 if (prev > exp->size) {
2717 LOGINT(ctx);
2718 return LY_EINT;
2719 }
2720
2721 exp->tokens = ly_realloc(exp->tokens, exp->size * sizeof *exp->tokens);
2722 LY_CHECK_ERR_RET(!exp->tokens, LOGMEM(ctx), LY_EMEM);
2723 exp->tok_pos = ly_realloc(exp->tok_pos, exp->size * sizeof *exp->tok_pos);
2724 LY_CHECK_ERR_RET(!exp->tok_pos, LOGMEM(ctx), LY_EMEM);
2725 exp->tok_len = ly_realloc(exp->tok_len, exp->size * sizeof *exp->tok_len);
2726 LY_CHECK_ERR_RET(!exp->tok_len, LOGMEM(ctx), LY_EMEM);
2727 }
2728
2729 exp->tokens[exp->used] = token;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002730 exp->tok_pos[exp->used] = tok_pos;
Radek Krejcib1646a92018-11-02 16:08:26 +01002731 exp->tok_len[exp->used] = tok_len;
2732 ++exp->used;
2733 return LY_SUCCESS;
2734}
2735
2736void
Michal Vasko14676352020-05-29 11:35:55 +02002737lyxp_expr_free(const struct ly_ctx *ctx, struct lyxp_expr *expr)
Radek Krejcib1646a92018-11-02 16:08:26 +01002738{
2739 uint16_t i;
2740
2741 if (!expr) {
2742 return;
2743 }
2744
2745 lydict_remove(ctx, expr->expr);
2746 free(expr->tokens);
2747 free(expr->tok_pos);
2748 free(expr->tok_len);
2749 if (expr->repeat) {
2750 for (i = 0; i < expr->used; ++i) {
2751 free(expr->repeat[i]);
2752 }
2753 }
2754 free(expr->repeat);
2755 free(expr);
2756}
2757
Michal Vasko49fec8e2022-05-24 10:28:33 +02002758/**
2759 * @brief Parse Axis name.
2760 *
2761 * @param[in] str String to parse.
2762 * @param[in] str_len Length of @p str.
2763 * @return LY_SUCCESS if an axis.
2764 * @return LY_ENOT otherwise.
2765 */
2766static LY_ERR
2767expr_parse_axis(const char *str, size_t str_len)
2768{
2769 switch (str_len) {
2770 case 4:
2771 if (!strncmp("self", str, str_len)) {
2772 return LY_SUCCESS;
2773 }
2774 break;
2775 case 5:
2776 if (!strncmp("child", str, str_len)) {
2777 return LY_SUCCESS;
2778 }
2779 break;
2780 case 6:
2781 if (!strncmp("parent", str, str_len)) {
2782 return LY_SUCCESS;
2783 }
2784 break;
2785 case 8:
2786 if (!strncmp("ancestor", str, str_len)) {
2787 return LY_SUCCESS;
2788 }
2789 break;
2790 case 9:
2791 if (!strncmp("attribute", str, str_len)) {
2792 return LY_SUCCESS;
2793 } else if (!strncmp("following", str, str_len)) {
2794 return LY_SUCCESS;
2795 } else if (!strncmp("namespace", str, str_len)) {
2796 LOGERR(NULL, LY_EINVAL, "Axis \"namespace\" not supported.");
2797 return LY_ENOT;
2798 } else if (!strncmp("preceding", str, str_len)) {
2799 return LY_SUCCESS;
2800 }
2801 break;
2802 case 10:
2803 if (!strncmp("descendant", str, str_len)) {
2804 return LY_SUCCESS;
2805 }
2806 break;
2807 case 16:
2808 if (!strncmp("ancestor-or-self", str, str_len)) {
2809 return LY_SUCCESS;
2810 }
2811 break;
2812 case 17:
2813 if (!strncmp("following-sibling", str, str_len)) {
2814 return LY_SUCCESS;
2815 } else if (!strncmp("preceding-sibling", str, str_len)) {
2816 return LY_SUCCESS;
2817 }
2818 break;
2819 case 18:
2820 if (!strncmp("descendant-or-self", str, str_len)) {
2821 return LY_SUCCESS;
2822 }
2823 break;
2824 }
2825
2826 return LY_ENOT;
2827}
2828
Radek Krejcif03a9e22020-09-18 20:09:31 +02002829LY_ERR
2830lyxp_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 +01002831{
Radek Krejcif03a9e22020-09-18 20:09:31 +02002832 LY_ERR ret = LY_SUCCESS;
2833 struct lyxp_expr *expr;
Radek Krejcid4270262019-01-07 15:07:25 +01002834 size_t parsed = 0, tok_len;
Radek Krejcib1646a92018-11-02 16:08:26 +01002835 enum lyxp_token tok_type;
Michal Vasko49fec8e2022-05-24 10:28:33 +02002836 ly_bool prev_func_check = 0, prev_ntype_check = 0, has_axis;
Michal Vasko004d3152020-06-11 19:59:22 +02002837 uint16_t tok_idx = 0;
Michal Vasko49fec8e2022-05-24 10:28:33 +02002838 ssize_t ncname_len;
Radek Krejcib1646a92018-11-02 16:08:26 +01002839
Radek Krejcif03a9e22020-09-18 20:09:31 +02002840 assert(expr_p);
2841
2842 if (!expr_str[0]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002843 LOGVAL(ctx, LY_VCODE_XP_EOF);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002844 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02002845 }
2846
2847 if (!expr_len) {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002848 expr_len = strlen(expr_str);
Michal Vasko004d3152020-06-11 19:59:22 +02002849 }
2850 if (expr_len > UINT16_MAX) {
Michal Vasko623ac7b2021-04-09 12:44:23 +02002851 LOGVAL(ctx, LYVE_XPATH, "XPath expression cannot be longer than %u characters.", UINT16_MAX);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002852 return LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01002853 }
2854
2855 /* init lyxp_expr structure */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002856 expr = calloc(1, sizeof *expr);
2857 LY_CHECK_ERR_GOTO(!expr, LOGMEM(ctx); ret = LY_EMEM, error);
2858 LY_CHECK_GOTO(ret = lydict_insert(ctx, expr_str, expr_len, &expr->expr), error);
2859 expr->used = 0;
2860 expr->size = LYXP_EXPR_SIZE_START;
2861 expr->tokens = malloc(expr->size * sizeof *expr->tokens);
2862 LY_CHECK_ERR_GOTO(!expr->tokens, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002863
Radek Krejcif03a9e22020-09-18 20:09:31 +02002864 expr->tok_pos = malloc(expr->size * sizeof *expr->tok_pos);
2865 LY_CHECK_ERR_GOTO(!expr->tok_pos, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002866
Radek Krejcif03a9e22020-09-18 20:09:31 +02002867 expr->tok_len = malloc(expr->size * sizeof *expr->tok_len);
2868 LY_CHECK_ERR_GOTO(!expr->tok_len, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002869
Michal Vasko004d3152020-06-11 19:59:22 +02002870 /* make expr 0-terminated */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002871 expr_str = expr->expr;
Michal Vasko004d3152020-06-11 19:59:22 +02002872
Radek Krejcif03a9e22020-09-18 20:09:31 +02002873 while (is_xmlws(expr_str[parsed])) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002874 ++parsed;
2875 }
2876
2877 do {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002878 if (expr_str[parsed] == '(') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002879
2880 /* '(' */
2881 tok_len = 1;
2882 tok_type = LYXP_TOKEN_PAR1;
2883
Michal Vasko49fec8e2022-05-24 10:28:33 +02002884 if (prev_ntype_check && expr->used && (expr->tokens[expr->used - 1] == LYXP_TOKEN_NAMETEST) &&
2885 (((expr->tok_len[expr->used - 1] == 4) &&
2886 (!strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "node", 4) ||
2887 !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "text", 4))) ||
2888 ((expr->tok_len[expr->used - 1] == 7) &&
2889 !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "comment", 7)))) {
2890 /* it is NodeType after all */
2891 expr->tokens[expr->used - 1] = LYXP_TOKEN_NODETYPE;
2892
2893 prev_ntype_check = 0;
2894 prev_func_check = 0;
2895 } else if (prev_func_check && expr->used && (expr->tokens[expr->used - 1] == LYXP_TOKEN_NAMETEST)) {
2896 /* it is FunctionName after all */
2897 expr->tokens[expr->used - 1] = LYXP_TOKEN_FUNCNAME;
2898
2899 prev_ntype_check = 0;
2900 prev_func_check = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01002901 }
2902
Radek Krejcif03a9e22020-09-18 20:09:31 +02002903 } else if (expr_str[parsed] == ')') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002904
2905 /* ')' */
2906 tok_len = 1;
2907 tok_type = LYXP_TOKEN_PAR2;
2908
Radek Krejcif03a9e22020-09-18 20:09:31 +02002909 } else if (expr_str[parsed] == '[') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002910
2911 /* '[' */
2912 tok_len = 1;
2913 tok_type = LYXP_TOKEN_BRACK1;
2914
Radek Krejcif03a9e22020-09-18 20:09:31 +02002915 } else if (expr_str[parsed] == ']') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002916
2917 /* ']' */
2918 tok_len = 1;
2919 tok_type = LYXP_TOKEN_BRACK2;
2920
Radek Krejcif03a9e22020-09-18 20:09:31 +02002921 } else if (!strncmp(&expr_str[parsed], "..", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002922
2923 /* '..' */
2924 tok_len = 2;
2925 tok_type = LYXP_TOKEN_DDOT;
2926
Radek Krejcif03a9e22020-09-18 20:09:31 +02002927 } else if ((expr_str[parsed] == '.') && (!isdigit(expr_str[parsed + 1]))) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002928
2929 /* '.' */
2930 tok_len = 1;
2931 tok_type = LYXP_TOKEN_DOT;
2932
Radek Krejcif03a9e22020-09-18 20:09:31 +02002933 } else if (expr_str[parsed] == '@') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002934
2935 /* '@' */
2936 tok_len = 1;
2937 tok_type = LYXP_TOKEN_AT;
2938
Radek Krejcif03a9e22020-09-18 20:09:31 +02002939 } else if (expr_str[parsed] == ',') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002940
2941 /* ',' */
2942 tok_len = 1;
2943 tok_type = LYXP_TOKEN_COMMA;
2944
Radek Krejcif03a9e22020-09-18 20:09:31 +02002945 } else if (expr_str[parsed] == '\'') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002946
2947 /* Literal with ' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002948 for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\''); ++tok_len) {}
2949 LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0',
Radek Krejci2efc45b2020-12-22 16:25:44 +01002950 LOGVAL(ctx, LY_VCODE_XP_EOE, expr_str[parsed], &expr_str[parsed]); ret = LY_EVALID,
Michal Vasko69730152020-10-09 16:30:07 +02002951 error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002952 ++tok_len;
2953 tok_type = LYXP_TOKEN_LITERAL;
2954
Radek Krejcif03a9e22020-09-18 20:09:31 +02002955 } else if (expr_str[parsed] == '\"') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002956
2957 /* Literal with " */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002958 for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\"'); ++tok_len) {}
2959 LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0',
Radek Krejci2efc45b2020-12-22 16:25:44 +01002960 LOGVAL(ctx, LY_VCODE_XP_EOE, expr_str[parsed], &expr_str[parsed]); ret = LY_EVALID,
Michal Vasko69730152020-10-09 16:30:07 +02002961 error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002962 ++tok_len;
2963 tok_type = LYXP_TOKEN_LITERAL;
2964
Radek Krejcif03a9e22020-09-18 20:09:31 +02002965 } else if ((expr_str[parsed] == '.') || (isdigit(expr_str[parsed]))) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002966
2967 /* Number */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002968 for (tok_len = 0; isdigit(expr_str[parsed + tok_len]); ++tok_len) {}
2969 if (expr_str[parsed + tok_len] == '.') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002970 ++tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02002971 for ( ; isdigit(expr_str[parsed + tok_len]); ++tok_len) {}
Radek Krejcib1646a92018-11-02 16:08:26 +01002972 }
2973 tok_type = LYXP_TOKEN_NUMBER;
2974
aPiecekfba75362021-10-07 12:39:48 +02002975 } else if (expr_str[parsed] == '$') {
2976
2977 /* VariableReference */
2978 parsed++;
Michal Vasko49fec8e2022-05-24 10:28:33 +02002979 ncname_len = parse_ncname(&expr_str[parsed]);
aPiecekfba75362021-10-07 12:39:48 +02002980 LY_CHECK_ERR_GOTO(ncname_len < 1, LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed - ncname_len],
2981 parsed - ncname_len + 1, expr_str); ret = LY_EVALID, error);
2982 tok_len = ncname_len;
2983 LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == ':',
2984 LOGVAL(ctx, LYVE_XPATH, "Variable with prefix is not supported."); ret = LY_EVALID,
2985 error);
2986 tok_type = LYXP_TOKEN_VARREF;
2987
Radek Krejcif03a9e22020-09-18 20:09:31 +02002988 } else if (expr_str[parsed] == '/') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002989
2990 /* Operator '/', '//' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002991 if (!strncmp(&expr_str[parsed], "//", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002992 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002993 tok_type = LYXP_TOKEN_OPER_RPATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002994 } else {
2995 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002996 tok_type = LYXP_TOKEN_OPER_PATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002997 }
Radek Krejcib1646a92018-11-02 16:08:26 +01002998
Radek Krejcif03a9e22020-09-18 20:09:31 +02002999 } else if (!strncmp(&expr_str[parsed], "!=", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01003000
Michal Vasko3e48bf32020-06-01 08:39:07 +02003001 /* Operator '!=' */
Radek Krejcib1646a92018-11-02 16:08:26 +01003002 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003003 tok_type = LYXP_TOKEN_OPER_NEQUAL;
3004
Radek Krejcif03a9e22020-09-18 20:09:31 +02003005 } else if (!strncmp(&expr_str[parsed], "<=", 2) || !strncmp(&expr_str[parsed], ">=", 2)) {
Michal Vasko3e48bf32020-06-01 08:39:07 +02003006
3007 /* Operator '<=', '>=' */
3008 tok_len = 2;
3009 tok_type = LYXP_TOKEN_OPER_COMP;
Radek Krejcib1646a92018-11-02 16:08:26 +01003010
Radek Krejcif03a9e22020-09-18 20:09:31 +02003011 } else if (expr_str[parsed] == '|') {
Radek Krejcib1646a92018-11-02 16:08:26 +01003012
3013 /* Operator '|' */
3014 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003015 tok_type = LYXP_TOKEN_OPER_UNI;
Radek Krejcib1646a92018-11-02 16:08:26 +01003016
Radek Krejcif03a9e22020-09-18 20:09:31 +02003017 } else if ((expr_str[parsed] == '+') || (expr_str[parsed] == '-')) {
Radek Krejcib1646a92018-11-02 16:08:26 +01003018
3019 /* Operator '+', '-' */
3020 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003021 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01003022
Radek Krejcif03a9e22020-09-18 20:09:31 +02003023 } else if (expr_str[parsed] == '=') {
Radek Krejcib1646a92018-11-02 16:08:26 +01003024
Michal Vasko3e48bf32020-06-01 08:39:07 +02003025 /* Operator '=' */
Radek Krejcib1646a92018-11-02 16:08:26 +01003026 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003027 tok_type = LYXP_TOKEN_OPER_EQUAL;
3028
Radek Krejcif03a9e22020-09-18 20:09:31 +02003029 } else if ((expr_str[parsed] == '<') || (expr_str[parsed] == '>')) {
Michal Vasko3e48bf32020-06-01 08:39:07 +02003030
3031 /* Operator '<', '>' */
3032 tok_len = 1;
3033 tok_type = LYXP_TOKEN_OPER_COMP;
Radek Krejcib1646a92018-11-02 16:08:26 +01003034
Michal Vasko69730152020-10-09 16:30:07 +02003035 } else if (expr->used && (expr->tokens[expr->used - 1] != LYXP_TOKEN_AT) &&
3036 (expr->tokens[expr->used - 1] != LYXP_TOKEN_PAR1) &&
3037 (expr->tokens[expr->used - 1] != LYXP_TOKEN_BRACK1) &&
3038 (expr->tokens[expr->used - 1] != LYXP_TOKEN_COMMA) &&
3039 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_LOG) &&
3040 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_EQUAL) &&
3041 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_NEQUAL) &&
3042 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_COMP) &&
3043 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_MATH) &&
3044 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_UNI) &&
3045 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_PATH) &&
3046 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_RPATH)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01003047
3048 /* Operator '*', 'or', 'and', 'mod', or 'div' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02003049 if (expr_str[parsed] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01003050 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003051 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01003052
Radek Krejcif03a9e22020-09-18 20:09:31 +02003053 } else if (!strncmp(&expr_str[parsed], "or", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01003054 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003055 tok_type = LYXP_TOKEN_OPER_LOG;
Radek Krejcib1646a92018-11-02 16:08:26 +01003056
Radek Krejcif03a9e22020-09-18 20:09:31 +02003057 } else if (!strncmp(&expr_str[parsed], "and", 3)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01003058 tok_len = 3;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003059 tok_type = LYXP_TOKEN_OPER_LOG;
Radek Krejcib1646a92018-11-02 16:08:26 +01003060
Radek Krejcif03a9e22020-09-18 20:09:31 +02003061 } else if (!strncmp(&expr_str[parsed], "mod", 3) || !strncmp(&expr_str[parsed], "div", 3)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01003062 tok_len = 3;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003063 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01003064
Michal Vasko49fec8e2022-05-24 10:28:33 +02003065 } else if (prev_ntype_check || prev_func_check) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003066 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 +02003067 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 +02003068 ret = LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01003069 goto error;
3070 } else {
Michal Vasko774ce402021-04-14 15:35:06 +02003071 LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed], parsed + 1, expr_str);
Radek Krejcif03a9e22020-09-18 20:09:31 +02003072 ret = LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01003073 goto error;
3074 }
Radek Krejcib1646a92018-11-02 16:08:26 +01003075 } else {
3076
Michal Vasko49fec8e2022-05-24 10:28:33 +02003077 /* (AxisName '::')? ((NCName ':')? '*' | QName) or NodeType/FunctionName */
3078 if (expr_str[parsed] == '*') {
3079 ncname_len = 1;
3080 } else {
3081 ncname_len = parse_ncname(&expr_str[parsed]);
3082 LY_CHECK_ERR_GOTO(ncname_len < 1, LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed - ncname_len],
3083 parsed - ncname_len + 1, expr_str); ret = LY_EVALID, error);
3084 }
Radek Krejcib1646a92018-11-02 16:08:26 +01003085 tok_len = ncname_len;
3086
Michal Vasko49fec8e2022-05-24 10:28:33 +02003087 has_axis = 0;
3088 if (!strncmp(&expr_str[parsed + tok_len], "::", 2)) {
3089 /* axis */
3090 LY_CHECK_ERR_GOTO(expr_parse_axis(&expr_str[parsed], ncname_len),
3091 LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed], parsed + 1, expr_str); ret = LY_EVALID, error);
3092 tok_type = LYXP_TOKEN_AXISNAME;
3093
3094 LY_CHECK_GOTO(ret = exp_add_token(ctx, expr, tok_type, parsed, tok_len), error);
3095 parsed += tok_len;
3096
3097 /* '::' */
3098 tok_len = 2;
3099 tok_type = LYXP_TOKEN_DCOLON;
3100
3101 LY_CHECK_GOTO(ret = exp_add_token(ctx, expr, tok_type, parsed, tok_len), error);
3102 parsed += tok_len;
3103
3104 if (expr_str[parsed] == '*') {
3105 ncname_len = 1;
3106 } else {
3107 ncname_len = parse_ncname(&expr_str[parsed]);
3108 LY_CHECK_ERR_GOTO(ncname_len < 1, LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed - ncname_len],
3109 parsed - ncname_len + 1, expr_str); ret = LY_EVALID, error);
3110 }
3111 tok_len = ncname_len;
3112
3113 has_axis = 1;
3114 }
3115
Radek Krejcif03a9e22020-09-18 20:09:31 +02003116 if (expr_str[parsed + tok_len] == ':') {
Radek Krejcib1646a92018-11-02 16:08:26 +01003117 ++tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02003118 if (expr_str[parsed + tok_len] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01003119 ++tok_len;
3120 } else {
Radek Krejcif03a9e22020-09-18 20:09:31 +02003121 ncname_len = parse_ncname(&expr_str[parsed + tok_len]);
Michal Vaskoe2be5462021-08-04 10:49:42 +02003122 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 +02003123 parsed - ncname_len + 1, expr_str); ret = LY_EVALID, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01003124 tok_len += ncname_len;
3125 }
Michal Vasko49fec8e2022-05-24 10:28:33 +02003126 /* remove old flags to prevent ambiguities */
3127 prev_ntype_check = 0;
3128 prev_func_check = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01003129 tok_type = LYXP_TOKEN_NAMETEST;
3130 } else {
Michal Vasko49fec8e2022-05-24 10:28:33 +02003131 /* if not '*', there is no prefix so it can still be NodeType/FunctionName, we can't finally decide now */
3132 prev_ntype_check = (expr_str[parsed] == '*') ? 0 : 1;
3133 prev_func_check = (prev_ntype_check && !has_axis) ? 1 : 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01003134 tok_type = LYXP_TOKEN_NAMETEST;
3135 }
3136 }
3137
3138 /* store the token, move on to the next one */
Radek Krejcif03a9e22020-09-18 20:09:31 +02003139 LY_CHECK_GOTO(ret = exp_add_token(ctx, expr, tok_type, parsed, tok_len), error);
Radek Krejcib1646a92018-11-02 16:08:26 +01003140 parsed += tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02003141 while (is_xmlws(expr_str[parsed])) {
Radek Krejcib1646a92018-11-02 16:08:26 +01003142 ++parsed;
3143 }
3144
Radek Krejcif03a9e22020-09-18 20:09:31 +02003145 } while (expr_str[parsed]);
Radek Krejcib1646a92018-11-02 16:08:26 +01003146
Michal Vasko004d3152020-06-11 19:59:22 +02003147 if (reparse) {
3148 /* prealloc repeat */
Radek Krejcif03a9e22020-09-18 20:09:31 +02003149 expr->repeat = calloc(expr->size, sizeof *expr->repeat);
3150 LY_CHECK_ERR_GOTO(!expr->repeat, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01003151
Michal Vasko004d3152020-06-11 19:59:22 +02003152 /* fill repeat */
aPiecekbf968d92021-05-27 14:35:05 +02003153 LY_CHECK_ERR_GOTO(reparse_or_expr(ctx, expr, &tok_idx, 0), ret = LY_EVALID, error);
Radek Krejcif03a9e22020-09-18 20:09:31 +02003154 if (expr->used > tok_idx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003155 LOGVAL(ctx, LYVE_XPATH, "Unparsed characters \"%s\" left at the end of an XPath expression.",
Michal Vasko69730152020-10-09 16:30:07 +02003156 &expr->expr[expr->tok_pos[tok_idx]]);
Radek Krejcif03a9e22020-09-18 20:09:31 +02003157 ret = LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02003158 goto error;
3159 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003160 }
3161
Radek Krejcif03a9e22020-09-18 20:09:31 +02003162 print_expr_struct_debug(expr);
3163 *expr_p = expr;
3164 return LY_SUCCESS;
Radek Krejcib1646a92018-11-02 16:08:26 +01003165
3166error:
Radek Krejcif03a9e22020-09-18 20:09:31 +02003167 lyxp_expr_free(ctx, expr);
3168 return ret;
Radek Krejcib1646a92018-11-02 16:08:26 +01003169}
3170
Michal Vasko1734be92020-09-22 08:55:10 +02003171LY_ERR
Michal Vaskoe33134a2022-07-29 14:54:40 +02003172lyxp_expr_dup(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t start_idx, uint16_t end_idx,
3173 struct lyxp_expr **dup_p)
Michal Vasko004d3152020-06-11 19:59:22 +02003174{
Michal Vasko1734be92020-09-22 08:55:10 +02003175 LY_ERR ret = LY_SUCCESS;
3176 struct lyxp_expr *dup = NULL;
Michal Vaskoe33134a2022-07-29 14:54:40 +02003177 uint16_t used = 0, i, j, k, expr_len;
3178 const char *expr_start;
3179
3180 assert((!start_idx && !end_idx) || ((start_idx < exp->used) && (end_idx < exp->used) && (start_idx <= end_idx)));
Michal Vasko004d3152020-06-11 19:59:22 +02003181
Michal Vasko7f45cf22020-10-01 12:49:44 +02003182 if (!exp) {
3183 goto cleanup;
3184 }
3185
Michal Vaskoe33134a2022-07-29 14:54:40 +02003186 if (!start_idx && !end_idx) {
3187 end_idx = exp->used - 1;
3188 }
3189
3190 expr_start = exp->expr + exp->tok_pos[start_idx];
3191 expr_len = (exp->tok_pos[end_idx] + exp->tok_len[end_idx]) - exp->tok_pos[start_idx];
3192
Michal Vasko004d3152020-06-11 19:59:22 +02003193 dup = calloc(1, sizeof *dup);
Michal Vasko1734be92020-09-22 08:55:10 +02003194 LY_CHECK_ERR_GOTO(!dup, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02003195
Michal Vasko08e9b112021-06-11 15:41:17 +02003196 if (exp->used) {
Michal Vaskoe33134a2022-07-29 14:54:40 +02003197 used = (end_idx - start_idx) + 1;
3198
3199 dup->tokens = malloc(used * sizeof *dup->tokens);
Michal Vasko08e9b112021-06-11 15:41:17 +02003200 LY_CHECK_ERR_GOTO(!dup->tokens, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vaskoe33134a2022-07-29 14:54:40 +02003201 memcpy(dup->tokens, exp->tokens + start_idx, used * sizeof *dup->tokens);
Michal Vasko004d3152020-06-11 19:59:22 +02003202
Michal Vaskoe33134a2022-07-29 14:54:40 +02003203 dup->tok_pos = malloc(used * sizeof *dup->tok_pos);
Michal Vasko08e9b112021-06-11 15:41:17 +02003204 LY_CHECK_ERR_GOTO(!dup->tok_pos, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vaskoe33134a2022-07-29 14:54:40 +02003205 memcpy(dup->tok_pos, exp->tok_pos + start_idx, used * sizeof *dup->tok_pos);
Michal Vasko004d3152020-06-11 19:59:22 +02003206
Michal Vaskoe33134a2022-07-29 14:54:40 +02003207 if (start_idx) {
3208 /* fix the indices in the expression */
3209 for (i = 0; i < used; ++i) {
3210 dup->tok_pos[i] -= expr_start - exp->expr;
3211 }
3212 }
3213
3214 dup->tok_len = malloc(used * sizeof *dup->tok_len);
Michal Vasko08e9b112021-06-11 15:41:17 +02003215 LY_CHECK_ERR_GOTO(!dup->tok_len, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vaskoe33134a2022-07-29 14:54:40 +02003216 memcpy(dup->tok_len, exp->tok_len + start_idx, used * sizeof *dup->tok_len);
Michal Vasko004d3152020-06-11 19:59:22 +02003217
Michal Vasko79a7a872022-06-17 09:00:48 +02003218 if (exp->repeat) {
Michal Vaskoe33134a2022-07-29 14:54:40 +02003219 dup->repeat = malloc(used * sizeof *dup->repeat);
Michal Vasko79a7a872022-06-17 09:00:48 +02003220 LY_CHECK_ERR_GOTO(!dup->repeat, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vaskoe33134a2022-07-29 14:54:40 +02003221 for (i = start_idx; i <= end_idx; ++i) {
Michal Vasko79a7a872022-06-17 09:00:48 +02003222 if (!exp->repeat[i]) {
Michal Vaskoe33134a2022-07-29 14:54:40 +02003223 dup->repeat[i - start_idx] = NULL;
Michal Vasko79a7a872022-06-17 09:00:48 +02003224 } else {
3225 for (j = 0; exp->repeat[i][j]; ++j) {}
3226 /* the ending 0 as well */
3227 ++j;
Michal Vasko004d3152020-06-11 19:59:22 +02003228
Michal Vaskoe33134a2022-07-29 14:54:40 +02003229 dup->repeat[i - start_idx] = malloc(j * sizeof **dup->repeat);
3230 LY_CHECK_ERR_GOTO(!dup->repeat[i - start_idx], LOGMEM(ctx); ret = LY_EMEM, cleanup);
3231 memcpy(dup->repeat[i - start_idx], exp->repeat[i], j * sizeof **dup->repeat);
3232 dup->repeat[i - start_idx][j - 1] = 0;
3233
3234 if (start_idx) {
3235 /* fix the indices in the tokens */
3236 for (k = 0; k < j; ++k) {
3237 dup->repeat[i - start_idx][k] -= start_idx;
3238 }
3239 }
Michal Vasko79a7a872022-06-17 09:00:48 +02003240 }
Michal Vasko08e9b112021-06-11 15:41:17 +02003241 }
Michal Vasko004d3152020-06-11 19:59:22 +02003242 }
3243 }
3244
Michal Vaskoe33134a2022-07-29 14:54:40 +02003245 dup->used = used;
3246 dup->size = used;
3247
3248 /* copy only subexpression */
3249 LY_CHECK_GOTO(ret = lydict_insert(ctx, expr_start, expr_len, &dup->expr), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02003250
Michal Vasko1734be92020-09-22 08:55:10 +02003251cleanup:
3252 if (ret) {
3253 lyxp_expr_free(ctx, dup);
3254 } else {
3255 *dup_p = dup;
3256 }
3257 return ret;
Michal Vasko004d3152020-06-11 19:59:22 +02003258}
3259
Michal Vasko03ff5a72019-09-11 13:49:33 +02003260/**
3261 * @brief Get the last-added schema node that is currently in the context.
3262 *
3263 * @param[in] set Set to search in.
3264 * @return Last-added schema context node, NULL if no node is in context.
3265 */
3266static struct lysc_node *
3267warn_get_scnode_in_ctx(struct lyxp_set *set)
3268{
3269 uint32_t i;
3270
3271 if (!set || (set->type != LYXP_SET_SCNODE_SET)) {
3272 return NULL;
3273 }
3274
3275 i = set->used;
3276 do {
3277 --i;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003278 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003279 /* if there are more, simply return the first found (last added) */
3280 return set->val.scnodes[i].scnode;
3281 }
3282 } while (i);
3283
3284 return NULL;
3285}
3286
3287/**
3288 * @brief Test whether a type is numeric - integer type or decimal64.
3289 *
3290 * @param[in] type Type to test.
Radek Krejci857189e2020-09-01 13:26:36 +02003291 * @return Boolean value whether @p type is numeric type or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003292 */
Radek Krejci857189e2020-09-01 13:26:36 +02003293static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003294warn_is_numeric_type(struct lysc_type *type)
3295{
3296 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003297 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003298 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003299
3300 switch (type->basetype) {
3301 case LY_TYPE_DEC64:
3302 case LY_TYPE_INT8:
3303 case LY_TYPE_UINT8:
3304 case LY_TYPE_INT16:
3305 case LY_TYPE_UINT16:
3306 case LY_TYPE_INT32:
3307 case LY_TYPE_UINT32:
3308 case LY_TYPE_INT64:
3309 case LY_TYPE_UINT64:
3310 return 1;
3311 case LY_TYPE_UNION:
3312 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003313 LY_ARRAY_FOR(uni->types, u) {
3314 ret = warn_is_numeric_type(uni->types[u]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003315 if (ret) {
3316 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003317 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003318 }
3319 }
3320 /* did not find any suitable type */
3321 return 0;
3322 case LY_TYPE_LEAFREF:
3323 return warn_is_numeric_type(((struct lysc_type_leafref *)type)->realtype);
3324 default:
3325 return 0;
3326 }
3327}
3328
3329/**
3330 * @brief Test whether a type is string-like - no integers, decimal64 or binary.
3331 *
3332 * @param[in] type Type to test.
Radek Krejci857189e2020-09-01 13:26:36 +02003333 * @return Boolean value whether @p type's basetype is string type or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003334 */
Radek Krejci857189e2020-09-01 13:26:36 +02003335static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003336warn_is_string_type(struct lysc_type *type)
3337{
3338 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003339 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003340 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003341
3342 switch (type->basetype) {
3343 case LY_TYPE_BITS:
3344 case LY_TYPE_ENUM:
3345 case LY_TYPE_IDENT:
3346 case LY_TYPE_INST:
3347 case LY_TYPE_STRING:
3348 return 1;
3349 case LY_TYPE_UNION:
3350 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003351 LY_ARRAY_FOR(uni->types, u) {
3352 ret = warn_is_string_type(uni->types[u]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003353 if (ret) {
3354 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003355 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003356 }
3357 }
3358 /* did not find any suitable type */
3359 return 0;
3360 case LY_TYPE_LEAFREF:
3361 return warn_is_string_type(((struct lysc_type_leafref *)type)->realtype);
3362 default:
3363 return 0;
3364 }
3365}
3366
3367/**
3368 * @brief Test whether a type is one specific type.
3369 *
3370 * @param[in] type Type to test.
3371 * @param[in] base Expected type.
Radek Krejci857189e2020-09-01 13:26:36 +02003372 * @return Boolean value whether the given @p type is of the specific basetype @p base.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003373 */
Radek Krejci857189e2020-09-01 13:26:36 +02003374static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003375warn_is_specific_type(struct lysc_type *type, LY_DATA_TYPE base)
3376{
3377 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003378 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003379 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003380
3381 if (type->basetype == base) {
3382 return 1;
3383 } else if (type->basetype == LY_TYPE_UNION) {
3384 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003385 LY_ARRAY_FOR(uni->types, u) {
3386 ret = warn_is_specific_type(uni->types[u], base);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003387 if (ret) {
3388 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003389 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003390 }
3391 }
3392 /* did not find any suitable type */
3393 return 0;
3394 } else if (type->basetype == LY_TYPE_LEAFREF) {
3395 return warn_is_specific_type(((struct lysc_type_leafref *)type)->realtype, base);
3396 }
3397
3398 return 0;
3399}
3400
3401/**
3402 * @brief Get next type of a (union) type.
3403 *
3404 * @param[in] type Base type.
3405 * @param[in] prev_type Previously returned type.
3406 * @return Next type or NULL.
3407 */
3408static struct lysc_type *
3409warn_is_equal_type_next_type(struct lysc_type *type, struct lysc_type *prev_type)
3410{
3411 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003412 ly_bool found = 0;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003413 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003414
3415 switch (type->basetype) {
3416 case LY_TYPE_UNION:
3417 uni = (struct lysc_type_union *)type;
3418 if (!prev_type) {
3419 return uni->types[0];
3420 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003421 LY_ARRAY_FOR(uni->types, u) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003422 if (found) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003423 return uni->types[u];
Michal Vasko03ff5a72019-09-11 13:49:33 +02003424 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003425 if (prev_type == uni->types[u]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003426 found = 1;
3427 }
3428 }
3429 return NULL;
3430 default:
3431 if (prev_type) {
3432 assert(type == prev_type);
3433 return NULL;
3434 } else {
3435 return type;
3436 }
3437 }
3438}
3439
3440/**
3441 * @brief Test whether 2 types have a common type.
3442 *
3443 * @param[in] type1 First type.
3444 * @param[in] type2 Second type.
3445 * @return 1 if they do, 0 otherwise.
3446 */
3447static int
3448warn_is_equal_type(struct lysc_type *type1, struct lysc_type *type2)
3449{
3450 struct lysc_type *t1, *rt1, *t2, *rt2;
3451
3452 t1 = NULL;
3453 while ((t1 = warn_is_equal_type_next_type(type1, t1))) {
3454 if (t1->basetype == LY_TYPE_LEAFREF) {
3455 rt1 = ((struct lysc_type_leafref *)t1)->realtype;
3456 } else {
3457 rt1 = t1;
3458 }
3459
3460 t2 = NULL;
3461 while ((t2 = warn_is_equal_type_next_type(type2, t2))) {
3462 if (t2->basetype == LY_TYPE_LEAFREF) {
3463 rt2 = ((struct lysc_type_leafref *)t2)->realtype;
3464 } else {
3465 rt2 = t2;
3466 }
3467
3468 if (rt2->basetype == rt1->basetype) {
3469 /* match found */
3470 return 1;
3471 }
3472 }
3473 }
3474
3475 return 0;
3476}
3477
3478/**
Michal Vaskoaa956522021-11-11 10:45:34 +01003479 * @brief Print warning with information about the XPath subexpression that caused previous warning.
3480 *
3481 * @param[in] ctx Context for logging.
3482 * @param[in] tok_pos Index of the subexpression in the whole expression.
3483 * @param[in] subexpr Subexpression start.
3484 * @param[in] subexpr_len Length of @p subexpr to print.
3485 * @param[in] cur_scnode Expression context node.
3486 */
3487static void
3488warn_subexpr_log(const struct ly_ctx *ctx, uint16_t tok_pos, const char *subexpr, int subexpr_len,
3489 const struct lysc_node *cur_scnode)
3490{
3491 char *path;
3492
3493 path = lysc_path(cur_scnode, LYSC_PATH_LOG, NULL, 0);
3494 LOGWRN(ctx, "Previous warning generated by XPath subexpression[%" PRIu16 "] \"%.*s\" with context node \"%s\".",
3495 tok_pos, subexpr_len, subexpr, path);
3496 free(path);
3497}
3498
3499/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02003500 * @brief Check both operands of comparison operators.
3501 *
3502 * @param[in] ctx Context for errors.
3503 * @param[in] set1 First operand set.
3504 * @param[in] set2 Second operand set.
3505 * @param[in] numbers_only Whether accept only numbers or other types are fine too (for '=' and '!=').
3506 * @param[in] expr Start of the expression to print with the warning.
3507 * @param[in] tok_pos Token position.
3508 */
3509static void
Michal Vaskoaa956522021-11-11 10:45:34 +01003510warn_operands(struct ly_ctx *ctx, struct lyxp_set *set1, struct lyxp_set *set2, ly_bool numbers_only, const char *expr,
3511 uint16_t tok_pos)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003512{
3513 struct lysc_node_leaf *node1, *node2;
Radek Krejci857189e2020-09-01 13:26:36 +02003514 ly_bool leaves = 1, warning = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003515
3516 node1 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set1);
3517 node2 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set2);
3518
3519 if (!node1 && !node2) {
3520 /* no node-sets involved, nothing to do */
3521 return;
3522 }
3523
3524 if (node1) {
3525 if (!(node1->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3526 LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node1->nodetype), node1->name);
3527 warning = 1;
3528 leaves = 0;
3529 } else if (numbers_only && !warn_is_numeric_type(node1->type)) {
3530 LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node1->name);
3531 warning = 1;
3532 }
3533 }
3534
3535 if (node2) {
3536 if (!(node2->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3537 LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node2->nodetype), node2->name);
3538 warning = 1;
3539 leaves = 0;
3540 } else if (numbers_only && !warn_is_numeric_type(node2->type)) {
3541 LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node2->name);
3542 warning = 1;
3543 }
3544 }
3545
3546 if (node1 && node2 && leaves && !numbers_only) {
Michal Vasko69730152020-10-09 16:30:07 +02003547 if ((warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type)) ||
3548 (!warn_is_numeric_type(node1->type) && warn_is_numeric_type(node2->type)) ||
3549 (!warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type) &&
3550 !warn_is_equal_type(node1->type, node2->type))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003551 LOGWRN(ctx, "Incompatible types of operands \"%s\" and \"%s\" for comparison.", node1->name, node2->name);
3552 warning = 1;
3553 }
3554 }
3555
3556 if (warning) {
Michal Vaskoaa956522021-11-11 10:45:34 +01003557 warn_subexpr_log(ctx, tok_pos, expr + tok_pos, 20, set1->cur_scnode);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003558 }
3559}
3560
3561/**
3562 * @brief Check that a value is valid for a leaf. If not applicable, does nothing.
3563 *
3564 * @param[in] exp Parsed XPath expression.
3565 * @param[in] set Set with the leaf/leaf-list.
3566 * @param[in] val_exp Index of the value (literal/number) in @p exp.
3567 * @param[in] equal_exp Index of the start of the equality expression in @p exp.
3568 * @param[in] last_equal_exp Index of the end of the equality expression in @p exp.
3569 */
3570static void
Michal Vasko40308e72020-10-20 16:38:40 +02003571warn_equality_value(const struct lyxp_expr *exp, struct lyxp_set *set, uint16_t val_exp, uint16_t equal_exp,
3572 uint16_t last_equal_exp)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003573{
3574 struct lysc_node *scnode;
3575 struct lysc_type *type;
3576 char *value;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003577 struct lyd_value storage;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003578 LY_ERR rc;
3579 struct ly_err_item *err = NULL;
3580
Michal Vasko69730152020-10-09 16:30:07 +02003581 if ((scnode = warn_get_scnode_in_ctx(set)) && (scnode->nodetype & (LYS_LEAF | LYS_LEAFLIST)) &&
3582 ((exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) || (exp->tokens[val_exp] == LYXP_TOKEN_NUMBER))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003583 /* check that the node can have the specified value */
3584 if (exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) {
3585 value = strndup(exp->expr + exp->tok_pos[val_exp] + 1, exp->tok_len[val_exp] - 2);
3586 } else {
3587 value = strndup(exp->expr + exp->tok_pos[val_exp], exp->tok_len[val_exp]);
3588 }
3589 if (!value) {
3590 LOGMEM(set->ctx);
3591 return;
3592 }
3593
3594 if ((((struct lysc_node_leaf *)scnode)->type->basetype == LY_TYPE_IDENT) && !strchr(value, ':')) {
3595 LOGWRN(set->ctx, "Identityref \"%s\" comparison with identity \"%s\" without prefix, consider adding"
Michal Vasko69730152020-10-09 16:30:07 +02003596 " a prefix or best using \"derived-from(-or-self)()\" functions.", scnode->name, value);
Michal Vaskoaa956522021-11-11 10:45:34 +01003597 warn_subexpr_log(set->ctx, exp->tok_pos[equal_exp], exp->expr + exp->tok_pos[equal_exp],
Radek Krejci0f969882020-08-21 16:56:47 +02003598 (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp],
Michal Vaskoaa956522021-11-11 10:45:34 +01003599 set->cur_scnode);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003600 }
3601
3602 type = ((struct lysc_node_leaf *)scnode)->type;
3603 if (type->basetype != LY_TYPE_IDENT) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003604 rc = type->plugin->store(set->ctx, type, value, strlen(value), 0, set->format, set->prefix_data,
Michal Vasko405cc9e2020-12-01 12:01:27 +01003605 LYD_HINT_DATA, scnode, &storage, NULL, &err);
Michal Vaskobf42e832020-11-23 16:59:42 +01003606 if (rc == LY_EINCOMPLETE) {
3607 rc = LY_SUCCESS;
3608 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003609
3610 if (err) {
3611 LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type (%s).", value, err->msg);
3612 ly_err_free(err);
3613 } else if (rc != LY_SUCCESS) {
3614 LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type.", value);
3615 }
3616 if (rc != LY_SUCCESS) {
Michal Vaskoaa956522021-11-11 10:45:34 +01003617 warn_subexpr_log(set->ctx, exp->tok_pos[equal_exp], exp->expr + exp->tok_pos[equal_exp],
Radek Krejci0f969882020-08-21 16:56:47 +02003618 (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp],
Michal Vaskoaa956522021-11-11 10:45:34 +01003619 set->cur_scnode);
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003620 } else {
3621 type->plugin->free(set->ctx, &storage);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003622 }
3623 }
3624 free(value);
3625 }
3626}
3627
3628/*
3629 * XPath functions
3630 */
3631
3632/**
3633 * @brief Execute the YANG 1.1 bit-is-set(node-set, string) function. Returns LYXP_SET_BOOLEAN
3634 * depending on whether the first node bit value from the second argument is set.
3635 *
3636 * @param[in] args Array of arguments.
3637 * @param[in] arg_count Count of elements in @p args.
3638 * @param[in,out] set Context and result set at the same time.
3639 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003640 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003641 */
3642static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003643xpath_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 +02003644{
3645 struct lyd_node_term *leaf;
3646 struct lysc_node_leaf *sleaf;
Michal Vasko2588b952021-07-29 07:43:26 +02003647 struct lyd_value_bits *bits;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003648 LY_ERR rc = LY_SUCCESS;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003649 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003650
3651 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003652 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003653 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003654 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3655 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3656 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3657 sleaf->name);
3658 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_BITS)) {
3659 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"bits\".", __func__, sleaf->name);
3660 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003661 }
3662
3663 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3664 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003665 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3666 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003667 } else if (!warn_is_string_type(sleaf->type)) {
3668 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003669 }
3670 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003671 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003672 return rc;
3673 }
3674
Michal Vaskod3678892020-05-21 10:06:58 +02003675 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003676 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 +02003677 return LY_EVALID;
3678 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003679 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003680 LY_CHECK_RET(rc);
3681
3682 set_fill_boolean(set, 0);
Michal Vaskod3678892020-05-21 10:06:58 +02003683 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003684 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
Michal Vasko2588b952021-07-29 07:43:26 +02003685 if ((leaf->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (leaf->value.realtype->basetype == LY_TYPE_BITS)) {
3686 LYD_VALUE_GET(&leaf->value, bits);
3687 LY_ARRAY_FOR(bits->items, u) {
3688 if (!strcmp(bits->items[u]->name, args[1]->val.str)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003689 set_fill_boolean(set, 1);
3690 break;
3691 }
3692 }
3693 }
3694 }
3695
3696 return LY_SUCCESS;
3697}
3698
3699/**
3700 * @brief Execute the XPath boolean(object) function. Returns LYXP_SET_BOOLEAN
3701 * with the argument converted to boolean.
3702 *
3703 * @param[in] args Array of arguments.
3704 * @param[in] arg_count Count of elements in @p args.
3705 * @param[in,out] set Context and result set at the same time.
3706 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003707 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003708 */
3709static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003710xpath_boolean(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003711{
3712 LY_ERR rc;
3713
3714 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02003715 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003716 return LY_SUCCESS;
3717 }
3718
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003719 rc = lyxp_set_cast(args[0], LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003720 LY_CHECK_RET(rc);
3721 set_fill_set(set, args[0]);
3722
3723 return LY_SUCCESS;
3724}
3725
3726/**
3727 * @brief Execute the XPath ceiling(number) function. Returns LYXP_SET_NUMBER
3728 * with the first argument rounded up to the nearest integer.
3729 *
3730 * @param[in] args Array of arguments.
3731 * @param[in] arg_count Count of elements in @p args.
3732 * @param[in,out] set Context and result set at the same time.
3733 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003734 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003735 */
3736static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003737xpath_ceiling(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003738{
3739 struct lysc_node_leaf *sleaf;
3740 LY_ERR rc = LY_SUCCESS;
3741
3742 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003743 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003744 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003745 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3746 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3747 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3748 sleaf->name);
3749 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
3750 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
3751 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003752 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003753 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003754 return rc;
3755 }
3756
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003757 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003758 LY_CHECK_RET(rc);
3759 if ((long long)args[0]->val.num != args[0]->val.num) {
3760 set_fill_number(set, ((long long)args[0]->val.num) + 1);
3761 } else {
3762 set_fill_number(set, args[0]->val.num);
3763 }
3764
3765 return LY_SUCCESS;
3766}
3767
3768/**
3769 * @brief Execute the XPath concat(string, string, string*) function.
3770 * Returns LYXP_SET_STRING with the concatenation of all the arguments.
3771 *
3772 * @param[in] args Array of arguments.
3773 * @param[in] arg_count Count of elements in @p args.
3774 * @param[in,out] set Context and result set at the same time.
3775 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003776 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003777 */
3778static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003779xpath_concat(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003780{
3781 uint16_t i;
3782 char *str = NULL;
3783 size_t used = 1;
3784 LY_ERR rc = LY_SUCCESS;
3785 struct lysc_node_leaf *sleaf;
3786
3787 if (options & LYXP_SCNODE_ALL) {
3788 for (i = 0; i < arg_count; ++i) {
3789 if ((args[i]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[i]))) {
3790 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3791 LOGWRN(set->ctx, "Argument #%u of %s is a %s node \"%s\".",
Michal Vasko69730152020-10-09 16:30:07 +02003792 i + 1, __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003793 } else if (!warn_is_string_type(sleaf->type)) {
Radek Krejci70124c82020-08-14 22:17:03 +02003794 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 +02003795 }
3796 }
3797 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003798 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003799 return rc;
3800 }
3801
3802 for (i = 0; i < arg_count; ++i) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003803 rc = lyxp_set_cast(args[i], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003804 if (rc != LY_SUCCESS) {
3805 free(str);
3806 return rc;
3807 }
3808
3809 str = ly_realloc(str, (used + strlen(args[i]->val.str)) * sizeof(char));
3810 LY_CHECK_ERR_RET(!str, LOGMEM(set->ctx), LY_EMEM);
3811 strcpy(str + used - 1, args[i]->val.str);
3812 used += strlen(args[i]->val.str);
3813 }
3814
3815 /* free, kind of */
Michal Vaskod3678892020-05-21 10:06:58 +02003816 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003817 set->type = LYXP_SET_STRING;
3818 set->val.str = str;
3819
3820 return LY_SUCCESS;
3821}
3822
3823/**
3824 * @brief Execute the XPath contains(string, string) function.
3825 * Returns LYXP_SET_BOOLEAN whether the second argument can
3826 * be found in the first or not.
3827 *
3828 * @param[in] args Array of arguments.
3829 * @param[in] arg_count Count of elements in @p args.
3830 * @param[in,out] set Context and result set at the same time.
3831 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003832 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003833 */
3834static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003835xpath_contains(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003836{
3837 struct lysc_node_leaf *sleaf;
3838 LY_ERR rc = LY_SUCCESS;
3839
3840 if (options & LYXP_SCNODE_ALL) {
3841 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3842 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003843 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3844 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003845 } else if (!warn_is_string_type(sleaf->type)) {
3846 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003847 }
3848 }
3849
3850 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3851 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003852 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3853 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003854 } else if (!warn_is_string_type(sleaf->type)) {
3855 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003856 }
3857 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003858 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003859 return rc;
3860 }
3861
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003862 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003863 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003864 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003865 LY_CHECK_RET(rc);
3866
3867 if (strstr(args[0]->val.str, args[1]->val.str)) {
3868 set_fill_boolean(set, 1);
3869 } else {
3870 set_fill_boolean(set, 0);
3871 }
3872
3873 return LY_SUCCESS;
3874}
3875
3876/**
3877 * @brief Execute the XPath count(node-set) function. Returns LYXP_SET_NUMBER
3878 * with the size of the node-set from the argument.
3879 *
3880 * @param[in] args Array of arguments.
3881 * @param[in] arg_count Count of elements in @p args.
3882 * @param[in,out] set Context and result set at the same time.
3883 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003884 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003885 */
3886static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003887xpath_count(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003888{
Michal Vasko03ff5a72019-09-11 13:49:33 +02003889 LY_ERR rc = LY_SUCCESS;
3890
3891 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003892 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003893 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003894 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003895 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003896 return rc;
3897 }
3898
Michal Vasko03ff5a72019-09-11 13:49:33 +02003899 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003900 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "count(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003901 return LY_EVALID;
3902 }
3903
3904 set_fill_number(set, args[0]->used);
3905 return LY_SUCCESS;
3906}
3907
3908/**
3909 * @brief Execute the XPath current() function. Returns LYXP_SET_NODE_SET
3910 * with the context with the intial node.
3911 *
3912 * @param[in] args Array of arguments.
3913 * @param[in] arg_count Count of elements in @p args.
3914 * @param[in,out] set Context and result set at the same time.
3915 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003916 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003917 */
3918static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003919xpath_current(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003920{
3921 if (arg_count || args) {
Radek Krejcie87c7dc2021-06-02 21:25:42 +02003922 LOGVAL(set->ctx, LY_VCODE_XP_INARGCOUNT, arg_count, LY_PRI_LENSTR("current()"));
Michal Vasko03ff5a72019-09-11 13:49:33 +02003923 return LY_EVALID;
3924 }
3925
3926 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02003927 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003928
Michal Vasko296dfaf2021-12-13 16:57:42 +01003929 if (set->cur_scnode) {
3930 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, set->cur_scnode, LYXP_NODE_ELEM, NULL));
3931 } else {
3932 /* root node */
3933 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, NULL, set->root_type, NULL));
3934 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003935 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02003936 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003937
Michal Vasko296dfaf2021-12-13 16:57:42 +01003938 if (set->cur_node) {
3939 /* position is filled later */
3940 set_insert_node(set, set->cur_node, 0, LYXP_NODE_ELEM, 0);
3941 } else {
3942 /* root node */
3943 set_insert_node(set, NULL, 0, set->root_type, 0);
3944 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003945 }
3946
3947 return LY_SUCCESS;
3948}
3949
3950/**
3951 * @brief Execute the YANG 1.1 deref(node-set) function. Returns LYXP_SET_NODE_SET with either
3952 * leafref or instance-identifier target node(s).
3953 *
3954 * @param[in] args Array of arguments.
3955 * @param[in] arg_count Count of elements in @p args.
3956 * @param[in,out] set Context and result set at the same time.
3957 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003958 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003959 */
3960static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003961xpath_deref(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003962{
3963 struct lyd_node_term *leaf;
Michal Vasko42e497c2020-01-06 08:38:25 +01003964 struct lysc_node_leaf *sleaf = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02003965 struct lysc_type_leafref *lref;
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003966 const struct lysc_node *target;
Michal Vasko004d3152020-06-11 19:59:22 +02003967 struct ly_path *p;
3968 struct lyd_node *node;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003969 char *errmsg = NULL;
Michal Vasko00cbf532020-06-15 13:58:47 +02003970 uint8_t oper;
Michal Vasko741bb562021-06-24 11:59:50 +02003971 LY_ERR r;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003972
3973 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003974 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003975 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003976 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
Michal Vasko423ba3f2021-07-19 13:08:50 +02003977 if (!(sleaf->nodetype & LYD_NODE_TERM)) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003978 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3979 sleaf->name);
Michal Vaskoed725d72021-06-23 12:03:45 +02003980 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_LEAFREF) &&
3981 !warn_is_specific_type(sleaf->type, LY_TYPE_INST)) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003982 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"leafref\" nor \"instance-identifier\".",
3983 __func__, sleaf->name);
3984 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003985 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003986 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko423ba3f2021-07-19 13:08:50 +02003987 if (sleaf && (sleaf->nodetype & LYD_NODE_TERM) && (sleaf->type->basetype == LY_TYPE_LEAFREF)) {
Michal Vasko004d3152020-06-11 19:59:22 +02003988 lref = (struct lysc_type_leafref *)sleaf->type;
Michal Vaskod1e53b92021-01-28 13:11:06 +01003989 oper = (sleaf->flags & LYS_IS_OUTPUT) ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT;
Michal Vasko004d3152020-06-11 19:59:22 +02003990
3991 /* it was already evaluated on schema, it must succeed */
Michal Vasko741bb562021-06-24 11:59:50 +02003992 r = ly_path_compile_leafref(set->ctx, &sleaf->node, NULL, lref->path, oper, LY_PATH_TARGET_MANY,
Michal Vasko24fc4d12021-07-12 14:41:20 +02003993 LY_VALUE_SCHEMA_RESOLVED, lref->prefixes, &p);
Michal Vasko741bb562021-06-24 11:59:50 +02003994 if (!r) {
3995 /* get the target node */
3996 target = p[LY_ARRAY_COUNT(p) - 1].node;
3997 ly_path_free(set->ctx, p);
Michal Vasko004d3152020-06-11 19:59:22 +02003998
Michal Vasko741bb562021-06-24 11:59:50 +02003999 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, target, LYXP_NODE_ELEM, NULL));
4000 } /* else the target was found before but is disabled so it was removed */
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02004001 }
4002
Michal Vasko741bb562021-06-24 11:59:50 +02004003 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004004 }
4005
Michal Vaskod3678892020-05-21 10:06:58 +02004006 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004007 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "deref(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004008 return LY_EVALID;
4009 }
4010
Michal Vaskod3678892020-05-21 10:06:58 +02004011 lyxp_set_free_content(set);
4012 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004013 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
4014 sleaf = (struct lysc_node_leaf *)leaf->schema;
4015 if (sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
4016 if (sleaf->type->basetype == LY_TYPE_LEAFREF) {
4017 /* find leafref target */
Radek Krejci0b013302021-03-29 15:22:32 +02004018 if (lyplg_type_resolve_leafref((struct lysc_type_leafref *)sleaf->type, &leaf->node, &leaf->value, set->tree,
Michal Vasko9e685082021-01-29 14:49:09 +01004019 &node, &errmsg)) {
Michal Vasko004d3152020-06-11 19:59:22 +02004020 LOGERR(set->ctx, LY_EVALID, errmsg);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004021 free(errmsg);
Michal Vasko004d3152020-06-11 19:59:22 +02004022 return LY_EVALID;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004023 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004024 } else {
4025 assert(sleaf->type->basetype == LY_TYPE_INST);
Michal Vasko004d3152020-06-11 19:59:22 +02004026 if (ly_path_eval(leaf->value.target, set->tree, &node)) {
Michal Vaskoba99a3e2020-08-18 15:50:05 +02004027 LOGERR(set->ctx, LY_EVALID, "Invalid instance-identifier \"%s\" value - required instance not found.",
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02004028 lyd_get_value(&leaf->node));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004029 return LY_EVALID;
4030 }
4031 }
Michal Vasko004d3152020-06-11 19:59:22 +02004032
4033 /* insert it */
4034 set_insert_node(set, node, 0, LYXP_NODE_ELEM, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004035 }
4036 }
4037
4038 return LY_SUCCESS;
4039}
4040
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004041static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02004042xpath_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 +02004043{
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01004044 uint32_t i;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004045 LY_ARRAY_COUNT_TYPE u;
4046 struct lyd_node_term *leaf;
4047 struct lysc_node_leaf *sleaf;
4048 struct lyd_meta *meta;
Michal Vasko93923692021-05-07 15:28:02 +02004049 struct lyd_value *val;
4050 const struct lys_module *mod;
4051 const char *id_name;
4052 uint16_t id_len;
4053 struct lysc_ident *id;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004054 LY_ERR rc = LY_SUCCESS;
Radek Krejci857189e2020-09-01 13:26:36 +02004055 ly_bool found;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004056
4057 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02004058 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004059 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", func);
Michal Vasko5676f4e2021-04-06 17:14:45 +02004060 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4061 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4062 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype),
4063 sleaf->name);
4064 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_IDENT)) {
4065 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"identityref\".", func, sleaf->name);
4066 }
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004067 }
4068
4069 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4070 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4071 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype),
Michal Vasko69730152020-10-09 16:30:07 +02004072 sleaf->name);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004073 } else if (!warn_is_string_type(sleaf->type)) {
4074 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", func, sleaf->name);
4075 }
4076 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004077 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004078 return rc;
4079 }
4080
4081 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004082 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 +02004083 return LY_EVALID;
4084 }
4085 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
4086 LY_CHECK_RET(rc);
4087
Michal Vasko93923692021-05-07 15:28:02 +02004088 /* parse the identity */
4089 id_name = args[1]->val.str;
4090 id_len = strlen(id_name);
4091 rc = moveto_resolve_model(&id_name, &id_len, set, set->cur_node ? set->cur_node->schema : NULL, &mod);
4092 LY_CHECK_RET(rc);
4093 if (!mod) {
4094 LOGVAL(set->ctx, LYVE_XPATH, "Identity \"%.*s\" without a prefix.", (int)id_len, id_name);
4095 return LY_EVALID;
4096 }
4097
4098 /* find the identity */
4099 found = 0;
4100 LY_ARRAY_FOR(mod->identities, u) {
4101 if (!ly_strncmp(mod->identities[u].name, id_name, id_len)) {
4102 /* we have match */
4103 found = 1;
4104 break;
4105 }
4106 }
4107 if (!found) {
4108 LOGVAL(set->ctx, LYVE_XPATH, "Identity \"%.*s\" not found in module \"%s\".", (int)id_len, id_name, mod->name);
4109 return LY_EVALID;
4110 }
4111 id = &mod->identities[u];
4112
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004113 set_fill_boolean(set, 0);
4114 found = 0;
4115 for (i = 0; i < args[0]->used; ++i) {
4116 if ((args[0]->val.nodes[i].type != LYXP_NODE_ELEM) && (args[0]->val.nodes[i].type != LYXP_NODE_META)) {
4117 continue;
4118 }
4119
4120 if (args[0]->val.nodes[i].type == LYXP_NODE_ELEM) {
4121 leaf = (struct lyd_node_term *)args[0]->val.nodes[i].node;
4122 sleaf = (struct lysc_node_leaf *)leaf->schema;
4123 val = &leaf->value;
4124 if (!(sleaf->nodetype & LYD_NODE_TERM) || (leaf->value.realtype->basetype != LY_TYPE_IDENT)) {
4125 /* uninteresting */
4126 continue;
4127 }
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004128 } else {
4129 meta = args[0]->val.meta[i].meta;
4130 val = &meta->value;
4131 if (val->realtype->basetype != LY_TYPE_IDENT) {
4132 /* uninteresting */
4133 continue;
4134 }
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004135 }
4136
Michal Vasko93923692021-05-07 15:28:02 +02004137 /* check the identity itself */
4138 if (self_match && (id == val->ident)) {
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004139 set_fill_boolean(set, 1);
4140 found = 1;
4141 }
Michal Vasko93923692021-05-07 15:28:02 +02004142 if (!found && !lyplg_type_identity_isderived(id, val->ident)) {
4143 set_fill_boolean(set, 1);
4144 found = 1;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004145 }
4146
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004147 if (found) {
4148 break;
4149 }
4150 }
4151
4152 return LY_SUCCESS;
4153}
4154
Michal Vasko03ff5a72019-09-11 13:49:33 +02004155/**
4156 * @brief Execute the YANG 1.1 derived-from(node-set, string) function. Returns LYXP_SET_BOOLEAN depending
4157 * on whether the first argument nodes contain a node of an identity derived from the second
4158 * argument identity.
4159 *
4160 * @param[in] args Array of arguments.
4161 * @param[in] arg_count Count of elements in @p args.
4162 * @param[in,out] set Context and result set at the same time.
4163 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004164 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004165 */
4166static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004167xpath_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 +02004168{
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004169 return xpath_derived_(args, set, options, 0, __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004170}
4171
4172/**
4173 * @brief Execute the YANG 1.1 derived-from-or-self(node-set, string) function. Returns LYXP_SET_BOOLEAN depending
4174 * on whether the first argument nodes contain a node of an identity that either is or is derived from
4175 * the second argument identity.
4176 *
4177 * @param[in] args Array of arguments.
4178 * @param[in] arg_count Count of elements in @p args.
4179 * @param[in,out] set Context and result set at the same time.
4180 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004181 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004182 */
4183static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004184xpath_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 +02004185{
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004186 return xpath_derived_(args, set, options, 1, __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004187}
4188
4189/**
4190 * @brief Execute the YANG 1.1 enum-value(node-set) function. Returns LYXP_SET_NUMBER
4191 * with the integer value of the first node's enum value, otherwise NaN.
4192 *
4193 * @param[in] args Array of arguments.
4194 * @param[in] arg_count Count of elements in @p args.
4195 * @param[in,out] set Context and result set at the same time.
4196 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004197 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004198 */
4199static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004200xpath_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 +02004201{
4202 struct lyd_node_term *leaf;
4203 struct lysc_node_leaf *sleaf;
4204 LY_ERR rc = LY_SUCCESS;
4205
4206 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02004207 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004208 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02004209 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4210 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4211 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
4212 sleaf->name);
4213 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_ENUM)) {
4214 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"enumeration\".", __func__, sleaf->name);
4215 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004216 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004217 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004218 return rc;
4219 }
4220
Michal Vaskod3678892020-05-21 10:06:58 +02004221 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004222 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "enum-value(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004223 return LY_EVALID;
4224 }
4225
4226 set_fill_number(set, NAN);
Michal Vaskod3678892020-05-21 10:06:58 +02004227 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004228 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
4229 sleaf = (struct lysc_node_leaf *)leaf->schema;
4230 if ((sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (sleaf->type->basetype == LY_TYPE_ENUM)) {
4231 set_fill_number(set, leaf->value.enum_item->value);
4232 }
4233 }
4234
4235 return LY_SUCCESS;
4236}
4237
4238/**
4239 * @brief Execute the XPath false() function. Returns LYXP_SET_BOOLEAN
4240 * with false value.
4241 *
4242 * @param[in] args Array of arguments.
4243 * @param[in] arg_count Count of elements in @p args.
4244 * @param[in,out] set Context and result set at the same time.
4245 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004246 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004247 */
4248static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004249xpath_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 +02004250{
4251 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004252 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004253 return LY_SUCCESS;
4254 }
4255
4256 set_fill_boolean(set, 0);
4257 return LY_SUCCESS;
4258}
4259
4260/**
4261 * @brief Execute the XPath floor(number) function. Returns LYXP_SET_NUMBER
4262 * with the first argument floored (truncated).
4263 *
4264 * @param[in] args Array of arguments.
4265 * @param[in] arg_count Count of elements in @p args.
4266 * @param[in,out] set Context and result set at the same time.
4267 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004268 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004269 */
4270static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004271xpath_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 +02004272{
4273 LY_ERR rc;
4274
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004275 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004276 LY_CHECK_RET(rc);
4277 if (isfinite(args[0]->val.num)) {
4278 set_fill_number(set, (long long)args[0]->val.num);
4279 }
4280
4281 return LY_SUCCESS;
4282}
4283
4284/**
4285 * @brief Execute the XPath lang(string) function. Returns LYXP_SET_BOOLEAN
4286 * whether the language of the text matches the one from the argument.
4287 *
4288 * @param[in] args Array of arguments.
4289 * @param[in] arg_count Count of elements in @p args.
4290 * @param[in,out] set Context and result set at the same time.
4291 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004292 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004293 */
4294static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004295xpath_lang(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004296{
4297 const struct lyd_node *node;
4298 struct lysc_node_leaf *sleaf;
Michal Vasko9f96a052020-03-10 09:41:45 +01004299 struct lyd_meta *meta = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004300 const char *val;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004301 LY_ERR rc = LY_SUCCESS;
4302
4303 if (options & LYXP_SCNODE_ALL) {
4304 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4305 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004306 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
4307 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004308 } else if (!warn_is_string_type(sleaf->type)) {
4309 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004310 }
4311 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004312 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004313 return rc;
4314 }
4315
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004316 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004317 LY_CHECK_RET(rc);
4318
Michal Vasko03ff5a72019-09-11 13:49:33 +02004319 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004320 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "lang(string)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004321 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004322 } else if (!set->used) {
4323 set_fill_boolean(set, 0);
4324 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004325 }
4326
4327 switch (set->val.nodes[0].type) {
4328 case LYXP_NODE_ELEM:
4329 case LYXP_NODE_TEXT:
4330 node = set->val.nodes[0].node;
4331 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004332 case LYXP_NODE_META:
4333 node = set->val.meta[0].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004334 break;
4335 default:
4336 /* nothing to do with roots */
4337 set_fill_boolean(set, 0);
4338 return LY_SUCCESS;
4339 }
4340
Michal Vasko9f96a052020-03-10 09:41:45 +01004341 /* find lang metadata */
Michal Vasko9e685082021-01-29 14:49:09 +01004342 for ( ; node; node = lyd_parent(node)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01004343 for (meta = node->meta; meta; meta = meta->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004344 /* annotations */
Michal Vasko9f96a052020-03-10 09:41:45 +01004345 if (meta->name && !strcmp(meta->name, "lang") && !strcmp(meta->annotation->module->name, "xml")) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004346 break;
4347 }
4348 }
4349
Michal Vasko9f96a052020-03-10 09:41:45 +01004350 if (meta) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004351 break;
4352 }
4353 }
4354
4355 /* compare languages */
Michal Vasko9f96a052020-03-10 09:41:45 +01004356 if (!meta) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004357 set_fill_boolean(set, 0);
4358 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004359 uint64_t i;
4360
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02004361 val = lyd_get_meta_value(meta);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004362 for (i = 0; args[0]->val.str[i]; ++i) {
4363 if (tolower(args[0]->val.str[i]) != tolower(val[i])) {
4364 set_fill_boolean(set, 0);
4365 break;
4366 }
4367 }
4368 if (!args[0]->val.str[i]) {
4369 if (!val[i] || (val[i] == '-')) {
4370 set_fill_boolean(set, 1);
4371 } else {
4372 set_fill_boolean(set, 0);
4373 }
4374 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004375 }
4376
4377 return LY_SUCCESS;
4378}
4379
4380/**
4381 * @brief Execute the XPath last() function. Returns LYXP_SET_NUMBER
4382 * with the context size.
4383 *
4384 * @param[in] args Array of arguments.
4385 * @param[in] arg_count Count of elements in @p args.
4386 * @param[in,out] set Context and result set at the same time.
4387 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004388 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004389 */
4390static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004391xpath_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 +02004392{
4393 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004394 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004395 return LY_SUCCESS;
4396 }
4397
Michal Vasko03ff5a72019-09-11 13:49:33 +02004398 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004399 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "last()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004400 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004401 } else if (!set->used) {
4402 set_fill_number(set, 0);
4403 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004404 }
4405
4406 set_fill_number(set, set->ctx_size);
4407 return LY_SUCCESS;
4408}
4409
4410/**
4411 * @brief Execute the XPath local-name(node-set?) function. Returns LYXP_SET_STRING
4412 * with the node name without namespace from the argument or the context.
4413 *
4414 * @param[in] args Array of arguments.
4415 * @param[in] arg_count Count of elements in @p args.
4416 * @param[in,out] set Context and result set at the same time.
4417 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004418 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004419 */
4420static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004421xpath_local_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004422{
4423 struct lyxp_set_node *item;
Michal Vasko69730152020-10-09 16:30:07 +02004424
Michal Vasko03ff5a72019-09-11 13:49:33 +02004425 /* suppress unused variable warning */
4426 (void)options;
4427
4428 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004429 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004430 return LY_SUCCESS;
4431 }
4432
4433 if (arg_count) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004434 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004435 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004436 "local-name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004437 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004438 } else if (!args[0]->used) {
4439 set_fill_string(set, "", 0);
4440 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004441 }
4442
4443 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004444 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004445
4446 item = &args[0]->val.nodes[0];
4447 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004448 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004449 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "local-name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004450 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004451 } else if (!set->used) {
4452 set_fill_string(set, "", 0);
4453 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004454 }
4455
4456 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004457 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004458
4459 item = &set->val.nodes[0];
4460 }
4461
4462 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004463 case LYXP_NODE_NONE:
4464 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004465 case LYXP_NODE_ROOT:
4466 case LYXP_NODE_ROOT_CONFIG:
4467 case LYXP_NODE_TEXT:
4468 set_fill_string(set, "", 0);
4469 break;
4470 case LYXP_NODE_ELEM:
4471 set_fill_string(set, item->node->schema->name, strlen(item->node->schema->name));
4472 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004473 case LYXP_NODE_META:
4474 set_fill_string(set, ((struct lyd_meta *)item->node)->name, strlen(((struct lyd_meta *)item->node)->name));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004475 break;
4476 }
4477
4478 return LY_SUCCESS;
4479}
4480
4481/**
4482 * @brief Execute the XPath name(node-set?) function. Returns LYXP_SET_STRING
4483 * with the node name fully qualified (with namespace) from the argument or the context.
4484 *
4485 * @param[in] args Array of arguments.
4486 * @param[in] arg_count Count of elements in @p args.
4487 * @param[in,out] set Context and result set at the same time.
4488 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004489 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004490 */
4491static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004492xpath_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004493{
4494 struct lyxp_set_node *item;
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004495 struct lys_module *mod = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004496 char *str;
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004497 const char *name = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004498
4499 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004500 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004501 return LY_SUCCESS;
4502 }
4503
4504 if (arg_count) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004505 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004506 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004507 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004508 } else if (!args[0]->used) {
4509 set_fill_string(set, "", 0);
4510 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004511 }
4512
4513 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004514 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004515
4516 item = &args[0]->val.nodes[0];
4517 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004518 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004519 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004520 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004521 } else if (!set->used) {
4522 set_fill_string(set, "", 0);
4523 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004524 }
4525
4526 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004527 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004528
4529 item = &set->val.nodes[0];
4530 }
4531
4532 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004533 case LYXP_NODE_NONE:
4534 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004535 case LYXP_NODE_ROOT:
4536 case LYXP_NODE_ROOT_CONFIG:
4537 case LYXP_NODE_TEXT:
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004538 /* keep NULL */
Michal Vasko03ff5a72019-09-11 13:49:33 +02004539 break;
4540 case LYXP_NODE_ELEM:
4541 mod = item->node->schema->module;
4542 name = item->node->schema->name;
4543 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004544 case LYXP_NODE_META:
4545 mod = ((struct lyd_meta *)item->node)->annotation->module;
4546 name = ((struct lyd_meta *)item->node)->name;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004547 break;
4548 }
4549
4550 if (mod && name) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004551 int rc = asprintf(&str, "%s:%s", ly_get_prefix(mod, set->format, set->prefix_data), name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004552 LY_CHECK_ERR_RET(rc == -1, LOGMEM(set->ctx), LY_EMEM);
4553 set_fill_string(set, str, strlen(str));
4554 free(str);
4555 } else {
4556 set_fill_string(set, "", 0);
4557 }
4558
4559 return LY_SUCCESS;
4560}
4561
4562/**
4563 * @brief Execute the XPath namespace-uri(node-set?) function. Returns LYXP_SET_STRING
4564 * with the namespace of the node from the argument or the context.
4565 *
4566 * @param[in] args Array of arguments.
4567 * @param[in] arg_count Count of elements in @p args.
4568 * @param[in,out] set Context and result set at the same time.
4569 * @param[in] options XPath options.
4570 * @return LY_ERR (LY_EINVAL for wrong arguments on schema)
4571 */
4572static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004573xpath_namespace_uri(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004574{
4575 struct lyxp_set_node *item;
4576 struct lys_module *mod;
Michal Vasko69730152020-10-09 16:30:07 +02004577
Michal Vasko03ff5a72019-09-11 13:49:33 +02004578 /* suppress unused variable warning */
4579 (void)options;
4580
4581 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004582 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004583 return LY_SUCCESS;
4584 }
4585
4586 if (arg_count) {
Michal Vaskod3678892020-05-21 10:06:58 +02004587 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004588 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
Michal Vasko69730152020-10-09 16:30:07 +02004589 "namespace-uri(node-set?)");
Michal Vaskod3678892020-05-21 10:06:58 +02004590 return LY_EVALID;
4591 } else if (!args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004592 set_fill_string(set, "", 0);
4593 return LY_SUCCESS;
4594 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004595
4596 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004597 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004598
4599 item = &args[0]->val.nodes[0];
4600 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004601 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004602 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "namespace-uri(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004603 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004604 } else if (!set->used) {
4605 set_fill_string(set, "", 0);
4606 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004607 }
4608
4609 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004610 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004611
4612 item = &set->val.nodes[0];
4613 }
4614
4615 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004616 case LYXP_NODE_NONE:
4617 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004618 case LYXP_NODE_ROOT:
4619 case LYXP_NODE_ROOT_CONFIG:
4620 case LYXP_NODE_TEXT:
4621 set_fill_string(set, "", 0);
4622 break;
4623 case LYXP_NODE_ELEM:
Michal Vasko9f96a052020-03-10 09:41:45 +01004624 case LYXP_NODE_META:
Michal Vasko03ff5a72019-09-11 13:49:33 +02004625 if (item->type == LYXP_NODE_ELEM) {
4626 mod = item->node->schema->module;
Michal Vasko9f96a052020-03-10 09:41:45 +01004627 } else { /* LYXP_NODE_META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02004628 /* annotations */
Michal Vasko9f96a052020-03-10 09:41:45 +01004629 mod = ((struct lyd_meta *)item->node)->annotation->module;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004630 }
4631
4632 set_fill_string(set, mod->ns, strlen(mod->ns));
4633 break;
4634 }
4635
4636 return LY_SUCCESS;
4637}
4638
4639/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02004640 * @brief Execute the XPath normalize-space(string?) function. Returns LYXP_SET_STRING
4641 * with normalized value (no leading, trailing, double white spaces) of the node
4642 * from the argument or the context.
4643 *
4644 * @param[in] args Array of arguments.
4645 * @param[in] arg_count Count of elements in @p args.
4646 * @param[in,out] set Context and result set at the same time.
4647 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004648 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004649 */
4650static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004651xpath_normalize_space(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004652{
4653 uint16_t i, new_used;
4654 char *new;
Radek Krejci857189e2020-09-01 13:26:36 +02004655 ly_bool have_spaces = 0, space_before = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004656 struct lysc_node_leaf *sleaf;
4657 LY_ERR rc = LY_SUCCESS;
4658
4659 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004660 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) &&
4661 (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004662 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004663 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
4664 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004665 } else if (!warn_is_string_type(sleaf->type)) {
4666 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004667 }
4668 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004669 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004670 return rc;
4671 }
4672
4673 if (arg_count) {
4674 set_fill_set(set, args[0]);
4675 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004676 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004677 LY_CHECK_RET(rc);
4678
4679 /* is there any normalization necessary? */
4680 for (i = 0; set->val.str[i]; ++i) {
4681 if (is_xmlws(set->val.str[i])) {
4682 if ((i == 0) || space_before || (!set->val.str[i + 1])) {
4683 have_spaces = 1;
4684 break;
4685 }
4686 space_before = 1;
4687 } else {
4688 space_before = 0;
4689 }
4690 }
4691
4692 /* yep, there is */
4693 if (have_spaces) {
4694 /* it's enough, at least one character will go, makes space for ending '\0' */
4695 new = malloc(strlen(set->val.str) * sizeof(char));
4696 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4697 new_used = 0;
4698
4699 space_before = 0;
4700 for (i = 0; set->val.str[i]; ++i) {
4701 if (is_xmlws(set->val.str[i])) {
4702 if ((i == 0) || space_before) {
4703 space_before = 1;
4704 continue;
4705 } else {
4706 space_before = 1;
4707 }
4708 } else {
4709 space_before = 0;
4710 }
4711
4712 new[new_used] = (space_before ? ' ' : set->val.str[i]);
4713 ++new_used;
4714 }
4715
4716 /* at worst there is one trailing space now */
4717 if (new_used && is_xmlws(new[new_used - 1])) {
4718 --new_used;
4719 }
4720
4721 new = ly_realloc(new, (new_used + 1) * sizeof(char));
4722 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4723 new[new_used] = '\0';
4724
4725 free(set->val.str);
4726 set->val.str = new;
4727 }
4728
4729 return LY_SUCCESS;
4730}
4731
4732/**
4733 * @brief Execute the XPath not(boolean) function. Returns LYXP_SET_BOOLEAN
4734 * with the argument converted to boolean and logically inverted.
4735 *
4736 * @param[in] args Array of arguments.
4737 * @param[in] arg_count Count of elements in @p args.
4738 * @param[in,out] set Context and result set at the same time.
4739 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004740 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004741 */
4742static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004743xpath_not(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004744{
4745 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004746 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004747 return LY_SUCCESS;
4748 }
4749
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004750 lyxp_set_cast(args[0], LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +02004751 if (args[0]->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004752 set_fill_boolean(set, 0);
4753 } else {
4754 set_fill_boolean(set, 1);
4755 }
4756
4757 return LY_SUCCESS;
4758}
4759
4760/**
4761 * @brief Execute the XPath number(object?) function. Returns LYXP_SET_NUMBER
4762 * with the number representation of either the argument or the context.
4763 *
4764 * @param[in] args Array of arguments.
4765 * @param[in] arg_count Count of elements in @p args.
4766 * @param[in,out] set Context and result set at the same time.
4767 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004768 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004769 */
4770static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004771xpath_number(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004772{
4773 LY_ERR rc;
4774
4775 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004776 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004777 return LY_SUCCESS;
4778 }
4779
4780 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004781 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004782 LY_CHECK_RET(rc);
4783 set_fill_set(set, args[0]);
4784 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004785 rc = lyxp_set_cast(set, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004786 LY_CHECK_RET(rc);
4787 }
4788
4789 return LY_SUCCESS;
4790}
4791
4792/**
4793 * @brief Execute the XPath position() function. Returns LYXP_SET_NUMBER
4794 * with the context position.
4795 *
4796 * @param[in] args Array of arguments.
4797 * @param[in] arg_count Count of elements in @p args.
4798 * @param[in,out] set Context and result set at the same time.
4799 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004800 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004801 */
4802static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004803xpath_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 +02004804{
4805 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004806 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004807 return LY_SUCCESS;
4808 }
4809
Michal Vasko03ff5a72019-09-11 13:49:33 +02004810 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004811 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "position()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004812 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004813 } else if (!set->used) {
4814 set_fill_number(set, 0);
4815 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004816 }
4817
4818 set_fill_number(set, set->ctx_pos);
4819
4820 /* UNUSED in 'Release' build type */
4821 (void)options;
4822 return LY_SUCCESS;
4823}
4824
4825/**
4826 * @brief Execute the YANG 1.1 re-match(string, string) function. Returns LYXP_SET_BOOLEAN
4827 * depending on whether the second argument regex matches the first argument string. For details refer to
4828 * YANG 1.1 RFC section 10.2.1.
4829 *
4830 * @param[in] args Array of arguments.
4831 * @param[in] arg_count Count of elements in @p args.
4832 * @param[in,out] set Context and result set at the same time.
4833 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004834 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004835 */
4836static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004837xpath_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 +02004838{
4839 struct lysc_pattern **patterns = NULL, **pattern;
4840 struct lysc_node_leaf *sleaf;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004841 LY_ERR rc = LY_SUCCESS;
4842 struct ly_err_item *err;
4843
4844 if (options & LYXP_SCNODE_ALL) {
4845 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4846 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4847 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 +02004848 } else if (!warn_is_string_type(sleaf->type)) {
4849 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004850 }
4851 }
4852
4853 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4854 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4855 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 +02004856 } else if (!warn_is_string_type(sleaf->type)) {
4857 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004858 }
4859 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004860 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004861 return rc;
4862 }
4863
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004864 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004865 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004866 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004867 LY_CHECK_RET(rc);
4868
4869 LY_ARRAY_NEW_RET(set->ctx, patterns, pattern, LY_EMEM);
Radek Iša45802b52021-02-09 09:21:58 +01004870 *pattern = calloc(1, sizeof **pattern);
Radek Krejciddace2c2021-01-08 11:30:56 +01004871 LOG_LOCSET(NULL, set->cur_node, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01004872 rc = lys_compile_type_pattern_check(set->ctx, args[1]->val.str, &(*pattern)->code);
Michal Vasko4a7d4d62021-12-13 17:05:06 +01004873 if (set->cur_node) {
4874 LOG_LOCBACK(0, 1, 0, 0);
4875 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004876 if (rc != LY_SUCCESS) {
4877 LY_ARRAY_FREE(patterns);
4878 return rc;
4879 }
4880
Radek Krejci0b013302021-03-29 15:22:32 +02004881 rc = lyplg_type_validate_patterns(patterns, args[0]->val.str, strlen(args[0]->val.str), &err);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004882 pcre2_code_free((*pattern)->code);
4883 free(*pattern);
4884 LY_ARRAY_FREE(patterns);
4885 if (rc && (rc != LY_EVALID)) {
Michal Vasko177d0ed2020-11-23 16:43:03 +01004886 ly_err_print(set->ctx, err);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004887 ly_err_free(err);
4888 return rc;
4889 }
4890
4891 if (rc == LY_EVALID) {
4892 ly_err_free(err);
4893 set_fill_boolean(set, 0);
4894 } else {
4895 set_fill_boolean(set, 1);
4896 }
4897
4898 return LY_SUCCESS;
4899}
4900
4901/**
4902 * @brief Execute the XPath round(number) function. Returns LYXP_SET_NUMBER
4903 * with the rounded first argument. For details refer to
4904 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-round.
4905 *
4906 * @param[in] args Array of arguments.
4907 * @param[in] arg_count Count of elements in @p args.
4908 * @param[in,out] set Context and result set at the same time.
4909 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004910 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004911 */
4912static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004913xpath_round(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004914{
4915 struct lysc_node_leaf *sleaf;
4916 LY_ERR rc = LY_SUCCESS;
4917
4918 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02004919 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004920 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02004921 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4922 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4923 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
4924 sleaf->name);
4925 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
4926 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
4927 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004928 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004929 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004930 return rc;
4931 }
4932
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004933 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004934 LY_CHECK_RET(rc);
4935
4936 /* cover only the cases where floor can't be used */
4937 if ((args[0]->val.num == -0.0f) || ((args[0]->val.num < 0) && (args[0]->val.num >= -0.5))) {
4938 set_fill_number(set, -0.0f);
4939 } else {
4940 args[0]->val.num += 0.5;
4941 rc = xpath_floor(args, 1, args[0], options);
4942 LY_CHECK_RET(rc);
4943 set_fill_number(set, args[0]->val.num);
4944 }
4945
4946 return LY_SUCCESS;
4947}
4948
4949/**
4950 * @brief Execute the XPath starts-with(string, string) function.
4951 * Returns LYXP_SET_BOOLEAN whether the second argument is
4952 * the prefix of the first or not.
4953 *
4954 * @param[in] args Array of arguments.
4955 * @param[in] arg_count Count of elements in @p args.
4956 * @param[in,out] set Context and result set at the same time.
4957 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004958 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004959 */
4960static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004961xpath_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 +02004962{
4963 struct lysc_node_leaf *sleaf;
4964 LY_ERR rc = LY_SUCCESS;
4965
4966 if (options & LYXP_SCNODE_ALL) {
4967 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4968 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4969 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 +02004970 } else if (!warn_is_string_type(sleaf->type)) {
4971 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004972 }
4973 }
4974
4975 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4976 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4977 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 +02004978 } else if (!warn_is_string_type(sleaf->type)) {
4979 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004980 }
4981 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004982 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004983 return rc;
4984 }
4985
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004986 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004987 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004988 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004989 LY_CHECK_RET(rc);
4990
4991 if (strncmp(args[0]->val.str, args[1]->val.str, strlen(args[1]->val.str))) {
4992 set_fill_boolean(set, 0);
4993 } else {
4994 set_fill_boolean(set, 1);
4995 }
4996
4997 return LY_SUCCESS;
4998}
4999
5000/**
5001 * @brief Execute the XPath string(object?) function. Returns LYXP_SET_STRING
5002 * with the string representation of either the argument or the context.
5003 *
5004 * @param[in] args Array of arguments.
5005 * @param[in] arg_count Count of elements in @p args.
5006 * @param[in,out] set Context and result set at the same time.
5007 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005008 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005009 */
5010static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005011xpath_string(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005012{
5013 LY_ERR rc;
5014
5015 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02005016 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005017 return LY_SUCCESS;
5018 }
5019
5020 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005021 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005022 LY_CHECK_RET(rc);
5023 set_fill_set(set, args[0]);
5024 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005025 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005026 LY_CHECK_RET(rc);
5027 }
5028
5029 return LY_SUCCESS;
5030}
5031
5032/**
5033 * @brief Execute the XPath string-length(string?) function. Returns LYXP_SET_NUMBER
5034 * with the length of the string in either the argument or the context.
5035 *
5036 * @param[in] args Array of arguments.
5037 * @param[in] arg_count Count of elements in @p args.
5038 * @param[in,out] set Context and result set at the same time.
5039 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005040 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005041 */
5042static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005043xpath_string_length(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005044{
5045 struct lysc_node_leaf *sleaf;
5046 LY_ERR rc = LY_SUCCESS;
5047
5048 if (options & LYXP_SCNODE_ALL) {
5049 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5050 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5051 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 +02005052 } else if (!warn_is_string_type(sleaf->type)) {
5053 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005054 }
5055 }
5056 if (!arg_count && (set->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set))) {
5057 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5058 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 +02005059 } else if (!warn_is_string_type(sleaf->type)) {
5060 LOGWRN(set->ctx, "Argument #0 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005061 }
5062 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005063 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005064 return rc;
5065 }
5066
5067 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005068 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005069 LY_CHECK_RET(rc);
5070 set_fill_number(set, strlen(args[0]->val.str));
5071 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005072 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005073 LY_CHECK_RET(rc);
5074 set_fill_number(set, strlen(set->val.str));
5075 }
5076
5077 return LY_SUCCESS;
5078}
5079
5080/**
5081 * @brief Execute the XPath substring(string, number, number?) function.
5082 * Returns LYXP_SET_STRING substring of the first argument starting
5083 * on the second argument index ending on the third argument index,
5084 * indexed from 1. For exact definition refer to
5085 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-substring.
5086 *
5087 * @param[in] args Array of arguments.
5088 * @param[in] arg_count Count of elements in @p args.
5089 * @param[in,out] set Context and result set at the same time.
5090 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005091 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005092 */
5093static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005094xpath_substring(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005095{
Michal Vasko6db996e2022-07-28 10:28:04 +02005096 int64_t start;
5097 int32_t len;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005098 uint16_t str_start, str_len, pos;
5099 struct lysc_node_leaf *sleaf;
5100 LY_ERR rc = LY_SUCCESS;
5101
5102 if (options & LYXP_SCNODE_ALL) {
5103 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5104 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5105 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 +02005106 } else if (!warn_is_string_type(sleaf->type)) {
5107 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005108 }
5109 }
5110
5111 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5112 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5113 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 +02005114 } else if (!warn_is_numeric_type(sleaf->type)) {
5115 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005116 }
5117 }
5118
Michal Vasko69730152020-10-09 16:30:07 +02005119 if ((arg_count == 3) && (args[2]->type == LYXP_SET_SCNODE_SET) &&
5120 (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005121 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5122 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 +02005123 } else if (!warn_is_numeric_type(sleaf->type)) {
5124 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005125 }
5126 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005127 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005128 return rc;
5129 }
5130
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005131 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005132 LY_CHECK_RET(rc);
5133
5134 /* start */
5135 if (xpath_round(&args[1], 1, args[1], options)) {
5136 return -1;
5137 }
5138 if (isfinite(args[1]->val.num)) {
5139 start = args[1]->val.num - 1;
5140 } else if (isinf(args[1]->val.num) && signbit(args[1]->val.num)) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02005141 start = INT32_MIN;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005142 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02005143 start = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005144 }
5145
5146 /* len */
5147 if (arg_count == 3) {
5148 rc = xpath_round(&args[2], 1, args[2], options);
5149 LY_CHECK_RET(rc);
Radek Krejci1deb5be2020-08-26 16:43:36 +02005150 if (isnan(args[2]->val.num) || signbit(args[2]->val.num)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005151 len = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005152 } else if (isfinite(args[2]->val.num)) {
5153 len = args[2]->val.num;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005154 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02005155 len = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005156 }
5157 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02005158 len = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005159 }
5160
5161 /* find matching character positions */
5162 str_start = 0;
5163 str_len = 0;
5164 for (pos = 0; args[0]->val.str[pos]; ++pos) {
5165 if (pos < start) {
5166 ++str_start;
5167 } else if (pos < start + len) {
5168 ++str_len;
5169 } else {
5170 break;
5171 }
5172 }
5173
5174 set_fill_string(set, args[0]->val.str + str_start, str_len);
5175 return LY_SUCCESS;
5176}
5177
5178/**
5179 * @brief Execute the XPath substring-after(string, string) function.
5180 * Returns LYXP_SET_STRING with the string succeeding the occurance
5181 * of the second argument in the first or an empty string.
5182 *
5183 * @param[in] args Array of arguments.
5184 * @param[in] arg_count Count of elements in @p args.
5185 * @param[in,out] set Context and result set at the same time.
5186 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005187 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005188 */
5189static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005190xpath_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 +02005191{
5192 char *ptr;
5193 struct lysc_node_leaf *sleaf;
5194 LY_ERR rc = LY_SUCCESS;
5195
5196 if (options & LYXP_SCNODE_ALL) {
5197 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5198 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5199 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 +02005200 } else if (!warn_is_string_type(sleaf->type)) {
5201 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005202 }
5203 }
5204
5205 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5206 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5207 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 +02005208 } else if (!warn_is_string_type(sleaf->type)) {
5209 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005210 }
5211 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005212 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005213 return rc;
5214 }
5215
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005216 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005217 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005218 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005219 LY_CHECK_RET(rc);
5220
5221 ptr = strstr(args[0]->val.str, args[1]->val.str);
5222 if (ptr) {
5223 set_fill_string(set, ptr + strlen(args[1]->val.str), strlen(ptr + strlen(args[1]->val.str)));
5224 } else {
5225 set_fill_string(set, "", 0);
5226 }
5227
5228 return LY_SUCCESS;
5229}
5230
5231/**
5232 * @brief Execute the XPath substring-before(string, string) function.
5233 * Returns LYXP_SET_STRING with the string preceding the occurance
5234 * of the second argument in the first or an empty string.
5235 *
5236 * @param[in] args Array of arguments.
5237 * @param[in] arg_count Count of elements in @p args.
5238 * @param[in,out] set Context and result set at the same time.
5239 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005240 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005241 */
5242static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005243xpath_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 +02005244{
5245 char *ptr;
5246 struct lysc_node_leaf *sleaf;
5247 LY_ERR rc = LY_SUCCESS;
5248
5249 if (options & LYXP_SCNODE_ALL) {
5250 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5251 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5252 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 +02005253 } else if (!warn_is_string_type(sleaf->type)) {
5254 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005255 }
5256 }
5257
5258 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5259 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5260 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 +02005261 } else if (!warn_is_string_type(sleaf->type)) {
5262 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005263 }
5264 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005265 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005266 return rc;
5267 }
5268
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005269 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005270 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005271 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005272 LY_CHECK_RET(rc);
5273
5274 ptr = strstr(args[0]->val.str, args[1]->val.str);
5275 if (ptr) {
5276 set_fill_string(set, args[0]->val.str, ptr - args[0]->val.str);
5277 } else {
5278 set_fill_string(set, "", 0);
5279 }
5280
5281 return LY_SUCCESS;
5282}
5283
5284/**
5285 * @brief Execute the XPath sum(node-set) function. Returns LYXP_SET_NUMBER
5286 * with the sum of all the nodes in the context.
5287 *
5288 * @param[in] args Array of arguments.
5289 * @param[in] arg_count Count of elements in @p args.
5290 * @param[in,out] set Context and result set at the same time.
5291 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005292 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005293 */
5294static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005295xpath_sum(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005296{
5297 long double num;
5298 char *str;
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01005299 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005300 struct lyxp_set set_item;
5301 struct lysc_node_leaf *sleaf;
5302 LY_ERR rc = LY_SUCCESS;
5303
5304 if (options & LYXP_SCNODE_ALL) {
5305 if (args[0]->type == LYXP_SET_SCNODE_SET) {
5306 for (i = 0; i < args[0]->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005307 if (args[0]->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005308 sleaf = (struct lysc_node_leaf *)args[0]->val.scnodes[i].scnode;
5309 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5310 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__,
Michal Vasko69730152020-10-09 16:30:07 +02005311 lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005312 } else if (!warn_is_numeric_type(sleaf->type)) {
5313 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005314 }
5315 }
5316 }
5317 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005318 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005319 return rc;
5320 }
5321
5322 set_fill_number(set, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005323
5324 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005325 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "sum(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02005326 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02005327 } else if (!args[0]->used) {
5328 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005329 }
5330
Michal Vasko5c4e5892019-11-14 12:31:38 +01005331 set_init(&set_item, set);
5332
Michal Vasko03ff5a72019-09-11 13:49:33 +02005333 set_item.type = LYXP_SET_NODE_SET;
Michal Vasko41decbf2021-11-02 11:50:21 +01005334 set_item.val.nodes = calloc(1, sizeof *set_item.val.nodes);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005335 LY_CHECK_ERR_RET(!set_item.val.nodes, LOGMEM(set->ctx), LY_EMEM);
5336
5337 set_item.used = 1;
5338 set_item.size = 1;
5339
5340 for (i = 0; i < args[0]->used; ++i) {
5341 set_item.val.nodes[0] = args[0]->val.nodes[i];
5342
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005343 rc = cast_node_set_to_string(&set_item, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005344 LY_CHECK_RET(rc);
5345 num = cast_string_to_number(str);
5346 free(str);
5347 set->val.num += num;
5348 }
5349
5350 free(set_item.val.nodes);
5351
5352 return LY_SUCCESS;
5353}
5354
5355/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02005356 * @brief Execute the XPath translate(string, string, string) function.
5357 * Returns LYXP_SET_STRING with the first argument with the characters
5358 * from the second argument replaced by those on the corresponding
5359 * positions in the third argument.
5360 *
5361 * @param[in] args Array of arguments.
5362 * @param[in] arg_count Count of elements in @p args.
5363 * @param[in,out] set Context and result set at the same time.
5364 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005365 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005366 */
5367static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005368xpath_translate(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005369{
5370 uint16_t i, j, new_used;
5371 char *new;
Radek Krejci857189e2020-09-01 13:26:36 +02005372 ly_bool have_removed;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005373 struct lysc_node_leaf *sleaf;
5374 LY_ERR rc = LY_SUCCESS;
5375
5376 if (options & LYXP_SCNODE_ALL) {
5377 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5378 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5379 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 +02005380 } else if (!warn_is_string_type(sleaf->type)) {
5381 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005382 }
5383 }
5384
5385 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5386 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5387 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 +02005388 } else if (!warn_is_string_type(sleaf->type)) {
5389 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005390 }
5391 }
5392
5393 if ((args[2]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
5394 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5395 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 +02005396 } else if (!warn_is_string_type(sleaf->type)) {
5397 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005398 }
5399 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005400 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005401 return rc;
5402 }
5403
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005404 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005405 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005406 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005407 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005408 rc = lyxp_set_cast(args[2], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005409 LY_CHECK_RET(rc);
5410
5411 new = malloc((strlen(args[0]->val.str) + 1) * sizeof(char));
5412 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5413 new_used = 0;
5414
5415 have_removed = 0;
5416 for (i = 0; args[0]->val.str[i]; ++i) {
Radek Krejci857189e2020-09-01 13:26:36 +02005417 ly_bool found = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005418
5419 for (j = 0; args[1]->val.str[j]; ++j) {
5420 if (args[0]->val.str[i] == args[1]->val.str[j]) {
5421 /* removing this char */
5422 if (j >= strlen(args[2]->val.str)) {
5423 have_removed = 1;
5424 found = 1;
5425 break;
5426 }
5427 /* replacing this char */
5428 new[new_used] = args[2]->val.str[j];
5429 ++new_used;
5430 found = 1;
5431 break;
5432 }
5433 }
5434
5435 /* copying this char */
5436 if (!found) {
5437 new[new_used] = args[0]->val.str[i];
5438 ++new_used;
5439 }
5440 }
5441
5442 if (have_removed) {
5443 new = ly_realloc(new, (new_used + 1) * sizeof(char));
5444 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5445 }
5446 new[new_used] = '\0';
5447
Michal Vaskod3678892020-05-21 10:06:58 +02005448 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005449 set->type = LYXP_SET_STRING;
5450 set->val.str = new;
5451
5452 return LY_SUCCESS;
5453}
5454
5455/**
5456 * @brief Execute the XPath true() function. Returns LYXP_SET_BOOLEAN
5457 * with true value.
5458 *
5459 * @param[in] args Array of arguments.
5460 * @param[in] arg_count Count of elements in @p args.
5461 * @param[in,out] set Context and result set at the same time.
5462 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005463 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005464 */
5465static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005466xpath_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 +02005467{
5468 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02005469 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005470 return LY_SUCCESS;
5471 }
5472
5473 set_fill_boolean(set, 1);
5474 return LY_SUCCESS;
5475}
5476
Michal Vasko03ff5a72019-09-11 13:49:33 +02005477/**
Michal Vasko49fec8e2022-05-24 10:28:33 +02005478 * @brief Execute the XPath node() processing instruction (node type). Returns LYXP_SET_NODE_SET
5479 * with only nodes from the context.
5480 *
5481 * @param[in,out] set Context and result set at the same time.
5482 * @param[in] axis Axis to search on.
5483 * @param[in] options XPath options.
5484 * @return LY_ERR
5485 */
5486static LY_ERR
5487xpath_pi_node(struct lyxp_set *set, enum lyxp_axis axis, uint32_t options)
5488{
5489 if (options & LYXP_SCNODE_ALL) {
5490 return moveto_scnode(set, NULL, NULL, axis, options);
5491 }
5492
5493 if (set->type != LYXP_SET_NODE_SET) {
5494 lyxp_set_free_content(set);
5495 return LY_SUCCESS;
5496 }
5497
5498 /* just like moving to a node with no restrictions */
5499 return moveto_node(set, NULL, NULL, axis, options);
5500}
5501
5502/**
5503 * @brief Execute the XPath text() processing instruction (node type). Returns LYXP_SET_NODE_SET
5504 * with the text content of the nodes in the context.
5505 *
5506 * @param[in,out] set Context and result set at the same time.
5507 * @param[in] axis Axis to search on.
5508 * @param[in] options XPath options.
5509 * @return LY_ERR
5510 */
5511static LY_ERR
5512xpath_pi_text(struct lyxp_set *set, enum lyxp_axis axis, uint32_t options)
5513{
5514 uint32_t i;
5515
5516 if (options & LYXP_SCNODE_ALL) {
5517 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
5518 return LY_SUCCESS;
5519 }
5520
5521 if (set->type != LYXP_SET_NODE_SET) {
5522 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "text()");
5523 return LY_EVALID;
5524 }
5525
5526 if (axis != LYXP_AXIS_CHILD) {
5527 /* even following and preceding axescan return text nodes, but whatever */
5528 lyxp_set_free_content(set);
5529 return LY_SUCCESS;
5530 }
5531
5532 for (i = 0; i < set->used; ++i) {
5533 switch (set->val.nodes[i].type) {
5534 case LYXP_NODE_NONE:
5535 LOGINT_RET(set->ctx);
5536 case LYXP_NODE_ELEM:
5537 if (set->val.nodes[i].node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
5538 set->val.nodes[i].type = LYXP_NODE_TEXT;
5539 break;
5540 }
5541 /* fall through */
5542 case LYXP_NODE_ROOT:
5543 case LYXP_NODE_ROOT_CONFIG:
5544 case LYXP_NODE_TEXT:
5545 case LYXP_NODE_META:
5546 set_remove_node_none(set, i);
5547 break;
5548 }
5549 }
5550 set_remove_nodes_none(set);
5551
5552 return LY_SUCCESS;
5553}
5554
5555/**
Michal Vasko6346ece2019-09-24 13:12:53 +02005556 * @brief Skip prefix and return corresponding model if there is a prefix. Logs directly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005557 *
Michal Vasko2104e9f2020-03-06 08:23:25 +01005558 * XPath @p set is expected to be a (sc)node set!
5559 *
Michal Vasko6346ece2019-09-24 13:12:53 +02005560 * @param[in,out] qname Qualified node name. If includes prefix, it is skipped.
5561 * @param[in,out] qname_len Length of @p qname, is updated accordingly.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005562 * @param[in] set Set with general XPath context.
5563 * @param[in] ctx_scnode Context node to inherit module for unprefixed node for ::LY_PREF_JSON.
Michal Vasko6346ece2019-09-24 13:12:53 +02005564 * @param[out] moveto_mod Expected module of a matching node.
5565 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005566 */
Michal Vasko6346ece2019-09-24 13:12:53 +02005567static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005568moveto_resolve_model(const char **qname, uint16_t *qname_len, const struct lyxp_set *set,
5569 const struct lysc_node *ctx_scnode, const struct lys_module **moveto_mod)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005570{
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02005571 const struct lys_module *mod = NULL;
Michal Vasko6346ece2019-09-24 13:12:53 +02005572 const char *ptr;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005573 size_t pref_len;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005574
Michal Vasko2104e9f2020-03-06 08:23:25 +01005575 assert((set->type == LYXP_SET_NODE_SET) || (set->type == LYXP_SET_SCNODE_SET));
5576
Michal Vasko6346ece2019-09-24 13:12:53 +02005577 if ((ptr = ly_strnchr(*qname, ':', *qname_len))) {
5578 /* specific module */
5579 pref_len = ptr - *qname;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005580 mod = ly_resolve_prefix(set->ctx, *qname, pref_len, set->format, set->prefix_data);
Michal Vasko6346ece2019-09-24 13:12:53 +02005581
Michal Vasko004d3152020-06-11 19:59:22 +02005582 /* check for errors and non-implemented modules, as they are not valid */
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005583 if (!mod || !mod->implemented) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005584 LOGVAL(set->ctx, LY_VCODE_XP_INMOD, pref_len, *qname);
Michal Vasko6346ece2019-09-24 13:12:53 +02005585 return LY_EVALID;
5586 }
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005587
Michal Vasko6346ece2019-09-24 13:12:53 +02005588 *qname += pref_len + 1;
5589 *qname_len -= pref_len + 1;
5590 } else if (((*qname)[0] == '*') && (*qname_len == 1)) {
5591 /* all modules - special case */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005592 mod = NULL;
Michal Vasko6346ece2019-09-24 13:12:53 +02005593 } else {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005594 switch (set->format) {
Radek Krejci8df109d2021-04-23 12:19:08 +02005595 case LY_VALUE_SCHEMA:
5596 case LY_VALUE_SCHEMA_RESOLVED:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005597 /* current module */
5598 mod = set->cur_mod;
5599 break;
Radek Krejci224d4b42021-04-23 13:54:59 +02005600 case LY_VALUE_CANON:
Radek Krejci8df109d2021-04-23 12:19:08 +02005601 case LY_VALUE_JSON:
Radek Krejcif9943642021-04-26 10:18:21 +02005602 case LY_VALUE_LYB:
Michal Vaskoddd76592022-01-17 13:34:48 +01005603 case LY_VALUE_STR_NS:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005604 /* inherit parent (context node) module */
5605 if (ctx_scnode) {
5606 mod = ctx_scnode->module;
5607 } else {
5608 mod = NULL;
5609 }
5610 break;
Radek Krejci8df109d2021-04-23 12:19:08 +02005611 case LY_VALUE_XML:
Michal Vasko52143d12021-04-14 15:36:39 +02005612 /* all nodes need to be prefixed */
5613 LOGVAL(set->ctx, LYVE_DATA, "Non-prefixed node \"%.*s\" in XML xpath found.", *qname_len, *qname);
5614 return LY_EVALID;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005615 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005616 }
5617
Michal Vasko6346ece2019-09-24 13:12:53 +02005618 *moveto_mod = mod;
5619 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005620}
5621
5622/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02005623 * @brief Move context @p set to the root. Handles absolute path.
5624 * Result is LYXP_SET_NODE_SET.
5625 *
5626 * @param[in,out] set Set to use.
5627 * @param[in] options Xpath options.
Michal Vaskob0099a92020-08-31 14:55:23 +02005628 * @return LY_ERR value.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005629 */
Michal Vaskob0099a92020-08-31 14:55:23 +02005630static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005631moveto_root(struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005632{
aPiecek8b0cc152021-05-31 16:40:31 +02005633 assert(!(options & LYXP_SKIP_EXPR));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005634
5635 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02005636 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskob0099a92020-08-31 14:55:23 +02005637 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, NULL, set->root_type, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005638 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005639 set->type = LYXP_SET_NODE_SET;
5640 set->used = 0;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005641 set_insert_node(set, NULL, 0, set->root_type, 0);
Michal Vasko306e2832022-07-25 09:15:17 +02005642 set->non_child_axis = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005643 }
Michal Vaskob0099a92020-08-31 14:55:23 +02005644
5645 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005646}
5647
5648/**
5649 * @brief Check @p node as a part of NameTest processing.
5650 *
5651 * @param[in] node Node to check.
Michal Vasko49fec8e2022-05-24 10:28:33 +02005652 * @param[in] node_type Node type of @p node.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005653 * @param[in] set Set to read general context from.
Michal Vaskod3678892020-05-21 10:06:58 +02005654 * @param[in] node_name Node name in the dictionary to move to, NULL for any node.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005655 * @param[in] moveto_mod Expected module of the node, NULL for no prefix.
Michal Vaskocdad7122020-11-09 21:04:44 +01005656 * @param[in] options XPath options.
Michal Vasko6346ece2019-09-24 13:12:53 +02005657 * @return LY_ERR (LY_ENOT if node does not match, LY_EINCOMPLETE on unresolved when,
Michal Vaskocd2c88a2022-06-07 10:54:34 +02005658 * LY_EINVAL if neither node nor any children match)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005659 */
5660static LY_ERR
Michal Vasko49fec8e2022-05-24 10:28:33 +02005661moveto_node_check(const struct lyd_node *node, enum lyxp_node_type node_type, const struct lyxp_set *set,
5662 const char *node_name, const struct lys_module *moveto_mod, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005663{
Michal Vasko49fec8e2022-05-24 10:28:33 +02005664 if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT)) {
5665 assert(node_type == set->root_type);
5666
5667 if (node_name || moveto_mod) {
5668 /* root will not match a specific node */
5669 return LY_ENOT;
5670 }
5671 return LY_SUCCESS;
5672 } else if (node_type != LYXP_NODE_ELEM) {
5673 /* other types will not match */
5674 return LY_ENOT;
5675 }
5676
Michal Vaskodca9f122021-07-16 13:56:22 +02005677 if (!node->schema) {
5678 /* opaque node never matches */
5679 return LY_ENOT;
5680 }
5681
Michal Vasko03ff5a72019-09-11 13:49:33 +02005682 /* module check */
Michal Vasko19089f02022-06-07 11:02:11 +02005683 if (moveto_mod) {
5684 if (!(node->flags & LYD_EXT) && (node->schema->module != moveto_mod)) {
5685 return LY_ENOT;
5686 } else if ((node->flags & LYD_EXT) && strcmp(node->schema->module->name, moveto_mod->name)) {
5687 return LY_ENOT;
5688 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005689 }
5690
Michal Vasko5c4e5892019-11-14 12:31:38 +01005691 /* context check */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005692 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (node->schema->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005693 return LY_EINVAL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005694 } else if (set->context_op && (node->schema->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) &&
5695 (node->schema != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005696 return LY_EINVAL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005697 }
5698
5699 /* name check */
Michal Vasko19089f02022-06-07 11:02:11 +02005700 if (node_name) {
5701 if (!(node->flags & LYD_EXT) && (node->schema->name != node_name)) {
5702 return LY_ENOT;
5703 } else if ((node->flags & LYD_EXT) && strcmp(node->schema->name, node_name)) {
5704 return LY_ENOT;
5705 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005706 }
5707
Michal Vaskoa1424542019-11-14 16:08:52 +01005708 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01005709 if (!(options & LYXP_IGNORE_WHEN) && lysc_has_when(node->schema) && !(node->flags & LYD_WHEN_TRUE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005710 return LY_EINCOMPLETE;
Michal Vaskoa1424542019-11-14 16:08:52 +01005711 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005712
5713 /* match */
5714 return LY_SUCCESS;
5715}
5716
5717/**
Michal Vasko49fec8e2022-05-24 10:28:33 +02005718 * @brief Get the next node in a forward DFS.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005719 *
Michal Vasko49fec8e2022-05-24 10:28:33 +02005720 * @param[in] iter Last returned node.
5721 * @param[in] stop Node to stop the search on and not return.
5722 * @return Next node, NULL if there are no more.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005723 */
Michal Vasko49fec8e2022-05-24 10:28:33 +02005724static const struct lyd_node *
5725moveto_axis_node_next_dfs_forward(const struct lyd_node *iter, const struct lyd_node *stop)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005726{
Michal Vasko49fec8e2022-05-24 10:28:33 +02005727 const struct lyd_node *next = NULL;
5728
5729 /* 1) child */
5730 next = lyd_child(iter);
5731 if (!next) {
5732 if (iter == stop) {
5733 /* reached stop, no more descendants */
5734 return NULL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005735 }
Michal Vasko49fec8e2022-05-24 10:28:33 +02005736 /* 2) child next sibling */
5737 next = iter->next;
5738 }
5739 while (!next) {
5740 iter = lyd_parent(iter);
5741 if ((!stop && !iter) || (stop && (lyd_parent(iter) == lyd_parent(stop)))) {
5742 return NULL;
5743 }
5744 next = iter->next;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005745 }
5746
Michal Vasko49fec8e2022-05-24 10:28:33 +02005747 return next;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005748}
5749
5750/**
Michal Vasko49fec8e2022-05-24 10:28:33 +02005751 * @brief Get the next node in a backward DFS.
5752 *
5753 * @param[in] iter Last returned node.
5754 * @param[in] stop Node to stop the search on and not return.
5755 * @return Next node, NULL if there are no more.
5756 */
5757static const struct lyd_node *
5758moveto_axis_node_next_dfs_backward(const struct lyd_node *iter, const struct lyd_node *stop)
5759{
5760 const struct lyd_node *next = NULL;
5761
5762 /* 1) previous sibling innermost last child */
5763 next = iter->prev->next ? iter->prev : NULL;
5764 while (next && lyd_child(next)) {
5765 next = lyd_child(next);
5766 next = next->prev;
5767 }
5768
5769 if (!next) {
5770 /* 2) parent */
5771 iter = lyd_parent(iter);
5772 if ((!stop && !iter) || (stop && (lyd_parent(iter) == lyd_parent(stop)))) {
5773 return NULL;
5774 }
5775 next = iter;
5776 }
5777
5778 return next;
5779}
5780
5781/**
5782 * @brief Get the first node on an axis for a context node.
5783 *
5784 * @param[in,out] iter NULL, updated to the next node.
5785 * @param[in,out] iter_type Node type 0 of @p iter, updated to the node type of the next node.
5786 * @param[in] node Context node.
5787 * @param[in] node_type Type of @p node.
5788 * @param[in] axis Axis to use.
5789 * @param[in] set XPath set with the general context.
5790 * @return LY_SUCCESS on success.
5791 * @return LY_ENOTFOUND if no next node found.
5792 */
5793static LY_ERR
5794moveto_axis_node_next_first(const struct lyd_node **iter, enum lyxp_node_type *iter_type, const struct lyd_node *node,
5795 enum lyxp_node_type node_type, enum lyxp_axis axis, struct lyxp_set *set)
5796{
5797 const struct lyd_node *next = NULL;
5798 enum lyxp_node_type next_type = 0;
5799
5800 assert(!*iter);
5801 assert(!*iter_type);
5802
5803 switch (axis) {
5804 case LYXP_AXIS_ANCESTOR_OR_SELF:
5805 case LYXP_AXIS_DESCENDANT_OR_SELF:
5806 case LYXP_AXIS_SELF:
5807 /* return the context node */
5808 next = node;
5809 next_type = node_type;
5810 break;
5811
5812 case LYXP_AXIS_ANCESTOR:
5813 case LYXP_AXIS_PARENT:
5814 if (node_type == LYXP_NODE_ELEM) {
5815 next = lyd_parent(node);
5816 next_type = next ? LYXP_NODE_ELEM : set->root_type;
5817 } else if (node_type == LYXP_NODE_TEXT) {
5818 next = node;
5819 next_type = LYXP_NODE_ELEM;
5820 } else if (node_type == LYXP_NODE_META) {
5821 next = ((struct lyd_meta *)node)->parent;
5822 next_type = LYXP_NODE_ELEM;
5823 } /* else root does not have a parent */
5824 break;
5825
5826 case LYXP_AXIS_CHILD:
5827 if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT)) {
5828 assert(!node);
5829
5830 /* search in all the trees */
5831 next = set->tree;
5832 next_type = next ? LYXP_NODE_ELEM : 0;
5833 } else {
5834 /* search in children */
5835 next = lyd_child(node);
5836 next_type = next ? LYXP_NODE_ELEM : 0;
5837 }
5838 break;
5839
5840 case LYXP_AXIS_DESCENDANT:
5841 if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT)) {
5842 /* top-level nodes */
5843 next = set->tree;
5844 next_type = LYXP_NODE_ELEM;
5845 } else if (node_type == LYXP_NODE_ELEM) {
5846 /* start from the context node */
5847 next = moveto_axis_node_next_dfs_forward(node, node);
5848 next_type = next ? LYXP_NODE_ELEM : 0;
5849 } /* else no children */
5850 break;
5851
5852 case LYXP_AXIS_FOLLOWING:
5853 case LYXP_AXIS_FOLLOWING_SIBLING:
5854 if (node_type == LYXP_NODE_ELEM) {
5855 /* first next sibling */
5856 next = node->next;
5857 next_type = next ? LYXP_NODE_ELEM : 0;
5858 } /* else no sibling */
5859 break;
5860
5861 case LYXP_AXIS_PRECEDING:
5862 if ((node_type == LYXP_NODE_ELEM) && node->prev->next) {
5863 /* skip ancestors */
5864 next = moveto_axis_node_next_dfs_backward(node, NULL);
5865 assert(next);
5866 next_type = LYXP_NODE_ELEM;
5867 } /* else no sibling */
5868 break;
5869
5870 case LYXP_AXIS_PRECEDING_SIBLING:
5871 if (node_type == LYXP_NODE_ELEM) {
5872 /* first previous sibling */
5873 next = node->prev->next ? node->prev : NULL;
5874 next_type = next ? LYXP_NODE_ELEM : 0;
5875 } /* else no sibling */
5876 break;
5877
5878 case LYXP_AXIS_ATTRIBUTE:
5879 /* handled specially */
5880 assert(0);
5881 LOGINT(set->ctx);
5882 break;
5883 }
5884
5885 *iter = next;
5886 *iter_type = next_type;
5887 return next_type ? LY_SUCCESS : LY_ENOTFOUND;
5888}
5889
5890/**
5891 * @brief Iterate over all nodes on an axis for a context node.
5892 *
5893 * @param[in,out] iter Last returned node, start with NULL, updated to the next node.
5894 * @param[in,out] iter_type Node type of @p iter, start with 0, updated to the node type of the next node.
5895 * @param[in] node Context node.
5896 * @param[in] node_type Type of @p node.
5897 * @param[in] axis Axis to use.
5898 * @param[in] set XPath set with the general context.
5899 * @return LY_SUCCESS on success.
5900 * @return LY_ENOTFOUND if no next node found.
5901 */
5902static LY_ERR
5903moveto_axis_node_next(const struct lyd_node **iter, enum lyxp_node_type *iter_type, const struct lyd_node *node,
5904 enum lyxp_node_type node_type, enum lyxp_axis axis, struct lyxp_set *set)
5905{
5906 const struct lyd_node *next = NULL;
5907 enum lyxp_node_type next_type = 0;
5908
5909 if (!*iter_type) {
5910 /* first returned node */
5911 return moveto_axis_node_next_first(iter, iter_type, node, node_type, axis, set);
5912 }
5913
5914 switch (axis) {
5915 case LYXP_AXIS_ANCESTOR_OR_SELF:
5916 if ((*iter == node) && (*iter_type == node_type)) {
5917 /* fake first ancestor, we returned self before */
5918 *iter = NULL;
5919 *iter_type = 0;
5920 return moveto_axis_node_next_first(iter, iter_type, node, node_type, LYXP_AXIS_ANCESTOR, set);
5921 } /* else continue ancestor */
5922
5923 /* fallthrough */
5924 case LYXP_AXIS_ANCESTOR:
5925 if (*iter_type == LYXP_NODE_ELEM) {
5926 /* iter parent */
5927 next = lyd_parent(*iter);
5928 next_type = next ? LYXP_NODE_ELEM : set->root_type;
5929 } /* else root, no ancestors */
5930 break;
5931
5932 case LYXP_AXIS_CHILD:
5933 assert(*iter_type == LYXP_NODE_ELEM);
5934
5935 /* next sibling (child) */
5936 next = (*iter)->next;
5937 next_type = next ? LYXP_NODE_ELEM : 0;
5938 break;
5939
5940 case LYXP_AXIS_DESCENDANT_OR_SELF:
5941 if ((*iter == node) && (*iter_type == node_type)) {
5942 /* fake first descendant, we returned self before */
5943 *iter = NULL;
5944 *iter_type = 0;
5945 return moveto_axis_node_next_first(iter, iter_type, node, node_type, LYXP_AXIS_DESCENDANT, set);
5946 } /* else continue descendant */
5947
5948 /* fallthrough */
5949 case LYXP_AXIS_DESCENDANT:
5950 assert(*iter_type == LYXP_NODE_ELEM);
5951 next = moveto_axis_node_next_dfs_forward(*iter, node);
5952 next_type = next ? LYXP_NODE_ELEM : 0;
5953 break;
5954
5955 case LYXP_AXIS_FOLLOWING:
5956 assert(*iter_type == LYXP_NODE_ELEM);
5957 next = moveto_axis_node_next_dfs_forward(*iter, NULL);
5958 next_type = next ? LYXP_NODE_ELEM : 0;
5959 break;
5960
5961 case LYXP_AXIS_FOLLOWING_SIBLING:
5962 assert(*iter_type == LYXP_NODE_ELEM);
5963
5964 /* next sibling */
5965 next = (*iter)->next;
5966 next_type = next ? LYXP_NODE_ELEM : 0;
5967 break;
5968
5969 case LYXP_AXIS_PARENT:
5970 case LYXP_AXIS_SELF:
5971 /* parent/self was returned before */
5972 break;
5973
5974 case LYXP_AXIS_PRECEDING:
5975 assert(*iter_type == LYXP_NODE_ELEM);
5976 next = moveto_axis_node_next_dfs_backward(*iter, NULL);
5977 next_type = next ? LYXP_NODE_ELEM : 0;
5978 break;
5979
5980 case LYXP_AXIS_PRECEDING_SIBLING:
5981 assert(*iter_type == LYXP_NODE_ELEM);
5982
5983 /* previous sibling */
5984 next = (*iter)->prev->next ? (*iter)->prev : NULL;
5985 next_type = next ? LYXP_NODE_ELEM : 0;
5986 break;
5987
5988 case LYXP_AXIS_ATTRIBUTE:
5989 /* handled specially */
5990 assert(0);
5991 LOGINT(set->ctx);
5992 break;
5993 }
5994
5995 *iter = next;
5996 *iter_type = next_type;
5997 return next_type ? LY_SUCCESS : LY_ENOTFOUND;
5998}
5999
6000/**
6001 * @brief Move context @p set to a node. Result is LYXP_SET_NODE_SET. Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006002 *
6003 * @param[in,out] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006004 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02006005 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko49fec8e2022-05-24 10:28:33 +02006006 * @param[in] axis Axis to search on.
Michal Vaskocdad7122020-11-09 21:04:44 +01006007 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006008 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6009 */
6010static LY_ERR
Michal Vasko49fec8e2022-05-24 10:28:33 +02006011moveto_node(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, enum lyxp_axis axis,
6012 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006013{
Michal Vasko230cf972021-12-02 12:31:00 +01006014 LY_ERR r, rc = LY_SUCCESS;
Michal Vasko49fec8e2022-05-24 10:28:33 +02006015 const struct lyd_node *iter;
6016 enum lyxp_node_type iter_type;
Michal Vasko230cf972021-12-02 12:31:00 +01006017 struct lyxp_set result;
Michal Vasko49fec8e2022-05-24 10:28:33 +02006018 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006019
aPiecek8b0cc152021-05-31 16:40:31 +02006020 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006021 return LY_SUCCESS;
6022 }
6023
6024 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006025 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006026 return LY_EVALID;
6027 }
6028
Michal Vasko230cf972021-12-02 12:31:00 +01006029 /* init result set */
6030 set_init(&result, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006031
Michal Vasko49fec8e2022-05-24 10:28:33 +02006032 for (i = 0; i < set->used; ++i) {
6033 /* iterate over all the nodes on the axis of the node */
6034 iter = NULL;
6035 iter_type = 0;
6036 while (!moveto_axis_node_next(&iter, &iter_type, set->val.nodes[i].node, set->val.nodes[i].type, axis, set)) {
6037 r = moveto_node_check(iter, iter_type, set, ncname, moveto_mod, options);
6038 if (r == LY_EINCOMPLETE) {
Michal Vasko230cf972021-12-02 12:31:00 +01006039 rc = r;
6040 goto cleanup;
Michal Vasko49fec8e2022-05-24 10:28:33 +02006041 } else if (r) {
6042 continue;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006043 }
Michal Vasko49fec8e2022-05-24 10:28:33 +02006044
6045 /* check for duplicates if they are possible */
6046 switch (axis) {
6047 case LYXP_AXIS_ANCESTOR:
6048 case LYXP_AXIS_ANCESTOR_OR_SELF:
6049 case LYXP_AXIS_DESCENDANT:
6050 case LYXP_AXIS_DESCENDANT_OR_SELF:
6051 case LYXP_AXIS_FOLLOWING:
6052 case LYXP_AXIS_FOLLOWING_SIBLING:
6053 case LYXP_AXIS_PARENT:
6054 case LYXP_AXIS_PRECEDING:
6055 case LYXP_AXIS_PRECEDING_SIBLING:
Michal Vasko306e2832022-07-25 09:15:17 +02006056 result.non_child_axis = 1;
Michal Vasko49fec8e2022-05-24 10:28:33 +02006057 if (set_dup_node_check(&result, iter, iter_type, -1)) {
6058 continue;
6059 }
6060 break;
6061 case LYXP_AXIS_CHILD:
6062 case LYXP_AXIS_SELF:
6063 break;
6064 case LYXP_AXIS_ATTRIBUTE:
6065 /* handled specially */
6066 assert(0);
6067 LOGINT(set->ctx);
6068 break;
6069 }
6070
6071 /* matching node */
6072 set_insert_node(&result, iter, 0, iter_type, result.used);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006073 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006074 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006075
Michal Vasko230cf972021-12-02 12:31:00 +01006076 /* move result to the set */
6077 lyxp_set_free_content(set);
6078 *set = result;
6079 result.type = LYXP_SET_NUMBER;
Michal Vasko49fec8e2022-05-24 10:28:33 +02006080
Michal Vasko306e2832022-07-25 09:15:17 +02006081 /* sort the final set if the document order could have been broken */
6082 if (set->non_child_axis) {
6083 set_sort(set);
6084 } else {
6085 assert(!set_sort(set));
6086 }
Michal Vasko230cf972021-12-02 12:31:00 +01006087
6088cleanup:
6089 lyxp_set_free_content(&result);
6090 return rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006091}
6092
6093/**
Michal Vasko49fec8e2022-05-24 10:28:33 +02006094 * @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 +02006095 *
6096 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02006097 * @param[in] scnode Matching node schema.
Michal Vasko004d3152020-06-11 19:59:22 +02006098 * @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 +01006099 * @param[in] options XPath options.
Michal Vaskod3678892020-05-21 10:06:58 +02006100 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6101 */
6102static LY_ERR
Michal Vasko49fec8e2022-05-24 10:28:33 +02006103moveto_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 +01006104 uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02006105{
Michal Vaskoddd76592022-01-17 13:34:48 +01006106 LY_ERR ret = LY_SUCCESS, r;
Michal Vaskod3678892020-05-21 10:06:58 +02006107 uint32_t i;
Michal Vaskod3678892020-05-21 10:06:58 +02006108 const struct lyd_node *siblings;
Michal Vasko230cf972021-12-02 12:31:00 +01006109 struct lyxp_set result;
Michal Vasko004d3152020-06-11 19:59:22 +02006110 struct lyd_node *sub, *inst = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02006111
Michal Vasko004d3152020-06-11 19:59:22 +02006112 assert(scnode && (!(scnode->nodetype & (LYS_LIST | LYS_LEAFLIST)) || predicates));
Michal Vaskod3678892020-05-21 10:06:58 +02006113
Michal Vasko50aaa072021-12-02 13:11:56 +01006114 /* init result set */
6115 set_init(&result, set);
6116
aPiecek8b0cc152021-05-31 16:40:31 +02006117 if (options & LYXP_SKIP_EXPR) {
Michal Vasko004d3152020-06-11 19:59:22 +02006118 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02006119 }
6120
6121 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006122 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko004d3152020-06-11 19:59:22 +02006123 ret = LY_EVALID;
6124 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02006125 }
6126
6127 /* context check for all the nodes since we have the schema node */
6128 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) {
6129 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02006130 goto cleanup;
Michal Vasko69730152020-10-09 16:30:07 +02006131 } else if (set->context_op && (scnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) &&
6132 (scnode != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02006133 lyxp_set_free_content(set);
6134 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +02006135 }
6136
6137 /* create specific data instance if needed */
6138 if (scnode->nodetype == LYS_LIST) {
6139 LY_CHECK_GOTO(ret = lyd_create_list(scnode, predicates, &inst), cleanup);
6140 } else if (scnode->nodetype == LYS_LEAFLIST) {
6141 LY_CHECK_GOTO(ret = lyd_create_term2(scnode, &predicates[0].value, &inst), cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02006142 }
6143
Michal Vasko230cf972021-12-02 12:31:00 +01006144 for (i = 0; i < set->used; ++i) {
Michal Vaskod3678892020-05-21 10:06:58 +02006145 siblings = NULL;
6146
6147 if ((set->val.nodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.nodes[i].type == LYXP_NODE_ROOT)) {
6148 assert(!set->val.nodes[i].node);
6149
6150 /* search in all the trees */
6151 siblings = set->tree;
6152 } else if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
6153 /* search in children */
Radek Krejcia1c1e542020-09-29 16:06:52 +02006154 siblings = lyd_child(set->val.nodes[i].node);
Michal Vaskod3678892020-05-21 10:06:58 +02006155 }
6156
6157 /* find the node using hashes */
Michal Vasko004d3152020-06-11 19:59:22 +02006158 if (inst) {
Michal Vaskoddd76592022-01-17 13:34:48 +01006159 r = lyd_find_sibling_first(siblings, inst, &sub);
Michal Vaskod3678892020-05-21 10:06:58 +02006160 } else {
Michal Vaskoddd76592022-01-17 13:34:48 +01006161 r = lyd_find_sibling_val(siblings, scnode, NULL, 0, &sub);
Michal Vaskod3678892020-05-21 10:06:58 +02006162 }
Michal Vaskoddd76592022-01-17 13:34:48 +01006163 LY_CHECK_ERR_GOTO(r && (r != LY_ENOTFOUND), ret = r, cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02006164
6165 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01006166 if (!(options & LYXP_IGNORE_WHEN) && sub && lysc_has_when(sub->schema) && !(sub->flags & LYD_WHEN_TRUE)) {
Michal Vasko004d3152020-06-11 19:59:22 +02006167 ret = LY_EINCOMPLETE;
6168 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02006169 }
6170
6171 if (sub) {
6172 /* pos filled later */
Michal Vasko230cf972021-12-02 12:31:00 +01006173 set_insert_node(&result, sub, 0, LYXP_NODE_ELEM, result.used);
Michal Vaskod3678892020-05-21 10:06:58 +02006174 }
6175 }
6176
Michal Vasko230cf972021-12-02 12:31:00 +01006177 /* move result to the set */
6178 lyxp_set_free_content(set);
6179 *set = result;
6180 result.type = LYXP_SET_NUMBER;
6181 assert(!set_sort(set));
6182
Michal Vasko004d3152020-06-11 19:59:22 +02006183cleanup:
Michal Vasko230cf972021-12-02 12:31:00 +01006184 lyxp_set_free_content(&result);
Michal Vasko004d3152020-06-11 19:59:22 +02006185 lyd_free_tree(inst);
6186 return ret;
Michal Vaskod3678892020-05-21 10:06:58 +02006187}
6188
6189/**
Michal Vasko49fec8e2022-05-24 10:28:33 +02006190 * @brief Check @p node as a part of schema NameTest processing.
6191 *
6192 * @param[in] node Schema node to check.
6193 * @param[in] ctx_scnode Context node.
6194 * @param[in] set Set to read general context from.
6195 * @param[in] node_name Node name in the dictionary to move to, NULL for any nodes.
6196 * @param[in] moveto_mod Expected module of the node, NULL for no prefix.
6197 * @return LY_ERR (LY_ENOT if node does not match, LY_EINVAL if neither node nor any children match)
6198 */
6199static LY_ERR
6200moveto_scnode_check(const struct lysc_node *node, const struct lysc_node *ctx_scnode, const struct lyxp_set *set,
6201 const char *node_name, const struct lys_module *moveto_mod)
6202{
6203 if (!moveto_mod && node_name) {
6204 switch (set->format) {
6205 case LY_VALUE_SCHEMA:
6206 case LY_VALUE_SCHEMA_RESOLVED:
6207 /* use current module */
6208 moveto_mod = set->cur_mod;
6209 break;
6210 case LY_VALUE_JSON:
6211 case LY_VALUE_LYB:
6212 case LY_VALUE_STR_NS:
6213 /* inherit module of the context node, if any */
6214 if (ctx_scnode) {
6215 moveto_mod = ctx_scnode->module;
6216 }
6217 break;
6218 case LY_VALUE_CANON:
6219 case LY_VALUE_XML:
6220 /* not defined */
6221 LOGINT(set->ctx);
6222 return LY_EINVAL;
6223 }
6224 }
6225
6226 if (!node) {
6227 /* root will not match a specific node */
6228 if (node_name || moveto_mod) {
6229 return LY_ENOT;
6230 }
6231 return LY_SUCCESS;
6232 }
6233
6234 /* module check */
6235 if (moveto_mod && (node->module != moveto_mod)) {
6236 return LY_ENOT;
6237 }
6238
6239 /* context check */
6240 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (node->flags & LYS_CONFIG_R)) {
6241 return LY_EINVAL;
6242 } else if (set->context_op && (node->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && (node != set->context_op)) {
6243 return LY_EINVAL;
6244 }
6245
6246 /* name check */
6247 if (node_name && (node->name != node_name)) {
6248 return LY_ENOT;
6249 }
6250
6251 /* match */
6252 return LY_SUCCESS;
6253}
6254
6255/**
6256 * @brief Get the next node in a forward schema node DFS.
6257 *
6258 * @param[in] iter Last returned node.
6259 * @param[in] stop Node to stop the search on and not return.
6260 * @param[in] getnext_opts Options for ::lys_getnext().
6261 * @return Next node, NULL if there are no more.
6262 */
6263static const struct lysc_node *
6264moveto_axis_scnode_next_dfs_forward(const struct lysc_node *iter, const struct lysc_node *stop, uint32_t getnext_opts)
6265{
6266 const struct lysc_node *next = NULL;
6267
6268 next = lysc_node_child(iter);
6269 if (!next) {
6270 /* no children, try siblings */
Michal Vasko34a22fe2022-06-15 07:58:55 +02006271 if ((iter == stop) || !lysc_data_parent(iter)) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02006272 /* we are done, no next element to process */
6273 return NULL;
6274 }
6275
6276 next = lys_getnext(iter, lysc_data_parent(iter), NULL, getnext_opts);
6277 }
6278 while (!next && iter) {
6279 /* parent is already processed, go to its sibling */
6280 iter = iter->parent;
Michal Vasko34a22fe2022-06-15 07:58:55 +02006281 if ((iter == stop) || !lysc_data_parent(iter)) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02006282 /* we are done, no next element to process */
6283 return NULL;
6284 }
6285 next = lys_getnext(iter, lysc_data_parent(iter), NULL, getnext_opts);
6286 }
6287
6288 return next;
6289}
6290
6291/**
6292 * @brief Consider schema node based on its in_ctx enum value.
6293 *
6294 * @param[in,out] in_ctx In_ctx enum of the schema node, may be updated.
6295 * @param[in] axis Axis to use.
6296 * @return LY_SUCCESS on success.
6297 * @return LY_ENOT if the node should not be returned.
6298 */
6299static LY_ERR
6300moveto_axis_scnode_next_in_ctx(int32_t *in_ctx, enum lyxp_axis axis)
6301{
6302 switch (axis) {
6303 case LYXP_AXIS_SELF:
6304 if ((*in_ctx == LYXP_SET_SCNODE_START) || (*in_ctx == LYXP_SET_SCNODE_ATOM_CTX)) {
6305 /* additionally put the start node into context */
6306 *in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
6307 return LY_SUCCESS;
6308 }
6309 break;
6310 case LYXP_AXIS_PARENT:
6311 case LYXP_AXIS_ANCESTOR_OR_SELF:
6312 case LYXP_AXIS_ANCESTOR:
6313 case LYXP_AXIS_DESCENDANT_OR_SELF:
6314 case LYXP_AXIS_DESCENDANT:
6315 case LYXP_AXIS_FOLLOWING:
6316 case LYXP_AXIS_FOLLOWING_SIBLING:
6317 case LYXP_AXIS_PRECEDING:
6318 case LYXP_AXIS_PRECEDING_SIBLING:
6319 case LYXP_AXIS_CHILD:
6320 if (*in_ctx == LYXP_SET_SCNODE_START) {
6321 /* remember that context node was used */
6322 *in_ctx = LYXP_SET_SCNODE_START_USED;
6323 return LY_SUCCESS;
6324 } else if (*in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
6325 /* traversed */
6326 *in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
6327 return LY_SUCCESS;
6328 }
6329 break;
6330 case LYXP_AXIS_ATTRIBUTE:
6331 /* unreachable */
6332 assert(0);
6333 LOGINT(NULL);
6334 break;
6335 }
6336
6337 return LY_ENOT;
6338}
6339
6340/**
6341 * @brief Get previous sibling for a schema node.
6342 *
6343 * @param[in] scnode Schema node.
6344 * @param[in] getnext_opts Options for ::lys_getnext().
6345 * @return Previous sibling, NULL if none.
6346 */
6347static const struct lysc_node *
6348moveto_axis_scnode_preceding_sibling(const struct lysc_node *scnode, uint32_t getnext_opts)
6349{
6350 const struct lysc_node *next = NULL, *prev = NULL;
6351
6352 while ((next = lys_getnext(next, lysc_data_parent(scnode), scnode->module->compiled, getnext_opts))) {
6353 if (next == scnode) {
6354 break;
6355 }
6356
6357 prev = next;
6358 }
6359
6360 return prev;
6361}
6362
6363/**
6364 * @brief Get the first schema node on an axis for a context node.
6365 *
6366 * @param[in,out] iter Last returned node, start with NULL, updated to the next node.
6367 * @param[in,out] iter_type Node type of @p iter, start with 0, updated to the node type of the next node.
6368 * @param[in,out] iter_mod Internal module iterator, do not change.
6369 * @param[in,out] iter_mod_idx Internal module index iterator, do not change.
6370 * @param[in] scnode Context node.
6371 * @param[in] node_type Type of @p scnode.
6372 * @param[in] in_ctx In_ctx enum of @p scnode.
6373 * @param[in] axis Axis to use.
6374 * @param[in] set XPath set with the general context.
6375 * @param[in] getnext_opts Options for ::lys_getnext().
6376 * @return LY_SUCCESS on success.
6377 * @return LY_ENOTFOUND if no next node found.
6378 */
6379static LY_ERR
6380moveto_axis_scnode_next_first(const struct lysc_node **iter, enum lyxp_node_type *iter_type, const struct lys_module **iter_mod,
6381 uint32_t *iter_mod_idx, const struct lysc_node *scnode, enum lyxp_node_type node_type, enum lyxp_axis axis,
6382 struct lyxp_set *set, uint32_t getnext_opts)
6383{
6384 const struct lysc_node *next = NULL;
6385 enum lyxp_node_type next_type = 0;
6386
6387 assert(!*iter);
6388 assert(!*iter_type);
6389
6390 *iter_mod = NULL;
6391 *iter_mod_idx = 0;
6392
6393 switch (axis) {
6394 case LYXP_AXIS_ANCESTOR_OR_SELF:
6395 case LYXP_AXIS_DESCENDANT_OR_SELF:
6396 case LYXP_AXIS_SELF:
6397 if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT) || (node_type == LYXP_NODE_ELEM)) {
6398 /* just return the node */
6399 next = scnode;
6400 next_type = node_type;
6401 }
6402 break;
6403
6404 case LYXP_AXIS_ANCESTOR:
6405 case LYXP_AXIS_PARENT:
6406 if (node_type == LYXP_NODE_ELEM) {
6407 next = lysc_data_parent(scnode);
6408 next_type = next ? LYXP_NODE_ELEM : set->root_type;
6409 } /* else no parent */
6410 break;
6411
6412 case LYXP_AXIS_DESCENDANT:
6413 case LYXP_AXIS_CHILD:
6414 if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT)) {
6415 /* it can actually be in any module, it's all <running>, and even if it's moveto_mod (if set),
6416 * it can be in a top-level augment */
6417 while ((*iter_mod = ly_ctx_get_module_iter(set->ctx, iter_mod_idx))) {
6418 /* module may not be implemented or not compiled yet */
6419 if (!(*iter_mod)->compiled) {
6420 continue;
6421 }
6422
6423 /* get next node */
6424 if ((next = lys_getnext(NULL, NULL, (*iter_mod)->compiled, getnext_opts))) {
6425 next_type = LYXP_NODE_ELEM;
6426 break;
6427 }
6428 }
6429 } else if (node_type == LYXP_NODE_ELEM) {
6430 /* get next node */
6431 next = lys_getnext(NULL, scnode, NULL, getnext_opts);
6432 next_type = next ? LYXP_NODE_ELEM : 0;
6433 }
6434 break;
6435
6436 case LYXP_AXIS_FOLLOWING:
6437 case LYXP_AXIS_FOLLOWING_SIBLING:
6438 if (node_type == LYXP_NODE_ELEM) {
6439 /* first next sibling */
6440 next = lys_getnext(scnode, lysc_data_parent(scnode), scnode->module->compiled, getnext_opts);
6441 next_type = next ? LYXP_NODE_ELEM : 0;
6442 } /* else no sibling */
6443 break;
6444
6445 case LYXP_AXIS_PRECEDING:
6446 case LYXP_AXIS_PRECEDING_SIBLING:
6447 if (node_type == LYXP_NODE_ELEM) {
6448 /* first parent sibling */
6449 next = lys_getnext(NULL, lysc_data_parent(scnode), scnode->module->compiled, getnext_opts);
6450 if (next == scnode) {
6451 /* no preceding sibling */
6452 next = NULL;
6453 }
6454 next_type = next ? LYXP_NODE_ELEM : 0;
6455 } /* else no sibling */
6456 break;
6457
6458 case LYXP_AXIS_ATTRIBUTE:
6459 /* unreachable */
6460 assert(0);
6461 LOGINT(set->ctx);
6462 break;
6463 }
6464
6465 *iter = next;
6466 *iter_type = next_type;
6467 return next_type ? LY_SUCCESS : LY_ENOTFOUND;
6468}
6469
6470/**
6471 * @brief Iterate over all schema nodes on an axis for a context node.
6472 *
6473 * @param[in,out] iter Last returned node, start with NULL, updated to the next node.
6474 * @param[in,out] iter_type Node type of @p iter, start with 0, updated to the node type of the next node.
6475 * @param[in,out] iter_mod Internal module iterator, do not change.
6476 * @param[in,out] iter_mod_idx Internal module index iterator, do not change.
6477 * @param[in] scnode Context node.
6478 * @param[in] node_type Type of @p scnode.
6479 * @param[in] axis Axis to use.
6480 * @param[in] set XPath set with the general context.
6481 * @param[in] getnext_opts Options for ::lys_getnext().
6482 * @return LY_SUCCESS on success.
6483 * @return LY_ENOTFOUND if no next node found.
6484 */
6485static LY_ERR
6486moveto_axis_scnode_next(const struct lysc_node **iter, enum lyxp_node_type *iter_type, const struct lys_module **iter_mod,
6487 uint32_t *iter_mod_idx, const struct lysc_node *scnode, enum lyxp_node_type node_type, enum lyxp_axis axis,
6488 struct lyxp_set *set, uint32_t getnext_opts)
6489{
6490 const struct lysc_node *next = NULL, *dfs_stop;
6491 enum lyxp_node_type next_type = 0;
6492
6493 if (!*iter_type) {
6494 /* first returned node */
6495 return moveto_axis_scnode_next_first(iter, iter_type, iter_mod, iter_mod_idx, scnode, node_type, axis, set,
6496 getnext_opts);
6497 }
6498
6499 switch (axis) {
6500 case LYXP_AXIS_PARENT:
6501 case LYXP_AXIS_SELF:
6502 /* parent/self was returned before */
6503 break;
6504
6505 case LYXP_AXIS_ANCESTOR_OR_SELF:
6506 if ((*iter == scnode) && (*iter_type == node_type)) {
6507 /* fake first ancestor, we returned self before */
6508 *iter = NULL;
6509 *iter_type = 0;
6510 return moveto_axis_scnode_next_first(iter, iter_type, iter_mod, iter_mod_idx, scnode, node_type,
6511 LYXP_AXIS_ANCESTOR, set, getnext_opts);
6512 } /* else continue ancestor */
6513
6514 /* fallthrough */
6515 case LYXP_AXIS_ANCESTOR:
6516 if (*iter_type == LYXP_NODE_ELEM) {
6517 next = lysc_data_parent(*iter);
6518 next_type = next ? LYXP_NODE_ELEM : set->root_type;
6519 } /* else no ancestor */
6520 break;
6521
6522 case LYXP_AXIS_DESCENDANT_OR_SELF:
6523 if ((*iter == scnode) && (*iter_type == node_type)) {
6524 /* fake first descendant, we returned self before */
6525 *iter = NULL;
6526 *iter_type = 0;
6527 return moveto_axis_scnode_next_first(iter, iter_type, iter_mod, iter_mod_idx, scnode, node_type,
6528 LYXP_AXIS_DESCENDANT, set, getnext_opts);
6529 } /* else DFS until context node */
6530 dfs_stop = scnode;
6531
6532 /* fallthrough */
6533 case LYXP_AXIS_DESCENDANT:
6534 if (axis == LYXP_AXIS_DESCENDANT) {
6535 /* DFS until the context node */
6536 dfs_stop = scnode;
6537 }
6538
6539 /* fallthrough */
6540 case LYXP_AXIS_PRECEDING:
6541 if (axis == LYXP_AXIS_PRECEDING) {
6542 /* DFS until the previous sibling */
6543 dfs_stop = moveto_axis_scnode_preceding_sibling(scnode, getnext_opts);
6544 assert(dfs_stop);
6545
6546 if (*iter == dfs_stop) {
6547 /* we are done */
6548 break;
6549 }
6550 }
6551
6552 /* fallthrough */
6553 case LYXP_AXIS_FOLLOWING:
6554 if (axis == LYXP_AXIS_FOLLOWING) {
6555 /* DFS through the whole module */
6556 dfs_stop = NULL;
6557 }
6558
6559 /* nested nodes */
6560 assert(*iter);
6561 next = moveto_axis_scnode_next_dfs_forward(*iter, dfs_stop, getnext_opts);
6562 if (next) {
6563 next_type = LYXP_NODE_ELEM;
6564 break;
6565 } /* else get next top-level node just like a child */
6566
6567 /* fallthrough */
6568 case LYXP_AXIS_CHILD:
6569 case LYXP_AXIS_FOLLOWING_SIBLING:
6570 if (!*iter_mod) {
6571 /* nodes from a single module */
6572 if ((next = lys_getnext(*iter, lysc_data_parent(*iter), (*iter)->module->compiled, getnext_opts))) {
6573 next_type = LYXP_NODE_ELEM;
6574 break;
6575 }
6576
6577 assert(scnode);
6578 if (!lysc_data_parent(scnode)) {
6579 /* iterating over top-level nodes, find next */
6580 while (lysc_data_parent(*iter)) {
6581 *iter = lysc_data_parent(*iter);
6582 }
6583 if ((next = lys_getnext(*iter, NULL, (*iter)->module->compiled, getnext_opts))) {
6584 next_type = LYXP_NODE_ELEM;
6585 break;
6586 }
6587 }
6588 }
6589
6590 while (*iter_mod) {
6591 /* module top-level nodes */
6592 if ((next = lys_getnext(*iter, NULL, (*iter_mod)->compiled, getnext_opts))) {
6593 next_type = LYXP_NODE_ELEM;
6594 break;
6595 }
6596
6597 /* get next module */
6598 while ((*iter_mod = ly_ctx_get_module_iter(set->ctx, iter_mod_idx))) {
6599 /* module may not be implemented or not compiled yet */
6600 if ((*iter_mod)->compiled) {
6601 break;
6602 }
6603 }
6604
6605 /* new module, start over */
6606 *iter = NULL;
6607 }
6608 break;
6609
6610 case LYXP_AXIS_PRECEDING_SIBLING:
6611 assert(*iter);
6612
6613 /* next parent sibling until scnode */
6614 next = lys_getnext(*iter, lysc_data_parent(*iter), (*iter)->module->compiled, getnext_opts);
6615 if (next == scnode) {
6616 /* no previous sibling */
6617 next = NULL;
6618 }
6619 next_type = next ? LYXP_NODE_ELEM : 0;
6620 break;
6621
6622 case LYXP_AXIS_ATTRIBUTE:
6623 /* unreachable */
6624 assert(0);
6625 LOGINT(set->ctx);
6626 break;
6627 }
6628
6629 *iter = next;
6630 *iter_type = next_type;
6631 return next_type ? LY_SUCCESS : LY_ENOTFOUND;
6632}
6633
6634/**
Michal Vaskod3678892020-05-21 10:06:58 +02006635 * @brief Move context @p set to a schema node. Result is LYXP_SET_SCNODE_SET (or LYXP_SET_EMPTY).
6636 *
6637 * @param[in,out] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006638 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02006639 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko49fec8e2022-05-24 10:28:33 +02006640 * @param[in] axis Axis to search on.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006641 * @param[in] options XPath options.
6642 * @return LY_ERR
6643 */
6644static LY_ERR
Michal Vasko49fec8e2022-05-24 10:28:33 +02006645moveto_scnode(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, enum lyxp_axis axis,
6646 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006647{
Radek Krejci857189e2020-09-01 13:26:36 +02006648 ly_bool temp_ctx = 0;
Michal Vasko49fec8e2022-05-24 10:28:33 +02006649 uint32_t getnext_opts, orig_used, i, mod_idx, idx;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006650 const struct lys_module *mod;
Michal Vasko49fec8e2022-05-24 10:28:33 +02006651 const struct lysc_node *iter;
6652 enum lyxp_node_type iter_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006653
aPiecek8b0cc152021-05-31 16:40:31 +02006654 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006655 return LY_SUCCESS;
6656 }
6657
6658 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006659 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006660 return LY_EVALID;
6661 }
6662
Michal Vaskocafad9d2019-11-07 15:20:03 +01006663 /* getnext opts */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01006664 getnext_opts = 0;
Michal Vaskocafad9d2019-11-07 15:20:03 +01006665 if (options & LYXP_SCNODE_OUTPUT) {
6666 getnext_opts |= LYS_GETNEXT_OUTPUT;
6667 }
6668
Michal Vasko03ff5a72019-09-11 13:49:33 +02006669 orig_used = set->used;
6670 for (i = 0; i < orig_used; ++i) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02006671 /* update in_ctx first */
6672 if (moveto_axis_scnode_next_in_ctx(&set->val.scnodes[i].in_ctx, axis)) {
6673 /* not usable, skip */
6674 continue;
6675 }
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006676
Michal Vasko49fec8e2022-05-24 10:28:33 +02006677 iter = NULL;
6678 iter_type = 0;
6679 while (!moveto_axis_scnode_next(&iter, &iter_type, &mod, &mod_idx, set->val.scnodes[i].scnode,
6680 set->val.scnodes[i].type, axis, set, getnext_opts)) {
6681 if (moveto_scnode_check(iter, NULL, set, ncname, moveto_mod)) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006682 continue;
6683 }
6684
Michal Vasko49fec8e2022-05-24 10:28:33 +02006685 /* insert */
6686 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, iter_type, &idx));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006687
Michal Vasko49fec8e2022-05-24 10:28:33 +02006688 /* we need to prevent these nodes from being considered in this moveto */
6689 if ((idx < orig_used) && (idx > i)) {
6690 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX;
6691 temp_ctx = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006692 }
6693 }
6694 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006695
6696 /* correct temporary in_ctx values */
6697 if (temp_ctx) {
6698 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006699 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_NEW_CTX) {
6700 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006701 }
6702 }
6703 }
6704
6705 return LY_SUCCESS;
6706}
6707
6708/**
Michal Vasko49fec8e2022-05-24 10:28:33 +02006709 * @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 +02006710 * Context position aware.
6711 *
6712 * @param[in] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006713 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02006714 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vaskocdad7122020-11-09 21:04:44 +01006715 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006716 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6717 */
6718static LY_ERR
Michal Vasko49fec8e2022-05-24 10:28:33 +02006719moveto_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 +02006720{
6721 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006722 const struct lyd_node *next, *elem, *start;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006723 struct lyxp_set ret_set;
6724 LY_ERR rc;
6725
aPiecek8b0cc152021-05-31 16:40:31 +02006726 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006727 return LY_SUCCESS;
6728 }
6729
6730 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006731 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006732 return LY_EVALID;
6733 }
6734
Michal Vasko9f96a052020-03-10 09:41:45 +01006735 /* 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 +02006736 rc = xpath_pi_node(set, LYXP_AXIS_CHILD, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006737 LY_CHECK_RET(rc);
6738
Michal Vasko6346ece2019-09-24 13:12:53 +02006739 /* this loop traverses all the nodes in the set and adds/keeps only those that match qname */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006740 set_init(&ret_set, set);
6741 for (i = 0; i < set->used; ++i) {
6742
6743 /* TREE DFS */
6744 start = set->val.nodes[i].node;
6745 for (elem = next = start; elem; elem = next) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02006746 rc = moveto_node_check(elem, LYXP_NODE_ELEM, set, ncname, moveto_mod, options);
Michal Vasko6346ece2019-09-24 13:12:53 +02006747 if (!rc) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006748 /* add matching node into result set */
6749 set_insert_node(&ret_set, elem, 0, LYXP_NODE_ELEM, ret_set.used);
6750 if (set_dup_node_check(set, elem, LYXP_NODE_ELEM, i)) {
6751 /* the node is a duplicate, we'll process it later in the set */
6752 goto skip_children;
6753 }
Michal Vasko6346ece2019-09-24 13:12:53 +02006754 } else if (rc == LY_EINCOMPLETE) {
Michal Vasko6346ece2019-09-24 13:12:53 +02006755 return rc;
6756 } else if (rc == LY_EINVAL) {
6757 goto skip_children;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006758 }
6759
6760 /* TREE DFS NEXT ELEM */
6761 /* select element for the next run - children first */
Radek Krejcia1c1e542020-09-29 16:06:52 +02006762 next = lyd_child(elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006763 if (!next) {
6764skip_children:
6765 /* no children, so try siblings, but only if it's not the start,
6766 * that is considered to be the root and it's siblings are not traversed */
6767 if (elem != start) {
6768 next = elem->next;
6769 } else {
6770 break;
6771 }
6772 }
6773 while (!next) {
6774 /* no siblings, go back through the parents */
Michal Vasko9e685082021-01-29 14:49:09 +01006775 if (lyd_parent(elem) == start) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006776 /* we are done, no next element to process */
6777 break;
6778 }
6779 /* parent is already processed, go to its sibling */
Michal Vasko9e685082021-01-29 14:49:09 +01006780 elem = lyd_parent(elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006781 next = elem->next;
6782 }
6783 }
6784 }
6785
6786 /* make the temporary set the current one */
6787 ret_set.ctx_pos = set->ctx_pos;
6788 ret_set.ctx_size = set->ctx_size;
Michal Vaskod3678892020-05-21 10:06:58 +02006789 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006790 memcpy(set, &ret_set, sizeof *set);
Michal Vasko306e2832022-07-25 09:15:17 +02006791 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006792
6793 return LY_SUCCESS;
6794}
6795
6796/**
Michal Vasko49fec8e2022-05-24 10:28:33 +02006797 * @brief Move context @p set to a child schema node and all its descendants starting from a node.
6798 * Result is LYXP_SET_NODE_SET.
6799 *
6800 * @param[in] set Set to use.
6801 * @param[in] start Start node whose subtree to add.
6802 * @param[in] start_idx Index of @p start in @p set.
6803 * @param[in] moveto_mod Matching node module, NULL for no prefix.
6804 * @param[in] ncname Matching node name in the dictionary, NULL for any.
6805 * @param[in] options XPath options.
6806 * @return LY_ERR value.
6807 */
6808static LY_ERR
6809moveto_scnode_dfs(struct lyxp_set *set, const struct lysc_node *start, uint32_t start_idx,
6810 const struct lys_module *moveto_mod, const char *ncname, uint32_t options)
6811{
6812 const struct lysc_node *next, *elem;
6813 uint32_t idx;
6814 LY_ERR rc;
6815
6816 /* TREE DFS */
6817 for (elem = next = start; elem; elem = next) {
6818 if ((elem == start) || (elem->nodetype & (LYS_CHOICE | LYS_CASE))) {
6819 /* schema-only nodes, skip root */
6820 goto next_iter;
6821 }
6822
6823 rc = moveto_scnode_check(elem, start, set, ncname, moveto_mod);
6824 if (!rc) {
6825 if (lyxp_set_scnode_contains(set, elem, LYXP_NODE_ELEM, start_idx, &idx)) {
6826 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
6827 if (idx > start_idx) {
6828 /* we will process it later in the set */
6829 goto skip_children;
6830 }
6831 } else {
6832 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, elem, LYXP_NODE_ELEM, NULL));
6833 }
6834 } else if (rc == LY_EINVAL) {
6835 goto skip_children;
6836 }
6837
6838next_iter:
6839 /* TREE DFS NEXT ELEM */
6840 /* select element for the next run - children first */
6841 next = lysc_node_child(elem);
6842 if (next && (next->nodetype == LYS_INPUT) && (options & LYXP_SCNODE_OUTPUT)) {
6843 next = next->next;
6844 } else if (next && (next->nodetype == LYS_OUTPUT) && !(options & LYXP_SCNODE_OUTPUT)) {
6845 next = next->next;
6846 }
6847 if (!next) {
6848skip_children:
6849 /* no children, so try siblings, but only if it's not the start,
6850 * that is considered to be the root and it's siblings are not traversed */
6851 if (elem != start) {
6852 next = elem->next;
6853 } else {
6854 break;
6855 }
6856 }
6857 while (!next) {
6858 /* no siblings, go back through the parents */
6859 if (elem->parent == start) {
6860 /* we are done, no next element to process */
6861 break;
6862 }
6863 /* parent is already processed, go to its sibling */
6864 elem = elem->parent;
6865 next = elem->next;
6866 }
6867 }
6868
6869 return LY_SUCCESS;
6870}
6871
6872/**
6873 * @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 +02006874 *
6875 * @param[in] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006876 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02006877 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006878 * @param[in] options XPath options.
Michal Vasko49fec8e2022-05-24 10:28:33 +02006879 * @return LY_ERR value.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006880 */
6881static LY_ERR
Michal Vasko49fec8e2022-05-24 10:28:33 +02006882moveto_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 +02006883{
Michal Vasko49fec8e2022-05-24 10:28:33 +02006884 uint32_t i, orig_used, mod_idx;
6885 const struct lys_module *mod;
6886 const struct lysc_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006887
aPiecek8b0cc152021-05-31 16:40:31 +02006888 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006889 return LY_SUCCESS;
6890 }
6891
6892 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006893 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006894 return LY_EVALID;
6895 }
6896
Michal Vasko03ff5a72019-09-11 13:49:33 +02006897 orig_used = set->used;
6898 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006899 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
6900 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006901 continue;
6902 }
6903
6904 /* remember context node */
Radek Krejcif13b87b2020-12-01 22:02:17 +01006905 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vaskoec4df482019-12-16 10:02:18 +01006906 } else {
Michal Vasko1a09b212021-05-06 13:00:10 +02006907 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006908 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006909
Michal Vasko49fec8e2022-05-24 10:28:33 +02006910 if ((set->val.scnodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.scnodes[i].type == LYXP_NODE_ROOT)) {
6911 /* traverse all top-level nodes in all the modules */
6912 mod_idx = 0;
6913 while ((mod = ly_ctx_get_module_iter(set->ctx, &mod_idx))) {
6914 /* module may not be implemented or not compiled yet */
6915 if (!mod->compiled) {
6916 continue;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006917 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006918
Michal Vasko49fec8e2022-05-24 10:28:33 +02006919 root = NULL;
6920 /* no getnext opts needed */
6921 while ((root = lys_getnext(root, NULL, mod->compiled, 0))) {
6922 LY_CHECK_RET(moveto_scnode_dfs(set, root, i, moveto_mod, ncname, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006923 }
6924 }
Michal Vasko49fec8e2022-05-24 10:28:33 +02006925
6926 } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
6927 /* add all the descendants recursively */
6928 LY_CHECK_RET(moveto_scnode_dfs(set, set->val.scnodes[i].scnode, i, moveto_mod, ncname, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006929 }
6930 }
6931
6932 return LY_SUCCESS;
6933}
6934
6935/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02006936 * @brief Move context @p set to an attribute. Result is LYXP_SET_NODE_SET.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006937 * Indirectly context position aware.
6938 *
6939 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02006940 * @param[in] mod Matching metadata module, NULL for any.
6941 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
aPiecek8b0cc152021-05-31 16:40:31 +02006942 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006943 * @return LY_ERR
6944 */
6945static LY_ERR
aPiecek8b0cc152021-05-31 16:40:31 +02006946moveto_attr(struct lyxp_set *set, const struct lys_module *mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006947{
Michal Vasko9f96a052020-03-10 09:41:45 +01006948 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006949
aPiecek8b0cc152021-05-31 16:40:31 +02006950 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006951 return LY_SUCCESS;
6952 }
6953
6954 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006955 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006956 return LY_EVALID;
6957 }
6958
Radek Krejci1deb5be2020-08-26 16:43:36 +02006959 for (uint32_t i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02006960 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006961
6962 /* only attributes of an elem (not dummy) can be in the result, skip all the rest;
6963 * our attributes are always qualified */
Michal Vasko5c4e5892019-11-14 12:31:38 +01006964 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01006965 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006966
6967 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02006968 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006969 continue;
6970 }
6971
Michal Vaskod3678892020-05-21 10:06:58 +02006972 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006973 /* match */
6974 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01006975 set->val.meta[i].meta = sub;
6976 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006977 /* pos does not change */
6978 replaced = 1;
6979 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01006980 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 +02006981 }
6982 ++i;
6983 }
6984 }
6985 }
6986
6987 if (!replaced) {
6988 /* no match */
6989 set_remove_node(set, i);
6990 }
6991 }
6992
6993 return LY_SUCCESS;
6994}
6995
6996/**
6997 * @brief Move context @p set1 to union with @p set2. @p set2 is emptied afterwards.
Michal Vasko61ac2f62020-05-25 12:39:51 +02006998 * Result is LYXP_SET_NODE_SET. Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006999 *
7000 * @param[in,out] set1 Set to use for the result.
7001 * @param[in] set2 Set that is copied to @p set1.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007002 * @return LY_ERR
7003 */
7004static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007005moveto_union(struct lyxp_set *set1, struct lyxp_set *set2)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007006{
7007 LY_ERR rc;
7008
Michal Vaskod3678892020-05-21 10:06:58 +02007009 if ((set1->type != LYXP_SET_NODE_SET) || (set2->type != LYXP_SET_NODE_SET)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01007010 LOGVAL(set1->ctx, LY_VCODE_XP_INOP_2, "union", print_set_type(set1), print_set_type(set2));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007011 return LY_EVALID;
7012 }
7013
7014 /* set2 is empty or both set1 and set2 */
Michal Vaskod3678892020-05-21 10:06:58 +02007015 if (!set2->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007016 return LY_SUCCESS;
7017 }
7018
Michal Vaskod3678892020-05-21 10:06:58 +02007019 if (!set1->used) {
aPiecekadc1e4f2021-10-07 11:15:12 +02007020 /* release hidden allocated data (lyxp_set.size) */
7021 lyxp_set_free_content(set1);
7022 /* direct copying of the entire structure */
Michal Vasko03ff5a72019-09-11 13:49:33 +02007023 memcpy(set1, set2, sizeof *set1);
7024 /* dynamic memory belongs to set1 now, do not free */
Michal Vaskod3678892020-05-21 10:06:58 +02007025 memset(set2, 0, sizeof *set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007026 return LY_SUCCESS;
7027 }
7028
7029 /* we assume sets are sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007030 assert(!set_sort(set1) && !set_sort(set2));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007031
7032 /* sort, remove duplicates */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007033 rc = set_sorted_merge(set1, set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007034 LY_CHECK_RET(rc);
7035
7036 /* final set must be sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007037 assert(!set_sort(set1));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007038
7039 return LY_SUCCESS;
7040}
7041
7042/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02007043 * @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 +02007044 * Context position aware.
7045 *
7046 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02007047 * @param[in] mod Matching metadata module, NULL for any.
7048 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
Michal Vaskocdad7122020-11-09 21:04:44 +01007049 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007050 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7051 */
7052static int
Michal Vaskocdad7122020-11-09 21:04:44 +01007053moveto_attr_alldesc(struct lyxp_set *set, const struct lys_module *mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007054{
Michal Vasko9f96a052020-03-10 09:41:45 +01007055 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007056 struct lyxp_set *set_all_desc = NULL;
7057 LY_ERR rc;
7058
aPiecek8b0cc152021-05-31 16:40:31 +02007059 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007060 return LY_SUCCESS;
7061 }
7062
7063 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01007064 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007065 return LY_EVALID;
7066 }
7067
Michal Vasko03ff5a72019-09-11 13:49:33 +02007068 /* can be optimized similarly to moveto_node_alldesc() and save considerable amount of memory,
7069 * but it likely won't be used much, so it's a waste of time */
7070 /* copy the context */
7071 set_all_desc = set_copy(set);
7072 /* get all descendant nodes (the original context nodes are removed) */
Michal Vasko49fec8e2022-05-24 10:28:33 +02007073 rc = moveto_node_alldesc_child(set_all_desc, NULL, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007074 if (rc != LY_SUCCESS) {
7075 lyxp_set_free(set_all_desc);
7076 return rc;
7077 }
7078 /* prepend the original context nodes */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007079 rc = moveto_union(set, set_all_desc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007080 if (rc != LY_SUCCESS) {
7081 lyxp_set_free(set_all_desc);
7082 return rc;
7083 }
7084 lyxp_set_free(set_all_desc);
7085
Radek Krejci1deb5be2020-08-26 16:43:36 +02007086 for (uint32_t i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02007087 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007088
7089 /* only attributes of an elem can be in the result, skip all the rest,
7090 * we have all attributes qualified in lyd tree */
7091 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01007092 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007093 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02007094 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007095 continue;
7096 }
7097
Michal Vaskod3678892020-05-21 10:06:58 +02007098 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007099 /* match */
7100 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01007101 set->val.meta[i].meta = sub;
7102 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007103 /* pos does not change */
7104 replaced = 1;
7105 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01007106 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 +02007107 }
7108 ++i;
7109 }
7110 }
7111 }
7112
7113 if (!replaced) {
7114 /* no match */
7115 set_remove_node(set, i);
7116 }
7117 }
7118
7119 return LY_SUCCESS;
7120}
7121
7122/**
Michal Vasko8abcecc2022-07-28 09:55:01 +02007123 * @brief Move context @p set1 single item to the result of a comparison.
7124 *
7125 * @param[in] set1 First set with the item to compare.
7126 * @param[in] idx1 Index of the item in @p set1.
7127 * @param[in] set2 Second set.
7128 * @param[in] op Comparison operator to process.
7129 * @param[in] switch_operands Whether to switch sets as operands; whether it is `set1 op set2` or `set2 op set1`.
7130 * @param[out] result Result of the comparison.
7131 * @return LY_ERR value.
7132 */
7133static LY_ERR
7134moveto_op_comp_item(const struct lyxp_set *set1, uint32_t idx1, struct lyxp_set *set2, const char *op,
7135 ly_bool switch_operands, ly_bool *result)
7136{
7137 struct lyxp_set tmp1 = {0};
7138 LY_ERR rc = LY_SUCCESS;
7139
7140 assert(set1->type == LYXP_SET_NODE_SET);
7141
7142 /* cast set1 */
7143 switch (set2->type) {
7144 case LYXP_SET_NUMBER:
7145 rc = set_comp_cast(&tmp1, set1, LYXP_SET_NUMBER, idx1);
7146 break;
7147 case LYXP_SET_BOOLEAN:
7148 rc = set_comp_cast(&tmp1, set1, LYXP_SET_BOOLEAN, idx1);
7149 break;
7150 default:
7151 rc = set_comp_cast(&tmp1, set1, LYXP_SET_STRING, idx1);
7152 break;
7153 }
7154 LY_CHECK_GOTO(rc, cleanup);
7155
7156 /* canonize set2 */
7157 LY_CHECK_GOTO(rc = set_comp_canonize(set2, &set1->val.nodes[idx1]), cleanup);
7158
7159 /* compare recursively and store the result */
7160 if (switch_operands) {
7161 LY_CHECK_GOTO(rc = moveto_op_comp(set2, &tmp1, op), cleanup);
7162 *result = set2->val.bln;
7163 } else {
7164 LY_CHECK_GOTO(rc = moveto_op_comp(&tmp1, set2, op), cleanup);
7165 *result = tmp1.val.bln;
7166 }
7167
7168cleanup:
7169 lyxp_set_free_content(&tmp1);
7170 return rc;
7171}
7172
7173/**
7174 * @brief Move context @p set1 to the result of a comparison. Handles '=', '!=', '<=', '<', '>=', or '>'.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007175 * Result is LYXP_SET_BOOLEAN. Indirectly context position aware.
7176 *
7177 * @param[in,out] set1 Set to use for the result.
7178 * @param[in] set2 Set acting as the second operand for @p op.
7179 * @param[in] op Comparison operator to process.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007180 * @return LY_ERR
7181 */
7182static LY_ERR
Michal Vasko8abcecc2022-07-28 09:55:01 +02007183moveto_op_comp(struct lyxp_set *set1, struct lyxp_set *set2, const char *op)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007184{
7185 /*
7186 * NODE SET + NODE SET = NODE SET + STRING /(1 NODE SET) 2 STRING
7187 * NODE SET + STRING = STRING + STRING /1 STRING (2 STRING)
7188 * NODE SET + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
7189 * NODE SET + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
7190 * STRING + NODE SET = STRING + STRING /(1 STRING) 2 STRING
7191 * NUMBER + NODE SET = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
7192 * BOOLEAN + NODE SET = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
7193 *
7194 * '=' or '!='
7195 * BOOLEAN + BOOLEAN
7196 * BOOLEAN + STRING = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
7197 * BOOLEAN + NUMBER = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
7198 * STRING + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
7199 * NUMBER + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
7200 * NUMBER + NUMBER
7201 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
7202 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
7203 * STRING + STRING
7204 *
7205 * '<=', '<', '>=', '>'
7206 * NUMBER + NUMBER
7207 * BOOLEAN + BOOLEAN = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
7208 * BOOLEAN + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
7209 * BOOLEAN + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
7210 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
7211 * STRING + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
7212 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
7213 * NUMBER + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
7214 * STRING + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
7215 */
Michal Vasko8abcecc2022-07-28 09:55:01 +02007216 ly_bool result;
7217 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007218 LY_ERR rc;
7219
Michal Vasko03ff5a72019-09-11 13:49:33 +02007220 /* iterative evaluation with node-sets */
7221 if ((set1->type == LYXP_SET_NODE_SET) || (set2->type == LYXP_SET_NODE_SET)) {
7222 if (set1->type == LYXP_SET_NODE_SET) {
7223 for (i = 0; i < set1->used; ++i) {
Michal Vasko8abcecc2022-07-28 09:55:01 +02007224 /* evaluate for the single item */
7225 LY_CHECK_RET(moveto_op_comp_item(set1, i, set2, op, 0, &result));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007226
7227 /* lazy evaluation until true */
Michal Vasko8abcecc2022-07-28 09:55:01 +02007228 if (result) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007229 set_fill_boolean(set1, 1);
7230 return LY_SUCCESS;
7231 }
7232 }
7233 } else {
7234 for (i = 0; i < set2->used; ++i) {
Michal Vasko8abcecc2022-07-28 09:55:01 +02007235 /* evaluate for the single item */
7236 LY_CHECK_RET(moveto_op_comp_item(set2, i, set1, op, 1, &result));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007237
7238 /* lazy evaluation until true */
Michal Vasko8abcecc2022-07-28 09:55:01 +02007239 if (result) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007240 set_fill_boolean(set1, 1);
7241 return LY_SUCCESS;
7242 }
7243 }
7244 }
7245
Michal Vasko8abcecc2022-07-28 09:55:01 +02007246 /* false for all the nodes */
Michal Vasko03ff5a72019-09-11 13:49:33 +02007247 set_fill_boolean(set1, 0);
7248 return LY_SUCCESS;
7249 }
7250
7251 /* first convert properly */
7252 if ((op[0] == '=') || (op[0] == '!')) {
7253 if ((set1->type == LYXP_SET_BOOLEAN) || (set2->type == LYXP_SET_BOOLEAN)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007254 lyxp_set_cast(set1, LYXP_SET_BOOLEAN);
7255 lyxp_set_cast(set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007256 } else if ((set1->type == LYXP_SET_NUMBER) || (set2->type == LYXP_SET_NUMBER)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007257 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007258 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007259 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007260 LY_CHECK_RET(rc);
7261 } /* else we have 2 strings */
7262 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007263 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007264 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007265 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007266 LY_CHECK_RET(rc);
7267 }
7268
7269 assert(set1->type == set2->type);
7270
7271 /* compute result */
7272 if (op[0] == '=') {
7273 if (set1->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +02007274 result = (set1->val.bln == set2->val.bln);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007275 } else if (set1->type == LYXP_SET_NUMBER) {
7276 result = (set1->val.num == set2->val.num);
7277 } else {
7278 assert(set1->type == LYXP_SET_STRING);
Michal Vaskoac6c72f2019-11-14 16:09:34 +01007279 result = !strcmp(set1->val.str, set2->val.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007280 }
7281 } else if (op[0] == '!') {
7282 if (set1->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +02007283 result = (set1->val.bln != set2->val.bln);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007284 } else if (set1->type == LYXP_SET_NUMBER) {
7285 result = (set1->val.num != set2->val.num);
7286 } else {
7287 assert(set1->type == LYXP_SET_STRING);
Michal Vaskoc2058432020-11-06 17:26:21 +01007288 result = strcmp(set1->val.str, set2->val.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007289 }
7290 } else {
7291 assert(set1->type == LYXP_SET_NUMBER);
7292 if (op[0] == '<') {
7293 if (op[1] == '=') {
7294 result = (set1->val.num <= set2->val.num);
7295 } else {
7296 result = (set1->val.num < set2->val.num);
7297 }
7298 } else {
7299 if (op[1] == '=') {
7300 result = (set1->val.num >= set2->val.num);
7301 } else {
7302 result = (set1->val.num > set2->val.num);
7303 }
7304 }
7305 }
7306
7307 /* assign result */
7308 if (result) {
7309 set_fill_boolean(set1, 1);
7310 } else {
7311 set_fill_boolean(set1, 0);
7312 }
7313
7314 return LY_SUCCESS;
7315}
7316
7317/**
7318 * @brief Move context @p set to the result of a basic operation. Handles '+', '-', unary '-', '*', 'div',
7319 * or 'mod'. Result is LYXP_SET_NUMBER. Indirectly context position aware.
7320 *
7321 * @param[in,out] set1 Set to use for the result.
7322 * @param[in] set2 Set acting as the second operand for @p op.
7323 * @param[in] op Operator to process.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007324 * @return LY_ERR
7325 */
7326static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007327moveto_op_math(struct lyxp_set *set1, struct lyxp_set *set2, const char *op)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007328{
7329 LY_ERR rc;
7330
7331 /* unary '-' */
7332 if (!set2 && (op[0] == '-')) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007333 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007334 LY_CHECK_RET(rc);
7335 set1->val.num *= -1;
7336 lyxp_set_free(set2);
7337 return LY_SUCCESS;
7338 }
7339
7340 assert(set1 && set2);
7341
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007342 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007343 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007344 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007345 LY_CHECK_RET(rc);
7346
7347 switch (op[0]) {
7348 /* '+' */
7349 case '+':
7350 set1->val.num += set2->val.num;
7351 break;
7352
7353 /* '-' */
7354 case '-':
7355 set1->val.num -= set2->val.num;
7356 break;
7357
7358 /* '*' */
7359 case '*':
7360 set1->val.num *= set2->val.num;
7361 break;
7362
7363 /* 'div' */
7364 case 'd':
7365 set1->val.num /= set2->val.num;
7366 break;
7367
7368 /* 'mod' */
7369 case 'm':
7370 set1->val.num = ((long long)set1->val.num) % ((long long)set2->val.num);
7371 break;
7372
7373 default:
7374 LOGINT_RET(set1->ctx);
7375 }
7376
7377 return LY_SUCCESS;
7378}
7379
Michal Vasko03ff5a72019-09-11 13:49:33 +02007380/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02007381 * @brief Evaluate Predicate. Logs directly on error.
7382 *
Michal Vaskod3678892020-05-21 10:06:58 +02007383 * [9] Predicate ::= '[' Expr ']'
Michal Vasko03ff5a72019-09-11 13:49:33 +02007384 *
7385 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007386 * @param[in] tok_idx Position in the expression @p exp.
aPiecek8b0cc152021-05-31 16:40:31 +02007387 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007388 * @param[in] options XPath options.
Michal Vasko49fec8e2022-05-24 10:28:33 +02007389 * @param[in] axis Axis to search on.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007390 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7391 */
7392static LY_ERR
Michal Vasko49fec8e2022-05-24 10:28:33 +02007393eval_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 +02007394{
7395 LY_ERR rc;
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01007396 uint16_t orig_exp;
7397 uint32_t i, orig_pos, orig_size;
Michal Vasko5c4e5892019-11-14 12:31:38 +01007398 int32_t pred_in_ctx;
Michal Vasko88a9e802022-05-24 10:50:28 +02007399 ly_bool reverse_axis = 0;
aPiecekfff4dca2021-10-07 10:59:53 +02007400 struct lyxp_set set2 = {0};
Michal Vasko03ff5a72019-09-11 13:49:33 +02007401
7402 /* '[' */
aPiecek8b0cc152021-05-31 16:40:31 +02007403 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02007404 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007405 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007406
aPiecek8b0cc152021-05-31 16:40:31 +02007407 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007408only_parse:
aPiecek8b0cc152021-05-31 16:40:31 +02007409 rc = eval_expr_select(exp, tok_idx, 0, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007410 LY_CHECK_RET(rc);
7411 } else if (set->type == LYXP_SET_NODE_SET) {
7412 /* 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 +01007413 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007414
7415 /* empty set, nothing to evaluate */
7416 if (!set->used) {
7417 goto only_parse;
7418 }
7419
Michal Vasko49fec8e2022-05-24 10:28:33 +02007420 /* decide forward or reverse axis */
7421 switch (axis) {
7422 case LYXP_AXIS_ANCESTOR:
7423 case LYXP_AXIS_ANCESTOR_OR_SELF:
7424 case LYXP_AXIS_PRECEDING:
7425 case LYXP_AXIS_PRECEDING_SIBLING:
7426 reverse_axis = 1;
7427 break;
7428 case LYXP_AXIS_DESCENDANT:
7429 case LYXP_AXIS_DESCENDANT_OR_SELF:
7430 case LYXP_AXIS_FOLLOWING:
7431 case LYXP_AXIS_FOLLOWING_SIBLING:
7432 case LYXP_AXIS_PARENT:
7433 case LYXP_AXIS_CHILD:
7434 case LYXP_AXIS_SELF:
7435 case LYXP_AXIS_ATTRIBUTE:
7436 reverse_axis = 0;
7437 break;
7438 }
7439
Michal Vasko004d3152020-06-11 19:59:22 +02007440 orig_exp = *tok_idx;
Michal Vasko49fec8e2022-05-24 10:28:33 +02007441 orig_pos = reverse_axis ? set->used + 1 : 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007442 orig_size = set->used;
Michal Vasko39dbf352020-05-21 10:08:59 +02007443 for (i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007444 set_init(&set2, set);
7445 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 +02007446
7447 /* remember the node context position for position() and context size for last() */
7448 orig_pos += reverse_axis ? -1 : 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007449
7450 set2.ctx_pos = orig_pos;
7451 set2.ctx_size = orig_size;
Michal Vasko004d3152020-06-11 19:59:22 +02007452 *tok_idx = orig_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007453
Michal Vasko004d3152020-06-11 19:59:22 +02007454 rc = eval_expr_select(exp, tok_idx, 0, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007455 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02007456 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007457 return rc;
7458 }
7459
Michal Vasko49fec8e2022-05-24 10:28:33 +02007460 /* number is a proximity position */
Michal Vasko03ff5a72019-09-11 13:49:33 +02007461 if (set2.type == LYXP_SET_NUMBER) {
7462 if ((long long)set2.val.num == orig_pos) {
7463 set2.val.num = 1;
7464 } else {
7465 set2.val.num = 0;
7466 }
7467 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007468 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007469
7470 /* predicate satisfied or not? */
Michal Vasko004d3152020-06-11 19:59:22 +02007471 if (!set2.val.bln) {
Michal Vasko2caefc12019-11-14 16:07:56 +01007472 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007473 }
7474 }
Michal Vasko2caefc12019-11-14 16:07:56 +01007475 set_remove_nodes_none(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007476
7477 } else if (set->type == LYXP_SET_SCNODE_SET) {
7478 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01007479 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007480 /* there is a currently-valid node */
7481 break;
7482 }
7483 }
7484 /* empty set, nothing to evaluate */
7485 if (i == set->used) {
7486 goto only_parse;
7487 }
7488
Michal Vasko004d3152020-06-11 19:59:22 +02007489 orig_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007490
Michal Vasko03ff5a72019-09-11 13:49:33 +02007491 /* set special in_ctx to all the valid snodes */
7492 pred_in_ctx = set_scnode_new_in_ctx(set);
7493
7494 /* use the valid snodes one-by-one */
7495 for (i = 0; i < set->used; ++i) {
7496 if (set->val.scnodes[i].in_ctx != pred_in_ctx) {
7497 continue;
7498 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01007499 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007500
Michal Vasko004d3152020-06-11 19:59:22 +02007501 *tok_idx = orig_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007502
Michal Vasko004d3152020-06-11 19:59:22 +02007503 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007504 LY_CHECK_RET(rc);
7505
7506 set->val.scnodes[i].in_ctx = pred_in_ctx;
7507 }
7508
7509 /* restore the state as it was before the predicate */
7510 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01007511 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007512 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007513 } else if (set->val.scnodes[i].in_ctx == pred_in_ctx) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01007514 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007515 }
7516 }
7517
7518 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02007519 set2.type = LYXP_SET_NODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007520 set_fill_set(&set2, set);
7521
Michal Vasko004d3152020-06-11 19:59:22 +02007522 rc = eval_expr_select(exp, tok_idx, 0, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007523 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02007524 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007525 return rc;
7526 }
7527
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007528 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +02007529 if (!set2.val.bln) {
Michal Vaskod3678892020-05-21 10:06:58 +02007530 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007531 }
Michal Vaskod3678892020-05-21 10:06:58 +02007532 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007533 }
7534
7535 /* ']' */
Michal Vasko004d3152020-06-11 19:59:22 +02007536 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_BRACK2);
aPiecek8b0cc152021-05-31 16:40:31 +02007537 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02007538 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007539 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007540
7541 return LY_SUCCESS;
7542}
7543
7544/**
Michal Vaskod3678892020-05-21 10:06:58 +02007545 * @brief Evaluate Literal. Logs directly on error.
7546 *
7547 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007548 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02007549 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7550 */
7551static void
Michal Vasko40308e72020-10-20 16:38:40 +02007552eval_literal(const struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set)
Michal Vaskod3678892020-05-21 10:06:58 +02007553{
7554 if (set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007555 if (exp->tok_len[*tok_idx] == 2) {
Michal Vaskod3678892020-05-21 10:06:58 +02007556 set_fill_string(set, "", 0);
7557 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007558 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 +02007559 }
7560 }
7561 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02007562 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007563 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007564}
7565
7566/**
Michal Vasko004d3152020-06-11 19:59:22 +02007567 * @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 +02007568 *
Michal Vasko004d3152020-06-11 19:59:22 +02007569 * @param[in] exp Full parsed XPath expression.
7570 * @param[in,out] tok_idx Index in @p exp at the beginning of the predicate, is updated on success.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007571 * @param[in] ctx_node Found schema node as the context for the predicate.
7572 * @param[in] cur_mod Current module for the expression.
7573 * @param[in] cur_node Current (original context) node.
Michal Vasko004d3152020-06-11 19:59:22 +02007574 * @param[in] format Format of any prefixes in key names/values.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007575 * @param[in] prefix_data Format-specific prefix data (see ::ly_resolve_prefix).
Michal Vasko004d3152020-06-11 19:59:22 +02007576 * @param[out] predicates Parsed predicates.
7577 * @param[out] pred_type Type of @p predicates.
7578 * @return LY_SUCCESS on success,
7579 * @return LY_ERR on any error.
Michal Vaskod3678892020-05-21 10:06:58 +02007580 */
7581static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007582eval_name_test_try_compile_predicates(const struct lyxp_expr *exp, uint16_t *tok_idx, const struct lysc_node *ctx_node,
Radek Krejci8df109d2021-04-23 12:19:08 +02007583 const struct lys_module *cur_mod, const struct lysc_node *cur_node, LY_VALUE_FORMAT format, void *prefix_data,
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007584 struct ly_path_predicate **predicates, enum ly_path_pred_type *pred_type)
Michal Vaskod3678892020-05-21 10:06:58 +02007585{
Michal Vasko004d3152020-06-11 19:59:22 +02007586 LY_ERR ret = LY_SUCCESS;
7587 uint16_t key_count, e_idx, pred_idx = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02007588 const struct lysc_node *key;
Michal Vasko004d3152020-06-11 19:59:22 +02007589 size_t pred_len;
Radek Krejci1deb5be2020-08-26 16:43:36 +02007590 uint32_t prev_lo;
Michal Vasko004d3152020-06-11 19:59:22 +02007591 struct lyxp_expr *exp2 = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02007592
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007593 assert(ctx_node->nodetype & (LYS_LIST | LYS_LEAFLIST));
Michal Vaskod3678892020-05-21 10:06:58 +02007594
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007595 if (ctx_node->nodetype == LYS_LIST) {
Michal Vasko004d3152020-06-11 19:59:22 +02007596 /* get key count */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007597 if (ctx_node->flags & LYS_KEYLESS) {
Michal Vasko004d3152020-06-11 19:59:22 +02007598 return LY_EINVAL;
7599 }
Michal Vasko544e58a2021-01-28 14:33:41 +01007600 for (key_count = 0, key = lysc_node_child(ctx_node); key && (key->flags & LYS_KEY); key = key->next, ++key_count) {}
Michal Vasko004d3152020-06-11 19:59:22 +02007601 assert(key_count);
Michal Vaskod3678892020-05-21 10:06:58 +02007602
Michal Vasko004d3152020-06-11 19:59:22 +02007603 /* learn where the predicates end */
7604 e_idx = *tok_idx;
7605 while (key_count) {
7606 /* '[' */
7607 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
7608 return LY_EINVAL;
7609 }
7610 ++e_idx;
7611
Michal Vasko3354d272021-04-06 09:40:06 +02007612 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_NAMETEST)) {
7613 /* definitely not a key */
7614 return LY_EINVAL;
7615 }
7616
Michal Vasko004d3152020-06-11 19:59:22 +02007617 /* ']' */
7618 while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) {
7619 ++e_idx;
7620 }
7621 ++e_idx;
7622
7623 /* another presumably key predicate parsed */
7624 --key_count;
7625 }
Michal Vasko004d3152020-06-11 19:59:22 +02007626 } else {
7627 /* learn just where this single predicate ends */
7628 e_idx = *tok_idx;
7629
Michal Vaskod3678892020-05-21 10:06:58 +02007630 /* '[' */
Michal Vasko004d3152020-06-11 19:59:22 +02007631 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
7632 return LY_EINVAL;
7633 }
7634 ++e_idx;
Michal Vaskod3678892020-05-21 10:06:58 +02007635
Michal Vasko3354d272021-04-06 09:40:06 +02007636 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_DOT)) {
7637 /* definitely not the value */
7638 return LY_EINVAL;
7639 }
7640
Michal Vaskod3678892020-05-21 10:06:58 +02007641 /* ']' */
Michal Vasko004d3152020-06-11 19:59:22 +02007642 while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) {
7643 ++e_idx;
7644 }
7645 ++e_idx;
Michal Vaskod3678892020-05-21 10:06:58 +02007646 }
7647
Michal Vasko004d3152020-06-11 19:59:22 +02007648 /* get the joined length of all the keys predicates/of the single leaf-list predicate */
7649 pred_len = (exp->tok_pos[e_idx - 1] + exp->tok_len[e_idx - 1]) - exp->tok_pos[*tok_idx];
7650
7651 /* turn logging off */
7652 prev_lo = ly_log_options(0);
7653
7654 /* parse the predicate(s) */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007655 LY_CHECK_GOTO(ret = ly_path_parse_predicate(ctx_node->module->ctx, ctx_node, exp->expr + exp->tok_pos[*tok_idx],
7656 pred_len, LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &exp2), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02007657
7658 /* compile */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007659 ret = ly_path_compile_predicate(ctx_node->module->ctx, cur_node, cur_mod, ctx_node, exp2, &pred_idx, format,
7660 prefix_data, predicates, pred_type);
Michal Vasko004d3152020-06-11 19:59:22 +02007661 LY_CHECK_GOTO(ret, cleanup);
7662
7663 /* success, the predicate must include all the needed information for hash-based search */
7664 *tok_idx = e_idx;
7665
7666cleanup:
7667 ly_log_options(prev_lo);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007668 lyxp_expr_free(ctx_node->module->ctx, exp2);
Michal Vasko004d3152020-06-11 19:59:22 +02007669 return ret;
Michal Vaskod3678892020-05-21 10:06:58 +02007670}
7671
7672/**
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007673 * @brief Search for/check the next schema node that could be the only matching schema node meaning the
7674 * data node(s) could be found using a single hash-based search.
7675 *
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007676 * @param[in] ctx libyang context.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007677 * @param[in] node Next context node to check.
7678 * @param[in] name Expected node name.
7679 * @param[in] name_len Length of @p name.
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007680 * @param[in] moveto_mod Expected node module, can be NULL for JSON format with no prefix.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007681 * @param[in] root_type XPath root type.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007682 * @param[in] format Prefix format.
7683 * @param[in,out] found Previously found node, is updated.
7684 * @return LY_SUCCESS on success,
7685 * @return LY_ENOT if the whole check failed and hashes cannot be used.
7686 */
7687static LY_ERR
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007688eval_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 +02007689 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 +02007690 const struct lysc_node **found)
7691{
7692 const struct lysc_node *scnode;
7693 const struct lys_module *mod;
7694 uint32_t idx = 0;
7695
Radek Krejci8df109d2021-04-23 12:19:08 +02007696 assert((format == LY_VALUE_JSON) || moveto_mod);
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007697
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007698continue_search:
Michal Vasko7d1d0912020-10-16 08:38:30 +02007699 scnode = NULL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007700 if (!node) {
Radek Krejci8df109d2021-04-23 12:19:08 +02007701 if ((format == LY_VALUE_JSON) && !moveto_mod) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007702 /* search all modules for a single match */
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007703 while ((mod = ly_ctx_get_module_iter(ctx, &idx))) {
Michal Vasko35a3b1d2021-07-14 09:34:16 +02007704 if (!mod->implemented) {
7705 continue;
7706 }
7707
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007708 scnode = lys_find_child(NULL, mod, name, name_len, 0, 0);
7709 if (scnode) {
7710 /* we have found a match */
7711 break;
7712 }
7713 }
7714
Michal Vasko7d1d0912020-10-16 08:38:30 +02007715 if (!scnode) {
7716 /* all modules searched */
7717 idx = 0;
7718 }
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007719 } else {
7720 /* search in top-level */
7721 scnode = lys_find_child(NULL, moveto_mod, name, name_len, 0, 0);
7722 }
7723 } else if (!*found || (lysc_data_parent(*found) != node->schema)) {
Radek Krejci8df109d2021-04-23 12:19:08 +02007724 if ((format == LY_VALUE_JSON) && !moveto_mod) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007725 /* we must adjust the module to inherit the one from the context node */
7726 moveto_mod = node->schema->module;
7727 }
7728
7729 /* search in children, do not repeat the same search */
7730 scnode = lys_find_child(node->schema, moveto_mod, name, name_len, 0, 0);
Michal Vasko7d1d0912020-10-16 08:38:30 +02007731 } /* else skip redundant search */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007732
7733 /* additional context check */
7734 if (scnode && (root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) {
7735 scnode = NULL;
7736 }
7737
7738 if (scnode) {
7739 if (*found) {
7740 /* we found a schema node with the same name but at different level, give up, too complicated
7741 * (more hash-based searches would be required, not supported) */
7742 return LY_ENOT;
7743 } else {
7744 /* remember the found schema node and continue to make sure it can be used */
7745 *found = scnode;
7746 }
7747 }
7748
7749 if (idx) {
7750 /* continue searching all the following models */
7751 goto continue_search;
7752 }
7753
7754 return LY_SUCCESS;
7755}
7756
7757/**
Michal Vasko4ad69e72021-10-26 16:25:55 +02007758 * @brief Generate message when no matching schema nodes were found for a path segment.
7759 *
7760 * @param[in] set XPath set.
7761 * @param[in] scparent Previous schema parent in the context, if only one.
7762 * @param[in] ncname XPath NCName being evaluated.
7763 * @param[in] ncname_len Length of @p ncname.
7764 * @param[in] expr Whole XPath expression.
7765 * @param[in] options XPath options.
7766 */
7767static void
7768eval_name_test_scnode_no_match_msg(struct lyxp_set *set, const struct lyxp_set_scnode *scparent, const char *ncname,
7769 uint16_t ncname_len, const char *expr, uint32_t options)
7770{
7771 const char *format;
7772 char *path = NULL, *ppath = NULL;
7773
7774 path = lysc_path(set->cur_scnode, LYSC_PATH_LOG, NULL, 0);
7775 if (scparent) {
7776 /* generate path for the parent */
7777 if (scparent->type == LYXP_NODE_ELEM) {
7778 ppath = lysc_path(scparent->scnode, LYSC_PATH_LOG, NULL, 0);
7779 } else if (scparent->type == LYXP_NODE_ROOT) {
7780 ppath = strdup("<root>");
7781 } else if (scparent->type == LYXP_NODE_ROOT_CONFIG) {
7782 ppath = strdup("<config-root>");
7783 }
7784 }
7785 if (ppath) {
7786 format = "Schema node \"%.*s\" for parent \"%s\" not found; in expr \"%.*s\" with context node \"%s\".";
7787 if (options & LYXP_SCNODE_ERROR) {
7788 LOGERR(set->ctx, LY_EVALID, format, ncname_len, ncname, ppath, (ncname - expr) + ncname_len, expr, path);
7789 } else {
7790 LOGWRN(set->ctx, format, ncname_len, ncname, ppath, (ncname - expr) + ncname_len, expr, path);
7791 }
7792 } else {
7793 format = "Schema node \"%.*s\" not found; in expr \"%.*s\" with context node \"%s\".";
7794 if (options & LYXP_SCNODE_ERROR) {
7795 LOGERR(set->ctx, LY_EVALID, format, ncname_len, ncname, (ncname - expr) + ncname_len, expr, path);
7796 } else {
7797 LOGWRN(set->ctx, format, ncname_len, ncname, (ncname - expr) + ncname_len, expr, path);
7798 }
7799 }
7800 free(path);
7801 free(ppath);
7802}
7803
7804/**
Michal Vaskod3678892020-05-21 10:06:58 +02007805 * @brief Evaluate NameTest and any following Predicates. Logs directly on error.
7806 *
7807 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
7808 * [6] NodeTest ::= NameTest | NodeType '(' ')'
7809 * [7] NameTest ::= '*' | NCName ':' '*' | QName
7810 *
7811 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007812 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko49fec8e2022-05-24 10:28:33 +02007813 * @param[in] axis What axis to search on.
Michal Vaskod3678892020-05-21 10:06:58 +02007814 * @param[in] all_desc Whether to search all the descendants or children only.
aPiecek8b0cc152021-05-31 16:40:31 +02007815 * @param[in,out] set Context and result set.
Michal Vaskod3678892020-05-21 10:06:58 +02007816 * @param[in] options XPath options.
Michal Vaskoa036a6b2021-10-12 16:05:23 +02007817 * @return LY_ERR (LY_EINCOMPLETE on unresolved when, LY_ENOT for not found schema node)
Michal Vaskod3678892020-05-21 10:06:58 +02007818 */
7819static LY_ERR
Michal Vasko49fec8e2022-05-24 10:28:33 +02007820eval_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 +02007821 struct lyxp_set *set, uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02007822{
Michal Vaskoa036a6b2021-10-12 16:05:23 +02007823 LY_ERR rc = LY_SUCCESS, r;
Michal Vasko004d3152020-06-11 19:59:22 +02007824 const char *ncname, *ncname_dict = NULL;
Michal Vasko56c008c2022-07-29 14:57:47 +02007825 uint32_t i, ncname_len;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007826 const struct lys_module *moveto_mod = NULL;
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007827 const struct lysc_node *scnode = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02007828 struct ly_path_predicate *predicates = NULL;
7829 enum ly_path_pred_type pred_type = 0;
Michal Vaskoa036a6b2021-10-12 16:05:23 +02007830 int scnode_skip_pred = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02007831
aPiecek8b0cc152021-05-31 16:40:31 +02007832 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02007833 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007834 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007835
aPiecek8b0cc152021-05-31 16:40:31 +02007836 if (options & LYXP_SKIP_EXPR) {
Michal Vaskod3678892020-05-21 10:06:58 +02007837 goto moveto;
7838 }
7839
Michal Vasko004d3152020-06-11 19:59:22 +02007840 ncname = &exp->expr[exp->tok_pos[*tok_idx - 1]];
7841 ncname_len = exp->tok_len[*tok_idx - 1];
Michal Vaskod3678892020-05-21 10:06:58 +02007842
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007843 if ((ncname[0] == '*') && (ncname_len == 1)) {
7844 /* all nodes will match */
7845 goto moveto;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007846 }
7847
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007848 /* parse (and skip) module name */
7849 rc = moveto_resolve_model(&ncname, &ncname_len, set, NULL, &moveto_mod);
Michal Vaskod3678892020-05-21 10:06:58 +02007850 LY_CHECK_GOTO(rc, cleanup);
7851
Michal Vasko49fec8e2022-05-24 10:28:33 +02007852 if ((ncname[0] == '*') && (ncname_len == 1)) {
7853 /* all nodes from the module will match */
7854 goto moveto;
7855 }
7856
7857 if (((set->format == LY_VALUE_JSON) || moveto_mod) && (axis == LYXP_AXIS_CHILD) && !all_desc &&
7858 (set->type == LYXP_SET_NODE_SET)) {
Michal Vaskod3678892020-05-21 10:06:58 +02007859 /* find the matching schema node in some parent in the context */
Michal Vasko56c008c2022-07-29 14:57:47 +02007860 for (i = 0; i < set->used; ++i) {
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007861 if (eval_name_test_with_predicate_get_scnode(set->ctx, set->val.nodes[i].node, ncname, ncname_len,
7862 moveto_mod, set->root_type, set->format, &scnode)) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007863 /* check failed */
7864 scnode = NULL;
7865 break;
Michal Vaskod3678892020-05-21 10:06:58 +02007866 }
7867 }
7868
Michal Vasko004d3152020-06-11 19:59:22 +02007869 if (scnode && (scnode->nodetype & (LYS_LIST | LYS_LEAFLIST))) {
7870 /* try to create the predicates */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007871 if (eval_name_test_try_compile_predicates(exp, tok_idx, scnode, set->cur_mod, set->cur_node ?
7872 set->cur_node->schema : NULL, set->format, set->prefix_data, &predicates, &pred_type)) {
Michal Vasko004d3152020-06-11 19:59:22 +02007873 /* hashes cannot be used */
Michal Vaskod3678892020-05-21 10:06:58 +02007874 scnode = NULL;
7875 }
7876 }
7877 }
7878
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007879 if (!scnode) {
7880 /* we are not able to match based on a schema node and not all the modules match ("*"),
Michal Vasko004d3152020-06-11 19:59:22 +02007881 * use dictionary for efficient comparison */
Radek Krejci011e4aa2020-09-04 15:22:31 +02007882 LY_CHECK_GOTO(rc = lydict_insert(set->ctx, ncname, ncname_len, &ncname_dict), cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02007883 }
7884
7885moveto:
7886 /* move to the attribute(s), data node(s), or schema node(s) */
Michal Vasko49fec8e2022-05-24 10:28:33 +02007887 if (axis == LYXP_AXIS_ATTRIBUTE) {
aPiecek8b0cc152021-05-31 16:40:31 +02007888 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007889 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskod3678892020-05-21 10:06:58 +02007890 } else {
7891 if (all_desc) {
Michal Vaskocdad7122020-11-09 21:04:44 +01007892 rc = moveto_attr_alldesc(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007893 } else {
aPiecek8b0cc152021-05-31 16:40:31 +02007894 rc = moveto_attr(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007895 }
7896 LY_CHECK_GOTO(rc, cleanup);
7897 }
7898 } else {
aPiecek8b0cc152021-05-31 16:40:31 +02007899 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Michal Vaskoa036a6b2021-10-12 16:05:23 +02007900 const struct lyxp_set_scnode *scparent = NULL;
Michal Vasko56c008c2022-07-29 14:57:47 +02007901 ly_bool found = 0;
Michal Vaskoa036a6b2021-10-12 16:05:23 +02007902
7903 /* remember parent if there is only one, to print in the warning */
7904 for (i = 0; i < set->used; ++i) {
7905 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
7906 if (!scparent) {
7907 /* remember the context node */
7908 scparent = &set->val.scnodes[i];
7909 } else {
7910 /* several context nodes, no reasonable error possible */
7911 scparent = NULL;
7912 break;
7913 }
7914 }
7915 }
Radek Krejci1deb5be2020-08-26 16:43:36 +02007916
Michal Vasko49fec8e2022-05-24 10:28:33 +02007917 if (all_desc && (axis == LYXP_AXIS_CHILD)) {
7918 /* efficient evaluation that does not add all the descendants into the set */
7919 rc = moveto_scnode_alldesc_child(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007920 } else {
Michal Vasko49fec8e2022-05-24 10:28:33 +02007921 if (all_desc) {
7922 /* "//" == "/descendant-or-self::node()/" */
7923 rc = xpath_pi_node(set, LYXP_AXIS_DESCENDANT_OR_SELF, options);
7924 LY_CHECK_GOTO(rc, cleanup);
7925 }
7926 rc = moveto_scnode(set, moveto_mod, ncname_dict, axis, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007927 }
7928 LY_CHECK_GOTO(rc, cleanup);
7929
Michal Vasko56c008c2022-07-29 14:57:47 +02007930 i = set->used;
7931 do {
7932 --i;
Michal Vasko1a09b212021-05-06 13:00:10 +02007933 if (set->val.scnodes[i].in_ctx > LYXP_SET_SCNODE_ATOM_NODE) {
Michal Vasko56c008c2022-07-29 14:57:47 +02007934 found = 1;
Michal Vaskod3678892020-05-21 10:06:58 +02007935 break;
7936 }
Michal Vasko56c008c2022-07-29 14:57:47 +02007937 } while (i);
7938 if (!found) {
Michal Vasko4ad69e72021-10-26 16:25:55 +02007939 /* generate message */
7940 eval_name_test_scnode_no_match_msg(set, scparent, ncname, ncname_len, exp->expr, options);
7941
7942 if (options & LYXP_SCNODE_ERROR) {
7943 /* error */
7944 rc = LY_EVALID;
7945 goto cleanup;
Michal Vaskoa036a6b2021-10-12 16:05:23 +02007946 }
Michal Vaskoa036a6b2021-10-12 16:05:23 +02007947
7948 /* skip the predicates and the rest of this path to not generate invalid warnings */
7949 rc = LY_ENOT;
7950 scnode_skip_pred = 1;
Michal Vaskod3678892020-05-21 10:06:58 +02007951 }
7952 } else {
Michal Vasko49fec8e2022-05-24 10:28:33 +02007953 if (all_desc && (axis == LYXP_AXIS_CHILD)) {
7954 /* efficient evaluation */
7955 rc = moveto_node_alldesc_child(set, moveto_mod, ncname_dict, options);
7956 } else if (scnode && (axis == LYXP_AXIS_CHILD)) {
7957 /* we can find the child nodes using hashes */
7958 rc = moveto_node_hash_child(set, scnode, predicates, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007959 } else {
Michal Vasko49fec8e2022-05-24 10:28:33 +02007960 if (all_desc) {
7961 /* "//" == "/descendant-or-self::node()/" */
7962 rc = xpath_pi_node(set, LYXP_AXIS_DESCENDANT_OR_SELF, options);
7963 LY_CHECK_GOTO(rc, cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02007964 }
Michal Vasko49fec8e2022-05-24 10:28:33 +02007965 rc = moveto_node(set, moveto_mod, ncname_dict, axis, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007966 }
7967 LY_CHECK_GOTO(rc, cleanup);
7968 }
7969 }
7970
Michal Vaskoa036a6b2021-10-12 16:05:23 +02007971 if (scnode_skip_pred) {
7972 /* skip predicates */
7973 options |= LYXP_SKIP_EXPR;
7974 }
7975
Michal Vaskod3678892020-05-21 10:06:58 +02007976 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007977 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02007978 r = eval_predicate(exp, tok_idx, set, options, axis);
Michal Vaskoa036a6b2021-10-12 16:05:23 +02007979 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02007980 }
7981
7982cleanup:
Michal Vaskoa036a6b2021-10-12 16:05:23 +02007983 if (scnode_skip_pred) {
7984 /* restore options */
7985 options &= ~LYXP_SKIP_EXPR;
7986 }
aPiecek8b0cc152021-05-31 16:40:31 +02007987 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko004d3152020-06-11 19:59:22 +02007988 lydict_remove(set->ctx, ncname_dict);
Michal Vaskof7e16e22020-10-21 09:24:39 +02007989 ly_path_predicates_free(set->ctx, pred_type, predicates);
Michal Vaskodb51a8d2020-05-27 15:22:29 +02007990 }
Michal Vaskod3678892020-05-21 10:06:58 +02007991 return rc;
7992}
7993
7994/**
7995 * @brief Evaluate NodeType and any following Predicates. Logs directly on error.
7996 *
7997 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
7998 * [6] NodeTest ::= NameTest | NodeType '(' ')'
7999 * [8] NodeType ::= 'text' | 'node'
8000 *
8001 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008002 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko49fec8e2022-05-24 10:28:33 +02008003 * @param[in] axis Axis to search on.
8004 * @param[in] all_desc Whether to search all the descendants or axis only.
aPiecek8b0cc152021-05-31 16:40:31 +02008005 * @param[in,out] set Context and result set.
Michal Vaskod3678892020-05-21 10:06:58 +02008006 * @param[in] options XPath options.
8007 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8008 */
8009static LY_ERR
Michal Vasko49fec8e2022-05-24 10:28:33 +02008010eval_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 +02008011 struct lyxp_set *set, uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02008012{
8013 LY_ERR rc;
8014
Michal Vaskod3678892020-05-21 10:06:58 +02008015 (void)all_desc;
8016
aPiecek8b0cc152021-05-31 16:40:31 +02008017 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko004d3152020-06-11 19:59:22 +02008018 assert(exp->tok_len[*tok_idx] == 4);
Michal Vasko49fec8e2022-05-24 10:28:33 +02008019 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "node", 4)) {
8020 rc = xpath_pi_node(set, axis, options);
Michal Vaskod3678892020-05-21 10:06:58 +02008021 } else {
Michal Vasko49fec8e2022-05-24 10:28:33 +02008022 assert(!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "text", 4));
8023 rc = xpath_pi_text(set, axis, options);
Michal Vaskod3678892020-05-21 10:06:58 +02008024 }
Michal Vasko49fec8e2022-05-24 10:28:33 +02008025 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02008026 }
aPiecek8b0cc152021-05-31 16:40:31 +02008027 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008028 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008029 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02008030
8031 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02008032 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
aPiecek8b0cc152021-05-31 16:40:31 +02008033 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008034 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008035 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02008036
8037 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02008038 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
aPiecek8b0cc152021-05-31 16:40:31 +02008039 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008040 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008041 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02008042
8043 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02008044 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02008045 rc = eval_predicate(exp, tok_idx, set, options, axis);
Michal Vaskod3678892020-05-21 10:06:58 +02008046 LY_CHECK_RET(rc);
8047 }
8048
8049 return LY_SUCCESS;
8050}
8051
8052/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02008053 * @brief Evaluate RelativeLocationPath. Logs directly on error.
8054 *
8055 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
8056 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
Michal Vaskod3678892020-05-21 10:06:58 +02008057 * [6] NodeTest ::= NameTest | NodeType '(' ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02008058 *
8059 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008060 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008061 * @param[in] all_desc Whether to search all the descendants or children only.
aPiecek8b0cc152021-05-31 16:40:31 +02008062 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008063 * @param[in] options XPath options.
8064 * @return LY_ERR (YL_EINCOMPLETE on unresolved when)
8065 */
8066static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008067eval_relative_location_path(const struct lyxp_expr *exp, uint16_t *tok_idx, ly_bool all_desc, struct lyxp_set *set,
8068 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008069{
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008070 LY_ERR rc = LY_SUCCESS;
Michal Vasko49fec8e2022-05-24 10:28:33 +02008071 enum lyxp_axis axis;
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008072 int scnode_skip_path = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008073
8074 goto step;
8075 do {
8076 /* evaluate '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02008077 if (exp->tok_len[*tok_idx] == 1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008078 all_desc = 0;
8079 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02008080 assert(exp->tok_len[*tok_idx] == 2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008081 all_desc = 1;
8082 }
aPiecek8b0cc152021-05-31 16:40:31 +02008083 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008084 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008085 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008086
8087step:
Michal Vasko49fec8e2022-05-24 10:28:33 +02008088 /* AxisSpecifier */
8089 if (exp->tokens[*tok_idx] == LYXP_TOKEN_AXISNAME) {
8090 axis = str2axis(exp->expr + exp->tok_pos[*tok_idx], exp->tok_len[*tok_idx]);
Michal Vaskod3678892020-05-21 10:06:58 +02008091
aPiecek8b0cc152021-05-31 16:40:31 +02008092 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008093 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
8094 ++(*tok_idx);
8095
8096 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_DCOLON);
8097 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
8098 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
8099 ++(*tok_idx);
8100 } else if (exp->tokens[*tok_idx] == LYXP_TOKEN_AT) {
8101 axis = LYXP_AXIS_ATTRIBUTE;
8102
8103 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
8104 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008105 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02008106 } else {
Michal Vasko49fec8e2022-05-24 10:28:33 +02008107 /* default */
8108 axis = LYXP_AXIS_CHILD;
Michal Vaskod3678892020-05-21 10:06:58 +02008109 }
8110
Michal Vasko49fec8e2022-05-24 10:28:33 +02008111 /* NodeTest Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02008112 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008113 case LYXP_TOKEN_DOT:
8114 /* evaluate '.' */
Michal Vasko49fec8e2022-05-24 10:28:33 +02008115 if (!(options & LYXP_SKIP_EXPR)) {
8116 if (((options & LYXP_SCNODE_ALL) && (set->type != LYXP_SET_SCNODE_SET)) ||
8117 (!(options & LYXP_SCNODE_ALL) && (set->type != LYXP_SET_NODE_SET))) {
8118 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
8119 rc = LY_EVALID;
8120 goto cleanup;
8121 }
8122
8123 if (all_desc) {
8124 rc = xpath_pi_node(set, LYXP_AXIS_DESCENDANT_OR_SELF, options);
8125 LY_CHECK_GOTO(rc, cleanup);
8126 }
8127 rc = xpath_pi_node(set, LYXP_AXIS_SELF, options);
8128 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008129 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008130 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008131 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008132 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008133 break;
8134
8135 case LYXP_TOKEN_DDOT:
8136 /* evaluate '..' */
Michal Vasko49fec8e2022-05-24 10:28:33 +02008137 if (!(options & LYXP_SKIP_EXPR)) {
8138 if (((options & LYXP_SCNODE_ALL) && (set->type != LYXP_SET_SCNODE_SET)) ||
8139 (!(options & LYXP_SCNODE_ALL) && (set->type != LYXP_SET_NODE_SET))) {
8140 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
8141 rc = LY_EVALID;
8142 goto cleanup;
8143 }
8144
8145 if (all_desc) {
8146 rc = xpath_pi_node(set, LYXP_AXIS_DESCENDANT_OR_SELF, options);
8147 LY_CHECK_GOTO(rc, cleanup);
8148 }
8149 rc = xpath_pi_node(set, LYXP_AXIS_PARENT, options);
8150 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008151 }
aPiecek8b0cc152021-05-31 16:40:31 +02008152 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008153 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008154 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008155 break;
8156
Michal Vasko03ff5a72019-09-11 13:49:33 +02008157 case LYXP_TOKEN_NAMETEST:
Michal Vaskod3678892020-05-21 10:06:58 +02008158 /* evaluate NameTest Predicate* */
Michal Vasko49fec8e2022-05-24 10:28:33 +02008159 rc = eval_name_test_with_predicate(exp, tok_idx, axis, all_desc, set, options);
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008160 if (rc == LY_ENOT) {
8161 assert(options & LYXP_SCNODE_ALL);
8162 /* skip the rest of this path */
8163 rc = LY_SUCCESS;
8164 scnode_skip_path = 1;
8165 options |= LYXP_SKIP_EXPR;
8166 }
8167 LY_CHECK_GOTO(rc, cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02008168 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008169
Michal Vaskod3678892020-05-21 10:06:58 +02008170 case LYXP_TOKEN_NODETYPE:
8171 /* evaluate NodeType Predicate* */
Michal Vasko49fec8e2022-05-24 10:28:33 +02008172 rc = eval_node_type_with_predicate(exp, tok_idx, axis, all_desc, set, options);
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008173 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008174 break;
8175
8176 default:
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008177 LOGINT(set->ctx);
8178 rc = LY_EINT;
8179 goto cleanup;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008180 }
Michal Vasko004d3152020-06-11 19:59:22 +02008181 } while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008182
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008183cleanup:
8184 if (scnode_skip_path) {
8185 options &= ~LYXP_SKIP_EXPR;
8186 }
8187 return rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008188}
8189
8190/**
8191 * @brief Evaluate AbsoluteLocationPath. Logs directly on error.
8192 *
8193 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
8194 *
8195 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008196 * @param[in] tok_idx Position in the expression @p exp.
aPiecek8b0cc152021-05-31 16:40:31 +02008197 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008198 * @param[in] options XPath options.
8199 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8200 */
8201static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008202eval_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 +02008203{
Radek Krejci857189e2020-09-01 13:26:36 +02008204 ly_bool all_desc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008205
aPiecek8b0cc152021-05-31 16:40:31 +02008206 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008207 /* no matter what tokens follow, we need to be at the root */
Michal Vaskob0099a92020-08-31 14:55:23 +02008208 LY_CHECK_RET(moveto_root(set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008209 }
8210
8211 /* '/' RelativeLocationPath? */
Michal Vasko004d3152020-06-11 19:59:22 +02008212 if (exp->tok_len[*tok_idx] == 1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008213 /* evaluate '/' - deferred */
8214 all_desc = 0;
aPiecek8b0cc152021-05-31 16:40:31 +02008215 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008216 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008217 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008218
Michal Vasko004d3152020-06-11 19:59:22 +02008219 if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008220 return LY_SUCCESS;
8221 }
Michal Vasko004d3152020-06-11 19:59:22 +02008222 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008223 case LYXP_TOKEN_DOT:
8224 case LYXP_TOKEN_DDOT:
Michal Vasko49fec8e2022-05-24 10:28:33 +02008225 case LYXP_TOKEN_AXISNAME:
Michal Vasko03ff5a72019-09-11 13:49:33 +02008226 case LYXP_TOKEN_AT:
8227 case LYXP_TOKEN_NAMETEST:
8228 case LYXP_TOKEN_NODETYPE:
Michal Vaskob0099a92020-08-31 14:55:23 +02008229 LY_CHECK_RET(eval_relative_location_path(exp, tok_idx, all_desc, set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008230 break;
8231 default:
8232 break;
8233 }
8234
Michal Vasko03ff5a72019-09-11 13:49:33 +02008235 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02008236 /* '//' RelativeLocationPath */
Michal Vasko03ff5a72019-09-11 13:49:33 +02008237 /* evaluate '//' - deferred so as not to waste memory by remembering all the nodes */
8238 all_desc = 1;
aPiecek8b0cc152021-05-31 16:40:31 +02008239 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008240 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008241 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008242
Michal Vaskob0099a92020-08-31 14:55:23 +02008243 LY_CHECK_RET(eval_relative_location_path(exp, tok_idx, all_desc, set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008244 }
8245
8246 return LY_SUCCESS;
8247}
8248
8249/**
8250 * @brief Evaluate FunctionCall. Logs directly on error.
8251 *
Michal Vaskod3678892020-05-21 10:06:58 +02008252 * [11] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02008253 *
8254 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008255 * @param[in] tok_idx Position in the expression @p exp.
aPiecek8b0cc152021-05-31 16:40:31 +02008256 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008257 * @param[in] options XPath options.
8258 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8259 */
8260static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008261eval_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 +02008262{
8263 LY_ERR rc;
Michal Vasko69730152020-10-09 16:30:07 +02008264
Radek Krejci1deb5be2020-08-26 16:43:36 +02008265 LY_ERR (*xpath_func)(struct lyxp_set **, uint16_t, struct lyxp_set *, uint32_t) = NULL;
Michal Vasko0cbf54f2019-12-16 10:01:06 +01008266 uint16_t arg_count = 0, i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008267 struct lyxp_set **args = NULL, **args_aux;
8268
aPiecek8b0cc152021-05-31 16:40:31 +02008269 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008270 /* FunctionName */
Michal Vasko004d3152020-06-11 19:59:22 +02008271 switch (exp->tok_len[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008272 case 3:
Michal Vasko004d3152020-06-11 19:59:22 +02008273 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008274 xpath_func = &xpath_not;
Michal Vasko004d3152020-06-11 19:59:22 +02008275 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008276 xpath_func = &xpath_sum;
8277 }
8278 break;
8279 case 4:
Michal Vasko004d3152020-06-11 19:59:22 +02008280 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008281 xpath_func = &xpath_lang;
Michal Vasko004d3152020-06-11 19:59:22 +02008282 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008283 xpath_func = &xpath_last;
Michal Vasko004d3152020-06-11 19:59:22 +02008284 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008285 xpath_func = &xpath_name;
Michal Vasko004d3152020-06-11 19:59:22 +02008286 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008287 xpath_func = &xpath_true;
8288 }
8289 break;
8290 case 5:
Michal Vasko004d3152020-06-11 19:59:22 +02008291 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008292 xpath_func = &xpath_count;
Michal Vasko004d3152020-06-11 19:59:22 +02008293 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008294 xpath_func = &xpath_false;
Michal Vasko004d3152020-06-11 19:59:22 +02008295 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008296 xpath_func = &xpath_floor;
Michal Vasko004d3152020-06-11 19:59:22 +02008297 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008298 xpath_func = &xpath_round;
Michal Vasko004d3152020-06-11 19:59:22 +02008299 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008300 xpath_func = &xpath_deref;
8301 }
8302 break;
8303 case 6:
Michal Vasko004d3152020-06-11 19:59:22 +02008304 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008305 xpath_func = &xpath_concat;
Michal Vasko004d3152020-06-11 19:59:22 +02008306 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008307 xpath_func = &xpath_number;
Michal Vasko004d3152020-06-11 19:59:22 +02008308 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008309 xpath_func = &xpath_string;
8310 }
8311 break;
8312 case 7:
Michal Vasko004d3152020-06-11 19:59:22 +02008313 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008314 xpath_func = &xpath_boolean;
Michal Vasko004d3152020-06-11 19:59:22 +02008315 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008316 xpath_func = &xpath_ceiling;
Michal Vasko004d3152020-06-11 19:59:22 +02008317 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008318 xpath_func = &xpath_current;
8319 }
8320 break;
8321 case 8:
Michal Vasko004d3152020-06-11 19:59:22 +02008322 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008323 xpath_func = &xpath_contains;
Michal Vasko004d3152020-06-11 19:59:22 +02008324 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008325 xpath_func = &xpath_position;
Michal Vasko004d3152020-06-11 19:59:22 +02008326 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "re-match", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008327 xpath_func = &xpath_re_match;
8328 }
8329 break;
8330 case 9:
Michal Vasko004d3152020-06-11 19:59:22 +02008331 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008332 xpath_func = &xpath_substring;
Michal Vasko004d3152020-06-11 19:59:22 +02008333 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008334 xpath_func = &xpath_translate;
8335 }
8336 break;
8337 case 10:
Michal Vasko004d3152020-06-11 19:59:22 +02008338 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008339 xpath_func = &xpath_local_name;
Michal Vasko004d3152020-06-11 19:59:22 +02008340 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008341 xpath_func = &xpath_enum_value;
Michal Vasko004d3152020-06-11 19:59:22 +02008342 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008343 xpath_func = &xpath_bit_is_set;
8344 }
8345 break;
8346 case 11:
Michal Vasko004d3152020-06-11 19:59:22 +02008347 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008348 xpath_func = &xpath_starts_with;
8349 }
8350 break;
8351 case 12:
Michal Vasko004d3152020-06-11 19:59:22 +02008352 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008353 xpath_func = &xpath_derived_from;
8354 }
8355 break;
8356 case 13:
Michal Vasko004d3152020-06-11 19:59:22 +02008357 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008358 xpath_func = &xpath_namespace_uri;
Michal Vasko004d3152020-06-11 19:59:22 +02008359 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string-length", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008360 xpath_func = &xpath_string_length;
8361 }
8362 break;
8363 case 15:
Michal Vasko004d3152020-06-11 19:59:22 +02008364 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008365 xpath_func = &xpath_normalize_space;
Michal Vasko004d3152020-06-11 19:59:22 +02008366 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-after", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008367 xpath_func = &xpath_substring_after;
8368 }
8369 break;
8370 case 16:
Michal Vasko004d3152020-06-11 19:59:22 +02008371 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008372 xpath_func = &xpath_substring_before;
8373 }
8374 break;
8375 case 20:
Michal Vasko004d3152020-06-11 19:59:22 +02008376 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from-or-self", 20)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008377 xpath_func = &xpath_derived_from_or_self;
8378 }
8379 break;
8380 }
8381
8382 if (!xpath_func) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01008383 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 +02008384 return LY_EVALID;
8385 }
8386 }
8387
aPiecek8b0cc152021-05-31 16:40:31 +02008388 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008389 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008390 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008391
8392 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02008393 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
aPiecek8b0cc152021-05-31 16:40:31 +02008394 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008395 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008396 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008397
8398 /* ( Expr ( ',' Expr )* )? */
Michal Vasko004d3152020-06-11 19:59:22 +02008399 if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) {
aPiecek8b0cc152021-05-31 16:40:31 +02008400 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008401 args = malloc(sizeof *args);
8402 LY_CHECK_ERR_GOTO(!args, LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
8403 arg_count = 1;
8404 args[0] = set_copy(set);
8405 if (!args[0]) {
8406 rc = LY_EMEM;
8407 goto cleanup;
8408 }
8409
Michal Vasko004d3152020-06-11 19:59:22 +02008410 rc = eval_expr_select(exp, tok_idx, 0, args[0], options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008411 LY_CHECK_GOTO(rc, cleanup);
8412 } else {
aPiecek8b0cc152021-05-31 16:40:31 +02008413 rc = eval_expr_select(exp, tok_idx, 0, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008414 LY_CHECK_GOTO(rc, cleanup);
8415 }
8416 }
Michal Vasko004d3152020-06-11 19:59:22 +02008417 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) {
aPiecek8b0cc152021-05-31 16:40:31 +02008418 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008419 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008420 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008421
aPiecek8b0cc152021-05-31 16:40:31 +02008422 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008423 ++arg_count;
8424 args_aux = realloc(args, arg_count * sizeof *args);
8425 LY_CHECK_ERR_GOTO(!args_aux, arg_count--; LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
8426 args = args_aux;
8427 args[arg_count - 1] = set_copy(set);
8428 if (!args[arg_count - 1]) {
8429 rc = LY_EMEM;
8430 goto cleanup;
8431 }
8432
Michal Vasko004d3152020-06-11 19:59:22 +02008433 rc = eval_expr_select(exp, tok_idx, 0, args[arg_count - 1], options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008434 LY_CHECK_GOTO(rc, cleanup);
8435 } else {
aPiecek8b0cc152021-05-31 16:40:31 +02008436 rc = eval_expr_select(exp, tok_idx, 0, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008437 LY_CHECK_GOTO(rc, cleanup);
8438 }
8439 }
8440
8441 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02008442 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
aPiecek8b0cc152021-05-31 16:40:31 +02008443 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008444 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008445 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008446
aPiecek8b0cc152021-05-31 16:40:31 +02008447 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008448 /* evaluate function */
8449 rc = xpath_func(args, arg_count, set, options);
8450
8451 if (options & LYXP_SCNODE_ALL) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008452 /* merge all nodes from arg evaluations */
8453 for (i = 0; i < arg_count; ++i) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008454 set_scnode_clear_ctx(args[i], LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008455 lyxp_set_scnode_merge(set, args[i]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008456 }
8457 }
8458 } else {
8459 rc = LY_SUCCESS;
8460 }
8461
8462cleanup:
8463 for (i = 0; i < arg_count; ++i) {
8464 lyxp_set_free(args[i]);
8465 }
8466 free(args);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008467 return rc;
8468}
8469
8470/**
8471 * @brief Evaluate Number. Logs directly on error.
8472 *
8473 * @param[in] ctx Context for errors.
8474 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008475 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008476 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8477 * @return LY_ERR
8478 */
8479static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008480eval_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 +02008481{
8482 long double num;
8483 char *endptr;
8484
8485 if (set) {
8486 errno = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02008487 num = strtold(&exp->expr[exp->tok_pos[*tok_idx]], &endptr);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008488 if (errno) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01008489 LOGVAL(ctx, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]);
8490 LOGVAL(ctx, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double (%s).",
Michal Vasko69730152020-10-09 16:30:07 +02008491 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]], strerror(errno));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008492 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02008493 } else if (endptr - &exp->expr[exp->tok_pos[*tok_idx]] != exp->tok_len[*tok_idx]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01008494 LOGVAL(ctx, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]);
8495 LOGVAL(ctx, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double.",
Michal Vasko69730152020-10-09 16:30:07 +02008496 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008497 return LY_EVALID;
8498 }
8499
8500 set_fill_number(set, num);
8501 }
8502
8503 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008504 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008505 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008506 return LY_SUCCESS;
8507}
8508
aPiecekdf23eee2021-10-07 12:21:50 +02008509LY_ERR
8510lyxp_vars_find(struct lyxp_var *vars, const char *name, size_t name_len, struct lyxp_var **var)
8511{
8512 LY_ERR ret = LY_ENOTFOUND;
8513 LY_ARRAY_COUNT_TYPE u;
8514
8515 assert(vars && name);
8516
8517 name_len = name_len ? name_len : strlen(name);
8518
8519 LY_ARRAY_FOR(vars, u) {
8520 if (!strncmp(vars[u].name, name, name_len)) {
8521 ret = LY_SUCCESS;
8522 break;
8523 }
8524 }
8525
8526 if (var && !ret) {
8527 *var = &vars[u];
8528 }
8529
8530 return ret;
8531}
8532
Michal Vasko03ff5a72019-09-11 13:49:33 +02008533/**
aPiecekfba75362021-10-07 12:39:48 +02008534 * @brief Evaluate VariableReference.
8535 *
8536 * @param[in] exp Parsed XPath expression.
8537 * @param[in] tok_idx Position in the expression @p exp.
8538 * @param[in] vars [Sized array](@ref sizedarrays) of XPath variables.
8539 * @param[in,out] set Context and result set.
8540 * @param[in] options XPath options.
8541 * @return LY_ERR value.
8542 */
8543static LY_ERR
8544eval_variable_reference(const struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set, uint32_t options)
8545{
8546 LY_ERR ret;
8547 const char *name;
8548 struct lyxp_var *var;
8549 const struct lyxp_var *vars;
8550 struct lyxp_expr *tokens = NULL;
8551 uint16_t token_index;
8552
8553 vars = set->vars;
8554
Michal Vasko49fec8e2022-05-24 10:28:33 +02008555 /* find out the name and value of the variable */
aPiecekfba75362021-10-07 12:39:48 +02008556 name = &exp->expr[exp->tok_pos[*tok_idx]];
8557 ret = lyxp_vars_find((struct lyxp_var *)vars, name, exp->tok_len[*tok_idx], &var);
8558 LY_CHECK_ERR_RET(ret, LOGERR(set->ctx, ret,
8559 "XPath variable \"%s\" not defined.", name), ret);
8560
Michal Vasko49fec8e2022-05-24 10:28:33 +02008561 /* parse value */
aPiecekfba75362021-10-07 12:39:48 +02008562 ret = lyxp_expr_parse(set->ctx, var->value, 0, 1, &tokens);
8563 LY_CHECK_GOTO(ret, cleanup);
8564
Michal Vasko49fec8e2022-05-24 10:28:33 +02008565 /* evaluate value */
aPiecekfba75362021-10-07 12:39:48 +02008566 token_index = 0;
8567 ret = eval_expr_select(tokens, &token_index, 0, set, options);
8568 LY_CHECK_GOTO(ret, cleanup);
8569
8570cleanup:
8571 lyxp_expr_free(set->ctx, tokens);
8572
8573 return ret;
8574}
8575
8576/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02008577 * @brief Evaluate PathExpr. Logs directly on error.
8578 *
Michal Vaskod3678892020-05-21 10:06:58 +02008579 * [12] PathExpr ::= LocationPath | PrimaryExpr Predicate*
Michal Vasko03ff5a72019-09-11 13:49:33 +02008580 * | PrimaryExpr Predicate* '/' RelativeLocationPath
8581 * | PrimaryExpr Predicate* '//' RelativeLocationPath
8582 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
aPiecekfba75362021-10-07 12:39:48 +02008583 * [10] PrimaryExpr ::= VariableReference | '(' Expr ')' | Literal | Number | FunctionCall
Michal Vasko03ff5a72019-09-11 13:49:33 +02008584 *
8585 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008586 * @param[in] tok_idx Position in the expression @p exp.
aPiecek8b0cc152021-05-31 16:40:31 +02008587 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008588 * @param[in] options XPath options.
8589 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8590 */
8591static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008592eval_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 +02008593{
Michal Vasko49fec8e2022-05-24 10:28:33 +02008594 ly_bool all_desc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008595 LY_ERR rc;
8596
Michal Vasko004d3152020-06-11 19:59:22 +02008597 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008598 case LYXP_TOKEN_PAR1:
8599 /* '(' Expr ')' */
8600
8601 /* '(' */
aPiecek8b0cc152021-05-31 16:40:31 +02008602 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008603 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008604 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008605
8606 /* Expr */
Michal Vasko004d3152020-06-11 19:59:22 +02008607 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008608 LY_CHECK_RET(rc);
8609
8610 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02008611 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
aPiecek8b0cc152021-05-31 16:40:31 +02008612 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008613 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008614 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008615
Michal Vasko03ff5a72019-09-11 13:49:33 +02008616 goto predicate;
8617
8618 case LYXP_TOKEN_DOT:
8619 case LYXP_TOKEN_DDOT:
Michal Vasko49fec8e2022-05-24 10:28:33 +02008620 case LYXP_TOKEN_AXISNAME:
Michal Vasko03ff5a72019-09-11 13:49:33 +02008621 case LYXP_TOKEN_AT:
8622 case LYXP_TOKEN_NAMETEST:
8623 case LYXP_TOKEN_NODETYPE:
8624 /* RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02008625 rc = eval_relative_location_path(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008626 LY_CHECK_RET(rc);
8627 break;
8628
aPiecekfba75362021-10-07 12:39:48 +02008629 case LYXP_TOKEN_VARREF:
8630 /* VariableReference */
8631 rc = eval_variable_reference(exp, tok_idx, set, options);
8632 LY_CHECK_RET(rc);
8633 ++(*tok_idx);
8634
aPiecekfba75362021-10-07 12:39:48 +02008635 goto predicate;
8636
Michal Vasko03ff5a72019-09-11 13:49:33 +02008637 case LYXP_TOKEN_FUNCNAME:
8638 /* FunctionCall */
aPiecek8b0cc152021-05-31 16:40:31 +02008639 rc = eval_function_call(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008640 LY_CHECK_RET(rc);
8641
Michal Vasko03ff5a72019-09-11 13:49:33 +02008642 goto predicate;
8643
Michal Vasko3e48bf32020-06-01 08:39:07 +02008644 case LYXP_TOKEN_OPER_PATH:
8645 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +02008646 /* AbsoluteLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02008647 rc = eval_absolute_location_path(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008648 LY_CHECK_RET(rc);
8649 break;
8650
8651 case LYXP_TOKEN_LITERAL:
8652 /* Literal */
aPiecek8b0cc152021-05-31 16:40:31 +02008653 if ((options & LYXP_SKIP_EXPR) || (options & LYXP_SCNODE_ALL)) {
8654 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008655 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008656 }
Michal Vasko004d3152020-06-11 19:59:22 +02008657 eval_literal(exp, tok_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008658 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02008659 eval_literal(exp, tok_idx, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008660 }
8661
Michal Vasko03ff5a72019-09-11 13:49:33 +02008662 goto predicate;
8663
8664 case LYXP_TOKEN_NUMBER:
8665 /* Number */
aPiecek8b0cc152021-05-31 16:40:31 +02008666 if ((options & LYXP_SKIP_EXPR) || (options & LYXP_SCNODE_ALL)) {
8667 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008668 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008669 }
Michal Vasko004d3152020-06-11 19:59:22 +02008670 rc = eval_number(NULL, exp, tok_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008671 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02008672 rc = eval_number(set->ctx, exp, tok_idx, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008673 }
8674 LY_CHECK_RET(rc);
8675
Michal Vasko03ff5a72019-09-11 13:49:33 +02008676 goto predicate;
8677
8678 default:
Michal Vasko49fec8e2022-05-24 10:28:33 +02008679 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 +02008680 return LY_EVALID;
8681 }
8682
8683 return LY_SUCCESS;
8684
8685predicate:
8686 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02008687 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02008688 rc = eval_predicate(exp, tok_idx, set, options, LYXP_AXIS_CHILD);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008689 LY_CHECK_RET(rc);
8690 }
8691
8692 /* ('/' or '//') RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02008693 if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008694
8695 /* evaluate '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02008696 if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008697 all_desc = 0;
8698 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008699 all_desc = 1;
8700 }
8701
aPiecek8b0cc152021-05-31 16:40:31 +02008702 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008703 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008704 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008705
Michal Vasko004d3152020-06-11 19:59:22 +02008706 rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008707 LY_CHECK_RET(rc);
8708 }
8709
8710 return LY_SUCCESS;
8711}
8712
8713/**
8714 * @brief Evaluate UnionExpr. Logs directly on error.
8715 *
Michal Vaskod3678892020-05-21 10:06:58 +02008716 * [20] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008717 *
8718 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008719 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008720 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008721 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008722 * @param[in] options XPath options.
8723 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8724 */
8725static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008726eval_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 +02008727{
8728 LY_ERR rc = LY_SUCCESS;
8729 struct lyxp_set orig_set, set2;
8730 uint16_t i;
8731
8732 assert(repeat);
8733
8734 set_init(&orig_set, set);
8735 set_init(&set2, set);
8736
8737 set_fill_set(&orig_set, set);
8738
Michal Vasko004d3152020-06-11 19:59:22 +02008739 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008740 LY_CHECK_GOTO(rc, cleanup);
8741
8742 /* ('|' PathExpr)* */
8743 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008744 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_UNI);
aPiecek8b0cc152021-05-31 16:40:31 +02008745 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008746 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008747 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008748
aPiecek8b0cc152021-05-31 16:40:31 +02008749 if (options & LYXP_SKIP_EXPR) {
8750 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008751 LY_CHECK_GOTO(rc, cleanup);
8752 continue;
8753 }
8754
8755 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008756 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008757 LY_CHECK_GOTO(rc, cleanup);
8758
8759 /* eval */
8760 if (options & LYXP_SCNODE_ALL) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01008761 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008762 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008763 rc = moveto_union(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008764 LY_CHECK_GOTO(rc, cleanup);
8765 }
8766 }
8767
8768cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008769 lyxp_set_free_content(&orig_set);
8770 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008771 return rc;
8772}
8773
8774/**
8775 * @brief Evaluate UnaryExpr. Logs directly on error.
8776 *
Michal Vaskod3678892020-05-21 10:06:58 +02008777 * [19] UnaryExpr ::= UnionExpr | '-' UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008778 *
8779 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008780 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008781 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008782 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008783 * @param[in] options XPath options.
8784 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8785 */
8786static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008787eval_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 +02008788{
8789 LY_ERR rc;
8790 uint16_t this_op, i;
8791
8792 assert(repeat);
8793
8794 /* ('-')+ */
Michal Vasko004d3152020-06-11 19:59:22 +02008795 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008796 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008797 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 +02008798
aPiecek8b0cc152021-05-31 16:40:31 +02008799 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008800 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008801 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008802 }
8803
Michal Vasko004d3152020-06-11 19:59:22 +02008804 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNARY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008805 LY_CHECK_RET(rc);
8806
aPiecek8b0cc152021-05-31 16:40:31 +02008807 if (!(options & LYXP_SKIP_EXPR) && (repeat % 2)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008808 if (options & LYXP_SCNODE_ALL) {
8809 warn_operands(set->ctx, set, NULL, 1, exp->expr, exp->tok_pos[this_op]);
8810 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008811 rc = moveto_op_math(set, NULL, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008812 LY_CHECK_RET(rc);
8813 }
8814 }
8815
8816 return LY_SUCCESS;
8817}
8818
8819/**
8820 * @brief Evaluate MultiplicativeExpr. Logs directly on error.
8821 *
Michal Vaskod3678892020-05-21 10:06:58 +02008822 * [18] MultiplicativeExpr ::= UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008823 * | MultiplicativeExpr '*' UnaryExpr
8824 * | MultiplicativeExpr 'div' UnaryExpr
8825 * | MultiplicativeExpr 'mod' UnaryExpr
8826 *
8827 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008828 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008829 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008830 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008831 * @param[in] options XPath options.
8832 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8833 */
8834static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008835eval_multiplicative_expr(const struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set,
8836 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008837{
8838 LY_ERR rc;
8839 uint16_t this_op;
8840 struct lyxp_set orig_set, set2;
8841 uint16_t i;
8842
8843 assert(repeat);
8844
8845 set_init(&orig_set, set);
8846 set_init(&set2, set);
8847
8848 set_fill_set(&orig_set, set);
8849
Michal Vasko004d3152020-06-11 19:59:22 +02008850 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008851 LY_CHECK_GOTO(rc, cleanup);
8852
8853 /* ('*' / 'div' / 'mod' UnaryExpr)* */
8854 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008855 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008856
Michal Vasko004d3152020-06-11 19:59:22 +02008857 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH);
aPiecek8b0cc152021-05-31 16:40:31 +02008858 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008859 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008860 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008861
aPiecek8b0cc152021-05-31 16:40:31 +02008862 if (options & LYXP_SKIP_EXPR) {
8863 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008864 LY_CHECK_GOTO(rc, cleanup);
8865 continue;
8866 }
8867
8868 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008869 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008870 LY_CHECK_GOTO(rc, cleanup);
8871
8872 /* eval */
8873 if (options & LYXP_SCNODE_ALL) {
8874 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008875 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02008876 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008877 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008878 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008879 LY_CHECK_GOTO(rc, cleanup);
8880 }
8881 }
8882
8883cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008884 lyxp_set_free_content(&orig_set);
8885 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008886 return rc;
8887}
8888
8889/**
8890 * @brief Evaluate AdditiveExpr. Logs directly on error.
8891 *
Michal Vaskod3678892020-05-21 10:06:58 +02008892 * [17] AdditiveExpr ::= MultiplicativeExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008893 * | AdditiveExpr '+' MultiplicativeExpr
8894 * | AdditiveExpr '-' MultiplicativeExpr
8895 *
8896 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008897 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008898 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008899 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008900 * @param[in] options XPath options.
8901 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8902 */
8903static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008904eval_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 +02008905{
8906 LY_ERR rc;
8907 uint16_t this_op;
8908 struct lyxp_set orig_set, set2;
8909 uint16_t i;
8910
8911 assert(repeat);
8912
8913 set_init(&orig_set, set);
8914 set_init(&set2, set);
8915
8916 set_fill_set(&orig_set, set);
8917
Michal Vasko004d3152020-06-11 19:59:22 +02008918 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008919 LY_CHECK_GOTO(rc, cleanup);
8920
8921 /* ('+' / '-' MultiplicativeExpr)* */
8922 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008923 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008924
Michal Vasko004d3152020-06-11 19:59:22 +02008925 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008926 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008927 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008928 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008929
aPiecek8b0cc152021-05-31 16:40:31 +02008930 if (options & LYXP_SKIP_EXPR) {
8931 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008932 LY_CHECK_GOTO(rc, cleanup);
8933 continue;
8934 }
8935
8936 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008937 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008938 LY_CHECK_GOTO(rc, cleanup);
8939
8940 /* eval */
8941 if (options & LYXP_SCNODE_ALL) {
8942 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008943 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02008944 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008945 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008946 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008947 LY_CHECK_GOTO(rc, cleanup);
8948 }
8949 }
8950
8951cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008952 lyxp_set_free_content(&orig_set);
8953 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008954 return rc;
8955}
8956
8957/**
8958 * @brief Evaluate RelationalExpr. Logs directly on error.
8959 *
Michal Vaskod3678892020-05-21 10:06:58 +02008960 * [16] RelationalExpr ::= AdditiveExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008961 * | RelationalExpr '<' AdditiveExpr
8962 * | RelationalExpr '>' AdditiveExpr
8963 * | RelationalExpr '<=' AdditiveExpr
8964 * | RelationalExpr '>=' AdditiveExpr
8965 *
8966 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008967 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008968 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008969 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008970 * @param[in] options XPath options.
8971 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8972 */
8973static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008974eval_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 +02008975{
8976 LY_ERR rc;
8977 uint16_t this_op;
8978 struct lyxp_set orig_set, set2;
8979 uint16_t i;
8980
8981 assert(repeat);
8982
8983 set_init(&orig_set, set);
8984 set_init(&set2, set);
8985
8986 set_fill_set(&orig_set, set);
8987
Michal Vasko004d3152020-06-11 19:59:22 +02008988 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008989 LY_CHECK_GOTO(rc, cleanup);
8990
8991 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
8992 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008993 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008994
Michal Vasko004d3152020-06-11 19:59:22 +02008995 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_COMP);
aPiecek8b0cc152021-05-31 16:40:31 +02008996 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008997 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008998 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008999
aPiecek8b0cc152021-05-31 16:40:31 +02009000 if (options & LYXP_SKIP_EXPR) {
9001 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009002 LY_CHECK_GOTO(rc, cleanup);
9003 continue;
9004 }
9005
9006 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02009007 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009008 LY_CHECK_GOTO(rc, cleanup);
9009
9010 /* eval */
9011 if (options & LYXP_SCNODE_ALL) {
9012 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01009013 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02009014 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009015 } else {
Michal Vasko8abcecc2022-07-28 09:55:01 +02009016 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009017 LY_CHECK_GOTO(rc, cleanup);
9018 }
9019 }
9020
9021cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02009022 lyxp_set_free_content(&orig_set);
9023 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009024 return rc;
9025}
9026
9027/**
9028 * @brief Evaluate EqualityExpr. Logs directly on error.
9029 *
Michal Vaskod3678892020-05-21 10:06:58 +02009030 * [15] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02009031 * | EqualityExpr '!=' RelationalExpr
9032 *
9033 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02009034 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009035 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02009036 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009037 * @param[in] options XPath options.
9038 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
9039 */
9040static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02009041eval_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 +02009042{
9043 LY_ERR rc;
9044 uint16_t this_op;
9045 struct lyxp_set orig_set, set2;
9046 uint16_t i;
9047
9048 assert(repeat);
9049
9050 set_init(&orig_set, set);
9051 set_init(&set2, set);
9052
9053 set_fill_set(&orig_set, set);
9054
Michal Vasko004d3152020-06-11 19:59:22 +02009055 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009056 LY_CHECK_GOTO(rc, cleanup);
9057
9058 /* ('=' / '!=' RelationalExpr)* */
9059 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02009060 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009061
Michal Vasko004d3152020-06-11 19:59:22 +02009062 assert((exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_EQUAL) || (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_NEQUAL));
aPiecek8b0cc152021-05-31 16:40:31 +02009063 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02009064 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02009065 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009066
aPiecek8b0cc152021-05-31 16:40:31 +02009067 if (options & LYXP_SKIP_EXPR) {
9068 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009069 LY_CHECK_GOTO(rc, cleanup);
9070 continue;
9071 }
9072
9073 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02009074 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009075 LY_CHECK_GOTO(rc, cleanup);
9076
9077 /* eval */
9078 if (options & LYXP_SCNODE_ALL) {
9079 warn_operands(set->ctx, set, &set2, 0, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vasko004d3152020-06-11 19:59:22 +02009080 warn_equality_value(exp, set, *tok_idx - 1, this_op - 1, *tok_idx - 1);
9081 warn_equality_value(exp, &set2, this_op - 1, this_op - 1, *tok_idx - 1);
Michal Vaskoecd62de2019-11-13 12:35:11 +01009082 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02009083 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009084 } else {
Michal Vasko8abcecc2022-07-28 09:55:01 +02009085 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009086 LY_CHECK_GOTO(rc, cleanup);
9087 }
9088 }
9089
9090cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02009091 lyxp_set_free_content(&orig_set);
9092 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009093 return rc;
9094}
9095
9096/**
9097 * @brief Evaluate AndExpr. Logs directly on error.
9098 *
Michal Vaskod3678892020-05-21 10:06:58 +02009099 * [14] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02009100 *
9101 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02009102 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009103 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02009104 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009105 * @param[in] options XPath options.
9106 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
9107 */
9108static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02009109eval_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 +02009110{
9111 LY_ERR rc;
9112 struct lyxp_set orig_set, set2;
9113 uint16_t i;
9114
9115 assert(repeat);
9116
9117 set_init(&orig_set, set);
9118 set_init(&set2, set);
9119
9120 set_fill_set(&orig_set, set);
9121
Michal Vasko004d3152020-06-11 19:59:22 +02009122 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009123 LY_CHECK_GOTO(rc, cleanup);
9124
9125 /* cast to boolean, we know that will be the final result */
aPiecek8b0cc152021-05-31 16:40:31 +02009126 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02009127 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009128 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009129 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009130 }
9131
9132 /* ('and' EqualityExpr)* */
9133 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02009134 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG);
aPiecek8b0cc152021-05-31 16:40:31 +02009135 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, ((options & LYXP_SKIP_EXPR) || !set->val.bln ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02009136 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02009137 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009138
9139 /* lazy evaluation */
aPiecek8b0cc152021-05-31 16:40:31 +02009140 if ((options & LYXP_SKIP_EXPR) || ((set->type == LYXP_SET_BOOLEAN) && !set->val.bln)) {
9141 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009142 LY_CHECK_GOTO(rc, cleanup);
9143 continue;
9144 }
9145
9146 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02009147 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009148 LY_CHECK_GOTO(rc, cleanup);
9149
9150 /* eval - just get boolean value actually */
9151 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vasko1a09b212021-05-06 13:00:10 +02009152 set_scnode_clear_ctx(&set2, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskoecd62de2019-11-13 12:35:11 +01009153 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009154 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009155 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009156 set_fill_set(set, &set2);
9157 }
9158 }
9159
9160cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02009161 lyxp_set_free_content(&orig_set);
9162 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009163 return rc;
9164}
9165
9166/**
9167 * @brief Evaluate OrExpr. Logs directly on error.
9168 *
Michal Vaskod3678892020-05-21 10:06:58 +02009169 * [13] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02009170 *
9171 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02009172 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009173 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02009174 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009175 * @param[in] options XPath options.
9176 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
9177 */
9178static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02009179eval_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 +02009180{
9181 LY_ERR rc;
9182 struct lyxp_set orig_set, set2;
9183 uint16_t i;
9184
9185 assert(repeat);
9186
9187 set_init(&orig_set, set);
9188 set_init(&set2, set);
9189
9190 set_fill_set(&orig_set, set);
9191
Michal Vasko004d3152020-06-11 19:59:22 +02009192 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009193 LY_CHECK_GOTO(rc, cleanup);
9194
9195 /* cast to boolean, we know that will be the final result */
aPiecek8b0cc152021-05-31 16:40:31 +02009196 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02009197 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009198 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009199 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009200 }
9201
9202 /* ('or' AndExpr)* */
9203 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02009204 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG);
aPiecek8b0cc152021-05-31 16:40:31 +02009205 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, ((options & LYXP_SKIP_EXPR) || set->val.bln ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02009206 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02009207 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009208
9209 /* lazy evaluation */
aPiecek8b0cc152021-05-31 16:40:31 +02009210 if ((options & LYXP_SKIP_EXPR) || ((set->type == LYXP_SET_BOOLEAN) && set->val.bln)) {
9211 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009212 LY_CHECK_GOTO(rc, cleanup);
9213 continue;
9214 }
9215
9216 set_fill_set(&set2, &orig_set);
9217 /* expr_type cound have been LYXP_EXPR_NONE in all these later calls (except for the first one),
9218 * but it does not matter */
Michal Vasko004d3152020-06-11 19:59:22 +02009219 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009220 LY_CHECK_GOTO(rc, cleanup);
9221
9222 /* eval - just get boolean value actually */
9223 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vasko1a09b212021-05-06 13:00:10 +02009224 set_scnode_clear_ctx(&set2, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskoecd62de2019-11-13 12:35:11 +01009225 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009226 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009227 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009228 set_fill_set(set, &set2);
9229 }
9230 }
9231
9232cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02009233 lyxp_set_free_content(&orig_set);
9234 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009235 return rc;
9236}
9237
9238/**
Michal Vasko004d3152020-06-11 19:59:22 +02009239 * @brief Decide what expression is at the pointer @p tok_idx and evaluate it accordingly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009240 *
9241 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02009242 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009243 * @param[in] etype Expression type to evaluate.
aPiecek8b0cc152021-05-31 16:40:31 +02009244 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009245 * @param[in] options XPath options.
9246 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
9247 */
9248static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02009249eval_expr_select(const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_expr_type etype, struct lyxp_set *set,
9250 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02009251{
9252 uint16_t i, count;
9253 enum lyxp_expr_type next_etype;
9254 LY_ERR rc;
9255
9256 /* process operator repeats */
Michal Vasko004d3152020-06-11 19:59:22 +02009257 if (!exp->repeat[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02009258 next_etype = LYXP_EXPR_NONE;
9259 } else {
9260 /* find etype repeat */
Radek Krejci1e008d22020-08-17 11:37:37 +02009261 for (i = 0; exp->repeat[*tok_idx][i] > etype; ++i) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02009262
9263 /* select one-priority lower because etype expression called us */
9264 if (i) {
Michal Vasko004d3152020-06-11 19:59:22 +02009265 next_etype = exp->repeat[*tok_idx][i - 1];
Michal Vasko03ff5a72019-09-11 13:49:33 +02009266 /* count repeats for that expression */
Radek Krejci1e008d22020-08-17 11:37:37 +02009267 for (count = 0; i && exp->repeat[*tok_idx][i - 1] == next_etype; ++count, --i) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02009268 } else {
9269 next_etype = LYXP_EXPR_NONE;
9270 }
9271 }
9272
9273 /* decide what expression are we parsing based on the repeat */
9274 switch (next_etype) {
9275 case LYXP_EXPR_OR:
Michal Vasko004d3152020-06-11 19:59:22 +02009276 rc = eval_or_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009277 break;
9278 case LYXP_EXPR_AND:
Michal Vasko004d3152020-06-11 19:59:22 +02009279 rc = eval_and_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009280 break;
9281 case LYXP_EXPR_EQUALITY:
Michal Vasko004d3152020-06-11 19:59:22 +02009282 rc = eval_equality_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009283 break;
9284 case LYXP_EXPR_RELATIONAL:
Michal Vasko004d3152020-06-11 19:59:22 +02009285 rc = eval_relational_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009286 break;
9287 case LYXP_EXPR_ADDITIVE:
Michal Vasko004d3152020-06-11 19:59:22 +02009288 rc = eval_additive_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009289 break;
9290 case LYXP_EXPR_MULTIPLICATIVE:
Michal Vasko004d3152020-06-11 19:59:22 +02009291 rc = eval_multiplicative_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009292 break;
9293 case LYXP_EXPR_UNARY:
Michal Vasko004d3152020-06-11 19:59:22 +02009294 rc = eval_unary_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009295 break;
9296 case LYXP_EXPR_UNION:
Michal Vasko004d3152020-06-11 19:59:22 +02009297 rc = eval_union_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009298 break;
9299 case LYXP_EXPR_NONE:
Michal Vasko004d3152020-06-11 19:59:22 +02009300 rc = eval_path_expr(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009301 break;
9302 default:
9303 LOGINT_RET(set->ctx);
9304 }
9305
9306 return rc;
9307}
9308
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009309/**
9310 * @brief Get root type.
9311 *
9312 * @param[in] ctx_node Context node.
9313 * @param[in] ctx_scnode Schema context node.
9314 * @param[in] options XPath options.
9315 * @return Root type.
9316 */
9317static enum lyxp_node_type
Radek Krejci1deb5be2020-08-26 16:43:36 +02009318lyxp_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 +01009319{
Michal Vasko6b26e742020-07-17 15:02:10 +02009320 const struct lysc_node *op;
9321
Michal Vaskoa27245c2022-05-02 09:01:35 +02009322 /* explicit */
9323 if (options & LYXP_ACCESS_TREE_ALL) {
9324 return LYXP_NODE_ROOT;
9325 } else if (options & LYXP_ACCESS_TREE_CONFIG) {
9326 return LYXP_NODE_ROOT_CONFIG;
9327 }
9328
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009329 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009330 /* schema */
Radek Krejci1e008d22020-08-17 11:37:37 +02009331 for (op = ctx_scnode; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent) {}
Michal Vasko6b26e742020-07-17 15:02:10 +02009332
9333 if (op || (options & LYXP_SCNODE)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009334 /* general root that can access everything */
9335 return LYXP_NODE_ROOT;
9336 } else if (!ctx_scnode || (ctx_scnode->flags & LYS_CONFIG_W)) {
9337 /* root context node can access only config data (because we said so, it is unspecified) */
9338 return LYXP_NODE_ROOT_CONFIG;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009339 }
Michal Vasko6b26e742020-07-17 15:02:10 +02009340 return LYXP_NODE_ROOT;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009341 }
9342
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009343 /* data */
Michal Vasko6b26e742020-07-17 15:02:10 +02009344 op = ctx_node ? ctx_node->schema : NULL;
Michal Vaskod989ba02020-08-24 10:59:24 +02009345 for ( ; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent) {}
Michal Vasko6b26e742020-07-17 15:02:10 +02009346
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009347 if (op || !(options & LYXP_SCHEMA)) {
9348 /* general root that can access everything */
9349 return LYXP_NODE_ROOT;
Christian Hoppsb6ecaea2021-02-06 09:45:38 -05009350 } else if (!ctx_node || !ctx_node->schema || (ctx_node->schema->flags & LYS_CONFIG_W)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009351 /* root context node can access only config data (because we said so, it is unspecified) */
9352 return LYXP_NODE_ROOT_CONFIG;
9353 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009354 return LYXP_NODE_ROOT;
9355}
9356
Michal Vasko03ff5a72019-09-11 13:49:33 +02009357LY_ERR
Michal Vasko400e9672021-01-11 13:39:17 +01009358lyxp_eval(const struct ly_ctx *ctx, const struct lyxp_expr *exp, const struct lys_module *cur_mod,
Michal Vaskoa3e92bc2022-07-29 14:56:23 +02009359 LY_VALUE_FORMAT format, void *prefix_data, const struct lyd_node *cur_node, const struct lyd_node *ctx_node,
9360 const struct lyd_node *tree, const struct lyxp_var *vars, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02009361{
Michal Vasko004d3152020-06-11 19:59:22 +02009362 uint16_t tok_idx = 0;
Michal Vaskoddd76592022-01-17 13:34:48 +01009363 const struct lysc_node *snode;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009364 LY_ERR rc;
9365
Michal Vasko400e9672021-01-11 13:39:17 +01009366 LY_CHECK_ARG_RET(ctx, ctx, exp, set, LY_EINVAL);
Radek Krejci8df109d2021-04-23 12:19:08 +02009367 if (!cur_mod && ((format == LY_VALUE_SCHEMA) || (format == LY_VALUE_SCHEMA_RESOLVED))) {
Michal Vaskoddd76592022-01-17 13:34:48 +01009368 LOGERR(ctx, LY_EINVAL, "Current module must be set if schema format is used.");
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009369 return LY_EINVAL;
Michal Vasko004d3152020-06-11 19:59:22 +02009370 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02009371
Michal Vaskod3bb12f2020-12-04 14:33:09 +01009372 if (tree) {
9373 /* adjust the pointer to be the first top-level sibling */
9374 while (tree->parent) {
9375 tree = lyd_parent(tree);
9376 }
9377 tree = lyd_first_sibling(tree);
Michal Vaskoddd76592022-01-17 13:34:48 +01009378
9379 for (snode = tree->schema->parent; snode && (snode->nodetype & (LYS_CASE | LYS_CHOICE)); snode = snode->parent) {}
9380 if (snode) {
9381 /* unable to evaluate absolute paths */
9382 LOGERR(ctx, LY_EINVAL, "Data node \"%s\" has no parent but is not instance of a top-level schema node.",
9383 LYD_NAME(tree));
9384 return LY_EINVAL;
9385 }
Michal Vaskod3bb12f2020-12-04 14:33:09 +01009386 }
9387
Michal Vasko03ff5a72019-09-11 13:49:33 +02009388 /* prepare set for evaluation */
Michal Vasko03ff5a72019-09-11 13:49:33 +02009389 memset(set, 0, sizeof *set);
Michal Vaskod3678892020-05-21 10:06:58 +02009390 set->type = LYXP_SET_NODE_SET;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009391 set->root_type = lyxp_get_root_type(ctx_node, NULL, options);
9392 set_insert_node(set, (struct lyd_node *)ctx_node, 0, ctx_node ? LYXP_NODE_ELEM : set->root_type, 0);
9393
Michal Vasko400e9672021-01-11 13:39:17 +01009394 set->ctx = (struct ly_ctx *)ctx;
Michal Vaskoa3e92bc2022-07-29 14:56:23 +02009395 set->cur_node = cur_node;
9396 for (set->context_op = cur_node ? cur_node->schema : NULL;
Radek Krejci0f969882020-08-21 16:56:47 +02009397 set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF));
9398 set->context_op = set->context_op->parent) {}
Michal Vaskof03ed032020-03-04 13:31:44 +01009399 set->tree = tree;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009400 set->cur_mod = cur_mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009401 set->format = format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009402 set->prefix_data = prefix_data;
aPiecekfba75362021-10-07 12:39:48 +02009403 set->vars = vars;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009404
Radek Krejciddace2c2021-01-08 11:30:56 +01009405 LOG_LOCSET(NULL, set->cur_node, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01009406
Michal Vasko03ff5a72019-09-11 13:49:33 +02009407 /* evaluate */
Michal Vasko004d3152020-06-11 19:59:22 +02009408 rc = eval_expr_select(exp, &tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009409 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02009410 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009411 }
9412
Michal Vasko4a7d4d62021-12-13 17:05:06 +01009413 if (set->cur_node) {
9414 LOG_LOCBACK(0, 1, 0, 0);
9415 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02009416 return rc;
9417}
9418
9419#if 0
9420
9421/* full xml printing of set elements, not used currently */
9422
9423void
9424lyxp_set_print_xml(FILE *f, struct lyxp_set *set)
9425{
9426 uint32_t i;
9427 char *str_num;
9428 struct lyout out;
9429
9430 memset(&out, 0, sizeof out);
9431
9432 out.type = LYOUT_STREAM;
9433 out.method.f = f;
9434
9435 switch (set->type) {
9436 case LYXP_SET_EMPTY:
Michal Vasko5233e962020-08-14 14:26:20 +02009437 ly_print_(&out, "Empty XPath set\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02009438 break;
9439 case LYXP_SET_BOOLEAN:
Michal Vasko5233e962020-08-14 14:26:20 +02009440 ly_print_(&out, "Boolean XPath set:\n");
9441 ly_print_(&out, "%s\n\n", set->value.bool ? "true" : "false");
Michal Vasko03ff5a72019-09-11 13:49:33 +02009442 break;
9443 case LYXP_SET_STRING:
Michal Vasko5233e962020-08-14 14:26:20 +02009444 ly_print_(&out, "String XPath set:\n");
9445 ly_print_(&out, "\"%s\"\n\n", set->value.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009446 break;
9447 case LYXP_SET_NUMBER:
Michal Vasko5233e962020-08-14 14:26:20 +02009448 ly_print_(&out, "Number XPath set:\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02009449
9450 if (isnan(set->value.num)) {
9451 str_num = strdup("NaN");
9452 } else if ((set->value.num == 0) || (set->value.num == -0.0f)) {
9453 str_num = strdup("0");
9454 } else if (isinf(set->value.num) && !signbit(set->value.num)) {
9455 str_num = strdup("Infinity");
9456 } else if (isinf(set->value.num) && signbit(set->value.num)) {
9457 str_num = strdup("-Infinity");
9458 } else if ((long long)set->value.num == set->value.num) {
9459 if (asprintf(&str_num, "%lld", (long long)set->value.num) == -1) {
9460 str_num = NULL;
9461 }
9462 } else {
9463 if (asprintf(&str_num, "%03.1Lf", set->value.num) == -1) {
9464 str_num = NULL;
9465 }
9466 }
9467 if (!str_num) {
9468 LOGMEM;
9469 return;
9470 }
Michal Vasko5233e962020-08-14 14:26:20 +02009471 ly_print_(&out, "%s\n\n", str_num);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009472 free(str_num);
9473 break;
9474 case LYXP_SET_NODE_SET:
Michal Vasko5233e962020-08-14 14:26:20 +02009475 ly_print_(&out, "Node XPath set:\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02009476
9477 for (i = 0; i < set->used; ++i) {
Michal Vasko5233e962020-08-14 14:26:20 +02009478 ly_print_(&out, "%d. ", i + 1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009479 switch (set->node_type[i]) {
9480 case LYXP_NODE_ROOT_ALL:
Michal Vasko5233e962020-08-14 14:26:20 +02009481 ly_print_(&out, "ROOT all\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02009482 break;
9483 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko5233e962020-08-14 14:26:20 +02009484 ly_print_(&out, "ROOT config\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02009485 break;
9486 case LYXP_NODE_ROOT_STATE:
Michal Vasko5233e962020-08-14 14:26:20 +02009487 ly_print_(&out, "ROOT state\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02009488 break;
9489 case LYXP_NODE_ROOT_NOTIF:
Michal Vasko5233e962020-08-14 14:26:20 +02009490 ly_print_(&out, "ROOT notification \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009491 break;
9492 case LYXP_NODE_ROOT_RPC:
Michal Vasko5233e962020-08-14 14:26:20 +02009493 ly_print_(&out, "ROOT rpc \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009494 break;
9495 case LYXP_NODE_ROOT_OUTPUT:
Michal Vasko5233e962020-08-14 14:26:20 +02009496 ly_print_(&out, "ROOT output \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009497 break;
9498 case LYXP_NODE_ELEM:
Michal Vasko5233e962020-08-14 14:26:20 +02009499 ly_print_(&out, "ELEM \"%s\"\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009500 xml_print_node(&out, 1, set->value.nodes[i], 1, LYP_FORMAT);
Michal Vasko5233e962020-08-14 14:26:20 +02009501 ly_print_(&out, "\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02009502 break;
9503 case LYXP_NODE_TEXT:
Michal Vasko5233e962020-08-14 14:26:20 +02009504 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 +02009505 break;
9506 case LYXP_NODE_ATTR:
Michal Vasko5233e962020-08-14 14:26:20 +02009507 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 +02009508 break;
9509 }
9510 }
9511 break;
9512 }
9513}
9514
9515#endif
9516
9517LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009518lyxp_set_cast(struct lyxp_set *set, enum lyxp_set_type target)
Michal Vasko03ff5a72019-09-11 13:49:33 +02009519{
9520 long double num;
9521 char *str;
9522 LY_ERR rc;
9523
9524 if (!set || (set->type == target)) {
9525 return LY_SUCCESS;
9526 }
9527
9528 /* it's not possible to convert anything into a node set */
Michal Vaskod3678892020-05-21 10:06:58 +02009529 assert(target != LYXP_SET_NODE_SET);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009530
9531 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vaskod3678892020-05-21 10:06:58 +02009532 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009533 return LY_EINVAL;
9534 }
9535
9536 /* to STRING */
Michal Vaskod3678892020-05-21 10:06:58 +02009537 if ((target == LYXP_SET_STRING) || ((target == LYXP_SET_NUMBER) && (set->type == LYXP_SET_NODE_SET))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02009538 switch (set->type) {
9539 case LYXP_SET_NUMBER:
9540 if (isnan(set->val.num)) {
9541 set->val.str = strdup("NaN");
9542 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
9543 } else if ((set->val.num == 0) || (set->val.num == -0.0f)) {
9544 set->val.str = strdup("0");
9545 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
9546 } else if (isinf(set->val.num) && !signbit(set->val.num)) {
9547 set->val.str = strdup("Infinity");
9548 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
9549 } else if (isinf(set->val.num) && signbit(set->val.num)) {
9550 set->val.str = strdup("-Infinity");
9551 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
9552 } else if ((long long)set->val.num == set->val.num) {
9553 if (asprintf(&str, "%lld", (long long)set->val.num) == -1) {
9554 LOGMEM_RET(set->ctx);
9555 }
9556 set->val.str = str;
9557 } else {
9558 if (asprintf(&str, "%03.1Lf", set->val.num) == -1) {
9559 LOGMEM_RET(set->ctx);
9560 }
9561 set->val.str = str;
9562 }
9563 break;
9564 case LYXP_SET_BOOLEAN:
Michal Vasko004d3152020-06-11 19:59:22 +02009565 if (set->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02009566 set->val.str = strdup("true");
9567 } else {
9568 set->val.str = strdup("false");
9569 }
9570 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), LY_EMEM);
9571 break;
9572 case LYXP_SET_NODE_SET:
Michal Vasko03ff5a72019-09-11 13:49:33 +02009573 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009574 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02009575
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009576 rc = cast_node_set_to_string(set, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009577 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02009578 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009579 set->val.str = str;
9580 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009581 default:
9582 LOGINT_RET(set->ctx);
9583 }
9584 set->type = LYXP_SET_STRING;
9585 }
9586
9587 /* to NUMBER */
9588 if (target == LYXP_SET_NUMBER) {
9589 switch (set->type) {
9590 case LYXP_SET_STRING:
9591 num = cast_string_to_number(set->val.str);
Michal Vaskod3678892020-05-21 10:06:58 +02009592 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009593 set->val.num = num;
9594 break;
9595 case LYXP_SET_BOOLEAN:
Michal Vasko004d3152020-06-11 19:59:22 +02009596 if (set->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02009597 set->val.num = 1;
9598 } else {
9599 set->val.num = 0;
9600 }
9601 break;
9602 default:
9603 LOGINT_RET(set->ctx);
9604 }
9605 set->type = LYXP_SET_NUMBER;
9606 }
9607
9608 /* to BOOLEAN */
9609 if (target == LYXP_SET_BOOLEAN) {
9610 switch (set->type) {
9611 case LYXP_SET_NUMBER:
9612 if ((set->val.num == 0) || (set->val.num == -0.0f) || isnan(set->val.num)) {
Michal Vasko004d3152020-06-11 19:59:22 +02009613 set->val.bln = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009614 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02009615 set->val.bln = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009616 }
9617 break;
9618 case LYXP_SET_STRING:
9619 if (set->val.str[0]) {
Michal Vaskod3678892020-05-21 10:06:58 +02009620 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02009621 set->val.bln = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009622 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02009623 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02009624 set->val.bln = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009625 }
9626 break;
9627 case LYXP_SET_NODE_SET:
Michal Vaskod3678892020-05-21 10:06:58 +02009628 if (set->used) {
9629 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02009630 set->val.bln = 1;
Michal Vaskod3678892020-05-21 10:06:58 +02009631 } else {
9632 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02009633 set->val.bln = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02009634 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02009635 break;
9636 default:
9637 LOGINT_RET(set->ctx);
9638 }
9639 set->type = LYXP_SET_BOOLEAN;
9640 }
9641
Michal Vasko03ff5a72019-09-11 13:49:33 +02009642 return LY_SUCCESS;
9643}
9644
9645LY_ERR
Michal Vasko400e9672021-01-11 13:39:17 +01009646lyxp_atomize(const struct ly_ctx *ctx, const struct lyxp_expr *exp, const struct lys_module *cur_mod,
Michal Vaskoa3e92bc2022-07-29 14:56:23 +02009647 LY_VALUE_FORMAT format, void *prefix_data, const struct lysc_node *cur_scnode,
9648 const struct lysc_node *ctx_scnode, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02009649{
Radek Krejci2efc45b2020-12-22 16:25:44 +01009650 LY_ERR ret;
Michal Vasko004d3152020-06-11 19:59:22 +02009651 uint16_t tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009652
Michal Vasko400e9672021-01-11 13:39:17 +01009653 LY_CHECK_ARG_RET(ctx, ctx, exp, set, LY_EINVAL);
Radek Krejci8df109d2021-04-23 12:19:08 +02009654 if (!cur_mod && ((format == LY_VALUE_SCHEMA) || (format == LY_VALUE_SCHEMA_RESOLVED))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009655 LOGARG(NULL, "Current module must be set if schema format is used.");
9656 return LY_EINVAL;
Michal Vasko004d3152020-06-11 19:59:22 +02009657 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02009658
9659 /* prepare set for evaluation */
Michal Vasko03ff5a72019-09-11 13:49:33 +02009660 memset(set, 0, sizeof *set);
9661 set->type = LYXP_SET_SCNODE_SET;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009662 set->root_type = lyxp_get_root_type(NULL, ctx_scnode, options);
9663 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, ctx_scnode, ctx_scnode ? LYXP_NODE_ELEM : set->root_type, NULL));
Radek Krejcif13b87b2020-12-01 22:02:17 +01009664 set->val.scnodes[0].in_ctx = LYXP_SET_SCNODE_START;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009665
Michal Vasko400e9672021-01-11 13:39:17 +01009666 set->ctx = (struct ly_ctx *)ctx;
Michal Vaskoa3e92bc2022-07-29 14:56:23 +02009667 set->cur_scnode = cur_scnode;
9668 for (set->context_op = cur_scnode;
Radek Krejci0f969882020-08-21 16:56:47 +02009669 set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF));
9670 set->context_op = set->context_op->parent) {}
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009671 set->cur_mod = cur_mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009672 set->format = format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009673 set->prefix_data = prefix_data;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009674
Radek Krejciddace2c2021-01-08 11:30:56 +01009675 LOG_LOCSET(set->cur_scnode, NULL, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01009676
Michal Vasko03ff5a72019-09-11 13:49:33 +02009677 /* evaluate */
Radek Krejci2efc45b2020-12-22 16:25:44 +01009678 ret = eval_expr_select(exp, &tok_idx, 0, set, options);
9679
Radek Krejciddace2c2021-01-08 11:30:56 +01009680 LOG_LOCBACK(1, 0, 0, 0);
Radek Krejci2efc45b2020-12-22 16:25:44 +01009681 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009682}
Michal Vaskod43d71a2020-08-07 14:54:58 +02009683
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01009684LIBYANG_API_DEF const char *
Michal Vaskod43d71a2020-08-07 14:54:58 +02009685lyxp_get_expr(const struct lyxp_expr *path)
9686{
9687 if (!path) {
9688 return NULL;
9689 }
9690
9691 return path->expr;
9692}