blob: ed24eb1964d7f8227e0c932d096cc1b7a0984117 [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 Vasko61ac2f62020-05-25 12:39:51 +02006 * Copyright (c) 2015 - 2020 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 Vasko03ff5a72019-09-11 13:49:33 +020050
aPiecek96dc1e32021-10-08 15:45:59 +020051/* Functions are divided into the following basic classes:
52 *
53 * (re)parse functions:
54 * Parse functions parse the expression into
55 * tokens (syntactic analysis).
56 * Reparse functions perform semantic analysis
57 * (do not save the result, just a check) of
58 * the expression and fill repeat indices.
59 *
60 * warn functions:
61 * Warn functions check specific reasonable conditions for schema XPath
62 * and print a warning if they are not satisfied.
63 *
64 * moveto functions:
65 * They and only they actually change the context (set).
66 *
67 * eval functions:
68 * They execute a parsed XPath expression on some data subtree.
69 */
70
Michal Vasko03ff5a72019-09-11 13:49:33 +020071/**
72 * @brief Print the type of an XPath \p set.
73 *
74 * @param[in] set Set to use.
75 * @return Set type string.
76 */
77static const char *
78print_set_type(struct lyxp_set *set)
79{
80 switch (set->type) {
Michal Vasko03ff5a72019-09-11 13:49:33 +020081 case LYXP_SET_NODE_SET:
82 return "node set";
83 case LYXP_SET_SCNODE_SET:
84 return "schema node set";
85 case LYXP_SET_BOOLEAN:
86 return "boolean";
87 case LYXP_SET_NUMBER:
88 return "number";
89 case LYXP_SET_STRING:
90 return "string";
91 }
92
93 return NULL;
94}
95
Michal Vasko24cddf82020-06-01 08:17:01 +020096const char *
97lyxp_print_token(enum lyxp_token tok)
Michal Vasko03ff5a72019-09-11 13:49:33 +020098{
99 switch (tok) {
100 case LYXP_TOKEN_PAR1:
101 return "(";
102 case LYXP_TOKEN_PAR2:
103 return ")";
104 case LYXP_TOKEN_BRACK1:
105 return "[";
106 case LYXP_TOKEN_BRACK2:
107 return "]";
108 case LYXP_TOKEN_DOT:
109 return ".";
110 case LYXP_TOKEN_DDOT:
111 return "..";
112 case LYXP_TOKEN_AT:
113 return "@";
114 case LYXP_TOKEN_COMMA:
115 return ",";
116 case LYXP_TOKEN_NAMETEST:
117 return "NameTest";
118 case LYXP_TOKEN_NODETYPE:
119 return "NodeType";
aPiecekfba75362021-10-07 12:39:48 +0200120 case LYXP_TOKEN_VARREF:
121 return "VariableReference";
Michal Vasko03ff5a72019-09-11 13:49:33 +0200122 case LYXP_TOKEN_FUNCNAME:
123 return "FunctionName";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200124 case LYXP_TOKEN_OPER_LOG:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200125 return "Operator(Logic)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200126 case LYXP_TOKEN_OPER_EQUAL:
127 return "Operator(Equal)";
128 case LYXP_TOKEN_OPER_NEQUAL:
129 return "Operator(Non-equal)";
130 case LYXP_TOKEN_OPER_COMP:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200131 return "Operator(Comparison)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200132 case LYXP_TOKEN_OPER_MATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200133 return "Operator(Math)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200134 case LYXP_TOKEN_OPER_UNI:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200135 return "Operator(Union)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200136 case LYXP_TOKEN_OPER_PATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200137 return "Operator(Path)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200138 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko14676352020-05-29 11:35:55 +0200139 return "Operator(Recursive Path)";
Michal Vasko03ff5a72019-09-11 13:49:33 +0200140 case LYXP_TOKEN_LITERAL:
141 return "Literal";
142 case LYXP_TOKEN_NUMBER:
143 return "Number";
144 default:
145 LOGINT(NULL);
146 return "";
147 }
148}
149
150/**
151 * @brief Print the whole expression \p exp to debug output.
152 *
153 * @param[in] exp Expression to use.
154 */
155static void
Michal Vasko40308e72020-10-20 16:38:40 +0200156print_expr_struct_debug(const struct lyxp_expr *exp)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200157{
Radek Krejcif13b87b2020-12-01 22:02:17 +0100158#define MSG_BUFFER_SIZE 128
159 char tmp[MSG_BUFFER_SIZE];
Michal Vasko1fdd8fa2021-01-08 09:21:45 +0100160 uint32_t i, j;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200161
Radek Krejci52b6d512020-10-12 12:33:17 +0200162 if (!exp || (ly_ll < LY_LLDBG)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200163 return;
164 }
165
166 LOGDBG(LY_LDGXPATH, "expression \"%s\":", exp->expr);
167 for (i = 0; i < exp->used; ++i) {
Michal Vasko24cddf82020-06-01 08:17:01 +0200168 sprintf(tmp, "\ttoken %s, in expression \"%.*s\"", lyxp_print_token(exp->tokens[i]), exp->tok_len[i],
Michal Vasko69730152020-10-09 16:30:07 +0200169 &exp->expr[exp->tok_pos[i]]);
Michal Vasko23049552021-03-04 15:57:44 +0100170 if (exp->repeat && exp->repeat[i]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200171 sprintf(tmp + strlen(tmp), " (repeat %d", exp->repeat[i][0]);
172 for (j = 1; exp->repeat[i][j]; ++j) {
173 sprintf(tmp + strlen(tmp), ", %d", exp->repeat[i][j]);
174 }
175 strcat(tmp, ")");
176 }
177 LOGDBG(LY_LDGXPATH, tmp);
178 }
Radek Krejcif13b87b2020-12-01 22:02:17 +0100179#undef MSG_BUFFER_SIZE
Michal Vasko03ff5a72019-09-11 13:49:33 +0200180}
181
182#ifndef NDEBUG
183
184/**
185 * @brief Print XPath set content to debug output.
186 *
187 * @param[in] set Set to print.
188 */
189static void
190print_set_debug(struct lyxp_set *set)
191{
192 uint32_t i;
193 char *str;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200194 struct lyxp_set_node *item;
195 struct lyxp_set_scnode *sitem;
196
Radek Krejci52b6d512020-10-12 12:33:17 +0200197 if (ly_ll < LY_LLDBG) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200198 return;
199 }
200
201 switch (set->type) {
202 case LYXP_SET_NODE_SET:
203 LOGDBG(LY_LDGXPATH, "set NODE SET:");
204 for (i = 0; i < set->used; ++i) {
205 item = &set->val.nodes[i];
206
207 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +0100208 case LYXP_NODE_NONE:
209 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): NONE", i + 1, item->pos);
210 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200211 case LYXP_NODE_ROOT:
212 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT", i + 1, item->pos);
213 break;
214 case LYXP_NODE_ROOT_CONFIG:
215 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT CONFIG", i + 1, item->pos);
216 break;
217 case LYXP_NODE_ELEM:
Michal Vasko9e685082021-01-29 14:49:09 +0100218 if ((item->node->schema->nodetype == LYS_LIST) && (lyd_child(item->node)->schema->nodetype == LYS_LEAF)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200219 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (1st child val: %s)", i + 1, item->pos,
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200220 item->node->schema->name, lyd_get_value(lyd_child(item->node)));
Michal Vasko9e685082021-01-29 14:49:09 +0100221 } else if (item->node->schema->nodetype == LYS_LEAFLIST) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200222 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (val: %s)", i + 1, item->pos,
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200223 item->node->schema->name, lyd_get_value(item->node));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200224 } else {
225 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s", i + 1, item->pos, item->node->schema->name);
226 }
227 break;
228 case LYXP_NODE_TEXT:
229 if (item->node->schema->nodetype & LYS_ANYDATA) {
230 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): TEXT <%s>", i + 1, item->pos,
Michal Vasko69730152020-10-09 16:30:07 +0200231 item->node->schema->nodetype == LYS_ANYXML ? "anyxml" : "anydata");
Michal Vasko03ff5a72019-09-11 13:49:33 +0200232 } else {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200233 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 +0200234 }
235 break;
Michal Vasko9f96a052020-03-10 09:41:45 +0100236 case LYXP_NODE_META:
237 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 +0200238 set->val.meta[i].meta->value);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200239 break;
240 }
241 }
242 break;
243
244 case LYXP_SET_SCNODE_SET:
245 LOGDBG(LY_LDGXPATH, "set SCNODE SET:");
246 for (i = 0; i < set->used; ++i) {
247 sitem = &set->val.scnodes[i];
248
249 switch (sitem->type) {
250 case LYXP_NODE_ROOT:
251 LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT", i + 1, sitem->in_ctx);
252 break;
253 case LYXP_NODE_ROOT_CONFIG:
254 LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT CONFIG", i + 1, sitem->in_ctx);
255 break;
256 case LYXP_NODE_ELEM:
257 LOGDBG(LY_LDGXPATH, "\t%d (%u): ELEM %s", i + 1, sitem->in_ctx, sitem->scnode->name);
258 break;
259 default:
260 LOGINT(NULL);
261 break;
262 }
263 }
264 break;
265
Michal Vasko03ff5a72019-09-11 13:49:33 +0200266 case LYXP_SET_BOOLEAN:
267 LOGDBG(LY_LDGXPATH, "set BOOLEAN");
Michal Vasko004d3152020-06-11 19:59:22 +0200268 LOGDBG(LY_LDGXPATH, "\t%s", (set->val.bln ? "true" : "false"));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200269 break;
270
271 case LYXP_SET_STRING:
272 LOGDBG(LY_LDGXPATH, "set STRING");
273 LOGDBG(LY_LDGXPATH, "\t%s", set->val.str);
274 break;
275
276 case LYXP_SET_NUMBER:
277 LOGDBG(LY_LDGXPATH, "set NUMBER");
278
279 if (isnan(set->val.num)) {
280 str = strdup("NaN");
281 } else if ((set->val.num == 0) || (set->val.num == -0.0f)) {
282 str = strdup("0");
283 } else if (isinf(set->val.num) && !signbit(set->val.num)) {
284 str = strdup("Infinity");
285 } else if (isinf(set->val.num) && signbit(set->val.num)) {
286 str = strdup("-Infinity");
287 } else if ((long long)set->val.num == set->val.num) {
288 if (asprintf(&str, "%lld", (long long)set->val.num) == -1) {
289 str = NULL;
290 }
291 } else {
292 if (asprintf(&str, "%03.1Lf", set->val.num) == -1) {
293 str = NULL;
294 }
295 }
296 LY_CHECK_ERR_RET(!str, LOGMEM(NULL), );
297
298 LOGDBG(LY_LDGXPATH, "\t%s", str);
299 free(str);
300 }
301}
302
303#endif
304
305/**
306 * @brief Realloc the string \p str.
307 *
308 * @param[in] ctx libyang context for logging.
309 * @param[in] needed How much free space is required.
310 * @param[in,out] str Pointer to the string to use.
311 * @param[in,out] used Used bytes in \p str.
312 * @param[in,out] size Allocated bytes in \p str.
313 * @return LY_ERR
314 */
315static LY_ERR
Michal Vasko52927e22020-03-16 17:26:14 +0100316cast_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 +0200317{
318 if (*size - *used < needed) {
319 do {
320 if ((UINT16_MAX - *size) < LYXP_STRING_CAST_SIZE_STEP) {
321 LOGERR(ctx, LY_EINVAL, "XPath string length limit (%u) reached.", UINT16_MAX);
322 return LY_EINVAL;
323 }
324 *size += LYXP_STRING_CAST_SIZE_STEP;
325 } while (*size - *used < needed);
326 *str = ly_realloc(*str, *size * sizeof(char));
327 LY_CHECK_ERR_RET(!(*str), LOGMEM(ctx), LY_EMEM);
328 }
329
330 return LY_SUCCESS;
331}
332
333/**
334 * @brief Cast nodes recursively to one string @p str.
335 *
336 * @param[in] node Node to cast.
337 * @param[in] fake_cont Whether to put the data into a "fake" container.
338 * @param[in] root_type Type of the XPath root.
339 * @param[in] indent Current indent.
340 * @param[in,out] str Resulting string.
341 * @param[in,out] used Used bytes in @p str.
342 * @param[in,out] size Allocated bytes in @p str.
343 * @return LY_ERR
344 */
345static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200346cast_string_recursive(const struct lyd_node *node, ly_bool fake_cont, enum lyxp_node_type root_type, uint16_t indent, char **str,
Radek Krejci0f969882020-08-21 16:56:47 +0200347 uint16_t *used, uint16_t *size)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200348{
Radek Krejci7f769d72020-07-11 23:13:56 +0200349 char *buf, *line, *ptr = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200350 const char *value_str;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200351 const struct lyd_node *child;
Michal Vasko60ea6352020-06-29 13:39:39 +0200352 struct lyd_node *tree;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200353 struct lyd_node_any *any;
354 LY_ERR rc;
355
356 if ((root_type == LYXP_NODE_ROOT_CONFIG) && (node->schema->flags & LYS_CONFIG_R)) {
357 return LY_SUCCESS;
358 }
359
360 if (fake_cont) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200361 rc = cast_string_realloc(LYD_CTX(node), 1, str, used, size);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200362 LY_CHECK_RET(rc);
363 strcpy(*str + (*used - 1), "\n");
364 ++(*used);
365
366 ++indent;
367 }
368
369 switch (node->schema->nodetype) {
370 case LYS_CONTAINER:
371 case LYS_LIST:
372 case LYS_RPC:
373 case LYS_NOTIF:
Michal Vaskob7be7a82020-08-20 09:09:04 +0200374 rc = cast_string_realloc(LYD_CTX(node), 1, str, used, size);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200375 LY_CHECK_RET(rc);
376 strcpy(*str + (*used - 1), "\n");
377 ++(*used);
378
Radek Krejcia1c1e542020-09-29 16:06:52 +0200379 for (child = lyd_child(node); child; child = child->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200380 rc = cast_string_recursive(child, 0, root_type, indent + 1, str, used, size);
381 LY_CHECK_RET(rc);
382 }
383
384 break;
385
386 case LYS_LEAF:
387 case LYS_LEAFLIST:
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200388 value_str = lyd_get_value(node);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200389
390 /* print indent */
Michal Vaskob7be7a82020-08-20 09:09:04 +0200391 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 +0200392 memset(*str + (*used - 1), ' ', indent * 2);
393 *used += indent * 2;
394
395 /* print value */
396 if (*used == 1) {
397 sprintf(*str + (*used - 1), "%s", value_str);
398 *used += strlen(value_str);
399 } else {
400 sprintf(*str + (*used - 1), "%s\n", value_str);
401 *used += strlen(value_str) + 1;
402 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200403
404 break;
405
406 case LYS_ANYXML:
407 case LYS_ANYDATA:
408 any = (struct lyd_node_any *)node;
409 if (!(void *)any->value.tree) {
410 /* no content */
411 buf = strdup("");
Michal Vaskob7be7a82020-08-20 09:09:04 +0200412 LY_CHECK_ERR_RET(!buf, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200413 } else {
Radek Krejci241f6b52020-05-21 18:13:49 +0200414 struct ly_out *out;
Radek Krejcia5bba312020-01-09 15:41:20 +0100415
Michal Vasko60ea6352020-06-29 13:39:39 +0200416 if (any->value_type == LYD_ANYDATA_LYB) {
417 /* try to parse it into a data tree */
Michal Vaskob7be7a82020-08-20 09:09:04 +0200418 if (lyd_parse_data_mem((struct ly_ctx *)LYD_CTX(node), any->value.mem, LYD_LYB, LYD_PARSE_ONLY | LYD_PARSE_STRICT, 0, &tree) == LY_SUCCESS) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200419 /* successfully parsed */
420 free(any->value.mem);
421 any->value.tree = tree;
422 any->value_type = LYD_ANYDATA_DATATREE;
423 }
Radek Krejci7931b192020-06-25 17:05:03 +0200424 /* error is covered by the following switch where LYD_ANYDATA_LYB causes failure */
Michal Vasko60ea6352020-06-29 13:39:39 +0200425 }
426
Michal Vasko03ff5a72019-09-11 13:49:33 +0200427 switch (any->value_type) {
428 case LYD_ANYDATA_STRING:
429 case LYD_ANYDATA_XML:
430 case LYD_ANYDATA_JSON:
431 buf = strdup(any->value.json);
Michal Vaskob7be7a82020-08-20 09:09:04 +0200432 LY_CHECK_ERR_RET(!buf, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200433 break;
434 case LYD_ANYDATA_DATATREE:
Radek Krejci84ce7b12020-06-11 17:28:25 +0200435 LY_CHECK_RET(ly_out_new_memory(&buf, 0, &out));
Michal Vasko3a41dff2020-07-15 14:30:28 +0200436 rc = lyd_print_all(out, any->value.tree, LYD_XML, 0);
Radek Krejci241f6b52020-05-21 18:13:49 +0200437 ly_out_free(out, NULL, 0);
Radek Krejcia5bba312020-01-09 15:41:20 +0100438 LY_CHECK_RET(rc < 0, -rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200439 break;
Michal Vasko60ea6352020-06-29 13:39:39 +0200440 case LYD_ANYDATA_LYB:
Michal Vaskob7be7a82020-08-20 09:09:04 +0200441 LOGERR(LYD_CTX(node), LY_EINVAL, "Cannot convert LYB anydata into string.");
Michal Vasko60ea6352020-06-29 13:39:39 +0200442 return LY_EINVAL;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200443 }
444 }
445
446 line = strtok_r(buf, "\n", &ptr);
447 do {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200448 rc = cast_string_realloc(LYD_CTX(node), indent * 2 + strlen(line) + 1, str, used, size);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200449 if (rc != LY_SUCCESS) {
450 free(buf);
451 return rc;
452 }
453 memset(*str + (*used - 1), ' ', indent * 2);
454 *used += indent * 2;
455
456 strcpy(*str + (*used - 1), line);
457 *used += strlen(line);
458
459 strcpy(*str + (*used - 1), "\n");
460 *used += 1;
461 } while ((line = strtok_r(NULL, "\n", &ptr)));
462
463 free(buf);
464 break;
465
466 default:
Michal Vaskob7be7a82020-08-20 09:09:04 +0200467 LOGINT_RET(LYD_CTX(node));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200468 }
469
470 if (fake_cont) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200471 rc = cast_string_realloc(LYD_CTX(node), 1, str, used, size);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200472 LY_CHECK_RET(rc);
473 strcpy(*str + (*used - 1), "\n");
474 ++(*used);
475
476 --indent;
477 }
478
479 return LY_SUCCESS;
480}
481
482/**
483 * @brief Cast an element into a string.
484 *
485 * @param[in] node Node to cast.
486 * @param[in] fake_cont Whether to put the data into a "fake" container.
487 * @param[in] root_type Type of the XPath root.
488 * @param[out] str Element cast to dynamically-allocated string.
489 * @return LY_ERR
490 */
491static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200492cast_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 +0200493{
494 uint16_t used, size;
495 LY_ERR rc;
496
497 *str = malloc(LYXP_STRING_CAST_SIZE_START * sizeof(char));
Michal Vaskob7be7a82020-08-20 09:09:04 +0200498 LY_CHECK_ERR_RET(!*str, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200499 (*str)[0] = '\0';
500 used = 1;
501 size = LYXP_STRING_CAST_SIZE_START;
502
503 rc = cast_string_recursive(node, fake_cont, root_type, 0, str, &used, &size);
504 if (rc != LY_SUCCESS) {
505 free(*str);
506 return rc;
507 }
508
509 if (size > used) {
510 *str = ly_realloc(*str, used * sizeof(char));
Michal Vaskob7be7a82020-08-20 09:09:04 +0200511 LY_CHECK_ERR_RET(!*str, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200512 }
513 return LY_SUCCESS;
514}
515
516/**
517 * @brief Cast a LYXP_SET_NODE_SET set into a string.
518 * Context position aware.
519 *
520 * @param[in] set Set to cast.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200521 * @param[out] str Cast dynamically-allocated string.
522 * @return LY_ERR
523 */
524static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100525cast_node_set_to_string(struct lyxp_set *set, char **str)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200526{
Michal Vaskoaa677062021-03-09 13:52:53 +0100527 if (!set->used) {
528 *str = strdup("");
529 if (!*str) {
530 LOGMEM_RET(set->ctx);
531 }
532 return LY_SUCCESS;
533 }
534
Michal Vasko03ff5a72019-09-11 13:49:33 +0200535 switch (set->val.nodes[0].type) {
Michal Vasko2caefc12019-11-14 16:07:56 +0100536 case LYXP_NODE_NONE:
537 /* invalid */
538 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200539 case LYXP_NODE_ROOT:
540 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100541 return cast_string_elem(set->val.nodes[0].node, 1, set->root_type, str);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200542 case LYXP_NODE_ELEM:
543 case LYXP_NODE_TEXT:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100544 return cast_string_elem(set->val.nodes[0].node, 0, set->root_type, str);
Michal Vasko9f96a052020-03-10 09:41:45 +0100545 case LYXP_NODE_META:
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200546 *str = strdup(lyd_get_meta_value(set->val.meta[0].meta));
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200547 if (!*str) {
548 LOGMEM_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200549 }
550 return LY_SUCCESS;
551 }
552
553 LOGINT_RET(set->ctx);
554}
555
556/**
557 * @brief Cast a string into an XPath number.
558 *
559 * @param[in] str String to use.
560 * @return Cast number.
561 */
562static long double
563cast_string_to_number(const char *str)
564{
565 long double num;
566 char *ptr;
567
568 errno = 0;
569 num = strtold(str, &ptr);
570 if (errno || *ptr) {
571 num = NAN;
572 }
573 return num;
574}
575
576/**
577 * @brief Callback for checking value equality.
578 *
Michal Vasko62524a92021-02-26 10:08:50 +0100579 * Implementation of ::lyht_value_equal_cb.
Radek Krejci857189e2020-09-01 13:26:36 +0200580 *
Michal Vasko03ff5a72019-09-11 13:49:33 +0200581 * @param[in] val1_p First value.
582 * @param[in] val2_p Second value.
583 * @param[in] mod Whether hash table is being modified.
584 * @param[in] cb_data Callback data.
Radek Krejci857189e2020-09-01 13:26:36 +0200585 * @return Boolean value whether values are equal or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200586 */
Radek Krejci857189e2020-09-01 13:26:36 +0200587static ly_bool
588set_values_equal_cb(void *val1_p, void *val2_p, ly_bool UNUSED(mod), void *UNUSED(cb_data))
Michal Vasko03ff5a72019-09-11 13:49:33 +0200589{
590 struct lyxp_set_hash_node *val1, *val2;
591
592 val1 = (struct lyxp_set_hash_node *)val1_p;
593 val2 = (struct lyxp_set_hash_node *)val2_p;
594
595 if ((val1->node == val2->node) && (val1->type == val2->type)) {
596 return 1;
597 }
598
599 return 0;
600}
601
602/**
603 * @brief Insert node and its hash into set.
604 *
605 * @param[in] set et to insert to.
606 * @param[in] node Node with hash.
607 * @param[in] type Node type.
608 */
609static void
610set_insert_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type)
611{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200612 LY_ERR r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200613 uint32_t i, hash;
614 struct lyxp_set_hash_node hnode;
615
616 if (!set->ht && (set->used >= LYD_HT_MIN_ITEMS)) {
617 /* create hash table and add all the nodes */
618 set->ht = lyht_new(1, sizeof(struct lyxp_set_hash_node), set_values_equal_cb, NULL, 1);
619 for (i = 0; i < set->used; ++i) {
620 hnode.node = set->val.nodes[i].node;
621 hnode.type = set->val.nodes[i].type;
622
623 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
624 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
625 hash = dict_hash_multi(hash, NULL, 0);
626
627 r = lyht_insert(set->ht, &hnode, hash, NULL);
628 assert(!r);
629 (void)r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200630
Michal Vasko9d6befd2019-12-11 14:56:56 +0100631 if (hnode.node == node) {
632 /* it was just added, do not add it twice */
633 node = NULL;
634 }
635 }
636 }
637
638 if (set->ht && node) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200639 /* add the new node into hash table */
640 hnode.node = node;
641 hnode.type = type;
642
643 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
644 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
645 hash = dict_hash_multi(hash, NULL, 0);
646
647 r = lyht_insert(set->ht, &hnode, hash, NULL);
648 assert(!r);
649 (void)r;
650 }
651}
652
653/**
654 * @brief Remove node and its hash from set.
655 *
656 * @param[in] set Set to remove from.
657 * @param[in] node Node to remove.
658 * @param[in] type Node type.
659 */
660static void
661set_remove_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type)
662{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200663 LY_ERR r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200664 struct lyxp_set_hash_node hnode;
665 uint32_t hash;
666
667 if (set->ht) {
668 hnode.node = node;
669 hnode.type = type;
670
671 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
672 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
673 hash = dict_hash_multi(hash, NULL, 0);
674
675 r = lyht_remove(set->ht, &hnode, hash);
676 assert(!r);
677 (void)r;
678
679 if (!set->ht->used) {
680 lyht_free(set->ht);
681 set->ht = NULL;
682 }
683 }
684}
685
686/**
687 * @brief Check whether node is in set based on its hash.
688 *
689 * @param[in] set Set to search in.
690 * @param[in] node Node to search for.
691 * @param[in] type Node type.
692 * @param[in] skip_idx Index in @p set to skip.
693 * @return LY_ERR
694 */
695static LY_ERR
696set_dup_node_hash_check(const struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type, int skip_idx)
697{
698 struct lyxp_set_hash_node hnode, *match_p;
699 uint32_t hash;
700
701 hnode.node = node;
702 hnode.type = type;
703
704 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
705 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
706 hash = dict_hash_multi(hash, NULL, 0);
707
708 if (!lyht_find(set->ht, &hnode, hash, (void **)&match_p)) {
709 if ((skip_idx > -1) && (set->val.nodes[skip_idx].node == match_p->node) && (set->val.nodes[skip_idx].type == match_p->type)) {
710 /* we found it on the index that should be skipped, find another */
711 hnode = *match_p;
712 if (lyht_find_next(set->ht, &hnode, hash, (void **)&match_p)) {
713 /* none other found */
714 return LY_SUCCESS;
715 }
716 }
717
718 return LY_EEXIST;
719 }
720
721 /* not found */
722 return LY_SUCCESS;
723}
724
Michal Vaskod3678892020-05-21 10:06:58 +0200725void
726lyxp_set_free_content(struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200727{
728 if (!set) {
729 return;
730 }
731
732 if (set->type == LYXP_SET_NODE_SET) {
733 free(set->val.nodes);
734 lyht_free(set->ht);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200735 } else if (set->type == LYXP_SET_SCNODE_SET) {
736 free(set->val.scnodes);
Michal Vaskod3678892020-05-21 10:06:58 +0200737 lyht_free(set->ht);
738 } else {
739 if (set->type == LYXP_SET_STRING) {
740 free(set->val.str);
741 }
742 set->type = LYXP_SET_NODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200743 }
Michal Vaskod3678892020-05-21 10:06:58 +0200744
745 set->val.nodes = NULL;
746 set->used = 0;
747 set->size = 0;
748 set->ht = NULL;
749 set->ctx_pos = 0;
aPiecek748da732021-06-01 09:56:43 +0200750 set->ctx_size = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200751}
752
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100753/**
754 * @brief Free dynamically-allocated set.
755 *
756 * @param[in] set Set to free.
757 */
758static void
Michal Vasko03ff5a72019-09-11 13:49:33 +0200759lyxp_set_free(struct lyxp_set *set)
760{
761 if (!set) {
762 return;
763 }
764
Michal Vaskod3678892020-05-21 10:06:58 +0200765 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200766 free(set);
767}
768
769/**
770 * @brief Initialize set context.
771 *
772 * @param[in] new Set to initialize.
773 * @param[in] set Arbitrary initialized set.
774 */
775static void
Michal Vasko4c7763f2020-07-27 17:40:37 +0200776set_init(struct lyxp_set *new, const struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200777{
778 memset(new, 0, sizeof *new);
Michal Vasko02a77382019-09-12 11:47:35 +0200779 if (set) {
780 new->ctx = set->ctx;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200781 new->cur_node = set->cur_node;
Michal Vasko588112f2019-12-10 14:51:53 +0100782 new->root_type = set->root_type;
Michal Vasko6b26e742020-07-17 15:02:10 +0200783 new->context_op = set->context_op;
Michal Vaskof03ed032020-03-04 13:31:44 +0100784 new->tree = set->tree;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200785 new->cur_mod = set->cur_mod;
Michal Vasko02a77382019-09-12 11:47:35 +0200786 new->format = set->format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200787 new->prefix_data = set->prefix_data;
aPiecekfba75362021-10-07 12:39:48 +0200788 new->vars = set->vars;
Michal Vasko02a77382019-09-12 11:47:35 +0200789 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200790}
791
792/**
793 * @brief Create a deep copy of a set.
794 *
795 * @param[in] set Set to copy.
796 * @return Copy of @p set.
797 */
798static struct lyxp_set *
799set_copy(struct lyxp_set *set)
800{
801 struct lyxp_set *ret;
Michal Vasko1fdd8fa2021-01-08 09:21:45 +0100802 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200803
804 if (!set) {
805 return NULL;
806 }
807
808 ret = malloc(sizeof *ret);
809 LY_CHECK_ERR_RET(!ret, LOGMEM(set->ctx), NULL);
810 set_init(ret, set);
811
812 if (set->type == LYXP_SET_SCNODE_SET) {
813 ret->type = set->type;
814
815 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100816 if ((set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) ||
817 (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +0200818 uint32_t idx;
819 LY_CHECK_ERR_RET(lyxp_set_scnode_insert_node(ret, set->val.scnodes[i].scnode, set->val.scnodes[i].type, &idx),
820 lyxp_set_free(ret), NULL);
Michal Vasko3f27c522020-01-06 08:37:49 +0100821 /* coverity seems to think scnodes can be NULL */
Radek Krejciaa6b53f2020-08-27 15:19:03 +0200822 if (!ret->val.scnodes) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200823 lyxp_set_free(ret);
824 return NULL;
825 }
Michal Vaskoba716542019-12-16 10:01:58 +0100826 ret->val.scnodes[idx].in_ctx = set->val.scnodes[i].in_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200827 }
828 }
829 } else if (set->type == LYXP_SET_NODE_SET) {
830 ret->type = set->type;
Michal Vasko08e9b112021-06-11 15:41:17 +0200831 if (set->used) {
832 ret->val.nodes = malloc(set->used * sizeof *ret->val.nodes);
833 LY_CHECK_ERR_RET(!ret->val.nodes, LOGMEM(set->ctx); free(ret), NULL);
834 memcpy(ret->val.nodes, set->val.nodes, set->used * sizeof *ret->val.nodes);
835 } else {
836 ret->val.nodes = NULL;
837 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200838
839 ret->used = ret->size = set->used;
840 ret->ctx_pos = set->ctx_pos;
841 ret->ctx_size = set->ctx_size;
Michal Vasko4a04e542021-02-01 08:58:30 +0100842 if (set->ht) {
843 ret->ht = lyht_dup(set->ht);
844 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200845 } else {
Radek Krejci0f969882020-08-21 16:56:47 +0200846 memcpy(ret, set, sizeof *ret);
847 if (set->type == LYXP_SET_STRING) {
848 ret->val.str = strdup(set->val.str);
849 LY_CHECK_ERR_RET(!ret->val.str, LOGMEM(set->ctx); free(ret), NULL);
850 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200851 }
852
853 return ret;
854}
855
856/**
857 * @brief Fill XPath set with a string. Any current data are disposed of.
858 *
859 * @param[in] set Set to fill.
860 * @param[in] string String to fill into \p set.
861 * @param[in] str_len Length of \p string. 0 is a valid value!
862 */
863static void
864set_fill_string(struct lyxp_set *set, const char *string, uint16_t str_len)
865{
Michal Vaskod3678892020-05-21 10:06:58 +0200866 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200867
868 set->type = LYXP_SET_STRING;
869 if ((str_len == 0) && (string[0] != '\0')) {
870 string = "";
871 }
872 set->val.str = strndup(string, str_len);
873}
874
875/**
876 * @brief Fill XPath set with a number. Any current data are disposed of.
877 *
878 * @param[in] set Set to fill.
879 * @param[in] number Number to fill into \p set.
880 */
881static void
882set_fill_number(struct lyxp_set *set, long double number)
883{
Michal Vaskod3678892020-05-21 10:06:58 +0200884 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200885
886 set->type = LYXP_SET_NUMBER;
887 set->val.num = number;
888}
889
890/**
891 * @brief Fill XPath set with a boolean. Any current data are disposed of.
892 *
893 * @param[in] set Set to fill.
894 * @param[in] boolean Boolean to fill into \p set.
895 */
896static void
Radek Krejci857189e2020-09-01 13:26:36 +0200897set_fill_boolean(struct lyxp_set *set, ly_bool boolean)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200898{
Michal Vaskod3678892020-05-21 10:06:58 +0200899 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200900
901 set->type = LYXP_SET_BOOLEAN;
Michal Vasko004d3152020-06-11 19:59:22 +0200902 set->val.bln = boolean;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200903}
904
905/**
906 * @brief Fill XPath set with the value from another set (deep assign).
907 * Any current data are disposed of.
908 *
909 * @param[in] trg Set to fill.
910 * @param[in] src Source set to copy into \p trg.
911 */
912static void
Michal Vasko4c7763f2020-07-27 17:40:37 +0200913set_fill_set(struct lyxp_set *trg, const struct lyxp_set *src)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200914{
915 if (!trg || !src) {
916 return;
917 }
918
919 if (trg->type == LYXP_SET_NODE_SET) {
920 free(trg->val.nodes);
921 } else if (trg->type == LYXP_SET_STRING) {
922 free(trg->val.str);
923 }
924 set_init(trg, src);
925
926 if (src->type == LYXP_SET_SCNODE_SET) {
927 trg->type = LYXP_SET_SCNODE_SET;
928 trg->used = src->used;
929 trg->size = src->used;
930
Michal Vasko08e9b112021-06-11 15:41:17 +0200931 if (trg->size) {
932 trg->val.scnodes = ly_realloc(trg->val.scnodes, trg->size * sizeof *trg->val.scnodes);
933 LY_CHECK_ERR_RET(!trg->val.scnodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), );
934 memcpy(trg->val.scnodes, src->val.scnodes, src->used * sizeof *src->val.scnodes);
935 } else {
936 trg->val.scnodes = NULL;
937 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200938 } else if (src->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +0200939 set_fill_boolean(trg, src->val.bln);
Michal Vasko44f3d2c2020-08-24 09:49:38 +0200940 } else if (src->type == LYXP_SET_NUMBER) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200941 set_fill_number(trg, src->val.num);
942 } else if (src->type == LYXP_SET_STRING) {
943 set_fill_string(trg, src->val.str, strlen(src->val.str));
944 } else {
945 if (trg->type == LYXP_SET_NODE_SET) {
946 free(trg->val.nodes);
947 } else if (trg->type == LYXP_SET_STRING) {
948 free(trg->val.str);
949 }
950
Michal Vaskod3678892020-05-21 10:06:58 +0200951 assert(src->type == LYXP_SET_NODE_SET);
952
953 trg->type = LYXP_SET_NODE_SET;
954 trg->used = src->used;
955 trg->size = src->used;
956 trg->ctx_pos = src->ctx_pos;
957 trg->ctx_size = src->ctx_size;
958
Michal Vasko08e9b112021-06-11 15:41:17 +0200959 if (trg->size) {
960 trg->val.nodes = malloc(trg->size * sizeof *trg->val.nodes);
961 LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), );
962 memcpy(trg->val.nodes, src->val.nodes, src->used * sizeof *src->val.nodes);
963 } else {
964 trg->val.nodes = NULL;
965 }
Michal Vaskod3678892020-05-21 10:06:58 +0200966 if (src->ht) {
967 trg->ht = lyht_dup(src->ht);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200968 } else {
Michal Vaskod3678892020-05-21 10:06:58 +0200969 trg->ht = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200970 }
971 }
972}
973
974/**
975 * @brief Clear context of all schema nodes.
976 *
977 * @param[in] set Set to clear.
Michal Vasko1a09b212021-05-06 13:00:10 +0200978 * @param[in] new_ctx New context state for all the nodes currently in the context.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200979 */
980static void
Michal Vasko1a09b212021-05-06 13:00:10 +0200981set_scnode_clear_ctx(struct lyxp_set *set, int32_t new_ctx)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200982{
983 uint32_t i;
984
985 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100986 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko1a09b212021-05-06 13:00:10 +0200987 set->val.scnodes[i].in_ctx = new_ctx;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100988 } else if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START) {
989 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200990 }
991 }
992}
993
994/**
995 * @brief Remove a node from a set. Removing last node changes
996 * set into LYXP_SET_EMPTY. Context position aware.
997 *
998 * @param[in] set Set to use.
999 * @param[in] idx Index from @p set of the node to be removed.
1000 */
1001static void
1002set_remove_node(struct lyxp_set *set, uint32_t idx)
1003{
1004 assert(set && (set->type == LYXP_SET_NODE_SET));
1005 assert(idx < set->used);
1006
1007 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
1008
1009 --set->used;
Michal Vasko08e9b112021-06-11 15:41:17 +02001010 if (idx < set->used) {
1011 memmove(&set->val.nodes[idx], &set->val.nodes[idx + 1], (set->used - idx) * sizeof *set->val.nodes);
1012 } else if (!set->used) {
Michal Vaskod3678892020-05-21 10:06:58 +02001013 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001014 }
1015}
1016
1017/**
Michal Vasko2caefc12019-11-14 16:07:56 +01001018 * @brief Remove a node from a set by setting its type to LYXP_NODE_NONE.
Michal Vasko57eab132019-09-24 11:46:26 +02001019 *
1020 * @param[in] set Set to use.
1021 * @param[in] idx Index from @p set of the node to be removed.
1022 */
1023static void
Michal Vasko2caefc12019-11-14 16:07:56 +01001024set_remove_node_none(struct lyxp_set *set, uint32_t idx)
Michal Vasko57eab132019-09-24 11:46:26 +02001025{
1026 assert(set && (set->type == LYXP_SET_NODE_SET));
1027 assert(idx < set->used);
1028
Michal Vasko2caefc12019-11-14 16:07:56 +01001029 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1030 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
1031 }
1032 set->val.nodes[idx].type = LYXP_NODE_NONE;
Michal Vasko57eab132019-09-24 11:46:26 +02001033}
1034
1035/**
Michal Vasko2caefc12019-11-14 16:07:56 +01001036 * @brief Remove all LYXP_NODE_NONE nodes from a set. Removing last node changes
Michal Vasko57eab132019-09-24 11:46:26 +02001037 * set into LYXP_SET_EMPTY. Context position aware.
1038 *
1039 * @param[in] set Set to consolidate.
1040 */
1041static void
Michal Vasko2caefc12019-11-14 16:07:56 +01001042set_remove_nodes_none(struct lyxp_set *set)
Michal Vasko57eab132019-09-24 11:46:26 +02001043{
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01001044 uint32_t i, orig_used, end = 0;
1045 int64_t start;
Michal Vasko57eab132019-09-24 11:46:26 +02001046
Michal Vaskod3678892020-05-21 10:06:58 +02001047 assert(set);
Michal Vasko57eab132019-09-24 11:46:26 +02001048
1049 orig_used = set->used;
1050 set->used = 0;
Michal Vaskod989ba02020-08-24 10:59:24 +02001051 for (i = 0; i < orig_used; ) {
Michal Vasko57eab132019-09-24 11:46:26 +02001052 start = -1;
1053 do {
Michal Vasko2caefc12019-11-14 16:07:56 +01001054 if ((set->val.nodes[i].type != LYXP_NODE_NONE) && (start == -1)) {
Michal Vasko57eab132019-09-24 11:46:26 +02001055 start = i;
Michal Vasko2caefc12019-11-14 16:07:56 +01001056 } else if ((start > -1) && (set->val.nodes[i].type == LYXP_NODE_NONE)) {
Michal Vasko57eab132019-09-24 11:46:26 +02001057 end = i;
1058 ++i;
1059 break;
1060 }
1061
1062 ++i;
1063 if (i == orig_used) {
1064 end = i;
1065 }
1066 } while (i < orig_used);
1067
1068 if (start > -1) {
1069 /* move the whole chunk of valid nodes together */
1070 if (set->used != (unsigned)start) {
1071 memmove(&set->val.nodes[set->used], &set->val.nodes[start], (end - start) * sizeof *set->val.nodes);
1072 }
1073 set->used += end - start;
1074 }
1075 }
Michal Vasko57eab132019-09-24 11:46:26 +02001076}
1077
1078/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02001079 * @brief Check for duplicates in a node set.
1080 *
1081 * @param[in] set Set to check.
1082 * @param[in] node Node to look for in @p set.
1083 * @param[in] node_type Type of @p node.
1084 * @param[in] skip_idx Index from @p set to skip.
1085 * @return LY_ERR
1086 */
1087static LY_ERR
1088set_dup_node_check(const struct lyxp_set *set, const struct lyd_node *node, enum lyxp_node_type node_type, int skip_idx)
1089{
1090 uint32_t i;
1091
Michal Vasko2caefc12019-11-14 16:07:56 +01001092 if (set->ht && node) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001093 return set_dup_node_hash_check(set, (struct lyd_node *)node, node_type, skip_idx);
1094 }
1095
1096 for (i = 0; i < set->used; ++i) {
1097 if ((skip_idx > -1) && (i == (unsigned)skip_idx)) {
1098 continue;
1099 }
1100
1101 if ((set->val.nodes[i].node == node) && (set->val.nodes[i].type == node_type)) {
1102 return LY_EEXIST;
1103 }
1104 }
1105
1106 return LY_SUCCESS;
1107}
1108
Radek Krejci857189e2020-09-01 13:26:36 +02001109ly_bool
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001110lyxp_set_scnode_contains(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type, int skip_idx,
1111 uint32_t *index_p)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001112{
1113 uint32_t i;
1114
1115 for (i = 0; i < set->used; ++i) {
1116 if ((skip_idx > -1) && (i == (unsigned)skip_idx)) {
1117 continue;
1118 }
1119
1120 if ((set->val.scnodes[i].scnode == node) && (set->val.scnodes[i].type == node_type)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001121 if (index_p) {
1122 *index_p = i;
1123 }
1124 return 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001125 }
1126 }
1127
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001128 return 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001129}
1130
Michal Vaskoecd62de2019-11-13 12:35:11 +01001131void
1132lyxp_set_scnode_merge(struct lyxp_set *set1, struct lyxp_set *set2)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001133{
1134 uint32_t orig_used, i, j;
1135
Michal Vaskod3678892020-05-21 10:06:58 +02001136 assert((set1->type == LYXP_SET_SCNODE_SET) && (set2->type == LYXP_SET_SCNODE_SET));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001137
Michal Vaskod3678892020-05-21 10:06:58 +02001138 if (!set2->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001139 return;
1140 }
1141
Michal Vaskod3678892020-05-21 10:06:58 +02001142 if (!set1->used) {
aPiecekadc1e4f2021-10-07 11:15:12 +02001143 /* release hidden allocated data (lyxp_set.size) */
1144 lyxp_set_free_content(set1);
1145 /* direct copying of the entire structure */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001146 memcpy(set1, set2, sizeof *set1);
1147 return;
1148 }
1149
1150 if (set1->used + set2->used > set1->size) {
1151 set1->size = set1->used + set2->used;
1152 set1->val.scnodes = ly_realloc(set1->val.scnodes, set1->size * sizeof *set1->val.scnodes);
1153 LY_CHECK_ERR_RET(!set1->val.scnodes, LOGMEM(set1->ctx), );
1154 }
1155
1156 orig_used = set1->used;
1157
1158 for (i = 0; i < set2->used; ++i) {
1159 for (j = 0; j < orig_used; ++j) {
1160 /* detect duplicities */
1161 if (set1->val.scnodes[j].scnode == set2->val.scnodes[i].scnode) {
1162 break;
1163 }
1164 }
1165
Michal Vasko0587bce2020-12-10 12:19:21 +01001166 if (j < orig_used) {
1167 /* node is there, but update its status if needed */
1168 if (set1->val.scnodes[j].in_ctx == LYXP_SET_SCNODE_START_USED) {
1169 set1->val.scnodes[j].in_ctx = set2->val.scnodes[i].in_ctx;
Michal Vasko1a09b212021-05-06 13:00:10 +02001170 } else if ((set1->val.scnodes[j].in_ctx == LYXP_SET_SCNODE_ATOM_NODE) &&
1171 (set2->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_VAL)) {
1172 set1->val.scnodes[j].in_ctx = set2->val.scnodes[i].in_ctx;
Michal Vasko0587bce2020-12-10 12:19:21 +01001173 }
1174 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001175 memcpy(&set1->val.scnodes[set1->used], &set2->val.scnodes[i], sizeof *set2->val.scnodes);
1176 ++set1->used;
1177 }
1178 }
1179
Michal Vaskod3678892020-05-21 10:06:58 +02001180 lyxp_set_free_content(set2);
1181 set2->type = LYXP_SET_SCNODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001182}
1183
1184/**
1185 * @brief Insert a node into a set. Context position aware.
1186 *
1187 * @param[in] set Set to use.
1188 * @param[in] node Node to insert to @p set.
1189 * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting.
1190 * @param[in] node_type Node type of @p node.
1191 * @param[in] idx Index in @p set to insert into.
1192 */
1193static void
1194set_insert_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx)
1195{
Michal Vaskod3678892020-05-21 10:06:58 +02001196 assert(set && (set->type == LYXP_SET_NODE_SET));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001197
Michal Vaskod3678892020-05-21 10:06:58 +02001198 if (!set->size) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001199 /* first item */
1200 if (idx) {
1201 /* no real harm done, but it is a bug */
1202 LOGINT(set->ctx);
1203 idx = 0;
1204 }
1205 set->val.nodes = malloc(LYXP_SET_SIZE_START * sizeof *set->val.nodes);
1206 LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), );
1207 set->type = LYXP_SET_NODE_SET;
1208 set->used = 0;
1209 set->size = LYXP_SET_SIZE_START;
1210 set->ctx_pos = 1;
1211 set->ctx_size = 1;
1212 set->ht = NULL;
1213 } else {
1214 /* not an empty set */
1215 if (set->used == set->size) {
1216
1217 /* set is full */
1218 set->val.nodes = ly_realloc(set->val.nodes, (set->size + LYXP_SET_SIZE_STEP) * sizeof *set->val.nodes);
1219 LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), );
1220 set->size += LYXP_SET_SIZE_STEP;
1221 }
1222
1223 if (idx > set->used) {
1224 LOGINT(set->ctx);
1225 idx = set->used;
1226 }
1227
1228 /* make space for the new node */
1229 if (idx < set->used) {
1230 memmove(&set->val.nodes[idx + 1], &set->val.nodes[idx], (set->used - idx) * sizeof *set->val.nodes);
1231 }
1232 }
1233
1234 /* finally assign the value */
1235 set->val.nodes[idx].node = (struct lyd_node *)node;
1236 set->val.nodes[idx].type = node_type;
1237 set->val.nodes[idx].pos = pos;
1238 ++set->used;
1239
Michal Vasko2416fdd2021-10-01 11:07:10 +02001240 /* add into hash table */
1241 set_insert_node_hash(set, (struct lyd_node *)node, node_type);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001242}
1243
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001244LY_ERR
1245lyxp_set_scnode_insert_node(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type, uint32_t *index_p)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001246{
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001247 uint32_t index;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001248
1249 assert(set->type == LYXP_SET_SCNODE_SET);
1250
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001251 if (lyxp_set_scnode_contains(set, node, node_type, -1, &index)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001252 set->val.scnodes[index].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001253 } else {
1254 if (set->used == set->size) {
1255 set->val.scnodes = ly_realloc(set->val.scnodes, (set->size + LYXP_SET_SIZE_STEP) * sizeof *set->val.scnodes);
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001256 LY_CHECK_ERR_RET(!set->val.scnodes, LOGMEM(set->ctx), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001257 set->size += LYXP_SET_SIZE_STEP;
1258 }
1259
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001260 index = set->used;
1261 set->val.scnodes[index].scnode = (struct lysc_node *)node;
1262 set->val.scnodes[index].type = node_type;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001263 set->val.scnodes[index].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001264 ++set->used;
1265 }
1266
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001267 if (index_p) {
1268 *index_p = index;
1269 }
1270
1271 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001272}
1273
1274/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02001275 * @brief Set all nodes with ctx 1 to a new unique context value.
1276 *
1277 * @param[in] set Set to modify.
1278 * @return New context value.
1279 */
Michal Vasko5c4e5892019-11-14 12:31:38 +01001280static int32_t
Michal Vasko03ff5a72019-09-11 13:49:33 +02001281set_scnode_new_in_ctx(struct lyxp_set *set)
1282{
Michal Vasko5c4e5892019-11-14 12:31:38 +01001283 uint32_t i;
1284 int32_t ret_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001285
1286 assert(set->type == LYXP_SET_SCNODE_SET);
1287
Radek Krejcif13b87b2020-12-01 22:02:17 +01001288 ret_ctx = LYXP_SET_SCNODE_ATOM_PRED_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001289retry:
1290 for (i = 0; i < set->used; ++i) {
1291 if (set->val.scnodes[i].in_ctx >= ret_ctx) {
1292 ret_ctx = set->val.scnodes[i].in_ctx + 1;
1293 goto retry;
1294 }
1295 }
1296 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001297 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001298 set->val.scnodes[i].in_ctx = ret_ctx;
1299 }
1300 }
1301
1302 return ret_ctx;
1303}
1304
1305/**
1306 * @brief Get unique @p node position in the data.
1307 *
1308 * @param[in] node Node to find.
1309 * @param[in] node_type Node type of @p node.
1310 * @param[in] root Root node.
1311 * @param[in] root_type Type of the XPath @p root node.
1312 * @param[in] prev Node that we think is before @p node in DFS from @p root. Can optionally
1313 * be used to increase efficiency and start the DFS from this node.
1314 * @param[in] prev_pos Node @p prev position. Optional, but must be set if @p prev is set.
1315 * @return Node position.
1316 */
1317static uint32_t
1318get_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 +02001319 enum lyxp_node_type root_type, const struct lyd_node **prev, uint32_t *prev_pos)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001320{
Michal Vasko56daf732020-08-10 10:57:18 +02001321 const struct lyd_node *elem = NULL, *top_sibling;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001322 uint32_t pos = 1;
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001323 ly_bool found = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001324
1325 assert(prev && prev_pos && !root->prev->next);
1326
1327 if ((node_type == LYXP_NODE_ROOT) || (node_type == LYXP_NODE_ROOT_CONFIG)) {
1328 return 0;
1329 }
1330
1331 if (*prev) {
1332 /* start from the previous element instead from the root */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001333 pos = *prev_pos;
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001334 for (top_sibling = *prev; top_sibling->parent; top_sibling = lyd_parent(top_sibling)) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001335 goto dfs_search;
1336 }
1337
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001338 LY_LIST_FOR(root, top_sibling) {
Michal Vasko56daf732020-08-10 10:57:18 +02001339 LYD_TREE_DFS_BEGIN(top_sibling, elem) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001340dfs_search:
Michal Vaskoa9309bb2021-07-09 09:31:55 +02001341 LYD_TREE_DFS_continue = 0;
1342
Michal Vasko56daf732020-08-10 10:57:18 +02001343 if (*prev && !elem) {
1344 /* resume previous DFS */
1345 elem = LYD_TREE_DFS_next = (struct lyd_node *)*prev;
1346 LYD_TREE_DFS_continue = 0;
1347 }
1348
Michal Vasko03ff5a72019-09-11 13:49:33 +02001349 if ((root_type == LYXP_NODE_ROOT_CONFIG) && (elem->schema->flags & LYS_CONFIG_R)) {
Michal Vasko56daf732020-08-10 10:57:18 +02001350 /* skip */
1351 LYD_TREE_DFS_continue = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001352 } else {
Michal Vasko56daf732020-08-10 10:57:18 +02001353 if (elem == node) {
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001354 found = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001355 break;
1356 }
Michal Vasko56daf732020-08-10 10:57:18 +02001357 ++pos;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001358 }
Michal Vasko56daf732020-08-10 10:57:18 +02001359
1360 LYD_TREE_DFS_END(top_sibling, elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001361 }
1362
1363 /* node found */
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001364 if (found) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001365 break;
1366 }
1367 }
1368
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001369 if (!found) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001370 if (!(*prev)) {
1371 /* we went from root and failed to find it, cannot be */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001372 LOGINT(LYD_CTX(node));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001373 return 0;
1374 } else {
Michal Vasko56daf732020-08-10 10:57:18 +02001375 /* start the search again from the beginning */
1376 *prev = root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001377
Michal Vasko56daf732020-08-10 10:57:18 +02001378 top_sibling = root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001379 pos = 1;
1380 goto dfs_search;
1381 }
1382 }
1383
1384 /* remember the last found node for next time */
1385 *prev = node;
1386 *prev_pos = pos;
1387
1388 return pos;
1389}
1390
1391/**
1392 * @brief Assign (fill) missing node positions.
1393 *
1394 * @param[in] set Set to fill positions in.
1395 * @param[in] root Context root node.
1396 * @param[in] root_type Context root type.
1397 * @return LY_ERR
1398 */
1399static LY_ERR
1400set_assign_pos(struct lyxp_set *set, const struct lyd_node *root, enum lyxp_node_type root_type)
1401{
1402 const struct lyd_node *prev = NULL, *tmp_node;
1403 uint32_t i, tmp_pos = 0;
1404
1405 for (i = 0; i < set->used; ++i) {
1406 if (!set->val.nodes[i].pos) {
1407 tmp_node = NULL;
1408 switch (set->val.nodes[i].type) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001409 case LYXP_NODE_META:
1410 tmp_node = set->val.meta[i].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001411 if (!tmp_node) {
1412 LOGINT_RET(root->schema->module->ctx);
1413 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01001414 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001415 case LYXP_NODE_ELEM:
1416 case LYXP_NODE_TEXT:
1417 if (!tmp_node) {
1418 tmp_node = set->val.nodes[i].node;
1419 }
1420 set->val.nodes[i].pos = get_node_pos(tmp_node, set->val.nodes[i].type, root, root_type, &prev, &tmp_pos);
1421 break;
1422 default:
1423 /* all roots have position 0 */
1424 break;
1425 }
1426 }
1427 }
1428
1429 return LY_SUCCESS;
1430}
1431
1432/**
Michal Vasko9f96a052020-03-10 09:41:45 +01001433 * @brief Get unique @p meta position in the parent metadata.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001434 *
Michal Vasko9f96a052020-03-10 09:41:45 +01001435 * @param[in] meta Metadata to use.
1436 * @return Metadata position.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001437 */
1438static uint16_t
Michal Vasko9f96a052020-03-10 09:41:45 +01001439get_meta_pos(struct lyd_meta *meta)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001440{
1441 uint16_t pos = 0;
Michal Vasko9f96a052020-03-10 09:41:45 +01001442 struct lyd_meta *meta2;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001443
Michal Vasko9f96a052020-03-10 09:41:45 +01001444 for (meta2 = meta->parent->meta; meta2 && (meta2 != meta); meta2 = meta2->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001445 ++pos;
1446 }
1447
Michal Vasko9f96a052020-03-10 09:41:45 +01001448 assert(meta2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001449 return pos;
1450}
1451
1452/**
1453 * @brief Compare 2 nodes in respect to XPath document order.
1454 *
1455 * @param[in] item1 1st node.
1456 * @param[in] item2 2nd node.
1457 * @return If 1st > 2nd returns 1, 1st == 2nd returns 0, and 1st < 2nd returns -1.
1458 */
1459static int
1460set_sort_compare(struct lyxp_set_node *item1, struct lyxp_set_node *item2)
1461{
Michal Vasko9f96a052020-03-10 09:41:45 +01001462 uint32_t meta_pos1 = 0, meta_pos2 = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001463
1464 if (item1->pos < item2->pos) {
1465 return -1;
1466 }
1467
1468 if (item1->pos > item2->pos) {
1469 return 1;
1470 }
1471
1472 /* node positions are equal, the fun case */
1473
1474 /* 1st ELEM - == - 2nd TEXT, 1st TEXT - == - 2nd ELEM */
1475 /* special case since text nodes are actually saved as their parents */
1476 if ((item1->node == item2->node) && (item1->type != item2->type)) {
1477 if (item1->type == LYXP_NODE_ELEM) {
1478 assert(item2->type == LYXP_NODE_TEXT);
1479 return -1;
1480 } else {
1481 assert((item1->type == LYXP_NODE_TEXT) && (item2->type == LYXP_NODE_ELEM));
1482 return 1;
1483 }
1484 }
1485
Michal Vasko9f96a052020-03-10 09:41:45 +01001486 /* we need meta positions now */
1487 if (item1->type == LYXP_NODE_META) {
1488 meta_pos1 = get_meta_pos((struct lyd_meta *)item1->node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001489 }
Michal Vasko9f96a052020-03-10 09:41:45 +01001490 if (item2->type == LYXP_NODE_META) {
1491 meta_pos2 = get_meta_pos((struct lyd_meta *)item2->node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001492 }
1493
Michal Vasko9f96a052020-03-10 09:41:45 +01001494 /* 1st ROOT - 2nd ROOT, 1st ELEM - 2nd ELEM, 1st TEXT - 2nd TEXT, 1st META - =pos= - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001495 /* check for duplicates */
1496 if (item1->node == item2->node) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001497 assert((item1->type == item2->type) && ((item1->type != LYXP_NODE_META) || (meta_pos1 == meta_pos2)));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001498 return 0;
1499 }
1500
Michal Vasko9f96a052020-03-10 09:41:45 +01001501 /* 1st ELEM - 2nd TEXT, 1st ELEM - any pos - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001502 /* elem is always first, 2nd node is after it */
1503 if (item1->type == LYXP_NODE_ELEM) {
1504 assert(item2->type != LYXP_NODE_ELEM);
1505 return -1;
1506 }
1507
Michal Vasko9f96a052020-03-10 09:41:45 +01001508 /* 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 +02001509 /* 2nd is before 1st */
Michal Vasko69730152020-10-09 16:30:07 +02001510 if (((item1->type == LYXP_NODE_TEXT) &&
1511 ((item2->type == LYXP_NODE_ELEM) || (item2->type == LYXP_NODE_META))) ||
1512 ((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_ELEM)) ||
1513 (((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_META)) &&
1514 (meta_pos1 > meta_pos2))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001515 return 1;
1516 }
1517
Michal Vasko9f96a052020-03-10 09:41:45 +01001518 /* 1st META - any pos - 2nd TEXT, 1st META <pos< - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001519 /* 2nd is after 1st */
1520 return -1;
1521}
1522
1523/**
1524 * @brief Set cast for comparisons.
1525 *
1526 * @param[in] trg Target set to cast source into.
1527 * @param[in] src Source set.
1528 * @param[in] type Target set type.
1529 * @param[in] src_idx Source set node index.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001530 * @return LY_ERR
1531 */
1532static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001533set_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 +02001534{
1535 assert(src->type == LYXP_SET_NODE_SET);
1536
1537 set_init(trg, src);
1538
1539 /* insert node into target set */
1540 set_insert_node(trg, src->val.nodes[src_idx].node, src->val.nodes[src_idx].pos, src->val.nodes[src_idx].type, 0);
1541
1542 /* cast target set appropriately */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001543 return lyxp_set_cast(trg, type);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001544}
1545
Michal Vasko4c7763f2020-07-27 17:40:37 +02001546/**
1547 * @brief Set content canonization for comparisons.
1548 *
1549 * @param[in] trg Target set to put the canononized source into.
1550 * @param[in] src Source set.
1551 * @param[in] xp_node Source XPath node/meta to use for canonization.
1552 * @return LY_ERR
1553 */
1554static LY_ERR
1555set_comp_canonize(struct lyxp_set *trg, const struct lyxp_set *src, const struct lyxp_set_node *xp_node)
1556{
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001557 const struct lysc_type *type = NULL;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001558 struct lyd_value val;
1559 struct ly_err_item *err = NULL;
1560 char *str, *ptr;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001561 LY_ERR rc;
1562
1563 /* is there anything to canonize even? */
1564 if ((src->type == LYXP_SET_NUMBER) || (src->type == LYXP_SET_STRING)) {
1565 /* do we have a type to use for canonization? */
1566 if ((xp_node->type == LYXP_NODE_ELEM) && (xp_node->node->schema->nodetype & LYD_NODE_TERM)) {
1567 type = ((struct lyd_node_term *)xp_node->node)->value.realtype;
1568 } else if (xp_node->type == LYXP_NODE_META) {
1569 type = ((struct lyd_meta *)xp_node->node)->value.realtype;
1570 }
1571 }
1572 if (!type) {
1573 goto fill;
1574 }
1575
1576 if (src->type == LYXP_SET_NUMBER) {
1577 /* canonize number */
1578 if (asprintf(&str, "%Lf", src->val.num) == -1) {
1579 LOGMEM(src->ctx);
1580 return LY_EMEM;
1581 }
1582 } else {
1583 /* canonize string */
1584 str = strdup(src->val.str);
1585 }
1586
1587 /* ignore errors, the value may not satisfy schema constraints */
Radek Krejci0b013302021-03-29 15:22:32 +02001588 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 +01001589 LYD_HINT_DATA, xp_node->node->schema, &val, NULL, &err);
Michal Vasko4c7763f2020-07-27 17:40:37 +02001590 ly_err_free(err);
1591 if (rc) {
1592 /* invalid value */
Radek IÅ¡a48460222021-03-02 15:18:32 +01001593 /* function store automaticaly dealloc value when fail */
Michal Vasko4c7763f2020-07-27 17:40:37 +02001594 goto fill;
1595 }
1596
Michal Vasko4c7763f2020-07-27 17:40:37 +02001597 /* use the canonized value */
1598 set_init(trg, src);
1599 trg->type = src->type;
1600 if (src->type == LYXP_SET_NUMBER) {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001601 trg->val.num = strtold(type->plugin->print(src->ctx, &val, LY_VALUE_CANON, NULL, NULL, NULL), &ptr);
Michal Vasko4c7763f2020-07-27 17:40:37 +02001602 LY_CHECK_ERR_RET(ptr[0], LOGINT(src->ctx), LY_EINT);
1603 } else {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001604 trg->val.str = strdup(type->plugin->print(src->ctx, &val, LY_VALUE_CANON, NULL, NULL, NULL));
Michal Vasko4c7763f2020-07-27 17:40:37 +02001605 }
1606 type->plugin->free(src->ctx, &val);
1607 return LY_SUCCESS;
1608
1609fill:
1610 /* no canonization needed/possible */
1611 set_fill_set(trg, src);
1612 return LY_SUCCESS;
1613}
1614
Michal Vasko03ff5a72019-09-11 13:49:33 +02001615#ifndef NDEBUG
1616
1617/**
1618 * @brief Bubble sort @p set into XPath document order.
1619 * Context position aware. Unused in the 'Release' build target.
1620 *
1621 * @param[in] set Set to sort.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001622 * @return How many times the whole set was traversed - 1 (if set was sorted, returns 0).
1623 */
1624static int
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001625set_sort(struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001626{
1627 uint32_t i, j;
Radek Krejci1deb5be2020-08-26 16:43:36 +02001628 int ret = 0, cmp;
Radek Krejci857189e2020-09-01 13:26:36 +02001629 ly_bool inverted, change;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001630 const struct lyd_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001631 struct lyxp_set_node item;
1632 struct lyxp_set_hash_node hnode;
1633 uint64_t hash;
1634
Michal Vasko3cf8fbf2020-05-27 15:21:21 +02001635 if ((set->type != LYXP_SET_NODE_SET) || (set->used < 2)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001636 return 0;
1637 }
1638
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001639 /* find first top-level node to be used as anchor for positions */
Michal Vasko9e685082021-01-29 14:49:09 +01001640 for (root = set->cur_node; root->parent; root = lyd_parent(root)) {}
Michal Vaskod989ba02020-08-24 10:59:24 +02001641 for ( ; root->prev->next; root = root->prev) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001642
1643 /* fill positions */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001644 if (set_assign_pos(set, root, set->root_type)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001645 return -1;
1646 }
1647
1648 LOGDBG(LY_LDGXPATH, "SORT BEGIN");
1649 print_set_debug(set);
1650
1651 for (i = 0; i < set->used; ++i) {
1652 inverted = 0;
1653 change = 0;
1654
1655 for (j = 1; j < set->used - i; ++j) {
1656 /* compare node positions */
1657 if (inverted) {
1658 cmp = set_sort_compare(&set->val.nodes[j], &set->val.nodes[j - 1]);
1659 } else {
1660 cmp = set_sort_compare(&set->val.nodes[j - 1], &set->val.nodes[j]);
1661 }
1662
1663 /* swap if needed */
1664 if ((inverted && (cmp < 0)) || (!inverted && (cmp > 0))) {
1665 change = 1;
1666
1667 item = set->val.nodes[j - 1];
1668 set->val.nodes[j - 1] = set->val.nodes[j];
1669 set->val.nodes[j] = item;
1670 } else {
1671 /* whether node_pos1 should be smaller than node_pos2 or the other way around */
1672 inverted = !inverted;
1673 }
1674 }
1675
1676 ++ret;
1677
1678 if (!change) {
1679 break;
1680 }
1681 }
1682
1683 LOGDBG(LY_LDGXPATH, "SORT END %d", ret);
1684 print_set_debug(set);
1685
1686 /* check node hashes */
1687 if (set->used >= LYD_HT_MIN_ITEMS) {
1688 assert(set->ht);
1689 for (i = 0; i < set->used; ++i) {
1690 hnode.node = set->val.nodes[i].node;
1691 hnode.type = set->val.nodes[i].type;
1692
1693 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
1694 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
1695 hash = dict_hash_multi(hash, NULL, 0);
1696
1697 assert(!lyht_find(set->ht, &hnode, hash, NULL));
1698 }
1699 }
1700
1701 return ret - 1;
1702}
1703
Michal Vasko03ff5a72019-09-11 13:49:33 +02001704#endif
1705
1706/**
1707 * @brief Merge 2 sorted sets into one.
1708 *
1709 * @param[in,out] trg Set to merge into. Duplicates are removed.
1710 * @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 +02001711 * @return LY_ERR
1712 */
1713static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001714set_sorted_merge(struct lyxp_set *trg, struct lyxp_set *src)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001715{
1716 uint32_t i, j, k, count, dup_count;
1717 int cmp;
1718 const struct lyd_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001719
Michal Vaskod3678892020-05-21 10:06:58 +02001720 if ((trg->type != LYXP_SET_NODE_SET) || (src->type != LYXP_SET_NODE_SET)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001721 return LY_EINVAL;
1722 }
1723
Michal Vaskod3678892020-05-21 10:06:58 +02001724 if (!src->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001725 return LY_SUCCESS;
Michal Vaskod3678892020-05-21 10:06:58 +02001726 } else if (!trg->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001727 set_fill_set(trg, src);
Michal Vaskod3678892020-05-21 10:06:58 +02001728 lyxp_set_free_content(src);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001729 return LY_SUCCESS;
1730 }
1731
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001732 /* find first top-level node to be used as anchor for positions */
Michal Vasko9e685082021-01-29 14:49:09 +01001733 for (root = trg->cur_node; root->parent; root = lyd_parent(root)) {}
Michal Vaskod989ba02020-08-24 10:59:24 +02001734 for ( ; root->prev->next; root = root->prev) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001735
1736 /* fill positions */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001737 if (set_assign_pos(trg, root, trg->root_type) || set_assign_pos(src, root, src->root_type)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001738 return LY_EINT;
1739 }
1740
1741#ifndef NDEBUG
1742 LOGDBG(LY_LDGXPATH, "MERGE target");
1743 print_set_debug(trg);
1744 LOGDBG(LY_LDGXPATH, "MERGE source");
1745 print_set_debug(src);
1746#endif
1747
1748 /* make memory for the merge (duplicates are not detected yet, so space
1749 * will likely be wasted on them, too bad) */
1750 if (trg->size - trg->used < src->used) {
1751 trg->size = trg->used + src->used;
1752
1753 trg->val.nodes = ly_realloc(trg->val.nodes, trg->size * sizeof *trg->val.nodes);
1754 LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx), LY_EMEM);
1755 }
1756
1757 i = 0;
1758 j = 0;
1759 count = 0;
1760 dup_count = 0;
1761 do {
1762 cmp = set_sort_compare(&src->val.nodes[i], &trg->val.nodes[j]);
1763 if (!cmp) {
1764 if (!count) {
1765 /* duplicate, just skip it */
1766 ++i;
1767 ++j;
1768 } else {
1769 /* we are copying something already, so let's copy the duplicate too,
1770 * we are hoping that afterwards there are some more nodes to
1771 * copy and this way we can copy them all together */
1772 ++count;
1773 ++dup_count;
1774 ++i;
1775 ++j;
1776 }
1777 } else if (cmp < 0) {
1778 /* inserting src node into trg, just remember it for now */
1779 ++count;
1780 ++i;
1781
1782 /* insert the hash now */
1783 set_insert_node_hash(trg, src->val.nodes[i - 1].node, src->val.nodes[i - 1].type);
1784 } else if (count) {
1785copy_nodes:
1786 /* time to actually copy the nodes, we have found the largest block of nodes */
1787 memmove(&trg->val.nodes[j + (count - dup_count)],
1788 &trg->val.nodes[j],
1789 (trg->used - j) * sizeof *trg->val.nodes);
1790 memcpy(&trg->val.nodes[j - dup_count], &src->val.nodes[i - count], count * sizeof *src->val.nodes);
1791
1792 trg->used += count - dup_count;
1793 /* do not change i, except the copying above, we are basically doing exactly what is in the else branch below */
1794 j += count - dup_count;
1795
1796 count = 0;
1797 dup_count = 0;
1798 } else {
1799 ++j;
1800 }
1801 } while ((i < src->used) && (j < trg->used));
1802
1803 if ((i < src->used) || count) {
1804 /* insert all the hashes first */
1805 for (k = i; k < src->used; ++k) {
1806 set_insert_node_hash(trg, src->val.nodes[k].node, src->val.nodes[k].type);
1807 }
1808
1809 /* loop ended, but we need to copy something at trg end */
1810 count += src->used - i;
1811 i = src->used;
1812 goto copy_nodes;
1813 }
1814
1815 /* we are inserting hashes before the actual node insert, which causes
1816 * situations when there were initially not enough items for a hash table,
1817 * but even after some were inserted, hash table was not created (during
1818 * insertion the number of items is not updated yet) */
1819 if (!trg->ht && (trg->used >= LYD_HT_MIN_ITEMS)) {
1820 set_insert_node_hash(trg, NULL, 0);
1821 }
1822
1823#ifndef NDEBUG
1824 LOGDBG(LY_LDGXPATH, "MERGE result");
1825 print_set_debug(trg);
1826#endif
1827
Michal Vaskod3678892020-05-21 10:06:58 +02001828 lyxp_set_free_content(src);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001829 return LY_SUCCESS;
1830}
1831
Michal Vasko14676352020-05-29 11:35:55 +02001832LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02001833lyxp_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 +02001834{
Michal Vasko004d3152020-06-11 19:59:22 +02001835 if (exp->used == tok_idx) {
Michal Vasko14676352020-05-29 11:35:55 +02001836 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001837 LOGVAL(ctx, LY_VCODE_XP_EOF);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001838 }
Michal Vasko14676352020-05-29 11:35:55 +02001839 return LY_EINCOMPLETE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001840 }
1841
Michal Vasko004d3152020-06-11 19:59:22 +02001842 if (want_tok && (exp->tokens[tok_idx] != want_tok)) {
Michal Vasko14676352020-05-29 11:35:55 +02001843 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001844 LOGVAL(ctx, LY_VCODE_XP_INTOK2, lyxp_print_token(exp->tokens[tok_idx]),
Michal Vasko0b468e62020-10-19 09:33:04 +02001845 &exp->expr[exp->tok_pos[tok_idx]], lyxp_print_token(want_tok));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001846 }
Michal Vasko14676352020-05-29 11:35:55 +02001847 return LY_ENOT;
1848 }
1849
1850 return LY_SUCCESS;
1851}
1852
Michal Vasko004d3152020-06-11 19:59:22 +02001853LY_ERR
1854lyxp_next_token(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_token want_tok)
1855{
1856 LY_CHECK_RET(lyxp_check_token(ctx, exp, *tok_idx, want_tok));
1857
1858 /* skip the token */
1859 ++(*tok_idx);
1860
1861 return LY_SUCCESS;
1862}
1863
Michal Vasko14676352020-05-29 11:35:55 +02001864/* just like lyxp_check_token() but tests for 2 tokens */
1865static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02001866exp_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 +02001867 enum lyxp_token want_tok2)
Michal Vasko14676352020-05-29 11:35:55 +02001868{
Michal Vasko004d3152020-06-11 19:59:22 +02001869 if (exp->used == tok_idx) {
Michal Vasko14676352020-05-29 11:35:55 +02001870 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001871 LOGVAL(ctx, LY_VCODE_XP_EOF);
Michal Vasko14676352020-05-29 11:35:55 +02001872 }
1873 return LY_EINCOMPLETE;
1874 }
1875
Michal Vasko004d3152020-06-11 19:59:22 +02001876 if ((exp->tokens[tok_idx] != want_tok1) && (exp->tokens[tok_idx] != want_tok2)) {
Michal Vasko14676352020-05-29 11:35:55 +02001877 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001878 LOGVAL(ctx, LY_VCODE_XP_INTOK, lyxp_print_token(exp->tokens[tok_idx]),
Michal Vasko0b468e62020-10-19 09:33:04 +02001879 &exp->expr[exp->tok_pos[tok_idx]]);
Michal Vasko14676352020-05-29 11:35:55 +02001880 }
1881 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001882 }
1883
1884 return LY_SUCCESS;
1885}
1886
Michal Vasko4911eeb2021-06-28 11:23:05 +02001887LY_ERR
1888lyxp_next_token2(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_token want_tok1,
1889 enum lyxp_token want_tok2)
1890{
1891 LY_CHECK_RET(exp_check_token2(ctx, exp, *tok_idx, want_tok1, want_tok2));
1892
1893 /* skip the token */
1894 ++(*tok_idx);
1895
1896 return LY_SUCCESS;
1897}
1898
Michal Vasko03ff5a72019-09-11 13:49:33 +02001899/**
1900 * @brief Stack operation push on the repeat array.
1901 *
1902 * @param[in] exp Expression to use.
Michal Vasko004d3152020-06-11 19:59:22 +02001903 * @param[in] tok_idx Position in the expresion \p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001904 * @param[in] repeat_op_idx Index from \p exp of the operator token. This value is pushed.
1905 */
1906static void
Michal Vasko004d3152020-06-11 19:59:22 +02001907exp_repeat_push(struct lyxp_expr *exp, uint16_t tok_idx, uint16_t repeat_op_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001908{
1909 uint16_t i;
1910
Michal Vasko004d3152020-06-11 19:59:22 +02001911 if (exp->repeat[tok_idx]) {
Radek Krejci1e008d22020-08-17 11:37:37 +02001912 for (i = 0; exp->repeat[tok_idx][i]; ++i) {}
Michal Vasko004d3152020-06-11 19:59:22 +02001913 exp->repeat[tok_idx] = realloc(exp->repeat[tok_idx], (i + 2) * sizeof *exp->repeat[tok_idx]);
1914 LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), );
1915 exp->repeat[tok_idx][i] = repeat_op_idx;
1916 exp->repeat[tok_idx][i + 1] = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001917 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02001918 exp->repeat[tok_idx] = calloc(2, sizeof *exp->repeat[tok_idx]);
1919 LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), );
1920 exp->repeat[tok_idx][0] = repeat_op_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001921 }
1922}
1923
1924/**
1925 * @brief Reparse Predicate. Logs directly on error.
1926 *
1927 * [7] Predicate ::= '[' Expr ']'
1928 *
1929 * @param[in] ctx Context for logging.
1930 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02001931 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02001932 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001933 * @return LY_ERR
1934 */
1935static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02001936reparse_predicate(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t depth)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001937{
1938 LY_ERR rc;
1939
Michal Vasko004d3152020-06-11 19:59:22 +02001940 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001941 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02001942 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001943
aPiecekbf968d92021-05-27 14:35:05 +02001944 rc = reparse_or_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001945 LY_CHECK_RET(rc);
1946
Michal Vasko004d3152020-06-11 19:59:22 +02001947 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001948 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02001949 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001950
1951 return LY_SUCCESS;
1952}
1953
1954/**
1955 * @brief Reparse RelativeLocationPath. Logs directly on error.
1956 *
1957 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
1958 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
1959 * [6] NodeTest ::= NameTest | NodeType '(' ')'
1960 *
1961 * @param[in] ctx Context for logging.
1962 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02001963 * @param[in] tok_idx Position in the expression \p exp.
aPiecekbf968d92021-05-27 14:35:05 +02001964 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001965 * @return LY_ERR (LY_EINCOMPLETE on forward reference)
1966 */
1967static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02001968reparse_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 +02001969{
1970 LY_ERR rc;
1971
Michal Vasko004d3152020-06-11 19:59:22 +02001972 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001973 LY_CHECK_RET(rc);
1974
1975 goto step;
1976 do {
1977 /* '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02001978 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001979
Michal Vasko004d3152020-06-11 19:59:22 +02001980 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001981 LY_CHECK_RET(rc);
1982step:
1983 /* Step */
Michal Vasko004d3152020-06-11 19:59:22 +02001984 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001985 case LYXP_TOKEN_DOT:
Michal Vasko004d3152020-06-11 19:59:22 +02001986 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001987 break;
1988
1989 case LYXP_TOKEN_DDOT:
Michal Vasko004d3152020-06-11 19:59:22 +02001990 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001991 break;
1992
1993 case LYXP_TOKEN_AT:
Michal Vasko004d3152020-06-11 19:59:22 +02001994 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001995
Michal Vasko004d3152020-06-11 19:59:22 +02001996 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001997 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02001998 if ((exp->tokens[*tok_idx] != LYXP_TOKEN_NAMETEST) && (exp->tokens[*tok_idx] != LYXP_TOKEN_NODETYPE)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001999 LOGVAL(ctx, LY_VCODE_XP_INTOK, lyxp_print_token(exp->tokens[*tok_idx]), &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002000 return LY_EVALID;
2001 }
Radek Krejci0f969882020-08-21 16:56:47 +02002002 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02002003 case LYXP_TOKEN_NAMETEST:
Michal Vasko004d3152020-06-11 19:59:22 +02002004 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002005 goto reparse_predicate;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002006
2007 case LYXP_TOKEN_NODETYPE:
Michal Vasko004d3152020-06-11 19:59:22 +02002008 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002009
2010 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02002011 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002012 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002013 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002014
2015 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02002016 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002017 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002018 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002019
2020reparse_predicate:
2021 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002022 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
aPiecekbf968d92021-05-27 14:35:05 +02002023 rc = reparse_predicate(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002024 LY_CHECK_RET(rc);
2025 }
2026 break;
2027 default:
Radek Krejci2efc45b2020-12-22 16:25:44 +01002028 LOGVAL(ctx, LY_VCODE_XP_INTOK, lyxp_print_token(exp->tokens[*tok_idx]), &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002029 return LY_EVALID;
2030 }
Michal Vasko004d3152020-06-11 19:59:22 +02002031 } while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02002032
2033 return LY_SUCCESS;
2034}
2035
2036/**
2037 * @brief Reparse AbsoluteLocationPath. Logs directly on error.
2038 *
2039 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
2040 *
2041 * @param[in] ctx Context for logging.
2042 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002043 * @param[in] tok_idx Position in the expression \p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002044 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002045 * @return LY_ERR
2046 */
2047static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002048reparse_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 +02002049{
2050 LY_ERR rc;
2051
Michal Vasko004d3152020-06-11 19:59:22 +02002052 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 +02002053
2054 /* '/' RelativeLocationPath? */
Michal Vasko004d3152020-06-11 19:59:22 +02002055 if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002056 /* '/' */
Michal Vasko004d3152020-06-11 19:59:22 +02002057 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002058
Michal Vasko004d3152020-06-11 19:59:22 +02002059 if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002060 return LY_SUCCESS;
2061 }
Michal Vasko004d3152020-06-11 19:59:22 +02002062 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002063 case LYXP_TOKEN_DOT:
2064 case LYXP_TOKEN_DDOT:
2065 case LYXP_TOKEN_AT:
2066 case LYXP_TOKEN_NAMETEST:
2067 case LYXP_TOKEN_NODETYPE:
aPiecekbf968d92021-05-27 14:35:05 +02002068 rc = reparse_relative_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002069 LY_CHECK_RET(rc);
Radek Krejci0f969882020-08-21 16:56:47 +02002070 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02002071 default:
2072 break;
2073 }
2074
Michal Vasko03ff5a72019-09-11 13:49:33 +02002075 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02002076 /* '//' RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002077 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002078
aPiecekbf968d92021-05-27 14:35:05 +02002079 rc = reparse_relative_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002080 LY_CHECK_RET(rc);
2081 }
2082
2083 return LY_SUCCESS;
2084}
2085
2086/**
2087 * @brief Reparse FunctionCall. Logs directly on error.
2088 *
2089 * [9] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
2090 *
2091 * @param[in] ctx Context for logging.
2092 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002093 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002094 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002095 * @return LY_ERR
2096 */
2097static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002098reparse_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 +02002099{
Radek Krejci1deb5be2020-08-26 16:43:36 +02002100 int8_t min_arg_count = -1;
2101 uint32_t arg_count, max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002102 uint16_t func_tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002103 LY_ERR rc;
2104
Michal Vasko004d3152020-06-11 19:59:22 +02002105 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_FUNCNAME);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002106 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002107 func_tok_idx = *tok_idx;
2108 switch (exp->tok_len[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002109 case 3:
Michal Vasko004d3152020-06-11 19:59:22 +02002110 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002111 min_arg_count = 1;
2112 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002113 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002114 min_arg_count = 1;
2115 max_arg_count = 1;
2116 }
2117 break;
2118 case 4:
Michal Vasko004d3152020-06-11 19:59:22 +02002119 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002120 min_arg_count = 1;
2121 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002122 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002123 min_arg_count = 0;
2124 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002125 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002126 min_arg_count = 0;
2127 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002128 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002129 min_arg_count = 0;
2130 max_arg_count = 0;
2131 }
2132 break;
2133 case 5:
Michal Vasko004d3152020-06-11 19:59:22 +02002134 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002135 min_arg_count = 1;
2136 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002137 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002138 min_arg_count = 0;
2139 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002140 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002141 min_arg_count = 1;
2142 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002143 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002144 min_arg_count = 1;
2145 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002146 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002147 min_arg_count = 1;
2148 max_arg_count = 1;
2149 }
2150 break;
2151 case 6:
Michal Vasko004d3152020-06-11 19:59:22 +02002152 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002153 min_arg_count = 2;
Radek Krejci1deb5be2020-08-26 16:43:36 +02002154 max_arg_count = UINT32_MAX;
Michal Vasko004d3152020-06-11 19:59:22 +02002155 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002156 min_arg_count = 0;
2157 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002158 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002159 min_arg_count = 0;
2160 max_arg_count = 1;
2161 }
2162 break;
2163 case 7:
Michal Vasko004d3152020-06-11 19:59:22 +02002164 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002165 min_arg_count = 1;
2166 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002167 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002168 min_arg_count = 1;
2169 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002170 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002171 min_arg_count = 0;
2172 max_arg_count = 0;
2173 }
2174 break;
2175 case 8:
Michal Vasko004d3152020-06-11 19:59:22 +02002176 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002177 min_arg_count = 2;
2178 max_arg_count = 2;
Michal Vasko004d3152020-06-11 19:59:22 +02002179 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002180 min_arg_count = 0;
2181 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002182 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "re-match", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002183 min_arg_count = 2;
2184 max_arg_count = 2;
2185 }
2186 break;
2187 case 9:
Michal Vasko004d3152020-06-11 19:59:22 +02002188 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002189 min_arg_count = 2;
2190 max_arg_count = 3;
Michal Vasko004d3152020-06-11 19:59:22 +02002191 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002192 min_arg_count = 3;
2193 max_arg_count = 3;
2194 }
2195 break;
2196 case 10:
Michal Vasko004d3152020-06-11 19:59:22 +02002197 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002198 min_arg_count = 0;
2199 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002200 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002201 min_arg_count = 1;
2202 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002203 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002204 min_arg_count = 2;
2205 max_arg_count = 2;
2206 }
2207 break;
2208 case 11:
Michal Vasko004d3152020-06-11 19:59:22 +02002209 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002210 min_arg_count = 2;
2211 max_arg_count = 2;
2212 }
2213 break;
2214 case 12:
Michal Vasko004d3152020-06-11 19:59:22 +02002215 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002216 min_arg_count = 2;
2217 max_arg_count = 2;
2218 }
2219 break;
2220 case 13:
Michal Vasko004d3152020-06-11 19:59:22 +02002221 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002222 min_arg_count = 0;
2223 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002224 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string-length", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002225 min_arg_count = 0;
2226 max_arg_count = 1;
2227 }
2228 break;
2229 case 15:
Michal Vasko004d3152020-06-11 19:59:22 +02002230 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002231 min_arg_count = 0;
2232 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002233 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-after", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002234 min_arg_count = 2;
2235 max_arg_count = 2;
2236 }
2237 break;
2238 case 16:
Michal Vasko004d3152020-06-11 19:59:22 +02002239 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002240 min_arg_count = 2;
2241 max_arg_count = 2;
2242 }
2243 break;
2244 case 20:
Michal Vasko004d3152020-06-11 19:59:22 +02002245 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from-or-self", 20)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002246 min_arg_count = 2;
2247 max_arg_count = 2;
2248 }
2249 break;
2250 }
2251 if (min_arg_count == -1) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002252 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 +02002253 return LY_EINVAL;
2254 }
Michal Vasko004d3152020-06-11 19:59:22 +02002255 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002256
2257 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02002258 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002259 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002260 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002261
2262 /* ( Expr ( ',' Expr )* )? */
2263 arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002264 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002265 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002266 if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002267 ++arg_count;
aPiecekbf968d92021-05-27 14:35:05 +02002268 rc = reparse_or_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002269 LY_CHECK_RET(rc);
2270 }
Michal Vasko004d3152020-06-11 19:59:22 +02002271 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) {
2272 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002273
2274 ++arg_count;
aPiecekbf968d92021-05-27 14:35:05 +02002275 rc = reparse_or_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002276 LY_CHECK_RET(rc);
2277 }
2278
2279 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02002280 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002281 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002282 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002283
Radek Krejci857189e2020-09-01 13:26:36 +02002284 if ((arg_count < (uint32_t)min_arg_count) || (arg_count > max_arg_count)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002285 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 +02002286 return LY_EVALID;
2287 }
2288
2289 return LY_SUCCESS;
2290}
2291
2292/**
2293 * @brief Reparse PathExpr. Logs directly on error.
2294 *
2295 * [10] PathExpr ::= LocationPath | PrimaryExpr Predicate*
2296 * | PrimaryExpr Predicate* '/' RelativeLocationPath
2297 * | PrimaryExpr Predicate* '//' RelativeLocationPath
2298 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
aPiecekfba75362021-10-07 12:39:48 +02002299 * [8] PrimaryExpr ::= VariableReference | '(' Expr ')' | Literal | Number | FunctionCall
Michal Vasko03ff5a72019-09-11 13:49:33 +02002300 *
2301 * @param[in] ctx Context for logging.
2302 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002303 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002304 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002305 * @return LY_ERR
2306 */
2307static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002308reparse_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 +02002309{
2310 LY_ERR rc;
2311
Michal Vasko004d3152020-06-11 19:59:22 +02002312 if (lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko14676352020-05-29 11:35:55 +02002313 return LY_EVALID;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002314 }
2315
Michal Vasko004d3152020-06-11 19:59:22 +02002316 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002317 case LYXP_TOKEN_PAR1:
2318 /* '(' Expr ')' Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002319 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002320
aPiecekbf968d92021-05-27 14:35:05 +02002321 rc = reparse_or_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002322 LY_CHECK_RET(rc);
2323
Michal Vasko004d3152020-06-11 19:59:22 +02002324 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002325 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002326 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002327 goto predicate;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002328 case LYXP_TOKEN_DOT:
2329 case LYXP_TOKEN_DDOT:
2330 case LYXP_TOKEN_AT:
2331 case LYXP_TOKEN_NAMETEST:
2332 case LYXP_TOKEN_NODETYPE:
2333 /* RelativeLocationPath */
aPiecekbf968d92021-05-27 14:35:05 +02002334 rc = reparse_relative_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002335 LY_CHECK_RET(rc);
2336 break;
aPiecekfba75362021-10-07 12:39:48 +02002337 case LYXP_TOKEN_VARREF:
2338 /* VariableReference */
2339 ++(*tok_idx);
2340 goto predicate;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002341 case LYXP_TOKEN_FUNCNAME:
2342 /* FunctionCall */
aPiecekbf968d92021-05-27 14:35:05 +02002343 rc = reparse_function_call(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002344 LY_CHECK_RET(rc);
2345 goto predicate;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002346 case LYXP_TOKEN_OPER_PATH:
2347 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +02002348 /* AbsoluteLocationPath */
aPiecekbf968d92021-05-27 14:35:05 +02002349 rc = reparse_absolute_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002350 LY_CHECK_RET(rc);
2351 break;
2352 case LYXP_TOKEN_LITERAL:
2353 /* Literal */
Michal Vasko004d3152020-06-11 19:59:22 +02002354 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002355 goto predicate;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002356 case LYXP_TOKEN_NUMBER:
2357 /* Number */
Michal Vasko004d3152020-06-11 19:59:22 +02002358 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002359 goto predicate;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002360 default:
Radek Krejci2efc45b2020-12-22 16:25:44 +01002361 LOGVAL(ctx, LY_VCODE_XP_INTOK, lyxp_print_token(exp->tokens[*tok_idx]), &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002362 return LY_EVALID;
2363 }
2364
2365 return LY_SUCCESS;
2366
2367predicate:
2368 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002369 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
aPiecekbf968d92021-05-27 14:35:05 +02002370 rc = reparse_predicate(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002371 LY_CHECK_RET(rc);
2372 }
2373
2374 /* ('/' or '//') RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002375 if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002376
2377 /* '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02002378 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002379
aPiecekbf968d92021-05-27 14:35:05 +02002380 rc = reparse_relative_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002381 LY_CHECK_RET(rc);
2382 }
2383
2384 return LY_SUCCESS;
2385}
2386
2387/**
2388 * @brief Reparse UnaryExpr. Logs directly on error.
2389 *
2390 * [17] UnaryExpr ::= UnionExpr | '-' UnaryExpr
2391 * [18] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
2392 *
2393 * @param[in] ctx Context for logging.
2394 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002395 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002396 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002397 * @return LY_ERR
2398 */
2399static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002400reparse_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 +02002401{
2402 uint16_t prev_exp;
2403 LY_ERR rc;
2404
2405 /* ('-')* */
Michal Vasko004d3152020-06-11 19:59:22 +02002406 prev_exp = *tok_idx;
Michal Vasko69730152020-10-09 16:30:07 +02002407 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2408 (exp->expr[exp->tok_pos[*tok_idx]] == '-')) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002409 exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNARY);
Michal Vasko004d3152020-06-11 19:59:22 +02002410 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002411 }
2412
2413 /* PathExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002414 prev_exp = *tok_idx;
aPiecekbf968d92021-05-27 14:35:05 +02002415 rc = reparse_path_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002416 LY_CHECK_RET(rc);
2417
2418 /* ('|' PathExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002419 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_UNI)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002420 exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNION);
Michal Vasko004d3152020-06-11 19:59:22 +02002421 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002422
aPiecekbf968d92021-05-27 14:35:05 +02002423 rc = reparse_path_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002424 LY_CHECK_RET(rc);
2425 }
2426
2427 return LY_SUCCESS;
2428}
2429
2430/**
2431 * @brief Reparse AdditiveExpr. Logs directly on error.
2432 *
2433 * [15] AdditiveExpr ::= MultiplicativeExpr
2434 * | AdditiveExpr '+' MultiplicativeExpr
2435 * | AdditiveExpr '-' MultiplicativeExpr
2436 * [16] MultiplicativeExpr ::= UnaryExpr
2437 * | MultiplicativeExpr '*' UnaryExpr
2438 * | MultiplicativeExpr 'div' UnaryExpr
2439 * | MultiplicativeExpr 'mod' UnaryExpr
2440 *
2441 * @param[in] ctx Context for logging.
2442 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002443 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002444 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002445 * @return LY_ERR
2446 */
2447static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002448reparse_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 +02002449{
2450 uint16_t prev_add_exp, prev_mul_exp;
2451 LY_ERR rc;
2452
Michal Vasko004d3152020-06-11 19:59:22 +02002453 prev_add_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002454 goto reparse_multiplicative_expr;
2455
2456 /* ('+' / '-' MultiplicativeExpr)* */
Michal Vasko69730152020-10-09 16:30:07 +02002457 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2458 ((exp->expr[exp->tok_pos[*tok_idx]] == '+') || (exp->expr[exp->tok_pos[*tok_idx]] == '-'))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002459 exp_repeat_push(exp, prev_add_exp, LYXP_EXPR_ADDITIVE);
Michal Vasko004d3152020-06-11 19:59:22 +02002460 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002461
2462reparse_multiplicative_expr:
2463 /* UnaryExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002464 prev_mul_exp = *tok_idx;
aPiecekbf968d92021-05-27 14:35:05 +02002465 rc = reparse_unary_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002466 LY_CHECK_RET(rc);
2467
2468 /* ('*' / 'div' / 'mod' UnaryExpr)* */
Michal Vasko69730152020-10-09 16:30:07 +02002469 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2470 ((exp->expr[exp->tok_pos[*tok_idx]] == '*') || (exp->tok_len[*tok_idx] == 3))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002471 exp_repeat_push(exp, prev_mul_exp, LYXP_EXPR_MULTIPLICATIVE);
Michal Vasko004d3152020-06-11 19:59:22 +02002472 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002473
aPiecekbf968d92021-05-27 14:35:05 +02002474 rc = reparse_unary_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002475 LY_CHECK_RET(rc);
2476 }
2477 }
2478
2479 return LY_SUCCESS;
2480}
2481
2482/**
2483 * @brief Reparse EqualityExpr. Logs directly on error.
2484 *
2485 * [13] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
2486 * | EqualityExpr '!=' RelationalExpr
2487 * [14] RelationalExpr ::= AdditiveExpr
2488 * | RelationalExpr '<' AdditiveExpr
2489 * | RelationalExpr '>' AdditiveExpr
2490 * | RelationalExpr '<=' AdditiveExpr
2491 * | RelationalExpr '>=' AdditiveExpr
2492 *
2493 * @param[in] ctx Context for logging.
2494 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002495 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002496 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002497 * @return LY_ERR
2498 */
2499static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002500reparse_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 +02002501{
2502 uint16_t prev_eq_exp, prev_rel_exp;
2503 LY_ERR rc;
2504
Michal Vasko004d3152020-06-11 19:59:22 +02002505 prev_eq_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002506 goto reparse_additive_expr;
2507
2508 /* ('=' / '!=' RelationalExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002509 while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_EQUAL, LYXP_TOKEN_OPER_NEQUAL)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002510 exp_repeat_push(exp, prev_eq_exp, LYXP_EXPR_EQUALITY);
Michal Vasko004d3152020-06-11 19:59:22 +02002511 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002512
2513reparse_additive_expr:
2514 /* AdditiveExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002515 prev_rel_exp = *tok_idx;
aPiecekbf968d92021-05-27 14:35:05 +02002516 rc = reparse_additive_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002517 LY_CHECK_RET(rc);
2518
2519 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002520 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_COMP)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002521 exp_repeat_push(exp, prev_rel_exp, LYXP_EXPR_RELATIONAL);
Michal Vasko004d3152020-06-11 19:59:22 +02002522 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002523
aPiecekbf968d92021-05-27 14:35:05 +02002524 rc = reparse_additive_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002525 LY_CHECK_RET(rc);
2526 }
2527 }
2528
2529 return LY_SUCCESS;
2530}
2531
2532/**
2533 * @brief Reparse OrExpr. Logs directly on error.
2534 *
2535 * [11] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
2536 * [12] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
2537 *
2538 * @param[in] ctx Context for logging.
2539 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002540 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002541 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002542 * @return LY_ERR
2543 */
2544static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002545reparse_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 +02002546{
2547 uint16_t prev_or_exp, prev_and_exp;
2548 LY_ERR rc;
2549
aPiecekbf968d92021-05-27 14:35:05 +02002550 ++depth;
2551 LY_CHECK_ERR_RET(depth > LYXP_MAX_BLOCK_DEPTH, LOGVAL(ctx, LY_VCODE_XP_DEPTH), LY_EINVAL);
2552
Michal Vasko004d3152020-06-11 19:59:22 +02002553 prev_or_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002554 goto reparse_equality_expr;
2555
2556 /* ('or' AndExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002557 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 +02002558 exp_repeat_push(exp, prev_or_exp, LYXP_EXPR_OR);
Michal Vasko004d3152020-06-11 19:59:22 +02002559 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002560
2561reparse_equality_expr:
2562 /* EqualityExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002563 prev_and_exp = *tok_idx;
aPiecekbf968d92021-05-27 14:35:05 +02002564 rc = reparse_equality_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002565 LY_CHECK_RET(rc);
2566
2567 /* ('and' EqualityExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002568 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 +02002569 exp_repeat_push(exp, prev_and_exp, LYXP_EXPR_AND);
Michal Vasko004d3152020-06-11 19:59:22 +02002570 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002571
aPiecekbf968d92021-05-27 14:35:05 +02002572 rc = reparse_equality_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002573 LY_CHECK_RET(rc);
2574 }
2575 }
2576
2577 return LY_SUCCESS;
2578}
Radek Krejcib1646a92018-11-02 16:08:26 +01002579
2580/**
2581 * @brief Parse NCName.
2582 *
2583 * @param[in] ncname Name to parse.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002584 * @return Length of @p ncname valid bytes.
Radek Krejcib1646a92018-11-02 16:08:26 +01002585 */
Radek Krejcid4270262019-01-07 15:07:25 +01002586static long int
Radek Krejcib1646a92018-11-02 16:08:26 +01002587parse_ncname(const char *ncname)
2588{
Radek Krejci1deb5be2020-08-26 16:43:36 +02002589 uint32_t uc;
Radek Krejcid4270262019-01-07 15:07:25 +01002590 size_t size;
2591 long int len = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01002592
2593 LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), 0);
2594 if (!is_xmlqnamestartchar(uc) || (uc == ':')) {
2595 return len;
2596 }
2597
2598 do {
2599 len += size;
Radek Krejci9a564c92019-01-07 14:53:57 +01002600 if (!*ncname) {
2601 break;
2602 }
Radek Krejcid4270262019-01-07 15:07:25 +01002603 LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), -len);
Radek Krejcib1646a92018-11-02 16:08:26 +01002604 } while (is_xmlqnamechar(uc) && (uc != ':'));
2605
2606 return len;
2607}
2608
2609/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02002610 * @brief Add @p token into the expression @p exp.
Radek Krejcib1646a92018-11-02 16:08:26 +01002611 *
Michal Vasko03ff5a72019-09-11 13:49:33 +02002612 * @param[in] ctx Context for logging.
Radek Krejcib1646a92018-11-02 16:08:26 +01002613 * @param[in] exp Expression to use.
2614 * @param[in] token Token to add.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002615 * @param[in] tok_pos Token position in the XPath expression.
Radek Krejcib1646a92018-11-02 16:08:26 +01002616 * @param[in] tok_len Token length in the XPath expression.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002617 * @return LY_ERR
Radek Krejcib1646a92018-11-02 16:08:26 +01002618 */
2619static LY_ERR
Michal Vasko14676352020-05-29 11:35:55 +02002620exp_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 +01002621{
2622 uint32_t prev;
2623
2624 if (exp->used == exp->size) {
2625 prev = exp->size;
2626 exp->size += LYXP_EXPR_SIZE_STEP;
2627 if (prev > exp->size) {
2628 LOGINT(ctx);
2629 return LY_EINT;
2630 }
2631
2632 exp->tokens = ly_realloc(exp->tokens, exp->size * sizeof *exp->tokens);
2633 LY_CHECK_ERR_RET(!exp->tokens, LOGMEM(ctx), LY_EMEM);
2634 exp->tok_pos = ly_realloc(exp->tok_pos, exp->size * sizeof *exp->tok_pos);
2635 LY_CHECK_ERR_RET(!exp->tok_pos, LOGMEM(ctx), LY_EMEM);
2636 exp->tok_len = ly_realloc(exp->tok_len, exp->size * sizeof *exp->tok_len);
2637 LY_CHECK_ERR_RET(!exp->tok_len, LOGMEM(ctx), LY_EMEM);
2638 }
2639
2640 exp->tokens[exp->used] = token;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002641 exp->tok_pos[exp->used] = tok_pos;
Radek Krejcib1646a92018-11-02 16:08:26 +01002642 exp->tok_len[exp->used] = tok_len;
2643 ++exp->used;
2644 return LY_SUCCESS;
2645}
2646
2647void
Michal Vasko14676352020-05-29 11:35:55 +02002648lyxp_expr_free(const struct ly_ctx *ctx, struct lyxp_expr *expr)
Radek Krejcib1646a92018-11-02 16:08:26 +01002649{
2650 uint16_t i;
2651
2652 if (!expr) {
2653 return;
2654 }
2655
2656 lydict_remove(ctx, expr->expr);
2657 free(expr->tokens);
2658 free(expr->tok_pos);
2659 free(expr->tok_len);
2660 if (expr->repeat) {
2661 for (i = 0; i < expr->used; ++i) {
2662 free(expr->repeat[i]);
2663 }
2664 }
2665 free(expr->repeat);
2666 free(expr);
2667}
2668
Radek Krejcif03a9e22020-09-18 20:09:31 +02002669LY_ERR
2670lyxp_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 +01002671{
Radek Krejcif03a9e22020-09-18 20:09:31 +02002672 LY_ERR ret = LY_SUCCESS;
2673 struct lyxp_expr *expr;
Radek Krejcid4270262019-01-07 15:07:25 +01002674 size_t parsed = 0, tok_len;
Radek Krejcib1646a92018-11-02 16:08:26 +01002675 enum lyxp_token tok_type;
Radek Krejci857189e2020-09-01 13:26:36 +02002676 ly_bool prev_function_check = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002677 uint16_t tok_idx = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01002678
Radek Krejcif03a9e22020-09-18 20:09:31 +02002679 assert(expr_p);
2680
2681 if (!expr_str[0]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002682 LOGVAL(ctx, LY_VCODE_XP_EOF);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002683 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02002684 }
2685
2686 if (!expr_len) {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002687 expr_len = strlen(expr_str);
Michal Vasko004d3152020-06-11 19:59:22 +02002688 }
2689 if (expr_len > UINT16_MAX) {
Michal Vasko623ac7b2021-04-09 12:44:23 +02002690 LOGVAL(ctx, LYVE_XPATH, "XPath expression cannot be longer than %u characters.", UINT16_MAX);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002691 return LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01002692 }
2693
2694 /* init lyxp_expr structure */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002695 expr = calloc(1, sizeof *expr);
2696 LY_CHECK_ERR_GOTO(!expr, LOGMEM(ctx); ret = LY_EMEM, error);
2697 LY_CHECK_GOTO(ret = lydict_insert(ctx, expr_str, expr_len, &expr->expr), error);
2698 expr->used = 0;
2699 expr->size = LYXP_EXPR_SIZE_START;
2700 expr->tokens = malloc(expr->size * sizeof *expr->tokens);
2701 LY_CHECK_ERR_GOTO(!expr->tokens, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002702
Radek Krejcif03a9e22020-09-18 20:09:31 +02002703 expr->tok_pos = malloc(expr->size * sizeof *expr->tok_pos);
2704 LY_CHECK_ERR_GOTO(!expr->tok_pos, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002705
Radek Krejcif03a9e22020-09-18 20:09:31 +02002706 expr->tok_len = malloc(expr->size * sizeof *expr->tok_len);
2707 LY_CHECK_ERR_GOTO(!expr->tok_len, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002708
Michal Vasko004d3152020-06-11 19:59:22 +02002709 /* make expr 0-terminated */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002710 expr_str = expr->expr;
Michal Vasko004d3152020-06-11 19:59:22 +02002711
Radek Krejcif03a9e22020-09-18 20:09:31 +02002712 while (is_xmlws(expr_str[parsed])) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002713 ++parsed;
2714 }
2715
2716 do {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002717 if (expr_str[parsed] == '(') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002718
2719 /* '(' */
2720 tok_len = 1;
2721 tok_type = LYXP_TOKEN_PAR1;
2722
Radek Krejcif03a9e22020-09-18 20:09:31 +02002723 if (prev_function_check && expr->used && (expr->tokens[expr->used - 1] == LYXP_TOKEN_NAMETEST)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002724 /* it is a NodeType/FunctionName after all */
Michal Vasko69730152020-10-09 16:30:07 +02002725 if (((expr->tok_len[expr->used - 1] == 4) &&
2726 (!strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "node", 4) ||
2727 !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "text", 4))) ||
2728 ((expr->tok_len[expr->used - 1] == 7) &&
2729 !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "comment", 7))) {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002730 expr->tokens[expr->used - 1] = LYXP_TOKEN_NODETYPE;
Radek Krejcib1646a92018-11-02 16:08:26 +01002731 } else {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002732 expr->tokens[expr->used - 1] = LYXP_TOKEN_FUNCNAME;
Radek Krejcib1646a92018-11-02 16:08:26 +01002733 }
2734 prev_function_check = 0;
2735 }
2736
Radek Krejcif03a9e22020-09-18 20:09:31 +02002737 } else if (expr_str[parsed] == ')') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002738
2739 /* ')' */
2740 tok_len = 1;
2741 tok_type = LYXP_TOKEN_PAR2;
2742
Radek Krejcif03a9e22020-09-18 20:09:31 +02002743 } else if (expr_str[parsed] == '[') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002744
2745 /* '[' */
2746 tok_len = 1;
2747 tok_type = LYXP_TOKEN_BRACK1;
2748
Radek Krejcif03a9e22020-09-18 20:09:31 +02002749 } else if (expr_str[parsed] == ']') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002750
2751 /* ']' */
2752 tok_len = 1;
2753 tok_type = LYXP_TOKEN_BRACK2;
2754
Radek Krejcif03a9e22020-09-18 20:09:31 +02002755 } else if (!strncmp(&expr_str[parsed], "..", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002756
2757 /* '..' */
2758 tok_len = 2;
2759 tok_type = LYXP_TOKEN_DDOT;
2760
Radek Krejcif03a9e22020-09-18 20:09:31 +02002761 } else if ((expr_str[parsed] == '.') && (!isdigit(expr_str[parsed + 1]))) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002762
2763 /* '.' */
2764 tok_len = 1;
2765 tok_type = LYXP_TOKEN_DOT;
2766
Radek Krejcif03a9e22020-09-18 20:09:31 +02002767 } else if (expr_str[parsed] == '@') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002768
2769 /* '@' */
2770 tok_len = 1;
2771 tok_type = LYXP_TOKEN_AT;
2772
Radek Krejcif03a9e22020-09-18 20:09:31 +02002773 } else if (expr_str[parsed] == ',') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002774
2775 /* ',' */
2776 tok_len = 1;
2777 tok_type = LYXP_TOKEN_COMMA;
2778
Radek Krejcif03a9e22020-09-18 20:09:31 +02002779 } else if (expr_str[parsed] == '\'') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002780
2781 /* Literal with ' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002782 for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\''); ++tok_len) {}
2783 LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0',
Radek Krejci2efc45b2020-12-22 16:25:44 +01002784 LOGVAL(ctx, LY_VCODE_XP_EOE, expr_str[parsed], &expr_str[parsed]); ret = LY_EVALID,
Michal Vasko69730152020-10-09 16:30:07 +02002785 error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002786 ++tok_len;
2787 tok_type = LYXP_TOKEN_LITERAL;
2788
Radek Krejcif03a9e22020-09-18 20:09:31 +02002789 } else if (expr_str[parsed] == '\"') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002790
2791 /* Literal with " */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002792 for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\"'); ++tok_len) {}
2793 LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0',
Radek Krejci2efc45b2020-12-22 16:25:44 +01002794 LOGVAL(ctx, LY_VCODE_XP_EOE, expr_str[parsed], &expr_str[parsed]); ret = LY_EVALID,
Michal Vasko69730152020-10-09 16:30:07 +02002795 error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002796 ++tok_len;
2797 tok_type = LYXP_TOKEN_LITERAL;
2798
Radek Krejcif03a9e22020-09-18 20:09:31 +02002799 } else if ((expr_str[parsed] == '.') || (isdigit(expr_str[parsed]))) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002800
2801 /* Number */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002802 for (tok_len = 0; isdigit(expr_str[parsed + tok_len]); ++tok_len) {}
2803 if (expr_str[parsed + tok_len] == '.') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002804 ++tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02002805 for ( ; isdigit(expr_str[parsed + tok_len]); ++tok_len) {}
Radek Krejcib1646a92018-11-02 16:08:26 +01002806 }
2807 tok_type = LYXP_TOKEN_NUMBER;
2808
aPiecekfba75362021-10-07 12:39:48 +02002809 } else if (expr_str[parsed] == '$') {
2810
2811 /* VariableReference */
2812 parsed++;
2813 long int ncname_len = parse_ncname(&expr_str[parsed]);
2814 LY_CHECK_ERR_GOTO(ncname_len < 1, LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed - ncname_len],
2815 parsed - ncname_len + 1, expr_str); ret = LY_EVALID, error);
2816 tok_len = ncname_len;
2817 LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == ':',
2818 LOGVAL(ctx, LYVE_XPATH, "Variable with prefix is not supported."); ret = LY_EVALID,
2819 error);
2820 tok_type = LYXP_TOKEN_VARREF;
2821
Radek Krejcif03a9e22020-09-18 20:09:31 +02002822 } else if (expr_str[parsed] == '/') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002823
2824 /* Operator '/', '//' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002825 if (!strncmp(&expr_str[parsed], "//", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002826 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002827 tok_type = LYXP_TOKEN_OPER_RPATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002828 } else {
2829 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002830 tok_type = LYXP_TOKEN_OPER_PATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002831 }
Radek Krejcib1646a92018-11-02 16:08:26 +01002832
Radek Krejcif03a9e22020-09-18 20:09:31 +02002833 } else if (!strncmp(&expr_str[parsed], "!=", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002834
Michal Vasko3e48bf32020-06-01 08:39:07 +02002835 /* Operator '!=' */
Radek Krejcib1646a92018-11-02 16:08:26 +01002836 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002837 tok_type = LYXP_TOKEN_OPER_NEQUAL;
2838
Radek Krejcif03a9e22020-09-18 20:09:31 +02002839 } else if (!strncmp(&expr_str[parsed], "<=", 2) || !strncmp(&expr_str[parsed], ">=", 2)) {
Michal Vasko3e48bf32020-06-01 08:39:07 +02002840
2841 /* Operator '<=', '>=' */
2842 tok_len = 2;
2843 tok_type = LYXP_TOKEN_OPER_COMP;
Radek Krejcib1646a92018-11-02 16:08:26 +01002844
Radek Krejcif03a9e22020-09-18 20:09:31 +02002845 } else if (expr_str[parsed] == '|') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002846
2847 /* Operator '|' */
2848 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002849 tok_type = LYXP_TOKEN_OPER_UNI;
Radek Krejcib1646a92018-11-02 16:08:26 +01002850
Radek Krejcif03a9e22020-09-18 20:09:31 +02002851 } else if ((expr_str[parsed] == '+') || (expr_str[parsed] == '-')) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002852
2853 /* Operator '+', '-' */
2854 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002855 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002856
Radek Krejcif03a9e22020-09-18 20:09:31 +02002857 } else if (expr_str[parsed] == '=') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002858
Michal Vasko3e48bf32020-06-01 08:39:07 +02002859 /* Operator '=' */
Radek Krejcib1646a92018-11-02 16:08:26 +01002860 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002861 tok_type = LYXP_TOKEN_OPER_EQUAL;
2862
Radek Krejcif03a9e22020-09-18 20:09:31 +02002863 } else if ((expr_str[parsed] == '<') || (expr_str[parsed] == '>')) {
Michal Vasko3e48bf32020-06-01 08:39:07 +02002864
2865 /* Operator '<', '>' */
2866 tok_len = 1;
2867 tok_type = LYXP_TOKEN_OPER_COMP;
Radek Krejcib1646a92018-11-02 16:08:26 +01002868
Michal Vasko69730152020-10-09 16:30:07 +02002869 } else if (expr->used && (expr->tokens[expr->used - 1] != LYXP_TOKEN_AT) &&
2870 (expr->tokens[expr->used - 1] != LYXP_TOKEN_PAR1) &&
2871 (expr->tokens[expr->used - 1] != LYXP_TOKEN_BRACK1) &&
2872 (expr->tokens[expr->used - 1] != LYXP_TOKEN_COMMA) &&
2873 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_LOG) &&
2874 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_EQUAL) &&
2875 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_NEQUAL) &&
2876 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_COMP) &&
2877 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_MATH) &&
2878 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_UNI) &&
2879 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_PATH) &&
2880 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_RPATH)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002881
2882 /* Operator '*', 'or', 'and', 'mod', or 'div' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002883 if (expr_str[parsed] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002884 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002885 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002886
Radek Krejcif03a9e22020-09-18 20:09:31 +02002887 } else if (!strncmp(&expr_str[parsed], "or", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002888 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002889 tok_type = LYXP_TOKEN_OPER_LOG;
Radek Krejcib1646a92018-11-02 16:08:26 +01002890
Radek Krejcif03a9e22020-09-18 20:09:31 +02002891 } else if (!strncmp(&expr_str[parsed], "and", 3)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002892 tok_len = 3;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002893 tok_type = LYXP_TOKEN_OPER_LOG;
Radek Krejcib1646a92018-11-02 16:08:26 +01002894
Radek Krejcif03a9e22020-09-18 20:09:31 +02002895 } else if (!strncmp(&expr_str[parsed], "mod", 3) || !strncmp(&expr_str[parsed], "div", 3)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002896 tok_len = 3;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002897 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002898
2899 } else if (prev_function_check) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002900 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 +02002901 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 +02002902 ret = LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01002903 goto error;
2904 } else {
Michal Vasko774ce402021-04-14 15:35:06 +02002905 LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed], parsed + 1, expr_str);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002906 ret = LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01002907 goto error;
2908 }
Radek Krejcif03a9e22020-09-18 20:09:31 +02002909 } else if (expr_str[parsed] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002910
2911 /* NameTest '*' */
2912 tok_len = 1;
2913 tok_type = LYXP_TOKEN_NAMETEST;
2914
2915 } else {
2916
2917 /* NameTest (NCName ':' '*' | QName) or NodeType/FunctionName */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002918 long int ncname_len = parse_ncname(&expr_str[parsed]);
Michal Vaskoe2be5462021-08-04 10:49:42 +02002919 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 +02002920 parsed - ncname_len + 1, expr_str); ret = LY_EVALID, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002921 tok_len = ncname_len;
2922
Radek Krejcif03a9e22020-09-18 20:09:31 +02002923 if (expr_str[parsed + tok_len] == ':') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002924 ++tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02002925 if (expr_str[parsed + tok_len] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002926 ++tok_len;
2927 } else {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002928 ncname_len = parse_ncname(&expr_str[parsed + tok_len]);
Michal Vaskoe2be5462021-08-04 10:49:42 +02002929 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 +02002930 parsed - ncname_len + 1, expr_str); ret = LY_EVALID, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002931 tok_len += ncname_len;
2932 }
2933 /* remove old flag to prevent ambiguities */
2934 prev_function_check = 0;
2935 tok_type = LYXP_TOKEN_NAMETEST;
2936 } else {
2937 /* there is no prefix so it can still be NodeType/FunctionName, we can't finally decide now */
2938 prev_function_check = 1;
2939 tok_type = LYXP_TOKEN_NAMETEST;
2940 }
2941 }
2942
2943 /* store the token, move on to the next one */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002944 LY_CHECK_GOTO(ret = exp_add_token(ctx, expr, tok_type, parsed, tok_len), error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002945 parsed += tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02002946 while (is_xmlws(expr_str[parsed])) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002947 ++parsed;
2948 }
2949
Radek Krejcif03a9e22020-09-18 20:09:31 +02002950 } while (expr_str[parsed]);
Radek Krejcib1646a92018-11-02 16:08:26 +01002951
Michal Vasko004d3152020-06-11 19:59:22 +02002952 if (reparse) {
2953 /* prealloc repeat */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002954 expr->repeat = calloc(expr->size, sizeof *expr->repeat);
2955 LY_CHECK_ERR_GOTO(!expr->repeat, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002956
Michal Vasko004d3152020-06-11 19:59:22 +02002957 /* fill repeat */
aPiecekbf968d92021-05-27 14:35:05 +02002958 LY_CHECK_ERR_GOTO(reparse_or_expr(ctx, expr, &tok_idx, 0), ret = LY_EVALID, error);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002959 if (expr->used > tok_idx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002960 LOGVAL(ctx, LYVE_XPATH, "Unparsed characters \"%s\" left at the end of an XPath expression.",
Michal Vasko69730152020-10-09 16:30:07 +02002961 &expr->expr[expr->tok_pos[tok_idx]]);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002962 ret = LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02002963 goto error;
2964 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02002965 }
2966
Radek Krejcif03a9e22020-09-18 20:09:31 +02002967 print_expr_struct_debug(expr);
2968 *expr_p = expr;
2969 return LY_SUCCESS;
Radek Krejcib1646a92018-11-02 16:08:26 +01002970
2971error:
Radek Krejcif03a9e22020-09-18 20:09:31 +02002972 lyxp_expr_free(ctx, expr);
2973 return ret;
Radek Krejcib1646a92018-11-02 16:08:26 +01002974}
2975
Michal Vasko1734be92020-09-22 08:55:10 +02002976LY_ERR
2977lyxp_expr_dup(const struct ly_ctx *ctx, const struct lyxp_expr *exp, struct lyxp_expr **dup_p)
Michal Vasko004d3152020-06-11 19:59:22 +02002978{
Michal Vasko1734be92020-09-22 08:55:10 +02002979 LY_ERR ret = LY_SUCCESS;
2980 struct lyxp_expr *dup = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02002981 uint32_t i, j;
2982
Michal Vasko7f45cf22020-10-01 12:49:44 +02002983 if (!exp) {
2984 goto cleanup;
2985 }
2986
Michal Vasko004d3152020-06-11 19:59:22 +02002987 dup = calloc(1, sizeof *dup);
Michal Vasko1734be92020-09-22 08:55:10 +02002988 LY_CHECK_ERR_GOTO(!dup, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02002989
Michal Vasko08e9b112021-06-11 15:41:17 +02002990 if (exp->used) {
2991 dup->tokens = malloc(exp->used * sizeof *dup->tokens);
2992 LY_CHECK_ERR_GOTO(!dup->tokens, LOGMEM(ctx); ret = LY_EMEM, cleanup);
2993 memcpy(dup->tokens, exp->tokens, exp->used * sizeof *dup->tokens);
Michal Vasko004d3152020-06-11 19:59:22 +02002994
Michal Vasko08e9b112021-06-11 15:41:17 +02002995 dup->tok_pos = malloc(exp->used * sizeof *dup->tok_pos);
2996 LY_CHECK_ERR_GOTO(!dup->tok_pos, LOGMEM(ctx); ret = LY_EMEM, cleanup);
2997 memcpy(dup->tok_pos, exp->tok_pos, exp->used * sizeof *dup->tok_pos);
Michal Vasko004d3152020-06-11 19:59:22 +02002998
Michal Vasko08e9b112021-06-11 15:41:17 +02002999 dup->tok_len = malloc(exp->used * sizeof *dup->tok_len);
3000 LY_CHECK_ERR_GOTO(!dup->tok_len, LOGMEM(ctx); ret = LY_EMEM, cleanup);
3001 memcpy(dup->tok_len, exp->tok_len, exp->used * sizeof *dup->tok_len);
Michal Vasko004d3152020-06-11 19:59:22 +02003002
Michal Vasko08e9b112021-06-11 15:41:17 +02003003 dup->repeat = malloc(exp->used * sizeof *dup->repeat);
3004 LY_CHECK_ERR_GOTO(!dup->repeat, LOGMEM(ctx); ret = LY_EMEM, cleanup);
3005 for (i = 0; i < exp->used; ++i) {
3006 if (!exp->repeat[i]) {
3007 dup->repeat[i] = NULL;
3008 } else {
3009 for (j = 0; exp->repeat[i][j]; ++j) {}
3010 /* the ending 0 as well */
3011 ++j;
Michal Vasko004d3152020-06-11 19:59:22 +02003012
Michal Vasko08e9b112021-06-11 15:41:17 +02003013 dup->repeat[i] = malloc(j * sizeof **dup->repeat);
3014 LY_CHECK_ERR_GOTO(!dup->repeat[i], LOGMEM(ctx); ret = LY_EMEM, cleanup);
3015 memcpy(dup->repeat[i], exp->repeat[i], j * sizeof **dup->repeat);
3016 dup->repeat[i][j - 1] = 0;
3017 }
Michal Vasko004d3152020-06-11 19:59:22 +02003018 }
3019 }
3020
3021 dup->used = exp->used;
3022 dup->size = exp->used;
Michal Vasko1734be92020-09-22 08:55:10 +02003023 LY_CHECK_GOTO(ret = lydict_insert(ctx, exp->expr, 0, &dup->expr), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02003024
Michal Vasko1734be92020-09-22 08:55:10 +02003025cleanup:
3026 if (ret) {
3027 lyxp_expr_free(ctx, dup);
3028 } else {
3029 *dup_p = dup;
3030 }
3031 return ret;
Michal Vasko004d3152020-06-11 19:59:22 +02003032}
3033
Michal Vasko03ff5a72019-09-11 13:49:33 +02003034/**
3035 * @brief Get the last-added schema node that is currently in the context.
3036 *
3037 * @param[in] set Set to search in.
3038 * @return Last-added schema context node, NULL if no node is in context.
3039 */
3040static struct lysc_node *
3041warn_get_scnode_in_ctx(struct lyxp_set *set)
3042{
3043 uint32_t i;
3044
3045 if (!set || (set->type != LYXP_SET_SCNODE_SET)) {
3046 return NULL;
3047 }
3048
3049 i = set->used;
3050 do {
3051 --i;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003052 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003053 /* if there are more, simply return the first found (last added) */
3054 return set->val.scnodes[i].scnode;
3055 }
3056 } while (i);
3057
3058 return NULL;
3059}
3060
3061/**
3062 * @brief Test whether a type is numeric - integer type or decimal64.
3063 *
3064 * @param[in] type Type to test.
Radek Krejci857189e2020-09-01 13:26:36 +02003065 * @return Boolean value whether @p type is numeric type or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003066 */
Radek Krejci857189e2020-09-01 13:26:36 +02003067static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003068warn_is_numeric_type(struct lysc_type *type)
3069{
3070 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003071 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003072 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003073
3074 switch (type->basetype) {
3075 case LY_TYPE_DEC64:
3076 case LY_TYPE_INT8:
3077 case LY_TYPE_UINT8:
3078 case LY_TYPE_INT16:
3079 case LY_TYPE_UINT16:
3080 case LY_TYPE_INT32:
3081 case LY_TYPE_UINT32:
3082 case LY_TYPE_INT64:
3083 case LY_TYPE_UINT64:
3084 return 1;
3085 case LY_TYPE_UNION:
3086 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003087 LY_ARRAY_FOR(uni->types, u) {
3088 ret = warn_is_numeric_type(uni->types[u]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003089 if (ret) {
3090 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003091 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003092 }
3093 }
3094 /* did not find any suitable type */
3095 return 0;
3096 case LY_TYPE_LEAFREF:
3097 return warn_is_numeric_type(((struct lysc_type_leafref *)type)->realtype);
3098 default:
3099 return 0;
3100 }
3101}
3102
3103/**
3104 * @brief Test whether a type is string-like - no integers, decimal64 or binary.
3105 *
3106 * @param[in] type Type to test.
Radek Krejci857189e2020-09-01 13:26:36 +02003107 * @return Boolean value whether @p type's basetype is string type or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003108 */
Radek Krejci857189e2020-09-01 13:26:36 +02003109static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003110warn_is_string_type(struct lysc_type *type)
3111{
3112 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003113 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003114 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003115
3116 switch (type->basetype) {
3117 case LY_TYPE_BITS:
3118 case LY_TYPE_ENUM:
3119 case LY_TYPE_IDENT:
3120 case LY_TYPE_INST:
3121 case LY_TYPE_STRING:
3122 return 1;
3123 case LY_TYPE_UNION:
3124 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003125 LY_ARRAY_FOR(uni->types, u) {
3126 ret = warn_is_string_type(uni->types[u]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003127 if (ret) {
3128 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003129 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003130 }
3131 }
3132 /* did not find any suitable type */
3133 return 0;
3134 case LY_TYPE_LEAFREF:
3135 return warn_is_string_type(((struct lysc_type_leafref *)type)->realtype);
3136 default:
3137 return 0;
3138 }
3139}
3140
3141/**
3142 * @brief Test whether a type is one specific type.
3143 *
3144 * @param[in] type Type to test.
3145 * @param[in] base Expected type.
Radek Krejci857189e2020-09-01 13:26:36 +02003146 * @return Boolean value whether the given @p type is of the specific basetype @p base.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003147 */
Radek Krejci857189e2020-09-01 13:26:36 +02003148static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003149warn_is_specific_type(struct lysc_type *type, LY_DATA_TYPE base)
3150{
3151 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003152 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003153 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003154
3155 if (type->basetype == base) {
3156 return 1;
3157 } else if (type->basetype == LY_TYPE_UNION) {
3158 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003159 LY_ARRAY_FOR(uni->types, u) {
3160 ret = warn_is_specific_type(uni->types[u], base);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003161 if (ret) {
3162 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003163 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003164 }
3165 }
3166 /* did not find any suitable type */
3167 return 0;
3168 } else if (type->basetype == LY_TYPE_LEAFREF) {
3169 return warn_is_specific_type(((struct lysc_type_leafref *)type)->realtype, base);
3170 }
3171
3172 return 0;
3173}
3174
3175/**
3176 * @brief Get next type of a (union) type.
3177 *
3178 * @param[in] type Base type.
3179 * @param[in] prev_type Previously returned type.
3180 * @return Next type or NULL.
3181 */
3182static struct lysc_type *
3183warn_is_equal_type_next_type(struct lysc_type *type, struct lysc_type *prev_type)
3184{
3185 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003186 ly_bool found = 0;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003187 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003188
3189 switch (type->basetype) {
3190 case LY_TYPE_UNION:
3191 uni = (struct lysc_type_union *)type;
3192 if (!prev_type) {
3193 return uni->types[0];
3194 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003195 LY_ARRAY_FOR(uni->types, u) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003196 if (found) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003197 return uni->types[u];
Michal Vasko03ff5a72019-09-11 13:49:33 +02003198 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003199 if (prev_type == uni->types[u]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003200 found = 1;
3201 }
3202 }
3203 return NULL;
3204 default:
3205 if (prev_type) {
3206 assert(type == prev_type);
3207 return NULL;
3208 } else {
3209 return type;
3210 }
3211 }
3212}
3213
3214/**
3215 * @brief Test whether 2 types have a common type.
3216 *
3217 * @param[in] type1 First type.
3218 * @param[in] type2 Second type.
3219 * @return 1 if they do, 0 otherwise.
3220 */
3221static int
3222warn_is_equal_type(struct lysc_type *type1, struct lysc_type *type2)
3223{
3224 struct lysc_type *t1, *rt1, *t2, *rt2;
3225
3226 t1 = NULL;
3227 while ((t1 = warn_is_equal_type_next_type(type1, t1))) {
3228 if (t1->basetype == LY_TYPE_LEAFREF) {
3229 rt1 = ((struct lysc_type_leafref *)t1)->realtype;
3230 } else {
3231 rt1 = t1;
3232 }
3233
3234 t2 = NULL;
3235 while ((t2 = warn_is_equal_type_next_type(type2, t2))) {
3236 if (t2->basetype == LY_TYPE_LEAFREF) {
3237 rt2 = ((struct lysc_type_leafref *)t2)->realtype;
3238 } else {
3239 rt2 = t2;
3240 }
3241
3242 if (rt2->basetype == rt1->basetype) {
3243 /* match found */
3244 return 1;
3245 }
3246 }
3247 }
3248
3249 return 0;
3250}
3251
3252/**
Michal Vaskoaa956522021-11-11 10:45:34 +01003253 * @brief Print warning with information about the XPath subexpression that caused previous warning.
3254 *
3255 * @param[in] ctx Context for logging.
3256 * @param[in] tok_pos Index of the subexpression in the whole expression.
3257 * @param[in] subexpr Subexpression start.
3258 * @param[in] subexpr_len Length of @p subexpr to print.
3259 * @param[in] cur_scnode Expression context node.
3260 */
3261static void
3262warn_subexpr_log(const struct ly_ctx *ctx, uint16_t tok_pos, const char *subexpr, int subexpr_len,
3263 const struct lysc_node *cur_scnode)
3264{
3265 char *path;
3266
3267 path = lysc_path(cur_scnode, LYSC_PATH_LOG, NULL, 0);
3268 LOGWRN(ctx, "Previous warning generated by XPath subexpression[%" PRIu16 "] \"%.*s\" with context node \"%s\".",
3269 tok_pos, subexpr_len, subexpr, path);
3270 free(path);
3271}
3272
3273/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02003274 * @brief Check both operands of comparison operators.
3275 *
3276 * @param[in] ctx Context for errors.
3277 * @param[in] set1 First operand set.
3278 * @param[in] set2 Second operand set.
3279 * @param[in] numbers_only Whether accept only numbers or other types are fine too (for '=' and '!=').
3280 * @param[in] expr Start of the expression to print with the warning.
3281 * @param[in] tok_pos Token position.
3282 */
3283static void
Michal Vaskoaa956522021-11-11 10:45:34 +01003284warn_operands(struct ly_ctx *ctx, struct lyxp_set *set1, struct lyxp_set *set2, ly_bool numbers_only, const char *expr,
3285 uint16_t tok_pos)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003286{
3287 struct lysc_node_leaf *node1, *node2;
Radek Krejci857189e2020-09-01 13:26:36 +02003288 ly_bool leaves = 1, warning = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003289
3290 node1 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set1);
3291 node2 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set2);
3292
3293 if (!node1 && !node2) {
3294 /* no node-sets involved, nothing to do */
3295 return;
3296 }
3297
3298 if (node1) {
3299 if (!(node1->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3300 LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node1->nodetype), node1->name);
3301 warning = 1;
3302 leaves = 0;
3303 } else if (numbers_only && !warn_is_numeric_type(node1->type)) {
3304 LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node1->name);
3305 warning = 1;
3306 }
3307 }
3308
3309 if (node2) {
3310 if (!(node2->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3311 LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node2->nodetype), node2->name);
3312 warning = 1;
3313 leaves = 0;
3314 } else if (numbers_only && !warn_is_numeric_type(node2->type)) {
3315 LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node2->name);
3316 warning = 1;
3317 }
3318 }
3319
3320 if (node1 && node2 && leaves && !numbers_only) {
Michal Vasko69730152020-10-09 16:30:07 +02003321 if ((warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type)) ||
3322 (!warn_is_numeric_type(node1->type) && warn_is_numeric_type(node2->type)) ||
3323 (!warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type) &&
3324 !warn_is_equal_type(node1->type, node2->type))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003325 LOGWRN(ctx, "Incompatible types of operands \"%s\" and \"%s\" for comparison.", node1->name, node2->name);
3326 warning = 1;
3327 }
3328 }
3329
3330 if (warning) {
Michal Vaskoaa956522021-11-11 10:45:34 +01003331 warn_subexpr_log(ctx, tok_pos, expr + tok_pos, 20, set1->cur_scnode);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003332 }
3333}
3334
3335/**
3336 * @brief Check that a value is valid for a leaf. If not applicable, does nothing.
3337 *
3338 * @param[in] exp Parsed XPath expression.
3339 * @param[in] set Set with the leaf/leaf-list.
3340 * @param[in] val_exp Index of the value (literal/number) in @p exp.
3341 * @param[in] equal_exp Index of the start of the equality expression in @p exp.
3342 * @param[in] last_equal_exp Index of the end of the equality expression in @p exp.
3343 */
3344static void
Michal Vasko40308e72020-10-20 16:38:40 +02003345warn_equality_value(const struct lyxp_expr *exp, struct lyxp_set *set, uint16_t val_exp, uint16_t equal_exp,
3346 uint16_t last_equal_exp)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003347{
3348 struct lysc_node *scnode;
3349 struct lysc_type *type;
3350 char *value;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003351 struct lyd_value storage;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003352 LY_ERR rc;
3353 struct ly_err_item *err = NULL;
3354
Michal Vasko69730152020-10-09 16:30:07 +02003355 if ((scnode = warn_get_scnode_in_ctx(set)) && (scnode->nodetype & (LYS_LEAF | LYS_LEAFLIST)) &&
3356 ((exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) || (exp->tokens[val_exp] == LYXP_TOKEN_NUMBER))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003357 /* check that the node can have the specified value */
3358 if (exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) {
3359 value = strndup(exp->expr + exp->tok_pos[val_exp] + 1, exp->tok_len[val_exp] - 2);
3360 } else {
3361 value = strndup(exp->expr + exp->tok_pos[val_exp], exp->tok_len[val_exp]);
3362 }
3363 if (!value) {
3364 LOGMEM(set->ctx);
3365 return;
3366 }
3367
3368 if ((((struct lysc_node_leaf *)scnode)->type->basetype == LY_TYPE_IDENT) && !strchr(value, ':')) {
3369 LOGWRN(set->ctx, "Identityref \"%s\" comparison with identity \"%s\" without prefix, consider adding"
Michal Vasko69730152020-10-09 16:30:07 +02003370 " a prefix or best using \"derived-from(-or-self)()\" functions.", scnode->name, value);
Michal Vaskoaa956522021-11-11 10:45:34 +01003371 warn_subexpr_log(set->ctx, exp->tok_pos[equal_exp], exp->expr + exp->tok_pos[equal_exp],
Radek Krejci0f969882020-08-21 16:56:47 +02003372 (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp],
Michal Vaskoaa956522021-11-11 10:45:34 +01003373 set->cur_scnode);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003374 }
3375
3376 type = ((struct lysc_node_leaf *)scnode)->type;
3377 if (type->basetype != LY_TYPE_IDENT) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003378 rc = type->plugin->store(set->ctx, type, value, strlen(value), 0, set->format, set->prefix_data,
Michal Vasko405cc9e2020-12-01 12:01:27 +01003379 LYD_HINT_DATA, scnode, &storage, NULL, &err);
Michal Vaskobf42e832020-11-23 16:59:42 +01003380 if (rc == LY_EINCOMPLETE) {
3381 rc = LY_SUCCESS;
3382 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003383
3384 if (err) {
3385 LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type (%s).", value, err->msg);
3386 ly_err_free(err);
3387 } else if (rc != LY_SUCCESS) {
3388 LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type.", value);
3389 }
3390 if (rc != LY_SUCCESS) {
Michal Vaskoaa956522021-11-11 10:45:34 +01003391 warn_subexpr_log(set->ctx, exp->tok_pos[equal_exp], exp->expr + exp->tok_pos[equal_exp],
Radek Krejci0f969882020-08-21 16:56:47 +02003392 (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp],
Michal Vaskoaa956522021-11-11 10:45:34 +01003393 set->cur_scnode);
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003394 } else {
3395 type->plugin->free(set->ctx, &storage);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003396 }
3397 }
3398 free(value);
3399 }
3400}
3401
3402/*
3403 * XPath functions
3404 */
3405
3406/**
3407 * @brief Execute the YANG 1.1 bit-is-set(node-set, string) function. Returns LYXP_SET_BOOLEAN
3408 * depending on whether the first node bit value from the second argument is set.
3409 *
3410 * @param[in] args Array of arguments.
3411 * @param[in] arg_count Count of elements in @p args.
3412 * @param[in,out] set Context and result set at the same time.
3413 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003414 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003415 */
3416static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003417xpath_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 +02003418{
3419 struct lyd_node_term *leaf;
3420 struct lysc_node_leaf *sleaf;
Michal Vasko2588b952021-07-29 07:43:26 +02003421 struct lyd_value_bits *bits;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003422 LY_ERR rc = LY_SUCCESS;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003423 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003424
3425 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003426 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003427 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003428 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3429 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3430 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3431 sleaf->name);
3432 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_BITS)) {
3433 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"bits\".", __func__, sleaf->name);
3434 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003435 }
3436
3437 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3438 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003439 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3440 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003441 } else if (!warn_is_string_type(sleaf->type)) {
3442 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003443 }
3444 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003445 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003446 return rc;
3447 }
3448
Michal Vaskod3678892020-05-21 10:06:58 +02003449 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003450 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 +02003451 return LY_EVALID;
3452 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003453 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003454 LY_CHECK_RET(rc);
3455
3456 set_fill_boolean(set, 0);
Michal Vaskod3678892020-05-21 10:06:58 +02003457 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003458 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
Michal Vasko2588b952021-07-29 07:43:26 +02003459 if ((leaf->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (leaf->value.realtype->basetype == LY_TYPE_BITS)) {
3460 LYD_VALUE_GET(&leaf->value, bits);
3461 LY_ARRAY_FOR(bits->items, u) {
3462 if (!strcmp(bits->items[u]->name, args[1]->val.str)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003463 set_fill_boolean(set, 1);
3464 break;
3465 }
3466 }
3467 }
3468 }
3469
3470 return LY_SUCCESS;
3471}
3472
3473/**
3474 * @brief Execute the XPath boolean(object) function. Returns LYXP_SET_BOOLEAN
3475 * with the argument converted to boolean.
3476 *
3477 * @param[in] args Array of arguments.
3478 * @param[in] arg_count Count of elements in @p args.
3479 * @param[in,out] set Context and result set at the same time.
3480 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003481 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003482 */
3483static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003484xpath_boolean(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003485{
3486 LY_ERR rc;
3487
3488 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02003489 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003490 return LY_SUCCESS;
3491 }
3492
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003493 rc = lyxp_set_cast(args[0], LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003494 LY_CHECK_RET(rc);
3495 set_fill_set(set, args[0]);
3496
3497 return LY_SUCCESS;
3498}
3499
3500/**
3501 * @brief Execute the XPath ceiling(number) function. Returns LYXP_SET_NUMBER
3502 * with the first argument rounded up to the nearest integer.
3503 *
3504 * @param[in] args Array of arguments.
3505 * @param[in] arg_count Count of elements in @p args.
3506 * @param[in,out] set Context and result set at the same time.
3507 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003508 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003509 */
3510static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003511xpath_ceiling(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003512{
3513 struct lysc_node_leaf *sleaf;
3514 LY_ERR rc = LY_SUCCESS;
3515
3516 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003517 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003518 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003519 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3520 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3521 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3522 sleaf->name);
3523 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
3524 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
3525 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003526 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003527 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003528 return rc;
3529 }
3530
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003531 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003532 LY_CHECK_RET(rc);
3533 if ((long long)args[0]->val.num != args[0]->val.num) {
3534 set_fill_number(set, ((long long)args[0]->val.num) + 1);
3535 } else {
3536 set_fill_number(set, args[0]->val.num);
3537 }
3538
3539 return LY_SUCCESS;
3540}
3541
3542/**
3543 * @brief Execute the XPath concat(string, string, string*) function.
3544 * Returns LYXP_SET_STRING with the concatenation of all the arguments.
3545 *
3546 * @param[in] args Array of arguments.
3547 * @param[in] arg_count Count of elements in @p args.
3548 * @param[in,out] set Context and result set at the same time.
3549 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003550 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003551 */
3552static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003553xpath_concat(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003554{
3555 uint16_t i;
3556 char *str = NULL;
3557 size_t used = 1;
3558 LY_ERR rc = LY_SUCCESS;
3559 struct lysc_node_leaf *sleaf;
3560
3561 if (options & LYXP_SCNODE_ALL) {
3562 for (i = 0; i < arg_count; ++i) {
3563 if ((args[i]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[i]))) {
3564 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3565 LOGWRN(set->ctx, "Argument #%u of %s is a %s node \"%s\".",
Michal Vasko69730152020-10-09 16:30:07 +02003566 i + 1, __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003567 } else if (!warn_is_string_type(sleaf->type)) {
Radek Krejci70124c82020-08-14 22:17:03 +02003568 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 +02003569 }
3570 }
3571 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003572 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003573 return rc;
3574 }
3575
3576 for (i = 0; i < arg_count; ++i) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003577 rc = lyxp_set_cast(args[i], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003578 if (rc != LY_SUCCESS) {
3579 free(str);
3580 return rc;
3581 }
3582
3583 str = ly_realloc(str, (used + strlen(args[i]->val.str)) * sizeof(char));
3584 LY_CHECK_ERR_RET(!str, LOGMEM(set->ctx), LY_EMEM);
3585 strcpy(str + used - 1, args[i]->val.str);
3586 used += strlen(args[i]->val.str);
3587 }
3588
3589 /* free, kind of */
Michal Vaskod3678892020-05-21 10:06:58 +02003590 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003591 set->type = LYXP_SET_STRING;
3592 set->val.str = str;
3593
3594 return LY_SUCCESS;
3595}
3596
3597/**
3598 * @brief Execute the XPath contains(string, string) function.
3599 * Returns LYXP_SET_BOOLEAN whether the second argument can
3600 * be found in the first or not.
3601 *
3602 * @param[in] args Array of arguments.
3603 * @param[in] arg_count Count of elements in @p args.
3604 * @param[in,out] set Context and result set at the same time.
3605 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003606 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003607 */
3608static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003609xpath_contains(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003610{
3611 struct lysc_node_leaf *sleaf;
3612 LY_ERR rc = LY_SUCCESS;
3613
3614 if (options & LYXP_SCNODE_ALL) {
3615 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3616 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003617 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3618 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003619 } else if (!warn_is_string_type(sleaf->type)) {
3620 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003621 }
3622 }
3623
3624 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3625 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003626 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3627 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003628 } else if (!warn_is_string_type(sleaf->type)) {
3629 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003630 }
3631 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003632 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003633 return rc;
3634 }
3635
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003636 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003637 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003638 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003639 LY_CHECK_RET(rc);
3640
3641 if (strstr(args[0]->val.str, args[1]->val.str)) {
3642 set_fill_boolean(set, 1);
3643 } else {
3644 set_fill_boolean(set, 0);
3645 }
3646
3647 return LY_SUCCESS;
3648}
3649
3650/**
3651 * @brief Execute the XPath count(node-set) function. Returns LYXP_SET_NUMBER
3652 * with the size of the node-set from the argument.
3653 *
3654 * @param[in] args Array of arguments.
3655 * @param[in] arg_count Count of elements in @p args.
3656 * @param[in,out] set Context and result set at the same time.
3657 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003658 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003659 */
3660static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003661xpath_count(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003662{
Michal Vasko03ff5a72019-09-11 13:49:33 +02003663 LY_ERR rc = LY_SUCCESS;
3664
3665 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003666 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003667 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003668 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003669 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003670 return rc;
3671 }
3672
Michal Vasko03ff5a72019-09-11 13:49:33 +02003673 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003674 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "count(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003675 return LY_EVALID;
3676 }
3677
3678 set_fill_number(set, args[0]->used);
3679 return LY_SUCCESS;
3680}
3681
3682/**
3683 * @brief Execute the XPath current() function. Returns LYXP_SET_NODE_SET
3684 * with the context with the intial node.
3685 *
3686 * @param[in] args Array of arguments.
3687 * @param[in] arg_count Count of elements in @p args.
3688 * @param[in,out] set Context and result set at the same time.
3689 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003690 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003691 */
3692static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003693xpath_current(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003694{
3695 if (arg_count || args) {
Radek Krejcie87c7dc2021-06-02 21:25:42 +02003696 LOGVAL(set->ctx, LY_VCODE_XP_INARGCOUNT, arg_count, LY_PRI_LENSTR("current()"));
Michal Vasko03ff5a72019-09-11 13:49:33 +02003697 return LY_EVALID;
3698 }
3699
3700 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02003701 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003702
Michal Vasko296dfaf2021-12-13 16:57:42 +01003703 if (set->cur_scnode) {
3704 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, set->cur_scnode, LYXP_NODE_ELEM, NULL));
3705 } else {
3706 /* root node */
3707 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, NULL, set->root_type, NULL));
3708 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003709 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02003710 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003711
Michal Vasko296dfaf2021-12-13 16:57:42 +01003712 if (set->cur_node) {
3713 /* position is filled later */
3714 set_insert_node(set, set->cur_node, 0, LYXP_NODE_ELEM, 0);
3715 } else {
3716 /* root node */
3717 set_insert_node(set, NULL, 0, set->root_type, 0);
3718 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003719 }
3720
3721 return LY_SUCCESS;
3722}
3723
3724/**
3725 * @brief Execute the YANG 1.1 deref(node-set) function. Returns LYXP_SET_NODE_SET with either
3726 * leafref or instance-identifier target node(s).
3727 *
3728 * @param[in] args Array of arguments.
3729 * @param[in] arg_count Count of elements in @p args.
3730 * @param[in,out] set Context and result set at the same time.
3731 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003732 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003733 */
3734static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003735xpath_deref(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003736{
3737 struct lyd_node_term *leaf;
Michal Vasko42e497c2020-01-06 08:38:25 +01003738 struct lysc_node_leaf *sleaf = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02003739 struct lysc_type_leafref *lref;
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003740 const struct lysc_node *target;
Michal Vasko004d3152020-06-11 19:59:22 +02003741 struct ly_path *p;
3742 struct lyd_node *node;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003743 char *errmsg = NULL;
Michal Vasko00cbf532020-06-15 13:58:47 +02003744 uint8_t oper;
Michal Vasko741bb562021-06-24 11:59:50 +02003745 LY_ERR r;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003746
3747 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003748 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003749 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003750 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
Michal Vasko423ba3f2021-07-19 13:08:50 +02003751 if (!(sleaf->nodetype & LYD_NODE_TERM)) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003752 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3753 sleaf->name);
Michal Vaskoed725d72021-06-23 12:03:45 +02003754 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_LEAFREF) &&
3755 !warn_is_specific_type(sleaf->type, LY_TYPE_INST)) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003756 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"leafref\" nor \"instance-identifier\".",
3757 __func__, sleaf->name);
3758 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003759 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003760 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko423ba3f2021-07-19 13:08:50 +02003761 if (sleaf && (sleaf->nodetype & LYD_NODE_TERM) && (sleaf->type->basetype == LY_TYPE_LEAFREF)) {
Michal Vasko004d3152020-06-11 19:59:22 +02003762 lref = (struct lysc_type_leafref *)sleaf->type;
Michal Vaskod1e53b92021-01-28 13:11:06 +01003763 oper = (sleaf->flags & LYS_IS_OUTPUT) ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT;
Michal Vasko004d3152020-06-11 19:59:22 +02003764
3765 /* it was already evaluated on schema, it must succeed */
Michal Vasko741bb562021-06-24 11:59:50 +02003766 r = ly_path_compile_leafref(set->ctx, &sleaf->node, NULL, lref->path, oper, LY_PATH_TARGET_MANY,
Michal Vasko24fc4d12021-07-12 14:41:20 +02003767 LY_VALUE_SCHEMA_RESOLVED, lref->prefixes, &p);
Michal Vasko741bb562021-06-24 11:59:50 +02003768 if (!r) {
3769 /* get the target node */
3770 target = p[LY_ARRAY_COUNT(p) - 1].node;
3771 ly_path_free(set->ctx, p);
Michal Vasko004d3152020-06-11 19:59:22 +02003772
Michal Vasko741bb562021-06-24 11:59:50 +02003773 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, target, LYXP_NODE_ELEM, NULL));
3774 } /* else the target was found before but is disabled so it was removed */
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003775 }
3776
Michal Vasko741bb562021-06-24 11:59:50 +02003777 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003778 }
3779
Michal Vaskod3678892020-05-21 10:06:58 +02003780 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003781 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "deref(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003782 return LY_EVALID;
3783 }
3784
Michal Vaskod3678892020-05-21 10:06:58 +02003785 lyxp_set_free_content(set);
3786 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003787 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
3788 sleaf = (struct lysc_node_leaf *)leaf->schema;
3789 if (sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
3790 if (sleaf->type->basetype == LY_TYPE_LEAFREF) {
3791 /* find leafref target */
Radek Krejci0b013302021-03-29 15:22:32 +02003792 if (lyplg_type_resolve_leafref((struct lysc_type_leafref *)sleaf->type, &leaf->node, &leaf->value, set->tree,
Michal Vasko9e685082021-01-29 14:49:09 +01003793 &node, &errmsg)) {
Michal Vasko004d3152020-06-11 19:59:22 +02003794 LOGERR(set->ctx, LY_EVALID, errmsg);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003795 free(errmsg);
Michal Vasko004d3152020-06-11 19:59:22 +02003796 return LY_EVALID;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003797 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003798 } else {
3799 assert(sleaf->type->basetype == LY_TYPE_INST);
Michal Vasko004d3152020-06-11 19:59:22 +02003800 if (ly_path_eval(leaf->value.target, set->tree, &node)) {
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003801 LOGERR(set->ctx, LY_EVALID, "Invalid instance-identifier \"%s\" value - required instance not found.",
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02003802 lyd_get_value(&leaf->node));
Michal Vasko03ff5a72019-09-11 13:49:33 +02003803 return LY_EVALID;
3804 }
3805 }
Michal Vasko004d3152020-06-11 19:59:22 +02003806
3807 /* insert it */
3808 set_insert_node(set, node, 0, LYXP_NODE_ELEM, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003809 }
3810 }
3811
3812 return LY_SUCCESS;
3813}
3814
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003815static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02003816xpath_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 +02003817{
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01003818 uint32_t i;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003819 LY_ARRAY_COUNT_TYPE u;
3820 struct lyd_node_term *leaf;
3821 struct lysc_node_leaf *sleaf;
3822 struct lyd_meta *meta;
Michal Vasko93923692021-05-07 15:28:02 +02003823 struct lyd_value *val;
3824 const struct lys_module *mod;
3825 const char *id_name;
3826 uint16_t id_len;
3827 struct lysc_ident *id;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003828 LY_ERR rc = LY_SUCCESS;
Radek Krejci857189e2020-09-01 13:26:36 +02003829 ly_bool found;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003830
3831 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003832 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003833 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", func);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003834 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3835 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3836 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype),
3837 sleaf->name);
3838 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_IDENT)) {
3839 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"identityref\".", func, sleaf->name);
3840 }
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003841 }
3842
3843 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3844 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3845 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype),
Michal Vasko69730152020-10-09 16:30:07 +02003846 sleaf->name);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003847 } else if (!warn_is_string_type(sleaf->type)) {
3848 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", func, sleaf->name);
3849 }
3850 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003851 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003852 return rc;
3853 }
3854
3855 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003856 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 +02003857 return LY_EVALID;
3858 }
3859 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
3860 LY_CHECK_RET(rc);
3861
Michal Vasko93923692021-05-07 15:28:02 +02003862 /* parse the identity */
3863 id_name = args[1]->val.str;
3864 id_len = strlen(id_name);
3865 rc = moveto_resolve_model(&id_name, &id_len, set, set->cur_node ? set->cur_node->schema : NULL, &mod);
3866 LY_CHECK_RET(rc);
3867 if (!mod) {
3868 LOGVAL(set->ctx, LYVE_XPATH, "Identity \"%.*s\" without a prefix.", (int)id_len, id_name);
3869 return LY_EVALID;
3870 }
3871
3872 /* find the identity */
3873 found = 0;
3874 LY_ARRAY_FOR(mod->identities, u) {
3875 if (!ly_strncmp(mod->identities[u].name, id_name, id_len)) {
3876 /* we have match */
3877 found = 1;
3878 break;
3879 }
3880 }
3881 if (!found) {
3882 LOGVAL(set->ctx, LYVE_XPATH, "Identity \"%.*s\" not found in module \"%s\".", (int)id_len, id_name, mod->name);
3883 return LY_EVALID;
3884 }
3885 id = &mod->identities[u];
3886
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003887 set_fill_boolean(set, 0);
3888 found = 0;
3889 for (i = 0; i < args[0]->used; ++i) {
3890 if ((args[0]->val.nodes[i].type != LYXP_NODE_ELEM) && (args[0]->val.nodes[i].type != LYXP_NODE_META)) {
3891 continue;
3892 }
3893
3894 if (args[0]->val.nodes[i].type == LYXP_NODE_ELEM) {
3895 leaf = (struct lyd_node_term *)args[0]->val.nodes[i].node;
3896 sleaf = (struct lysc_node_leaf *)leaf->schema;
3897 val = &leaf->value;
3898 if (!(sleaf->nodetype & LYD_NODE_TERM) || (leaf->value.realtype->basetype != LY_TYPE_IDENT)) {
3899 /* uninteresting */
3900 continue;
3901 }
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003902 } else {
3903 meta = args[0]->val.meta[i].meta;
3904 val = &meta->value;
3905 if (val->realtype->basetype != LY_TYPE_IDENT) {
3906 /* uninteresting */
3907 continue;
3908 }
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003909 }
3910
Michal Vasko93923692021-05-07 15:28:02 +02003911 /* check the identity itself */
3912 if (self_match && (id == val->ident)) {
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003913 set_fill_boolean(set, 1);
3914 found = 1;
3915 }
Michal Vasko93923692021-05-07 15:28:02 +02003916 if (!found && !lyplg_type_identity_isderived(id, val->ident)) {
3917 set_fill_boolean(set, 1);
3918 found = 1;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003919 }
3920
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003921 if (found) {
3922 break;
3923 }
3924 }
3925
3926 return LY_SUCCESS;
3927}
3928
Michal Vasko03ff5a72019-09-11 13:49:33 +02003929/**
3930 * @brief Execute the YANG 1.1 derived-from(node-set, string) function. Returns LYXP_SET_BOOLEAN depending
3931 * on whether the first argument nodes contain a node of an identity derived from the second
3932 * argument identity.
3933 *
3934 * @param[in] args Array of arguments.
3935 * @param[in] arg_count Count of elements in @p args.
3936 * @param[in,out] set Context and result set at the same time.
3937 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003938 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003939 */
3940static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003941xpath_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 +02003942{
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003943 return xpath_derived_(args, set, options, 0, __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003944}
3945
3946/**
3947 * @brief Execute the YANG 1.1 derived-from-or-self(node-set, string) function. Returns LYXP_SET_BOOLEAN depending
3948 * on whether the first argument nodes contain a node of an identity that either is or is derived from
3949 * the second argument identity.
3950 *
3951 * @param[in] args Array of arguments.
3952 * @param[in] arg_count Count of elements in @p args.
3953 * @param[in,out] set Context and result set at the same time.
3954 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003955 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003956 */
3957static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003958xpath_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 +02003959{
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003960 return xpath_derived_(args, set, options, 1, __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003961}
3962
3963/**
3964 * @brief Execute the YANG 1.1 enum-value(node-set) function. Returns LYXP_SET_NUMBER
3965 * with the integer value of the first node's enum value, otherwise NaN.
3966 *
3967 * @param[in] args Array of arguments.
3968 * @param[in] arg_count Count of elements in @p args.
3969 * @param[in,out] set Context and result set at the same time.
3970 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003971 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003972 */
3973static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003974xpath_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 +02003975{
3976 struct lyd_node_term *leaf;
3977 struct lysc_node_leaf *sleaf;
3978 LY_ERR rc = LY_SUCCESS;
3979
3980 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003981 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003982 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003983 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3984 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3985 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3986 sleaf->name);
3987 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_ENUM)) {
3988 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"enumeration\".", __func__, sleaf->name);
3989 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003990 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003991 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003992 return rc;
3993 }
3994
Michal Vaskod3678892020-05-21 10:06:58 +02003995 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003996 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "enum-value(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003997 return LY_EVALID;
3998 }
3999
4000 set_fill_number(set, NAN);
Michal Vaskod3678892020-05-21 10:06:58 +02004001 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004002 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
4003 sleaf = (struct lysc_node_leaf *)leaf->schema;
4004 if ((sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (sleaf->type->basetype == LY_TYPE_ENUM)) {
4005 set_fill_number(set, leaf->value.enum_item->value);
4006 }
4007 }
4008
4009 return LY_SUCCESS;
4010}
4011
4012/**
4013 * @brief Execute the XPath false() function. Returns LYXP_SET_BOOLEAN
4014 * with false value.
4015 *
4016 * @param[in] args Array of arguments.
4017 * @param[in] arg_count Count of elements in @p args.
4018 * @param[in,out] set Context and result set at the same time.
4019 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004020 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004021 */
4022static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004023xpath_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 +02004024{
4025 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004026 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004027 return LY_SUCCESS;
4028 }
4029
4030 set_fill_boolean(set, 0);
4031 return LY_SUCCESS;
4032}
4033
4034/**
4035 * @brief Execute the XPath floor(number) function. Returns LYXP_SET_NUMBER
4036 * with the first argument floored (truncated).
4037 *
4038 * @param[in] args Array of arguments.
4039 * @param[in] arg_count Count of elements in @p args.
4040 * @param[in,out] set Context and result set at the same time.
4041 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004042 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004043 */
4044static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004045xpath_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 +02004046{
4047 LY_ERR rc;
4048
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004049 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004050 LY_CHECK_RET(rc);
4051 if (isfinite(args[0]->val.num)) {
4052 set_fill_number(set, (long long)args[0]->val.num);
4053 }
4054
4055 return LY_SUCCESS;
4056}
4057
4058/**
4059 * @brief Execute the XPath lang(string) function. Returns LYXP_SET_BOOLEAN
4060 * whether the language of the text matches the one from the argument.
4061 *
4062 * @param[in] args Array of arguments.
4063 * @param[in] arg_count Count of elements in @p args.
4064 * @param[in,out] set Context and result set at the same time.
4065 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004066 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004067 */
4068static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004069xpath_lang(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004070{
4071 const struct lyd_node *node;
4072 struct lysc_node_leaf *sleaf;
Michal Vasko9f96a052020-03-10 09:41:45 +01004073 struct lyd_meta *meta = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004074 const char *val;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004075 LY_ERR rc = LY_SUCCESS;
4076
4077 if (options & LYXP_SCNODE_ALL) {
4078 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4079 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004080 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
4081 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004082 } else if (!warn_is_string_type(sleaf->type)) {
4083 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004084 }
4085 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004086 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004087 return rc;
4088 }
4089
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004090 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004091 LY_CHECK_RET(rc);
4092
Michal Vasko03ff5a72019-09-11 13:49:33 +02004093 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004094 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "lang(string)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004095 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004096 } else if (!set->used) {
4097 set_fill_boolean(set, 0);
4098 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004099 }
4100
4101 switch (set->val.nodes[0].type) {
4102 case LYXP_NODE_ELEM:
4103 case LYXP_NODE_TEXT:
4104 node = set->val.nodes[0].node;
4105 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004106 case LYXP_NODE_META:
4107 node = set->val.meta[0].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004108 break;
4109 default:
4110 /* nothing to do with roots */
4111 set_fill_boolean(set, 0);
4112 return LY_SUCCESS;
4113 }
4114
Michal Vasko9f96a052020-03-10 09:41:45 +01004115 /* find lang metadata */
Michal Vasko9e685082021-01-29 14:49:09 +01004116 for ( ; node; node = lyd_parent(node)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01004117 for (meta = node->meta; meta; meta = meta->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004118 /* annotations */
Michal Vasko9f96a052020-03-10 09:41:45 +01004119 if (meta->name && !strcmp(meta->name, "lang") && !strcmp(meta->annotation->module->name, "xml")) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004120 break;
4121 }
4122 }
4123
Michal Vasko9f96a052020-03-10 09:41:45 +01004124 if (meta) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004125 break;
4126 }
4127 }
4128
4129 /* compare languages */
Michal Vasko9f96a052020-03-10 09:41:45 +01004130 if (!meta) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004131 set_fill_boolean(set, 0);
4132 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004133 uint64_t i;
4134
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02004135 val = lyd_get_meta_value(meta);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004136 for (i = 0; args[0]->val.str[i]; ++i) {
4137 if (tolower(args[0]->val.str[i]) != tolower(val[i])) {
4138 set_fill_boolean(set, 0);
4139 break;
4140 }
4141 }
4142 if (!args[0]->val.str[i]) {
4143 if (!val[i] || (val[i] == '-')) {
4144 set_fill_boolean(set, 1);
4145 } else {
4146 set_fill_boolean(set, 0);
4147 }
4148 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004149 }
4150
4151 return LY_SUCCESS;
4152}
4153
4154/**
4155 * @brief Execute the XPath last() function. Returns LYXP_SET_NUMBER
4156 * with the context size.
4157 *
4158 * @param[in] args Array of arguments.
4159 * @param[in] arg_count Count of elements in @p args.
4160 * @param[in,out] set Context and result set at the same time.
4161 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004162 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004163 */
4164static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004165xpath_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 +02004166{
4167 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004168 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004169 return LY_SUCCESS;
4170 }
4171
Michal Vasko03ff5a72019-09-11 13:49:33 +02004172 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004173 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "last()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004174 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004175 } else if (!set->used) {
4176 set_fill_number(set, 0);
4177 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004178 }
4179
4180 set_fill_number(set, set->ctx_size);
4181 return LY_SUCCESS;
4182}
4183
4184/**
4185 * @brief Execute the XPath local-name(node-set?) function. Returns LYXP_SET_STRING
4186 * with the node name without namespace from the argument or the context.
4187 *
4188 * @param[in] args Array of arguments.
4189 * @param[in] arg_count Count of elements in @p args.
4190 * @param[in,out] set Context and result set at the same time.
4191 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004192 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004193 */
4194static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004195xpath_local_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004196{
4197 struct lyxp_set_node *item;
Michal Vasko69730152020-10-09 16:30:07 +02004198
Michal Vasko03ff5a72019-09-11 13:49:33 +02004199 /* suppress unused variable warning */
4200 (void)options;
4201
4202 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004203 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004204 return LY_SUCCESS;
4205 }
4206
4207 if (arg_count) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004208 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004209 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004210 "local-name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004211 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004212 } else if (!args[0]->used) {
4213 set_fill_string(set, "", 0);
4214 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004215 }
4216
4217 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004218 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004219
4220 item = &args[0]->val.nodes[0];
4221 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004222 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004223 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "local-name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004224 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004225 } else if (!set->used) {
4226 set_fill_string(set, "", 0);
4227 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004228 }
4229
4230 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004231 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004232
4233 item = &set->val.nodes[0];
4234 }
4235
4236 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004237 case LYXP_NODE_NONE:
4238 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004239 case LYXP_NODE_ROOT:
4240 case LYXP_NODE_ROOT_CONFIG:
4241 case LYXP_NODE_TEXT:
4242 set_fill_string(set, "", 0);
4243 break;
4244 case LYXP_NODE_ELEM:
4245 set_fill_string(set, item->node->schema->name, strlen(item->node->schema->name));
4246 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004247 case LYXP_NODE_META:
4248 set_fill_string(set, ((struct lyd_meta *)item->node)->name, strlen(((struct lyd_meta *)item->node)->name));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004249 break;
4250 }
4251
4252 return LY_SUCCESS;
4253}
4254
4255/**
4256 * @brief Execute the XPath name(node-set?) function. Returns LYXP_SET_STRING
4257 * with the node name fully qualified (with namespace) from the argument or the context.
4258 *
4259 * @param[in] args Array of arguments.
4260 * @param[in] arg_count Count of elements in @p args.
4261 * @param[in,out] set Context and result set at the same time.
4262 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004263 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004264 */
4265static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004266xpath_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004267{
4268 struct lyxp_set_node *item;
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004269 struct lys_module *mod = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004270 char *str;
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004271 const char *name = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004272
4273 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004274 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004275 return LY_SUCCESS;
4276 }
4277
4278 if (arg_count) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004279 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004280 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004281 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004282 } else if (!args[0]->used) {
4283 set_fill_string(set, "", 0);
4284 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004285 }
4286
4287 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004288 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004289
4290 item = &args[0]->val.nodes[0];
4291 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004292 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004293 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004294 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004295 } else if (!set->used) {
4296 set_fill_string(set, "", 0);
4297 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004298 }
4299
4300 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004301 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004302
4303 item = &set->val.nodes[0];
4304 }
4305
4306 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004307 case LYXP_NODE_NONE:
4308 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004309 case LYXP_NODE_ROOT:
4310 case LYXP_NODE_ROOT_CONFIG:
4311 case LYXP_NODE_TEXT:
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004312 /* keep NULL */
Michal Vasko03ff5a72019-09-11 13:49:33 +02004313 break;
4314 case LYXP_NODE_ELEM:
4315 mod = item->node->schema->module;
4316 name = item->node->schema->name;
4317 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004318 case LYXP_NODE_META:
4319 mod = ((struct lyd_meta *)item->node)->annotation->module;
4320 name = ((struct lyd_meta *)item->node)->name;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004321 break;
4322 }
4323
4324 if (mod && name) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004325 int rc = asprintf(&str, "%s:%s", ly_get_prefix(mod, set->format, set->prefix_data), name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004326 LY_CHECK_ERR_RET(rc == -1, LOGMEM(set->ctx), LY_EMEM);
4327 set_fill_string(set, str, strlen(str));
4328 free(str);
4329 } else {
4330 set_fill_string(set, "", 0);
4331 }
4332
4333 return LY_SUCCESS;
4334}
4335
4336/**
4337 * @brief Execute the XPath namespace-uri(node-set?) function. Returns LYXP_SET_STRING
4338 * with the namespace of the node from the argument or the context.
4339 *
4340 * @param[in] args Array of arguments.
4341 * @param[in] arg_count Count of elements in @p args.
4342 * @param[in,out] set Context and result set at the same time.
4343 * @param[in] options XPath options.
4344 * @return LY_ERR (LY_EINVAL for wrong arguments on schema)
4345 */
4346static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004347xpath_namespace_uri(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004348{
4349 struct lyxp_set_node *item;
4350 struct lys_module *mod;
Michal Vasko69730152020-10-09 16:30:07 +02004351
Michal Vasko03ff5a72019-09-11 13:49:33 +02004352 /* suppress unused variable warning */
4353 (void)options;
4354
4355 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004356 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004357 return LY_SUCCESS;
4358 }
4359
4360 if (arg_count) {
Michal Vaskod3678892020-05-21 10:06:58 +02004361 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004362 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
Michal Vasko69730152020-10-09 16:30:07 +02004363 "namespace-uri(node-set?)");
Michal Vaskod3678892020-05-21 10:06:58 +02004364 return LY_EVALID;
4365 } else if (!args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004366 set_fill_string(set, "", 0);
4367 return LY_SUCCESS;
4368 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004369
4370 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004371 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004372
4373 item = &args[0]->val.nodes[0];
4374 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004375 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004376 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "namespace-uri(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004377 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004378 } else if (!set->used) {
4379 set_fill_string(set, "", 0);
4380 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004381 }
4382
4383 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004384 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004385
4386 item = &set->val.nodes[0];
4387 }
4388
4389 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004390 case LYXP_NODE_NONE:
4391 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004392 case LYXP_NODE_ROOT:
4393 case LYXP_NODE_ROOT_CONFIG:
4394 case LYXP_NODE_TEXT:
4395 set_fill_string(set, "", 0);
4396 break;
4397 case LYXP_NODE_ELEM:
Michal Vasko9f96a052020-03-10 09:41:45 +01004398 case LYXP_NODE_META:
Michal Vasko03ff5a72019-09-11 13:49:33 +02004399 if (item->type == LYXP_NODE_ELEM) {
4400 mod = item->node->schema->module;
Michal Vasko9f96a052020-03-10 09:41:45 +01004401 } else { /* LYXP_NODE_META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02004402 /* annotations */
Michal Vasko9f96a052020-03-10 09:41:45 +01004403 mod = ((struct lyd_meta *)item->node)->annotation->module;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004404 }
4405
4406 set_fill_string(set, mod->ns, strlen(mod->ns));
4407 break;
4408 }
4409
4410 return LY_SUCCESS;
4411}
4412
4413/**
4414 * @brief Execute the XPath node() function (node type). Returns LYXP_SET_NODE_SET
4415 * with only nodes from the context. In practice it either leaves the context
4416 * as it is or returns an empty node set.
4417 *
4418 * @param[in] args Array of arguments.
4419 * @param[in] arg_count Count of elements in @p args.
4420 * @param[in,out] set Context and result set at the same time.
4421 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004422 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004423 */
4424static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004425xpath_node(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004426{
4427 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004428 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004429 return LY_SUCCESS;
4430 }
4431
4432 if (set->type != LYXP_SET_NODE_SET) {
Michal Vaskod3678892020-05-21 10:06:58 +02004433 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004434 }
4435 return LY_SUCCESS;
4436}
4437
4438/**
4439 * @brief Execute the XPath normalize-space(string?) function. Returns LYXP_SET_STRING
4440 * with normalized value (no leading, trailing, double white spaces) of the node
4441 * from the argument or the context.
4442 *
4443 * @param[in] args Array of arguments.
4444 * @param[in] arg_count Count of elements in @p args.
4445 * @param[in,out] set Context and result set at the same time.
4446 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004447 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004448 */
4449static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004450xpath_normalize_space(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004451{
4452 uint16_t i, new_used;
4453 char *new;
Radek Krejci857189e2020-09-01 13:26:36 +02004454 ly_bool have_spaces = 0, space_before = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004455 struct lysc_node_leaf *sleaf;
4456 LY_ERR rc = LY_SUCCESS;
4457
4458 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004459 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) &&
4460 (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004461 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004462 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
4463 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004464 } else if (!warn_is_string_type(sleaf->type)) {
4465 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004466 }
4467 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004468 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004469 return rc;
4470 }
4471
4472 if (arg_count) {
4473 set_fill_set(set, args[0]);
4474 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004475 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004476 LY_CHECK_RET(rc);
4477
4478 /* is there any normalization necessary? */
4479 for (i = 0; set->val.str[i]; ++i) {
4480 if (is_xmlws(set->val.str[i])) {
4481 if ((i == 0) || space_before || (!set->val.str[i + 1])) {
4482 have_spaces = 1;
4483 break;
4484 }
4485 space_before = 1;
4486 } else {
4487 space_before = 0;
4488 }
4489 }
4490
4491 /* yep, there is */
4492 if (have_spaces) {
4493 /* it's enough, at least one character will go, makes space for ending '\0' */
4494 new = malloc(strlen(set->val.str) * sizeof(char));
4495 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4496 new_used = 0;
4497
4498 space_before = 0;
4499 for (i = 0; set->val.str[i]; ++i) {
4500 if (is_xmlws(set->val.str[i])) {
4501 if ((i == 0) || space_before) {
4502 space_before = 1;
4503 continue;
4504 } else {
4505 space_before = 1;
4506 }
4507 } else {
4508 space_before = 0;
4509 }
4510
4511 new[new_used] = (space_before ? ' ' : set->val.str[i]);
4512 ++new_used;
4513 }
4514
4515 /* at worst there is one trailing space now */
4516 if (new_used && is_xmlws(new[new_used - 1])) {
4517 --new_used;
4518 }
4519
4520 new = ly_realloc(new, (new_used + 1) * sizeof(char));
4521 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4522 new[new_used] = '\0';
4523
4524 free(set->val.str);
4525 set->val.str = new;
4526 }
4527
4528 return LY_SUCCESS;
4529}
4530
4531/**
4532 * @brief Execute the XPath not(boolean) function. Returns LYXP_SET_BOOLEAN
4533 * with the argument converted to boolean and logically inverted.
4534 *
4535 * @param[in] args Array of arguments.
4536 * @param[in] arg_count Count of elements in @p args.
4537 * @param[in,out] set Context and result set at the same time.
4538 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004539 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004540 */
4541static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004542xpath_not(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004543{
4544 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004545 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004546 return LY_SUCCESS;
4547 }
4548
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004549 lyxp_set_cast(args[0], LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +02004550 if (args[0]->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004551 set_fill_boolean(set, 0);
4552 } else {
4553 set_fill_boolean(set, 1);
4554 }
4555
4556 return LY_SUCCESS;
4557}
4558
4559/**
4560 * @brief Execute the XPath number(object?) function. Returns LYXP_SET_NUMBER
4561 * with the number representation of either the argument or the context.
4562 *
4563 * @param[in] args Array of arguments.
4564 * @param[in] arg_count Count of elements in @p args.
4565 * @param[in,out] set Context and result set at the same time.
4566 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004567 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004568 */
4569static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004570xpath_number(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004571{
4572 LY_ERR rc;
4573
4574 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004575 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004576 return LY_SUCCESS;
4577 }
4578
4579 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004580 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004581 LY_CHECK_RET(rc);
4582 set_fill_set(set, args[0]);
4583 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004584 rc = lyxp_set_cast(set, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004585 LY_CHECK_RET(rc);
4586 }
4587
4588 return LY_SUCCESS;
4589}
4590
4591/**
4592 * @brief Execute the XPath position() function. Returns LYXP_SET_NUMBER
4593 * with the context position.
4594 *
4595 * @param[in] args Array of arguments.
4596 * @param[in] arg_count Count of elements in @p args.
4597 * @param[in,out] set Context and result set at the same time.
4598 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004599 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004600 */
4601static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004602xpath_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 +02004603{
4604 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004605 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004606 return LY_SUCCESS;
4607 }
4608
Michal Vasko03ff5a72019-09-11 13:49:33 +02004609 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004610 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "position()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004611 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004612 } else if (!set->used) {
4613 set_fill_number(set, 0);
4614 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004615 }
4616
4617 set_fill_number(set, set->ctx_pos);
4618
4619 /* UNUSED in 'Release' build type */
4620 (void)options;
4621 return LY_SUCCESS;
4622}
4623
4624/**
4625 * @brief Execute the YANG 1.1 re-match(string, string) function. Returns LYXP_SET_BOOLEAN
4626 * depending on whether the second argument regex matches the first argument string. For details refer to
4627 * YANG 1.1 RFC section 10.2.1.
4628 *
4629 * @param[in] args Array of arguments.
4630 * @param[in] arg_count Count of elements in @p args.
4631 * @param[in,out] set Context and result set at the same time.
4632 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004633 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004634 */
4635static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004636xpath_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 +02004637{
4638 struct lysc_pattern **patterns = NULL, **pattern;
4639 struct lysc_node_leaf *sleaf;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004640 LY_ERR rc = LY_SUCCESS;
4641 struct ly_err_item *err;
4642
4643 if (options & LYXP_SCNODE_ALL) {
4644 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4645 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4646 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 +02004647 } else if (!warn_is_string_type(sleaf->type)) {
4648 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004649 }
4650 }
4651
4652 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4653 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4654 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 +02004655 } else if (!warn_is_string_type(sleaf->type)) {
4656 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004657 }
4658 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004659 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004660 return rc;
4661 }
4662
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004663 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004664 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004665 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004666 LY_CHECK_RET(rc);
4667
4668 LY_ARRAY_NEW_RET(set->ctx, patterns, pattern, LY_EMEM);
Radek IÅ¡a45802b52021-02-09 09:21:58 +01004669 *pattern = calloc(1, sizeof **pattern);
Radek Krejciddace2c2021-01-08 11:30:56 +01004670 LOG_LOCSET(NULL, set->cur_node, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01004671 rc = lys_compile_type_pattern_check(set->ctx, args[1]->val.str, &(*pattern)->code);
Radek Krejciddace2c2021-01-08 11:30:56 +01004672 LOG_LOCBACK(0, 1, 0, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004673 if (rc != LY_SUCCESS) {
4674 LY_ARRAY_FREE(patterns);
4675 return rc;
4676 }
4677
Radek Krejci0b013302021-03-29 15:22:32 +02004678 rc = lyplg_type_validate_patterns(patterns, args[0]->val.str, strlen(args[0]->val.str), &err);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004679 pcre2_code_free((*pattern)->code);
4680 free(*pattern);
4681 LY_ARRAY_FREE(patterns);
4682 if (rc && (rc != LY_EVALID)) {
Michal Vasko177d0ed2020-11-23 16:43:03 +01004683 ly_err_print(set->ctx, err);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004684 ly_err_free(err);
4685 return rc;
4686 }
4687
4688 if (rc == LY_EVALID) {
4689 ly_err_free(err);
4690 set_fill_boolean(set, 0);
4691 } else {
4692 set_fill_boolean(set, 1);
4693 }
4694
4695 return LY_SUCCESS;
4696}
4697
4698/**
4699 * @brief Execute the XPath round(number) function. Returns LYXP_SET_NUMBER
4700 * with the rounded first argument. For details refer to
4701 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-round.
4702 *
4703 * @param[in] args Array of arguments.
4704 * @param[in] arg_count Count of elements in @p args.
4705 * @param[in,out] set Context and result set at the same time.
4706 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004707 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004708 */
4709static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004710xpath_round(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004711{
4712 struct lysc_node_leaf *sleaf;
4713 LY_ERR rc = LY_SUCCESS;
4714
4715 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02004716 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004717 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02004718 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4719 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4720 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
4721 sleaf->name);
4722 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
4723 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
4724 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004725 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004726 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004727 return rc;
4728 }
4729
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004730 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004731 LY_CHECK_RET(rc);
4732
4733 /* cover only the cases where floor can't be used */
4734 if ((args[0]->val.num == -0.0f) || ((args[0]->val.num < 0) && (args[0]->val.num >= -0.5))) {
4735 set_fill_number(set, -0.0f);
4736 } else {
4737 args[0]->val.num += 0.5;
4738 rc = xpath_floor(args, 1, args[0], options);
4739 LY_CHECK_RET(rc);
4740 set_fill_number(set, args[0]->val.num);
4741 }
4742
4743 return LY_SUCCESS;
4744}
4745
4746/**
4747 * @brief Execute the XPath starts-with(string, string) function.
4748 * Returns LYXP_SET_BOOLEAN whether the second argument is
4749 * the prefix of the first or not.
4750 *
4751 * @param[in] args Array of arguments.
4752 * @param[in] arg_count Count of elements in @p args.
4753 * @param[in,out] set Context and result set at the same time.
4754 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004755 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004756 */
4757static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004758xpath_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 +02004759{
4760 struct lysc_node_leaf *sleaf;
4761 LY_ERR rc = LY_SUCCESS;
4762
4763 if (options & LYXP_SCNODE_ALL) {
4764 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4765 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4766 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 +02004767 } else if (!warn_is_string_type(sleaf->type)) {
4768 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004769 }
4770 }
4771
4772 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4773 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4774 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 +02004775 } else if (!warn_is_string_type(sleaf->type)) {
4776 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004777 }
4778 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004779 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004780 return rc;
4781 }
4782
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004783 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004784 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004785 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004786 LY_CHECK_RET(rc);
4787
4788 if (strncmp(args[0]->val.str, args[1]->val.str, strlen(args[1]->val.str))) {
4789 set_fill_boolean(set, 0);
4790 } else {
4791 set_fill_boolean(set, 1);
4792 }
4793
4794 return LY_SUCCESS;
4795}
4796
4797/**
4798 * @brief Execute the XPath string(object?) function. Returns LYXP_SET_STRING
4799 * with the string representation of either the argument or the context.
4800 *
4801 * @param[in] args Array of arguments.
4802 * @param[in] arg_count Count of elements in @p args.
4803 * @param[in,out] set Context and result set at the same time.
4804 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004805 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004806 */
4807static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004808xpath_string(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004809{
4810 LY_ERR rc;
4811
4812 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004813 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004814 return LY_SUCCESS;
4815 }
4816
4817 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004818 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004819 LY_CHECK_RET(rc);
4820 set_fill_set(set, args[0]);
4821 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004822 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004823 LY_CHECK_RET(rc);
4824 }
4825
4826 return LY_SUCCESS;
4827}
4828
4829/**
4830 * @brief Execute the XPath string-length(string?) function. Returns LYXP_SET_NUMBER
4831 * with the length of the string in either the argument or the context.
4832 *
4833 * @param[in] args Array of arguments.
4834 * @param[in] arg_count Count of elements in @p args.
4835 * @param[in,out] set Context and result set at the same time.
4836 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004837 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004838 */
4839static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004840xpath_string_length(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004841{
4842 struct lysc_node_leaf *sleaf;
4843 LY_ERR rc = LY_SUCCESS;
4844
4845 if (options & LYXP_SCNODE_ALL) {
4846 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4847 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4848 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 +02004849 } else if (!warn_is_string_type(sleaf->type)) {
4850 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004851 }
4852 }
4853 if (!arg_count && (set->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set))) {
4854 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4855 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 +02004856 } else if (!warn_is_string_type(sleaf->type)) {
4857 LOGWRN(set->ctx, "Argument #0 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004858 }
4859 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004860 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004861 return rc;
4862 }
4863
4864 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004865 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004866 LY_CHECK_RET(rc);
4867 set_fill_number(set, strlen(args[0]->val.str));
4868 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004869 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004870 LY_CHECK_RET(rc);
4871 set_fill_number(set, strlen(set->val.str));
4872 }
4873
4874 return LY_SUCCESS;
4875}
4876
4877/**
4878 * @brief Execute the XPath substring(string, number, number?) function.
4879 * Returns LYXP_SET_STRING substring of the first argument starting
4880 * on the second argument index ending on the third argument index,
4881 * indexed from 1. For exact definition refer to
4882 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-substring.
4883 *
4884 * @param[in] args Array of arguments.
4885 * @param[in] arg_count Count of elements in @p args.
4886 * @param[in,out] set Context and result set at the same time.
4887 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004888 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004889 */
4890static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004891xpath_substring(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004892{
Radek Krejci1deb5be2020-08-26 16:43:36 +02004893 int32_t start, len;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004894 uint16_t str_start, str_len, pos;
4895 struct lysc_node_leaf *sleaf;
4896 LY_ERR rc = LY_SUCCESS;
4897
4898 if (options & LYXP_SCNODE_ALL) {
4899 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4900 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4901 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 +02004902 } else if (!warn_is_string_type(sleaf->type)) {
4903 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004904 }
4905 }
4906
4907 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4908 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4909 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 +02004910 } else if (!warn_is_numeric_type(sleaf->type)) {
4911 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004912 }
4913 }
4914
Michal Vasko69730152020-10-09 16:30:07 +02004915 if ((arg_count == 3) && (args[2]->type == LYXP_SET_SCNODE_SET) &&
4916 (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004917 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4918 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 +02004919 } else if (!warn_is_numeric_type(sleaf->type)) {
4920 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004921 }
4922 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004923 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004924 return rc;
4925 }
4926
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004927 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004928 LY_CHECK_RET(rc);
4929
4930 /* start */
4931 if (xpath_round(&args[1], 1, args[1], options)) {
4932 return -1;
4933 }
4934 if (isfinite(args[1]->val.num)) {
4935 start = args[1]->val.num - 1;
4936 } else if (isinf(args[1]->val.num) && signbit(args[1]->val.num)) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004937 start = INT32_MIN;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004938 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004939 start = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004940 }
4941
4942 /* len */
4943 if (arg_count == 3) {
4944 rc = xpath_round(&args[2], 1, args[2], options);
4945 LY_CHECK_RET(rc);
Radek Krejci1deb5be2020-08-26 16:43:36 +02004946 if (isnan(args[2]->val.num) || signbit(args[2]->val.num)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004947 len = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +02004948 } else if (isfinite(args[2]->val.num)) {
4949 len = args[2]->val.num;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004950 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004951 len = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004952 }
4953 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004954 len = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004955 }
4956
4957 /* find matching character positions */
4958 str_start = 0;
4959 str_len = 0;
4960 for (pos = 0; args[0]->val.str[pos]; ++pos) {
4961 if (pos < start) {
4962 ++str_start;
4963 } else if (pos < start + len) {
4964 ++str_len;
4965 } else {
4966 break;
4967 }
4968 }
4969
4970 set_fill_string(set, args[0]->val.str + str_start, str_len);
4971 return LY_SUCCESS;
4972}
4973
4974/**
4975 * @brief Execute the XPath substring-after(string, string) function.
4976 * Returns LYXP_SET_STRING with the string succeeding the occurance
4977 * of the second argument in the first or an empty string.
4978 *
4979 * @param[in] args Array of arguments.
4980 * @param[in] arg_count Count of elements in @p args.
4981 * @param[in,out] set Context and result set at the same time.
4982 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004983 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004984 */
4985static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004986xpath_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 +02004987{
4988 char *ptr;
4989 struct lysc_node_leaf *sleaf;
4990 LY_ERR rc = LY_SUCCESS;
4991
4992 if (options & LYXP_SCNODE_ALL) {
4993 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4994 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4995 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 +02004996 } else if (!warn_is_string_type(sleaf->type)) {
4997 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004998 }
4999 }
5000
5001 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5002 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5003 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 +02005004 } else if (!warn_is_string_type(sleaf->type)) {
5005 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005006 }
5007 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005008 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005009 return rc;
5010 }
5011
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005012 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005013 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005014 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005015 LY_CHECK_RET(rc);
5016
5017 ptr = strstr(args[0]->val.str, args[1]->val.str);
5018 if (ptr) {
5019 set_fill_string(set, ptr + strlen(args[1]->val.str), strlen(ptr + strlen(args[1]->val.str)));
5020 } else {
5021 set_fill_string(set, "", 0);
5022 }
5023
5024 return LY_SUCCESS;
5025}
5026
5027/**
5028 * @brief Execute the XPath substring-before(string, string) function.
5029 * Returns LYXP_SET_STRING with the string preceding the occurance
5030 * of the second argument in the first or an empty string.
5031 *
5032 * @param[in] args Array of arguments.
5033 * @param[in] arg_count Count of elements in @p args.
5034 * @param[in,out] set Context and result set at the same time.
5035 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005036 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005037 */
5038static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005039xpath_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 +02005040{
5041 char *ptr;
5042 struct lysc_node_leaf *sleaf;
5043 LY_ERR rc = LY_SUCCESS;
5044
5045 if (options & LYXP_SCNODE_ALL) {
5046 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5047 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5048 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 +02005049 } else if (!warn_is_string_type(sleaf->type)) {
5050 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005051 }
5052 }
5053
5054 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5055 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5056 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 +02005057 } else if (!warn_is_string_type(sleaf->type)) {
5058 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005059 }
5060 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005061 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005062 return rc;
5063 }
5064
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005065 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005066 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005067 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005068 LY_CHECK_RET(rc);
5069
5070 ptr = strstr(args[0]->val.str, args[1]->val.str);
5071 if (ptr) {
5072 set_fill_string(set, args[0]->val.str, ptr - args[0]->val.str);
5073 } else {
5074 set_fill_string(set, "", 0);
5075 }
5076
5077 return LY_SUCCESS;
5078}
5079
5080/**
5081 * @brief Execute the XPath sum(node-set) function. Returns LYXP_SET_NUMBER
5082 * with the sum of all the nodes in the context.
5083 *
5084 * @param[in] args Array of arguments.
5085 * @param[in] arg_count Count of elements in @p args.
5086 * @param[in,out] set Context and result set at the same time.
5087 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005088 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005089 */
5090static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005091xpath_sum(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005092{
5093 long double num;
5094 char *str;
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01005095 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005096 struct lyxp_set set_item;
5097 struct lysc_node_leaf *sleaf;
5098 LY_ERR rc = LY_SUCCESS;
5099
5100 if (options & LYXP_SCNODE_ALL) {
5101 if (args[0]->type == LYXP_SET_SCNODE_SET) {
5102 for (i = 0; i < args[0]->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005103 if (args[0]->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005104 sleaf = (struct lysc_node_leaf *)args[0]->val.scnodes[i].scnode;
5105 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5106 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__,
Michal Vasko69730152020-10-09 16:30:07 +02005107 lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005108 } else if (!warn_is_numeric_type(sleaf->type)) {
5109 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005110 }
5111 }
5112 }
5113 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005114 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005115 return rc;
5116 }
5117
5118 set_fill_number(set, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005119
5120 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005121 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "sum(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02005122 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02005123 } else if (!args[0]->used) {
5124 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005125 }
5126
Michal Vasko5c4e5892019-11-14 12:31:38 +01005127 set_init(&set_item, set);
5128
Michal Vasko03ff5a72019-09-11 13:49:33 +02005129 set_item.type = LYXP_SET_NODE_SET;
Michal Vasko41decbf2021-11-02 11:50:21 +01005130 set_item.val.nodes = calloc(1, sizeof *set_item.val.nodes);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005131 LY_CHECK_ERR_RET(!set_item.val.nodes, LOGMEM(set->ctx), LY_EMEM);
5132
5133 set_item.used = 1;
5134 set_item.size = 1;
5135
5136 for (i = 0; i < args[0]->used; ++i) {
5137 set_item.val.nodes[0] = args[0]->val.nodes[i];
5138
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005139 rc = cast_node_set_to_string(&set_item, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005140 LY_CHECK_RET(rc);
5141 num = cast_string_to_number(str);
5142 free(str);
5143 set->val.num += num;
5144 }
5145
5146 free(set_item.val.nodes);
5147
5148 return LY_SUCCESS;
5149}
5150
5151/**
5152 * @brief Execute the XPath text() function (node type). Returns LYXP_SET_NODE_SET
5153 * with the text content of the nodes in the context.
5154 *
5155 * @param[in] args Array of arguments.
5156 * @param[in] arg_count Count of elements in @p args.
5157 * @param[in,out] set Context and result set at the same time.
5158 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005159 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005160 */
5161static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005162xpath_text(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005163{
5164 uint32_t i;
5165
5166 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02005167 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005168 return LY_SUCCESS;
5169 }
5170
Michal Vasko03ff5a72019-09-11 13:49:33 +02005171 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005172 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "text()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02005173 return LY_EVALID;
5174 }
5175
Michal Vaskod989ba02020-08-24 10:59:24 +02005176 for (i = 0; i < set->used; ) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005177 switch (set->val.nodes[i].type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01005178 case LYXP_NODE_NONE:
5179 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005180 case LYXP_NODE_ELEM:
Michal Vasko03ff5a72019-09-11 13:49:33 +02005181 if (set->val.nodes[i].node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
5182 set->val.nodes[i].type = LYXP_NODE_TEXT;
5183 ++i;
5184 break;
5185 }
Radek Krejci0f969882020-08-21 16:56:47 +02005186 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005187 case LYXP_NODE_ROOT:
5188 case LYXP_NODE_ROOT_CONFIG:
5189 case LYXP_NODE_TEXT:
Michal Vasko9f96a052020-03-10 09:41:45 +01005190 case LYXP_NODE_META:
Michal Vasko03ff5a72019-09-11 13:49:33 +02005191 set_remove_node(set, i);
5192 break;
5193 }
5194 }
5195
5196 return LY_SUCCESS;
5197}
5198
5199/**
5200 * @brief Execute the XPath translate(string, string, string) function.
5201 * Returns LYXP_SET_STRING with the first argument with the characters
5202 * from the second argument replaced by those on the corresponding
5203 * positions in the third argument.
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_translate(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 uint16_t i, j, new_used;
5215 char *new;
Radek Krejci857189e2020-09-01 13:26:36 +02005216 ly_bool have_removed;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005217 struct lysc_node_leaf *sleaf;
5218 LY_ERR rc = LY_SUCCESS;
5219
5220 if (options & LYXP_SCNODE_ALL) {
5221 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5222 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5223 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 +02005224 } else if (!warn_is_string_type(sleaf->type)) {
5225 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005226 }
5227 }
5228
5229 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5230 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5231 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 +02005232 } else if (!warn_is_string_type(sleaf->type)) {
5233 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005234 }
5235 }
5236
5237 if ((args[2]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
5238 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5239 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 +02005240 } else if (!warn_is_string_type(sleaf->type)) {
5241 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005242 }
5243 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005244 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005245 return rc;
5246 }
5247
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005248 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005249 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005250 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005251 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005252 rc = lyxp_set_cast(args[2], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005253 LY_CHECK_RET(rc);
5254
5255 new = malloc((strlen(args[0]->val.str) + 1) * sizeof(char));
5256 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5257 new_used = 0;
5258
5259 have_removed = 0;
5260 for (i = 0; args[0]->val.str[i]; ++i) {
Radek Krejci857189e2020-09-01 13:26:36 +02005261 ly_bool found = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005262
5263 for (j = 0; args[1]->val.str[j]; ++j) {
5264 if (args[0]->val.str[i] == args[1]->val.str[j]) {
5265 /* removing this char */
5266 if (j >= strlen(args[2]->val.str)) {
5267 have_removed = 1;
5268 found = 1;
5269 break;
5270 }
5271 /* replacing this char */
5272 new[new_used] = args[2]->val.str[j];
5273 ++new_used;
5274 found = 1;
5275 break;
5276 }
5277 }
5278
5279 /* copying this char */
5280 if (!found) {
5281 new[new_used] = args[0]->val.str[i];
5282 ++new_used;
5283 }
5284 }
5285
5286 if (have_removed) {
5287 new = ly_realloc(new, (new_used + 1) * sizeof(char));
5288 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5289 }
5290 new[new_used] = '\0';
5291
Michal Vaskod3678892020-05-21 10:06:58 +02005292 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005293 set->type = LYXP_SET_STRING;
5294 set->val.str = new;
5295
5296 return LY_SUCCESS;
5297}
5298
5299/**
5300 * @brief Execute the XPath true() function. Returns LYXP_SET_BOOLEAN
5301 * with true value.
5302 *
5303 * @param[in] args Array of arguments.
5304 * @param[in] arg_count Count of elements in @p args.
5305 * @param[in,out] set Context and result set at the same time.
5306 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005307 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005308 */
5309static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005310xpath_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 +02005311{
5312 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02005313 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005314 return LY_SUCCESS;
5315 }
5316
5317 set_fill_boolean(set, 1);
5318 return LY_SUCCESS;
5319}
5320
Michal Vasko03ff5a72019-09-11 13:49:33 +02005321/**
Michal Vasko6346ece2019-09-24 13:12:53 +02005322 * @brief Skip prefix and return corresponding model if there is a prefix. Logs directly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005323 *
Michal Vasko2104e9f2020-03-06 08:23:25 +01005324 * XPath @p set is expected to be a (sc)node set!
5325 *
Michal Vasko6346ece2019-09-24 13:12:53 +02005326 * @param[in,out] qname Qualified node name. If includes prefix, it is skipped.
5327 * @param[in,out] qname_len Length of @p qname, is updated accordingly.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005328 * @param[in] set Set with general XPath context.
5329 * @param[in] ctx_scnode Context node to inherit module for unprefixed node for ::LY_PREF_JSON.
Michal Vasko6346ece2019-09-24 13:12:53 +02005330 * @param[out] moveto_mod Expected module of a matching node.
5331 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005332 */
Michal Vasko6346ece2019-09-24 13:12:53 +02005333static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005334moveto_resolve_model(const char **qname, uint16_t *qname_len, const struct lyxp_set *set,
5335 const struct lysc_node *ctx_scnode, const struct lys_module **moveto_mod)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005336{
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02005337 const struct lys_module *mod = NULL;
Michal Vasko6346ece2019-09-24 13:12:53 +02005338 const char *ptr;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005339 size_t pref_len;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005340
Michal Vasko2104e9f2020-03-06 08:23:25 +01005341 assert((set->type == LYXP_SET_NODE_SET) || (set->type == LYXP_SET_SCNODE_SET));
5342
Michal Vasko6346ece2019-09-24 13:12:53 +02005343 if ((ptr = ly_strnchr(*qname, ':', *qname_len))) {
5344 /* specific module */
5345 pref_len = ptr - *qname;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005346 mod = ly_resolve_prefix(set->ctx, *qname, pref_len, set->format, set->prefix_data);
Michal Vasko6346ece2019-09-24 13:12:53 +02005347
Michal Vasko004d3152020-06-11 19:59:22 +02005348 /* check for errors and non-implemented modules, as they are not valid */
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005349 if (!mod || !mod->implemented) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005350 LOGVAL(set->ctx, LY_VCODE_XP_INMOD, pref_len, *qname);
Michal Vasko6346ece2019-09-24 13:12:53 +02005351 return LY_EVALID;
5352 }
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005353
Michal Vasko6346ece2019-09-24 13:12:53 +02005354 *qname += pref_len + 1;
5355 *qname_len -= pref_len + 1;
5356 } else if (((*qname)[0] == '*') && (*qname_len == 1)) {
5357 /* all modules - special case */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005358 mod = NULL;
Michal Vasko6346ece2019-09-24 13:12:53 +02005359 } else {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005360 switch (set->format) {
Radek Krejci8df109d2021-04-23 12:19:08 +02005361 case LY_VALUE_SCHEMA:
5362 case LY_VALUE_SCHEMA_RESOLVED:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005363 /* current module */
5364 mod = set->cur_mod;
5365 break;
Radek Krejci224d4b42021-04-23 13:54:59 +02005366 case LY_VALUE_CANON:
Radek Krejci8df109d2021-04-23 12:19:08 +02005367 case LY_VALUE_JSON:
Radek Krejcif9943642021-04-26 10:18:21 +02005368 case LY_VALUE_LYB:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005369 /* inherit parent (context node) module */
5370 if (ctx_scnode) {
5371 mod = ctx_scnode->module;
5372 } else {
5373 mod = NULL;
5374 }
5375 break;
Radek Krejci8df109d2021-04-23 12:19:08 +02005376 case LY_VALUE_XML:
Michal Vasko52143d12021-04-14 15:36:39 +02005377 /* all nodes need to be prefixed */
5378 LOGVAL(set->ctx, LYVE_DATA, "Non-prefixed node \"%.*s\" in XML xpath found.", *qname_len, *qname);
5379 return LY_EVALID;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005380 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005381 }
5382
Michal Vasko6346ece2019-09-24 13:12:53 +02005383 *moveto_mod = mod;
5384 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005385}
5386
5387/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02005388 * @brief Move context @p set to the root. Handles absolute path.
5389 * Result is LYXP_SET_NODE_SET.
5390 *
5391 * @param[in,out] set Set to use.
5392 * @param[in] options Xpath options.
Michal Vaskob0099a92020-08-31 14:55:23 +02005393 * @return LY_ERR value.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005394 */
Michal Vaskob0099a92020-08-31 14:55:23 +02005395static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005396moveto_root(struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005397{
aPiecek8b0cc152021-05-31 16:40:31 +02005398 assert(!(options & LYXP_SKIP_EXPR));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005399
5400 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02005401 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskob0099a92020-08-31 14:55:23 +02005402 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, NULL, set->root_type, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005403 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005404 set->type = LYXP_SET_NODE_SET;
5405 set->used = 0;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005406 set_insert_node(set, NULL, 0, set->root_type, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005407 }
Michal Vaskob0099a92020-08-31 14:55:23 +02005408
5409 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005410}
5411
5412/**
5413 * @brief Check @p node as a part of NameTest processing.
5414 *
5415 * @param[in] node Node to check.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005416 * @param[in] set Set to read general context from.
Michal Vaskod3678892020-05-21 10:06:58 +02005417 * @param[in] node_name Node name in the dictionary to move to, NULL for any node.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005418 * @param[in] moveto_mod Expected module of the node, NULL for no prefix.
Michal Vaskocdad7122020-11-09 21:04:44 +01005419 * @param[in] options XPath options.
Michal Vasko6346ece2019-09-24 13:12:53 +02005420 * @return LY_ERR (LY_ENOT if node does not match, LY_EINCOMPLETE on unresolved when,
5421 * LY_EINVAL if netither node nor any children match)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005422 */
5423static LY_ERR
Michal Vaskodb08ce52021-10-06 08:57:49 +02005424moveto_node_check(const struct lyd_node *node, const struct lyxp_set *set, const char *node_name,
5425 const struct lys_module *moveto_mod, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005426{
Michal Vaskodca9f122021-07-16 13:56:22 +02005427 if (!node->schema) {
5428 /* opaque node never matches */
5429 return LY_ENOT;
5430 }
5431
Michal Vasko03ff5a72019-09-11 13:49:33 +02005432 /* module check */
5433 if (moveto_mod && (node->schema->module != moveto_mod)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005434 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005435 }
5436
Michal Vasko5c4e5892019-11-14 12:31:38 +01005437 /* context check */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005438 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (node->schema->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005439 return LY_EINVAL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005440 } else if (set->context_op && (node->schema->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) &&
5441 (node->schema != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005442 return LY_EINVAL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005443 }
5444
5445 /* name check */
Michal Vasko61ac2f62020-05-25 12:39:51 +02005446 if (node_name && strcmp(node_name, "*") && (node->schema->name != node_name)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005447 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005448 }
5449
Michal Vaskoa1424542019-11-14 16:08:52 +01005450 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01005451 if (!(options & LYXP_IGNORE_WHEN) && lysc_has_when(node->schema) && !(node->flags & LYD_WHEN_TRUE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005452 return LY_EINCOMPLETE;
Michal Vaskoa1424542019-11-14 16:08:52 +01005453 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005454
5455 /* match */
5456 return LY_SUCCESS;
5457}
5458
5459/**
5460 * @brief Check @p node as a part of schema NameTest processing.
5461 *
5462 * @param[in] node Schema node to check.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005463 * @param[in] ctx_scnode Context node.
5464 * @param[in] set Set to read general context from.
Michal Vaskod3678892020-05-21 10:06:58 +02005465 * @param[in] node_name Node name in the dictionary to move to, NULL for any nodes.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005466 * @param[in] moveto_mod Expected module of the node, NULL for no prefix.
Michal Vasko6346ece2019-09-24 13:12:53 +02005467 * @return LY_ERR (LY_ENOT if node does not match, LY_EINVAL if neither node nor any children match)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005468 */
5469static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005470moveto_scnode_check(const struct lysc_node *node, const struct lysc_node *ctx_scnode, const struct lyxp_set *set,
Radek Krejci0f969882020-08-21 16:56:47 +02005471 const char *node_name, const struct lys_module *moveto_mod)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005472{
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005473 if (!moveto_mod && (!node_name || strcmp(node_name, "*"))) {
5474 switch (set->format) {
Radek Krejci8df109d2021-04-23 12:19:08 +02005475 case LY_VALUE_SCHEMA:
5476 case LY_VALUE_SCHEMA_RESOLVED:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005477 /* use current module */
5478 moveto_mod = set->cur_mod;
5479 break;
Radek Krejci8df109d2021-04-23 12:19:08 +02005480 case LY_VALUE_JSON:
Radek Krejcif9943642021-04-26 10:18:21 +02005481 case LY_VALUE_LYB:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005482 /* inherit module of the context node, if any */
5483 if (ctx_scnode) {
5484 moveto_mod = ctx_scnode->module;
5485 }
5486 break;
Radek Krejci224d4b42021-04-23 13:54:59 +02005487 case LY_VALUE_CANON:
Radek Krejci8df109d2021-04-23 12:19:08 +02005488 case LY_VALUE_XML:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005489 /* not defined */
5490 LOGINT(set->ctx);
5491 return LY_EINVAL;
5492 }
5493 }
5494
Michal Vasko03ff5a72019-09-11 13:49:33 +02005495 /* module check */
Michal Vaskod3678892020-05-21 10:06:58 +02005496 if (moveto_mod && (node->module != moveto_mod)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005497 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005498 }
5499
5500 /* context check */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005501 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (node->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005502 return LY_EINVAL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005503 } else if (set->context_op && (node->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && (node != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005504 return LY_EINVAL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005505 }
5506
5507 /* name check */
Michal Vasko61ac2f62020-05-25 12:39:51 +02005508 if (node_name && strcmp(node_name, "*") && (node->name != node_name)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005509 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005510 }
5511
5512 /* match */
5513 return LY_SUCCESS;
5514}
5515
5516/**
Michal Vaskod3678892020-05-21 10:06:58 +02005517 * @brief Move context @p set to a node. Result is LYXP_SET_NODE_SET (or LYXP_SET_EMPTY). Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005518 *
5519 * @param[in,out] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005520 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005521 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vaskocdad7122020-11-09 21:04:44 +01005522 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005523 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5524 */
5525static LY_ERR
Michal Vaskocdad7122020-11-09 21:04:44 +01005526moveto_node(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005527{
Michal Vasko230cf972021-12-02 12:31:00 +01005528 LY_ERR r, rc = LY_SUCCESS;
Michal Vaskodb08ce52021-10-06 08:57:49 +02005529 const struct lyd_node *siblings, *sub;
Michal Vasko230cf972021-12-02 12:31:00 +01005530 struct lyxp_set result;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005531
aPiecek8b0cc152021-05-31 16:40:31 +02005532 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005533 return LY_SUCCESS;
5534 }
5535
5536 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005537 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005538 return LY_EVALID;
5539 }
5540
Michal Vasko230cf972021-12-02 12:31:00 +01005541 /* init result set */
5542 set_init(&result, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005543
Michal Vasko230cf972021-12-02 12:31:00 +01005544 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005545 if ((set->val.nodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.nodes[i].type == LYXP_NODE_ROOT)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005546 assert(!set->val.nodes[i].node);
Michal Vaskod3678892020-05-21 10:06:58 +02005547
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005548 /* search in all the trees */
Michal Vaskod3678892020-05-21 10:06:58 +02005549 siblings = set->tree;
Michal Vasko5bfd4be2020-06-23 13:26:19 +02005550 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005551 /* search in children */
Michal Vaskodb08ce52021-10-06 08:57:49 +02005552 siblings = lyd_child(set->val.nodes[i].node);
Michal Vaskod3678892020-05-21 10:06:58 +02005553 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005554
Michal Vaskod3678892020-05-21 10:06:58 +02005555 for (sub = siblings; sub; sub = sub->next) {
Michal Vasko230cf972021-12-02 12:31:00 +01005556 r = moveto_node_check(sub, set, ncname, moveto_mod, options);
5557 if (r == LY_SUCCESS) {
5558 /* matching node */
5559 set_insert_node(&result, sub, 0, LYXP_NODE_ELEM, result.used);
5560 } else if (r == LY_EINCOMPLETE) {
5561 rc = r;
5562 goto cleanup;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005563 }
5564 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005565 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005566
Michal Vasko230cf972021-12-02 12:31:00 +01005567 /* move result to the set */
5568 lyxp_set_free_content(set);
5569 *set = result;
5570 result.type = LYXP_SET_NUMBER;
5571 assert(!set_sort(set));
5572
5573cleanup:
5574 lyxp_set_free_content(&result);
5575 return rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005576}
5577
5578/**
Michal Vaskod3678892020-05-21 10:06:58 +02005579 * @brief Move context @p set to a node using hashes. Result is LYXP_SET_NODE_SET (or LYXP_SET_EMPTY).
5580 * Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005581 *
5582 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005583 * @param[in] scnode Matching node schema.
Michal Vasko004d3152020-06-11 19:59:22 +02005584 * @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 +01005585 * @param[in] options XPath options.
Michal Vaskod3678892020-05-21 10:06:58 +02005586 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5587 */
5588static LY_ERR
Michal Vaskocdad7122020-11-09 21:04:44 +01005589moveto_node_hash(struct lyxp_set *set, const struct lysc_node *scnode, const struct ly_path_predicate *predicates,
5590 uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02005591{
Michal Vasko004d3152020-06-11 19:59:22 +02005592 LY_ERR ret = LY_SUCCESS;
Michal Vaskod3678892020-05-21 10:06:58 +02005593 uint32_t i;
Michal Vaskod3678892020-05-21 10:06:58 +02005594 const struct lyd_node *siblings;
Michal Vasko230cf972021-12-02 12:31:00 +01005595 struct lyxp_set result;
Michal Vasko004d3152020-06-11 19:59:22 +02005596 struct lyd_node *sub, *inst = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02005597
Michal Vasko004d3152020-06-11 19:59:22 +02005598 assert(scnode && (!(scnode->nodetype & (LYS_LIST | LYS_LEAFLIST)) || predicates));
Michal Vaskod3678892020-05-21 10:06:58 +02005599
Michal Vasko50aaa072021-12-02 13:11:56 +01005600 /* init result set */
5601 set_init(&result, set);
5602
aPiecek8b0cc152021-05-31 16:40:31 +02005603 if (options & LYXP_SKIP_EXPR) {
Michal Vasko004d3152020-06-11 19:59:22 +02005604 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005605 }
5606
5607 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005608 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko004d3152020-06-11 19:59:22 +02005609 ret = LY_EVALID;
5610 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005611 }
5612
5613 /* context check for all the nodes since we have the schema node */
5614 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) {
5615 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02005616 goto cleanup;
Michal Vasko69730152020-10-09 16:30:07 +02005617 } else if (set->context_op && (scnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) &&
5618 (scnode != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005619 lyxp_set_free_content(set);
5620 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +02005621 }
5622
5623 /* create specific data instance if needed */
5624 if (scnode->nodetype == LYS_LIST) {
5625 LY_CHECK_GOTO(ret = lyd_create_list(scnode, predicates, &inst), cleanup);
5626 } else if (scnode->nodetype == LYS_LEAFLIST) {
5627 LY_CHECK_GOTO(ret = lyd_create_term2(scnode, &predicates[0].value, &inst), cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02005628 }
5629
Michal Vasko230cf972021-12-02 12:31:00 +01005630 for (i = 0; i < set->used; ++i) {
Michal Vaskod3678892020-05-21 10:06:58 +02005631 siblings = NULL;
5632
5633 if ((set->val.nodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.nodes[i].type == LYXP_NODE_ROOT)) {
5634 assert(!set->val.nodes[i].node);
5635
5636 /* search in all the trees */
5637 siblings = set->tree;
5638 } else if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
5639 /* search in children */
Radek Krejcia1c1e542020-09-29 16:06:52 +02005640 siblings = lyd_child(set->val.nodes[i].node);
Michal Vaskod3678892020-05-21 10:06:58 +02005641 }
5642
5643 /* find the node using hashes */
Michal Vasko004d3152020-06-11 19:59:22 +02005644 if (inst) {
5645 lyd_find_sibling_first(siblings, inst, &sub);
Michal Vaskod3678892020-05-21 10:06:58 +02005646 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02005647 lyd_find_sibling_val(siblings, scnode, NULL, 0, &sub);
Michal Vaskod3678892020-05-21 10:06:58 +02005648 }
5649
5650 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01005651 if (!(options & LYXP_IGNORE_WHEN) && sub && lysc_has_when(sub->schema) && !(sub->flags & LYD_WHEN_TRUE)) {
Michal Vasko004d3152020-06-11 19:59:22 +02005652 ret = LY_EINCOMPLETE;
5653 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005654 }
5655
5656 if (sub) {
5657 /* pos filled later */
Michal Vasko230cf972021-12-02 12:31:00 +01005658 set_insert_node(&result, sub, 0, LYXP_NODE_ELEM, result.used);
Michal Vaskod3678892020-05-21 10:06:58 +02005659 }
5660 }
5661
Michal Vasko230cf972021-12-02 12:31:00 +01005662 /* move result to the set */
5663 lyxp_set_free_content(set);
5664 *set = result;
5665 result.type = LYXP_SET_NUMBER;
5666 assert(!set_sort(set));
5667
Michal Vasko004d3152020-06-11 19:59:22 +02005668cleanup:
Michal Vasko230cf972021-12-02 12:31:00 +01005669 lyxp_set_free_content(&result);
Michal Vasko004d3152020-06-11 19:59:22 +02005670 lyd_free_tree(inst);
5671 return ret;
Michal Vaskod3678892020-05-21 10:06:58 +02005672}
5673
5674/**
5675 * @brief Move context @p set to a schema node. Result is LYXP_SET_SCNODE_SET (or LYXP_SET_EMPTY).
5676 *
5677 * @param[in,out] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005678 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005679 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005680 * @param[in] options XPath options.
5681 * @return LY_ERR
5682 */
5683static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005684moveto_scnode(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005685{
Radek Krejci857189e2020-09-01 13:26:36 +02005686 ly_bool temp_ctx = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005687 uint32_t getnext_opts;
5688 uint32_t orig_used, i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005689 uint32_t mod_idx;
Michal Vasko519fd602020-05-26 12:17:39 +02005690 const struct lysc_node *iter, *start_parent;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005691 const struct lys_module *mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005692
aPiecek8b0cc152021-05-31 16:40:31 +02005693 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005694 return LY_SUCCESS;
5695 }
5696
5697 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005698 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005699 return LY_EVALID;
5700 }
5701
Michal Vaskocafad9d2019-11-07 15:20:03 +01005702 /* getnext opts */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01005703 getnext_opts = 0;
Michal Vaskocafad9d2019-11-07 15:20:03 +01005704 if (options & LYXP_SCNODE_OUTPUT) {
5705 getnext_opts |= LYS_GETNEXT_OUTPUT;
5706 }
5707
Michal Vasko03ff5a72019-09-11 13:49:33 +02005708 orig_used = set->used;
5709 for (i = 0; i < orig_used; ++i) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005710 uint32_t idx;
5711
Radek Krejcif13b87b2020-12-01 22:02:17 +01005712 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
5713 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01005714 continue;
5715 }
5716
5717 /* remember context node */
Radek Krejcif13b87b2020-12-01 22:02:17 +01005718 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vaskoec4df482019-12-16 10:02:18 +01005719 } else {
Michal Vasko1a09b212021-05-06 13:00:10 +02005720 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005721 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005722
5723 start_parent = set->val.scnodes[i].scnode;
5724
5725 if ((set->val.scnodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.scnodes[i].type == LYXP_NODE_ROOT)) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005726 /* it can actually be in any module, it's all <running>, and even if it's moveto_mod (if set),
Michal Vasko9e9f26d2020-10-12 16:31:33 +02005727 * it can be in a top-level augment (the root node itself is useless in this case) */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005728 mod_idx = 0;
Michal Vaskoa51ef072021-07-02 10:40:30 +02005729 while ((mod = ly_ctx_get_module_iter(set->ctx, &mod_idx))) {
Michal Vasko519fd602020-05-26 12:17:39 +02005730 iter = NULL;
Michal Vasko919954a2021-07-26 09:21:42 +02005731 /* module may not be implemented or not compiled yet */
5732 while (mod->compiled && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005733 if (!moveto_scnode_check(iter, NULL, set, ncname, moveto_mod)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005734 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, &idx));
5735
Michal Vasko03ff5a72019-09-11 13:49:33 +02005736 /* we need to prevent these nodes from being considered in this moveto */
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005737 if ((idx < orig_used) && (idx > i)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005738 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005739 temp_ctx = 1;
5740 }
5741 }
5742 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005743 }
5744
Michal Vasko519fd602020-05-26 12:17:39 +02005745 } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
5746 iter = NULL;
5747 while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005748 if (!moveto_scnode_check(iter, start_parent, set, ncname, moveto_mod)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005749 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, &idx));
5750
5751 if ((idx < orig_used) && (idx > i)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005752 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005753 temp_ctx = 1;
5754 }
5755 }
5756 }
5757 }
5758 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005759
5760 /* correct temporary in_ctx values */
5761 if (temp_ctx) {
5762 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005763 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_NEW_CTX) {
5764 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005765 }
5766 }
5767 }
5768
5769 return LY_SUCCESS;
5770}
5771
5772/**
Michal Vaskod3678892020-05-21 10:06:58 +02005773 * @brief Move context @p set to a node and all its descendants. Result is LYXP_SET_NODE_SET (or LYXP_SET_EMPTY).
Michal Vasko03ff5a72019-09-11 13:49:33 +02005774 * Context position aware.
5775 *
5776 * @param[in] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005777 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005778 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vaskocdad7122020-11-09 21:04:44 +01005779 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005780 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5781 */
5782static LY_ERR
Michal Vaskocdad7122020-11-09 21:04:44 +01005783moveto_node_alldesc(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005784{
5785 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005786 const struct lyd_node *next, *elem, *start;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005787 struct lyxp_set ret_set;
5788 LY_ERR rc;
5789
aPiecek8b0cc152021-05-31 16:40:31 +02005790 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005791 return LY_SUCCESS;
5792 }
5793
5794 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005795 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005796 return LY_EVALID;
5797 }
5798
Michal Vasko9f96a052020-03-10 09:41:45 +01005799 /* replace the original nodes (and throws away all text and meta nodes, root is replaced by a child) */
Michal Vaskocdad7122020-11-09 21:04:44 +01005800 rc = moveto_node(set, NULL, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005801 LY_CHECK_RET(rc);
5802
Michal Vasko6346ece2019-09-24 13:12:53 +02005803 /* this loop traverses all the nodes in the set and adds/keeps only those that match qname */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005804 set_init(&ret_set, set);
5805 for (i = 0; i < set->used; ++i) {
5806
5807 /* TREE DFS */
5808 start = set->val.nodes[i].node;
5809 for (elem = next = start; elem; elem = next) {
Michal Vaskodb08ce52021-10-06 08:57:49 +02005810 rc = moveto_node_check(elem, set, ncname, moveto_mod, options);
Michal Vasko6346ece2019-09-24 13:12:53 +02005811 if (!rc) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005812 /* add matching node into result set */
5813 set_insert_node(&ret_set, elem, 0, LYXP_NODE_ELEM, ret_set.used);
5814 if (set_dup_node_check(set, elem, LYXP_NODE_ELEM, i)) {
5815 /* the node is a duplicate, we'll process it later in the set */
5816 goto skip_children;
5817 }
Michal Vasko6346ece2019-09-24 13:12:53 +02005818 } else if (rc == LY_EINCOMPLETE) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005819 return rc;
5820 } else if (rc == LY_EINVAL) {
5821 goto skip_children;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005822 }
5823
5824 /* TREE DFS NEXT ELEM */
5825 /* select element for the next run - children first */
Radek Krejcia1c1e542020-09-29 16:06:52 +02005826 next = lyd_child(elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005827 if (!next) {
5828skip_children:
5829 /* no children, so try siblings, but only if it's not the start,
5830 * that is considered to be the root and it's siblings are not traversed */
5831 if (elem != start) {
5832 next = elem->next;
5833 } else {
5834 break;
5835 }
5836 }
5837 while (!next) {
5838 /* no siblings, go back through the parents */
Michal Vasko9e685082021-01-29 14:49:09 +01005839 if (lyd_parent(elem) == start) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005840 /* we are done, no next element to process */
5841 break;
5842 }
5843 /* parent is already processed, go to its sibling */
Michal Vasko9e685082021-01-29 14:49:09 +01005844 elem = lyd_parent(elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005845 next = elem->next;
5846 }
5847 }
5848 }
5849
5850 /* make the temporary set the current one */
5851 ret_set.ctx_pos = set->ctx_pos;
5852 ret_set.ctx_size = set->ctx_size;
Michal Vaskod3678892020-05-21 10:06:58 +02005853 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005854 memcpy(set, &ret_set, sizeof *set);
5855
5856 return LY_SUCCESS;
5857}
5858
5859/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02005860 * @brief Move context @p set to a schema node and all its descendants. Result is LYXP_SET_NODE_SET.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005861 *
5862 * @param[in] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005863 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005864 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005865 * @param[in] options XPath options.
5866 * @return LY_ERR
5867 */
5868static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005869moveto_scnode_alldesc(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005870{
Radek Krejci1deb5be2020-08-26 16:43:36 +02005871 uint32_t i, orig_used;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005872 const struct lysc_node *next, *elem, *start;
Michal Vasko6346ece2019-09-24 13:12:53 +02005873 LY_ERR rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005874
aPiecek8b0cc152021-05-31 16:40:31 +02005875 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005876 return LY_SUCCESS;
5877 }
5878
5879 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005880 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005881 return LY_EVALID;
5882 }
5883
Michal Vasko03ff5a72019-09-11 13:49:33 +02005884 orig_used = set->used;
5885 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005886 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
5887 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01005888 continue;
5889 }
5890
5891 /* remember context node */
Radek Krejcif13b87b2020-12-01 22:02:17 +01005892 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vaskoec4df482019-12-16 10:02:18 +01005893 } else {
Michal Vasko1a09b212021-05-06 13:00:10 +02005894 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005895 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005896
5897 /* TREE DFS */
5898 start = set->val.scnodes[i].scnode;
5899 for (elem = next = start; elem; elem = next) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005900 if ((elem == start) || (elem->nodetype & (LYS_CHOICE | LYS_CASE))) {
5901 /* schema-only nodes, skip root */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005902 goto next_iter;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005903 }
5904
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005905 rc = moveto_scnode_check(elem, start, set, ncname, moveto_mod);
Michal Vasko6346ece2019-09-24 13:12:53 +02005906 if (!rc) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005907 uint32_t idx;
5908
5909 if (lyxp_set_scnode_contains(set, elem, LYXP_NODE_ELEM, i, &idx)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005910 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005911 if ((uint32_t)idx > i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005912 /* we will process it later in the set */
5913 goto skip_children;
5914 }
5915 } else {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005916 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, elem, LYXP_NODE_ELEM, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005917 }
Michal Vasko6346ece2019-09-24 13:12:53 +02005918 } else if (rc == LY_EINVAL) {
5919 goto skip_children;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005920 }
5921
5922next_iter:
5923 /* TREE DFS NEXT ELEM */
5924 /* select element for the next run - children first */
Michal Vasko544e58a2021-01-28 14:33:41 +01005925 next = lysc_node_child(elem);
5926 if (next && (next->nodetype == LYS_INPUT) && (options & LYXP_SCNODE_OUTPUT)) {
5927 next = next->next;
5928 } else if (next && (next->nodetype == LYS_OUTPUT) && !(options & LYXP_SCNODE_OUTPUT)) {
5929 next = next->next;
5930 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005931 if (!next) {
5932skip_children:
5933 /* no children, so try siblings, but only if it's not the start,
5934 * that is considered to be the root and it's siblings are not traversed */
5935 if (elem != start) {
5936 next = elem->next;
5937 } else {
5938 break;
5939 }
5940 }
5941 while (!next) {
5942 /* no siblings, go back through the parents */
5943 if (elem->parent == start) {
5944 /* we are done, no next element to process */
5945 break;
5946 }
5947 /* parent is already processed, go to its sibling */
5948 elem = elem->parent;
5949 next = elem->next;
5950 }
5951 }
5952 }
5953
5954 return LY_SUCCESS;
5955}
5956
5957/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02005958 * @brief Move context @p set to an attribute. Result is LYXP_SET_NODE_SET.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005959 * Indirectly context position aware.
5960 *
5961 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005962 * @param[in] mod Matching metadata module, NULL for any.
5963 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
aPiecek8b0cc152021-05-31 16:40:31 +02005964 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005965 * @return LY_ERR
5966 */
5967static LY_ERR
aPiecek8b0cc152021-05-31 16:40:31 +02005968moveto_attr(struct lyxp_set *set, const struct lys_module *mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005969{
Michal Vasko9f96a052020-03-10 09:41:45 +01005970 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005971
aPiecek8b0cc152021-05-31 16:40:31 +02005972 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005973 return LY_SUCCESS;
5974 }
5975
5976 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005977 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005978 return LY_EVALID;
5979 }
5980
Radek Krejci1deb5be2020-08-26 16:43:36 +02005981 for (uint32_t i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02005982 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005983
5984 /* only attributes of an elem (not dummy) can be in the result, skip all the rest;
5985 * our attributes are always qualified */
Michal Vasko5c4e5892019-11-14 12:31:38 +01005986 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01005987 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005988
5989 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02005990 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005991 continue;
5992 }
5993
Michal Vaskod3678892020-05-21 10:06:58 +02005994 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005995 /* match */
5996 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01005997 set->val.meta[i].meta = sub;
5998 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005999 /* pos does not change */
6000 replaced = 1;
6001 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01006002 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 +02006003 }
6004 ++i;
6005 }
6006 }
6007 }
6008
6009 if (!replaced) {
6010 /* no match */
6011 set_remove_node(set, i);
6012 }
6013 }
6014
6015 return LY_SUCCESS;
6016}
6017
6018/**
6019 * @brief Move context @p set1 to union with @p set2. @p set2 is emptied afterwards.
Michal Vasko61ac2f62020-05-25 12:39:51 +02006020 * Result is LYXP_SET_NODE_SET. Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006021 *
6022 * @param[in,out] set1 Set to use for the result.
6023 * @param[in] set2 Set that is copied to @p set1.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006024 * @return LY_ERR
6025 */
6026static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006027moveto_union(struct lyxp_set *set1, struct lyxp_set *set2)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006028{
6029 LY_ERR rc;
6030
Michal Vaskod3678892020-05-21 10:06:58 +02006031 if ((set1->type != LYXP_SET_NODE_SET) || (set2->type != LYXP_SET_NODE_SET)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006032 LOGVAL(set1->ctx, LY_VCODE_XP_INOP_2, "union", print_set_type(set1), print_set_type(set2));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006033 return LY_EVALID;
6034 }
6035
6036 /* set2 is empty or both set1 and set2 */
Michal Vaskod3678892020-05-21 10:06:58 +02006037 if (!set2->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006038 return LY_SUCCESS;
6039 }
6040
Michal Vaskod3678892020-05-21 10:06:58 +02006041 if (!set1->used) {
aPiecekadc1e4f2021-10-07 11:15:12 +02006042 /* release hidden allocated data (lyxp_set.size) */
6043 lyxp_set_free_content(set1);
6044 /* direct copying of the entire structure */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006045 memcpy(set1, set2, sizeof *set1);
6046 /* dynamic memory belongs to set1 now, do not free */
Michal Vaskod3678892020-05-21 10:06:58 +02006047 memset(set2, 0, sizeof *set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006048 return LY_SUCCESS;
6049 }
6050
6051 /* we assume sets are sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006052 assert(!set_sort(set1) && !set_sort(set2));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006053
6054 /* sort, remove duplicates */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006055 rc = set_sorted_merge(set1, set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006056 LY_CHECK_RET(rc);
6057
6058 /* final set must be sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006059 assert(!set_sort(set1));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006060
6061 return LY_SUCCESS;
6062}
6063
6064/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02006065 * @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 +02006066 * Context position aware.
6067 *
6068 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02006069 * @param[in] mod Matching metadata module, NULL for any.
6070 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
Michal Vaskocdad7122020-11-09 21:04:44 +01006071 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006072 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6073 */
6074static int
Michal Vaskocdad7122020-11-09 21:04:44 +01006075moveto_attr_alldesc(struct lyxp_set *set, const struct lys_module *mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006076{
Michal Vasko9f96a052020-03-10 09:41:45 +01006077 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006078 struct lyxp_set *set_all_desc = NULL;
6079 LY_ERR rc;
6080
aPiecek8b0cc152021-05-31 16:40:31 +02006081 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006082 return LY_SUCCESS;
6083 }
6084
6085 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006086 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006087 return LY_EVALID;
6088 }
6089
Michal Vasko03ff5a72019-09-11 13:49:33 +02006090 /* can be optimized similarly to moveto_node_alldesc() and save considerable amount of memory,
6091 * but it likely won't be used much, so it's a waste of time */
6092 /* copy the context */
6093 set_all_desc = set_copy(set);
6094 /* get all descendant nodes (the original context nodes are removed) */
Michal Vaskocdad7122020-11-09 21:04:44 +01006095 rc = moveto_node_alldesc(set_all_desc, NULL, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006096 if (rc != LY_SUCCESS) {
6097 lyxp_set_free(set_all_desc);
6098 return rc;
6099 }
6100 /* prepend the original context nodes */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006101 rc = moveto_union(set, set_all_desc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006102 if (rc != LY_SUCCESS) {
6103 lyxp_set_free(set_all_desc);
6104 return rc;
6105 }
6106 lyxp_set_free(set_all_desc);
6107
Radek Krejci1deb5be2020-08-26 16:43:36 +02006108 for (uint32_t i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02006109 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006110
6111 /* only attributes of an elem can be in the result, skip all the rest,
6112 * we have all attributes qualified in lyd tree */
6113 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01006114 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006115 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02006116 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006117 continue;
6118 }
6119
Michal Vaskod3678892020-05-21 10:06:58 +02006120 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006121 /* match */
6122 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01006123 set->val.meta[i].meta = sub;
6124 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006125 /* pos does not change */
6126 replaced = 1;
6127 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01006128 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 +02006129 }
6130 ++i;
6131 }
6132 }
6133 }
6134
6135 if (!replaced) {
6136 /* no match */
6137 set_remove_node(set, i);
6138 }
6139 }
6140
6141 return LY_SUCCESS;
6142}
6143
6144/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02006145 * @brief Move context @p set to self and al chilren, recursively. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET.
6146 * Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006147 *
6148 * @param[in] parent Current parent.
6149 * @param[in] parent_pos Position of @p parent.
6150 * @param[in] parent_type Node type of @p parent.
6151 * @param[in,out] to_set Set to use.
6152 * @param[in] dup_check_set Set for checking duplicities.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006153 * @param[in] options XPath options.
6154 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6155 */
6156static LY_ERR
6157moveto_self_add_children_r(const struct lyd_node *parent, uint32_t parent_pos, enum lyxp_node_type parent_type,
Radek Krejci1deb5be2020-08-26 16:43:36 +02006158 struct lyxp_set *to_set, const struct lyxp_set *dup_check_set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006159{
Michal Vasko61ac2f62020-05-25 12:39:51 +02006160 const struct lyd_node *iter, *first;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006161 LY_ERR rc;
6162
6163 switch (parent_type) {
6164 case LYXP_NODE_ROOT:
6165 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko61ac2f62020-05-25 12:39:51 +02006166 assert(!parent);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006167
Michal Vasko61ac2f62020-05-25 12:39:51 +02006168 /* add all top-level nodes as elements */
6169 first = to_set->tree;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006170 break;
6171 case LYXP_NODE_ELEM:
Michal Vasko61ac2f62020-05-25 12:39:51 +02006172 /* add just the text node of this term element node */
6173 if (parent->schema->nodetype & (LYD_NODE_TERM | LYD_NODE_ANY)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006174 if (!set_dup_node_check(dup_check_set, parent, LYXP_NODE_TEXT, -1)) {
6175 set_insert_node(to_set, parent, parent_pos, LYXP_NODE_TEXT, to_set->used);
6176 }
Michal Vasko61ac2f62020-05-25 12:39:51 +02006177 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006178 }
Michal Vasko61ac2f62020-05-25 12:39:51 +02006179
6180 /* add all the children of this node */
Radek Krejcia1c1e542020-09-29 16:06:52 +02006181 first = lyd_child(parent);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006182 break;
6183 default:
6184 LOGINT_RET(parent->schema->module->ctx);
6185 }
6186
Michal Vasko61ac2f62020-05-25 12:39:51 +02006187 /* add all top-level nodes as elements */
6188 LY_LIST_FOR(first, iter) {
6189 /* context check */
6190 if ((parent_type == LYXP_NODE_ROOT_CONFIG) && (iter->schema->flags & LYS_CONFIG_R)) {
6191 continue;
6192 }
6193
6194 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01006195 if (!(options & LYXP_IGNORE_WHEN) && lysc_has_when(iter->schema) && !(iter->flags & LYD_WHEN_TRUE)) {
Michal Vasko61ac2f62020-05-25 12:39:51 +02006196 return LY_EINCOMPLETE;
6197 }
6198
6199 if (!set_dup_node_check(dup_check_set, iter, LYXP_NODE_ELEM, -1)) {
6200 set_insert_node(to_set, iter, 0, LYXP_NODE_ELEM, to_set->used);
6201
6202 /* also add all the children of this node, recursively */
6203 rc = moveto_self_add_children_r(iter, 0, LYXP_NODE_ELEM, to_set, dup_check_set, options);
6204 LY_CHECK_RET(rc);
6205 }
6206 }
6207
Michal Vasko03ff5a72019-09-11 13:49:33 +02006208 return LY_SUCCESS;
6209}
6210
6211/**
6212 * @brief Move context @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET
6213 * (or LYXP_SET_EMPTY). Context position aware.
6214 *
6215 * @param[in,out] set Set to use.
6216 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6217 * @param[in] options XPath options.
6218 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6219 */
6220static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006221moveto_self(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006222{
Michal Vasko03ff5a72019-09-11 13:49:33 +02006223 struct lyxp_set ret_set;
6224 LY_ERR rc;
6225
aPiecek8b0cc152021-05-31 16:40:31 +02006226 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006227 return LY_SUCCESS;
6228 }
6229
6230 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006231 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006232 return LY_EVALID;
6233 }
6234
6235 /* nothing to do */
6236 if (!all_desc) {
6237 return LY_SUCCESS;
6238 }
6239
Michal Vasko03ff5a72019-09-11 13:49:33 +02006240 /* add all the children, they get added recursively */
6241 set_init(&ret_set, set);
Radek Krejci1deb5be2020-08-26 16:43:36 +02006242 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006243 /* copy the current node to tmp */
6244 set_insert_node(&ret_set, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, ret_set.used);
6245
6246 /* do not touch attributes and text nodes */
Michal Vasko9f96a052020-03-10 09:41:45 +01006247 if ((set->val.nodes[i].type == LYXP_NODE_TEXT) || (set->val.nodes[i].type == LYXP_NODE_META)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006248 continue;
6249 }
6250
Michal Vasko03ff5a72019-09-11 13:49:33 +02006251 /* add all the children */
6252 rc = moveto_self_add_children_r(set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, &ret_set,
Michal Vasko69730152020-10-09 16:30:07 +02006253 set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006254 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006255 lyxp_set_free_content(&ret_set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006256 return rc;
6257 }
6258 }
6259
6260 /* use the temporary set as the current one */
6261 ret_set.ctx_pos = set->ctx_pos;
6262 ret_set.ctx_size = set->ctx_size;
Michal Vaskod3678892020-05-21 10:06:58 +02006263 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006264 memcpy(set, &ret_set, sizeof *set);
6265
6266 return LY_SUCCESS;
6267}
6268
6269/**
6270 * @brief Move context schema @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_SCNODE_SET
6271 * (or LYXP_SET_EMPTY).
6272 *
6273 * @param[in,out] set Set to use.
6274 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6275 * @param[in] options XPath options.
6276 * @return LY_ERR
6277 */
6278static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006279moveto_scnode_self(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006280{
Radek Krejci1deb5be2020-08-26 16:43:36 +02006281 uint32_t getnext_opts;
6282 uint32_t mod_idx;
Michal Vasko519fd602020-05-26 12:17:39 +02006283 const struct lysc_node *iter, *start_parent;
6284 const struct lys_module *mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006285
aPiecek8b0cc152021-05-31 16:40:31 +02006286 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006287 return LY_SUCCESS;
6288 }
6289
6290 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006291 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006292 return LY_EVALID;
6293 }
6294
Michal Vasko519fd602020-05-26 12:17:39 +02006295 /* getnext opts */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01006296 getnext_opts = 0;
Michal Vasko519fd602020-05-26 12:17:39 +02006297 if (options & LYXP_SCNODE_OUTPUT) {
6298 getnext_opts |= LYS_GETNEXT_OUTPUT;
6299 }
6300
6301 /* add all the children, recursively as they are being added into the same set */
Radek Krejci1deb5be2020-08-26 16:43:36 +02006302 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vaskoe657f962020-12-10 12:19:48 +01006303 if (!all_desc) {
6304 /* traverse the start node */
6305 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START) {
6306 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
6307 }
6308 continue;
6309 }
6310
Radek Krejcif13b87b2020-12-01 22:02:17 +01006311 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
6312 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006313 continue;
6314 }
6315
Michal Vasko519fd602020-05-26 12:17:39 +02006316 /* remember context node */
Radek Krejcif13b87b2020-12-01 22:02:17 +01006317 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vasko519fd602020-05-26 12:17:39 +02006318 } else {
Michal Vasko1a09b212021-05-06 13:00:10 +02006319 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006320 }
6321
Michal Vasko519fd602020-05-26 12:17:39 +02006322 start_parent = set->val.scnodes[i].scnode;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006323
Michal Vasko519fd602020-05-26 12:17:39 +02006324 if ((set->val.scnodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.scnodes[i].type == LYXP_NODE_ROOT)) {
6325 /* it can actually be in any module, it's all <running> */
6326 mod_idx = 0;
Michal Vaskoa51ef072021-07-02 10:40:30 +02006327 while ((mod = ly_ctx_get_module_iter(set->ctx, &mod_idx))) {
Michal Vasko519fd602020-05-26 12:17:39 +02006328 iter = NULL;
6329 /* module may not be implemented */
6330 while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) {
6331 /* context check */
6332 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (iter->flags & LYS_CONFIG_R)) {
6333 continue;
6334 }
6335
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006336 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, NULL));
Michal Vasko519fd602020-05-26 12:17:39 +02006337 /* throw away the insert index, we want to consider that node again, recursively */
6338 }
6339 }
6340
6341 } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
6342 iter = NULL;
6343 while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006344 /* context check */
Michal Vasko519fd602020-05-26 12:17:39 +02006345 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (iter->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006346 continue;
6347 }
6348
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006349 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006350 }
6351 }
6352 }
6353
6354 return LY_SUCCESS;
6355}
6356
6357/**
6358 * @brief Move context @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_NODE_SET
6359 * (or LYXP_SET_EMPTY). Context position aware.
6360 *
6361 * @param[in] set Set to use.
6362 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6363 * @param[in] options XPath options.
6364 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6365 */
6366static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006367moveto_parent(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006368{
Michal Vasko230cf972021-12-02 12:31:00 +01006369 LY_ERR rc = LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006370 struct lyd_node *node, *new_node;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006371 enum lyxp_node_type new_type;
Michal Vasko230cf972021-12-02 12:31:00 +01006372 struct lyxp_set result;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006373
aPiecek8b0cc152021-05-31 16:40:31 +02006374 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006375 return LY_SUCCESS;
6376 }
6377
6378 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006379 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006380 return LY_EVALID;
6381 }
6382
6383 if (all_desc) {
6384 /* <path>//.. == <path>//./.. */
6385 rc = moveto_self(set, 1, options);
6386 LY_CHECK_RET(rc);
6387 }
6388
Michal Vasko230cf972021-12-02 12:31:00 +01006389 /* init result set */
6390 set_init(&result, set);
6391
Radek Krejci1deb5be2020-08-26 16:43:36 +02006392 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006393 node = set->val.nodes[i].node;
6394
6395 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9e685082021-01-29 14:49:09 +01006396 new_node = lyd_parent(node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006397 } else if (set->val.nodes[i].type == LYXP_NODE_TEXT) {
6398 new_node = node;
Michal Vasko9f96a052020-03-10 09:41:45 +01006399 } else if (set->val.nodes[i].type == LYXP_NODE_META) {
6400 new_node = set->val.meta[i].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006401 if (!new_node) {
Michal Vasko230cf972021-12-02 12:31:00 +01006402 LOGINT(set->ctx);
6403 rc = LY_EINT;
6404 goto cleanup;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006405 }
6406 } else {
6407 /* root does not have a parent */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006408 continue;
6409 }
6410
Michal Vaskoa1424542019-11-14 16:08:52 +01006411 /* when check */
Michal Vasko230cf972021-12-02 12:31:00 +01006412 if (!(options & LYXP_IGNORE_WHEN) && new_node && lysc_has_when(new_node->schema) &&
6413 !(new_node->flags & LYD_WHEN_TRUE)) {
6414 rc = LY_EINCOMPLETE;
6415 goto cleanup;
Michal Vaskoa1424542019-11-14 16:08:52 +01006416 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006417
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006418 if (!new_node) {
Radek Krejcif6a11002020-08-21 13:29:07 +02006419 /* node already there can also be the root */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006420 new_type = set->root_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006421 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02006422 /* node has a standard parent (it can equal the root, it's not the root yet since they are fake) */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006423 new_type = LYXP_NODE_ELEM;
6424 }
6425
Michal Vasko230cf972021-12-02 12:31:00 +01006426 /* check for duplicates, several nodes may have the same parent */
6427 if (!set_dup_node_check(&result, new_node, new_type, -1)) {
6428 set_insert_node(&result, new_node, 0, new_type, result.used);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006429 }
6430 }
6431
Michal Vasko230cf972021-12-02 12:31:00 +01006432 /* move result to the set */
6433 lyxp_set_free_content(set);
6434 *set = result;
6435 result.type = LYXP_SET_NUMBER;
6436 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006437
Michal Vasko230cf972021-12-02 12:31:00 +01006438cleanup:
6439 lyxp_set_free_content(&result);
6440 return rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006441}
6442
6443/**
6444 * @brief Move context schema @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_SCNODE_SET
6445 * (or LYXP_SET_EMPTY).
6446 *
6447 * @param[in] set Set to use.
6448 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6449 * @param[in] options XPath options.
6450 * @return LY_ERR
6451 */
6452static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006453moveto_scnode_parent(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006454{
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006455 uint32_t i, orig_used, idx;
Radek Krejci857189e2020-09-01 13:26:36 +02006456 ly_bool temp_ctx = 0;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006457 const struct lysc_node *node, *new_node;
6458 enum lyxp_node_type new_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006459
aPiecek8b0cc152021-05-31 16:40:31 +02006460 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006461 return LY_SUCCESS;
6462 }
6463
6464 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006465 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006466 return LY_EVALID;
6467 }
6468
6469 if (all_desc) {
6470 /* <path>//.. == <path>//./.. */
Radek Krejci1deb5be2020-08-26 16:43:36 +02006471 LY_CHECK_RET(moveto_scnode_self(set, 1, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006472 }
6473
Michal Vasko03ff5a72019-09-11 13:49:33 +02006474 orig_used = set->used;
6475 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006476 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
6477 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006478 continue;
6479 }
6480
6481 /* remember context node */
Radek Krejcif13b87b2020-12-01 22:02:17 +01006482 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vaskoec4df482019-12-16 10:02:18 +01006483 } else {
Michal Vasko1a09b212021-05-06 13:00:10 +02006484 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006485 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006486
6487 node = set->val.scnodes[i].scnode;
6488
6489 if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
Michal Vaskod3678892020-05-21 10:06:58 +02006490 new_node = lysc_data_parent(node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006491 } else {
6492 /* root does not have a parent */
6493 continue;
6494 }
6495
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006496 if (!new_node) {
Radek Krejcif6a11002020-08-21 13:29:07 +02006497 /* node has no parent */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006498 new_type = set->root_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006499
Michal Vasko03ff5a72019-09-11 13:49:33 +02006500 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02006501 /* node has a standard parent (it can equal the root, it's not the root yet since they are fake) */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006502 new_type = LYXP_NODE_ELEM;
6503 }
6504
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006505 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, new_node, new_type, &idx));
6506 if ((idx < orig_used) && (idx > i)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006507 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006508 temp_ctx = 1;
6509 }
6510 }
6511
6512 if (temp_ctx) {
6513 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006514 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_NEW_CTX) {
6515 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006516 }
6517 }
6518 }
6519
6520 return LY_SUCCESS;
6521}
6522
6523/**
6524 * @brief Move context @p set to the result of a comparison. Handles '=', '!=', '<=', '<', '>=', or '>'.
6525 * Result is LYXP_SET_BOOLEAN. Indirectly context position aware.
6526 *
6527 * @param[in,out] set1 Set to use for the result.
6528 * @param[in] set2 Set acting as the second operand for @p op.
6529 * @param[in] op Comparison operator to process.
6530 * @param[in] options XPath options.
6531 * @return LY_ERR
6532 */
6533static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02006534moveto_op_comp(struct lyxp_set *set1, struct lyxp_set *set2, const char *op, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006535{
6536 /*
6537 * NODE SET + NODE SET = NODE SET + STRING /(1 NODE SET) 2 STRING
6538 * NODE SET + STRING = STRING + STRING /1 STRING (2 STRING)
6539 * NODE SET + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6540 * NODE SET + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6541 * STRING + NODE SET = STRING + STRING /(1 STRING) 2 STRING
6542 * NUMBER + NODE SET = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6543 * BOOLEAN + NODE SET = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6544 *
6545 * '=' or '!='
6546 * BOOLEAN + BOOLEAN
6547 * BOOLEAN + STRING = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6548 * BOOLEAN + NUMBER = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6549 * STRING + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6550 * NUMBER + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6551 * NUMBER + NUMBER
6552 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6553 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6554 * STRING + STRING
6555 *
6556 * '<=', '<', '>=', '>'
6557 * NUMBER + NUMBER
6558 * BOOLEAN + BOOLEAN = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6559 * BOOLEAN + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6560 * BOOLEAN + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6561 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6562 * STRING + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6563 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6564 * NUMBER + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6565 * STRING + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6566 */
6567 struct lyxp_set iter1, iter2;
6568 int result;
6569 int64_t i;
6570 LY_ERR rc;
6571
Michal Vasko004d3152020-06-11 19:59:22 +02006572 memset(&iter1, 0, sizeof iter1);
6573 memset(&iter2, 0, sizeof iter2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006574
6575 /* iterative evaluation with node-sets */
6576 if ((set1->type == LYXP_SET_NODE_SET) || (set2->type == LYXP_SET_NODE_SET)) {
6577 if (set1->type == LYXP_SET_NODE_SET) {
6578 for (i = 0; i < set1->used; ++i) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02006579 /* cast set1 */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006580 switch (set2->type) {
6581 case LYXP_SET_NUMBER:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006582 rc = set_comp_cast(&iter1, set1, LYXP_SET_NUMBER, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006583 break;
6584 case LYXP_SET_BOOLEAN:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006585 rc = set_comp_cast(&iter1, set1, LYXP_SET_BOOLEAN, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006586 break;
6587 default:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006588 rc = set_comp_cast(&iter1, set1, LYXP_SET_STRING, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006589 break;
6590 }
6591 LY_CHECK_RET(rc);
6592
Michal Vasko4c7763f2020-07-27 17:40:37 +02006593 /* canonize set2 */
6594 LY_CHECK_ERR_RET(rc = set_comp_canonize(&iter2, set2, &set1->val.nodes[i]), lyxp_set_free_content(&iter1), rc);
6595
6596 /* compare recursively */
6597 rc = moveto_op_comp(&iter1, &iter2, op, options);
6598 lyxp_set_free_content(&iter2);
6599 LY_CHECK_ERR_RET(rc, lyxp_set_free_content(&iter1), rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006600
6601 /* lazy evaluation until true */
Michal Vasko004d3152020-06-11 19:59:22 +02006602 if (iter1.val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006603 set_fill_boolean(set1, 1);
6604 return LY_SUCCESS;
6605 }
6606 }
6607 } else {
6608 for (i = 0; i < set2->used; ++i) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02006609 /* set set2 */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006610 switch (set1->type) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02006611 case LYXP_SET_NUMBER:
6612 rc = set_comp_cast(&iter2, set2, LYXP_SET_NUMBER, i);
6613 break;
6614 case LYXP_SET_BOOLEAN:
6615 rc = set_comp_cast(&iter2, set2, LYXP_SET_BOOLEAN, i);
6616 break;
6617 default:
6618 rc = set_comp_cast(&iter2, set2, LYXP_SET_STRING, i);
6619 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006620 }
6621 LY_CHECK_RET(rc);
6622
Michal Vasko4c7763f2020-07-27 17:40:37 +02006623 /* canonize set1 */
6624 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 +02006625
Michal Vasko4c7763f2020-07-27 17:40:37 +02006626 /* compare recursively */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006627 rc = moveto_op_comp(&iter1, &iter2, op, options);
Michal Vaskod3678892020-05-21 10:06:58 +02006628 lyxp_set_free_content(&iter2);
Michal Vasko4c7763f2020-07-27 17:40:37 +02006629 LY_CHECK_ERR_RET(rc, lyxp_set_free_content(&iter1), rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006630
6631 /* lazy evaluation until true */
Michal Vasko004d3152020-06-11 19:59:22 +02006632 if (iter1.val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006633 set_fill_boolean(set1, 1);
6634 return LY_SUCCESS;
6635 }
6636 }
6637 }
6638
6639 /* false for all nodes */
6640 set_fill_boolean(set1, 0);
6641 return LY_SUCCESS;
6642 }
6643
6644 /* first convert properly */
6645 if ((op[0] == '=') || (op[0] == '!')) {
6646 if ((set1->type == LYXP_SET_BOOLEAN) || (set2->type == LYXP_SET_BOOLEAN)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006647 lyxp_set_cast(set1, LYXP_SET_BOOLEAN);
6648 lyxp_set_cast(set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006649 } else if ((set1->type == LYXP_SET_NUMBER) || (set2->type == LYXP_SET_NUMBER)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006650 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006651 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006652 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006653 LY_CHECK_RET(rc);
6654 } /* else we have 2 strings */
6655 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006656 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006657 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006658 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006659 LY_CHECK_RET(rc);
6660 }
6661
6662 assert(set1->type == set2->type);
6663
6664 /* compute result */
6665 if (op[0] == '=') {
6666 if (set1->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +02006667 result = (set1->val.bln == set2->val.bln);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006668 } else if (set1->type == LYXP_SET_NUMBER) {
6669 result = (set1->val.num == set2->val.num);
6670 } else {
6671 assert(set1->type == LYXP_SET_STRING);
Michal Vaskoac6c72f2019-11-14 16:09:34 +01006672 result = !strcmp(set1->val.str, set2->val.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006673 }
6674 } else if (op[0] == '!') {
6675 if (set1->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +02006676 result = (set1->val.bln != set2->val.bln);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006677 } else if (set1->type == LYXP_SET_NUMBER) {
6678 result = (set1->val.num != set2->val.num);
6679 } else {
6680 assert(set1->type == LYXP_SET_STRING);
Michal Vaskoc2058432020-11-06 17:26:21 +01006681 result = strcmp(set1->val.str, set2->val.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006682 }
6683 } else {
6684 assert(set1->type == LYXP_SET_NUMBER);
6685 if (op[0] == '<') {
6686 if (op[1] == '=') {
6687 result = (set1->val.num <= set2->val.num);
6688 } else {
6689 result = (set1->val.num < set2->val.num);
6690 }
6691 } else {
6692 if (op[1] == '=') {
6693 result = (set1->val.num >= set2->val.num);
6694 } else {
6695 result = (set1->val.num > set2->val.num);
6696 }
6697 }
6698 }
6699
6700 /* assign result */
6701 if (result) {
6702 set_fill_boolean(set1, 1);
6703 } else {
6704 set_fill_boolean(set1, 0);
6705 }
6706
6707 return LY_SUCCESS;
6708}
6709
6710/**
6711 * @brief Move context @p set to the result of a basic operation. Handles '+', '-', unary '-', '*', 'div',
6712 * or 'mod'. Result is LYXP_SET_NUMBER. Indirectly context position aware.
6713 *
6714 * @param[in,out] set1 Set to use for the result.
6715 * @param[in] set2 Set acting as the second operand for @p op.
6716 * @param[in] op Operator to process.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006717 * @return LY_ERR
6718 */
6719static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006720moveto_op_math(struct lyxp_set *set1, struct lyxp_set *set2, const char *op)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006721{
6722 LY_ERR rc;
6723
6724 /* unary '-' */
6725 if (!set2 && (op[0] == '-')) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006726 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006727 LY_CHECK_RET(rc);
6728 set1->val.num *= -1;
6729 lyxp_set_free(set2);
6730 return LY_SUCCESS;
6731 }
6732
6733 assert(set1 && set2);
6734
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006735 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006736 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006737 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006738 LY_CHECK_RET(rc);
6739
6740 switch (op[0]) {
6741 /* '+' */
6742 case '+':
6743 set1->val.num += set2->val.num;
6744 break;
6745
6746 /* '-' */
6747 case '-':
6748 set1->val.num -= set2->val.num;
6749 break;
6750
6751 /* '*' */
6752 case '*':
6753 set1->val.num *= set2->val.num;
6754 break;
6755
6756 /* 'div' */
6757 case 'd':
6758 set1->val.num /= set2->val.num;
6759 break;
6760
6761 /* 'mod' */
6762 case 'm':
6763 set1->val.num = ((long long)set1->val.num) % ((long long)set2->val.num);
6764 break;
6765
6766 default:
6767 LOGINT_RET(set1->ctx);
6768 }
6769
6770 return LY_SUCCESS;
6771}
6772
Michal Vasko03ff5a72019-09-11 13:49:33 +02006773/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02006774 * @brief Evaluate Predicate. Logs directly on error.
6775 *
Michal Vaskod3678892020-05-21 10:06:58 +02006776 * [9] Predicate ::= '[' Expr ']'
Michal Vasko03ff5a72019-09-11 13:49:33 +02006777 *
6778 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02006779 * @param[in] tok_idx Position in the expression @p exp.
aPiecek8b0cc152021-05-31 16:40:31 +02006780 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006781 * @param[in] options XPath options.
6782 * @param[in] parent_pos_pred Whether parent predicate was a positional one.
6783 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6784 */
6785static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02006786eval_predicate(const struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set, uint32_t options, ly_bool parent_pos_pred)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006787{
6788 LY_ERR rc;
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01006789 uint16_t orig_exp;
6790 uint32_t i, orig_pos, orig_size;
Michal Vasko5c4e5892019-11-14 12:31:38 +01006791 int32_t pred_in_ctx;
aPiecekfff4dca2021-10-07 10:59:53 +02006792 struct lyxp_set set2 = {0};
Michal Vasko03ff5a72019-09-11 13:49:33 +02006793 struct lyd_node *orig_parent;
6794
6795 /* '[' */
aPiecek8b0cc152021-05-31 16:40:31 +02006796 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02006797 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02006798 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006799
aPiecek8b0cc152021-05-31 16:40:31 +02006800 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006801only_parse:
aPiecek8b0cc152021-05-31 16:40:31 +02006802 rc = eval_expr_select(exp, tok_idx, 0, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006803 LY_CHECK_RET(rc);
6804 } else if (set->type == LYXP_SET_NODE_SET) {
6805 /* 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 +01006806 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006807
6808 /* empty set, nothing to evaluate */
6809 if (!set->used) {
6810 goto only_parse;
6811 }
6812
Michal Vasko004d3152020-06-11 19:59:22 +02006813 orig_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006814 orig_pos = 0;
6815 orig_size = set->used;
6816 orig_parent = NULL;
Michal Vasko39dbf352020-05-21 10:08:59 +02006817 for (i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006818 set_init(&set2, set);
6819 set_insert_node(&set2, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, 0);
6820 /* remember the node context position for position() and context size for last(),
6821 * predicates should always be evaluated with respect to the child axis (since we do
6822 * not support explicit axes) so we assign positions based on their parents */
Michal Vasko9e685082021-01-29 14:49:09 +01006823 if (parent_pos_pred && (lyd_parent(set->val.nodes[i].node) != orig_parent)) {
6824 orig_parent = lyd_parent(set->val.nodes[i].node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006825 orig_pos = 1;
6826 } else {
6827 ++orig_pos;
6828 }
6829
6830 set2.ctx_pos = orig_pos;
6831 set2.ctx_size = orig_size;
Michal Vasko004d3152020-06-11 19:59:22 +02006832 *tok_idx = orig_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006833
Michal Vasko004d3152020-06-11 19:59:22 +02006834 rc = eval_expr_select(exp, tok_idx, 0, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006835 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006836 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006837 return rc;
6838 }
6839
6840 /* number is a position */
6841 if (set2.type == LYXP_SET_NUMBER) {
6842 if ((long long)set2.val.num == orig_pos) {
6843 set2.val.num = 1;
6844 } else {
6845 set2.val.num = 0;
6846 }
6847 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006848 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006849
6850 /* predicate satisfied or not? */
Michal Vasko004d3152020-06-11 19:59:22 +02006851 if (!set2.val.bln) {
Michal Vasko2caefc12019-11-14 16:07:56 +01006852 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006853 }
6854 }
Michal Vasko2caefc12019-11-14 16:07:56 +01006855 set_remove_nodes_none(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006856
6857 } else if (set->type == LYXP_SET_SCNODE_SET) {
6858 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006859 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006860 /* there is a currently-valid node */
6861 break;
6862 }
6863 }
6864 /* empty set, nothing to evaluate */
6865 if (i == set->used) {
6866 goto only_parse;
6867 }
6868
Michal Vasko004d3152020-06-11 19:59:22 +02006869 orig_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006870
Michal Vasko03ff5a72019-09-11 13:49:33 +02006871 /* set special in_ctx to all the valid snodes */
6872 pred_in_ctx = set_scnode_new_in_ctx(set);
6873
6874 /* use the valid snodes one-by-one */
6875 for (i = 0; i < set->used; ++i) {
6876 if (set->val.scnodes[i].in_ctx != pred_in_ctx) {
6877 continue;
6878 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01006879 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006880
Michal Vasko004d3152020-06-11 19:59:22 +02006881 *tok_idx = orig_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006882
Michal Vasko004d3152020-06-11 19:59:22 +02006883 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006884 LY_CHECK_RET(rc);
6885
6886 set->val.scnodes[i].in_ctx = pred_in_ctx;
6887 }
6888
6889 /* restore the state as it was before the predicate */
6890 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006891 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko1a09b212021-05-06 13:00:10 +02006892 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006893 } else if (set->val.scnodes[i].in_ctx == pred_in_ctx) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006894 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006895 }
6896 }
6897
6898 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02006899 set2.type = LYXP_SET_NODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006900 set_fill_set(&set2, set);
6901
Michal Vasko004d3152020-06-11 19:59:22 +02006902 rc = eval_expr_select(exp, tok_idx, 0, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006903 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006904 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006905 return rc;
6906 }
6907
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006908 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +02006909 if (!set2.val.bln) {
Michal Vaskod3678892020-05-21 10:06:58 +02006910 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006911 }
Michal Vaskod3678892020-05-21 10:06:58 +02006912 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006913 }
6914
6915 /* ']' */
Michal Vasko004d3152020-06-11 19:59:22 +02006916 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_BRACK2);
aPiecek8b0cc152021-05-31 16:40:31 +02006917 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02006918 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02006919 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006920
6921 return LY_SUCCESS;
6922}
6923
6924/**
Michal Vaskod3678892020-05-21 10:06:58 +02006925 * @brief Evaluate Literal. Logs directly on error.
6926 *
6927 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02006928 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02006929 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
6930 */
6931static void
Michal Vasko40308e72020-10-20 16:38:40 +02006932eval_literal(const struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set)
Michal Vaskod3678892020-05-21 10:06:58 +02006933{
6934 if (set) {
Michal Vasko004d3152020-06-11 19:59:22 +02006935 if (exp->tok_len[*tok_idx] == 2) {
Michal Vaskod3678892020-05-21 10:06:58 +02006936 set_fill_string(set, "", 0);
6937 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02006938 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 +02006939 }
6940 }
6941 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02006942 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02006943 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02006944}
6945
6946/**
Michal Vasko004d3152020-06-11 19:59:22 +02006947 * @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 +02006948 *
Michal Vasko004d3152020-06-11 19:59:22 +02006949 * @param[in] exp Full parsed XPath expression.
6950 * @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 +02006951 * @param[in] ctx_node Found schema node as the context for the predicate.
6952 * @param[in] cur_mod Current module for the expression.
6953 * @param[in] cur_node Current (original context) node.
Michal Vasko004d3152020-06-11 19:59:22 +02006954 * @param[in] format Format of any prefixes in key names/values.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006955 * @param[in] prefix_data Format-specific prefix data (see ::ly_resolve_prefix).
Michal Vasko004d3152020-06-11 19:59:22 +02006956 * @param[out] predicates Parsed predicates.
6957 * @param[out] pred_type Type of @p predicates.
6958 * @return LY_SUCCESS on success,
6959 * @return LY_ERR on any error.
Michal Vaskod3678892020-05-21 10:06:58 +02006960 */
6961static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02006962eval_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 +02006963 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 +02006964 struct ly_path_predicate **predicates, enum ly_path_pred_type *pred_type)
Michal Vaskod3678892020-05-21 10:06:58 +02006965{
Michal Vasko004d3152020-06-11 19:59:22 +02006966 LY_ERR ret = LY_SUCCESS;
6967 uint16_t key_count, e_idx, pred_idx = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02006968 const struct lysc_node *key;
Michal Vasko004d3152020-06-11 19:59:22 +02006969 size_t pred_len;
Radek Krejci1deb5be2020-08-26 16:43:36 +02006970 uint32_t prev_lo;
Michal Vasko004d3152020-06-11 19:59:22 +02006971 struct lyxp_expr *exp2 = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02006972
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006973 assert(ctx_node->nodetype & (LYS_LIST | LYS_LEAFLIST));
Michal Vaskod3678892020-05-21 10:06:58 +02006974
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006975 if (ctx_node->nodetype == LYS_LIST) {
Michal Vasko004d3152020-06-11 19:59:22 +02006976 /* get key count */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006977 if (ctx_node->flags & LYS_KEYLESS) {
Michal Vasko004d3152020-06-11 19:59:22 +02006978 return LY_EINVAL;
6979 }
Michal Vasko544e58a2021-01-28 14:33:41 +01006980 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 +02006981 assert(key_count);
Michal Vaskod3678892020-05-21 10:06:58 +02006982
Michal Vasko004d3152020-06-11 19:59:22 +02006983 /* learn where the predicates end */
6984 e_idx = *tok_idx;
6985 while (key_count) {
6986 /* '[' */
6987 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
6988 return LY_EINVAL;
6989 }
6990 ++e_idx;
6991
Michal Vasko3354d272021-04-06 09:40:06 +02006992 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_NAMETEST)) {
6993 /* definitely not a key */
6994 return LY_EINVAL;
6995 }
6996
Michal Vasko004d3152020-06-11 19:59:22 +02006997 /* ']' */
6998 while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) {
6999 ++e_idx;
7000 }
7001 ++e_idx;
7002
7003 /* another presumably key predicate parsed */
7004 --key_count;
7005 }
Michal Vasko004d3152020-06-11 19:59:22 +02007006 } else {
7007 /* learn just where this single predicate ends */
7008 e_idx = *tok_idx;
7009
Michal Vaskod3678892020-05-21 10:06:58 +02007010 /* '[' */
Michal Vasko004d3152020-06-11 19:59:22 +02007011 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
7012 return LY_EINVAL;
7013 }
7014 ++e_idx;
Michal Vaskod3678892020-05-21 10:06:58 +02007015
Michal Vasko3354d272021-04-06 09:40:06 +02007016 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_DOT)) {
7017 /* definitely not the value */
7018 return LY_EINVAL;
7019 }
7020
Michal Vaskod3678892020-05-21 10:06:58 +02007021 /* ']' */
Michal Vasko004d3152020-06-11 19:59:22 +02007022 while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) {
7023 ++e_idx;
7024 }
7025 ++e_idx;
Michal Vaskod3678892020-05-21 10:06:58 +02007026 }
7027
Michal Vasko004d3152020-06-11 19:59:22 +02007028 /* get the joined length of all the keys predicates/of the single leaf-list predicate */
7029 pred_len = (exp->tok_pos[e_idx - 1] + exp->tok_len[e_idx - 1]) - exp->tok_pos[*tok_idx];
7030
7031 /* turn logging off */
7032 prev_lo = ly_log_options(0);
7033
7034 /* parse the predicate(s) */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007035 LY_CHECK_GOTO(ret = ly_path_parse_predicate(ctx_node->module->ctx, ctx_node, exp->expr + exp->tok_pos[*tok_idx],
7036 pred_len, LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &exp2), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02007037
7038 /* compile */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007039 ret = ly_path_compile_predicate(ctx_node->module->ctx, cur_node, cur_mod, ctx_node, exp2, &pred_idx, format,
7040 prefix_data, predicates, pred_type);
Michal Vasko004d3152020-06-11 19:59:22 +02007041 LY_CHECK_GOTO(ret, cleanup);
7042
7043 /* success, the predicate must include all the needed information for hash-based search */
7044 *tok_idx = e_idx;
7045
7046cleanup:
7047 ly_log_options(prev_lo);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007048 lyxp_expr_free(ctx_node->module->ctx, exp2);
Michal Vasko004d3152020-06-11 19:59:22 +02007049 return ret;
Michal Vaskod3678892020-05-21 10:06:58 +02007050}
7051
7052/**
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007053 * @brief Search for/check the next schema node that could be the only matching schema node meaning the
7054 * data node(s) could be found using a single hash-based search.
7055 *
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007056 * @param[in] ctx libyang context.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007057 * @param[in] node Next context node to check.
7058 * @param[in] name Expected node name.
7059 * @param[in] name_len Length of @p name.
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007060 * @param[in] moveto_mod Expected node module, can be NULL for JSON format with no prefix.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007061 * @param[in] root_type XPath root type.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007062 * @param[in] format Prefix format.
7063 * @param[in,out] found Previously found node, is updated.
7064 * @return LY_SUCCESS on success,
7065 * @return LY_ENOT if the whole check failed and hashes cannot be used.
7066 */
7067static LY_ERR
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007068eval_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 +02007069 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 +02007070 const struct lysc_node **found)
7071{
7072 const struct lysc_node *scnode;
7073 const struct lys_module *mod;
7074 uint32_t idx = 0;
7075
Radek Krejci8df109d2021-04-23 12:19:08 +02007076 assert((format == LY_VALUE_JSON) || moveto_mod);
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007077
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007078continue_search:
Michal Vasko7d1d0912020-10-16 08:38:30 +02007079 scnode = NULL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007080 if (!node) {
Radek Krejci8df109d2021-04-23 12:19:08 +02007081 if ((format == LY_VALUE_JSON) && !moveto_mod) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007082 /* search all modules for a single match */
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007083 while ((mod = ly_ctx_get_module_iter(ctx, &idx))) {
Michal Vasko35a3b1d2021-07-14 09:34:16 +02007084 if (!mod->implemented) {
7085 continue;
7086 }
7087
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007088 scnode = lys_find_child(NULL, mod, name, name_len, 0, 0);
7089 if (scnode) {
7090 /* we have found a match */
7091 break;
7092 }
7093 }
7094
Michal Vasko7d1d0912020-10-16 08:38:30 +02007095 if (!scnode) {
7096 /* all modules searched */
7097 idx = 0;
7098 }
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007099 } else {
7100 /* search in top-level */
7101 scnode = lys_find_child(NULL, moveto_mod, name, name_len, 0, 0);
7102 }
7103 } else if (!*found || (lysc_data_parent(*found) != node->schema)) {
Radek Krejci8df109d2021-04-23 12:19:08 +02007104 if ((format == LY_VALUE_JSON) && !moveto_mod) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007105 /* we must adjust the module to inherit the one from the context node */
7106 moveto_mod = node->schema->module;
7107 }
7108
7109 /* search in children, do not repeat the same search */
7110 scnode = lys_find_child(node->schema, moveto_mod, name, name_len, 0, 0);
Michal Vasko7d1d0912020-10-16 08:38:30 +02007111 } /* else skip redundant search */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007112
7113 /* additional context check */
7114 if (scnode && (root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) {
7115 scnode = NULL;
7116 }
7117
7118 if (scnode) {
7119 if (*found) {
7120 /* we found a schema node with the same name but at different level, give up, too complicated
7121 * (more hash-based searches would be required, not supported) */
7122 return LY_ENOT;
7123 } else {
7124 /* remember the found schema node and continue to make sure it can be used */
7125 *found = scnode;
7126 }
7127 }
7128
7129 if (idx) {
7130 /* continue searching all the following models */
7131 goto continue_search;
7132 }
7133
7134 return LY_SUCCESS;
7135}
7136
7137/**
Michal Vasko4ad69e72021-10-26 16:25:55 +02007138 * @brief Generate message when no matching schema nodes were found for a path segment.
7139 *
7140 * @param[in] set XPath set.
7141 * @param[in] scparent Previous schema parent in the context, if only one.
7142 * @param[in] ncname XPath NCName being evaluated.
7143 * @param[in] ncname_len Length of @p ncname.
7144 * @param[in] expr Whole XPath expression.
7145 * @param[in] options XPath options.
7146 */
7147static void
7148eval_name_test_scnode_no_match_msg(struct lyxp_set *set, const struct lyxp_set_scnode *scparent, const char *ncname,
7149 uint16_t ncname_len, const char *expr, uint32_t options)
7150{
7151 const char *format;
7152 char *path = NULL, *ppath = NULL;
7153
7154 path = lysc_path(set->cur_scnode, LYSC_PATH_LOG, NULL, 0);
7155 if (scparent) {
7156 /* generate path for the parent */
7157 if (scparent->type == LYXP_NODE_ELEM) {
7158 ppath = lysc_path(scparent->scnode, LYSC_PATH_LOG, NULL, 0);
7159 } else if (scparent->type == LYXP_NODE_ROOT) {
7160 ppath = strdup("<root>");
7161 } else if (scparent->type == LYXP_NODE_ROOT_CONFIG) {
7162 ppath = strdup("<config-root>");
7163 }
7164 }
7165 if (ppath) {
7166 format = "Schema node \"%.*s\" for parent \"%s\" not found; in expr \"%.*s\" with context node \"%s\".";
7167 if (options & LYXP_SCNODE_ERROR) {
7168 LOGERR(set->ctx, LY_EVALID, format, ncname_len, ncname, ppath, (ncname - expr) + ncname_len, expr, path);
7169 } else {
7170 LOGWRN(set->ctx, format, ncname_len, ncname, ppath, (ncname - expr) + ncname_len, expr, path);
7171 }
7172 } else {
7173 format = "Schema node \"%.*s\" not found; in expr \"%.*s\" with context node \"%s\".";
7174 if (options & LYXP_SCNODE_ERROR) {
7175 LOGERR(set->ctx, LY_EVALID, format, ncname_len, ncname, (ncname - expr) + ncname_len, expr, path);
7176 } else {
7177 LOGWRN(set->ctx, format, ncname_len, ncname, (ncname - expr) + ncname_len, expr, path);
7178 }
7179 }
7180 free(path);
7181 free(ppath);
7182}
7183
7184/**
Michal Vaskod3678892020-05-21 10:06:58 +02007185 * @brief Evaluate NameTest and any following Predicates. Logs directly on error.
7186 *
7187 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
7188 * [6] NodeTest ::= NameTest | NodeType '(' ')'
7189 * [7] NameTest ::= '*' | NCName ':' '*' | QName
7190 *
7191 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007192 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02007193 * @param[in] attr_axis Whether to search attributes or standard nodes.
7194 * @param[in] all_desc Whether to search all the descendants or children only.
aPiecek8b0cc152021-05-31 16:40:31 +02007195 * @param[in,out] set Context and result set.
Michal Vaskod3678892020-05-21 10:06:58 +02007196 * @param[in] options XPath options.
Michal Vaskoa036a6b2021-10-12 16:05:23 +02007197 * @return LY_ERR (LY_EINCOMPLETE on unresolved when, LY_ENOT for not found schema node)
Michal Vaskod3678892020-05-21 10:06:58 +02007198 */
7199static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007200eval_name_test_with_predicate(const struct lyxp_expr *exp, uint16_t *tok_idx, ly_bool attr_axis, ly_bool all_desc,
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007201 struct lyxp_set *set, uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02007202{
Michal Vaskoa036a6b2021-10-12 16:05:23 +02007203 LY_ERR rc = LY_SUCCESS, r;
Michal Vasko004d3152020-06-11 19:59:22 +02007204 const char *ncname, *ncname_dict = NULL;
7205 uint16_t ncname_len;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007206 const struct lys_module *moveto_mod = NULL;
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007207 const struct lysc_node *scnode = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02007208 struct ly_path_predicate *predicates = NULL;
7209 enum ly_path_pred_type pred_type = 0;
Michal Vaskoa036a6b2021-10-12 16:05:23 +02007210 int scnode_skip_pred = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02007211
aPiecek8b0cc152021-05-31 16:40:31 +02007212 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007213 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007214 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007215
aPiecek8b0cc152021-05-31 16:40:31 +02007216 if (options & LYXP_SKIP_EXPR) {
Michal Vaskod3678892020-05-21 10:06:58 +02007217 goto moveto;
7218 }
7219
Michal Vasko004d3152020-06-11 19:59:22 +02007220 ncname = &exp->expr[exp->tok_pos[*tok_idx - 1]];
7221 ncname_len = exp->tok_len[*tok_idx - 1];
Michal Vaskod3678892020-05-21 10:06:58 +02007222
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007223 if ((ncname[0] == '*') && (ncname_len == 1)) {
7224 /* all nodes will match */
7225 goto moveto;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007226 }
7227
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007228 /* parse (and skip) module name */
7229 rc = moveto_resolve_model(&ncname, &ncname_len, set, NULL, &moveto_mod);
Michal Vaskod3678892020-05-21 10:06:58 +02007230 LY_CHECK_GOTO(rc, cleanup);
7231
Radek Krejci8df109d2021-04-23 12:19:08 +02007232 if (((set->format == LY_VALUE_JSON) || moveto_mod) && !attr_axis && !all_desc && (set->type == LYXP_SET_NODE_SET)) {
Michal Vaskod3678892020-05-21 10:06:58 +02007233 /* find the matching schema node in some parent in the context */
Radek Krejci1deb5be2020-08-26 16:43:36 +02007234 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007235 if (eval_name_test_with_predicate_get_scnode(set->ctx, set->val.nodes[i].node, ncname, ncname_len,
7236 moveto_mod, set->root_type, set->format, &scnode)) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007237 /* check failed */
7238 scnode = NULL;
7239 break;
Michal Vaskod3678892020-05-21 10:06:58 +02007240 }
7241 }
7242
Michal Vasko004d3152020-06-11 19:59:22 +02007243 if (scnode && (scnode->nodetype & (LYS_LIST | LYS_LEAFLIST))) {
7244 /* try to create the predicates */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007245 if (eval_name_test_try_compile_predicates(exp, tok_idx, scnode, set->cur_mod, set->cur_node ?
7246 set->cur_node->schema : NULL, set->format, set->prefix_data, &predicates, &pred_type)) {
Michal Vasko004d3152020-06-11 19:59:22 +02007247 /* hashes cannot be used */
Michal Vaskod3678892020-05-21 10:06:58 +02007248 scnode = NULL;
7249 }
7250 }
7251 }
7252
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007253 if (!scnode) {
7254 /* we are not able to match based on a schema node and not all the modules match ("*"),
Michal Vasko004d3152020-06-11 19:59:22 +02007255 * use dictionary for efficient comparison */
Radek Krejci011e4aa2020-09-04 15:22:31 +02007256 LY_CHECK_GOTO(rc = lydict_insert(set->ctx, ncname, ncname_len, &ncname_dict), cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02007257 }
7258
7259moveto:
7260 /* move to the attribute(s), data node(s), or schema node(s) */
7261 if (attr_axis) {
aPiecek8b0cc152021-05-31 16:40:31 +02007262 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007263 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskod3678892020-05-21 10:06:58 +02007264 } else {
7265 if (all_desc) {
Michal Vaskocdad7122020-11-09 21:04:44 +01007266 rc = moveto_attr_alldesc(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007267 } else {
aPiecek8b0cc152021-05-31 16:40:31 +02007268 rc = moveto_attr(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007269 }
7270 LY_CHECK_GOTO(rc, cleanup);
7271 }
7272 } else {
aPiecek8b0cc152021-05-31 16:40:31 +02007273 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02007274 int64_t i;
Michal Vaskoa036a6b2021-10-12 16:05:23 +02007275 const struct lyxp_set_scnode *scparent = NULL;
Michal Vaskoa036a6b2021-10-12 16:05:23 +02007276
7277 /* remember parent if there is only one, to print in the warning */
7278 for (i = 0; i < set->used; ++i) {
7279 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
7280 if (!scparent) {
7281 /* remember the context node */
7282 scparent = &set->val.scnodes[i];
7283 } else {
7284 /* several context nodes, no reasonable error possible */
7285 scparent = NULL;
7286 break;
7287 }
7288 }
7289 }
Radek Krejci1deb5be2020-08-26 16:43:36 +02007290
Michal Vaskod3678892020-05-21 10:06:58 +02007291 if (all_desc) {
Michal Vasko004d3152020-06-11 19:59:22 +02007292 rc = moveto_scnode_alldesc(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007293 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007294 rc = moveto_scnode(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007295 }
7296 LY_CHECK_GOTO(rc, cleanup);
7297
7298 for (i = set->used - 1; i > -1; --i) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007299 if (set->val.scnodes[i].in_ctx > LYXP_SET_SCNODE_ATOM_NODE) {
Michal Vaskod3678892020-05-21 10:06:58 +02007300 break;
7301 }
7302 }
7303 if (i == -1) {
Michal Vasko4ad69e72021-10-26 16:25:55 +02007304 /* generate message */
7305 eval_name_test_scnode_no_match_msg(set, scparent, ncname, ncname_len, exp->expr, options);
7306
7307 if (options & LYXP_SCNODE_ERROR) {
7308 /* error */
7309 rc = LY_EVALID;
7310 goto cleanup;
Michal Vaskoa036a6b2021-10-12 16:05:23 +02007311 }
Michal Vaskoa036a6b2021-10-12 16:05:23 +02007312
7313 /* skip the predicates and the rest of this path to not generate invalid warnings */
7314 rc = LY_ENOT;
7315 scnode_skip_pred = 1;
Michal Vaskod3678892020-05-21 10:06:58 +02007316 }
7317 } else {
7318 if (all_desc) {
Michal Vaskocdad7122020-11-09 21:04:44 +01007319 rc = moveto_node_alldesc(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007320 } else {
7321 if (scnode) {
7322 /* we can find the nodes using hashes */
Michal Vaskocdad7122020-11-09 21:04:44 +01007323 rc = moveto_node_hash(set, scnode, predicates, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007324 } else {
Michal Vaskocdad7122020-11-09 21:04:44 +01007325 rc = moveto_node(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007326 }
7327 }
7328 LY_CHECK_GOTO(rc, cleanup);
7329 }
7330 }
7331
Michal Vaskoa036a6b2021-10-12 16:05:23 +02007332 if (scnode_skip_pred) {
7333 /* skip predicates */
7334 options |= LYXP_SKIP_EXPR;
7335 }
7336
Michal Vaskod3678892020-05-21 10:06:58 +02007337 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007338 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
Michal Vaskoa036a6b2021-10-12 16:05:23 +02007339 r = eval_predicate(exp, tok_idx, set, options, 1);
7340 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02007341 }
7342
7343cleanup:
Michal Vaskoa036a6b2021-10-12 16:05:23 +02007344 if (scnode_skip_pred) {
7345 /* restore options */
7346 options &= ~LYXP_SKIP_EXPR;
7347 }
aPiecek8b0cc152021-05-31 16:40:31 +02007348 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko004d3152020-06-11 19:59:22 +02007349 lydict_remove(set->ctx, ncname_dict);
Michal Vaskof7e16e22020-10-21 09:24:39 +02007350 ly_path_predicates_free(set->ctx, pred_type, predicates);
Michal Vaskodb51a8d2020-05-27 15:22:29 +02007351 }
Michal Vaskod3678892020-05-21 10:06:58 +02007352 return rc;
7353}
7354
7355/**
7356 * @brief Evaluate NodeType and any following Predicates. Logs directly on error.
7357 *
7358 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
7359 * [6] NodeTest ::= NameTest | NodeType '(' ')'
7360 * [8] NodeType ::= 'text' | 'node'
7361 *
7362 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007363 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02007364 * @param[in] attr_axis Whether to search attributes or standard nodes.
7365 * @param[in] all_desc Whether to search all the descendants or children only.
aPiecek8b0cc152021-05-31 16:40:31 +02007366 * @param[in,out] set Context and result set.
Michal Vaskod3678892020-05-21 10:06:58 +02007367 * @param[in] options XPath options.
7368 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7369 */
7370static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007371eval_node_type_with_predicate(const struct lyxp_expr *exp, uint16_t *tok_idx, ly_bool attr_axis, ly_bool all_desc,
Radek Krejci1deb5be2020-08-26 16:43:36 +02007372 struct lyxp_set *set, uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02007373{
7374 LY_ERR rc;
7375
7376 /* TODO */
7377 (void)attr_axis;
7378 (void)all_desc;
7379
aPiecek8b0cc152021-05-31 16:40:31 +02007380 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko004d3152020-06-11 19:59:22 +02007381 assert(exp->tok_len[*tok_idx] == 4);
Michal Vaskod3678892020-05-21 10:06:58 +02007382 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007383 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
aPiecek8b0cc152021-05-31 16:40:31 +02007384 options |= LYXP_SKIP_EXPR;
Michal Vaskod3678892020-05-21 10:06:58 +02007385 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007386 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "node", 4)) {
Michal Vaskod3678892020-05-21 10:06:58 +02007387 rc = xpath_node(NULL, 0, set, options);
7388 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007389 assert(!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "text", 4));
Michal Vaskod3678892020-05-21 10:06:58 +02007390 rc = xpath_text(NULL, 0, set, options);
7391 }
7392 LY_CHECK_RET(rc);
7393 }
7394 }
aPiecek8b0cc152021-05-31 16:40:31 +02007395 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007396 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007397 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007398
7399 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02007400 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
aPiecek8b0cc152021-05-31 16:40:31 +02007401 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007402 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007403 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007404
7405 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007406 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
aPiecek8b0cc152021-05-31 16:40:31 +02007407 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007408 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007409 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007410
7411 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007412 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7413 rc = eval_predicate(exp, tok_idx, set, options, 1);
Michal Vaskod3678892020-05-21 10:06:58 +02007414 LY_CHECK_RET(rc);
7415 }
7416
7417 return LY_SUCCESS;
7418}
7419
7420/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02007421 * @brief Evaluate RelativeLocationPath. Logs directly on error.
7422 *
7423 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
7424 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
Michal Vaskod3678892020-05-21 10:06:58 +02007425 * [6] NodeTest ::= NameTest | NodeType '(' ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02007426 *
7427 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007428 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007429 * @param[in] all_desc Whether to search all the descendants or children only.
aPiecek8b0cc152021-05-31 16:40:31 +02007430 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007431 * @param[in] options XPath options.
7432 * @return LY_ERR (YL_EINCOMPLETE on unresolved when)
7433 */
7434static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007435eval_relative_location_path(const struct lyxp_expr *exp, uint16_t *tok_idx, ly_bool all_desc, struct lyxp_set *set,
7436 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007437{
Radek Krejci857189e2020-09-01 13:26:36 +02007438 ly_bool attr_axis;
Michal Vaskoa036a6b2021-10-12 16:05:23 +02007439 LY_ERR rc = LY_SUCCESS;
7440 int scnode_skip_path = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007441
7442 goto step;
7443 do {
7444 /* evaluate '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02007445 if (exp->tok_len[*tok_idx] == 1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007446 all_desc = 0;
7447 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007448 assert(exp->tok_len[*tok_idx] == 2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007449 all_desc = 1;
7450 }
aPiecek8b0cc152021-05-31 16:40:31 +02007451 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007452 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007453 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007454
7455step:
Michal Vaskod3678892020-05-21 10:06:58 +02007456 /* evaluate abbreviated axis '@'? if any */
Michal Vasko004d3152020-06-11 19:59:22 +02007457 if (exp->tokens[*tok_idx] == LYXP_TOKEN_AT) {
Michal Vaskod3678892020-05-21 10:06:58 +02007458 attr_axis = 1;
7459
aPiecek8b0cc152021-05-31 16:40:31 +02007460 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007461 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007462 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007463 } else {
7464 attr_axis = 0;
7465 }
7466
Michal Vasko03ff5a72019-09-11 13:49:33 +02007467 /* Step */
Michal Vasko004d3152020-06-11 19:59:22 +02007468 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007469 case LYXP_TOKEN_DOT:
7470 /* evaluate '.' */
aPiecek8b0cc152021-05-31 16:40:31 +02007471 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007472 rc = moveto_scnode_self(set, all_desc, options);
7473 } else {
7474 rc = moveto_self(set, all_desc, options);
7475 }
Michal Vaskoa036a6b2021-10-12 16:05:23 +02007476 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007477 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007478 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007479 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007480 break;
7481
7482 case LYXP_TOKEN_DDOT:
7483 /* evaluate '..' */
aPiecek8b0cc152021-05-31 16:40:31 +02007484 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007485 rc = moveto_scnode_parent(set, all_desc, options);
7486 } else {
7487 rc = moveto_parent(set, all_desc, options);
7488 }
Michal Vaskoa036a6b2021-10-12 16:05:23 +02007489 LY_CHECK_GOTO(rc, cleanup);
aPiecek8b0cc152021-05-31 16:40:31 +02007490 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007491 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007492 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007493 break;
7494
Michal Vasko03ff5a72019-09-11 13:49:33 +02007495 case LYXP_TOKEN_NAMETEST:
Michal Vaskod3678892020-05-21 10:06:58 +02007496 /* evaluate NameTest Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007497 rc = eval_name_test_with_predicate(exp, tok_idx, attr_axis, all_desc, set, options);
Michal Vaskoa036a6b2021-10-12 16:05:23 +02007498 if (rc == LY_ENOT) {
7499 assert(options & LYXP_SCNODE_ALL);
7500 /* skip the rest of this path */
7501 rc = LY_SUCCESS;
7502 scnode_skip_path = 1;
7503 options |= LYXP_SKIP_EXPR;
7504 }
7505 LY_CHECK_GOTO(rc, cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02007506 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007507
Michal Vaskod3678892020-05-21 10:06:58 +02007508 case LYXP_TOKEN_NODETYPE:
7509 /* evaluate NodeType Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007510 rc = eval_node_type_with_predicate(exp, tok_idx, attr_axis, all_desc, set, options);
Michal Vaskoa036a6b2021-10-12 16:05:23 +02007511 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007512 break;
7513
7514 default:
Michal Vaskoa036a6b2021-10-12 16:05:23 +02007515 LOGINT(set->ctx);
7516 rc = LY_EINT;
7517 goto cleanup;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007518 }
Michal Vasko004d3152020-06-11 19:59:22 +02007519 } while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007520
Michal Vaskoa036a6b2021-10-12 16:05:23 +02007521cleanup:
7522 if (scnode_skip_path) {
7523 options &= ~LYXP_SKIP_EXPR;
7524 }
7525 return rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007526}
7527
7528/**
7529 * @brief Evaluate AbsoluteLocationPath. Logs directly on error.
7530 *
7531 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
7532 *
7533 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007534 * @param[in] tok_idx Position in the expression @p exp.
aPiecek8b0cc152021-05-31 16:40:31 +02007535 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007536 * @param[in] options XPath options.
7537 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7538 */
7539static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007540eval_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 +02007541{
Radek Krejci857189e2020-09-01 13:26:36 +02007542 ly_bool all_desc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007543
aPiecek8b0cc152021-05-31 16:40:31 +02007544 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007545 /* no matter what tokens follow, we need to be at the root */
Michal Vaskob0099a92020-08-31 14:55:23 +02007546 LY_CHECK_RET(moveto_root(set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007547 }
7548
7549 /* '/' RelativeLocationPath? */
Michal Vasko004d3152020-06-11 19:59:22 +02007550 if (exp->tok_len[*tok_idx] == 1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007551 /* evaluate '/' - deferred */
7552 all_desc = 0;
aPiecek8b0cc152021-05-31 16:40:31 +02007553 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007554 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007555 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007556
Michal Vasko004d3152020-06-11 19:59:22 +02007557 if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007558 return LY_SUCCESS;
7559 }
Michal Vasko004d3152020-06-11 19:59:22 +02007560 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007561 case LYXP_TOKEN_DOT:
7562 case LYXP_TOKEN_DDOT:
7563 case LYXP_TOKEN_AT:
7564 case LYXP_TOKEN_NAMETEST:
7565 case LYXP_TOKEN_NODETYPE:
Michal Vaskob0099a92020-08-31 14:55:23 +02007566 LY_CHECK_RET(eval_relative_location_path(exp, tok_idx, all_desc, set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007567 break;
7568 default:
7569 break;
7570 }
7571
Michal Vasko03ff5a72019-09-11 13:49:33 +02007572 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02007573 /* '//' RelativeLocationPath */
Michal Vasko03ff5a72019-09-11 13:49:33 +02007574 /* evaluate '//' - deferred so as not to waste memory by remembering all the nodes */
7575 all_desc = 1;
aPiecek8b0cc152021-05-31 16:40:31 +02007576 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007577 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007578 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007579
Michal Vaskob0099a92020-08-31 14:55:23 +02007580 LY_CHECK_RET(eval_relative_location_path(exp, tok_idx, all_desc, set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007581 }
7582
7583 return LY_SUCCESS;
7584}
7585
7586/**
7587 * @brief Evaluate FunctionCall. Logs directly on error.
7588 *
Michal Vaskod3678892020-05-21 10:06:58 +02007589 * [11] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02007590 *
7591 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007592 * @param[in] tok_idx Position in the expression @p exp.
aPiecek8b0cc152021-05-31 16:40:31 +02007593 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007594 * @param[in] options XPath options.
7595 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7596 */
7597static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007598eval_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 +02007599{
7600 LY_ERR rc;
Michal Vasko69730152020-10-09 16:30:07 +02007601
Radek Krejci1deb5be2020-08-26 16:43:36 +02007602 LY_ERR (*xpath_func)(struct lyxp_set **, uint16_t, struct lyxp_set *, uint32_t) = NULL;
Michal Vasko0cbf54f2019-12-16 10:01:06 +01007603 uint16_t arg_count = 0, i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007604 struct lyxp_set **args = NULL, **args_aux;
7605
aPiecek8b0cc152021-05-31 16:40:31 +02007606 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007607 /* FunctionName */
Michal Vasko004d3152020-06-11 19:59:22 +02007608 switch (exp->tok_len[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007609 case 3:
Michal Vasko004d3152020-06-11 19:59:22 +02007610 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007611 xpath_func = &xpath_not;
Michal Vasko004d3152020-06-11 19:59:22 +02007612 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007613 xpath_func = &xpath_sum;
7614 }
7615 break;
7616 case 4:
Michal Vasko004d3152020-06-11 19:59:22 +02007617 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007618 xpath_func = &xpath_lang;
Michal Vasko004d3152020-06-11 19:59:22 +02007619 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007620 xpath_func = &xpath_last;
Michal Vasko004d3152020-06-11 19:59:22 +02007621 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007622 xpath_func = &xpath_name;
Michal Vasko004d3152020-06-11 19:59:22 +02007623 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007624 xpath_func = &xpath_true;
7625 }
7626 break;
7627 case 5:
Michal Vasko004d3152020-06-11 19:59:22 +02007628 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007629 xpath_func = &xpath_count;
Michal Vasko004d3152020-06-11 19:59:22 +02007630 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007631 xpath_func = &xpath_false;
Michal Vasko004d3152020-06-11 19:59:22 +02007632 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007633 xpath_func = &xpath_floor;
Michal Vasko004d3152020-06-11 19:59:22 +02007634 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007635 xpath_func = &xpath_round;
Michal Vasko004d3152020-06-11 19:59:22 +02007636 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007637 xpath_func = &xpath_deref;
7638 }
7639 break;
7640 case 6:
Michal Vasko004d3152020-06-11 19:59:22 +02007641 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007642 xpath_func = &xpath_concat;
Michal Vasko004d3152020-06-11 19:59:22 +02007643 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007644 xpath_func = &xpath_number;
Michal Vasko004d3152020-06-11 19:59:22 +02007645 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007646 xpath_func = &xpath_string;
7647 }
7648 break;
7649 case 7:
Michal Vasko004d3152020-06-11 19:59:22 +02007650 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007651 xpath_func = &xpath_boolean;
Michal Vasko004d3152020-06-11 19:59:22 +02007652 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007653 xpath_func = &xpath_ceiling;
Michal Vasko004d3152020-06-11 19:59:22 +02007654 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007655 xpath_func = &xpath_current;
7656 }
7657 break;
7658 case 8:
Michal Vasko004d3152020-06-11 19:59:22 +02007659 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007660 xpath_func = &xpath_contains;
Michal Vasko004d3152020-06-11 19:59:22 +02007661 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007662 xpath_func = &xpath_position;
Michal Vasko004d3152020-06-11 19:59:22 +02007663 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "re-match", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007664 xpath_func = &xpath_re_match;
7665 }
7666 break;
7667 case 9:
Michal Vasko004d3152020-06-11 19:59:22 +02007668 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007669 xpath_func = &xpath_substring;
Michal Vasko004d3152020-06-11 19:59:22 +02007670 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007671 xpath_func = &xpath_translate;
7672 }
7673 break;
7674 case 10:
Michal Vasko004d3152020-06-11 19:59:22 +02007675 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007676 xpath_func = &xpath_local_name;
Michal Vasko004d3152020-06-11 19:59:22 +02007677 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007678 xpath_func = &xpath_enum_value;
Michal Vasko004d3152020-06-11 19:59:22 +02007679 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007680 xpath_func = &xpath_bit_is_set;
7681 }
7682 break;
7683 case 11:
Michal Vasko004d3152020-06-11 19:59:22 +02007684 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007685 xpath_func = &xpath_starts_with;
7686 }
7687 break;
7688 case 12:
Michal Vasko004d3152020-06-11 19:59:22 +02007689 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007690 xpath_func = &xpath_derived_from;
7691 }
7692 break;
7693 case 13:
Michal Vasko004d3152020-06-11 19:59:22 +02007694 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007695 xpath_func = &xpath_namespace_uri;
Michal Vasko004d3152020-06-11 19:59:22 +02007696 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string-length", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007697 xpath_func = &xpath_string_length;
7698 }
7699 break;
7700 case 15:
Michal Vasko004d3152020-06-11 19:59:22 +02007701 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007702 xpath_func = &xpath_normalize_space;
Michal Vasko004d3152020-06-11 19:59:22 +02007703 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-after", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007704 xpath_func = &xpath_substring_after;
7705 }
7706 break;
7707 case 16:
Michal Vasko004d3152020-06-11 19:59:22 +02007708 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007709 xpath_func = &xpath_substring_before;
7710 }
7711 break;
7712 case 20:
Michal Vasko004d3152020-06-11 19:59:22 +02007713 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from-or-self", 20)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007714 xpath_func = &xpath_derived_from_or_self;
7715 }
7716 break;
7717 }
7718
7719 if (!xpath_func) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01007720 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 +02007721 return LY_EVALID;
7722 }
7723 }
7724
aPiecek8b0cc152021-05-31 16:40:31 +02007725 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007726 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007727 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007728
7729 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02007730 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
aPiecek8b0cc152021-05-31 16:40:31 +02007731 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007732 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007733 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007734
7735 /* ( Expr ( ',' Expr )* )? */
Michal Vasko004d3152020-06-11 19:59:22 +02007736 if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) {
aPiecek8b0cc152021-05-31 16:40:31 +02007737 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007738 args = malloc(sizeof *args);
7739 LY_CHECK_ERR_GOTO(!args, LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
7740 arg_count = 1;
7741 args[0] = set_copy(set);
7742 if (!args[0]) {
7743 rc = LY_EMEM;
7744 goto cleanup;
7745 }
7746
Michal Vasko004d3152020-06-11 19:59:22 +02007747 rc = eval_expr_select(exp, tok_idx, 0, args[0], options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007748 LY_CHECK_GOTO(rc, cleanup);
7749 } else {
aPiecek8b0cc152021-05-31 16:40:31 +02007750 rc = eval_expr_select(exp, tok_idx, 0, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007751 LY_CHECK_GOTO(rc, cleanup);
7752 }
7753 }
Michal Vasko004d3152020-06-11 19:59:22 +02007754 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) {
aPiecek8b0cc152021-05-31 16:40:31 +02007755 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007756 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007757 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007758
aPiecek8b0cc152021-05-31 16:40:31 +02007759 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007760 ++arg_count;
7761 args_aux = realloc(args, arg_count * sizeof *args);
7762 LY_CHECK_ERR_GOTO(!args_aux, arg_count--; LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
7763 args = args_aux;
7764 args[arg_count - 1] = set_copy(set);
7765 if (!args[arg_count - 1]) {
7766 rc = LY_EMEM;
7767 goto cleanup;
7768 }
7769
Michal Vasko004d3152020-06-11 19:59:22 +02007770 rc = eval_expr_select(exp, tok_idx, 0, args[arg_count - 1], options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007771 LY_CHECK_GOTO(rc, cleanup);
7772 } else {
aPiecek8b0cc152021-05-31 16:40:31 +02007773 rc = eval_expr_select(exp, tok_idx, 0, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007774 LY_CHECK_GOTO(rc, cleanup);
7775 }
7776 }
7777
7778 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007779 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
aPiecek8b0cc152021-05-31 16:40:31 +02007780 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007781 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007782 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007783
aPiecek8b0cc152021-05-31 16:40:31 +02007784 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007785 /* evaluate function */
7786 rc = xpath_func(args, arg_count, set, options);
7787
7788 if (options & LYXP_SCNODE_ALL) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007789 /* merge all nodes from arg evaluations */
7790 for (i = 0; i < arg_count; ++i) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007791 set_scnode_clear_ctx(args[i], LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007792 lyxp_set_scnode_merge(set, args[i]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007793 }
7794 }
7795 } else {
7796 rc = LY_SUCCESS;
7797 }
7798
7799cleanup:
7800 for (i = 0; i < arg_count; ++i) {
7801 lyxp_set_free(args[i]);
7802 }
7803 free(args);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007804 return rc;
7805}
7806
7807/**
7808 * @brief Evaluate Number. Logs directly on error.
7809 *
7810 * @param[in] ctx Context for errors.
7811 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007812 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007813 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7814 * @return LY_ERR
7815 */
7816static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007817eval_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 +02007818{
7819 long double num;
7820 char *endptr;
7821
7822 if (set) {
7823 errno = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02007824 num = strtold(&exp->expr[exp->tok_pos[*tok_idx]], &endptr);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007825 if (errno) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01007826 LOGVAL(ctx, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]);
7827 LOGVAL(ctx, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double (%s).",
Michal Vasko69730152020-10-09 16:30:07 +02007828 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]], strerror(errno));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007829 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02007830 } else if (endptr - &exp->expr[exp->tok_pos[*tok_idx]] != exp->tok_len[*tok_idx]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01007831 LOGVAL(ctx, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]);
7832 LOGVAL(ctx, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double.",
Michal Vasko69730152020-10-09 16:30:07 +02007833 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007834 return LY_EVALID;
7835 }
7836
7837 set_fill_number(set, num);
7838 }
7839
7840 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007841 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007842 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007843 return LY_SUCCESS;
7844}
7845
aPiecekdf23eee2021-10-07 12:21:50 +02007846LY_ERR
7847lyxp_vars_find(struct lyxp_var *vars, const char *name, size_t name_len, struct lyxp_var **var)
7848{
7849 LY_ERR ret = LY_ENOTFOUND;
7850 LY_ARRAY_COUNT_TYPE u;
7851
7852 assert(vars && name);
7853
7854 name_len = name_len ? name_len : strlen(name);
7855
7856 LY_ARRAY_FOR(vars, u) {
7857 if (!strncmp(vars[u].name, name, name_len)) {
7858 ret = LY_SUCCESS;
7859 break;
7860 }
7861 }
7862
7863 if (var && !ret) {
7864 *var = &vars[u];
7865 }
7866
7867 return ret;
7868}
7869
Michal Vasko03ff5a72019-09-11 13:49:33 +02007870/**
aPiecekfba75362021-10-07 12:39:48 +02007871 * @brief Evaluate VariableReference.
7872 *
7873 * @param[in] exp Parsed XPath expression.
7874 * @param[in] tok_idx Position in the expression @p exp.
7875 * @param[in] vars [Sized array](@ref sizedarrays) of XPath variables.
7876 * @param[in,out] set Context and result set.
7877 * @param[in] options XPath options.
7878 * @return LY_ERR value.
7879 */
7880static LY_ERR
7881eval_variable_reference(const struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set, uint32_t options)
7882{
7883 LY_ERR ret;
7884 const char *name;
7885 struct lyxp_var *var;
7886 const struct lyxp_var *vars;
7887 struct lyxp_expr *tokens = NULL;
7888 uint16_t token_index;
7889
7890 vars = set->vars;
7891
7892 /* Find out the name and value of the variable. */
7893 name = &exp->expr[exp->tok_pos[*tok_idx]];
7894 ret = lyxp_vars_find((struct lyxp_var *)vars, name, exp->tok_len[*tok_idx], &var);
7895 LY_CHECK_ERR_RET(ret, LOGERR(set->ctx, ret,
7896 "XPath variable \"%s\" not defined.", name), ret);
7897
7898 /* Parse value. */
7899 ret = lyxp_expr_parse(set->ctx, var->value, 0, 1, &tokens);
7900 LY_CHECK_GOTO(ret, cleanup);
7901
7902 /* Evaluate value. */
7903 token_index = 0;
7904 ret = eval_expr_select(tokens, &token_index, 0, set, options);
7905 LY_CHECK_GOTO(ret, cleanup);
7906
7907cleanup:
7908 lyxp_expr_free(set->ctx, tokens);
7909
7910 return ret;
7911}
7912
7913/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02007914 * @brief Evaluate PathExpr. Logs directly on error.
7915 *
Michal Vaskod3678892020-05-21 10:06:58 +02007916 * [12] PathExpr ::= LocationPath | PrimaryExpr Predicate*
Michal Vasko03ff5a72019-09-11 13:49:33 +02007917 * | PrimaryExpr Predicate* '/' RelativeLocationPath
7918 * | PrimaryExpr Predicate* '//' RelativeLocationPath
7919 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
aPiecekfba75362021-10-07 12:39:48 +02007920 * [10] PrimaryExpr ::= VariableReference | '(' Expr ')' | Literal | Number | FunctionCall
Michal Vasko03ff5a72019-09-11 13:49:33 +02007921 *
7922 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007923 * @param[in] tok_idx Position in the expression @p exp.
aPiecek8b0cc152021-05-31 16:40:31 +02007924 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007925 * @param[in] options XPath options.
7926 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7927 */
7928static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007929eval_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 +02007930{
Radek Krejci857189e2020-09-01 13:26:36 +02007931 ly_bool all_desc, parent_pos_pred;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007932 LY_ERR rc;
7933
Michal Vasko004d3152020-06-11 19:59:22 +02007934 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007935 case LYXP_TOKEN_PAR1:
7936 /* '(' Expr ')' */
7937
7938 /* '(' */
aPiecek8b0cc152021-05-31 16:40:31 +02007939 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007940 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007941 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007942
7943 /* Expr */
Michal Vasko004d3152020-06-11 19:59:22 +02007944 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007945 LY_CHECK_RET(rc);
7946
7947 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007948 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
aPiecek8b0cc152021-05-31 16:40:31 +02007949 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007950 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007951 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007952
7953 parent_pos_pred = 0;
7954 goto predicate;
7955
7956 case LYXP_TOKEN_DOT:
7957 case LYXP_TOKEN_DDOT:
7958 case LYXP_TOKEN_AT:
7959 case LYXP_TOKEN_NAMETEST:
7960 case LYXP_TOKEN_NODETYPE:
7961 /* RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007962 rc = eval_relative_location_path(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007963 LY_CHECK_RET(rc);
7964 break;
7965
aPiecekfba75362021-10-07 12:39:48 +02007966 case LYXP_TOKEN_VARREF:
7967 /* VariableReference */
7968 rc = eval_variable_reference(exp, tok_idx, set, options);
7969 LY_CHECK_RET(rc);
7970 ++(*tok_idx);
7971
7972 parent_pos_pred = 1;
7973 goto predicate;
7974
Michal Vasko03ff5a72019-09-11 13:49:33 +02007975 case LYXP_TOKEN_FUNCNAME:
7976 /* FunctionCall */
aPiecek8b0cc152021-05-31 16:40:31 +02007977 rc = eval_function_call(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007978 LY_CHECK_RET(rc);
7979
7980 parent_pos_pred = 1;
7981 goto predicate;
7982
Michal Vasko3e48bf32020-06-01 08:39:07 +02007983 case LYXP_TOKEN_OPER_PATH:
7984 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +02007985 /* AbsoluteLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007986 rc = eval_absolute_location_path(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007987 LY_CHECK_RET(rc);
7988 break;
7989
7990 case LYXP_TOKEN_LITERAL:
7991 /* Literal */
aPiecek8b0cc152021-05-31 16:40:31 +02007992 if ((options & LYXP_SKIP_EXPR) || (options & LYXP_SCNODE_ALL)) {
7993 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007994 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007995 }
Michal Vasko004d3152020-06-11 19:59:22 +02007996 eval_literal(exp, tok_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007997 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007998 eval_literal(exp, tok_idx, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007999 }
8000
8001 parent_pos_pred = 1;
8002 goto predicate;
8003
8004 case LYXP_TOKEN_NUMBER:
8005 /* Number */
aPiecek8b0cc152021-05-31 16:40:31 +02008006 if ((options & LYXP_SKIP_EXPR) || (options & LYXP_SCNODE_ALL)) {
8007 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008008 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008009 }
Michal Vasko004d3152020-06-11 19:59:22 +02008010 rc = eval_number(NULL, exp, tok_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008011 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02008012 rc = eval_number(set->ctx, exp, tok_idx, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008013 }
8014 LY_CHECK_RET(rc);
8015
8016 parent_pos_pred = 1;
8017 goto predicate;
8018
8019 default:
Radek Krejci2efc45b2020-12-22 16:25:44 +01008020 LOGVAL(set->ctx, LY_VCODE_XP_INTOK, lyxp_print_token(exp->tokens[*tok_idx]), &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008021 return LY_EVALID;
8022 }
8023
8024 return LY_SUCCESS;
8025
8026predicate:
8027 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02008028 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
8029 rc = eval_predicate(exp, tok_idx, set, options, parent_pos_pred);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008030 LY_CHECK_RET(rc);
8031 }
8032
8033 /* ('/' or '//') RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02008034 if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008035
8036 /* evaluate '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02008037 if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008038 all_desc = 0;
8039 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008040 all_desc = 1;
8041 }
8042
aPiecek8b0cc152021-05-31 16:40:31 +02008043 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02008044 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008045 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008046
Michal Vasko004d3152020-06-11 19:59:22 +02008047 rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008048 LY_CHECK_RET(rc);
8049 }
8050
8051 return LY_SUCCESS;
8052}
8053
8054/**
8055 * @brief Evaluate UnionExpr. Logs directly on error.
8056 *
Michal Vaskod3678892020-05-21 10:06:58 +02008057 * [20] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008058 *
8059 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008060 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008061 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008062 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008063 * @param[in] options XPath options.
8064 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8065 */
8066static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008067eval_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 +02008068{
8069 LY_ERR rc = LY_SUCCESS;
8070 struct lyxp_set orig_set, set2;
8071 uint16_t i;
8072
8073 assert(repeat);
8074
8075 set_init(&orig_set, set);
8076 set_init(&set2, set);
8077
8078 set_fill_set(&orig_set, set);
8079
Michal Vasko004d3152020-06-11 19:59:22 +02008080 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008081 LY_CHECK_GOTO(rc, cleanup);
8082
8083 /* ('|' PathExpr)* */
8084 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008085 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_UNI);
aPiecek8b0cc152021-05-31 16:40:31 +02008086 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02008087 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008088 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008089
aPiecek8b0cc152021-05-31 16:40:31 +02008090 if (options & LYXP_SKIP_EXPR) {
8091 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008092 LY_CHECK_GOTO(rc, cleanup);
8093 continue;
8094 }
8095
8096 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008097 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008098 LY_CHECK_GOTO(rc, cleanup);
8099
8100 /* eval */
8101 if (options & LYXP_SCNODE_ALL) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01008102 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008103 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008104 rc = moveto_union(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008105 LY_CHECK_GOTO(rc, cleanup);
8106 }
8107 }
8108
8109cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008110 lyxp_set_free_content(&orig_set);
8111 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008112 return rc;
8113}
8114
8115/**
8116 * @brief Evaluate UnaryExpr. Logs directly on error.
8117 *
Michal Vaskod3678892020-05-21 10:06:58 +02008118 * [19] UnaryExpr ::= UnionExpr | '-' UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008119 *
8120 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008121 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008122 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008123 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008124 * @param[in] options XPath options.
8125 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8126 */
8127static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008128eval_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 +02008129{
8130 LY_ERR rc;
8131 uint16_t this_op, i;
8132
8133 assert(repeat);
8134
8135 /* ('-')+ */
Michal Vasko004d3152020-06-11 19:59:22 +02008136 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008137 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008138 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 +02008139
aPiecek8b0cc152021-05-31 16:40:31 +02008140 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02008141 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008142 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008143 }
8144
Michal Vasko004d3152020-06-11 19:59:22 +02008145 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNARY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008146 LY_CHECK_RET(rc);
8147
aPiecek8b0cc152021-05-31 16:40:31 +02008148 if (!(options & LYXP_SKIP_EXPR) && (repeat % 2)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008149 if (options & LYXP_SCNODE_ALL) {
8150 warn_operands(set->ctx, set, NULL, 1, exp->expr, exp->tok_pos[this_op]);
8151 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008152 rc = moveto_op_math(set, NULL, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008153 LY_CHECK_RET(rc);
8154 }
8155 }
8156
8157 return LY_SUCCESS;
8158}
8159
8160/**
8161 * @brief Evaluate MultiplicativeExpr. Logs directly on error.
8162 *
Michal Vaskod3678892020-05-21 10:06:58 +02008163 * [18] MultiplicativeExpr ::= UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008164 * | MultiplicativeExpr '*' UnaryExpr
8165 * | MultiplicativeExpr 'div' UnaryExpr
8166 * | MultiplicativeExpr 'mod' UnaryExpr
8167 *
8168 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008169 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008170 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008171 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008172 * @param[in] options XPath options.
8173 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8174 */
8175static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008176eval_multiplicative_expr(const struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set,
8177 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008178{
8179 LY_ERR rc;
8180 uint16_t this_op;
8181 struct lyxp_set orig_set, set2;
8182 uint16_t i;
8183
8184 assert(repeat);
8185
8186 set_init(&orig_set, set);
8187 set_init(&set2, set);
8188
8189 set_fill_set(&orig_set, set);
8190
Michal Vasko004d3152020-06-11 19:59:22 +02008191 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008192 LY_CHECK_GOTO(rc, cleanup);
8193
8194 /* ('*' / 'div' / 'mod' UnaryExpr)* */
8195 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008196 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008197
Michal Vasko004d3152020-06-11 19:59:22 +02008198 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH);
aPiecek8b0cc152021-05-31 16:40:31 +02008199 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02008200 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008201 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008202
aPiecek8b0cc152021-05-31 16:40:31 +02008203 if (options & LYXP_SKIP_EXPR) {
8204 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008205 LY_CHECK_GOTO(rc, cleanup);
8206 continue;
8207 }
8208
8209 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008210 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008211 LY_CHECK_GOTO(rc, cleanup);
8212
8213 /* eval */
8214 if (options & LYXP_SCNODE_ALL) {
8215 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008216 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02008217 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008218 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008219 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008220 LY_CHECK_GOTO(rc, cleanup);
8221 }
8222 }
8223
8224cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008225 lyxp_set_free_content(&orig_set);
8226 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008227 return rc;
8228}
8229
8230/**
8231 * @brief Evaluate AdditiveExpr. Logs directly on error.
8232 *
Michal Vaskod3678892020-05-21 10:06:58 +02008233 * [17] AdditiveExpr ::= MultiplicativeExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008234 * | AdditiveExpr '+' MultiplicativeExpr
8235 * | AdditiveExpr '-' MultiplicativeExpr
8236 *
8237 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008238 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008239 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008240 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008241 * @param[in] options XPath options.
8242 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8243 */
8244static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008245eval_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 +02008246{
8247 LY_ERR rc;
8248 uint16_t this_op;
8249 struct lyxp_set orig_set, set2;
8250 uint16_t i;
8251
8252 assert(repeat);
8253
8254 set_init(&orig_set, set);
8255 set_init(&set2, set);
8256
8257 set_fill_set(&orig_set, set);
8258
Michal Vasko004d3152020-06-11 19:59:22 +02008259 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008260 LY_CHECK_GOTO(rc, cleanup);
8261
8262 /* ('+' / '-' MultiplicativeExpr)* */
8263 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008264 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008265
Michal Vasko004d3152020-06-11 19:59:22 +02008266 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008267 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02008268 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008269 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008270
aPiecek8b0cc152021-05-31 16:40:31 +02008271 if (options & LYXP_SKIP_EXPR) {
8272 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008273 LY_CHECK_GOTO(rc, cleanup);
8274 continue;
8275 }
8276
8277 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008278 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008279 LY_CHECK_GOTO(rc, cleanup);
8280
8281 /* eval */
8282 if (options & LYXP_SCNODE_ALL) {
8283 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008284 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02008285 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008286 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008287 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008288 LY_CHECK_GOTO(rc, cleanup);
8289 }
8290 }
8291
8292cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008293 lyxp_set_free_content(&orig_set);
8294 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008295 return rc;
8296}
8297
8298/**
8299 * @brief Evaluate RelationalExpr. Logs directly on error.
8300 *
Michal Vaskod3678892020-05-21 10:06:58 +02008301 * [16] RelationalExpr ::= AdditiveExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008302 * | RelationalExpr '<' AdditiveExpr
8303 * | RelationalExpr '>' AdditiveExpr
8304 * | RelationalExpr '<=' AdditiveExpr
8305 * | RelationalExpr '>=' AdditiveExpr
8306 *
8307 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008308 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008309 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008310 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008311 * @param[in] options XPath options.
8312 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8313 */
8314static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008315eval_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 +02008316{
8317 LY_ERR rc;
8318 uint16_t this_op;
8319 struct lyxp_set orig_set, set2;
8320 uint16_t i;
8321
8322 assert(repeat);
8323
8324 set_init(&orig_set, set);
8325 set_init(&set2, set);
8326
8327 set_fill_set(&orig_set, set);
8328
Michal Vasko004d3152020-06-11 19:59:22 +02008329 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008330 LY_CHECK_GOTO(rc, cleanup);
8331
8332 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
8333 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008334 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008335
Michal Vasko004d3152020-06-11 19:59:22 +02008336 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_COMP);
aPiecek8b0cc152021-05-31 16:40:31 +02008337 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02008338 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008339 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008340
aPiecek8b0cc152021-05-31 16:40:31 +02008341 if (options & LYXP_SKIP_EXPR) {
8342 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008343 LY_CHECK_GOTO(rc, cleanup);
8344 continue;
8345 }
8346
8347 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008348 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008349 LY_CHECK_GOTO(rc, cleanup);
8350
8351 /* eval */
8352 if (options & LYXP_SCNODE_ALL) {
8353 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008354 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02008355 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008356 } else {
8357 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options);
8358 LY_CHECK_GOTO(rc, cleanup);
8359 }
8360 }
8361
8362cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008363 lyxp_set_free_content(&orig_set);
8364 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008365 return rc;
8366}
8367
8368/**
8369 * @brief Evaluate EqualityExpr. Logs directly on error.
8370 *
Michal Vaskod3678892020-05-21 10:06:58 +02008371 * [15] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008372 * | EqualityExpr '!=' RelationalExpr
8373 *
8374 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008375 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008376 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008377 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008378 * @param[in] options XPath options.
8379 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8380 */
8381static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008382eval_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 +02008383{
8384 LY_ERR rc;
8385 uint16_t this_op;
8386 struct lyxp_set orig_set, set2;
8387 uint16_t i;
8388
8389 assert(repeat);
8390
8391 set_init(&orig_set, set);
8392 set_init(&set2, set);
8393
8394 set_fill_set(&orig_set, set);
8395
Michal Vasko004d3152020-06-11 19:59:22 +02008396 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008397 LY_CHECK_GOTO(rc, cleanup);
8398
8399 /* ('=' / '!=' RelationalExpr)* */
8400 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008401 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008402
Michal Vasko004d3152020-06-11 19:59:22 +02008403 assert((exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_EQUAL) || (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_NEQUAL));
aPiecek8b0cc152021-05-31 16:40:31 +02008404 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02008405 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008406 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008407
aPiecek8b0cc152021-05-31 16:40:31 +02008408 if (options & LYXP_SKIP_EXPR) {
8409 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008410 LY_CHECK_GOTO(rc, cleanup);
8411 continue;
8412 }
8413
8414 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008415 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008416 LY_CHECK_GOTO(rc, cleanup);
8417
8418 /* eval */
8419 if (options & LYXP_SCNODE_ALL) {
8420 warn_operands(set->ctx, set, &set2, 0, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vasko004d3152020-06-11 19:59:22 +02008421 warn_equality_value(exp, set, *tok_idx - 1, this_op - 1, *tok_idx - 1);
8422 warn_equality_value(exp, &set2, this_op - 1, this_op - 1, *tok_idx - 1);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008423 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02008424 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008425 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008426 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options);
8427 LY_CHECK_GOTO(rc, cleanup);
8428 }
8429 }
8430
8431cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008432 lyxp_set_free_content(&orig_set);
8433 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008434 return rc;
8435}
8436
8437/**
8438 * @brief Evaluate AndExpr. Logs directly on error.
8439 *
Michal Vaskod3678892020-05-21 10:06:58 +02008440 * [14] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008441 *
8442 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008443 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008444 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008445 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008446 * @param[in] options XPath options.
8447 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8448 */
8449static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008450eval_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 +02008451{
8452 LY_ERR rc;
8453 struct lyxp_set orig_set, set2;
8454 uint16_t i;
8455
8456 assert(repeat);
8457
8458 set_init(&orig_set, set);
8459 set_init(&set2, set);
8460
8461 set_fill_set(&orig_set, set);
8462
Michal Vasko004d3152020-06-11 19:59:22 +02008463 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008464 LY_CHECK_GOTO(rc, cleanup);
8465
8466 /* cast to boolean, we know that will be the final result */
aPiecek8b0cc152021-05-31 16:40:31 +02008467 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008468 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008469 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008470 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008471 }
8472
8473 /* ('and' EqualityExpr)* */
8474 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008475 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG);
aPiecek8b0cc152021-05-31 16:40:31 +02008476 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, ((options & LYXP_SKIP_EXPR) || !set->val.bln ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02008477 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008478 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008479
8480 /* lazy evaluation */
aPiecek8b0cc152021-05-31 16:40:31 +02008481 if ((options & LYXP_SKIP_EXPR) || ((set->type == LYXP_SET_BOOLEAN) && !set->val.bln)) {
8482 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008483 LY_CHECK_GOTO(rc, cleanup);
8484 continue;
8485 }
8486
8487 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008488 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008489 LY_CHECK_GOTO(rc, cleanup);
8490
8491 /* eval - just get boolean value actually */
8492 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008493 set_scnode_clear_ctx(&set2, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008494 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008495 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008496 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008497 set_fill_set(set, &set2);
8498 }
8499 }
8500
8501cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008502 lyxp_set_free_content(&orig_set);
8503 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008504 return rc;
8505}
8506
8507/**
8508 * @brief Evaluate OrExpr. Logs directly on error.
8509 *
Michal Vaskod3678892020-05-21 10:06:58 +02008510 * [13] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008511 *
8512 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008513 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008514 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008515 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008516 * @param[in] options XPath options.
8517 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8518 */
8519static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008520eval_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 +02008521{
8522 LY_ERR rc;
8523 struct lyxp_set orig_set, set2;
8524 uint16_t i;
8525
8526 assert(repeat);
8527
8528 set_init(&orig_set, set);
8529 set_init(&set2, set);
8530
8531 set_fill_set(&orig_set, set);
8532
Michal Vasko004d3152020-06-11 19:59:22 +02008533 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008534 LY_CHECK_GOTO(rc, cleanup);
8535
8536 /* cast to boolean, we know that will be the final result */
aPiecek8b0cc152021-05-31 16:40:31 +02008537 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008538 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008539 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008540 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008541 }
8542
8543 /* ('or' AndExpr)* */
8544 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008545 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG);
aPiecek8b0cc152021-05-31 16:40:31 +02008546 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, ((options & LYXP_SKIP_EXPR) || set->val.bln ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02008547 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008548 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008549
8550 /* lazy evaluation */
aPiecek8b0cc152021-05-31 16:40:31 +02008551 if ((options & LYXP_SKIP_EXPR) || ((set->type == LYXP_SET_BOOLEAN) && set->val.bln)) {
8552 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008553 LY_CHECK_GOTO(rc, cleanup);
8554 continue;
8555 }
8556
8557 set_fill_set(&set2, &orig_set);
8558 /* expr_type cound have been LYXP_EXPR_NONE in all these later calls (except for the first one),
8559 * but it does not matter */
Michal Vasko004d3152020-06-11 19:59:22 +02008560 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008561 LY_CHECK_GOTO(rc, cleanup);
8562
8563 /* eval - just get boolean value actually */
8564 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008565 set_scnode_clear_ctx(&set2, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008566 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008567 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008568 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008569 set_fill_set(set, &set2);
8570 }
8571 }
8572
8573cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008574 lyxp_set_free_content(&orig_set);
8575 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008576 return rc;
8577}
8578
8579/**
Michal Vasko004d3152020-06-11 19:59:22 +02008580 * @brief Decide what expression is at the pointer @p tok_idx and evaluate it accordingly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008581 *
8582 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008583 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008584 * @param[in] etype Expression type to evaluate.
aPiecek8b0cc152021-05-31 16:40:31 +02008585 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008586 * @param[in] options XPath options.
8587 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8588 */
8589static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008590eval_expr_select(const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_expr_type etype, struct lyxp_set *set,
8591 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008592{
8593 uint16_t i, count;
8594 enum lyxp_expr_type next_etype;
8595 LY_ERR rc;
8596
8597 /* process operator repeats */
Michal Vasko004d3152020-06-11 19:59:22 +02008598 if (!exp->repeat[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008599 next_etype = LYXP_EXPR_NONE;
8600 } else {
8601 /* find etype repeat */
Radek Krejci1e008d22020-08-17 11:37:37 +02008602 for (i = 0; exp->repeat[*tok_idx][i] > etype; ++i) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02008603
8604 /* select one-priority lower because etype expression called us */
8605 if (i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008606 next_etype = exp->repeat[*tok_idx][i - 1];
Michal Vasko03ff5a72019-09-11 13:49:33 +02008607 /* count repeats for that expression */
Radek Krejci1e008d22020-08-17 11:37:37 +02008608 for (count = 0; i && exp->repeat[*tok_idx][i - 1] == next_etype; ++count, --i) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02008609 } else {
8610 next_etype = LYXP_EXPR_NONE;
8611 }
8612 }
8613
8614 /* decide what expression are we parsing based on the repeat */
8615 switch (next_etype) {
8616 case LYXP_EXPR_OR:
Michal Vasko004d3152020-06-11 19:59:22 +02008617 rc = eval_or_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008618 break;
8619 case LYXP_EXPR_AND:
Michal Vasko004d3152020-06-11 19:59:22 +02008620 rc = eval_and_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008621 break;
8622 case LYXP_EXPR_EQUALITY:
Michal Vasko004d3152020-06-11 19:59:22 +02008623 rc = eval_equality_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008624 break;
8625 case LYXP_EXPR_RELATIONAL:
Michal Vasko004d3152020-06-11 19:59:22 +02008626 rc = eval_relational_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008627 break;
8628 case LYXP_EXPR_ADDITIVE:
Michal Vasko004d3152020-06-11 19:59:22 +02008629 rc = eval_additive_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008630 break;
8631 case LYXP_EXPR_MULTIPLICATIVE:
Michal Vasko004d3152020-06-11 19:59:22 +02008632 rc = eval_multiplicative_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008633 break;
8634 case LYXP_EXPR_UNARY:
Michal Vasko004d3152020-06-11 19:59:22 +02008635 rc = eval_unary_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008636 break;
8637 case LYXP_EXPR_UNION:
Michal Vasko004d3152020-06-11 19:59:22 +02008638 rc = eval_union_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008639 break;
8640 case LYXP_EXPR_NONE:
Michal Vasko004d3152020-06-11 19:59:22 +02008641 rc = eval_path_expr(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008642 break;
8643 default:
8644 LOGINT_RET(set->ctx);
8645 }
8646
8647 return rc;
8648}
8649
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008650/**
8651 * @brief Get root type.
8652 *
8653 * @param[in] ctx_node Context node.
8654 * @param[in] ctx_scnode Schema context node.
8655 * @param[in] options XPath options.
8656 * @return Root type.
8657 */
8658static enum lyxp_node_type
Radek Krejci1deb5be2020-08-26 16:43:36 +02008659lyxp_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 +01008660{
Michal Vasko6b26e742020-07-17 15:02:10 +02008661 const struct lysc_node *op;
8662
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008663 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008664 /* schema */
Radek Krejci1e008d22020-08-17 11:37:37 +02008665 for (op = ctx_scnode; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent) {}
Michal Vasko6b26e742020-07-17 15:02:10 +02008666
8667 if (op || (options & LYXP_SCNODE)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008668 /* general root that can access everything */
8669 return LYXP_NODE_ROOT;
8670 } else if (!ctx_scnode || (ctx_scnode->flags & LYS_CONFIG_W)) {
8671 /* root context node can access only config data (because we said so, it is unspecified) */
8672 return LYXP_NODE_ROOT_CONFIG;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008673 }
Michal Vasko6b26e742020-07-17 15:02:10 +02008674 return LYXP_NODE_ROOT;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008675 }
8676
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008677 /* data */
Michal Vasko6b26e742020-07-17 15:02:10 +02008678 op = ctx_node ? ctx_node->schema : NULL;
Michal Vaskod989ba02020-08-24 10:59:24 +02008679 for ( ; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent) {}
Michal Vasko6b26e742020-07-17 15:02:10 +02008680
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008681 if (op || !(options & LYXP_SCHEMA)) {
8682 /* general root that can access everything */
8683 return LYXP_NODE_ROOT;
Christian Hoppsb6ecaea2021-02-06 09:45:38 -05008684 } else if (!ctx_node || !ctx_node->schema || (ctx_node->schema->flags & LYS_CONFIG_W)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008685 /* root context node can access only config data (because we said so, it is unspecified) */
8686 return LYXP_NODE_ROOT_CONFIG;
8687 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008688 return LYXP_NODE_ROOT;
8689}
8690
Michal Vasko03ff5a72019-09-11 13:49:33 +02008691LY_ERR
Michal Vasko400e9672021-01-11 13:39:17 +01008692lyxp_eval(const struct ly_ctx *ctx, const struct lyxp_expr *exp, const struct lys_module *cur_mod,
Radek Krejci8df109d2021-04-23 12:19:08 +02008693 LY_VALUE_FORMAT format, void *prefix_data, const struct lyd_node *ctx_node, const struct lyd_node *tree,
aPiecekfba75362021-10-07 12:39:48 +02008694 const struct lyxp_var *vars, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008695{
Michal Vasko004d3152020-06-11 19:59:22 +02008696 uint16_t tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008697 LY_ERR rc;
8698
Michal Vasko400e9672021-01-11 13:39:17 +01008699 LY_CHECK_ARG_RET(ctx, ctx, exp, set, LY_EINVAL);
Radek Krejci8df109d2021-04-23 12:19:08 +02008700 if (!cur_mod && ((format == LY_VALUE_SCHEMA) || (format == LY_VALUE_SCHEMA_RESOLVED))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008701 LOGARG(NULL, "Current module must be set if schema format is used.");
8702 return LY_EINVAL;
Michal Vasko004d3152020-06-11 19:59:22 +02008703 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008704
Michal Vaskod3bb12f2020-12-04 14:33:09 +01008705 if (tree) {
8706 /* adjust the pointer to be the first top-level sibling */
8707 while (tree->parent) {
8708 tree = lyd_parent(tree);
8709 }
8710 tree = lyd_first_sibling(tree);
8711 }
8712
Michal Vasko03ff5a72019-09-11 13:49:33 +02008713 /* prepare set for evaluation */
Michal Vasko03ff5a72019-09-11 13:49:33 +02008714 memset(set, 0, sizeof *set);
Michal Vaskod3678892020-05-21 10:06:58 +02008715 set->type = LYXP_SET_NODE_SET;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008716 set->root_type = lyxp_get_root_type(ctx_node, NULL, options);
8717 set_insert_node(set, (struct lyd_node *)ctx_node, 0, ctx_node ? LYXP_NODE_ELEM : set->root_type, 0);
8718
Michal Vasko400e9672021-01-11 13:39:17 +01008719 set->ctx = (struct ly_ctx *)ctx;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008720 set->cur_node = ctx_node;
8721 for (set->context_op = ctx_node ? ctx_node->schema : NULL;
Radek Krejci0f969882020-08-21 16:56:47 +02008722 set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF));
8723 set->context_op = set->context_op->parent) {}
Michal Vaskof03ed032020-03-04 13:31:44 +01008724 set->tree = tree;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008725 set->cur_mod = cur_mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008726 set->format = format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008727 set->prefix_data = prefix_data;
aPiecekfba75362021-10-07 12:39:48 +02008728 set->vars = vars;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008729
Radek Krejciddace2c2021-01-08 11:30:56 +01008730 LOG_LOCSET(NULL, set->cur_node, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01008731
Michal Vasko03ff5a72019-09-11 13:49:33 +02008732 /* evaluate */
Michal Vasko004d3152020-06-11 19:59:22 +02008733 rc = eval_expr_select(exp, &tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008734 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02008735 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008736 }
8737
Radek Krejciddace2c2021-01-08 11:30:56 +01008738 LOG_LOCBACK(0, 1, 0, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008739 return rc;
8740}
8741
8742#if 0
8743
8744/* full xml printing of set elements, not used currently */
8745
8746void
8747lyxp_set_print_xml(FILE *f, struct lyxp_set *set)
8748{
8749 uint32_t i;
8750 char *str_num;
8751 struct lyout out;
8752
8753 memset(&out, 0, sizeof out);
8754
8755 out.type = LYOUT_STREAM;
8756 out.method.f = f;
8757
8758 switch (set->type) {
8759 case LYXP_SET_EMPTY:
Michal Vasko5233e962020-08-14 14:26:20 +02008760 ly_print_(&out, "Empty XPath set\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008761 break;
8762 case LYXP_SET_BOOLEAN:
Michal Vasko5233e962020-08-14 14:26:20 +02008763 ly_print_(&out, "Boolean XPath set:\n");
8764 ly_print_(&out, "%s\n\n", set->value.bool ? "true" : "false");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008765 break;
8766 case LYXP_SET_STRING:
Michal Vasko5233e962020-08-14 14:26:20 +02008767 ly_print_(&out, "String XPath set:\n");
8768 ly_print_(&out, "\"%s\"\n\n", set->value.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008769 break;
8770 case LYXP_SET_NUMBER:
Michal Vasko5233e962020-08-14 14:26:20 +02008771 ly_print_(&out, "Number XPath set:\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008772
8773 if (isnan(set->value.num)) {
8774 str_num = strdup("NaN");
8775 } else if ((set->value.num == 0) || (set->value.num == -0.0f)) {
8776 str_num = strdup("0");
8777 } else if (isinf(set->value.num) && !signbit(set->value.num)) {
8778 str_num = strdup("Infinity");
8779 } else if (isinf(set->value.num) && signbit(set->value.num)) {
8780 str_num = strdup("-Infinity");
8781 } else if ((long long)set->value.num == set->value.num) {
8782 if (asprintf(&str_num, "%lld", (long long)set->value.num) == -1) {
8783 str_num = NULL;
8784 }
8785 } else {
8786 if (asprintf(&str_num, "%03.1Lf", set->value.num) == -1) {
8787 str_num = NULL;
8788 }
8789 }
8790 if (!str_num) {
8791 LOGMEM;
8792 return;
8793 }
Michal Vasko5233e962020-08-14 14:26:20 +02008794 ly_print_(&out, "%s\n\n", str_num);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008795 free(str_num);
8796 break;
8797 case LYXP_SET_NODE_SET:
Michal Vasko5233e962020-08-14 14:26:20 +02008798 ly_print_(&out, "Node XPath set:\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008799
8800 for (i = 0; i < set->used; ++i) {
Michal Vasko5233e962020-08-14 14:26:20 +02008801 ly_print_(&out, "%d. ", i + 1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008802 switch (set->node_type[i]) {
8803 case LYXP_NODE_ROOT_ALL:
Michal Vasko5233e962020-08-14 14:26:20 +02008804 ly_print_(&out, "ROOT all\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008805 break;
8806 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko5233e962020-08-14 14:26:20 +02008807 ly_print_(&out, "ROOT config\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008808 break;
8809 case LYXP_NODE_ROOT_STATE:
Michal Vasko5233e962020-08-14 14:26:20 +02008810 ly_print_(&out, "ROOT state\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008811 break;
8812 case LYXP_NODE_ROOT_NOTIF:
Michal Vasko5233e962020-08-14 14:26:20 +02008813 ly_print_(&out, "ROOT notification \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008814 break;
8815 case LYXP_NODE_ROOT_RPC:
Michal Vasko5233e962020-08-14 14:26:20 +02008816 ly_print_(&out, "ROOT rpc \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008817 break;
8818 case LYXP_NODE_ROOT_OUTPUT:
Michal Vasko5233e962020-08-14 14:26:20 +02008819 ly_print_(&out, "ROOT output \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008820 break;
8821 case LYXP_NODE_ELEM:
Michal Vasko5233e962020-08-14 14:26:20 +02008822 ly_print_(&out, "ELEM \"%s\"\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008823 xml_print_node(&out, 1, set->value.nodes[i], 1, LYP_FORMAT);
Michal Vasko5233e962020-08-14 14:26:20 +02008824 ly_print_(&out, "\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008825 break;
8826 case LYXP_NODE_TEXT:
Michal Vasko5233e962020-08-14 14:26:20 +02008827 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 +02008828 break;
8829 case LYXP_NODE_ATTR:
Michal Vasko5233e962020-08-14 14:26:20 +02008830 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 +02008831 break;
8832 }
8833 }
8834 break;
8835 }
8836}
8837
8838#endif
8839
8840LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008841lyxp_set_cast(struct lyxp_set *set, enum lyxp_set_type target)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008842{
8843 long double num;
8844 char *str;
8845 LY_ERR rc;
8846
8847 if (!set || (set->type == target)) {
8848 return LY_SUCCESS;
8849 }
8850
8851 /* it's not possible to convert anything into a node set */
Michal Vaskod3678892020-05-21 10:06:58 +02008852 assert(target != LYXP_SET_NODE_SET);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008853
8854 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vaskod3678892020-05-21 10:06:58 +02008855 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008856 return LY_EINVAL;
8857 }
8858
8859 /* to STRING */
Michal Vaskod3678892020-05-21 10:06:58 +02008860 if ((target == LYXP_SET_STRING) || ((target == LYXP_SET_NUMBER) && (set->type == LYXP_SET_NODE_SET))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008861 switch (set->type) {
8862 case LYXP_SET_NUMBER:
8863 if (isnan(set->val.num)) {
8864 set->val.str = strdup("NaN");
8865 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8866 } else if ((set->val.num == 0) || (set->val.num == -0.0f)) {
8867 set->val.str = strdup("0");
8868 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8869 } else if (isinf(set->val.num) && !signbit(set->val.num)) {
8870 set->val.str = strdup("Infinity");
8871 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8872 } else if (isinf(set->val.num) && signbit(set->val.num)) {
8873 set->val.str = strdup("-Infinity");
8874 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8875 } else if ((long long)set->val.num == set->val.num) {
8876 if (asprintf(&str, "%lld", (long long)set->val.num) == -1) {
8877 LOGMEM_RET(set->ctx);
8878 }
8879 set->val.str = str;
8880 } else {
8881 if (asprintf(&str, "%03.1Lf", set->val.num) == -1) {
8882 LOGMEM_RET(set->ctx);
8883 }
8884 set->val.str = str;
8885 }
8886 break;
8887 case LYXP_SET_BOOLEAN:
Michal Vasko004d3152020-06-11 19:59:22 +02008888 if (set->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008889 set->val.str = strdup("true");
8890 } else {
8891 set->val.str = strdup("false");
8892 }
8893 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), LY_EMEM);
8894 break;
8895 case LYXP_SET_NODE_SET:
Michal Vasko03ff5a72019-09-11 13:49:33 +02008896 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008897 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008898
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008899 rc = cast_node_set_to_string(set, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008900 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02008901 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008902 set->val.str = str;
8903 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008904 default:
8905 LOGINT_RET(set->ctx);
8906 }
8907 set->type = LYXP_SET_STRING;
8908 }
8909
8910 /* to NUMBER */
8911 if (target == LYXP_SET_NUMBER) {
8912 switch (set->type) {
8913 case LYXP_SET_STRING:
8914 num = cast_string_to_number(set->val.str);
Michal Vaskod3678892020-05-21 10:06:58 +02008915 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008916 set->val.num = num;
8917 break;
8918 case LYXP_SET_BOOLEAN:
Michal Vasko004d3152020-06-11 19:59:22 +02008919 if (set->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008920 set->val.num = 1;
8921 } else {
8922 set->val.num = 0;
8923 }
8924 break;
8925 default:
8926 LOGINT_RET(set->ctx);
8927 }
8928 set->type = LYXP_SET_NUMBER;
8929 }
8930
8931 /* to BOOLEAN */
8932 if (target == LYXP_SET_BOOLEAN) {
8933 switch (set->type) {
8934 case LYXP_SET_NUMBER:
8935 if ((set->val.num == 0) || (set->val.num == -0.0f) || isnan(set->val.num)) {
Michal Vasko004d3152020-06-11 19:59:22 +02008936 set->val.bln = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008937 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02008938 set->val.bln = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008939 }
8940 break;
8941 case LYXP_SET_STRING:
8942 if (set->val.str[0]) {
Michal Vaskod3678892020-05-21 10:06:58 +02008943 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008944 set->val.bln = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008945 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02008946 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008947 set->val.bln = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008948 }
8949 break;
8950 case LYXP_SET_NODE_SET:
Michal Vaskod3678892020-05-21 10:06:58 +02008951 if (set->used) {
8952 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008953 set->val.bln = 1;
Michal Vaskod3678892020-05-21 10:06:58 +02008954 } else {
8955 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008956 set->val.bln = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02008957 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008958 break;
8959 default:
8960 LOGINT_RET(set->ctx);
8961 }
8962 set->type = LYXP_SET_BOOLEAN;
8963 }
8964
Michal Vasko03ff5a72019-09-11 13:49:33 +02008965 return LY_SUCCESS;
8966}
8967
8968LY_ERR
Michal Vasko400e9672021-01-11 13:39:17 +01008969lyxp_atomize(const struct ly_ctx *ctx, const struct lyxp_expr *exp, const struct lys_module *cur_mod,
Radek Krejci8df109d2021-04-23 12:19:08 +02008970 LY_VALUE_FORMAT format, void *prefix_data, const struct lysc_node *ctx_scnode, struct lyxp_set *set,
Michal Vasko400e9672021-01-11 13:39:17 +01008971 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008972{
Radek Krejci2efc45b2020-12-22 16:25:44 +01008973 LY_ERR ret;
Michal Vasko004d3152020-06-11 19:59:22 +02008974 uint16_t tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008975
Michal Vasko400e9672021-01-11 13:39:17 +01008976 LY_CHECK_ARG_RET(ctx, ctx, exp, set, LY_EINVAL);
Radek Krejci8df109d2021-04-23 12:19:08 +02008977 if (!cur_mod && ((format == LY_VALUE_SCHEMA) || (format == LY_VALUE_SCHEMA_RESOLVED))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008978 LOGARG(NULL, "Current module must be set if schema format is used.");
8979 return LY_EINVAL;
Michal Vasko004d3152020-06-11 19:59:22 +02008980 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008981
8982 /* prepare set for evaluation */
Michal Vasko03ff5a72019-09-11 13:49:33 +02008983 memset(set, 0, sizeof *set);
8984 set->type = LYXP_SET_SCNODE_SET;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008985 set->root_type = lyxp_get_root_type(NULL, ctx_scnode, options);
8986 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 +01008987 set->val.scnodes[0].in_ctx = LYXP_SET_SCNODE_START;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008988
Michal Vasko400e9672021-01-11 13:39:17 +01008989 set->ctx = (struct ly_ctx *)ctx;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008990 set->cur_scnode = ctx_scnode;
Michal Vasko6b26e742020-07-17 15:02:10 +02008991 for (set->context_op = ctx_scnode;
Radek Krejci0f969882020-08-21 16:56:47 +02008992 set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF));
8993 set->context_op = set->context_op->parent) {}
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008994 set->cur_mod = cur_mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008995 set->format = format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008996 set->prefix_data = prefix_data;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008997
Radek Krejciddace2c2021-01-08 11:30:56 +01008998 LOG_LOCSET(set->cur_scnode, NULL, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01008999
Michal Vasko03ff5a72019-09-11 13:49:33 +02009000 /* evaluate */
Radek Krejci2efc45b2020-12-22 16:25:44 +01009001 ret = eval_expr_select(exp, &tok_idx, 0, set, options);
9002
Radek Krejciddace2c2021-01-08 11:30:56 +01009003 LOG_LOCBACK(1, 0, 0, 0);
Radek Krejci2efc45b2020-12-22 16:25:44 +01009004 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009005}
Michal Vaskod43d71a2020-08-07 14:54:58 +02009006
9007API const char *
9008lyxp_get_expr(const struct lyxp_expr *path)
9009{
9010 if (!path) {
9011 return NULL;
9012 }
9013
9014 return path->expr;
9015}