blob: d4ff63c594f5624dac9f2c746b4603cf17fc99d5 [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 Vasko49fec8e2022-05-24 10:28:33 +020050static LY_ERR moveto_node(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname,
51 enum lyxp_axis axis, uint32_t options);
52static LY_ERR moveto_scnode(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname,
53 enum lyxp_axis axis, uint32_t options);
Michal Vasko03ff5a72019-09-11 13:49:33 +020054
aPiecek96dc1e32021-10-08 15:45:59 +020055/* Functions are divided into the following basic classes:
56 *
57 * (re)parse functions:
58 * Parse functions parse the expression into
59 * tokens (syntactic analysis).
60 * Reparse functions perform semantic analysis
61 * (do not save the result, just a check) of
62 * the expression and fill repeat indices.
63 *
64 * warn functions:
65 * Warn functions check specific reasonable conditions for schema XPath
66 * and print a warning if they are not satisfied.
67 *
68 * moveto functions:
69 * They and only they actually change the context (set).
70 *
71 * eval functions:
72 * They execute a parsed XPath expression on some data subtree.
73 */
74
Michal Vasko03ff5a72019-09-11 13:49:33 +020075/**
76 * @brief Print the type of an XPath \p set.
77 *
78 * @param[in] set Set to use.
79 * @return Set type string.
80 */
81static const char *
82print_set_type(struct lyxp_set *set)
83{
84 switch (set->type) {
Michal Vasko03ff5a72019-09-11 13:49:33 +020085 case LYXP_SET_NODE_SET:
86 return "node set";
87 case LYXP_SET_SCNODE_SET:
88 return "schema node set";
89 case LYXP_SET_BOOLEAN:
90 return "boolean";
91 case LYXP_SET_NUMBER:
92 return "number";
93 case LYXP_SET_STRING:
94 return "string";
95 }
96
97 return NULL;
98}
99
Michal Vasko24cddf82020-06-01 08:17:01 +0200100const char *
Michal Vasko49fec8e2022-05-24 10:28:33 +0200101lyxp_token2str(enum lyxp_token tok)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200102{
103 switch (tok) {
104 case LYXP_TOKEN_PAR1:
105 return "(";
106 case LYXP_TOKEN_PAR2:
107 return ")";
108 case LYXP_TOKEN_BRACK1:
109 return "[";
110 case LYXP_TOKEN_BRACK2:
111 return "]";
112 case LYXP_TOKEN_DOT:
113 return ".";
114 case LYXP_TOKEN_DDOT:
115 return "..";
116 case LYXP_TOKEN_AT:
117 return "@";
118 case LYXP_TOKEN_COMMA:
119 return ",";
120 case LYXP_TOKEN_NAMETEST:
121 return "NameTest";
122 case LYXP_TOKEN_NODETYPE:
123 return "NodeType";
aPiecekfba75362021-10-07 12:39:48 +0200124 case LYXP_TOKEN_VARREF:
125 return "VariableReference";
Michal Vasko03ff5a72019-09-11 13:49:33 +0200126 case LYXP_TOKEN_FUNCNAME:
127 return "FunctionName";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200128 case LYXP_TOKEN_OPER_LOG:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200129 return "Operator(Logic)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200130 case LYXP_TOKEN_OPER_EQUAL:
131 return "Operator(Equal)";
132 case LYXP_TOKEN_OPER_NEQUAL:
133 return "Operator(Non-equal)";
134 case LYXP_TOKEN_OPER_COMP:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200135 return "Operator(Comparison)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200136 case LYXP_TOKEN_OPER_MATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200137 return "Operator(Math)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200138 case LYXP_TOKEN_OPER_UNI:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200139 return "Operator(Union)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200140 case LYXP_TOKEN_OPER_PATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200141 return "Operator(Path)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200142 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko14676352020-05-29 11:35:55 +0200143 return "Operator(Recursive Path)";
Michal Vasko03ff5a72019-09-11 13:49:33 +0200144 case LYXP_TOKEN_LITERAL:
145 return "Literal";
146 case LYXP_TOKEN_NUMBER:
147 return "Number";
148 default:
149 LOGINT(NULL);
150 return "";
151 }
152}
153
154/**
Michal Vasko49fec8e2022-05-24 10:28:33 +0200155 * @brief Transform string into an axis.
156 *
157 * @param[in] str String to transform.
158 * @param[in] str_len Length of @p str.
159 * @return Transformed axis.
160 */
161static enum lyxp_axis
162str2axis(const char *str, uint16_t str_len)
163{
164 switch (str_len) {
165 case 4:
166 assert(!strncmp("self", str, str_len));
167 return LYXP_AXIS_SELF;
168 case 5:
169 assert(!strncmp("child", str, str_len));
170 return LYXP_AXIS_CHILD;
171 case 6:
172 assert(!strncmp("parent", str, str_len));
173 return LYXP_AXIS_PARENT;
174 case 8:
175 assert(!strncmp("ancestor", str, str_len));
176 return LYXP_AXIS_ANCESTOR;
177 case 9:
178 if (str[0] == 'a') {
179 assert(!strncmp("attribute", str, str_len));
180 return LYXP_AXIS_ATTRIBUTE;
181 } else if (str[0] == 'f') {
182 assert(!strncmp("following", str, str_len));
183 return LYXP_AXIS_FOLLOWING;
184 } else {
185 assert(!strncmp("preceding", str, str_len));
186 return LYXP_AXIS_PRECEDING;
187 }
188 break;
189 case 10:
190 assert(!strncmp("descendant", str, str_len));
191 return LYXP_AXIS_DESCENDANT;
192 case 16:
193 assert(!strncmp("ancestor-or-self", str, str_len));
194 return LYXP_AXIS_ANCESTOR_OR_SELF;
195 case 17:
196 if (str[0] == 'f') {
197 assert(!strncmp("following-sibling", str, str_len));
198 return LYXP_AXIS_FOLLOWING_SIBLING;
199 } else {
200 assert(!strncmp("preceding-sibling", str, str_len));
201 return LYXP_AXIS_PRECEDING_SIBLING;
202 }
203 break;
204 case 18:
205 assert(!strncmp("descendant-or-self", str, str_len));
206 return LYXP_AXIS_DESCENDANT_OR_SELF;
207 }
208
209 LOGINT(NULL);
210 return 0;
211}
212
213/**
Michal Vasko03ff5a72019-09-11 13:49:33 +0200214 * @brief Print the whole expression \p exp to debug output.
215 *
216 * @param[in] exp Expression to use.
217 */
218static void
Michal Vasko40308e72020-10-20 16:38:40 +0200219print_expr_struct_debug(const struct lyxp_expr *exp)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200220{
Radek Krejcif13b87b2020-12-01 22:02:17 +0100221#define MSG_BUFFER_SIZE 128
222 char tmp[MSG_BUFFER_SIZE];
Michal Vasko1fdd8fa2021-01-08 09:21:45 +0100223 uint32_t i, j;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200224
Radek Krejci52b6d512020-10-12 12:33:17 +0200225 if (!exp || (ly_ll < LY_LLDBG)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200226 return;
227 }
228
229 LOGDBG(LY_LDGXPATH, "expression \"%s\":", exp->expr);
230 for (i = 0; i < exp->used; ++i) {
Michal Vasko49fec8e2022-05-24 10:28:33 +0200231 sprintf(tmp, "\ttoken %s, in expression \"%.*s\"", lyxp_token2str(exp->tokens[i]), exp->tok_len[i],
Michal Vasko69730152020-10-09 16:30:07 +0200232 &exp->expr[exp->tok_pos[i]]);
Michal Vasko23049552021-03-04 15:57:44 +0100233 if (exp->repeat && exp->repeat[i]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200234 sprintf(tmp + strlen(tmp), " (repeat %d", exp->repeat[i][0]);
235 for (j = 1; exp->repeat[i][j]; ++j) {
236 sprintf(tmp + strlen(tmp), ", %d", exp->repeat[i][j]);
237 }
238 strcat(tmp, ")");
239 }
240 LOGDBG(LY_LDGXPATH, tmp);
241 }
Radek Krejcif13b87b2020-12-01 22:02:17 +0100242#undef MSG_BUFFER_SIZE
Michal Vasko03ff5a72019-09-11 13:49:33 +0200243}
244
245#ifndef NDEBUG
246
247/**
248 * @brief Print XPath set content to debug output.
249 *
250 * @param[in] set Set to print.
251 */
252static void
253print_set_debug(struct lyxp_set *set)
254{
255 uint32_t i;
256 char *str;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200257 struct lyxp_set_node *item;
258 struct lyxp_set_scnode *sitem;
259
Radek Krejci52b6d512020-10-12 12:33:17 +0200260 if (ly_ll < LY_LLDBG) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200261 return;
262 }
263
264 switch (set->type) {
265 case LYXP_SET_NODE_SET:
266 LOGDBG(LY_LDGXPATH, "set NODE SET:");
267 for (i = 0; i < set->used; ++i) {
268 item = &set->val.nodes[i];
269
270 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +0100271 case LYXP_NODE_NONE:
272 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): NONE", i + 1, item->pos);
273 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200274 case LYXP_NODE_ROOT:
275 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT", i + 1, item->pos);
276 break;
277 case LYXP_NODE_ROOT_CONFIG:
278 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT CONFIG", i + 1, item->pos);
279 break;
280 case LYXP_NODE_ELEM:
Michal Vasko9e685082021-01-29 14:49:09 +0100281 if ((item->node->schema->nodetype == LYS_LIST) && (lyd_child(item->node)->schema->nodetype == LYS_LEAF)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200282 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (1st child val: %s)", i + 1, item->pos,
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200283 item->node->schema->name, lyd_get_value(lyd_child(item->node)));
Michal Vasko9e685082021-01-29 14:49:09 +0100284 } else if (item->node->schema->nodetype == LYS_LEAFLIST) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200285 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (val: %s)", i + 1, item->pos,
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200286 item->node->schema->name, lyd_get_value(item->node));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200287 } else {
288 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s", i + 1, item->pos, item->node->schema->name);
289 }
290 break;
291 case LYXP_NODE_TEXT:
292 if (item->node->schema->nodetype & LYS_ANYDATA) {
293 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): TEXT <%s>", i + 1, item->pos,
Michal Vasko69730152020-10-09 16:30:07 +0200294 item->node->schema->nodetype == LYS_ANYXML ? "anyxml" : "anydata");
Michal Vasko03ff5a72019-09-11 13:49:33 +0200295 } else {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200296 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 +0200297 }
298 break;
Michal Vasko9f96a052020-03-10 09:41:45 +0100299 case LYXP_NODE_META:
300 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 +0200301 set->val.meta[i].meta->value);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200302 break;
303 }
304 }
305 break;
306
307 case LYXP_SET_SCNODE_SET:
308 LOGDBG(LY_LDGXPATH, "set SCNODE SET:");
309 for (i = 0; i < set->used; ++i) {
310 sitem = &set->val.scnodes[i];
311
312 switch (sitem->type) {
313 case LYXP_NODE_ROOT:
314 LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT", i + 1, sitem->in_ctx);
315 break;
316 case LYXP_NODE_ROOT_CONFIG:
317 LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT CONFIG", i + 1, sitem->in_ctx);
318 break;
319 case LYXP_NODE_ELEM:
320 LOGDBG(LY_LDGXPATH, "\t%d (%u): ELEM %s", i + 1, sitem->in_ctx, sitem->scnode->name);
321 break;
322 default:
323 LOGINT(NULL);
324 break;
325 }
326 }
327 break;
328
Michal Vasko03ff5a72019-09-11 13:49:33 +0200329 case LYXP_SET_BOOLEAN:
330 LOGDBG(LY_LDGXPATH, "set BOOLEAN");
Michal Vasko004d3152020-06-11 19:59:22 +0200331 LOGDBG(LY_LDGXPATH, "\t%s", (set->val.bln ? "true" : "false"));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200332 break;
333
334 case LYXP_SET_STRING:
335 LOGDBG(LY_LDGXPATH, "set STRING");
336 LOGDBG(LY_LDGXPATH, "\t%s", set->val.str);
337 break;
338
339 case LYXP_SET_NUMBER:
340 LOGDBG(LY_LDGXPATH, "set NUMBER");
341
342 if (isnan(set->val.num)) {
343 str = strdup("NaN");
344 } else if ((set->val.num == 0) || (set->val.num == -0.0f)) {
345 str = strdup("0");
346 } else if (isinf(set->val.num) && !signbit(set->val.num)) {
347 str = strdup("Infinity");
348 } else if (isinf(set->val.num) && signbit(set->val.num)) {
349 str = strdup("-Infinity");
350 } else if ((long long)set->val.num == set->val.num) {
351 if (asprintf(&str, "%lld", (long long)set->val.num) == -1) {
352 str = NULL;
353 }
354 } else {
355 if (asprintf(&str, "%03.1Lf", set->val.num) == -1) {
356 str = NULL;
357 }
358 }
359 LY_CHECK_ERR_RET(!str, LOGMEM(NULL), );
360
361 LOGDBG(LY_LDGXPATH, "\t%s", str);
362 free(str);
363 }
364}
365
366#endif
367
368/**
369 * @brief Realloc the string \p str.
370 *
371 * @param[in] ctx libyang context for logging.
372 * @param[in] needed How much free space is required.
373 * @param[in,out] str Pointer to the string to use.
374 * @param[in,out] used Used bytes in \p str.
375 * @param[in,out] size Allocated bytes in \p str.
376 * @return LY_ERR
377 */
378static LY_ERR
Michal Vasko52927e22020-03-16 17:26:14 +0100379cast_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 +0200380{
381 if (*size - *used < needed) {
382 do {
383 if ((UINT16_MAX - *size) < LYXP_STRING_CAST_SIZE_STEP) {
384 LOGERR(ctx, LY_EINVAL, "XPath string length limit (%u) reached.", UINT16_MAX);
385 return LY_EINVAL;
386 }
387 *size += LYXP_STRING_CAST_SIZE_STEP;
388 } while (*size - *used < needed);
389 *str = ly_realloc(*str, *size * sizeof(char));
390 LY_CHECK_ERR_RET(!(*str), LOGMEM(ctx), LY_EMEM);
391 }
392
393 return LY_SUCCESS;
394}
395
396/**
397 * @brief Cast nodes recursively to one string @p str.
398 *
399 * @param[in] node Node to cast.
400 * @param[in] fake_cont Whether to put the data into a "fake" container.
401 * @param[in] root_type Type of the XPath root.
402 * @param[in] indent Current indent.
403 * @param[in,out] str Resulting string.
404 * @param[in,out] used Used bytes in @p str.
405 * @param[in,out] size Allocated bytes in @p str.
406 * @return LY_ERR
407 */
408static LY_ERR
Michal Vaskoddd76592022-01-17 13:34:48 +0100409cast_string_recursive(const struct lyd_node *node, ly_bool fake_cont, enum lyxp_node_type root_type, uint16_t indent,
410 char **str, uint16_t *used, uint16_t *size)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200411{
Radek Krejci7f769d72020-07-11 23:13:56 +0200412 char *buf, *line, *ptr = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200413 const char *value_str;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200414 const struct lyd_node *child;
Michal Vasko60ea6352020-06-29 13:39:39 +0200415 struct lyd_node *tree;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200416 struct lyd_node_any *any;
417 LY_ERR rc;
418
419 if ((root_type == LYXP_NODE_ROOT_CONFIG) && (node->schema->flags & LYS_CONFIG_R)) {
420 return LY_SUCCESS;
421 }
422
423 if (fake_cont) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200424 rc = cast_string_realloc(LYD_CTX(node), 1, str, used, size);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200425 LY_CHECK_RET(rc);
426 strcpy(*str + (*used - 1), "\n");
427 ++(*used);
428
429 ++indent;
430 }
431
432 switch (node->schema->nodetype) {
433 case LYS_CONTAINER:
434 case LYS_LIST:
435 case LYS_RPC:
436 case LYS_NOTIF:
Michal Vaskob7be7a82020-08-20 09:09:04 +0200437 rc = cast_string_realloc(LYD_CTX(node), 1, str, used, size);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200438 LY_CHECK_RET(rc);
439 strcpy(*str + (*used - 1), "\n");
440 ++(*used);
441
Radek Krejcia1c1e542020-09-29 16:06:52 +0200442 for (child = lyd_child(node); child; child = child->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200443 rc = cast_string_recursive(child, 0, root_type, indent + 1, str, used, size);
444 LY_CHECK_RET(rc);
445 }
446
447 break;
448
449 case LYS_LEAF:
450 case LYS_LEAFLIST:
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200451 value_str = lyd_get_value(node);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200452
453 /* print indent */
Michal Vaskob7be7a82020-08-20 09:09:04 +0200454 LY_CHECK_RET(cast_string_realloc(LYD_CTX(node), indent * 2 + strlen(value_str) + 1, str, used, size));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200455 memset(*str + (*used - 1), ' ', indent * 2);
456 *used += indent * 2;
457
458 /* print value */
459 if (*used == 1) {
460 sprintf(*str + (*used - 1), "%s", value_str);
461 *used += strlen(value_str);
462 } else {
463 sprintf(*str + (*used - 1), "%s\n", value_str);
464 *used += strlen(value_str) + 1;
465 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200466
467 break;
468
469 case LYS_ANYXML:
470 case LYS_ANYDATA:
471 any = (struct lyd_node_any *)node;
472 if (!(void *)any->value.tree) {
473 /* no content */
474 buf = strdup("");
Michal Vaskob7be7a82020-08-20 09:09:04 +0200475 LY_CHECK_ERR_RET(!buf, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200476 } else {
Radek Krejci241f6b52020-05-21 18:13:49 +0200477 struct ly_out *out;
Radek Krejcia5bba312020-01-09 15:41:20 +0100478
Michal Vasko60ea6352020-06-29 13:39:39 +0200479 if (any->value_type == LYD_ANYDATA_LYB) {
480 /* try to parse it into a data tree */
Michal Vaskoddd76592022-01-17 13:34:48 +0100481 if (lyd_parse_data_mem((struct ly_ctx *)LYD_CTX(node), any->value.mem, LYD_LYB,
482 LYD_PARSE_ONLY | LYD_PARSE_STRICT, 0, &tree) == LY_SUCCESS) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200483 /* successfully parsed */
484 free(any->value.mem);
485 any->value.tree = tree;
486 any->value_type = LYD_ANYDATA_DATATREE;
487 }
Radek Krejci7931b192020-06-25 17:05:03 +0200488 /* error is covered by the following switch where LYD_ANYDATA_LYB causes failure */
Michal Vasko60ea6352020-06-29 13:39:39 +0200489 }
490
Michal Vasko03ff5a72019-09-11 13:49:33 +0200491 switch (any->value_type) {
492 case LYD_ANYDATA_STRING:
493 case LYD_ANYDATA_XML:
494 case LYD_ANYDATA_JSON:
495 buf = strdup(any->value.json);
Michal Vaskob7be7a82020-08-20 09:09:04 +0200496 LY_CHECK_ERR_RET(!buf, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200497 break;
498 case LYD_ANYDATA_DATATREE:
Radek Krejci84ce7b12020-06-11 17:28:25 +0200499 LY_CHECK_RET(ly_out_new_memory(&buf, 0, &out));
Michal Vasko3a41dff2020-07-15 14:30:28 +0200500 rc = lyd_print_all(out, any->value.tree, LYD_XML, 0);
Radek Krejci241f6b52020-05-21 18:13:49 +0200501 ly_out_free(out, NULL, 0);
Radek Krejcia5bba312020-01-09 15:41:20 +0100502 LY_CHECK_RET(rc < 0, -rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200503 break;
Michal Vasko60ea6352020-06-29 13:39:39 +0200504 case LYD_ANYDATA_LYB:
Michal Vaskob7be7a82020-08-20 09:09:04 +0200505 LOGERR(LYD_CTX(node), LY_EINVAL, "Cannot convert LYB anydata into string.");
Michal Vasko60ea6352020-06-29 13:39:39 +0200506 return LY_EINVAL;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200507 }
508 }
509
510 line = strtok_r(buf, "\n", &ptr);
511 do {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200512 rc = cast_string_realloc(LYD_CTX(node), indent * 2 + strlen(line) + 1, str, used, size);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200513 if (rc != LY_SUCCESS) {
514 free(buf);
515 return rc;
516 }
517 memset(*str + (*used - 1), ' ', indent * 2);
518 *used += indent * 2;
519
520 strcpy(*str + (*used - 1), line);
521 *used += strlen(line);
522
523 strcpy(*str + (*used - 1), "\n");
524 *used += 1;
525 } while ((line = strtok_r(NULL, "\n", &ptr)));
526
527 free(buf);
528 break;
529
530 default:
Michal Vaskob7be7a82020-08-20 09:09:04 +0200531 LOGINT_RET(LYD_CTX(node));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200532 }
533
534 if (fake_cont) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200535 rc = cast_string_realloc(LYD_CTX(node), 1, str, used, size);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200536 LY_CHECK_RET(rc);
537 strcpy(*str + (*used - 1), "\n");
538 ++(*used);
539
540 --indent;
541 }
542
543 return LY_SUCCESS;
544}
545
546/**
547 * @brief Cast an element into a string.
548 *
549 * @param[in] node Node to cast.
550 * @param[in] fake_cont Whether to put the data into a "fake" container.
551 * @param[in] root_type Type of the XPath root.
552 * @param[out] str Element cast to dynamically-allocated string.
553 * @return LY_ERR
554 */
555static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200556cast_string_elem(struct lyd_node *node, ly_bool fake_cont, enum lyxp_node_type root_type, char **str)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200557{
558 uint16_t used, size;
559 LY_ERR rc;
560
561 *str = malloc(LYXP_STRING_CAST_SIZE_START * sizeof(char));
Michal Vaskob7be7a82020-08-20 09:09:04 +0200562 LY_CHECK_ERR_RET(!*str, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200563 (*str)[0] = '\0';
564 used = 1;
565 size = LYXP_STRING_CAST_SIZE_START;
566
567 rc = cast_string_recursive(node, fake_cont, root_type, 0, str, &used, &size);
568 if (rc != LY_SUCCESS) {
569 free(*str);
570 return rc;
571 }
572
573 if (size > used) {
574 *str = ly_realloc(*str, used * sizeof(char));
Michal Vaskob7be7a82020-08-20 09:09:04 +0200575 LY_CHECK_ERR_RET(!*str, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200576 }
577 return LY_SUCCESS;
578}
579
580/**
581 * @brief Cast a LYXP_SET_NODE_SET set into a string.
582 * Context position aware.
583 *
584 * @param[in] set Set to cast.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200585 * @param[out] str Cast dynamically-allocated string.
586 * @return LY_ERR
587 */
588static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100589cast_node_set_to_string(struct lyxp_set *set, char **str)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200590{
Michal Vaskoaa677062021-03-09 13:52:53 +0100591 if (!set->used) {
592 *str = strdup("");
593 if (!*str) {
594 LOGMEM_RET(set->ctx);
595 }
596 return LY_SUCCESS;
597 }
598
Michal Vasko03ff5a72019-09-11 13:49:33 +0200599 switch (set->val.nodes[0].type) {
Michal Vasko2caefc12019-11-14 16:07:56 +0100600 case LYXP_NODE_NONE:
601 /* invalid */
602 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200603 case LYXP_NODE_ROOT:
604 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100605 return cast_string_elem(set->val.nodes[0].node, 1, set->root_type, str);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200606 case LYXP_NODE_ELEM:
607 case LYXP_NODE_TEXT:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100608 return cast_string_elem(set->val.nodes[0].node, 0, set->root_type, str);
Michal Vasko9f96a052020-03-10 09:41:45 +0100609 case LYXP_NODE_META:
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200610 *str = strdup(lyd_get_meta_value(set->val.meta[0].meta));
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200611 if (!*str) {
612 LOGMEM_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200613 }
614 return LY_SUCCESS;
615 }
616
617 LOGINT_RET(set->ctx);
618}
619
620/**
621 * @brief Cast a string into an XPath number.
622 *
623 * @param[in] str String to use.
624 * @return Cast number.
625 */
626static long double
627cast_string_to_number(const char *str)
628{
629 long double num;
630 char *ptr;
631
632 errno = 0;
633 num = strtold(str, &ptr);
634 if (errno || *ptr) {
635 num = NAN;
636 }
637 return num;
638}
639
640/**
641 * @brief Callback for checking value equality.
642 *
Michal Vasko62524a92021-02-26 10:08:50 +0100643 * Implementation of ::lyht_value_equal_cb.
Radek Krejci857189e2020-09-01 13:26:36 +0200644 *
Michal Vasko03ff5a72019-09-11 13:49:33 +0200645 * @param[in] val1_p First value.
646 * @param[in] val2_p Second value.
647 * @param[in] mod Whether hash table is being modified.
648 * @param[in] cb_data Callback data.
Radek Krejci857189e2020-09-01 13:26:36 +0200649 * @return Boolean value whether values are equal or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200650 */
Radek Krejci857189e2020-09-01 13:26:36 +0200651static ly_bool
652set_values_equal_cb(void *val1_p, void *val2_p, ly_bool UNUSED(mod), void *UNUSED(cb_data))
Michal Vasko03ff5a72019-09-11 13:49:33 +0200653{
654 struct lyxp_set_hash_node *val1, *val2;
655
656 val1 = (struct lyxp_set_hash_node *)val1_p;
657 val2 = (struct lyxp_set_hash_node *)val2_p;
658
659 if ((val1->node == val2->node) && (val1->type == val2->type)) {
660 return 1;
661 }
662
663 return 0;
664}
665
666/**
667 * @brief Insert node and its hash into set.
668 *
669 * @param[in] set et to insert to.
670 * @param[in] node Node with hash.
671 * @param[in] type Node type.
672 */
673static void
674set_insert_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type)
675{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200676 LY_ERR r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200677 uint32_t i, hash;
678 struct lyxp_set_hash_node hnode;
679
680 if (!set->ht && (set->used >= LYD_HT_MIN_ITEMS)) {
681 /* create hash table and add all the nodes */
682 set->ht = lyht_new(1, sizeof(struct lyxp_set_hash_node), set_values_equal_cb, NULL, 1);
683 for (i = 0; i < set->used; ++i) {
684 hnode.node = set->val.nodes[i].node;
685 hnode.type = set->val.nodes[i].type;
686
687 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
688 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
689 hash = dict_hash_multi(hash, NULL, 0);
690
691 r = lyht_insert(set->ht, &hnode, hash, NULL);
692 assert(!r);
693 (void)r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200694
Michal Vasko9d6befd2019-12-11 14:56:56 +0100695 if (hnode.node == node) {
696 /* it was just added, do not add it twice */
697 node = NULL;
698 }
699 }
700 }
701
702 if (set->ht && node) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200703 /* add the new node into hash table */
704 hnode.node = node;
705 hnode.type = type;
706
707 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
708 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
709 hash = dict_hash_multi(hash, NULL, 0);
710
711 r = lyht_insert(set->ht, &hnode, hash, NULL);
712 assert(!r);
713 (void)r;
714 }
715}
716
717/**
718 * @brief Remove node and its hash from set.
719 *
720 * @param[in] set Set to remove from.
721 * @param[in] node Node to remove.
722 * @param[in] type Node type.
723 */
724static void
725set_remove_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type)
726{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200727 LY_ERR r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200728 struct lyxp_set_hash_node hnode;
729 uint32_t hash;
730
731 if (set->ht) {
732 hnode.node = node;
733 hnode.type = type;
734
735 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
736 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
737 hash = dict_hash_multi(hash, NULL, 0);
738
739 r = lyht_remove(set->ht, &hnode, hash);
740 assert(!r);
741 (void)r;
742
743 if (!set->ht->used) {
744 lyht_free(set->ht);
745 set->ht = NULL;
746 }
747 }
748}
749
750/**
751 * @brief Check whether node is in set based on its hash.
752 *
753 * @param[in] set Set to search in.
754 * @param[in] node Node to search for.
755 * @param[in] type Node type.
756 * @param[in] skip_idx Index in @p set to skip.
757 * @return LY_ERR
758 */
759static LY_ERR
760set_dup_node_hash_check(const struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type, int skip_idx)
761{
762 struct lyxp_set_hash_node hnode, *match_p;
763 uint32_t hash;
764
765 hnode.node = node;
766 hnode.type = type;
767
768 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
769 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
770 hash = dict_hash_multi(hash, NULL, 0);
771
772 if (!lyht_find(set->ht, &hnode, hash, (void **)&match_p)) {
773 if ((skip_idx > -1) && (set->val.nodes[skip_idx].node == match_p->node) && (set->val.nodes[skip_idx].type == match_p->type)) {
774 /* we found it on the index that should be skipped, find another */
775 hnode = *match_p;
776 if (lyht_find_next(set->ht, &hnode, hash, (void **)&match_p)) {
777 /* none other found */
778 return LY_SUCCESS;
779 }
780 }
781
782 return LY_EEXIST;
783 }
784
785 /* not found */
786 return LY_SUCCESS;
787}
788
Michal Vaskod3678892020-05-21 10:06:58 +0200789void
790lyxp_set_free_content(struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200791{
792 if (!set) {
793 return;
794 }
795
796 if (set->type == LYXP_SET_NODE_SET) {
797 free(set->val.nodes);
798 lyht_free(set->ht);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200799 } else if (set->type == LYXP_SET_SCNODE_SET) {
800 free(set->val.scnodes);
Michal Vaskod3678892020-05-21 10:06:58 +0200801 lyht_free(set->ht);
802 } else {
803 if (set->type == LYXP_SET_STRING) {
804 free(set->val.str);
805 }
806 set->type = LYXP_SET_NODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200807 }
Michal Vaskod3678892020-05-21 10:06:58 +0200808
809 set->val.nodes = NULL;
810 set->used = 0;
811 set->size = 0;
812 set->ht = NULL;
813 set->ctx_pos = 0;
aPiecek748da732021-06-01 09:56:43 +0200814 set->ctx_size = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200815}
816
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100817/**
818 * @brief Free dynamically-allocated set.
819 *
820 * @param[in] set Set to free.
821 */
822static void
Michal Vasko03ff5a72019-09-11 13:49:33 +0200823lyxp_set_free(struct lyxp_set *set)
824{
825 if (!set) {
826 return;
827 }
828
Michal Vaskod3678892020-05-21 10:06:58 +0200829 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200830 free(set);
831}
832
833/**
834 * @brief Initialize set context.
835 *
836 * @param[in] new Set to initialize.
837 * @param[in] set Arbitrary initialized set.
838 */
839static void
Michal Vasko4c7763f2020-07-27 17:40:37 +0200840set_init(struct lyxp_set *new, const struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200841{
842 memset(new, 0, sizeof *new);
Michal Vasko02a77382019-09-12 11:47:35 +0200843 if (set) {
844 new->ctx = set->ctx;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200845 new->cur_node = set->cur_node;
Michal Vasko588112f2019-12-10 14:51:53 +0100846 new->root_type = set->root_type;
Michal Vasko6b26e742020-07-17 15:02:10 +0200847 new->context_op = set->context_op;
Michal Vaskof03ed032020-03-04 13:31:44 +0100848 new->tree = set->tree;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200849 new->cur_mod = set->cur_mod;
Michal Vasko02a77382019-09-12 11:47:35 +0200850 new->format = set->format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200851 new->prefix_data = set->prefix_data;
aPiecekfba75362021-10-07 12:39:48 +0200852 new->vars = set->vars;
Michal Vasko02a77382019-09-12 11:47:35 +0200853 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200854}
855
856/**
857 * @brief Create a deep copy of a set.
858 *
859 * @param[in] set Set to copy.
860 * @return Copy of @p set.
861 */
862static struct lyxp_set *
863set_copy(struct lyxp_set *set)
864{
865 struct lyxp_set *ret;
Michal Vasko1fdd8fa2021-01-08 09:21:45 +0100866 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200867
868 if (!set) {
869 return NULL;
870 }
871
872 ret = malloc(sizeof *ret);
873 LY_CHECK_ERR_RET(!ret, LOGMEM(set->ctx), NULL);
874 set_init(ret, set);
875
876 if (set->type == LYXP_SET_SCNODE_SET) {
877 ret->type = set->type;
878
879 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100880 if ((set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) ||
881 (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +0200882 uint32_t idx;
883 LY_CHECK_ERR_RET(lyxp_set_scnode_insert_node(ret, set->val.scnodes[i].scnode, set->val.scnodes[i].type, &idx),
884 lyxp_set_free(ret), NULL);
Michal Vasko3f27c522020-01-06 08:37:49 +0100885 /* coverity seems to think scnodes can be NULL */
Radek Krejciaa6b53f2020-08-27 15:19:03 +0200886 if (!ret->val.scnodes) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200887 lyxp_set_free(ret);
888 return NULL;
889 }
Michal Vaskoba716542019-12-16 10:01:58 +0100890 ret->val.scnodes[idx].in_ctx = set->val.scnodes[i].in_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200891 }
892 }
893 } else if (set->type == LYXP_SET_NODE_SET) {
894 ret->type = set->type;
Michal Vasko08e9b112021-06-11 15:41:17 +0200895 if (set->used) {
896 ret->val.nodes = malloc(set->used * sizeof *ret->val.nodes);
897 LY_CHECK_ERR_RET(!ret->val.nodes, LOGMEM(set->ctx); free(ret), NULL);
898 memcpy(ret->val.nodes, set->val.nodes, set->used * sizeof *ret->val.nodes);
899 } else {
900 ret->val.nodes = NULL;
901 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200902
903 ret->used = ret->size = set->used;
904 ret->ctx_pos = set->ctx_pos;
905 ret->ctx_size = set->ctx_size;
Michal Vasko4a04e542021-02-01 08:58:30 +0100906 if (set->ht) {
907 ret->ht = lyht_dup(set->ht);
908 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200909 } else {
Radek Krejci0f969882020-08-21 16:56:47 +0200910 memcpy(ret, set, sizeof *ret);
911 if (set->type == LYXP_SET_STRING) {
912 ret->val.str = strdup(set->val.str);
913 LY_CHECK_ERR_RET(!ret->val.str, LOGMEM(set->ctx); free(ret), NULL);
914 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200915 }
916
917 return ret;
918}
919
920/**
921 * @brief Fill XPath set with a string. Any current data are disposed of.
922 *
923 * @param[in] set Set to fill.
924 * @param[in] string String to fill into \p set.
925 * @param[in] str_len Length of \p string. 0 is a valid value!
926 */
927static void
928set_fill_string(struct lyxp_set *set, const char *string, uint16_t str_len)
929{
Michal Vaskod3678892020-05-21 10:06:58 +0200930 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200931
932 set->type = LYXP_SET_STRING;
933 if ((str_len == 0) && (string[0] != '\0')) {
934 string = "";
935 }
936 set->val.str = strndup(string, str_len);
937}
938
939/**
940 * @brief Fill XPath set with a number. Any current data are disposed of.
941 *
942 * @param[in] set Set to fill.
943 * @param[in] number Number to fill into \p set.
944 */
945static void
946set_fill_number(struct lyxp_set *set, long double number)
947{
Michal Vaskod3678892020-05-21 10:06:58 +0200948 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200949
950 set->type = LYXP_SET_NUMBER;
951 set->val.num = number;
952}
953
954/**
955 * @brief Fill XPath set with a boolean. Any current data are disposed of.
956 *
957 * @param[in] set Set to fill.
958 * @param[in] boolean Boolean to fill into \p set.
959 */
960static void
Radek Krejci857189e2020-09-01 13:26:36 +0200961set_fill_boolean(struct lyxp_set *set, ly_bool boolean)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200962{
Michal Vaskod3678892020-05-21 10:06:58 +0200963 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200964
965 set->type = LYXP_SET_BOOLEAN;
Michal Vasko004d3152020-06-11 19:59:22 +0200966 set->val.bln = boolean;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200967}
968
969/**
970 * @brief Fill XPath set with the value from another set (deep assign).
971 * Any current data are disposed of.
972 *
973 * @param[in] trg Set to fill.
974 * @param[in] src Source set to copy into \p trg.
975 */
976static void
Michal Vasko4c7763f2020-07-27 17:40:37 +0200977set_fill_set(struct lyxp_set *trg, const struct lyxp_set *src)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200978{
979 if (!trg || !src) {
980 return;
981 }
982
983 if (trg->type == LYXP_SET_NODE_SET) {
984 free(trg->val.nodes);
985 } else if (trg->type == LYXP_SET_STRING) {
986 free(trg->val.str);
987 }
988 set_init(trg, src);
989
990 if (src->type == LYXP_SET_SCNODE_SET) {
991 trg->type = LYXP_SET_SCNODE_SET;
992 trg->used = src->used;
993 trg->size = src->used;
994
Michal Vasko08e9b112021-06-11 15:41:17 +0200995 if (trg->size) {
996 trg->val.scnodes = ly_realloc(trg->val.scnodes, trg->size * sizeof *trg->val.scnodes);
997 LY_CHECK_ERR_RET(!trg->val.scnodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), );
998 memcpy(trg->val.scnodes, src->val.scnodes, src->used * sizeof *src->val.scnodes);
999 } else {
1000 trg->val.scnodes = NULL;
1001 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001002 } else if (src->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +02001003 set_fill_boolean(trg, src->val.bln);
Michal Vasko44f3d2c2020-08-24 09:49:38 +02001004 } else if (src->type == LYXP_SET_NUMBER) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001005 set_fill_number(trg, src->val.num);
1006 } else if (src->type == LYXP_SET_STRING) {
1007 set_fill_string(trg, src->val.str, strlen(src->val.str));
1008 } else {
1009 if (trg->type == LYXP_SET_NODE_SET) {
1010 free(trg->val.nodes);
1011 } else if (trg->type == LYXP_SET_STRING) {
1012 free(trg->val.str);
1013 }
1014
Michal Vaskod3678892020-05-21 10:06:58 +02001015 assert(src->type == LYXP_SET_NODE_SET);
1016
1017 trg->type = LYXP_SET_NODE_SET;
1018 trg->used = src->used;
1019 trg->size = src->used;
1020 trg->ctx_pos = src->ctx_pos;
1021 trg->ctx_size = src->ctx_size;
1022
Michal Vasko08e9b112021-06-11 15:41:17 +02001023 if (trg->size) {
1024 trg->val.nodes = malloc(trg->size * sizeof *trg->val.nodes);
1025 LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), );
1026 memcpy(trg->val.nodes, src->val.nodes, src->used * sizeof *src->val.nodes);
1027 } else {
1028 trg->val.nodes = NULL;
1029 }
Michal Vaskod3678892020-05-21 10:06:58 +02001030 if (src->ht) {
1031 trg->ht = lyht_dup(src->ht);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001032 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02001033 trg->ht = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001034 }
1035 }
1036}
1037
1038/**
1039 * @brief Clear context of all schema nodes.
1040 *
1041 * @param[in] set Set to clear.
Michal Vasko1a09b212021-05-06 13:00:10 +02001042 * @param[in] new_ctx New context state for all the nodes currently in the context.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001043 */
1044static void
Michal Vasko1a09b212021-05-06 13:00:10 +02001045set_scnode_clear_ctx(struct lyxp_set *set, int32_t new_ctx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001046{
1047 uint32_t i;
1048
1049 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001050 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko1a09b212021-05-06 13:00:10 +02001051 set->val.scnodes[i].in_ctx = new_ctx;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001052 } else if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START) {
1053 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001054 }
1055 }
1056}
1057
1058/**
1059 * @brief Remove a node from a set. Removing last node changes
1060 * set into LYXP_SET_EMPTY. Context position aware.
1061 *
1062 * @param[in] set Set to use.
1063 * @param[in] idx Index from @p set of the node to be removed.
1064 */
1065static void
1066set_remove_node(struct lyxp_set *set, uint32_t idx)
1067{
1068 assert(set && (set->type == LYXP_SET_NODE_SET));
1069 assert(idx < set->used);
1070
1071 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
1072
1073 --set->used;
Michal Vasko08e9b112021-06-11 15:41:17 +02001074 if (idx < set->used) {
1075 memmove(&set->val.nodes[idx], &set->val.nodes[idx + 1], (set->used - idx) * sizeof *set->val.nodes);
1076 } else if (!set->used) {
Michal Vaskod3678892020-05-21 10:06:58 +02001077 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001078 }
1079}
1080
1081/**
Michal Vasko2caefc12019-11-14 16:07:56 +01001082 * @brief Remove a node from a set by setting its type to LYXP_NODE_NONE.
Michal Vasko57eab132019-09-24 11:46:26 +02001083 *
1084 * @param[in] set Set to use.
1085 * @param[in] idx Index from @p set of the node to be removed.
1086 */
1087static void
Michal Vasko2caefc12019-11-14 16:07:56 +01001088set_remove_node_none(struct lyxp_set *set, uint32_t idx)
Michal Vasko57eab132019-09-24 11:46:26 +02001089{
1090 assert(set && (set->type == LYXP_SET_NODE_SET));
1091 assert(idx < set->used);
1092
Michal Vasko2caefc12019-11-14 16:07:56 +01001093 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1094 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
1095 }
1096 set->val.nodes[idx].type = LYXP_NODE_NONE;
Michal Vasko57eab132019-09-24 11:46:26 +02001097}
1098
1099/**
Michal Vasko2caefc12019-11-14 16:07:56 +01001100 * @brief Remove all LYXP_NODE_NONE nodes from a set. Removing last node changes
Michal Vasko57eab132019-09-24 11:46:26 +02001101 * set into LYXP_SET_EMPTY. Context position aware.
1102 *
1103 * @param[in] set Set to consolidate.
1104 */
1105static void
Michal Vasko2caefc12019-11-14 16:07:56 +01001106set_remove_nodes_none(struct lyxp_set *set)
Michal Vasko57eab132019-09-24 11:46:26 +02001107{
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01001108 uint32_t i, orig_used, end = 0;
1109 int64_t start;
Michal Vasko57eab132019-09-24 11:46:26 +02001110
Michal Vaskod3678892020-05-21 10:06:58 +02001111 assert(set);
Michal Vasko57eab132019-09-24 11:46:26 +02001112
1113 orig_used = set->used;
1114 set->used = 0;
Michal Vaskod989ba02020-08-24 10:59:24 +02001115 for (i = 0; i < orig_used; ) {
Michal Vasko57eab132019-09-24 11:46:26 +02001116 start = -1;
1117 do {
Michal Vasko2caefc12019-11-14 16:07:56 +01001118 if ((set->val.nodes[i].type != LYXP_NODE_NONE) && (start == -1)) {
Michal Vasko57eab132019-09-24 11:46:26 +02001119 start = i;
Michal Vasko2caefc12019-11-14 16:07:56 +01001120 } else if ((start > -1) && (set->val.nodes[i].type == LYXP_NODE_NONE)) {
Michal Vasko57eab132019-09-24 11:46:26 +02001121 end = i;
1122 ++i;
1123 break;
1124 }
1125
1126 ++i;
1127 if (i == orig_used) {
1128 end = i;
1129 }
1130 } while (i < orig_used);
1131
1132 if (start > -1) {
1133 /* move the whole chunk of valid nodes together */
1134 if (set->used != (unsigned)start) {
1135 memmove(&set->val.nodes[set->used], &set->val.nodes[start], (end - start) * sizeof *set->val.nodes);
1136 }
1137 set->used += end - start;
1138 }
1139 }
Michal Vasko57eab132019-09-24 11:46:26 +02001140}
1141
1142/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02001143 * @brief Check for duplicates in a node set.
1144 *
1145 * @param[in] set Set to check.
1146 * @param[in] node Node to look for in @p set.
1147 * @param[in] node_type Type of @p node.
1148 * @param[in] skip_idx Index from @p set to skip.
1149 * @return LY_ERR
1150 */
1151static LY_ERR
1152set_dup_node_check(const struct lyxp_set *set, const struct lyd_node *node, enum lyxp_node_type node_type, int skip_idx)
1153{
1154 uint32_t i;
1155
Michal Vasko2caefc12019-11-14 16:07:56 +01001156 if (set->ht && node) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001157 return set_dup_node_hash_check(set, (struct lyd_node *)node, node_type, skip_idx);
1158 }
1159
1160 for (i = 0; i < set->used; ++i) {
1161 if ((skip_idx > -1) && (i == (unsigned)skip_idx)) {
1162 continue;
1163 }
1164
1165 if ((set->val.nodes[i].node == node) && (set->val.nodes[i].type == node_type)) {
1166 return LY_EEXIST;
1167 }
1168 }
1169
1170 return LY_SUCCESS;
1171}
1172
Radek Krejci857189e2020-09-01 13:26:36 +02001173ly_bool
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001174lyxp_set_scnode_contains(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type, int skip_idx,
1175 uint32_t *index_p)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001176{
1177 uint32_t i;
1178
1179 for (i = 0; i < set->used; ++i) {
1180 if ((skip_idx > -1) && (i == (unsigned)skip_idx)) {
1181 continue;
1182 }
1183
1184 if ((set->val.scnodes[i].scnode == node) && (set->val.scnodes[i].type == node_type)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001185 if (index_p) {
1186 *index_p = i;
1187 }
1188 return 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001189 }
1190 }
1191
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001192 return 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001193}
1194
Michal Vaskoecd62de2019-11-13 12:35:11 +01001195void
1196lyxp_set_scnode_merge(struct lyxp_set *set1, struct lyxp_set *set2)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001197{
1198 uint32_t orig_used, i, j;
1199
Michal Vaskod3678892020-05-21 10:06:58 +02001200 assert((set1->type == LYXP_SET_SCNODE_SET) && (set2->type == LYXP_SET_SCNODE_SET));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001201
Michal Vaskod3678892020-05-21 10:06:58 +02001202 if (!set2->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001203 return;
1204 }
1205
Michal Vaskod3678892020-05-21 10:06:58 +02001206 if (!set1->used) {
aPiecekadc1e4f2021-10-07 11:15:12 +02001207 /* release hidden allocated data (lyxp_set.size) */
1208 lyxp_set_free_content(set1);
1209 /* direct copying of the entire structure */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001210 memcpy(set1, set2, sizeof *set1);
1211 return;
1212 }
1213
1214 if (set1->used + set2->used > set1->size) {
1215 set1->size = set1->used + set2->used;
1216 set1->val.scnodes = ly_realloc(set1->val.scnodes, set1->size * sizeof *set1->val.scnodes);
1217 LY_CHECK_ERR_RET(!set1->val.scnodes, LOGMEM(set1->ctx), );
1218 }
1219
1220 orig_used = set1->used;
1221
1222 for (i = 0; i < set2->used; ++i) {
1223 for (j = 0; j < orig_used; ++j) {
1224 /* detect duplicities */
1225 if (set1->val.scnodes[j].scnode == set2->val.scnodes[i].scnode) {
1226 break;
1227 }
1228 }
1229
Michal Vasko0587bce2020-12-10 12:19:21 +01001230 if (j < orig_used) {
1231 /* node is there, but update its status if needed */
1232 if (set1->val.scnodes[j].in_ctx == LYXP_SET_SCNODE_START_USED) {
1233 set1->val.scnodes[j].in_ctx = set2->val.scnodes[i].in_ctx;
Michal Vasko1a09b212021-05-06 13:00:10 +02001234 } else if ((set1->val.scnodes[j].in_ctx == LYXP_SET_SCNODE_ATOM_NODE) &&
1235 (set2->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_VAL)) {
1236 set1->val.scnodes[j].in_ctx = set2->val.scnodes[i].in_ctx;
Michal Vasko0587bce2020-12-10 12:19:21 +01001237 }
1238 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001239 memcpy(&set1->val.scnodes[set1->used], &set2->val.scnodes[i], sizeof *set2->val.scnodes);
1240 ++set1->used;
1241 }
1242 }
1243
Michal Vaskod3678892020-05-21 10:06:58 +02001244 lyxp_set_free_content(set2);
1245 set2->type = LYXP_SET_SCNODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001246}
1247
1248/**
1249 * @brief Insert a node into a set. Context position aware.
1250 *
1251 * @param[in] set Set to use.
1252 * @param[in] node Node to insert to @p set.
1253 * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting.
1254 * @param[in] node_type Node type of @p node.
1255 * @param[in] idx Index in @p set to insert into.
1256 */
1257static void
1258set_insert_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx)
1259{
Michal Vaskod3678892020-05-21 10:06:58 +02001260 assert(set && (set->type == LYXP_SET_NODE_SET));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001261
Michal Vaskod3678892020-05-21 10:06:58 +02001262 if (!set->size) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001263 /* first item */
1264 if (idx) {
1265 /* no real harm done, but it is a bug */
1266 LOGINT(set->ctx);
1267 idx = 0;
1268 }
1269 set->val.nodes = malloc(LYXP_SET_SIZE_START * sizeof *set->val.nodes);
1270 LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), );
1271 set->type = LYXP_SET_NODE_SET;
1272 set->used = 0;
1273 set->size = LYXP_SET_SIZE_START;
1274 set->ctx_pos = 1;
1275 set->ctx_size = 1;
1276 set->ht = NULL;
1277 } else {
1278 /* not an empty set */
1279 if (set->used == set->size) {
1280
1281 /* set is full */
Michal Vasko871df522022-04-06 12:14:41 +02001282 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 +02001283 LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), );
Michal Vasko871df522022-04-06 12:14:41 +02001284 set->size *= LYXP_SET_SIZE_MUL_STEP;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001285 }
1286
1287 if (idx > set->used) {
1288 LOGINT(set->ctx);
1289 idx = set->used;
1290 }
1291
1292 /* make space for the new node */
1293 if (idx < set->used) {
1294 memmove(&set->val.nodes[idx + 1], &set->val.nodes[idx], (set->used - idx) * sizeof *set->val.nodes);
1295 }
1296 }
1297
1298 /* finally assign the value */
1299 set->val.nodes[idx].node = (struct lyd_node *)node;
1300 set->val.nodes[idx].type = node_type;
1301 set->val.nodes[idx].pos = pos;
1302 ++set->used;
1303
Michal Vasko2416fdd2021-10-01 11:07:10 +02001304 /* add into hash table */
1305 set_insert_node_hash(set, (struct lyd_node *)node, node_type);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001306}
1307
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001308LY_ERR
Michal Vaskoddd76592022-01-17 13:34:48 +01001309lyxp_set_scnode_insert_node(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type,
1310 uint32_t *index_p)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001311{
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001312 uint32_t index;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001313
1314 assert(set->type == LYXP_SET_SCNODE_SET);
1315
Michal Vasko871df522022-04-06 12:14:41 +02001316 if (!set->size) {
1317 /* first item */
1318 set->val.scnodes = malloc(LYXP_SET_SIZE_START * sizeof *set->val.scnodes);
1319 LY_CHECK_ERR_RET(!set->val.scnodes, LOGMEM(set->ctx), LY_EMEM);
1320 set->type = LYXP_SET_SCNODE_SET;
1321 set->used = 0;
1322 set->size = LYXP_SET_SIZE_START;
1323 set->ctx_pos = 1;
1324 set->ctx_size = 1;
1325 set->ht = NULL;
1326 }
1327
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001328 if (lyxp_set_scnode_contains(set, node, node_type, -1, &index)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001329 set->val.scnodes[index].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001330 } else {
1331 if (set->used == set->size) {
Michal Vasko871df522022-04-06 12:14:41 +02001332 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 +02001333 LY_CHECK_ERR_RET(!set->val.scnodes, LOGMEM(set->ctx), LY_EMEM);
Michal Vasko871df522022-04-06 12:14:41 +02001334 set->size *= LYXP_SET_SIZE_MUL_STEP;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001335 }
1336
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001337 index = set->used;
1338 set->val.scnodes[index].scnode = (struct lysc_node *)node;
1339 set->val.scnodes[index].type = node_type;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001340 set->val.scnodes[index].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001341 ++set->used;
1342 }
1343
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001344 if (index_p) {
1345 *index_p = index;
1346 }
1347
1348 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001349}
1350
1351/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02001352 * @brief Set all nodes with ctx 1 to a new unique context value.
1353 *
1354 * @param[in] set Set to modify.
1355 * @return New context value.
1356 */
Michal Vasko5c4e5892019-11-14 12:31:38 +01001357static int32_t
Michal Vasko03ff5a72019-09-11 13:49:33 +02001358set_scnode_new_in_ctx(struct lyxp_set *set)
1359{
Michal Vasko5c4e5892019-11-14 12:31:38 +01001360 uint32_t i;
1361 int32_t ret_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001362
1363 assert(set->type == LYXP_SET_SCNODE_SET);
1364
Radek Krejcif13b87b2020-12-01 22:02:17 +01001365 ret_ctx = LYXP_SET_SCNODE_ATOM_PRED_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001366retry:
1367 for (i = 0; i < set->used; ++i) {
1368 if (set->val.scnodes[i].in_ctx >= ret_ctx) {
1369 ret_ctx = set->val.scnodes[i].in_ctx + 1;
1370 goto retry;
1371 }
1372 }
1373 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001374 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001375 set->val.scnodes[i].in_ctx = ret_ctx;
1376 }
1377 }
1378
1379 return ret_ctx;
1380}
1381
1382/**
1383 * @brief Get unique @p node position in the data.
1384 *
1385 * @param[in] node Node to find.
1386 * @param[in] node_type Node type of @p node.
1387 * @param[in] root Root node.
1388 * @param[in] root_type Type of the XPath @p root node.
1389 * @param[in] prev Node that we think is before @p node in DFS from @p root. Can optionally
1390 * be used to increase efficiency and start the DFS from this node.
1391 * @param[in] prev_pos Node @p prev position. Optional, but must be set if @p prev is set.
1392 * @return Node position.
1393 */
1394static uint32_t
1395get_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 +02001396 enum lyxp_node_type root_type, const struct lyd_node **prev, uint32_t *prev_pos)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001397{
Michal Vasko56daf732020-08-10 10:57:18 +02001398 const struct lyd_node *elem = NULL, *top_sibling;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001399 uint32_t pos = 1;
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001400 ly_bool found = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001401
1402 assert(prev && prev_pos && !root->prev->next);
1403
1404 if ((node_type == LYXP_NODE_ROOT) || (node_type == LYXP_NODE_ROOT_CONFIG)) {
1405 return 0;
1406 }
1407
1408 if (*prev) {
1409 /* start from the previous element instead from the root */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001410 pos = *prev_pos;
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001411 for (top_sibling = *prev; top_sibling->parent; top_sibling = lyd_parent(top_sibling)) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001412 goto dfs_search;
1413 }
1414
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001415 LY_LIST_FOR(root, top_sibling) {
Michal Vasko56daf732020-08-10 10:57:18 +02001416 LYD_TREE_DFS_BEGIN(top_sibling, elem) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001417dfs_search:
Michal Vaskoa9309bb2021-07-09 09:31:55 +02001418 LYD_TREE_DFS_continue = 0;
1419
Michal Vasko56daf732020-08-10 10:57:18 +02001420 if (*prev && !elem) {
1421 /* resume previous DFS */
1422 elem = LYD_TREE_DFS_next = (struct lyd_node *)*prev;
1423 LYD_TREE_DFS_continue = 0;
1424 }
1425
Michal Vasko482a6822022-05-06 08:56:12 +02001426 if (!elem->schema || ((root_type == LYXP_NODE_ROOT_CONFIG) && (elem->schema->flags & LYS_CONFIG_R))) {
Michal Vasko56daf732020-08-10 10:57:18 +02001427 /* skip */
1428 LYD_TREE_DFS_continue = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001429 } else {
Michal Vasko56daf732020-08-10 10:57:18 +02001430 if (elem == node) {
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001431 found = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001432 break;
1433 }
Michal Vasko56daf732020-08-10 10:57:18 +02001434 ++pos;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001435 }
Michal Vasko56daf732020-08-10 10:57:18 +02001436
1437 LYD_TREE_DFS_END(top_sibling, elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001438 }
1439
1440 /* node found */
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001441 if (found) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001442 break;
1443 }
1444 }
1445
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001446 if (!found) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001447 if (!(*prev)) {
1448 /* we went from root and failed to find it, cannot be */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001449 LOGINT(LYD_CTX(node));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001450 return 0;
1451 } else {
Michal Vasko56daf732020-08-10 10:57:18 +02001452 /* start the search again from the beginning */
1453 *prev = root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001454
Michal Vasko56daf732020-08-10 10:57:18 +02001455 top_sibling = root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001456 pos = 1;
1457 goto dfs_search;
1458 }
1459 }
1460
1461 /* remember the last found node for next time */
1462 *prev = node;
1463 *prev_pos = pos;
1464
1465 return pos;
1466}
1467
1468/**
1469 * @brief Assign (fill) missing node positions.
1470 *
1471 * @param[in] set Set to fill positions in.
1472 * @param[in] root Context root node.
1473 * @param[in] root_type Context root type.
1474 * @return LY_ERR
1475 */
1476static LY_ERR
1477set_assign_pos(struct lyxp_set *set, const struct lyd_node *root, enum lyxp_node_type root_type)
1478{
1479 const struct lyd_node *prev = NULL, *tmp_node;
1480 uint32_t i, tmp_pos = 0;
1481
1482 for (i = 0; i < set->used; ++i) {
1483 if (!set->val.nodes[i].pos) {
1484 tmp_node = NULL;
1485 switch (set->val.nodes[i].type) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001486 case LYXP_NODE_META:
1487 tmp_node = set->val.meta[i].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001488 if (!tmp_node) {
1489 LOGINT_RET(root->schema->module->ctx);
1490 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01001491 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001492 case LYXP_NODE_ELEM:
1493 case LYXP_NODE_TEXT:
1494 if (!tmp_node) {
1495 tmp_node = set->val.nodes[i].node;
1496 }
1497 set->val.nodes[i].pos = get_node_pos(tmp_node, set->val.nodes[i].type, root, root_type, &prev, &tmp_pos);
1498 break;
1499 default:
1500 /* all roots have position 0 */
1501 break;
1502 }
1503 }
1504 }
1505
1506 return LY_SUCCESS;
1507}
1508
1509/**
Michal Vasko9f96a052020-03-10 09:41:45 +01001510 * @brief Get unique @p meta position in the parent metadata.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001511 *
Michal Vasko9f96a052020-03-10 09:41:45 +01001512 * @param[in] meta Metadata to use.
1513 * @return Metadata position.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001514 */
1515static uint16_t
Michal Vasko9f96a052020-03-10 09:41:45 +01001516get_meta_pos(struct lyd_meta *meta)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001517{
1518 uint16_t pos = 0;
Michal Vasko9f96a052020-03-10 09:41:45 +01001519 struct lyd_meta *meta2;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001520
Michal Vasko9f96a052020-03-10 09:41:45 +01001521 for (meta2 = meta->parent->meta; meta2 && (meta2 != meta); meta2 = meta2->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001522 ++pos;
1523 }
1524
Michal Vasko9f96a052020-03-10 09:41:45 +01001525 assert(meta2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001526 return pos;
1527}
1528
1529/**
1530 * @brief Compare 2 nodes in respect to XPath document order.
1531 *
1532 * @param[in] item1 1st node.
1533 * @param[in] item2 2nd node.
1534 * @return If 1st > 2nd returns 1, 1st == 2nd returns 0, and 1st < 2nd returns -1.
1535 */
1536static int
1537set_sort_compare(struct lyxp_set_node *item1, struct lyxp_set_node *item2)
1538{
Michal Vasko9f96a052020-03-10 09:41:45 +01001539 uint32_t meta_pos1 = 0, meta_pos2 = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001540
1541 if (item1->pos < item2->pos) {
1542 return -1;
1543 }
1544
1545 if (item1->pos > item2->pos) {
1546 return 1;
1547 }
1548
1549 /* node positions are equal, the fun case */
1550
1551 /* 1st ELEM - == - 2nd TEXT, 1st TEXT - == - 2nd ELEM */
1552 /* special case since text nodes are actually saved as their parents */
1553 if ((item1->node == item2->node) && (item1->type != item2->type)) {
1554 if (item1->type == LYXP_NODE_ELEM) {
1555 assert(item2->type == LYXP_NODE_TEXT);
1556 return -1;
1557 } else {
1558 assert((item1->type == LYXP_NODE_TEXT) && (item2->type == LYXP_NODE_ELEM));
1559 return 1;
1560 }
1561 }
1562
Michal Vasko9f96a052020-03-10 09:41:45 +01001563 /* we need meta positions now */
1564 if (item1->type == LYXP_NODE_META) {
1565 meta_pos1 = get_meta_pos((struct lyd_meta *)item1->node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001566 }
Michal Vasko9f96a052020-03-10 09:41:45 +01001567 if (item2->type == LYXP_NODE_META) {
1568 meta_pos2 = get_meta_pos((struct lyd_meta *)item2->node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001569 }
1570
Michal Vasko9f96a052020-03-10 09:41:45 +01001571 /* 1st ROOT - 2nd ROOT, 1st ELEM - 2nd ELEM, 1st TEXT - 2nd TEXT, 1st META - =pos= - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001572 /* check for duplicates */
1573 if (item1->node == item2->node) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001574 assert((item1->type == item2->type) && ((item1->type != LYXP_NODE_META) || (meta_pos1 == meta_pos2)));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001575 return 0;
1576 }
1577
Michal Vasko9f96a052020-03-10 09:41:45 +01001578 /* 1st ELEM - 2nd TEXT, 1st ELEM - any pos - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001579 /* elem is always first, 2nd node is after it */
1580 if (item1->type == LYXP_NODE_ELEM) {
1581 assert(item2->type != LYXP_NODE_ELEM);
1582 return -1;
1583 }
1584
Michal Vasko9f96a052020-03-10 09:41:45 +01001585 /* 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 +02001586 /* 2nd is before 1st */
Michal Vasko69730152020-10-09 16:30:07 +02001587 if (((item1->type == LYXP_NODE_TEXT) &&
1588 ((item2->type == LYXP_NODE_ELEM) || (item2->type == LYXP_NODE_META))) ||
1589 ((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_ELEM)) ||
1590 (((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_META)) &&
1591 (meta_pos1 > meta_pos2))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001592 return 1;
1593 }
1594
Michal Vasko9f96a052020-03-10 09:41:45 +01001595 /* 1st META - any pos - 2nd TEXT, 1st META <pos< - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001596 /* 2nd is after 1st */
1597 return -1;
1598}
1599
1600/**
1601 * @brief Set cast for comparisons.
1602 *
1603 * @param[in] trg Target set to cast source into.
1604 * @param[in] src Source set.
1605 * @param[in] type Target set type.
1606 * @param[in] src_idx Source set node index.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001607 * @return LY_ERR
1608 */
1609static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001610set_comp_cast(struct lyxp_set *trg, struct lyxp_set *src, enum lyxp_set_type type, uint32_t src_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001611{
1612 assert(src->type == LYXP_SET_NODE_SET);
1613
1614 set_init(trg, src);
1615
1616 /* insert node into target set */
1617 set_insert_node(trg, src->val.nodes[src_idx].node, src->val.nodes[src_idx].pos, src->val.nodes[src_idx].type, 0);
1618
1619 /* cast target set appropriately */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001620 return lyxp_set_cast(trg, type);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001621}
1622
Michal Vasko4c7763f2020-07-27 17:40:37 +02001623/**
1624 * @brief Set content canonization for comparisons.
1625 *
1626 * @param[in] trg Target set to put the canononized source into.
1627 * @param[in] src Source set.
1628 * @param[in] xp_node Source XPath node/meta to use for canonization.
1629 * @return LY_ERR
1630 */
1631static LY_ERR
1632set_comp_canonize(struct lyxp_set *trg, const struct lyxp_set *src, const struct lyxp_set_node *xp_node)
1633{
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001634 const struct lysc_type *type = NULL;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001635 struct lyd_value val;
1636 struct ly_err_item *err = NULL;
1637 char *str, *ptr;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001638 LY_ERR rc;
1639
1640 /* is there anything to canonize even? */
1641 if ((src->type == LYXP_SET_NUMBER) || (src->type == LYXP_SET_STRING)) {
1642 /* do we have a type to use for canonization? */
1643 if ((xp_node->type == LYXP_NODE_ELEM) && (xp_node->node->schema->nodetype & LYD_NODE_TERM)) {
1644 type = ((struct lyd_node_term *)xp_node->node)->value.realtype;
1645 } else if (xp_node->type == LYXP_NODE_META) {
1646 type = ((struct lyd_meta *)xp_node->node)->value.realtype;
1647 }
1648 }
1649 if (!type) {
1650 goto fill;
1651 }
1652
1653 if (src->type == LYXP_SET_NUMBER) {
1654 /* canonize number */
1655 if (asprintf(&str, "%Lf", src->val.num) == -1) {
1656 LOGMEM(src->ctx);
1657 return LY_EMEM;
1658 }
1659 } else {
1660 /* canonize string */
1661 str = strdup(src->val.str);
1662 }
1663
1664 /* ignore errors, the value may not satisfy schema constraints */
Radek Krejci0b013302021-03-29 15:22:32 +02001665 rc = type->plugin->store(src->ctx, type, str, strlen(str), LYPLG_TYPE_STORE_DYNAMIC, src->format, src->prefix_data,
Michal Vasko405cc9e2020-12-01 12:01:27 +01001666 LYD_HINT_DATA, xp_node->node->schema, &val, NULL, &err);
Michal Vasko4c7763f2020-07-27 17:40:37 +02001667 ly_err_free(err);
1668 if (rc) {
1669 /* invalid value */
Radek Iša48460222021-03-02 15:18:32 +01001670 /* function store automaticaly dealloc value when fail */
Michal Vasko4c7763f2020-07-27 17:40:37 +02001671 goto fill;
1672 }
1673
Michal Vasko4c7763f2020-07-27 17:40:37 +02001674 /* use the canonized value */
1675 set_init(trg, src);
1676 trg->type = src->type;
1677 if (src->type == LYXP_SET_NUMBER) {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001678 trg->val.num = strtold(type->plugin->print(src->ctx, &val, LY_VALUE_CANON, NULL, NULL, NULL), &ptr);
Michal Vasko4c7763f2020-07-27 17:40:37 +02001679 LY_CHECK_ERR_RET(ptr[0], LOGINT(src->ctx), LY_EINT);
1680 } else {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001681 trg->val.str = strdup(type->plugin->print(src->ctx, &val, LY_VALUE_CANON, NULL, NULL, NULL));
Michal Vasko4c7763f2020-07-27 17:40:37 +02001682 }
1683 type->plugin->free(src->ctx, &val);
1684 return LY_SUCCESS;
1685
1686fill:
1687 /* no canonization needed/possible */
1688 set_fill_set(trg, src);
1689 return LY_SUCCESS;
1690}
1691
Michal Vasko03ff5a72019-09-11 13:49:33 +02001692/**
1693 * @brief Bubble sort @p set into XPath document order.
Michal Vasko49fec8e2022-05-24 10:28:33 +02001694 * Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001695 *
1696 * @param[in] set Set to sort.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001697 * @return How many times the whole set was traversed - 1 (if set was sorted, returns 0).
1698 */
1699static int
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001700set_sort(struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001701{
1702 uint32_t i, j;
Radek Krejci1deb5be2020-08-26 16:43:36 +02001703 int ret = 0, cmp;
Radek Krejci857189e2020-09-01 13:26:36 +02001704 ly_bool inverted, change;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001705 const struct lyd_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001706 struct lyxp_set_node item;
1707 struct lyxp_set_hash_node hnode;
1708 uint64_t hash;
1709
Michal Vasko3cf8fbf2020-05-27 15:21:21 +02001710 if ((set->type != LYXP_SET_NODE_SET) || (set->used < 2)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001711 return 0;
1712 }
1713
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001714 /* find first top-level node to be used as anchor for positions */
Michal Vasko4a7d4d62021-12-13 17:05:06 +01001715 for (root = set->tree; root->parent; root = lyd_parent(root)) {}
Michal Vaskod989ba02020-08-24 10:59:24 +02001716 for ( ; root->prev->next; root = root->prev) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001717
1718 /* fill positions */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001719 if (set_assign_pos(set, root, set->root_type)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001720 return -1;
1721 }
1722
Michal Vasko88a9e802022-05-24 10:50:28 +02001723#ifndef NDEBUG
Michal Vasko03ff5a72019-09-11 13:49:33 +02001724 LOGDBG(LY_LDGXPATH, "SORT BEGIN");
1725 print_set_debug(set);
Michal Vasko88a9e802022-05-24 10:50:28 +02001726#endif
Michal Vasko03ff5a72019-09-11 13:49:33 +02001727
1728 for (i = 0; i < set->used; ++i) {
1729 inverted = 0;
1730 change = 0;
1731
1732 for (j = 1; j < set->used - i; ++j) {
1733 /* compare node positions */
1734 if (inverted) {
1735 cmp = set_sort_compare(&set->val.nodes[j], &set->val.nodes[j - 1]);
1736 } else {
1737 cmp = set_sort_compare(&set->val.nodes[j - 1], &set->val.nodes[j]);
1738 }
1739
1740 /* swap if needed */
1741 if ((inverted && (cmp < 0)) || (!inverted && (cmp > 0))) {
1742 change = 1;
1743
1744 item = set->val.nodes[j - 1];
1745 set->val.nodes[j - 1] = set->val.nodes[j];
1746 set->val.nodes[j] = item;
1747 } else {
1748 /* whether node_pos1 should be smaller than node_pos2 or the other way around */
1749 inverted = !inverted;
1750 }
1751 }
1752
1753 ++ret;
1754
1755 if (!change) {
1756 break;
1757 }
1758 }
1759
Michal Vasko88a9e802022-05-24 10:50:28 +02001760#ifndef NDEBUG
Michal Vasko03ff5a72019-09-11 13:49:33 +02001761 LOGDBG(LY_LDGXPATH, "SORT END %d", ret);
1762 print_set_debug(set);
Michal Vasko88a9e802022-05-24 10:50:28 +02001763#endif
Michal Vasko03ff5a72019-09-11 13:49:33 +02001764
1765 /* check node hashes */
1766 if (set->used >= LYD_HT_MIN_ITEMS) {
1767 assert(set->ht);
1768 for (i = 0; i < set->used; ++i) {
1769 hnode.node = set->val.nodes[i].node;
1770 hnode.type = set->val.nodes[i].type;
1771
1772 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
1773 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
1774 hash = dict_hash_multi(hash, NULL, 0);
1775
1776 assert(!lyht_find(set->ht, &hnode, hash, NULL));
1777 }
1778 }
1779
1780 return ret - 1;
1781}
1782
Michal Vasko03ff5a72019-09-11 13:49:33 +02001783/**
1784 * @brief Merge 2 sorted sets into one.
1785 *
1786 * @param[in,out] trg Set to merge into. Duplicates are removed.
1787 * @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 +02001788 * @return LY_ERR
1789 */
1790static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001791set_sorted_merge(struct lyxp_set *trg, struct lyxp_set *src)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001792{
1793 uint32_t i, j, k, count, dup_count;
1794 int cmp;
1795 const struct lyd_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001796
Michal Vaskod3678892020-05-21 10:06:58 +02001797 if ((trg->type != LYXP_SET_NODE_SET) || (src->type != LYXP_SET_NODE_SET)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001798 return LY_EINVAL;
1799 }
1800
Michal Vaskod3678892020-05-21 10:06:58 +02001801 if (!src->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001802 return LY_SUCCESS;
Michal Vaskod3678892020-05-21 10:06:58 +02001803 } else if (!trg->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001804 set_fill_set(trg, src);
Michal Vaskod3678892020-05-21 10:06:58 +02001805 lyxp_set_free_content(src);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001806 return LY_SUCCESS;
1807 }
1808
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001809 /* find first top-level node to be used as anchor for positions */
Michal Vasko0143b682021-12-13 17:13:09 +01001810 for (root = trg->tree; root->parent; root = lyd_parent(root)) {}
Michal Vaskod989ba02020-08-24 10:59:24 +02001811 for ( ; root->prev->next; root = root->prev) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001812
1813 /* fill positions */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001814 if (set_assign_pos(trg, root, trg->root_type) || set_assign_pos(src, root, src->root_type)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001815 return LY_EINT;
1816 }
1817
1818#ifndef NDEBUG
1819 LOGDBG(LY_LDGXPATH, "MERGE target");
1820 print_set_debug(trg);
1821 LOGDBG(LY_LDGXPATH, "MERGE source");
1822 print_set_debug(src);
1823#endif
1824
1825 /* make memory for the merge (duplicates are not detected yet, so space
1826 * will likely be wasted on them, too bad) */
1827 if (trg->size - trg->used < src->used) {
1828 trg->size = trg->used + src->used;
1829
1830 trg->val.nodes = ly_realloc(trg->val.nodes, trg->size * sizeof *trg->val.nodes);
1831 LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx), LY_EMEM);
1832 }
1833
1834 i = 0;
1835 j = 0;
1836 count = 0;
1837 dup_count = 0;
1838 do {
1839 cmp = set_sort_compare(&src->val.nodes[i], &trg->val.nodes[j]);
1840 if (!cmp) {
1841 if (!count) {
1842 /* duplicate, just skip it */
1843 ++i;
1844 ++j;
1845 } else {
1846 /* we are copying something already, so let's copy the duplicate too,
1847 * we are hoping that afterwards there are some more nodes to
1848 * copy and this way we can copy them all together */
1849 ++count;
1850 ++dup_count;
1851 ++i;
1852 ++j;
1853 }
1854 } else if (cmp < 0) {
1855 /* inserting src node into trg, just remember it for now */
1856 ++count;
1857 ++i;
1858
1859 /* insert the hash now */
1860 set_insert_node_hash(trg, src->val.nodes[i - 1].node, src->val.nodes[i - 1].type);
1861 } else if (count) {
1862copy_nodes:
1863 /* time to actually copy the nodes, we have found the largest block of nodes */
1864 memmove(&trg->val.nodes[j + (count - dup_count)],
1865 &trg->val.nodes[j],
1866 (trg->used - j) * sizeof *trg->val.nodes);
1867 memcpy(&trg->val.nodes[j - dup_count], &src->val.nodes[i - count], count * sizeof *src->val.nodes);
1868
1869 trg->used += count - dup_count;
1870 /* do not change i, except the copying above, we are basically doing exactly what is in the else branch below */
1871 j += count - dup_count;
1872
1873 count = 0;
1874 dup_count = 0;
1875 } else {
1876 ++j;
1877 }
1878 } while ((i < src->used) && (j < trg->used));
1879
1880 if ((i < src->used) || count) {
1881 /* insert all the hashes first */
1882 for (k = i; k < src->used; ++k) {
1883 set_insert_node_hash(trg, src->val.nodes[k].node, src->val.nodes[k].type);
1884 }
1885
1886 /* loop ended, but we need to copy something at trg end */
1887 count += src->used - i;
1888 i = src->used;
1889 goto copy_nodes;
1890 }
1891
1892 /* we are inserting hashes before the actual node insert, which causes
1893 * situations when there were initially not enough items for a hash table,
1894 * but even after some were inserted, hash table was not created (during
1895 * insertion the number of items is not updated yet) */
1896 if (!trg->ht && (trg->used >= LYD_HT_MIN_ITEMS)) {
1897 set_insert_node_hash(trg, NULL, 0);
1898 }
1899
1900#ifndef NDEBUG
1901 LOGDBG(LY_LDGXPATH, "MERGE result");
1902 print_set_debug(trg);
1903#endif
1904
Michal Vaskod3678892020-05-21 10:06:58 +02001905 lyxp_set_free_content(src);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001906 return LY_SUCCESS;
1907}
1908
Michal Vasko14676352020-05-29 11:35:55 +02001909LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02001910lyxp_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 +02001911{
Michal Vasko004d3152020-06-11 19:59:22 +02001912 if (exp->used == tok_idx) {
Michal Vasko14676352020-05-29 11:35:55 +02001913 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001914 LOGVAL(ctx, LY_VCODE_XP_EOF);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001915 }
Michal Vasko14676352020-05-29 11:35:55 +02001916 return LY_EINCOMPLETE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001917 }
1918
Michal Vasko004d3152020-06-11 19:59:22 +02001919 if (want_tok && (exp->tokens[tok_idx] != want_tok)) {
Michal Vasko14676352020-05-29 11:35:55 +02001920 if (ctx) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02001921 LOGVAL(ctx, LY_VCODE_XP_INTOK2, lyxp_token2str(exp->tokens[tok_idx]),
1922 &exp->expr[exp->tok_pos[tok_idx]], lyxp_token2str(want_tok));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001923 }
Michal Vasko14676352020-05-29 11:35:55 +02001924 return LY_ENOT;
1925 }
1926
1927 return LY_SUCCESS;
1928}
1929
Michal Vasko004d3152020-06-11 19:59:22 +02001930LY_ERR
1931lyxp_next_token(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_token want_tok)
1932{
1933 LY_CHECK_RET(lyxp_check_token(ctx, exp, *tok_idx, want_tok));
1934
1935 /* skip the token */
1936 ++(*tok_idx);
1937
1938 return LY_SUCCESS;
1939}
1940
Michal Vasko14676352020-05-29 11:35:55 +02001941/* just like lyxp_check_token() but tests for 2 tokens */
1942static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02001943exp_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 +02001944 enum lyxp_token want_tok2)
Michal Vasko14676352020-05-29 11:35:55 +02001945{
Michal Vasko004d3152020-06-11 19:59:22 +02001946 if (exp->used == tok_idx) {
Michal Vasko14676352020-05-29 11:35:55 +02001947 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001948 LOGVAL(ctx, LY_VCODE_XP_EOF);
Michal Vasko14676352020-05-29 11:35:55 +02001949 }
1950 return LY_EINCOMPLETE;
1951 }
1952
Michal Vasko004d3152020-06-11 19:59:22 +02001953 if ((exp->tokens[tok_idx] != want_tok1) && (exp->tokens[tok_idx] != want_tok2)) {
Michal Vasko14676352020-05-29 11:35:55 +02001954 if (ctx) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02001955 LOGVAL(ctx, LY_VCODE_XP_INTOK, lyxp_token2str(exp->tokens[tok_idx]),
Michal Vasko0b468e62020-10-19 09:33:04 +02001956 &exp->expr[exp->tok_pos[tok_idx]]);
Michal Vasko14676352020-05-29 11:35:55 +02001957 }
1958 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001959 }
1960
1961 return LY_SUCCESS;
1962}
1963
Michal Vasko4911eeb2021-06-28 11:23:05 +02001964LY_ERR
1965lyxp_next_token2(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_token want_tok1,
1966 enum lyxp_token want_tok2)
1967{
1968 LY_CHECK_RET(exp_check_token2(ctx, exp, *tok_idx, want_tok1, want_tok2));
1969
1970 /* skip the token */
1971 ++(*tok_idx);
1972
1973 return LY_SUCCESS;
1974}
1975
Michal Vasko03ff5a72019-09-11 13:49:33 +02001976/**
1977 * @brief Stack operation push on the repeat array.
1978 *
1979 * @param[in] exp Expression to use.
Michal Vasko004d3152020-06-11 19:59:22 +02001980 * @param[in] tok_idx Position in the expresion \p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001981 * @param[in] repeat_op_idx Index from \p exp of the operator token. This value is pushed.
1982 */
1983static void
Michal Vasko004d3152020-06-11 19:59:22 +02001984exp_repeat_push(struct lyxp_expr *exp, uint16_t tok_idx, uint16_t repeat_op_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001985{
1986 uint16_t i;
1987
Michal Vasko004d3152020-06-11 19:59:22 +02001988 if (exp->repeat[tok_idx]) {
Radek Krejci1e008d22020-08-17 11:37:37 +02001989 for (i = 0; exp->repeat[tok_idx][i]; ++i) {}
Michal Vasko004d3152020-06-11 19:59:22 +02001990 exp->repeat[tok_idx] = realloc(exp->repeat[tok_idx], (i + 2) * sizeof *exp->repeat[tok_idx]);
1991 LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), );
1992 exp->repeat[tok_idx][i] = repeat_op_idx;
1993 exp->repeat[tok_idx][i + 1] = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001994 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02001995 exp->repeat[tok_idx] = calloc(2, sizeof *exp->repeat[tok_idx]);
1996 LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), );
1997 exp->repeat[tok_idx][0] = repeat_op_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001998 }
1999}
2000
2001/**
2002 * @brief Reparse Predicate. Logs directly on error.
2003 *
2004 * [7] Predicate ::= '[' Expr ']'
2005 *
2006 * @param[in] ctx Context for logging.
2007 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002008 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002009 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002010 * @return LY_ERR
2011 */
2012static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002013reparse_predicate(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t depth)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002014{
2015 LY_ERR rc;
2016
Michal Vasko004d3152020-06-11 19:59:22 +02002017 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002018 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002019 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002020
aPiecekbf968d92021-05-27 14:35:05 +02002021 rc = reparse_or_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002022 LY_CHECK_RET(rc);
2023
Michal Vasko004d3152020-06-11 19:59:22 +02002024 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002025 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002026 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002027
2028 return LY_SUCCESS;
2029}
2030
2031/**
2032 * @brief Reparse RelativeLocationPath. Logs directly on error.
2033 *
2034 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
2035 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
2036 * [6] NodeTest ::= NameTest | NodeType '(' ')'
2037 *
2038 * @param[in] ctx Context for logging.
2039 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002040 * @param[in] tok_idx Position in the expression \p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002041 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002042 * @return LY_ERR (LY_EINCOMPLETE on forward reference)
2043 */
2044static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002045reparse_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 +02002046{
2047 LY_ERR rc;
2048
Michal Vasko004d3152020-06-11 19:59:22 +02002049 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002050 LY_CHECK_RET(rc);
2051
2052 goto step;
2053 do {
2054 /* '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02002055 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002056
Michal Vasko004d3152020-06-11 19:59:22 +02002057 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002058 LY_CHECK_RET(rc);
2059step:
2060 /* Step */
Michal Vasko004d3152020-06-11 19:59:22 +02002061 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002062 case LYXP_TOKEN_DOT:
Michal Vasko004d3152020-06-11 19:59:22 +02002063 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002064 break;
2065
2066 case LYXP_TOKEN_DDOT:
Michal Vasko004d3152020-06-11 19:59:22 +02002067 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002068 break;
2069
Michal Vasko49fec8e2022-05-24 10:28:33 +02002070 case LYXP_TOKEN_AXISNAME:
2071 ++(*tok_idx);
2072
2073 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_DCOLON);
2074 LY_CHECK_RET(rc);
2075
2076 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02002077 case LYXP_TOKEN_AT:
Michal Vasko004d3152020-06-11 19:59:22 +02002078 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002079
Michal Vasko004d3152020-06-11 19:59:22 +02002080 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002081 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002082 if ((exp->tokens[*tok_idx] != LYXP_TOKEN_NAMETEST) && (exp->tokens[*tok_idx] != LYXP_TOKEN_NODETYPE)) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02002083 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 +02002084 return LY_EVALID;
2085 }
Michal Vasko49fec8e2022-05-24 10:28:33 +02002086 if (exp->tokens[*tok_idx] == LYXP_TOKEN_NODETYPE) {
2087 goto reparse_nodetype;
2088 }
Radek Krejci0f969882020-08-21 16:56:47 +02002089 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02002090 case LYXP_TOKEN_NAMETEST:
Michal Vasko004d3152020-06-11 19:59:22 +02002091 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002092 goto reparse_predicate;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002093
2094 case LYXP_TOKEN_NODETYPE:
Michal Vasko49fec8e2022-05-24 10:28:33 +02002095reparse_nodetype:
Michal Vasko004d3152020-06-11 19:59:22 +02002096 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002097
2098 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02002099 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002100 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002101 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002102
2103 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02002104 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002105 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002106 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002107
2108reparse_predicate:
2109 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002110 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
aPiecekbf968d92021-05-27 14:35:05 +02002111 rc = reparse_predicate(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002112 LY_CHECK_RET(rc);
2113 }
2114 break;
2115 default:
Michal Vasko49fec8e2022-05-24 10:28:33 +02002116 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 +02002117 return LY_EVALID;
2118 }
Michal Vasko004d3152020-06-11 19:59:22 +02002119 } while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02002120
2121 return LY_SUCCESS;
2122}
2123
2124/**
2125 * @brief Reparse AbsoluteLocationPath. Logs directly on error.
2126 *
2127 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
2128 *
2129 * @param[in] ctx Context for logging.
2130 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002131 * @param[in] tok_idx Position in the expression \p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002132 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002133 * @return LY_ERR
2134 */
2135static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002136reparse_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 +02002137{
2138 LY_ERR rc;
2139
Michal Vasko004d3152020-06-11 19:59:22 +02002140 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 +02002141
2142 /* '/' RelativeLocationPath? */
Michal Vasko004d3152020-06-11 19:59:22 +02002143 if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002144 /* '/' */
Michal Vasko004d3152020-06-11 19:59:22 +02002145 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002146
Michal Vasko004d3152020-06-11 19:59:22 +02002147 if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002148 return LY_SUCCESS;
2149 }
Michal Vasko004d3152020-06-11 19:59:22 +02002150 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002151 case LYXP_TOKEN_DOT:
2152 case LYXP_TOKEN_DDOT:
Michal Vasko49fec8e2022-05-24 10:28:33 +02002153 case LYXP_TOKEN_AXISNAME:
Michal Vasko03ff5a72019-09-11 13:49:33 +02002154 case LYXP_TOKEN_AT:
2155 case LYXP_TOKEN_NAMETEST:
2156 case LYXP_TOKEN_NODETYPE:
aPiecekbf968d92021-05-27 14:35:05 +02002157 rc = reparse_relative_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002158 LY_CHECK_RET(rc);
Radek Krejci0f969882020-08-21 16:56:47 +02002159 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02002160 default:
2161 break;
2162 }
2163
Michal Vasko03ff5a72019-09-11 13:49:33 +02002164 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02002165 /* '//' RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002166 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002167
aPiecekbf968d92021-05-27 14:35:05 +02002168 rc = reparse_relative_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002169 LY_CHECK_RET(rc);
2170 }
2171
2172 return LY_SUCCESS;
2173}
2174
2175/**
2176 * @brief Reparse FunctionCall. Logs directly on error.
2177 *
2178 * [9] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
2179 *
2180 * @param[in] ctx Context for logging.
2181 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002182 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002183 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002184 * @return LY_ERR
2185 */
2186static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002187reparse_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 +02002188{
Radek Krejci1deb5be2020-08-26 16:43:36 +02002189 int8_t min_arg_count = -1;
2190 uint32_t arg_count, max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002191 uint16_t func_tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002192 LY_ERR rc;
2193
Michal Vasko004d3152020-06-11 19:59:22 +02002194 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_FUNCNAME);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002195 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002196 func_tok_idx = *tok_idx;
2197 switch (exp->tok_len[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002198 case 3:
Michal Vasko004d3152020-06-11 19:59:22 +02002199 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002200 min_arg_count = 1;
2201 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002202 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002203 min_arg_count = 1;
2204 max_arg_count = 1;
2205 }
2206 break;
2207 case 4:
Michal Vasko004d3152020-06-11 19:59:22 +02002208 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002209 min_arg_count = 1;
2210 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002211 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002212 min_arg_count = 0;
2213 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002214 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002215 min_arg_count = 0;
2216 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002217 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002218 min_arg_count = 0;
2219 max_arg_count = 0;
2220 }
2221 break;
2222 case 5:
Michal Vasko004d3152020-06-11 19:59:22 +02002223 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002224 min_arg_count = 1;
2225 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002226 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002227 min_arg_count = 0;
2228 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002229 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002230 min_arg_count = 1;
2231 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002232 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002233 min_arg_count = 1;
2234 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002235 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002236 min_arg_count = 1;
2237 max_arg_count = 1;
2238 }
2239 break;
2240 case 6:
Michal Vasko004d3152020-06-11 19:59:22 +02002241 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002242 min_arg_count = 2;
Radek Krejci1deb5be2020-08-26 16:43:36 +02002243 max_arg_count = UINT32_MAX;
Michal Vasko004d3152020-06-11 19:59:22 +02002244 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002245 min_arg_count = 0;
2246 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002247 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002248 min_arg_count = 0;
2249 max_arg_count = 1;
2250 }
2251 break;
2252 case 7:
Michal Vasko004d3152020-06-11 19:59:22 +02002253 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002254 min_arg_count = 1;
2255 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002256 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002257 min_arg_count = 1;
2258 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002259 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002260 min_arg_count = 0;
2261 max_arg_count = 0;
2262 }
2263 break;
2264 case 8:
Michal Vasko004d3152020-06-11 19:59:22 +02002265 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002266 min_arg_count = 2;
2267 max_arg_count = 2;
Michal Vasko004d3152020-06-11 19:59:22 +02002268 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002269 min_arg_count = 0;
2270 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002271 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "re-match", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002272 min_arg_count = 2;
2273 max_arg_count = 2;
2274 }
2275 break;
2276 case 9:
Michal Vasko004d3152020-06-11 19:59:22 +02002277 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002278 min_arg_count = 2;
2279 max_arg_count = 3;
Michal Vasko004d3152020-06-11 19:59:22 +02002280 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002281 min_arg_count = 3;
2282 max_arg_count = 3;
2283 }
2284 break;
2285 case 10:
Michal Vasko004d3152020-06-11 19:59:22 +02002286 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002287 min_arg_count = 0;
2288 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002289 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002290 min_arg_count = 1;
2291 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002292 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002293 min_arg_count = 2;
2294 max_arg_count = 2;
2295 }
2296 break;
2297 case 11:
Michal Vasko004d3152020-06-11 19:59:22 +02002298 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002299 min_arg_count = 2;
2300 max_arg_count = 2;
2301 }
2302 break;
2303 case 12:
Michal Vasko004d3152020-06-11 19:59:22 +02002304 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002305 min_arg_count = 2;
2306 max_arg_count = 2;
2307 }
2308 break;
2309 case 13:
Michal Vasko004d3152020-06-11 19:59:22 +02002310 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002311 min_arg_count = 0;
2312 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002313 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string-length", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002314 min_arg_count = 0;
2315 max_arg_count = 1;
2316 }
2317 break;
2318 case 15:
Michal Vasko004d3152020-06-11 19:59:22 +02002319 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002320 min_arg_count = 0;
2321 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002322 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-after", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002323 min_arg_count = 2;
2324 max_arg_count = 2;
2325 }
2326 break;
2327 case 16:
Michal Vasko004d3152020-06-11 19:59:22 +02002328 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002329 min_arg_count = 2;
2330 max_arg_count = 2;
2331 }
2332 break;
2333 case 20:
Michal Vasko004d3152020-06-11 19:59:22 +02002334 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from-or-self", 20)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002335 min_arg_count = 2;
2336 max_arg_count = 2;
2337 }
2338 break;
2339 }
2340 if (min_arg_count == -1) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002341 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 +02002342 return LY_EINVAL;
2343 }
Michal Vasko004d3152020-06-11 19:59:22 +02002344 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002345
2346 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02002347 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002348 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002349 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002350
2351 /* ( Expr ( ',' Expr )* )? */
2352 arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002353 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002354 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002355 if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002356 ++arg_count;
aPiecekbf968d92021-05-27 14:35:05 +02002357 rc = reparse_or_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002358 LY_CHECK_RET(rc);
2359 }
Michal Vasko004d3152020-06-11 19:59:22 +02002360 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) {
2361 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002362
2363 ++arg_count;
aPiecekbf968d92021-05-27 14:35:05 +02002364 rc = reparse_or_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002365 LY_CHECK_RET(rc);
2366 }
2367
2368 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02002369 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002370 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002371 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002372
Radek Krejci857189e2020-09-01 13:26:36 +02002373 if ((arg_count < (uint32_t)min_arg_count) || (arg_count > max_arg_count)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002374 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 +02002375 return LY_EVALID;
2376 }
2377
2378 return LY_SUCCESS;
2379}
2380
2381/**
2382 * @brief Reparse PathExpr. Logs directly on error.
2383 *
2384 * [10] PathExpr ::= LocationPath | PrimaryExpr Predicate*
2385 * | PrimaryExpr Predicate* '/' RelativeLocationPath
2386 * | PrimaryExpr Predicate* '//' RelativeLocationPath
2387 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
aPiecekfba75362021-10-07 12:39:48 +02002388 * [8] PrimaryExpr ::= VariableReference | '(' Expr ')' | Literal | Number | FunctionCall
Michal Vasko03ff5a72019-09-11 13:49:33 +02002389 *
2390 * @param[in] ctx Context for logging.
2391 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002392 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002393 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002394 * @return LY_ERR
2395 */
2396static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002397reparse_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 +02002398{
2399 LY_ERR rc;
2400
Michal Vasko004d3152020-06-11 19:59:22 +02002401 if (lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko14676352020-05-29 11:35:55 +02002402 return LY_EVALID;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002403 }
2404
Michal Vasko004d3152020-06-11 19:59:22 +02002405 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002406 case LYXP_TOKEN_PAR1:
2407 /* '(' Expr ')' Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002408 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002409
aPiecekbf968d92021-05-27 14:35:05 +02002410 rc = reparse_or_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002411 LY_CHECK_RET(rc);
2412
Michal Vasko004d3152020-06-11 19:59:22 +02002413 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002414 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002415 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002416 goto predicate;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002417 case LYXP_TOKEN_DOT:
2418 case LYXP_TOKEN_DDOT:
Michal Vasko49fec8e2022-05-24 10:28:33 +02002419 case LYXP_TOKEN_AXISNAME:
Michal Vasko03ff5a72019-09-11 13:49:33 +02002420 case LYXP_TOKEN_AT:
2421 case LYXP_TOKEN_NAMETEST:
2422 case LYXP_TOKEN_NODETYPE:
2423 /* RelativeLocationPath */
aPiecekbf968d92021-05-27 14:35:05 +02002424 rc = reparse_relative_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002425 LY_CHECK_RET(rc);
2426 break;
aPiecekfba75362021-10-07 12:39:48 +02002427 case LYXP_TOKEN_VARREF:
2428 /* VariableReference */
2429 ++(*tok_idx);
2430 goto predicate;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002431 case LYXP_TOKEN_FUNCNAME:
2432 /* FunctionCall */
aPiecekbf968d92021-05-27 14:35:05 +02002433 rc = reparse_function_call(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002434 LY_CHECK_RET(rc);
2435 goto predicate;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002436 case LYXP_TOKEN_OPER_PATH:
2437 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +02002438 /* AbsoluteLocationPath */
aPiecekbf968d92021-05-27 14:35:05 +02002439 rc = reparse_absolute_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002440 LY_CHECK_RET(rc);
2441 break;
2442 case LYXP_TOKEN_LITERAL:
2443 /* Literal */
Michal Vasko004d3152020-06-11 19:59:22 +02002444 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002445 goto predicate;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002446 case LYXP_TOKEN_NUMBER:
2447 /* Number */
Michal Vasko004d3152020-06-11 19:59:22 +02002448 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002449 goto predicate;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002450 default:
Michal Vasko49fec8e2022-05-24 10:28:33 +02002451 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 +02002452 return LY_EVALID;
2453 }
2454
2455 return LY_SUCCESS;
2456
2457predicate:
2458 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002459 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
aPiecekbf968d92021-05-27 14:35:05 +02002460 rc = reparse_predicate(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002461 LY_CHECK_RET(rc);
2462 }
2463
2464 /* ('/' or '//') RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002465 if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002466
2467 /* '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02002468 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002469
aPiecekbf968d92021-05-27 14:35:05 +02002470 rc = reparse_relative_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002471 LY_CHECK_RET(rc);
2472 }
2473
2474 return LY_SUCCESS;
2475}
2476
2477/**
2478 * @brief Reparse UnaryExpr. Logs directly on error.
2479 *
2480 * [17] UnaryExpr ::= UnionExpr | '-' UnaryExpr
2481 * [18] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
2482 *
2483 * @param[in] ctx Context for logging.
2484 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002485 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002486 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002487 * @return LY_ERR
2488 */
2489static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002490reparse_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 +02002491{
2492 uint16_t prev_exp;
2493 LY_ERR rc;
2494
2495 /* ('-')* */
Michal Vasko004d3152020-06-11 19:59:22 +02002496 prev_exp = *tok_idx;
Michal Vasko69730152020-10-09 16:30:07 +02002497 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2498 (exp->expr[exp->tok_pos[*tok_idx]] == '-')) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002499 exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNARY);
Michal Vasko004d3152020-06-11 19:59:22 +02002500 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002501 }
2502
2503 /* PathExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002504 prev_exp = *tok_idx;
aPiecekbf968d92021-05-27 14:35:05 +02002505 rc = reparse_path_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002506 LY_CHECK_RET(rc);
2507
2508 /* ('|' PathExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002509 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_UNI)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002510 exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNION);
Michal Vasko004d3152020-06-11 19:59:22 +02002511 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002512
aPiecekbf968d92021-05-27 14:35:05 +02002513 rc = reparse_path_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002514 LY_CHECK_RET(rc);
2515 }
2516
2517 return LY_SUCCESS;
2518}
2519
2520/**
2521 * @brief Reparse AdditiveExpr. Logs directly on error.
2522 *
2523 * [15] AdditiveExpr ::= MultiplicativeExpr
2524 * | AdditiveExpr '+' MultiplicativeExpr
2525 * | AdditiveExpr '-' MultiplicativeExpr
2526 * [16] MultiplicativeExpr ::= UnaryExpr
2527 * | MultiplicativeExpr '*' UnaryExpr
2528 * | MultiplicativeExpr 'div' UnaryExpr
2529 * | MultiplicativeExpr 'mod' UnaryExpr
2530 *
2531 * @param[in] ctx Context for logging.
2532 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002533 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002534 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002535 * @return LY_ERR
2536 */
2537static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002538reparse_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 +02002539{
2540 uint16_t prev_add_exp, prev_mul_exp;
2541 LY_ERR rc;
2542
Michal Vasko004d3152020-06-11 19:59:22 +02002543 prev_add_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002544 goto reparse_multiplicative_expr;
2545
2546 /* ('+' / '-' MultiplicativeExpr)* */
Michal Vasko69730152020-10-09 16:30:07 +02002547 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2548 ((exp->expr[exp->tok_pos[*tok_idx]] == '+') || (exp->expr[exp->tok_pos[*tok_idx]] == '-'))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002549 exp_repeat_push(exp, prev_add_exp, LYXP_EXPR_ADDITIVE);
Michal Vasko004d3152020-06-11 19:59:22 +02002550 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002551
2552reparse_multiplicative_expr:
2553 /* UnaryExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002554 prev_mul_exp = *tok_idx;
aPiecekbf968d92021-05-27 14:35:05 +02002555 rc = reparse_unary_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002556 LY_CHECK_RET(rc);
2557
2558 /* ('*' / 'div' / 'mod' UnaryExpr)* */
Michal Vasko69730152020-10-09 16:30:07 +02002559 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2560 ((exp->expr[exp->tok_pos[*tok_idx]] == '*') || (exp->tok_len[*tok_idx] == 3))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002561 exp_repeat_push(exp, prev_mul_exp, LYXP_EXPR_MULTIPLICATIVE);
Michal Vasko004d3152020-06-11 19:59:22 +02002562 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002563
aPiecekbf968d92021-05-27 14:35:05 +02002564 rc = reparse_unary_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002565 LY_CHECK_RET(rc);
2566 }
2567 }
2568
2569 return LY_SUCCESS;
2570}
2571
2572/**
2573 * @brief Reparse EqualityExpr. Logs directly on error.
2574 *
2575 * [13] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
2576 * | EqualityExpr '!=' RelationalExpr
2577 * [14] RelationalExpr ::= AdditiveExpr
2578 * | RelationalExpr '<' AdditiveExpr
2579 * | RelationalExpr '>' AdditiveExpr
2580 * | RelationalExpr '<=' AdditiveExpr
2581 * | RelationalExpr '>=' AdditiveExpr
2582 *
2583 * @param[in] ctx Context for logging.
2584 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002585 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002586 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002587 * @return LY_ERR
2588 */
2589static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002590reparse_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 +02002591{
2592 uint16_t prev_eq_exp, prev_rel_exp;
2593 LY_ERR rc;
2594
Michal Vasko004d3152020-06-11 19:59:22 +02002595 prev_eq_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002596 goto reparse_additive_expr;
2597
2598 /* ('=' / '!=' RelationalExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002599 while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_EQUAL, LYXP_TOKEN_OPER_NEQUAL)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002600 exp_repeat_push(exp, prev_eq_exp, LYXP_EXPR_EQUALITY);
Michal Vasko004d3152020-06-11 19:59:22 +02002601 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002602
2603reparse_additive_expr:
2604 /* AdditiveExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002605 prev_rel_exp = *tok_idx;
aPiecekbf968d92021-05-27 14:35:05 +02002606 rc = reparse_additive_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002607 LY_CHECK_RET(rc);
2608
2609 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002610 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_COMP)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002611 exp_repeat_push(exp, prev_rel_exp, LYXP_EXPR_RELATIONAL);
Michal Vasko004d3152020-06-11 19:59:22 +02002612 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002613
aPiecekbf968d92021-05-27 14:35:05 +02002614 rc = reparse_additive_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002615 LY_CHECK_RET(rc);
2616 }
2617 }
2618
2619 return LY_SUCCESS;
2620}
2621
2622/**
2623 * @brief Reparse OrExpr. Logs directly on error.
2624 *
2625 * [11] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
2626 * [12] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
2627 *
2628 * @param[in] ctx Context for logging.
2629 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002630 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002631 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002632 * @return LY_ERR
2633 */
2634static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002635reparse_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 +02002636{
2637 uint16_t prev_or_exp, prev_and_exp;
2638 LY_ERR rc;
2639
aPiecekbf968d92021-05-27 14:35:05 +02002640 ++depth;
2641 LY_CHECK_ERR_RET(depth > LYXP_MAX_BLOCK_DEPTH, LOGVAL(ctx, LY_VCODE_XP_DEPTH), LY_EINVAL);
2642
Michal Vasko004d3152020-06-11 19:59:22 +02002643 prev_or_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002644 goto reparse_equality_expr;
2645
2646 /* ('or' AndExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002647 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 +02002648 exp_repeat_push(exp, prev_or_exp, LYXP_EXPR_OR);
Michal Vasko004d3152020-06-11 19:59:22 +02002649 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002650
2651reparse_equality_expr:
2652 /* EqualityExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002653 prev_and_exp = *tok_idx;
aPiecekbf968d92021-05-27 14:35:05 +02002654 rc = reparse_equality_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002655 LY_CHECK_RET(rc);
2656
2657 /* ('and' EqualityExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002658 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 +02002659 exp_repeat_push(exp, prev_and_exp, LYXP_EXPR_AND);
Michal Vasko004d3152020-06-11 19:59:22 +02002660 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002661
aPiecekbf968d92021-05-27 14:35:05 +02002662 rc = reparse_equality_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002663 LY_CHECK_RET(rc);
2664 }
2665 }
2666
2667 return LY_SUCCESS;
2668}
Radek Krejcib1646a92018-11-02 16:08:26 +01002669
2670/**
2671 * @brief Parse NCName.
2672 *
2673 * @param[in] ncname Name to parse.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002674 * @return Length of @p ncname valid bytes.
Radek Krejcib1646a92018-11-02 16:08:26 +01002675 */
Michal Vasko49fec8e2022-05-24 10:28:33 +02002676static ssize_t
Radek Krejcib1646a92018-11-02 16:08:26 +01002677parse_ncname(const char *ncname)
2678{
Radek Krejci1deb5be2020-08-26 16:43:36 +02002679 uint32_t uc;
Radek Krejcid4270262019-01-07 15:07:25 +01002680 size_t size;
Michal Vasko49fec8e2022-05-24 10:28:33 +02002681 ssize_t len = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01002682
2683 LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), 0);
2684 if (!is_xmlqnamestartchar(uc) || (uc == ':')) {
2685 return len;
2686 }
2687
2688 do {
2689 len += size;
Radek Krejci9a564c92019-01-07 14:53:57 +01002690 if (!*ncname) {
2691 break;
2692 }
Radek Krejcid4270262019-01-07 15:07:25 +01002693 LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), -len);
Radek Krejcib1646a92018-11-02 16:08:26 +01002694 } while (is_xmlqnamechar(uc) && (uc != ':'));
2695
2696 return len;
2697}
2698
2699/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02002700 * @brief Add @p token into the expression @p exp.
Radek Krejcib1646a92018-11-02 16:08:26 +01002701 *
Michal Vasko03ff5a72019-09-11 13:49:33 +02002702 * @param[in] ctx Context for logging.
Radek Krejcib1646a92018-11-02 16:08:26 +01002703 * @param[in] exp Expression to use.
2704 * @param[in] token Token to add.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002705 * @param[in] tok_pos Token position in the XPath expression.
Radek Krejcib1646a92018-11-02 16:08:26 +01002706 * @param[in] tok_len Token length in the XPath expression.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002707 * @return LY_ERR
Radek Krejcib1646a92018-11-02 16:08:26 +01002708 */
2709static LY_ERR
Michal Vasko14676352020-05-29 11:35:55 +02002710exp_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 +01002711{
2712 uint32_t prev;
2713
2714 if (exp->used == exp->size) {
2715 prev = exp->size;
2716 exp->size += LYXP_EXPR_SIZE_STEP;
2717 if (prev > exp->size) {
2718 LOGINT(ctx);
2719 return LY_EINT;
2720 }
2721
2722 exp->tokens = ly_realloc(exp->tokens, exp->size * sizeof *exp->tokens);
2723 LY_CHECK_ERR_RET(!exp->tokens, LOGMEM(ctx), LY_EMEM);
2724 exp->tok_pos = ly_realloc(exp->tok_pos, exp->size * sizeof *exp->tok_pos);
2725 LY_CHECK_ERR_RET(!exp->tok_pos, LOGMEM(ctx), LY_EMEM);
2726 exp->tok_len = ly_realloc(exp->tok_len, exp->size * sizeof *exp->tok_len);
2727 LY_CHECK_ERR_RET(!exp->tok_len, LOGMEM(ctx), LY_EMEM);
2728 }
2729
2730 exp->tokens[exp->used] = token;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002731 exp->tok_pos[exp->used] = tok_pos;
Radek Krejcib1646a92018-11-02 16:08:26 +01002732 exp->tok_len[exp->used] = tok_len;
2733 ++exp->used;
2734 return LY_SUCCESS;
2735}
2736
2737void
Michal Vasko14676352020-05-29 11:35:55 +02002738lyxp_expr_free(const struct ly_ctx *ctx, struct lyxp_expr *expr)
Radek Krejcib1646a92018-11-02 16:08:26 +01002739{
2740 uint16_t i;
2741
2742 if (!expr) {
2743 return;
2744 }
2745
2746 lydict_remove(ctx, expr->expr);
2747 free(expr->tokens);
2748 free(expr->tok_pos);
2749 free(expr->tok_len);
2750 if (expr->repeat) {
2751 for (i = 0; i < expr->used; ++i) {
2752 free(expr->repeat[i]);
2753 }
2754 }
2755 free(expr->repeat);
2756 free(expr);
2757}
2758
Michal Vasko49fec8e2022-05-24 10:28:33 +02002759/**
2760 * @brief Parse Axis name.
2761 *
2762 * @param[in] str String to parse.
2763 * @param[in] str_len Length of @p str.
2764 * @return LY_SUCCESS if an axis.
2765 * @return LY_ENOT otherwise.
2766 */
2767static LY_ERR
2768expr_parse_axis(const char *str, size_t str_len)
2769{
2770 switch (str_len) {
2771 case 4:
2772 if (!strncmp("self", str, str_len)) {
2773 return LY_SUCCESS;
2774 }
2775 break;
2776 case 5:
2777 if (!strncmp("child", str, str_len)) {
2778 return LY_SUCCESS;
2779 }
2780 break;
2781 case 6:
2782 if (!strncmp("parent", str, str_len)) {
2783 return LY_SUCCESS;
2784 }
2785 break;
2786 case 8:
2787 if (!strncmp("ancestor", str, str_len)) {
2788 return LY_SUCCESS;
2789 }
2790 break;
2791 case 9:
2792 if (!strncmp("attribute", str, str_len)) {
2793 return LY_SUCCESS;
2794 } else if (!strncmp("following", str, str_len)) {
2795 return LY_SUCCESS;
2796 } else if (!strncmp("namespace", str, str_len)) {
2797 LOGERR(NULL, LY_EINVAL, "Axis \"namespace\" not supported.");
2798 return LY_ENOT;
2799 } else if (!strncmp("preceding", str, str_len)) {
2800 return LY_SUCCESS;
2801 }
2802 break;
2803 case 10:
2804 if (!strncmp("descendant", str, str_len)) {
2805 return LY_SUCCESS;
2806 }
2807 break;
2808 case 16:
2809 if (!strncmp("ancestor-or-self", str, str_len)) {
2810 return LY_SUCCESS;
2811 }
2812 break;
2813 case 17:
2814 if (!strncmp("following-sibling", str, str_len)) {
2815 return LY_SUCCESS;
2816 } else if (!strncmp("preceding-sibling", str, str_len)) {
2817 return LY_SUCCESS;
2818 }
2819 break;
2820 case 18:
2821 if (!strncmp("descendant-or-self", str, str_len)) {
2822 return LY_SUCCESS;
2823 }
2824 break;
2825 }
2826
2827 return LY_ENOT;
2828}
2829
Radek Krejcif03a9e22020-09-18 20:09:31 +02002830LY_ERR
2831lyxp_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 +01002832{
Radek Krejcif03a9e22020-09-18 20:09:31 +02002833 LY_ERR ret = LY_SUCCESS;
2834 struct lyxp_expr *expr;
Radek Krejcid4270262019-01-07 15:07:25 +01002835 size_t parsed = 0, tok_len;
Radek Krejcib1646a92018-11-02 16:08:26 +01002836 enum lyxp_token tok_type;
Michal Vasko49fec8e2022-05-24 10:28:33 +02002837 ly_bool prev_func_check = 0, prev_ntype_check = 0, has_axis;
Michal Vasko004d3152020-06-11 19:59:22 +02002838 uint16_t tok_idx = 0;
Michal Vasko49fec8e2022-05-24 10:28:33 +02002839 ssize_t ncname_len;
Radek Krejcib1646a92018-11-02 16:08:26 +01002840
Radek Krejcif03a9e22020-09-18 20:09:31 +02002841 assert(expr_p);
2842
2843 if (!expr_str[0]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002844 LOGVAL(ctx, LY_VCODE_XP_EOF);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002845 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02002846 }
2847
2848 if (!expr_len) {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002849 expr_len = strlen(expr_str);
Michal Vasko004d3152020-06-11 19:59:22 +02002850 }
2851 if (expr_len > UINT16_MAX) {
Michal Vasko623ac7b2021-04-09 12:44:23 +02002852 LOGVAL(ctx, LYVE_XPATH, "XPath expression cannot be longer than %u characters.", UINT16_MAX);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002853 return LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01002854 }
2855
2856 /* init lyxp_expr structure */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002857 expr = calloc(1, sizeof *expr);
2858 LY_CHECK_ERR_GOTO(!expr, LOGMEM(ctx); ret = LY_EMEM, error);
2859 LY_CHECK_GOTO(ret = lydict_insert(ctx, expr_str, expr_len, &expr->expr), error);
2860 expr->used = 0;
2861 expr->size = LYXP_EXPR_SIZE_START;
2862 expr->tokens = malloc(expr->size * sizeof *expr->tokens);
2863 LY_CHECK_ERR_GOTO(!expr->tokens, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002864
Radek Krejcif03a9e22020-09-18 20:09:31 +02002865 expr->tok_pos = malloc(expr->size * sizeof *expr->tok_pos);
2866 LY_CHECK_ERR_GOTO(!expr->tok_pos, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002867
Radek Krejcif03a9e22020-09-18 20:09:31 +02002868 expr->tok_len = malloc(expr->size * sizeof *expr->tok_len);
2869 LY_CHECK_ERR_GOTO(!expr->tok_len, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002870
Michal Vasko004d3152020-06-11 19:59:22 +02002871 /* make expr 0-terminated */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002872 expr_str = expr->expr;
Michal Vasko004d3152020-06-11 19:59:22 +02002873
Radek Krejcif03a9e22020-09-18 20:09:31 +02002874 while (is_xmlws(expr_str[parsed])) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002875 ++parsed;
2876 }
2877
2878 do {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002879 if (expr_str[parsed] == '(') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002880
2881 /* '(' */
2882 tok_len = 1;
2883 tok_type = LYXP_TOKEN_PAR1;
2884
Michal Vasko49fec8e2022-05-24 10:28:33 +02002885 if (prev_ntype_check && expr->used && (expr->tokens[expr->used - 1] == LYXP_TOKEN_NAMETEST) &&
2886 (((expr->tok_len[expr->used - 1] == 4) &&
2887 (!strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "node", 4) ||
2888 !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "text", 4))) ||
2889 ((expr->tok_len[expr->used - 1] == 7) &&
2890 !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "comment", 7)))) {
2891 /* it is NodeType after all */
2892 expr->tokens[expr->used - 1] = LYXP_TOKEN_NODETYPE;
2893
2894 prev_ntype_check = 0;
2895 prev_func_check = 0;
2896 } else if (prev_func_check && expr->used && (expr->tokens[expr->used - 1] == LYXP_TOKEN_NAMETEST)) {
2897 /* it is FunctionName after all */
2898 expr->tokens[expr->used - 1] = LYXP_TOKEN_FUNCNAME;
2899
2900 prev_ntype_check = 0;
2901 prev_func_check = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01002902 }
2903
Radek Krejcif03a9e22020-09-18 20:09:31 +02002904 } else if (expr_str[parsed] == ')') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002905
2906 /* ')' */
2907 tok_len = 1;
2908 tok_type = LYXP_TOKEN_PAR2;
2909
Radek Krejcif03a9e22020-09-18 20:09:31 +02002910 } else if (expr_str[parsed] == '[') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002911
2912 /* '[' */
2913 tok_len = 1;
2914 tok_type = LYXP_TOKEN_BRACK1;
2915
Radek Krejcif03a9e22020-09-18 20:09:31 +02002916 } else if (expr_str[parsed] == ']') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002917
2918 /* ']' */
2919 tok_len = 1;
2920 tok_type = LYXP_TOKEN_BRACK2;
2921
Radek Krejcif03a9e22020-09-18 20:09:31 +02002922 } else if (!strncmp(&expr_str[parsed], "..", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002923
2924 /* '..' */
2925 tok_len = 2;
2926 tok_type = LYXP_TOKEN_DDOT;
2927
Radek Krejcif03a9e22020-09-18 20:09:31 +02002928 } else if ((expr_str[parsed] == '.') && (!isdigit(expr_str[parsed + 1]))) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002929
2930 /* '.' */
2931 tok_len = 1;
2932 tok_type = LYXP_TOKEN_DOT;
2933
Radek Krejcif03a9e22020-09-18 20:09:31 +02002934 } else if (expr_str[parsed] == '@') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002935
2936 /* '@' */
2937 tok_len = 1;
2938 tok_type = LYXP_TOKEN_AT;
2939
Radek Krejcif03a9e22020-09-18 20:09:31 +02002940 } else if (expr_str[parsed] == ',') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002941
2942 /* ',' */
2943 tok_len = 1;
2944 tok_type = LYXP_TOKEN_COMMA;
2945
Radek Krejcif03a9e22020-09-18 20:09:31 +02002946 } else if (expr_str[parsed] == '\'') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002947
2948 /* Literal with ' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002949 for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\''); ++tok_len) {}
2950 LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0',
Radek Krejci2efc45b2020-12-22 16:25:44 +01002951 LOGVAL(ctx, LY_VCODE_XP_EOE, expr_str[parsed], &expr_str[parsed]); ret = LY_EVALID,
Michal Vasko69730152020-10-09 16:30:07 +02002952 error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002953 ++tok_len;
2954 tok_type = LYXP_TOKEN_LITERAL;
2955
Radek Krejcif03a9e22020-09-18 20:09:31 +02002956 } else if (expr_str[parsed] == '\"') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002957
2958 /* Literal with " */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002959 for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\"'); ++tok_len) {}
2960 LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0',
Radek Krejci2efc45b2020-12-22 16:25:44 +01002961 LOGVAL(ctx, LY_VCODE_XP_EOE, expr_str[parsed], &expr_str[parsed]); ret = LY_EVALID,
Michal Vasko69730152020-10-09 16:30:07 +02002962 error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002963 ++tok_len;
2964 tok_type = LYXP_TOKEN_LITERAL;
2965
Radek Krejcif03a9e22020-09-18 20:09:31 +02002966 } else if ((expr_str[parsed] == '.') || (isdigit(expr_str[parsed]))) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002967
2968 /* Number */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002969 for (tok_len = 0; isdigit(expr_str[parsed + tok_len]); ++tok_len) {}
2970 if (expr_str[parsed + tok_len] == '.') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002971 ++tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02002972 for ( ; isdigit(expr_str[parsed + tok_len]); ++tok_len) {}
Radek Krejcib1646a92018-11-02 16:08:26 +01002973 }
2974 tok_type = LYXP_TOKEN_NUMBER;
2975
aPiecekfba75362021-10-07 12:39:48 +02002976 } else if (expr_str[parsed] == '$') {
2977
2978 /* VariableReference */
2979 parsed++;
Michal Vasko49fec8e2022-05-24 10:28:33 +02002980 ncname_len = parse_ncname(&expr_str[parsed]);
aPiecekfba75362021-10-07 12:39:48 +02002981 LY_CHECK_ERR_GOTO(ncname_len < 1, LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed - ncname_len],
2982 parsed - ncname_len + 1, expr_str); ret = LY_EVALID, error);
2983 tok_len = ncname_len;
2984 LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == ':',
2985 LOGVAL(ctx, LYVE_XPATH, "Variable with prefix is not supported."); ret = LY_EVALID,
2986 error);
2987 tok_type = LYXP_TOKEN_VARREF;
2988
Radek Krejcif03a9e22020-09-18 20:09:31 +02002989 } else if (expr_str[parsed] == '/') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002990
2991 /* Operator '/', '//' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002992 if (!strncmp(&expr_str[parsed], "//", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002993 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002994 tok_type = LYXP_TOKEN_OPER_RPATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002995 } else {
2996 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002997 tok_type = LYXP_TOKEN_OPER_PATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002998 }
Radek Krejcib1646a92018-11-02 16:08:26 +01002999
Radek Krejcif03a9e22020-09-18 20:09:31 +02003000 } else if (!strncmp(&expr_str[parsed], "!=", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01003001
Michal Vasko3e48bf32020-06-01 08:39:07 +02003002 /* Operator '!=' */
Radek Krejcib1646a92018-11-02 16:08:26 +01003003 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003004 tok_type = LYXP_TOKEN_OPER_NEQUAL;
3005
Radek Krejcif03a9e22020-09-18 20:09:31 +02003006 } else if (!strncmp(&expr_str[parsed], "<=", 2) || !strncmp(&expr_str[parsed], ">=", 2)) {
Michal Vasko3e48bf32020-06-01 08:39:07 +02003007
3008 /* Operator '<=', '>=' */
3009 tok_len = 2;
3010 tok_type = LYXP_TOKEN_OPER_COMP;
Radek Krejcib1646a92018-11-02 16:08:26 +01003011
Radek Krejcif03a9e22020-09-18 20:09:31 +02003012 } else if (expr_str[parsed] == '|') {
Radek Krejcib1646a92018-11-02 16:08:26 +01003013
3014 /* Operator '|' */
3015 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003016 tok_type = LYXP_TOKEN_OPER_UNI;
Radek Krejcib1646a92018-11-02 16:08:26 +01003017
Radek Krejcif03a9e22020-09-18 20:09:31 +02003018 } else if ((expr_str[parsed] == '+') || (expr_str[parsed] == '-')) {
Radek Krejcib1646a92018-11-02 16:08:26 +01003019
3020 /* Operator '+', '-' */
3021 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003022 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01003023
Radek Krejcif03a9e22020-09-18 20:09:31 +02003024 } else if (expr_str[parsed] == '=') {
Radek Krejcib1646a92018-11-02 16:08:26 +01003025
Michal Vasko3e48bf32020-06-01 08:39:07 +02003026 /* Operator '=' */
Radek Krejcib1646a92018-11-02 16:08:26 +01003027 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003028 tok_type = LYXP_TOKEN_OPER_EQUAL;
3029
Radek Krejcif03a9e22020-09-18 20:09:31 +02003030 } else if ((expr_str[parsed] == '<') || (expr_str[parsed] == '>')) {
Michal Vasko3e48bf32020-06-01 08:39:07 +02003031
3032 /* Operator '<', '>' */
3033 tok_len = 1;
3034 tok_type = LYXP_TOKEN_OPER_COMP;
Radek Krejcib1646a92018-11-02 16:08:26 +01003035
Michal Vasko69730152020-10-09 16:30:07 +02003036 } else if (expr->used && (expr->tokens[expr->used - 1] != LYXP_TOKEN_AT) &&
3037 (expr->tokens[expr->used - 1] != LYXP_TOKEN_PAR1) &&
3038 (expr->tokens[expr->used - 1] != LYXP_TOKEN_BRACK1) &&
3039 (expr->tokens[expr->used - 1] != LYXP_TOKEN_COMMA) &&
3040 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_LOG) &&
3041 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_EQUAL) &&
3042 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_NEQUAL) &&
3043 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_COMP) &&
3044 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_MATH) &&
3045 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_UNI) &&
3046 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_PATH) &&
3047 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_RPATH)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01003048
3049 /* Operator '*', 'or', 'and', 'mod', or 'div' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02003050 if (expr_str[parsed] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01003051 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003052 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01003053
Radek Krejcif03a9e22020-09-18 20:09:31 +02003054 } else if (!strncmp(&expr_str[parsed], "or", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01003055 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003056 tok_type = LYXP_TOKEN_OPER_LOG;
Radek Krejcib1646a92018-11-02 16:08:26 +01003057
Radek Krejcif03a9e22020-09-18 20:09:31 +02003058 } else if (!strncmp(&expr_str[parsed], "and", 3)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01003059 tok_len = 3;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003060 tok_type = LYXP_TOKEN_OPER_LOG;
Radek Krejcib1646a92018-11-02 16:08:26 +01003061
Radek Krejcif03a9e22020-09-18 20:09:31 +02003062 } else if (!strncmp(&expr_str[parsed], "mod", 3) || !strncmp(&expr_str[parsed], "div", 3)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01003063 tok_len = 3;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003064 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01003065
Michal Vasko49fec8e2022-05-24 10:28:33 +02003066 } else if (prev_ntype_check || prev_func_check) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003067 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 +02003068 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 +02003069 ret = LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01003070 goto error;
3071 } else {
Michal Vasko774ce402021-04-14 15:35:06 +02003072 LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed], parsed + 1, expr_str);
Radek Krejcif03a9e22020-09-18 20:09:31 +02003073 ret = LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01003074 goto error;
3075 }
Radek Krejcib1646a92018-11-02 16:08:26 +01003076 } else {
3077
Michal Vasko49fec8e2022-05-24 10:28:33 +02003078 /* (AxisName '::')? ((NCName ':')? '*' | QName) or NodeType/FunctionName */
3079 if (expr_str[parsed] == '*') {
3080 ncname_len = 1;
3081 } else {
3082 ncname_len = parse_ncname(&expr_str[parsed]);
3083 LY_CHECK_ERR_GOTO(ncname_len < 1, LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed - ncname_len],
3084 parsed - ncname_len + 1, expr_str); ret = LY_EVALID, error);
3085 }
Radek Krejcib1646a92018-11-02 16:08:26 +01003086 tok_len = ncname_len;
3087
Michal Vasko49fec8e2022-05-24 10:28:33 +02003088 has_axis = 0;
3089 if (!strncmp(&expr_str[parsed + tok_len], "::", 2)) {
3090 /* axis */
3091 LY_CHECK_ERR_GOTO(expr_parse_axis(&expr_str[parsed], ncname_len),
3092 LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed], parsed + 1, expr_str); ret = LY_EVALID, error);
3093 tok_type = LYXP_TOKEN_AXISNAME;
3094
3095 LY_CHECK_GOTO(ret = exp_add_token(ctx, expr, tok_type, parsed, tok_len), error);
3096 parsed += tok_len;
3097
3098 /* '::' */
3099 tok_len = 2;
3100 tok_type = LYXP_TOKEN_DCOLON;
3101
3102 LY_CHECK_GOTO(ret = exp_add_token(ctx, expr, tok_type, parsed, tok_len), error);
3103 parsed += tok_len;
3104
3105 if (expr_str[parsed] == '*') {
3106 ncname_len = 1;
3107 } else {
3108 ncname_len = parse_ncname(&expr_str[parsed]);
3109 LY_CHECK_ERR_GOTO(ncname_len < 1, LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed - ncname_len],
3110 parsed - ncname_len + 1, expr_str); ret = LY_EVALID, error);
3111 }
3112 tok_len = ncname_len;
3113
3114 has_axis = 1;
3115 }
3116
Radek Krejcif03a9e22020-09-18 20:09:31 +02003117 if (expr_str[parsed + tok_len] == ':') {
Radek Krejcib1646a92018-11-02 16:08:26 +01003118 ++tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02003119 if (expr_str[parsed + tok_len] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01003120 ++tok_len;
3121 } else {
Radek Krejcif03a9e22020-09-18 20:09:31 +02003122 ncname_len = parse_ncname(&expr_str[parsed + tok_len]);
Michal Vaskoe2be5462021-08-04 10:49:42 +02003123 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 +02003124 parsed - ncname_len + 1, expr_str); ret = LY_EVALID, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01003125 tok_len += ncname_len;
3126 }
Michal Vasko49fec8e2022-05-24 10:28:33 +02003127 /* remove old flags to prevent ambiguities */
3128 prev_ntype_check = 0;
3129 prev_func_check = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01003130 tok_type = LYXP_TOKEN_NAMETEST;
3131 } else {
Michal Vasko49fec8e2022-05-24 10:28:33 +02003132 /* if not '*', there is no prefix so it can still be NodeType/FunctionName, we can't finally decide now */
3133 prev_ntype_check = (expr_str[parsed] == '*') ? 0 : 1;
3134 prev_func_check = (prev_ntype_check && !has_axis) ? 1 : 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01003135 tok_type = LYXP_TOKEN_NAMETEST;
3136 }
3137 }
3138
3139 /* store the token, move on to the next one */
Radek Krejcif03a9e22020-09-18 20:09:31 +02003140 LY_CHECK_GOTO(ret = exp_add_token(ctx, expr, tok_type, parsed, tok_len), error);
Radek Krejcib1646a92018-11-02 16:08:26 +01003141 parsed += tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02003142 while (is_xmlws(expr_str[parsed])) {
Radek Krejcib1646a92018-11-02 16:08:26 +01003143 ++parsed;
3144 }
3145
Radek Krejcif03a9e22020-09-18 20:09:31 +02003146 } while (expr_str[parsed]);
Radek Krejcib1646a92018-11-02 16:08:26 +01003147
Michal Vasko004d3152020-06-11 19:59:22 +02003148 if (reparse) {
3149 /* prealloc repeat */
Radek Krejcif03a9e22020-09-18 20:09:31 +02003150 expr->repeat = calloc(expr->size, sizeof *expr->repeat);
3151 LY_CHECK_ERR_GOTO(!expr->repeat, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01003152
Michal Vasko004d3152020-06-11 19:59:22 +02003153 /* fill repeat */
aPiecekbf968d92021-05-27 14:35:05 +02003154 LY_CHECK_ERR_GOTO(reparse_or_expr(ctx, expr, &tok_idx, 0), ret = LY_EVALID, error);
Radek Krejcif03a9e22020-09-18 20:09:31 +02003155 if (expr->used > tok_idx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003156 LOGVAL(ctx, LYVE_XPATH, "Unparsed characters \"%s\" left at the end of an XPath expression.",
Michal Vasko69730152020-10-09 16:30:07 +02003157 &expr->expr[expr->tok_pos[tok_idx]]);
Radek Krejcif03a9e22020-09-18 20:09:31 +02003158 ret = LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02003159 goto error;
3160 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003161 }
3162
Radek Krejcif03a9e22020-09-18 20:09:31 +02003163 print_expr_struct_debug(expr);
3164 *expr_p = expr;
3165 return LY_SUCCESS;
Radek Krejcib1646a92018-11-02 16:08:26 +01003166
3167error:
Radek Krejcif03a9e22020-09-18 20:09:31 +02003168 lyxp_expr_free(ctx, expr);
3169 return ret;
Radek Krejcib1646a92018-11-02 16:08:26 +01003170}
3171
Michal Vasko1734be92020-09-22 08:55:10 +02003172LY_ERR
3173lyxp_expr_dup(const struct ly_ctx *ctx, const struct lyxp_expr *exp, struct lyxp_expr **dup_p)
Michal Vasko004d3152020-06-11 19:59:22 +02003174{
Michal Vasko1734be92020-09-22 08:55:10 +02003175 LY_ERR ret = LY_SUCCESS;
3176 struct lyxp_expr *dup = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02003177 uint32_t i, j;
3178
Michal Vasko7f45cf22020-10-01 12:49:44 +02003179 if (!exp) {
3180 goto cleanup;
3181 }
3182
Michal Vasko004d3152020-06-11 19:59:22 +02003183 dup = calloc(1, sizeof *dup);
Michal Vasko1734be92020-09-22 08:55:10 +02003184 LY_CHECK_ERR_GOTO(!dup, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02003185
Michal Vasko08e9b112021-06-11 15:41:17 +02003186 if (exp->used) {
3187 dup->tokens = malloc(exp->used * sizeof *dup->tokens);
3188 LY_CHECK_ERR_GOTO(!dup->tokens, LOGMEM(ctx); ret = LY_EMEM, cleanup);
3189 memcpy(dup->tokens, exp->tokens, exp->used * sizeof *dup->tokens);
Michal Vasko004d3152020-06-11 19:59:22 +02003190
Michal Vasko08e9b112021-06-11 15:41:17 +02003191 dup->tok_pos = malloc(exp->used * sizeof *dup->tok_pos);
3192 LY_CHECK_ERR_GOTO(!dup->tok_pos, LOGMEM(ctx); ret = LY_EMEM, cleanup);
3193 memcpy(dup->tok_pos, exp->tok_pos, exp->used * sizeof *dup->tok_pos);
Michal Vasko004d3152020-06-11 19:59:22 +02003194
Michal Vasko08e9b112021-06-11 15:41:17 +02003195 dup->tok_len = malloc(exp->used * sizeof *dup->tok_len);
3196 LY_CHECK_ERR_GOTO(!dup->tok_len, LOGMEM(ctx); ret = LY_EMEM, cleanup);
3197 memcpy(dup->tok_len, exp->tok_len, exp->used * sizeof *dup->tok_len);
Michal Vasko004d3152020-06-11 19:59:22 +02003198
Michal Vasko08e9b112021-06-11 15:41:17 +02003199 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 Vasko08e9b112021-06-11 15:41:17 +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 Vasko004d3152020-06-11 19:59:22 +02003214 }
3215 }
3216
3217 dup->used = exp->used;
3218 dup->size = exp->used;
Michal Vasko1734be92020-09-22 08:55:10 +02003219 LY_CHECK_GOTO(ret = lydict_insert(ctx, exp->expr, 0, &dup->expr), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02003220
Michal Vasko1734be92020-09-22 08:55:10 +02003221cleanup:
3222 if (ret) {
3223 lyxp_expr_free(ctx, dup);
3224 } else {
3225 *dup_p = dup;
3226 }
3227 return ret;
Michal Vasko004d3152020-06-11 19:59:22 +02003228}
3229
Michal Vasko03ff5a72019-09-11 13:49:33 +02003230/**
3231 * @brief Get the last-added schema node that is currently in the context.
3232 *
3233 * @param[in] set Set to search in.
3234 * @return Last-added schema context node, NULL if no node is in context.
3235 */
3236static struct lysc_node *
3237warn_get_scnode_in_ctx(struct lyxp_set *set)
3238{
3239 uint32_t i;
3240
3241 if (!set || (set->type != LYXP_SET_SCNODE_SET)) {
3242 return NULL;
3243 }
3244
3245 i = set->used;
3246 do {
3247 --i;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003248 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003249 /* if there are more, simply return the first found (last added) */
3250 return set->val.scnodes[i].scnode;
3251 }
3252 } while (i);
3253
3254 return NULL;
3255}
3256
3257/**
3258 * @brief Test whether a type is numeric - integer type or decimal64.
3259 *
3260 * @param[in] type Type to test.
Radek Krejci857189e2020-09-01 13:26:36 +02003261 * @return Boolean value whether @p type is numeric type or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003262 */
Radek Krejci857189e2020-09-01 13:26:36 +02003263static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003264warn_is_numeric_type(struct lysc_type *type)
3265{
3266 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003267 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003268 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003269
3270 switch (type->basetype) {
3271 case LY_TYPE_DEC64:
3272 case LY_TYPE_INT8:
3273 case LY_TYPE_UINT8:
3274 case LY_TYPE_INT16:
3275 case LY_TYPE_UINT16:
3276 case LY_TYPE_INT32:
3277 case LY_TYPE_UINT32:
3278 case LY_TYPE_INT64:
3279 case LY_TYPE_UINT64:
3280 return 1;
3281 case LY_TYPE_UNION:
3282 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003283 LY_ARRAY_FOR(uni->types, u) {
3284 ret = warn_is_numeric_type(uni->types[u]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003285 if (ret) {
3286 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003287 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003288 }
3289 }
3290 /* did not find any suitable type */
3291 return 0;
3292 case LY_TYPE_LEAFREF:
3293 return warn_is_numeric_type(((struct lysc_type_leafref *)type)->realtype);
3294 default:
3295 return 0;
3296 }
3297}
3298
3299/**
3300 * @brief Test whether a type is string-like - no integers, decimal64 or binary.
3301 *
3302 * @param[in] type Type to test.
Radek Krejci857189e2020-09-01 13:26:36 +02003303 * @return Boolean value whether @p type's basetype is string type or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003304 */
Radek Krejci857189e2020-09-01 13:26:36 +02003305static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003306warn_is_string_type(struct lysc_type *type)
3307{
3308 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003309 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003310 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003311
3312 switch (type->basetype) {
3313 case LY_TYPE_BITS:
3314 case LY_TYPE_ENUM:
3315 case LY_TYPE_IDENT:
3316 case LY_TYPE_INST:
3317 case LY_TYPE_STRING:
3318 return 1;
3319 case LY_TYPE_UNION:
3320 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003321 LY_ARRAY_FOR(uni->types, u) {
3322 ret = warn_is_string_type(uni->types[u]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003323 if (ret) {
3324 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003325 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003326 }
3327 }
3328 /* did not find any suitable type */
3329 return 0;
3330 case LY_TYPE_LEAFREF:
3331 return warn_is_string_type(((struct lysc_type_leafref *)type)->realtype);
3332 default:
3333 return 0;
3334 }
3335}
3336
3337/**
3338 * @brief Test whether a type is one specific type.
3339 *
3340 * @param[in] type Type to test.
3341 * @param[in] base Expected type.
Radek Krejci857189e2020-09-01 13:26:36 +02003342 * @return Boolean value whether the given @p type is of the specific basetype @p base.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003343 */
Radek Krejci857189e2020-09-01 13:26:36 +02003344static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003345warn_is_specific_type(struct lysc_type *type, LY_DATA_TYPE base)
3346{
3347 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003348 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003349 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003350
3351 if (type->basetype == base) {
3352 return 1;
3353 } else if (type->basetype == LY_TYPE_UNION) {
3354 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003355 LY_ARRAY_FOR(uni->types, u) {
3356 ret = warn_is_specific_type(uni->types[u], base);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003357 if (ret) {
3358 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003359 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003360 }
3361 }
3362 /* did not find any suitable type */
3363 return 0;
3364 } else if (type->basetype == LY_TYPE_LEAFREF) {
3365 return warn_is_specific_type(((struct lysc_type_leafref *)type)->realtype, base);
3366 }
3367
3368 return 0;
3369}
3370
3371/**
3372 * @brief Get next type of a (union) type.
3373 *
3374 * @param[in] type Base type.
3375 * @param[in] prev_type Previously returned type.
3376 * @return Next type or NULL.
3377 */
3378static struct lysc_type *
3379warn_is_equal_type_next_type(struct lysc_type *type, struct lysc_type *prev_type)
3380{
3381 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003382 ly_bool found = 0;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003383 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003384
3385 switch (type->basetype) {
3386 case LY_TYPE_UNION:
3387 uni = (struct lysc_type_union *)type;
3388 if (!prev_type) {
3389 return uni->types[0];
3390 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003391 LY_ARRAY_FOR(uni->types, u) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003392 if (found) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003393 return uni->types[u];
Michal Vasko03ff5a72019-09-11 13:49:33 +02003394 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003395 if (prev_type == uni->types[u]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003396 found = 1;
3397 }
3398 }
3399 return NULL;
3400 default:
3401 if (prev_type) {
3402 assert(type == prev_type);
3403 return NULL;
3404 } else {
3405 return type;
3406 }
3407 }
3408}
3409
3410/**
3411 * @brief Test whether 2 types have a common type.
3412 *
3413 * @param[in] type1 First type.
3414 * @param[in] type2 Second type.
3415 * @return 1 if they do, 0 otherwise.
3416 */
3417static int
3418warn_is_equal_type(struct lysc_type *type1, struct lysc_type *type2)
3419{
3420 struct lysc_type *t1, *rt1, *t2, *rt2;
3421
3422 t1 = NULL;
3423 while ((t1 = warn_is_equal_type_next_type(type1, t1))) {
3424 if (t1->basetype == LY_TYPE_LEAFREF) {
3425 rt1 = ((struct lysc_type_leafref *)t1)->realtype;
3426 } else {
3427 rt1 = t1;
3428 }
3429
3430 t2 = NULL;
3431 while ((t2 = warn_is_equal_type_next_type(type2, t2))) {
3432 if (t2->basetype == LY_TYPE_LEAFREF) {
3433 rt2 = ((struct lysc_type_leafref *)t2)->realtype;
3434 } else {
3435 rt2 = t2;
3436 }
3437
3438 if (rt2->basetype == rt1->basetype) {
3439 /* match found */
3440 return 1;
3441 }
3442 }
3443 }
3444
3445 return 0;
3446}
3447
3448/**
Michal Vaskoaa956522021-11-11 10:45:34 +01003449 * @brief Print warning with information about the XPath subexpression that caused previous warning.
3450 *
3451 * @param[in] ctx Context for logging.
3452 * @param[in] tok_pos Index of the subexpression in the whole expression.
3453 * @param[in] subexpr Subexpression start.
3454 * @param[in] subexpr_len Length of @p subexpr to print.
3455 * @param[in] cur_scnode Expression context node.
3456 */
3457static void
3458warn_subexpr_log(const struct ly_ctx *ctx, uint16_t tok_pos, const char *subexpr, int subexpr_len,
3459 const struct lysc_node *cur_scnode)
3460{
3461 char *path;
3462
3463 path = lysc_path(cur_scnode, LYSC_PATH_LOG, NULL, 0);
3464 LOGWRN(ctx, "Previous warning generated by XPath subexpression[%" PRIu16 "] \"%.*s\" with context node \"%s\".",
3465 tok_pos, subexpr_len, subexpr, path);
3466 free(path);
3467}
3468
3469/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02003470 * @brief Check both operands of comparison operators.
3471 *
3472 * @param[in] ctx Context for errors.
3473 * @param[in] set1 First operand set.
3474 * @param[in] set2 Second operand set.
3475 * @param[in] numbers_only Whether accept only numbers or other types are fine too (for '=' and '!=').
3476 * @param[in] expr Start of the expression to print with the warning.
3477 * @param[in] tok_pos Token position.
3478 */
3479static void
Michal Vaskoaa956522021-11-11 10:45:34 +01003480warn_operands(struct ly_ctx *ctx, struct lyxp_set *set1, struct lyxp_set *set2, ly_bool numbers_only, const char *expr,
3481 uint16_t tok_pos)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003482{
3483 struct lysc_node_leaf *node1, *node2;
Radek Krejci857189e2020-09-01 13:26:36 +02003484 ly_bool leaves = 1, warning = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003485
3486 node1 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set1);
3487 node2 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set2);
3488
3489 if (!node1 && !node2) {
3490 /* no node-sets involved, nothing to do */
3491 return;
3492 }
3493
3494 if (node1) {
3495 if (!(node1->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3496 LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node1->nodetype), node1->name);
3497 warning = 1;
3498 leaves = 0;
3499 } else if (numbers_only && !warn_is_numeric_type(node1->type)) {
3500 LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node1->name);
3501 warning = 1;
3502 }
3503 }
3504
3505 if (node2) {
3506 if (!(node2->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3507 LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node2->nodetype), node2->name);
3508 warning = 1;
3509 leaves = 0;
3510 } else if (numbers_only && !warn_is_numeric_type(node2->type)) {
3511 LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node2->name);
3512 warning = 1;
3513 }
3514 }
3515
3516 if (node1 && node2 && leaves && !numbers_only) {
Michal Vasko69730152020-10-09 16:30:07 +02003517 if ((warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type)) ||
3518 (!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_equal_type(node1->type, node2->type))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003521 LOGWRN(ctx, "Incompatible types of operands \"%s\" and \"%s\" for comparison.", node1->name, node2->name);
3522 warning = 1;
3523 }
3524 }
3525
3526 if (warning) {
Michal Vaskoaa956522021-11-11 10:45:34 +01003527 warn_subexpr_log(ctx, tok_pos, expr + tok_pos, 20, set1->cur_scnode);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003528 }
3529}
3530
3531/**
3532 * @brief Check that a value is valid for a leaf. If not applicable, does nothing.
3533 *
3534 * @param[in] exp Parsed XPath expression.
3535 * @param[in] set Set with the leaf/leaf-list.
3536 * @param[in] val_exp Index of the value (literal/number) in @p exp.
3537 * @param[in] equal_exp Index of the start of the equality expression in @p exp.
3538 * @param[in] last_equal_exp Index of the end of the equality expression in @p exp.
3539 */
3540static void
Michal Vasko40308e72020-10-20 16:38:40 +02003541warn_equality_value(const struct lyxp_expr *exp, struct lyxp_set *set, uint16_t val_exp, uint16_t equal_exp,
3542 uint16_t last_equal_exp)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003543{
3544 struct lysc_node *scnode;
3545 struct lysc_type *type;
3546 char *value;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003547 struct lyd_value storage;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003548 LY_ERR rc;
3549 struct ly_err_item *err = NULL;
3550
Michal Vasko69730152020-10-09 16:30:07 +02003551 if ((scnode = warn_get_scnode_in_ctx(set)) && (scnode->nodetype & (LYS_LEAF | LYS_LEAFLIST)) &&
3552 ((exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) || (exp->tokens[val_exp] == LYXP_TOKEN_NUMBER))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003553 /* check that the node can have the specified value */
3554 if (exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) {
3555 value = strndup(exp->expr + exp->tok_pos[val_exp] + 1, exp->tok_len[val_exp] - 2);
3556 } else {
3557 value = strndup(exp->expr + exp->tok_pos[val_exp], exp->tok_len[val_exp]);
3558 }
3559 if (!value) {
3560 LOGMEM(set->ctx);
3561 return;
3562 }
3563
3564 if ((((struct lysc_node_leaf *)scnode)->type->basetype == LY_TYPE_IDENT) && !strchr(value, ':')) {
3565 LOGWRN(set->ctx, "Identityref \"%s\" comparison with identity \"%s\" without prefix, consider adding"
Michal Vasko69730152020-10-09 16:30:07 +02003566 " a prefix or best using \"derived-from(-or-self)()\" functions.", scnode->name, value);
Michal Vaskoaa956522021-11-11 10:45:34 +01003567 warn_subexpr_log(set->ctx, exp->tok_pos[equal_exp], exp->expr + exp->tok_pos[equal_exp],
Radek Krejci0f969882020-08-21 16:56:47 +02003568 (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp],
Michal Vaskoaa956522021-11-11 10:45:34 +01003569 set->cur_scnode);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003570 }
3571
3572 type = ((struct lysc_node_leaf *)scnode)->type;
3573 if (type->basetype != LY_TYPE_IDENT) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003574 rc = type->plugin->store(set->ctx, type, value, strlen(value), 0, set->format, set->prefix_data,
Michal Vasko405cc9e2020-12-01 12:01:27 +01003575 LYD_HINT_DATA, scnode, &storage, NULL, &err);
Michal Vaskobf42e832020-11-23 16:59:42 +01003576 if (rc == LY_EINCOMPLETE) {
3577 rc = LY_SUCCESS;
3578 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003579
3580 if (err) {
3581 LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type (%s).", value, err->msg);
3582 ly_err_free(err);
3583 } else if (rc != LY_SUCCESS) {
3584 LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type.", value);
3585 }
3586 if (rc != LY_SUCCESS) {
Michal Vaskoaa956522021-11-11 10:45:34 +01003587 warn_subexpr_log(set->ctx, exp->tok_pos[equal_exp], exp->expr + exp->tok_pos[equal_exp],
Radek Krejci0f969882020-08-21 16:56:47 +02003588 (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp],
Michal Vaskoaa956522021-11-11 10:45:34 +01003589 set->cur_scnode);
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003590 } else {
3591 type->plugin->free(set->ctx, &storage);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003592 }
3593 }
3594 free(value);
3595 }
3596}
3597
3598/*
3599 * XPath functions
3600 */
3601
3602/**
3603 * @brief Execute the YANG 1.1 bit-is-set(node-set, string) function. Returns LYXP_SET_BOOLEAN
3604 * depending on whether the first node bit value from the second argument is set.
3605 *
3606 * @param[in] args Array of arguments.
3607 * @param[in] arg_count Count of elements in @p args.
3608 * @param[in,out] set Context and result set at the same time.
3609 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003610 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003611 */
3612static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003613xpath_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 +02003614{
3615 struct lyd_node_term *leaf;
3616 struct lysc_node_leaf *sleaf;
Michal Vasko2588b952021-07-29 07:43:26 +02003617 struct lyd_value_bits *bits;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003618 LY_ERR rc = LY_SUCCESS;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003619 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003620
3621 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003622 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003623 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003624 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3625 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3626 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3627 sleaf->name);
3628 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_BITS)) {
3629 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"bits\".", __func__, sleaf->name);
3630 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003631 }
3632
3633 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3634 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003635 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3636 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003637 } else if (!warn_is_string_type(sleaf->type)) {
3638 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003639 }
3640 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003641 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003642 return rc;
3643 }
3644
Michal Vaskod3678892020-05-21 10:06:58 +02003645 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003646 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 +02003647 return LY_EVALID;
3648 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003649 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003650 LY_CHECK_RET(rc);
3651
3652 set_fill_boolean(set, 0);
Michal Vaskod3678892020-05-21 10:06:58 +02003653 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003654 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
Michal Vasko2588b952021-07-29 07:43:26 +02003655 if ((leaf->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (leaf->value.realtype->basetype == LY_TYPE_BITS)) {
3656 LYD_VALUE_GET(&leaf->value, bits);
3657 LY_ARRAY_FOR(bits->items, u) {
3658 if (!strcmp(bits->items[u]->name, args[1]->val.str)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003659 set_fill_boolean(set, 1);
3660 break;
3661 }
3662 }
3663 }
3664 }
3665
3666 return LY_SUCCESS;
3667}
3668
3669/**
3670 * @brief Execute the XPath boolean(object) function. Returns LYXP_SET_BOOLEAN
3671 * with the argument converted to boolean.
3672 *
3673 * @param[in] args Array of arguments.
3674 * @param[in] arg_count Count of elements in @p args.
3675 * @param[in,out] set Context and result set at the same time.
3676 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003677 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003678 */
3679static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003680xpath_boolean(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003681{
3682 LY_ERR rc;
3683
3684 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02003685 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003686 return LY_SUCCESS;
3687 }
3688
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003689 rc = lyxp_set_cast(args[0], LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003690 LY_CHECK_RET(rc);
3691 set_fill_set(set, args[0]);
3692
3693 return LY_SUCCESS;
3694}
3695
3696/**
3697 * @brief Execute the XPath ceiling(number) function. Returns LYXP_SET_NUMBER
3698 * with the first argument rounded up to the nearest integer.
3699 *
3700 * @param[in] args Array of arguments.
3701 * @param[in] arg_count Count of elements in @p args.
3702 * @param[in,out] set Context and result set at the same time.
3703 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003704 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003705 */
3706static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003707xpath_ceiling(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003708{
3709 struct lysc_node_leaf *sleaf;
3710 LY_ERR rc = LY_SUCCESS;
3711
3712 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003713 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003714 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003715 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3716 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3717 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3718 sleaf->name);
3719 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
3720 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
3721 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003722 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003723 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003724 return rc;
3725 }
3726
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003727 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003728 LY_CHECK_RET(rc);
3729 if ((long long)args[0]->val.num != args[0]->val.num) {
3730 set_fill_number(set, ((long long)args[0]->val.num) + 1);
3731 } else {
3732 set_fill_number(set, args[0]->val.num);
3733 }
3734
3735 return LY_SUCCESS;
3736}
3737
3738/**
3739 * @brief Execute the XPath concat(string, string, string*) function.
3740 * Returns LYXP_SET_STRING with the concatenation of all the arguments.
3741 *
3742 * @param[in] args Array of arguments.
3743 * @param[in] arg_count Count of elements in @p args.
3744 * @param[in,out] set Context and result set at the same time.
3745 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003746 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003747 */
3748static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003749xpath_concat(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003750{
3751 uint16_t i;
3752 char *str = NULL;
3753 size_t used = 1;
3754 LY_ERR rc = LY_SUCCESS;
3755 struct lysc_node_leaf *sleaf;
3756
3757 if (options & LYXP_SCNODE_ALL) {
3758 for (i = 0; i < arg_count; ++i) {
3759 if ((args[i]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[i]))) {
3760 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3761 LOGWRN(set->ctx, "Argument #%u of %s is a %s node \"%s\".",
Michal Vasko69730152020-10-09 16:30:07 +02003762 i + 1, __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003763 } else if (!warn_is_string_type(sleaf->type)) {
Radek Krejci70124c82020-08-14 22:17:03 +02003764 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 +02003765 }
3766 }
3767 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003768 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003769 return rc;
3770 }
3771
3772 for (i = 0; i < arg_count; ++i) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003773 rc = lyxp_set_cast(args[i], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003774 if (rc != LY_SUCCESS) {
3775 free(str);
3776 return rc;
3777 }
3778
3779 str = ly_realloc(str, (used + strlen(args[i]->val.str)) * sizeof(char));
3780 LY_CHECK_ERR_RET(!str, LOGMEM(set->ctx), LY_EMEM);
3781 strcpy(str + used - 1, args[i]->val.str);
3782 used += strlen(args[i]->val.str);
3783 }
3784
3785 /* free, kind of */
Michal Vaskod3678892020-05-21 10:06:58 +02003786 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003787 set->type = LYXP_SET_STRING;
3788 set->val.str = str;
3789
3790 return LY_SUCCESS;
3791}
3792
3793/**
3794 * @brief Execute the XPath contains(string, string) function.
3795 * Returns LYXP_SET_BOOLEAN whether the second argument can
3796 * be found in the first or not.
3797 *
3798 * @param[in] args Array of arguments.
3799 * @param[in] arg_count Count of elements in @p args.
3800 * @param[in,out] set Context and result set at the same time.
3801 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003802 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003803 */
3804static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003805xpath_contains(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003806{
3807 struct lysc_node_leaf *sleaf;
3808 LY_ERR rc = LY_SUCCESS;
3809
3810 if (options & LYXP_SCNODE_ALL) {
3811 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3812 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003813 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3814 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003815 } else if (!warn_is_string_type(sleaf->type)) {
3816 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003817 }
3818 }
3819
3820 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3821 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003822 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3823 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003824 } else if (!warn_is_string_type(sleaf->type)) {
3825 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003826 }
3827 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003828 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003829 return rc;
3830 }
3831
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003832 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003833 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003834 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003835 LY_CHECK_RET(rc);
3836
3837 if (strstr(args[0]->val.str, args[1]->val.str)) {
3838 set_fill_boolean(set, 1);
3839 } else {
3840 set_fill_boolean(set, 0);
3841 }
3842
3843 return LY_SUCCESS;
3844}
3845
3846/**
3847 * @brief Execute the XPath count(node-set) function. Returns LYXP_SET_NUMBER
3848 * with the size of the node-set from the argument.
3849 *
3850 * @param[in] args Array of arguments.
3851 * @param[in] arg_count Count of elements in @p args.
3852 * @param[in,out] set Context and result set at the same time.
3853 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003854 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003855 */
3856static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003857xpath_count(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003858{
Michal Vasko03ff5a72019-09-11 13:49:33 +02003859 LY_ERR rc = LY_SUCCESS;
3860
3861 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003862 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003863 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003864 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003865 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003866 return rc;
3867 }
3868
Michal Vasko03ff5a72019-09-11 13:49:33 +02003869 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003870 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "count(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003871 return LY_EVALID;
3872 }
3873
3874 set_fill_number(set, args[0]->used);
3875 return LY_SUCCESS;
3876}
3877
3878/**
3879 * @brief Execute the XPath current() function. Returns LYXP_SET_NODE_SET
3880 * with the context with the intial node.
3881 *
3882 * @param[in] args Array of arguments.
3883 * @param[in] arg_count Count of elements in @p args.
3884 * @param[in,out] set Context and result set at the same time.
3885 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003886 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003887 */
3888static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003889xpath_current(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003890{
3891 if (arg_count || args) {
Radek Krejcie87c7dc2021-06-02 21:25:42 +02003892 LOGVAL(set->ctx, LY_VCODE_XP_INARGCOUNT, arg_count, LY_PRI_LENSTR("current()"));
Michal Vasko03ff5a72019-09-11 13:49:33 +02003893 return LY_EVALID;
3894 }
3895
3896 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02003897 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003898
Michal Vasko296dfaf2021-12-13 16:57:42 +01003899 if (set->cur_scnode) {
3900 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, set->cur_scnode, LYXP_NODE_ELEM, NULL));
3901 } else {
3902 /* root node */
3903 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, NULL, set->root_type, NULL));
3904 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003905 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02003906 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003907
Michal Vasko296dfaf2021-12-13 16:57:42 +01003908 if (set->cur_node) {
3909 /* position is filled later */
3910 set_insert_node(set, set->cur_node, 0, LYXP_NODE_ELEM, 0);
3911 } else {
3912 /* root node */
3913 set_insert_node(set, NULL, 0, set->root_type, 0);
3914 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003915 }
3916
3917 return LY_SUCCESS;
3918}
3919
3920/**
3921 * @brief Execute the YANG 1.1 deref(node-set) function. Returns LYXP_SET_NODE_SET with either
3922 * leafref or instance-identifier target node(s).
3923 *
3924 * @param[in] args Array of arguments.
3925 * @param[in] arg_count Count of elements in @p args.
3926 * @param[in,out] set Context and result set at the same time.
3927 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003928 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003929 */
3930static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003931xpath_deref(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003932{
3933 struct lyd_node_term *leaf;
Michal Vasko42e497c2020-01-06 08:38:25 +01003934 struct lysc_node_leaf *sleaf = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02003935 struct lysc_type_leafref *lref;
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003936 const struct lysc_node *target;
Michal Vasko004d3152020-06-11 19:59:22 +02003937 struct ly_path *p;
3938 struct lyd_node *node;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003939 char *errmsg = NULL;
Michal Vasko00cbf532020-06-15 13:58:47 +02003940 uint8_t oper;
Michal Vasko741bb562021-06-24 11:59:50 +02003941 LY_ERR r;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003942
3943 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003944 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003945 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003946 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
Michal Vasko423ba3f2021-07-19 13:08:50 +02003947 if (!(sleaf->nodetype & LYD_NODE_TERM)) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003948 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3949 sleaf->name);
Michal Vaskoed725d72021-06-23 12:03:45 +02003950 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_LEAFREF) &&
3951 !warn_is_specific_type(sleaf->type, LY_TYPE_INST)) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003952 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"leafref\" nor \"instance-identifier\".",
3953 __func__, sleaf->name);
3954 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003955 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003956 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko423ba3f2021-07-19 13:08:50 +02003957 if (sleaf && (sleaf->nodetype & LYD_NODE_TERM) && (sleaf->type->basetype == LY_TYPE_LEAFREF)) {
Michal Vasko004d3152020-06-11 19:59:22 +02003958 lref = (struct lysc_type_leafref *)sleaf->type;
Michal Vaskod1e53b92021-01-28 13:11:06 +01003959 oper = (sleaf->flags & LYS_IS_OUTPUT) ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT;
Michal Vasko004d3152020-06-11 19:59:22 +02003960
3961 /* it was already evaluated on schema, it must succeed */
Michal Vasko741bb562021-06-24 11:59:50 +02003962 r = ly_path_compile_leafref(set->ctx, &sleaf->node, NULL, lref->path, oper, LY_PATH_TARGET_MANY,
Michal Vasko24fc4d12021-07-12 14:41:20 +02003963 LY_VALUE_SCHEMA_RESOLVED, lref->prefixes, &p);
Michal Vasko741bb562021-06-24 11:59:50 +02003964 if (!r) {
3965 /* get the target node */
3966 target = p[LY_ARRAY_COUNT(p) - 1].node;
3967 ly_path_free(set->ctx, p);
Michal Vasko004d3152020-06-11 19:59:22 +02003968
Michal Vasko741bb562021-06-24 11:59:50 +02003969 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, target, LYXP_NODE_ELEM, NULL));
3970 } /* else the target was found before but is disabled so it was removed */
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003971 }
3972
Michal Vasko741bb562021-06-24 11:59:50 +02003973 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003974 }
3975
Michal Vaskod3678892020-05-21 10:06:58 +02003976 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003977 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "deref(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003978 return LY_EVALID;
3979 }
3980
Michal Vaskod3678892020-05-21 10:06:58 +02003981 lyxp_set_free_content(set);
3982 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003983 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
3984 sleaf = (struct lysc_node_leaf *)leaf->schema;
3985 if (sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
3986 if (sleaf->type->basetype == LY_TYPE_LEAFREF) {
3987 /* find leafref target */
Radek Krejci0b013302021-03-29 15:22:32 +02003988 if (lyplg_type_resolve_leafref((struct lysc_type_leafref *)sleaf->type, &leaf->node, &leaf->value, set->tree,
Michal Vasko9e685082021-01-29 14:49:09 +01003989 &node, &errmsg)) {
Michal Vasko004d3152020-06-11 19:59:22 +02003990 LOGERR(set->ctx, LY_EVALID, errmsg);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003991 free(errmsg);
Michal Vasko004d3152020-06-11 19:59:22 +02003992 return LY_EVALID;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003993 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003994 } else {
3995 assert(sleaf->type->basetype == LY_TYPE_INST);
Michal Vasko004d3152020-06-11 19:59:22 +02003996 if (ly_path_eval(leaf->value.target, set->tree, &node)) {
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003997 LOGERR(set->ctx, LY_EVALID, "Invalid instance-identifier \"%s\" value - required instance not found.",
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02003998 lyd_get_value(&leaf->node));
Michal Vasko03ff5a72019-09-11 13:49:33 +02003999 return LY_EVALID;
4000 }
4001 }
Michal Vasko004d3152020-06-11 19:59:22 +02004002
4003 /* insert it */
4004 set_insert_node(set, node, 0, LYXP_NODE_ELEM, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004005 }
4006 }
4007
4008 return LY_SUCCESS;
4009}
4010
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004011static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02004012xpath_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 +02004013{
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01004014 uint32_t i;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004015 LY_ARRAY_COUNT_TYPE u;
4016 struct lyd_node_term *leaf;
4017 struct lysc_node_leaf *sleaf;
4018 struct lyd_meta *meta;
Michal Vasko93923692021-05-07 15:28:02 +02004019 struct lyd_value *val;
4020 const struct lys_module *mod;
4021 const char *id_name;
4022 uint16_t id_len;
4023 struct lysc_ident *id;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004024 LY_ERR rc = LY_SUCCESS;
Radek Krejci857189e2020-09-01 13:26:36 +02004025 ly_bool found;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004026
4027 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02004028 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004029 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", func);
Michal Vasko5676f4e2021-04-06 17:14:45 +02004030 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4031 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4032 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype),
4033 sleaf->name);
4034 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_IDENT)) {
4035 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"identityref\".", func, sleaf->name);
4036 }
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004037 }
4038
4039 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4040 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4041 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype),
Michal Vasko69730152020-10-09 16:30:07 +02004042 sleaf->name);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004043 } else if (!warn_is_string_type(sleaf->type)) {
4044 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", func, sleaf->name);
4045 }
4046 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004047 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004048 return rc;
4049 }
4050
4051 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004052 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 +02004053 return LY_EVALID;
4054 }
4055 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
4056 LY_CHECK_RET(rc);
4057
Michal Vasko93923692021-05-07 15:28:02 +02004058 /* parse the identity */
4059 id_name = args[1]->val.str;
4060 id_len = strlen(id_name);
4061 rc = moveto_resolve_model(&id_name, &id_len, set, set->cur_node ? set->cur_node->schema : NULL, &mod);
4062 LY_CHECK_RET(rc);
4063 if (!mod) {
4064 LOGVAL(set->ctx, LYVE_XPATH, "Identity \"%.*s\" without a prefix.", (int)id_len, id_name);
4065 return LY_EVALID;
4066 }
4067
4068 /* find the identity */
4069 found = 0;
4070 LY_ARRAY_FOR(mod->identities, u) {
4071 if (!ly_strncmp(mod->identities[u].name, id_name, id_len)) {
4072 /* we have match */
4073 found = 1;
4074 break;
4075 }
4076 }
4077 if (!found) {
4078 LOGVAL(set->ctx, LYVE_XPATH, "Identity \"%.*s\" not found in module \"%s\".", (int)id_len, id_name, mod->name);
4079 return LY_EVALID;
4080 }
4081 id = &mod->identities[u];
4082
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004083 set_fill_boolean(set, 0);
4084 found = 0;
4085 for (i = 0; i < args[0]->used; ++i) {
4086 if ((args[0]->val.nodes[i].type != LYXP_NODE_ELEM) && (args[0]->val.nodes[i].type != LYXP_NODE_META)) {
4087 continue;
4088 }
4089
4090 if (args[0]->val.nodes[i].type == LYXP_NODE_ELEM) {
4091 leaf = (struct lyd_node_term *)args[0]->val.nodes[i].node;
4092 sleaf = (struct lysc_node_leaf *)leaf->schema;
4093 val = &leaf->value;
4094 if (!(sleaf->nodetype & LYD_NODE_TERM) || (leaf->value.realtype->basetype != LY_TYPE_IDENT)) {
4095 /* uninteresting */
4096 continue;
4097 }
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004098 } else {
4099 meta = args[0]->val.meta[i].meta;
4100 val = &meta->value;
4101 if (val->realtype->basetype != LY_TYPE_IDENT) {
4102 /* uninteresting */
4103 continue;
4104 }
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004105 }
4106
Michal Vasko93923692021-05-07 15:28:02 +02004107 /* check the identity itself */
4108 if (self_match && (id == val->ident)) {
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004109 set_fill_boolean(set, 1);
4110 found = 1;
4111 }
Michal Vasko93923692021-05-07 15:28:02 +02004112 if (!found && !lyplg_type_identity_isderived(id, val->ident)) {
4113 set_fill_boolean(set, 1);
4114 found = 1;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004115 }
4116
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004117 if (found) {
4118 break;
4119 }
4120 }
4121
4122 return LY_SUCCESS;
4123}
4124
Michal Vasko03ff5a72019-09-11 13:49:33 +02004125/**
4126 * @brief Execute the YANG 1.1 derived-from(node-set, string) function. Returns LYXP_SET_BOOLEAN depending
4127 * on whether the first argument nodes contain a node of an identity derived from the second
4128 * argument identity.
4129 *
4130 * @param[in] args Array of arguments.
4131 * @param[in] arg_count Count of elements in @p args.
4132 * @param[in,out] set Context and result set at the same time.
4133 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004134 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004135 */
4136static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004137xpath_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 +02004138{
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004139 return xpath_derived_(args, set, options, 0, __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004140}
4141
4142/**
4143 * @brief Execute the YANG 1.1 derived-from-or-self(node-set, string) function. Returns LYXP_SET_BOOLEAN depending
4144 * on whether the first argument nodes contain a node of an identity that either is or is derived from
4145 * the second argument identity.
4146 *
4147 * @param[in] args Array of arguments.
4148 * @param[in] arg_count Count of elements in @p args.
4149 * @param[in,out] set Context and result set at the same time.
4150 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004151 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004152 */
4153static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004154xpath_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 +02004155{
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004156 return xpath_derived_(args, set, options, 1, __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004157}
4158
4159/**
4160 * @brief Execute the YANG 1.1 enum-value(node-set) function. Returns LYXP_SET_NUMBER
4161 * with the integer value of the first node's enum value, otherwise NaN.
4162 *
4163 * @param[in] args Array of arguments.
4164 * @param[in] arg_count Count of elements in @p args.
4165 * @param[in,out] set Context and result set at the same time.
4166 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004167 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004168 */
4169static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004170xpath_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 +02004171{
4172 struct lyd_node_term *leaf;
4173 struct lysc_node_leaf *sleaf;
4174 LY_ERR rc = LY_SUCCESS;
4175
4176 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02004177 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004178 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02004179 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4180 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4181 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
4182 sleaf->name);
4183 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_ENUM)) {
4184 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"enumeration\".", __func__, sleaf->name);
4185 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004186 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004187 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004188 return rc;
4189 }
4190
Michal Vaskod3678892020-05-21 10:06:58 +02004191 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004192 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "enum-value(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004193 return LY_EVALID;
4194 }
4195
4196 set_fill_number(set, NAN);
Michal Vaskod3678892020-05-21 10:06:58 +02004197 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004198 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
4199 sleaf = (struct lysc_node_leaf *)leaf->schema;
4200 if ((sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (sleaf->type->basetype == LY_TYPE_ENUM)) {
4201 set_fill_number(set, leaf->value.enum_item->value);
4202 }
4203 }
4204
4205 return LY_SUCCESS;
4206}
4207
4208/**
4209 * @brief Execute the XPath false() function. Returns LYXP_SET_BOOLEAN
4210 * with false value.
4211 *
4212 * @param[in] args Array of arguments.
4213 * @param[in] arg_count Count of elements in @p args.
4214 * @param[in,out] set Context and result set at the same time.
4215 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004216 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004217 */
4218static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004219xpath_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 +02004220{
4221 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004222 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004223 return LY_SUCCESS;
4224 }
4225
4226 set_fill_boolean(set, 0);
4227 return LY_SUCCESS;
4228}
4229
4230/**
4231 * @brief Execute the XPath floor(number) function. Returns LYXP_SET_NUMBER
4232 * with the first argument floored (truncated).
4233 *
4234 * @param[in] args Array of arguments.
4235 * @param[in] arg_count Count of elements in @p args.
4236 * @param[in,out] set Context and result set at the same time.
4237 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004238 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004239 */
4240static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004241xpath_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 +02004242{
4243 LY_ERR rc;
4244
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004245 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004246 LY_CHECK_RET(rc);
4247 if (isfinite(args[0]->val.num)) {
4248 set_fill_number(set, (long long)args[0]->val.num);
4249 }
4250
4251 return LY_SUCCESS;
4252}
4253
4254/**
4255 * @brief Execute the XPath lang(string) function. Returns LYXP_SET_BOOLEAN
4256 * whether the language of the text matches the one from the argument.
4257 *
4258 * @param[in] args Array of arguments.
4259 * @param[in] arg_count Count of elements in @p args.
4260 * @param[in,out] set Context and result set at the same time.
4261 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004262 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004263 */
4264static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004265xpath_lang(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004266{
4267 const struct lyd_node *node;
4268 struct lysc_node_leaf *sleaf;
Michal Vasko9f96a052020-03-10 09:41:45 +01004269 struct lyd_meta *meta = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004270 const char *val;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004271 LY_ERR rc = LY_SUCCESS;
4272
4273 if (options & LYXP_SCNODE_ALL) {
4274 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4275 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004276 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
4277 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004278 } else if (!warn_is_string_type(sleaf->type)) {
4279 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004280 }
4281 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004282 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004283 return rc;
4284 }
4285
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004286 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004287 LY_CHECK_RET(rc);
4288
Michal Vasko03ff5a72019-09-11 13:49:33 +02004289 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004290 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "lang(string)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004291 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004292 } else if (!set->used) {
4293 set_fill_boolean(set, 0);
4294 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004295 }
4296
4297 switch (set->val.nodes[0].type) {
4298 case LYXP_NODE_ELEM:
4299 case LYXP_NODE_TEXT:
4300 node = set->val.nodes[0].node;
4301 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004302 case LYXP_NODE_META:
4303 node = set->val.meta[0].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004304 break;
4305 default:
4306 /* nothing to do with roots */
4307 set_fill_boolean(set, 0);
4308 return LY_SUCCESS;
4309 }
4310
Michal Vasko9f96a052020-03-10 09:41:45 +01004311 /* find lang metadata */
Michal Vasko9e685082021-01-29 14:49:09 +01004312 for ( ; node; node = lyd_parent(node)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01004313 for (meta = node->meta; meta; meta = meta->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004314 /* annotations */
Michal Vasko9f96a052020-03-10 09:41:45 +01004315 if (meta->name && !strcmp(meta->name, "lang") && !strcmp(meta->annotation->module->name, "xml")) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004316 break;
4317 }
4318 }
4319
Michal Vasko9f96a052020-03-10 09:41:45 +01004320 if (meta) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004321 break;
4322 }
4323 }
4324
4325 /* compare languages */
Michal Vasko9f96a052020-03-10 09:41:45 +01004326 if (!meta) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004327 set_fill_boolean(set, 0);
4328 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004329 uint64_t i;
4330
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02004331 val = lyd_get_meta_value(meta);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004332 for (i = 0; args[0]->val.str[i]; ++i) {
4333 if (tolower(args[0]->val.str[i]) != tolower(val[i])) {
4334 set_fill_boolean(set, 0);
4335 break;
4336 }
4337 }
4338 if (!args[0]->val.str[i]) {
4339 if (!val[i] || (val[i] == '-')) {
4340 set_fill_boolean(set, 1);
4341 } else {
4342 set_fill_boolean(set, 0);
4343 }
4344 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004345 }
4346
4347 return LY_SUCCESS;
4348}
4349
4350/**
4351 * @brief Execute the XPath last() function. Returns LYXP_SET_NUMBER
4352 * with the context size.
4353 *
4354 * @param[in] args Array of arguments.
4355 * @param[in] arg_count Count of elements in @p args.
4356 * @param[in,out] set Context and result set at the same time.
4357 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004358 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004359 */
4360static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004361xpath_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 +02004362{
4363 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004364 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004365 return LY_SUCCESS;
4366 }
4367
Michal Vasko03ff5a72019-09-11 13:49:33 +02004368 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004369 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "last()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004370 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004371 } else if (!set->used) {
4372 set_fill_number(set, 0);
4373 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004374 }
4375
4376 set_fill_number(set, set->ctx_size);
4377 return LY_SUCCESS;
4378}
4379
4380/**
4381 * @brief Execute the XPath local-name(node-set?) function. Returns LYXP_SET_STRING
4382 * with the node name without namespace from the argument or the context.
4383 *
4384 * @param[in] args Array of arguments.
4385 * @param[in] arg_count Count of elements in @p args.
4386 * @param[in,out] set Context and result set at the same time.
4387 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004388 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004389 */
4390static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004391xpath_local_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004392{
4393 struct lyxp_set_node *item;
Michal Vasko69730152020-10-09 16:30:07 +02004394
Michal Vasko03ff5a72019-09-11 13:49:33 +02004395 /* suppress unused variable warning */
4396 (void)options;
4397
4398 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004399 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004400 return LY_SUCCESS;
4401 }
4402
4403 if (arg_count) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004404 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004405 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004406 "local-name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004407 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004408 } else if (!args[0]->used) {
4409 set_fill_string(set, "", 0);
4410 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004411 }
4412
4413 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004414 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004415
4416 item = &args[0]->val.nodes[0];
4417 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004418 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004419 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "local-name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004420 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004421 } else if (!set->used) {
4422 set_fill_string(set, "", 0);
4423 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004424 }
4425
4426 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004427 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004428
4429 item = &set->val.nodes[0];
4430 }
4431
4432 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004433 case LYXP_NODE_NONE:
4434 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004435 case LYXP_NODE_ROOT:
4436 case LYXP_NODE_ROOT_CONFIG:
4437 case LYXP_NODE_TEXT:
4438 set_fill_string(set, "", 0);
4439 break;
4440 case LYXP_NODE_ELEM:
4441 set_fill_string(set, item->node->schema->name, strlen(item->node->schema->name));
4442 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004443 case LYXP_NODE_META:
4444 set_fill_string(set, ((struct lyd_meta *)item->node)->name, strlen(((struct lyd_meta *)item->node)->name));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004445 break;
4446 }
4447
4448 return LY_SUCCESS;
4449}
4450
4451/**
4452 * @brief Execute the XPath name(node-set?) function. Returns LYXP_SET_STRING
4453 * with the node name fully qualified (with namespace) from the argument or the context.
4454 *
4455 * @param[in] args Array of arguments.
4456 * @param[in] arg_count Count of elements in @p args.
4457 * @param[in,out] set Context and result set at the same time.
4458 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004459 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004460 */
4461static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004462xpath_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004463{
4464 struct lyxp_set_node *item;
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004465 struct lys_module *mod = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004466 char *str;
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004467 const char *name = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004468
4469 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004470 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004471 return LY_SUCCESS;
4472 }
4473
4474 if (arg_count) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004475 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004476 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004477 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004478 } else if (!args[0]->used) {
4479 set_fill_string(set, "", 0);
4480 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004481 }
4482
4483 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004484 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004485
4486 item = &args[0]->val.nodes[0];
4487 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004488 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004489 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004490 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004491 } else if (!set->used) {
4492 set_fill_string(set, "", 0);
4493 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004494 }
4495
4496 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004497 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004498
4499 item = &set->val.nodes[0];
4500 }
4501
4502 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004503 case LYXP_NODE_NONE:
4504 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004505 case LYXP_NODE_ROOT:
4506 case LYXP_NODE_ROOT_CONFIG:
4507 case LYXP_NODE_TEXT:
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004508 /* keep NULL */
Michal Vasko03ff5a72019-09-11 13:49:33 +02004509 break;
4510 case LYXP_NODE_ELEM:
4511 mod = item->node->schema->module;
4512 name = item->node->schema->name;
4513 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004514 case LYXP_NODE_META:
4515 mod = ((struct lyd_meta *)item->node)->annotation->module;
4516 name = ((struct lyd_meta *)item->node)->name;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004517 break;
4518 }
4519
4520 if (mod && name) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004521 int rc = asprintf(&str, "%s:%s", ly_get_prefix(mod, set->format, set->prefix_data), name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004522 LY_CHECK_ERR_RET(rc == -1, LOGMEM(set->ctx), LY_EMEM);
4523 set_fill_string(set, str, strlen(str));
4524 free(str);
4525 } else {
4526 set_fill_string(set, "", 0);
4527 }
4528
4529 return LY_SUCCESS;
4530}
4531
4532/**
4533 * @brief Execute the XPath namespace-uri(node-set?) function. Returns LYXP_SET_STRING
4534 * with the namespace of the node from the argument or the context.
4535 *
4536 * @param[in] args Array of arguments.
4537 * @param[in] arg_count Count of elements in @p args.
4538 * @param[in,out] set Context and result set at the same time.
4539 * @param[in] options XPath options.
4540 * @return LY_ERR (LY_EINVAL for wrong arguments on schema)
4541 */
4542static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004543xpath_namespace_uri(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004544{
4545 struct lyxp_set_node *item;
4546 struct lys_module *mod;
Michal Vasko69730152020-10-09 16:30:07 +02004547
Michal Vasko03ff5a72019-09-11 13:49:33 +02004548 /* suppress unused variable warning */
4549 (void)options;
4550
4551 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004552 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004553 return LY_SUCCESS;
4554 }
4555
4556 if (arg_count) {
Michal Vaskod3678892020-05-21 10:06:58 +02004557 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004558 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
Michal Vasko69730152020-10-09 16:30:07 +02004559 "namespace-uri(node-set?)");
Michal Vaskod3678892020-05-21 10:06:58 +02004560 return LY_EVALID;
4561 } else if (!args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004562 set_fill_string(set, "", 0);
4563 return LY_SUCCESS;
4564 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004565
4566 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004567 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004568
4569 item = &args[0]->val.nodes[0];
4570 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004571 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004572 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "namespace-uri(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004573 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004574 } else if (!set->used) {
4575 set_fill_string(set, "", 0);
4576 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004577 }
4578
4579 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004580 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004581
4582 item = &set->val.nodes[0];
4583 }
4584
4585 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004586 case LYXP_NODE_NONE:
4587 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004588 case LYXP_NODE_ROOT:
4589 case LYXP_NODE_ROOT_CONFIG:
4590 case LYXP_NODE_TEXT:
4591 set_fill_string(set, "", 0);
4592 break;
4593 case LYXP_NODE_ELEM:
Michal Vasko9f96a052020-03-10 09:41:45 +01004594 case LYXP_NODE_META:
Michal Vasko03ff5a72019-09-11 13:49:33 +02004595 if (item->type == LYXP_NODE_ELEM) {
4596 mod = item->node->schema->module;
Michal Vasko9f96a052020-03-10 09:41:45 +01004597 } else { /* LYXP_NODE_META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02004598 /* annotations */
Michal Vasko9f96a052020-03-10 09:41:45 +01004599 mod = ((struct lyd_meta *)item->node)->annotation->module;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004600 }
4601
4602 set_fill_string(set, mod->ns, strlen(mod->ns));
4603 break;
4604 }
4605
4606 return LY_SUCCESS;
4607}
4608
4609/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02004610 * @brief Execute the XPath normalize-space(string?) function. Returns LYXP_SET_STRING
4611 * with normalized value (no leading, trailing, double white spaces) of the node
4612 * from the argument or the context.
4613 *
4614 * @param[in] args Array of arguments.
4615 * @param[in] arg_count Count of elements in @p args.
4616 * @param[in,out] set Context and result set at the same time.
4617 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004618 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004619 */
4620static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004621xpath_normalize_space(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004622{
4623 uint16_t i, new_used;
4624 char *new;
Radek Krejci857189e2020-09-01 13:26:36 +02004625 ly_bool have_spaces = 0, space_before = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004626 struct lysc_node_leaf *sleaf;
4627 LY_ERR rc = LY_SUCCESS;
4628
4629 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004630 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) &&
4631 (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004632 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004633 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
4634 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004635 } else if (!warn_is_string_type(sleaf->type)) {
4636 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004637 }
4638 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004639 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004640 return rc;
4641 }
4642
4643 if (arg_count) {
4644 set_fill_set(set, args[0]);
4645 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004646 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004647 LY_CHECK_RET(rc);
4648
4649 /* is there any normalization necessary? */
4650 for (i = 0; set->val.str[i]; ++i) {
4651 if (is_xmlws(set->val.str[i])) {
4652 if ((i == 0) || space_before || (!set->val.str[i + 1])) {
4653 have_spaces = 1;
4654 break;
4655 }
4656 space_before = 1;
4657 } else {
4658 space_before = 0;
4659 }
4660 }
4661
4662 /* yep, there is */
4663 if (have_spaces) {
4664 /* it's enough, at least one character will go, makes space for ending '\0' */
4665 new = malloc(strlen(set->val.str) * sizeof(char));
4666 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4667 new_used = 0;
4668
4669 space_before = 0;
4670 for (i = 0; set->val.str[i]; ++i) {
4671 if (is_xmlws(set->val.str[i])) {
4672 if ((i == 0) || space_before) {
4673 space_before = 1;
4674 continue;
4675 } else {
4676 space_before = 1;
4677 }
4678 } else {
4679 space_before = 0;
4680 }
4681
4682 new[new_used] = (space_before ? ' ' : set->val.str[i]);
4683 ++new_used;
4684 }
4685
4686 /* at worst there is one trailing space now */
4687 if (new_used && is_xmlws(new[new_used - 1])) {
4688 --new_used;
4689 }
4690
4691 new = ly_realloc(new, (new_used + 1) * sizeof(char));
4692 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4693 new[new_used] = '\0';
4694
4695 free(set->val.str);
4696 set->val.str = new;
4697 }
4698
4699 return LY_SUCCESS;
4700}
4701
4702/**
4703 * @brief Execute the XPath not(boolean) function. Returns LYXP_SET_BOOLEAN
4704 * with the argument converted to boolean and logically inverted.
4705 *
4706 * @param[in] args Array of arguments.
4707 * @param[in] arg_count Count of elements in @p args.
4708 * @param[in,out] set Context and result set at the same time.
4709 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004710 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004711 */
4712static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004713xpath_not(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004714{
4715 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004716 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004717 return LY_SUCCESS;
4718 }
4719
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004720 lyxp_set_cast(args[0], LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +02004721 if (args[0]->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004722 set_fill_boolean(set, 0);
4723 } else {
4724 set_fill_boolean(set, 1);
4725 }
4726
4727 return LY_SUCCESS;
4728}
4729
4730/**
4731 * @brief Execute the XPath number(object?) function. Returns LYXP_SET_NUMBER
4732 * with the number representation of either the argument or the context.
4733 *
4734 * @param[in] args Array of arguments.
4735 * @param[in] arg_count Count of elements in @p args.
4736 * @param[in,out] set Context and result set at the same time.
4737 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004738 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004739 */
4740static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004741xpath_number(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004742{
4743 LY_ERR rc;
4744
4745 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004746 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004747 return LY_SUCCESS;
4748 }
4749
4750 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004751 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004752 LY_CHECK_RET(rc);
4753 set_fill_set(set, args[0]);
4754 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004755 rc = lyxp_set_cast(set, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004756 LY_CHECK_RET(rc);
4757 }
4758
4759 return LY_SUCCESS;
4760}
4761
4762/**
4763 * @brief Execute the XPath position() function. Returns LYXP_SET_NUMBER
4764 * with the context position.
4765 *
4766 * @param[in] args Array of arguments.
4767 * @param[in] arg_count Count of elements in @p args.
4768 * @param[in,out] set Context and result set at the same time.
4769 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004770 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004771 */
4772static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004773xpath_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 +02004774{
4775 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004776 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004777 return LY_SUCCESS;
4778 }
4779
Michal Vasko03ff5a72019-09-11 13:49:33 +02004780 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004781 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "position()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004782 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004783 } else if (!set->used) {
4784 set_fill_number(set, 0);
4785 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004786 }
4787
4788 set_fill_number(set, set->ctx_pos);
4789
4790 /* UNUSED in 'Release' build type */
4791 (void)options;
4792 return LY_SUCCESS;
4793}
4794
4795/**
4796 * @brief Execute the YANG 1.1 re-match(string, string) function. Returns LYXP_SET_BOOLEAN
4797 * depending on whether the second argument regex matches the first argument string. For details refer to
4798 * YANG 1.1 RFC section 10.2.1.
4799 *
4800 * @param[in] args Array of arguments.
4801 * @param[in] arg_count Count of elements in @p args.
4802 * @param[in,out] set Context and result set at the same time.
4803 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004804 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004805 */
4806static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004807xpath_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 +02004808{
4809 struct lysc_pattern **patterns = NULL, **pattern;
4810 struct lysc_node_leaf *sleaf;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004811 LY_ERR rc = LY_SUCCESS;
4812 struct ly_err_item *err;
4813
4814 if (options & LYXP_SCNODE_ALL) {
4815 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4816 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4817 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 +02004818 } else if (!warn_is_string_type(sleaf->type)) {
4819 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004820 }
4821 }
4822
4823 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4824 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4825 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 +02004826 } else if (!warn_is_string_type(sleaf->type)) {
4827 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004828 }
4829 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004830 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004831 return rc;
4832 }
4833
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004834 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004835 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004836 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004837 LY_CHECK_RET(rc);
4838
4839 LY_ARRAY_NEW_RET(set->ctx, patterns, pattern, LY_EMEM);
Radek Iša45802b52021-02-09 09:21:58 +01004840 *pattern = calloc(1, sizeof **pattern);
Radek Krejciddace2c2021-01-08 11:30:56 +01004841 LOG_LOCSET(NULL, set->cur_node, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01004842 rc = lys_compile_type_pattern_check(set->ctx, args[1]->val.str, &(*pattern)->code);
Michal Vasko4a7d4d62021-12-13 17:05:06 +01004843 if (set->cur_node) {
4844 LOG_LOCBACK(0, 1, 0, 0);
4845 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004846 if (rc != LY_SUCCESS) {
4847 LY_ARRAY_FREE(patterns);
4848 return rc;
4849 }
4850
Radek Krejci0b013302021-03-29 15:22:32 +02004851 rc = lyplg_type_validate_patterns(patterns, args[0]->val.str, strlen(args[0]->val.str), &err);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004852 pcre2_code_free((*pattern)->code);
4853 free(*pattern);
4854 LY_ARRAY_FREE(patterns);
4855 if (rc && (rc != LY_EVALID)) {
Michal Vasko177d0ed2020-11-23 16:43:03 +01004856 ly_err_print(set->ctx, err);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004857 ly_err_free(err);
4858 return rc;
4859 }
4860
4861 if (rc == LY_EVALID) {
4862 ly_err_free(err);
4863 set_fill_boolean(set, 0);
4864 } else {
4865 set_fill_boolean(set, 1);
4866 }
4867
4868 return LY_SUCCESS;
4869}
4870
4871/**
4872 * @brief Execute the XPath round(number) function. Returns LYXP_SET_NUMBER
4873 * with the rounded first argument. For details refer to
4874 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-round.
4875 *
4876 * @param[in] args Array of arguments.
4877 * @param[in] arg_count Count of elements in @p args.
4878 * @param[in,out] set Context and result set at the same time.
4879 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004880 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004881 */
4882static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004883xpath_round(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004884{
4885 struct lysc_node_leaf *sleaf;
4886 LY_ERR rc = LY_SUCCESS;
4887
4888 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02004889 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004890 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02004891 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4892 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4893 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
4894 sleaf->name);
4895 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
4896 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
4897 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004898 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004899 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004900 return rc;
4901 }
4902
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004903 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004904 LY_CHECK_RET(rc);
4905
4906 /* cover only the cases where floor can't be used */
4907 if ((args[0]->val.num == -0.0f) || ((args[0]->val.num < 0) && (args[0]->val.num >= -0.5))) {
4908 set_fill_number(set, -0.0f);
4909 } else {
4910 args[0]->val.num += 0.5;
4911 rc = xpath_floor(args, 1, args[0], options);
4912 LY_CHECK_RET(rc);
4913 set_fill_number(set, args[0]->val.num);
4914 }
4915
4916 return LY_SUCCESS;
4917}
4918
4919/**
4920 * @brief Execute the XPath starts-with(string, string) function.
4921 * Returns LYXP_SET_BOOLEAN whether the second argument is
4922 * the prefix of the first or not.
4923 *
4924 * @param[in] args Array of arguments.
4925 * @param[in] arg_count Count of elements in @p args.
4926 * @param[in,out] set Context and result set at the same time.
4927 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004928 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004929 */
4930static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004931xpath_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 +02004932{
4933 struct lysc_node_leaf *sleaf;
4934 LY_ERR rc = LY_SUCCESS;
4935
4936 if (options & LYXP_SCNODE_ALL) {
4937 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4938 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4939 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004940 } else if (!warn_is_string_type(sleaf->type)) {
4941 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004942 }
4943 }
4944
4945 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4946 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4947 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 +02004948 } else if (!warn_is_string_type(sleaf->type)) {
4949 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004950 }
4951 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004952 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004953 return rc;
4954 }
4955
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004956 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004957 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004958 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004959 LY_CHECK_RET(rc);
4960
4961 if (strncmp(args[0]->val.str, args[1]->val.str, strlen(args[1]->val.str))) {
4962 set_fill_boolean(set, 0);
4963 } else {
4964 set_fill_boolean(set, 1);
4965 }
4966
4967 return LY_SUCCESS;
4968}
4969
4970/**
4971 * @brief Execute the XPath string(object?) function. Returns LYXP_SET_STRING
4972 * with the string representation of either the argument or the context.
4973 *
4974 * @param[in] args Array of arguments.
4975 * @param[in] arg_count Count of elements in @p args.
4976 * @param[in,out] set Context and result set at the same time.
4977 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004978 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004979 */
4980static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004981xpath_string(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004982{
4983 LY_ERR rc;
4984
4985 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004986 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004987 return LY_SUCCESS;
4988 }
4989
4990 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004991 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004992 LY_CHECK_RET(rc);
4993 set_fill_set(set, args[0]);
4994 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004995 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004996 LY_CHECK_RET(rc);
4997 }
4998
4999 return LY_SUCCESS;
5000}
5001
5002/**
5003 * @brief Execute the XPath string-length(string?) function. Returns LYXP_SET_NUMBER
5004 * with the length of the string in either the argument or the context.
5005 *
5006 * @param[in] args Array of arguments.
5007 * @param[in] arg_count Count of elements in @p args.
5008 * @param[in,out] set Context and result set at the same time.
5009 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005010 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005011 */
5012static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005013xpath_string_length(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005014{
5015 struct lysc_node_leaf *sleaf;
5016 LY_ERR rc = LY_SUCCESS;
5017
5018 if (options & LYXP_SCNODE_ALL) {
5019 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5020 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5021 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 +02005022 } else if (!warn_is_string_type(sleaf->type)) {
5023 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005024 }
5025 }
5026 if (!arg_count && (set->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set))) {
5027 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5028 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 +02005029 } else if (!warn_is_string_type(sleaf->type)) {
5030 LOGWRN(set->ctx, "Argument #0 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005031 }
5032 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005033 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005034 return rc;
5035 }
5036
5037 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005038 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005039 LY_CHECK_RET(rc);
5040 set_fill_number(set, strlen(args[0]->val.str));
5041 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005042 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005043 LY_CHECK_RET(rc);
5044 set_fill_number(set, strlen(set->val.str));
5045 }
5046
5047 return LY_SUCCESS;
5048}
5049
5050/**
5051 * @brief Execute the XPath substring(string, number, number?) function.
5052 * Returns LYXP_SET_STRING substring of the first argument starting
5053 * on the second argument index ending on the third argument index,
5054 * indexed from 1. For exact definition refer to
5055 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-substring.
5056 *
5057 * @param[in] args Array of arguments.
5058 * @param[in] arg_count Count of elements in @p args.
5059 * @param[in,out] set Context and result set at the same time.
5060 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005061 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005062 */
5063static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005064xpath_substring(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005065{
Radek Krejci1deb5be2020-08-26 16:43:36 +02005066 int32_t start, len;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005067 uint16_t str_start, str_len, pos;
5068 struct lysc_node_leaf *sleaf;
5069 LY_ERR rc = LY_SUCCESS;
5070
5071 if (options & LYXP_SCNODE_ALL) {
5072 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5073 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5074 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 +02005075 } else if (!warn_is_string_type(sleaf->type)) {
5076 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005077 }
5078 }
5079
5080 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5081 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5082 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 +02005083 } else if (!warn_is_numeric_type(sleaf->type)) {
5084 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005085 }
5086 }
5087
Michal Vasko69730152020-10-09 16:30:07 +02005088 if ((arg_count == 3) && (args[2]->type == LYXP_SET_SCNODE_SET) &&
5089 (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005090 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5091 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 +02005092 } else if (!warn_is_numeric_type(sleaf->type)) {
5093 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005094 }
5095 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005096 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005097 return rc;
5098 }
5099
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005100 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005101 LY_CHECK_RET(rc);
5102
5103 /* start */
5104 if (xpath_round(&args[1], 1, args[1], options)) {
5105 return -1;
5106 }
5107 if (isfinite(args[1]->val.num)) {
5108 start = args[1]->val.num - 1;
5109 } else if (isinf(args[1]->val.num) && signbit(args[1]->val.num)) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02005110 start = INT32_MIN;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005111 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02005112 start = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005113 }
5114
5115 /* len */
5116 if (arg_count == 3) {
5117 rc = xpath_round(&args[2], 1, args[2], options);
5118 LY_CHECK_RET(rc);
Radek Krejci1deb5be2020-08-26 16:43:36 +02005119 if (isnan(args[2]->val.num) || signbit(args[2]->val.num)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005120 len = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005121 } else if (isfinite(args[2]->val.num)) {
5122 len = args[2]->val.num;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005123 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02005124 len = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005125 }
5126 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02005127 len = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005128 }
5129
5130 /* find matching character positions */
5131 str_start = 0;
5132 str_len = 0;
5133 for (pos = 0; args[0]->val.str[pos]; ++pos) {
5134 if (pos < start) {
5135 ++str_start;
5136 } else if (pos < start + len) {
5137 ++str_len;
5138 } else {
5139 break;
5140 }
5141 }
5142
5143 set_fill_string(set, args[0]->val.str + str_start, str_len);
5144 return LY_SUCCESS;
5145}
5146
5147/**
5148 * @brief Execute the XPath substring-after(string, string) function.
5149 * Returns LYXP_SET_STRING with the string succeeding the occurance
5150 * of the second argument in the first or an empty string.
5151 *
5152 * @param[in] args Array of arguments.
5153 * @param[in] arg_count Count of elements in @p args.
5154 * @param[in,out] set Context and result set at the same time.
5155 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005156 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005157 */
5158static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005159xpath_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 +02005160{
5161 char *ptr;
5162 struct lysc_node_leaf *sleaf;
5163 LY_ERR rc = LY_SUCCESS;
5164
5165 if (options & LYXP_SCNODE_ALL) {
5166 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5167 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5168 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 +02005169 } else if (!warn_is_string_type(sleaf->type)) {
5170 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005171 }
5172 }
5173
5174 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5175 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5176 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 +02005177 } else if (!warn_is_string_type(sleaf->type)) {
5178 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005179 }
5180 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005181 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005182 return rc;
5183 }
5184
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005185 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005186 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005187 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005188 LY_CHECK_RET(rc);
5189
5190 ptr = strstr(args[0]->val.str, args[1]->val.str);
5191 if (ptr) {
5192 set_fill_string(set, ptr + strlen(args[1]->val.str), strlen(ptr + strlen(args[1]->val.str)));
5193 } else {
5194 set_fill_string(set, "", 0);
5195 }
5196
5197 return LY_SUCCESS;
5198}
5199
5200/**
5201 * @brief Execute the XPath substring-before(string, string) function.
5202 * Returns LYXP_SET_STRING with the string preceding the occurance
5203 * of the second argument in the first or an empty string.
5204 *
5205 * @param[in] args Array of arguments.
5206 * @param[in] arg_count Count of elements in @p args.
5207 * @param[in,out] set Context and result set at the same time.
5208 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005209 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005210 */
5211static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005212xpath_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 +02005213{
5214 char *ptr;
5215 struct lysc_node_leaf *sleaf;
5216 LY_ERR rc = LY_SUCCESS;
5217
5218 if (options & LYXP_SCNODE_ALL) {
5219 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5220 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5221 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 +02005222 } else if (!warn_is_string_type(sleaf->type)) {
5223 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005224 }
5225 }
5226
5227 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5228 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5229 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 +02005230 } else if (!warn_is_string_type(sleaf->type)) {
5231 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005232 }
5233 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005234 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005235 return rc;
5236 }
5237
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005238 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005239 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005240 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005241 LY_CHECK_RET(rc);
5242
5243 ptr = strstr(args[0]->val.str, args[1]->val.str);
5244 if (ptr) {
5245 set_fill_string(set, args[0]->val.str, ptr - args[0]->val.str);
5246 } else {
5247 set_fill_string(set, "", 0);
5248 }
5249
5250 return LY_SUCCESS;
5251}
5252
5253/**
5254 * @brief Execute the XPath sum(node-set) function. Returns LYXP_SET_NUMBER
5255 * with the sum of all the nodes in the context.
5256 *
5257 * @param[in] args Array of arguments.
5258 * @param[in] arg_count Count of elements in @p args.
5259 * @param[in,out] set Context and result set at the same time.
5260 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005261 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005262 */
5263static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005264xpath_sum(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005265{
5266 long double num;
5267 char *str;
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01005268 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005269 struct lyxp_set set_item;
5270 struct lysc_node_leaf *sleaf;
5271 LY_ERR rc = LY_SUCCESS;
5272
5273 if (options & LYXP_SCNODE_ALL) {
5274 if (args[0]->type == LYXP_SET_SCNODE_SET) {
5275 for (i = 0; i < args[0]->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005276 if (args[0]->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005277 sleaf = (struct lysc_node_leaf *)args[0]->val.scnodes[i].scnode;
5278 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5279 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__,
Michal Vasko69730152020-10-09 16:30:07 +02005280 lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005281 } else if (!warn_is_numeric_type(sleaf->type)) {
5282 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005283 }
5284 }
5285 }
5286 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005287 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005288 return rc;
5289 }
5290
5291 set_fill_number(set, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005292
5293 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005294 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "sum(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02005295 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02005296 } else if (!args[0]->used) {
5297 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005298 }
5299
Michal Vasko5c4e5892019-11-14 12:31:38 +01005300 set_init(&set_item, set);
5301
Michal Vasko03ff5a72019-09-11 13:49:33 +02005302 set_item.type = LYXP_SET_NODE_SET;
Michal Vasko41decbf2021-11-02 11:50:21 +01005303 set_item.val.nodes = calloc(1, sizeof *set_item.val.nodes);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005304 LY_CHECK_ERR_RET(!set_item.val.nodes, LOGMEM(set->ctx), LY_EMEM);
5305
5306 set_item.used = 1;
5307 set_item.size = 1;
5308
5309 for (i = 0; i < args[0]->used; ++i) {
5310 set_item.val.nodes[0] = args[0]->val.nodes[i];
5311
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005312 rc = cast_node_set_to_string(&set_item, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005313 LY_CHECK_RET(rc);
5314 num = cast_string_to_number(str);
5315 free(str);
5316 set->val.num += num;
5317 }
5318
5319 free(set_item.val.nodes);
5320
5321 return LY_SUCCESS;
5322}
5323
5324/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02005325 * @brief Execute the XPath translate(string, string, string) function.
5326 * Returns LYXP_SET_STRING with the first argument with the characters
5327 * from the second argument replaced by those on the corresponding
5328 * positions in the third argument.
5329 *
5330 * @param[in] args Array of arguments.
5331 * @param[in] arg_count Count of elements in @p args.
5332 * @param[in,out] set Context and result set at the same time.
5333 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005334 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005335 */
5336static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005337xpath_translate(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005338{
5339 uint16_t i, j, new_used;
5340 char *new;
Radek Krejci857189e2020-09-01 13:26:36 +02005341 ly_bool have_removed;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005342 struct lysc_node_leaf *sleaf;
5343 LY_ERR rc = LY_SUCCESS;
5344
5345 if (options & LYXP_SCNODE_ALL) {
5346 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5347 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5348 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 +02005349 } else if (!warn_is_string_type(sleaf->type)) {
5350 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005351 }
5352 }
5353
5354 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5355 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5356 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 +02005357 } else if (!warn_is_string_type(sleaf->type)) {
5358 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005359 }
5360 }
5361
5362 if ((args[2]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
5363 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5364 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 +02005365 } else if (!warn_is_string_type(sleaf->type)) {
5366 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005367 }
5368 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005369 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005370 return rc;
5371 }
5372
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005373 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005374 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005375 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005376 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005377 rc = lyxp_set_cast(args[2], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005378 LY_CHECK_RET(rc);
5379
5380 new = malloc((strlen(args[0]->val.str) + 1) * sizeof(char));
5381 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5382 new_used = 0;
5383
5384 have_removed = 0;
5385 for (i = 0; args[0]->val.str[i]; ++i) {
Radek Krejci857189e2020-09-01 13:26:36 +02005386 ly_bool found = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005387
5388 for (j = 0; args[1]->val.str[j]; ++j) {
5389 if (args[0]->val.str[i] == args[1]->val.str[j]) {
5390 /* removing this char */
5391 if (j >= strlen(args[2]->val.str)) {
5392 have_removed = 1;
5393 found = 1;
5394 break;
5395 }
5396 /* replacing this char */
5397 new[new_used] = args[2]->val.str[j];
5398 ++new_used;
5399 found = 1;
5400 break;
5401 }
5402 }
5403
5404 /* copying this char */
5405 if (!found) {
5406 new[new_used] = args[0]->val.str[i];
5407 ++new_used;
5408 }
5409 }
5410
5411 if (have_removed) {
5412 new = ly_realloc(new, (new_used + 1) * sizeof(char));
5413 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5414 }
5415 new[new_used] = '\0';
5416
Michal Vaskod3678892020-05-21 10:06:58 +02005417 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005418 set->type = LYXP_SET_STRING;
5419 set->val.str = new;
5420
5421 return LY_SUCCESS;
5422}
5423
5424/**
5425 * @brief Execute the XPath true() function. Returns LYXP_SET_BOOLEAN
5426 * with true value.
5427 *
5428 * @param[in] args Array of arguments.
5429 * @param[in] arg_count Count of elements in @p args.
5430 * @param[in,out] set Context and result set at the same time.
5431 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005432 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005433 */
5434static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005435xpath_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 +02005436{
5437 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02005438 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005439 return LY_SUCCESS;
5440 }
5441
5442 set_fill_boolean(set, 1);
5443 return LY_SUCCESS;
5444}
5445
Michal Vasko03ff5a72019-09-11 13:49:33 +02005446/**
Michal Vasko49fec8e2022-05-24 10:28:33 +02005447 * @brief Execute the XPath node() processing instruction (node type). Returns LYXP_SET_NODE_SET
5448 * with only nodes from the context.
5449 *
5450 * @param[in,out] set Context and result set at the same time.
5451 * @param[in] axis Axis to search on.
5452 * @param[in] options XPath options.
5453 * @return LY_ERR
5454 */
5455static LY_ERR
5456xpath_pi_node(struct lyxp_set *set, enum lyxp_axis axis, uint32_t options)
5457{
5458 if (options & LYXP_SCNODE_ALL) {
5459 return moveto_scnode(set, NULL, NULL, axis, options);
5460 }
5461
5462 if (set->type != LYXP_SET_NODE_SET) {
5463 lyxp_set_free_content(set);
5464 return LY_SUCCESS;
5465 }
5466
5467 /* just like moving to a node with no restrictions */
5468 return moveto_node(set, NULL, NULL, axis, options);
5469}
5470
5471/**
5472 * @brief Execute the XPath text() processing instruction (node type). Returns LYXP_SET_NODE_SET
5473 * with the text content of the nodes in the context.
5474 *
5475 * @param[in,out] set Context and result set at the same time.
5476 * @param[in] axis Axis to search on.
5477 * @param[in] options XPath options.
5478 * @return LY_ERR
5479 */
5480static LY_ERR
5481xpath_pi_text(struct lyxp_set *set, enum lyxp_axis axis, uint32_t options)
5482{
5483 uint32_t i;
5484
5485 if (options & LYXP_SCNODE_ALL) {
5486 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
5487 return LY_SUCCESS;
5488 }
5489
5490 if (set->type != LYXP_SET_NODE_SET) {
5491 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "text()");
5492 return LY_EVALID;
5493 }
5494
5495 if (axis != LYXP_AXIS_CHILD) {
5496 /* even following and preceding axescan return text nodes, but whatever */
5497 lyxp_set_free_content(set);
5498 return LY_SUCCESS;
5499 }
5500
5501 for (i = 0; i < set->used; ++i) {
5502 switch (set->val.nodes[i].type) {
5503 case LYXP_NODE_NONE:
5504 LOGINT_RET(set->ctx);
5505 case LYXP_NODE_ELEM:
5506 if (set->val.nodes[i].node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
5507 set->val.nodes[i].type = LYXP_NODE_TEXT;
5508 break;
5509 }
5510 /* fall through */
5511 case LYXP_NODE_ROOT:
5512 case LYXP_NODE_ROOT_CONFIG:
5513 case LYXP_NODE_TEXT:
5514 case LYXP_NODE_META:
5515 set_remove_node_none(set, i);
5516 break;
5517 }
5518 }
5519 set_remove_nodes_none(set);
5520
5521 return LY_SUCCESS;
5522}
5523
5524/**
Michal Vasko6346ece2019-09-24 13:12:53 +02005525 * @brief Skip prefix and return corresponding model if there is a prefix. Logs directly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005526 *
Michal Vasko2104e9f2020-03-06 08:23:25 +01005527 * XPath @p set is expected to be a (sc)node set!
5528 *
Michal Vasko6346ece2019-09-24 13:12:53 +02005529 * @param[in,out] qname Qualified node name. If includes prefix, it is skipped.
5530 * @param[in,out] qname_len Length of @p qname, is updated accordingly.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005531 * @param[in] set Set with general XPath context.
5532 * @param[in] ctx_scnode Context node to inherit module for unprefixed node for ::LY_PREF_JSON.
Michal Vasko6346ece2019-09-24 13:12:53 +02005533 * @param[out] moveto_mod Expected module of a matching node.
5534 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005535 */
Michal Vasko6346ece2019-09-24 13:12:53 +02005536static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005537moveto_resolve_model(const char **qname, uint16_t *qname_len, const struct lyxp_set *set,
5538 const struct lysc_node *ctx_scnode, const struct lys_module **moveto_mod)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005539{
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02005540 const struct lys_module *mod = NULL;
Michal Vasko6346ece2019-09-24 13:12:53 +02005541 const char *ptr;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005542 size_t pref_len;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005543
Michal Vasko2104e9f2020-03-06 08:23:25 +01005544 assert((set->type == LYXP_SET_NODE_SET) || (set->type == LYXP_SET_SCNODE_SET));
5545
Michal Vasko6346ece2019-09-24 13:12:53 +02005546 if ((ptr = ly_strnchr(*qname, ':', *qname_len))) {
5547 /* specific module */
5548 pref_len = ptr - *qname;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005549 mod = ly_resolve_prefix(set->ctx, *qname, pref_len, set->format, set->prefix_data);
Michal Vasko6346ece2019-09-24 13:12:53 +02005550
Michal Vasko004d3152020-06-11 19:59:22 +02005551 /* check for errors and non-implemented modules, as they are not valid */
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005552 if (!mod || !mod->implemented) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005553 LOGVAL(set->ctx, LY_VCODE_XP_INMOD, pref_len, *qname);
Michal Vasko6346ece2019-09-24 13:12:53 +02005554 return LY_EVALID;
5555 }
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005556
Michal Vasko6346ece2019-09-24 13:12:53 +02005557 *qname += pref_len + 1;
5558 *qname_len -= pref_len + 1;
5559 } else if (((*qname)[0] == '*') && (*qname_len == 1)) {
5560 /* all modules - special case */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005561 mod = NULL;
Michal Vasko6346ece2019-09-24 13:12:53 +02005562 } else {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005563 switch (set->format) {
Radek Krejci8df109d2021-04-23 12:19:08 +02005564 case LY_VALUE_SCHEMA:
5565 case LY_VALUE_SCHEMA_RESOLVED:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005566 /* current module */
5567 mod = set->cur_mod;
5568 break;
Radek Krejci224d4b42021-04-23 13:54:59 +02005569 case LY_VALUE_CANON:
Radek Krejci8df109d2021-04-23 12:19:08 +02005570 case LY_VALUE_JSON:
Radek Krejcif9943642021-04-26 10:18:21 +02005571 case LY_VALUE_LYB:
Michal Vaskoddd76592022-01-17 13:34:48 +01005572 case LY_VALUE_STR_NS:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005573 /* inherit parent (context node) module */
5574 if (ctx_scnode) {
5575 mod = ctx_scnode->module;
5576 } else {
5577 mod = NULL;
5578 }
5579 break;
Radek Krejci8df109d2021-04-23 12:19:08 +02005580 case LY_VALUE_XML:
Michal Vasko52143d12021-04-14 15:36:39 +02005581 /* all nodes need to be prefixed */
5582 LOGVAL(set->ctx, LYVE_DATA, "Non-prefixed node \"%.*s\" in XML xpath found.", *qname_len, *qname);
5583 return LY_EVALID;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005584 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005585 }
5586
Michal Vasko6346ece2019-09-24 13:12:53 +02005587 *moveto_mod = mod;
5588 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005589}
5590
5591/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02005592 * @brief Move context @p set to the root. Handles absolute path.
5593 * Result is LYXP_SET_NODE_SET.
5594 *
5595 * @param[in,out] set Set to use.
5596 * @param[in] options Xpath options.
Michal Vaskob0099a92020-08-31 14:55:23 +02005597 * @return LY_ERR value.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005598 */
Michal Vaskob0099a92020-08-31 14:55:23 +02005599static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005600moveto_root(struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005601{
aPiecek8b0cc152021-05-31 16:40:31 +02005602 assert(!(options & LYXP_SKIP_EXPR));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005603
5604 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02005605 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskob0099a92020-08-31 14:55:23 +02005606 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, NULL, set->root_type, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005607 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005608 set->type = LYXP_SET_NODE_SET;
5609 set->used = 0;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005610 set_insert_node(set, NULL, 0, set->root_type, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005611 }
Michal Vaskob0099a92020-08-31 14:55:23 +02005612
5613 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005614}
5615
5616/**
5617 * @brief Check @p node as a part of NameTest processing.
5618 *
5619 * @param[in] node Node to check.
Michal Vasko49fec8e2022-05-24 10:28:33 +02005620 * @param[in] node_type Node type of @p node.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005621 * @param[in] set Set to read general context from.
Michal Vaskod3678892020-05-21 10:06:58 +02005622 * @param[in] node_name Node name in the dictionary to move to, NULL for any node.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005623 * @param[in] moveto_mod Expected module of the node, NULL for no prefix.
Michal Vaskocdad7122020-11-09 21:04:44 +01005624 * @param[in] options XPath options.
Michal Vasko6346ece2019-09-24 13:12:53 +02005625 * @return LY_ERR (LY_ENOT if node does not match, LY_EINCOMPLETE on unresolved when,
Michal Vaskocd2c88a2022-06-07 10:54:34 +02005626 * LY_EINVAL if neither node nor any children match)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005627 */
5628static LY_ERR
Michal Vasko49fec8e2022-05-24 10:28:33 +02005629moveto_node_check(const struct lyd_node *node, enum lyxp_node_type node_type, const struct lyxp_set *set,
5630 const char *node_name, const struct lys_module *moveto_mod, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005631{
Michal Vasko49fec8e2022-05-24 10:28:33 +02005632 if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT)) {
5633 assert(node_type == set->root_type);
5634
5635 if (node_name || moveto_mod) {
5636 /* root will not match a specific node */
5637 return LY_ENOT;
5638 }
5639 return LY_SUCCESS;
5640 } else if (node_type != LYXP_NODE_ELEM) {
5641 /* other types will not match */
5642 return LY_ENOT;
5643 }
5644
Michal Vaskodca9f122021-07-16 13:56:22 +02005645 if (!node->schema) {
5646 /* opaque node never matches */
5647 return LY_ENOT;
5648 }
5649
Michal Vasko03ff5a72019-09-11 13:49:33 +02005650 /* module check */
Michal Vasko19089f02022-06-07 11:02:11 +02005651 if (moveto_mod) {
5652 if (!(node->flags & LYD_EXT) && (node->schema->module != moveto_mod)) {
5653 return LY_ENOT;
5654 } else if ((node->flags & LYD_EXT) && strcmp(node->schema->module->name, moveto_mod->name)) {
5655 return LY_ENOT;
5656 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005657 }
5658
Michal Vasko5c4e5892019-11-14 12:31:38 +01005659 /* context check */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005660 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (node->schema->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005661 return LY_EINVAL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005662 } else if (set->context_op && (node->schema->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) &&
5663 (node->schema != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005664 return LY_EINVAL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005665 }
5666
5667 /* name check */
Michal Vasko19089f02022-06-07 11:02:11 +02005668 if (node_name) {
5669 if (!(node->flags & LYD_EXT) && (node->schema->name != node_name)) {
5670 return LY_ENOT;
5671 } else if ((node->flags & LYD_EXT) && strcmp(node->schema->name, node_name)) {
5672 return LY_ENOT;
5673 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005674 }
5675
Michal Vaskoa1424542019-11-14 16:08:52 +01005676 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01005677 if (!(options & LYXP_IGNORE_WHEN) && lysc_has_when(node->schema) && !(node->flags & LYD_WHEN_TRUE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005678 return LY_EINCOMPLETE;
Michal Vaskoa1424542019-11-14 16:08:52 +01005679 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005680
5681 /* match */
5682 return LY_SUCCESS;
5683}
5684
5685/**
Michal Vasko49fec8e2022-05-24 10:28:33 +02005686 * @brief Get the next node in a forward DFS.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005687 *
Michal Vasko49fec8e2022-05-24 10:28:33 +02005688 * @param[in] iter Last returned node.
5689 * @param[in] stop Node to stop the search on and not return.
5690 * @return Next node, NULL if there are no more.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005691 */
Michal Vasko49fec8e2022-05-24 10:28:33 +02005692static const struct lyd_node *
5693moveto_axis_node_next_dfs_forward(const struct lyd_node *iter, const struct lyd_node *stop)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005694{
Michal Vasko49fec8e2022-05-24 10:28:33 +02005695 const struct lyd_node *next = NULL;
5696
5697 /* 1) child */
5698 next = lyd_child(iter);
5699 if (!next) {
5700 if (iter == stop) {
5701 /* reached stop, no more descendants */
5702 return NULL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005703 }
Michal Vasko49fec8e2022-05-24 10:28:33 +02005704 /* 2) child next sibling */
5705 next = iter->next;
5706 }
5707 while (!next) {
5708 iter = lyd_parent(iter);
5709 if ((!stop && !iter) || (stop && (lyd_parent(iter) == lyd_parent(stop)))) {
5710 return NULL;
5711 }
5712 next = iter->next;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005713 }
5714
Michal Vasko49fec8e2022-05-24 10:28:33 +02005715 return next;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005716}
5717
5718/**
Michal Vasko49fec8e2022-05-24 10:28:33 +02005719 * @brief Get the next node in a backward DFS.
5720 *
5721 * @param[in] iter Last returned node.
5722 * @param[in] stop Node to stop the search on and not return.
5723 * @return Next node, NULL if there are no more.
5724 */
5725static const struct lyd_node *
5726moveto_axis_node_next_dfs_backward(const struct lyd_node *iter, const struct lyd_node *stop)
5727{
5728 const struct lyd_node *next = NULL;
5729
5730 /* 1) previous sibling innermost last child */
5731 next = iter->prev->next ? iter->prev : NULL;
5732 while (next && lyd_child(next)) {
5733 next = lyd_child(next);
5734 next = next->prev;
5735 }
5736
5737 if (!next) {
5738 /* 2) parent */
5739 iter = lyd_parent(iter);
5740 if ((!stop && !iter) || (stop && (lyd_parent(iter) == lyd_parent(stop)))) {
5741 return NULL;
5742 }
5743 next = iter;
5744 }
5745
5746 return next;
5747}
5748
5749/**
5750 * @brief Get the first node on an axis for a context node.
5751 *
5752 * @param[in,out] iter NULL, updated to the next node.
5753 * @param[in,out] iter_type Node type 0 of @p iter, updated to the node type of the next node.
5754 * @param[in] node Context node.
5755 * @param[in] node_type Type of @p node.
5756 * @param[in] axis Axis to use.
5757 * @param[in] set XPath set with the general context.
5758 * @return LY_SUCCESS on success.
5759 * @return LY_ENOTFOUND if no next node found.
5760 */
5761static LY_ERR
5762moveto_axis_node_next_first(const struct lyd_node **iter, enum lyxp_node_type *iter_type, const struct lyd_node *node,
5763 enum lyxp_node_type node_type, enum lyxp_axis axis, struct lyxp_set *set)
5764{
5765 const struct lyd_node *next = NULL;
5766 enum lyxp_node_type next_type = 0;
5767
5768 assert(!*iter);
5769 assert(!*iter_type);
5770
5771 switch (axis) {
5772 case LYXP_AXIS_ANCESTOR_OR_SELF:
5773 case LYXP_AXIS_DESCENDANT_OR_SELF:
5774 case LYXP_AXIS_SELF:
5775 /* return the context node */
5776 next = node;
5777 next_type = node_type;
5778 break;
5779
5780 case LYXP_AXIS_ANCESTOR:
5781 case LYXP_AXIS_PARENT:
5782 if (node_type == LYXP_NODE_ELEM) {
5783 next = lyd_parent(node);
5784 next_type = next ? LYXP_NODE_ELEM : set->root_type;
5785 } else if (node_type == LYXP_NODE_TEXT) {
5786 next = node;
5787 next_type = LYXP_NODE_ELEM;
5788 } else if (node_type == LYXP_NODE_META) {
5789 next = ((struct lyd_meta *)node)->parent;
5790 next_type = LYXP_NODE_ELEM;
5791 } /* else root does not have a parent */
5792 break;
5793
5794 case LYXP_AXIS_CHILD:
5795 if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT)) {
5796 assert(!node);
5797
5798 /* search in all the trees */
5799 next = set->tree;
5800 next_type = next ? LYXP_NODE_ELEM : 0;
5801 } else {
5802 /* search in children */
5803 next = lyd_child(node);
5804 next_type = next ? LYXP_NODE_ELEM : 0;
5805 }
5806 break;
5807
5808 case LYXP_AXIS_DESCENDANT:
5809 if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT)) {
5810 /* top-level nodes */
5811 next = set->tree;
5812 next_type = LYXP_NODE_ELEM;
5813 } else if (node_type == LYXP_NODE_ELEM) {
5814 /* start from the context node */
5815 next = moveto_axis_node_next_dfs_forward(node, node);
5816 next_type = next ? LYXP_NODE_ELEM : 0;
5817 } /* else no children */
5818 break;
5819
5820 case LYXP_AXIS_FOLLOWING:
5821 case LYXP_AXIS_FOLLOWING_SIBLING:
5822 if (node_type == LYXP_NODE_ELEM) {
5823 /* first next sibling */
5824 next = node->next;
5825 next_type = next ? LYXP_NODE_ELEM : 0;
5826 } /* else no sibling */
5827 break;
5828
5829 case LYXP_AXIS_PRECEDING:
5830 if ((node_type == LYXP_NODE_ELEM) && node->prev->next) {
5831 /* skip ancestors */
5832 next = moveto_axis_node_next_dfs_backward(node, NULL);
5833 assert(next);
5834 next_type = LYXP_NODE_ELEM;
5835 } /* else no sibling */
5836 break;
5837
5838 case LYXP_AXIS_PRECEDING_SIBLING:
5839 if (node_type == LYXP_NODE_ELEM) {
5840 /* first previous sibling */
5841 next = node->prev->next ? node->prev : NULL;
5842 next_type = next ? LYXP_NODE_ELEM : 0;
5843 } /* else no sibling */
5844 break;
5845
5846 case LYXP_AXIS_ATTRIBUTE:
5847 /* handled specially */
5848 assert(0);
5849 LOGINT(set->ctx);
5850 break;
5851 }
5852
5853 *iter = next;
5854 *iter_type = next_type;
5855 return next_type ? LY_SUCCESS : LY_ENOTFOUND;
5856}
5857
5858/**
5859 * @brief Iterate over all nodes on an axis for a context node.
5860 *
5861 * @param[in,out] iter Last returned node, start with NULL, updated to the next node.
5862 * @param[in,out] iter_type Node type of @p iter, start with 0, updated to the node type of the next node.
5863 * @param[in] node Context node.
5864 * @param[in] node_type Type of @p node.
5865 * @param[in] axis Axis to use.
5866 * @param[in] set XPath set with the general context.
5867 * @return LY_SUCCESS on success.
5868 * @return LY_ENOTFOUND if no next node found.
5869 */
5870static LY_ERR
5871moveto_axis_node_next(const struct lyd_node **iter, enum lyxp_node_type *iter_type, const struct lyd_node *node,
5872 enum lyxp_node_type node_type, enum lyxp_axis axis, struct lyxp_set *set)
5873{
5874 const struct lyd_node *next = NULL;
5875 enum lyxp_node_type next_type = 0;
5876
5877 if (!*iter_type) {
5878 /* first returned node */
5879 return moveto_axis_node_next_first(iter, iter_type, node, node_type, axis, set);
5880 }
5881
5882 switch (axis) {
5883 case LYXP_AXIS_ANCESTOR_OR_SELF:
5884 if ((*iter == node) && (*iter_type == node_type)) {
5885 /* fake first ancestor, we returned self before */
5886 *iter = NULL;
5887 *iter_type = 0;
5888 return moveto_axis_node_next_first(iter, iter_type, node, node_type, LYXP_AXIS_ANCESTOR, set);
5889 } /* else continue ancestor */
5890
5891 /* fallthrough */
5892 case LYXP_AXIS_ANCESTOR:
5893 if (*iter_type == LYXP_NODE_ELEM) {
5894 /* iter parent */
5895 next = lyd_parent(*iter);
5896 next_type = next ? LYXP_NODE_ELEM : set->root_type;
5897 } /* else root, no ancestors */
5898 break;
5899
5900 case LYXP_AXIS_CHILD:
5901 assert(*iter_type == LYXP_NODE_ELEM);
5902
5903 /* next sibling (child) */
5904 next = (*iter)->next;
5905 next_type = next ? LYXP_NODE_ELEM : 0;
5906 break;
5907
5908 case LYXP_AXIS_DESCENDANT_OR_SELF:
5909 if ((*iter == node) && (*iter_type == node_type)) {
5910 /* fake first descendant, we returned self before */
5911 *iter = NULL;
5912 *iter_type = 0;
5913 return moveto_axis_node_next_first(iter, iter_type, node, node_type, LYXP_AXIS_DESCENDANT, set);
5914 } /* else continue descendant */
5915
5916 /* fallthrough */
5917 case LYXP_AXIS_DESCENDANT:
5918 assert(*iter_type == LYXP_NODE_ELEM);
5919 next = moveto_axis_node_next_dfs_forward(*iter, node);
5920 next_type = next ? LYXP_NODE_ELEM : 0;
5921 break;
5922
5923 case LYXP_AXIS_FOLLOWING:
5924 assert(*iter_type == LYXP_NODE_ELEM);
5925 next = moveto_axis_node_next_dfs_forward(*iter, NULL);
5926 next_type = next ? LYXP_NODE_ELEM : 0;
5927 break;
5928
5929 case LYXP_AXIS_FOLLOWING_SIBLING:
5930 assert(*iter_type == LYXP_NODE_ELEM);
5931
5932 /* next sibling */
5933 next = (*iter)->next;
5934 next_type = next ? LYXP_NODE_ELEM : 0;
5935 break;
5936
5937 case LYXP_AXIS_PARENT:
5938 case LYXP_AXIS_SELF:
5939 /* parent/self was returned before */
5940 break;
5941
5942 case LYXP_AXIS_PRECEDING:
5943 assert(*iter_type == LYXP_NODE_ELEM);
5944 next = moveto_axis_node_next_dfs_backward(*iter, NULL);
5945 next_type = next ? LYXP_NODE_ELEM : 0;
5946 break;
5947
5948 case LYXP_AXIS_PRECEDING_SIBLING:
5949 assert(*iter_type == LYXP_NODE_ELEM);
5950
5951 /* previous sibling */
5952 next = (*iter)->prev->next ? (*iter)->prev : NULL;
5953 next_type = next ? LYXP_NODE_ELEM : 0;
5954 break;
5955
5956 case LYXP_AXIS_ATTRIBUTE:
5957 /* handled specially */
5958 assert(0);
5959 LOGINT(set->ctx);
5960 break;
5961 }
5962
5963 *iter = next;
5964 *iter_type = next_type;
5965 return next_type ? LY_SUCCESS : LY_ENOTFOUND;
5966}
5967
5968/**
5969 * @brief Move context @p set to a node. Result is LYXP_SET_NODE_SET. Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005970 *
5971 * @param[in,out] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005972 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005973 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko49fec8e2022-05-24 10:28:33 +02005974 * @param[in] axis Axis to search on.
Michal Vaskocdad7122020-11-09 21:04:44 +01005975 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005976 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5977 */
5978static LY_ERR
Michal Vasko49fec8e2022-05-24 10:28:33 +02005979moveto_node(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, enum lyxp_axis axis,
5980 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005981{
Michal Vasko230cf972021-12-02 12:31:00 +01005982 LY_ERR r, rc = LY_SUCCESS;
Michal Vasko49fec8e2022-05-24 10:28:33 +02005983 const struct lyd_node *iter;
5984 enum lyxp_node_type iter_type;
Michal Vasko230cf972021-12-02 12:31:00 +01005985 struct lyxp_set result;
Michal Vasko49fec8e2022-05-24 10:28:33 +02005986 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005987
aPiecek8b0cc152021-05-31 16:40:31 +02005988 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005989 return LY_SUCCESS;
5990 }
5991
5992 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005993 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005994 return LY_EVALID;
5995 }
5996
Michal Vasko230cf972021-12-02 12:31:00 +01005997 /* init result set */
5998 set_init(&result, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005999
Michal Vasko49fec8e2022-05-24 10:28:33 +02006000 for (i = 0; i < set->used; ++i) {
6001 /* iterate over all the nodes on the axis of the node */
6002 iter = NULL;
6003 iter_type = 0;
6004 while (!moveto_axis_node_next(&iter, &iter_type, set->val.nodes[i].node, set->val.nodes[i].type, axis, set)) {
6005 r = moveto_node_check(iter, iter_type, set, ncname, moveto_mod, options);
6006 if (r == LY_EINCOMPLETE) {
Michal Vasko230cf972021-12-02 12:31:00 +01006007 rc = r;
6008 goto cleanup;
Michal Vasko49fec8e2022-05-24 10:28:33 +02006009 } else if (r) {
6010 continue;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006011 }
Michal Vasko49fec8e2022-05-24 10:28:33 +02006012
6013 /* check for duplicates if they are possible */
6014 switch (axis) {
6015 case LYXP_AXIS_ANCESTOR:
6016 case LYXP_AXIS_ANCESTOR_OR_SELF:
6017 case LYXP_AXIS_DESCENDANT:
6018 case LYXP_AXIS_DESCENDANT_OR_SELF:
6019 case LYXP_AXIS_FOLLOWING:
6020 case LYXP_AXIS_FOLLOWING_SIBLING:
6021 case LYXP_AXIS_PARENT:
6022 case LYXP_AXIS_PRECEDING:
6023 case LYXP_AXIS_PRECEDING_SIBLING:
6024 if (set_dup_node_check(&result, iter, iter_type, -1)) {
6025 continue;
6026 }
6027 break;
6028 case LYXP_AXIS_CHILD:
6029 case LYXP_AXIS_SELF:
6030 break;
6031 case LYXP_AXIS_ATTRIBUTE:
6032 /* handled specially */
6033 assert(0);
6034 LOGINT(set->ctx);
6035 break;
6036 }
6037
6038 /* matching node */
6039 set_insert_node(&result, iter, 0, iter_type, result.used);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006040 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006041 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006042
Michal Vasko230cf972021-12-02 12:31:00 +01006043 /* move result to the set */
6044 lyxp_set_free_content(set);
6045 *set = result;
6046 result.type = LYXP_SET_NUMBER;
Michal Vasko49fec8e2022-05-24 10:28:33 +02006047
6048 /* sort the final set */
6049 set_sort(set);
Michal Vasko230cf972021-12-02 12:31:00 +01006050
6051cleanup:
6052 lyxp_set_free_content(&result);
6053 return rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006054}
6055
6056/**
Michal Vasko49fec8e2022-05-24 10:28:33 +02006057 * @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 +02006058 *
6059 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02006060 * @param[in] scnode Matching node schema.
Michal Vasko004d3152020-06-11 19:59:22 +02006061 * @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 +01006062 * @param[in] options XPath options.
Michal Vaskod3678892020-05-21 10:06:58 +02006063 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6064 */
6065static LY_ERR
Michal Vasko49fec8e2022-05-24 10:28:33 +02006066moveto_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 +01006067 uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02006068{
Michal Vaskoddd76592022-01-17 13:34:48 +01006069 LY_ERR ret = LY_SUCCESS, r;
Michal Vaskod3678892020-05-21 10:06:58 +02006070 uint32_t i;
Michal Vaskod3678892020-05-21 10:06:58 +02006071 const struct lyd_node *siblings;
Michal Vasko230cf972021-12-02 12:31:00 +01006072 struct lyxp_set result;
Michal Vasko004d3152020-06-11 19:59:22 +02006073 struct lyd_node *sub, *inst = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02006074
Michal Vasko004d3152020-06-11 19:59:22 +02006075 assert(scnode && (!(scnode->nodetype & (LYS_LIST | LYS_LEAFLIST)) || predicates));
Michal Vaskod3678892020-05-21 10:06:58 +02006076
Michal Vasko50aaa072021-12-02 13:11:56 +01006077 /* init result set */
6078 set_init(&result, set);
6079
aPiecek8b0cc152021-05-31 16:40:31 +02006080 if (options & LYXP_SKIP_EXPR) {
Michal Vasko004d3152020-06-11 19:59:22 +02006081 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02006082 }
6083
6084 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006085 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko004d3152020-06-11 19:59:22 +02006086 ret = LY_EVALID;
6087 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02006088 }
6089
6090 /* context check for all the nodes since we have the schema node */
6091 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) {
6092 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02006093 goto cleanup;
Michal Vasko69730152020-10-09 16:30:07 +02006094 } else if (set->context_op && (scnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) &&
6095 (scnode != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02006096 lyxp_set_free_content(set);
6097 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +02006098 }
6099
6100 /* create specific data instance if needed */
6101 if (scnode->nodetype == LYS_LIST) {
6102 LY_CHECK_GOTO(ret = lyd_create_list(scnode, predicates, &inst), cleanup);
6103 } else if (scnode->nodetype == LYS_LEAFLIST) {
6104 LY_CHECK_GOTO(ret = lyd_create_term2(scnode, &predicates[0].value, &inst), cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02006105 }
6106
Michal Vasko230cf972021-12-02 12:31:00 +01006107 for (i = 0; i < set->used; ++i) {
Michal Vaskod3678892020-05-21 10:06:58 +02006108 siblings = NULL;
6109
6110 if ((set->val.nodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.nodes[i].type == LYXP_NODE_ROOT)) {
6111 assert(!set->val.nodes[i].node);
6112
6113 /* search in all the trees */
6114 siblings = set->tree;
6115 } else if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
6116 /* search in children */
Radek Krejcia1c1e542020-09-29 16:06:52 +02006117 siblings = lyd_child(set->val.nodes[i].node);
Michal Vaskod3678892020-05-21 10:06:58 +02006118 }
6119
6120 /* find the node using hashes */
Michal Vasko004d3152020-06-11 19:59:22 +02006121 if (inst) {
Michal Vaskoddd76592022-01-17 13:34:48 +01006122 r = lyd_find_sibling_first(siblings, inst, &sub);
Michal Vaskod3678892020-05-21 10:06:58 +02006123 } else {
Michal Vaskoddd76592022-01-17 13:34:48 +01006124 r = lyd_find_sibling_val(siblings, scnode, NULL, 0, &sub);
Michal Vaskod3678892020-05-21 10:06:58 +02006125 }
Michal Vaskoddd76592022-01-17 13:34:48 +01006126 LY_CHECK_ERR_GOTO(r && (r != LY_ENOTFOUND), ret = r, cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02006127
6128 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01006129 if (!(options & LYXP_IGNORE_WHEN) && sub && lysc_has_when(sub->schema) && !(sub->flags & LYD_WHEN_TRUE)) {
Michal Vasko004d3152020-06-11 19:59:22 +02006130 ret = LY_EINCOMPLETE;
6131 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02006132 }
6133
6134 if (sub) {
6135 /* pos filled later */
Michal Vasko230cf972021-12-02 12:31:00 +01006136 set_insert_node(&result, sub, 0, LYXP_NODE_ELEM, result.used);
Michal Vaskod3678892020-05-21 10:06:58 +02006137 }
6138 }
6139
Michal Vasko230cf972021-12-02 12:31:00 +01006140 /* move result to the set */
6141 lyxp_set_free_content(set);
6142 *set = result;
6143 result.type = LYXP_SET_NUMBER;
6144 assert(!set_sort(set));
6145
Michal Vasko004d3152020-06-11 19:59:22 +02006146cleanup:
Michal Vasko230cf972021-12-02 12:31:00 +01006147 lyxp_set_free_content(&result);
Michal Vasko004d3152020-06-11 19:59:22 +02006148 lyd_free_tree(inst);
6149 return ret;
Michal Vaskod3678892020-05-21 10:06:58 +02006150}
6151
6152/**
Michal Vasko49fec8e2022-05-24 10:28:33 +02006153 * @brief Check @p node as a part of schema NameTest processing.
6154 *
6155 * @param[in] node Schema node to check.
6156 * @param[in] ctx_scnode Context node.
6157 * @param[in] set Set to read general context from.
6158 * @param[in] node_name Node name in the dictionary to move to, NULL for any nodes.
6159 * @param[in] moveto_mod Expected module of the node, NULL for no prefix.
6160 * @return LY_ERR (LY_ENOT if node does not match, LY_EINVAL if neither node nor any children match)
6161 */
6162static LY_ERR
6163moveto_scnode_check(const struct lysc_node *node, const struct lysc_node *ctx_scnode, const struct lyxp_set *set,
6164 const char *node_name, const struct lys_module *moveto_mod)
6165{
6166 if (!moveto_mod && node_name) {
6167 switch (set->format) {
6168 case LY_VALUE_SCHEMA:
6169 case LY_VALUE_SCHEMA_RESOLVED:
6170 /* use current module */
6171 moveto_mod = set->cur_mod;
6172 break;
6173 case LY_VALUE_JSON:
6174 case LY_VALUE_LYB:
6175 case LY_VALUE_STR_NS:
6176 /* inherit module of the context node, if any */
6177 if (ctx_scnode) {
6178 moveto_mod = ctx_scnode->module;
6179 }
6180 break;
6181 case LY_VALUE_CANON:
6182 case LY_VALUE_XML:
6183 /* not defined */
6184 LOGINT(set->ctx);
6185 return LY_EINVAL;
6186 }
6187 }
6188
6189 if (!node) {
6190 /* root will not match a specific node */
6191 if (node_name || moveto_mod) {
6192 return LY_ENOT;
6193 }
6194 return LY_SUCCESS;
6195 }
6196
6197 /* module check */
6198 if (moveto_mod && (node->module != moveto_mod)) {
6199 return LY_ENOT;
6200 }
6201
6202 /* context check */
6203 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (node->flags & LYS_CONFIG_R)) {
6204 return LY_EINVAL;
6205 } else if (set->context_op && (node->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && (node != set->context_op)) {
6206 return LY_EINVAL;
6207 }
6208
6209 /* name check */
6210 if (node_name && (node->name != node_name)) {
6211 return LY_ENOT;
6212 }
6213
6214 /* match */
6215 return LY_SUCCESS;
6216}
6217
6218/**
6219 * @brief Get the next node in a forward schema node DFS.
6220 *
6221 * @param[in] iter Last returned node.
6222 * @param[in] stop Node to stop the search on and not return.
6223 * @param[in] getnext_opts Options for ::lys_getnext().
6224 * @return Next node, NULL if there are no more.
6225 */
6226static const struct lysc_node *
6227moveto_axis_scnode_next_dfs_forward(const struct lysc_node *iter, const struct lysc_node *stop, uint32_t getnext_opts)
6228{
6229 const struct lysc_node *next = NULL;
6230
6231 next = lysc_node_child(iter);
6232 if (!next) {
6233 /* no children, try siblings */
6234 if (iter == stop) {
6235 /* we are done, no next element to process */
6236 return NULL;
6237 }
6238
6239 next = lys_getnext(iter, lysc_data_parent(iter), NULL, getnext_opts);
6240 }
6241 while (!next && iter) {
6242 /* parent is already processed, go to its sibling */
6243 iter = iter->parent;
6244 if (iter == stop) {
6245 /* we are done, no next element to process */
6246 return NULL;
6247 }
6248 next = lys_getnext(iter, lysc_data_parent(iter), NULL, getnext_opts);
6249 }
6250
6251 return next;
6252}
6253
6254/**
6255 * @brief Consider schema node based on its in_ctx enum value.
6256 *
6257 * @param[in,out] in_ctx In_ctx enum of the schema node, may be updated.
6258 * @param[in] axis Axis to use.
6259 * @return LY_SUCCESS on success.
6260 * @return LY_ENOT if the node should not be returned.
6261 */
6262static LY_ERR
6263moveto_axis_scnode_next_in_ctx(int32_t *in_ctx, enum lyxp_axis axis)
6264{
6265 switch (axis) {
6266 case LYXP_AXIS_SELF:
6267 if ((*in_ctx == LYXP_SET_SCNODE_START) || (*in_ctx == LYXP_SET_SCNODE_ATOM_CTX)) {
6268 /* additionally put the start node into context */
6269 *in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
6270 return LY_SUCCESS;
6271 }
6272 break;
6273 case LYXP_AXIS_PARENT:
6274 case LYXP_AXIS_ANCESTOR_OR_SELF:
6275 case LYXP_AXIS_ANCESTOR:
6276 case LYXP_AXIS_DESCENDANT_OR_SELF:
6277 case LYXP_AXIS_DESCENDANT:
6278 case LYXP_AXIS_FOLLOWING:
6279 case LYXP_AXIS_FOLLOWING_SIBLING:
6280 case LYXP_AXIS_PRECEDING:
6281 case LYXP_AXIS_PRECEDING_SIBLING:
6282 case LYXP_AXIS_CHILD:
6283 if (*in_ctx == LYXP_SET_SCNODE_START) {
6284 /* remember that context node was used */
6285 *in_ctx = LYXP_SET_SCNODE_START_USED;
6286 return LY_SUCCESS;
6287 } else if (*in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
6288 /* traversed */
6289 *in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
6290 return LY_SUCCESS;
6291 }
6292 break;
6293 case LYXP_AXIS_ATTRIBUTE:
6294 /* unreachable */
6295 assert(0);
6296 LOGINT(NULL);
6297 break;
6298 }
6299
6300 return LY_ENOT;
6301}
6302
6303/**
6304 * @brief Get previous sibling for a schema node.
6305 *
6306 * @param[in] scnode Schema node.
6307 * @param[in] getnext_opts Options for ::lys_getnext().
6308 * @return Previous sibling, NULL if none.
6309 */
6310static const struct lysc_node *
6311moveto_axis_scnode_preceding_sibling(const struct lysc_node *scnode, uint32_t getnext_opts)
6312{
6313 const struct lysc_node *next = NULL, *prev = NULL;
6314
6315 while ((next = lys_getnext(next, lysc_data_parent(scnode), scnode->module->compiled, getnext_opts))) {
6316 if (next == scnode) {
6317 break;
6318 }
6319
6320 prev = next;
6321 }
6322
6323 return prev;
6324}
6325
6326/**
6327 * @brief Get the first schema node on an axis for a context node.
6328 *
6329 * @param[in,out] iter Last returned node, start with NULL, updated to the next node.
6330 * @param[in,out] iter_type Node type of @p iter, start with 0, updated to the node type of the next node.
6331 * @param[in,out] iter_mod Internal module iterator, do not change.
6332 * @param[in,out] iter_mod_idx Internal module index iterator, do not change.
6333 * @param[in] scnode Context node.
6334 * @param[in] node_type Type of @p scnode.
6335 * @param[in] in_ctx In_ctx enum of @p scnode.
6336 * @param[in] axis Axis to use.
6337 * @param[in] set XPath set with the general context.
6338 * @param[in] getnext_opts Options for ::lys_getnext().
6339 * @return LY_SUCCESS on success.
6340 * @return LY_ENOTFOUND if no next node found.
6341 */
6342static LY_ERR
6343moveto_axis_scnode_next_first(const struct lysc_node **iter, enum lyxp_node_type *iter_type, const struct lys_module **iter_mod,
6344 uint32_t *iter_mod_idx, const struct lysc_node *scnode, enum lyxp_node_type node_type, enum lyxp_axis axis,
6345 struct lyxp_set *set, uint32_t getnext_opts)
6346{
6347 const struct lysc_node *next = NULL;
6348 enum lyxp_node_type next_type = 0;
6349
6350 assert(!*iter);
6351 assert(!*iter_type);
6352
6353 *iter_mod = NULL;
6354 *iter_mod_idx = 0;
6355
6356 switch (axis) {
6357 case LYXP_AXIS_ANCESTOR_OR_SELF:
6358 case LYXP_AXIS_DESCENDANT_OR_SELF:
6359 case LYXP_AXIS_SELF:
6360 if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT) || (node_type == LYXP_NODE_ELEM)) {
6361 /* just return the node */
6362 next = scnode;
6363 next_type = node_type;
6364 }
6365 break;
6366
6367 case LYXP_AXIS_ANCESTOR:
6368 case LYXP_AXIS_PARENT:
6369 if (node_type == LYXP_NODE_ELEM) {
6370 next = lysc_data_parent(scnode);
6371 next_type = next ? LYXP_NODE_ELEM : set->root_type;
6372 } /* else no parent */
6373 break;
6374
6375 case LYXP_AXIS_DESCENDANT:
6376 case LYXP_AXIS_CHILD:
6377 if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT)) {
6378 /* it can actually be in any module, it's all <running>, and even if it's moveto_mod (if set),
6379 * it can be in a top-level augment */
6380 while ((*iter_mod = ly_ctx_get_module_iter(set->ctx, iter_mod_idx))) {
6381 /* module may not be implemented or not compiled yet */
6382 if (!(*iter_mod)->compiled) {
6383 continue;
6384 }
6385
6386 /* get next node */
6387 if ((next = lys_getnext(NULL, NULL, (*iter_mod)->compiled, getnext_opts))) {
6388 next_type = LYXP_NODE_ELEM;
6389 break;
6390 }
6391 }
6392 } else if (node_type == LYXP_NODE_ELEM) {
6393 /* get next node */
6394 next = lys_getnext(NULL, scnode, NULL, getnext_opts);
6395 next_type = next ? LYXP_NODE_ELEM : 0;
6396 }
6397 break;
6398
6399 case LYXP_AXIS_FOLLOWING:
6400 case LYXP_AXIS_FOLLOWING_SIBLING:
6401 if (node_type == LYXP_NODE_ELEM) {
6402 /* first next sibling */
6403 next = lys_getnext(scnode, lysc_data_parent(scnode), scnode->module->compiled, getnext_opts);
6404 next_type = next ? LYXP_NODE_ELEM : 0;
6405 } /* else no sibling */
6406 break;
6407
6408 case LYXP_AXIS_PRECEDING:
6409 case LYXP_AXIS_PRECEDING_SIBLING:
6410 if (node_type == LYXP_NODE_ELEM) {
6411 /* first parent sibling */
6412 next = lys_getnext(NULL, lysc_data_parent(scnode), scnode->module->compiled, getnext_opts);
6413 if (next == scnode) {
6414 /* no preceding sibling */
6415 next = NULL;
6416 }
6417 next_type = next ? LYXP_NODE_ELEM : 0;
6418 } /* else no sibling */
6419 break;
6420
6421 case LYXP_AXIS_ATTRIBUTE:
6422 /* unreachable */
6423 assert(0);
6424 LOGINT(set->ctx);
6425 break;
6426 }
6427
6428 *iter = next;
6429 *iter_type = next_type;
6430 return next_type ? LY_SUCCESS : LY_ENOTFOUND;
6431}
6432
6433/**
6434 * @brief Iterate over all schema nodes on an axis for a context node.
6435 *
6436 * @param[in,out] iter Last returned node, start with NULL, updated to the next node.
6437 * @param[in,out] iter_type Node type of @p iter, start with 0, updated to the node type of the next node.
6438 * @param[in,out] iter_mod Internal module iterator, do not change.
6439 * @param[in,out] iter_mod_idx Internal module index iterator, do not change.
6440 * @param[in] scnode Context node.
6441 * @param[in] node_type Type of @p scnode.
6442 * @param[in] axis Axis to use.
6443 * @param[in] set XPath set with the general context.
6444 * @param[in] getnext_opts Options for ::lys_getnext().
6445 * @return LY_SUCCESS on success.
6446 * @return LY_ENOTFOUND if no next node found.
6447 */
6448static LY_ERR
6449moveto_axis_scnode_next(const struct lysc_node **iter, enum lyxp_node_type *iter_type, const struct lys_module **iter_mod,
6450 uint32_t *iter_mod_idx, const struct lysc_node *scnode, enum lyxp_node_type node_type, enum lyxp_axis axis,
6451 struct lyxp_set *set, uint32_t getnext_opts)
6452{
6453 const struct lysc_node *next = NULL, *dfs_stop;
6454 enum lyxp_node_type next_type = 0;
6455
6456 if (!*iter_type) {
6457 /* first returned node */
6458 return moveto_axis_scnode_next_first(iter, iter_type, iter_mod, iter_mod_idx, scnode, node_type, axis, set,
6459 getnext_opts);
6460 }
6461
6462 switch (axis) {
6463 case LYXP_AXIS_PARENT:
6464 case LYXP_AXIS_SELF:
6465 /* parent/self was returned before */
6466 break;
6467
6468 case LYXP_AXIS_ANCESTOR_OR_SELF:
6469 if ((*iter == scnode) && (*iter_type == node_type)) {
6470 /* fake first ancestor, we returned self before */
6471 *iter = NULL;
6472 *iter_type = 0;
6473 return moveto_axis_scnode_next_first(iter, iter_type, iter_mod, iter_mod_idx, scnode, node_type,
6474 LYXP_AXIS_ANCESTOR, set, getnext_opts);
6475 } /* else continue ancestor */
6476
6477 /* fallthrough */
6478 case LYXP_AXIS_ANCESTOR:
6479 if (*iter_type == LYXP_NODE_ELEM) {
6480 next = lysc_data_parent(*iter);
6481 next_type = next ? LYXP_NODE_ELEM : set->root_type;
6482 } /* else no ancestor */
6483 break;
6484
6485 case LYXP_AXIS_DESCENDANT_OR_SELF:
6486 if ((*iter == scnode) && (*iter_type == node_type)) {
6487 /* fake first descendant, we returned self before */
6488 *iter = NULL;
6489 *iter_type = 0;
6490 return moveto_axis_scnode_next_first(iter, iter_type, iter_mod, iter_mod_idx, scnode, node_type,
6491 LYXP_AXIS_DESCENDANT, set, getnext_opts);
6492 } /* else DFS until context node */
6493 dfs_stop = scnode;
6494
6495 /* fallthrough */
6496 case LYXP_AXIS_DESCENDANT:
6497 if (axis == LYXP_AXIS_DESCENDANT) {
6498 /* DFS until the context node */
6499 dfs_stop = scnode;
6500 }
6501
6502 /* fallthrough */
6503 case LYXP_AXIS_PRECEDING:
6504 if (axis == LYXP_AXIS_PRECEDING) {
6505 /* DFS until the previous sibling */
6506 dfs_stop = moveto_axis_scnode_preceding_sibling(scnode, getnext_opts);
6507 assert(dfs_stop);
6508
6509 if (*iter == dfs_stop) {
6510 /* we are done */
6511 break;
6512 }
6513 }
6514
6515 /* fallthrough */
6516 case LYXP_AXIS_FOLLOWING:
6517 if (axis == LYXP_AXIS_FOLLOWING) {
6518 /* DFS through the whole module */
6519 dfs_stop = NULL;
6520 }
6521
6522 /* nested nodes */
6523 assert(*iter);
6524 next = moveto_axis_scnode_next_dfs_forward(*iter, dfs_stop, getnext_opts);
6525 if (next) {
6526 next_type = LYXP_NODE_ELEM;
6527 break;
6528 } /* else get next top-level node just like a child */
6529
6530 /* fallthrough */
6531 case LYXP_AXIS_CHILD:
6532 case LYXP_AXIS_FOLLOWING_SIBLING:
6533 if (!*iter_mod) {
6534 /* nodes from a single module */
6535 if ((next = lys_getnext(*iter, lysc_data_parent(*iter), (*iter)->module->compiled, getnext_opts))) {
6536 next_type = LYXP_NODE_ELEM;
6537 break;
6538 }
6539
6540 assert(scnode);
6541 if (!lysc_data_parent(scnode)) {
6542 /* iterating over top-level nodes, find next */
6543 while (lysc_data_parent(*iter)) {
6544 *iter = lysc_data_parent(*iter);
6545 }
6546 if ((next = lys_getnext(*iter, NULL, (*iter)->module->compiled, getnext_opts))) {
6547 next_type = LYXP_NODE_ELEM;
6548 break;
6549 }
6550 }
6551 }
6552
6553 while (*iter_mod) {
6554 /* module top-level nodes */
6555 if ((next = lys_getnext(*iter, NULL, (*iter_mod)->compiled, getnext_opts))) {
6556 next_type = LYXP_NODE_ELEM;
6557 break;
6558 }
6559
6560 /* get next module */
6561 while ((*iter_mod = ly_ctx_get_module_iter(set->ctx, iter_mod_idx))) {
6562 /* module may not be implemented or not compiled yet */
6563 if ((*iter_mod)->compiled) {
6564 break;
6565 }
6566 }
6567
6568 /* new module, start over */
6569 *iter = NULL;
6570 }
6571 break;
6572
6573 case LYXP_AXIS_PRECEDING_SIBLING:
6574 assert(*iter);
6575
6576 /* next parent sibling until scnode */
6577 next = lys_getnext(*iter, lysc_data_parent(*iter), (*iter)->module->compiled, getnext_opts);
6578 if (next == scnode) {
6579 /* no previous sibling */
6580 next = NULL;
6581 }
6582 next_type = next ? LYXP_NODE_ELEM : 0;
6583 break;
6584
6585 case LYXP_AXIS_ATTRIBUTE:
6586 /* unreachable */
6587 assert(0);
6588 LOGINT(set->ctx);
6589 break;
6590 }
6591
6592 *iter = next;
6593 *iter_type = next_type;
6594 return next_type ? LY_SUCCESS : LY_ENOTFOUND;
6595}
6596
6597/**
Michal Vaskod3678892020-05-21 10:06:58 +02006598 * @brief Move context @p set to a schema node. Result is LYXP_SET_SCNODE_SET (or LYXP_SET_EMPTY).
6599 *
6600 * @param[in,out] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006601 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02006602 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko49fec8e2022-05-24 10:28:33 +02006603 * @param[in] axis Axis to search on.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006604 * @param[in] options XPath options.
6605 * @return LY_ERR
6606 */
6607static LY_ERR
Michal Vasko49fec8e2022-05-24 10:28:33 +02006608moveto_scnode(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, enum lyxp_axis axis,
6609 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006610{
Radek Krejci857189e2020-09-01 13:26:36 +02006611 ly_bool temp_ctx = 0;
Michal Vasko49fec8e2022-05-24 10:28:33 +02006612 uint32_t getnext_opts, orig_used, i, mod_idx, idx;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006613 const struct lys_module *mod;
Michal Vasko49fec8e2022-05-24 10:28:33 +02006614 const struct lysc_node *iter;
6615 enum lyxp_node_type iter_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006616
aPiecek8b0cc152021-05-31 16:40:31 +02006617 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006618 return LY_SUCCESS;
6619 }
6620
6621 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006622 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006623 return LY_EVALID;
6624 }
6625
Michal Vaskocafad9d2019-11-07 15:20:03 +01006626 /* getnext opts */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01006627 getnext_opts = 0;
Michal Vaskocafad9d2019-11-07 15:20:03 +01006628 if (options & LYXP_SCNODE_OUTPUT) {
6629 getnext_opts |= LYS_GETNEXT_OUTPUT;
6630 }
6631
Michal Vasko03ff5a72019-09-11 13:49:33 +02006632 orig_used = set->used;
6633 for (i = 0; i < orig_used; ++i) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02006634 /* update in_ctx first */
6635 if (moveto_axis_scnode_next_in_ctx(&set->val.scnodes[i].in_ctx, axis)) {
6636 /* not usable, skip */
6637 continue;
6638 }
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006639
Michal Vasko49fec8e2022-05-24 10:28:33 +02006640 iter = NULL;
6641 iter_type = 0;
6642 while (!moveto_axis_scnode_next(&iter, &iter_type, &mod, &mod_idx, set->val.scnodes[i].scnode,
6643 set->val.scnodes[i].type, axis, set, getnext_opts)) {
6644 if (moveto_scnode_check(iter, NULL, set, ncname, moveto_mod)) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006645 continue;
6646 }
6647
Michal Vasko49fec8e2022-05-24 10:28:33 +02006648 /* insert */
6649 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, iter_type, &idx));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006650
Michal Vasko49fec8e2022-05-24 10:28:33 +02006651 /* we need to prevent these nodes from being considered in this moveto */
6652 if ((idx < orig_used) && (idx > i)) {
6653 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX;
6654 temp_ctx = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006655 }
6656 }
6657 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006658
6659 /* correct temporary in_ctx values */
6660 if (temp_ctx) {
6661 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006662 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_NEW_CTX) {
6663 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006664 }
6665 }
6666 }
6667
6668 return LY_SUCCESS;
6669}
6670
6671/**
Michal Vasko49fec8e2022-05-24 10:28:33 +02006672 * @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 +02006673 * Context position aware.
6674 *
6675 * @param[in] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006676 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02006677 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vaskocdad7122020-11-09 21:04:44 +01006678 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006679 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6680 */
6681static LY_ERR
Michal Vasko49fec8e2022-05-24 10:28:33 +02006682moveto_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 +02006683{
6684 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006685 const struct lyd_node *next, *elem, *start;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006686 struct lyxp_set ret_set;
6687 LY_ERR rc;
6688
aPiecek8b0cc152021-05-31 16:40:31 +02006689 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006690 return LY_SUCCESS;
6691 }
6692
6693 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006694 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006695 return LY_EVALID;
6696 }
6697
Michal Vasko9f96a052020-03-10 09:41:45 +01006698 /* 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 +02006699 rc = xpath_pi_node(set, LYXP_AXIS_CHILD, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006700 LY_CHECK_RET(rc);
6701
Michal Vasko6346ece2019-09-24 13:12:53 +02006702 /* this loop traverses all the nodes in the set and adds/keeps only those that match qname */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006703 set_init(&ret_set, set);
6704 for (i = 0; i < set->used; ++i) {
6705
6706 /* TREE DFS */
6707 start = set->val.nodes[i].node;
6708 for (elem = next = start; elem; elem = next) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02006709 rc = moveto_node_check(elem, LYXP_NODE_ELEM, set, ncname, moveto_mod, options);
Michal Vasko6346ece2019-09-24 13:12:53 +02006710 if (!rc) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006711 /* add matching node into result set */
6712 set_insert_node(&ret_set, elem, 0, LYXP_NODE_ELEM, ret_set.used);
6713 if (set_dup_node_check(set, elem, LYXP_NODE_ELEM, i)) {
6714 /* the node is a duplicate, we'll process it later in the set */
6715 goto skip_children;
6716 }
Michal Vasko6346ece2019-09-24 13:12:53 +02006717 } else if (rc == LY_EINCOMPLETE) {
Michal Vasko6346ece2019-09-24 13:12:53 +02006718 return rc;
6719 } else if (rc == LY_EINVAL) {
6720 goto skip_children;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006721 }
6722
6723 /* TREE DFS NEXT ELEM */
6724 /* select element for the next run - children first */
Radek Krejcia1c1e542020-09-29 16:06:52 +02006725 next = lyd_child(elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006726 if (!next) {
6727skip_children:
6728 /* no children, so try siblings, but only if it's not the start,
6729 * that is considered to be the root and it's siblings are not traversed */
6730 if (elem != start) {
6731 next = elem->next;
6732 } else {
6733 break;
6734 }
6735 }
6736 while (!next) {
6737 /* no siblings, go back through the parents */
Michal Vasko9e685082021-01-29 14:49:09 +01006738 if (lyd_parent(elem) == start) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006739 /* we are done, no next element to process */
6740 break;
6741 }
6742 /* parent is already processed, go to its sibling */
Michal Vasko9e685082021-01-29 14:49:09 +01006743 elem = lyd_parent(elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006744 next = elem->next;
6745 }
6746 }
6747 }
6748
6749 /* make the temporary set the current one */
6750 ret_set.ctx_pos = set->ctx_pos;
6751 ret_set.ctx_size = set->ctx_size;
Michal Vaskod3678892020-05-21 10:06:58 +02006752 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006753 memcpy(set, &ret_set, sizeof *set);
6754
6755 return LY_SUCCESS;
6756}
6757
6758/**
Michal Vasko49fec8e2022-05-24 10:28:33 +02006759 * @brief Move context @p set to a child schema node and all its descendants starting from a node.
6760 * Result is LYXP_SET_NODE_SET.
6761 *
6762 * @param[in] set Set to use.
6763 * @param[in] start Start node whose subtree to add.
6764 * @param[in] start_idx Index of @p start in @p set.
6765 * @param[in] moveto_mod Matching node module, NULL for no prefix.
6766 * @param[in] ncname Matching node name in the dictionary, NULL for any.
6767 * @param[in] options XPath options.
6768 * @return LY_ERR value.
6769 */
6770static LY_ERR
6771moveto_scnode_dfs(struct lyxp_set *set, const struct lysc_node *start, uint32_t start_idx,
6772 const struct lys_module *moveto_mod, const char *ncname, uint32_t options)
6773{
6774 const struct lysc_node *next, *elem;
6775 uint32_t idx;
6776 LY_ERR rc;
6777
6778 /* TREE DFS */
6779 for (elem = next = start; elem; elem = next) {
6780 if ((elem == start) || (elem->nodetype & (LYS_CHOICE | LYS_CASE))) {
6781 /* schema-only nodes, skip root */
6782 goto next_iter;
6783 }
6784
6785 rc = moveto_scnode_check(elem, start, set, ncname, moveto_mod);
6786 if (!rc) {
6787 if (lyxp_set_scnode_contains(set, elem, LYXP_NODE_ELEM, start_idx, &idx)) {
6788 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
6789 if (idx > start_idx) {
6790 /* we will process it later in the set */
6791 goto skip_children;
6792 }
6793 } else {
6794 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, elem, LYXP_NODE_ELEM, NULL));
6795 }
6796 } else if (rc == LY_EINVAL) {
6797 goto skip_children;
6798 }
6799
6800next_iter:
6801 /* TREE DFS NEXT ELEM */
6802 /* select element for the next run - children first */
6803 next = lysc_node_child(elem);
6804 if (next && (next->nodetype == LYS_INPUT) && (options & LYXP_SCNODE_OUTPUT)) {
6805 next = next->next;
6806 } else if (next && (next->nodetype == LYS_OUTPUT) && !(options & LYXP_SCNODE_OUTPUT)) {
6807 next = next->next;
6808 }
6809 if (!next) {
6810skip_children:
6811 /* no children, so try siblings, but only if it's not the start,
6812 * that is considered to be the root and it's siblings are not traversed */
6813 if (elem != start) {
6814 next = elem->next;
6815 } else {
6816 break;
6817 }
6818 }
6819 while (!next) {
6820 /* no siblings, go back through the parents */
6821 if (elem->parent == start) {
6822 /* we are done, no next element to process */
6823 break;
6824 }
6825 /* parent is already processed, go to its sibling */
6826 elem = elem->parent;
6827 next = elem->next;
6828 }
6829 }
6830
6831 return LY_SUCCESS;
6832}
6833
6834/**
6835 * @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 +02006836 *
6837 * @param[in] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006838 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02006839 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006840 * @param[in] options XPath options.
Michal Vasko49fec8e2022-05-24 10:28:33 +02006841 * @return LY_ERR value.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006842 */
6843static LY_ERR
Michal Vasko49fec8e2022-05-24 10:28:33 +02006844moveto_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 +02006845{
Michal Vasko49fec8e2022-05-24 10:28:33 +02006846 uint32_t i, orig_used, mod_idx;
6847 const struct lys_module *mod;
6848 const struct lysc_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006849
aPiecek8b0cc152021-05-31 16:40:31 +02006850 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006851 return LY_SUCCESS;
6852 }
6853
6854 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006855 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006856 return LY_EVALID;
6857 }
6858
Michal Vasko03ff5a72019-09-11 13:49:33 +02006859 orig_used = set->used;
6860 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006861 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
6862 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006863 continue;
6864 }
6865
6866 /* remember context node */
Radek Krejcif13b87b2020-12-01 22:02:17 +01006867 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vaskoec4df482019-12-16 10:02:18 +01006868 } else {
Michal Vasko1a09b212021-05-06 13:00:10 +02006869 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006870 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006871
Michal Vasko49fec8e2022-05-24 10:28:33 +02006872 if ((set->val.scnodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.scnodes[i].type == LYXP_NODE_ROOT)) {
6873 /* traverse all top-level nodes in all the modules */
6874 mod_idx = 0;
6875 while ((mod = ly_ctx_get_module_iter(set->ctx, &mod_idx))) {
6876 /* module may not be implemented or not compiled yet */
6877 if (!mod->compiled) {
6878 continue;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006879 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006880
Michal Vasko49fec8e2022-05-24 10:28:33 +02006881 root = NULL;
6882 /* no getnext opts needed */
6883 while ((root = lys_getnext(root, NULL, mod->compiled, 0))) {
6884 LY_CHECK_RET(moveto_scnode_dfs(set, root, i, moveto_mod, ncname, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006885 }
6886 }
Michal Vasko49fec8e2022-05-24 10:28:33 +02006887
6888 } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
6889 /* add all the descendants recursively */
6890 LY_CHECK_RET(moveto_scnode_dfs(set, set->val.scnodes[i].scnode, i, moveto_mod, ncname, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006891 }
6892 }
6893
6894 return LY_SUCCESS;
6895}
6896
6897/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02006898 * @brief Move context @p set to an attribute. Result is LYXP_SET_NODE_SET.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006899 * Indirectly context position aware.
6900 *
6901 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02006902 * @param[in] mod Matching metadata module, NULL for any.
6903 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
aPiecek8b0cc152021-05-31 16:40:31 +02006904 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006905 * @return LY_ERR
6906 */
6907static LY_ERR
aPiecek8b0cc152021-05-31 16:40:31 +02006908moveto_attr(struct lyxp_set *set, const struct lys_module *mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006909{
Michal Vasko9f96a052020-03-10 09:41:45 +01006910 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006911
aPiecek8b0cc152021-05-31 16:40:31 +02006912 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006913 return LY_SUCCESS;
6914 }
6915
6916 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006917 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006918 return LY_EVALID;
6919 }
6920
Radek Krejci1deb5be2020-08-26 16:43:36 +02006921 for (uint32_t i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02006922 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006923
6924 /* only attributes of an elem (not dummy) can be in the result, skip all the rest;
6925 * our attributes are always qualified */
Michal Vasko5c4e5892019-11-14 12:31:38 +01006926 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01006927 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006928
6929 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02006930 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006931 continue;
6932 }
6933
Michal Vaskod3678892020-05-21 10:06:58 +02006934 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006935 /* match */
6936 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01006937 set->val.meta[i].meta = sub;
6938 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006939 /* pos does not change */
6940 replaced = 1;
6941 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01006942 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 +02006943 }
6944 ++i;
6945 }
6946 }
6947 }
6948
6949 if (!replaced) {
6950 /* no match */
6951 set_remove_node(set, i);
6952 }
6953 }
6954
6955 return LY_SUCCESS;
6956}
6957
6958/**
6959 * @brief Move context @p set1 to union with @p set2. @p set2 is emptied afterwards.
Michal Vasko61ac2f62020-05-25 12:39:51 +02006960 * Result is LYXP_SET_NODE_SET. Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006961 *
6962 * @param[in,out] set1 Set to use for the result.
6963 * @param[in] set2 Set that is copied to @p set1.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006964 * @return LY_ERR
6965 */
6966static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006967moveto_union(struct lyxp_set *set1, struct lyxp_set *set2)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006968{
6969 LY_ERR rc;
6970
Michal Vaskod3678892020-05-21 10:06:58 +02006971 if ((set1->type != LYXP_SET_NODE_SET) || (set2->type != LYXP_SET_NODE_SET)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006972 LOGVAL(set1->ctx, LY_VCODE_XP_INOP_2, "union", print_set_type(set1), print_set_type(set2));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006973 return LY_EVALID;
6974 }
6975
6976 /* set2 is empty or both set1 and set2 */
Michal Vaskod3678892020-05-21 10:06:58 +02006977 if (!set2->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006978 return LY_SUCCESS;
6979 }
6980
Michal Vaskod3678892020-05-21 10:06:58 +02006981 if (!set1->used) {
aPiecekadc1e4f2021-10-07 11:15:12 +02006982 /* release hidden allocated data (lyxp_set.size) */
6983 lyxp_set_free_content(set1);
6984 /* direct copying of the entire structure */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006985 memcpy(set1, set2, sizeof *set1);
6986 /* dynamic memory belongs to set1 now, do not free */
Michal Vaskod3678892020-05-21 10:06:58 +02006987 memset(set2, 0, sizeof *set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006988 return LY_SUCCESS;
6989 }
6990
6991 /* we assume sets are sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006992 assert(!set_sort(set1) && !set_sort(set2));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006993
6994 /* sort, remove duplicates */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006995 rc = set_sorted_merge(set1, set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006996 LY_CHECK_RET(rc);
6997
6998 /* final set must be sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006999 assert(!set_sort(set1));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007000
7001 return LY_SUCCESS;
7002}
7003
7004/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02007005 * @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 +02007006 * Context position aware.
7007 *
7008 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02007009 * @param[in] mod Matching metadata module, NULL for any.
7010 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
Michal Vaskocdad7122020-11-09 21:04:44 +01007011 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007012 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7013 */
7014static int
Michal Vaskocdad7122020-11-09 21:04:44 +01007015moveto_attr_alldesc(struct lyxp_set *set, const struct lys_module *mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007016{
Michal Vasko9f96a052020-03-10 09:41:45 +01007017 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007018 struct lyxp_set *set_all_desc = NULL;
7019 LY_ERR rc;
7020
aPiecek8b0cc152021-05-31 16:40:31 +02007021 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007022 return LY_SUCCESS;
7023 }
7024
7025 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01007026 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007027 return LY_EVALID;
7028 }
7029
Michal Vasko03ff5a72019-09-11 13:49:33 +02007030 /* can be optimized similarly to moveto_node_alldesc() and save considerable amount of memory,
7031 * but it likely won't be used much, so it's a waste of time */
7032 /* copy the context */
7033 set_all_desc = set_copy(set);
7034 /* get all descendant nodes (the original context nodes are removed) */
Michal Vasko49fec8e2022-05-24 10:28:33 +02007035 rc = moveto_node_alldesc_child(set_all_desc, NULL, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007036 if (rc != LY_SUCCESS) {
7037 lyxp_set_free(set_all_desc);
7038 return rc;
7039 }
7040 /* prepend the original context nodes */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007041 rc = moveto_union(set, set_all_desc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007042 if (rc != LY_SUCCESS) {
7043 lyxp_set_free(set_all_desc);
7044 return rc;
7045 }
7046 lyxp_set_free(set_all_desc);
7047
Radek Krejci1deb5be2020-08-26 16:43:36 +02007048 for (uint32_t i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02007049 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007050
7051 /* only attributes of an elem can be in the result, skip all the rest,
7052 * we have all attributes qualified in lyd tree */
7053 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01007054 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007055 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02007056 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007057 continue;
7058 }
7059
Michal Vaskod3678892020-05-21 10:06:58 +02007060 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007061 /* match */
7062 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01007063 set->val.meta[i].meta = sub;
7064 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007065 /* pos does not change */
7066 replaced = 1;
7067 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01007068 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 +02007069 }
7070 ++i;
7071 }
7072 }
7073 }
7074
7075 if (!replaced) {
7076 /* no match */
7077 set_remove_node(set, i);
7078 }
7079 }
7080
7081 return LY_SUCCESS;
7082}
7083
7084/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02007085 * @brief Move context @p set to the result of a comparison. Handles '=', '!=', '<=', '<', '>=', or '>'.
7086 * Result is LYXP_SET_BOOLEAN. Indirectly context position aware.
7087 *
7088 * @param[in,out] set1 Set to use for the result.
7089 * @param[in] set2 Set acting as the second operand for @p op.
7090 * @param[in] op Comparison operator to process.
7091 * @param[in] options XPath options.
7092 * @return LY_ERR
7093 */
7094static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02007095moveto_op_comp(struct lyxp_set *set1, struct lyxp_set *set2, const char *op, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007096{
7097 /*
7098 * NODE SET + NODE SET = NODE SET + STRING /(1 NODE SET) 2 STRING
7099 * NODE SET + STRING = STRING + STRING /1 STRING (2 STRING)
7100 * NODE SET + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
7101 * NODE SET + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
7102 * STRING + NODE SET = STRING + STRING /(1 STRING) 2 STRING
7103 * NUMBER + NODE SET = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
7104 * BOOLEAN + NODE SET = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
7105 *
7106 * '=' or '!='
7107 * BOOLEAN + BOOLEAN
7108 * BOOLEAN + STRING = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
7109 * BOOLEAN + NUMBER = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
7110 * STRING + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
7111 * NUMBER + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
7112 * NUMBER + NUMBER
7113 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
7114 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
7115 * STRING + STRING
7116 *
7117 * '<=', '<', '>=', '>'
7118 * NUMBER + NUMBER
7119 * BOOLEAN + BOOLEAN = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
7120 * BOOLEAN + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
7121 * BOOLEAN + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
7122 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
7123 * STRING + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
7124 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
7125 * NUMBER + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
7126 * STRING + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
7127 */
7128 struct lyxp_set iter1, iter2;
7129 int result;
7130 int64_t i;
7131 LY_ERR rc;
7132
Michal Vasko004d3152020-06-11 19:59:22 +02007133 memset(&iter1, 0, sizeof iter1);
7134 memset(&iter2, 0, sizeof iter2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007135
7136 /* iterative evaluation with node-sets */
7137 if ((set1->type == LYXP_SET_NODE_SET) || (set2->type == LYXP_SET_NODE_SET)) {
7138 if (set1->type == LYXP_SET_NODE_SET) {
7139 for (i = 0; i < set1->used; ++i) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02007140 /* cast set1 */
Michal Vasko03ff5a72019-09-11 13:49:33 +02007141 switch (set2->type) {
7142 case LYXP_SET_NUMBER:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007143 rc = set_comp_cast(&iter1, set1, LYXP_SET_NUMBER, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007144 break;
7145 case LYXP_SET_BOOLEAN:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007146 rc = set_comp_cast(&iter1, set1, LYXP_SET_BOOLEAN, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007147 break;
7148 default:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007149 rc = set_comp_cast(&iter1, set1, LYXP_SET_STRING, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007150 break;
7151 }
7152 LY_CHECK_RET(rc);
7153
Michal Vasko4c7763f2020-07-27 17:40:37 +02007154 /* canonize set2 */
7155 LY_CHECK_ERR_RET(rc = set_comp_canonize(&iter2, set2, &set1->val.nodes[i]), lyxp_set_free_content(&iter1), rc);
7156
7157 /* compare recursively */
7158 rc = moveto_op_comp(&iter1, &iter2, op, options);
7159 lyxp_set_free_content(&iter2);
7160 LY_CHECK_ERR_RET(rc, lyxp_set_free_content(&iter1), rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007161
7162 /* lazy evaluation until true */
Michal Vasko004d3152020-06-11 19:59:22 +02007163 if (iter1.val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007164 set_fill_boolean(set1, 1);
7165 return LY_SUCCESS;
7166 }
7167 }
7168 } else {
7169 for (i = 0; i < set2->used; ++i) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02007170 /* set set2 */
Michal Vasko03ff5a72019-09-11 13:49:33 +02007171 switch (set1->type) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02007172 case LYXP_SET_NUMBER:
7173 rc = set_comp_cast(&iter2, set2, LYXP_SET_NUMBER, i);
7174 break;
7175 case LYXP_SET_BOOLEAN:
7176 rc = set_comp_cast(&iter2, set2, LYXP_SET_BOOLEAN, i);
7177 break;
7178 default:
7179 rc = set_comp_cast(&iter2, set2, LYXP_SET_STRING, i);
7180 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007181 }
7182 LY_CHECK_RET(rc);
7183
Michal Vasko4c7763f2020-07-27 17:40:37 +02007184 /* canonize set1 */
7185 LY_CHECK_ERR_RET(rc = set_comp_canonize(&iter1, set1, &set2->val.nodes[i]), lyxp_set_free_content(&iter2), rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007186
Michal Vasko4c7763f2020-07-27 17:40:37 +02007187 /* compare recursively */
Michal Vasko03ff5a72019-09-11 13:49:33 +02007188 rc = moveto_op_comp(&iter1, &iter2, op, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007189 lyxp_set_free_content(&iter2);
Michal Vasko4c7763f2020-07-27 17:40:37 +02007190 LY_CHECK_ERR_RET(rc, lyxp_set_free_content(&iter1), rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007191
7192 /* lazy evaluation until true */
Michal Vasko004d3152020-06-11 19:59:22 +02007193 if (iter1.val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007194 set_fill_boolean(set1, 1);
7195 return LY_SUCCESS;
7196 }
7197 }
7198 }
7199
7200 /* false for all nodes */
7201 set_fill_boolean(set1, 0);
7202 return LY_SUCCESS;
7203 }
7204
7205 /* first convert properly */
7206 if ((op[0] == '=') || (op[0] == '!')) {
7207 if ((set1->type == LYXP_SET_BOOLEAN) || (set2->type == LYXP_SET_BOOLEAN)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007208 lyxp_set_cast(set1, LYXP_SET_BOOLEAN);
7209 lyxp_set_cast(set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007210 } else if ((set1->type == LYXP_SET_NUMBER) || (set2->type == LYXP_SET_NUMBER)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007211 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007212 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007213 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007214 LY_CHECK_RET(rc);
7215 } /* else we have 2 strings */
7216 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007217 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007218 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007219 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007220 LY_CHECK_RET(rc);
7221 }
7222
7223 assert(set1->type == set2->type);
7224
7225 /* compute result */
7226 if (op[0] == '=') {
7227 if (set1->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +02007228 result = (set1->val.bln == set2->val.bln);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007229 } else if (set1->type == LYXP_SET_NUMBER) {
7230 result = (set1->val.num == set2->val.num);
7231 } else {
7232 assert(set1->type == LYXP_SET_STRING);
Michal Vaskoac6c72f2019-11-14 16:09:34 +01007233 result = !strcmp(set1->val.str, set2->val.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007234 }
7235 } else if (op[0] == '!') {
7236 if (set1->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +02007237 result = (set1->val.bln != set2->val.bln);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007238 } else if (set1->type == LYXP_SET_NUMBER) {
7239 result = (set1->val.num != set2->val.num);
7240 } else {
7241 assert(set1->type == LYXP_SET_STRING);
Michal Vaskoc2058432020-11-06 17:26:21 +01007242 result = strcmp(set1->val.str, set2->val.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007243 }
7244 } else {
7245 assert(set1->type == LYXP_SET_NUMBER);
7246 if (op[0] == '<') {
7247 if (op[1] == '=') {
7248 result = (set1->val.num <= set2->val.num);
7249 } else {
7250 result = (set1->val.num < set2->val.num);
7251 }
7252 } else {
7253 if (op[1] == '=') {
7254 result = (set1->val.num >= set2->val.num);
7255 } else {
7256 result = (set1->val.num > set2->val.num);
7257 }
7258 }
7259 }
7260
7261 /* assign result */
7262 if (result) {
7263 set_fill_boolean(set1, 1);
7264 } else {
7265 set_fill_boolean(set1, 0);
7266 }
7267
7268 return LY_SUCCESS;
7269}
7270
7271/**
7272 * @brief Move context @p set to the result of a basic operation. Handles '+', '-', unary '-', '*', 'div',
7273 * or 'mod'. Result is LYXP_SET_NUMBER. Indirectly context position aware.
7274 *
7275 * @param[in,out] set1 Set to use for the result.
7276 * @param[in] set2 Set acting as the second operand for @p op.
7277 * @param[in] op Operator to process.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007278 * @return LY_ERR
7279 */
7280static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007281moveto_op_math(struct lyxp_set *set1, struct lyxp_set *set2, const char *op)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007282{
7283 LY_ERR rc;
7284
7285 /* unary '-' */
7286 if (!set2 && (op[0] == '-')) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007287 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007288 LY_CHECK_RET(rc);
7289 set1->val.num *= -1;
7290 lyxp_set_free(set2);
7291 return LY_SUCCESS;
7292 }
7293
7294 assert(set1 && set2);
7295
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007296 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007297 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007298 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007299 LY_CHECK_RET(rc);
7300
7301 switch (op[0]) {
7302 /* '+' */
7303 case '+':
7304 set1->val.num += set2->val.num;
7305 break;
7306
7307 /* '-' */
7308 case '-':
7309 set1->val.num -= set2->val.num;
7310 break;
7311
7312 /* '*' */
7313 case '*':
7314 set1->val.num *= set2->val.num;
7315 break;
7316
7317 /* 'div' */
7318 case 'd':
7319 set1->val.num /= set2->val.num;
7320 break;
7321
7322 /* 'mod' */
7323 case 'm':
7324 set1->val.num = ((long long)set1->val.num) % ((long long)set2->val.num);
7325 break;
7326
7327 default:
7328 LOGINT_RET(set1->ctx);
7329 }
7330
7331 return LY_SUCCESS;
7332}
7333
Michal Vasko03ff5a72019-09-11 13:49:33 +02007334/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02007335 * @brief Evaluate Predicate. Logs directly on error.
7336 *
Michal Vaskod3678892020-05-21 10:06:58 +02007337 * [9] Predicate ::= '[' Expr ']'
Michal Vasko03ff5a72019-09-11 13:49:33 +02007338 *
7339 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007340 * @param[in] tok_idx Position in the expression @p exp.
aPiecek8b0cc152021-05-31 16:40:31 +02007341 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007342 * @param[in] options XPath options.
Michal Vasko49fec8e2022-05-24 10:28:33 +02007343 * @param[in] axis Axis to search on.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007344 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7345 */
7346static LY_ERR
Michal Vasko49fec8e2022-05-24 10:28:33 +02007347eval_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 +02007348{
7349 LY_ERR rc;
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01007350 uint16_t orig_exp;
7351 uint32_t i, orig_pos, orig_size;
Michal Vasko5c4e5892019-11-14 12:31:38 +01007352 int32_t pred_in_ctx;
Michal Vasko88a9e802022-05-24 10:50:28 +02007353 ly_bool reverse_axis = 0;
aPiecekfff4dca2021-10-07 10:59:53 +02007354 struct lyxp_set set2 = {0};
Michal Vasko03ff5a72019-09-11 13:49:33 +02007355
7356 /* '[' */
aPiecek8b0cc152021-05-31 16:40:31 +02007357 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02007358 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007359 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007360
aPiecek8b0cc152021-05-31 16:40:31 +02007361 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007362only_parse:
aPiecek8b0cc152021-05-31 16:40:31 +02007363 rc = eval_expr_select(exp, tok_idx, 0, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007364 LY_CHECK_RET(rc);
7365 } else if (set->type == LYXP_SET_NODE_SET) {
7366 /* 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 +01007367 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007368
7369 /* empty set, nothing to evaluate */
7370 if (!set->used) {
7371 goto only_parse;
7372 }
7373
Michal Vasko49fec8e2022-05-24 10:28:33 +02007374 /* decide forward or reverse axis */
7375 switch (axis) {
7376 case LYXP_AXIS_ANCESTOR:
7377 case LYXP_AXIS_ANCESTOR_OR_SELF:
7378 case LYXP_AXIS_PRECEDING:
7379 case LYXP_AXIS_PRECEDING_SIBLING:
7380 reverse_axis = 1;
7381 break;
7382 case LYXP_AXIS_DESCENDANT:
7383 case LYXP_AXIS_DESCENDANT_OR_SELF:
7384 case LYXP_AXIS_FOLLOWING:
7385 case LYXP_AXIS_FOLLOWING_SIBLING:
7386 case LYXP_AXIS_PARENT:
7387 case LYXP_AXIS_CHILD:
7388 case LYXP_AXIS_SELF:
7389 case LYXP_AXIS_ATTRIBUTE:
7390 reverse_axis = 0;
7391 break;
7392 }
7393
Michal Vasko004d3152020-06-11 19:59:22 +02007394 orig_exp = *tok_idx;
Michal Vasko49fec8e2022-05-24 10:28:33 +02007395 orig_pos = reverse_axis ? set->used + 1 : 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007396 orig_size = set->used;
Michal Vasko39dbf352020-05-21 10:08:59 +02007397 for (i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007398 set_init(&set2, set);
7399 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 +02007400
7401 /* remember the node context position for position() and context size for last() */
7402 orig_pos += reverse_axis ? -1 : 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007403
7404 set2.ctx_pos = orig_pos;
7405 set2.ctx_size = orig_size;
Michal Vasko004d3152020-06-11 19:59:22 +02007406 *tok_idx = orig_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007407
Michal Vasko004d3152020-06-11 19:59:22 +02007408 rc = eval_expr_select(exp, tok_idx, 0, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007409 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02007410 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007411 return rc;
7412 }
7413
Michal Vasko49fec8e2022-05-24 10:28:33 +02007414 /* number is a proximity position */
Michal Vasko03ff5a72019-09-11 13:49:33 +02007415 if (set2.type == LYXP_SET_NUMBER) {
7416 if ((long long)set2.val.num == orig_pos) {
7417 set2.val.num = 1;
7418 } else {
7419 set2.val.num = 0;
7420 }
7421 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007422 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007423
7424 /* predicate satisfied or not? */
Michal Vasko004d3152020-06-11 19:59:22 +02007425 if (!set2.val.bln) {
Michal Vasko2caefc12019-11-14 16:07:56 +01007426 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007427 }
7428 }
Michal Vasko2caefc12019-11-14 16:07:56 +01007429 set_remove_nodes_none(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007430
7431 } else if (set->type == LYXP_SET_SCNODE_SET) {
7432 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01007433 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007434 /* there is a currently-valid node */
7435 break;
7436 }
7437 }
7438 /* empty set, nothing to evaluate */
7439 if (i == set->used) {
7440 goto only_parse;
7441 }
7442
Michal Vasko004d3152020-06-11 19:59:22 +02007443 orig_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007444
Michal Vasko03ff5a72019-09-11 13:49:33 +02007445 /* set special in_ctx to all the valid snodes */
7446 pred_in_ctx = set_scnode_new_in_ctx(set);
7447
7448 /* use the valid snodes one-by-one */
7449 for (i = 0; i < set->used; ++i) {
7450 if (set->val.scnodes[i].in_ctx != pred_in_ctx) {
7451 continue;
7452 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01007453 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007454
Michal Vasko004d3152020-06-11 19:59:22 +02007455 *tok_idx = orig_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007456
Michal Vasko004d3152020-06-11 19:59:22 +02007457 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007458 LY_CHECK_RET(rc);
7459
7460 set->val.scnodes[i].in_ctx = pred_in_ctx;
7461 }
7462
7463 /* restore the state as it was before the predicate */
7464 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01007465 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007466 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007467 } else if (set->val.scnodes[i].in_ctx == pred_in_ctx) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01007468 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007469 }
7470 }
7471
7472 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02007473 set2.type = LYXP_SET_NODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007474 set_fill_set(&set2, set);
7475
Michal Vasko004d3152020-06-11 19:59:22 +02007476 rc = eval_expr_select(exp, tok_idx, 0, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007477 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02007478 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007479 return rc;
7480 }
7481
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007482 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +02007483 if (!set2.val.bln) {
Michal Vaskod3678892020-05-21 10:06:58 +02007484 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007485 }
Michal Vaskod3678892020-05-21 10:06:58 +02007486 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007487 }
7488
7489 /* ']' */
Michal Vasko004d3152020-06-11 19:59:22 +02007490 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_BRACK2);
aPiecek8b0cc152021-05-31 16:40:31 +02007491 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02007492 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007493 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007494
7495 return LY_SUCCESS;
7496}
7497
7498/**
Michal Vaskod3678892020-05-21 10:06:58 +02007499 * @brief Evaluate Literal. Logs directly on error.
7500 *
7501 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007502 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02007503 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7504 */
7505static void
Michal Vasko40308e72020-10-20 16:38:40 +02007506eval_literal(const struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set)
Michal Vaskod3678892020-05-21 10:06:58 +02007507{
7508 if (set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007509 if (exp->tok_len[*tok_idx] == 2) {
Michal Vaskod3678892020-05-21 10:06:58 +02007510 set_fill_string(set, "", 0);
7511 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007512 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 +02007513 }
7514 }
7515 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02007516 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007517 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007518}
7519
7520/**
Michal Vasko004d3152020-06-11 19:59:22 +02007521 * @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 +02007522 *
Michal Vasko004d3152020-06-11 19:59:22 +02007523 * @param[in] exp Full parsed XPath expression.
7524 * @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 +02007525 * @param[in] ctx_node Found schema node as the context for the predicate.
7526 * @param[in] cur_mod Current module for the expression.
7527 * @param[in] cur_node Current (original context) node.
Michal Vasko004d3152020-06-11 19:59:22 +02007528 * @param[in] format Format of any prefixes in key names/values.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007529 * @param[in] prefix_data Format-specific prefix data (see ::ly_resolve_prefix).
Michal Vasko004d3152020-06-11 19:59:22 +02007530 * @param[out] predicates Parsed predicates.
7531 * @param[out] pred_type Type of @p predicates.
7532 * @return LY_SUCCESS on success,
7533 * @return LY_ERR on any error.
Michal Vaskod3678892020-05-21 10:06:58 +02007534 */
7535static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007536eval_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 +02007537 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 +02007538 struct ly_path_predicate **predicates, enum ly_path_pred_type *pred_type)
Michal Vaskod3678892020-05-21 10:06:58 +02007539{
Michal Vasko004d3152020-06-11 19:59:22 +02007540 LY_ERR ret = LY_SUCCESS;
7541 uint16_t key_count, e_idx, pred_idx = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02007542 const struct lysc_node *key;
Michal Vasko004d3152020-06-11 19:59:22 +02007543 size_t pred_len;
Radek Krejci1deb5be2020-08-26 16:43:36 +02007544 uint32_t prev_lo;
Michal Vasko004d3152020-06-11 19:59:22 +02007545 struct lyxp_expr *exp2 = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02007546
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007547 assert(ctx_node->nodetype & (LYS_LIST | LYS_LEAFLIST));
Michal Vaskod3678892020-05-21 10:06:58 +02007548
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007549 if (ctx_node->nodetype == LYS_LIST) {
Michal Vasko004d3152020-06-11 19:59:22 +02007550 /* get key count */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007551 if (ctx_node->flags & LYS_KEYLESS) {
Michal Vasko004d3152020-06-11 19:59:22 +02007552 return LY_EINVAL;
7553 }
Michal Vasko544e58a2021-01-28 14:33:41 +01007554 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 +02007555 assert(key_count);
Michal Vaskod3678892020-05-21 10:06:58 +02007556
Michal Vasko004d3152020-06-11 19:59:22 +02007557 /* learn where the predicates end */
7558 e_idx = *tok_idx;
7559 while (key_count) {
7560 /* '[' */
7561 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
7562 return LY_EINVAL;
7563 }
7564 ++e_idx;
7565
Michal Vasko3354d272021-04-06 09:40:06 +02007566 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_NAMETEST)) {
7567 /* definitely not a key */
7568 return LY_EINVAL;
7569 }
7570
Michal Vasko004d3152020-06-11 19:59:22 +02007571 /* ']' */
7572 while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) {
7573 ++e_idx;
7574 }
7575 ++e_idx;
7576
7577 /* another presumably key predicate parsed */
7578 --key_count;
7579 }
Michal Vasko004d3152020-06-11 19:59:22 +02007580 } else {
7581 /* learn just where this single predicate ends */
7582 e_idx = *tok_idx;
7583
Michal Vaskod3678892020-05-21 10:06:58 +02007584 /* '[' */
Michal Vasko004d3152020-06-11 19:59:22 +02007585 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
7586 return LY_EINVAL;
7587 }
7588 ++e_idx;
Michal Vaskod3678892020-05-21 10:06:58 +02007589
Michal Vasko3354d272021-04-06 09:40:06 +02007590 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_DOT)) {
7591 /* definitely not the value */
7592 return LY_EINVAL;
7593 }
7594
Michal Vaskod3678892020-05-21 10:06:58 +02007595 /* ']' */
Michal Vasko004d3152020-06-11 19:59:22 +02007596 while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) {
7597 ++e_idx;
7598 }
7599 ++e_idx;
Michal Vaskod3678892020-05-21 10:06:58 +02007600 }
7601
Michal Vasko004d3152020-06-11 19:59:22 +02007602 /* get the joined length of all the keys predicates/of the single leaf-list predicate */
7603 pred_len = (exp->tok_pos[e_idx - 1] + exp->tok_len[e_idx - 1]) - exp->tok_pos[*tok_idx];
7604
7605 /* turn logging off */
7606 prev_lo = ly_log_options(0);
7607
7608 /* parse the predicate(s) */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007609 LY_CHECK_GOTO(ret = ly_path_parse_predicate(ctx_node->module->ctx, ctx_node, exp->expr + exp->tok_pos[*tok_idx],
7610 pred_len, LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &exp2), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02007611
7612 /* compile */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007613 ret = ly_path_compile_predicate(ctx_node->module->ctx, cur_node, cur_mod, ctx_node, exp2, &pred_idx, format,
7614 prefix_data, predicates, pred_type);
Michal Vasko004d3152020-06-11 19:59:22 +02007615 LY_CHECK_GOTO(ret, cleanup);
7616
7617 /* success, the predicate must include all the needed information for hash-based search */
7618 *tok_idx = e_idx;
7619
7620cleanup:
7621 ly_log_options(prev_lo);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007622 lyxp_expr_free(ctx_node->module->ctx, exp2);
Michal Vasko004d3152020-06-11 19:59:22 +02007623 return ret;
Michal Vaskod3678892020-05-21 10:06:58 +02007624}
7625
7626/**
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007627 * @brief Search for/check the next schema node that could be the only matching schema node meaning the
7628 * data node(s) could be found using a single hash-based search.
7629 *
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007630 * @param[in] ctx libyang context.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007631 * @param[in] node Next context node to check.
7632 * @param[in] name Expected node name.
7633 * @param[in] name_len Length of @p name.
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007634 * @param[in] moveto_mod Expected node module, can be NULL for JSON format with no prefix.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007635 * @param[in] root_type XPath root type.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007636 * @param[in] format Prefix format.
7637 * @param[in,out] found Previously found node, is updated.
7638 * @return LY_SUCCESS on success,
7639 * @return LY_ENOT if the whole check failed and hashes cannot be used.
7640 */
7641static LY_ERR
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007642eval_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 +02007643 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 +02007644 const struct lysc_node **found)
7645{
7646 const struct lysc_node *scnode;
7647 const struct lys_module *mod;
7648 uint32_t idx = 0;
7649
Radek Krejci8df109d2021-04-23 12:19:08 +02007650 assert((format == LY_VALUE_JSON) || moveto_mod);
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007651
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007652continue_search:
Michal Vasko7d1d0912020-10-16 08:38:30 +02007653 scnode = NULL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007654 if (!node) {
Radek Krejci8df109d2021-04-23 12:19:08 +02007655 if ((format == LY_VALUE_JSON) && !moveto_mod) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007656 /* search all modules for a single match */
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007657 while ((mod = ly_ctx_get_module_iter(ctx, &idx))) {
Michal Vasko35a3b1d2021-07-14 09:34:16 +02007658 if (!mod->implemented) {
7659 continue;
7660 }
7661
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007662 scnode = lys_find_child(NULL, mod, name, name_len, 0, 0);
7663 if (scnode) {
7664 /* we have found a match */
7665 break;
7666 }
7667 }
7668
Michal Vasko7d1d0912020-10-16 08:38:30 +02007669 if (!scnode) {
7670 /* all modules searched */
7671 idx = 0;
7672 }
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007673 } else {
7674 /* search in top-level */
7675 scnode = lys_find_child(NULL, moveto_mod, name, name_len, 0, 0);
7676 }
7677 } else if (!*found || (lysc_data_parent(*found) != node->schema)) {
Radek Krejci8df109d2021-04-23 12:19:08 +02007678 if ((format == LY_VALUE_JSON) && !moveto_mod) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007679 /* we must adjust the module to inherit the one from the context node */
7680 moveto_mod = node->schema->module;
7681 }
7682
7683 /* search in children, do not repeat the same search */
7684 scnode = lys_find_child(node->schema, moveto_mod, name, name_len, 0, 0);
Michal Vasko7d1d0912020-10-16 08:38:30 +02007685 } /* else skip redundant search */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007686
7687 /* additional context check */
7688 if (scnode && (root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) {
7689 scnode = NULL;
7690 }
7691
7692 if (scnode) {
7693 if (*found) {
7694 /* we found a schema node with the same name but at different level, give up, too complicated
7695 * (more hash-based searches would be required, not supported) */
7696 return LY_ENOT;
7697 } else {
7698 /* remember the found schema node and continue to make sure it can be used */
7699 *found = scnode;
7700 }
7701 }
7702
7703 if (idx) {
7704 /* continue searching all the following models */
7705 goto continue_search;
7706 }
7707
7708 return LY_SUCCESS;
7709}
7710
7711/**
Michal Vasko4ad69e72021-10-26 16:25:55 +02007712 * @brief Generate message when no matching schema nodes were found for a path segment.
7713 *
7714 * @param[in] set XPath set.
7715 * @param[in] scparent Previous schema parent in the context, if only one.
7716 * @param[in] ncname XPath NCName being evaluated.
7717 * @param[in] ncname_len Length of @p ncname.
7718 * @param[in] expr Whole XPath expression.
7719 * @param[in] options XPath options.
7720 */
7721static void
7722eval_name_test_scnode_no_match_msg(struct lyxp_set *set, const struct lyxp_set_scnode *scparent, const char *ncname,
7723 uint16_t ncname_len, const char *expr, uint32_t options)
7724{
7725 const char *format;
7726 char *path = NULL, *ppath = NULL;
7727
7728 path = lysc_path(set->cur_scnode, LYSC_PATH_LOG, NULL, 0);
7729 if (scparent) {
7730 /* generate path for the parent */
7731 if (scparent->type == LYXP_NODE_ELEM) {
7732 ppath = lysc_path(scparent->scnode, LYSC_PATH_LOG, NULL, 0);
7733 } else if (scparent->type == LYXP_NODE_ROOT) {
7734 ppath = strdup("<root>");
7735 } else if (scparent->type == LYXP_NODE_ROOT_CONFIG) {
7736 ppath = strdup("<config-root>");
7737 }
7738 }
7739 if (ppath) {
7740 format = "Schema node \"%.*s\" for parent \"%s\" not found; in expr \"%.*s\" with context node \"%s\".";
7741 if (options & LYXP_SCNODE_ERROR) {
7742 LOGERR(set->ctx, LY_EVALID, format, ncname_len, ncname, ppath, (ncname - expr) + ncname_len, expr, path);
7743 } else {
7744 LOGWRN(set->ctx, format, ncname_len, ncname, ppath, (ncname - expr) + ncname_len, expr, path);
7745 }
7746 } else {
7747 format = "Schema node \"%.*s\" not found; in expr \"%.*s\" with context node \"%s\".";
7748 if (options & LYXP_SCNODE_ERROR) {
7749 LOGERR(set->ctx, LY_EVALID, format, ncname_len, ncname, (ncname - expr) + ncname_len, expr, path);
7750 } else {
7751 LOGWRN(set->ctx, format, ncname_len, ncname, (ncname - expr) + ncname_len, expr, path);
7752 }
7753 }
7754 free(path);
7755 free(ppath);
7756}
7757
7758/**
Michal Vaskod3678892020-05-21 10:06:58 +02007759 * @brief Evaluate NameTest and any following Predicates. Logs directly on error.
7760 *
7761 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
7762 * [6] NodeTest ::= NameTest | NodeType '(' ')'
7763 * [7] NameTest ::= '*' | NCName ':' '*' | QName
7764 *
7765 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007766 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko49fec8e2022-05-24 10:28:33 +02007767 * @param[in] axis What axis to search on.
Michal Vaskod3678892020-05-21 10:06:58 +02007768 * @param[in] all_desc Whether to search all the descendants or children only.
aPiecek8b0cc152021-05-31 16:40:31 +02007769 * @param[in,out] set Context and result set.
Michal Vaskod3678892020-05-21 10:06:58 +02007770 * @param[in] options XPath options.
Michal Vaskoa036a6b2021-10-12 16:05:23 +02007771 * @return LY_ERR (LY_EINCOMPLETE on unresolved when, LY_ENOT for not found schema node)
Michal Vaskod3678892020-05-21 10:06:58 +02007772 */
7773static LY_ERR
Michal Vasko49fec8e2022-05-24 10:28:33 +02007774eval_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 +02007775 struct lyxp_set *set, uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02007776{
Michal Vaskoa036a6b2021-10-12 16:05:23 +02007777 LY_ERR rc = LY_SUCCESS, r;
Michal Vasko004d3152020-06-11 19:59:22 +02007778 const char *ncname, *ncname_dict = NULL;
7779 uint16_t ncname_len;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007780 const struct lys_module *moveto_mod = NULL;
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007781 const struct lysc_node *scnode = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02007782 struct ly_path_predicate *predicates = NULL;
7783 enum ly_path_pred_type pred_type = 0;
Michal Vaskoa036a6b2021-10-12 16:05:23 +02007784 int scnode_skip_pred = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02007785
aPiecek8b0cc152021-05-31 16:40:31 +02007786 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02007787 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007788 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007789
aPiecek8b0cc152021-05-31 16:40:31 +02007790 if (options & LYXP_SKIP_EXPR) {
Michal Vaskod3678892020-05-21 10:06:58 +02007791 goto moveto;
7792 }
7793
Michal Vasko004d3152020-06-11 19:59:22 +02007794 ncname = &exp->expr[exp->tok_pos[*tok_idx - 1]];
7795 ncname_len = exp->tok_len[*tok_idx - 1];
Michal Vaskod3678892020-05-21 10:06:58 +02007796
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007797 if ((ncname[0] == '*') && (ncname_len == 1)) {
7798 /* all nodes will match */
7799 goto moveto;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007800 }
7801
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007802 /* parse (and skip) module name */
7803 rc = moveto_resolve_model(&ncname, &ncname_len, set, NULL, &moveto_mod);
Michal Vaskod3678892020-05-21 10:06:58 +02007804 LY_CHECK_GOTO(rc, cleanup);
7805
Michal Vasko49fec8e2022-05-24 10:28:33 +02007806 if ((ncname[0] == '*') && (ncname_len == 1)) {
7807 /* all nodes from the module will match */
7808 goto moveto;
7809 }
7810
7811 if (((set->format == LY_VALUE_JSON) || moveto_mod) && (axis == LYXP_AXIS_CHILD) && !all_desc &&
7812 (set->type == LYXP_SET_NODE_SET)) {
Michal Vaskod3678892020-05-21 10:06:58 +02007813 /* find the matching schema node in some parent in the context */
Radek Krejci1deb5be2020-08-26 16:43:36 +02007814 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007815 if (eval_name_test_with_predicate_get_scnode(set->ctx, set->val.nodes[i].node, ncname, ncname_len,
7816 moveto_mod, set->root_type, set->format, &scnode)) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007817 /* check failed */
7818 scnode = NULL;
7819 break;
Michal Vaskod3678892020-05-21 10:06:58 +02007820 }
7821 }
7822
Michal Vasko004d3152020-06-11 19:59:22 +02007823 if (scnode && (scnode->nodetype & (LYS_LIST | LYS_LEAFLIST))) {
7824 /* try to create the predicates */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007825 if (eval_name_test_try_compile_predicates(exp, tok_idx, scnode, set->cur_mod, set->cur_node ?
7826 set->cur_node->schema : NULL, set->format, set->prefix_data, &predicates, &pred_type)) {
Michal Vasko004d3152020-06-11 19:59:22 +02007827 /* hashes cannot be used */
Michal Vaskod3678892020-05-21 10:06:58 +02007828 scnode = NULL;
7829 }
7830 }
7831 }
7832
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007833 if (!scnode) {
7834 /* we are not able to match based on a schema node and not all the modules match ("*"),
Michal Vasko004d3152020-06-11 19:59:22 +02007835 * use dictionary for efficient comparison */
Radek Krejci011e4aa2020-09-04 15:22:31 +02007836 LY_CHECK_GOTO(rc = lydict_insert(set->ctx, ncname, ncname_len, &ncname_dict), cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02007837 }
7838
7839moveto:
7840 /* move to the attribute(s), data node(s), or schema node(s) */
Michal Vasko49fec8e2022-05-24 10:28:33 +02007841 if (axis == LYXP_AXIS_ATTRIBUTE) {
aPiecek8b0cc152021-05-31 16:40:31 +02007842 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007843 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskod3678892020-05-21 10:06:58 +02007844 } else {
7845 if (all_desc) {
Michal Vaskocdad7122020-11-09 21:04:44 +01007846 rc = moveto_attr_alldesc(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007847 } else {
aPiecek8b0cc152021-05-31 16:40:31 +02007848 rc = moveto_attr(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007849 }
7850 LY_CHECK_GOTO(rc, cleanup);
7851 }
7852 } else {
aPiecek8b0cc152021-05-31 16:40:31 +02007853 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02007854 int64_t i;
Michal Vaskoa036a6b2021-10-12 16:05:23 +02007855 const struct lyxp_set_scnode *scparent = NULL;
Michal Vaskoa036a6b2021-10-12 16:05:23 +02007856
7857 /* remember parent if there is only one, to print in the warning */
7858 for (i = 0; i < set->used; ++i) {
7859 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
7860 if (!scparent) {
7861 /* remember the context node */
7862 scparent = &set->val.scnodes[i];
7863 } else {
7864 /* several context nodes, no reasonable error possible */
7865 scparent = NULL;
7866 break;
7867 }
7868 }
7869 }
Radek Krejci1deb5be2020-08-26 16:43:36 +02007870
Michal Vasko49fec8e2022-05-24 10:28:33 +02007871 if (all_desc && (axis == LYXP_AXIS_CHILD)) {
7872 /* efficient evaluation that does not add all the descendants into the set */
7873 rc = moveto_scnode_alldesc_child(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007874 } else {
Michal Vasko49fec8e2022-05-24 10:28:33 +02007875 if (all_desc) {
7876 /* "//" == "/descendant-or-self::node()/" */
7877 rc = xpath_pi_node(set, LYXP_AXIS_DESCENDANT_OR_SELF, options);
7878 LY_CHECK_GOTO(rc, cleanup);
7879 }
7880 rc = moveto_scnode(set, moveto_mod, ncname_dict, axis, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007881 }
7882 LY_CHECK_GOTO(rc, cleanup);
7883
7884 for (i = set->used - 1; i > -1; --i) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007885 if (set->val.scnodes[i].in_ctx > LYXP_SET_SCNODE_ATOM_NODE) {
Michal Vaskod3678892020-05-21 10:06:58 +02007886 break;
7887 }
7888 }
7889 if (i == -1) {
Michal Vasko4ad69e72021-10-26 16:25:55 +02007890 /* generate message */
7891 eval_name_test_scnode_no_match_msg(set, scparent, ncname, ncname_len, exp->expr, options);
7892
7893 if (options & LYXP_SCNODE_ERROR) {
7894 /* error */
7895 rc = LY_EVALID;
7896 goto cleanup;
Michal Vaskoa036a6b2021-10-12 16:05:23 +02007897 }
Michal Vaskoa036a6b2021-10-12 16:05:23 +02007898
7899 /* skip the predicates and the rest of this path to not generate invalid warnings */
7900 rc = LY_ENOT;
7901 scnode_skip_pred = 1;
Michal Vaskod3678892020-05-21 10:06:58 +02007902 }
7903 } else {
Michal Vasko49fec8e2022-05-24 10:28:33 +02007904 if (all_desc && (axis == LYXP_AXIS_CHILD)) {
7905 /* efficient evaluation */
7906 rc = moveto_node_alldesc_child(set, moveto_mod, ncname_dict, options);
7907 } else if (scnode && (axis == LYXP_AXIS_CHILD)) {
7908 /* we can find the child nodes using hashes */
7909 rc = moveto_node_hash_child(set, scnode, predicates, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007910 } else {
Michal Vasko49fec8e2022-05-24 10:28:33 +02007911 if (all_desc) {
7912 /* "//" == "/descendant-or-self::node()/" */
7913 rc = xpath_pi_node(set, LYXP_AXIS_DESCENDANT_OR_SELF, options);
7914 LY_CHECK_GOTO(rc, cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02007915 }
Michal Vasko49fec8e2022-05-24 10:28:33 +02007916 rc = moveto_node(set, moveto_mod, ncname_dict, axis, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007917 }
7918 LY_CHECK_GOTO(rc, cleanup);
7919 }
7920 }
7921
Michal Vaskoa036a6b2021-10-12 16:05:23 +02007922 if (scnode_skip_pred) {
7923 /* skip predicates */
7924 options |= LYXP_SKIP_EXPR;
7925 }
7926
Michal Vaskod3678892020-05-21 10:06:58 +02007927 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007928 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02007929 r = eval_predicate(exp, tok_idx, set, options, axis);
Michal Vaskoa036a6b2021-10-12 16:05:23 +02007930 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02007931 }
7932
7933cleanup:
Michal Vaskoa036a6b2021-10-12 16:05:23 +02007934 if (scnode_skip_pred) {
7935 /* restore options */
7936 options &= ~LYXP_SKIP_EXPR;
7937 }
aPiecek8b0cc152021-05-31 16:40:31 +02007938 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko004d3152020-06-11 19:59:22 +02007939 lydict_remove(set->ctx, ncname_dict);
Michal Vaskof7e16e22020-10-21 09:24:39 +02007940 ly_path_predicates_free(set->ctx, pred_type, predicates);
Michal Vaskodb51a8d2020-05-27 15:22:29 +02007941 }
Michal Vaskod3678892020-05-21 10:06:58 +02007942 return rc;
7943}
7944
7945/**
7946 * @brief Evaluate NodeType and any following Predicates. Logs directly on error.
7947 *
7948 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
7949 * [6] NodeTest ::= NameTest | NodeType '(' ')'
7950 * [8] NodeType ::= 'text' | 'node'
7951 *
7952 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007953 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko49fec8e2022-05-24 10:28:33 +02007954 * @param[in] axis Axis to search on.
7955 * @param[in] all_desc Whether to search all the descendants or axis only.
aPiecek8b0cc152021-05-31 16:40:31 +02007956 * @param[in,out] set Context and result set.
Michal Vaskod3678892020-05-21 10:06:58 +02007957 * @param[in] options XPath options.
7958 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7959 */
7960static LY_ERR
Michal Vasko49fec8e2022-05-24 10:28:33 +02007961eval_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 +02007962 struct lyxp_set *set, uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02007963{
7964 LY_ERR rc;
7965
Michal Vaskod3678892020-05-21 10:06:58 +02007966 (void)all_desc;
7967
aPiecek8b0cc152021-05-31 16:40:31 +02007968 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko004d3152020-06-11 19:59:22 +02007969 assert(exp->tok_len[*tok_idx] == 4);
Michal Vasko49fec8e2022-05-24 10:28:33 +02007970 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "node", 4)) {
7971 rc = xpath_pi_node(set, axis, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007972 } else {
Michal Vasko49fec8e2022-05-24 10:28:33 +02007973 assert(!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "text", 4));
7974 rc = xpath_pi_text(set, axis, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007975 }
Michal Vasko49fec8e2022-05-24 10:28:33 +02007976 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02007977 }
aPiecek8b0cc152021-05-31 16:40:31 +02007978 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02007979 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007980 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007981
7982 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02007983 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
aPiecek8b0cc152021-05-31 16:40:31 +02007984 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02007985 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007986 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007987
7988 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007989 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
aPiecek8b0cc152021-05-31 16:40:31 +02007990 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02007991 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007992 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007993
7994 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007995 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02007996 rc = eval_predicate(exp, tok_idx, set, options, axis);
Michal Vaskod3678892020-05-21 10:06:58 +02007997 LY_CHECK_RET(rc);
7998 }
7999
8000 return LY_SUCCESS;
8001}
8002
8003/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02008004 * @brief Evaluate RelativeLocationPath. Logs directly on error.
8005 *
8006 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
8007 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
Michal Vaskod3678892020-05-21 10:06:58 +02008008 * [6] NodeTest ::= NameTest | NodeType '(' ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02008009 *
8010 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008011 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008012 * @param[in] all_desc Whether to search all the descendants or children only.
aPiecek8b0cc152021-05-31 16:40:31 +02008013 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008014 * @param[in] options XPath options.
8015 * @return LY_ERR (YL_EINCOMPLETE on unresolved when)
8016 */
8017static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008018eval_relative_location_path(const struct lyxp_expr *exp, uint16_t *tok_idx, ly_bool all_desc, struct lyxp_set *set,
8019 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008020{
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008021 LY_ERR rc = LY_SUCCESS;
Michal Vasko49fec8e2022-05-24 10:28:33 +02008022 enum lyxp_axis axis;
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008023 int scnode_skip_path = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008024
8025 goto step;
8026 do {
8027 /* evaluate '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02008028 if (exp->tok_len[*tok_idx] == 1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008029 all_desc = 0;
8030 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02008031 assert(exp->tok_len[*tok_idx] == 2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008032 all_desc = 1;
8033 }
aPiecek8b0cc152021-05-31 16:40:31 +02008034 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008035 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008036 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008037
8038step:
Michal Vasko49fec8e2022-05-24 10:28:33 +02008039 /* AxisSpecifier */
8040 if (exp->tokens[*tok_idx] == LYXP_TOKEN_AXISNAME) {
8041 axis = str2axis(exp->expr + exp->tok_pos[*tok_idx], exp->tok_len[*tok_idx]);
Michal Vaskod3678892020-05-21 10:06:58 +02008042
aPiecek8b0cc152021-05-31 16:40:31 +02008043 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008044 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
8045 ++(*tok_idx);
8046
8047 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_DCOLON);
8048 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
8049 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
8050 ++(*tok_idx);
8051 } else if (exp->tokens[*tok_idx] == LYXP_TOKEN_AT) {
8052 axis = LYXP_AXIS_ATTRIBUTE;
8053
8054 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
8055 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008056 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02008057 } else {
Michal Vasko49fec8e2022-05-24 10:28:33 +02008058 /* default */
8059 axis = LYXP_AXIS_CHILD;
Michal Vaskod3678892020-05-21 10:06:58 +02008060 }
8061
Michal Vasko49fec8e2022-05-24 10:28:33 +02008062 /* NodeTest Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02008063 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008064 case LYXP_TOKEN_DOT:
8065 /* evaluate '.' */
Michal Vasko49fec8e2022-05-24 10:28:33 +02008066 if (!(options & LYXP_SKIP_EXPR)) {
8067 if (((options & LYXP_SCNODE_ALL) && (set->type != LYXP_SET_SCNODE_SET)) ||
8068 (!(options & LYXP_SCNODE_ALL) && (set->type != LYXP_SET_NODE_SET))) {
8069 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
8070 rc = LY_EVALID;
8071 goto cleanup;
8072 }
8073
8074 if (all_desc) {
8075 rc = xpath_pi_node(set, LYXP_AXIS_DESCENDANT_OR_SELF, options);
8076 LY_CHECK_GOTO(rc, cleanup);
8077 }
8078 rc = xpath_pi_node(set, LYXP_AXIS_SELF, options);
8079 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008080 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008081 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008082 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008083 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008084 break;
8085
8086 case LYXP_TOKEN_DDOT:
8087 /* evaluate '..' */
Michal Vasko49fec8e2022-05-24 10:28:33 +02008088 if (!(options & LYXP_SKIP_EXPR)) {
8089 if (((options & LYXP_SCNODE_ALL) && (set->type != LYXP_SET_SCNODE_SET)) ||
8090 (!(options & LYXP_SCNODE_ALL) && (set->type != LYXP_SET_NODE_SET))) {
8091 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
8092 rc = LY_EVALID;
8093 goto cleanup;
8094 }
8095
8096 if (all_desc) {
8097 rc = xpath_pi_node(set, LYXP_AXIS_DESCENDANT_OR_SELF, options);
8098 LY_CHECK_GOTO(rc, cleanup);
8099 }
8100 rc = xpath_pi_node(set, LYXP_AXIS_PARENT, options);
8101 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008102 }
aPiecek8b0cc152021-05-31 16:40:31 +02008103 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008104 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008105 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008106 break;
8107
Michal Vasko03ff5a72019-09-11 13:49:33 +02008108 case LYXP_TOKEN_NAMETEST:
Michal Vaskod3678892020-05-21 10:06:58 +02008109 /* evaluate NameTest Predicate* */
Michal Vasko49fec8e2022-05-24 10:28:33 +02008110 rc = eval_name_test_with_predicate(exp, tok_idx, axis, all_desc, set, options);
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008111 if (rc == LY_ENOT) {
8112 assert(options & LYXP_SCNODE_ALL);
8113 /* skip the rest of this path */
8114 rc = LY_SUCCESS;
8115 scnode_skip_path = 1;
8116 options |= LYXP_SKIP_EXPR;
8117 }
8118 LY_CHECK_GOTO(rc, cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02008119 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008120
Michal Vaskod3678892020-05-21 10:06:58 +02008121 case LYXP_TOKEN_NODETYPE:
8122 /* evaluate NodeType Predicate* */
Michal Vasko49fec8e2022-05-24 10:28:33 +02008123 rc = eval_node_type_with_predicate(exp, tok_idx, axis, all_desc, set, options);
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008124 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008125 break;
8126
8127 default:
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008128 LOGINT(set->ctx);
8129 rc = LY_EINT;
8130 goto cleanup;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008131 }
Michal Vasko004d3152020-06-11 19:59:22 +02008132 } while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008133
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008134cleanup:
8135 if (scnode_skip_path) {
8136 options &= ~LYXP_SKIP_EXPR;
8137 }
8138 return rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008139}
8140
8141/**
8142 * @brief Evaluate AbsoluteLocationPath. Logs directly on error.
8143 *
8144 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
8145 *
8146 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008147 * @param[in] tok_idx Position in the expression @p exp.
aPiecek8b0cc152021-05-31 16:40:31 +02008148 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008149 * @param[in] options XPath options.
8150 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8151 */
8152static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008153eval_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 +02008154{
Radek Krejci857189e2020-09-01 13:26:36 +02008155 ly_bool all_desc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008156
aPiecek8b0cc152021-05-31 16:40:31 +02008157 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008158 /* no matter what tokens follow, we need to be at the root */
Michal Vaskob0099a92020-08-31 14:55:23 +02008159 LY_CHECK_RET(moveto_root(set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008160 }
8161
8162 /* '/' RelativeLocationPath? */
Michal Vasko004d3152020-06-11 19:59:22 +02008163 if (exp->tok_len[*tok_idx] == 1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008164 /* evaluate '/' - deferred */
8165 all_desc = 0;
aPiecek8b0cc152021-05-31 16:40:31 +02008166 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008167 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008168 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008169
Michal Vasko004d3152020-06-11 19:59:22 +02008170 if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008171 return LY_SUCCESS;
8172 }
Michal Vasko004d3152020-06-11 19:59:22 +02008173 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008174 case LYXP_TOKEN_DOT:
8175 case LYXP_TOKEN_DDOT:
Michal Vasko49fec8e2022-05-24 10:28:33 +02008176 case LYXP_TOKEN_AXISNAME:
Michal Vasko03ff5a72019-09-11 13:49:33 +02008177 case LYXP_TOKEN_AT:
8178 case LYXP_TOKEN_NAMETEST:
8179 case LYXP_TOKEN_NODETYPE:
Michal Vaskob0099a92020-08-31 14:55:23 +02008180 LY_CHECK_RET(eval_relative_location_path(exp, tok_idx, all_desc, set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008181 break;
8182 default:
8183 break;
8184 }
8185
Michal Vasko03ff5a72019-09-11 13:49:33 +02008186 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02008187 /* '//' RelativeLocationPath */
Michal Vasko03ff5a72019-09-11 13:49:33 +02008188 /* evaluate '//' - deferred so as not to waste memory by remembering all the nodes */
8189 all_desc = 1;
aPiecek8b0cc152021-05-31 16:40:31 +02008190 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008191 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008192 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008193
Michal Vaskob0099a92020-08-31 14:55:23 +02008194 LY_CHECK_RET(eval_relative_location_path(exp, tok_idx, all_desc, set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008195 }
8196
8197 return LY_SUCCESS;
8198}
8199
8200/**
8201 * @brief Evaluate FunctionCall. Logs directly on error.
8202 *
Michal Vaskod3678892020-05-21 10:06:58 +02008203 * [11] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02008204 *
8205 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008206 * @param[in] tok_idx Position in the expression @p exp.
aPiecek8b0cc152021-05-31 16:40:31 +02008207 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008208 * @param[in] options XPath options.
8209 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8210 */
8211static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008212eval_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 +02008213{
8214 LY_ERR rc;
Michal Vasko69730152020-10-09 16:30:07 +02008215
Radek Krejci1deb5be2020-08-26 16:43:36 +02008216 LY_ERR (*xpath_func)(struct lyxp_set **, uint16_t, struct lyxp_set *, uint32_t) = NULL;
Michal Vasko0cbf54f2019-12-16 10:01:06 +01008217 uint16_t arg_count = 0, i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008218 struct lyxp_set **args = NULL, **args_aux;
8219
aPiecek8b0cc152021-05-31 16:40:31 +02008220 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008221 /* FunctionName */
Michal Vasko004d3152020-06-11 19:59:22 +02008222 switch (exp->tok_len[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008223 case 3:
Michal Vasko004d3152020-06-11 19:59:22 +02008224 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008225 xpath_func = &xpath_not;
Michal Vasko004d3152020-06-11 19:59:22 +02008226 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008227 xpath_func = &xpath_sum;
8228 }
8229 break;
8230 case 4:
Michal Vasko004d3152020-06-11 19:59:22 +02008231 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008232 xpath_func = &xpath_lang;
Michal Vasko004d3152020-06-11 19:59:22 +02008233 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008234 xpath_func = &xpath_last;
Michal Vasko004d3152020-06-11 19:59:22 +02008235 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008236 xpath_func = &xpath_name;
Michal Vasko004d3152020-06-11 19:59:22 +02008237 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008238 xpath_func = &xpath_true;
8239 }
8240 break;
8241 case 5:
Michal Vasko004d3152020-06-11 19:59:22 +02008242 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008243 xpath_func = &xpath_count;
Michal Vasko004d3152020-06-11 19:59:22 +02008244 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008245 xpath_func = &xpath_false;
Michal Vasko004d3152020-06-11 19:59:22 +02008246 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008247 xpath_func = &xpath_floor;
Michal Vasko004d3152020-06-11 19:59:22 +02008248 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008249 xpath_func = &xpath_round;
Michal Vasko004d3152020-06-11 19:59:22 +02008250 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008251 xpath_func = &xpath_deref;
8252 }
8253 break;
8254 case 6:
Michal Vasko004d3152020-06-11 19:59:22 +02008255 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008256 xpath_func = &xpath_concat;
Michal Vasko004d3152020-06-11 19:59:22 +02008257 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008258 xpath_func = &xpath_number;
Michal Vasko004d3152020-06-11 19:59:22 +02008259 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008260 xpath_func = &xpath_string;
8261 }
8262 break;
8263 case 7:
Michal Vasko004d3152020-06-11 19:59:22 +02008264 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008265 xpath_func = &xpath_boolean;
Michal Vasko004d3152020-06-11 19:59:22 +02008266 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008267 xpath_func = &xpath_ceiling;
Michal Vasko004d3152020-06-11 19:59:22 +02008268 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008269 xpath_func = &xpath_current;
8270 }
8271 break;
8272 case 8:
Michal Vasko004d3152020-06-11 19:59:22 +02008273 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008274 xpath_func = &xpath_contains;
Michal Vasko004d3152020-06-11 19:59:22 +02008275 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008276 xpath_func = &xpath_position;
Michal Vasko004d3152020-06-11 19:59:22 +02008277 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "re-match", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008278 xpath_func = &xpath_re_match;
8279 }
8280 break;
8281 case 9:
Michal Vasko004d3152020-06-11 19:59:22 +02008282 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008283 xpath_func = &xpath_substring;
Michal Vasko004d3152020-06-11 19:59:22 +02008284 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008285 xpath_func = &xpath_translate;
8286 }
8287 break;
8288 case 10:
Michal Vasko004d3152020-06-11 19:59:22 +02008289 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008290 xpath_func = &xpath_local_name;
Michal Vasko004d3152020-06-11 19:59:22 +02008291 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008292 xpath_func = &xpath_enum_value;
Michal Vasko004d3152020-06-11 19:59:22 +02008293 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008294 xpath_func = &xpath_bit_is_set;
8295 }
8296 break;
8297 case 11:
Michal Vasko004d3152020-06-11 19:59:22 +02008298 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008299 xpath_func = &xpath_starts_with;
8300 }
8301 break;
8302 case 12:
Michal Vasko004d3152020-06-11 19:59:22 +02008303 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008304 xpath_func = &xpath_derived_from;
8305 }
8306 break;
8307 case 13:
Michal Vasko004d3152020-06-11 19:59:22 +02008308 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008309 xpath_func = &xpath_namespace_uri;
Michal Vasko004d3152020-06-11 19:59:22 +02008310 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string-length", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008311 xpath_func = &xpath_string_length;
8312 }
8313 break;
8314 case 15:
Michal Vasko004d3152020-06-11 19:59:22 +02008315 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008316 xpath_func = &xpath_normalize_space;
Michal Vasko004d3152020-06-11 19:59:22 +02008317 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-after", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008318 xpath_func = &xpath_substring_after;
8319 }
8320 break;
8321 case 16:
Michal Vasko004d3152020-06-11 19:59:22 +02008322 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008323 xpath_func = &xpath_substring_before;
8324 }
8325 break;
8326 case 20:
Michal Vasko004d3152020-06-11 19:59:22 +02008327 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from-or-self", 20)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008328 xpath_func = &xpath_derived_from_or_self;
8329 }
8330 break;
8331 }
8332
8333 if (!xpath_func) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01008334 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 +02008335 return LY_EVALID;
8336 }
8337 }
8338
aPiecek8b0cc152021-05-31 16:40:31 +02008339 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008340 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008341 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008342
8343 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02008344 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
aPiecek8b0cc152021-05-31 16:40:31 +02008345 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008346 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008347 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008348
8349 /* ( Expr ( ',' Expr )* )? */
Michal Vasko004d3152020-06-11 19:59:22 +02008350 if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) {
aPiecek8b0cc152021-05-31 16:40:31 +02008351 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008352 args = malloc(sizeof *args);
8353 LY_CHECK_ERR_GOTO(!args, LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
8354 arg_count = 1;
8355 args[0] = set_copy(set);
8356 if (!args[0]) {
8357 rc = LY_EMEM;
8358 goto cleanup;
8359 }
8360
Michal Vasko004d3152020-06-11 19:59:22 +02008361 rc = eval_expr_select(exp, tok_idx, 0, args[0], options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008362 LY_CHECK_GOTO(rc, cleanup);
8363 } else {
aPiecek8b0cc152021-05-31 16:40:31 +02008364 rc = eval_expr_select(exp, tok_idx, 0, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008365 LY_CHECK_GOTO(rc, cleanup);
8366 }
8367 }
Michal Vasko004d3152020-06-11 19:59:22 +02008368 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) {
aPiecek8b0cc152021-05-31 16:40:31 +02008369 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008370 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008371 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008372
aPiecek8b0cc152021-05-31 16:40:31 +02008373 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008374 ++arg_count;
8375 args_aux = realloc(args, arg_count * sizeof *args);
8376 LY_CHECK_ERR_GOTO(!args_aux, arg_count--; LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
8377 args = args_aux;
8378 args[arg_count - 1] = set_copy(set);
8379 if (!args[arg_count - 1]) {
8380 rc = LY_EMEM;
8381 goto cleanup;
8382 }
8383
Michal Vasko004d3152020-06-11 19:59:22 +02008384 rc = eval_expr_select(exp, tok_idx, 0, args[arg_count - 1], options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008385 LY_CHECK_GOTO(rc, cleanup);
8386 } else {
aPiecek8b0cc152021-05-31 16:40:31 +02008387 rc = eval_expr_select(exp, tok_idx, 0, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008388 LY_CHECK_GOTO(rc, cleanup);
8389 }
8390 }
8391
8392 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02008393 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
aPiecek8b0cc152021-05-31 16:40:31 +02008394 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008395 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008396 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008397
aPiecek8b0cc152021-05-31 16:40:31 +02008398 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008399 /* evaluate function */
8400 rc = xpath_func(args, arg_count, set, options);
8401
8402 if (options & LYXP_SCNODE_ALL) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008403 /* merge all nodes from arg evaluations */
8404 for (i = 0; i < arg_count; ++i) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008405 set_scnode_clear_ctx(args[i], LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008406 lyxp_set_scnode_merge(set, args[i]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008407 }
8408 }
8409 } else {
8410 rc = LY_SUCCESS;
8411 }
8412
8413cleanup:
8414 for (i = 0; i < arg_count; ++i) {
8415 lyxp_set_free(args[i]);
8416 }
8417 free(args);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008418 return rc;
8419}
8420
8421/**
8422 * @brief Evaluate Number. Logs directly on error.
8423 *
8424 * @param[in] ctx Context for errors.
8425 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008426 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008427 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8428 * @return LY_ERR
8429 */
8430static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008431eval_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 +02008432{
8433 long double num;
8434 char *endptr;
8435
8436 if (set) {
8437 errno = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02008438 num = strtold(&exp->expr[exp->tok_pos[*tok_idx]], &endptr);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008439 if (errno) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01008440 LOGVAL(ctx, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]);
8441 LOGVAL(ctx, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double (%s).",
Michal Vasko69730152020-10-09 16:30:07 +02008442 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]], strerror(errno));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008443 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02008444 } else if (endptr - &exp->expr[exp->tok_pos[*tok_idx]] != exp->tok_len[*tok_idx]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01008445 LOGVAL(ctx, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]);
8446 LOGVAL(ctx, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double.",
Michal Vasko69730152020-10-09 16:30:07 +02008447 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008448 return LY_EVALID;
8449 }
8450
8451 set_fill_number(set, num);
8452 }
8453
8454 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008455 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008456 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008457 return LY_SUCCESS;
8458}
8459
aPiecekdf23eee2021-10-07 12:21:50 +02008460LY_ERR
8461lyxp_vars_find(struct lyxp_var *vars, const char *name, size_t name_len, struct lyxp_var **var)
8462{
8463 LY_ERR ret = LY_ENOTFOUND;
8464 LY_ARRAY_COUNT_TYPE u;
8465
8466 assert(vars && name);
8467
8468 name_len = name_len ? name_len : strlen(name);
8469
8470 LY_ARRAY_FOR(vars, u) {
8471 if (!strncmp(vars[u].name, name, name_len)) {
8472 ret = LY_SUCCESS;
8473 break;
8474 }
8475 }
8476
8477 if (var && !ret) {
8478 *var = &vars[u];
8479 }
8480
8481 return ret;
8482}
8483
Michal Vasko03ff5a72019-09-11 13:49:33 +02008484/**
aPiecekfba75362021-10-07 12:39:48 +02008485 * @brief Evaluate VariableReference.
8486 *
8487 * @param[in] exp Parsed XPath expression.
8488 * @param[in] tok_idx Position in the expression @p exp.
8489 * @param[in] vars [Sized array](@ref sizedarrays) of XPath variables.
8490 * @param[in,out] set Context and result set.
8491 * @param[in] options XPath options.
8492 * @return LY_ERR value.
8493 */
8494static LY_ERR
8495eval_variable_reference(const struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set, uint32_t options)
8496{
8497 LY_ERR ret;
8498 const char *name;
8499 struct lyxp_var *var;
8500 const struct lyxp_var *vars;
8501 struct lyxp_expr *tokens = NULL;
8502 uint16_t token_index;
8503
8504 vars = set->vars;
8505
Michal Vasko49fec8e2022-05-24 10:28:33 +02008506 /* find out the name and value of the variable */
aPiecekfba75362021-10-07 12:39:48 +02008507 name = &exp->expr[exp->tok_pos[*tok_idx]];
8508 ret = lyxp_vars_find((struct lyxp_var *)vars, name, exp->tok_len[*tok_idx], &var);
8509 LY_CHECK_ERR_RET(ret, LOGERR(set->ctx, ret,
8510 "XPath variable \"%s\" not defined.", name), ret);
8511
Michal Vasko49fec8e2022-05-24 10:28:33 +02008512 /* parse value */
aPiecekfba75362021-10-07 12:39:48 +02008513 ret = lyxp_expr_parse(set->ctx, var->value, 0, 1, &tokens);
8514 LY_CHECK_GOTO(ret, cleanup);
8515
Michal Vasko49fec8e2022-05-24 10:28:33 +02008516 /* evaluate value */
aPiecekfba75362021-10-07 12:39:48 +02008517 token_index = 0;
8518 ret = eval_expr_select(tokens, &token_index, 0, set, options);
8519 LY_CHECK_GOTO(ret, cleanup);
8520
8521cleanup:
8522 lyxp_expr_free(set->ctx, tokens);
8523
8524 return ret;
8525}
8526
8527/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02008528 * @brief Evaluate PathExpr. Logs directly on error.
8529 *
Michal Vaskod3678892020-05-21 10:06:58 +02008530 * [12] PathExpr ::= LocationPath | PrimaryExpr Predicate*
Michal Vasko03ff5a72019-09-11 13:49:33 +02008531 * | PrimaryExpr Predicate* '/' RelativeLocationPath
8532 * | PrimaryExpr Predicate* '//' RelativeLocationPath
8533 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
aPiecekfba75362021-10-07 12:39:48 +02008534 * [10] PrimaryExpr ::= VariableReference | '(' Expr ')' | Literal | Number | FunctionCall
Michal Vasko03ff5a72019-09-11 13:49:33 +02008535 *
8536 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008537 * @param[in] tok_idx Position in the expression @p exp.
aPiecek8b0cc152021-05-31 16:40:31 +02008538 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008539 * @param[in] options XPath options.
8540 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8541 */
8542static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008543eval_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 +02008544{
Michal Vasko49fec8e2022-05-24 10:28:33 +02008545 ly_bool all_desc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008546 LY_ERR rc;
8547
Michal Vasko004d3152020-06-11 19:59:22 +02008548 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008549 case LYXP_TOKEN_PAR1:
8550 /* '(' Expr ')' */
8551
8552 /* '(' */
aPiecek8b0cc152021-05-31 16:40:31 +02008553 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008554 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008555 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008556
8557 /* Expr */
Michal Vasko004d3152020-06-11 19:59:22 +02008558 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008559 LY_CHECK_RET(rc);
8560
8561 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02008562 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
aPiecek8b0cc152021-05-31 16:40:31 +02008563 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008564 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008565 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008566
Michal Vasko03ff5a72019-09-11 13:49:33 +02008567 goto predicate;
8568
8569 case LYXP_TOKEN_DOT:
8570 case LYXP_TOKEN_DDOT:
Michal Vasko49fec8e2022-05-24 10:28:33 +02008571 case LYXP_TOKEN_AXISNAME:
Michal Vasko03ff5a72019-09-11 13:49:33 +02008572 case LYXP_TOKEN_AT:
8573 case LYXP_TOKEN_NAMETEST:
8574 case LYXP_TOKEN_NODETYPE:
8575 /* RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02008576 rc = eval_relative_location_path(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008577 LY_CHECK_RET(rc);
8578 break;
8579
aPiecekfba75362021-10-07 12:39:48 +02008580 case LYXP_TOKEN_VARREF:
8581 /* VariableReference */
8582 rc = eval_variable_reference(exp, tok_idx, set, options);
8583 LY_CHECK_RET(rc);
8584 ++(*tok_idx);
8585
aPiecekfba75362021-10-07 12:39:48 +02008586 goto predicate;
8587
Michal Vasko03ff5a72019-09-11 13:49:33 +02008588 case LYXP_TOKEN_FUNCNAME:
8589 /* FunctionCall */
aPiecek8b0cc152021-05-31 16:40:31 +02008590 rc = eval_function_call(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008591 LY_CHECK_RET(rc);
8592
Michal Vasko03ff5a72019-09-11 13:49:33 +02008593 goto predicate;
8594
Michal Vasko3e48bf32020-06-01 08:39:07 +02008595 case LYXP_TOKEN_OPER_PATH:
8596 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +02008597 /* AbsoluteLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02008598 rc = eval_absolute_location_path(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008599 LY_CHECK_RET(rc);
8600 break;
8601
8602 case LYXP_TOKEN_LITERAL:
8603 /* Literal */
aPiecek8b0cc152021-05-31 16:40:31 +02008604 if ((options & LYXP_SKIP_EXPR) || (options & LYXP_SCNODE_ALL)) {
8605 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008606 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008607 }
Michal Vasko004d3152020-06-11 19:59:22 +02008608 eval_literal(exp, tok_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008609 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02008610 eval_literal(exp, tok_idx, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008611 }
8612
Michal Vasko03ff5a72019-09-11 13:49:33 +02008613 goto predicate;
8614
8615 case LYXP_TOKEN_NUMBER:
8616 /* Number */
aPiecek8b0cc152021-05-31 16:40:31 +02008617 if ((options & LYXP_SKIP_EXPR) || (options & LYXP_SCNODE_ALL)) {
8618 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008619 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008620 }
Michal Vasko004d3152020-06-11 19:59:22 +02008621 rc = eval_number(NULL, exp, tok_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008622 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02008623 rc = eval_number(set->ctx, exp, tok_idx, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008624 }
8625 LY_CHECK_RET(rc);
8626
Michal Vasko03ff5a72019-09-11 13:49:33 +02008627 goto predicate;
8628
8629 default:
Michal Vasko49fec8e2022-05-24 10:28:33 +02008630 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 +02008631 return LY_EVALID;
8632 }
8633
8634 return LY_SUCCESS;
8635
8636predicate:
8637 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02008638 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02008639 rc = eval_predicate(exp, tok_idx, set, options, LYXP_AXIS_CHILD);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008640 LY_CHECK_RET(rc);
8641 }
8642
8643 /* ('/' or '//') RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02008644 if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008645
8646 /* evaluate '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02008647 if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008648 all_desc = 0;
8649 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008650 all_desc = 1;
8651 }
8652
aPiecek8b0cc152021-05-31 16:40:31 +02008653 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008654 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008655 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008656
Michal Vasko004d3152020-06-11 19:59:22 +02008657 rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008658 LY_CHECK_RET(rc);
8659 }
8660
8661 return LY_SUCCESS;
8662}
8663
8664/**
8665 * @brief Evaluate UnionExpr. Logs directly on error.
8666 *
Michal Vaskod3678892020-05-21 10:06:58 +02008667 * [20] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008668 *
8669 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008670 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008671 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008672 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008673 * @param[in] options XPath options.
8674 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8675 */
8676static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008677eval_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 +02008678{
8679 LY_ERR rc = LY_SUCCESS;
8680 struct lyxp_set orig_set, set2;
8681 uint16_t i;
8682
8683 assert(repeat);
8684
8685 set_init(&orig_set, set);
8686 set_init(&set2, set);
8687
8688 set_fill_set(&orig_set, set);
8689
Michal Vasko004d3152020-06-11 19:59:22 +02008690 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008691 LY_CHECK_GOTO(rc, cleanup);
8692
8693 /* ('|' PathExpr)* */
8694 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008695 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_UNI);
aPiecek8b0cc152021-05-31 16:40:31 +02008696 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008697 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008698 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008699
aPiecek8b0cc152021-05-31 16:40:31 +02008700 if (options & LYXP_SKIP_EXPR) {
8701 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008702 LY_CHECK_GOTO(rc, cleanup);
8703 continue;
8704 }
8705
8706 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008707 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008708 LY_CHECK_GOTO(rc, cleanup);
8709
8710 /* eval */
8711 if (options & LYXP_SCNODE_ALL) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01008712 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008713 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008714 rc = moveto_union(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008715 LY_CHECK_GOTO(rc, cleanup);
8716 }
8717 }
8718
8719cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008720 lyxp_set_free_content(&orig_set);
8721 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008722 return rc;
8723}
8724
8725/**
8726 * @brief Evaluate UnaryExpr. Logs directly on error.
8727 *
Michal Vaskod3678892020-05-21 10:06:58 +02008728 * [19] UnaryExpr ::= UnionExpr | '-' UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008729 *
8730 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008731 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008732 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008733 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008734 * @param[in] options XPath options.
8735 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8736 */
8737static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008738eval_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 +02008739{
8740 LY_ERR rc;
8741 uint16_t this_op, i;
8742
8743 assert(repeat);
8744
8745 /* ('-')+ */
Michal Vasko004d3152020-06-11 19:59:22 +02008746 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008747 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008748 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 +02008749
aPiecek8b0cc152021-05-31 16:40:31 +02008750 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008751 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008752 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008753 }
8754
Michal Vasko004d3152020-06-11 19:59:22 +02008755 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNARY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008756 LY_CHECK_RET(rc);
8757
aPiecek8b0cc152021-05-31 16:40:31 +02008758 if (!(options & LYXP_SKIP_EXPR) && (repeat % 2)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008759 if (options & LYXP_SCNODE_ALL) {
8760 warn_operands(set->ctx, set, NULL, 1, exp->expr, exp->tok_pos[this_op]);
8761 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008762 rc = moveto_op_math(set, NULL, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008763 LY_CHECK_RET(rc);
8764 }
8765 }
8766
8767 return LY_SUCCESS;
8768}
8769
8770/**
8771 * @brief Evaluate MultiplicativeExpr. Logs directly on error.
8772 *
Michal Vaskod3678892020-05-21 10:06:58 +02008773 * [18] MultiplicativeExpr ::= UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008774 * | MultiplicativeExpr '*' UnaryExpr
8775 * | MultiplicativeExpr 'div' UnaryExpr
8776 * | MultiplicativeExpr 'mod' UnaryExpr
8777 *
8778 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008779 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008780 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008781 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008782 * @param[in] options XPath options.
8783 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8784 */
8785static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008786eval_multiplicative_expr(const struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set,
8787 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008788{
8789 LY_ERR rc;
8790 uint16_t this_op;
8791 struct lyxp_set orig_set, set2;
8792 uint16_t i;
8793
8794 assert(repeat);
8795
8796 set_init(&orig_set, set);
8797 set_init(&set2, set);
8798
8799 set_fill_set(&orig_set, set);
8800
Michal Vasko004d3152020-06-11 19:59:22 +02008801 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008802 LY_CHECK_GOTO(rc, cleanup);
8803
8804 /* ('*' / 'div' / 'mod' UnaryExpr)* */
8805 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008806 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008807
Michal Vasko004d3152020-06-11 19:59:22 +02008808 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH);
aPiecek8b0cc152021-05-31 16:40:31 +02008809 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008810 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008811 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008812
aPiecek8b0cc152021-05-31 16:40:31 +02008813 if (options & LYXP_SKIP_EXPR) {
8814 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008815 LY_CHECK_GOTO(rc, cleanup);
8816 continue;
8817 }
8818
8819 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008820 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008821 LY_CHECK_GOTO(rc, cleanup);
8822
8823 /* eval */
8824 if (options & LYXP_SCNODE_ALL) {
8825 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008826 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02008827 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008828 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008829 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008830 LY_CHECK_GOTO(rc, cleanup);
8831 }
8832 }
8833
8834cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008835 lyxp_set_free_content(&orig_set);
8836 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008837 return rc;
8838}
8839
8840/**
8841 * @brief Evaluate AdditiveExpr. Logs directly on error.
8842 *
Michal Vaskod3678892020-05-21 10:06:58 +02008843 * [17] AdditiveExpr ::= MultiplicativeExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008844 * | AdditiveExpr '+' MultiplicativeExpr
8845 * | AdditiveExpr '-' MultiplicativeExpr
8846 *
8847 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008848 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008849 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008850 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008851 * @param[in] options XPath options.
8852 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8853 */
8854static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008855eval_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 +02008856{
8857 LY_ERR rc;
8858 uint16_t this_op;
8859 struct lyxp_set orig_set, set2;
8860 uint16_t i;
8861
8862 assert(repeat);
8863
8864 set_init(&orig_set, set);
8865 set_init(&set2, set);
8866
8867 set_fill_set(&orig_set, set);
8868
Michal Vasko004d3152020-06-11 19:59:22 +02008869 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008870 LY_CHECK_GOTO(rc, cleanup);
8871
8872 /* ('+' / '-' MultiplicativeExpr)* */
8873 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008874 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008875
Michal Vasko004d3152020-06-11 19:59:22 +02008876 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008877 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008878 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008879 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008880
aPiecek8b0cc152021-05-31 16:40:31 +02008881 if (options & LYXP_SKIP_EXPR) {
8882 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008883 LY_CHECK_GOTO(rc, cleanup);
8884 continue;
8885 }
8886
8887 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008888 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008889 LY_CHECK_GOTO(rc, cleanup);
8890
8891 /* eval */
8892 if (options & LYXP_SCNODE_ALL) {
8893 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008894 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02008895 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008896 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008897 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008898 LY_CHECK_GOTO(rc, cleanup);
8899 }
8900 }
8901
8902cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008903 lyxp_set_free_content(&orig_set);
8904 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008905 return rc;
8906}
8907
8908/**
8909 * @brief Evaluate RelationalExpr. Logs directly on error.
8910 *
Michal Vaskod3678892020-05-21 10:06:58 +02008911 * [16] RelationalExpr ::= AdditiveExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008912 * | RelationalExpr '<' AdditiveExpr
8913 * | RelationalExpr '>' AdditiveExpr
8914 * | RelationalExpr '<=' AdditiveExpr
8915 * | RelationalExpr '>=' AdditiveExpr
8916 *
8917 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008918 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008919 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008920 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008921 * @param[in] options XPath options.
8922 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8923 */
8924static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008925eval_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 +02008926{
8927 LY_ERR rc;
8928 uint16_t this_op;
8929 struct lyxp_set orig_set, set2;
8930 uint16_t i;
8931
8932 assert(repeat);
8933
8934 set_init(&orig_set, set);
8935 set_init(&set2, set);
8936
8937 set_fill_set(&orig_set, set);
8938
Michal Vasko004d3152020-06-11 19:59:22 +02008939 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008940 LY_CHECK_GOTO(rc, cleanup);
8941
8942 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
8943 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008944 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008945
Michal Vasko004d3152020-06-11 19:59:22 +02008946 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_COMP);
aPiecek8b0cc152021-05-31 16:40:31 +02008947 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008948 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008949 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008950
aPiecek8b0cc152021-05-31 16:40:31 +02008951 if (options & LYXP_SKIP_EXPR) {
8952 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008953 LY_CHECK_GOTO(rc, cleanup);
8954 continue;
8955 }
8956
8957 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008958 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008959 LY_CHECK_GOTO(rc, cleanup);
8960
8961 /* eval */
8962 if (options & LYXP_SCNODE_ALL) {
8963 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008964 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02008965 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008966 } else {
8967 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options);
8968 LY_CHECK_GOTO(rc, cleanup);
8969 }
8970 }
8971
8972cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008973 lyxp_set_free_content(&orig_set);
8974 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008975 return rc;
8976}
8977
8978/**
8979 * @brief Evaluate EqualityExpr. Logs directly on error.
8980 *
Michal Vaskod3678892020-05-21 10:06:58 +02008981 * [15] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008982 * | EqualityExpr '!=' RelationalExpr
8983 *
8984 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008985 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008986 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008987 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008988 * @param[in] options XPath options.
8989 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8990 */
8991static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008992eval_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 +02008993{
8994 LY_ERR rc;
8995 uint16_t this_op;
8996 struct lyxp_set orig_set, set2;
8997 uint16_t i;
8998
8999 assert(repeat);
9000
9001 set_init(&orig_set, set);
9002 set_init(&set2, set);
9003
9004 set_fill_set(&orig_set, set);
9005
Michal Vasko004d3152020-06-11 19:59:22 +02009006 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009007 LY_CHECK_GOTO(rc, cleanup);
9008
9009 /* ('=' / '!=' RelationalExpr)* */
9010 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02009011 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009012
Michal Vasko004d3152020-06-11 19:59:22 +02009013 assert((exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_EQUAL) || (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_NEQUAL));
aPiecek8b0cc152021-05-31 16:40:31 +02009014 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02009015 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02009016 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009017
aPiecek8b0cc152021-05-31 16:40:31 +02009018 if (options & LYXP_SKIP_EXPR) {
9019 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009020 LY_CHECK_GOTO(rc, cleanup);
9021 continue;
9022 }
9023
9024 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02009025 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009026 LY_CHECK_GOTO(rc, cleanup);
9027
9028 /* eval */
9029 if (options & LYXP_SCNODE_ALL) {
9030 warn_operands(set->ctx, set, &set2, 0, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vasko004d3152020-06-11 19:59:22 +02009031 warn_equality_value(exp, set, *tok_idx - 1, this_op - 1, *tok_idx - 1);
9032 warn_equality_value(exp, &set2, this_op - 1, this_op - 1, *tok_idx - 1);
Michal Vaskoecd62de2019-11-13 12:35:11 +01009033 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02009034 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009035 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02009036 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options);
9037 LY_CHECK_GOTO(rc, cleanup);
9038 }
9039 }
9040
9041cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02009042 lyxp_set_free_content(&orig_set);
9043 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009044 return rc;
9045}
9046
9047/**
9048 * @brief Evaluate AndExpr. Logs directly on error.
9049 *
Michal Vaskod3678892020-05-21 10:06:58 +02009050 * [14] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02009051 *
9052 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02009053 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009054 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02009055 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009056 * @param[in] options XPath options.
9057 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
9058 */
9059static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02009060eval_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 +02009061{
9062 LY_ERR rc;
9063 struct lyxp_set orig_set, set2;
9064 uint16_t i;
9065
9066 assert(repeat);
9067
9068 set_init(&orig_set, set);
9069 set_init(&set2, set);
9070
9071 set_fill_set(&orig_set, set);
9072
Michal Vasko004d3152020-06-11 19:59:22 +02009073 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009074 LY_CHECK_GOTO(rc, cleanup);
9075
9076 /* cast to boolean, we know that will be the final result */
aPiecek8b0cc152021-05-31 16:40:31 +02009077 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02009078 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009079 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009080 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009081 }
9082
9083 /* ('and' EqualityExpr)* */
9084 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02009085 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG);
aPiecek8b0cc152021-05-31 16:40:31 +02009086 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, ((options & LYXP_SKIP_EXPR) || !set->val.bln ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02009087 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02009088 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009089
9090 /* lazy evaluation */
aPiecek8b0cc152021-05-31 16:40:31 +02009091 if ((options & LYXP_SKIP_EXPR) || ((set->type == LYXP_SET_BOOLEAN) && !set->val.bln)) {
9092 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009093 LY_CHECK_GOTO(rc, cleanup);
9094 continue;
9095 }
9096
9097 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02009098 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009099 LY_CHECK_GOTO(rc, cleanup);
9100
9101 /* eval - just get boolean value actually */
9102 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vasko1a09b212021-05-06 13:00:10 +02009103 set_scnode_clear_ctx(&set2, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskoecd62de2019-11-13 12:35:11 +01009104 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009105 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009106 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009107 set_fill_set(set, &set2);
9108 }
9109 }
9110
9111cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02009112 lyxp_set_free_content(&orig_set);
9113 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009114 return rc;
9115}
9116
9117/**
9118 * @brief Evaluate OrExpr. Logs directly on error.
9119 *
Michal Vaskod3678892020-05-21 10:06:58 +02009120 * [13] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02009121 *
9122 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02009123 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009124 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02009125 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009126 * @param[in] options XPath options.
9127 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
9128 */
9129static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02009130eval_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 +02009131{
9132 LY_ERR rc;
9133 struct lyxp_set orig_set, set2;
9134 uint16_t i;
9135
9136 assert(repeat);
9137
9138 set_init(&orig_set, set);
9139 set_init(&set2, set);
9140
9141 set_fill_set(&orig_set, set);
9142
Michal Vasko004d3152020-06-11 19:59:22 +02009143 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009144 LY_CHECK_GOTO(rc, cleanup);
9145
9146 /* cast to boolean, we know that will be the final result */
aPiecek8b0cc152021-05-31 16:40:31 +02009147 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02009148 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009149 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009150 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009151 }
9152
9153 /* ('or' AndExpr)* */
9154 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02009155 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG);
aPiecek8b0cc152021-05-31 16:40:31 +02009156 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, ((options & LYXP_SKIP_EXPR) || set->val.bln ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02009157 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02009158 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009159
9160 /* lazy evaluation */
aPiecek8b0cc152021-05-31 16:40:31 +02009161 if ((options & LYXP_SKIP_EXPR) || ((set->type == LYXP_SET_BOOLEAN) && set->val.bln)) {
9162 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009163 LY_CHECK_GOTO(rc, cleanup);
9164 continue;
9165 }
9166
9167 set_fill_set(&set2, &orig_set);
9168 /* expr_type cound have been LYXP_EXPR_NONE in all these later calls (except for the first one),
9169 * but it does not matter */
Michal Vasko004d3152020-06-11 19:59:22 +02009170 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009171 LY_CHECK_GOTO(rc, cleanup);
9172
9173 /* eval - just get boolean value actually */
9174 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vasko1a09b212021-05-06 13:00:10 +02009175 set_scnode_clear_ctx(&set2, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskoecd62de2019-11-13 12:35:11 +01009176 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009177 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009178 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009179 set_fill_set(set, &set2);
9180 }
9181 }
9182
9183cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02009184 lyxp_set_free_content(&orig_set);
9185 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009186 return rc;
9187}
9188
9189/**
Michal Vasko004d3152020-06-11 19:59:22 +02009190 * @brief Decide what expression is at the pointer @p tok_idx and evaluate it accordingly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009191 *
9192 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02009193 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009194 * @param[in] etype Expression type to evaluate.
aPiecek8b0cc152021-05-31 16:40:31 +02009195 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009196 * @param[in] options XPath options.
9197 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
9198 */
9199static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02009200eval_expr_select(const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_expr_type etype, struct lyxp_set *set,
9201 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02009202{
9203 uint16_t i, count;
9204 enum lyxp_expr_type next_etype;
9205 LY_ERR rc;
9206
9207 /* process operator repeats */
Michal Vasko004d3152020-06-11 19:59:22 +02009208 if (!exp->repeat[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02009209 next_etype = LYXP_EXPR_NONE;
9210 } else {
9211 /* find etype repeat */
Radek Krejci1e008d22020-08-17 11:37:37 +02009212 for (i = 0; exp->repeat[*tok_idx][i] > etype; ++i) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02009213
9214 /* select one-priority lower because etype expression called us */
9215 if (i) {
Michal Vasko004d3152020-06-11 19:59:22 +02009216 next_etype = exp->repeat[*tok_idx][i - 1];
Michal Vasko03ff5a72019-09-11 13:49:33 +02009217 /* count repeats for that expression */
Radek Krejci1e008d22020-08-17 11:37:37 +02009218 for (count = 0; i && exp->repeat[*tok_idx][i - 1] == next_etype; ++count, --i) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02009219 } else {
9220 next_etype = LYXP_EXPR_NONE;
9221 }
9222 }
9223
9224 /* decide what expression are we parsing based on the repeat */
9225 switch (next_etype) {
9226 case LYXP_EXPR_OR:
Michal Vasko004d3152020-06-11 19:59:22 +02009227 rc = eval_or_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009228 break;
9229 case LYXP_EXPR_AND:
Michal Vasko004d3152020-06-11 19:59:22 +02009230 rc = eval_and_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009231 break;
9232 case LYXP_EXPR_EQUALITY:
Michal Vasko004d3152020-06-11 19:59:22 +02009233 rc = eval_equality_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009234 break;
9235 case LYXP_EXPR_RELATIONAL:
Michal Vasko004d3152020-06-11 19:59:22 +02009236 rc = eval_relational_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009237 break;
9238 case LYXP_EXPR_ADDITIVE:
Michal Vasko004d3152020-06-11 19:59:22 +02009239 rc = eval_additive_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009240 break;
9241 case LYXP_EXPR_MULTIPLICATIVE:
Michal Vasko004d3152020-06-11 19:59:22 +02009242 rc = eval_multiplicative_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009243 break;
9244 case LYXP_EXPR_UNARY:
Michal Vasko004d3152020-06-11 19:59:22 +02009245 rc = eval_unary_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009246 break;
9247 case LYXP_EXPR_UNION:
Michal Vasko004d3152020-06-11 19:59:22 +02009248 rc = eval_union_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009249 break;
9250 case LYXP_EXPR_NONE:
Michal Vasko004d3152020-06-11 19:59:22 +02009251 rc = eval_path_expr(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009252 break;
9253 default:
9254 LOGINT_RET(set->ctx);
9255 }
9256
9257 return rc;
9258}
9259
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009260/**
9261 * @brief Get root type.
9262 *
9263 * @param[in] ctx_node Context node.
9264 * @param[in] ctx_scnode Schema context node.
9265 * @param[in] options XPath options.
9266 * @return Root type.
9267 */
9268static enum lyxp_node_type
Radek Krejci1deb5be2020-08-26 16:43:36 +02009269lyxp_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 +01009270{
Michal Vasko6b26e742020-07-17 15:02:10 +02009271 const struct lysc_node *op;
9272
Michal Vaskoa27245c2022-05-02 09:01:35 +02009273 /* explicit */
9274 if (options & LYXP_ACCESS_TREE_ALL) {
9275 return LYXP_NODE_ROOT;
9276 } else if (options & LYXP_ACCESS_TREE_CONFIG) {
9277 return LYXP_NODE_ROOT_CONFIG;
9278 }
9279
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009280 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009281 /* schema */
Radek Krejci1e008d22020-08-17 11:37:37 +02009282 for (op = ctx_scnode; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent) {}
Michal Vasko6b26e742020-07-17 15:02:10 +02009283
9284 if (op || (options & LYXP_SCNODE)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009285 /* general root that can access everything */
9286 return LYXP_NODE_ROOT;
9287 } else if (!ctx_scnode || (ctx_scnode->flags & LYS_CONFIG_W)) {
9288 /* root context node can access only config data (because we said so, it is unspecified) */
9289 return LYXP_NODE_ROOT_CONFIG;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009290 }
Michal Vasko6b26e742020-07-17 15:02:10 +02009291 return LYXP_NODE_ROOT;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009292 }
9293
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009294 /* data */
Michal Vasko6b26e742020-07-17 15:02:10 +02009295 op = ctx_node ? ctx_node->schema : NULL;
Michal Vaskod989ba02020-08-24 10:59:24 +02009296 for ( ; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent) {}
Michal Vasko6b26e742020-07-17 15:02:10 +02009297
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009298 if (op || !(options & LYXP_SCHEMA)) {
9299 /* general root that can access everything */
9300 return LYXP_NODE_ROOT;
Christian Hoppsb6ecaea2021-02-06 09:45:38 -05009301 } else if (!ctx_node || !ctx_node->schema || (ctx_node->schema->flags & LYS_CONFIG_W)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009302 /* root context node can access only config data (because we said so, it is unspecified) */
9303 return LYXP_NODE_ROOT_CONFIG;
9304 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009305 return LYXP_NODE_ROOT;
9306}
9307
Michal Vasko03ff5a72019-09-11 13:49:33 +02009308LY_ERR
Michal Vasko400e9672021-01-11 13:39:17 +01009309lyxp_eval(const struct ly_ctx *ctx, const struct lyxp_expr *exp, const struct lys_module *cur_mod,
Radek Krejci8df109d2021-04-23 12:19:08 +02009310 LY_VALUE_FORMAT format, void *prefix_data, const struct lyd_node *ctx_node, const struct lyd_node *tree,
aPiecekfba75362021-10-07 12:39:48 +02009311 const struct lyxp_var *vars, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02009312{
Michal Vasko004d3152020-06-11 19:59:22 +02009313 uint16_t tok_idx = 0;
Michal Vaskoddd76592022-01-17 13:34:48 +01009314 const struct lysc_node *snode;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009315 LY_ERR rc;
9316
Michal Vasko400e9672021-01-11 13:39:17 +01009317 LY_CHECK_ARG_RET(ctx, ctx, exp, set, LY_EINVAL);
Radek Krejci8df109d2021-04-23 12:19:08 +02009318 if (!cur_mod && ((format == LY_VALUE_SCHEMA) || (format == LY_VALUE_SCHEMA_RESOLVED))) {
Michal Vaskoddd76592022-01-17 13:34:48 +01009319 LOGERR(ctx, LY_EINVAL, "Current module must be set if schema format is used.");
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009320 return LY_EINVAL;
Michal Vasko004d3152020-06-11 19:59:22 +02009321 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02009322
Michal Vaskod3bb12f2020-12-04 14:33:09 +01009323 if (tree) {
9324 /* adjust the pointer to be the first top-level sibling */
9325 while (tree->parent) {
9326 tree = lyd_parent(tree);
9327 }
9328 tree = lyd_first_sibling(tree);
Michal Vaskoddd76592022-01-17 13:34:48 +01009329
9330 for (snode = tree->schema->parent; snode && (snode->nodetype & (LYS_CASE | LYS_CHOICE)); snode = snode->parent) {}
9331 if (snode) {
9332 /* unable to evaluate absolute paths */
9333 LOGERR(ctx, LY_EINVAL, "Data node \"%s\" has no parent but is not instance of a top-level schema node.",
9334 LYD_NAME(tree));
9335 return LY_EINVAL;
9336 }
Michal Vaskod3bb12f2020-12-04 14:33:09 +01009337 }
9338
Michal Vasko03ff5a72019-09-11 13:49:33 +02009339 /* prepare set for evaluation */
Michal Vasko03ff5a72019-09-11 13:49:33 +02009340 memset(set, 0, sizeof *set);
Michal Vaskod3678892020-05-21 10:06:58 +02009341 set->type = LYXP_SET_NODE_SET;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009342 set->root_type = lyxp_get_root_type(ctx_node, NULL, options);
9343 set_insert_node(set, (struct lyd_node *)ctx_node, 0, ctx_node ? LYXP_NODE_ELEM : set->root_type, 0);
9344
Michal Vasko400e9672021-01-11 13:39:17 +01009345 set->ctx = (struct ly_ctx *)ctx;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009346 set->cur_node = ctx_node;
9347 for (set->context_op = ctx_node ? ctx_node->schema : NULL;
Radek Krejci0f969882020-08-21 16:56:47 +02009348 set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF));
9349 set->context_op = set->context_op->parent) {}
Michal Vaskof03ed032020-03-04 13:31:44 +01009350 set->tree = tree;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009351 set->cur_mod = cur_mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009352 set->format = format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009353 set->prefix_data = prefix_data;
aPiecekfba75362021-10-07 12:39:48 +02009354 set->vars = vars;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009355
Radek Krejciddace2c2021-01-08 11:30:56 +01009356 LOG_LOCSET(NULL, set->cur_node, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01009357
Michal Vasko03ff5a72019-09-11 13:49:33 +02009358 /* evaluate */
Michal Vasko004d3152020-06-11 19:59:22 +02009359 rc = eval_expr_select(exp, &tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009360 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02009361 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009362 }
9363
Michal Vasko4a7d4d62021-12-13 17:05:06 +01009364 if (set->cur_node) {
9365 LOG_LOCBACK(0, 1, 0, 0);
9366 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02009367 return rc;
9368}
9369
9370#if 0
9371
9372/* full xml printing of set elements, not used currently */
9373
9374void
9375lyxp_set_print_xml(FILE *f, struct lyxp_set *set)
9376{
9377 uint32_t i;
9378 char *str_num;
9379 struct lyout out;
9380
9381 memset(&out, 0, sizeof out);
9382
9383 out.type = LYOUT_STREAM;
9384 out.method.f = f;
9385
9386 switch (set->type) {
9387 case LYXP_SET_EMPTY:
Michal Vasko5233e962020-08-14 14:26:20 +02009388 ly_print_(&out, "Empty XPath set\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02009389 break;
9390 case LYXP_SET_BOOLEAN:
Michal Vasko5233e962020-08-14 14:26:20 +02009391 ly_print_(&out, "Boolean XPath set:\n");
9392 ly_print_(&out, "%s\n\n", set->value.bool ? "true" : "false");
Michal Vasko03ff5a72019-09-11 13:49:33 +02009393 break;
9394 case LYXP_SET_STRING:
Michal Vasko5233e962020-08-14 14:26:20 +02009395 ly_print_(&out, "String XPath set:\n");
9396 ly_print_(&out, "\"%s\"\n\n", set->value.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009397 break;
9398 case LYXP_SET_NUMBER:
Michal Vasko5233e962020-08-14 14:26:20 +02009399 ly_print_(&out, "Number XPath set:\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02009400
9401 if (isnan(set->value.num)) {
9402 str_num = strdup("NaN");
9403 } else if ((set->value.num == 0) || (set->value.num == -0.0f)) {
9404 str_num = strdup("0");
9405 } else if (isinf(set->value.num) && !signbit(set->value.num)) {
9406 str_num = strdup("Infinity");
9407 } else if (isinf(set->value.num) && signbit(set->value.num)) {
9408 str_num = strdup("-Infinity");
9409 } else if ((long long)set->value.num == set->value.num) {
9410 if (asprintf(&str_num, "%lld", (long long)set->value.num) == -1) {
9411 str_num = NULL;
9412 }
9413 } else {
9414 if (asprintf(&str_num, "%03.1Lf", set->value.num) == -1) {
9415 str_num = NULL;
9416 }
9417 }
9418 if (!str_num) {
9419 LOGMEM;
9420 return;
9421 }
Michal Vasko5233e962020-08-14 14:26:20 +02009422 ly_print_(&out, "%s\n\n", str_num);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009423 free(str_num);
9424 break;
9425 case LYXP_SET_NODE_SET:
Michal Vasko5233e962020-08-14 14:26:20 +02009426 ly_print_(&out, "Node XPath set:\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02009427
9428 for (i = 0; i < set->used; ++i) {
Michal Vasko5233e962020-08-14 14:26:20 +02009429 ly_print_(&out, "%d. ", i + 1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009430 switch (set->node_type[i]) {
9431 case LYXP_NODE_ROOT_ALL:
Michal Vasko5233e962020-08-14 14:26:20 +02009432 ly_print_(&out, "ROOT all\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02009433 break;
9434 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko5233e962020-08-14 14:26:20 +02009435 ly_print_(&out, "ROOT config\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02009436 break;
9437 case LYXP_NODE_ROOT_STATE:
Michal Vasko5233e962020-08-14 14:26:20 +02009438 ly_print_(&out, "ROOT state\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02009439 break;
9440 case LYXP_NODE_ROOT_NOTIF:
Michal Vasko5233e962020-08-14 14:26:20 +02009441 ly_print_(&out, "ROOT notification \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009442 break;
9443 case LYXP_NODE_ROOT_RPC:
Michal Vasko5233e962020-08-14 14:26:20 +02009444 ly_print_(&out, "ROOT rpc \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009445 break;
9446 case LYXP_NODE_ROOT_OUTPUT:
Michal Vasko5233e962020-08-14 14:26:20 +02009447 ly_print_(&out, "ROOT output \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009448 break;
9449 case LYXP_NODE_ELEM:
Michal Vasko5233e962020-08-14 14:26:20 +02009450 ly_print_(&out, "ELEM \"%s\"\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009451 xml_print_node(&out, 1, set->value.nodes[i], 1, LYP_FORMAT);
Michal Vasko5233e962020-08-14 14:26:20 +02009452 ly_print_(&out, "\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02009453 break;
9454 case LYXP_NODE_TEXT:
Michal Vasko5233e962020-08-14 14:26:20 +02009455 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 +02009456 break;
9457 case LYXP_NODE_ATTR:
Michal Vasko5233e962020-08-14 14:26:20 +02009458 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 +02009459 break;
9460 }
9461 }
9462 break;
9463 }
9464}
9465
9466#endif
9467
9468LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009469lyxp_set_cast(struct lyxp_set *set, enum lyxp_set_type target)
Michal Vasko03ff5a72019-09-11 13:49:33 +02009470{
9471 long double num;
9472 char *str;
9473 LY_ERR rc;
9474
9475 if (!set || (set->type == target)) {
9476 return LY_SUCCESS;
9477 }
9478
9479 /* it's not possible to convert anything into a node set */
Michal Vaskod3678892020-05-21 10:06:58 +02009480 assert(target != LYXP_SET_NODE_SET);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009481
9482 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vaskod3678892020-05-21 10:06:58 +02009483 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009484 return LY_EINVAL;
9485 }
9486
9487 /* to STRING */
Michal Vaskod3678892020-05-21 10:06:58 +02009488 if ((target == LYXP_SET_STRING) || ((target == LYXP_SET_NUMBER) && (set->type == LYXP_SET_NODE_SET))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02009489 switch (set->type) {
9490 case LYXP_SET_NUMBER:
9491 if (isnan(set->val.num)) {
9492 set->val.str = strdup("NaN");
9493 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
9494 } else if ((set->val.num == 0) || (set->val.num == -0.0f)) {
9495 set->val.str = strdup("0");
9496 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
9497 } else if (isinf(set->val.num) && !signbit(set->val.num)) {
9498 set->val.str = strdup("Infinity");
9499 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
9500 } else if (isinf(set->val.num) && signbit(set->val.num)) {
9501 set->val.str = strdup("-Infinity");
9502 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
9503 } else if ((long long)set->val.num == set->val.num) {
9504 if (asprintf(&str, "%lld", (long long)set->val.num) == -1) {
9505 LOGMEM_RET(set->ctx);
9506 }
9507 set->val.str = str;
9508 } else {
9509 if (asprintf(&str, "%03.1Lf", set->val.num) == -1) {
9510 LOGMEM_RET(set->ctx);
9511 }
9512 set->val.str = str;
9513 }
9514 break;
9515 case LYXP_SET_BOOLEAN:
Michal Vasko004d3152020-06-11 19:59:22 +02009516 if (set->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02009517 set->val.str = strdup("true");
9518 } else {
9519 set->val.str = strdup("false");
9520 }
9521 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), LY_EMEM);
9522 break;
9523 case LYXP_SET_NODE_SET:
Michal Vasko03ff5a72019-09-11 13:49:33 +02009524 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009525 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02009526
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009527 rc = cast_node_set_to_string(set, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009528 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02009529 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009530 set->val.str = str;
9531 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009532 default:
9533 LOGINT_RET(set->ctx);
9534 }
9535 set->type = LYXP_SET_STRING;
9536 }
9537
9538 /* to NUMBER */
9539 if (target == LYXP_SET_NUMBER) {
9540 switch (set->type) {
9541 case LYXP_SET_STRING:
9542 num = cast_string_to_number(set->val.str);
Michal Vaskod3678892020-05-21 10:06:58 +02009543 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009544 set->val.num = num;
9545 break;
9546 case LYXP_SET_BOOLEAN:
Michal Vasko004d3152020-06-11 19:59:22 +02009547 if (set->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02009548 set->val.num = 1;
9549 } else {
9550 set->val.num = 0;
9551 }
9552 break;
9553 default:
9554 LOGINT_RET(set->ctx);
9555 }
9556 set->type = LYXP_SET_NUMBER;
9557 }
9558
9559 /* to BOOLEAN */
9560 if (target == LYXP_SET_BOOLEAN) {
9561 switch (set->type) {
9562 case LYXP_SET_NUMBER:
9563 if ((set->val.num == 0) || (set->val.num == -0.0f) || isnan(set->val.num)) {
Michal Vasko004d3152020-06-11 19:59:22 +02009564 set->val.bln = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009565 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02009566 set->val.bln = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009567 }
9568 break;
9569 case LYXP_SET_STRING:
9570 if (set->val.str[0]) {
Michal Vaskod3678892020-05-21 10:06:58 +02009571 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02009572 set->val.bln = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009573 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02009574 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02009575 set->val.bln = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009576 }
9577 break;
9578 case LYXP_SET_NODE_SET:
Michal Vaskod3678892020-05-21 10:06:58 +02009579 if (set->used) {
9580 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02009581 set->val.bln = 1;
Michal Vaskod3678892020-05-21 10:06:58 +02009582 } else {
9583 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02009584 set->val.bln = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02009585 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02009586 break;
9587 default:
9588 LOGINT_RET(set->ctx);
9589 }
9590 set->type = LYXP_SET_BOOLEAN;
9591 }
9592
Michal Vasko03ff5a72019-09-11 13:49:33 +02009593 return LY_SUCCESS;
9594}
9595
9596LY_ERR
Michal Vasko400e9672021-01-11 13:39:17 +01009597lyxp_atomize(const struct ly_ctx *ctx, const struct lyxp_expr *exp, const struct lys_module *cur_mod,
Radek Krejci8df109d2021-04-23 12:19:08 +02009598 LY_VALUE_FORMAT format, void *prefix_data, const struct lysc_node *ctx_scnode, struct lyxp_set *set,
Michal Vasko400e9672021-01-11 13:39:17 +01009599 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02009600{
Radek Krejci2efc45b2020-12-22 16:25:44 +01009601 LY_ERR ret;
Michal Vasko004d3152020-06-11 19:59:22 +02009602 uint16_t tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009603
Michal Vasko400e9672021-01-11 13:39:17 +01009604 LY_CHECK_ARG_RET(ctx, ctx, exp, set, LY_EINVAL);
Radek Krejci8df109d2021-04-23 12:19:08 +02009605 if (!cur_mod && ((format == LY_VALUE_SCHEMA) || (format == LY_VALUE_SCHEMA_RESOLVED))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009606 LOGARG(NULL, "Current module must be set if schema format is used.");
9607 return LY_EINVAL;
Michal Vasko004d3152020-06-11 19:59:22 +02009608 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02009609
9610 /* prepare set for evaluation */
Michal Vasko03ff5a72019-09-11 13:49:33 +02009611 memset(set, 0, sizeof *set);
9612 set->type = LYXP_SET_SCNODE_SET;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009613 set->root_type = lyxp_get_root_type(NULL, ctx_scnode, options);
9614 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 +01009615 set->val.scnodes[0].in_ctx = LYXP_SET_SCNODE_START;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009616
Michal Vasko400e9672021-01-11 13:39:17 +01009617 set->ctx = (struct ly_ctx *)ctx;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009618 set->cur_scnode = ctx_scnode;
Michal Vasko6b26e742020-07-17 15:02:10 +02009619 for (set->context_op = ctx_scnode;
Radek Krejci0f969882020-08-21 16:56:47 +02009620 set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF));
9621 set->context_op = set->context_op->parent) {}
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009622 set->cur_mod = cur_mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009623 set->format = format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009624 set->prefix_data = prefix_data;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009625
Radek Krejciddace2c2021-01-08 11:30:56 +01009626 LOG_LOCSET(set->cur_scnode, NULL, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01009627
Michal Vasko03ff5a72019-09-11 13:49:33 +02009628 /* evaluate */
Radek Krejci2efc45b2020-12-22 16:25:44 +01009629 ret = eval_expr_select(exp, &tok_idx, 0, set, options);
9630
Radek Krejciddace2c2021-01-08 11:30:56 +01009631 LOG_LOCBACK(1, 0, 0, 0);
Radek Krejci2efc45b2020-12-22 16:25:44 +01009632 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009633}
Michal Vaskod43d71a2020-08-07 14:54:58 +02009634
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01009635LIBYANG_API_DEF const char *
Michal Vaskod43d71a2020-08-07 14:54:58 +02009636lyxp_get_expr(const struct lyxp_expr *path)
9637{
9638 if (!path) {
9639 return NULL;
9640 }
9641
9642 return path->expr;
9643}