blob: 25982e209c3d5cd1e06f82987a8204c34c274ad1 [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
3172lyxp_expr_dup(const struct ly_ctx *ctx, const struct lyxp_expr *exp, struct lyxp_expr **dup_p)
Michal Vasko004d3152020-06-11 19:59:22 +02003173{
Michal Vasko1734be92020-09-22 08:55:10 +02003174 LY_ERR ret = LY_SUCCESS;
3175 struct lyxp_expr *dup = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02003176 uint32_t i, j;
3177
Michal Vasko7f45cf22020-10-01 12:49:44 +02003178 if (!exp) {
3179 goto cleanup;
3180 }
3181
Michal Vasko004d3152020-06-11 19:59:22 +02003182 dup = calloc(1, sizeof *dup);
Michal Vasko1734be92020-09-22 08:55:10 +02003183 LY_CHECK_ERR_GOTO(!dup, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02003184
Michal Vasko08e9b112021-06-11 15:41:17 +02003185 if (exp->used) {
3186 dup->tokens = malloc(exp->used * sizeof *dup->tokens);
3187 LY_CHECK_ERR_GOTO(!dup->tokens, LOGMEM(ctx); ret = LY_EMEM, cleanup);
3188 memcpy(dup->tokens, exp->tokens, exp->used * sizeof *dup->tokens);
Michal Vasko004d3152020-06-11 19:59:22 +02003189
Michal Vasko08e9b112021-06-11 15:41:17 +02003190 dup->tok_pos = malloc(exp->used * sizeof *dup->tok_pos);
3191 LY_CHECK_ERR_GOTO(!dup->tok_pos, LOGMEM(ctx); ret = LY_EMEM, cleanup);
3192 memcpy(dup->tok_pos, exp->tok_pos, exp->used * sizeof *dup->tok_pos);
Michal Vasko004d3152020-06-11 19:59:22 +02003193
Michal Vasko08e9b112021-06-11 15:41:17 +02003194 dup->tok_len = malloc(exp->used * sizeof *dup->tok_len);
3195 LY_CHECK_ERR_GOTO(!dup->tok_len, LOGMEM(ctx); ret = LY_EMEM, cleanup);
3196 memcpy(dup->tok_len, exp->tok_len, exp->used * sizeof *dup->tok_len);
Michal Vasko004d3152020-06-11 19:59:22 +02003197
Michal Vasko79a7a872022-06-17 09:00:48 +02003198 if (exp->repeat) {
3199 dup->repeat = malloc(exp->used * sizeof *dup->repeat);
3200 LY_CHECK_ERR_GOTO(!dup->repeat, LOGMEM(ctx); ret = LY_EMEM, cleanup);
3201 for (i = 0; i < exp->used; ++i) {
3202 if (!exp->repeat[i]) {
3203 dup->repeat[i] = NULL;
3204 } else {
3205 for (j = 0; exp->repeat[i][j]; ++j) {}
3206 /* the ending 0 as well */
3207 ++j;
Michal Vasko004d3152020-06-11 19:59:22 +02003208
Michal Vasko79a7a872022-06-17 09:00:48 +02003209 dup->repeat[i] = malloc(j * sizeof **dup->repeat);
3210 LY_CHECK_ERR_GOTO(!dup->repeat[i], LOGMEM(ctx); ret = LY_EMEM, cleanup);
3211 memcpy(dup->repeat[i], exp->repeat[i], j * sizeof **dup->repeat);
3212 dup->repeat[i][j - 1] = 0;
3213 }
Michal Vasko08e9b112021-06-11 15:41:17 +02003214 }
Michal Vasko004d3152020-06-11 19:59:22 +02003215 }
3216 }
3217
3218 dup->used = exp->used;
3219 dup->size = exp->used;
Michal Vasko1734be92020-09-22 08:55:10 +02003220 LY_CHECK_GOTO(ret = lydict_insert(ctx, exp->expr, 0, &dup->expr), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02003221
Michal Vasko1734be92020-09-22 08:55:10 +02003222cleanup:
3223 if (ret) {
3224 lyxp_expr_free(ctx, dup);
3225 } else {
3226 *dup_p = dup;
3227 }
3228 return ret;
Michal Vasko004d3152020-06-11 19:59:22 +02003229}
3230
Michal Vasko03ff5a72019-09-11 13:49:33 +02003231/**
3232 * @brief Get the last-added schema node that is currently in the context.
3233 *
3234 * @param[in] set Set to search in.
3235 * @return Last-added schema context node, NULL if no node is in context.
3236 */
3237static struct lysc_node *
3238warn_get_scnode_in_ctx(struct lyxp_set *set)
3239{
3240 uint32_t i;
3241
3242 if (!set || (set->type != LYXP_SET_SCNODE_SET)) {
3243 return NULL;
3244 }
3245
3246 i = set->used;
3247 do {
3248 --i;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003249 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003250 /* if there are more, simply return the first found (last added) */
3251 return set->val.scnodes[i].scnode;
3252 }
3253 } while (i);
3254
3255 return NULL;
3256}
3257
3258/**
3259 * @brief Test whether a type is numeric - integer type or decimal64.
3260 *
3261 * @param[in] type Type to test.
Radek Krejci857189e2020-09-01 13:26:36 +02003262 * @return Boolean value whether @p type is numeric type or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003263 */
Radek Krejci857189e2020-09-01 13:26:36 +02003264static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003265warn_is_numeric_type(struct lysc_type *type)
3266{
3267 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003268 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003269 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003270
3271 switch (type->basetype) {
3272 case LY_TYPE_DEC64:
3273 case LY_TYPE_INT8:
3274 case LY_TYPE_UINT8:
3275 case LY_TYPE_INT16:
3276 case LY_TYPE_UINT16:
3277 case LY_TYPE_INT32:
3278 case LY_TYPE_UINT32:
3279 case LY_TYPE_INT64:
3280 case LY_TYPE_UINT64:
3281 return 1;
3282 case LY_TYPE_UNION:
3283 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003284 LY_ARRAY_FOR(uni->types, u) {
3285 ret = warn_is_numeric_type(uni->types[u]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003286 if (ret) {
3287 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003288 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003289 }
3290 }
3291 /* did not find any suitable type */
3292 return 0;
3293 case LY_TYPE_LEAFREF:
3294 return warn_is_numeric_type(((struct lysc_type_leafref *)type)->realtype);
3295 default:
3296 return 0;
3297 }
3298}
3299
3300/**
3301 * @brief Test whether a type is string-like - no integers, decimal64 or binary.
3302 *
3303 * @param[in] type Type to test.
Radek Krejci857189e2020-09-01 13:26:36 +02003304 * @return Boolean value whether @p type's basetype is string type or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003305 */
Radek Krejci857189e2020-09-01 13:26:36 +02003306static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003307warn_is_string_type(struct lysc_type *type)
3308{
3309 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003310 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003311 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003312
3313 switch (type->basetype) {
3314 case LY_TYPE_BITS:
3315 case LY_TYPE_ENUM:
3316 case LY_TYPE_IDENT:
3317 case LY_TYPE_INST:
3318 case LY_TYPE_STRING:
3319 return 1;
3320 case LY_TYPE_UNION:
3321 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003322 LY_ARRAY_FOR(uni->types, u) {
3323 ret = warn_is_string_type(uni->types[u]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003324 if (ret) {
3325 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003326 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003327 }
3328 }
3329 /* did not find any suitable type */
3330 return 0;
3331 case LY_TYPE_LEAFREF:
3332 return warn_is_string_type(((struct lysc_type_leafref *)type)->realtype);
3333 default:
3334 return 0;
3335 }
3336}
3337
3338/**
3339 * @brief Test whether a type is one specific type.
3340 *
3341 * @param[in] type Type to test.
3342 * @param[in] base Expected type.
Radek Krejci857189e2020-09-01 13:26:36 +02003343 * @return Boolean value whether the given @p type is of the specific basetype @p base.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003344 */
Radek Krejci857189e2020-09-01 13:26:36 +02003345static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003346warn_is_specific_type(struct lysc_type *type, LY_DATA_TYPE base)
3347{
3348 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003349 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003350 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003351
3352 if (type->basetype == base) {
3353 return 1;
3354 } else if (type->basetype == LY_TYPE_UNION) {
3355 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003356 LY_ARRAY_FOR(uni->types, u) {
3357 ret = warn_is_specific_type(uni->types[u], base);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003358 if (ret) {
3359 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003360 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003361 }
3362 }
3363 /* did not find any suitable type */
3364 return 0;
3365 } else if (type->basetype == LY_TYPE_LEAFREF) {
3366 return warn_is_specific_type(((struct lysc_type_leafref *)type)->realtype, base);
3367 }
3368
3369 return 0;
3370}
3371
3372/**
3373 * @brief Get next type of a (union) type.
3374 *
3375 * @param[in] type Base type.
3376 * @param[in] prev_type Previously returned type.
3377 * @return Next type or NULL.
3378 */
3379static struct lysc_type *
3380warn_is_equal_type_next_type(struct lysc_type *type, struct lysc_type *prev_type)
3381{
3382 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003383 ly_bool found = 0;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003384 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003385
3386 switch (type->basetype) {
3387 case LY_TYPE_UNION:
3388 uni = (struct lysc_type_union *)type;
3389 if (!prev_type) {
3390 return uni->types[0];
3391 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003392 LY_ARRAY_FOR(uni->types, u) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003393 if (found) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003394 return uni->types[u];
Michal Vasko03ff5a72019-09-11 13:49:33 +02003395 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003396 if (prev_type == uni->types[u]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003397 found = 1;
3398 }
3399 }
3400 return NULL;
3401 default:
3402 if (prev_type) {
3403 assert(type == prev_type);
3404 return NULL;
3405 } else {
3406 return type;
3407 }
3408 }
3409}
3410
3411/**
3412 * @brief Test whether 2 types have a common type.
3413 *
3414 * @param[in] type1 First type.
3415 * @param[in] type2 Second type.
3416 * @return 1 if they do, 0 otherwise.
3417 */
3418static int
3419warn_is_equal_type(struct lysc_type *type1, struct lysc_type *type2)
3420{
3421 struct lysc_type *t1, *rt1, *t2, *rt2;
3422
3423 t1 = NULL;
3424 while ((t1 = warn_is_equal_type_next_type(type1, t1))) {
3425 if (t1->basetype == LY_TYPE_LEAFREF) {
3426 rt1 = ((struct lysc_type_leafref *)t1)->realtype;
3427 } else {
3428 rt1 = t1;
3429 }
3430
3431 t2 = NULL;
3432 while ((t2 = warn_is_equal_type_next_type(type2, t2))) {
3433 if (t2->basetype == LY_TYPE_LEAFREF) {
3434 rt2 = ((struct lysc_type_leafref *)t2)->realtype;
3435 } else {
3436 rt2 = t2;
3437 }
3438
3439 if (rt2->basetype == rt1->basetype) {
3440 /* match found */
3441 return 1;
3442 }
3443 }
3444 }
3445
3446 return 0;
3447}
3448
3449/**
Michal Vaskoaa956522021-11-11 10:45:34 +01003450 * @brief Print warning with information about the XPath subexpression that caused previous warning.
3451 *
3452 * @param[in] ctx Context for logging.
3453 * @param[in] tok_pos Index of the subexpression in the whole expression.
3454 * @param[in] subexpr Subexpression start.
3455 * @param[in] subexpr_len Length of @p subexpr to print.
3456 * @param[in] cur_scnode Expression context node.
3457 */
3458static void
3459warn_subexpr_log(const struct ly_ctx *ctx, uint16_t tok_pos, const char *subexpr, int subexpr_len,
3460 const struct lysc_node *cur_scnode)
3461{
3462 char *path;
3463
3464 path = lysc_path(cur_scnode, LYSC_PATH_LOG, NULL, 0);
3465 LOGWRN(ctx, "Previous warning generated by XPath subexpression[%" PRIu16 "] \"%.*s\" with context node \"%s\".",
3466 tok_pos, subexpr_len, subexpr, path);
3467 free(path);
3468}
3469
3470/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02003471 * @brief Check both operands of comparison operators.
3472 *
3473 * @param[in] ctx Context for errors.
3474 * @param[in] set1 First operand set.
3475 * @param[in] set2 Second operand set.
3476 * @param[in] numbers_only Whether accept only numbers or other types are fine too (for '=' and '!=').
3477 * @param[in] expr Start of the expression to print with the warning.
3478 * @param[in] tok_pos Token position.
3479 */
3480static void
Michal Vaskoaa956522021-11-11 10:45:34 +01003481warn_operands(struct ly_ctx *ctx, struct lyxp_set *set1, struct lyxp_set *set2, ly_bool numbers_only, const char *expr,
3482 uint16_t tok_pos)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003483{
3484 struct lysc_node_leaf *node1, *node2;
Radek Krejci857189e2020-09-01 13:26:36 +02003485 ly_bool leaves = 1, warning = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003486
3487 node1 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set1);
3488 node2 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set2);
3489
3490 if (!node1 && !node2) {
3491 /* no node-sets involved, nothing to do */
3492 return;
3493 }
3494
3495 if (node1) {
3496 if (!(node1->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3497 LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node1->nodetype), node1->name);
3498 warning = 1;
3499 leaves = 0;
3500 } else if (numbers_only && !warn_is_numeric_type(node1->type)) {
3501 LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node1->name);
3502 warning = 1;
3503 }
3504 }
3505
3506 if (node2) {
3507 if (!(node2->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3508 LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node2->nodetype), node2->name);
3509 warning = 1;
3510 leaves = 0;
3511 } else if (numbers_only && !warn_is_numeric_type(node2->type)) {
3512 LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node2->name);
3513 warning = 1;
3514 }
3515 }
3516
3517 if (node1 && node2 && leaves && !numbers_only) {
Michal Vasko69730152020-10-09 16:30:07 +02003518 if ((warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type)) ||
3519 (!warn_is_numeric_type(node1->type) && warn_is_numeric_type(node2->type)) ||
3520 (!warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type) &&
3521 !warn_is_equal_type(node1->type, node2->type))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003522 LOGWRN(ctx, "Incompatible types of operands \"%s\" and \"%s\" for comparison.", node1->name, node2->name);
3523 warning = 1;
3524 }
3525 }
3526
3527 if (warning) {
Michal Vaskoaa956522021-11-11 10:45:34 +01003528 warn_subexpr_log(ctx, tok_pos, expr + tok_pos, 20, set1->cur_scnode);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003529 }
3530}
3531
3532/**
3533 * @brief Check that a value is valid for a leaf. If not applicable, does nothing.
3534 *
3535 * @param[in] exp Parsed XPath expression.
3536 * @param[in] set Set with the leaf/leaf-list.
3537 * @param[in] val_exp Index of the value (literal/number) in @p exp.
3538 * @param[in] equal_exp Index of the start of the equality expression in @p exp.
3539 * @param[in] last_equal_exp Index of the end of the equality expression in @p exp.
3540 */
3541static void
Michal Vasko40308e72020-10-20 16:38:40 +02003542warn_equality_value(const struct lyxp_expr *exp, struct lyxp_set *set, uint16_t val_exp, uint16_t equal_exp,
3543 uint16_t last_equal_exp)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003544{
3545 struct lysc_node *scnode;
3546 struct lysc_type *type;
3547 char *value;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003548 struct lyd_value storage;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003549 LY_ERR rc;
3550 struct ly_err_item *err = NULL;
3551
Michal Vasko69730152020-10-09 16:30:07 +02003552 if ((scnode = warn_get_scnode_in_ctx(set)) && (scnode->nodetype & (LYS_LEAF | LYS_LEAFLIST)) &&
3553 ((exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) || (exp->tokens[val_exp] == LYXP_TOKEN_NUMBER))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003554 /* check that the node can have the specified value */
3555 if (exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) {
3556 value = strndup(exp->expr + exp->tok_pos[val_exp] + 1, exp->tok_len[val_exp] - 2);
3557 } else {
3558 value = strndup(exp->expr + exp->tok_pos[val_exp], exp->tok_len[val_exp]);
3559 }
3560 if (!value) {
3561 LOGMEM(set->ctx);
3562 return;
3563 }
3564
3565 if ((((struct lysc_node_leaf *)scnode)->type->basetype == LY_TYPE_IDENT) && !strchr(value, ':')) {
3566 LOGWRN(set->ctx, "Identityref \"%s\" comparison with identity \"%s\" without prefix, consider adding"
Michal Vasko69730152020-10-09 16:30:07 +02003567 " a prefix or best using \"derived-from(-or-self)()\" functions.", scnode->name, value);
Michal Vaskoaa956522021-11-11 10:45:34 +01003568 warn_subexpr_log(set->ctx, exp->tok_pos[equal_exp], exp->expr + exp->tok_pos[equal_exp],
Radek Krejci0f969882020-08-21 16:56:47 +02003569 (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp],
Michal Vaskoaa956522021-11-11 10:45:34 +01003570 set->cur_scnode);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003571 }
3572
3573 type = ((struct lysc_node_leaf *)scnode)->type;
3574 if (type->basetype != LY_TYPE_IDENT) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003575 rc = type->plugin->store(set->ctx, type, value, strlen(value), 0, set->format, set->prefix_data,
Michal Vasko405cc9e2020-12-01 12:01:27 +01003576 LYD_HINT_DATA, scnode, &storage, NULL, &err);
Michal Vaskobf42e832020-11-23 16:59:42 +01003577 if (rc == LY_EINCOMPLETE) {
3578 rc = LY_SUCCESS;
3579 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003580
3581 if (err) {
3582 LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type (%s).", value, err->msg);
3583 ly_err_free(err);
3584 } else if (rc != LY_SUCCESS) {
3585 LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type.", value);
3586 }
3587 if (rc != LY_SUCCESS) {
Michal Vaskoaa956522021-11-11 10:45:34 +01003588 warn_subexpr_log(set->ctx, exp->tok_pos[equal_exp], exp->expr + exp->tok_pos[equal_exp],
Radek Krejci0f969882020-08-21 16:56:47 +02003589 (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp],
Michal Vaskoaa956522021-11-11 10:45:34 +01003590 set->cur_scnode);
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003591 } else {
3592 type->plugin->free(set->ctx, &storage);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003593 }
3594 }
3595 free(value);
3596 }
3597}
3598
3599/*
3600 * XPath functions
3601 */
3602
3603/**
3604 * @brief Execute the YANG 1.1 bit-is-set(node-set, string) function. Returns LYXP_SET_BOOLEAN
3605 * depending on whether the first node bit value from the second argument is set.
3606 *
3607 * @param[in] args Array of arguments.
3608 * @param[in] arg_count Count of elements in @p args.
3609 * @param[in,out] set Context and result set at the same time.
3610 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003611 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003612 */
3613static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003614xpath_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 +02003615{
3616 struct lyd_node_term *leaf;
3617 struct lysc_node_leaf *sleaf;
Michal Vasko2588b952021-07-29 07:43:26 +02003618 struct lyd_value_bits *bits;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003619 LY_ERR rc = LY_SUCCESS;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003620 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003621
3622 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003623 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003624 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003625 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3626 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3627 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3628 sleaf->name);
3629 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_BITS)) {
3630 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"bits\".", __func__, sleaf->name);
3631 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003632 }
3633
3634 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3635 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003636 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3637 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003638 } else if (!warn_is_string_type(sleaf->type)) {
3639 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003640 }
3641 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003642 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003643 return rc;
3644 }
3645
Michal Vaskod3678892020-05-21 10:06:58 +02003646 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003647 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 +02003648 return LY_EVALID;
3649 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003650 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003651 LY_CHECK_RET(rc);
3652
3653 set_fill_boolean(set, 0);
Michal Vaskod3678892020-05-21 10:06:58 +02003654 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003655 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
Michal Vasko2588b952021-07-29 07:43:26 +02003656 if ((leaf->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (leaf->value.realtype->basetype == LY_TYPE_BITS)) {
3657 LYD_VALUE_GET(&leaf->value, bits);
3658 LY_ARRAY_FOR(bits->items, u) {
3659 if (!strcmp(bits->items[u]->name, args[1]->val.str)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003660 set_fill_boolean(set, 1);
3661 break;
3662 }
3663 }
3664 }
3665 }
3666
3667 return LY_SUCCESS;
3668}
3669
3670/**
3671 * @brief Execute the XPath boolean(object) function. Returns LYXP_SET_BOOLEAN
3672 * with the argument converted to boolean.
3673 *
3674 * @param[in] args Array of arguments.
3675 * @param[in] arg_count Count of elements in @p args.
3676 * @param[in,out] set Context and result set at the same time.
3677 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003678 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003679 */
3680static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003681xpath_boolean(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003682{
3683 LY_ERR rc;
3684
3685 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02003686 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003687 return LY_SUCCESS;
3688 }
3689
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003690 rc = lyxp_set_cast(args[0], LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003691 LY_CHECK_RET(rc);
3692 set_fill_set(set, args[0]);
3693
3694 return LY_SUCCESS;
3695}
3696
3697/**
3698 * @brief Execute the XPath ceiling(number) function. Returns LYXP_SET_NUMBER
3699 * with the first argument rounded up to the nearest integer.
3700 *
3701 * @param[in] args Array of arguments.
3702 * @param[in] arg_count Count of elements in @p args.
3703 * @param[in,out] set Context and result set at the same time.
3704 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003705 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003706 */
3707static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003708xpath_ceiling(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003709{
3710 struct lysc_node_leaf *sleaf;
3711 LY_ERR rc = LY_SUCCESS;
3712
3713 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003714 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003715 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003716 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3717 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3718 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3719 sleaf->name);
3720 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
3721 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
3722 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003723 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003724 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003725 return rc;
3726 }
3727
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003728 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003729 LY_CHECK_RET(rc);
3730 if ((long long)args[0]->val.num != args[0]->val.num) {
3731 set_fill_number(set, ((long long)args[0]->val.num) + 1);
3732 } else {
3733 set_fill_number(set, args[0]->val.num);
3734 }
3735
3736 return LY_SUCCESS;
3737}
3738
3739/**
3740 * @brief Execute the XPath concat(string, string, string*) function.
3741 * Returns LYXP_SET_STRING with the concatenation of all the arguments.
3742 *
3743 * @param[in] args Array of arguments.
3744 * @param[in] arg_count Count of elements in @p args.
3745 * @param[in,out] set Context and result set at the same time.
3746 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003747 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003748 */
3749static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003750xpath_concat(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003751{
3752 uint16_t i;
3753 char *str = NULL;
3754 size_t used = 1;
3755 LY_ERR rc = LY_SUCCESS;
3756 struct lysc_node_leaf *sleaf;
3757
3758 if (options & LYXP_SCNODE_ALL) {
3759 for (i = 0; i < arg_count; ++i) {
3760 if ((args[i]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[i]))) {
3761 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3762 LOGWRN(set->ctx, "Argument #%u of %s is a %s node \"%s\".",
Michal Vasko69730152020-10-09 16:30:07 +02003763 i + 1, __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003764 } else if (!warn_is_string_type(sleaf->type)) {
Radek Krejci70124c82020-08-14 22:17:03 +02003765 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 +02003766 }
3767 }
3768 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003769 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003770 return rc;
3771 }
3772
3773 for (i = 0; i < arg_count; ++i) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003774 rc = lyxp_set_cast(args[i], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003775 if (rc != LY_SUCCESS) {
3776 free(str);
3777 return rc;
3778 }
3779
3780 str = ly_realloc(str, (used + strlen(args[i]->val.str)) * sizeof(char));
3781 LY_CHECK_ERR_RET(!str, LOGMEM(set->ctx), LY_EMEM);
3782 strcpy(str + used - 1, args[i]->val.str);
3783 used += strlen(args[i]->val.str);
3784 }
3785
3786 /* free, kind of */
Michal Vaskod3678892020-05-21 10:06:58 +02003787 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003788 set->type = LYXP_SET_STRING;
3789 set->val.str = str;
3790
3791 return LY_SUCCESS;
3792}
3793
3794/**
3795 * @brief Execute the XPath contains(string, string) function.
3796 * Returns LYXP_SET_BOOLEAN whether the second argument can
3797 * be found in the first or not.
3798 *
3799 * @param[in] args Array of arguments.
3800 * @param[in] arg_count Count of elements in @p args.
3801 * @param[in,out] set Context and result set at the same time.
3802 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003803 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003804 */
3805static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003806xpath_contains(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003807{
3808 struct lysc_node_leaf *sleaf;
3809 LY_ERR rc = LY_SUCCESS;
3810
3811 if (options & LYXP_SCNODE_ALL) {
3812 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3813 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003814 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3815 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003816 } else if (!warn_is_string_type(sleaf->type)) {
3817 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003818 }
3819 }
3820
3821 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3822 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003823 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3824 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003825 } else if (!warn_is_string_type(sleaf->type)) {
3826 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003827 }
3828 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003829 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003830 return rc;
3831 }
3832
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003833 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003834 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003835 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003836 LY_CHECK_RET(rc);
3837
3838 if (strstr(args[0]->val.str, args[1]->val.str)) {
3839 set_fill_boolean(set, 1);
3840 } else {
3841 set_fill_boolean(set, 0);
3842 }
3843
3844 return LY_SUCCESS;
3845}
3846
3847/**
3848 * @brief Execute the XPath count(node-set) function. Returns LYXP_SET_NUMBER
3849 * with the size of the node-set from the argument.
3850 *
3851 * @param[in] args Array of arguments.
3852 * @param[in] arg_count Count of elements in @p args.
3853 * @param[in,out] set Context and result set at the same time.
3854 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003855 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003856 */
3857static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003858xpath_count(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003859{
Michal Vasko03ff5a72019-09-11 13:49:33 +02003860 LY_ERR rc = LY_SUCCESS;
3861
3862 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003863 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003864 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003865 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003866 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003867 return rc;
3868 }
3869
Michal Vasko03ff5a72019-09-11 13:49:33 +02003870 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003871 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "count(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003872 return LY_EVALID;
3873 }
3874
3875 set_fill_number(set, args[0]->used);
3876 return LY_SUCCESS;
3877}
3878
3879/**
3880 * @brief Execute the XPath current() function. Returns LYXP_SET_NODE_SET
3881 * with the context with the intial node.
3882 *
3883 * @param[in] args Array of arguments.
3884 * @param[in] arg_count Count of elements in @p args.
3885 * @param[in,out] set Context and result set at the same time.
3886 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003887 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003888 */
3889static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003890xpath_current(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003891{
3892 if (arg_count || args) {
Radek Krejcie87c7dc2021-06-02 21:25:42 +02003893 LOGVAL(set->ctx, LY_VCODE_XP_INARGCOUNT, arg_count, LY_PRI_LENSTR("current()"));
Michal Vasko03ff5a72019-09-11 13:49:33 +02003894 return LY_EVALID;
3895 }
3896
3897 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02003898 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003899
Michal Vasko296dfaf2021-12-13 16:57:42 +01003900 if (set->cur_scnode) {
3901 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, set->cur_scnode, LYXP_NODE_ELEM, NULL));
3902 } else {
3903 /* root node */
3904 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, NULL, set->root_type, NULL));
3905 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003906 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02003907 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003908
Michal Vasko296dfaf2021-12-13 16:57:42 +01003909 if (set->cur_node) {
3910 /* position is filled later */
3911 set_insert_node(set, set->cur_node, 0, LYXP_NODE_ELEM, 0);
3912 } else {
3913 /* root node */
3914 set_insert_node(set, NULL, 0, set->root_type, 0);
3915 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003916 }
3917
3918 return LY_SUCCESS;
3919}
3920
3921/**
3922 * @brief Execute the YANG 1.1 deref(node-set) function. Returns LYXP_SET_NODE_SET with either
3923 * leafref or instance-identifier target node(s).
3924 *
3925 * @param[in] args Array of arguments.
3926 * @param[in] arg_count Count of elements in @p args.
3927 * @param[in,out] set Context and result set at the same time.
3928 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003929 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003930 */
3931static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003932xpath_deref(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003933{
3934 struct lyd_node_term *leaf;
Michal Vasko42e497c2020-01-06 08:38:25 +01003935 struct lysc_node_leaf *sleaf = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02003936 struct lysc_type_leafref *lref;
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003937 const struct lysc_node *target;
Michal Vasko004d3152020-06-11 19:59:22 +02003938 struct ly_path *p;
3939 struct lyd_node *node;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003940 char *errmsg = NULL;
Michal Vasko00cbf532020-06-15 13:58:47 +02003941 uint8_t oper;
Michal Vasko741bb562021-06-24 11:59:50 +02003942 LY_ERR r;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003943
3944 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003945 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003946 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003947 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
Michal Vasko423ba3f2021-07-19 13:08:50 +02003948 if (!(sleaf->nodetype & LYD_NODE_TERM)) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003949 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3950 sleaf->name);
Michal Vaskoed725d72021-06-23 12:03:45 +02003951 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_LEAFREF) &&
3952 !warn_is_specific_type(sleaf->type, LY_TYPE_INST)) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003953 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"leafref\" nor \"instance-identifier\".",
3954 __func__, sleaf->name);
3955 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003956 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003957 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko423ba3f2021-07-19 13:08:50 +02003958 if (sleaf && (sleaf->nodetype & LYD_NODE_TERM) && (sleaf->type->basetype == LY_TYPE_LEAFREF)) {
Michal Vasko004d3152020-06-11 19:59:22 +02003959 lref = (struct lysc_type_leafref *)sleaf->type;
Michal Vaskod1e53b92021-01-28 13:11:06 +01003960 oper = (sleaf->flags & LYS_IS_OUTPUT) ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT;
Michal Vasko004d3152020-06-11 19:59:22 +02003961
3962 /* it was already evaluated on schema, it must succeed */
Michal Vasko741bb562021-06-24 11:59:50 +02003963 r = ly_path_compile_leafref(set->ctx, &sleaf->node, NULL, lref->path, oper, LY_PATH_TARGET_MANY,
Michal Vasko24fc4d12021-07-12 14:41:20 +02003964 LY_VALUE_SCHEMA_RESOLVED, lref->prefixes, &p);
Michal Vasko741bb562021-06-24 11:59:50 +02003965 if (!r) {
3966 /* get the target node */
3967 target = p[LY_ARRAY_COUNT(p) - 1].node;
3968 ly_path_free(set->ctx, p);
Michal Vasko004d3152020-06-11 19:59:22 +02003969
Michal Vasko741bb562021-06-24 11:59:50 +02003970 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, target, LYXP_NODE_ELEM, NULL));
3971 } /* else the target was found before but is disabled so it was removed */
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003972 }
3973
Michal Vasko741bb562021-06-24 11:59:50 +02003974 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003975 }
3976
Michal Vaskod3678892020-05-21 10:06:58 +02003977 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003978 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "deref(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003979 return LY_EVALID;
3980 }
3981
Michal Vaskod3678892020-05-21 10:06:58 +02003982 lyxp_set_free_content(set);
3983 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003984 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
3985 sleaf = (struct lysc_node_leaf *)leaf->schema;
3986 if (sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
3987 if (sleaf->type->basetype == LY_TYPE_LEAFREF) {
3988 /* find leafref target */
Radek Krejci0b013302021-03-29 15:22:32 +02003989 if (lyplg_type_resolve_leafref((struct lysc_type_leafref *)sleaf->type, &leaf->node, &leaf->value, set->tree,
Michal Vasko9e685082021-01-29 14:49:09 +01003990 &node, &errmsg)) {
Michal Vasko004d3152020-06-11 19:59:22 +02003991 LOGERR(set->ctx, LY_EVALID, errmsg);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003992 free(errmsg);
Michal Vasko004d3152020-06-11 19:59:22 +02003993 return LY_EVALID;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003994 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003995 } else {
3996 assert(sleaf->type->basetype == LY_TYPE_INST);
Michal Vasko004d3152020-06-11 19:59:22 +02003997 if (ly_path_eval(leaf->value.target, set->tree, &node)) {
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003998 LOGERR(set->ctx, LY_EVALID, "Invalid instance-identifier \"%s\" value - required instance not found.",
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02003999 lyd_get_value(&leaf->node));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004000 return LY_EVALID;
4001 }
4002 }
Michal Vasko004d3152020-06-11 19:59:22 +02004003
4004 /* insert it */
4005 set_insert_node(set, node, 0, LYXP_NODE_ELEM, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004006 }
4007 }
4008
4009 return LY_SUCCESS;
4010}
4011
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004012static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02004013xpath_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 +02004014{
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01004015 uint32_t i;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004016 LY_ARRAY_COUNT_TYPE u;
4017 struct lyd_node_term *leaf;
4018 struct lysc_node_leaf *sleaf;
4019 struct lyd_meta *meta;
Michal Vasko93923692021-05-07 15:28:02 +02004020 struct lyd_value *val;
4021 const struct lys_module *mod;
4022 const char *id_name;
4023 uint16_t id_len;
4024 struct lysc_ident *id;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004025 LY_ERR rc = LY_SUCCESS;
Radek Krejci857189e2020-09-01 13:26:36 +02004026 ly_bool found;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004027
4028 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02004029 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004030 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", func);
Michal Vasko5676f4e2021-04-06 17:14:45 +02004031 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4032 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4033 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype),
4034 sleaf->name);
4035 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_IDENT)) {
4036 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"identityref\".", func, sleaf->name);
4037 }
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004038 }
4039
4040 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4041 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4042 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype),
Michal Vasko69730152020-10-09 16:30:07 +02004043 sleaf->name);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004044 } else if (!warn_is_string_type(sleaf->type)) {
4045 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", func, sleaf->name);
4046 }
4047 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004048 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004049 return rc;
4050 }
4051
4052 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004053 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 +02004054 return LY_EVALID;
4055 }
4056 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
4057 LY_CHECK_RET(rc);
4058
Michal Vasko93923692021-05-07 15:28:02 +02004059 /* parse the identity */
4060 id_name = args[1]->val.str;
4061 id_len = strlen(id_name);
4062 rc = moveto_resolve_model(&id_name, &id_len, set, set->cur_node ? set->cur_node->schema : NULL, &mod);
4063 LY_CHECK_RET(rc);
4064 if (!mod) {
4065 LOGVAL(set->ctx, LYVE_XPATH, "Identity \"%.*s\" without a prefix.", (int)id_len, id_name);
4066 return LY_EVALID;
4067 }
4068
4069 /* find the identity */
4070 found = 0;
4071 LY_ARRAY_FOR(mod->identities, u) {
4072 if (!ly_strncmp(mod->identities[u].name, id_name, id_len)) {
4073 /* we have match */
4074 found = 1;
4075 break;
4076 }
4077 }
4078 if (!found) {
4079 LOGVAL(set->ctx, LYVE_XPATH, "Identity \"%.*s\" not found in module \"%s\".", (int)id_len, id_name, mod->name);
4080 return LY_EVALID;
4081 }
4082 id = &mod->identities[u];
4083
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004084 set_fill_boolean(set, 0);
4085 found = 0;
4086 for (i = 0; i < args[0]->used; ++i) {
4087 if ((args[0]->val.nodes[i].type != LYXP_NODE_ELEM) && (args[0]->val.nodes[i].type != LYXP_NODE_META)) {
4088 continue;
4089 }
4090
4091 if (args[0]->val.nodes[i].type == LYXP_NODE_ELEM) {
4092 leaf = (struct lyd_node_term *)args[0]->val.nodes[i].node;
4093 sleaf = (struct lysc_node_leaf *)leaf->schema;
4094 val = &leaf->value;
4095 if (!(sleaf->nodetype & LYD_NODE_TERM) || (leaf->value.realtype->basetype != LY_TYPE_IDENT)) {
4096 /* uninteresting */
4097 continue;
4098 }
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004099 } else {
4100 meta = args[0]->val.meta[i].meta;
4101 val = &meta->value;
4102 if (val->realtype->basetype != LY_TYPE_IDENT) {
4103 /* uninteresting */
4104 continue;
4105 }
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004106 }
4107
Michal Vasko93923692021-05-07 15:28:02 +02004108 /* check the identity itself */
4109 if (self_match && (id == val->ident)) {
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004110 set_fill_boolean(set, 1);
4111 found = 1;
4112 }
Michal Vasko93923692021-05-07 15:28:02 +02004113 if (!found && !lyplg_type_identity_isderived(id, val->ident)) {
4114 set_fill_boolean(set, 1);
4115 found = 1;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004116 }
4117
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004118 if (found) {
4119 break;
4120 }
4121 }
4122
4123 return LY_SUCCESS;
4124}
4125
Michal Vasko03ff5a72019-09-11 13:49:33 +02004126/**
4127 * @brief Execute the YANG 1.1 derived-from(node-set, string) function. Returns LYXP_SET_BOOLEAN depending
4128 * on whether the first argument nodes contain a node of an identity derived from the second
4129 * argument identity.
4130 *
4131 * @param[in] args Array of arguments.
4132 * @param[in] arg_count Count of elements in @p args.
4133 * @param[in,out] set Context and result set at the same time.
4134 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004135 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004136 */
4137static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004138xpath_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 +02004139{
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004140 return xpath_derived_(args, set, options, 0, __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004141}
4142
4143/**
4144 * @brief Execute the YANG 1.1 derived-from-or-self(node-set, string) function. Returns LYXP_SET_BOOLEAN depending
4145 * on whether the first argument nodes contain a node of an identity that either is or is derived from
4146 * the second argument identity.
4147 *
4148 * @param[in] args Array of arguments.
4149 * @param[in] arg_count Count of elements in @p args.
4150 * @param[in,out] set Context and result set at the same time.
4151 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004152 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004153 */
4154static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004155xpath_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 +02004156{
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004157 return xpath_derived_(args, set, options, 1, __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004158}
4159
4160/**
4161 * @brief Execute the YANG 1.1 enum-value(node-set) function. Returns LYXP_SET_NUMBER
4162 * with the integer value of the first node's enum value, otherwise NaN.
4163 *
4164 * @param[in] args Array of arguments.
4165 * @param[in] arg_count Count of elements in @p args.
4166 * @param[in,out] set Context and result set at the same time.
4167 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004168 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004169 */
4170static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004171xpath_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 +02004172{
4173 struct lyd_node_term *leaf;
4174 struct lysc_node_leaf *sleaf;
4175 LY_ERR rc = LY_SUCCESS;
4176
4177 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02004178 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004179 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02004180 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4181 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4182 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
4183 sleaf->name);
4184 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_ENUM)) {
4185 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"enumeration\".", __func__, sleaf->name);
4186 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004187 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004188 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004189 return rc;
4190 }
4191
Michal Vaskod3678892020-05-21 10:06:58 +02004192 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004193 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "enum-value(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004194 return LY_EVALID;
4195 }
4196
4197 set_fill_number(set, NAN);
Michal Vaskod3678892020-05-21 10:06:58 +02004198 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004199 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
4200 sleaf = (struct lysc_node_leaf *)leaf->schema;
4201 if ((sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (sleaf->type->basetype == LY_TYPE_ENUM)) {
4202 set_fill_number(set, leaf->value.enum_item->value);
4203 }
4204 }
4205
4206 return LY_SUCCESS;
4207}
4208
4209/**
4210 * @brief Execute the XPath false() function. Returns LYXP_SET_BOOLEAN
4211 * with false value.
4212 *
4213 * @param[in] args Array of arguments.
4214 * @param[in] arg_count Count of elements in @p args.
4215 * @param[in,out] set Context and result set at the same time.
4216 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004217 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004218 */
4219static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004220xpath_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 +02004221{
4222 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004223 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004224 return LY_SUCCESS;
4225 }
4226
4227 set_fill_boolean(set, 0);
4228 return LY_SUCCESS;
4229}
4230
4231/**
4232 * @brief Execute the XPath floor(number) function. Returns LYXP_SET_NUMBER
4233 * with the first argument floored (truncated).
4234 *
4235 * @param[in] args Array of arguments.
4236 * @param[in] arg_count Count of elements in @p args.
4237 * @param[in,out] set Context and result set at the same time.
4238 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004239 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004240 */
4241static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004242xpath_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 +02004243{
4244 LY_ERR rc;
4245
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004246 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004247 LY_CHECK_RET(rc);
4248 if (isfinite(args[0]->val.num)) {
4249 set_fill_number(set, (long long)args[0]->val.num);
4250 }
4251
4252 return LY_SUCCESS;
4253}
4254
4255/**
4256 * @brief Execute the XPath lang(string) function. Returns LYXP_SET_BOOLEAN
4257 * whether the language of the text matches the one from the argument.
4258 *
4259 * @param[in] args Array of arguments.
4260 * @param[in] arg_count Count of elements in @p args.
4261 * @param[in,out] set Context and result set at the same time.
4262 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004263 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004264 */
4265static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004266xpath_lang(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004267{
4268 const struct lyd_node *node;
4269 struct lysc_node_leaf *sleaf;
Michal Vasko9f96a052020-03-10 09:41:45 +01004270 struct lyd_meta *meta = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004271 const char *val;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004272 LY_ERR rc = LY_SUCCESS;
4273
4274 if (options & LYXP_SCNODE_ALL) {
4275 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4276 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004277 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
4278 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004279 } else if (!warn_is_string_type(sleaf->type)) {
4280 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004281 }
4282 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004283 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004284 return rc;
4285 }
4286
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004287 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004288 LY_CHECK_RET(rc);
4289
Michal Vasko03ff5a72019-09-11 13:49:33 +02004290 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004291 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "lang(string)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004292 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004293 } else if (!set->used) {
4294 set_fill_boolean(set, 0);
4295 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004296 }
4297
4298 switch (set->val.nodes[0].type) {
4299 case LYXP_NODE_ELEM:
4300 case LYXP_NODE_TEXT:
4301 node = set->val.nodes[0].node;
4302 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004303 case LYXP_NODE_META:
4304 node = set->val.meta[0].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004305 break;
4306 default:
4307 /* nothing to do with roots */
4308 set_fill_boolean(set, 0);
4309 return LY_SUCCESS;
4310 }
4311
Michal Vasko9f96a052020-03-10 09:41:45 +01004312 /* find lang metadata */
Michal Vasko9e685082021-01-29 14:49:09 +01004313 for ( ; node; node = lyd_parent(node)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01004314 for (meta = node->meta; meta; meta = meta->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004315 /* annotations */
Michal Vasko9f96a052020-03-10 09:41:45 +01004316 if (meta->name && !strcmp(meta->name, "lang") && !strcmp(meta->annotation->module->name, "xml")) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004317 break;
4318 }
4319 }
4320
Michal Vasko9f96a052020-03-10 09:41:45 +01004321 if (meta) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004322 break;
4323 }
4324 }
4325
4326 /* compare languages */
Michal Vasko9f96a052020-03-10 09:41:45 +01004327 if (!meta) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004328 set_fill_boolean(set, 0);
4329 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004330 uint64_t i;
4331
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02004332 val = lyd_get_meta_value(meta);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004333 for (i = 0; args[0]->val.str[i]; ++i) {
4334 if (tolower(args[0]->val.str[i]) != tolower(val[i])) {
4335 set_fill_boolean(set, 0);
4336 break;
4337 }
4338 }
4339 if (!args[0]->val.str[i]) {
4340 if (!val[i] || (val[i] == '-')) {
4341 set_fill_boolean(set, 1);
4342 } else {
4343 set_fill_boolean(set, 0);
4344 }
4345 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004346 }
4347
4348 return LY_SUCCESS;
4349}
4350
4351/**
4352 * @brief Execute the XPath last() function. Returns LYXP_SET_NUMBER
4353 * with the context size.
4354 *
4355 * @param[in] args Array of arguments.
4356 * @param[in] arg_count Count of elements in @p args.
4357 * @param[in,out] set Context and result set at the same time.
4358 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004359 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004360 */
4361static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004362xpath_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 +02004363{
4364 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004365 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004366 return LY_SUCCESS;
4367 }
4368
Michal Vasko03ff5a72019-09-11 13:49:33 +02004369 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004370 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "last()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004371 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004372 } else if (!set->used) {
4373 set_fill_number(set, 0);
4374 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004375 }
4376
4377 set_fill_number(set, set->ctx_size);
4378 return LY_SUCCESS;
4379}
4380
4381/**
4382 * @brief Execute the XPath local-name(node-set?) function. Returns LYXP_SET_STRING
4383 * with the node name without namespace from the argument or the context.
4384 *
4385 * @param[in] args Array of arguments.
4386 * @param[in] arg_count Count of elements in @p args.
4387 * @param[in,out] set Context and result set at the same time.
4388 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004389 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004390 */
4391static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004392xpath_local_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004393{
4394 struct lyxp_set_node *item;
Michal Vasko69730152020-10-09 16:30:07 +02004395
Michal Vasko03ff5a72019-09-11 13:49:33 +02004396 /* suppress unused variable warning */
4397 (void)options;
4398
4399 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004400 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004401 return LY_SUCCESS;
4402 }
4403
4404 if (arg_count) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004405 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004406 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004407 "local-name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004408 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004409 } else if (!args[0]->used) {
4410 set_fill_string(set, "", 0);
4411 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004412 }
4413
4414 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004415 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004416
4417 item = &args[0]->val.nodes[0];
4418 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004419 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004420 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "local-name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004421 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004422 } else if (!set->used) {
4423 set_fill_string(set, "", 0);
4424 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004425 }
4426
4427 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004428 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004429
4430 item = &set->val.nodes[0];
4431 }
4432
4433 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004434 case LYXP_NODE_NONE:
4435 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004436 case LYXP_NODE_ROOT:
4437 case LYXP_NODE_ROOT_CONFIG:
4438 case LYXP_NODE_TEXT:
4439 set_fill_string(set, "", 0);
4440 break;
4441 case LYXP_NODE_ELEM:
4442 set_fill_string(set, item->node->schema->name, strlen(item->node->schema->name));
4443 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004444 case LYXP_NODE_META:
4445 set_fill_string(set, ((struct lyd_meta *)item->node)->name, strlen(((struct lyd_meta *)item->node)->name));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004446 break;
4447 }
4448
4449 return LY_SUCCESS;
4450}
4451
4452/**
4453 * @brief Execute the XPath name(node-set?) function. Returns LYXP_SET_STRING
4454 * with the node name fully qualified (with namespace) from the argument or the context.
4455 *
4456 * @param[in] args Array of arguments.
4457 * @param[in] arg_count Count of elements in @p args.
4458 * @param[in,out] set Context and result set at the same time.
4459 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004460 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004461 */
4462static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004463xpath_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004464{
4465 struct lyxp_set_node *item;
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004466 struct lys_module *mod = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004467 char *str;
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004468 const char *name = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004469
4470 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004471 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004472 return LY_SUCCESS;
4473 }
4474
4475 if (arg_count) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004476 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004477 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004478 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004479 } else if (!args[0]->used) {
4480 set_fill_string(set, "", 0);
4481 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004482 }
4483
4484 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004485 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004486
4487 item = &args[0]->val.nodes[0];
4488 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004489 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004490 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004491 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004492 } else if (!set->used) {
4493 set_fill_string(set, "", 0);
4494 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004495 }
4496
4497 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004498 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004499
4500 item = &set->val.nodes[0];
4501 }
4502
4503 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004504 case LYXP_NODE_NONE:
4505 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004506 case LYXP_NODE_ROOT:
4507 case LYXP_NODE_ROOT_CONFIG:
4508 case LYXP_NODE_TEXT:
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004509 /* keep NULL */
Michal Vasko03ff5a72019-09-11 13:49:33 +02004510 break;
4511 case LYXP_NODE_ELEM:
4512 mod = item->node->schema->module;
4513 name = item->node->schema->name;
4514 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004515 case LYXP_NODE_META:
4516 mod = ((struct lyd_meta *)item->node)->annotation->module;
4517 name = ((struct lyd_meta *)item->node)->name;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004518 break;
4519 }
4520
4521 if (mod && name) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004522 int rc = asprintf(&str, "%s:%s", ly_get_prefix(mod, set->format, set->prefix_data), name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004523 LY_CHECK_ERR_RET(rc == -1, LOGMEM(set->ctx), LY_EMEM);
4524 set_fill_string(set, str, strlen(str));
4525 free(str);
4526 } else {
4527 set_fill_string(set, "", 0);
4528 }
4529
4530 return LY_SUCCESS;
4531}
4532
4533/**
4534 * @brief Execute the XPath namespace-uri(node-set?) function. Returns LYXP_SET_STRING
4535 * with the namespace of the node from the argument or the context.
4536 *
4537 * @param[in] args Array of arguments.
4538 * @param[in] arg_count Count of elements in @p args.
4539 * @param[in,out] set Context and result set at the same time.
4540 * @param[in] options XPath options.
4541 * @return LY_ERR (LY_EINVAL for wrong arguments on schema)
4542 */
4543static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004544xpath_namespace_uri(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004545{
4546 struct lyxp_set_node *item;
4547 struct lys_module *mod;
Michal Vasko69730152020-10-09 16:30:07 +02004548
Michal Vasko03ff5a72019-09-11 13:49:33 +02004549 /* suppress unused variable warning */
4550 (void)options;
4551
4552 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004553 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004554 return LY_SUCCESS;
4555 }
4556
4557 if (arg_count) {
Michal Vaskod3678892020-05-21 10:06:58 +02004558 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004559 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
Michal Vasko69730152020-10-09 16:30:07 +02004560 "namespace-uri(node-set?)");
Michal Vaskod3678892020-05-21 10:06:58 +02004561 return LY_EVALID;
4562 } else if (!args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004563 set_fill_string(set, "", 0);
4564 return LY_SUCCESS;
4565 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004566
4567 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004568 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004569
4570 item = &args[0]->val.nodes[0];
4571 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004572 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004573 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "namespace-uri(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004574 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004575 } else if (!set->used) {
4576 set_fill_string(set, "", 0);
4577 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004578 }
4579
4580 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004581 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004582
4583 item = &set->val.nodes[0];
4584 }
4585
4586 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004587 case LYXP_NODE_NONE:
4588 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004589 case LYXP_NODE_ROOT:
4590 case LYXP_NODE_ROOT_CONFIG:
4591 case LYXP_NODE_TEXT:
4592 set_fill_string(set, "", 0);
4593 break;
4594 case LYXP_NODE_ELEM:
Michal Vasko9f96a052020-03-10 09:41:45 +01004595 case LYXP_NODE_META:
Michal Vasko03ff5a72019-09-11 13:49:33 +02004596 if (item->type == LYXP_NODE_ELEM) {
4597 mod = item->node->schema->module;
Michal Vasko9f96a052020-03-10 09:41:45 +01004598 } else { /* LYXP_NODE_META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02004599 /* annotations */
Michal Vasko9f96a052020-03-10 09:41:45 +01004600 mod = ((struct lyd_meta *)item->node)->annotation->module;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004601 }
4602
4603 set_fill_string(set, mod->ns, strlen(mod->ns));
4604 break;
4605 }
4606
4607 return LY_SUCCESS;
4608}
4609
4610/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02004611 * @brief Execute the XPath normalize-space(string?) function. Returns LYXP_SET_STRING
4612 * with normalized value (no leading, trailing, double white spaces) of the node
4613 * from the argument or the context.
4614 *
4615 * @param[in] args Array of arguments.
4616 * @param[in] arg_count Count of elements in @p args.
4617 * @param[in,out] set Context and result set at the same time.
4618 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004619 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004620 */
4621static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004622xpath_normalize_space(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004623{
4624 uint16_t i, new_used;
4625 char *new;
Radek Krejci857189e2020-09-01 13:26:36 +02004626 ly_bool have_spaces = 0, space_before = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004627 struct lysc_node_leaf *sleaf;
4628 LY_ERR rc = LY_SUCCESS;
4629
4630 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004631 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) &&
4632 (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004633 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004634 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
4635 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004636 } else if (!warn_is_string_type(sleaf->type)) {
4637 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004638 }
4639 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004640 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004641 return rc;
4642 }
4643
4644 if (arg_count) {
4645 set_fill_set(set, args[0]);
4646 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004647 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004648 LY_CHECK_RET(rc);
4649
4650 /* is there any normalization necessary? */
4651 for (i = 0; set->val.str[i]; ++i) {
4652 if (is_xmlws(set->val.str[i])) {
4653 if ((i == 0) || space_before || (!set->val.str[i + 1])) {
4654 have_spaces = 1;
4655 break;
4656 }
4657 space_before = 1;
4658 } else {
4659 space_before = 0;
4660 }
4661 }
4662
4663 /* yep, there is */
4664 if (have_spaces) {
4665 /* it's enough, at least one character will go, makes space for ending '\0' */
4666 new = malloc(strlen(set->val.str) * sizeof(char));
4667 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4668 new_used = 0;
4669
4670 space_before = 0;
4671 for (i = 0; set->val.str[i]; ++i) {
4672 if (is_xmlws(set->val.str[i])) {
4673 if ((i == 0) || space_before) {
4674 space_before = 1;
4675 continue;
4676 } else {
4677 space_before = 1;
4678 }
4679 } else {
4680 space_before = 0;
4681 }
4682
4683 new[new_used] = (space_before ? ' ' : set->val.str[i]);
4684 ++new_used;
4685 }
4686
4687 /* at worst there is one trailing space now */
4688 if (new_used && is_xmlws(new[new_used - 1])) {
4689 --new_used;
4690 }
4691
4692 new = ly_realloc(new, (new_used + 1) * sizeof(char));
4693 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4694 new[new_used] = '\0';
4695
4696 free(set->val.str);
4697 set->val.str = new;
4698 }
4699
4700 return LY_SUCCESS;
4701}
4702
4703/**
4704 * @brief Execute the XPath not(boolean) function. Returns LYXP_SET_BOOLEAN
4705 * with the argument converted to boolean and logically inverted.
4706 *
4707 * @param[in] args Array of arguments.
4708 * @param[in] arg_count Count of elements in @p args.
4709 * @param[in,out] set Context and result set at the same time.
4710 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004711 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004712 */
4713static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004714xpath_not(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004715{
4716 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004717 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004718 return LY_SUCCESS;
4719 }
4720
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004721 lyxp_set_cast(args[0], LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +02004722 if (args[0]->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004723 set_fill_boolean(set, 0);
4724 } else {
4725 set_fill_boolean(set, 1);
4726 }
4727
4728 return LY_SUCCESS;
4729}
4730
4731/**
4732 * @brief Execute the XPath number(object?) function. Returns LYXP_SET_NUMBER
4733 * with the number representation of either the argument or the context.
4734 *
4735 * @param[in] args Array of arguments.
4736 * @param[in] arg_count Count of elements in @p args.
4737 * @param[in,out] set Context and result set at the same time.
4738 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004739 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004740 */
4741static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004742xpath_number(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004743{
4744 LY_ERR rc;
4745
4746 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004747 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004748 return LY_SUCCESS;
4749 }
4750
4751 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004752 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004753 LY_CHECK_RET(rc);
4754 set_fill_set(set, args[0]);
4755 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004756 rc = lyxp_set_cast(set, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004757 LY_CHECK_RET(rc);
4758 }
4759
4760 return LY_SUCCESS;
4761}
4762
4763/**
4764 * @brief Execute the XPath position() function. Returns LYXP_SET_NUMBER
4765 * with the context position.
4766 *
4767 * @param[in] args Array of arguments.
4768 * @param[in] arg_count Count of elements in @p args.
4769 * @param[in,out] set Context and result set at the same time.
4770 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004771 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004772 */
4773static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004774xpath_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 +02004775{
4776 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004777 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004778 return LY_SUCCESS;
4779 }
4780
Michal Vasko03ff5a72019-09-11 13:49:33 +02004781 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004782 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "position()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004783 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004784 } else if (!set->used) {
4785 set_fill_number(set, 0);
4786 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004787 }
4788
4789 set_fill_number(set, set->ctx_pos);
4790
4791 /* UNUSED in 'Release' build type */
4792 (void)options;
4793 return LY_SUCCESS;
4794}
4795
4796/**
4797 * @brief Execute the YANG 1.1 re-match(string, string) function. Returns LYXP_SET_BOOLEAN
4798 * depending on whether the second argument regex matches the first argument string. For details refer to
4799 * YANG 1.1 RFC section 10.2.1.
4800 *
4801 * @param[in] args Array of arguments.
4802 * @param[in] arg_count Count of elements in @p args.
4803 * @param[in,out] set Context and result set at the same time.
4804 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004805 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004806 */
4807static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004808xpath_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 +02004809{
4810 struct lysc_pattern **patterns = NULL, **pattern;
4811 struct lysc_node_leaf *sleaf;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004812 LY_ERR rc = LY_SUCCESS;
4813 struct ly_err_item *err;
4814
4815 if (options & LYXP_SCNODE_ALL) {
4816 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4817 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4818 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 +02004819 } else if (!warn_is_string_type(sleaf->type)) {
4820 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004821 }
4822 }
4823
4824 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4825 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4826 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 +02004827 } else if (!warn_is_string_type(sleaf->type)) {
4828 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004829 }
4830 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004831 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004832 return rc;
4833 }
4834
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004835 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004836 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004837 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004838 LY_CHECK_RET(rc);
4839
4840 LY_ARRAY_NEW_RET(set->ctx, patterns, pattern, LY_EMEM);
Radek Iša45802b52021-02-09 09:21:58 +01004841 *pattern = calloc(1, sizeof **pattern);
Radek Krejciddace2c2021-01-08 11:30:56 +01004842 LOG_LOCSET(NULL, set->cur_node, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01004843 rc = lys_compile_type_pattern_check(set->ctx, args[1]->val.str, &(*pattern)->code);
Michal Vasko4a7d4d62021-12-13 17:05:06 +01004844 if (set->cur_node) {
4845 LOG_LOCBACK(0, 1, 0, 0);
4846 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004847 if (rc != LY_SUCCESS) {
4848 LY_ARRAY_FREE(patterns);
4849 return rc;
4850 }
4851
Radek Krejci0b013302021-03-29 15:22:32 +02004852 rc = lyplg_type_validate_patterns(patterns, args[0]->val.str, strlen(args[0]->val.str), &err);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004853 pcre2_code_free((*pattern)->code);
4854 free(*pattern);
4855 LY_ARRAY_FREE(patterns);
4856 if (rc && (rc != LY_EVALID)) {
Michal Vasko177d0ed2020-11-23 16:43:03 +01004857 ly_err_print(set->ctx, err);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004858 ly_err_free(err);
4859 return rc;
4860 }
4861
4862 if (rc == LY_EVALID) {
4863 ly_err_free(err);
4864 set_fill_boolean(set, 0);
4865 } else {
4866 set_fill_boolean(set, 1);
4867 }
4868
4869 return LY_SUCCESS;
4870}
4871
4872/**
4873 * @brief Execute the XPath round(number) function. Returns LYXP_SET_NUMBER
4874 * with the rounded first argument. For details refer to
4875 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-round.
4876 *
4877 * @param[in] args Array of arguments.
4878 * @param[in] arg_count Count of elements in @p args.
4879 * @param[in,out] set Context and result set at the same time.
4880 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004881 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004882 */
4883static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004884xpath_round(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004885{
4886 struct lysc_node_leaf *sleaf;
4887 LY_ERR rc = LY_SUCCESS;
4888
4889 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02004890 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004891 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02004892 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4893 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4894 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
4895 sleaf->name);
4896 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
4897 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
4898 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004899 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004900 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004901 return rc;
4902 }
4903
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004904 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004905 LY_CHECK_RET(rc);
4906
4907 /* cover only the cases where floor can't be used */
4908 if ((args[0]->val.num == -0.0f) || ((args[0]->val.num < 0) && (args[0]->val.num >= -0.5))) {
4909 set_fill_number(set, -0.0f);
4910 } else {
4911 args[0]->val.num += 0.5;
4912 rc = xpath_floor(args, 1, args[0], options);
4913 LY_CHECK_RET(rc);
4914 set_fill_number(set, args[0]->val.num);
4915 }
4916
4917 return LY_SUCCESS;
4918}
4919
4920/**
4921 * @brief Execute the XPath starts-with(string, string) function.
4922 * Returns LYXP_SET_BOOLEAN whether the second argument is
4923 * the prefix of the first or not.
4924 *
4925 * @param[in] args Array of arguments.
4926 * @param[in] arg_count Count of elements in @p args.
4927 * @param[in,out] set Context and result set at the same time.
4928 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004929 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004930 */
4931static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004932xpath_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 +02004933{
4934 struct lysc_node_leaf *sleaf;
4935 LY_ERR rc = LY_SUCCESS;
4936
4937 if (options & LYXP_SCNODE_ALL) {
4938 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4939 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4940 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 +02004941 } else if (!warn_is_string_type(sleaf->type)) {
4942 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004943 }
4944 }
4945
4946 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4947 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4948 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 +02004949 } else if (!warn_is_string_type(sleaf->type)) {
4950 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004951 }
4952 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004953 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004954 return rc;
4955 }
4956
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004957 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004958 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004959 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004960 LY_CHECK_RET(rc);
4961
4962 if (strncmp(args[0]->val.str, args[1]->val.str, strlen(args[1]->val.str))) {
4963 set_fill_boolean(set, 0);
4964 } else {
4965 set_fill_boolean(set, 1);
4966 }
4967
4968 return LY_SUCCESS;
4969}
4970
4971/**
4972 * @brief Execute the XPath string(object?) function. Returns LYXP_SET_STRING
4973 * with the string representation of either the argument or the context.
4974 *
4975 * @param[in] args Array of arguments.
4976 * @param[in] arg_count Count of elements in @p args.
4977 * @param[in,out] set Context and result set at the same time.
4978 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004979 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004980 */
4981static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004982xpath_string(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004983{
4984 LY_ERR rc;
4985
4986 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004987 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004988 return LY_SUCCESS;
4989 }
4990
4991 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004992 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004993 LY_CHECK_RET(rc);
4994 set_fill_set(set, args[0]);
4995 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004996 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004997 LY_CHECK_RET(rc);
4998 }
4999
5000 return LY_SUCCESS;
5001}
5002
5003/**
5004 * @brief Execute the XPath string-length(string?) function. Returns LYXP_SET_NUMBER
5005 * with the length of the string in either the argument or the context.
5006 *
5007 * @param[in] args Array of arguments.
5008 * @param[in] arg_count Count of elements in @p args.
5009 * @param[in,out] set Context and result set at the same time.
5010 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005011 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005012 */
5013static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005014xpath_string_length(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005015{
5016 struct lysc_node_leaf *sleaf;
5017 LY_ERR rc = LY_SUCCESS;
5018
5019 if (options & LYXP_SCNODE_ALL) {
5020 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5021 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5022 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 +02005023 } else if (!warn_is_string_type(sleaf->type)) {
5024 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005025 }
5026 }
5027 if (!arg_count && (set->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set))) {
5028 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5029 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 +02005030 } else if (!warn_is_string_type(sleaf->type)) {
5031 LOGWRN(set->ctx, "Argument #0 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005032 }
5033 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005034 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005035 return rc;
5036 }
5037
5038 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005039 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005040 LY_CHECK_RET(rc);
5041 set_fill_number(set, strlen(args[0]->val.str));
5042 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005043 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005044 LY_CHECK_RET(rc);
5045 set_fill_number(set, strlen(set->val.str));
5046 }
5047
5048 return LY_SUCCESS;
5049}
5050
5051/**
5052 * @brief Execute the XPath substring(string, number, number?) function.
5053 * Returns LYXP_SET_STRING substring of the first argument starting
5054 * on the second argument index ending on the third argument index,
5055 * indexed from 1. For exact definition refer to
5056 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-substring.
5057 *
5058 * @param[in] args Array of arguments.
5059 * @param[in] arg_count Count of elements in @p args.
5060 * @param[in,out] set Context and result set at the same time.
5061 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005062 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005063 */
5064static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005065xpath_substring(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005066{
Radek Krejci1deb5be2020-08-26 16:43:36 +02005067 int32_t start, len;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005068 uint16_t str_start, str_len, pos;
5069 struct lysc_node_leaf *sleaf;
5070 LY_ERR rc = LY_SUCCESS;
5071
5072 if (options & LYXP_SCNODE_ALL) {
5073 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5074 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5075 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 +02005076 } else if (!warn_is_string_type(sleaf->type)) {
5077 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005078 }
5079 }
5080
5081 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5082 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5083 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 +02005084 } else if (!warn_is_numeric_type(sleaf->type)) {
5085 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005086 }
5087 }
5088
Michal Vasko69730152020-10-09 16:30:07 +02005089 if ((arg_count == 3) && (args[2]->type == LYXP_SET_SCNODE_SET) &&
5090 (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005091 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5092 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 +02005093 } else if (!warn_is_numeric_type(sleaf->type)) {
5094 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005095 }
5096 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005097 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005098 return rc;
5099 }
5100
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005101 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005102 LY_CHECK_RET(rc);
5103
5104 /* start */
5105 if (xpath_round(&args[1], 1, args[1], options)) {
5106 return -1;
5107 }
5108 if (isfinite(args[1]->val.num)) {
5109 start = args[1]->val.num - 1;
5110 } else if (isinf(args[1]->val.num) && signbit(args[1]->val.num)) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02005111 start = INT32_MIN;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005112 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02005113 start = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005114 }
5115
5116 /* len */
5117 if (arg_count == 3) {
5118 rc = xpath_round(&args[2], 1, args[2], options);
5119 LY_CHECK_RET(rc);
Radek Krejci1deb5be2020-08-26 16:43:36 +02005120 if (isnan(args[2]->val.num) || signbit(args[2]->val.num)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005121 len = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005122 } else if (isfinite(args[2]->val.num)) {
5123 len = args[2]->val.num;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005124 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02005125 len = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005126 }
5127 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02005128 len = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005129 }
5130
5131 /* find matching character positions */
5132 str_start = 0;
5133 str_len = 0;
5134 for (pos = 0; args[0]->val.str[pos]; ++pos) {
5135 if (pos < start) {
5136 ++str_start;
5137 } else if (pos < start + len) {
5138 ++str_len;
5139 } else {
5140 break;
5141 }
5142 }
5143
5144 set_fill_string(set, args[0]->val.str + str_start, str_len);
5145 return LY_SUCCESS;
5146}
5147
5148/**
5149 * @brief Execute the XPath substring-after(string, string) function.
5150 * Returns LYXP_SET_STRING with the string succeeding the occurance
5151 * of the second argument in the first or an empty string.
5152 *
5153 * @param[in] args Array of arguments.
5154 * @param[in] arg_count Count of elements in @p args.
5155 * @param[in,out] set Context and result set at the same time.
5156 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005157 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005158 */
5159static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005160xpath_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 +02005161{
5162 char *ptr;
5163 struct lysc_node_leaf *sleaf;
5164 LY_ERR rc = LY_SUCCESS;
5165
5166 if (options & LYXP_SCNODE_ALL) {
5167 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5168 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5169 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 +02005170 } else if (!warn_is_string_type(sleaf->type)) {
5171 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005172 }
5173 }
5174
5175 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5176 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5177 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 +02005178 } else if (!warn_is_string_type(sleaf->type)) {
5179 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005180 }
5181 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005182 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005183 return rc;
5184 }
5185
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005186 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005187 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005188 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005189 LY_CHECK_RET(rc);
5190
5191 ptr = strstr(args[0]->val.str, args[1]->val.str);
5192 if (ptr) {
5193 set_fill_string(set, ptr + strlen(args[1]->val.str), strlen(ptr + strlen(args[1]->val.str)));
5194 } else {
5195 set_fill_string(set, "", 0);
5196 }
5197
5198 return LY_SUCCESS;
5199}
5200
5201/**
5202 * @brief Execute the XPath substring-before(string, string) function.
5203 * Returns LYXP_SET_STRING with the string preceding the occurance
5204 * of the second argument in the first or an empty string.
5205 *
5206 * @param[in] args Array of arguments.
5207 * @param[in] arg_count Count of elements in @p args.
5208 * @param[in,out] set Context and result set at the same time.
5209 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005210 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005211 */
5212static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005213xpath_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 +02005214{
5215 char *ptr;
5216 struct lysc_node_leaf *sleaf;
5217 LY_ERR rc = LY_SUCCESS;
5218
5219 if (options & LYXP_SCNODE_ALL) {
5220 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5221 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5222 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 +02005223 } else if (!warn_is_string_type(sleaf->type)) {
5224 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005225 }
5226 }
5227
5228 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5229 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5230 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 +02005231 } else if (!warn_is_string_type(sleaf->type)) {
5232 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005233 }
5234 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005235 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005236 return rc;
5237 }
5238
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005239 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005240 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005241 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005242 LY_CHECK_RET(rc);
5243
5244 ptr = strstr(args[0]->val.str, args[1]->val.str);
5245 if (ptr) {
5246 set_fill_string(set, args[0]->val.str, ptr - args[0]->val.str);
5247 } else {
5248 set_fill_string(set, "", 0);
5249 }
5250
5251 return LY_SUCCESS;
5252}
5253
5254/**
5255 * @brief Execute the XPath sum(node-set) function. Returns LYXP_SET_NUMBER
5256 * with the sum of all the nodes in the context.
5257 *
5258 * @param[in] args Array of arguments.
5259 * @param[in] arg_count Count of elements in @p args.
5260 * @param[in,out] set Context and result set at the same time.
5261 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005262 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005263 */
5264static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005265xpath_sum(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005266{
5267 long double num;
5268 char *str;
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01005269 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005270 struct lyxp_set set_item;
5271 struct lysc_node_leaf *sleaf;
5272 LY_ERR rc = LY_SUCCESS;
5273
5274 if (options & LYXP_SCNODE_ALL) {
5275 if (args[0]->type == LYXP_SET_SCNODE_SET) {
5276 for (i = 0; i < args[0]->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005277 if (args[0]->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005278 sleaf = (struct lysc_node_leaf *)args[0]->val.scnodes[i].scnode;
5279 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5280 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__,
Michal Vasko69730152020-10-09 16:30:07 +02005281 lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005282 } else if (!warn_is_numeric_type(sleaf->type)) {
5283 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005284 }
5285 }
5286 }
5287 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005288 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005289 return rc;
5290 }
5291
5292 set_fill_number(set, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005293
5294 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005295 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "sum(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02005296 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02005297 } else if (!args[0]->used) {
5298 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005299 }
5300
Michal Vasko5c4e5892019-11-14 12:31:38 +01005301 set_init(&set_item, set);
5302
Michal Vasko03ff5a72019-09-11 13:49:33 +02005303 set_item.type = LYXP_SET_NODE_SET;
Michal Vasko41decbf2021-11-02 11:50:21 +01005304 set_item.val.nodes = calloc(1, sizeof *set_item.val.nodes);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005305 LY_CHECK_ERR_RET(!set_item.val.nodes, LOGMEM(set->ctx), LY_EMEM);
5306
5307 set_item.used = 1;
5308 set_item.size = 1;
5309
5310 for (i = 0; i < args[0]->used; ++i) {
5311 set_item.val.nodes[0] = args[0]->val.nodes[i];
5312
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005313 rc = cast_node_set_to_string(&set_item, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005314 LY_CHECK_RET(rc);
5315 num = cast_string_to_number(str);
5316 free(str);
5317 set->val.num += num;
5318 }
5319
5320 free(set_item.val.nodes);
5321
5322 return LY_SUCCESS;
5323}
5324
5325/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02005326 * @brief Execute the XPath translate(string, string, string) function.
5327 * Returns LYXP_SET_STRING with the first argument with the characters
5328 * from the second argument replaced by those on the corresponding
5329 * positions in the third argument.
5330 *
5331 * @param[in] args Array of arguments.
5332 * @param[in] arg_count Count of elements in @p args.
5333 * @param[in,out] set Context and result set at the same time.
5334 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005335 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005336 */
5337static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005338xpath_translate(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005339{
5340 uint16_t i, j, new_used;
5341 char *new;
Radek Krejci857189e2020-09-01 13:26:36 +02005342 ly_bool have_removed;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005343 struct lysc_node_leaf *sleaf;
5344 LY_ERR rc = LY_SUCCESS;
5345
5346 if (options & LYXP_SCNODE_ALL) {
5347 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5348 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5349 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 +02005350 } else if (!warn_is_string_type(sleaf->type)) {
5351 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005352 }
5353 }
5354
5355 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5356 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5357 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 +02005358 } else if (!warn_is_string_type(sleaf->type)) {
5359 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005360 }
5361 }
5362
5363 if ((args[2]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
5364 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5365 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 +02005366 } else if (!warn_is_string_type(sleaf->type)) {
5367 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005368 }
5369 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005370 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005371 return rc;
5372 }
5373
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005374 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005375 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005376 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005377 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005378 rc = lyxp_set_cast(args[2], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005379 LY_CHECK_RET(rc);
5380
5381 new = malloc((strlen(args[0]->val.str) + 1) * sizeof(char));
5382 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5383 new_used = 0;
5384
5385 have_removed = 0;
5386 for (i = 0; args[0]->val.str[i]; ++i) {
Radek Krejci857189e2020-09-01 13:26:36 +02005387 ly_bool found = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005388
5389 for (j = 0; args[1]->val.str[j]; ++j) {
5390 if (args[0]->val.str[i] == args[1]->val.str[j]) {
5391 /* removing this char */
5392 if (j >= strlen(args[2]->val.str)) {
5393 have_removed = 1;
5394 found = 1;
5395 break;
5396 }
5397 /* replacing this char */
5398 new[new_used] = args[2]->val.str[j];
5399 ++new_used;
5400 found = 1;
5401 break;
5402 }
5403 }
5404
5405 /* copying this char */
5406 if (!found) {
5407 new[new_used] = args[0]->val.str[i];
5408 ++new_used;
5409 }
5410 }
5411
5412 if (have_removed) {
5413 new = ly_realloc(new, (new_used + 1) * sizeof(char));
5414 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5415 }
5416 new[new_used] = '\0';
5417
Michal Vaskod3678892020-05-21 10:06:58 +02005418 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005419 set->type = LYXP_SET_STRING;
5420 set->val.str = new;
5421
5422 return LY_SUCCESS;
5423}
5424
5425/**
5426 * @brief Execute the XPath true() function. Returns LYXP_SET_BOOLEAN
5427 * with true value.
5428 *
5429 * @param[in] args Array of arguments.
5430 * @param[in] arg_count Count of elements in @p args.
5431 * @param[in,out] set Context and result set at the same time.
5432 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005433 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005434 */
5435static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005436xpath_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 +02005437{
5438 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02005439 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005440 return LY_SUCCESS;
5441 }
5442
5443 set_fill_boolean(set, 1);
5444 return LY_SUCCESS;
5445}
5446
Michal Vasko03ff5a72019-09-11 13:49:33 +02005447/**
Michal Vasko49fec8e2022-05-24 10:28:33 +02005448 * @brief Execute the XPath node() processing instruction (node type). Returns LYXP_SET_NODE_SET
5449 * with only nodes from the context.
5450 *
5451 * @param[in,out] set Context and result set at the same time.
5452 * @param[in] axis Axis to search on.
5453 * @param[in] options XPath options.
5454 * @return LY_ERR
5455 */
5456static LY_ERR
5457xpath_pi_node(struct lyxp_set *set, enum lyxp_axis axis, uint32_t options)
5458{
5459 if (options & LYXP_SCNODE_ALL) {
5460 return moveto_scnode(set, NULL, NULL, axis, options);
5461 }
5462
5463 if (set->type != LYXP_SET_NODE_SET) {
5464 lyxp_set_free_content(set);
5465 return LY_SUCCESS;
5466 }
5467
5468 /* just like moving to a node with no restrictions */
5469 return moveto_node(set, NULL, NULL, axis, options);
5470}
5471
5472/**
5473 * @brief Execute the XPath text() processing instruction (node type). Returns LYXP_SET_NODE_SET
5474 * with the text content of the nodes in the context.
5475 *
5476 * @param[in,out] set Context and result set at the same time.
5477 * @param[in] axis Axis to search on.
5478 * @param[in] options XPath options.
5479 * @return LY_ERR
5480 */
5481static LY_ERR
5482xpath_pi_text(struct lyxp_set *set, enum lyxp_axis axis, uint32_t options)
5483{
5484 uint32_t i;
5485
5486 if (options & LYXP_SCNODE_ALL) {
5487 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
5488 return LY_SUCCESS;
5489 }
5490
5491 if (set->type != LYXP_SET_NODE_SET) {
5492 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "text()");
5493 return LY_EVALID;
5494 }
5495
5496 if (axis != LYXP_AXIS_CHILD) {
5497 /* even following and preceding axescan return text nodes, but whatever */
5498 lyxp_set_free_content(set);
5499 return LY_SUCCESS;
5500 }
5501
5502 for (i = 0; i < set->used; ++i) {
5503 switch (set->val.nodes[i].type) {
5504 case LYXP_NODE_NONE:
5505 LOGINT_RET(set->ctx);
5506 case LYXP_NODE_ELEM:
5507 if (set->val.nodes[i].node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
5508 set->val.nodes[i].type = LYXP_NODE_TEXT;
5509 break;
5510 }
5511 /* fall through */
5512 case LYXP_NODE_ROOT:
5513 case LYXP_NODE_ROOT_CONFIG:
5514 case LYXP_NODE_TEXT:
5515 case LYXP_NODE_META:
5516 set_remove_node_none(set, i);
5517 break;
5518 }
5519 }
5520 set_remove_nodes_none(set);
5521
5522 return LY_SUCCESS;
5523}
5524
5525/**
Michal Vasko6346ece2019-09-24 13:12:53 +02005526 * @brief Skip prefix and return corresponding model if there is a prefix. Logs directly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005527 *
Michal Vasko2104e9f2020-03-06 08:23:25 +01005528 * XPath @p set is expected to be a (sc)node set!
5529 *
Michal Vasko6346ece2019-09-24 13:12:53 +02005530 * @param[in,out] qname Qualified node name. If includes prefix, it is skipped.
5531 * @param[in,out] qname_len Length of @p qname, is updated accordingly.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005532 * @param[in] set Set with general XPath context.
5533 * @param[in] ctx_scnode Context node to inherit module for unprefixed node for ::LY_PREF_JSON.
Michal Vasko6346ece2019-09-24 13:12:53 +02005534 * @param[out] moveto_mod Expected module of a matching node.
5535 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005536 */
Michal Vasko6346ece2019-09-24 13:12:53 +02005537static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005538moveto_resolve_model(const char **qname, uint16_t *qname_len, const struct lyxp_set *set,
5539 const struct lysc_node *ctx_scnode, const struct lys_module **moveto_mod)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005540{
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02005541 const struct lys_module *mod = NULL;
Michal Vasko6346ece2019-09-24 13:12:53 +02005542 const char *ptr;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005543 size_t pref_len;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005544
Michal Vasko2104e9f2020-03-06 08:23:25 +01005545 assert((set->type == LYXP_SET_NODE_SET) || (set->type == LYXP_SET_SCNODE_SET));
5546
Michal Vasko6346ece2019-09-24 13:12:53 +02005547 if ((ptr = ly_strnchr(*qname, ':', *qname_len))) {
5548 /* specific module */
5549 pref_len = ptr - *qname;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005550 mod = ly_resolve_prefix(set->ctx, *qname, pref_len, set->format, set->prefix_data);
Michal Vasko6346ece2019-09-24 13:12:53 +02005551
Michal Vasko004d3152020-06-11 19:59:22 +02005552 /* check for errors and non-implemented modules, as they are not valid */
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005553 if (!mod || !mod->implemented) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005554 LOGVAL(set->ctx, LY_VCODE_XP_INMOD, pref_len, *qname);
Michal Vasko6346ece2019-09-24 13:12:53 +02005555 return LY_EVALID;
5556 }
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005557
Michal Vasko6346ece2019-09-24 13:12:53 +02005558 *qname += pref_len + 1;
5559 *qname_len -= pref_len + 1;
5560 } else if (((*qname)[0] == '*') && (*qname_len == 1)) {
5561 /* all modules - special case */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005562 mod = NULL;
Michal Vasko6346ece2019-09-24 13:12:53 +02005563 } else {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005564 switch (set->format) {
Radek Krejci8df109d2021-04-23 12:19:08 +02005565 case LY_VALUE_SCHEMA:
5566 case LY_VALUE_SCHEMA_RESOLVED:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005567 /* current module */
5568 mod = set->cur_mod;
5569 break;
Radek Krejci224d4b42021-04-23 13:54:59 +02005570 case LY_VALUE_CANON:
Radek Krejci8df109d2021-04-23 12:19:08 +02005571 case LY_VALUE_JSON:
Radek Krejcif9943642021-04-26 10:18:21 +02005572 case LY_VALUE_LYB:
Michal Vaskoddd76592022-01-17 13:34:48 +01005573 case LY_VALUE_STR_NS:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005574 /* inherit parent (context node) module */
5575 if (ctx_scnode) {
5576 mod = ctx_scnode->module;
5577 } else {
5578 mod = NULL;
5579 }
5580 break;
Radek Krejci8df109d2021-04-23 12:19:08 +02005581 case LY_VALUE_XML:
Michal Vasko52143d12021-04-14 15:36:39 +02005582 /* all nodes need to be prefixed */
5583 LOGVAL(set->ctx, LYVE_DATA, "Non-prefixed node \"%.*s\" in XML xpath found.", *qname_len, *qname);
5584 return LY_EVALID;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005585 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005586 }
5587
Michal Vasko6346ece2019-09-24 13:12:53 +02005588 *moveto_mod = mod;
5589 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005590}
5591
5592/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02005593 * @brief Move context @p set to the root. Handles absolute path.
5594 * Result is LYXP_SET_NODE_SET.
5595 *
5596 * @param[in,out] set Set to use.
5597 * @param[in] options Xpath options.
Michal Vaskob0099a92020-08-31 14:55:23 +02005598 * @return LY_ERR value.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005599 */
Michal Vaskob0099a92020-08-31 14:55:23 +02005600static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005601moveto_root(struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005602{
aPiecek8b0cc152021-05-31 16:40:31 +02005603 assert(!(options & LYXP_SKIP_EXPR));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005604
5605 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02005606 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskob0099a92020-08-31 14:55:23 +02005607 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, NULL, set->root_type, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005608 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005609 set->type = LYXP_SET_NODE_SET;
5610 set->used = 0;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005611 set_insert_node(set, NULL, 0, set->root_type, 0);
Michal Vasko306e2832022-07-25 09:15:17 +02005612 set->non_child_axis = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005613 }
Michal Vaskob0099a92020-08-31 14:55:23 +02005614
5615 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005616}
5617
5618/**
5619 * @brief Check @p node as a part of NameTest processing.
5620 *
5621 * @param[in] node Node to check.
Michal Vasko49fec8e2022-05-24 10:28:33 +02005622 * @param[in] node_type Node type of @p node.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005623 * @param[in] set Set to read general context from.
Michal Vaskod3678892020-05-21 10:06:58 +02005624 * @param[in] node_name Node name in the dictionary to move to, NULL for any node.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005625 * @param[in] moveto_mod Expected module of the node, NULL for no prefix.
Michal Vaskocdad7122020-11-09 21:04:44 +01005626 * @param[in] options XPath options.
Michal Vasko6346ece2019-09-24 13:12:53 +02005627 * @return LY_ERR (LY_ENOT if node does not match, LY_EINCOMPLETE on unresolved when,
Michal Vaskocd2c88a2022-06-07 10:54:34 +02005628 * LY_EINVAL if neither node nor any children match)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005629 */
5630static LY_ERR
Michal Vasko49fec8e2022-05-24 10:28:33 +02005631moveto_node_check(const struct lyd_node *node, enum lyxp_node_type node_type, const struct lyxp_set *set,
5632 const char *node_name, const struct lys_module *moveto_mod, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005633{
Michal Vasko49fec8e2022-05-24 10:28:33 +02005634 if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT)) {
5635 assert(node_type == set->root_type);
5636
5637 if (node_name || moveto_mod) {
5638 /* root will not match a specific node */
5639 return LY_ENOT;
5640 }
5641 return LY_SUCCESS;
5642 } else if (node_type != LYXP_NODE_ELEM) {
5643 /* other types will not match */
5644 return LY_ENOT;
5645 }
5646
Michal Vaskodca9f122021-07-16 13:56:22 +02005647 if (!node->schema) {
5648 /* opaque node never matches */
5649 return LY_ENOT;
5650 }
5651
Michal Vasko03ff5a72019-09-11 13:49:33 +02005652 /* module check */
Michal Vasko19089f02022-06-07 11:02:11 +02005653 if (moveto_mod) {
5654 if (!(node->flags & LYD_EXT) && (node->schema->module != moveto_mod)) {
5655 return LY_ENOT;
5656 } else if ((node->flags & LYD_EXT) && strcmp(node->schema->module->name, moveto_mod->name)) {
5657 return LY_ENOT;
5658 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005659 }
5660
Michal Vasko5c4e5892019-11-14 12:31:38 +01005661 /* context check */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005662 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (node->schema->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005663 return LY_EINVAL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005664 } else if (set->context_op && (node->schema->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) &&
5665 (node->schema != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005666 return LY_EINVAL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005667 }
5668
5669 /* name check */
Michal Vasko19089f02022-06-07 11:02:11 +02005670 if (node_name) {
5671 if (!(node->flags & LYD_EXT) && (node->schema->name != node_name)) {
5672 return LY_ENOT;
5673 } else if ((node->flags & LYD_EXT) && strcmp(node->schema->name, node_name)) {
5674 return LY_ENOT;
5675 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005676 }
5677
Michal Vaskoa1424542019-11-14 16:08:52 +01005678 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01005679 if (!(options & LYXP_IGNORE_WHEN) && lysc_has_when(node->schema) && !(node->flags & LYD_WHEN_TRUE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005680 return LY_EINCOMPLETE;
Michal Vaskoa1424542019-11-14 16:08:52 +01005681 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005682
5683 /* match */
5684 return LY_SUCCESS;
5685}
5686
5687/**
Michal Vasko49fec8e2022-05-24 10:28:33 +02005688 * @brief Get the next node in a forward DFS.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005689 *
Michal Vasko49fec8e2022-05-24 10:28:33 +02005690 * @param[in] iter Last returned node.
5691 * @param[in] stop Node to stop the search on and not return.
5692 * @return Next node, NULL if there are no more.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005693 */
Michal Vasko49fec8e2022-05-24 10:28:33 +02005694static const struct lyd_node *
5695moveto_axis_node_next_dfs_forward(const struct lyd_node *iter, const struct lyd_node *stop)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005696{
Michal Vasko49fec8e2022-05-24 10:28:33 +02005697 const struct lyd_node *next = NULL;
5698
5699 /* 1) child */
5700 next = lyd_child(iter);
5701 if (!next) {
5702 if (iter == stop) {
5703 /* reached stop, no more descendants */
5704 return NULL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005705 }
Michal Vasko49fec8e2022-05-24 10:28:33 +02005706 /* 2) child next sibling */
5707 next = iter->next;
5708 }
5709 while (!next) {
5710 iter = lyd_parent(iter);
5711 if ((!stop && !iter) || (stop && (lyd_parent(iter) == lyd_parent(stop)))) {
5712 return NULL;
5713 }
5714 next = iter->next;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005715 }
5716
Michal Vasko49fec8e2022-05-24 10:28:33 +02005717 return next;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005718}
5719
5720/**
Michal Vasko49fec8e2022-05-24 10:28:33 +02005721 * @brief Get the next node in a backward DFS.
5722 *
5723 * @param[in] iter Last returned node.
5724 * @param[in] stop Node to stop the search on and not return.
5725 * @return Next node, NULL if there are no more.
5726 */
5727static const struct lyd_node *
5728moveto_axis_node_next_dfs_backward(const struct lyd_node *iter, const struct lyd_node *stop)
5729{
5730 const struct lyd_node *next = NULL;
5731
5732 /* 1) previous sibling innermost last child */
5733 next = iter->prev->next ? iter->prev : NULL;
5734 while (next && lyd_child(next)) {
5735 next = lyd_child(next);
5736 next = next->prev;
5737 }
5738
5739 if (!next) {
5740 /* 2) parent */
5741 iter = lyd_parent(iter);
5742 if ((!stop && !iter) || (stop && (lyd_parent(iter) == lyd_parent(stop)))) {
5743 return NULL;
5744 }
5745 next = iter;
5746 }
5747
5748 return next;
5749}
5750
5751/**
5752 * @brief Get the first node on an axis for a context node.
5753 *
5754 * @param[in,out] iter NULL, updated to the next node.
5755 * @param[in,out] iter_type Node type 0 of @p iter, updated to the node type of the next node.
5756 * @param[in] node Context node.
5757 * @param[in] node_type Type of @p node.
5758 * @param[in] axis Axis to use.
5759 * @param[in] set XPath set with the general context.
5760 * @return LY_SUCCESS on success.
5761 * @return LY_ENOTFOUND if no next node found.
5762 */
5763static LY_ERR
5764moveto_axis_node_next_first(const struct lyd_node **iter, enum lyxp_node_type *iter_type, const struct lyd_node *node,
5765 enum lyxp_node_type node_type, enum lyxp_axis axis, struct lyxp_set *set)
5766{
5767 const struct lyd_node *next = NULL;
5768 enum lyxp_node_type next_type = 0;
5769
5770 assert(!*iter);
5771 assert(!*iter_type);
5772
5773 switch (axis) {
5774 case LYXP_AXIS_ANCESTOR_OR_SELF:
5775 case LYXP_AXIS_DESCENDANT_OR_SELF:
5776 case LYXP_AXIS_SELF:
5777 /* return the context node */
5778 next = node;
5779 next_type = node_type;
5780 break;
5781
5782 case LYXP_AXIS_ANCESTOR:
5783 case LYXP_AXIS_PARENT:
5784 if (node_type == LYXP_NODE_ELEM) {
5785 next = lyd_parent(node);
5786 next_type = next ? LYXP_NODE_ELEM : set->root_type;
5787 } else if (node_type == LYXP_NODE_TEXT) {
5788 next = node;
5789 next_type = LYXP_NODE_ELEM;
5790 } else if (node_type == LYXP_NODE_META) {
5791 next = ((struct lyd_meta *)node)->parent;
5792 next_type = LYXP_NODE_ELEM;
5793 } /* else root does not have a parent */
5794 break;
5795
5796 case LYXP_AXIS_CHILD:
5797 if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT)) {
5798 assert(!node);
5799
5800 /* search in all the trees */
5801 next = set->tree;
5802 next_type = next ? LYXP_NODE_ELEM : 0;
5803 } else {
5804 /* search in children */
5805 next = lyd_child(node);
5806 next_type = next ? LYXP_NODE_ELEM : 0;
5807 }
5808 break;
5809
5810 case LYXP_AXIS_DESCENDANT:
5811 if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT)) {
5812 /* top-level nodes */
5813 next = set->tree;
5814 next_type = LYXP_NODE_ELEM;
5815 } else if (node_type == LYXP_NODE_ELEM) {
5816 /* start from the context node */
5817 next = moveto_axis_node_next_dfs_forward(node, node);
5818 next_type = next ? LYXP_NODE_ELEM : 0;
5819 } /* else no children */
5820 break;
5821
5822 case LYXP_AXIS_FOLLOWING:
5823 case LYXP_AXIS_FOLLOWING_SIBLING:
5824 if (node_type == LYXP_NODE_ELEM) {
5825 /* first next sibling */
5826 next = node->next;
5827 next_type = next ? LYXP_NODE_ELEM : 0;
5828 } /* else no sibling */
5829 break;
5830
5831 case LYXP_AXIS_PRECEDING:
5832 if ((node_type == LYXP_NODE_ELEM) && node->prev->next) {
5833 /* skip ancestors */
5834 next = moveto_axis_node_next_dfs_backward(node, NULL);
5835 assert(next);
5836 next_type = LYXP_NODE_ELEM;
5837 } /* else no sibling */
5838 break;
5839
5840 case LYXP_AXIS_PRECEDING_SIBLING:
5841 if (node_type == LYXP_NODE_ELEM) {
5842 /* first previous sibling */
5843 next = node->prev->next ? node->prev : NULL;
5844 next_type = next ? LYXP_NODE_ELEM : 0;
5845 } /* else no sibling */
5846 break;
5847
5848 case LYXP_AXIS_ATTRIBUTE:
5849 /* handled specially */
5850 assert(0);
5851 LOGINT(set->ctx);
5852 break;
5853 }
5854
5855 *iter = next;
5856 *iter_type = next_type;
5857 return next_type ? LY_SUCCESS : LY_ENOTFOUND;
5858}
5859
5860/**
5861 * @brief Iterate over all nodes on an axis for a context node.
5862 *
5863 * @param[in,out] iter Last returned node, start with NULL, updated to the next node.
5864 * @param[in,out] iter_type Node type of @p iter, start with 0, updated to the node type of the next node.
5865 * @param[in] node Context node.
5866 * @param[in] node_type Type of @p node.
5867 * @param[in] axis Axis to use.
5868 * @param[in] set XPath set with the general context.
5869 * @return LY_SUCCESS on success.
5870 * @return LY_ENOTFOUND if no next node found.
5871 */
5872static LY_ERR
5873moveto_axis_node_next(const struct lyd_node **iter, enum lyxp_node_type *iter_type, const struct lyd_node *node,
5874 enum lyxp_node_type node_type, enum lyxp_axis axis, struct lyxp_set *set)
5875{
5876 const struct lyd_node *next = NULL;
5877 enum lyxp_node_type next_type = 0;
5878
5879 if (!*iter_type) {
5880 /* first returned node */
5881 return moveto_axis_node_next_first(iter, iter_type, node, node_type, axis, set);
5882 }
5883
5884 switch (axis) {
5885 case LYXP_AXIS_ANCESTOR_OR_SELF:
5886 if ((*iter == node) && (*iter_type == node_type)) {
5887 /* fake first ancestor, we returned self before */
5888 *iter = NULL;
5889 *iter_type = 0;
5890 return moveto_axis_node_next_first(iter, iter_type, node, node_type, LYXP_AXIS_ANCESTOR, set);
5891 } /* else continue ancestor */
5892
5893 /* fallthrough */
5894 case LYXP_AXIS_ANCESTOR:
5895 if (*iter_type == LYXP_NODE_ELEM) {
5896 /* iter parent */
5897 next = lyd_parent(*iter);
5898 next_type = next ? LYXP_NODE_ELEM : set->root_type;
5899 } /* else root, no ancestors */
5900 break;
5901
5902 case LYXP_AXIS_CHILD:
5903 assert(*iter_type == LYXP_NODE_ELEM);
5904
5905 /* next sibling (child) */
5906 next = (*iter)->next;
5907 next_type = next ? LYXP_NODE_ELEM : 0;
5908 break;
5909
5910 case LYXP_AXIS_DESCENDANT_OR_SELF:
5911 if ((*iter == node) && (*iter_type == node_type)) {
5912 /* fake first descendant, we returned self before */
5913 *iter = NULL;
5914 *iter_type = 0;
5915 return moveto_axis_node_next_first(iter, iter_type, node, node_type, LYXP_AXIS_DESCENDANT, set);
5916 } /* else continue descendant */
5917
5918 /* fallthrough */
5919 case LYXP_AXIS_DESCENDANT:
5920 assert(*iter_type == LYXP_NODE_ELEM);
5921 next = moveto_axis_node_next_dfs_forward(*iter, node);
5922 next_type = next ? LYXP_NODE_ELEM : 0;
5923 break;
5924
5925 case LYXP_AXIS_FOLLOWING:
5926 assert(*iter_type == LYXP_NODE_ELEM);
5927 next = moveto_axis_node_next_dfs_forward(*iter, NULL);
5928 next_type = next ? LYXP_NODE_ELEM : 0;
5929 break;
5930
5931 case LYXP_AXIS_FOLLOWING_SIBLING:
5932 assert(*iter_type == LYXP_NODE_ELEM);
5933
5934 /* next sibling */
5935 next = (*iter)->next;
5936 next_type = next ? LYXP_NODE_ELEM : 0;
5937 break;
5938
5939 case LYXP_AXIS_PARENT:
5940 case LYXP_AXIS_SELF:
5941 /* parent/self was returned before */
5942 break;
5943
5944 case LYXP_AXIS_PRECEDING:
5945 assert(*iter_type == LYXP_NODE_ELEM);
5946 next = moveto_axis_node_next_dfs_backward(*iter, NULL);
5947 next_type = next ? LYXP_NODE_ELEM : 0;
5948 break;
5949
5950 case LYXP_AXIS_PRECEDING_SIBLING:
5951 assert(*iter_type == LYXP_NODE_ELEM);
5952
5953 /* previous sibling */
5954 next = (*iter)->prev->next ? (*iter)->prev : NULL;
5955 next_type = next ? LYXP_NODE_ELEM : 0;
5956 break;
5957
5958 case LYXP_AXIS_ATTRIBUTE:
5959 /* handled specially */
5960 assert(0);
5961 LOGINT(set->ctx);
5962 break;
5963 }
5964
5965 *iter = next;
5966 *iter_type = next_type;
5967 return next_type ? LY_SUCCESS : LY_ENOTFOUND;
5968}
5969
5970/**
5971 * @brief Move context @p set to a node. Result is LYXP_SET_NODE_SET. Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005972 *
5973 * @param[in,out] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005974 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005975 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko49fec8e2022-05-24 10:28:33 +02005976 * @param[in] axis Axis to search on.
Michal Vaskocdad7122020-11-09 21:04:44 +01005977 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005978 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5979 */
5980static LY_ERR
Michal Vasko49fec8e2022-05-24 10:28:33 +02005981moveto_node(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, enum lyxp_axis axis,
5982 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005983{
Michal Vasko230cf972021-12-02 12:31:00 +01005984 LY_ERR r, rc = LY_SUCCESS;
Michal Vasko49fec8e2022-05-24 10:28:33 +02005985 const struct lyd_node *iter;
5986 enum lyxp_node_type iter_type;
Michal Vasko230cf972021-12-02 12:31:00 +01005987 struct lyxp_set result;
Michal Vasko49fec8e2022-05-24 10:28:33 +02005988 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005989
aPiecek8b0cc152021-05-31 16:40:31 +02005990 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005991 return LY_SUCCESS;
5992 }
5993
5994 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005995 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005996 return LY_EVALID;
5997 }
5998
Michal Vasko230cf972021-12-02 12:31:00 +01005999 /* init result set */
6000 set_init(&result, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006001
Michal Vasko49fec8e2022-05-24 10:28:33 +02006002 for (i = 0; i < set->used; ++i) {
6003 /* iterate over all the nodes on the axis of the node */
6004 iter = NULL;
6005 iter_type = 0;
6006 while (!moveto_axis_node_next(&iter, &iter_type, set->val.nodes[i].node, set->val.nodes[i].type, axis, set)) {
6007 r = moveto_node_check(iter, iter_type, set, ncname, moveto_mod, options);
6008 if (r == LY_EINCOMPLETE) {
Michal Vasko230cf972021-12-02 12:31:00 +01006009 rc = r;
6010 goto cleanup;
Michal Vasko49fec8e2022-05-24 10:28:33 +02006011 } else if (r) {
6012 continue;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006013 }
Michal Vasko49fec8e2022-05-24 10:28:33 +02006014
6015 /* check for duplicates if they are possible */
6016 switch (axis) {
6017 case LYXP_AXIS_ANCESTOR:
6018 case LYXP_AXIS_ANCESTOR_OR_SELF:
6019 case LYXP_AXIS_DESCENDANT:
6020 case LYXP_AXIS_DESCENDANT_OR_SELF:
6021 case LYXP_AXIS_FOLLOWING:
6022 case LYXP_AXIS_FOLLOWING_SIBLING:
6023 case LYXP_AXIS_PARENT:
6024 case LYXP_AXIS_PRECEDING:
6025 case LYXP_AXIS_PRECEDING_SIBLING:
Michal Vasko306e2832022-07-25 09:15:17 +02006026 result.non_child_axis = 1;
Michal Vasko49fec8e2022-05-24 10:28:33 +02006027 if (set_dup_node_check(&result, iter, iter_type, -1)) {
6028 continue;
6029 }
6030 break;
6031 case LYXP_AXIS_CHILD:
6032 case LYXP_AXIS_SELF:
6033 break;
6034 case LYXP_AXIS_ATTRIBUTE:
6035 /* handled specially */
6036 assert(0);
6037 LOGINT(set->ctx);
6038 break;
6039 }
6040
6041 /* matching node */
6042 set_insert_node(&result, iter, 0, iter_type, result.used);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006043 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006044 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006045
Michal Vasko230cf972021-12-02 12:31:00 +01006046 /* move result to the set */
6047 lyxp_set_free_content(set);
6048 *set = result;
6049 result.type = LYXP_SET_NUMBER;
Michal Vasko49fec8e2022-05-24 10:28:33 +02006050
Michal Vasko306e2832022-07-25 09:15:17 +02006051 /* sort the final set if the document order could have been broken */
6052 if (set->non_child_axis) {
6053 set_sort(set);
6054 } else {
6055 assert(!set_sort(set));
6056 }
Michal Vasko230cf972021-12-02 12:31:00 +01006057
6058cleanup:
6059 lyxp_set_free_content(&result);
6060 return rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006061}
6062
6063/**
Michal Vasko49fec8e2022-05-24 10:28:33 +02006064 * @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 +02006065 *
6066 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02006067 * @param[in] scnode Matching node schema.
Michal Vasko004d3152020-06-11 19:59:22 +02006068 * @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 +01006069 * @param[in] options XPath options.
Michal Vaskod3678892020-05-21 10:06:58 +02006070 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6071 */
6072static LY_ERR
Michal Vasko49fec8e2022-05-24 10:28:33 +02006073moveto_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 +01006074 uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02006075{
Michal Vaskoddd76592022-01-17 13:34:48 +01006076 LY_ERR ret = LY_SUCCESS, r;
Michal Vaskod3678892020-05-21 10:06:58 +02006077 uint32_t i;
Michal Vaskod3678892020-05-21 10:06:58 +02006078 const struct lyd_node *siblings;
Michal Vasko230cf972021-12-02 12:31:00 +01006079 struct lyxp_set result;
Michal Vasko004d3152020-06-11 19:59:22 +02006080 struct lyd_node *sub, *inst = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02006081
Michal Vasko004d3152020-06-11 19:59:22 +02006082 assert(scnode && (!(scnode->nodetype & (LYS_LIST | LYS_LEAFLIST)) || predicates));
Michal Vaskod3678892020-05-21 10:06:58 +02006083
Michal Vasko50aaa072021-12-02 13:11:56 +01006084 /* init result set */
6085 set_init(&result, set);
6086
aPiecek8b0cc152021-05-31 16:40:31 +02006087 if (options & LYXP_SKIP_EXPR) {
Michal Vasko004d3152020-06-11 19:59:22 +02006088 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02006089 }
6090
6091 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006092 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko004d3152020-06-11 19:59:22 +02006093 ret = LY_EVALID;
6094 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02006095 }
6096
6097 /* context check for all the nodes since we have the schema node */
6098 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) {
6099 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02006100 goto cleanup;
Michal Vasko69730152020-10-09 16:30:07 +02006101 } else if (set->context_op && (scnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) &&
6102 (scnode != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02006103 lyxp_set_free_content(set);
6104 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +02006105 }
6106
6107 /* create specific data instance if needed */
6108 if (scnode->nodetype == LYS_LIST) {
6109 LY_CHECK_GOTO(ret = lyd_create_list(scnode, predicates, &inst), cleanup);
6110 } else if (scnode->nodetype == LYS_LEAFLIST) {
6111 LY_CHECK_GOTO(ret = lyd_create_term2(scnode, &predicates[0].value, &inst), cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02006112 }
6113
Michal Vasko230cf972021-12-02 12:31:00 +01006114 for (i = 0; i < set->used; ++i) {
Michal Vaskod3678892020-05-21 10:06:58 +02006115 siblings = NULL;
6116
6117 if ((set->val.nodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.nodes[i].type == LYXP_NODE_ROOT)) {
6118 assert(!set->val.nodes[i].node);
6119
6120 /* search in all the trees */
6121 siblings = set->tree;
6122 } else if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
6123 /* search in children */
Radek Krejcia1c1e542020-09-29 16:06:52 +02006124 siblings = lyd_child(set->val.nodes[i].node);
Michal Vaskod3678892020-05-21 10:06:58 +02006125 }
6126
6127 /* find the node using hashes */
Michal Vasko004d3152020-06-11 19:59:22 +02006128 if (inst) {
Michal Vaskoddd76592022-01-17 13:34:48 +01006129 r = lyd_find_sibling_first(siblings, inst, &sub);
Michal Vaskod3678892020-05-21 10:06:58 +02006130 } else {
Michal Vaskoddd76592022-01-17 13:34:48 +01006131 r = lyd_find_sibling_val(siblings, scnode, NULL, 0, &sub);
Michal Vaskod3678892020-05-21 10:06:58 +02006132 }
Michal Vaskoddd76592022-01-17 13:34:48 +01006133 LY_CHECK_ERR_GOTO(r && (r != LY_ENOTFOUND), ret = r, cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02006134
6135 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01006136 if (!(options & LYXP_IGNORE_WHEN) && sub && lysc_has_when(sub->schema) && !(sub->flags & LYD_WHEN_TRUE)) {
Michal Vasko004d3152020-06-11 19:59:22 +02006137 ret = LY_EINCOMPLETE;
6138 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02006139 }
6140
6141 if (sub) {
6142 /* pos filled later */
Michal Vasko230cf972021-12-02 12:31:00 +01006143 set_insert_node(&result, sub, 0, LYXP_NODE_ELEM, result.used);
Michal Vaskod3678892020-05-21 10:06:58 +02006144 }
6145 }
6146
Michal Vasko230cf972021-12-02 12:31:00 +01006147 /* move result to the set */
6148 lyxp_set_free_content(set);
6149 *set = result;
6150 result.type = LYXP_SET_NUMBER;
6151 assert(!set_sort(set));
6152
Michal Vasko004d3152020-06-11 19:59:22 +02006153cleanup:
Michal Vasko230cf972021-12-02 12:31:00 +01006154 lyxp_set_free_content(&result);
Michal Vasko004d3152020-06-11 19:59:22 +02006155 lyd_free_tree(inst);
6156 return ret;
Michal Vaskod3678892020-05-21 10:06:58 +02006157}
6158
6159/**
Michal Vasko49fec8e2022-05-24 10:28:33 +02006160 * @brief Check @p node as a part of schema NameTest processing.
6161 *
6162 * @param[in] node Schema node to check.
6163 * @param[in] ctx_scnode Context node.
6164 * @param[in] set Set to read general context from.
6165 * @param[in] node_name Node name in the dictionary to move to, NULL for any nodes.
6166 * @param[in] moveto_mod Expected module of the node, NULL for no prefix.
6167 * @return LY_ERR (LY_ENOT if node does not match, LY_EINVAL if neither node nor any children match)
6168 */
6169static LY_ERR
6170moveto_scnode_check(const struct lysc_node *node, const struct lysc_node *ctx_scnode, const struct lyxp_set *set,
6171 const char *node_name, const struct lys_module *moveto_mod)
6172{
6173 if (!moveto_mod && node_name) {
6174 switch (set->format) {
6175 case LY_VALUE_SCHEMA:
6176 case LY_VALUE_SCHEMA_RESOLVED:
6177 /* use current module */
6178 moveto_mod = set->cur_mod;
6179 break;
6180 case LY_VALUE_JSON:
6181 case LY_VALUE_LYB:
6182 case LY_VALUE_STR_NS:
6183 /* inherit module of the context node, if any */
6184 if (ctx_scnode) {
6185 moveto_mod = ctx_scnode->module;
6186 }
6187 break;
6188 case LY_VALUE_CANON:
6189 case LY_VALUE_XML:
6190 /* not defined */
6191 LOGINT(set->ctx);
6192 return LY_EINVAL;
6193 }
6194 }
6195
6196 if (!node) {
6197 /* root will not match a specific node */
6198 if (node_name || moveto_mod) {
6199 return LY_ENOT;
6200 }
6201 return LY_SUCCESS;
6202 }
6203
6204 /* module check */
6205 if (moveto_mod && (node->module != moveto_mod)) {
6206 return LY_ENOT;
6207 }
6208
6209 /* context check */
6210 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (node->flags & LYS_CONFIG_R)) {
6211 return LY_EINVAL;
6212 } else if (set->context_op && (node->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && (node != set->context_op)) {
6213 return LY_EINVAL;
6214 }
6215
6216 /* name check */
6217 if (node_name && (node->name != node_name)) {
6218 return LY_ENOT;
6219 }
6220
6221 /* match */
6222 return LY_SUCCESS;
6223}
6224
6225/**
6226 * @brief Get the next node in a forward schema node DFS.
6227 *
6228 * @param[in] iter Last returned node.
6229 * @param[in] stop Node to stop the search on and not return.
6230 * @param[in] getnext_opts Options for ::lys_getnext().
6231 * @return Next node, NULL if there are no more.
6232 */
6233static const struct lysc_node *
6234moveto_axis_scnode_next_dfs_forward(const struct lysc_node *iter, const struct lysc_node *stop, uint32_t getnext_opts)
6235{
6236 const struct lysc_node *next = NULL;
6237
6238 next = lysc_node_child(iter);
6239 if (!next) {
6240 /* no children, try siblings */
Michal Vasko34a22fe2022-06-15 07:58:55 +02006241 if ((iter == stop) || !lysc_data_parent(iter)) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02006242 /* we are done, no next element to process */
6243 return NULL;
6244 }
6245
6246 next = lys_getnext(iter, lysc_data_parent(iter), NULL, getnext_opts);
6247 }
6248 while (!next && iter) {
6249 /* parent is already processed, go to its sibling */
6250 iter = iter->parent;
Michal Vasko34a22fe2022-06-15 07:58:55 +02006251 if ((iter == stop) || !lysc_data_parent(iter)) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02006252 /* we are done, no next element to process */
6253 return NULL;
6254 }
6255 next = lys_getnext(iter, lysc_data_parent(iter), NULL, getnext_opts);
6256 }
6257
6258 return next;
6259}
6260
6261/**
6262 * @brief Consider schema node based on its in_ctx enum value.
6263 *
6264 * @param[in,out] in_ctx In_ctx enum of the schema node, may be updated.
6265 * @param[in] axis Axis to use.
6266 * @return LY_SUCCESS on success.
6267 * @return LY_ENOT if the node should not be returned.
6268 */
6269static LY_ERR
6270moveto_axis_scnode_next_in_ctx(int32_t *in_ctx, enum lyxp_axis axis)
6271{
6272 switch (axis) {
6273 case LYXP_AXIS_SELF:
6274 if ((*in_ctx == LYXP_SET_SCNODE_START) || (*in_ctx == LYXP_SET_SCNODE_ATOM_CTX)) {
6275 /* additionally put the start node into context */
6276 *in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
6277 return LY_SUCCESS;
6278 }
6279 break;
6280 case LYXP_AXIS_PARENT:
6281 case LYXP_AXIS_ANCESTOR_OR_SELF:
6282 case LYXP_AXIS_ANCESTOR:
6283 case LYXP_AXIS_DESCENDANT_OR_SELF:
6284 case LYXP_AXIS_DESCENDANT:
6285 case LYXP_AXIS_FOLLOWING:
6286 case LYXP_AXIS_FOLLOWING_SIBLING:
6287 case LYXP_AXIS_PRECEDING:
6288 case LYXP_AXIS_PRECEDING_SIBLING:
6289 case LYXP_AXIS_CHILD:
6290 if (*in_ctx == LYXP_SET_SCNODE_START) {
6291 /* remember that context node was used */
6292 *in_ctx = LYXP_SET_SCNODE_START_USED;
6293 return LY_SUCCESS;
6294 } else if (*in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
6295 /* traversed */
6296 *in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
6297 return LY_SUCCESS;
6298 }
6299 break;
6300 case LYXP_AXIS_ATTRIBUTE:
6301 /* unreachable */
6302 assert(0);
6303 LOGINT(NULL);
6304 break;
6305 }
6306
6307 return LY_ENOT;
6308}
6309
6310/**
6311 * @brief Get previous sibling for a schema node.
6312 *
6313 * @param[in] scnode Schema node.
6314 * @param[in] getnext_opts Options for ::lys_getnext().
6315 * @return Previous sibling, NULL if none.
6316 */
6317static const struct lysc_node *
6318moveto_axis_scnode_preceding_sibling(const struct lysc_node *scnode, uint32_t getnext_opts)
6319{
6320 const struct lysc_node *next = NULL, *prev = NULL;
6321
6322 while ((next = lys_getnext(next, lysc_data_parent(scnode), scnode->module->compiled, getnext_opts))) {
6323 if (next == scnode) {
6324 break;
6325 }
6326
6327 prev = next;
6328 }
6329
6330 return prev;
6331}
6332
6333/**
6334 * @brief Get the first schema node on an axis for a context node.
6335 *
6336 * @param[in,out] iter Last returned node, start with NULL, updated to the next node.
6337 * @param[in,out] iter_type Node type of @p iter, start with 0, updated to the node type of the next node.
6338 * @param[in,out] iter_mod Internal module iterator, do not change.
6339 * @param[in,out] iter_mod_idx Internal module index iterator, do not change.
6340 * @param[in] scnode Context node.
6341 * @param[in] node_type Type of @p scnode.
6342 * @param[in] in_ctx In_ctx enum of @p scnode.
6343 * @param[in] axis Axis to use.
6344 * @param[in] set XPath set with the general context.
6345 * @param[in] getnext_opts Options for ::lys_getnext().
6346 * @return LY_SUCCESS on success.
6347 * @return LY_ENOTFOUND if no next node found.
6348 */
6349static LY_ERR
6350moveto_axis_scnode_next_first(const struct lysc_node **iter, enum lyxp_node_type *iter_type, const struct lys_module **iter_mod,
6351 uint32_t *iter_mod_idx, const struct lysc_node *scnode, enum lyxp_node_type node_type, enum lyxp_axis axis,
6352 struct lyxp_set *set, uint32_t getnext_opts)
6353{
6354 const struct lysc_node *next = NULL;
6355 enum lyxp_node_type next_type = 0;
6356
6357 assert(!*iter);
6358 assert(!*iter_type);
6359
6360 *iter_mod = NULL;
6361 *iter_mod_idx = 0;
6362
6363 switch (axis) {
6364 case LYXP_AXIS_ANCESTOR_OR_SELF:
6365 case LYXP_AXIS_DESCENDANT_OR_SELF:
6366 case LYXP_AXIS_SELF:
6367 if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT) || (node_type == LYXP_NODE_ELEM)) {
6368 /* just return the node */
6369 next = scnode;
6370 next_type = node_type;
6371 }
6372 break;
6373
6374 case LYXP_AXIS_ANCESTOR:
6375 case LYXP_AXIS_PARENT:
6376 if (node_type == LYXP_NODE_ELEM) {
6377 next = lysc_data_parent(scnode);
6378 next_type = next ? LYXP_NODE_ELEM : set->root_type;
6379 } /* else no parent */
6380 break;
6381
6382 case LYXP_AXIS_DESCENDANT:
6383 case LYXP_AXIS_CHILD:
6384 if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT)) {
6385 /* it can actually be in any module, it's all <running>, and even if it's moveto_mod (if set),
6386 * it can be in a top-level augment */
6387 while ((*iter_mod = ly_ctx_get_module_iter(set->ctx, iter_mod_idx))) {
6388 /* module may not be implemented or not compiled yet */
6389 if (!(*iter_mod)->compiled) {
6390 continue;
6391 }
6392
6393 /* get next node */
6394 if ((next = lys_getnext(NULL, NULL, (*iter_mod)->compiled, getnext_opts))) {
6395 next_type = LYXP_NODE_ELEM;
6396 break;
6397 }
6398 }
6399 } else if (node_type == LYXP_NODE_ELEM) {
6400 /* get next node */
6401 next = lys_getnext(NULL, scnode, NULL, getnext_opts);
6402 next_type = next ? LYXP_NODE_ELEM : 0;
6403 }
6404 break;
6405
6406 case LYXP_AXIS_FOLLOWING:
6407 case LYXP_AXIS_FOLLOWING_SIBLING:
6408 if (node_type == LYXP_NODE_ELEM) {
6409 /* first next sibling */
6410 next = lys_getnext(scnode, lysc_data_parent(scnode), scnode->module->compiled, getnext_opts);
6411 next_type = next ? LYXP_NODE_ELEM : 0;
6412 } /* else no sibling */
6413 break;
6414
6415 case LYXP_AXIS_PRECEDING:
6416 case LYXP_AXIS_PRECEDING_SIBLING:
6417 if (node_type == LYXP_NODE_ELEM) {
6418 /* first parent sibling */
6419 next = lys_getnext(NULL, lysc_data_parent(scnode), scnode->module->compiled, getnext_opts);
6420 if (next == scnode) {
6421 /* no preceding sibling */
6422 next = NULL;
6423 }
6424 next_type = next ? LYXP_NODE_ELEM : 0;
6425 } /* else no sibling */
6426 break;
6427
6428 case LYXP_AXIS_ATTRIBUTE:
6429 /* unreachable */
6430 assert(0);
6431 LOGINT(set->ctx);
6432 break;
6433 }
6434
6435 *iter = next;
6436 *iter_type = next_type;
6437 return next_type ? LY_SUCCESS : LY_ENOTFOUND;
6438}
6439
6440/**
6441 * @brief Iterate over all schema nodes on an axis for a context node.
6442 *
6443 * @param[in,out] iter Last returned node, start with NULL, updated to the next node.
6444 * @param[in,out] iter_type Node type of @p iter, start with 0, updated to the node type of the next node.
6445 * @param[in,out] iter_mod Internal module iterator, do not change.
6446 * @param[in,out] iter_mod_idx Internal module index iterator, do not change.
6447 * @param[in] scnode Context node.
6448 * @param[in] node_type Type of @p scnode.
6449 * @param[in] axis Axis to use.
6450 * @param[in] set XPath set with the general context.
6451 * @param[in] getnext_opts Options for ::lys_getnext().
6452 * @return LY_SUCCESS on success.
6453 * @return LY_ENOTFOUND if no next node found.
6454 */
6455static LY_ERR
6456moveto_axis_scnode_next(const struct lysc_node **iter, enum lyxp_node_type *iter_type, const struct lys_module **iter_mod,
6457 uint32_t *iter_mod_idx, const struct lysc_node *scnode, enum lyxp_node_type node_type, enum lyxp_axis axis,
6458 struct lyxp_set *set, uint32_t getnext_opts)
6459{
6460 const struct lysc_node *next = NULL, *dfs_stop;
6461 enum lyxp_node_type next_type = 0;
6462
6463 if (!*iter_type) {
6464 /* first returned node */
6465 return moveto_axis_scnode_next_first(iter, iter_type, iter_mod, iter_mod_idx, scnode, node_type, axis, set,
6466 getnext_opts);
6467 }
6468
6469 switch (axis) {
6470 case LYXP_AXIS_PARENT:
6471 case LYXP_AXIS_SELF:
6472 /* parent/self was returned before */
6473 break;
6474
6475 case LYXP_AXIS_ANCESTOR_OR_SELF:
6476 if ((*iter == scnode) && (*iter_type == node_type)) {
6477 /* fake first ancestor, we returned self before */
6478 *iter = NULL;
6479 *iter_type = 0;
6480 return moveto_axis_scnode_next_first(iter, iter_type, iter_mod, iter_mod_idx, scnode, node_type,
6481 LYXP_AXIS_ANCESTOR, set, getnext_opts);
6482 } /* else continue ancestor */
6483
6484 /* fallthrough */
6485 case LYXP_AXIS_ANCESTOR:
6486 if (*iter_type == LYXP_NODE_ELEM) {
6487 next = lysc_data_parent(*iter);
6488 next_type = next ? LYXP_NODE_ELEM : set->root_type;
6489 } /* else no ancestor */
6490 break;
6491
6492 case LYXP_AXIS_DESCENDANT_OR_SELF:
6493 if ((*iter == scnode) && (*iter_type == node_type)) {
6494 /* fake first descendant, we returned self before */
6495 *iter = NULL;
6496 *iter_type = 0;
6497 return moveto_axis_scnode_next_first(iter, iter_type, iter_mod, iter_mod_idx, scnode, node_type,
6498 LYXP_AXIS_DESCENDANT, set, getnext_opts);
6499 } /* else DFS until context node */
6500 dfs_stop = scnode;
6501
6502 /* fallthrough */
6503 case LYXP_AXIS_DESCENDANT:
6504 if (axis == LYXP_AXIS_DESCENDANT) {
6505 /* DFS until the context node */
6506 dfs_stop = scnode;
6507 }
6508
6509 /* fallthrough */
6510 case LYXP_AXIS_PRECEDING:
6511 if (axis == LYXP_AXIS_PRECEDING) {
6512 /* DFS until the previous sibling */
6513 dfs_stop = moveto_axis_scnode_preceding_sibling(scnode, getnext_opts);
6514 assert(dfs_stop);
6515
6516 if (*iter == dfs_stop) {
6517 /* we are done */
6518 break;
6519 }
6520 }
6521
6522 /* fallthrough */
6523 case LYXP_AXIS_FOLLOWING:
6524 if (axis == LYXP_AXIS_FOLLOWING) {
6525 /* DFS through the whole module */
6526 dfs_stop = NULL;
6527 }
6528
6529 /* nested nodes */
6530 assert(*iter);
6531 next = moveto_axis_scnode_next_dfs_forward(*iter, dfs_stop, getnext_opts);
6532 if (next) {
6533 next_type = LYXP_NODE_ELEM;
6534 break;
6535 } /* else get next top-level node just like a child */
6536
6537 /* fallthrough */
6538 case LYXP_AXIS_CHILD:
6539 case LYXP_AXIS_FOLLOWING_SIBLING:
6540 if (!*iter_mod) {
6541 /* nodes from a single module */
6542 if ((next = lys_getnext(*iter, lysc_data_parent(*iter), (*iter)->module->compiled, getnext_opts))) {
6543 next_type = LYXP_NODE_ELEM;
6544 break;
6545 }
6546
6547 assert(scnode);
6548 if (!lysc_data_parent(scnode)) {
6549 /* iterating over top-level nodes, find next */
6550 while (lysc_data_parent(*iter)) {
6551 *iter = lysc_data_parent(*iter);
6552 }
6553 if ((next = lys_getnext(*iter, NULL, (*iter)->module->compiled, getnext_opts))) {
6554 next_type = LYXP_NODE_ELEM;
6555 break;
6556 }
6557 }
6558 }
6559
6560 while (*iter_mod) {
6561 /* module top-level nodes */
6562 if ((next = lys_getnext(*iter, NULL, (*iter_mod)->compiled, getnext_opts))) {
6563 next_type = LYXP_NODE_ELEM;
6564 break;
6565 }
6566
6567 /* get next module */
6568 while ((*iter_mod = ly_ctx_get_module_iter(set->ctx, iter_mod_idx))) {
6569 /* module may not be implemented or not compiled yet */
6570 if ((*iter_mod)->compiled) {
6571 break;
6572 }
6573 }
6574
6575 /* new module, start over */
6576 *iter = NULL;
6577 }
6578 break;
6579
6580 case LYXP_AXIS_PRECEDING_SIBLING:
6581 assert(*iter);
6582
6583 /* next parent sibling until scnode */
6584 next = lys_getnext(*iter, lysc_data_parent(*iter), (*iter)->module->compiled, getnext_opts);
6585 if (next == scnode) {
6586 /* no previous sibling */
6587 next = NULL;
6588 }
6589 next_type = next ? LYXP_NODE_ELEM : 0;
6590 break;
6591
6592 case LYXP_AXIS_ATTRIBUTE:
6593 /* unreachable */
6594 assert(0);
6595 LOGINT(set->ctx);
6596 break;
6597 }
6598
6599 *iter = next;
6600 *iter_type = next_type;
6601 return next_type ? LY_SUCCESS : LY_ENOTFOUND;
6602}
6603
6604/**
Michal Vaskod3678892020-05-21 10:06:58 +02006605 * @brief Move context @p set to a schema node. Result is LYXP_SET_SCNODE_SET (or LYXP_SET_EMPTY).
6606 *
6607 * @param[in,out] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006608 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02006609 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko49fec8e2022-05-24 10:28:33 +02006610 * @param[in] axis Axis to search on.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006611 * @param[in] options XPath options.
6612 * @return LY_ERR
6613 */
6614static LY_ERR
Michal Vasko49fec8e2022-05-24 10:28:33 +02006615moveto_scnode(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, enum lyxp_axis axis,
6616 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006617{
Radek Krejci857189e2020-09-01 13:26:36 +02006618 ly_bool temp_ctx = 0;
Michal Vasko49fec8e2022-05-24 10:28:33 +02006619 uint32_t getnext_opts, orig_used, i, mod_idx, idx;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006620 const struct lys_module *mod;
Michal Vasko49fec8e2022-05-24 10:28:33 +02006621 const struct lysc_node *iter;
6622 enum lyxp_node_type iter_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006623
aPiecek8b0cc152021-05-31 16:40:31 +02006624 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006625 return LY_SUCCESS;
6626 }
6627
6628 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006629 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006630 return LY_EVALID;
6631 }
6632
Michal Vaskocafad9d2019-11-07 15:20:03 +01006633 /* getnext opts */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01006634 getnext_opts = 0;
Michal Vaskocafad9d2019-11-07 15:20:03 +01006635 if (options & LYXP_SCNODE_OUTPUT) {
6636 getnext_opts |= LYS_GETNEXT_OUTPUT;
6637 }
6638
Michal Vasko03ff5a72019-09-11 13:49:33 +02006639 orig_used = set->used;
6640 for (i = 0; i < orig_used; ++i) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02006641 /* update in_ctx first */
6642 if (moveto_axis_scnode_next_in_ctx(&set->val.scnodes[i].in_ctx, axis)) {
6643 /* not usable, skip */
6644 continue;
6645 }
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006646
Michal Vasko49fec8e2022-05-24 10:28:33 +02006647 iter = NULL;
6648 iter_type = 0;
6649 while (!moveto_axis_scnode_next(&iter, &iter_type, &mod, &mod_idx, set->val.scnodes[i].scnode,
6650 set->val.scnodes[i].type, axis, set, getnext_opts)) {
6651 if (moveto_scnode_check(iter, NULL, set, ncname, moveto_mod)) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006652 continue;
6653 }
6654
Michal Vasko49fec8e2022-05-24 10:28:33 +02006655 /* insert */
6656 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, iter_type, &idx));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006657
Michal Vasko49fec8e2022-05-24 10:28:33 +02006658 /* we need to prevent these nodes from being considered in this moveto */
6659 if ((idx < orig_used) && (idx > i)) {
6660 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX;
6661 temp_ctx = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006662 }
6663 }
6664 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006665
6666 /* correct temporary in_ctx values */
6667 if (temp_ctx) {
6668 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006669 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_NEW_CTX) {
6670 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006671 }
6672 }
6673 }
6674
6675 return LY_SUCCESS;
6676}
6677
6678/**
Michal Vasko49fec8e2022-05-24 10:28:33 +02006679 * @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 +02006680 * Context position aware.
6681 *
6682 * @param[in] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006683 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02006684 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vaskocdad7122020-11-09 21:04:44 +01006685 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006686 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6687 */
6688static LY_ERR
Michal Vasko49fec8e2022-05-24 10:28:33 +02006689moveto_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 +02006690{
6691 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006692 const struct lyd_node *next, *elem, *start;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006693 struct lyxp_set ret_set;
6694 LY_ERR rc;
6695
aPiecek8b0cc152021-05-31 16:40:31 +02006696 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006697 return LY_SUCCESS;
6698 }
6699
6700 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006701 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006702 return LY_EVALID;
6703 }
6704
Michal Vasko9f96a052020-03-10 09:41:45 +01006705 /* 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 +02006706 rc = xpath_pi_node(set, LYXP_AXIS_CHILD, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006707 LY_CHECK_RET(rc);
6708
Michal Vasko6346ece2019-09-24 13:12:53 +02006709 /* this loop traverses all the nodes in the set and adds/keeps only those that match qname */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006710 set_init(&ret_set, set);
6711 for (i = 0; i < set->used; ++i) {
6712
6713 /* TREE DFS */
6714 start = set->val.nodes[i].node;
6715 for (elem = next = start; elem; elem = next) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02006716 rc = moveto_node_check(elem, LYXP_NODE_ELEM, set, ncname, moveto_mod, options);
Michal Vasko6346ece2019-09-24 13:12:53 +02006717 if (!rc) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006718 /* add matching node into result set */
6719 set_insert_node(&ret_set, elem, 0, LYXP_NODE_ELEM, ret_set.used);
6720 if (set_dup_node_check(set, elem, LYXP_NODE_ELEM, i)) {
6721 /* the node is a duplicate, we'll process it later in the set */
6722 goto skip_children;
6723 }
Michal Vasko6346ece2019-09-24 13:12:53 +02006724 } else if (rc == LY_EINCOMPLETE) {
Michal Vasko6346ece2019-09-24 13:12:53 +02006725 return rc;
6726 } else if (rc == LY_EINVAL) {
6727 goto skip_children;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006728 }
6729
6730 /* TREE DFS NEXT ELEM */
6731 /* select element for the next run - children first */
Radek Krejcia1c1e542020-09-29 16:06:52 +02006732 next = lyd_child(elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006733 if (!next) {
6734skip_children:
6735 /* no children, so try siblings, but only if it's not the start,
6736 * that is considered to be the root and it's siblings are not traversed */
6737 if (elem != start) {
6738 next = elem->next;
6739 } else {
6740 break;
6741 }
6742 }
6743 while (!next) {
6744 /* no siblings, go back through the parents */
Michal Vasko9e685082021-01-29 14:49:09 +01006745 if (lyd_parent(elem) == start) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006746 /* we are done, no next element to process */
6747 break;
6748 }
6749 /* parent is already processed, go to its sibling */
Michal Vasko9e685082021-01-29 14:49:09 +01006750 elem = lyd_parent(elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006751 next = elem->next;
6752 }
6753 }
6754 }
6755
6756 /* make the temporary set the current one */
6757 ret_set.ctx_pos = set->ctx_pos;
6758 ret_set.ctx_size = set->ctx_size;
Michal Vaskod3678892020-05-21 10:06:58 +02006759 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006760 memcpy(set, &ret_set, sizeof *set);
Michal Vasko306e2832022-07-25 09:15:17 +02006761 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006762
6763 return LY_SUCCESS;
6764}
6765
6766/**
Michal Vasko49fec8e2022-05-24 10:28:33 +02006767 * @brief Move context @p set to a child schema node and all its descendants starting from a node.
6768 * Result is LYXP_SET_NODE_SET.
6769 *
6770 * @param[in] set Set to use.
6771 * @param[in] start Start node whose subtree to add.
6772 * @param[in] start_idx Index of @p start in @p set.
6773 * @param[in] moveto_mod Matching node module, NULL for no prefix.
6774 * @param[in] ncname Matching node name in the dictionary, NULL for any.
6775 * @param[in] options XPath options.
6776 * @return LY_ERR value.
6777 */
6778static LY_ERR
6779moveto_scnode_dfs(struct lyxp_set *set, const struct lysc_node *start, uint32_t start_idx,
6780 const struct lys_module *moveto_mod, const char *ncname, uint32_t options)
6781{
6782 const struct lysc_node *next, *elem;
6783 uint32_t idx;
6784 LY_ERR rc;
6785
6786 /* TREE DFS */
6787 for (elem = next = start; elem; elem = next) {
6788 if ((elem == start) || (elem->nodetype & (LYS_CHOICE | LYS_CASE))) {
6789 /* schema-only nodes, skip root */
6790 goto next_iter;
6791 }
6792
6793 rc = moveto_scnode_check(elem, start, set, ncname, moveto_mod);
6794 if (!rc) {
6795 if (lyxp_set_scnode_contains(set, elem, LYXP_NODE_ELEM, start_idx, &idx)) {
6796 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
6797 if (idx > start_idx) {
6798 /* we will process it later in the set */
6799 goto skip_children;
6800 }
6801 } else {
6802 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, elem, LYXP_NODE_ELEM, NULL));
6803 }
6804 } else if (rc == LY_EINVAL) {
6805 goto skip_children;
6806 }
6807
6808next_iter:
6809 /* TREE DFS NEXT ELEM */
6810 /* select element for the next run - children first */
6811 next = lysc_node_child(elem);
6812 if (next && (next->nodetype == LYS_INPUT) && (options & LYXP_SCNODE_OUTPUT)) {
6813 next = next->next;
6814 } else if (next && (next->nodetype == LYS_OUTPUT) && !(options & LYXP_SCNODE_OUTPUT)) {
6815 next = next->next;
6816 }
6817 if (!next) {
6818skip_children:
6819 /* no children, so try siblings, but only if it's not the start,
6820 * that is considered to be the root and it's siblings are not traversed */
6821 if (elem != start) {
6822 next = elem->next;
6823 } else {
6824 break;
6825 }
6826 }
6827 while (!next) {
6828 /* no siblings, go back through the parents */
6829 if (elem->parent == start) {
6830 /* we are done, no next element to process */
6831 break;
6832 }
6833 /* parent is already processed, go to its sibling */
6834 elem = elem->parent;
6835 next = elem->next;
6836 }
6837 }
6838
6839 return LY_SUCCESS;
6840}
6841
6842/**
6843 * @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 +02006844 *
6845 * @param[in] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006846 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02006847 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006848 * @param[in] options XPath options.
Michal Vasko49fec8e2022-05-24 10:28:33 +02006849 * @return LY_ERR value.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006850 */
6851static LY_ERR
Michal Vasko49fec8e2022-05-24 10:28:33 +02006852moveto_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 +02006853{
Michal Vasko49fec8e2022-05-24 10:28:33 +02006854 uint32_t i, orig_used, mod_idx;
6855 const struct lys_module *mod;
6856 const struct lysc_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006857
aPiecek8b0cc152021-05-31 16:40:31 +02006858 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006859 return LY_SUCCESS;
6860 }
6861
6862 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006863 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006864 return LY_EVALID;
6865 }
6866
Michal Vasko03ff5a72019-09-11 13:49:33 +02006867 orig_used = set->used;
6868 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006869 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
6870 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006871 continue;
6872 }
6873
6874 /* remember context node */
Radek Krejcif13b87b2020-12-01 22:02:17 +01006875 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vaskoec4df482019-12-16 10:02:18 +01006876 } else {
Michal Vasko1a09b212021-05-06 13:00:10 +02006877 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006878 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006879
Michal Vasko49fec8e2022-05-24 10:28:33 +02006880 if ((set->val.scnodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.scnodes[i].type == LYXP_NODE_ROOT)) {
6881 /* traverse all top-level nodes in all the modules */
6882 mod_idx = 0;
6883 while ((mod = ly_ctx_get_module_iter(set->ctx, &mod_idx))) {
6884 /* module may not be implemented or not compiled yet */
6885 if (!mod->compiled) {
6886 continue;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006887 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006888
Michal Vasko49fec8e2022-05-24 10:28:33 +02006889 root = NULL;
6890 /* no getnext opts needed */
6891 while ((root = lys_getnext(root, NULL, mod->compiled, 0))) {
6892 LY_CHECK_RET(moveto_scnode_dfs(set, root, i, moveto_mod, ncname, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006893 }
6894 }
Michal Vasko49fec8e2022-05-24 10:28:33 +02006895
6896 } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
6897 /* add all the descendants recursively */
6898 LY_CHECK_RET(moveto_scnode_dfs(set, set->val.scnodes[i].scnode, i, moveto_mod, ncname, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006899 }
6900 }
6901
6902 return LY_SUCCESS;
6903}
6904
6905/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02006906 * @brief Move context @p set to an attribute. Result is LYXP_SET_NODE_SET.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006907 * Indirectly context position aware.
6908 *
6909 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02006910 * @param[in] mod Matching metadata module, NULL for any.
6911 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
aPiecek8b0cc152021-05-31 16:40:31 +02006912 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006913 * @return LY_ERR
6914 */
6915static LY_ERR
aPiecek8b0cc152021-05-31 16:40:31 +02006916moveto_attr(struct lyxp_set *set, const struct lys_module *mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006917{
Michal Vasko9f96a052020-03-10 09:41:45 +01006918 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006919
aPiecek8b0cc152021-05-31 16:40:31 +02006920 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006921 return LY_SUCCESS;
6922 }
6923
6924 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006925 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006926 return LY_EVALID;
6927 }
6928
Radek Krejci1deb5be2020-08-26 16:43:36 +02006929 for (uint32_t i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02006930 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006931
6932 /* only attributes of an elem (not dummy) can be in the result, skip all the rest;
6933 * our attributes are always qualified */
Michal Vasko5c4e5892019-11-14 12:31:38 +01006934 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01006935 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006936
6937 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02006938 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006939 continue;
6940 }
6941
Michal Vaskod3678892020-05-21 10:06:58 +02006942 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006943 /* match */
6944 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01006945 set->val.meta[i].meta = sub;
6946 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006947 /* pos does not change */
6948 replaced = 1;
6949 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01006950 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 +02006951 }
6952 ++i;
6953 }
6954 }
6955 }
6956
6957 if (!replaced) {
6958 /* no match */
6959 set_remove_node(set, i);
6960 }
6961 }
6962
6963 return LY_SUCCESS;
6964}
6965
6966/**
6967 * @brief Move context @p set1 to union with @p set2. @p set2 is emptied afterwards.
Michal Vasko61ac2f62020-05-25 12:39:51 +02006968 * Result is LYXP_SET_NODE_SET. Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006969 *
6970 * @param[in,out] set1 Set to use for the result.
6971 * @param[in] set2 Set that is copied to @p set1.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006972 * @return LY_ERR
6973 */
6974static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006975moveto_union(struct lyxp_set *set1, struct lyxp_set *set2)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006976{
6977 LY_ERR rc;
6978
Michal Vaskod3678892020-05-21 10:06:58 +02006979 if ((set1->type != LYXP_SET_NODE_SET) || (set2->type != LYXP_SET_NODE_SET)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006980 LOGVAL(set1->ctx, LY_VCODE_XP_INOP_2, "union", print_set_type(set1), print_set_type(set2));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006981 return LY_EVALID;
6982 }
6983
6984 /* set2 is empty or both set1 and set2 */
Michal Vaskod3678892020-05-21 10:06:58 +02006985 if (!set2->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006986 return LY_SUCCESS;
6987 }
6988
Michal Vaskod3678892020-05-21 10:06:58 +02006989 if (!set1->used) {
aPiecekadc1e4f2021-10-07 11:15:12 +02006990 /* release hidden allocated data (lyxp_set.size) */
6991 lyxp_set_free_content(set1);
6992 /* direct copying of the entire structure */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006993 memcpy(set1, set2, sizeof *set1);
6994 /* dynamic memory belongs to set1 now, do not free */
Michal Vaskod3678892020-05-21 10:06:58 +02006995 memset(set2, 0, sizeof *set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006996 return LY_SUCCESS;
6997 }
6998
6999 /* we assume sets are sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007000 assert(!set_sort(set1) && !set_sort(set2));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007001
7002 /* sort, remove duplicates */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007003 rc = set_sorted_merge(set1, set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007004 LY_CHECK_RET(rc);
7005
7006 /* final set must be sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007007 assert(!set_sort(set1));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007008
7009 return LY_SUCCESS;
7010}
7011
7012/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02007013 * @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 +02007014 * Context position aware.
7015 *
7016 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02007017 * @param[in] mod Matching metadata module, NULL for any.
7018 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
Michal Vaskocdad7122020-11-09 21:04:44 +01007019 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007020 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7021 */
7022static int
Michal Vaskocdad7122020-11-09 21:04:44 +01007023moveto_attr_alldesc(struct lyxp_set *set, const struct lys_module *mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007024{
Michal Vasko9f96a052020-03-10 09:41:45 +01007025 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007026 struct lyxp_set *set_all_desc = NULL;
7027 LY_ERR rc;
7028
aPiecek8b0cc152021-05-31 16:40:31 +02007029 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007030 return LY_SUCCESS;
7031 }
7032
7033 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01007034 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007035 return LY_EVALID;
7036 }
7037
Michal Vasko03ff5a72019-09-11 13:49:33 +02007038 /* can be optimized similarly to moveto_node_alldesc() and save considerable amount of memory,
7039 * but it likely won't be used much, so it's a waste of time */
7040 /* copy the context */
7041 set_all_desc = set_copy(set);
7042 /* get all descendant nodes (the original context nodes are removed) */
Michal Vasko49fec8e2022-05-24 10:28:33 +02007043 rc = moveto_node_alldesc_child(set_all_desc, NULL, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007044 if (rc != LY_SUCCESS) {
7045 lyxp_set_free(set_all_desc);
7046 return rc;
7047 }
7048 /* prepend the original context nodes */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007049 rc = moveto_union(set, set_all_desc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007050 if (rc != LY_SUCCESS) {
7051 lyxp_set_free(set_all_desc);
7052 return rc;
7053 }
7054 lyxp_set_free(set_all_desc);
7055
Radek Krejci1deb5be2020-08-26 16:43:36 +02007056 for (uint32_t i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02007057 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007058
7059 /* only attributes of an elem can be in the result, skip all the rest,
7060 * we have all attributes qualified in lyd tree */
7061 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01007062 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007063 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02007064 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007065 continue;
7066 }
7067
Michal Vaskod3678892020-05-21 10:06:58 +02007068 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007069 /* match */
7070 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01007071 set->val.meta[i].meta = sub;
7072 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007073 /* pos does not change */
7074 replaced = 1;
7075 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01007076 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 +02007077 }
7078 ++i;
7079 }
7080 }
7081 }
7082
7083 if (!replaced) {
7084 /* no match */
7085 set_remove_node(set, i);
7086 }
7087 }
7088
7089 return LY_SUCCESS;
7090}
7091
7092/**
Michal Vasko8abcecc2022-07-28 09:55:01 +02007093 * @brief Move context @p set1 single item to the result of a comparison.
7094 *
7095 * @param[in] set1 First set with the item to compare.
7096 * @param[in] idx1 Index of the item in @p set1.
7097 * @param[in] set2 Second set.
7098 * @param[in] op Comparison operator to process.
7099 * @param[in] switch_operands Whether to switch sets as operands; whether it is `set1 op set2` or `set2 op set1`.
7100 * @param[out] result Result of the comparison.
7101 * @return LY_ERR value.
7102 */
7103static LY_ERR
7104moveto_op_comp_item(const struct lyxp_set *set1, uint32_t idx1, struct lyxp_set *set2, const char *op,
7105 ly_bool switch_operands, ly_bool *result)
7106{
7107 struct lyxp_set tmp1 = {0};
7108 LY_ERR rc = LY_SUCCESS;
7109
7110 assert(set1->type == LYXP_SET_NODE_SET);
7111
7112 /* cast set1 */
7113 switch (set2->type) {
7114 case LYXP_SET_NUMBER:
7115 rc = set_comp_cast(&tmp1, set1, LYXP_SET_NUMBER, idx1);
7116 break;
7117 case LYXP_SET_BOOLEAN:
7118 rc = set_comp_cast(&tmp1, set1, LYXP_SET_BOOLEAN, idx1);
7119 break;
7120 default:
7121 rc = set_comp_cast(&tmp1, set1, LYXP_SET_STRING, idx1);
7122 break;
7123 }
7124 LY_CHECK_GOTO(rc, cleanup);
7125
7126 /* canonize set2 */
7127 LY_CHECK_GOTO(rc = set_comp_canonize(set2, &set1->val.nodes[idx1]), cleanup);
7128
7129 /* compare recursively and store the result */
7130 if (switch_operands) {
7131 LY_CHECK_GOTO(rc = moveto_op_comp(set2, &tmp1, op), cleanup);
7132 *result = set2->val.bln;
7133 } else {
7134 LY_CHECK_GOTO(rc = moveto_op_comp(&tmp1, set2, op), cleanup);
7135 *result = tmp1.val.bln;
7136 }
7137
7138cleanup:
7139 lyxp_set_free_content(&tmp1);
7140 return rc;
7141}
7142
7143/**
7144 * @brief Move context @p set1 to the result of a comparison. Handles '=', '!=', '<=', '<', '>=', or '>'.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007145 * Result is LYXP_SET_BOOLEAN. Indirectly context position aware.
7146 *
7147 * @param[in,out] set1 Set to use for the result.
7148 * @param[in] set2 Set acting as the second operand for @p op.
7149 * @param[in] op Comparison operator to process.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007150 * @return LY_ERR
7151 */
7152static LY_ERR
Michal Vasko8abcecc2022-07-28 09:55:01 +02007153moveto_op_comp(struct lyxp_set *set1, struct lyxp_set *set2, const char *op)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007154{
7155 /*
7156 * NODE SET + NODE SET = NODE SET + STRING /(1 NODE SET) 2 STRING
7157 * NODE SET + STRING = STRING + STRING /1 STRING (2 STRING)
7158 * NODE SET + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
7159 * NODE SET + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
7160 * STRING + NODE SET = STRING + STRING /(1 STRING) 2 STRING
7161 * NUMBER + NODE SET = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
7162 * BOOLEAN + NODE SET = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
7163 *
7164 * '=' or '!='
7165 * BOOLEAN + BOOLEAN
7166 * BOOLEAN + STRING = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
7167 * BOOLEAN + NUMBER = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
7168 * STRING + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
7169 * NUMBER + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
7170 * NUMBER + NUMBER
7171 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
7172 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
7173 * STRING + STRING
7174 *
7175 * '<=', '<', '>=', '>'
7176 * NUMBER + NUMBER
7177 * BOOLEAN + BOOLEAN = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
7178 * BOOLEAN + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
7179 * BOOLEAN + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
7180 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
7181 * STRING + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
7182 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
7183 * NUMBER + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
7184 * STRING + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
7185 */
Michal Vasko8abcecc2022-07-28 09:55:01 +02007186 ly_bool result;
7187 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007188 LY_ERR rc;
7189
Michal Vasko03ff5a72019-09-11 13:49:33 +02007190 /* iterative evaluation with node-sets */
7191 if ((set1->type == LYXP_SET_NODE_SET) || (set2->type == LYXP_SET_NODE_SET)) {
7192 if (set1->type == LYXP_SET_NODE_SET) {
7193 for (i = 0; i < set1->used; ++i) {
Michal Vasko8abcecc2022-07-28 09:55:01 +02007194 /* evaluate for the single item */
7195 LY_CHECK_RET(moveto_op_comp_item(set1, i, set2, op, 0, &result));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007196
7197 /* lazy evaluation until true */
Michal Vasko8abcecc2022-07-28 09:55:01 +02007198 if (result) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007199 set_fill_boolean(set1, 1);
7200 return LY_SUCCESS;
7201 }
7202 }
7203 } else {
7204 for (i = 0; i < set2->used; ++i) {
Michal Vasko8abcecc2022-07-28 09:55:01 +02007205 /* evaluate for the single item */
7206 LY_CHECK_RET(moveto_op_comp_item(set2, i, set1, op, 1, &result));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007207
7208 /* lazy evaluation until true */
Michal Vasko8abcecc2022-07-28 09:55:01 +02007209 if (result) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007210 set_fill_boolean(set1, 1);
7211 return LY_SUCCESS;
7212 }
7213 }
7214 }
7215
Michal Vasko8abcecc2022-07-28 09:55:01 +02007216 /* false for all the nodes */
Michal Vasko03ff5a72019-09-11 13:49:33 +02007217 set_fill_boolean(set1, 0);
7218 return LY_SUCCESS;
7219 }
7220
7221 /* first convert properly */
7222 if ((op[0] == '=') || (op[0] == '!')) {
7223 if ((set1->type == LYXP_SET_BOOLEAN) || (set2->type == LYXP_SET_BOOLEAN)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007224 lyxp_set_cast(set1, LYXP_SET_BOOLEAN);
7225 lyxp_set_cast(set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007226 } else if ((set1->type == LYXP_SET_NUMBER) || (set2->type == LYXP_SET_NUMBER)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007227 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007228 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007229 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007230 LY_CHECK_RET(rc);
7231 } /* else we have 2 strings */
7232 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007233 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007234 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007235 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007236 LY_CHECK_RET(rc);
7237 }
7238
7239 assert(set1->type == set2->type);
7240
7241 /* compute result */
7242 if (op[0] == '=') {
7243 if (set1->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +02007244 result = (set1->val.bln == set2->val.bln);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007245 } else if (set1->type == LYXP_SET_NUMBER) {
7246 result = (set1->val.num == set2->val.num);
7247 } else {
7248 assert(set1->type == LYXP_SET_STRING);
Michal Vaskoac6c72f2019-11-14 16:09:34 +01007249 result = !strcmp(set1->val.str, set2->val.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007250 }
7251 } else if (op[0] == '!') {
7252 if (set1->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +02007253 result = (set1->val.bln != set2->val.bln);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007254 } else if (set1->type == LYXP_SET_NUMBER) {
7255 result = (set1->val.num != set2->val.num);
7256 } else {
7257 assert(set1->type == LYXP_SET_STRING);
Michal Vaskoc2058432020-11-06 17:26:21 +01007258 result = strcmp(set1->val.str, set2->val.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007259 }
7260 } else {
7261 assert(set1->type == LYXP_SET_NUMBER);
7262 if (op[0] == '<') {
7263 if (op[1] == '=') {
7264 result = (set1->val.num <= set2->val.num);
7265 } else {
7266 result = (set1->val.num < set2->val.num);
7267 }
7268 } else {
7269 if (op[1] == '=') {
7270 result = (set1->val.num >= set2->val.num);
7271 } else {
7272 result = (set1->val.num > set2->val.num);
7273 }
7274 }
7275 }
7276
7277 /* assign result */
7278 if (result) {
7279 set_fill_boolean(set1, 1);
7280 } else {
7281 set_fill_boolean(set1, 0);
7282 }
7283
7284 return LY_SUCCESS;
7285}
7286
7287/**
7288 * @brief Move context @p set to the result of a basic operation. Handles '+', '-', unary '-', '*', 'div',
7289 * or 'mod'. Result is LYXP_SET_NUMBER. Indirectly context position aware.
7290 *
7291 * @param[in,out] set1 Set to use for the result.
7292 * @param[in] set2 Set acting as the second operand for @p op.
7293 * @param[in] op Operator to process.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007294 * @return LY_ERR
7295 */
7296static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007297moveto_op_math(struct lyxp_set *set1, struct lyxp_set *set2, const char *op)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007298{
7299 LY_ERR rc;
7300
7301 /* unary '-' */
7302 if (!set2 && (op[0] == '-')) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007303 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007304 LY_CHECK_RET(rc);
7305 set1->val.num *= -1;
7306 lyxp_set_free(set2);
7307 return LY_SUCCESS;
7308 }
7309
7310 assert(set1 && set2);
7311
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007312 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007313 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007314 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007315 LY_CHECK_RET(rc);
7316
7317 switch (op[0]) {
7318 /* '+' */
7319 case '+':
7320 set1->val.num += set2->val.num;
7321 break;
7322
7323 /* '-' */
7324 case '-':
7325 set1->val.num -= set2->val.num;
7326 break;
7327
7328 /* '*' */
7329 case '*':
7330 set1->val.num *= set2->val.num;
7331 break;
7332
7333 /* 'div' */
7334 case 'd':
7335 set1->val.num /= set2->val.num;
7336 break;
7337
7338 /* 'mod' */
7339 case 'm':
7340 set1->val.num = ((long long)set1->val.num) % ((long long)set2->val.num);
7341 break;
7342
7343 default:
7344 LOGINT_RET(set1->ctx);
7345 }
7346
7347 return LY_SUCCESS;
7348}
7349
Michal Vasko03ff5a72019-09-11 13:49:33 +02007350/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02007351 * @brief Evaluate Predicate. Logs directly on error.
7352 *
Michal Vaskod3678892020-05-21 10:06:58 +02007353 * [9] Predicate ::= '[' Expr ']'
Michal Vasko03ff5a72019-09-11 13:49:33 +02007354 *
7355 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007356 * @param[in] tok_idx Position in the expression @p exp.
aPiecek8b0cc152021-05-31 16:40:31 +02007357 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007358 * @param[in] options XPath options.
Michal Vasko49fec8e2022-05-24 10:28:33 +02007359 * @param[in] axis Axis to search on.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007360 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7361 */
7362static LY_ERR
Michal Vasko49fec8e2022-05-24 10:28:33 +02007363eval_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 +02007364{
7365 LY_ERR rc;
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01007366 uint16_t orig_exp;
7367 uint32_t i, orig_pos, orig_size;
Michal Vasko5c4e5892019-11-14 12:31:38 +01007368 int32_t pred_in_ctx;
Michal Vasko88a9e802022-05-24 10:50:28 +02007369 ly_bool reverse_axis = 0;
aPiecekfff4dca2021-10-07 10:59:53 +02007370 struct lyxp_set set2 = {0};
Michal Vasko03ff5a72019-09-11 13:49:33 +02007371
7372 /* '[' */
aPiecek8b0cc152021-05-31 16:40:31 +02007373 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02007374 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007375 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007376
aPiecek8b0cc152021-05-31 16:40:31 +02007377 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007378only_parse:
aPiecek8b0cc152021-05-31 16:40:31 +02007379 rc = eval_expr_select(exp, tok_idx, 0, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007380 LY_CHECK_RET(rc);
7381 } else if (set->type == LYXP_SET_NODE_SET) {
7382 /* 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 +01007383 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007384
7385 /* empty set, nothing to evaluate */
7386 if (!set->used) {
7387 goto only_parse;
7388 }
7389
Michal Vasko49fec8e2022-05-24 10:28:33 +02007390 /* decide forward or reverse axis */
7391 switch (axis) {
7392 case LYXP_AXIS_ANCESTOR:
7393 case LYXP_AXIS_ANCESTOR_OR_SELF:
7394 case LYXP_AXIS_PRECEDING:
7395 case LYXP_AXIS_PRECEDING_SIBLING:
7396 reverse_axis = 1;
7397 break;
7398 case LYXP_AXIS_DESCENDANT:
7399 case LYXP_AXIS_DESCENDANT_OR_SELF:
7400 case LYXP_AXIS_FOLLOWING:
7401 case LYXP_AXIS_FOLLOWING_SIBLING:
7402 case LYXP_AXIS_PARENT:
7403 case LYXP_AXIS_CHILD:
7404 case LYXP_AXIS_SELF:
7405 case LYXP_AXIS_ATTRIBUTE:
7406 reverse_axis = 0;
7407 break;
7408 }
7409
Michal Vasko004d3152020-06-11 19:59:22 +02007410 orig_exp = *tok_idx;
Michal Vasko49fec8e2022-05-24 10:28:33 +02007411 orig_pos = reverse_axis ? set->used + 1 : 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007412 orig_size = set->used;
Michal Vasko39dbf352020-05-21 10:08:59 +02007413 for (i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007414 set_init(&set2, set);
7415 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 +02007416
7417 /* remember the node context position for position() and context size for last() */
7418 orig_pos += reverse_axis ? -1 : 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007419
7420 set2.ctx_pos = orig_pos;
7421 set2.ctx_size = orig_size;
Michal Vasko004d3152020-06-11 19:59:22 +02007422 *tok_idx = orig_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007423
Michal Vasko004d3152020-06-11 19:59:22 +02007424 rc = eval_expr_select(exp, tok_idx, 0, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007425 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02007426 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007427 return rc;
7428 }
7429
Michal Vasko49fec8e2022-05-24 10:28:33 +02007430 /* number is a proximity position */
Michal Vasko03ff5a72019-09-11 13:49:33 +02007431 if (set2.type == LYXP_SET_NUMBER) {
7432 if ((long long)set2.val.num == orig_pos) {
7433 set2.val.num = 1;
7434 } else {
7435 set2.val.num = 0;
7436 }
7437 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007438 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007439
7440 /* predicate satisfied or not? */
Michal Vasko004d3152020-06-11 19:59:22 +02007441 if (!set2.val.bln) {
Michal Vasko2caefc12019-11-14 16:07:56 +01007442 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007443 }
7444 }
Michal Vasko2caefc12019-11-14 16:07:56 +01007445 set_remove_nodes_none(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007446
7447 } else if (set->type == LYXP_SET_SCNODE_SET) {
7448 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01007449 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007450 /* there is a currently-valid node */
7451 break;
7452 }
7453 }
7454 /* empty set, nothing to evaluate */
7455 if (i == set->used) {
7456 goto only_parse;
7457 }
7458
Michal Vasko004d3152020-06-11 19:59:22 +02007459 orig_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007460
Michal Vasko03ff5a72019-09-11 13:49:33 +02007461 /* set special in_ctx to all the valid snodes */
7462 pred_in_ctx = set_scnode_new_in_ctx(set);
7463
7464 /* use the valid snodes one-by-one */
7465 for (i = 0; i < set->used; ++i) {
7466 if (set->val.scnodes[i].in_ctx != pred_in_ctx) {
7467 continue;
7468 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01007469 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007470
Michal Vasko004d3152020-06-11 19:59:22 +02007471 *tok_idx = orig_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007472
Michal Vasko004d3152020-06-11 19:59:22 +02007473 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007474 LY_CHECK_RET(rc);
7475
7476 set->val.scnodes[i].in_ctx = pred_in_ctx;
7477 }
7478
7479 /* restore the state as it was before the predicate */
7480 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01007481 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007482 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007483 } else if (set->val.scnodes[i].in_ctx == pred_in_ctx) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01007484 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007485 }
7486 }
7487
7488 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02007489 set2.type = LYXP_SET_NODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007490 set_fill_set(&set2, set);
7491
Michal Vasko004d3152020-06-11 19:59:22 +02007492 rc = eval_expr_select(exp, tok_idx, 0, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007493 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02007494 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007495 return rc;
7496 }
7497
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007498 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +02007499 if (!set2.val.bln) {
Michal Vaskod3678892020-05-21 10:06:58 +02007500 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007501 }
Michal Vaskod3678892020-05-21 10:06:58 +02007502 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007503 }
7504
7505 /* ']' */
Michal Vasko004d3152020-06-11 19:59:22 +02007506 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_BRACK2);
aPiecek8b0cc152021-05-31 16:40:31 +02007507 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02007508 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007509 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007510
7511 return LY_SUCCESS;
7512}
7513
7514/**
Michal Vaskod3678892020-05-21 10:06:58 +02007515 * @brief Evaluate Literal. Logs directly on error.
7516 *
7517 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007518 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02007519 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7520 */
7521static void
Michal Vasko40308e72020-10-20 16:38:40 +02007522eval_literal(const struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set)
Michal Vaskod3678892020-05-21 10:06:58 +02007523{
7524 if (set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007525 if (exp->tok_len[*tok_idx] == 2) {
Michal Vaskod3678892020-05-21 10:06:58 +02007526 set_fill_string(set, "", 0);
7527 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007528 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 +02007529 }
7530 }
7531 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02007532 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007533 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007534}
7535
7536/**
Michal Vasko004d3152020-06-11 19:59:22 +02007537 * @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 +02007538 *
Michal Vasko004d3152020-06-11 19:59:22 +02007539 * @param[in] exp Full parsed XPath expression.
7540 * @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 +02007541 * @param[in] ctx_node Found schema node as the context for the predicate.
7542 * @param[in] cur_mod Current module for the expression.
7543 * @param[in] cur_node Current (original context) node.
Michal Vasko004d3152020-06-11 19:59:22 +02007544 * @param[in] format Format of any prefixes in key names/values.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007545 * @param[in] prefix_data Format-specific prefix data (see ::ly_resolve_prefix).
Michal Vasko004d3152020-06-11 19:59:22 +02007546 * @param[out] predicates Parsed predicates.
7547 * @param[out] pred_type Type of @p predicates.
7548 * @return LY_SUCCESS on success,
7549 * @return LY_ERR on any error.
Michal Vaskod3678892020-05-21 10:06:58 +02007550 */
7551static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007552eval_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 +02007553 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 +02007554 struct ly_path_predicate **predicates, enum ly_path_pred_type *pred_type)
Michal Vaskod3678892020-05-21 10:06:58 +02007555{
Michal Vasko004d3152020-06-11 19:59:22 +02007556 LY_ERR ret = LY_SUCCESS;
7557 uint16_t key_count, e_idx, pred_idx = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02007558 const struct lysc_node *key;
Michal Vasko004d3152020-06-11 19:59:22 +02007559 size_t pred_len;
Radek Krejci1deb5be2020-08-26 16:43:36 +02007560 uint32_t prev_lo;
Michal Vasko004d3152020-06-11 19:59:22 +02007561 struct lyxp_expr *exp2 = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02007562
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007563 assert(ctx_node->nodetype & (LYS_LIST | LYS_LEAFLIST));
Michal Vaskod3678892020-05-21 10:06:58 +02007564
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007565 if (ctx_node->nodetype == LYS_LIST) {
Michal Vasko004d3152020-06-11 19:59:22 +02007566 /* get key count */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007567 if (ctx_node->flags & LYS_KEYLESS) {
Michal Vasko004d3152020-06-11 19:59:22 +02007568 return LY_EINVAL;
7569 }
Michal Vasko544e58a2021-01-28 14:33:41 +01007570 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 +02007571 assert(key_count);
Michal Vaskod3678892020-05-21 10:06:58 +02007572
Michal Vasko004d3152020-06-11 19:59:22 +02007573 /* learn where the predicates end */
7574 e_idx = *tok_idx;
7575 while (key_count) {
7576 /* '[' */
7577 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
7578 return LY_EINVAL;
7579 }
7580 ++e_idx;
7581
Michal Vasko3354d272021-04-06 09:40:06 +02007582 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_NAMETEST)) {
7583 /* definitely not a key */
7584 return LY_EINVAL;
7585 }
7586
Michal Vasko004d3152020-06-11 19:59:22 +02007587 /* ']' */
7588 while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) {
7589 ++e_idx;
7590 }
7591 ++e_idx;
7592
7593 /* another presumably key predicate parsed */
7594 --key_count;
7595 }
Michal Vasko004d3152020-06-11 19:59:22 +02007596 } else {
7597 /* learn just where this single predicate ends */
7598 e_idx = *tok_idx;
7599
Michal Vaskod3678892020-05-21 10:06:58 +02007600 /* '[' */
Michal Vasko004d3152020-06-11 19:59:22 +02007601 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
7602 return LY_EINVAL;
7603 }
7604 ++e_idx;
Michal Vaskod3678892020-05-21 10:06:58 +02007605
Michal Vasko3354d272021-04-06 09:40:06 +02007606 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_DOT)) {
7607 /* definitely not the value */
7608 return LY_EINVAL;
7609 }
7610
Michal Vaskod3678892020-05-21 10:06:58 +02007611 /* ']' */
Michal Vasko004d3152020-06-11 19:59:22 +02007612 while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) {
7613 ++e_idx;
7614 }
7615 ++e_idx;
Michal Vaskod3678892020-05-21 10:06:58 +02007616 }
7617
Michal Vasko004d3152020-06-11 19:59:22 +02007618 /* get the joined length of all the keys predicates/of the single leaf-list predicate */
7619 pred_len = (exp->tok_pos[e_idx - 1] + exp->tok_len[e_idx - 1]) - exp->tok_pos[*tok_idx];
7620
7621 /* turn logging off */
7622 prev_lo = ly_log_options(0);
7623
7624 /* parse the predicate(s) */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007625 LY_CHECK_GOTO(ret = ly_path_parse_predicate(ctx_node->module->ctx, ctx_node, exp->expr + exp->tok_pos[*tok_idx],
7626 pred_len, LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &exp2), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02007627
7628 /* compile */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007629 ret = ly_path_compile_predicate(ctx_node->module->ctx, cur_node, cur_mod, ctx_node, exp2, &pred_idx, format,
7630 prefix_data, predicates, pred_type);
Michal Vasko004d3152020-06-11 19:59:22 +02007631 LY_CHECK_GOTO(ret, cleanup);
7632
7633 /* success, the predicate must include all the needed information for hash-based search */
7634 *tok_idx = e_idx;
7635
7636cleanup:
7637 ly_log_options(prev_lo);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007638 lyxp_expr_free(ctx_node->module->ctx, exp2);
Michal Vasko004d3152020-06-11 19:59:22 +02007639 return ret;
Michal Vaskod3678892020-05-21 10:06:58 +02007640}
7641
7642/**
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007643 * @brief Search for/check the next schema node that could be the only matching schema node meaning the
7644 * data node(s) could be found using a single hash-based search.
7645 *
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007646 * @param[in] ctx libyang context.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007647 * @param[in] node Next context node to check.
7648 * @param[in] name Expected node name.
7649 * @param[in] name_len Length of @p name.
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007650 * @param[in] moveto_mod Expected node module, can be NULL for JSON format with no prefix.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007651 * @param[in] root_type XPath root type.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007652 * @param[in] format Prefix format.
7653 * @param[in,out] found Previously found node, is updated.
7654 * @return LY_SUCCESS on success,
7655 * @return LY_ENOT if the whole check failed and hashes cannot be used.
7656 */
7657static LY_ERR
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007658eval_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 +02007659 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 +02007660 const struct lysc_node **found)
7661{
7662 const struct lysc_node *scnode;
7663 const struct lys_module *mod;
7664 uint32_t idx = 0;
7665
Radek Krejci8df109d2021-04-23 12:19:08 +02007666 assert((format == LY_VALUE_JSON) || moveto_mod);
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007667
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007668continue_search:
Michal Vasko7d1d0912020-10-16 08:38:30 +02007669 scnode = NULL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007670 if (!node) {
Radek Krejci8df109d2021-04-23 12:19:08 +02007671 if ((format == LY_VALUE_JSON) && !moveto_mod) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007672 /* search all modules for a single match */
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007673 while ((mod = ly_ctx_get_module_iter(ctx, &idx))) {
Michal Vasko35a3b1d2021-07-14 09:34:16 +02007674 if (!mod->implemented) {
7675 continue;
7676 }
7677
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007678 scnode = lys_find_child(NULL, mod, name, name_len, 0, 0);
7679 if (scnode) {
7680 /* we have found a match */
7681 break;
7682 }
7683 }
7684
Michal Vasko7d1d0912020-10-16 08:38:30 +02007685 if (!scnode) {
7686 /* all modules searched */
7687 idx = 0;
7688 }
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007689 } else {
7690 /* search in top-level */
7691 scnode = lys_find_child(NULL, moveto_mod, name, name_len, 0, 0);
7692 }
7693 } else if (!*found || (lysc_data_parent(*found) != node->schema)) {
Radek Krejci8df109d2021-04-23 12:19:08 +02007694 if ((format == LY_VALUE_JSON) && !moveto_mod) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007695 /* we must adjust the module to inherit the one from the context node */
7696 moveto_mod = node->schema->module;
7697 }
7698
7699 /* search in children, do not repeat the same search */
7700 scnode = lys_find_child(node->schema, moveto_mod, name, name_len, 0, 0);
Michal Vasko7d1d0912020-10-16 08:38:30 +02007701 } /* else skip redundant search */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007702
7703 /* additional context check */
7704 if (scnode && (root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) {
7705 scnode = NULL;
7706 }
7707
7708 if (scnode) {
7709 if (*found) {
7710 /* we found a schema node with the same name but at different level, give up, too complicated
7711 * (more hash-based searches would be required, not supported) */
7712 return LY_ENOT;
7713 } else {
7714 /* remember the found schema node and continue to make sure it can be used */
7715 *found = scnode;
7716 }
7717 }
7718
7719 if (idx) {
7720 /* continue searching all the following models */
7721 goto continue_search;
7722 }
7723
7724 return LY_SUCCESS;
7725}
7726
7727/**
Michal Vasko4ad69e72021-10-26 16:25:55 +02007728 * @brief Generate message when no matching schema nodes were found for a path segment.
7729 *
7730 * @param[in] set XPath set.
7731 * @param[in] scparent Previous schema parent in the context, if only one.
7732 * @param[in] ncname XPath NCName being evaluated.
7733 * @param[in] ncname_len Length of @p ncname.
7734 * @param[in] expr Whole XPath expression.
7735 * @param[in] options XPath options.
7736 */
7737static void
7738eval_name_test_scnode_no_match_msg(struct lyxp_set *set, const struct lyxp_set_scnode *scparent, const char *ncname,
7739 uint16_t ncname_len, const char *expr, uint32_t options)
7740{
7741 const char *format;
7742 char *path = NULL, *ppath = NULL;
7743
7744 path = lysc_path(set->cur_scnode, LYSC_PATH_LOG, NULL, 0);
7745 if (scparent) {
7746 /* generate path for the parent */
7747 if (scparent->type == LYXP_NODE_ELEM) {
7748 ppath = lysc_path(scparent->scnode, LYSC_PATH_LOG, NULL, 0);
7749 } else if (scparent->type == LYXP_NODE_ROOT) {
7750 ppath = strdup("<root>");
7751 } else if (scparent->type == LYXP_NODE_ROOT_CONFIG) {
7752 ppath = strdup("<config-root>");
7753 }
7754 }
7755 if (ppath) {
7756 format = "Schema node \"%.*s\" for parent \"%s\" not found; in expr \"%.*s\" with context node \"%s\".";
7757 if (options & LYXP_SCNODE_ERROR) {
7758 LOGERR(set->ctx, LY_EVALID, format, ncname_len, ncname, ppath, (ncname - expr) + ncname_len, expr, path);
7759 } else {
7760 LOGWRN(set->ctx, format, ncname_len, ncname, ppath, (ncname - expr) + ncname_len, expr, path);
7761 }
7762 } else {
7763 format = "Schema node \"%.*s\" not found; in expr \"%.*s\" with context node \"%s\".";
7764 if (options & LYXP_SCNODE_ERROR) {
7765 LOGERR(set->ctx, LY_EVALID, format, ncname_len, ncname, (ncname - expr) + ncname_len, expr, path);
7766 } else {
7767 LOGWRN(set->ctx, format, ncname_len, ncname, (ncname - expr) + ncname_len, expr, path);
7768 }
7769 }
7770 free(path);
7771 free(ppath);
7772}
7773
7774/**
Michal Vaskod3678892020-05-21 10:06:58 +02007775 * @brief Evaluate NameTest and any following Predicates. Logs directly on error.
7776 *
7777 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
7778 * [6] NodeTest ::= NameTest | NodeType '(' ')'
7779 * [7] NameTest ::= '*' | NCName ':' '*' | QName
7780 *
7781 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007782 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko49fec8e2022-05-24 10:28:33 +02007783 * @param[in] axis What axis to search on.
Michal Vaskod3678892020-05-21 10:06:58 +02007784 * @param[in] all_desc Whether to search all the descendants or children only.
aPiecek8b0cc152021-05-31 16:40:31 +02007785 * @param[in,out] set Context and result set.
Michal Vaskod3678892020-05-21 10:06:58 +02007786 * @param[in] options XPath options.
Michal Vaskoa036a6b2021-10-12 16:05:23 +02007787 * @return LY_ERR (LY_EINCOMPLETE on unresolved when, LY_ENOT for not found schema node)
Michal Vaskod3678892020-05-21 10:06:58 +02007788 */
7789static LY_ERR
Michal Vasko49fec8e2022-05-24 10:28:33 +02007790eval_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 +02007791 struct lyxp_set *set, uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02007792{
Michal Vaskoa036a6b2021-10-12 16:05:23 +02007793 LY_ERR rc = LY_SUCCESS, r;
Michal Vasko004d3152020-06-11 19:59:22 +02007794 const char *ncname, *ncname_dict = NULL;
7795 uint16_t ncname_len;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007796 const struct lys_module *moveto_mod = NULL;
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007797 const struct lysc_node *scnode = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02007798 struct ly_path_predicate *predicates = NULL;
7799 enum ly_path_pred_type pred_type = 0;
Michal Vaskoa036a6b2021-10-12 16:05:23 +02007800 int scnode_skip_pred = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02007801
aPiecek8b0cc152021-05-31 16:40:31 +02007802 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02007803 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007804 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007805
aPiecek8b0cc152021-05-31 16:40:31 +02007806 if (options & LYXP_SKIP_EXPR) {
Michal Vaskod3678892020-05-21 10:06:58 +02007807 goto moveto;
7808 }
7809
Michal Vasko004d3152020-06-11 19:59:22 +02007810 ncname = &exp->expr[exp->tok_pos[*tok_idx - 1]];
7811 ncname_len = exp->tok_len[*tok_idx - 1];
Michal Vaskod3678892020-05-21 10:06:58 +02007812
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007813 if ((ncname[0] == '*') && (ncname_len == 1)) {
7814 /* all nodes will match */
7815 goto moveto;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007816 }
7817
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007818 /* parse (and skip) module name */
7819 rc = moveto_resolve_model(&ncname, &ncname_len, set, NULL, &moveto_mod);
Michal Vaskod3678892020-05-21 10:06:58 +02007820 LY_CHECK_GOTO(rc, cleanup);
7821
Michal Vasko49fec8e2022-05-24 10:28:33 +02007822 if ((ncname[0] == '*') && (ncname_len == 1)) {
7823 /* all nodes from the module will match */
7824 goto moveto;
7825 }
7826
7827 if (((set->format == LY_VALUE_JSON) || moveto_mod) && (axis == LYXP_AXIS_CHILD) && !all_desc &&
7828 (set->type == LYXP_SET_NODE_SET)) {
Michal Vaskod3678892020-05-21 10:06:58 +02007829 /* find the matching schema node in some parent in the context */
Radek Krejci1deb5be2020-08-26 16:43:36 +02007830 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007831 if (eval_name_test_with_predicate_get_scnode(set->ctx, set->val.nodes[i].node, ncname, ncname_len,
7832 moveto_mod, set->root_type, set->format, &scnode)) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007833 /* check failed */
7834 scnode = NULL;
7835 break;
Michal Vaskod3678892020-05-21 10:06:58 +02007836 }
7837 }
7838
Michal Vasko004d3152020-06-11 19:59:22 +02007839 if (scnode && (scnode->nodetype & (LYS_LIST | LYS_LEAFLIST))) {
7840 /* try to create the predicates */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007841 if (eval_name_test_try_compile_predicates(exp, tok_idx, scnode, set->cur_mod, set->cur_node ?
7842 set->cur_node->schema : NULL, set->format, set->prefix_data, &predicates, &pred_type)) {
Michal Vasko004d3152020-06-11 19:59:22 +02007843 /* hashes cannot be used */
Michal Vaskod3678892020-05-21 10:06:58 +02007844 scnode = NULL;
7845 }
7846 }
7847 }
7848
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007849 if (!scnode) {
7850 /* we are not able to match based on a schema node and not all the modules match ("*"),
Michal Vasko004d3152020-06-11 19:59:22 +02007851 * use dictionary for efficient comparison */
Radek Krejci011e4aa2020-09-04 15:22:31 +02007852 LY_CHECK_GOTO(rc = lydict_insert(set->ctx, ncname, ncname_len, &ncname_dict), cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02007853 }
7854
7855moveto:
7856 /* move to the attribute(s), data node(s), or schema node(s) */
Michal Vasko49fec8e2022-05-24 10:28:33 +02007857 if (axis == LYXP_AXIS_ATTRIBUTE) {
aPiecek8b0cc152021-05-31 16:40:31 +02007858 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007859 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskod3678892020-05-21 10:06:58 +02007860 } else {
7861 if (all_desc) {
Michal Vaskocdad7122020-11-09 21:04:44 +01007862 rc = moveto_attr_alldesc(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007863 } else {
aPiecek8b0cc152021-05-31 16:40:31 +02007864 rc = moveto_attr(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007865 }
7866 LY_CHECK_GOTO(rc, cleanup);
7867 }
7868 } else {
aPiecek8b0cc152021-05-31 16:40:31 +02007869 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02007870 int64_t i;
Michal Vaskoa036a6b2021-10-12 16:05:23 +02007871 const struct lyxp_set_scnode *scparent = NULL;
Michal Vaskoa036a6b2021-10-12 16:05:23 +02007872
7873 /* remember parent if there is only one, to print in the warning */
7874 for (i = 0; i < set->used; ++i) {
7875 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
7876 if (!scparent) {
7877 /* remember the context node */
7878 scparent = &set->val.scnodes[i];
7879 } else {
7880 /* several context nodes, no reasonable error possible */
7881 scparent = NULL;
7882 break;
7883 }
7884 }
7885 }
Radek Krejci1deb5be2020-08-26 16:43:36 +02007886
Michal Vasko49fec8e2022-05-24 10:28:33 +02007887 if (all_desc && (axis == LYXP_AXIS_CHILD)) {
7888 /* efficient evaluation that does not add all the descendants into the set */
7889 rc = moveto_scnode_alldesc_child(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007890 } else {
Michal Vasko49fec8e2022-05-24 10:28:33 +02007891 if (all_desc) {
7892 /* "//" == "/descendant-or-self::node()/" */
7893 rc = xpath_pi_node(set, LYXP_AXIS_DESCENDANT_OR_SELF, options);
7894 LY_CHECK_GOTO(rc, cleanup);
7895 }
7896 rc = moveto_scnode(set, moveto_mod, ncname_dict, axis, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007897 }
7898 LY_CHECK_GOTO(rc, cleanup);
7899
7900 for (i = set->used - 1; i > -1; --i) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007901 if (set->val.scnodes[i].in_ctx > LYXP_SET_SCNODE_ATOM_NODE) {
Michal Vaskod3678892020-05-21 10:06:58 +02007902 break;
7903 }
7904 }
7905 if (i == -1) {
Michal Vasko4ad69e72021-10-26 16:25:55 +02007906 /* generate message */
7907 eval_name_test_scnode_no_match_msg(set, scparent, ncname, ncname_len, exp->expr, options);
7908
7909 if (options & LYXP_SCNODE_ERROR) {
7910 /* error */
7911 rc = LY_EVALID;
7912 goto cleanup;
Michal Vaskoa036a6b2021-10-12 16:05:23 +02007913 }
Michal Vaskoa036a6b2021-10-12 16:05:23 +02007914
7915 /* skip the predicates and the rest of this path to not generate invalid warnings */
7916 rc = LY_ENOT;
7917 scnode_skip_pred = 1;
Michal Vaskod3678892020-05-21 10:06:58 +02007918 }
7919 } else {
Michal Vasko49fec8e2022-05-24 10:28:33 +02007920 if (all_desc && (axis == LYXP_AXIS_CHILD)) {
7921 /* efficient evaluation */
7922 rc = moveto_node_alldesc_child(set, moveto_mod, ncname_dict, options);
7923 } else if (scnode && (axis == LYXP_AXIS_CHILD)) {
7924 /* we can find the child nodes using hashes */
7925 rc = moveto_node_hash_child(set, scnode, predicates, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007926 } else {
Michal Vasko49fec8e2022-05-24 10:28:33 +02007927 if (all_desc) {
7928 /* "//" == "/descendant-or-self::node()/" */
7929 rc = xpath_pi_node(set, LYXP_AXIS_DESCENDANT_OR_SELF, options);
7930 LY_CHECK_GOTO(rc, cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02007931 }
Michal Vasko49fec8e2022-05-24 10:28:33 +02007932 rc = moveto_node(set, moveto_mod, ncname_dict, axis, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007933 }
7934 LY_CHECK_GOTO(rc, cleanup);
7935 }
7936 }
7937
Michal Vaskoa036a6b2021-10-12 16:05:23 +02007938 if (scnode_skip_pred) {
7939 /* skip predicates */
7940 options |= LYXP_SKIP_EXPR;
7941 }
7942
Michal Vaskod3678892020-05-21 10:06:58 +02007943 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007944 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02007945 r = eval_predicate(exp, tok_idx, set, options, axis);
Michal Vaskoa036a6b2021-10-12 16:05:23 +02007946 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02007947 }
7948
7949cleanup:
Michal Vaskoa036a6b2021-10-12 16:05:23 +02007950 if (scnode_skip_pred) {
7951 /* restore options */
7952 options &= ~LYXP_SKIP_EXPR;
7953 }
aPiecek8b0cc152021-05-31 16:40:31 +02007954 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko004d3152020-06-11 19:59:22 +02007955 lydict_remove(set->ctx, ncname_dict);
Michal Vaskof7e16e22020-10-21 09:24:39 +02007956 ly_path_predicates_free(set->ctx, pred_type, predicates);
Michal Vaskodb51a8d2020-05-27 15:22:29 +02007957 }
Michal Vaskod3678892020-05-21 10:06:58 +02007958 return rc;
7959}
7960
7961/**
7962 * @brief Evaluate NodeType and any following Predicates. Logs directly on error.
7963 *
7964 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
7965 * [6] NodeTest ::= NameTest | NodeType '(' ')'
7966 * [8] NodeType ::= 'text' | 'node'
7967 *
7968 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007969 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko49fec8e2022-05-24 10:28:33 +02007970 * @param[in] axis Axis to search on.
7971 * @param[in] all_desc Whether to search all the descendants or axis only.
aPiecek8b0cc152021-05-31 16:40:31 +02007972 * @param[in,out] set Context and result set.
Michal Vaskod3678892020-05-21 10:06:58 +02007973 * @param[in] options XPath options.
7974 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7975 */
7976static LY_ERR
Michal Vasko49fec8e2022-05-24 10:28:33 +02007977eval_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 +02007978 struct lyxp_set *set, uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02007979{
7980 LY_ERR rc;
7981
Michal Vaskod3678892020-05-21 10:06:58 +02007982 (void)all_desc;
7983
aPiecek8b0cc152021-05-31 16:40:31 +02007984 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko004d3152020-06-11 19:59:22 +02007985 assert(exp->tok_len[*tok_idx] == 4);
Michal Vasko49fec8e2022-05-24 10:28:33 +02007986 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "node", 4)) {
7987 rc = xpath_pi_node(set, axis, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007988 } else {
Michal Vasko49fec8e2022-05-24 10:28:33 +02007989 assert(!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "text", 4));
7990 rc = xpath_pi_text(set, axis, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007991 }
Michal Vasko49fec8e2022-05-24 10:28:33 +02007992 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02007993 }
aPiecek8b0cc152021-05-31 16:40:31 +02007994 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02007995 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007996 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007997
7998 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02007999 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
aPiecek8b0cc152021-05-31 16:40:31 +02008000 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008001 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008002 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02008003
8004 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02008005 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
aPiecek8b0cc152021-05-31 16:40:31 +02008006 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008007 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008008 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02008009
8010 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02008011 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02008012 rc = eval_predicate(exp, tok_idx, set, options, axis);
Michal Vaskod3678892020-05-21 10:06:58 +02008013 LY_CHECK_RET(rc);
8014 }
8015
8016 return LY_SUCCESS;
8017}
8018
8019/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02008020 * @brief Evaluate RelativeLocationPath. Logs directly on error.
8021 *
8022 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
8023 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
Michal Vaskod3678892020-05-21 10:06:58 +02008024 * [6] NodeTest ::= NameTest | NodeType '(' ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02008025 *
8026 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008027 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008028 * @param[in] all_desc Whether to search all the descendants or children only.
aPiecek8b0cc152021-05-31 16:40:31 +02008029 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008030 * @param[in] options XPath options.
8031 * @return LY_ERR (YL_EINCOMPLETE on unresolved when)
8032 */
8033static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008034eval_relative_location_path(const struct lyxp_expr *exp, uint16_t *tok_idx, ly_bool all_desc, struct lyxp_set *set,
8035 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008036{
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008037 LY_ERR rc = LY_SUCCESS;
Michal Vasko49fec8e2022-05-24 10:28:33 +02008038 enum lyxp_axis axis;
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008039 int scnode_skip_path = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008040
8041 goto step;
8042 do {
8043 /* evaluate '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02008044 if (exp->tok_len[*tok_idx] == 1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008045 all_desc = 0;
8046 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02008047 assert(exp->tok_len[*tok_idx] == 2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008048 all_desc = 1;
8049 }
aPiecek8b0cc152021-05-31 16:40:31 +02008050 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008051 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008052 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008053
8054step:
Michal Vasko49fec8e2022-05-24 10:28:33 +02008055 /* AxisSpecifier */
8056 if (exp->tokens[*tok_idx] == LYXP_TOKEN_AXISNAME) {
8057 axis = str2axis(exp->expr + exp->tok_pos[*tok_idx], exp->tok_len[*tok_idx]);
Michal Vaskod3678892020-05-21 10:06:58 +02008058
aPiecek8b0cc152021-05-31 16:40:31 +02008059 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008060 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
8061 ++(*tok_idx);
8062
8063 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_DCOLON);
8064 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
8065 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
8066 ++(*tok_idx);
8067 } else if (exp->tokens[*tok_idx] == LYXP_TOKEN_AT) {
8068 axis = LYXP_AXIS_ATTRIBUTE;
8069
8070 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
8071 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008072 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02008073 } else {
Michal Vasko49fec8e2022-05-24 10:28:33 +02008074 /* default */
8075 axis = LYXP_AXIS_CHILD;
Michal Vaskod3678892020-05-21 10:06:58 +02008076 }
8077
Michal Vasko49fec8e2022-05-24 10:28:33 +02008078 /* NodeTest Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02008079 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008080 case LYXP_TOKEN_DOT:
8081 /* evaluate '.' */
Michal Vasko49fec8e2022-05-24 10:28:33 +02008082 if (!(options & LYXP_SKIP_EXPR)) {
8083 if (((options & LYXP_SCNODE_ALL) && (set->type != LYXP_SET_SCNODE_SET)) ||
8084 (!(options & LYXP_SCNODE_ALL) && (set->type != LYXP_SET_NODE_SET))) {
8085 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
8086 rc = LY_EVALID;
8087 goto cleanup;
8088 }
8089
8090 if (all_desc) {
8091 rc = xpath_pi_node(set, LYXP_AXIS_DESCENDANT_OR_SELF, options);
8092 LY_CHECK_GOTO(rc, cleanup);
8093 }
8094 rc = xpath_pi_node(set, LYXP_AXIS_SELF, options);
8095 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008096 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008097 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008098 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008099 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008100 break;
8101
8102 case LYXP_TOKEN_DDOT:
8103 /* evaluate '..' */
Michal Vasko49fec8e2022-05-24 10:28:33 +02008104 if (!(options & LYXP_SKIP_EXPR)) {
8105 if (((options & LYXP_SCNODE_ALL) && (set->type != LYXP_SET_SCNODE_SET)) ||
8106 (!(options & LYXP_SCNODE_ALL) && (set->type != LYXP_SET_NODE_SET))) {
8107 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
8108 rc = LY_EVALID;
8109 goto cleanup;
8110 }
8111
8112 if (all_desc) {
8113 rc = xpath_pi_node(set, LYXP_AXIS_DESCENDANT_OR_SELF, options);
8114 LY_CHECK_GOTO(rc, cleanup);
8115 }
8116 rc = xpath_pi_node(set, LYXP_AXIS_PARENT, options);
8117 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008118 }
aPiecek8b0cc152021-05-31 16:40:31 +02008119 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008120 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008121 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008122 break;
8123
Michal Vasko03ff5a72019-09-11 13:49:33 +02008124 case LYXP_TOKEN_NAMETEST:
Michal Vaskod3678892020-05-21 10:06:58 +02008125 /* evaluate NameTest Predicate* */
Michal Vasko49fec8e2022-05-24 10:28:33 +02008126 rc = eval_name_test_with_predicate(exp, tok_idx, axis, all_desc, set, options);
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008127 if (rc == LY_ENOT) {
8128 assert(options & LYXP_SCNODE_ALL);
8129 /* skip the rest of this path */
8130 rc = LY_SUCCESS;
8131 scnode_skip_path = 1;
8132 options |= LYXP_SKIP_EXPR;
8133 }
8134 LY_CHECK_GOTO(rc, cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02008135 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008136
Michal Vaskod3678892020-05-21 10:06:58 +02008137 case LYXP_TOKEN_NODETYPE:
8138 /* evaluate NodeType Predicate* */
Michal Vasko49fec8e2022-05-24 10:28:33 +02008139 rc = eval_node_type_with_predicate(exp, tok_idx, axis, all_desc, set, options);
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008140 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008141 break;
8142
8143 default:
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008144 LOGINT(set->ctx);
8145 rc = LY_EINT;
8146 goto cleanup;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008147 }
Michal Vasko004d3152020-06-11 19:59:22 +02008148 } while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008149
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008150cleanup:
8151 if (scnode_skip_path) {
8152 options &= ~LYXP_SKIP_EXPR;
8153 }
8154 return rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008155}
8156
8157/**
8158 * @brief Evaluate AbsoluteLocationPath. Logs directly on error.
8159 *
8160 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
8161 *
8162 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008163 * @param[in] tok_idx Position in the expression @p exp.
aPiecek8b0cc152021-05-31 16:40:31 +02008164 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008165 * @param[in] options XPath options.
8166 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8167 */
8168static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008169eval_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 +02008170{
Radek Krejci857189e2020-09-01 13:26:36 +02008171 ly_bool all_desc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008172
aPiecek8b0cc152021-05-31 16:40:31 +02008173 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008174 /* no matter what tokens follow, we need to be at the root */
Michal Vaskob0099a92020-08-31 14:55:23 +02008175 LY_CHECK_RET(moveto_root(set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008176 }
8177
8178 /* '/' RelativeLocationPath? */
Michal Vasko004d3152020-06-11 19:59:22 +02008179 if (exp->tok_len[*tok_idx] == 1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008180 /* evaluate '/' - deferred */
8181 all_desc = 0;
aPiecek8b0cc152021-05-31 16:40:31 +02008182 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008183 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008184 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008185
Michal Vasko004d3152020-06-11 19:59:22 +02008186 if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008187 return LY_SUCCESS;
8188 }
Michal Vasko004d3152020-06-11 19:59:22 +02008189 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008190 case LYXP_TOKEN_DOT:
8191 case LYXP_TOKEN_DDOT:
Michal Vasko49fec8e2022-05-24 10:28:33 +02008192 case LYXP_TOKEN_AXISNAME:
Michal Vasko03ff5a72019-09-11 13:49:33 +02008193 case LYXP_TOKEN_AT:
8194 case LYXP_TOKEN_NAMETEST:
8195 case LYXP_TOKEN_NODETYPE:
Michal Vaskob0099a92020-08-31 14:55:23 +02008196 LY_CHECK_RET(eval_relative_location_path(exp, tok_idx, all_desc, set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008197 break;
8198 default:
8199 break;
8200 }
8201
Michal Vasko03ff5a72019-09-11 13:49:33 +02008202 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02008203 /* '//' RelativeLocationPath */
Michal Vasko03ff5a72019-09-11 13:49:33 +02008204 /* evaluate '//' - deferred so as not to waste memory by remembering all the nodes */
8205 all_desc = 1;
aPiecek8b0cc152021-05-31 16:40:31 +02008206 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008207 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008208 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008209
Michal Vaskob0099a92020-08-31 14:55:23 +02008210 LY_CHECK_RET(eval_relative_location_path(exp, tok_idx, all_desc, set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008211 }
8212
8213 return LY_SUCCESS;
8214}
8215
8216/**
8217 * @brief Evaluate FunctionCall. Logs directly on error.
8218 *
Michal Vaskod3678892020-05-21 10:06:58 +02008219 * [11] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02008220 *
8221 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008222 * @param[in] tok_idx Position in the expression @p exp.
aPiecek8b0cc152021-05-31 16:40:31 +02008223 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008224 * @param[in] options XPath options.
8225 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8226 */
8227static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008228eval_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 +02008229{
8230 LY_ERR rc;
Michal Vasko69730152020-10-09 16:30:07 +02008231
Radek Krejci1deb5be2020-08-26 16:43:36 +02008232 LY_ERR (*xpath_func)(struct lyxp_set **, uint16_t, struct lyxp_set *, uint32_t) = NULL;
Michal Vasko0cbf54f2019-12-16 10:01:06 +01008233 uint16_t arg_count = 0, i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008234 struct lyxp_set **args = NULL, **args_aux;
8235
aPiecek8b0cc152021-05-31 16:40:31 +02008236 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008237 /* FunctionName */
Michal Vasko004d3152020-06-11 19:59:22 +02008238 switch (exp->tok_len[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008239 case 3:
Michal Vasko004d3152020-06-11 19:59:22 +02008240 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008241 xpath_func = &xpath_not;
Michal Vasko004d3152020-06-11 19:59:22 +02008242 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008243 xpath_func = &xpath_sum;
8244 }
8245 break;
8246 case 4:
Michal Vasko004d3152020-06-11 19:59:22 +02008247 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008248 xpath_func = &xpath_lang;
Michal Vasko004d3152020-06-11 19:59:22 +02008249 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008250 xpath_func = &xpath_last;
Michal Vasko004d3152020-06-11 19:59:22 +02008251 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008252 xpath_func = &xpath_name;
Michal Vasko004d3152020-06-11 19:59:22 +02008253 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008254 xpath_func = &xpath_true;
8255 }
8256 break;
8257 case 5:
Michal Vasko004d3152020-06-11 19:59:22 +02008258 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008259 xpath_func = &xpath_count;
Michal Vasko004d3152020-06-11 19:59:22 +02008260 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008261 xpath_func = &xpath_false;
Michal Vasko004d3152020-06-11 19:59:22 +02008262 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008263 xpath_func = &xpath_floor;
Michal Vasko004d3152020-06-11 19:59:22 +02008264 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008265 xpath_func = &xpath_round;
Michal Vasko004d3152020-06-11 19:59:22 +02008266 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008267 xpath_func = &xpath_deref;
8268 }
8269 break;
8270 case 6:
Michal Vasko004d3152020-06-11 19:59:22 +02008271 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008272 xpath_func = &xpath_concat;
Michal Vasko004d3152020-06-11 19:59:22 +02008273 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008274 xpath_func = &xpath_number;
Michal Vasko004d3152020-06-11 19:59:22 +02008275 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008276 xpath_func = &xpath_string;
8277 }
8278 break;
8279 case 7:
Michal Vasko004d3152020-06-11 19:59:22 +02008280 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008281 xpath_func = &xpath_boolean;
Michal Vasko004d3152020-06-11 19:59:22 +02008282 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008283 xpath_func = &xpath_ceiling;
Michal Vasko004d3152020-06-11 19:59:22 +02008284 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008285 xpath_func = &xpath_current;
8286 }
8287 break;
8288 case 8:
Michal Vasko004d3152020-06-11 19:59:22 +02008289 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008290 xpath_func = &xpath_contains;
Michal Vasko004d3152020-06-11 19:59:22 +02008291 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008292 xpath_func = &xpath_position;
Michal Vasko004d3152020-06-11 19:59:22 +02008293 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "re-match", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008294 xpath_func = &xpath_re_match;
8295 }
8296 break;
8297 case 9:
Michal Vasko004d3152020-06-11 19:59:22 +02008298 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008299 xpath_func = &xpath_substring;
Michal Vasko004d3152020-06-11 19:59:22 +02008300 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008301 xpath_func = &xpath_translate;
8302 }
8303 break;
8304 case 10:
Michal Vasko004d3152020-06-11 19:59:22 +02008305 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008306 xpath_func = &xpath_local_name;
Michal Vasko004d3152020-06-11 19:59:22 +02008307 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008308 xpath_func = &xpath_enum_value;
Michal Vasko004d3152020-06-11 19:59:22 +02008309 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008310 xpath_func = &xpath_bit_is_set;
8311 }
8312 break;
8313 case 11:
Michal Vasko004d3152020-06-11 19:59:22 +02008314 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008315 xpath_func = &xpath_starts_with;
8316 }
8317 break;
8318 case 12:
Michal Vasko004d3152020-06-11 19:59:22 +02008319 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008320 xpath_func = &xpath_derived_from;
8321 }
8322 break;
8323 case 13:
Michal Vasko004d3152020-06-11 19:59:22 +02008324 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008325 xpath_func = &xpath_namespace_uri;
Michal Vasko004d3152020-06-11 19:59:22 +02008326 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string-length", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008327 xpath_func = &xpath_string_length;
8328 }
8329 break;
8330 case 15:
Michal Vasko004d3152020-06-11 19:59:22 +02008331 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008332 xpath_func = &xpath_normalize_space;
Michal Vasko004d3152020-06-11 19:59:22 +02008333 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-after", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008334 xpath_func = &xpath_substring_after;
8335 }
8336 break;
8337 case 16:
Michal Vasko004d3152020-06-11 19:59:22 +02008338 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008339 xpath_func = &xpath_substring_before;
8340 }
8341 break;
8342 case 20:
Michal Vasko004d3152020-06-11 19:59:22 +02008343 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from-or-self", 20)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008344 xpath_func = &xpath_derived_from_or_self;
8345 }
8346 break;
8347 }
8348
8349 if (!xpath_func) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01008350 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 +02008351 return LY_EVALID;
8352 }
8353 }
8354
aPiecek8b0cc152021-05-31 16:40:31 +02008355 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008356 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008357 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008358
8359 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02008360 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
aPiecek8b0cc152021-05-31 16:40:31 +02008361 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008362 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008363 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008364
8365 /* ( Expr ( ',' Expr )* )? */
Michal Vasko004d3152020-06-11 19:59:22 +02008366 if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) {
aPiecek8b0cc152021-05-31 16:40:31 +02008367 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008368 args = malloc(sizeof *args);
8369 LY_CHECK_ERR_GOTO(!args, LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
8370 arg_count = 1;
8371 args[0] = set_copy(set);
8372 if (!args[0]) {
8373 rc = LY_EMEM;
8374 goto cleanup;
8375 }
8376
Michal Vasko004d3152020-06-11 19:59:22 +02008377 rc = eval_expr_select(exp, tok_idx, 0, args[0], options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008378 LY_CHECK_GOTO(rc, cleanup);
8379 } else {
aPiecek8b0cc152021-05-31 16:40:31 +02008380 rc = eval_expr_select(exp, tok_idx, 0, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008381 LY_CHECK_GOTO(rc, cleanup);
8382 }
8383 }
Michal Vasko004d3152020-06-11 19:59:22 +02008384 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) {
aPiecek8b0cc152021-05-31 16:40:31 +02008385 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008386 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008387 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008388
aPiecek8b0cc152021-05-31 16:40:31 +02008389 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008390 ++arg_count;
8391 args_aux = realloc(args, arg_count * sizeof *args);
8392 LY_CHECK_ERR_GOTO(!args_aux, arg_count--; LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
8393 args = args_aux;
8394 args[arg_count - 1] = set_copy(set);
8395 if (!args[arg_count - 1]) {
8396 rc = LY_EMEM;
8397 goto cleanup;
8398 }
8399
Michal Vasko004d3152020-06-11 19:59:22 +02008400 rc = eval_expr_select(exp, tok_idx, 0, args[arg_count - 1], options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008401 LY_CHECK_GOTO(rc, cleanup);
8402 } else {
aPiecek8b0cc152021-05-31 16:40:31 +02008403 rc = eval_expr_select(exp, tok_idx, 0, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008404 LY_CHECK_GOTO(rc, cleanup);
8405 }
8406 }
8407
8408 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02008409 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
aPiecek8b0cc152021-05-31 16:40:31 +02008410 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008411 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008412 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008413
aPiecek8b0cc152021-05-31 16:40:31 +02008414 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008415 /* evaluate function */
8416 rc = xpath_func(args, arg_count, set, options);
8417
8418 if (options & LYXP_SCNODE_ALL) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008419 /* merge all nodes from arg evaluations */
8420 for (i = 0; i < arg_count; ++i) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008421 set_scnode_clear_ctx(args[i], LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008422 lyxp_set_scnode_merge(set, args[i]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008423 }
8424 }
8425 } else {
8426 rc = LY_SUCCESS;
8427 }
8428
8429cleanup:
8430 for (i = 0; i < arg_count; ++i) {
8431 lyxp_set_free(args[i]);
8432 }
8433 free(args);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008434 return rc;
8435}
8436
8437/**
8438 * @brief Evaluate Number. Logs directly on error.
8439 *
8440 * @param[in] ctx Context for errors.
8441 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008442 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008443 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8444 * @return LY_ERR
8445 */
8446static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008447eval_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 +02008448{
8449 long double num;
8450 char *endptr;
8451
8452 if (set) {
8453 errno = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02008454 num = strtold(&exp->expr[exp->tok_pos[*tok_idx]], &endptr);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008455 if (errno) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01008456 LOGVAL(ctx, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]);
8457 LOGVAL(ctx, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double (%s).",
Michal Vasko69730152020-10-09 16:30:07 +02008458 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]], strerror(errno));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008459 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02008460 } else if (endptr - &exp->expr[exp->tok_pos[*tok_idx]] != exp->tok_len[*tok_idx]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01008461 LOGVAL(ctx, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]);
8462 LOGVAL(ctx, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double.",
Michal Vasko69730152020-10-09 16:30:07 +02008463 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008464 return LY_EVALID;
8465 }
8466
8467 set_fill_number(set, num);
8468 }
8469
8470 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008471 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008472 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008473 return LY_SUCCESS;
8474}
8475
aPiecekdf23eee2021-10-07 12:21:50 +02008476LY_ERR
8477lyxp_vars_find(struct lyxp_var *vars, const char *name, size_t name_len, struct lyxp_var **var)
8478{
8479 LY_ERR ret = LY_ENOTFOUND;
8480 LY_ARRAY_COUNT_TYPE u;
8481
8482 assert(vars && name);
8483
8484 name_len = name_len ? name_len : strlen(name);
8485
8486 LY_ARRAY_FOR(vars, u) {
8487 if (!strncmp(vars[u].name, name, name_len)) {
8488 ret = LY_SUCCESS;
8489 break;
8490 }
8491 }
8492
8493 if (var && !ret) {
8494 *var = &vars[u];
8495 }
8496
8497 return ret;
8498}
8499
Michal Vasko03ff5a72019-09-11 13:49:33 +02008500/**
aPiecekfba75362021-10-07 12:39:48 +02008501 * @brief Evaluate VariableReference.
8502 *
8503 * @param[in] exp Parsed XPath expression.
8504 * @param[in] tok_idx Position in the expression @p exp.
8505 * @param[in] vars [Sized array](@ref sizedarrays) of XPath variables.
8506 * @param[in,out] set Context and result set.
8507 * @param[in] options XPath options.
8508 * @return LY_ERR value.
8509 */
8510static LY_ERR
8511eval_variable_reference(const struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set, uint32_t options)
8512{
8513 LY_ERR ret;
8514 const char *name;
8515 struct lyxp_var *var;
8516 const struct lyxp_var *vars;
8517 struct lyxp_expr *tokens = NULL;
8518 uint16_t token_index;
8519
8520 vars = set->vars;
8521
Michal Vasko49fec8e2022-05-24 10:28:33 +02008522 /* find out the name and value of the variable */
aPiecekfba75362021-10-07 12:39:48 +02008523 name = &exp->expr[exp->tok_pos[*tok_idx]];
8524 ret = lyxp_vars_find((struct lyxp_var *)vars, name, exp->tok_len[*tok_idx], &var);
8525 LY_CHECK_ERR_RET(ret, LOGERR(set->ctx, ret,
8526 "XPath variable \"%s\" not defined.", name), ret);
8527
Michal Vasko49fec8e2022-05-24 10:28:33 +02008528 /* parse value */
aPiecekfba75362021-10-07 12:39:48 +02008529 ret = lyxp_expr_parse(set->ctx, var->value, 0, 1, &tokens);
8530 LY_CHECK_GOTO(ret, cleanup);
8531
Michal Vasko49fec8e2022-05-24 10:28:33 +02008532 /* evaluate value */
aPiecekfba75362021-10-07 12:39:48 +02008533 token_index = 0;
8534 ret = eval_expr_select(tokens, &token_index, 0, set, options);
8535 LY_CHECK_GOTO(ret, cleanup);
8536
8537cleanup:
8538 lyxp_expr_free(set->ctx, tokens);
8539
8540 return ret;
8541}
8542
8543/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02008544 * @brief Evaluate PathExpr. Logs directly on error.
8545 *
Michal Vaskod3678892020-05-21 10:06:58 +02008546 * [12] PathExpr ::= LocationPath | PrimaryExpr Predicate*
Michal Vasko03ff5a72019-09-11 13:49:33 +02008547 * | PrimaryExpr Predicate* '/' RelativeLocationPath
8548 * | PrimaryExpr Predicate* '//' RelativeLocationPath
8549 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
aPiecekfba75362021-10-07 12:39:48 +02008550 * [10] PrimaryExpr ::= VariableReference | '(' Expr ')' | Literal | Number | FunctionCall
Michal Vasko03ff5a72019-09-11 13:49:33 +02008551 *
8552 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008553 * @param[in] tok_idx Position in the expression @p exp.
aPiecek8b0cc152021-05-31 16:40:31 +02008554 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008555 * @param[in] options XPath options.
8556 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8557 */
8558static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008559eval_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 +02008560{
Michal Vasko49fec8e2022-05-24 10:28:33 +02008561 ly_bool all_desc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008562 LY_ERR rc;
8563
Michal Vasko004d3152020-06-11 19:59:22 +02008564 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008565 case LYXP_TOKEN_PAR1:
8566 /* '(' Expr ')' */
8567
8568 /* '(' */
aPiecek8b0cc152021-05-31 16:40:31 +02008569 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008570 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008571 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008572
8573 /* Expr */
Michal Vasko004d3152020-06-11 19:59:22 +02008574 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008575 LY_CHECK_RET(rc);
8576
8577 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02008578 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
aPiecek8b0cc152021-05-31 16:40:31 +02008579 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008580 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008581 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008582
Michal Vasko03ff5a72019-09-11 13:49:33 +02008583 goto predicate;
8584
8585 case LYXP_TOKEN_DOT:
8586 case LYXP_TOKEN_DDOT:
Michal Vasko49fec8e2022-05-24 10:28:33 +02008587 case LYXP_TOKEN_AXISNAME:
Michal Vasko03ff5a72019-09-11 13:49:33 +02008588 case LYXP_TOKEN_AT:
8589 case LYXP_TOKEN_NAMETEST:
8590 case LYXP_TOKEN_NODETYPE:
8591 /* RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02008592 rc = eval_relative_location_path(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008593 LY_CHECK_RET(rc);
8594 break;
8595
aPiecekfba75362021-10-07 12:39:48 +02008596 case LYXP_TOKEN_VARREF:
8597 /* VariableReference */
8598 rc = eval_variable_reference(exp, tok_idx, set, options);
8599 LY_CHECK_RET(rc);
8600 ++(*tok_idx);
8601
aPiecekfba75362021-10-07 12:39:48 +02008602 goto predicate;
8603
Michal Vasko03ff5a72019-09-11 13:49:33 +02008604 case LYXP_TOKEN_FUNCNAME:
8605 /* FunctionCall */
aPiecek8b0cc152021-05-31 16:40:31 +02008606 rc = eval_function_call(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008607 LY_CHECK_RET(rc);
8608
Michal Vasko03ff5a72019-09-11 13:49:33 +02008609 goto predicate;
8610
Michal Vasko3e48bf32020-06-01 08:39:07 +02008611 case LYXP_TOKEN_OPER_PATH:
8612 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +02008613 /* AbsoluteLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02008614 rc = eval_absolute_location_path(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008615 LY_CHECK_RET(rc);
8616 break;
8617
8618 case LYXP_TOKEN_LITERAL:
8619 /* Literal */
aPiecek8b0cc152021-05-31 16:40:31 +02008620 if ((options & LYXP_SKIP_EXPR) || (options & LYXP_SCNODE_ALL)) {
8621 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008622 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008623 }
Michal Vasko004d3152020-06-11 19:59:22 +02008624 eval_literal(exp, tok_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008625 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02008626 eval_literal(exp, tok_idx, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008627 }
8628
Michal Vasko03ff5a72019-09-11 13:49:33 +02008629 goto predicate;
8630
8631 case LYXP_TOKEN_NUMBER:
8632 /* Number */
aPiecek8b0cc152021-05-31 16:40:31 +02008633 if ((options & LYXP_SKIP_EXPR) || (options & LYXP_SCNODE_ALL)) {
8634 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008635 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008636 }
Michal Vasko004d3152020-06-11 19:59:22 +02008637 rc = eval_number(NULL, exp, tok_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008638 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02008639 rc = eval_number(set->ctx, exp, tok_idx, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008640 }
8641 LY_CHECK_RET(rc);
8642
Michal Vasko03ff5a72019-09-11 13:49:33 +02008643 goto predicate;
8644
8645 default:
Michal Vasko49fec8e2022-05-24 10:28:33 +02008646 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 +02008647 return LY_EVALID;
8648 }
8649
8650 return LY_SUCCESS;
8651
8652predicate:
8653 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02008654 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02008655 rc = eval_predicate(exp, tok_idx, set, options, LYXP_AXIS_CHILD);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008656 LY_CHECK_RET(rc);
8657 }
8658
8659 /* ('/' or '//') RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02008660 if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008661
8662 /* evaluate '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02008663 if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008664 all_desc = 0;
8665 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008666 all_desc = 1;
8667 }
8668
aPiecek8b0cc152021-05-31 16:40:31 +02008669 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008670 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008671 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008672
Michal Vasko004d3152020-06-11 19:59:22 +02008673 rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008674 LY_CHECK_RET(rc);
8675 }
8676
8677 return LY_SUCCESS;
8678}
8679
8680/**
8681 * @brief Evaluate UnionExpr. Logs directly on error.
8682 *
Michal Vaskod3678892020-05-21 10:06:58 +02008683 * [20] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008684 *
8685 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008686 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008687 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008688 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008689 * @param[in] options XPath options.
8690 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8691 */
8692static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008693eval_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 +02008694{
8695 LY_ERR rc = LY_SUCCESS;
8696 struct lyxp_set orig_set, set2;
8697 uint16_t i;
8698
8699 assert(repeat);
8700
8701 set_init(&orig_set, set);
8702 set_init(&set2, set);
8703
8704 set_fill_set(&orig_set, set);
8705
Michal Vasko004d3152020-06-11 19:59:22 +02008706 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008707 LY_CHECK_GOTO(rc, cleanup);
8708
8709 /* ('|' PathExpr)* */
8710 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008711 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_UNI);
aPiecek8b0cc152021-05-31 16:40:31 +02008712 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008713 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008714 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008715
aPiecek8b0cc152021-05-31 16:40:31 +02008716 if (options & LYXP_SKIP_EXPR) {
8717 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008718 LY_CHECK_GOTO(rc, cleanup);
8719 continue;
8720 }
8721
8722 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008723 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008724 LY_CHECK_GOTO(rc, cleanup);
8725
8726 /* eval */
8727 if (options & LYXP_SCNODE_ALL) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01008728 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008729 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008730 rc = moveto_union(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008731 LY_CHECK_GOTO(rc, cleanup);
8732 }
8733 }
8734
8735cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008736 lyxp_set_free_content(&orig_set);
8737 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008738 return rc;
8739}
8740
8741/**
8742 * @brief Evaluate UnaryExpr. Logs directly on error.
8743 *
Michal Vaskod3678892020-05-21 10:06:58 +02008744 * [19] UnaryExpr ::= UnionExpr | '-' UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008745 *
8746 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008747 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008748 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008749 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008750 * @param[in] options XPath options.
8751 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8752 */
8753static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008754eval_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 +02008755{
8756 LY_ERR rc;
8757 uint16_t this_op, i;
8758
8759 assert(repeat);
8760
8761 /* ('-')+ */
Michal Vasko004d3152020-06-11 19:59:22 +02008762 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008763 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008764 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 +02008765
aPiecek8b0cc152021-05-31 16:40:31 +02008766 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008767 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008768 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008769 }
8770
Michal Vasko004d3152020-06-11 19:59:22 +02008771 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNARY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008772 LY_CHECK_RET(rc);
8773
aPiecek8b0cc152021-05-31 16:40:31 +02008774 if (!(options & LYXP_SKIP_EXPR) && (repeat % 2)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008775 if (options & LYXP_SCNODE_ALL) {
8776 warn_operands(set->ctx, set, NULL, 1, exp->expr, exp->tok_pos[this_op]);
8777 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008778 rc = moveto_op_math(set, NULL, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008779 LY_CHECK_RET(rc);
8780 }
8781 }
8782
8783 return LY_SUCCESS;
8784}
8785
8786/**
8787 * @brief Evaluate MultiplicativeExpr. Logs directly on error.
8788 *
Michal Vaskod3678892020-05-21 10:06:58 +02008789 * [18] MultiplicativeExpr ::= UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008790 * | MultiplicativeExpr '*' UnaryExpr
8791 * | MultiplicativeExpr 'div' UnaryExpr
8792 * | MultiplicativeExpr 'mod' UnaryExpr
8793 *
8794 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008795 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008796 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008797 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008798 * @param[in] options XPath options.
8799 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8800 */
8801static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008802eval_multiplicative_expr(const struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set,
8803 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008804{
8805 LY_ERR rc;
8806 uint16_t this_op;
8807 struct lyxp_set orig_set, set2;
8808 uint16_t i;
8809
8810 assert(repeat);
8811
8812 set_init(&orig_set, set);
8813 set_init(&set2, set);
8814
8815 set_fill_set(&orig_set, set);
8816
Michal Vasko004d3152020-06-11 19:59:22 +02008817 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008818 LY_CHECK_GOTO(rc, cleanup);
8819
8820 /* ('*' / 'div' / 'mod' UnaryExpr)* */
8821 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008822 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008823
Michal Vasko004d3152020-06-11 19:59:22 +02008824 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH);
aPiecek8b0cc152021-05-31 16:40:31 +02008825 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008826 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008827 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008828
aPiecek8b0cc152021-05-31 16:40:31 +02008829 if (options & LYXP_SKIP_EXPR) {
8830 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008831 LY_CHECK_GOTO(rc, cleanup);
8832 continue;
8833 }
8834
8835 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008836 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008837 LY_CHECK_GOTO(rc, cleanup);
8838
8839 /* eval */
8840 if (options & LYXP_SCNODE_ALL) {
8841 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008842 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02008843 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008844 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008845 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008846 LY_CHECK_GOTO(rc, cleanup);
8847 }
8848 }
8849
8850cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008851 lyxp_set_free_content(&orig_set);
8852 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008853 return rc;
8854}
8855
8856/**
8857 * @brief Evaluate AdditiveExpr. Logs directly on error.
8858 *
Michal Vaskod3678892020-05-21 10:06:58 +02008859 * [17] AdditiveExpr ::= MultiplicativeExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008860 * | AdditiveExpr '+' MultiplicativeExpr
8861 * | AdditiveExpr '-' MultiplicativeExpr
8862 *
8863 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008864 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008865 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008866 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008867 * @param[in] options XPath options.
8868 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8869 */
8870static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008871eval_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 +02008872{
8873 LY_ERR rc;
8874 uint16_t this_op;
8875 struct lyxp_set orig_set, set2;
8876 uint16_t i;
8877
8878 assert(repeat);
8879
8880 set_init(&orig_set, set);
8881 set_init(&set2, set);
8882
8883 set_fill_set(&orig_set, set);
8884
Michal Vasko004d3152020-06-11 19:59:22 +02008885 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008886 LY_CHECK_GOTO(rc, cleanup);
8887
8888 /* ('+' / '-' MultiplicativeExpr)* */
8889 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008890 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008891
Michal Vasko004d3152020-06-11 19:59:22 +02008892 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008893 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008894 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008895 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008896
aPiecek8b0cc152021-05-31 16:40:31 +02008897 if (options & LYXP_SKIP_EXPR) {
8898 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008899 LY_CHECK_GOTO(rc, cleanup);
8900 continue;
8901 }
8902
8903 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008904 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008905 LY_CHECK_GOTO(rc, cleanup);
8906
8907 /* eval */
8908 if (options & LYXP_SCNODE_ALL) {
8909 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008910 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02008911 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008912 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008913 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008914 LY_CHECK_GOTO(rc, cleanup);
8915 }
8916 }
8917
8918cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008919 lyxp_set_free_content(&orig_set);
8920 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008921 return rc;
8922}
8923
8924/**
8925 * @brief Evaluate RelationalExpr. Logs directly on error.
8926 *
Michal Vaskod3678892020-05-21 10:06:58 +02008927 * [16] RelationalExpr ::= AdditiveExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008928 * | RelationalExpr '<' AdditiveExpr
8929 * | RelationalExpr '>' AdditiveExpr
8930 * | RelationalExpr '<=' AdditiveExpr
8931 * | RelationalExpr '>=' AdditiveExpr
8932 *
8933 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008934 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008935 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008936 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008937 * @param[in] options XPath options.
8938 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8939 */
8940static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008941eval_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 +02008942{
8943 LY_ERR rc;
8944 uint16_t this_op;
8945 struct lyxp_set orig_set, set2;
8946 uint16_t i;
8947
8948 assert(repeat);
8949
8950 set_init(&orig_set, set);
8951 set_init(&set2, set);
8952
8953 set_fill_set(&orig_set, set);
8954
Michal Vasko004d3152020-06-11 19:59:22 +02008955 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008956 LY_CHECK_GOTO(rc, cleanup);
8957
8958 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
8959 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008960 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008961
Michal Vasko004d3152020-06-11 19:59:22 +02008962 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_COMP);
aPiecek8b0cc152021-05-31 16:40:31 +02008963 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008964 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008965 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008966
aPiecek8b0cc152021-05-31 16:40:31 +02008967 if (options & LYXP_SKIP_EXPR) {
8968 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008969 LY_CHECK_GOTO(rc, cleanup);
8970 continue;
8971 }
8972
8973 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008974 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008975 LY_CHECK_GOTO(rc, cleanup);
8976
8977 /* eval */
8978 if (options & LYXP_SCNODE_ALL) {
8979 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008980 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02008981 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008982 } else {
Michal Vasko8abcecc2022-07-28 09:55:01 +02008983 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008984 LY_CHECK_GOTO(rc, cleanup);
8985 }
8986 }
8987
8988cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008989 lyxp_set_free_content(&orig_set);
8990 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008991 return rc;
8992}
8993
8994/**
8995 * @brief Evaluate EqualityExpr. Logs directly on error.
8996 *
Michal Vaskod3678892020-05-21 10:06:58 +02008997 * [15] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008998 * | EqualityExpr '!=' RelationalExpr
8999 *
9000 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02009001 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009002 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02009003 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009004 * @param[in] options XPath options.
9005 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
9006 */
9007static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02009008eval_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 +02009009{
9010 LY_ERR rc;
9011 uint16_t this_op;
9012 struct lyxp_set orig_set, set2;
9013 uint16_t i;
9014
9015 assert(repeat);
9016
9017 set_init(&orig_set, set);
9018 set_init(&set2, set);
9019
9020 set_fill_set(&orig_set, set);
9021
Michal Vasko004d3152020-06-11 19:59:22 +02009022 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009023 LY_CHECK_GOTO(rc, cleanup);
9024
9025 /* ('=' / '!=' RelationalExpr)* */
9026 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02009027 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009028
Michal Vasko004d3152020-06-11 19:59:22 +02009029 assert((exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_EQUAL) || (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_NEQUAL));
aPiecek8b0cc152021-05-31 16:40:31 +02009030 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02009031 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02009032 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009033
aPiecek8b0cc152021-05-31 16:40:31 +02009034 if (options & LYXP_SKIP_EXPR) {
9035 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009036 LY_CHECK_GOTO(rc, cleanup);
9037 continue;
9038 }
9039
9040 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02009041 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009042 LY_CHECK_GOTO(rc, cleanup);
9043
9044 /* eval */
9045 if (options & LYXP_SCNODE_ALL) {
9046 warn_operands(set->ctx, set, &set2, 0, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vasko004d3152020-06-11 19:59:22 +02009047 warn_equality_value(exp, set, *tok_idx - 1, this_op - 1, *tok_idx - 1);
9048 warn_equality_value(exp, &set2, this_op - 1, this_op - 1, *tok_idx - 1);
Michal Vaskoecd62de2019-11-13 12:35:11 +01009049 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02009050 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009051 } else {
Michal Vasko8abcecc2022-07-28 09:55:01 +02009052 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009053 LY_CHECK_GOTO(rc, cleanup);
9054 }
9055 }
9056
9057cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02009058 lyxp_set_free_content(&orig_set);
9059 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009060 return rc;
9061}
9062
9063/**
9064 * @brief Evaluate AndExpr. Logs directly on error.
9065 *
Michal Vaskod3678892020-05-21 10:06:58 +02009066 * [14] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02009067 *
9068 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02009069 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009070 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02009071 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009072 * @param[in] options XPath options.
9073 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
9074 */
9075static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02009076eval_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 +02009077{
9078 LY_ERR rc;
9079 struct lyxp_set orig_set, set2;
9080 uint16_t i;
9081
9082 assert(repeat);
9083
9084 set_init(&orig_set, set);
9085 set_init(&set2, set);
9086
9087 set_fill_set(&orig_set, set);
9088
Michal Vasko004d3152020-06-11 19:59:22 +02009089 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009090 LY_CHECK_GOTO(rc, cleanup);
9091
9092 /* cast to boolean, we know that will be the final result */
aPiecek8b0cc152021-05-31 16:40:31 +02009093 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02009094 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009095 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009096 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009097 }
9098
9099 /* ('and' EqualityExpr)* */
9100 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02009101 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG);
aPiecek8b0cc152021-05-31 16:40:31 +02009102 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, ((options & LYXP_SKIP_EXPR) || !set->val.bln ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02009103 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02009104 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009105
9106 /* lazy evaluation */
aPiecek8b0cc152021-05-31 16:40:31 +02009107 if ((options & LYXP_SKIP_EXPR) || ((set->type == LYXP_SET_BOOLEAN) && !set->val.bln)) {
9108 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009109 LY_CHECK_GOTO(rc, cleanup);
9110 continue;
9111 }
9112
9113 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02009114 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009115 LY_CHECK_GOTO(rc, cleanup);
9116
9117 /* eval - just get boolean value actually */
9118 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vasko1a09b212021-05-06 13:00:10 +02009119 set_scnode_clear_ctx(&set2, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskoecd62de2019-11-13 12:35:11 +01009120 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009121 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009122 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009123 set_fill_set(set, &set2);
9124 }
9125 }
9126
9127cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02009128 lyxp_set_free_content(&orig_set);
9129 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009130 return rc;
9131}
9132
9133/**
9134 * @brief Evaluate OrExpr. Logs directly on error.
9135 *
Michal Vaskod3678892020-05-21 10:06:58 +02009136 * [13] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02009137 *
9138 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02009139 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009140 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02009141 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009142 * @param[in] options XPath options.
9143 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
9144 */
9145static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02009146eval_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 +02009147{
9148 LY_ERR rc;
9149 struct lyxp_set orig_set, set2;
9150 uint16_t i;
9151
9152 assert(repeat);
9153
9154 set_init(&orig_set, set);
9155 set_init(&set2, set);
9156
9157 set_fill_set(&orig_set, set);
9158
Michal Vasko004d3152020-06-11 19:59:22 +02009159 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009160 LY_CHECK_GOTO(rc, cleanup);
9161
9162 /* cast to boolean, we know that will be the final result */
aPiecek8b0cc152021-05-31 16:40:31 +02009163 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02009164 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009165 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009166 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009167 }
9168
9169 /* ('or' AndExpr)* */
9170 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02009171 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG);
aPiecek8b0cc152021-05-31 16:40:31 +02009172 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, ((options & LYXP_SKIP_EXPR) || set->val.bln ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02009173 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02009174 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009175
9176 /* lazy evaluation */
aPiecek8b0cc152021-05-31 16:40:31 +02009177 if ((options & LYXP_SKIP_EXPR) || ((set->type == LYXP_SET_BOOLEAN) && set->val.bln)) {
9178 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009179 LY_CHECK_GOTO(rc, cleanup);
9180 continue;
9181 }
9182
9183 set_fill_set(&set2, &orig_set);
9184 /* expr_type cound have been LYXP_EXPR_NONE in all these later calls (except for the first one),
9185 * but it does not matter */
Michal Vasko004d3152020-06-11 19:59:22 +02009186 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009187 LY_CHECK_GOTO(rc, cleanup);
9188
9189 /* eval - just get boolean value actually */
9190 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vasko1a09b212021-05-06 13:00:10 +02009191 set_scnode_clear_ctx(&set2, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskoecd62de2019-11-13 12:35:11 +01009192 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009193 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009194 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009195 set_fill_set(set, &set2);
9196 }
9197 }
9198
9199cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02009200 lyxp_set_free_content(&orig_set);
9201 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009202 return rc;
9203}
9204
9205/**
Michal Vasko004d3152020-06-11 19:59:22 +02009206 * @brief Decide what expression is at the pointer @p tok_idx and evaluate it accordingly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009207 *
9208 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02009209 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009210 * @param[in] etype Expression type to evaluate.
aPiecek8b0cc152021-05-31 16:40:31 +02009211 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009212 * @param[in] options XPath options.
9213 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
9214 */
9215static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02009216eval_expr_select(const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_expr_type etype, struct lyxp_set *set,
9217 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02009218{
9219 uint16_t i, count;
9220 enum lyxp_expr_type next_etype;
9221 LY_ERR rc;
9222
9223 /* process operator repeats */
Michal Vasko004d3152020-06-11 19:59:22 +02009224 if (!exp->repeat[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02009225 next_etype = LYXP_EXPR_NONE;
9226 } else {
9227 /* find etype repeat */
Radek Krejci1e008d22020-08-17 11:37:37 +02009228 for (i = 0; exp->repeat[*tok_idx][i] > etype; ++i) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02009229
9230 /* select one-priority lower because etype expression called us */
9231 if (i) {
Michal Vasko004d3152020-06-11 19:59:22 +02009232 next_etype = exp->repeat[*tok_idx][i - 1];
Michal Vasko03ff5a72019-09-11 13:49:33 +02009233 /* count repeats for that expression */
Radek Krejci1e008d22020-08-17 11:37:37 +02009234 for (count = 0; i && exp->repeat[*tok_idx][i - 1] == next_etype; ++count, --i) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02009235 } else {
9236 next_etype = LYXP_EXPR_NONE;
9237 }
9238 }
9239
9240 /* decide what expression are we parsing based on the repeat */
9241 switch (next_etype) {
9242 case LYXP_EXPR_OR:
Michal Vasko004d3152020-06-11 19:59:22 +02009243 rc = eval_or_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009244 break;
9245 case LYXP_EXPR_AND:
Michal Vasko004d3152020-06-11 19:59:22 +02009246 rc = eval_and_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009247 break;
9248 case LYXP_EXPR_EQUALITY:
Michal Vasko004d3152020-06-11 19:59:22 +02009249 rc = eval_equality_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009250 break;
9251 case LYXP_EXPR_RELATIONAL:
Michal Vasko004d3152020-06-11 19:59:22 +02009252 rc = eval_relational_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009253 break;
9254 case LYXP_EXPR_ADDITIVE:
Michal Vasko004d3152020-06-11 19:59:22 +02009255 rc = eval_additive_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009256 break;
9257 case LYXP_EXPR_MULTIPLICATIVE:
Michal Vasko004d3152020-06-11 19:59:22 +02009258 rc = eval_multiplicative_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009259 break;
9260 case LYXP_EXPR_UNARY:
Michal Vasko004d3152020-06-11 19:59:22 +02009261 rc = eval_unary_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009262 break;
9263 case LYXP_EXPR_UNION:
Michal Vasko004d3152020-06-11 19:59:22 +02009264 rc = eval_union_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009265 break;
9266 case LYXP_EXPR_NONE:
Michal Vasko004d3152020-06-11 19:59:22 +02009267 rc = eval_path_expr(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009268 break;
9269 default:
9270 LOGINT_RET(set->ctx);
9271 }
9272
9273 return rc;
9274}
9275
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009276/**
9277 * @brief Get root type.
9278 *
9279 * @param[in] ctx_node Context node.
9280 * @param[in] ctx_scnode Schema context node.
9281 * @param[in] options XPath options.
9282 * @return Root type.
9283 */
9284static enum lyxp_node_type
Radek Krejci1deb5be2020-08-26 16:43:36 +02009285lyxp_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 +01009286{
Michal Vasko6b26e742020-07-17 15:02:10 +02009287 const struct lysc_node *op;
9288
Michal Vaskoa27245c2022-05-02 09:01:35 +02009289 /* explicit */
9290 if (options & LYXP_ACCESS_TREE_ALL) {
9291 return LYXP_NODE_ROOT;
9292 } else if (options & LYXP_ACCESS_TREE_CONFIG) {
9293 return LYXP_NODE_ROOT_CONFIG;
9294 }
9295
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009296 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009297 /* schema */
Radek Krejci1e008d22020-08-17 11:37:37 +02009298 for (op = ctx_scnode; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent) {}
Michal Vasko6b26e742020-07-17 15:02:10 +02009299
9300 if (op || (options & LYXP_SCNODE)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009301 /* general root that can access everything */
9302 return LYXP_NODE_ROOT;
9303 } else if (!ctx_scnode || (ctx_scnode->flags & LYS_CONFIG_W)) {
9304 /* root context node can access only config data (because we said so, it is unspecified) */
9305 return LYXP_NODE_ROOT_CONFIG;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009306 }
Michal Vasko6b26e742020-07-17 15:02:10 +02009307 return LYXP_NODE_ROOT;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009308 }
9309
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009310 /* data */
Michal Vasko6b26e742020-07-17 15:02:10 +02009311 op = ctx_node ? ctx_node->schema : NULL;
Michal Vaskod989ba02020-08-24 10:59:24 +02009312 for ( ; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent) {}
Michal Vasko6b26e742020-07-17 15:02:10 +02009313
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009314 if (op || !(options & LYXP_SCHEMA)) {
9315 /* general root that can access everything */
9316 return LYXP_NODE_ROOT;
Christian Hoppsb6ecaea2021-02-06 09:45:38 -05009317 } else if (!ctx_node || !ctx_node->schema || (ctx_node->schema->flags & LYS_CONFIG_W)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009318 /* root context node can access only config data (because we said so, it is unspecified) */
9319 return LYXP_NODE_ROOT_CONFIG;
9320 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009321 return LYXP_NODE_ROOT;
9322}
9323
Michal Vasko03ff5a72019-09-11 13:49:33 +02009324LY_ERR
Michal Vasko400e9672021-01-11 13:39:17 +01009325lyxp_eval(const struct ly_ctx *ctx, const struct lyxp_expr *exp, const struct lys_module *cur_mod,
Radek Krejci8df109d2021-04-23 12:19:08 +02009326 LY_VALUE_FORMAT format, void *prefix_data, const struct lyd_node *ctx_node, const struct lyd_node *tree,
aPiecekfba75362021-10-07 12:39:48 +02009327 const struct lyxp_var *vars, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02009328{
Michal Vasko004d3152020-06-11 19:59:22 +02009329 uint16_t tok_idx = 0;
Michal Vaskoddd76592022-01-17 13:34:48 +01009330 const struct lysc_node *snode;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009331 LY_ERR rc;
9332
Michal Vasko400e9672021-01-11 13:39:17 +01009333 LY_CHECK_ARG_RET(ctx, ctx, exp, set, LY_EINVAL);
Radek Krejci8df109d2021-04-23 12:19:08 +02009334 if (!cur_mod && ((format == LY_VALUE_SCHEMA) || (format == LY_VALUE_SCHEMA_RESOLVED))) {
Michal Vaskoddd76592022-01-17 13:34:48 +01009335 LOGERR(ctx, LY_EINVAL, "Current module must be set if schema format is used.");
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009336 return LY_EINVAL;
Michal Vasko004d3152020-06-11 19:59:22 +02009337 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02009338
Michal Vaskod3bb12f2020-12-04 14:33:09 +01009339 if (tree) {
9340 /* adjust the pointer to be the first top-level sibling */
9341 while (tree->parent) {
9342 tree = lyd_parent(tree);
9343 }
9344 tree = lyd_first_sibling(tree);
Michal Vaskoddd76592022-01-17 13:34:48 +01009345
9346 for (snode = tree->schema->parent; snode && (snode->nodetype & (LYS_CASE | LYS_CHOICE)); snode = snode->parent) {}
9347 if (snode) {
9348 /* unable to evaluate absolute paths */
9349 LOGERR(ctx, LY_EINVAL, "Data node \"%s\" has no parent but is not instance of a top-level schema node.",
9350 LYD_NAME(tree));
9351 return LY_EINVAL;
9352 }
Michal Vaskod3bb12f2020-12-04 14:33:09 +01009353 }
9354
Michal Vasko03ff5a72019-09-11 13:49:33 +02009355 /* prepare set for evaluation */
Michal Vasko03ff5a72019-09-11 13:49:33 +02009356 memset(set, 0, sizeof *set);
Michal Vaskod3678892020-05-21 10:06:58 +02009357 set->type = LYXP_SET_NODE_SET;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009358 set->root_type = lyxp_get_root_type(ctx_node, NULL, options);
9359 set_insert_node(set, (struct lyd_node *)ctx_node, 0, ctx_node ? LYXP_NODE_ELEM : set->root_type, 0);
9360
Michal Vasko400e9672021-01-11 13:39:17 +01009361 set->ctx = (struct ly_ctx *)ctx;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009362 set->cur_node = ctx_node;
9363 for (set->context_op = ctx_node ? ctx_node->schema : NULL;
Radek Krejci0f969882020-08-21 16:56:47 +02009364 set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF));
9365 set->context_op = set->context_op->parent) {}
Michal Vaskof03ed032020-03-04 13:31:44 +01009366 set->tree = tree;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009367 set->cur_mod = cur_mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009368 set->format = format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009369 set->prefix_data = prefix_data;
aPiecekfba75362021-10-07 12:39:48 +02009370 set->vars = vars;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009371
Radek Krejciddace2c2021-01-08 11:30:56 +01009372 LOG_LOCSET(NULL, set->cur_node, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01009373
Michal Vasko03ff5a72019-09-11 13:49:33 +02009374 /* evaluate */
Michal Vasko004d3152020-06-11 19:59:22 +02009375 rc = eval_expr_select(exp, &tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009376 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02009377 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009378 }
9379
Michal Vasko4a7d4d62021-12-13 17:05:06 +01009380 if (set->cur_node) {
9381 LOG_LOCBACK(0, 1, 0, 0);
9382 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02009383 return rc;
9384}
9385
9386#if 0
9387
9388/* full xml printing of set elements, not used currently */
9389
9390void
9391lyxp_set_print_xml(FILE *f, struct lyxp_set *set)
9392{
9393 uint32_t i;
9394 char *str_num;
9395 struct lyout out;
9396
9397 memset(&out, 0, sizeof out);
9398
9399 out.type = LYOUT_STREAM;
9400 out.method.f = f;
9401
9402 switch (set->type) {
9403 case LYXP_SET_EMPTY:
Michal Vasko5233e962020-08-14 14:26:20 +02009404 ly_print_(&out, "Empty XPath set\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02009405 break;
9406 case LYXP_SET_BOOLEAN:
Michal Vasko5233e962020-08-14 14:26:20 +02009407 ly_print_(&out, "Boolean XPath set:\n");
9408 ly_print_(&out, "%s\n\n", set->value.bool ? "true" : "false");
Michal Vasko03ff5a72019-09-11 13:49:33 +02009409 break;
9410 case LYXP_SET_STRING:
Michal Vasko5233e962020-08-14 14:26:20 +02009411 ly_print_(&out, "String XPath set:\n");
9412 ly_print_(&out, "\"%s\"\n\n", set->value.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009413 break;
9414 case LYXP_SET_NUMBER:
Michal Vasko5233e962020-08-14 14:26:20 +02009415 ly_print_(&out, "Number XPath set:\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02009416
9417 if (isnan(set->value.num)) {
9418 str_num = strdup("NaN");
9419 } else if ((set->value.num == 0) || (set->value.num == -0.0f)) {
9420 str_num = strdup("0");
9421 } else if (isinf(set->value.num) && !signbit(set->value.num)) {
9422 str_num = strdup("Infinity");
9423 } else if (isinf(set->value.num) && signbit(set->value.num)) {
9424 str_num = strdup("-Infinity");
9425 } else if ((long long)set->value.num == set->value.num) {
9426 if (asprintf(&str_num, "%lld", (long long)set->value.num) == -1) {
9427 str_num = NULL;
9428 }
9429 } else {
9430 if (asprintf(&str_num, "%03.1Lf", set->value.num) == -1) {
9431 str_num = NULL;
9432 }
9433 }
9434 if (!str_num) {
9435 LOGMEM;
9436 return;
9437 }
Michal Vasko5233e962020-08-14 14:26:20 +02009438 ly_print_(&out, "%s\n\n", str_num);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009439 free(str_num);
9440 break;
9441 case LYXP_SET_NODE_SET:
Michal Vasko5233e962020-08-14 14:26:20 +02009442 ly_print_(&out, "Node XPath set:\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02009443
9444 for (i = 0; i < set->used; ++i) {
Michal Vasko5233e962020-08-14 14:26:20 +02009445 ly_print_(&out, "%d. ", i + 1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009446 switch (set->node_type[i]) {
9447 case LYXP_NODE_ROOT_ALL:
Michal Vasko5233e962020-08-14 14:26:20 +02009448 ly_print_(&out, "ROOT all\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02009449 break;
9450 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko5233e962020-08-14 14:26:20 +02009451 ly_print_(&out, "ROOT config\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02009452 break;
9453 case LYXP_NODE_ROOT_STATE:
Michal Vasko5233e962020-08-14 14:26:20 +02009454 ly_print_(&out, "ROOT state\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02009455 break;
9456 case LYXP_NODE_ROOT_NOTIF:
Michal Vasko5233e962020-08-14 14:26:20 +02009457 ly_print_(&out, "ROOT notification \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009458 break;
9459 case LYXP_NODE_ROOT_RPC:
Michal Vasko5233e962020-08-14 14:26:20 +02009460 ly_print_(&out, "ROOT rpc \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009461 break;
9462 case LYXP_NODE_ROOT_OUTPUT:
Michal Vasko5233e962020-08-14 14:26:20 +02009463 ly_print_(&out, "ROOT output \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009464 break;
9465 case LYXP_NODE_ELEM:
Michal Vasko5233e962020-08-14 14:26:20 +02009466 ly_print_(&out, "ELEM \"%s\"\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009467 xml_print_node(&out, 1, set->value.nodes[i], 1, LYP_FORMAT);
Michal Vasko5233e962020-08-14 14:26:20 +02009468 ly_print_(&out, "\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02009469 break;
9470 case LYXP_NODE_TEXT:
Michal Vasko5233e962020-08-14 14:26:20 +02009471 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 +02009472 break;
9473 case LYXP_NODE_ATTR:
Michal Vasko5233e962020-08-14 14:26:20 +02009474 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 +02009475 break;
9476 }
9477 }
9478 break;
9479 }
9480}
9481
9482#endif
9483
9484LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009485lyxp_set_cast(struct lyxp_set *set, enum lyxp_set_type target)
Michal Vasko03ff5a72019-09-11 13:49:33 +02009486{
9487 long double num;
9488 char *str;
9489 LY_ERR rc;
9490
9491 if (!set || (set->type == target)) {
9492 return LY_SUCCESS;
9493 }
9494
9495 /* it's not possible to convert anything into a node set */
Michal Vaskod3678892020-05-21 10:06:58 +02009496 assert(target != LYXP_SET_NODE_SET);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009497
9498 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vaskod3678892020-05-21 10:06:58 +02009499 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009500 return LY_EINVAL;
9501 }
9502
9503 /* to STRING */
Michal Vaskod3678892020-05-21 10:06:58 +02009504 if ((target == LYXP_SET_STRING) || ((target == LYXP_SET_NUMBER) && (set->type == LYXP_SET_NODE_SET))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02009505 switch (set->type) {
9506 case LYXP_SET_NUMBER:
9507 if (isnan(set->val.num)) {
9508 set->val.str = strdup("NaN");
9509 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
9510 } else if ((set->val.num == 0) || (set->val.num == -0.0f)) {
9511 set->val.str = strdup("0");
9512 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
9513 } else if (isinf(set->val.num) && !signbit(set->val.num)) {
9514 set->val.str = strdup("Infinity");
9515 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
9516 } else if (isinf(set->val.num) && signbit(set->val.num)) {
9517 set->val.str = strdup("-Infinity");
9518 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
9519 } else if ((long long)set->val.num == set->val.num) {
9520 if (asprintf(&str, "%lld", (long long)set->val.num) == -1) {
9521 LOGMEM_RET(set->ctx);
9522 }
9523 set->val.str = str;
9524 } else {
9525 if (asprintf(&str, "%03.1Lf", set->val.num) == -1) {
9526 LOGMEM_RET(set->ctx);
9527 }
9528 set->val.str = str;
9529 }
9530 break;
9531 case LYXP_SET_BOOLEAN:
Michal Vasko004d3152020-06-11 19:59:22 +02009532 if (set->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02009533 set->val.str = strdup("true");
9534 } else {
9535 set->val.str = strdup("false");
9536 }
9537 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), LY_EMEM);
9538 break;
9539 case LYXP_SET_NODE_SET:
Michal Vasko03ff5a72019-09-11 13:49:33 +02009540 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009541 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02009542
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009543 rc = cast_node_set_to_string(set, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009544 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02009545 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009546 set->val.str = str;
9547 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009548 default:
9549 LOGINT_RET(set->ctx);
9550 }
9551 set->type = LYXP_SET_STRING;
9552 }
9553
9554 /* to NUMBER */
9555 if (target == LYXP_SET_NUMBER) {
9556 switch (set->type) {
9557 case LYXP_SET_STRING:
9558 num = cast_string_to_number(set->val.str);
Michal Vaskod3678892020-05-21 10:06:58 +02009559 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009560 set->val.num = num;
9561 break;
9562 case LYXP_SET_BOOLEAN:
Michal Vasko004d3152020-06-11 19:59:22 +02009563 if (set->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02009564 set->val.num = 1;
9565 } else {
9566 set->val.num = 0;
9567 }
9568 break;
9569 default:
9570 LOGINT_RET(set->ctx);
9571 }
9572 set->type = LYXP_SET_NUMBER;
9573 }
9574
9575 /* to BOOLEAN */
9576 if (target == LYXP_SET_BOOLEAN) {
9577 switch (set->type) {
9578 case LYXP_SET_NUMBER:
9579 if ((set->val.num == 0) || (set->val.num == -0.0f) || isnan(set->val.num)) {
Michal Vasko004d3152020-06-11 19:59:22 +02009580 set->val.bln = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009581 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02009582 set->val.bln = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009583 }
9584 break;
9585 case LYXP_SET_STRING:
9586 if (set->val.str[0]) {
Michal Vaskod3678892020-05-21 10:06:58 +02009587 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02009588 set->val.bln = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009589 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02009590 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02009591 set->val.bln = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009592 }
9593 break;
9594 case LYXP_SET_NODE_SET:
Michal Vaskod3678892020-05-21 10:06:58 +02009595 if (set->used) {
9596 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02009597 set->val.bln = 1;
Michal Vaskod3678892020-05-21 10:06:58 +02009598 } else {
9599 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02009600 set->val.bln = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02009601 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02009602 break;
9603 default:
9604 LOGINT_RET(set->ctx);
9605 }
9606 set->type = LYXP_SET_BOOLEAN;
9607 }
9608
Michal Vasko03ff5a72019-09-11 13:49:33 +02009609 return LY_SUCCESS;
9610}
9611
9612LY_ERR
Michal Vasko400e9672021-01-11 13:39:17 +01009613lyxp_atomize(const struct ly_ctx *ctx, const struct lyxp_expr *exp, const struct lys_module *cur_mod,
Radek Krejci8df109d2021-04-23 12:19:08 +02009614 LY_VALUE_FORMAT format, void *prefix_data, const struct lysc_node *ctx_scnode, struct lyxp_set *set,
Michal Vasko400e9672021-01-11 13:39:17 +01009615 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02009616{
Radek Krejci2efc45b2020-12-22 16:25:44 +01009617 LY_ERR ret;
Michal Vasko004d3152020-06-11 19:59:22 +02009618 uint16_t tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009619
Michal Vasko400e9672021-01-11 13:39:17 +01009620 LY_CHECK_ARG_RET(ctx, ctx, exp, set, LY_EINVAL);
Radek Krejci8df109d2021-04-23 12:19:08 +02009621 if (!cur_mod && ((format == LY_VALUE_SCHEMA) || (format == LY_VALUE_SCHEMA_RESOLVED))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009622 LOGARG(NULL, "Current module must be set if schema format is used.");
9623 return LY_EINVAL;
Michal Vasko004d3152020-06-11 19:59:22 +02009624 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02009625
9626 /* prepare set for evaluation */
Michal Vasko03ff5a72019-09-11 13:49:33 +02009627 memset(set, 0, sizeof *set);
9628 set->type = LYXP_SET_SCNODE_SET;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009629 set->root_type = lyxp_get_root_type(NULL, ctx_scnode, options);
9630 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 +01009631 set->val.scnodes[0].in_ctx = LYXP_SET_SCNODE_START;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009632
Michal Vasko400e9672021-01-11 13:39:17 +01009633 set->ctx = (struct ly_ctx *)ctx;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009634 set->cur_scnode = ctx_scnode;
Michal Vasko6b26e742020-07-17 15:02:10 +02009635 for (set->context_op = ctx_scnode;
Radek Krejci0f969882020-08-21 16:56:47 +02009636 set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF));
9637 set->context_op = set->context_op->parent) {}
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009638 set->cur_mod = cur_mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009639 set->format = format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009640 set->prefix_data = prefix_data;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009641
Radek Krejciddace2c2021-01-08 11:30:56 +01009642 LOG_LOCSET(set->cur_scnode, NULL, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01009643
Michal Vasko03ff5a72019-09-11 13:49:33 +02009644 /* evaluate */
Radek Krejci2efc45b2020-12-22 16:25:44 +01009645 ret = eval_expr_select(exp, &tok_idx, 0, set, options);
9646
Radek Krejciddace2c2021-01-08 11:30:56 +01009647 LOG_LOCBACK(1, 0, 0, 0);
Radek Krejci2efc45b2020-12-22 16:25:44 +01009648 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009649}
Michal Vaskod43d71a2020-08-07 14:54:58 +02009650
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01009651LIBYANG_API_DEF const char *
Michal Vaskod43d71a2020-08-07 14:54:58 +02009652lyxp_get_expr(const struct lyxp_expr *path)
9653{
9654 if (!path) {
9655 return NULL;
9656 }
9657
9658 return path->expr;
9659}