blob: 2c77d0173d7ee26f0f3dc19a2f117394e14ccc8d [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/**
1275 * @brief Replace a node in a set with another. Context position aware.
1276 *
1277 * @param[in] set Set to use.
1278 * @param[in] node Node to insert to @p set.
1279 * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting.
1280 * @param[in] node_type Node type of @p node.
1281 * @param[in] idx Index in @p set of the node to replace.
1282 */
1283static void
1284set_replace_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx)
1285{
1286 assert(set && (idx < set->used));
1287
Michal Vasko2caefc12019-11-14 16:07:56 +01001288 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1289 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
1290 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001291 set->val.nodes[idx].node = (struct lyd_node *)node;
1292 set->val.nodes[idx].type = node_type;
1293 set->val.nodes[idx].pos = pos;
Michal Vasko2caefc12019-11-14 16:07:56 +01001294 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1295 set_insert_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
1296 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001297}
1298
1299/**
1300 * @brief Set all nodes with ctx 1 to a new unique context value.
1301 *
1302 * @param[in] set Set to modify.
1303 * @return New context value.
1304 */
Michal Vasko5c4e5892019-11-14 12:31:38 +01001305static int32_t
Michal Vasko03ff5a72019-09-11 13:49:33 +02001306set_scnode_new_in_ctx(struct lyxp_set *set)
1307{
Michal Vasko5c4e5892019-11-14 12:31:38 +01001308 uint32_t i;
1309 int32_t ret_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001310
1311 assert(set->type == LYXP_SET_SCNODE_SET);
1312
Radek Krejcif13b87b2020-12-01 22:02:17 +01001313 ret_ctx = LYXP_SET_SCNODE_ATOM_PRED_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001314retry:
1315 for (i = 0; i < set->used; ++i) {
1316 if (set->val.scnodes[i].in_ctx >= ret_ctx) {
1317 ret_ctx = set->val.scnodes[i].in_ctx + 1;
1318 goto retry;
1319 }
1320 }
1321 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001322 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001323 set->val.scnodes[i].in_ctx = ret_ctx;
1324 }
1325 }
1326
1327 return ret_ctx;
1328}
1329
1330/**
1331 * @brief Get unique @p node position in the data.
1332 *
1333 * @param[in] node Node to find.
1334 * @param[in] node_type Node type of @p node.
1335 * @param[in] root Root node.
1336 * @param[in] root_type Type of the XPath @p root node.
1337 * @param[in] prev Node that we think is before @p node in DFS from @p root. Can optionally
1338 * be used to increase efficiency and start the DFS from this node.
1339 * @param[in] prev_pos Node @p prev position. Optional, but must be set if @p prev is set.
1340 * @return Node position.
1341 */
1342static uint32_t
1343get_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 +02001344 enum lyxp_node_type root_type, const struct lyd_node **prev, uint32_t *prev_pos)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001345{
Michal Vasko56daf732020-08-10 10:57:18 +02001346 const struct lyd_node *elem = NULL, *top_sibling;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001347 uint32_t pos = 1;
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001348 ly_bool found = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001349
1350 assert(prev && prev_pos && !root->prev->next);
1351
1352 if ((node_type == LYXP_NODE_ROOT) || (node_type == LYXP_NODE_ROOT_CONFIG)) {
1353 return 0;
1354 }
1355
1356 if (*prev) {
1357 /* start from the previous element instead from the root */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001358 pos = *prev_pos;
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001359 for (top_sibling = *prev; top_sibling->parent; top_sibling = lyd_parent(top_sibling)) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001360 goto dfs_search;
1361 }
1362
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001363 LY_LIST_FOR(root, top_sibling) {
Michal Vasko56daf732020-08-10 10:57:18 +02001364 LYD_TREE_DFS_BEGIN(top_sibling, elem) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001365dfs_search:
Michal Vaskoa9309bb2021-07-09 09:31:55 +02001366 LYD_TREE_DFS_continue = 0;
1367
Michal Vasko56daf732020-08-10 10:57:18 +02001368 if (*prev && !elem) {
1369 /* resume previous DFS */
1370 elem = LYD_TREE_DFS_next = (struct lyd_node *)*prev;
1371 LYD_TREE_DFS_continue = 0;
1372 }
1373
Michal Vasko03ff5a72019-09-11 13:49:33 +02001374 if ((root_type == LYXP_NODE_ROOT_CONFIG) && (elem->schema->flags & LYS_CONFIG_R)) {
Michal Vasko56daf732020-08-10 10:57:18 +02001375 /* skip */
1376 LYD_TREE_DFS_continue = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001377 } else {
Michal Vasko56daf732020-08-10 10:57:18 +02001378 if (elem == node) {
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001379 found = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001380 break;
1381 }
Michal Vasko56daf732020-08-10 10:57:18 +02001382 ++pos;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001383 }
Michal Vasko56daf732020-08-10 10:57:18 +02001384
1385 LYD_TREE_DFS_END(top_sibling, elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001386 }
1387
1388 /* node found */
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001389 if (found) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001390 break;
1391 }
1392 }
1393
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001394 if (!found) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001395 if (!(*prev)) {
1396 /* we went from root and failed to find it, cannot be */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001397 LOGINT(LYD_CTX(node));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001398 return 0;
1399 } else {
Michal Vasko56daf732020-08-10 10:57:18 +02001400 /* start the search again from the beginning */
1401 *prev = root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001402
Michal Vasko56daf732020-08-10 10:57:18 +02001403 top_sibling = root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001404 pos = 1;
1405 goto dfs_search;
1406 }
1407 }
1408
1409 /* remember the last found node for next time */
1410 *prev = node;
1411 *prev_pos = pos;
1412
1413 return pos;
1414}
1415
1416/**
1417 * @brief Assign (fill) missing node positions.
1418 *
1419 * @param[in] set Set to fill positions in.
1420 * @param[in] root Context root node.
1421 * @param[in] root_type Context root type.
1422 * @return LY_ERR
1423 */
1424static LY_ERR
1425set_assign_pos(struct lyxp_set *set, const struct lyd_node *root, enum lyxp_node_type root_type)
1426{
1427 const struct lyd_node *prev = NULL, *tmp_node;
1428 uint32_t i, tmp_pos = 0;
1429
1430 for (i = 0; i < set->used; ++i) {
1431 if (!set->val.nodes[i].pos) {
1432 tmp_node = NULL;
1433 switch (set->val.nodes[i].type) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001434 case LYXP_NODE_META:
1435 tmp_node = set->val.meta[i].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001436 if (!tmp_node) {
1437 LOGINT_RET(root->schema->module->ctx);
1438 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01001439 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001440 case LYXP_NODE_ELEM:
1441 case LYXP_NODE_TEXT:
1442 if (!tmp_node) {
1443 tmp_node = set->val.nodes[i].node;
1444 }
1445 set->val.nodes[i].pos = get_node_pos(tmp_node, set->val.nodes[i].type, root, root_type, &prev, &tmp_pos);
1446 break;
1447 default:
1448 /* all roots have position 0 */
1449 break;
1450 }
1451 }
1452 }
1453
1454 return LY_SUCCESS;
1455}
1456
1457/**
Michal Vasko9f96a052020-03-10 09:41:45 +01001458 * @brief Get unique @p meta position in the parent metadata.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001459 *
Michal Vasko9f96a052020-03-10 09:41:45 +01001460 * @param[in] meta Metadata to use.
1461 * @return Metadata position.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001462 */
1463static uint16_t
Michal Vasko9f96a052020-03-10 09:41:45 +01001464get_meta_pos(struct lyd_meta *meta)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001465{
1466 uint16_t pos = 0;
Michal Vasko9f96a052020-03-10 09:41:45 +01001467 struct lyd_meta *meta2;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001468
Michal Vasko9f96a052020-03-10 09:41:45 +01001469 for (meta2 = meta->parent->meta; meta2 && (meta2 != meta); meta2 = meta2->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001470 ++pos;
1471 }
1472
Michal Vasko9f96a052020-03-10 09:41:45 +01001473 assert(meta2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001474 return pos;
1475}
1476
1477/**
1478 * @brief Compare 2 nodes in respect to XPath document order.
1479 *
1480 * @param[in] item1 1st node.
1481 * @param[in] item2 2nd node.
1482 * @return If 1st > 2nd returns 1, 1st == 2nd returns 0, and 1st < 2nd returns -1.
1483 */
1484static int
1485set_sort_compare(struct lyxp_set_node *item1, struct lyxp_set_node *item2)
1486{
Michal Vasko9f96a052020-03-10 09:41:45 +01001487 uint32_t meta_pos1 = 0, meta_pos2 = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001488
1489 if (item1->pos < item2->pos) {
1490 return -1;
1491 }
1492
1493 if (item1->pos > item2->pos) {
1494 return 1;
1495 }
1496
1497 /* node positions are equal, the fun case */
1498
1499 /* 1st ELEM - == - 2nd TEXT, 1st TEXT - == - 2nd ELEM */
1500 /* special case since text nodes are actually saved as their parents */
1501 if ((item1->node == item2->node) && (item1->type != item2->type)) {
1502 if (item1->type == LYXP_NODE_ELEM) {
1503 assert(item2->type == LYXP_NODE_TEXT);
1504 return -1;
1505 } else {
1506 assert((item1->type == LYXP_NODE_TEXT) && (item2->type == LYXP_NODE_ELEM));
1507 return 1;
1508 }
1509 }
1510
Michal Vasko9f96a052020-03-10 09:41:45 +01001511 /* we need meta positions now */
1512 if (item1->type == LYXP_NODE_META) {
1513 meta_pos1 = get_meta_pos((struct lyd_meta *)item1->node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001514 }
Michal Vasko9f96a052020-03-10 09:41:45 +01001515 if (item2->type == LYXP_NODE_META) {
1516 meta_pos2 = get_meta_pos((struct lyd_meta *)item2->node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001517 }
1518
Michal Vasko9f96a052020-03-10 09:41:45 +01001519 /* 1st ROOT - 2nd ROOT, 1st ELEM - 2nd ELEM, 1st TEXT - 2nd TEXT, 1st META - =pos= - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001520 /* check for duplicates */
1521 if (item1->node == item2->node) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001522 assert((item1->type == item2->type) && ((item1->type != LYXP_NODE_META) || (meta_pos1 == meta_pos2)));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001523 return 0;
1524 }
1525
Michal Vasko9f96a052020-03-10 09:41:45 +01001526 /* 1st ELEM - 2nd TEXT, 1st ELEM - any pos - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001527 /* elem is always first, 2nd node is after it */
1528 if (item1->type == LYXP_NODE_ELEM) {
1529 assert(item2->type != LYXP_NODE_ELEM);
1530 return -1;
1531 }
1532
Michal Vasko9f96a052020-03-10 09:41:45 +01001533 /* 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 +02001534 /* 2nd is before 1st */
Michal Vasko69730152020-10-09 16:30:07 +02001535 if (((item1->type == LYXP_NODE_TEXT) &&
1536 ((item2->type == LYXP_NODE_ELEM) || (item2->type == LYXP_NODE_META))) ||
1537 ((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_ELEM)) ||
1538 (((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_META)) &&
1539 (meta_pos1 > meta_pos2))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001540 return 1;
1541 }
1542
Michal Vasko9f96a052020-03-10 09:41:45 +01001543 /* 1st META - any pos - 2nd TEXT, 1st META <pos< - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001544 /* 2nd is after 1st */
1545 return -1;
1546}
1547
1548/**
1549 * @brief Set cast for comparisons.
1550 *
1551 * @param[in] trg Target set to cast source into.
1552 * @param[in] src Source set.
1553 * @param[in] type Target set type.
1554 * @param[in] src_idx Source set node index.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001555 * @return LY_ERR
1556 */
1557static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001558set_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 +02001559{
1560 assert(src->type == LYXP_SET_NODE_SET);
1561
1562 set_init(trg, src);
1563
1564 /* insert node into target set */
1565 set_insert_node(trg, src->val.nodes[src_idx].node, src->val.nodes[src_idx].pos, src->val.nodes[src_idx].type, 0);
1566
1567 /* cast target set appropriately */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001568 return lyxp_set_cast(trg, type);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001569}
1570
Michal Vasko4c7763f2020-07-27 17:40:37 +02001571/**
1572 * @brief Set content canonization for comparisons.
1573 *
1574 * @param[in] trg Target set to put the canononized source into.
1575 * @param[in] src Source set.
1576 * @param[in] xp_node Source XPath node/meta to use for canonization.
1577 * @return LY_ERR
1578 */
1579static LY_ERR
1580set_comp_canonize(struct lyxp_set *trg, const struct lyxp_set *src, const struct lyxp_set_node *xp_node)
1581{
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001582 const struct lysc_type *type = NULL;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001583 struct lyd_value val;
1584 struct ly_err_item *err = NULL;
1585 char *str, *ptr;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001586 LY_ERR rc;
1587
1588 /* is there anything to canonize even? */
1589 if ((src->type == LYXP_SET_NUMBER) || (src->type == LYXP_SET_STRING)) {
1590 /* do we have a type to use for canonization? */
1591 if ((xp_node->type == LYXP_NODE_ELEM) && (xp_node->node->schema->nodetype & LYD_NODE_TERM)) {
1592 type = ((struct lyd_node_term *)xp_node->node)->value.realtype;
1593 } else if (xp_node->type == LYXP_NODE_META) {
1594 type = ((struct lyd_meta *)xp_node->node)->value.realtype;
1595 }
1596 }
1597 if (!type) {
1598 goto fill;
1599 }
1600
1601 if (src->type == LYXP_SET_NUMBER) {
1602 /* canonize number */
1603 if (asprintf(&str, "%Lf", src->val.num) == -1) {
1604 LOGMEM(src->ctx);
1605 return LY_EMEM;
1606 }
1607 } else {
1608 /* canonize string */
1609 str = strdup(src->val.str);
1610 }
1611
1612 /* ignore errors, the value may not satisfy schema constraints */
Radek Krejci0b013302021-03-29 15:22:32 +02001613 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 +01001614 LYD_HINT_DATA, xp_node->node->schema, &val, NULL, &err);
Michal Vasko4c7763f2020-07-27 17:40:37 +02001615 ly_err_free(err);
1616 if (rc) {
1617 /* invalid value */
Radek IÅ¡a48460222021-03-02 15:18:32 +01001618 /* function store automaticaly dealloc value when fail */
Michal Vasko4c7763f2020-07-27 17:40:37 +02001619 goto fill;
1620 }
1621
Michal Vasko4c7763f2020-07-27 17:40:37 +02001622 /* use the canonized value */
1623 set_init(trg, src);
1624 trg->type = src->type;
1625 if (src->type == LYXP_SET_NUMBER) {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001626 trg->val.num = strtold(type->plugin->print(src->ctx, &val, LY_VALUE_CANON, NULL, NULL, NULL), &ptr);
Michal Vasko4c7763f2020-07-27 17:40:37 +02001627 LY_CHECK_ERR_RET(ptr[0], LOGINT(src->ctx), LY_EINT);
1628 } else {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001629 trg->val.str = strdup(type->plugin->print(src->ctx, &val, LY_VALUE_CANON, NULL, NULL, NULL));
Michal Vasko4c7763f2020-07-27 17:40:37 +02001630 }
1631 type->plugin->free(src->ctx, &val);
1632 return LY_SUCCESS;
1633
1634fill:
1635 /* no canonization needed/possible */
1636 set_fill_set(trg, src);
1637 return LY_SUCCESS;
1638}
1639
Michal Vasko03ff5a72019-09-11 13:49:33 +02001640#ifndef NDEBUG
1641
1642/**
1643 * @brief Bubble sort @p set into XPath document order.
1644 * Context position aware. Unused in the 'Release' build target.
1645 *
1646 * @param[in] set Set to sort.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001647 * @return How many times the whole set was traversed - 1 (if set was sorted, returns 0).
1648 */
1649static int
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001650set_sort(struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001651{
1652 uint32_t i, j;
Radek Krejci1deb5be2020-08-26 16:43:36 +02001653 int ret = 0, cmp;
Radek Krejci857189e2020-09-01 13:26:36 +02001654 ly_bool inverted, change;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001655 const struct lyd_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001656 struct lyxp_set_node item;
1657 struct lyxp_set_hash_node hnode;
1658 uint64_t hash;
1659
Michal Vasko3cf8fbf2020-05-27 15:21:21 +02001660 if ((set->type != LYXP_SET_NODE_SET) || (set->used < 2)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001661 return 0;
1662 }
1663
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001664 /* find first top-level node to be used as anchor for positions */
Michal Vasko9e685082021-01-29 14:49:09 +01001665 for (root = set->cur_node; root->parent; root = lyd_parent(root)) {}
Michal Vaskod989ba02020-08-24 10:59:24 +02001666 for ( ; root->prev->next; root = root->prev) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001667
1668 /* fill positions */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001669 if (set_assign_pos(set, root, set->root_type)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001670 return -1;
1671 }
1672
1673 LOGDBG(LY_LDGXPATH, "SORT BEGIN");
1674 print_set_debug(set);
1675
1676 for (i = 0; i < set->used; ++i) {
1677 inverted = 0;
1678 change = 0;
1679
1680 for (j = 1; j < set->used - i; ++j) {
1681 /* compare node positions */
1682 if (inverted) {
1683 cmp = set_sort_compare(&set->val.nodes[j], &set->val.nodes[j - 1]);
1684 } else {
1685 cmp = set_sort_compare(&set->val.nodes[j - 1], &set->val.nodes[j]);
1686 }
1687
1688 /* swap if needed */
1689 if ((inverted && (cmp < 0)) || (!inverted && (cmp > 0))) {
1690 change = 1;
1691
1692 item = set->val.nodes[j - 1];
1693 set->val.nodes[j - 1] = set->val.nodes[j];
1694 set->val.nodes[j] = item;
1695 } else {
1696 /* whether node_pos1 should be smaller than node_pos2 or the other way around */
1697 inverted = !inverted;
1698 }
1699 }
1700
1701 ++ret;
1702
1703 if (!change) {
1704 break;
1705 }
1706 }
1707
1708 LOGDBG(LY_LDGXPATH, "SORT END %d", ret);
1709 print_set_debug(set);
1710
1711 /* check node hashes */
1712 if (set->used >= LYD_HT_MIN_ITEMS) {
1713 assert(set->ht);
1714 for (i = 0; i < set->used; ++i) {
1715 hnode.node = set->val.nodes[i].node;
1716 hnode.type = set->val.nodes[i].type;
1717
1718 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
1719 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
1720 hash = dict_hash_multi(hash, NULL, 0);
1721
1722 assert(!lyht_find(set->ht, &hnode, hash, NULL));
1723 }
1724 }
1725
1726 return ret - 1;
1727}
1728
1729/**
1730 * @brief Remove duplicate entries in a sorted node set.
1731 *
1732 * @param[in] set Sorted set to check.
1733 * @return LY_ERR (LY_EEXIST if some duplicates are found)
1734 */
1735static LY_ERR
1736set_sorted_dup_node_clean(struct lyxp_set *set)
1737{
1738 uint32_t i = 0;
1739 LY_ERR ret = LY_SUCCESS;
1740
1741 if (set->used > 1) {
1742 while (i < set->used - 1) {
Michal Vasko69730152020-10-09 16:30:07 +02001743 if ((set->val.nodes[i].node == set->val.nodes[i + 1].node) &&
1744 (set->val.nodes[i].type == set->val.nodes[i + 1].type)) {
Michal Vasko2caefc12019-11-14 16:07:56 +01001745 set_remove_node_none(set, i + 1);
Michal Vasko57eab132019-09-24 11:46:26 +02001746 ret = LY_EEXIST;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001747 }
Michal Vasko57eab132019-09-24 11:46:26 +02001748 ++i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001749 }
1750 }
1751
Michal Vasko2caefc12019-11-14 16:07:56 +01001752 set_remove_nodes_none(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001753 return ret;
1754}
1755
1756#endif
1757
1758/**
1759 * @brief Merge 2 sorted sets into one.
1760 *
1761 * @param[in,out] trg Set to merge into. Duplicates are removed.
1762 * @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 +02001763 * @return LY_ERR
1764 */
1765static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001766set_sorted_merge(struct lyxp_set *trg, struct lyxp_set *src)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001767{
1768 uint32_t i, j, k, count, dup_count;
1769 int cmp;
1770 const struct lyd_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001771
Michal Vaskod3678892020-05-21 10:06:58 +02001772 if ((trg->type != LYXP_SET_NODE_SET) || (src->type != LYXP_SET_NODE_SET)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001773 return LY_EINVAL;
1774 }
1775
Michal Vaskod3678892020-05-21 10:06:58 +02001776 if (!src->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001777 return LY_SUCCESS;
Michal Vaskod3678892020-05-21 10:06:58 +02001778 } else if (!trg->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001779 set_fill_set(trg, src);
Michal Vaskod3678892020-05-21 10:06:58 +02001780 lyxp_set_free_content(src);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001781 return LY_SUCCESS;
1782 }
1783
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001784 /* find first top-level node to be used as anchor for positions */
Michal Vasko9e685082021-01-29 14:49:09 +01001785 for (root = trg->cur_node; root->parent; root = lyd_parent(root)) {}
Michal Vaskod989ba02020-08-24 10:59:24 +02001786 for ( ; root->prev->next; root = root->prev) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001787
1788 /* fill positions */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001789 if (set_assign_pos(trg, root, trg->root_type) || set_assign_pos(src, root, src->root_type)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001790 return LY_EINT;
1791 }
1792
1793#ifndef NDEBUG
1794 LOGDBG(LY_LDGXPATH, "MERGE target");
1795 print_set_debug(trg);
1796 LOGDBG(LY_LDGXPATH, "MERGE source");
1797 print_set_debug(src);
1798#endif
1799
1800 /* make memory for the merge (duplicates are not detected yet, so space
1801 * will likely be wasted on them, too bad) */
1802 if (trg->size - trg->used < src->used) {
1803 trg->size = trg->used + src->used;
1804
1805 trg->val.nodes = ly_realloc(trg->val.nodes, trg->size * sizeof *trg->val.nodes);
1806 LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx), LY_EMEM);
1807 }
1808
1809 i = 0;
1810 j = 0;
1811 count = 0;
1812 dup_count = 0;
1813 do {
1814 cmp = set_sort_compare(&src->val.nodes[i], &trg->val.nodes[j]);
1815 if (!cmp) {
1816 if (!count) {
1817 /* duplicate, just skip it */
1818 ++i;
1819 ++j;
1820 } else {
1821 /* we are copying something already, so let's copy the duplicate too,
1822 * we are hoping that afterwards there are some more nodes to
1823 * copy and this way we can copy them all together */
1824 ++count;
1825 ++dup_count;
1826 ++i;
1827 ++j;
1828 }
1829 } else if (cmp < 0) {
1830 /* inserting src node into trg, just remember it for now */
1831 ++count;
1832 ++i;
1833
1834 /* insert the hash now */
1835 set_insert_node_hash(trg, src->val.nodes[i - 1].node, src->val.nodes[i - 1].type);
1836 } else if (count) {
1837copy_nodes:
1838 /* time to actually copy the nodes, we have found the largest block of nodes */
1839 memmove(&trg->val.nodes[j + (count - dup_count)],
1840 &trg->val.nodes[j],
1841 (trg->used - j) * sizeof *trg->val.nodes);
1842 memcpy(&trg->val.nodes[j - dup_count], &src->val.nodes[i - count], count * sizeof *src->val.nodes);
1843
1844 trg->used += count - dup_count;
1845 /* do not change i, except the copying above, we are basically doing exactly what is in the else branch below */
1846 j += count - dup_count;
1847
1848 count = 0;
1849 dup_count = 0;
1850 } else {
1851 ++j;
1852 }
1853 } while ((i < src->used) && (j < trg->used));
1854
1855 if ((i < src->used) || count) {
1856 /* insert all the hashes first */
1857 for (k = i; k < src->used; ++k) {
1858 set_insert_node_hash(trg, src->val.nodes[k].node, src->val.nodes[k].type);
1859 }
1860
1861 /* loop ended, but we need to copy something at trg end */
1862 count += src->used - i;
1863 i = src->used;
1864 goto copy_nodes;
1865 }
1866
1867 /* we are inserting hashes before the actual node insert, which causes
1868 * situations when there were initially not enough items for a hash table,
1869 * but even after some were inserted, hash table was not created (during
1870 * insertion the number of items is not updated yet) */
1871 if (!trg->ht && (trg->used >= LYD_HT_MIN_ITEMS)) {
1872 set_insert_node_hash(trg, NULL, 0);
1873 }
1874
1875#ifndef NDEBUG
1876 LOGDBG(LY_LDGXPATH, "MERGE result");
1877 print_set_debug(trg);
1878#endif
1879
Michal Vaskod3678892020-05-21 10:06:58 +02001880 lyxp_set_free_content(src);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001881 return LY_SUCCESS;
1882}
1883
Michal Vasko14676352020-05-29 11:35:55 +02001884LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02001885lyxp_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 +02001886{
Michal Vasko004d3152020-06-11 19:59:22 +02001887 if (exp->used == tok_idx) {
Michal Vasko14676352020-05-29 11:35:55 +02001888 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001889 LOGVAL(ctx, LY_VCODE_XP_EOF);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001890 }
Michal Vasko14676352020-05-29 11:35:55 +02001891 return LY_EINCOMPLETE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001892 }
1893
Michal Vasko004d3152020-06-11 19:59:22 +02001894 if (want_tok && (exp->tokens[tok_idx] != want_tok)) {
Michal Vasko14676352020-05-29 11:35:55 +02001895 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001896 LOGVAL(ctx, LY_VCODE_XP_INTOK2, lyxp_print_token(exp->tokens[tok_idx]),
Michal Vasko0b468e62020-10-19 09:33:04 +02001897 &exp->expr[exp->tok_pos[tok_idx]], lyxp_print_token(want_tok));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001898 }
Michal Vasko14676352020-05-29 11:35:55 +02001899 return LY_ENOT;
1900 }
1901
1902 return LY_SUCCESS;
1903}
1904
Michal Vasko004d3152020-06-11 19:59:22 +02001905LY_ERR
1906lyxp_next_token(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_token want_tok)
1907{
1908 LY_CHECK_RET(lyxp_check_token(ctx, exp, *tok_idx, want_tok));
1909
1910 /* skip the token */
1911 ++(*tok_idx);
1912
1913 return LY_SUCCESS;
1914}
1915
Michal Vasko14676352020-05-29 11:35:55 +02001916/* just like lyxp_check_token() but tests for 2 tokens */
1917static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02001918exp_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 +02001919 enum lyxp_token want_tok2)
Michal Vasko14676352020-05-29 11:35:55 +02001920{
Michal Vasko004d3152020-06-11 19:59:22 +02001921 if (exp->used == tok_idx) {
Michal Vasko14676352020-05-29 11:35:55 +02001922 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001923 LOGVAL(ctx, LY_VCODE_XP_EOF);
Michal Vasko14676352020-05-29 11:35:55 +02001924 }
1925 return LY_EINCOMPLETE;
1926 }
1927
Michal Vasko004d3152020-06-11 19:59:22 +02001928 if ((exp->tokens[tok_idx] != want_tok1) && (exp->tokens[tok_idx] != want_tok2)) {
Michal Vasko14676352020-05-29 11:35:55 +02001929 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001930 LOGVAL(ctx, LY_VCODE_XP_INTOK, lyxp_print_token(exp->tokens[tok_idx]),
Michal Vasko0b468e62020-10-19 09:33:04 +02001931 &exp->expr[exp->tok_pos[tok_idx]]);
Michal Vasko14676352020-05-29 11:35:55 +02001932 }
1933 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001934 }
1935
1936 return LY_SUCCESS;
1937}
1938
Michal Vasko4911eeb2021-06-28 11:23:05 +02001939LY_ERR
1940lyxp_next_token2(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_token want_tok1,
1941 enum lyxp_token want_tok2)
1942{
1943 LY_CHECK_RET(exp_check_token2(ctx, exp, *tok_idx, want_tok1, want_tok2));
1944
1945 /* skip the token */
1946 ++(*tok_idx);
1947
1948 return LY_SUCCESS;
1949}
1950
Michal Vasko03ff5a72019-09-11 13:49:33 +02001951/**
1952 * @brief Stack operation push on the repeat array.
1953 *
1954 * @param[in] exp Expression to use.
Michal Vasko004d3152020-06-11 19:59:22 +02001955 * @param[in] tok_idx Position in the expresion \p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001956 * @param[in] repeat_op_idx Index from \p exp of the operator token. This value is pushed.
1957 */
1958static void
Michal Vasko004d3152020-06-11 19:59:22 +02001959exp_repeat_push(struct lyxp_expr *exp, uint16_t tok_idx, uint16_t repeat_op_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001960{
1961 uint16_t i;
1962
Michal Vasko004d3152020-06-11 19:59:22 +02001963 if (exp->repeat[tok_idx]) {
Radek Krejci1e008d22020-08-17 11:37:37 +02001964 for (i = 0; exp->repeat[tok_idx][i]; ++i) {}
Michal Vasko004d3152020-06-11 19:59:22 +02001965 exp->repeat[tok_idx] = realloc(exp->repeat[tok_idx], (i + 2) * sizeof *exp->repeat[tok_idx]);
1966 LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), );
1967 exp->repeat[tok_idx][i] = repeat_op_idx;
1968 exp->repeat[tok_idx][i + 1] = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001969 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02001970 exp->repeat[tok_idx] = calloc(2, sizeof *exp->repeat[tok_idx]);
1971 LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), );
1972 exp->repeat[tok_idx][0] = repeat_op_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001973 }
1974}
1975
1976/**
1977 * @brief Reparse Predicate. Logs directly on error.
1978 *
1979 * [7] Predicate ::= '[' Expr ']'
1980 *
1981 * @param[in] ctx Context for logging.
1982 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02001983 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02001984 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001985 * @return LY_ERR
1986 */
1987static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02001988reparse_predicate(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t depth)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001989{
1990 LY_ERR rc;
1991
Michal Vasko004d3152020-06-11 19:59:22 +02001992 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001993 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02001994 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001995
aPiecekbf968d92021-05-27 14:35:05 +02001996 rc = reparse_or_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001997 LY_CHECK_RET(rc);
1998
Michal Vasko004d3152020-06-11 19:59:22 +02001999 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002000 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002001 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002002
2003 return LY_SUCCESS;
2004}
2005
2006/**
2007 * @brief Reparse RelativeLocationPath. Logs directly on error.
2008 *
2009 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
2010 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
2011 * [6] NodeTest ::= NameTest | NodeType '(' ')'
2012 *
2013 * @param[in] ctx Context for logging.
2014 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002015 * @param[in] tok_idx Position in the expression \p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002016 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002017 * @return LY_ERR (LY_EINCOMPLETE on forward reference)
2018 */
2019static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002020reparse_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 +02002021{
2022 LY_ERR rc;
2023
Michal Vasko004d3152020-06-11 19:59:22 +02002024 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002025 LY_CHECK_RET(rc);
2026
2027 goto step;
2028 do {
2029 /* '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02002030 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002031
Michal Vasko004d3152020-06-11 19:59:22 +02002032 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002033 LY_CHECK_RET(rc);
2034step:
2035 /* Step */
Michal Vasko004d3152020-06-11 19:59:22 +02002036 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002037 case LYXP_TOKEN_DOT:
Michal Vasko004d3152020-06-11 19:59:22 +02002038 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002039 break;
2040
2041 case LYXP_TOKEN_DDOT:
Michal Vasko004d3152020-06-11 19:59:22 +02002042 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002043 break;
2044
2045 case LYXP_TOKEN_AT:
Michal Vasko004d3152020-06-11 19:59:22 +02002046 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002047
Michal Vasko004d3152020-06-11 19:59:22 +02002048 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002049 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002050 if ((exp->tokens[*tok_idx] != LYXP_TOKEN_NAMETEST) && (exp->tokens[*tok_idx] != LYXP_TOKEN_NODETYPE)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002051 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 +02002052 return LY_EVALID;
2053 }
Radek Krejci0f969882020-08-21 16:56:47 +02002054 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02002055 case LYXP_TOKEN_NAMETEST:
Michal Vasko004d3152020-06-11 19:59:22 +02002056 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002057 goto reparse_predicate;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002058
2059 case LYXP_TOKEN_NODETYPE:
Michal Vasko004d3152020-06-11 19:59:22 +02002060 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002061
2062 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02002063 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002064 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002065 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002066
2067 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02002068 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002069 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002070 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002071
2072reparse_predicate:
2073 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002074 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
aPiecekbf968d92021-05-27 14:35:05 +02002075 rc = reparse_predicate(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002076 LY_CHECK_RET(rc);
2077 }
2078 break;
2079 default:
Radek Krejci2efc45b2020-12-22 16:25:44 +01002080 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 +02002081 return LY_EVALID;
2082 }
Michal Vasko004d3152020-06-11 19:59:22 +02002083 } while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02002084
2085 return LY_SUCCESS;
2086}
2087
2088/**
2089 * @brief Reparse AbsoluteLocationPath. Logs directly on error.
2090 *
2091 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
2092 *
2093 * @param[in] ctx Context for logging.
2094 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002095 * @param[in] tok_idx Position in the expression \p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002096 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002097 * @return LY_ERR
2098 */
2099static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002100reparse_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 +02002101{
2102 LY_ERR rc;
2103
Michal Vasko004d3152020-06-11 19:59:22 +02002104 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 +02002105
2106 /* '/' RelativeLocationPath? */
Michal Vasko004d3152020-06-11 19:59:22 +02002107 if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002108 /* '/' */
Michal Vasko004d3152020-06-11 19:59:22 +02002109 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002110
Michal Vasko004d3152020-06-11 19:59:22 +02002111 if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002112 return LY_SUCCESS;
2113 }
Michal Vasko004d3152020-06-11 19:59:22 +02002114 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002115 case LYXP_TOKEN_DOT:
2116 case LYXP_TOKEN_DDOT:
2117 case LYXP_TOKEN_AT:
2118 case LYXP_TOKEN_NAMETEST:
2119 case LYXP_TOKEN_NODETYPE:
aPiecekbf968d92021-05-27 14:35:05 +02002120 rc = reparse_relative_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002121 LY_CHECK_RET(rc);
Radek Krejci0f969882020-08-21 16:56:47 +02002122 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02002123 default:
2124 break;
2125 }
2126
Michal Vasko03ff5a72019-09-11 13:49:33 +02002127 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02002128 /* '//' RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002129 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002130
aPiecekbf968d92021-05-27 14:35:05 +02002131 rc = reparse_relative_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002132 LY_CHECK_RET(rc);
2133 }
2134
2135 return LY_SUCCESS;
2136}
2137
2138/**
2139 * @brief Reparse FunctionCall. Logs directly on error.
2140 *
2141 * [9] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
2142 *
2143 * @param[in] ctx Context for logging.
2144 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002145 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002146 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002147 * @return LY_ERR
2148 */
2149static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002150reparse_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 +02002151{
Radek Krejci1deb5be2020-08-26 16:43:36 +02002152 int8_t min_arg_count = -1;
2153 uint32_t arg_count, max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002154 uint16_t func_tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002155 LY_ERR rc;
2156
Michal Vasko004d3152020-06-11 19:59:22 +02002157 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_FUNCNAME);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002158 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002159 func_tok_idx = *tok_idx;
2160 switch (exp->tok_len[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002161 case 3:
Michal Vasko004d3152020-06-11 19:59:22 +02002162 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002163 min_arg_count = 1;
2164 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002165 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002166 min_arg_count = 1;
2167 max_arg_count = 1;
2168 }
2169 break;
2170 case 4:
Michal Vasko004d3152020-06-11 19:59:22 +02002171 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002172 min_arg_count = 1;
2173 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002174 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002175 min_arg_count = 0;
2176 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002177 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002178 min_arg_count = 0;
2179 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002180 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002181 min_arg_count = 0;
2182 max_arg_count = 0;
2183 }
2184 break;
2185 case 5:
Michal Vasko004d3152020-06-11 19:59:22 +02002186 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002187 min_arg_count = 1;
2188 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002189 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002190 min_arg_count = 0;
2191 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002192 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002193 min_arg_count = 1;
2194 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002195 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002196 min_arg_count = 1;
2197 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002198 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002199 min_arg_count = 1;
2200 max_arg_count = 1;
2201 }
2202 break;
2203 case 6:
Michal Vasko004d3152020-06-11 19:59:22 +02002204 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002205 min_arg_count = 2;
Radek Krejci1deb5be2020-08-26 16:43:36 +02002206 max_arg_count = UINT32_MAX;
Michal Vasko004d3152020-06-11 19:59:22 +02002207 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002208 min_arg_count = 0;
2209 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002210 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002211 min_arg_count = 0;
2212 max_arg_count = 1;
2213 }
2214 break;
2215 case 7:
Michal Vasko004d3152020-06-11 19:59:22 +02002216 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002217 min_arg_count = 1;
2218 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002219 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002220 min_arg_count = 1;
2221 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002222 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002223 min_arg_count = 0;
2224 max_arg_count = 0;
2225 }
2226 break;
2227 case 8:
Michal Vasko004d3152020-06-11 19:59:22 +02002228 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002229 min_arg_count = 2;
2230 max_arg_count = 2;
Michal Vasko004d3152020-06-11 19:59:22 +02002231 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002232 min_arg_count = 0;
2233 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002234 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "re-match", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002235 min_arg_count = 2;
2236 max_arg_count = 2;
2237 }
2238 break;
2239 case 9:
Michal Vasko004d3152020-06-11 19:59:22 +02002240 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002241 min_arg_count = 2;
2242 max_arg_count = 3;
Michal Vasko004d3152020-06-11 19:59:22 +02002243 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002244 min_arg_count = 3;
2245 max_arg_count = 3;
2246 }
2247 break;
2248 case 10:
Michal Vasko004d3152020-06-11 19:59:22 +02002249 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002250 min_arg_count = 0;
2251 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002252 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002253 min_arg_count = 1;
2254 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002255 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002256 min_arg_count = 2;
2257 max_arg_count = 2;
2258 }
2259 break;
2260 case 11:
Michal Vasko004d3152020-06-11 19:59:22 +02002261 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002262 min_arg_count = 2;
2263 max_arg_count = 2;
2264 }
2265 break;
2266 case 12:
Michal Vasko004d3152020-06-11 19:59:22 +02002267 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002268 min_arg_count = 2;
2269 max_arg_count = 2;
2270 }
2271 break;
2272 case 13:
Michal Vasko004d3152020-06-11 19:59:22 +02002273 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002274 min_arg_count = 0;
2275 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002276 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string-length", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002277 min_arg_count = 0;
2278 max_arg_count = 1;
2279 }
2280 break;
2281 case 15:
Michal Vasko004d3152020-06-11 19:59:22 +02002282 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002283 min_arg_count = 0;
2284 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002285 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-after", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002286 min_arg_count = 2;
2287 max_arg_count = 2;
2288 }
2289 break;
2290 case 16:
Michal Vasko004d3152020-06-11 19:59:22 +02002291 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002292 min_arg_count = 2;
2293 max_arg_count = 2;
2294 }
2295 break;
2296 case 20:
Michal Vasko004d3152020-06-11 19:59:22 +02002297 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from-or-self", 20)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002298 min_arg_count = 2;
2299 max_arg_count = 2;
2300 }
2301 break;
2302 }
2303 if (min_arg_count == -1) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002304 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 +02002305 return LY_EINVAL;
2306 }
Michal Vasko004d3152020-06-11 19:59:22 +02002307 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002308
2309 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02002310 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002311 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002312 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002313
2314 /* ( Expr ( ',' Expr )* )? */
2315 arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002316 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002317 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002318 if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002319 ++arg_count;
aPiecekbf968d92021-05-27 14:35:05 +02002320 rc = reparse_or_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002321 LY_CHECK_RET(rc);
2322 }
Michal Vasko004d3152020-06-11 19:59:22 +02002323 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) {
2324 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002325
2326 ++arg_count;
aPiecekbf968d92021-05-27 14:35:05 +02002327 rc = reparse_or_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002328 LY_CHECK_RET(rc);
2329 }
2330
2331 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02002332 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002333 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002334 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002335
Radek Krejci857189e2020-09-01 13:26:36 +02002336 if ((arg_count < (uint32_t)min_arg_count) || (arg_count > max_arg_count)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002337 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 +02002338 return LY_EVALID;
2339 }
2340
2341 return LY_SUCCESS;
2342}
2343
2344/**
2345 * @brief Reparse PathExpr. Logs directly on error.
2346 *
2347 * [10] PathExpr ::= LocationPath | PrimaryExpr Predicate*
2348 * | PrimaryExpr Predicate* '/' RelativeLocationPath
2349 * | PrimaryExpr Predicate* '//' RelativeLocationPath
2350 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
aPiecekfba75362021-10-07 12:39:48 +02002351 * [8] PrimaryExpr ::= VariableReference | '(' Expr ')' | Literal | Number | FunctionCall
Michal Vasko03ff5a72019-09-11 13:49:33 +02002352 *
2353 * @param[in] ctx Context for logging.
2354 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002355 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002356 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002357 * @return LY_ERR
2358 */
2359static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002360reparse_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 +02002361{
2362 LY_ERR rc;
2363
Michal Vasko004d3152020-06-11 19:59:22 +02002364 if (lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko14676352020-05-29 11:35:55 +02002365 return LY_EVALID;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002366 }
2367
Michal Vasko004d3152020-06-11 19:59:22 +02002368 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002369 case LYXP_TOKEN_PAR1:
2370 /* '(' Expr ')' Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002371 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002372
aPiecekbf968d92021-05-27 14:35:05 +02002373 rc = reparse_or_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002374 LY_CHECK_RET(rc);
2375
Michal Vasko004d3152020-06-11 19:59:22 +02002376 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002377 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002378 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002379 goto predicate;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002380 case LYXP_TOKEN_DOT:
2381 case LYXP_TOKEN_DDOT:
2382 case LYXP_TOKEN_AT:
2383 case LYXP_TOKEN_NAMETEST:
2384 case LYXP_TOKEN_NODETYPE:
2385 /* RelativeLocationPath */
aPiecekbf968d92021-05-27 14:35:05 +02002386 rc = reparse_relative_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002387 LY_CHECK_RET(rc);
2388 break;
aPiecekfba75362021-10-07 12:39:48 +02002389 case LYXP_TOKEN_VARREF:
2390 /* VariableReference */
2391 ++(*tok_idx);
2392 goto predicate;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002393 case LYXP_TOKEN_FUNCNAME:
2394 /* FunctionCall */
aPiecekbf968d92021-05-27 14:35:05 +02002395 rc = reparse_function_call(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002396 LY_CHECK_RET(rc);
2397 goto predicate;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002398 case LYXP_TOKEN_OPER_PATH:
2399 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +02002400 /* AbsoluteLocationPath */
aPiecekbf968d92021-05-27 14:35:05 +02002401 rc = reparse_absolute_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002402 LY_CHECK_RET(rc);
2403 break;
2404 case LYXP_TOKEN_LITERAL:
2405 /* Literal */
Michal Vasko004d3152020-06-11 19:59:22 +02002406 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002407 goto predicate;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002408 case LYXP_TOKEN_NUMBER:
2409 /* Number */
Michal Vasko004d3152020-06-11 19:59:22 +02002410 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002411 goto predicate;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002412 default:
Radek Krejci2efc45b2020-12-22 16:25:44 +01002413 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 +02002414 return LY_EVALID;
2415 }
2416
2417 return LY_SUCCESS;
2418
2419predicate:
2420 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002421 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
aPiecekbf968d92021-05-27 14:35:05 +02002422 rc = reparse_predicate(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002423 LY_CHECK_RET(rc);
2424 }
2425
2426 /* ('/' or '//') RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002427 if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002428
2429 /* '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02002430 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002431
aPiecekbf968d92021-05-27 14:35:05 +02002432 rc = reparse_relative_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002433 LY_CHECK_RET(rc);
2434 }
2435
2436 return LY_SUCCESS;
2437}
2438
2439/**
2440 * @brief Reparse UnaryExpr. Logs directly on error.
2441 *
2442 * [17] UnaryExpr ::= UnionExpr | '-' UnaryExpr
2443 * [18] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
2444 *
2445 * @param[in] ctx Context for logging.
2446 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002447 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002448 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002449 * @return LY_ERR
2450 */
2451static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002452reparse_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 +02002453{
2454 uint16_t prev_exp;
2455 LY_ERR rc;
2456
2457 /* ('-')* */
Michal Vasko004d3152020-06-11 19:59:22 +02002458 prev_exp = *tok_idx;
Michal Vasko69730152020-10-09 16:30:07 +02002459 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2460 (exp->expr[exp->tok_pos[*tok_idx]] == '-')) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002461 exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNARY);
Michal Vasko004d3152020-06-11 19:59:22 +02002462 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002463 }
2464
2465 /* PathExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002466 prev_exp = *tok_idx;
aPiecekbf968d92021-05-27 14:35:05 +02002467 rc = reparse_path_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002468 LY_CHECK_RET(rc);
2469
2470 /* ('|' PathExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002471 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_UNI)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002472 exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNION);
Michal Vasko004d3152020-06-11 19:59:22 +02002473 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002474
aPiecekbf968d92021-05-27 14:35:05 +02002475 rc = reparse_path_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002476 LY_CHECK_RET(rc);
2477 }
2478
2479 return LY_SUCCESS;
2480}
2481
2482/**
2483 * @brief Reparse AdditiveExpr. Logs directly on error.
2484 *
2485 * [15] AdditiveExpr ::= MultiplicativeExpr
2486 * | AdditiveExpr '+' MultiplicativeExpr
2487 * | AdditiveExpr '-' MultiplicativeExpr
2488 * [16] MultiplicativeExpr ::= UnaryExpr
2489 * | MultiplicativeExpr '*' UnaryExpr
2490 * | MultiplicativeExpr 'div' UnaryExpr
2491 * | MultiplicativeExpr 'mod' UnaryExpr
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_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 +02002501{
2502 uint16_t prev_add_exp, prev_mul_exp;
2503 LY_ERR rc;
2504
Michal Vasko004d3152020-06-11 19:59:22 +02002505 prev_add_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002506 goto reparse_multiplicative_expr;
2507
2508 /* ('+' / '-' MultiplicativeExpr)* */
Michal Vasko69730152020-10-09 16:30:07 +02002509 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2510 ((exp->expr[exp->tok_pos[*tok_idx]] == '+') || (exp->expr[exp->tok_pos[*tok_idx]] == '-'))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002511 exp_repeat_push(exp, prev_add_exp, LYXP_EXPR_ADDITIVE);
Michal Vasko004d3152020-06-11 19:59:22 +02002512 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002513
2514reparse_multiplicative_expr:
2515 /* UnaryExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002516 prev_mul_exp = *tok_idx;
aPiecekbf968d92021-05-27 14:35:05 +02002517 rc = reparse_unary_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002518 LY_CHECK_RET(rc);
2519
2520 /* ('*' / 'div' / 'mod' UnaryExpr)* */
Michal Vasko69730152020-10-09 16:30:07 +02002521 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2522 ((exp->expr[exp->tok_pos[*tok_idx]] == '*') || (exp->tok_len[*tok_idx] == 3))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002523 exp_repeat_push(exp, prev_mul_exp, LYXP_EXPR_MULTIPLICATIVE);
Michal Vasko004d3152020-06-11 19:59:22 +02002524 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002525
aPiecekbf968d92021-05-27 14:35:05 +02002526 rc = reparse_unary_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002527 LY_CHECK_RET(rc);
2528 }
2529 }
2530
2531 return LY_SUCCESS;
2532}
2533
2534/**
2535 * @brief Reparse EqualityExpr. Logs directly on error.
2536 *
2537 * [13] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
2538 * | EqualityExpr '!=' RelationalExpr
2539 * [14] RelationalExpr ::= AdditiveExpr
2540 * | RelationalExpr '<' AdditiveExpr
2541 * | RelationalExpr '>' AdditiveExpr
2542 * | RelationalExpr '<=' AdditiveExpr
2543 * | RelationalExpr '>=' AdditiveExpr
2544 *
2545 * @param[in] ctx Context for logging.
2546 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002547 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002548 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002549 * @return LY_ERR
2550 */
2551static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002552reparse_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 +02002553{
2554 uint16_t prev_eq_exp, prev_rel_exp;
2555 LY_ERR rc;
2556
Michal Vasko004d3152020-06-11 19:59:22 +02002557 prev_eq_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002558 goto reparse_additive_expr;
2559
2560 /* ('=' / '!=' RelationalExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002561 while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_EQUAL, LYXP_TOKEN_OPER_NEQUAL)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002562 exp_repeat_push(exp, prev_eq_exp, LYXP_EXPR_EQUALITY);
Michal Vasko004d3152020-06-11 19:59:22 +02002563 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002564
2565reparse_additive_expr:
2566 /* AdditiveExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002567 prev_rel_exp = *tok_idx;
aPiecekbf968d92021-05-27 14:35:05 +02002568 rc = reparse_additive_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002569 LY_CHECK_RET(rc);
2570
2571 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002572 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_COMP)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002573 exp_repeat_push(exp, prev_rel_exp, LYXP_EXPR_RELATIONAL);
Michal Vasko004d3152020-06-11 19:59:22 +02002574 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002575
aPiecekbf968d92021-05-27 14:35:05 +02002576 rc = reparse_additive_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002577 LY_CHECK_RET(rc);
2578 }
2579 }
2580
2581 return LY_SUCCESS;
2582}
2583
2584/**
2585 * @brief Reparse OrExpr. Logs directly on error.
2586 *
2587 * [11] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
2588 * [12] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
2589 *
2590 * @param[in] ctx Context for logging.
2591 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002592 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002593 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002594 * @return LY_ERR
2595 */
2596static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002597reparse_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 +02002598{
2599 uint16_t prev_or_exp, prev_and_exp;
2600 LY_ERR rc;
2601
aPiecekbf968d92021-05-27 14:35:05 +02002602 ++depth;
2603 LY_CHECK_ERR_RET(depth > LYXP_MAX_BLOCK_DEPTH, LOGVAL(ctx, LY_VCODE_XP_DEPTH), LY_EINVAL);
2604
Michal Vasko004d3152020-06-11 19:59:22 +02002605 prev_or_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002606 goto reparse_equality_expr;
2607
2608 /* ('or' AndExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002609 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 +02002610 exp_repeat_push(exp, prev_or_exp, LYXP_EXPR_OR);
Michal Vasko004d3152020-06-11 19:59:22 +02002611 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002612
2613reparse_equality_expr:
2614 /* EqualityExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002615 prev_and_exp = *tok_idx;
aPiecekbf968d92021-05-27 14:35:05 +02002616 rc = reparse_equality_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002617 LY_CHECK_RET(rc);
2618
2619 /* ('and' EqualityExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002620 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 +02002621 exp_repeat_push(exp, prev_and_exp, LYXP_EXPR_AND);
Michal Vasko004d3152020-06-11 19:59:22 +02002622 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002623
aPiecekbf968d92021-05-27 14:35:05 +02002624 rc = reparse_equality_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002625 LY_CHECK_RET(rc);
2626 }
2627 }
2628
2629 return LY_SUCCESS;
2630}
Radek Krejcib1646a92018-11-02 16:08:26 +01002631
2632/**
2633 * @brief Parse NCName.
2634 *
2635 * @param[in] ncname Name to parse.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002636 * @return Length of @p ncname valid bytes.
Radek Krejcib1646a92018-11-02 16:08:26 +01002637 */
Radek Krejcid4270262019-01-07 15:07:25 +01002638static long int
Radek Krejcib1646a92018-11-02 16:08:26 +01002639parse_ncname(const char *ncname)
2640{
Radek Krejci1deb5be2020-08-26 16:43:36 +02002641 uint32_t uc;
Radek Krejcid4270262019-01-07 15:07:25 +01002642 size_t size;
2643 long int len = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01002644
2645 LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), 0);
2646 if (!is_xmlqnamestartchar(uc) || (uc == ':')) {
2647 return len;
2648 }
2649
2650 do {
2651 len += size;
Radek Krejci9a564c92019-01-07 14:53:57 +01002652 if (!*ncname) {
2653 break;
2654 }
Radek Krejcid4270262019-01-07 15:07:25 +01002655 LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), -len);
Radek Krejcib1646a92018-11-02 16:08:26 +01002656 } while (is_xmlqnamechar(uc) && (uc != ':'));
2657
2658 return len;
2659}
2660
2661/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02002662 * @brief Add @p token into the expression @p exp.
Radek Krejcib1646a92018-11-02 16:08:26 +01002663 *
Michal Vasko03ff5a72019-09-11 13:49:33 +02002664 * @param[in] ctx Context for logging.
Radek Krejcib1646a92018-11-02 16:08:26 +01002665 * @param[in] exp Expression to use.
2666 * @param[in] token Token to add.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002667 * @param[in] tok_pos Token position in the XPath expression.
Radek Krejcib1646a92018-11-02 16:08:26 +01002668 * @param[in] tok_len Token length in the XPath expression.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002669 * @return LY_ERR
Radek Krejcib1646a92018-11-02 16:08:26 +01002670 */
2671static LY_ERR
Michal Vasko14676352020-05-29 11:35:55 +02002672exp_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 +01002673{
2674 uint32_t prev;
2675
2676 if (exp->used == exp->size) {
2677 prev = exp->size;
2678 exp->size += LYXP_EXPR_SIZE_STEP;
2679 if (prev > exp->size) {
2680 LOGINT(ctx);
2681 return LY_EINT;
2682 }
2683
2684 exp->tokens = ly_realloc(exp->tokens, exp->size * sizeof *exp->tokens);
2685 LY_CHECK_ERR_RET(!exp->tokens, LOGMEM(ctx), LY_EMEM);
2686 exp->tok_pos = ly_realloc(exp->tok_pos, exp->size * sizeof *exp->tok_pos);
2687 LY_CHECK_ERR_RET(!exp->tok_pos, LOGMEM(ctx), LY_EMEM);
2688 exp->tok_len = ly_realloc(exp->tok_len, exp->size * sizeof *exp->tok_len);
2689 LY_CHECK_ERR_RET(!exp->tok_len, LOGMEM(ctx), LY_EMEM);
2690 }
2691
2692 exp->tokens[exp->used] = token;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002693 exp->tok_pos[exp->used] = tok_pos;
Radek Krejcib1646a92018-11-02 16:08:26 +01002694 exp->tok_len[exp->used] = tok_len;
2695 ++exp->used;
2696 return LY_SUCCESS;
2697}
2698
2699void
Michal Vasko14676352020-05-29 11:35:55 +02002700lyxp_expr_free(const struct ly_ctx *ctx, struct lyxp_expr *expr)
Radek Krejcib1646a92018-11-02 16:08:26 +01002701{
2702 uint16_t i;
2703
2704 if (!expr) {
2705 return;
2706 }
2707
2708 lydict_remove(ctx, expr->expr);
2709 free(expr->tokens);
2710 free(expr->tok_pos);
2711 free(expr->tok_len);
2712 if (expr->repeat) {
2713 for (i = 0; i < expr->used; ++i) {
2714 free(expr->repeat[i]);
2715 }
2716 }
2717 free(expr->repeat);
2718 free(expr);
2719}
2720
Radek Krejcif03a9e22020-09-18 20:09:31 +02002721LY_ERR
2722lyxp_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 +01002723{
Radek Krejcif03a9e22020-09-18 20:09:31 +02002724 LY_ERR ret = LY_SUCCESS;
2725 struct lyxp_expr *expr;
Radek Krejcid4270262019-01-07 15:07:25 +01002726 size_t parsed = 0, tok_len;
Radek Krejcib1646a92018-11-02 16:08:26 +01002727 enum lyxp_token tok_type;
Radek Krejci857189e2020-09-01 13:26:36 +02002728 ly_bool prev_function_check = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002729 uint16_t tok_idx = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01002730
Radek Krejcif03a9e22020-09-18 20:09:31 +02002731 assert(expr_p);
2732
2733 if (!expr_str[0]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002734 LOGVAL(ctx, LY_VCODE_XP_EOF);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002735 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02002736 }
2737
2738 if (!expr_len) {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002739 expr_len = strlen(expr_str);
Michal Vasko004d3152020-06-11 19:59:22 +02002740 }
2741 if (expr_len > UINT16_MAX) {
Michal Vasko623ac7b2021-04-09 12:44:23 +02002742 LOGVAL(ctx, LYVE_XPATH, "XPath expression cannot be longer than %u characters.", UINT16_MAX);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002743 return LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01002744 }
2745
2746 /* init lyxp_expr structure */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002747 expr = calloc(1, sizeof *expr);
2748 LY_CHECK_ERR_GOTO(!expr, LOGMEM(ctx); ret = LY_EMEM, error);
2749 LY_CHECK_GOTO(ret = lydict_insert(ctx, expr_str, expr_len, &expr->expr), error);
2750 expr->used = 0;
2751 expr->size = LYXP_EXPR_SIZE_START;
2752 expr->tokens = malloc(expr->size * sizeof *expr->tokens);
2753 LY_CHECK_ERR_GOTO(!expr->tokens, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002754
Radek Krejcif03a9e22020-09-18 20:09:31 +02002755 expr->tok_pos = malloc(expr->size * sizeof *expr->tok_pos);
2756 LY_CHECK_ERR_GOTO(!expr->tok_pos, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002757
Radek Krejcif03a9e22020-09-18 20:09:31 +02002758 expr->tok_len = malloc(expr->size * sizeof *expr->tok_len);
2759 LY_CHECK_ERR_GOTO(!expr->tok_len, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002760
Michal Vasko004d3152020-06-11 19:59:22 +02002761 /* make expr 0-terminated */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002762 expr_str = expr->expr;
Michal Vasko004d3152020-06-11 19:59:22 +02002763
Radek Krejcif03a9e22020-09-18 20:09:31 +02002764 while (is_xmlws(expr_str[parsed])) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002765 ++parsed;
2766 }
2767
2768 do {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002769 if (expr_str[parsed] == '(') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002770
2771 /* '(' */
2772 tok_len = 1;
2773 tok_type = LYXP_TOKEN_PAR1;
2774
Radek Krejcif03a9e22020-09-18 20:09:31 +02002775 if (prev_function_check && expr->used && (expr->tokens[expr->used - 1] == LYXP_TOKEN_NAMETEST)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002776 /* it is a NodeType/FunctionName after all */
Michal Vasko69730152020-10-09 16:30:07 +02002777 if (((expr->tok_len[expr->used - 1] == 4) &&
2778 (!strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "node", 4) ||
2779 !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "text", 4))) ||
2780 ((expr->tok_len[expr->used - 1] == 7) &&
2781 !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "comment", 7))) {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002782 expr->tokens[expr->used - 1] = LYXP_TOKEN_NODETYPE;
Radek Krejcib1646a92018-11-02 16:08:26 +01002783 } else {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002784 expr->tokens[expr->used - 1] = LYXP_TOKEN_FUNCNAME;
Radek Krejcib1646a92018-11-02 16:08:26 +01002785 }
2786 prev_function_check = 0;
2787 }
2788
Radek Krejcif03a9e22020-09-18 20:09:31 +02002789 } else if (expr_str[parsed] == ')') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002790
2791 /* ')' */
2792 tok_len = 1;
2793 tok_type = LYXP_TOKEN_PAR2;
2794
Radek Krejcif03a9e22020-09-18 20:09:31 +02002795 } else if (expr_str[parsed] == '[') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002796
2797 /* '[' */
2798 tok_len = 1;
2799 tok_type = LYXP_TOKEN_BRACK1;
2800
Radek Krejcif03a9e22020-09-18 20:09:31 +02002801 } else if (expr_str[parsed] == ']') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002802
2803 /* ']' */
2804 tok_len = 1;
2805 tok_type = LYXP_TOKEN_BRACK2;
2806
Radek Krejcif03a9e22020-09-18 20:09:31 +02002807 } else if (!strncmp(&expr_str[parsed], "..", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002808
2809 /* '..' */
2810 tok_len = 2;
2811 tok_type = LYXP_TOKEN_DDOT;
2812
Radek Krejcif03a9e22020-09-18 20:09:31 +02002813 } else if ((expr_str[parsed] == '.') && (!isdigit(expr_str[parsed + 1]))) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002814
2815 /* '.' */
2816 tok_len = 1;
2817 tok_type = LYXP_TOKEN_DOT;
2818
Radek Krejcif03a9e22020-09-18 20:09:31 +02002819 } else if (expr_str[parsed] == '@') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002820
2821 /* '@' */
2822 tok_len = 1;
2823 tok_type = LYXP_TOKEN_AT;
2824
Radek Krejcif03a9e22020-09-18 20:09:31 +02002825 } else if (expr_str[parsed] == ',') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002826
2827 /* ',' */
2828 tok_len = 1;
2829 tok_type = LYXP_TOKEN_COMMA;
2830
Radek Krejcif03a9e22020-09-18 20:09:31 +02002831 } else if (expr_str[parsed] == '\'') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002832
2833 /* Literal with ' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002834 for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\''); ++tok_len) {}
2835 LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0',
Radek Krejci2efc45b2020-12-22 16:25:44 +01002836 LOGVAL(ctx, LY_VCODE_XP_EOE, expr_str[parsed], &expr_str[parsed]); ret = LY_EVALID,
Michal Vasko69730152020-10-09 16:30:07 +02002837 error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002838 ++tok_len;
2839 tok_type = LYXP_TOKEN_LITERAL;
2840
Radek Krejcif03a9e22020-09-18 20:09:31 +02002841 } else if (expr_str[parsed] == '\"') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002842
2843 /* Literal with " */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002844 for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\"'); ++tok_len) {}
2845 LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0',
Radek Krejci2efc45b2020-12-22 16:25:44 +01002846 LOGVAL(ctx, LY_VCODE_XP_EOE, expr_str[parsed], &expr_str[parsed]); ret = LY_EVALID,
Michal Vasko69730152020-10-09 16:30:07 +02002847 error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002848 ++tok_len;
2849 tok_type = LYXP_TOKEN_LITERAL;
2850
Radek Krejcif03a9e22020-09-18 20:09:31 +02002851 } else if ((expr_str[parsed] == '.') || (isdigit(expr_str[parsed]))) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002852
2853 /* Number */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002854 for (tok_len = 0; isdigit(expr_str[parsed + tok_len]); ++tok_len) {}
2855 if (expr_str[parsed + tok_len] == '.') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002856 ++tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02002857 for ( ; isdigit(expr_str[parsed + tok_len]); ++tok_len) {}
Radek Krejcib1646a92018-11-02 16:08:26 +01002858 }
2859 tok_type = LYXP_TOKEN_NUMBER;
2860
aPiecekfba75362021-10-07 12:39:48 +02002861 } else if (expr_str[parsed] == '$') {
2862
2863 /* VariableReference */
2864 parsed++;
2865 long int ncname_len = parse_ncname(&expr_str[parsed]);
2866 LY_CHECK_ERR_GOTO(ncname_len < 1, LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed - ncname_len],
2867 parsed - ncname_len + 1, expr_str); ret = LY_EVALID, error);
2868 tok_len = ncname_len;
2869 LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == ':',
2870 LOGVAL(ctx, LYVE_XPATH, "Variable with prefix is not supported."); ret = LY_EVALID,
2871 error);
2872 tok_type = LYXP_TOKEN_VARREF;
2873
Radek Krejcif03a9e22020-09-18 20:09:31 +02002874 } else if (expr_str[parsed] == '/') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002875
2876 /* Operator '/', '//' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002877 if (!strncmp(&expr_str[parsed], "//", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002878 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002879 tok_type = LYXP_TOKEN_OPER_RPATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002880 } else {
2881 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002882 tok_type = LYXP_TOKEN_OPER_PATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002883 }
Radek Krejcib1646a92018-11-02 16:08:26 +01002884
Radek Krejcif03a9e22020-09-18 20:09:31 +02002885 } else if (!strncmp(&expr_str[parsed], "!=", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002886
Michal Vasko3e48bf32020-06-01 08:39:07 +02002887 /* Operator '!=' */
Radek Krejcib1646a92018-11-02 16:08:26 +01002888 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002889 tok_type = LYXP_TOKEN_OPER_NEQUAL;
2890
Radek Krejcif03a9e22020-09-18 20:09:31 +02002891 } else if (!strncmp(&expr_str[parsed], "<=", 2) || !strncmp(&expr_str[parsed], ">=", 2)) {
Michal Vasko3e48bf32020-06-01 08:39:07 +02002892
2893 /* Operator '<=', '>=' */
2894 tok_len = 2;
2895 tok_type = LYXP_TOKEN_OPER_COMP;
Radek Krejcib1646a92018-11-02 16:08:26 +01002896
Radek Krejcif03a9e22020-09-18 20:09:31 +02002897 } else if (expr_str[parsed] == '|') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002898
2899 /* Operator '|' */
2900 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002901 tok_type = LYXP_TOKEN_OPER_UNI;
Radek Krejcib1646a92018-11-02 16:08:26 +01002902
Radek Krejcif03a9e22020-09-18 20:09:31 +02002903 } else if ((expr_str[parsed] == '+') || (expr_str[parsed] == '-')) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002904
2905 /* Operator '+', '-' */
2906 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002907 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002908
Radek Krejcif03a9e22020-09-18 20:09:31 +02002909 } else if (expr_str[parsed] == '=') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002910
Michal Vasko3e48bf32020-06-01 08:39:07 +02002911 /* Operator '=' */
Radek Krejcib1646a92018-11-02 16:08:26 +01002912 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002913 tok_type = LYXP_TOKEN_OPER_EQUAL;
2914
Radek Krejcif03a9e22020-09-18 20:09:31 +02002915 } else if ((expr_str[parsed] == '<') || (expr_str[parsed] == '>')) {
Michal Vasko3e48bf32020-06-01 08:39:07 +02002916
2917 /* Operator '<', '>' */
2918 tok_len = 1;
2919 tok_type = LYXP_TOKEN_OPER_COMP;
Radek Krejcib1646a92018-11-02 16:08:26 +01002920
Michal Vasko69730152020-10-09 16:30:07 +02002921 } else if (expr->used && (expr->tokens[expr->used - 1] != LYXP_TOKEN_AT) &&
2922 (expr->tokens[expr->used - 1] != LYXP_TOKEN_PAR1) &&
2923 (expr->tokens[expr->used - 1] != LYXP_TOKEN_BRACK1) &&
2924 (expr->tokens[expr->used - 1] != LYXP_TOKEN_COMMA) &&
2925 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_LOG) &&
2926 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_EQUAL) &&
2927 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_NEQUAL) &&
2928 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_COMP) &&
2929 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_MATH) &&
2930 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_UNI) &&
2931 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_PATH) &&
2932 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_RPATH)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002933
2934 /* Operator '*', 'or', 'and', 'mod', or 'div' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002935 if (expr_str[parsed] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002936 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002937 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002938
Radek Krejcif03a9e22020-09-18 20:09:31 +02002939 } else if (!strncmp(&expr_str[parsed], "or", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002940 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002941 tok_type = LYXP_TOKEN_OPER_LOG;
Radek Krejcib1646a92018-11-02 16:08:26 +01002942
Radek Krejcif03a9e22020-09-18 20:09:31 +02002943 } else if (!strncmp(&expr_str[parsed], "and", 3)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002944 tok_len = 3;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002945 tok_type = LYXP_TOKEN_OPER_LOG;
Radek Krejcib1646a92018-11-02 16:08:26 +01002946
Radek Krejcif03a9e22020-09-18 20:09:31 +02002947 } else if (!strncmp(&expr_str[parsed], "mod", 3) || !strncmp(&expr_str[parsed], "div", 3)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002948 tok_len = 3;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002949 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002950
2951 } else if (prev_function_check) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002952 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 +02002953 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 +02002954 ret = LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01002955 goto error;
2956 } else {
Michal Vasko774ce402021-04-14 15:35:06 +02002957 LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed], parsed + 1, expr_str);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002958 ret = LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01002959 goto error;
2960 }
Radek Krejcif03a9e22020-09-18 20:09:31 +02002961 } else if (expr_str[parsed] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002962
2963 /* NameTest '*' */
2964 tok_len = 1;
2965 tok_type = LYXP_TOKEN_NAMETEST;
2966
2967 } else {
2968
2969 /* NameTest (NCName ':' '*' | QName) or NodeType/FunctionName */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002970 long int ncname_len = parse_ncname(&expr_str[parsed]);
Michal Vaskoe2be5462021-08-04 10:49:42 +02002971 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 +02002972 parsed - ncname_len + 1, expr_str); ret = LY_EVALID, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002973 tok_len = ncname_len;
2974
Radek Krejcif03a9e22020-09-18 20:09:31 +02002975 if (expr_str[parsed + tok_len] == ':') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002976 ++tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02002977 if (expr_str[parsed + tok_len] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002978 ++tok_len;
2979 } else {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002980 ncname_len = parse_ncname(&expr_str[parsed + tok_len]);
Michal Vaskoe2be5462021-08-04 10:49:42 +02002981 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 +02002982 parsed - ncname_len + 1, expr_str); ret = LY_EVALID, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002983 tok_len += ncname_len;
2984 }
2985 /* remove old flag to prevent ambiguities */
2986 prev_function_check = 0;
2987 tok_type = LYXP_TOKEN_NAMETEST;
2988 } else {
2989 /* there is no prefix so it can still be NodeType/FunctionName, we can't finally decide now */
2990 prev_function_check = 1;
2991 tok_type = LYXP_TOKEN_NAMETEST;
2992 }
2993 }
2994
2995 /* store the token, move on to the next one */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002996 LY_CHECK_GOTO(ret = exp_add_token(ctx, expr, tok_type, parsed, tok_len), error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002997 parsed += tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02002998 while (is_xmlws(expr_str[parsed])) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002999 ++parsed;
3000 }
3001
Radek Krejcif03a9e22020-09-18 20:09:31 +02003002 } while (expr_str[parsed]);
Radek Krejcib1646a92018-11-02 16:08:26 +01003003
Michal Vasko004d3152020-06-11 19:59:22 +02003004 if (reparse) {
3005 /* prealloc repeat */
Radek Krejcif03a9e22020-09-18 20:09:31 +02003006 expr->repeat = calloc(expr->size, sizeof *expr->repeat);
3007 LY_CHECK_ERR_GOTO(!expr->repeat, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01003008
Michal Vasko004d3152020-06-11 19:59:22 +02003009 /* fill repeat */
aPiecekbf968d92021-05-27 14:35:05 +02003010 LY_CHECK_ERR_GOTO(reparse_or_expr(ctx, expr, &tok_idx, 0), ret = LY_EVALID, error);
Radek Krejcif03a9e22020-09-18 20:09:31 +02003011 if (expr->used > tok_idx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003012 LOGVAL(ctx, LYVE_XPATH, "Unparsed characters \"%s\" left at the end of an XPath expression.",
Michal Vasko69730152020-10-09 16:30:07 +02003013 &expr->expr[expr->tok_pos[tok_idx]]);
Radek Krejcif03a9e22020-09-18 20:09:31 +02003014 ret = LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02003015 goto error;
3016 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003017 }
3018
Radek Krejcif03a9e22020-09-18 20:09:31 +02003019 print_expr_struct_debug(expr);
3020 *expr_p = expr;
3021 return LY_SUCCESS;
Radek Krejcib1646a92018-11-02 16:08:26 +01003022
3023error:
Radek Krejcif03a9e22020-09-18 20:09:31 +02003024 lyxp_expr_free(ctx, expr);
3025 return ret;
Radek Krejcib1646a92018-11-02 16:08:26 +01003026}
3027
Michal Vasko1734be92020-09-22 08:55:10 +02003028LY_ERR
3029lyxp_expr_dup(const struct ly_ctx *ctx, const struct lyxp_expr *exp, struct lyxp_expr **dup_p)
Michal Vasko004d3152020-06-11 19:59:22 +02003030{
Michal Vasko1734be92020-09-22 08:55:10 +02003031 LY_ERR ret = LY_SUCCESS;
3032 struct lyxp_expr *dup = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02003033 uint32_t i, j;
3034
Michal Vasko7f45cf22020-10-01 12:49:44 +02003035 if (!exp) {
3036 goto cleanup;
3037 }
3038
Michal Vasko004d3152020-06-11 19:59:22 +02003039 dup = calloc(1, sizeof *dup);
Michal Vasko1734be92020-09-22 08:55:10 +02003040 LY_CHECK_ERR_GOTO(!dup, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02003041
Michal Vasko08e9b112021-06-11 15:41:17 +02003042 if (exp->used) {
3043 dup->tokens = malloc(exp->used * sizeof *dup->tokens);
3044 LY_CHECK_ERR_GOTO(!dup->tokens, LOGMEM(ctx); ret = LY_EMEM, cleanup);
3045 memcpy(dup->tokens, exp->tokens, exp->used * sizeof *dup->tokens);
Michal Vasko004d3152020-06-11 19:59:22 +02003046
Michal Vasko08e9b112021-06-11 15:41:17 +02003047 dup->tok_pos = malloc(exp->used * sizeof *dup->tok_pos);
3048 LY_CHECK_ERR_GOTO(!dup->tok_pos, LOGMEM(ctx); ret = LY_EMEM, cleanup);
3049 memcpy(dup->tok_pos, exp->tok_pos, exp->used * sizeof *dup->tok_pos);
Michal Vasko004d3152020-06-11 19:59:22 +02003050
Michal Vasko08e9b112021-06-11 15:41:17 +02003051 dup->tok_len = malloc(exp->used * sizeof *dup->tok_len);
3052 LY_CHECK_ERR_GOTO(!dup->tok_len, LOGMEM(ctx); ret = LY_EMEM, cleanup);
3053 memcpy(dup->tok_len, exp->tok_len, exp->used * sizeof *dup->tok_len);
Michal Vasko004d3152020-06-11 19:59:22 +02003054
Michal Vasko08e9b112021-06-11 15:41:17 +02003055 dup->repeat = malloc(exp->used * sizeof *dup->repeat);
3056 LY_CHECK_ERR_GOTO(!dup->repeat, LOGMEM(ctx); ret = LY_EMEM, cleanup);
3057 for (i = 0; i < exp->used; ++i) {
3058 if (!exp->repeat[i]) {
3059 dup->repeat[i] = NULL;
3060 } else {
3061 for (j = 0; exp->repeat[i][j]; ++j) {}
3062 /* the ending 0 as well */
3063 ++j;
Michal Vasko004d3152020-06-11 19:59:22 +02003064
Michal Vasko08e9b112021-06-11 15:41:17 +02003065 dup->repeat[i] = malloc(j * sizeof **dup->repeat);
3066 LY_CHECK_ERR_GOTO(!dup->repeat[i], LOGMEM(ctx); ret = LY_EMEM, cleanup);
3067 memcpy(dup->repeat[i], exp->repeat[i], j * sizeof **dup->repeat);
3068 dup->repeat[i][j - 1] = 0;
3069 }
Michal Vasko004d3152020-06-11 19:59:22 +02003070 }
3071 }
3072
3073 dup->used = exp->used;
3074 dup->size = exp->used;
Michal Vasko1734be92020-09-22 08:55:10 +02003075 LY_CHECK_GOTO(ret = lydict_insert(ctx, exp->expr, 0, &dup->expr), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02003076
Michal Vasko1734be92020-09-22 08:55:10 +02003077cleanup:
3078 if (ret) {
3079 lyxp_expr_free(ctx, dup);
3080 } else {
3081 *dup_p = dup;
3082 }
3083 return ret;
Michal Vasko004d3152020-06-11 19:59:22 +02003084}
3085
Michal Vasko03ff5a72019-09-11 13:49:33 +02003086/**
3087 * @brief Get the last-added schema node that is currently in the context.
3088 *
3089 * @param[in] set Set to search in.
3090 * @return Last-added schema context node, NULL if no node is in context.
3091 */
3092static struct lysc_node *
3093warn_get_scnode_in_ctx(struct lyxp_set *set)
3094{
3095 uint32_t i;
3096
3097 if (!set || (set->type != LYXP_SET_SCNODE_SET)) {
3098 return NULL;
3099 }
3100
3101 i = set->used;
3102 do {
3103 --i;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003104 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003105 /* if there are more, simply return the first found (last added) */
3106 return set->val.scnodes[i].scnode;
3107 }
3108 } while (i);
3109
3110 return NULL;
3111}
3112
3113/**
3114 * @brief Test whether a type is numeric - integer type or decimal64.
3115 *
3116 * @param[in] type Type to test.
Radek Krejci857189e2020-09-01 13:26:36 +02003117 * @return Boolean value whether @p type is numeric type or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003118 */
Radek Krejci857189e2020-09-01 13:26:36 +02003119static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003120warn_is_numeric_type(struct lysc_type *type)
3121{
3122 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003123 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003124 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003125
3126 switch (type->basetype) {
3127 case LY_TYPE_DEC64:
3128 case LY_TYPE_INT8:
3129 case LY_TYPE_UINT8:
3130 case LY_TYPE_INT16:
3131 case LY_TYPE_UINT16:
3132 case LY_TYPE_INT32:
3133 case LY_TYPE_UINT32:
3134 case LY_TYPE_INT64:
3135 case LY_TYPE_UINT64:
3136 return 1;
3137 case LY_TYPE_UNION:
3138 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003139 LY_ARRAY_FOR(uni->types, u) {
3140 ret = warn_is_numeric_type(uni->types[u]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003141 if (ret) {
3142 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003143 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003144 }
3145 }
3146 /* did not find any suitable type */
3147 return 0;
3148 case LY_TYPE_LEAFREF:
3149 return warn_is_numeric_type(((struct lysc_type_leafref *)type)->realtype);
3150 default:
3151 return 0;
3152 }
3153}
3154
3155/**
3156 * @brief Test whether a type is string-like - no integers, decimal64 or binary.
3157 *
3158 * @param[in] type Type to test.
Radek Krejci857189e2020-09-01 13:26:36 +02003159 * @return Boolean value whether @p type's basetype is string type or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003160 */
Radek Krejci857189e2020-09-01 13:26:36 +02003161static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003162warn_is_string_type(struct lysc_type *type)
3163{
3164 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003165 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003166 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003167
3168 switch (type->basetype) {
3169 case LY_TYPE_BITS:
3170 case LY_TYPE_ENUM:
3171 case LY_TYPE_IDENT:
3172 case LY_TYPE_INST:
3173 case LY_TYPE_STRING:
3174 return 1;
3175 case LY_TYPE_UNION:
3176 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003177 LY_ARRAY_FOR(uni->types, u) {
3178 ret = warn_is_string_type(uni->types[u]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003179 if (ret) {
3180 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003181 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003182 }
3183 }
3184 /* did not find any suitable type */
3185 return 0;
3186 case LY_TYPE_LEAFREF:
3187 return warn_is_string_type(((struct lysc_type_leafref *)type)->realtype);
3188 default:
3189 return 0;
3190 }
3191}
3192
3193/**
3194 * @brief Test whether a type is one specific type.
3195 *
3196 * @param[in] type Type to test.
3197 * @param[in] base Expected type.
Radek Krejci857189e2020-09-01 13:26:36 +02003198 * @return Boolean value whether the given @p type is of the specific basetype @p base.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003199 */
Radek Krejci857189e2020-09-01 13:26:36 +02003200static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003201warn_is_specific_type(struct lysc_type *type, LY_DATA_TYPE base)
3202{
3203 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003204 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003205 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003206
3207 if (type->basetype == base) {
3208 return 1;
3209 } else if (type->basetype == LY_TYPE_UNION) {
3210 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003211 LY_ARRAY_FOR(uni->types, u) {
3212 ret = warn_is_specific_type(uni->types[u], base);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003213 if (ret) {
3214 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003215 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003216 }
3217 }
3218 /* did not find any suitable type */
3219 return 0;
3220 } else if (type->basetype == LY_TYPE_LEAFREF) {
3221 return warn_is_specific_type(((struct lysc_type_leafref *)type)->realtype, base);
3222 }
3223
3224 return 0;
3225}
3226
3227/**
3228 * @brief Get next type of a (union) type.
3229 *
3230 * @param[in] type Base type.
3231 * @param[in] prev_type Previously returned type.
3232 * @return Next type or NULL.
3233 */
3234static struct lysc_type *
3235warn_is_equal_type_next_type(struct lysc_type *type, struct lysc_type *prev_type)
3236{
3237 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003238 ly_bool found = 0;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003239 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003240
3241 switch (type->basetype) {
3242 case LY_TYPE_UNION:
3243 uni = (struct lysc_type_union *)type;
3244 if (!prev_type) {
3245 return uni->types[0];
3246 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003247 LY_ARRAY_FOR(uni->types, u) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003248 if (found) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003249 return uni->types[u];
Michal Vasko03ff5a72019-09-11 13:49:33 +02003250 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003251 if (prev_type == uni->types[u]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003252 found = 1;
3253 }
3254 }
3255 return NULL;
3256 default:
3257 if (prev_type) {
3258 assert(type == prev_type);
3259 return NULL;
3260 } else {
3261 return type;
3262 }
3263 }
3264}
3265
3266/**
3267 * @brief Test whether 2 types have a common type.
3268 *
3269 * @param[in] type1 First type.
3270 * @param[in] type2 Second type.
3271 * @return 1 if they do, 0 otherwise.
3272 */
3273static int
3274warn_is_equal_type(struct lysc_type *type1, struct lysc_type *type2)
3275{
3276 struct lysc_type *t1, *rt1, *t2, *rt2;
3277
3278 t1 = NULL;
3279 while ((t1 = warn_is_equal_type_next_type(type1, t1))) {
3280 if (t1->basetype == LY_TYPE_LEAFREF) {
3281 rt1 = ((struct lysc_type_leafref *)t1)->realtype;
3282 } else {
3283 rt1 = t1;
3284 }
3285
3286 t2 = NULL;
3287 while ((t2 = warn_is_equal_type_next_type(type2, t2))) {
3288 if (t2->basetype == LY_TYPE_LEAFREF) {
3289 rt2 = ((struct lysc_type_leafref *)t2)->realtype;
3290 } else {
3291 rt2 = t2;
3292 }
3293
3294 if (rt2->basetype == rt1->basetype) {
3295 /* match found */
3296 return 1;
3297 }
3298 }
3299 }
3300
3301 return 0;
3302}
3303
3304/**
3305 * @brief Check both operands of comparison operators.
3306 *
3307 * @param[in] ctx Context for errors.
3308 * @param[in] set1 First operand set.
3309 * @param[in] set2 Second operand set.
3310 * @param[in] numbers_only Whether accept only numbers or other types are fine too (for '=' and '!=').
3311 * @param[in] expr Start of the expression to print with the warning.
3312 * @param[in] tok_pos Token position.
3313 */
3314static void
Radek Krejci857189e2020-09-01 13:26:36 +02003315warn_operands(struct ly_ctx *ctx, struct lyxp_set *set1, struct lyxp_set *set2, ly_bool numbers_only, const char *expr, uint16_t tok_pos)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003316{
3317 struct lysc_node_leaf *node1, *node2;
Radek Krejci857189e2020-09-01 13:26:36 +02003318 ly_bool leaves = 1, warning = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003319
3320 node1 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set1);
3321 node2 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set2);
3322
3323 if (!node1 && !node2) {
3324 /* no node-sets involved, nothing to do */
3325 return;
3326 }
3327
3328 if (node1) {
3329 if (!(node1->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3330 LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node1->nodetype), node1->name);
3331 warning = 1;
3332 leaves = 0;
3333 } else if (numbers_only && !warn_is_numeric_type(node1->type)) {
3334 LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node1->name);
3335 warning = 1;
3336 }
3337 }
3338
3339 if (node2) {
3340 if (!(node2->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3341 LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node2->nodetype), node2->name);
3342 warning = 1;
3343 leaves = 0;
3344 } else if (numbers_only && !warn_is_numeric_type(node2->type)) {
3345 LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node2->name);
3346 warning = 1;
3347 }
3348 }
3349
3350 if (node1 && node2 && leaves && !numbers_only) {
Michal Vasko69730152020-10-09 16:30:07 +02003351 if ((warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type)) ||
3352 (!warn_is_numeric_type(node1->type) && warn_is_numeric_type(node2->type)) ||
3353 (!warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type) &&
3354 !warn_is_equal_type(node1->type, node2->type))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003355 LOGWRN(ctx, "Incompatible types of operands \"%s\" and \"%s\" for comparison.", node1->name, node2->name);
3356 warning = 1;
3357 }
3358 }
3359
3360 if (warning) {
3361 LOGWRN(ctx, "Previous warning generated by XPath subexpression[%u] \"%.20s\".", tok_pos, expr + tok_pos);
3362 }
3363}
3364
3365/**
3366 * @brief Check that a value is valid for a leaf. If not applicable, does nothing.
3367 *
3368 * @param[in] exp Parsed XPath expression.
3369 * @param[in] set Set with the leaf/leaf-list.
3370 * @param[in] val_exp Index of the value (literal/number) in @p exp.
3371 * @param[in] equal_exp Index of the start of the equality expression in @p exp.
3372 * @param[in] last_equal_exp Index of the end of the equality expression in @p exp.
3373 */
3374static void
Michal Vasko40308e72020-10-20 16:38:40 +02003375warn_equality_value(const struct lyxp_expr *exp, struct lyxp_set *set, uint16_t val_exp, uint16_t equal_exp,
3376 uint16_t last_equal_exp)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003377{
3378 struct lysc_node *scnode;
3379 struct lysc_type *type;
3380 char *value;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003381 struct lyd_value storage;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003382 LY_ERR rc;
3383 struct ly_err_item *err = NULL;
3384
Michal Vasko69730152020-10-09 16:30:07 +02003385 if ((scnode = warn_get_scnode_in_ctx(set)) && (scnode->nodetype & (LYS_LEAF | LYS_LEAFLIST)) &&
3386 ((exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) || (exp->tokens[val_exp] == LYXP_TOKEN_NUMBER))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003387 /* check that the node can have the specified value */
3388 if (exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) {
3389 value = strndup(exp->expr + exp->tok_pos[val_exp] + 1, exp->tok_len[val_exp] - 2);
3390 } else {
3391 value = strndup(exp->expr + exp->tok_pos[val_exp], exp->tok_len[val_exp]);
3392 }
3393 if (!value) {
3394 LOGMEM(set->ctx);
3395 return;
3396 }
3397
3398 if ((((struct lysc_node_leaf *)scnode)->type->basetype == LY_TYPE_IDENT) && !strchr(value, ':')) {
3399 LOGWRN(set->ctx, "Identityref \"%s\" comparison with identity \"%s\" without prefix, consider adding"
Michal Vasko69730152020-10-09 16:30:07 +02003400 " a prefix or best using \"derived-from(-or-self)()\" functions.", scnode->name, value);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003401 LOGWRN(set->ctx, "Previous warning generated by XPath subexpression[%u] \"%.*s\".", exp->tok_pos[equal_exp],
Radek Krejci0f969882020-08-21 16:56:47 +02003402 (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp],
Michal Vasko69730152020-10-09 16:30:07 +02003403 exp->expr + exp->tok_pos[equal_exp]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003404 }
3405
3406 type = ((struct lysc_node_leaf *)scnode)->type;
3407 if (type->basetype != LY_TYPE_IDENT) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003408 rc = type->plugin->store(set->ctx, type, value, strlen(value), 0, set->format, set->prefix_data,
Michal Vasko405cc9e2020-12-01 12:01:27 +01003409 LYD_HINT_DATA, scnode, &storage, NULL, &err);
Michal Vaskobf42e832020-11-23 16:59:42 +01003410 if (rc == LY_EINCOMPLETE) {
3411 rc = LY_SUCCESS;
3412 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003413
3414 if (err) {
3415 LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type (%s).", value, err->msg);
3416 ly_err_free(err);
3417 } else if (rc != LY_SUCCESS) {
3418 LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type.", value);
3419 }
3420 if (rc != LY_SUCCESS) {
3421 LOGWRN(set->ctx, "Previous warning generated by XPath subexpression[%u] \"%.*s\".", exp->tok_pos[equal_exp],
Radek Krejci0f969882020-08-21 16:56:47 +02003422 (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp],
Michal Vasko69730152020-10-09 16:30:07 +02003423 exp->expr + exp->tok_pos[equal_exp]);
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003424 } else {
3425 type->plugin->free(set->ctx, &storage);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003426 }
3427 }
3428 free(value);
3429 }
3430}
3431
3432/*
3433 * XPath functions
3434 */
3435
3436/**
3437 * @brief Execute the YANG 1.1 bit-is-set(node-set, string) function. Returns LYXP_SET_BOOLEAN
3438 * depending on whether the first node bit value from the second argument is set.
3439 *
3440 * @param[in] args Array of arguments.
3441 * @param[in] arg_count Count of elements in @p args.
3442 * @param[in,out] set Context and result set at the same time.
3443 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003444 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003445 */
3446static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003447xpath_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 +02003448{
3449 struct lyd_node_term *leaf;
3450 struct lysc_node_leaf *sleaf;
Michal Vasko2588b952021-07-29 07:43:26 +02003451 struct lyd_value_bits *bits;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003452 LY_ERR rc = LY_SUCCESS;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003453 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003454
3455 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003456 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003457 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003458 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3459 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3460 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3461 sleaf->name);
3462 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_BITS)) {
3463 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"bits\".", __func__, sleaf->name);
3464 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003465 }
3466
3467 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3468 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003469 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3470 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003471 } else if (!warn_is_string_type(sleaf->type)) {
3472 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003473 }
3474 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003475 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003476 return rc;
3477 }
3478
Michal Vaskod3678892020-05-21 10:06:58 +02003479 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003480 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 +02003481 return LY_EVALID;
3482 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003483 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003484 LY_CHECK_RET(rc);
3485
3486 set_fill_boolean(set, 0);
Michal Vaskod3678892020-05-21 10:06:58 +02003487 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003488 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
Michal Vasko2588b952021-07-29 07:43:26 +02003489 if ((leaf->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (leaf->value.realtype->basetype == LY_TYPE_BITS)) {
3490 LYD_VALUE_GET(&leaf->value, bits);
3491 LY_ARRAY_FOR(bits->items, u) {
3492 if (!strcmp(bits->items[u]->name, args[1]->val.str)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003493 set_fill_boolean(set, 1);
3494 break;
3495 }
3496 }
3497 }
3498 }
3499
3500 return LY_SUCCESS;
3501}
3502
3503/**
3504 * @brief Execute the XPath boolean(object) function. Returns LYXP_SET_BOOLEAN
3505 * with the argument converted to boolean.
3506 *
3507 * @param[in] args Array of arguments.
3508 * @param[in] arg_count Count of elements in @p args.
3509 * @param[in,out] set Context and result set at the same time.
3510 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003511 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003512 */
3513static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003514xpath_boolean(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003515{
3516 LY_ERR rc;
3517
3518 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02003519 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003520 return LY_SUCCESS;
3521 }
3522
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003523 rc = lyxp_set_cast(args[0], LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003524 LY_CHECK_RET(rc);
3525 set_fill_set(set, args[0]);
3526
3527 return LY_SUCCESS;
3528}
3529
3530/**
3531 * @brief Execute the XPath ceiling(number) function. Returns LYXP_SET_NUMBER
3532 * with the first argument rounded up to the nearest integer.
3533 *
3534 * @param[in] args Array of arguments.
3535 * @param[in] arg_count Count of elements in @p args.
3536 * @param[in,out] set Context and result set at the same time.
3537 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003538 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003539 */
3540static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003541xpath_ceiling(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003542{
3543 struct lysc_node_leaf *sleaf;
3544 LY_ERR rc = LY_SUCCESS;
3545
3546 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003547 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003548 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003549 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3550 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3551 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3552 sleaf->name);
3553 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
3554 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
3555 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003556 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003557 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003558 return rc;
3559 }
3560
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003561 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003562 LY_CHECK_RET(rc);
3563 if ((long long)args[0]->val.num != args[0]->val.num) {
3564 set_fill_number(set, ((long long)args[0]->val.num) + 1);
3565 } else {
3566 set_fill_number(set, args[0]->val.num);
3567 }
3568
3569 return LY_SUCCESS;
3570}
3571
3572/**
3573 * @brief Execute the XPath concat(string, string, string*) function.
3574 * Returns LYXP_SET_STRING with the concatenation of all the arguments.
3575 *
3576 * @param[in] args Array of arguments.
3577 * @param[in] arg_count Count of elements in @p args.
3578 * @param[in,out] set Context and result set at the same time.
3579 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003580 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003581 */
3582static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003583xpath_concat(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003584{
3585 uint16_t i;
3586 char *str = NULL;
3587 size_t used = 1;
3588 LY_ERR rc = LY_SUCCESS;
3589 struct lysc_node_leaf *sleaf;
3590
3591 if (options & LYXP_SCNODE_ALL) {
3592 for (i = 0; i < arg_count; ++i) {
3593 if ((args[i]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[i]))) {
3594 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3595 LOGWRN(set->ctx, "Argument #%u of %s is a %s node \"%s\".",
Michal Vasko69730152020-10-09 16:30:07 +02003596 i + 1, __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003597 } else if (!warn_is_string_type(sleaf->type)) {
Radek Krejci70124c82020-08-14 22:17:03 +02003598 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 +02003599 }
3600 }
3601 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003602 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003603 return rc;
3604 }
3605
3606 for (i = 0; i < arg_count; ++i) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003607 rc = lyxp_set_cast(args[i], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003608 if (rc != LY_SUCCESS) {
3609 free(str);
3610 return rc;
3611 }
3612
3613 str = ly_realloc(str, (used + strlen(args[i]->val.str)) * sizeof(char));
3614 LY_CHECK_ERR_RET(!str, LOGMEM(set->ctx), LY_EMEM);
3615 strcpy(str + used - 1, args[i]->val.str);
3616 used += strlen(args[i]->val.str);
3617 }
3618
3619 /* free, kind of */
Michal Vaskod3678892020-05-21 10:06:58 +02003620 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003621 set->type = LYXP_SET_STRING;
3622 set->val.str = str;
3623
3624 return LY_SUCCESS;
3625}
3626
3627/**
3628 * @brief Execute the XPath contains(string, string) function.
3629 * Returns LYXP_SET_BOOLEAN whether the second argument can
3630 * be found in the first or not.
3631 *
3632 * @param[in] args Array of arguments.
3633 * @param[in] arg_count Count of elements in @p args.
3634 * @param[in,out] set Context and result set at the same time.
3635 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003636 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003637 */
3638static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003639xpath_contains(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003640{
3641 struct lysc_node_leaf *sleaf;
3642 LY_ERR rc = LY_SUCCESS;
3643
3644 if (options & LYXP_SCNODE_ALL) {
3645 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3646 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003647 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3648 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003649 } else if (!warn_is_string_type(sleaf->type)) {
3650 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003651 }
3652 }
3653
3654 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3655 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003656 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3657 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003658 } else if (!warn_is_string_type(sleaf->type)) {
3659 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003660 }
3661 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003662 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003663 return rc;
3664 }
3665
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003666 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003667 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003668 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003669 LY_CHECK_RET(rc);
3670
3671 if (strstr(args[0]->val.str, args[1]->val.str)) {
3672 set_fill_boolean(set, 1);
3673 } else {
3674 set_fill_boolean(set, 0);
3675 }
3676
3677 return LY_SUCCESS;
3678}
3679
3680/**
3681 * @brief Execute the XPath count(node-set) function. Returns LYXP_SET_NUMBER
3682 * with the size of the node-set from the argument.
3683 *
3684 * @param[in] args Array of arguments.
3685 * @param[in] arg_count Count of elements in @p args.
3686 * @param[in,out] set Context and result set at the same time.
3687 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003688 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003689 */
3690static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003691xpath_count(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003692{
Michal Vasko03ff5a72019-09-11 13:49:33 +02003693 LY_ERR rc = LY_SUCCESS;
3694
3695 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003696 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003697 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003698 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003699 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003700 return rc;
3701 }
3702
Michal Vasko03ff5a72019-09-11 13:49:33 +02003703 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003704 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "count(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003705 return LY_EVALID;
3706 }
3707
3708 set_fill_number(set, args[0]->used);
3709 return LY_SUCCESS;
3710}
3711
3712/**
3713 * @brief Execute the XPath current() function. Returns LYXP_SET_NODE_SET
3714 * with the context with the intial node.
3715 *
3716 * @param[in] args Array of arguments.
3717 * @param[in] arg_count Count of elements in @p args.
3718 * @param[in,out] set Context and result set at the same time.
3719 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003720 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003721 */
3722static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003723xpath_current(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003724{
3725 if (arg_count || args) {
Radek Krejcie87c7dc2021-06-02 21:25:42 +02003726 LOGVAL(set->ctx, LY_VCODE_XP_INARGCOUNT, arg_count, LY_PRI_LENSTR("current()"));
Michal Vasko03ff5a72019-09-11 13:49:33 +02003727 return LY_EVALID;
3728 }
3729
3730 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02003731 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003732
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003733 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, set->cur_scnode, LYXP_NODE_ELEM, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02003734 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02003735 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003736
3737 /* position is filled later */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003738 set_insert_node(set, set->cur_node, 0, LYXP_NODE_ELEM, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003739 }
3740
3741 return LY_SUCCESS;
3742}
3743
3744/**
3745 * @brief Execute the YANG 1.1 deref(node-set) function. Returns LYXP_SET_NODE_SET with either
3746 * leafref or instance-identifier target node(s).
3747 *
3748 * @param[in] args Array of arguments.
3749 * @param[in] arg_count Count of elements in @p args.
3750 * @param[in,out] set Context and result set at the same time.
3751 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003752 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003753 */
3754static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003755xpath_deref(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003756{
3757 struct lyd_node_term *leaf;
Michal Vasko42e497c2020-01-06 08:38:25 +01003758 struct lysc_node_leaf *sleaf = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02003759 struct lysc_type_leafref *lref;
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003760 const struct lysc_node *target;
Michal Vasko004d3152020-06-11 19:59:22 +02003761 struct ly_path *p;
3762 struct lyd_node *node;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003763 char *errmsg = NULL;
Michal Vasko00cbf532020-06-15 13:58:47 +02003764 uint8_t oper;
Michal Vasko741bb562021-06-24 11:59:50 +02003765 LY_ERR r;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003766
3767 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003768 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003769 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003770 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
Michal Vasko423ba3f2021-07-19 13:08:50 +02003771 if (!(sleaf->nodetype & LYD_NODE_TERM)) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003772 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3773 sleaf->name);
Michal Vaskoed725d72021-06-23 12:03:45 +02003774 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_LEAFREF) &&
3775 !warn_is_specific_type(sleaf->type, LY_TYPE_INST)) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003776 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"leafref\" nor \"instance-identifier\".",
3777 __func__, sleaf->name);
3778 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003779 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003780 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko423ba3f2021-07-19 13:08:50 +02003781 if (sleaf && (sleaf->nodetype & LYD_NODE_TERM) && (sleaf->type->basetype == LY_TYPE_LEAFREF)) {
Michal Vasko004d3152020-06-11 19:59:22 +02003782 lref = (struct lysc_type_leafref *)sleaf->type;
Michal Vaskod1e53b92021-01-28 13:11:06 +01003783 oper = (sleaf->flags & LYS_IS_OUTPUT) ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT;
Michal Vasko004d3152020-06-11 19:59:22 +02003784
3785 /* it was already evaluated on schema, it must succeed */
Michal Vasko741bb562021-06-24 11:59:50 +02003786 r = ly_path_compile_leafref(set->ctx, &sleaf->node, NULL, lref->path, oper, LY_PATH_TARGET_MANY,
Michal Vasko24fc4d12021-07-12 14:41:20 +02003787 LY_VALUE_SCHEMA_RESOLVED, lref->prefixes, &p);
Michal Vasko741bb562021-06-24 11:59:50 +02003788 if (!r) {
3789 /* get the target node */
3790 target = p[LY_ARRAY_COUNT(p) - 1].node;
3791 ly_path_free(set->ctx, p);
Michal Vasko004d3152020-06-11 19:59:22 +02003792
Michal Vasko741bb562021-06-24 11:59:50 +02003793 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, target, LYXP_NODE_ELEM, NULL));
3794 } /* else the target was found before but is disabled so it was removed */
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003795 }
3796
Michal Vasko741bb562021-06-24 11:59:50 +02003797 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003798 }
3799
Michal Vaskod3678892020-05-21 10:06:58 +02003800 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003801 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "deref(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003802 return LY_EVALID;
3803 }
3804
Michal Vaskod3678892020-05-21 10:06:58 +02003805 lyxp_set_free_content(set);
3806 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003807 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
3808 sleaf = (struct lysc_node_leaf *)leaf->schema;
3809 if (sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
3810 if (sleaf->type->basetype == LY_TYPE_LEAFREF) {
3811 /* find leafref target */
Radek Krejci0b013302021-03-29 15:22:32 +02003812 if (lyplg_type_resolve_leafref((struct lysc_type_leafref *)sleaf->type, &leaf->node, &leaf->value, set->tree,
Michal Vasko9e685082021-01-29 14:49:09 +01003813 &node, &errmsg)) {
Michal Vasko004d3152020-06-11 19:59:22 +02003814 LOGERR(set->ctx, LY_EVALID, errmsg);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003815 free(errmsg);
Michal Vasko004d3152020-06-11 19:59:22 +02003816 return LY_EVALID;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003817 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003818 } else {
3819 assert(sleaf->type->basetype == LY_TYPE_INST);
Michal Vasko004d3152020-06-11 19:59:22 +02003820 if (ly_path_eval(leaf->value.target, set->tree, &node)) {
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003821 LOGERR(set->ctx, LY_EVALID, "Invalid instance-identifier \"%s\" value - required instance not found.",
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02003822 lyd_get_value(&leaf->node));
Michal Vasko03ff5a72019-09-11 13:49:33 +02003823 return LY_EVALID;
3824 }
3825 }
Michal Vasko004d3152020-06-11 19:59:22 +02003826
3827 /* insert it */
3828 set_insert_node(set, node, 0, LYXP_NODE_ELEM, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003829 }
3830 }
3831
3832 return LY_SUCCESS;
3833}
3834
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003835static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02003836xpath_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 +02003837{
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01003838 uint32_t i;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003839 LY_ARRAY_COUNT_TYPE u;
3840 struct lyd_node_term *leaf;
3841 struct lysc_node_leaf *sleaf;
3842 struct lyd_meta *meta;
Michal Vasko93923692021-05-07 15:28:02 +02003843 struct lyd_value *val;
3844 const struct lys_module *mod;
3845 const char *id_name;
3846 uint16_t id_len;
3847 struct lysc_ident *id;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003848 LY_ERR rc = LY_SUCCESS;
Radek Krejci857189e2020-09-01 13:26:36 +02003849 ly_bool found;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003850
3851 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003852 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003853 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", func);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003854 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3855 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3856 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype),
3857 sleaf->name);
3858 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_IDENT)) {
3859 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"identityref\".", func, sleaf->name);
3860 }
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003861 }
3862
3863 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3864 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3865 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype),
Michal Vasko69730152020-10-09 16:30:07 +02003866 sleaf->name);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003867 } else if (!warn_is_string_type(sleaf->type)) {
3868 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", func, sleaf->name);
3869 }
3870 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003871 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003872 return rc;
3873 }
3874
3875 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003876 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 +02003877 return LY_EVALID;
3878 }
3879 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
3880 LY_CHECK_RET(rc);
3881
Michal Vasko93923692021-05-07 15:28:02 +02003882 /* parse the identity */
3883 id_name = args[1]->val.str;
3884 id_len = strlen(id_name);
3885 rc = moveto_resolve_model(&id_name, &id_len, set, set->cur_node ? set->cur_node->schema : NULL, &mod);
3886 LY_CHECK_RET(rc);
3887 if (!mod) {
3888 LOGVAL(set->ctx, LYVE_XPATH, "Identity \"%.*s\" without a prefix.", (int)id_len, id_name);
3889 return LY_EVALID;
3890 }
3891
3892 /* find the identity */
3893 found = 0;
3894 LY_ARRAY_FOR(mod->identities, u) {
3895 if (!ly_strncmp(mod->identities[u].name, id_name, id_len)) {
3896 /* we have match */
3897 found = 1;
3898 break;
3899 }
3900 }
3901 if (!found) {
3902 LOGVAL(set->ctx, LYVE_XPATH, "Identity \"%.*s\" not found in module \"%s\".", (int)id_len, id_name, mod->name);
3903 return LY_EVALID;
3904 }
3905 id = &mod->identities[u];
3906
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003907 set_fill_boolean(set, 0);
3908 found = 0;
3909 for (i = 0; i < args[0]->used; ++i) {
3910 if ((args[0]->val.nodes[i].type != LYXP_NODE_ELEM) && (args[0]->val.nodes[i].type != LYXP_NODE_META)) {
3911 continue;
3912 }
3913
3914 if (args[0]->val.nodes[i].type == LYXP_NODE_ELEM) {
3915 leaf = (struct lyd_node_term *)args[0]->val.nodes[i].node;
3916 sleaf = (struct lysc_node_leaf *)leaf->schema;
3917 val = &leaf->value;
3918 if (!(sleaf->nodetype & LYD_NODE_TERM) || (leaf->value.realtype->basetype != LY_TYPE_IDENT)) {
3919 /* uninteresting */
3920 continue;
3921 }
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003922 } else {
3923 meta = args[0]->val.meta[i].meta;
3924 val = &meta->value;
3925 if (val->realtype->basetype != LY_TYPE_IDENT) {
3926 /* uninteresting */
3927 continue;
3928 }
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003929 }
3930
Michal Vasko93923692021-05-07 15:28:02 +02003931 /* check the identity itself */
3932 if (self_match && (id == val->ident)) {
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003933 set_fill_boolean(set, 1);
3934 found = 1;
3935 }
Michal Vasko93923692021-05-07 15:28:02 +02003936 if (!found && !lyplg_type_identity_isderived(id, val->ident)) {
3937 set_fill_boolean(set, 1);
3938 found = 1;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003939 }
3940
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003941 if (found) {
3942 break;
3943 }
3944 }
3945
3946 return LY_SUCCESS;
3947}
3948
Michal Vasko03ff5a72019-09-11 13:49:33 +02003949/**
3950 * @brief Execute the YANG 1.1 derived-from(node-set, string) function. Returns LYXP_SET_BOOLEAN depending
3951 * on whether the first argument nodes contain a node of an identity derived from the second
3952 * argument identity.
3953 *
3954 * @param[in] args Array of arguments.
3955 * @param[in] arg_count Count of elements in @p args.
3956 * @param[in,out] set Context and result set at the same time.
3957 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003958 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003959 */
3960static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003961xpath_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 +02003962{
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003963 return xpath_derived_(args, set, options, 0, __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003964}
3965
3966/**
3967 * @brief Execute the YANG 1.1 derived-from-or-self(node-set, string) function. Returns LYXP_SET_BOOLEAN depending
3968 * on whether the first argument nodes contain a node of an identity that either is or is derived from
3969 * the second argument identity.
3970 *
3971 * @param[in] args Array of arguments.
3972 * @param[in] arg_count Count of elements in @p args.
3973 * @param[in,out] set Context and result set at the same time.
3974 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003975 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003976 */
3977static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003978xpath_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 +02003979{
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003980 return xpath_derived_(args, set, options, 1, __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003981}
3982
3983/**
3984 * @brief Execute the YANG 1.1 enum-value(node-set) function. Returns LYXP_SET_NUMBER
3985 * with the integer value of the first node's enum value, otherwise NaN.
3986 *
3987 * @param[in] args Array of arguments.
3988 * @param[in] arg_count Count of elements in @p args.
3989 * @param[in,out] set Context and result set at the same time.
3990 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003991 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003992 */
3993static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003994xpath_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 +02003995{
3996 struct lyd_node_term *leaf;
3997 struct lysc_node_leaf *sleaf;
3998 LY_ERR rc = LY_SUCCESS;
3999
4000 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02004001 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004002 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02004003 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4004 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4005 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
4006 sleaf->name);
4007 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_ENUM)) {
4008 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"enumeration\".", __func__, sleaf->name);
4009 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004010 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004011 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004012 return rc;
4013 }
4014
Michal Vaskod3678892020-05-21 10:06:58 +02004015 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004016 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "enum-value(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004017 return LY_EVALID;
4018 }
4019
4020 set_fill_number(set, NAN);
Michal Vaskod3678892020-05-21 10:06:58 +02004021 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004022 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
4023 sleaf = (struct lysc_node_leaf *)leaf->schema;
4024 if ((sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (sleaf->type->basetype == LY_TYPE_ENUM)) {
4025 set_fill_number(set, leaf->value.enum_item->value);
4026 }
4027 }
4028
4029 return LY_SUCCESS;
4030}
4031
4032/**
4033 * @brief Execute the XPath false() function. Returns LYXP_SET_BOOLEAN
4034 * with false value.
4035 *
4036 * @param[in] args Array of arguments.
4037 * @param[in] arg_count Count of elements in @p args.
4038 * @param[in,out] set Context and result set at the same time.
4039 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004040 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004041 */
4042static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004043xpath_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 +02004044{
4045 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004046 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004047 return LY_SUCCESS;
4048 }
4049
4050 set_fill_boolean(set, 0);
4051 return LY_SUCCESS;
4052}
4053
4054/**
4055 * @brief Execute the XPath floor(number) function. Returns LYXP_SET_NUMBER
4056 * with the first argument floored (truncated).
4057 *
4058 * @param[in] args Array of arguments.
4059 * @param[in] arg_count Count of elements in @p args.
4060 * @param[in,out] set Context and result set at the same time.
4061 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004062 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004063 */
4064static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004065xpath_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 +02004066{
4067 LY_ERR rc;
4068
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004069 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004070 LY_CHECK_RET(rc);
4071 if (isfinite(args[0]->val.num)) {
4072 set_fill_number(set, (long long)args[0]->val.num);
4073 }
4074
4075 return LY_SUCCESS;
4076}
4077
4078/**
4079 * @brief Execute the XPath lang(string) function. Returns LYXP_SET_BOOLEAN
4080 * whether the language of the text matches the one from the argument.
4081 *
4082 * @param[in] args Array of arguments.
4083 * @param[in] arg_count Count of elements in @p args.
4084 * @param[in,out] set Context and result set at the same time.
4085 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004086 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004087 */
4088static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004089xpath_lang(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004090{
4091 const struct lyd_node *node;
4092 struct lysc_node_leaf *sleaf;
Michal Vasko9f96a052020-03-10 09:41:45 +01004093 struct lyd_meta *meta = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004094 const char *val;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004095 LY_ERR rc = LY_SUCCESS;
4096
4097 if (options & LYXP_SCNODE_ALL) {
4098 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4099 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004100 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
4101 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004102 } else if (!warn_is_string_type(sleaf->type)) {
4103 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004104 }
4105 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004106 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004107 return rc;
4108 }
4109
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004110 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004111 LY_CHECK_RET(rc);
4112
Michal Vasko03ff5a72019-09-11 13:49:33 +02004113 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004114 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "lang(string)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004115 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004116 } else if (!set->used) {
4117 set_fill_boolean(set, 0);
4118 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004119 }
4120
4121 switch (set->val.nodes[0].type) {
4122 case LYXP_NODE_ELEM:
4123 case LYXP_NODE_TEXT:
4124 node = set->val.nodes[0].node;
4125 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004126 case LYXP_NODE_META:
4127 node = set->val.meta[0].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004128 break;
4129 default:
4130 /* nothing to do with roots */
4131 set_fill_boolean(set, 0);
4132 return LY_SUCCESS;
4133 }
4134
Michal Vasko9f96a052020-03-10 09:41:45 +01004135 /* find lang metadata */
Michal Vasko9e685082021-01-29 14:49:09 +01004136 for ( ; node; node = lyd_parent(node)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01004137 for (meta = node->meta; meta; meta = meta->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004138 /* annotations */
Michal Vasko9f96a052020-03-10 09:41:45 +01004139 if (meta->name && !strcmp(meta->name, "lang") && !strcmp(meta->annotation->module->name, "xml")) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004140 break;
4141 }
4142 }
4143
Michal Vasko9f96a052020-03-10 09:41:45 +01004144 if (meta) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004145 break;
4146 }
4147 }
4148
4149 /* compare languages */
Michal Vasko9f96a052020-03-10 09:41:45 +01004150 if (!meta) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004151 set_fill_boolean(set, 0);
4152 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004153 uint64_t i;
4154
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02004155 val = lyd_get_meta_value(meta);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004156 for (i = 0; args[0]->val.str[i]; ++i) {
4157 if (tolower(args[0]->val.str[i]) != tolower(val[i])) {
4158 set_fill_boolean(set, 0);
4159 break;
4160 }
4161 }
4162 if (!args[0]->val.str[i]) {
4163 if (!val[i] || (val[i] == '-')) {
4164 set_fill_boolean(set, 1);
4165 } else {
4166 set_fill_boolean(set, 0);
4167 }
4168 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004169 }
4170
4171 return LY_SUCCESS;
4172}
4173
4174/**
4175 * @brief Execute the XPath last() function. Returns LYXP_SET_NUMBER
4176 * with the context size.
4177 *
4178 * @param[in] args Array of arguments.
4179 * @param[in] arg_count Count of elements in @p args.
4180 * @param[in,out] set Context and result set at the same time.
4181 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004182 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004183 */
4184static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004185xpath_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 +02004186{
4187 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004188 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004189 return LY_SUCCESS;
4190 }
4191
Michal Vasko03ff5a72019-09-11 13:49:33 +02004192 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004193 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "last()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004194 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004195 } else if (!set->used) {
4196 set_fill_number(set, 0);
4197 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004198 }
4199
4200 set_fill_number(set, set->ctx_size);
4201 return LY_SUCCESS;
4202}
4203
4204/**
4205 * @brief Execute the XPath local-name(node-set?) function. Returns LYXP_SET_STRING
4206 * with the node name without namespace from the argument or the context.
4207 *
4208 * @param[in] args Array of arguments.
4209 * @param[in] arg_count Count of elements in @p args.
4210 * @param[in,out] set Context and result set at the same time.
4211 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004212 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004213 */
4214static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004215xpath_local_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004216{
4217 struct lyxp_set_node *item;
Michal Vasko69730152020-10-09 16:30:07 +02004218
Michal Vasko03ff5a72019-09-11 13:49:33 +02004219 /* suppress unused variable warning */
4220 (void)options;
4221
4222 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004223 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004224 return LY_SUCCESS;
4225 }
4226
4227 if (arg_count) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004228 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004229 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004230 "local-name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004231 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004232 } else if (!args[0]->used) {
4233 set_fill_string(set, "", 0);
4234 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004235 }
4236
4237 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004238 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004239
4240 item = &args[0]->val.nodes[0];
4241 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004242 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004243 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "local-name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004244 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004245 } else if (!set->used) {
4246 set_fill_string(set, "", 0);
4247 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004248 }
4249
4250 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004251 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004252
4253 item = &set->val.nodes[0];
4254 }
4255
4256 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004257 case LYXP_NODE_NONE:
4258 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004259 case LYXP_NODE_ROOT:
4260 case LYXP_NODE_ROOT_CONFIG:
4261 case LYXP_NODE_TEXT:
4262 set_fill_string(set, "", 0);
4263 break;
4264 case LYXP_NODE_ELEM:
4265 set_fill_string(set, item->node->schema->name, strlen(item->node->schema->name));
4266 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004267 case LYXP_NODE_META:
4268 set_fill_string(set, ((struct lyd_meta *)item->node)->name, strlen(((struct lyd_meta *)item->node)->name));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004269 break;
4270 }
4271
4272 return LY_SUCCESS;
4273}
4274
4275/**
4276 * @brief Execute the XPath name(node-set?) function. Returns LYXP_SET_STRING
4277 * with the node name fully qualified (with namespace) from the argument or the context.
4278 *
4279 * @param[in] args Array of arguments.
4280 * @param[in] arg_count Count of elements in @p args.
4281 * @param[in,out] set Context and result set at the same time.
4282 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004283 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004284 */
4285static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004286xpath_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004287{
4288 struct lyxp_set_node *item;
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004289 struct lys_module *mod = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004290 char *str;
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004291 const char *name = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004292
4293 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004294 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004295 return LY_SUCCESS;
4296 }
4297
4298 if (arg_count) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004299 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004300 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004301 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004302 } else if (!args[0]->used) {
4303 set_fill_string(set, "", 0);
4304 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004305 }
4306
4307 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004308 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004309
4310 item = &args[0]->val.nodes[0];
4311 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004312 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004313 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004314 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004315 } else if (!set->used) {
4316 set_fill_string(set, "", 0);
4317 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004318 }
4319
4320 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004321 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004322
4323 item = &set->val.nodes[0];
4324 }
4325
4326 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004327 case LYXP_NODE_NONE:
4328 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004329 case LYXP_NODE_ROOT:
4330 case LYXP_NODE_ROOT_CONFIG:
4331 case LYXP_NODE_TEXT:
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004332 /* keep NULL */
Michal Vasko03ff5a72019-09-11 13:49:33 +02004333 break;
4334 case LYXP_NODE_ELEM:
4335 mod = item->node->schema->module;
4336 name = item->node->schema->name;
4337 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004338 case LYXP_NODE_META:
4339 mod = ((struct lyd_meta *)item->node)->annotation->module;
4340 name = ((struct lyd_meta *)item->node)->name;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004341 break;
4342 }
4343
4344 if (mod && name) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004345 int rc = asprintf(&str, "%s:%s", ly_get_prefix(mod, set->format, set->prefix_data), name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004346 LY_CHECK_ERR_RET(rc == -1, LOGMEM(set->ctx), LY_EMEM);
4347 set_fill_string(set, str, strlen(str));
4348 free(str);
4349 } else {
4350 set_fill_string(set, "", 0);
4351 }
4352
4353 return LY_SUCCESS;
4354}
4355
4356/**
4357 * @brief Execute the XPath namespace-uri(node-set?) function. Returns LYXP_SET_STRING
4358 * with the namespace of the node from the argument or the context.
4359 *
4360 * @param[in] args Array of arguments.
4361 * @param[in] arg_count Count of elements in @p args.
4362 * @param[in,out] set Context and result set at the same time.
4363 * @param[in] options XPath options.
4364 * @return LY_ERR (LY_EINVAL for wrong arguments on schema)
4365 */
4366static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004367xpath_namespace_uri(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004368{
4369 struct lyxp_set_node *item;
4370 struct lys_module *mod;
Michal Vasko69730152020-10-09 16:30:07 +02004371
Michal Vasko03ff5a72019-09-11 13:49:33 +02004372 /* suppress unused variable warning */
4373 (void)options;
4374
4375 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004376 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004377 return LY_SUCCESS;
4378 }
4379
4380 if (arg_count) {
Michal Vaskod3678892020-05-21 10:06:58 +02004381 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004382 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
Michal Vasko69730152020-10-09 16:30:07 +02004383 "namespace-uri(node-set?)");
Michal Vaskod3678892020-05-21 10:06:58 +02004384 return LY_EVALID;
4385 } else if (!args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004386 set_fill_string(set, "", 0);
4387 return LY_SUCCESS;
4388 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004389
4390 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004391 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004392
4393 item = &args[0]->val.nodes[0];
4394 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004395 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004396 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "namespace-uri(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004397 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004398 } else if (!set->used) {
4399 set_fill_string(set, "", 0);
4400 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004401 }
4402
4403 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004404 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004405
4406 item = &set->val.nodes[0];
4407 }
4408
4409 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004410 case LYXP_NODE_NONE:
4411 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004412 case LYXP_NODE_ROOT:
4413 case LYXP_NODE_ROOT_CONFIG:
4414 case LYXP_NODE_TEXT:
4415 set_fill_string(set, "", 0);
4416 break;
4417 case LYXP_NODE_ELEM:
Michal Vasko9f96a052020-03-10 09:41:45 +01004418 case LYXP_NODE_META:
Michal Vasko03ff5a72019-09-11 13:49:33 +02004419 if (item->type == LYXP_NODE_ELEM) {
4420 mod = item->node->schema->module;
Michal Vasko9f96a052020-03-10 09:41:45 +01004421 } else { /* LYXP_NODE_META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02004422 /* annotations */
Michal Vasko9f96a052020-03-10 09:41:45 +01004423 mod = ((struct lyd_meta *)item->node)->annotation->module;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004424 }
4425
4426 set_fill_string(set, mod->ns, strlen(mod->ns));
4427 break;
4428 }
4429
4430 return LY_SUCCESS;
4431}
4432
4433/**
4434 * @brief Execute the XPath node() function (node type). Returns LYXP_SET_NODE_SET
4435 * with only nodes from the context. In practice it either leaves the context
4436 * as it is or returns an empty node set.
4437 *
4438 * @param[in] args Array of arguments.
4439 * @param[in] arg_count Count of elements in @p args.
4440 * @param[in,out] set Context and result set at the same time.
4441 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004442 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004443 */
4444static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004445xpath_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 +02004446{
4447 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004448 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004449 return LY_SUCCESS;
4450 }
4451
4452 if (set->type != LYXP_SET_NODE_SET) {
Michal Vaskod3678892020-05-21 10:06:58 +02004453 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004454 }
4455 return LY_SUCCESS;
4456}
4457
4458/**
4459 * @brief Execute the XPath normalize-space(string?) function. Returns LYXP_SET_STRING
4460 * with normalized value (no leading, trailing, double white spaces) of the node
4461 * from the argument or the context.
4462 *
4463 * @param[in] args Array of arguments.
4464 * @param[in] arg_count Count of elements in @p args.
4465 * @param[in,out] set Context and result set at the same time.
4466 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004467 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004468 */
4469static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004470xpath_normalize_space(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004471{
4472 uint16_t i, new_used;
4473 char *new;
Radek Krejci857189e2020-09-01 13:26:36 +02004474 ly_bool have_spaces = 0, space_before = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004475 struct lysc_node_leaf *sleaf;
4476 LY_ERR rc = LY_SUCCESS;
4477
4478 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004479 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) &&
4480 (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004481 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004482 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
4483 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004484 } else if (!warn_is_string_type(sleaf->type)) {
4485 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004486 }
4487 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004488 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004489 return rc;
4490 }
4491
4492 if (arg_count) {
4493 set_fill_set(set, args[0]);
4494 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004495 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004496 LY_CHECK_RET(rc);
4497
4498 /* is there any normalization necessary? */
4499 for (i = 0; set->val.str[i]; ++i) {
4500 if (is_xmlws(set->val.str[i])) {
4501 if ((i == 0) || space_before || (!set->val.str[i + 1])) {
4502 have_spaces = 1;
4503 break;
4504 }
4505 space_before = 1;
4506 } else {
4507 space_before = 0;
4508 }
4509 }
4510
4511 /* yep, there is */
4512 if (have_spaces) {
4513 /* it's enough, at least one character will go, makes space for ending '\0' */
4514 new = malloc(strlen(set->val.str) * sizeof(char));
4515 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4516 new_used = 0;
4517
4518 space_before = 0;
4519 for (i = 0; set->val.str[i]; ++i) {
4520 if (is_xmlws(set->val.str[i])) {
4521 if ((i == 0) || space_before) {
4522 space_before = 1;
4523 continue;
4524 } else {
4525 space_before = 1;
4526 }
4527 } else {
4528 space_before = 0;
4529 }
4530
4531 new[new_used] = (space_before ? ' ' : set->val.str[i]);
4532 ++new_used;
4533 }
4534
4535 /* at worst there is one trailing space now */
4536 if (new_used && is_xmlws(new[new_used - 1])) {
4537 --new_used;
4538 }
4539
4540 new = ly_realloc(new, (new_used + 1) * sizeof(char));
4541 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4542 new[new_used] = '\0';
4543
4544 free(set->val.str);
4545 set->val.str = new;
4546 }
4547
4548 return LY_SUCCESS;
4549}
4550
4551/**
4552 * @brief Execute the XPath not(boolean) function. Returns LYXP_SET_BOOLEAN
4553 * with the argument converted to boolean and logically inverted.
4554 *
4555 * @param[in] args Array of arguments.
4556 * @param[in] arg_count Count of elements in @p args.
4557 * @param[in,out] set Context and result set at the same time.
4558 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004559 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004560 */
4561static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004562xpath_not(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004563{
4564 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004565 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004566 return LY_SUCCESS;
4567 }
4568
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004569 lyxp_set_cast(args[0], LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +02004570 if (args[0]->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004571 set_fill_boolean(set, 0);
4572 } else {
4573 set_fill_boolean(set, 1);
4574 }
4575
4576 return LY_SUCCESS;
4577}
4578
4579/**
4580 * @brief Execute the XPath number(object?) function. Returns LYXP_SET_NUMBER
4581 * with the number representation of either the argument or the context.
4582 *
4583 * @param[in] args Array of arguments.
4584 * @param[in] arg_count Count of elements in @p args.
4585 * @param[in,out] set Context and result set at the same time.
4586 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004587 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004588 */
4589static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004590xpath_number(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004591{
4592 LY_ERR rc;
4593
4594 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004595 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004596 return LY_SUCCESS;
4597 }
4598
4599 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004600 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004601 LY_CHECK_RET(rc);
4602 set_fill_set(set, args[0]);
4603 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004604 rc = lyxp_set_cast(set, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004605 LY_CHECK_RET(rc);
4606 }
4607
4608 return LY_SUCCESS;
4609}
4610
4611/**
4612 * @brief Execute the XPath position() function. Returns LYXP_SET_NUMBER
4613 * with the context position.
4614 *
4615 * @param[in] args Array of arguments.
4616 * @param[in] arg_count Count of elements in @p args.
4617 * @param[in,out] set Context and result set at the same time.
4618 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004619 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004620 */
4621static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004622xpath_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 +02004623{
4624 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004625 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004626 return LY_SUCCESS;
4627 }
4628
Michal Vasko03ff5a72019-09-11 13:49:33 +02004629 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004630 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "position()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004631 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004632 } else if (!set->used) {
4633 set_fill_number(set, 0);
4634 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004635 }
4636
4637 set_fill_number(set, set->ctx_pos);
4638
4639 /* UNUSED in 'Release' build type */
4640 (void)options;
4641 return LY_SUCCESS;
4642}
4643
4644/**
4645 * @brief Execute the YANG 1.1 re-match(string, string) function. Returns LYXP_SET_BOOLEAN
4646 * depending on whether the second argument regex matches the first argument string. For details refer to
4647 * YANG 1.1 RFC section 10.2.1.
4648 *
4649 * @param[in] args Array of arguments.
4650 * @param[in] arg_count Count of elements in @p args.
4651 * @param[in,out] set Context and result set at the same time.
4652 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004653 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004654 */
4655static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004656xpath_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 +02004657{
4658 struct lysc_pattern **patterns = NULL, **pattern;
4659 struct lysc_node_leaf *sleaf;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004660 LY_ERR rc = LY_SUCCESS;
4661 struct ly_err_item *err;
4662
4663 if (options & LYXP_SCNODE_ALL) {
4664 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4665 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4666 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 +02004667 } else if (!warn_is_string_type(sleaf->type)) {
4668 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004669 }
4670 }
4671
4672 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4673 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4674 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 +02004675 } else if (!warn_is_string_type(sleaf->type)) {
4676 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004677 }
4678 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004679 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004680 return rc;
4681 }
4682
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004683 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004684 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004685 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004686 LY_CHECK_RET(rc);
4687
4688 LY_ARRAY_NEW_RET(set->ctx, patterns, pattern, LY_EMEM);
Radek IÅ¡a45802b52021-02-09 09:21:58 +01004689 *pattern = calloc(1, sizeof **pattern);
Radek Krejciddace2c2021-01-08 11:30:56 +01004690 LOG_LOCSET(NULL, set->cur_node, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01004691 rc = lys_compile_type_pattern_check(set->ctx, args[1]->val.str, &(*pattern)->code);
Radek Krejciddace2c2021-01-08 11:30:56 +01004692 LOG_LOCBACK(0, 1, 0, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004693 if (rc != LY_SUCCESS) {
4694 LY_ARRAY_FREE(patterns);
4695 return rc;
4696 }
4697
Radek Krejci0b013302021-03-29 15:22:32 +02004698 rc = lyplg_type_validate_patterns(patterns, args[0]->val.str, strlen(args[0]->val.str), &err);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004699 pcre2_code_free((*pattern)->code);
4700 free(*pattern);
4701 LY_ARRAY_FREE(patterns);
4702 if (rc && (rc != LY_EVALID)) {
Michal Vasko177d0ed2020-11-23 16:43:03 +01004703 ly_err_print(set->ctx, err);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004704 ly_err_free(err);
4705 return rc;
4706 }
4707
4708 if (rc == LY_EVALID) {
4709 ly_err_free(err);
4710 set_fill_boolean(set, 0);
4711 } else {
4712 set_fill_boolean(set, 1);
4713 }
4714
4715 return LY_SUCCESS;
4716}
4717
4718/**
4719 * @brief Execute the XPath round(number) function. Returns LYXP_SET_NUMBER
4720 * with the rounded first argument. For details refer to
4721 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-round.
4722 *
4723 * @param[in] args Array of arguments.
4724 * @param[in] arg_count Count of elements in @p args.
4725 * @param[in,out] set Context and result set at the same time.
4726 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004727 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004728 */
4729static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004730xpath_round(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004731{
4732 struct lysc_node_leaf *sleaf;
4733 LY_ERR rc = LY_SUCCESS;
4734
4735 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02004736 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004737 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02004738 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4739 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4740 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
4741 sleaf->name);
4742 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
4743 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
4744 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004745 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004746 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004747 return rc;
4748 }
4749
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004750 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004751 LY_CHECK_RET(rc);
4752
4753 /* cover only the cases where floor can't be used */
4754 if ((args[0]->val.num == -0.0f) || ((args[0]->val.num < 0) && (args[0]->val.num >= -0.5))) {
4755 set_fill_number(set, -0.0f);
4756 } else {
4757 args[0]->val.num += 0.5;
4758 rc = xpath_floor(args, 1, args[0], options);
4759 LY_CHECK_RET(rc);
4760 set_fill_number(set, args[0]->val.num);
4761 }
4762
4763 return LY_SUCCESS;
4764}
4765
4766/**
4767 * @brief Execute the XPath starts-with(string, string) function.
4768 * Returns LYXP_SET_BOOLEAN whether the second argument is
4769 * the prefix of the first or not.
4770 *
4771 * @param[in] args Array of arguments.
4772 * @param[in] arg_count Count of elements in @p args.
4773 * @param[in,out] set Context and result set at the same time.
4774 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004775 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004776 */
4777static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004778xpath_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 +02004779{
4780 struct lysc_node_leaf *sleaf;
4781 LY_ERR rc = LY_SUCCESS;
4782
4783 if (options & LYXP_SCNODE_ALL) {
4784 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4785 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4786 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 +02004787 } else if (!warn_is_string_type(sleaf->type)) {
4788 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004789 }
4790 }
4791
4792 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4793 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4794 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 +02004795 } else if (!warn_is_string_type(sleaf->type)) {
4796 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004797 }
4798 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004799 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004800 return rc;
4801 }
4802
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004803 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004804 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004805 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004806 LY_CHECK_RET(rc);
4807
4808 if (strncmp(args[0]->val.str, args[1]->val.str, strlen(args[1]->val.str))) {
4809 set_fill_boolean(set, 0);
4810 } else {
4811 set_fill_boolean(set, 1);
4812 }
4813
4814 return LY_SUCCESS;
4815}
4816
4817/**
4818 * @brief Execute the XPath string(object?) function. Returns LYXP_SET_STRING
4819 * with the string representation of either the argument or the context.
4820 *
4821 * @param[in] args Array of arguments.
4822 * @param[in] arg_count Count of elements in @p args.
4823 * @param[in,out] set Context and result set at the same time.
4824 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004825 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004826 */
4827static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004828xpath_string(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004829{
4830 LY_ERR rc;
4831
4832 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004833 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004834 return LY_SUCCESS;
4835 }
4836
4837 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004838 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004839 LY_CHECK_RET(rc);
4840 set_fill_set(set, args[0]);
4841 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004842 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004843 LY_CHECK_RET(rc);
4844 }
4845
4846 return LY_SUCCESS;
4847}
4848
4849/**
4850 * @brief Execute the XPath string-length(string?) function. Returns LYXP_SET_NUMBER
4851 * with the length of the string in either the argument or the context.
4852 *
4853 * @param[in] args Array of arguments.
4854 * @param[in] arg_count Count of elements in @p args.
4855 * @param[in,out] set Context and result set at the same time.
4856 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004857 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004858 */
4859static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004860xpath_string_length(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004861{
4862 struct lysc_node_leaf *sleaf;
4863 LY_ERR rc = LY_SUCCESS;
4864
4865 if (options & LYXP_SCNODE_ALL) {
4866 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4867 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4868 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 +02004869 } else if (!warn_is_string_type(sleaf->type)) {
4870 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004871 }
4872 }
4873 if (!arg_count && (set->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set))) {
4874 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4875 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 +02004876 } else if (!warn_is_string_type(sleaf->type)) {
4877 LOGWRN(set->ctx, "Argument #0 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004878 }
4879 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004880 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004881 return rc;
4882 }
4883
4884 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004885 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004886 LY_CHECK_RET(rc);
4887 set_fill_number(set, strlen(args[0]->val.str));
4888 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004889 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004890 LY_CHECK_RET(rc);
4891 set_fill_number(set, strlen(set->val.str));
4892 }
4893
4894 return LY_SUCCESS;
4895}
4896
4897/**
4898 * @brief Execute the XPath substring(string, number, number?) function.
4899 * Returns LYXP_SET_STRING substring of the first argument starting
4900 * on the second argument index ending on the third argument index,
4901 * indexed from 1. For exact definition refer to
4902 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-substring.
4903 *
4904 * @param[in] args Array of arguments.
4905 * @param[in] arg_count Count of elements in @p args.
4906 * @param[in,out] set Context and result set at the same time.
4907 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004908 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004909 */
4910static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004911xpath_substring(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004912{
Radek Krejci1deb5be2020-08-26 16:43:36 +02004913 int32_t start, len;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004914 uint16_t str_start, str_len, pos;
4915 struct lysc_node_leaf *sleaf;
4916 LY_ERR rc = LY_SUCCESS;
4917
4918 if (options & LYXP_SCNODE_ALL) {
4919 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4920 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4921 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 +02004922 } else if (!warn_is_string_type(sleaf->type)) {
4923 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004924 }
4925 }
4926
4927 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4928 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4929 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 +02004930 } else if (!warn_is_numeric_type(sleaf->type)) {
4931 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004932 }
4933 }
4934
Michal Vasko69730152020-10-09 16:30:07 +02004935 if ((arg_count == 3) && (args[2]->type == LYXP_SET_SCNODE_SET) &&
4936 (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004937 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4938 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 +02004939 } else if (!warn_is_numeric_type(sleaf->type)) {
4940 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004941 }
4942 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004943 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004944 return rc;
4945 }
4946
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004947 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004948 LY_CHECK_RET(rc);
4949
4950 /* start */
4951 if (xpath_round(&args[1], 1, args[1], options)) {
4952 return -1;
4953 }
4954 if (isfinite(args[1]->val.num)) {
4955 start = args[1]->val.num - 1;
4956 } else if (isinf(args[1]->val.num) && signbit(args[1]->val.num)) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004957 start = INT32_MIN;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004958 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004959 start = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004960 }
4961
4962 /* len */
4963 if (arg_count == 3) {
4964 rc = xpath_round(&args[2], 1, args[2], options);
4965 LY_CHECK_RET(rc);
Radek Krejci1deb5be2020-08-26 16:43:36 +02004966 if (isnan(args[2]->val.num) || signbit(args[2]->val.num)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004967 len = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +02004968 } else if (isfinite(args[2]->val.num)) {
4969 len = args[2]->val.num;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004970 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004971 len = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004972 }
4973 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004974 len = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004975 }
4976
4977 /* find matching character positions */
4978 str_start = 0;
4979 str_len = 0;
4980 for (pos = 0; args[0]->val.str[pos]; ++pos) {
4981 if (pos < start) {
4982 ++str_start;
4983 } else if (pos < start + len) {
4984 ++str_len;
4985 } else {
4986 break;
4987 }
4988 }
4989
4990 set_fill_string(set, args[0]->val.str + str_start, str_len);
4991 return LY_SUCCESS;
4992}
4993
4994/**
4995 * @brief Execute the XPath substring-after(string, string) function.
4996 * Returns LYXP_SET_STRING with the string succeeding the occurance
4997 * of the second argument in the first or an empty string.
4998 *
4999 * @param[in] args Array of arguments.
5000 * @param[in] arg_count Count of elements in @p args.
5001 * @param[in,out] set Context and result set at the same time.
5002 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005003 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005004 */
5005static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005006xpath_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 +02005007{
5008 char *ptr;
5009 struct lysc_node_leaf *sleaf;
5010 LY_ERR rc = LY_SUCCESS;
5011
5012 if (options & LYXP_SCNODE_ALL) {
5013 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5014 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5015 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 +02005016 } else if (!warn_is_string_type(sleaf->type)) {
5017 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005018 }
5019 }
5020
5021 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5022 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5023 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 +02005024 } else if (!warn_is_string_type(sleaf->type)) {
5025 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005026 }
5027 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005028 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005029 return rc;
5030 }
5031
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005032 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005033 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005034 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005035 LY_CHECK_RET(rc);
5036
5037 ptr = strstr(args[0]->val.str, args[1]->val.str);
5038 if (ptr) {
5039 set_fill_string(set, ptr + strlen(args[1]->val.str), strlen(ptr + strlen(args[1]->val.str)));
5040 } else {
5041 set_fill_string(set, "", 0);
5042 }
5043
5044 return LY_SUCCESS;
5045}
5046
5047/**
5048 * @brief Execute the XPath substring-before(string, string) function.
5049 * Returns LYXP_SET_STRING with the string preceding the occurance
5050 * of the second argument in the first or an empty string.
5051 *
5052 * @param[in] args Array of arguments.
5053 * @param[in] arg_count Count of elements in @p args.
5054 * @param[in,out] set Context and result set at the same time.
5055 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005056 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005057 */
5058static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005059xpath_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 +02005060{
5061 char *ptr;
5062 struct lysc_node_leaf *sleaf;
5063 LY_ERR rc = LY_SUCCESS;
5064
5065 if (options & LYXP_SCNODE_ALL) {
5066 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5067 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5068 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 +02005069 } else if (!warn_is_string_type(sleaf->type)) {
5070 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005071 }
5072 }
5073
5074 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5075 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5076 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 +02005077 } else if (!warn_is_string_type(sleaf->type)) {
5078 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005079 }
5080 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005081 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005082 return rc;
5083 }
5084
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005085 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005086 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005087 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005088 LY_CHECK_RET(rc);
5089
5090 ptr = strstr(args[0]->val.str, args[1]->val.str);
5091 if (ptr) {
5092 set_fill_string(set, args[0]->val.str, ptr - args[0]->val.str);
5093 } else {
5094 set_fill_string(set, "", 0);
5095 }
5096
5097 return LY_SUCCESS;
5098}
5099
5100/**
5101 * @brief Execute the XPath sum(node-set) function. Returns LYXP_SET_NUMBER
5102 * with the sum of all the nodes in the context.
5103 *
5104 * @param[in] args Array of arguments.
5105 * @param[in] arg_count Count of elements in @p args.
5106 * @param[in,out] set Context and result set at the same time.
5107 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005108 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005109 */
5110static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005111xpath_sum(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005112{
5113 long double num;
5114 char *str;
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01005115 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005116 struct lyxp_set set_item;
5117 struct lysc_node_leaf *sleaf;
5118 LY_ERR rc = LY_SUCCESS;
5119
5120 if (options & LYXP_SCNODE_ALL) {
5121 if (args[0]->type == LYXP_SET_SCNODE_SET) {
5122 for (i = 0; i < args[0]->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005123 if (args[0]->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005124 sleaf = (struct lysc_node_leaf *)args[0]->val.scnodes[i].scnode;
5125 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5126 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__,
Michal Vasko69730152020-10-09 16:30:07 +02005127 lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005128 } else if (!warn_is_numeric_type(sleaf->type)) {
5129 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005130 }
5131 }
5132 }
5133 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005134 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005135 return rc;
5136 }
5137
5138 set_fill_number(set, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005139
5140 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005141 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "sum(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02005142 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02005143 } else if (!args[0]->used) {
5144 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005145 }
5146
Michal Vasko5c4e5892019-11-14 12:31:38 +01005147 set_init(&set_item, set);
5148
Michal Vasko03ff5a72019-09-11 13:49:33 +02005149 set_item.type = LYXP_SET_NODE_SET;
Michal Vasko41decbf2021-11-02 11:50:21 +01005150 set_item.val.nodes = calloc(1, sizeof *set_item.val.nodes);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005151 LY_CHECK_ERR_RET(!set_item.val.nodes, LOGMEM(set->ctx), LY_EMEM);
5152
5153 set_item.used = 1;
5154 set_item.size = 1;
5155
5156 for (i = 0; i < args[0]->used; ++i) {
5157 set_item.val.nodes[0] = args[0]->val.nodes[i];
5158
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005159 rc = cast_node_set_to_string(&set_item, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005160 LY_CHECK_RET(rc);
5161 num = cast_string_to_number(str);
5162 free(str);
5163 set->val.num += num;
5164 }
5165
5166 free(set_item.val.nodes);
5167
5168 return LY_SUCCESS;
5169}
5170
5171/**
5172 * @brief Execute the XPath text() function (node type). Returns LYXP_SET_NODE_SET
5173 * with the text content of the nodes in the context.
5174 *
5175 * @param[in] args Array of arguments.
5176 * @param[in] arg_count Count of elements in @p args.
5177 * @param[in,out] set Context and result set at the same time.
5178 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005179 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005180 */
5181static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005182xpath_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 +02005183{
5184 uint32_t i;
5185
5186 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02005187 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005188 return LY_SUCCESS;
5189 }
5190
Michal Vasko03ff5a72019-09-11 13:49:33 +02005191 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005192 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "text()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02005193 return LY_EVALID;
5194 }
5195
Michal Vaskod989ba02020-08-24 10:59:24 +02005196 for (i = 0; i < set->used; ) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005197 switch (set->val.nodes[i].type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01005198 case LYXP_NODE_NONE:
5199 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005200 case LYXP_NODE_ELEM:
Michal Vasko03ff5a72019-09-11 13:49:33 +02005201 if (set->val.nodes[i].node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
5202 set->val.nodes[i].type = LYXP_NODE_TEXT;
5203 ++i;
5204 break;
5205 }
Radek Krejci0f969882020-08-21 16:56:47 +02005206 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005207 case LYXP_NODE_ROOT:
5208 case LYXP_NODE_ROOT_CONFIG:
5209 case LYXP_NODE_TEXT:
Michal Vasko9f96a052020-03-10 09:41:45 +01005210 case LYXP_NODE_META:
Michal Vasko03ff5a72019-09-11 13:49:33 +02005211 set_remove_node(set, i);
5212 break;
5213 }
5214 }
5215
5216 return LY_SUCCESS;
5217}
5218
5219/**
5220 * @brief Execute the XPath translate(string, string, string) function.
5221 * Returns LYXP_SET_STRING with the first argument with the characters
5222 * from the second argument replaced by those on the corresponding
5223 * positions in the third argument.
5224 *
5225 * @param[in] args Array of arguments.
5226 * @param[in] arg_count Count of elements in @p args.
5227 * @param[in,out] set Context and result set at the same time.
5228 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005229 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005230 */
5231static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005232xpath_translate(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005233{
5234 uint16_t i, j, new_used;
5235 char *new;
Radek Krejci857189e2020-09-01 13:26:36 +02005236 ly_bool have_removed;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005237 struct lysc_node_leaf *sleaf;
5238 LY_ERR rc = LY_SUCCESS;
5239
5240 if (options & LYXP_SCNODE_ALL) {
5241 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5242 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5243 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 +02005244 } else if (!warn_is_string_type(sleaf->type)) {
5245 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005246 }
5247 }
5248
5249 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5250 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5251 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 +02005252 } else if (!warn_is_string_type(sleaf->type)) {
5253 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005254 }
5255 }
5256
5257 if ((args[2]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
5258 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5259 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 +02005260 } else if (!warn_is_string_type(sleaf->type)) {
5261 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005262 }
5263 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005264 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005265 return rc;
5266 }
5267
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005268 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005269 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005270 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005271 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005272 rc = lyxp_set_cast(args[2], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005273 LY_CHECK_RET(rc);
5274
5275 new = malloc((strlen(args[0]->val.str) + 1) * sizeof(char));
5276 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5277 new_used = 0;
5278
5279 have_removed = 0;
5280 for (i = 0; args[0]->val.str[i]; ++i) {
Radek Krejci857189e2020-09-01 13:26:36 +02005281 ly_bool found = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005282
5283 for (j = 0; args[1]->val.str[j]; ++j) {
5284 if (args[0]->val.str[i] == args[1]->val.str[j]) {
5285 /* removing this char */
5286 if (j >= strlen(args[2]->val.str)) {
5287 have_removed = 1;
5288 found = 1;
5289 break;
5290 }
5291 /* replacing this char */
5292 new[new_used] = args[2]->val.str[j];
5293 ++new_used;
5294 found = 1;
5295 break;
5296 }
5297 }
5298
5299 /* copying this char */
5300 if (!found) {
5301 new[new_used] = args[0]->val.str[i];
5302 ++new_used;
5303 }
5304 }
5305
5306 if (have_removed) {
5307 new = ly_realloc(new, (new_used + 1) * sizeof(char));
5308 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5309 }
5310 new[new_used] = '\0';
5311
Michal Vaskod3678892020-05-21 10:06:58 +02005312 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005313 set->type = LYXP_SET_STRING;
5314 set->val.str = new;
5315
5316 return LY_SUCCESS;
5317}
5318
5319/**
5320 * @brief Execute the XPath true() function. Returns LYXP_SET_BOOLEAN
5321 * with true value.
5322 *
5323 * @param[in] args Array of arguments.
5324 * @param[in] arg_count Count of elements in @p args.
5325 * @param[in,out] set Context and result set at the same time.
5326 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005327 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005328 */
5329static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005330xpath_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 +02005331{
5332 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02005333 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005334 return LY_SUCCESS;
5335 }
5336
5337 set_fill_boolean(set, 1);
5338 return LY_SUCCESS;
5339}
5340
Michal Vasko03ff5a72019-09-11 13:49:33 +02005341/**
Michal Vasko6346ece2019-09-24 13:12:53 +02005342 * @brief Skip prefix and return corresponding model if there is a prefix. Logs directly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005343 *
Michal Vasko2104e9f2020-03-06 08:23:25 +01005344 * XPath @p set is expected to be a (sc)node set!
5345 *
Michal Vasko6346ece2019-09-24 13:12:53 +02005346 * @param[in,out] qname Qualified node name. If includes prefix, it is skipped.
5347 * @param[in,out] qname_len Length of @p qname, is updated accordingly.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005348 * @param[in] set Set with general XPath context.
5349 * @param[in] ctx_scnode Context node to inherit module for unprefixed node for ::LY_PREF_JSON.
Michal Vasko6346ece2019-09-24 13:12:53 +02005350 * @param[out] moveto_mod Expected module of a matching node.
5351 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005352 */
Michal Vasko6346ece2019-09-24 13:12:53 +02005353static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005354moveto_resolve_model(const char **qname, uint16_t *qname_len, const struct lyxp_set *set,
5355 const struct lysc_node *ctx_scnode, const struct lys_module **moveto_mod)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005356{
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02005357 const struct lys_module *mod = NULL;
Michal Vasko6346ece2019-09-24 13:12:53 +02005358 const char *ptr;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005359 size_t pref_len;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005360
Michal Vasko2104e9f2020-03-06 08:23:25 +01005361 assert((set->type == LYXP_SET_NODE_SET) || (set->type == LYXP_SET_SCNODE_SET));
5362
Michal Vasko6346ece2019-09-24 13:12:53 +02005363 if ((ptr = ly_strnchr(*qname, ':', *qname_len))) {
5364 /* specific module */
5365 pref_len = ptr - *qname;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005366 mod = ly_resolve_prefix(set->ctx, *qname, pref_len, set->format, set->prefix_data);
Michal Vasko6346ece2019-09-24 13:12:53 +02005367
Michal Vasko004d3152020-06-11 19:59:22 +02005368 /* check for errors and non-implemented modules, as they are not valid */
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005369 if (!mod || !mod->implemented) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005370 LOGVAL(set->ctx, LY_VCODE_XP_INMOD, pref_len, *qname);
Michal Vasko6346ece2019-09-24 13:12:53 +02005371 return LY_EVALID;
5372 }
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005373
Michal Vasko6346ece2019-09-24 13:12:53 +02005374 *qname += pref_len + 1;
5375 *qname_len -= pref_len + 1;
5376 } else if (((*qname)[0] == '*') && (*qname_len == 1)) {
5377 /* all modules - special case */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005378 mod = NULL;
Michal Vasko6346ece2019-09-24 13:12:53 +02005379 } else {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005380 switch (set->format) {
Radek Krejci8df109d2021-04-23 12:19:08 +02005381 case LY_VALUE_SCHEMA:
5382 case LY_VALUE_SCHEMA_RESOLVED:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005383 /* current module */
5384 mod = set->cur_mod;
5385 break;
Radek Krejci224d4b42021-04-23 13:54:59 +02005386 case LY_VALUE_CANON:
Radek Krejci8df109d2021-04-23 12:19:08 +02005387 case LY_VALUE_JSON:
Radek Krejcif9943642021-04-26 10:18:21 +02005388 case LY_VALUE_LYB:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005389 /* inherit parent (context node) module */
5390 if (ctx_scnode) {
5391 mod = ctx_scnode->module;
5392 } else {
5393 mod = NULL;
5394 }
5395 break;
Radek Krejci8df109d2021-04-23 12:19:08 +02005396 case LY_VALUE_XML:
Michal Vasko52143d12021-04-14 15:36:39 +02005397 /* all nodes need to be prefixed */
5398 LOGVAL(set->ctx, LYVE_DATA, "Non-prefixed node \"%.*s\" in XML xpath found.", *qname_len, *qname);
5399 return LY_EVALID;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005400 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005401 }
5402
Michal Vasko6346ece2019-09-24 13:12:53 +02005403 *moveto_mod = mod;
5404 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005405}
5406
5407/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02005408 * @brief Move context @p set to the root. Handles absolute path.
5409 * Result is LYXP_SET_NODE_SET.
5410 *
5411 * @param[in,out] set Set to use.
5412 * @param[in] options Xpath options.
Michal Vaskob0099a92020-08-31 14:55:23 +02005413 * @return LY_ERR value.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005414 */
Michal Vaskob0099a92020-08-31 14:55:23 +02005415static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005416moveto_root(struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005417{
aPiecek8b0cc152021-05-31 16:40:31 +02005418 assert(!(options & LYXP_SKIP_EXPR));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005419
5420 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02005421 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskob0099a92020-08-31 14:55:23 +02005422 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, NULL, set->root_type, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005423 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005424 set->type = LYXP_SET_NODE_SET;
5425 set->used = 0;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005426 set_insert_node(set, NULL, 0, set->root_type, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005427 }
Michal Vaskob0099a92020-08-31 14:55:23 +02005428
5429 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005430}
5431
5432/**
5433 * @brief Check @p node as a part of NameTest processing.
5434 *
5435 * @param[in] node Node to check.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005436 * @param[in] set Set to read general context from.
Michal Vaskod3678892020-05-21 10:06:58 +02005437 * @param[in] node_name Node name in the dictionary to move to, NULL for any node.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005438 * @param[in] moveto_mod Expected module of the node, NULL for no prefix.
Michal Vaskocdad7122020-11-09 21:04:44 +01005439 * @param[in] options XPath options.
Michal Vasko6346ece2019-09-24 13:12:53 +02005440 * @return LY_ERR (LY_ENOT if node does not match, LY_EINCOMPLETE on unresolved when,
5441 * LY_EINVAL if netither node nor any children match)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005442 */
5443static LY_ERR
Michal Vaskodb08ce52021-10-06 08:57:49 +02005444moveto_node_check(const struct lyd_node *node, const struct lyxp_set *set, const char *node_name,
5445 const struct lys_module *moveto_mod, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005446{
Michal Vaskodca9f122021-07-16 13:56:22 +02005447 if (!node->schema) {
5448 /* opaque node never matches */
5449 return LY_ENOT;
5450 }
5451
Michal Vasko03ff5a72019-09-11 13:49:33 +02005452 /* module check */
5453 if (moveto_mod && (node->schema->module != moveto_mod)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005454 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005455 }
5456
Michal Vasko5c4e5892019-11-14 12:31:38 +01005457 /* context check */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005458 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (node->schema->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005459 return LY_EINVAL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005460 } else if (set->context_op && (node->schema->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) &&
5461 (node->schema != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005462 return LY_EINVAL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005463 }
5464
5465 /* name check */
Michal Vasko61ac2f62020-05-25 12:39:51 +02005466 if (node_name && strcmp(node_name, "*") && (node->schema->name != node_name)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005467 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005468 }
5469
Michal Vaskoa1424542019-11-14 16:08:52 +01005470 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01005471 if (!(options & LYXP_IGNORE_WHEN) && lysc_has_when(node->schema) && !(node->flags & LYD_WHEN_TRUE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005472 return LY_EINCOMPLETE;
Michal Vaskoa1424542019-11-14 16:08:52 +01005473 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005474
5475 /* match */
5476 return LY_SUCCESS;
5477}
5478
5479/**
5480 * @brief Check @p node as a part of schema NameTest processing.
5481 *
5482 * @param[in] node Schema node to check.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005483 * @param[in] ctx_scnode Context node.
5484 * @param[in] set Set to read general context from.
Michal Vaskod3678892020-05-21 10:06:58 +02005485 * @param[in] node_name Node name in the dictionary to move to, NULL for any nodes.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005486 * @param[in] moveto_mod Expected module of the node, NULL for no prefix.
Michal Vasko6346ece2019-09-24 13:12:53 +02005487 * @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 +02005488 */
5489static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005490moveto_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 +02005491 const char *node_name, const struct lys_module *moveto_mod)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005492{
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005493 if (!moveto_mod && (!node_name || strcmp(node_name, "*"))) {
5494 switch (set->format) {
Radek Krejci8df109d2021-04-23 12:19:08 +02005495 case LY_VALUE_SCHEMA:
5496 case LY_VALUE_SCHEMA_RESOLVED:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005497 /* use current module */
5498 moveto_mod = set->cur_mod;
5499 break;
Radek Krejci8df109d2021-04-23 12:19:08 +02005500 case LY_VALUE_JSON:
Radek Krejcif9943642021-04-26 10:18:21 +02005501 case LY_VALUE_LYB:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005502 /* inherit module of the context node, if any */
5503 if (ctx_scnode) {
5504 moveto_mod = ctx_scnode->module;
5505 }
5506 break;
Radek Krejci224d4b42021-04-23 13:54:59 +02005507 case LY_VALUE_CANON:
Radek Krejci8df109d2021-04-23 12:19:08 +02005508 case LY_VALUE_XML:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005509 /* not defined */
5510 LOGINT(set->ctx);
5511 return LY_EINVAL;
5512 }
5513 }
5514
Michal Vasko03ff5a72019-09-11 13:49:33 +02005515 /* module check */
Michal Vaskod3678892020-05-21 10:06:58 +02005516 if (moveto_mod && (node->module != moveto_mod)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005517 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005518 }
5519
5520 /* context check */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005521 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (node->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005522 return LY_EINVAL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005523 } else if (set->context_op && (node->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && (node != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005524 return LY_EINVAL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005525 }
5526
5527 /* name check */
Michal Vasko61ac2f62020-05-25 12:39:51 +02005528 if (node_name && strcmp(node_name, "*") && (node->name != node_name)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005529 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005530 }
5531
5532 /* match */
5533 return LY_SUCCESS;
5534}
5535
5536/**
Michal Vaskod3678892020-05-21 10:06:58 +02005537 * @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 +02005538 *
5539 * @param[in,out] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005540 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005541 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vaskocdad7122020-11-09 21:04:44 +01005542 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005543 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5544 */
5545static LY_ERR
Michal Vaskocdad7122020-11-09 21:04:44 +01005546moveto_node(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005547{
Michal Vaskof03ed032020-03-04 13:31:44 +01005548 uint32_t i;
Michal Vaskodb08ce52021-10-06 08:57:49 +02005549 const struct lyd_node *siblings, *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005550 LY_ERR rc;
5551
aPiecek8b0cc152021-05-31 16:40:31 +02005552 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005553 return LY_SUCCESS;
5554 }
5555
5556 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005557 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005558 return LY_EVALID;
5559 }
5560
Michal Vasko03ff5a72019-09-11 13:49:33 +02005561 for (i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02005562 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005563
5564 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 +01005565 assert(!set->val.nodes[i].node);
Michal Vaskod3678892020-05-21 10:06:58 +02005566
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005567 /* search in all the trees */
Michal Vaskod3678892020-05-21 10:06:58 +02005568 siblings = set->tree;
Michal Vasko5bfd4be2020-06-23 13:26:19 +02005569 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005570 /* search in children */
Michal Vaskodb08ce52021-10-06 08:57:49 +02005571 siblings = lyd_child(set->val.nodes[i].node);
Michal Vaskod3678892020-05-21 10:06:58 +02005572 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005573
Michal Vaskod3678892020-05-21 10:06:58 +02005574 for (sub = siblings; sub; sub = sub->next) {
Michal Vaskodb08ce52021-10-06 08:57:49 +02005575 rc = moveto_node_check(sub, set, ncname, moveto_mod, options);
Michal Vaskod3678892020-05-21 10:06:58 +02005576 if (rc == LY_SUCCESS) {
5577 if (!replaced) {
5578 set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i);
5579 replaced = 1;
5580 } else {
5581 set_insert_node(set, sub, 0, LYXP_NODE_ELEM, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005582 }
Michal Vaskod3678892020-05-21 10:06:58 +02005583 ++i;
5584 } else if (rc == LY_EINCOMPLETE) {
5585 return rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005586 }
5587 }
5588
5589 if (!replaced) {
5590 /* no match */
5591 set_remove_node(set, i);
5592 }
5593 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005594
5595 return LY_SUCCESS;
5596}
5597
5598/**
Michal Vaskod3678892020-05-21 10:06:58 +02005599 * @brief Move context @p set to a node using hashes. Result is LYXP_SET_NODE_SET (or LYXP_SET_EMPTY).
5600 * Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005601 *
5602 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005603 * @param[in] scnode Matching node schema.
Michal Vasko004d3152020-06-11 19:59:22 +02005604 * @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 +01005605 * @param[in] options XPath options.
Michal Vaskod3678892020-05-21 10:06:58 +02005606 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5607 */
5608static LY_ERR
Michal Vaskocdad7122020-11-09 21:04:44 +01005609moveto_node_hash(struct lyxp_set *set, const struct lysc_node *scnode, const struct ly_path_predicate *predicates,
5610 uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02005611{
Michal Vasko004d3152020-06-11 19:59:22 +02005612 LY_ERR ret = LY_SUCCESS;
Michal Vaskod3678892020-05-21 10:06:58 +02005613 uint32_t i;
Michal Vaskod3678892020-05-21 10:06:58 +02005614 const struct lyd_node *siblings;
Michal Vasko004d3152020-06-11 19:59:22 +02005615 struct lyd_node *sub, *inst = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02005616
Michal Vasko004d3152020-06-11 19:59:22 +02005617 assert(scnode && (!(scnode->nodetype & (LYS_LIST | LYS_LEAFLIST)) || predicates));
Michal Vaskod3678892020-05-21 10:06:58 +02005618
aPiecek8b0cc152021-05-31 16:40:31 +02005619 if (options & LYXP_SKIP_EXPR) {
Michal Vasko004d3152020-06-11 19:59:22 +02005620 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005621 }
5622
5623 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005624 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko004d3152020-06-11 19:59:22 +02005625 ret = LY_EVALID;
5626 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005627 }
5628
5629 /* context check for all the nodes since we have the schema node */
5630 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) {
5631 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02005632 goto cleanup;
Michal Vasko69730152020-10-09 16:30:07 +02005633 } else if (set->context_op && (scnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) &&
5634 (scnode != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005635 lyxp_set_free_content(set);
5636 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +02005637 }
5638
5639 /* create specific data instance if needed */
5640 if (scnode->nodetype == LYS_LIST) {
5641 LY_CHECK_GOTO(ret = lyd_create_list(scnode, predicates, &inst), cleanup);
5642 } else if (scnode->nodetype == LYS_LEAFLIST) {
5643 LY_CHECK_GOTO(ret = lyd_create_term2(scnode, &predicates[0].value, &inst), cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02005644 }
5645
5646 for (i = 0; i < set->used; ) {
Michal Vaskod3678892020-05-21 10:06:58 +02005647 siblings = NULL;
5648
5649 if ((set->val.nodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.nodes[i].type == LYXP_NODE_ROOT)) {
5650 assert(!set->val.nodes[i].node);
5651
5652 /* search in all the trees */
5653 siblings = set->tree;
5654 } else if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
5655 /* search in children */
Radek Krejcia1c1e542020-09-29 16:06:52 +02005656 siblings = lyd_child(set->val.nodes[i].node);
Michal Vaskod3678892020-05-21 10:06:58 +02005657 }
5658
5659 /* find the node using hashes */
Michal Vasko004d3152020-06-11 19:59:22 +02005660 if (inst) {
5661 lyd_find_sibling_first(siblings, inst, &sub);
Michal Vaskod3678892020-05-21 10:06:58 +02005662 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02005663 lyd_find_sibling_val(siblings, scnode, NULL, 0, &sub);
Michal Vaskod3678892020-05-21 10:06:58 +02005664 }
5665
5666 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01005667 if (!(options & LYXP_IGNORE_WHEN) && sub && lysc_has_when(sub->schema) && !(sub->flags & LYD_WHEN_TRUE)) {
Michal Vasko004d3152020-06-11 19:59:22 +02005668 ret = LY_EINCOMPLETE;
5669 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005670 }
5671
5672 if (sub) {
5673 /* pos filled later */
Michal Vaskocb1b7c02020-07-03 13:38:12 +02005674 set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i);
Michal Vaskod3678892020-05-21 10:06:58 +02005675 ++i;
Michal Vaskocb1b7c02020-07-03 13:38:12 +02005676 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005677 /* no match */
5678 set_remove_node(set, i);
5679 }
5680 }
5681
Michal Vasko004d3152020-06-11 19:59:22 +02005682cleanup:
5683 lyd_free_tree(inst);
5684 return ret;
Michal Vaskod3678892020-05-21 10:06:58 +02005685}
5686
5687/**
5688 * @brief Move context @p set to a schema node. Result is LYXP_SET_SCNODE_SET (or LYXP_SET_EMPTY).
5689 *
5690 * @param[in,out] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005691 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005692 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005693 * @param[in] options XPath options.
5694 * @return LY_ERR
5695 */
5696static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005697moveto_scnode(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005698{
Radek Krejci857189e2020-09-01 13:26:36 +02005699 ly_bool temp_ctx = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005700 uint32_t getnext_opts;
5701 uint32_t orig_used, i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005702 uint32_t mod_idx;
Michal Vasko519fd602020-05-26 12:17:39 +02005703 const struct lysc_node *iter, *start_parent;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005704 const struct lys_module *mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005705
aPiecek8b0cc152021-05-31 16:40:31 +02005706 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005707 return LY_SUCCESS;
5708 }
5709
5710 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005711 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005712 return LY_EVALID;
5713 }
5714
Michal Vaskocafad9d2019-11-07 15:20:03 +01005715 /* getnext opts */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01005716 getnext_opts = 0;
Michal Vaskocafad9d2019-11-07 15:20:03 +01005717 if (options & LYXP_SCNODE_OUTPUT) {
5718 getnext_opts |= LYS_GETNEXT_OUTPUT;
5719 }
5720
Michal Vasko03ff5a72019-09-11 13:49:33 +02005721 orig_used = set->used;
5722 for (i = 0; i < orig_used; ++i) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005723 uint32_t idx;
5724
Radek Krejcif13b87b2020-12-01 22:02:17 +01005725 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
5726 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01005727 continue;
5728 }
5729
5730 /* remember context node */
Radek Krejcif13b87b2020-12-01 22:02:17 +01005731 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vaskoec4df482019-12-16 10:02:18 +01005732 } else {
Michal Vasko1a09b212021-05-06 13:00:10 +02005733 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005734 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005735
5736 start_parent = set->val.scnodes[i].scnode;
5737
5738 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 +02005739 /* 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 +02005740 * it can be in a top-level augment (the root node itself is useless in this case) */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005741 mod_idx = 0;
Michal Vaskoa51ef072021-07-02 10:40:30 +02005742 while ((mod = ly_ctx_get_module_iter(set->ctx, &mod_idx))) {
Michal Vasko519fd602020-05-26 12:17:39 +02005743 iter = NULL;
Michal Vasko919954a2021-07-26 09:21:42 +02005744 /* module may not be implemented or not compiled yet */
5745 while (mod->compiled && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005746 if (!moveto_scnode_check(iter, NULL, set, ncname, moveto_mod)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005747 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, &idx));
5748
Michal Vasko03ff5a72019-09-11 13:49:33 +02005749 /* we need to prevent these nodes from being considered in this moveto */
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005750 if ((idx < orig_used) && (idx > i)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005751 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005752 temp_ctx = 1;
5753 }
5754 }
5755 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005756 }
5757
Michal Vasko519fd602020-05-26 12:17:39 +02005758 } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
5759 iter = NULL;
5760 while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005761 if (!moveto_scnode_check(iter, start_parent, set, ncname, moveto_mod)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005762 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, &idx));
5763
5764 if ((idx < orig_used) && (idx > i)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005765 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005766 temp_ctx = 1;
5767 }
5768 }
5769 }
5770 }
5771 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005772
5773 /* correct temporary in_ctx values */
5774 if (temp_ctx) {
5775 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005776 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_NEW_CTX) {
5777 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005778 }
5779 }
5780 }
5781
5782 return LY_SUCCESS;
5783}
5784
5785/**
Michal Vaskod3678892020-05-21 10:06:58 +02005786 * @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 +02005787 * Context position aware.
5788 *
5789 * @param[in] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005790 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005791 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vaskocdad7122020-11-09 21:04:44 +01005792 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005793 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5794 */
5795static LY_ERR
Michal Vaskocdad7122020-11-09 21:04:44 +01005796moveto_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 +02005797{
5798 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005799 const struct lyd_node *next, *elem, *start;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005800 struct lyxp_set ret_set;
5801 LY_ERR rc;
5802
aPiecek8b0cc152021-05-31 16:40:31 +02005803 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005804 return LY_SUCCESS;
5805 }
5806
5807 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005808 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005809 return LY_EVALID;
5810 }
5811
Michal Vasko9f96a052020-03-10 09:41:45 +01005812 /* 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 +01005813 rc = moveto_node(set, NULL, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005814 LY_CHECK_RET(rc);
5815
Michal Vasko6346ece2019-09-24 13:12:53 +02005816 /* this loop traverses all the nodes in the set and adds/keeps only those that match qname */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005817 set_init(&ret_set, set);
5818 for (i = 0; i < set->used; ++i) {
5819
5820 /* TREE DFS */
5821 start = set->val.nodes[i].node;
5822 for (elem = next = start; elem; elem = next) {
Michal Vaskodb08ce52021-10-06 08:57:49 +02005823 rc = moveto_node_check(elem, set, ncname, moveto_mod, options);
Michal Vasko6346ece2019-09-24 13:12:53 +02005824 if (!rc) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005825 /* add matching node into result set */
5826 set_insert_node(&ret_set, elem, 0, LYXP_NODE_ELEM, ret_set.used);
5827 if (set_dup_node_check(set, elem, LYXP_NODE_ELEM, i)) {
5828 /* the node is a duplicate, we'll process it later in the set */
5829 goto skip_children;
5830 }
Michal Vasko6346ece2019-09-24 13:12:53 +02005831 } else if (rc == LY_EINCOMPLETE) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005832 return rc;
5833 } else if (rc == LY_EINVAL) {
5834 goto skip_children;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005835 }
5836
5837 /* TREE DFS NEXT ELEM */
5838 /* select element for the next run - children first */
Radek Krejcia1c1e542020-09-29 16:06:52 +02005839 next = lyd_child(elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005840 if (!next) {
5841skip_children:
5842 /* no children, so try siblings, but only if it's not the start,
5843 * that is considered to be the root and it's siblings are not traversed */
5844 if (elem != start) {
5845 next = elem->next;
5846 } else {
5847 break;
5848 }
5849 }
5850 while (!next) {
5851 /* no siblings, go back through the parents */
Michal Vasko9e685082021-01-29 14:49:09 +01005852 if (lyd_parent(elem) == start) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005853 /* we are done, no next element to process */
5854 break;
5855 }
5856 /* parent is already processed, go to its sibling */
Michal Vasko9e685082021-01-29 14:49:09 +01005857 elem = lyd_parent(elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005858 next = elem->next;
5859 }
5860 }
5861 }
5862
5863 /* make the temporary set the current one */
5864 ret_set.ctx_pos = set->ctx_pos;
5865 ret_set.ctx_size = set->ctx_size;
Michal Vaskod3678892020-05-21 10:06:58 +02005866 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005867 memcpy(set, &ret_set, sizeof *set);
5868
5869 return LY_SUCCESS;
5870}
5871
5872/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02005873 * @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 +02005874 *
5875 * @param[in] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005876 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005877 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005878 * @param[in] options XPath options.
5879 * @return LY_ERR
5880 */
5881static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005882moveto_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 +02005883{
Radek Krejci1deb5be2020-08-26 16:43:36 +02005884 uint32_t i, orig_used;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005885 const struct lysc_node *next, *elem, *start;
Michal Vasko6346ece2019-09-24 13:12:53 +02005886 LY_ERR rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005887
aPiecek8b0cc152021-05-31 16:40:31 +02005888 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005889 return LY_SUCCESS;
5890 }
5891
5892 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005893 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005894 return LY_EVALID;
5895 }
5896
Michal Vasko03ff5a72019-09-11 13:49:33 +02005897 orig_used = set->used;
5898 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005899 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
5900 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01005901 continue;
5902 }
5903
5904 /* remember context node */
Radek Krejcif13b87b2020-12-01 22:02:17 +01005905 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vaskoec4df482019-12-16 10:02:18 +01005906 } else {
Michal Vasko1a09b212021-05-06 13:00:10 +02005907 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005908 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005909
5910 /* TREE DFS */
5911 start = set->val.scnodes[i].scnode;
5912 for (elem = next = start; elem; elem = next) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005913 if ((elem == start) || (elem->nodetype & (LYS_CHOICE | LYS_CASE))) {
5914 /* schema-only nodes, skip root */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005915 goto next_iter;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005916 }
5917
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005918 rc = moveto_scnode_check(elem, start, set, ncname, moveto_mod);
Michal Vasko6346ece2019-09-24 13:12:53 +02005919 if (!rc) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005920 uint32_t idx;
5921
5922 if (lyxp_set_scnode_contains(set, elem, LYXP_NODE_ELEM, i, &idx)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005923 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005924 if ((uint32_t)idx > i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005925 /* we will process it later in the set */
5926 goto skip_children;
5927 }
5928 } else {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005929 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, elem, LYXP_NODE_ELEM, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005930 }
Michal Vasko6346ece2019-09-24 13:12:53 +02005931 } else if (rc == LY_EINVAL) {
5932 goto skip_children;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005933 }
5934
5935next_iter:
5936 /* TREE DFS NEXT ELEM */
5937 /* select element for the next run - children first */
Michal Vasko544e58a2021-01-28 14:33:41 +01005938 next = lysc_node_child(elem);
5939 if (next && (next->nodetype == LYS_INPUT) && (options & LYXP_SCNODE_OUTPUT)) {
5940 next = next->next;
5941 } else if (next && (next->nodetype == LYS_OUTPUT) && !(options & LYXP_SCNODE_OUTPUT)) {
5942 next = next->next;
5943 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005944 if (!next) {
5945skip_children:
5946 /* no children, so try siblings, but only if it's not the start,
5947 * that is considered to be the root and it's siblings are not traversed */
5948 if (elem != start) {
5949 next = elem->next;
5950 } else {
5951 break;
5952 }
5953 }
5954 while (!next) {
5955 /* no siblings, go back through the parents */
5956 if (elem->parent == start) {
5957 /* we are done, no next element to process */
5958 break;
5959 }
5960 /* parent is already processed, go to its sibling */
5961 elem = elem->parent;
5962 next = elem->next;
5963 }
5964 }
5965 }
5966
5967 return LY_SUCCESS;
5968}
5969
5970/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02005971 * @brief Move context @p set to an attribute. Result is LYXP_SET_NODE_SET.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005972 * Indirectly context position aware.
5973 *
5974 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005975 * @param[in] mod Matching metadata module, NULL for any.
5976 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
aPiecek8b0cc152021-05-31 16:40:31 +02005977 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005978 * @return LY_ERR
5979 */
5980static LY_ERR
aPiecek8b0cc152021-05-31 16:40:31 +02005981moveto_attr(struct lyxp_set *set, const struct lys_module *mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005982{
Michal Vasko9f96a052020-03-10 09:41:45 +01005983 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005984
aPiecek8b0cc152021-05-31 16:40:31 +02005985 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005986 return LY_SUCCESS;
5987 }
5988
5989 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005990 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005991 return LY_EVALID;
5992 }
5993
Radek Krejci1deb5be2020-08-26 16:43:36 +02005994 for (uint32_t i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02005995 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005996
5997 /* only attributes of an elem (not dummy) can be in the result, skip all the rest;
5998 * our attributes are always qualified */
Michal Vasko5c4e5892019-11-14 12:31:38 +01005999 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01006000 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006001
6002 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02006003 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006004 continue;
6005 }
6006
Michal Vaskod3678892020-05-21 10:06:58 +02006007 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006008 /* match */
6009 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01006010 set->val.meta[i].meta = sub;
6011 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006012 /* pos does not change */
6013 replaced = 1;
6014 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01006015 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 +02006016 }
6017 ++i;
6018 }
6019 }
6020 }
6021
6022 if (!replaced) {
6023 /* no match */
6024 set_remove_node(set, i);
6025 }
6026 }
6027
6028 return LY_SUCCESS;
6029}
6030
6031/**
6032 * @brief Move context @p set1 to union with @p set2. @p set2 is emptied afterwards.
Michal Vasko61ac2f62020-05-25 12:39:51 +02006033 * Result is LYXP_SET_NODE_SET. Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006034 *
6035 * @param[in,out] set1 Set to use for the result.
6036 * @param[in] set2 Set that is copied to @p set1.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006037 * @return LY_ERR
6038 */
6039static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006040moveto_union(struct lyxp_set *set1, struct lyxp_set *set2)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006041{
6042 LY_ERR rc;
6043
Michal Vaskod3678892020-05-21 10:06:58 +02006044 if ((set1->type != LYXP_SET_NODE_SET) || (set2->type != LYXP_SET_NODE_SET)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006045 LOGVAL(set1->ctx, LY_VCODE_XP_INOP_2, "union", print_set_type(set1), print_set_type(set2));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006046 return LY_EVALID;
6047 }
6048
6049 /* set2 is empty or both set1 and set2 */
Michal Vaskod3678892020-05-21 10:06:58 +02006050 if (!set2->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006051 return LY_SUCCESS;
6052 }
6053
Michal Vaskod3678892020-05-21 10:06:58 +02006054 if (!set1->used) {
aPiecekadc1e4f2021-10-07 11:15:12 +02006055 /* release hidden allocated data (lyxp_set.size) */
6056 lyxp_set_free_content(set1);
6057 /* direct copying of the entire structure */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006058 memcpy(set1, set2, sizeof *set1);
6059 /* dynamic memory belongs to set1 now, do not free */
Michal Vaskod3678892020-05-21 10:06:58 +02006060 memset(set2, 0, sizeof *set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006061 return LY_SUCCESS;
6062 }
6063
6064 /* we assume sets are sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006065 assert(!set_sort(set1) && !set_sort(set2));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006066
6067 /* sort, remove duplicates */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006068 rc = set_sorted_merge(set1, set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006069 LY_CHECK_RET(rc);
6070
6071 /* final set must be sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006072 assert(!set_sort(set1));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006073
6074 return LY_SUCCESS;
6075}
6076
6077/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02006078 * @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 +02006079 * Context position aware.
6080 *
6081 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02006082 * @param[in] mod Matching metadata module, NULL for any.
6083 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
Michal Vaskocdad7122020-11-09 21:04:44 +01006084 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006085 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6086 */
6087static int
Michal Vaskocdad7122020-11-09 21:04:44 +01006088moveto_attr_alldesc(struct lyxp_set *set, const struct lys_module *mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006089{
Michal Vasko9f96a052020-03-10 09:41:45 +01006090 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006091 struct lyxp_set *set_all_desc = NULL;
6092 LY_ERR rc;
6093
aPiecek8b0cc152021-05-31 16:40:31 +02006094 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006095 return LY_SUCCESS;
6096 }
6097
6098 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006099 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006100 return LY_EVALID;
6101 }
6102
Michal Vasko03ff5a72019-09-11 13:49:33 +02006103 /* can be optimized similarly to moveto_node_alldesc() and save considerable amount of memory,
6104 * but it likely won't be used much, so it's a waste of time */
6105 /* copy the context */
6106 set_all_desc = set_copy(set);
6107 /* get all descendant nodes (the original context nodes are removed) */
Michal Vaskocdad7122020-11-09 21:04:44 +01006108 rc = moveto_node_alldesc(set_all_desc, NULL, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006109 if (rc != LY_SUCCESS) {
6110 lyxp_set_free(set_all_desc);
6111 return rc;
6112 }
6113 /* prepend the original context nodes */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006114 rc = moveto_union(set, set_all_desc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006115 if (rc != LY_SUCCESS) {
6116 lyxp_set_free(set_all_desc);
6117 return rc;
6118 }
6119 lyxp_set_free(set_all_desc);
6120
Radek Krejci1deb5be2020-08-26 16:43:36 +02006121 for (uint32_t i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02006122 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006123
6124 /* only attributes of an elem can be in the result, skip all the rest,
6125 * we have all attributes qualified in lyd tree */
6126 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01006127 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006128 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02006129 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006130 continue;
6131 }
6132
Michal Vaskod3678892020-05-21 10:06:58 +02006133 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006134 /* match */
6135 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01006136 set->val.meta[i].meta = sub;
6137 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006138 /* pos does not change */
6139 replaced = 1;
6140 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01006141 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 +02006142 }
6143 ++i;
6144 }
6145 }
6146 }
6147
6148 if (!replaced) {
6149 /* no match */
6150 set_remove_node(set, i);
6151 }
6152 }
6153
6154 return LY_SUCCESS;
6155}
6156
6157/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02006158 * @brief Move context @p set to self and al chilren, recursively. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET.
6159 * Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006160 *
6161 * @param[in] parent Current parent.
6162 * @param[in] parent_pos Position of @p parent.
6163 * @param[in] parent_type Node type of @p parent.
6164 * @param[in,out] to_set Set to use.
6165 * @param[in] dup_check_set Set for checking duplicities.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006166 * @param[in] options XPath options.
6167 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6168 */
6169static LY_ERR
6170moveto_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 +02006171 struct lyxp_set *to_set, const struct lyxp_set *dup_check_set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006172{
Michal Vasko61ac2f62020-05-25 12:39:51 +02006173 const struct lyd_node *iter, *first;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006174 LY_ERR rc;
6175
6176 switch (parent_type) {
6177 case LYXP_NODE_ROOT:
6178 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko61ac2f62020-05-25 12:39:51 +02006179 assert(!parent);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006180
Michal Vasko61ac2f62020-05-25 12:39:51 +02006181 /* add all top-level nodes as elements */
6182 first = to_set->tree;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006183 break;
6184 case LYXP_NODE_ELEM:
Michal Vasko61ac2f62020-05-25 12:39:51 +02006185 /* add just the text node of this term element node */
6186 if (parent->schema->nodetype & (LYD_NODE_TERM | LYD_NODE_ANY)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006187 if (!set_dup_node_check(dup_check_set, parent, LYXP_NODE_TEXT, -1)) {
6188 set_insert_node(to_set, parent, parent_pos, LYXP_NODE_TEXT, to_set->used);
6189 }
Michal Vasko61ac2f62020-05-25 12:39:51 +02006190 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006191 }
Michal Vasko61ac2f62020-05-25 12:39:51 +02006192
6193 /* add all the children of this node */
Radek Krejcia1c1e542020-09-29 16:06:52 +02006194 first = lyd_child(parent);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006195 break;
6196 default:
6197 LOGINT_RET(parent->schema->module->ctx);
6198 }
6199
Michal Vasko61ac2f62020-05-25 12:39:51 +02006200 /* add all top-level nodes as elements */
6201 LY_LIST_FOR(first, iter) {
6202 /* context check */
6203 if ((parent_type == LYXP_NODE_ROOT_CONFIG) && (iter->schema->flags & LYS_CONFIG_R)) {
6204 continue;
6205 }
6206
6207 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01006208 if (!(options & LYXP_IGNORE_WHEN) && lysc_has_when(iter->schema) && !(iter->flags & LYD_WHEN_TRUE)) {
Michal Vasko61ac2f62020-05-25 12:39:51 +02006209 return LY_EINCOMPLETE;
6210 }
6211
6212 if (!set_dup_node_check(dup_check_set, iter, LYXP_NODE_ELEM, -1)) {
6213 set_insert_node(to_set, iter, 0, LYXP_NODE_ELEM, to_set->used);
6214
6215 /* also add all the children of this node, recursively */
6216 rc = moveto_self_add_children_r(iter, 0, LYXP_NODE_ELEM, to_set, dup_check_set, options);
6217 LY_CHECK_RET(rc);
6218 }
6219 }
6220
Michal Vasko03ff5a72019-09-11 13:49:33 +02006221 return LY_SUCCESS;
6222}
6223
6224/**
6225 * @brief Move context @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET
6226 * (or LYXP_SET_EMPTY). Context position aware.
6227 *
6228 * @param[in,out] set Set to use.
6229 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6230 * @param[in] options XPath options.
6231 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6232 */
6233static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006234moveto_self(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006235{
Michal Vasko03ff5a72019-09-11 13:49:33 +02006236 struct lyxp_set ret_set;
6237 LY_ERR rc;
6238
aPiecek8b0cc152021-05-31 16:40:31 +02006239 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006240 return LY_SUCCESS;
6241 }
6242
6243 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006244 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006245 return LY_EVALID;
6246 }
6247
6248 /* nothing to do */
6249 if (!all_desc) {
6250 return LY_SUCCESS;
6251 }
6252
Michal Vasko03ff5a72019-09-11 13:49:33 +02006253 /* add all the children, they get added recursively */
6254 set_init(&ret_set, set);
Radek Krejci1deb5be2020-08-26 16:43:36 +02006255 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006256 /* copy the current node to tmp */
6257 set_insert_node(&ret_set, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, ret_set.used);
6258
6259 /* do not touch attributes and text nodes */
Michal Vasko9f96a052020-03-10 09:41:45 +01006260 if ((set->val.nodes[i].type == LYXP_NODE_TEXT) || (set->val.nodes[i].type == LYXP_NODE_META)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006261 continue;
6262 }
6263
Michal Vasko03ff5a72019-09-11 13:49:33 +02006264 /* add all the children */
6265 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 +02006266 set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006267 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006268 lyxp_set_free_content(&ret_set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006269 return rc;
6270 }
6271 }
6272
6273 /* use the temporary set as the current one */
6274 ret_set.ctx_pos = set->ctx_pos;
6275 ret_set.ctx_size = set->ctx_size;
Michal Vaskod3678892020-05-21 10:06:58 +02006276 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006277 memcpy(set, &ret_set, sizeof *set);
6278
6279 return LY_SUCCESS;
6280}
6281
6282/**
6283 * @brief Move context schema @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_SCNODE_SET
6284 * (or LYXP_SET_EMPTY).
6285 *
6286 * @param[in,out] set Set to use.
6287 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6288 * @param[in] options XPath options.
6289 * @return LY_ERR
6290 */
6291static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006292moveto_scnode_self(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006293{
Radek Krejci1deb5be2020-08-26 16:43:36 +02006294 uint32_t getnext_opts;
6295 uint32_t mod_idx;
Michal Vasko519fd602020-05-26 12:17:39 +02006296 const struct lysc_node *iter, *start_parent;
6297 const struct lys_module *mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006298
aPiecek8b0cc152021-05-31 16:40:31 +02006299 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006300 return LY_SUCCESS;
6301 }
6302
6303 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006304 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006305 return LY_EVALID;
6306 }
6307
Michal Vasko519fd602020-05-26 12:17:39 +02006308 /* getnext opts */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01006309 getnext_opts = 0;
Michal Vasko519fd602020-05-26 12:17:39 +02006310 if (options & LYXP_SCNODE_OUTPUT) {
6311 getnext_opts |= LYS_GETNEXT_OUTPUT;
6312 }
6313
6314 /* add all the children, recursively as they are being added into the same set */
Radek Krejci1deb5be2020-08-26 16:43:36 +02006315 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vaskoe657f962020-12-10 12:19:48 +01006316 if (!all_desc) {
6317 /* traverse the start node */
6318 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START) {
6319 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
6320 }
6321 continue;
6322 }
6323
Radek Krejcif13b87b2020-12-01 22:02:17 +01006324 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
6325 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006326 continue;
6327 }
6328
Michal Vasko519fd602020-05-26 12:17:39 +02006329 /* remember context node */
Radek Krejcif13b87b2020-12-01 22:02:17 +01006330 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vasko519fd602020-05-26 12:17:39 +02006331 } else {
Michal Vasko1a09b212021-05-06 13:00:10 +02006332 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006333 }
6334
Michal Vasko519fd602020-05-26 12:17:39 +02006335 start_parent = set->val.scnodes[i].scnode;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006336
Michal Vasko519fd602020-05-26 12:17:39 +02006337 if ((set->val.scnodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.scnodes[i].type == LYXP_NODE_ROOT)) {
6338 /* it can actually be in any module, it's all <running> */
6339 mod_idx = 0;
Michal Vaskoa51ef072021-07-02 10:40:30 +02006340 while ((mod = ly_ctx_get_module_iter(set->ctx, &mod_idx))) {
Michal Vasko519fd602020-05-26 12:17:39 +02006341 iter = NULL;
6342 /* module may not be implemented */
6343 while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) {
6344 /* context check */
6345 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (iter->flags & LYS_CONFIG_R)) {
6346 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 Vasko519fd602020-05-26 12:17:39 +02006350 /* throw away the insert index, we want to consider that node again, recursively */
6351 }
6352 }
6353
6354 } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
6355 iter = NULL;
6356 while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006357 /* context check */
Michal Vasko519fd602020-05-26 12:17:39 +02006358 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (iter->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006359 continue;
6360 }
6361
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006362 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006363 }
6364 }
6365 }
6366
6367 return LY_SUCCESS;
6368}
6369
6370/**
6371 * @brief Move context @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_NODE_SET
6372 * (or LYXP_SET_EMPTY). Context position aware.
6373 *
6374 * @param[in] set Set to use.
6375 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6376 * @param[in] options XPath options.
6377 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6378 */
6379static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006380moveto_parent(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006381{
6382 LY_ERR rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006383 struct lyd_node *node, *new_node;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006384 enum lyxp_node_type new_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006385
aPiecek8b0cc152021-05-31 16:40:31 +02006386 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006387 return LY_SUCCESS;
6388 }
6389
6390 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006391 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006392 return LY_EVALID;
6393 }
6394
6395 if (all_desc) {
6396 /* <path>//.. == <path>//./.. */
6397 rc = moveto_self(set, 1, options);
6398 LY_CHECK_RET(rc);
6399 }
6400
Radek Krejci1deb5be2020-08-26 16:43:36 +02006401 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006402 node = set->val.nodes[i].node;
6403
6404 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9e685082021-01-29 14:49:09 +01006405 new_node = lyd_parent(node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006406 } else if (set->val.nodes[i].type == LYXP_NODE_TEXT) {
6407 new_node = node;
Michal Vasko9f96a052020-03-10 09:41:45 +01006408 } else if (set->val.nodes[i].type == LYXP_NODE_META) {
6409 new_node = set->val.meta[i].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006410 if (!new_node) {
6411 LOGINT_RET(set->ctx);
6412 }
6413 } else {
6414 /* root does not have a parent */
Michal Vasko2caefc12019-11-14 16:07:56 +01006415 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006416 continue;
6417 }
6418
Michal Vaskoa1424542019-11-14 16:08:52 +01006419 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01006420 if (!(options & LYXP_IGNORE_WHEN) && new_node && lysc_has_when(new_node->schema) && !(new_node->flags & LYD_WHEN_TRUE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006421 return LY_EINCOMPLETE;
Michal Vaskoa1424542019-11-14 16:08:52 +01006422 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006423
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006424 if (!new_node) {
Radek Krejcif6a11002020-08-21 13:29:07 +02006425 /* node already there can also be the root */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006426 new_type = set->root_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006427
Michal Vasko03ff5a72019-09-11 13:49:33 +02006428 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02006429 /* 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 +02006430 new_type = LYXP_NODE_ELEM;
6431 }
6432
Michal Vasko03ff5a72019-09-11 13:49:33 +02006433 if (set_dup_node_check(set, new_node, new_type, -1)) {
Michal Vasko2caefc12019-11-14 16:07:56 +01006434 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006435 } else {
6436 set_replace_node(set, new_node, 0, new_type, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006437 }
6438 }
6439
Michal Vasko2caefc12019-11-14 16:07:56 +01006440 set_remove_nodes_none(set);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006441 assert(!set_sort(set) && !set_sorted_dup_node_clean(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006442
6443 return LY_SUCCESS;
6444}
6445
6446/**
6447 * @brief Move context schema @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_SCNODE_SET
6448 * (or LYXP_SET_EMPTY).
6449 *
6450 * @param[in] set Set to use.
6451 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6452 * @param[in] options XPath options.
6453 * @return LY_ERR
6454 */
6455static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006456moveto_scnode_parent(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006457{
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006458 uint32_t i, orig_used, idx;
Radek Krejci857189e2020-09-01 13:26:36 +02006459 ly_bool temp_ctx = 0;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006460 const struct lysc_node *node, *new_node;
6461 enum lyxp_node_type new_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006462
aPiecek8b0cc152021-05-31 16:40:31 +02006463 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006464 return LY_SUCCESS;
6465 }
6466
6467 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006468 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006469 return LY_EVALID;
6470 }
6471
6472 if (all_desc) {
6473 /* <path>//.. == <path>//./.. */
Radek Krejci1deb5be2020-08-26 16:43:36 +02006474 LY_CHECK_RET(moveto_scnode_self(set, 1, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006475 }
6476
Michal Vasko03ff5a72019-09-11 13:49:33 +02006477 orig_used = set->used;
6478 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006479 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
6480 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006481 continue;
6482 }
6483
6484 /* remember context node */
Radek Krejcif13b87b2020-12-01 22:02:17 +01006485 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vaskoec4df482019-12-16 10:02:18 +01006486 } else {
Michal Vasko1a09b212021-05-06 13:00:10 +02006487 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006488 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006489
6490 node = set->val.scnodes[i].scnode;
6491
6492 if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
Michal Vaskod3678892020-05-21 10:06:58 +02006493 new_node = lysc_data_parent(node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006494 } else {
6495 /* root does not have a parent */
6496 continue;
6497 }
6498
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006499 if (!new_node) {
Radek Krejcif6a11002020-08-21 13:29:07 +02006500 /* node has no parent */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006501 new_type = set->root_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006502
Michal Vasko03ff5a72019-09-11 13:49:33 +02006503 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02006504 /* 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 +02006505 new_type = LYXP_NODE_ELEM;
6506 }
6507
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006508 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, new_node, new_type, &idx));
6509 if ((idx < orig_used) && (idx > i)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006510 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006511 temp_ctx = 1;
6512 }
6513 }
6514
6515 if (temp_ctx) {
6516 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006517 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_NEW_CTX) {
6518 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006519 }
6520 }
6521 }
6522
6523 return LY_SUCCESS;
6524}
6525
6526/**
6527 * @brief Move context @p set to the result of a comparison. Handles '=', '!=', '<=', '<', '>=', or '>'.
6528 * Result is LYXP_SET_BOOLEAN. Indirectly context position aware.
6529 *
6530 * @param[in,out] set1 Set to use for the result.
6531 * @param[in] set2 Set acting as the second operand for @p op.
6532 * @param[in] op Comparison operator to process.
6533 * @param[in] options XPath options.
6534 * @return LY_ERR
6535 */
6536static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02006537moveto_op_comp(struct lyxp_set *set1, struct lyxp_set *set2, const char *op, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006538{
6539 /*
6540 * NODE SET + NODE SET = NODE SET + STRING /(1 NODE SET) 2 STRING
6541 * NODE SET + STRING = STRING + STRING /1 STRING (2 STRING)
6542 * NODE SET + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6543 * NODE SET + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6544 * STRING + NODE SET = STRING + STRING /(1 STRING) 2 STRING
6545 * NUMBER + NODE SET = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6546 * BOOLEAN + NODE SET = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6547 *
6548 * '=' or '!='
6549 * BOOLEAN + BOOLEAN
6550 * BOOLEAN + STRING = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6551 * BOOLEAN + NUMBER = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6552 * STRING + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6553 * NUMBER + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6554 * NUMBER + NUMBER
6555 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6556 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6557 * STRING + STRING
6558 *
6559 * '<=', '<', '>=', '>'
6560 * NUMBER + NUMBER
6561 * BOOLEAN + BOOLEAN = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6562 * BOOLEAN + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6563 * BOOLEAN + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6564 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6565 * STRING + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6566 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6567 * NUMBER + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6568 * STRING + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6569 */
6570 struct lyxp_set iter1, iter2;
6571 int result;
6572 int64_t i;
6573 LY_ERR rc;
6574
Michal Vasko004d3152020-06-11 19:59:22 +02006575 memset(&iter1, 0, sizeof iter1);
6576 memset(&iter2, 0, sizeof iter2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006577
6578 /* iterative evaluation with node-sets */
6579 if ((set1->type == LYXP_SET_NODE_SET) || (set2->type == LYXP_SET_NODE_SET)) {
6580 if (set1->type == LYXP_SET_NODE_SET) {
6581 for (i = 0; i < set1->used; ++i) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02006582 /* cast set1 */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006583 switch (set2->type) {
6584 case LYXP_SET_NUMBER:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006585 rc = set_comp_cast(&iter1, set1, LYXP_SET_NUMBER, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006586 break;
6587 case LYXP_SET_BOOLEAN:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006588 rc = set_comp_cast(&iter1, set1, LYXP_SET_BOOLEAN, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006589 break;
6590 default:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006591 rc = set_comp_cast(&iter1, set1, LYXP_SET_STRING, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006592 break;
6593 }
6594 LY_CHECK_RET(rc);
6595
Michal Vasko4c7763f2020-07-27 17:40:37 +02006596 /* canonize set2 */
6597 LY_CHECK_ERR_RET(rc = set_comp_canonize(&iter2, set2, &set1->val.nodes[i]), lyxp_set_free_content(&iter1), rc);
6598
6599 /* compare recursively */
6600 rc = moveto_op_comp(&iter1, &iter2, op, options);
6601 lyxp_set_free_content(&iter2);
6602 LY_CHECK_ERR_RET(rc, lyxp_set_free_content(&iter1), rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006603
6604 /* lazy evaluation until true */
Michal Vasko004d3152020-06-11 19:59:22 +02006605 if (iter1.val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006606 set_fill_boolean(set1, 1);
6607 return LY_SUCCESS;
6608 }
6609 }
6610 } else {
6611 for (i = 0; i < set2->used; ++i) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02006612 /* set set2 */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006613 switch (set1->type) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02006614 case LYXP_SET_NUMBER:
6615 rc = set_comp_cast(&iter2, set2, LYXP_SET_NUMBER, i);
6616 break;
6617 case LYXP_SET_BOOLEAN:
6618 rc = set_comp_cast(&iter2, set2, LYXP_SET_BOOLEAN, i);
6619 break;
6620 default:
6621 rc = set_comp_cast(&iter2, set2, LYXP_SET_STRING, i);
6622 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006623 }
6624 LY_CHECK_RET(rc);
6625
Michal Vasko4c7763f2020-07-27 17:40:37 +02006626 /* canonize set1 */
6627 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 +02006628
Michal Vasko4c7763f2020-07-27 17:40:37 +02006629 /* compare recursively */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006630 rc = moveto_op_comp(&iter1, &iter2, op, options);
Michal Vaskod3678892020-05-21 10:06:58 +02006631 lyxp_set_free_content(&iter2);
Michal Vasko4c7763f2020-07-27 17:40:37 +02006632 LY_CHECK_ERR_RET(rc, lyxp_set_free_content(&iter1), rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006633
6634 /* lazy evaluation until true */
Michal Vasko004d3152020-06-11 19:59:22 +02006635 if (iter1.val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006636 set_fill_boolean(set1, 1);
6637 return LY_SUCCESS;
6638 }
6639 }
6640 }
6641
6642 /* false for all nodes */
6643 set_fill_boolean(set1, 0);
6644 return LY_SUCCESS;
6645 }
6646
6647 /* first convert properly */
6648 if ((op[0] == '=') || (op[0] == '!')) {
6649 if ((set1->type == LYXP_SET_BOOLEAN) || (set2->type == LYXP_SET_BOOLEAN)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006650 lyxp_set_cast(set1, LYXP_SET_BOOLEAN);
6651 lyxp_set_cast(set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006652 } else if ((set1->type == LYXP_SET_NUMBER) || (set2->type == LYXP_SET_NUMBER)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006653 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006654 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006655 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006656 LY_CHECK_RET(rc);
6657 } /* else we have 2 strings */
6658 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006659 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006660 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006661 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006662 LY_CHECK_RET(rc);
6663 }
6664
6665 assert(set1->type == set2->type);
6666
6667 /* compute result */
6668 if (op[0] == '=') {
6669 if (set1->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +02006670 result = (set1->val.bln == set2->val.bln);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006671 } else if (set1->type == LYXP_SET_NUMBER) {
6672 result = (set1->val.num == set2->val.num);
6673 } else {
6674 assert(set1->type == LYXP_SET_STRING);
Michal Vaskoac6c72f2019-11-14 16:09:34 +01006675 result = !strcmp(set1->val.str, set2->val.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006676 }
6677 } else if (op[0] == '!') {
6678 if (set1->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +02006679 result = (set1->val.bln != set2->val.bln);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006680 } else if (set1->type == LYXP_SET_NUMBER) {
6681 result = (set1->val.num != set2->val.num);
6682 } else {
6683 assert(set1->type == LYXP_SET_STRING);
Michal Vaskoc2058432020-11-06 17:26:21 +01006684 result = strcmp(set1->val.str, set2->val.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006685 }
6686 } else {
6687 assert(set1->type == LYXP_SET_NUMBER);
6688 if (op[0] == '<') {
6689 if (op[1] == '=') {
6690 result = (set1->val.num <= set2->val.num);
6691 } else {
6692 result = (set1->val.num < set2->val.num);
6693 }
6694 } else {
6695 if (op[1] == '=') {
6696 result = (set1->val.num >= set2->val.num);
6697 } else {
6698 result = (set1->val.num > set2->val.num);
6699 }
6700 }
6701 }
6702
6703 /* assign result */
6704 if (result) {
6705 set_fill_boolean(set1, 1);
6706 } else {
6707 set_fill_boolean(set1, 0);
6708 }
6709
6710 return LY_SUCCESS;
6711}
6712
6713/**
6714 * @brief Move context @p set to the result of a basic operation. Handles '+', '-', unary '-', '*', 'div',
6715 * or 'mod'. Result is LYXP_SET_NUMBER. Indirectly context position aware.
6716 *
6717 * @param[in,out] set1 Set to use for the result.
6718 * @param[in] set2 Set acting as the second operand for @p op.
6719 * @param[in] op Operator to process.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006720 * @return LY_ERR
6721 */
6722static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006723moveto_op_math(struct lyxp_set *set1, struct lyxp_set *set2, const char *op)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006724{
6725 LY_ERR rc;
6726
6727 /* unary '-' */
6728 if (!set2 && (op[0] == '-')) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006729 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006730 LY_CHECK_RET(rc);
6731 set1->val.num *= -1;
6732 lyxp_set_free(set2);
6733 return LY_SUCCESS;
6734 }
6735
6736 assert(set1 && set2);
6737
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006738 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006739 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006740 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006741 LY_CHECK_RET(rc);
6742
6743 switch (op[0]) {
6744 /* '+' */
6745 case '+':
6746 set1->val.num += set2->val.num;
6747 break;
6748
6749 /* '-' */
6750 case '-':
6751 set1->val.num -= set2->val.num;
6752 break;
6753
6754 /* '*' */
6755 case '*':
6756 set1->val.num *= set2->val.num;
6757 break;
6758
6759 /* 'div' */
6760 case 'd':
6761 set1->val.num /= set2->val.num;
6762 break;
6763
6764 /* 'mod' */
6765 case 'm':
6766 set1->val.num = ((long long)set1->val.num) % ((long long)set2->val.num);
6767 break;
6768
6769 default:
6770 LOGINT_RET(set1->ctx);
6771 }
6772
6773 return LY_SUCCESS;
6774}
6775
Michal Vasko03ff5a72019-09-11 13:49:33 +02006776/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02006777 * @brief Evaluate Predicate. Logs directly on error.
6778 *
Michal Vaskod3678892020-05-21 10:06:58 +02006779 * [9] Predicate ::= '[' Expr ']'
Michal Vasko03ff5a72019-09-11 13:49:33 +02006780 *
6781 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02006782 * @param[in] tok_idx Position in the expression @p exp.
aPiecek8b0cc152021-05-31 16:40:31 +02006783 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006784 * @param[in] options XPath options.
6785 * @param[in] parent_pos_pred Whether parent predicate was a positional one.
6786 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6787 */
6788static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02006789eval_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 +02006790{
6791 LY_ERR rc;
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01006792 uint16_t orig_exp;
6793 uint32_t i, orig_pos, orig_size;
Michal Vasko5c4e5892019-11-14 12:31:38 +01006794 int32_t pred_in_ctx;
aPiecekfff4dca2021-10-07 10:59:53 +02006795 struct lyxp_set set2 = {0};
Michal Vasko03ff5a72019-09-11 13:49:33 +02006796 struct lyd_node *orig_parent;
6797
6798 /* '[' */
aPiecek8b0cc152021-05-31 16:40:31 +02006799 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02006800 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02006801 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006802
aPiecek8b0cc152021-05-31 16:40:31 +02006803 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006804only_parse:
aPiecek8b0cc152021-05-31 16:40:31 +02006805 rc = eval_expr_select(exp, tok_idx, 0, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006806 LY_CHECK_RET(rc);
6807 } else if (set->type == LYXP_SET_NODE_SET) {
6808 /* 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 +01006809 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006810
6811 /* empty set, nothing to evaluate */
6812 if (!set->used) {
6813 goto only_parse;
6814 }
6815
Michal Vasko004d3152020-06-11 19:59:22 +02006816 orig_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006817 orig_pos = 0;
6818 orig_size = set->used;
6819 orig_parent = NULL;
Michal Vasko39dbf352020-05-21 10:08:59 +02006820 for (i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006821 set_init(&set2, set);
6822 set_insert_node(&set2, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, 0);
6823 /* remember the node context position for position() and context size for last(),
6824 * predicates should always be evaluated with respect to the child axis (since we do
6825 * not support explicit axes) so we assign positions based on their parents */
Michal Vasko9e685082021-01-29 14:49:09 +01006826 if (parent_pos_pred && (lyd_parent(set->val.nodes[i].node) != orig_parent)) {
6827 orig_parent = lyd_parent(set->val.nodes[i].node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006828 orig_pos = 1;
6829 } else {
6830 ++orig_pos;
6831 }
6832
6833 set2.ctx_pos = orig_pos;
6834 set2.ctx_size = orig_size;
Michal Vasko004d3152020-06-11 19:59:22 +02006835 *tok_idx = orig_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006836
Michal Vasko004d3152020-06-11 19:59:22 +02006837 rc = eval_expr_select(exp, tok_idx, 0, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006838 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006839 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006840 return rc;
6841 }
6842
6843 /* number is a position */
6844 if (set2.type == LYXP_SET_NUMBER) {
6845 if ((long long)set2.val.num == orig_pos) {
6846 set2.val.num = 1;
6847 } else {
6848 set2.val.num = 0;
6849 }
6850 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006851 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006852
6853 /* predicate satisfied or not? */
Michal Vasko004d3152020-06-11 19:59:22 +02006854 if (!set2.val.bln) {
Michal Vasko2caefc12019-11-14 16:07:56 +01006855 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006856 }
6857 }
Michal Vasko2caefc12019-11-14 16:07:56 +01006858 set_remove_nodes_none(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006859
6860 } else if (set->type == LYXP_SET_SCNODE_SET) {
6861 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006862 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006863 /* there is a currently-valid node */
6864 break;
6865 }
6866 }
6867 /* empty set, nothing to evaluate */
6868 if (i == set->used) {
6869 goto only_parse;
6870 }
6871
Michal Vasko004d3152020-06-11 19:59:22 +02006872 orig_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006873
Michal Vasko03ff5a72019-09-11 13:49:33 +02006874 /* set special in_ctx to all the valid snodes */
6875 pred_in_ctx = set_scnode_new_in_ctx(set);
6876
6877 /* use the valid snodes one-by-one */
6878 for (i = 0; i < set->used; ++i) {
6879 if (set->val.scnodes[i].in_ctx != pred_in_ctx) {
6880 continue;
6881 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01006882 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006883
Michal Vasko004d3152020-06-11 19:59:22 +02006884 *tok_idx = orig_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006885
Michal Vasko004d3152020-06-11 19:59:22 +02006886 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006887 LY_CHECK_RET(rc);
6888
6889 set->val.scnodes[i].in_ctx = pred_in_ctx;
6890 }
6891
6892 /* restore the state as it was before the predicate */
6893 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006894 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko1a09b212021-05-06 13:00:10 +02006895 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006896 } else if (set->val.scnodes[i].in_ctx == pred_in_ctx) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006897 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006898 }
6899 }
6900
6901 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02006902 set2.type = LYXP_SET_NODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006903 set_fill_set(&set2, set);
6904
Michal Vasko004d3152020-06-11 19:59:22 +02006905 rc = eval_expr_select(exp, tok_idx, 0, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006906 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006907 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006908 return rc;
6909 }
6910
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006911 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +02006912 if (!set2.val.bln) {
Michal Vaskod3678892020-05-21 10:06:58 +02006913 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006914 }
Michal Vaskod3678892020-05-21 10:06:58 +02006915 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006916 }
6917
6918 /* ']' */
Michal Vasko004d3152020-06-11 19:59:22 +02006919 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_BRACK2);
aPiecek8b0cc152021-05-31 16:40:31 +02006920 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02006921 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02006922 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006923
6924 return LY_SUCCESS;
6925}
6926
6927/**
Michal Vaskod3678892020-05-21 10:06:58 +02006928 * @brief Evaluate Literal. Logs directly on error.
6929 *
6930 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02006931 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02006932 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
6933 */
6934static void
Michal Vasko40308e72020-10-20 16:38:40 +02006935eval_literal(const struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set)
Michal Vaskod3678892020-05-21 10:06:58 +02006936{
6937 if (set) {
Michal Vasko004d3152020-06-11 19:59:22 +02006938 if (exp->tok_len[*tok_idx] == 2) {
Michal Vaskod3678892020-05-21 10:06:58 +02006939 set_fill_string(set, "", 0);
6940 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02006941 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 +02006942 }
6943 }
6944 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02006945 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02006946 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02006947}
6948
6949/**
Michal Vasko004d3152020-06-11 19:59:22 +02006950 * @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 +02006951 *
Michal Vasko004d3152020-06-11 19:59:22 +02006952 * @param[in] exp Full parsed XPath expression.
6953 * @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 +02006954 * @param[in] ctx_node Found schema node as the context for the predicate.
6955 * @param[in] cur_mod Current module for the expression.
6956 * @param[in] cur_node Current (original context) node.
Michal Vasko004d3152020-06-11 19:59:22 +02006957 * @param[in] format Format of any prefixes in key names/values.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006958 * @param[in] prefix_data Format-specific prefix data (see ::ly_resolve_prefix).
Michal Vasko004d3152020-06-11 19:59:22 +02006959 * @param[out] predicates Parsed predicates.
6960 * @param[out] pred_type Type of @p predicates.
6961 * @return LY_SUCCESS on success,
6962 * @return LY_ERR on any error.
Michal Vaskod3678892020-05-21 10:06:58 +02006963 */
6964static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02006965eval_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 +02006966 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 +02006967 struct ly_path_predicate **predicates, enum ly_path_pred_type *pred_type)
Michal Vaskod3678892020-05-21 10:06:58 +02006968{
Michal Vasko004d3152020-06-11 19:59:22 +02006969 LY_ERR ret = LY_SUCCESS;
6970 uint16_t key_count, e_idx, pred_idx = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02006971 const struct lysc_node *key;
Michal Vasko004d3152020-06-11 19:59:22 +02006972 size_t pred_len;
Radek Krejci1deb5be2020-08-26 16:43:36 +02006973 uint32_t prev_lo;
Michal Vasko004d3152020-06-11 19:59:22 +02006974 struct lyxp_expr *exp2 = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02006975
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006976 assert(ctx_node->nodetype & (LYS_LIST | LYS_LEAFLIST));
Michal Vaskod3678892020-05-21 10:06:58 +02006977
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006978 if (ctx_node->nodetype == LYS_LIST) {
Michal Vasko004d3152020-06-11 19:59:22 +02006979 /* get key count */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006980 if (ctx_node->flags & LYS_KEYLESS) {
Michal Vasko004d3152020-06-11 19:59:22 +02006981 return LY_EINVAL;
6982 }
Michal Vasko544e58a2021-01-28 14:33:41 +01006983 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 +02006984 assert(key_count);
Michal Vaskod3678892020-05-21 10:06:58 +02006985
Michal Vasko004d3152020-06-11 19:59:22 +02006986 /* learn where the predicates end */
6987 e_idx = *tok_idx;
6988 while (key_count) {
6989 /* '[' */
6990 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
6991 return LY_EINVAL;
6992 }
6993 ++e_idx;
6994
Michal Vasko3354d272021-04-06 09:40:06 +02006995 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_NAMETEST)) {
6996 /* definitely not a key */
6997 return LY_EINVAL;
6998 }
6999
Michal Vasko004d3152020-06-11 19:59:22 +02007000 /* ']' */
7001 while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) {
7002 ++e_idx;
7003 }
7004 ++e_idx;
7005
7006 /* another presumably key predicate parsed */
7007 --key_count;
7008 }
Michal Vasko004d3152020-06-11 19:59:22 +02007009 } else {
7010 /* learn just where this single predicate ends */
7011 e_idx = *tok_idx;
7012
Michal Vaskod3678892020-05-21 10:06:58 +02007013 /* '[' */
Michal Vasko004d3152020-06-11 19:59:22 +02007014 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
7015 return LY_EINVAL;
7016 }
7017 ++e_idx;
Michal Vaskod3678892020-05-21 10:06:58 +02007018
Michal Vasko3354d272021-04-06 09:40:06 +02007019 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_DOT)) {
7020 /* definitely not the value */
7021 return LY_EINVAL;
7022 }
7023
Michal Vaskod3678892020-05-21 10:06:58 +02007024 /* ']' */
Michal Vasko004d3152020-06-11 19:59:22 +02007025 while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) {
7026 ++e_idx;
7027 }
7028 ++e_idx;
Michal Vaskod3678892020-05-21 10:06:58 +02007029 }
7030
Michal Vasko004d3152020-06-11 19:59:22 +02007031 /* get the joined length of all the keys predicates/of the single leaf-list predicate */
7032 pred_len = (exp->tok_pos[e_idx - 1] + exp->tok_len[e_idx - 1]) - exp->tok_pos[*tok_idx];
7033
7034 /* turn logging off */
7035 prev_lo = ly_log_options(0);
7036
7037 /* parse the predicate(s) */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007038 LY_CHECK_GOTO(ret = ly_path_parse_predicate(ctx_node->module->ctx, ctx_node, exp->expr + exp->tok_pos[*tok_idx],
7039 pred_len, LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &exp2), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02007040
7041 /* compile */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007042 ret = ly_path_compile_predicate(ctx_node->module->ctx, cur_node, cur_mod, ctx_node, exp2, &pred_idx, format,
7043 prefix_data, predicates, pred_type);
Michal Vasko004d3152020-06-11 19:59:22 +02007044 LY_CHECK_GOTO(ret, cleanup);
7045
7046 /* success, the predicate must include all the needed information for hash-based search */
7047 *tok_idx = e_idx;
7048
7049cleanup:
7050 ly_log_options(prev_lo);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007051 lyxp_expr_free(ctx_node->module->ctx, exp2);
Michal Vasko004d3152020-06-11 19:59:22 +02007052 return ret;
Michal Vaskod3678892020-05-21 10:06:58 +02007053}
7054
7055/**
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007056 * @brief Search for/check the next schema node that could be the only matching schema node meaning the
7057 * data node(s) could be found using a single hash-based search.
7058 *
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007059 * @param[in] ctx libyang context.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007060 * @param[in] node Next context node to check.
7061 * @param[in] name Expected node name.
7062 * @param[in] name_len Length of @p name.
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007063 * @param[in] moveto_mod Expected node module, can be NULL for JSON format with no prefix.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007064 * @param[in] root_type XPath root type.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007065 * @param[in] format Prefix format.
7066 * @param[in,out] found Previously found node, is updated.
7067 * @return LY_SUCCESS on success,
7068 * @return LY_ENOT if the whole check failed and hashes cannot be used.
7069 */
7070static LY_ERR
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007071eval_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 +02007072 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 +02007073 const struct lysc_node **found)
7074{
7075 const struct lysc_node *scnode;
7076 const struct lys_module *mod;
7077 uint32_t idx = 0;
7078
Radek Krejci8df109d2021-04-23 12:19:08 +02007079 assert((format == LY_VALUE_JSON) || moveto_mod);
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007080
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007081continue_search:
Michal Vasko7d1d0912020-10-16 08:38:30 +02007082 scnode = NULL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007083 if (!node) {
Radek Krejci8df109d2021-04-23 12:19:08 +02007084 if ((format == LY_VALUE_JSON) && !moveto_mod) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007085 /* search all modules for a single match */
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007086 while ((mod = ly_ctx_get_module_iter(ctx, &idx))) {
Michal Vasko35a3b1d2021-07-14 09:34:16 +02007087 if (!mod->implemented) {
7088 continue;
7089 }
7090
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007091 scnode = lys_find_child(NULL, mod, name, name_len, 0, 0);
7092 if (scnode) {
7093 /* we have found a match */
7094 break;
7095 }
7096 }
7097
Michal Vasko7d1d0912020-10-16 08:38:30 +02007098 if (!scnode) {
7099 /* all modules searched */
7100 idx = 0;
7101 }
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007102 } else {
7103 /* search in top-level */
7104 scnode = lys_find_child(NULL, moveto_mod, name, name_len, 0, 0);
7105 }
7106 } else if (!*found || (lysc_data_parent(*found) != node->schema)) {
Radek Krejci8df109d2021-04-23 12:19:08 +02007107 if ((format == LY_VALUE_JSON) && !moveto_mod) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007108 /* we must adjust the module to inherit the one from the context node */
7109 moveto_mod = node->schema->module;
7110 }
7111
7112 /* search in children, do not repeat the same search */
7113 scnode = lys_find_child(node->schema, moveto_mod, name, name_len, 0, 0);
Michal Vasko7d1d0912020-10-16 08:38:30 +02007114 } /* else skip redundant search */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007115
7116 /* additional context check */
7117 if (scnode && (root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) {
7118 scnode = NULL;
7119 }
7120
7121 if (scnode) {
7122 if (*found) {
7123 /* we found a schema node with the same name but at different level, give up, too complicated
7124 * (more hash-based searches would be required, not supported) */
7125 return LY_ENOT;
7126 } else {
7127 /* remember the found schema node and continue to make sure it can be used */
7128 *found = scnode;
7129 }
7130 }
7131
7132 if (idx) {
7133 /* continue searching all the following models */
7134 goto continue_search;
7135 }
7136
7137 return LY_SUCCESS;
7138}
7139
7140/**
Michal Vasko4ad69e72021-10-26 16:25:55 +02007141 * @brief Generate message when no matching schema nodes were found for a path segment.
7142 *
7143 * @param[in] set XPath set.
7144 * @param[in] scparent Previous schema parent in the context, if only one.
7145 * @param[in] ncname XPath NCName being evaluated.
7146 * @param[in] ncname_len Length of @p ncname.
7147 * @param[in] expr Whole XPath expression.
7148 * @param[in] options XPath options.
7149 */
7150static void
7151eval_name_test_scnode_no_match_msg(struct lyxp_set *set, const struct lyxp_set_scnode *scparent, const char *ncname,
7152 uint16_t ncname_len, const char *expr, uint32_t options)
7153{
7154 const char *format;
7155 char *path = NULL, *ppath = NULL;
7156
7157 path = lysc_path(set->cur_scnode, LYSC_PATH_LOG, NULL, 0);
7158 if (scparent) {
7159 /* generate path for the parent */
7160 if (scparent->type == LYXP_NODE_ELEM) {
7161 ppath = lysc_path(scparent->scnode, LYSC_PATH_LOG, NULL, 0);
7162 } else if (scparent->type == LYXP_NODE_ROOT) {
7163 ppath = strdup("<root>");
7164 } else if (scparent->type == LYXP_NODE_ROOT_CONFIG) {
7165 ppath = strdup("<config-root>");
7166 }
7167 }
7168 if (ppath) {
7169 format = "Schema node \"%.*s\" for parent \"%s\" not found; in expr \"%.*s\" with context node \"%s\".";
7170 if (options & LYXP_SCNODE_ERROR) {
7171 LOGERR(set->ctx, LY_EVALID, format, ncname_len, ncname, ppath, (ncname - expr) + ncname_len, expr, path);
7172 } else {
7173 LOGWRN(set->ctx, format, ncname_len, ncname, ppath, (ncname - expr) + ncname_len, expr, path);
7174 }
7175 } else {
7176 format = "Schema node \"%.*s\" not found; in expr \"%.*s\" with context node \"%s\".";
7177 if (options & LYXP_SCNODE_ERROR) {
7178 LOGERR(set->ctx, LY_EVALID, format, ncname_len, ncname, (ncname - expr) + ncname_len, expr, path);
7179 } else {
7180 LOGWRN(set->ctx, format, ncname_len, ncname, (ncname - expr) + ncname_len, expr, path);
7181 }
7182 }
7183 free(path);
7184 free(ppath);
7185}
7186
7187/**
Michal Vaskod3678892020-05-21 10:06:58 +02007188 * @brief Evaluate NameTest and any following Predicates. Logs directly on error.
7189 *
7190 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
7191 * [6] NodeTest ::= NameTest | NodeType '(' ')'
7192 * [7] NameTest ::= '*' | NCName ':' '*' | QName
7193 *
7194 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007195 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02007196 * @param[in] attr_axis Whether to search attributes or standard nodes.
7197 * @param[in] all_desc Whether to search all the descendants or children only.
aPiecek8b0cc152021-05-31 16:40:31 +02007198 * @param[in,out] set Context and result set.
Michal Vaskod3678892020-05-21 10:06:58 +02007199 * @param[in] options XPath options.
Michal Vaskoa036a6b2021-10-12 16:05:23 +02007200 * @return LY_ERR (LY_EINCOMPLETE on unresolved when, LY_ENOT for not found schema node)
Michal Vaskod3678892020-05-21 10:06:58 +02007201 */
7202static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007203eval_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 +02007204 struct lyxp_set *set, uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02007205{
Michal Vaskoa036a6b2021-10-12 16:05:23 +02007206 LY_ERR rc = LY_SUCCESS, r;
Michal Vasko004d3152020-06-11 19:59:22 +02007207 const char *ncname, *ncname_dict = NULL;
7208 uint16_t ncname_len;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007209 const struct lys_module *moveto_mod = NULL;
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007210 const struct lysc_node *scnode = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02007211 struct ly_path_predicate *predicates = NULL;
7212 enum ly_path_pred_type pred_type = 0;
Michal Vaskoa036a6b2021-10-12 16:05:23 +02007213 int scnode_skip_pred = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02007214
aPiecek8b0cc152021-05-31 16:40:31 +02007215 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007216 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007217 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007218
aPiecek8b0cc152021-05-31 16:40:31 +02007219 if (options & LYXP_SKIP_EXPR) {
Michal Vaskod3678892020-05-21 10:06:58 +02007220 goto moveto;
7221 }
7222
Michal Vasko004d3152020-06-11 19:59:22 +02007223 ncname = &exp->expr[exp->tok_pos[*tok_idx - 1]];
7224 ncname_len = exp->tok_len[*tok_idx - 1];
Michal Vaskod3678892020-05-21 10:06:58 +02007225
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007226 if ((ncname[0] == '*') && (ncname_len == 1)) {
7227 /* all nodes will match */
7228 goto moveto;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007229 }
7230
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007231 /* parse (and skip) module name */
7232 rc = moveto_resolve_model(&ncname, &ncname_len, set, NULL, &moveto_mod);
Michal Vaskod3678892020-05-21 10:06:58 +02007233 LY_CHECK_GOTO(rc, cleanup);
7234
Radek Krejci8df109d2021-04-23 12:19:08 +02007235 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 +02007236 /* find the matching schema node in some parent in the context */
Radek Krejci1deb5be2020-08-26 16:43:36 +02007237 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007238 if (eval_name_test_with_predicate_get_scnode(set->ctx, set->val.nodes[i].node, ncname, ncname_len,
7239 moveto_mod, set->root_type, set->format, &scnode)) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007240 /* check failed */
7241 scnode = NULL;
7242 break;
Michal Vaskod3678892020-05-21 10:06:58 +02007243 }
7244 }
7245
Michal Vasko004d3152020-06-11 19:59:22 +02007246 if (scnode && (scnode->nodetype & (LYS_LIST | LYS_LEAFLIST))) {
7247 /* try to create the predicates */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007248 if (eval_name_test_try_compile_predicates(exp, tok_idx, scnode, set->cur_mod, set->cur_node ?
7249 set->cur_node->schema : NULL, set->format, set->prefix_data, &predicates, &pred_type)) {
Michal Vasko004d3152020-06-11 19:59:22 +02007250 /* hashes cannot be used */
Michal Vaskod3678892020-05-21 10:06:58 +02007251 scnode = NULL;
7252 }
7253 }
7254 }
7255
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007256 if (!scnode) {
7257 /* we are not able to match based on a schema node and not all the modules match ("*"),
Michal Vasko004d3152020-06-11 19:59:22 +02007258 * use dictionary for efficient comparison */
Radek Krejci011e4aa2020-09-04 15:22:31 +02007259 LY_CHECK_GOTO(rc = lydict_insert(set->ctx, ncname, ncname_len, &ncname_dict), cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02007260 }
7261
7262moveto:
7263 /* move to the attribute(s), data node(s), or schema node(s) */
7264 if (attr_axis) {
aPiecek8b0cc152021-05-31 16:40:31 +02007265 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007266 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskod3678892020-05-21 10:06:58 +02007267 } else {
7268 if (all_desc) {
Michal Vaskocdad7122020-11-09 21:04:44 +01007269 rc = moveto_attr_alldesc(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007270 } else {
aPiecek8b0cc152021-05-31 16:40:31 +02007271 rc = moveto_attr(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007272 }
7273 LY_CHECK_GOTO(rc, cleanup);
7274 }
7275 } else {
aPiecek8b0cc152021-05-31 16:40:31 +02007276 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02007277 int64_t i;
Michal Vaskoa036a6b2021-10-12 16:05:23 +02007278 const struct lyxp_set_scnode *scparent = NULL;
Michal Vaskoa036a6b2021-10-12 16:05:23 +02007279
7280 /* remember parent if there is only one, to print in the warning */
7281 for (i = 0; i < set->used; ++i) {
7282 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
7283 if (!scparent) {
7284 /* remember the context node */
7285 scparent = &set->val.scnodes[i];
7286 } else {
7287 /* several context nodes, no reasonable error possible */
7288 scparent = NULL;
7289 break;
7290 }
7291 }
7292 }
Radek Krejci1deb5be2020-08-26 16:43:36 +02007293
Michal Vaskod3678892020-05-21 10:06:58 +02007294 if (all_desc) {
Michal Vasko004d3152020-06-11 19:59:22 +02007295 rc = moveto_scnode_alldesc(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007296 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007297 rc = moveto_scnode(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007298 }
7299 LY_CHECK_GOTO(rc, cleanup);
7300
7301 for (i = set->used - 1; i > -1; --i) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007302 if (set->val.scnodes[i].in_ctx > LYXP_SET_SCNODE_ATOM_NODE) {
Michal Vaskod3678892020-05-21 10:06:58 +02007303 break;
7304 }
7305 }
7306 if (i == -1) {
Michal Vasko4ad69e72021-10-26 16:25:55 +02007307 /* generate message */
7308 eval_name_test_scnode_no_match_msg(set, scparent, ncname, ncname_len, exp->expr, options);
7309
7310 if (options & LYXP_SCNODE_ERROR) {
7311 /* error */
7312 rc = LY_EVALID;
7313 goto cleanup;
Michal Vaskoa036a6b2021-10-12 16:05:23 +02007314 }
Michal Vaskoa036a6b2021-10-12 16:05:23 +02007315
7316 /* skip the predicates and the rest of this path to not generate invalid warnings */
7317 rc = LY_ENOT;
7318 scnode_skip_pred = 1;
Michal Vaskod3678892020-05-21 10:06:58 +02007319 }
7320 } else {
7321 if (all_desc) {
Michal Vaskocdad7122020-11-09 21:04:44 +01007322 rc = moveto_node_alldesc(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007323 } else {
7324 if (scnode) {
7325 /* we can find the nodes using hashes */
Michal Vaskocdad7122020-11-09 21:04:44 +01007326 rc = moveto_node_hash(set, scnode, predicates, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007327 } else {
Michal Vaskocdad7122020-11-09 21:04:44 +01007328 rc = moveto_node(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007329 }
7330 }
7331 LY_CHECK_GOTO(rc, cleanup);
7332 }
7333 }
7334
Michal Vaskoa036a6b2021-10-12 16:05:23 +02007335 if (scnode_skip_pred) {
7336 /* skip predicates */
7337 options |= LYXP_SKIP_EXPR;
7338 }
7339
Michal Vaskod3678892020-05-21 10:06:58 +02007340 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007341 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
Michal Vaskoa036a6b2021-10-12 16:05:23 +02007342 r = eval_predicate(exp, tok_idx, set, options, 1);
7343 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02007344 }
7345
7346cleanup:
Michal Vaskoa036a6b2021-10-12 16:05:23 +02007347 if (scnode_skip_pred) {
7348 /* restore options */
7349 options &= ~LYXP_SKIP_EXPR;
7350 }
aPiecek8b0cc152021-05-31 16:40:31 +02007351 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko004d3152020-06-11 19:59:22 +02007352 lydict_remove(set->ctx, ncname_dict);
Michal Vaskof7e16e22020-10-21 09:24:39 +02007353 ly_path_predicates_free(set->ctx, pred_type, predicates);
Michal Vaskodb51a8d2020-05-27 15:22:29 +02007354 }
Michal Vaskod3678892020-05-21 10:06:58 +02007355 return rc;
7356}
7357
7358/**
7359 * @brief Evaluate NodeType and any following Predicates. Logs directly on error.
7360 *
7361 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
7362 * [6] NodeTest ::= NameTest | NodeType '(' ')'
7363 * [8] NodeType ::= 'text' | 'node'
7364 *
7365 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007366 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02007367 * @param[in] attr_axis Whether to search attributes or standard nodes.
7368 * @param[in] all_desc Whether to search all the descendants or children only.
aPiecek8b0cc152021-05-31 16:40:31 +02007369 * @param[in,out] set Context and result set.
Michal Vaskod3678892020-05-21 10:06:58 +02007370 * @param[in] options XPath options.
7371 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7372 */
7373static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007374eval_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 +02007375 struct lyxp_set *set, uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02007376{
7377 LY_ERR rc;
7378
7379 /* TODO */
7380 (void)attr_axis;
7381 (void)all_desc;
7382
aPiecek8b0cc152021-05-31 16:40:31 +02007383 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko004d3152020-06-11 19:59:22 +02007384 assert(exp->tok_len[*tok_idx] == 4);
Michal Vaskod3678892020-05-21 10:06:58 +02007385 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007386 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
aPiecek8b0cc152021-05-31 16:40:31 +02007387 options |= LYXP_SKIP_EXPR;
Michal Vaskod3678892020-05-21 10:06:58 +02007388 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007389 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "node", 4)) {
Michal Vaskod3678892020-05-21 10:06:58 +02007390 rc = xpath_node(NULL, 0, set, options);
7391 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007392 assert(!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "text", 4));
Michal Vaskod3678892020-05-21 10:06:58 +02007393 rc = xpath_text(NULL, 0, set, options);
7394 }
7395 LY_CHECK_RET(rc);
7396 }
7397 }
aPiecek8b0cc152021-05-31 16:40:31 +02007398 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007399 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007400 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007401
7402 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02007403 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
aPiecek8b0cc152021-05-31 16:40:31 +02007404 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007405 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007406 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007407
7408 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007409 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
aPiecek8b0cc152021-05-31 16:40:31 +02007410 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007411 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007412 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007413
7414 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007415 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7416 rc = eval_predicate(exp, tok_idx, set, options, 1);
Michal Vaskod3678892020-05-21 10:06:58 +02007417 LY_CHECK_RET(rc);
7418 }
7419
7420 return LY_SUCCESS;
7421}
7422
7423/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02007424 * @brief Evaluate RelativeLocationPath. Logs directly on error.
7425 *
7426 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
7427 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
Michal Vaskod3678892020-05-21 10:06:58 +02007428 * [6] NodeTest ::= NameTest | NodeType '(' ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02007429 *
7430 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007431 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007432 * @param[in] all_desc Whether to search all the descendants or children only.
aPiecek8b0cc152021-05-31 16:40:31 +02007433 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007434 * @param[in] options XPath options.
7435 * @return LY_ERR (YL_EINCOMPLETE on unresolved when)
7436 */
7437static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007438eval_relative_location_path(const struct lyxp_expr *exp, uint16_t *tok_idx, ly_bool all_desc, struct lyxp_set *set,
7439 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007440{
Radek Krejci857189e2020-09-01 13:26:36 +02007441 ly_bool attr_axis;
Michal Vaskoa036a6b2021-10-12 16:05:23 +02007442 LY_ERR rc = LY_SUCCESS;
7443 int scnode_skip_path = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007444
7445 goto step;
7446 do {
7447 /* evaluate '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02007448 if (exp->tok_len[*tok_idx] == 1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007449 all_desc = 0;
7450 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007451 assert(exp->tok_len[*tok_idx] == 2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007452 all_desc = 1;
7453 }
aPiecek8b0cc152021-05-31 16:40:31 +02007454 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007455 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007456 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007457
7458step:
Michal Vaskod3678892020-05-21 10:06:58 +02007459 /* evaluate abbreviated axis '@'? if any */
Michal Vasko004d3152020-06-11 19:59:22 +02007460 if (exp->tokens[*tok_idx] == LYXP_TOKEN_AT) {
Michal Vaskod3678892020-05-21 10:06:58 +02007461 attr_axis = 1;
7462
aPiecek8b0cc152021-05-31 16:40:31 +02007463 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007464 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007465 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007466 } else {
7467 attr_axis = 0;
7468 }
7469
Michal Vasko03ff5a72019-09-11 13:49:33 +02007470 /* Step */
Michal Vasko004d3152020-06-11 19:59:22 +02007471 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007472 case LYXP_TOKEN_DOT:
7473 /* evaluate '.' */
aPiecek8b0cc152021-05-31 16:40:31 +02007474 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007475 rc = moveto_scnode_self(set, all_desc, options);
7476 } else {
7477 rc = moveto_self(set, all_desc, options);
7478 }
Michal Vaskoa036a6b2021-10-12 16:05:23 +02007479 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007480 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007481 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007482 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007483 break;
7484
7485 case LYXP_TOKEN_DDOT:
7486 /* evaluate '..' */
aPiecek8b0cc152021-05-31 16:40:31 +02007487 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007488 rc = moveto_scnode_parent(set, all_desc, options);
7489 } else {
7490 rc = moveto_parent(set, all_desc, options);
7491 }
Michal Vaskoa036a6b2021-10-12 16:05:23 +02007492 LY_CHECK_GOTO(rc, cleanup);
aPiecek8b0cc152021-05-31 16:40:31 +02007493 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007494 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007495 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007496 break;
7497
Michal Vasko03ff5a72019-09-11 13:49:33 +02007498 case LYXP_TOKEN_NAMETEST:
Michal Vaskod3678892020-05-21 10:06:58 +02007499 /* evaluate NameTest Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007500 rc = eval_name_test_with_predicate(exp, tok_idx, attr_axis, all_desc, set, options);
Michal Vaskoa036a6b2021-10-12 16:05:23 +02007501 if (rc == LY_ENOT) {
7502 assert(options & LYXP_SCNODE_ALL);
7503 /* skip the rest of this path */
7504 rc = LY_SUCCESS;
7505 scnode_skip_path = 1;
7506 options |= LYXP_SKIP_EXPR;
7507 }
7508 LY_CHECK_GOTO(rc, cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02007509 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007510
Michal Vaskod3678892020-05-21 10:06:58 +02007511 case LYXP_TOKEN_NODETYPE:
7512 /* evaluate NodeType Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007513 rc = eval_node_type_with_predicate(exp, tok_idx, attr_axis, all_desc, set, options);
Michal Vaskoa036a6b2021-10-12 16:05:23 +02007514 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007515 break;
7516
7517 default:
Michal Vaskoa036a6b2021-10-12 16:05:23 +02007518 LOGINT(set->ctx);
7519 rc = LY_EINT;
7520 goto cleanup;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007521 }
Michal Vasko004d3152020-06-11 19:59:22 +02007522 } while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007523
Michal Vaskoa036a6b2021-10-12 16:05:23 +02007524cleanup:
7525 if (scnode_skip_path) {
7526 options &= ~LYXP_SKIP_EXPR;
7527 }
7528 return rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007529}
7530
7531/**
7532 * @brief Evaluate AbsoluteLocationPath. Logs directly on error.
7533 *
7534 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
7535 *
7536 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007537 * @param[in] tok_idx Position in the expression @p exp.
aPiecek8b0cc152021-05-31 16:40:31 +02007538 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007539 * @param[in] options XPath options.
7540 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7541 */
7542static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007543eval_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 +02007544{
Radek Krejci857189e2020-09-01 13:26:36 +02007545 ly_bool all_desc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007546
aPiecek8b0cc152021-05-31 16:40:31 +02007547 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007548 /* no matter what tokens follow, we need to be at the root */
Michal Vaskob0099a92020-08-31 14:55:23 +02007549 LY_CHECK_RET(moveto_root(set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007550 }
7551
7552 /* '/' RelativeLocationPath? */
Michal Vasko004d3152020-06-11 19:59:22 +02007553 if (exp->tok_len[*tok_idx] == 1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007554 /* evaluate '/' - deferred */
7555 all_desc = 0;
aPiecek8b0cc152021-05-31 16:40:31 +02007556 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007557 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007558 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007559
Michal Vasko004d3152020-06-11 19:59:22 +02007560 if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007561 return LY_SUCCESS;
7562 }
Michal Vasko004d3152020-06-11 19:59:22 +02007563 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007564 case LYXP_TOKEN_DOT:
7565 case LYXP_TOKEN_DDOT:
7566 case LYXP_TOKEN_AT:
7567 case LYXP_TOKEN_NAMETEST:
7568 case LYXP_TOKEN_NODETYPE:
Michal Vaskob0099a92020-08-31 14:55:23 +02007569 LY_CHECK_RET(eval_relative_location_path(exp, tok_idx, all_desc, set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007570 break;
7571 default:
7572 break;
7573 }
7574
Michal Vasko03ff5a72019-09-11 13:49:33 +02007575 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02007576 /* '//' RelativeLocationPath */
Michal Vasko03ff5a72019-09-11 13:49:33 +02007577 /* evaluate '//' - deferred so as not to waste memory by remembering all the nodes */
7578 all_desc = 1;
aPiecek8b0cc152021-05-31 16:40:31 +02007579 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007580 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007581 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007582
Michal Vaskob0099a92020-08-31 14:55:23 +02007583 LY_CHECK_RET(eval_relative_location_path(exp, tok_idx, all_desc, set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007584 }
7585
7586 return LY_SUCCESS;
7587}
7588
7589/**
7590 * @brief Evaluate FunctionCall. Logs directly on error.
7591 *
Michal Vaskod3678892020-05-21 10:06:58 +02007592 * [11] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02007593 *
7594 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007595 * @param[in] tok_idx Position in the expression @p exp.
aPiecek8b0cc152021-05-31 16:40:31 +02007596 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007597 * @param[in] options XPath options.
7598 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7599 */
7600static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007601eval_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 +02007602{
7603 LY_ERR rc;
Michal Vasko69730152020-10-09 16:30:07 +02007604
Radek Krejci1deb5be2020-08-26 16:43:36 +02007605 LY_ERR (*xpath_func)(struct lyxp_set **, uint16_t, struct lyxp_set *, uint32_t) = NULL;
Michal Vasko0cbf54f2019-12-16 10:01:06 +01007606 uint16_t arg_count = 0, i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007607 struct lyxp_set **args = NULL, **args_aux;
7608
aPiecek8b0cc152021-05-31 16:40:31 +02007609 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007610 /* FunctionName */
Michal Vasko004d3152020-06-11 19:59:22 +02007611 switch (exp->tok_len[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007612 case 3:
Michal Vasko004d3152020-06-11 19:59:22 +02007613 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007614 xpath_func = &xpath_not;
Michal Vasko004d3152020-06-11 19:59:22 +02007615 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007616 xpath_func = &xpath_sum;
7617 }
7618 break;
7619 case 4:
Michal Vasko004d3152020-06-11 19:59:22 +02007620 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007621 xpath_func = &xpath_lang;
Michal Vasko004d3152020-06-11 19:59:22 +02007622 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007623 xpath_func = &xpath_last;
Michal Vasko004d3152020-06-11 19:59:22 +02007624 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007625 xpath_func = &xpath_name;
Michal Vasko004d3152020-06-11 19:59:22 +02007626 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007627 xpath_func = &xpath_true;
7628 }
7629 break;
7630 case 5:
Michal Vasko004d3152020-06-11 19:59:22 +02007631 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007632 xpath_func = &xpath_count;
Michal Vasko004d3152020-06-11 19:59:22 +02007633 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007634 xpath_func = &xpath_false;
Michal Vasko004d3152020-06-11 19:59:22 +02007635 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007636 xpath_func = &xpath_floor;
Michal Vasko004d3152020-06-11 19:59:22 +02007637 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007638 xpath_func = &xpath_round;
Michal Vasko004d3152020-06-11 19:59:22 +02007639 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007640 xpath_func = &xpath_deref;
7641 }
7642 break;
7643 case 6:
Michal Vasko004d3152020-06-11 19:59:22 +02007644 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007645 xpath_func = &xpath_concat;
Michal Vasko004d3152020-06-11 19:59:22 +02007646 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007647 xpath_func = &xpath_number;
Michal Vasko004d3152020-06-11 19:59:22 +02007648 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007649 xpath_func = &xpath_string;
7650 }
7651 break;
7652 case 7:
Michal Vasko004d3152020-06-11 19:59:22 +02007653 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007654 xpath_func = &xpath_boolean;
Michal Vasko004d3152020-06-11 19:59:22 +02007655 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007656 xpath_func = &xpath_ceiling;
Michal Vasko004d3152020-06-11 19:59:22 +02007657 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007658 xpath_func = &xpath_current;
7659 }
7660 break;
7661 case 8:
Michal Vasko004d3152020-06-11 19:59:22 +02007662 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007663 xpath_func = &xpath_contains;
Michal Vasko004d3152020-06-11 19:59:22 +02007664 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007665 xpath_func = &xpath_position;
Michal Vasko004d3152020-06-11 19:59:22 +02007666 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "re-match", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007667 xpath_func = &xpath_re_match;
7668 }
7669 break;
7670 case 9:
Michal Vasko004d3152020-06-11 19:59:22 +02007671 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007672 xpath_func = &xpath_substring;
Michal Vasko004d3152020-06-11 19:59:22 +02007673 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007674 xpath_func = &xpath_translate;
7675 }
7676 break;
7677 case 10:
Michal Vasko004d3152020-06-11 19:59:22 +02007678 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007679 xpath_func = &xpath_local_name;
Michal Vasko004d3152020-06-11 19:59:22 +02007680 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007681 xpath_func = &xpath_enum_value;
Michal Vasko004d3152020-06-11 19:59:22 +02007682 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007683 xpath_func = &xpath_bit_is_set;
7684 }
7685 break;
7686 case 11:
Michal Vasko004d3152020-06-11 19:59:22 +02007687 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007688 xpath_func = &xpath_starts_with;
7689 }
7690 break;
7691 case 12:
Michal Vasko004d3152020-06-11 19:59:22 +02007692 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007693 xpath_func = &xpath_derived_from;
7694 }
7695 break;
7696 case 13:
Michal Vasko004d3152020-06-11 19:59:22 +02007697 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007698 xpath_func = &xpath_namespace_uri;
Michal Vasko004d3152020-06-11 19:59:22 +02007699 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string-length", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007700 xpath_func = &xpath_string_length;
7701 }
7702 break;
7703 case 15:
Michal Vasko004d3152020-06-11 19:59:22 +02007704 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007705 xpath_func = &xpath_normalize_space;
Michal Vasko004d3152020-06-11 19:59:22 +02007706 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-after", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007707 xpath_func = &xpath_substring_after;
7708 }
7709 break;
7710 case 16:
Michal Vasko004d3152020-06-11 19:59:22 +02007711 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007712 xpath_func = &xpath_substring_before;
7713 }
7714 break;
7715 case 20:
Michal Vasko004d3152020-06-11 19:59:22 +02007716 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from-or-self", 20)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007717 xpath_func = &xpath_derived_from_or_self;
7718 }
7719 break;
7720 }
7721
7722 if (!xpath_func) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01007723 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 +02007724 return LY_EVALID;
7725 }
7726 }
7727
aPiecek8b0cc152021-05-31 16:40:31 +02007728 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007729 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007730 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007731
7732 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02007733 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
aPiecek8b0cc152021-05-31 16:40:31 +02007734 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007735 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007736 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007737
7738 /* ( Expr ( ',' Expr )* )? */
Michal Vasko004d3152020-06-11 19:59:22 +02007739 if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) {
aPiecek8b0cc152021-05-31 16:40:31 +02007740 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007741 args = malloc(sizeof *args);
7742 LY_CHECK_ERR_GOTO(!args, LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
7743 arg_count = 1;
7744 args[0] = set_copy(set);
7745 if (!args[0]) {
7746 rc = LY_EMEM;
7747 goto cleanup;
7748 }
7749
Michal Vasko004d3152020-06-11 19:59:22 +02007750 rc = eval_expr_select(exp, tok_idx, 0, args[0], options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007751 LY_CHECK_GOTO(rc, cleanup);
7752 } else {
aPiecek8b0cc152021-05-31 16:40:31 +02007753 rc = eval_expr_select(exp, tok_idx, 0, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007754 LY_CHECK_GOTO(rc, cleanup);
7755 }
7756 }
Michal Vasko004d3152020-06-11 19:59:22 +02007757 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) {
aPiecek8b0cc152021-05-31 16:40:31 +02007758 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007759 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007760 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007761
aPiecek8b0cc152021-05-31 16:40:31 +02007762 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007763 ++arg_count;
7764 args_aux = realloc(args, arg_count * sizeof *args);
7765 LY_CHECK_ERR_GOTO(!args_aux, arg_count--; LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
7766 args = args_aux;
7767 args[arg_count - 1] = set_copy(set);
7768 if (!args[arg_count - 1]) {
7769 rc = LY_EMEM;
7770 goto cleanup;
7771 }
7772
Michal Vasko004d3152020-06-11 19:59:22 +02007773 rc = eval_expr_select(exp, tok_idx, 0, args[arg_count - 1], options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007774 LY_CHECK_GOTO(rc, cleanup);
7775 } else {
aPiecek8b0cc152021-05-31 16:40:31 +02007776 rc = eval_expr_select(exp, tok_idx, 0, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007777 LY_CHECK_GOTO(rc, cleanup);
7778 }
7779 }
7780
7781 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007782 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
aPiecek8b0cc152021-05-31 16:40:31 +02007783 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007784 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007785 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007786
aPiecek8b0cc152021-05-31 16:40:31 +02007787 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007788 /* evaluate function */
7789 rc = xpath_func(args, arg_count, set, options);
7790
7791 if (options & LYXP_SCNODE_ALL) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007792 /* merge all nodes from arg evaluations */
7793 for (i = 0; i < arg_count; ++i) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007794 set_scnode_clear_ctx(args[i], LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007795 lyxp_set_scnode_merge(set, args[i]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007796 }
7797 }
7798 } else {
7799 rc = LY_SUCCESS;
7800 }
7801
7802cleanup:
7803 for (i = 0; i < arg_count; ++i) {
7804 lyxp_set_free(args[i]);
7805 }
7806 free(args);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007807 return rc;
7808}
7809
7810/**
7811 * @brief Evaluate Number. Logs directly on error.
7812 *
7813 * @param[in] ctx Context for errors.
7814 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007815 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007816 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7817 * @return LY_ERR
7818 */
7819static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007820eval_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 +02007821{
7822 long double num;
7823 char *endptr;
7824
7825 if (set) {
7826 errno = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02007827 num = strtold(&exp->expr[exp->tok_pos[*tok_idx]], &endptr);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007828 if (errno) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01007829 LOGVAL(ctx, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]);
7830 LOGVAL(ctx, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double (%s).",
Michal Vasko69730152020-10-09 16:30:07 +02007831 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]], strerror(errno));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007832 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02007833 } else if (endptr - &exp->expr[exp->tok_pos[*tok_idx]] != exp->tok_len[*tok_idx]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01007834 LOGVAL(ctx, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]);
7835 LOGVAL(ctx, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double.",
Michal Vasko69730152020-10-09 16:30:07 +02007836 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007837 return LY_EVALID;
7838 }
7839
7840 set_fill_number(set, num);
7841 }
7842
7843 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007844 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007845 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007846 return LY_SUCCESS;
7847}
7848
aPiecekdf23eee2021-10-07 12:21:50 +02007849LY_ERR
7850lyxp_vars_find(struct lyxp_var *vars, const char *name, size_t name_len, struct lyxp_var **var)
7851{
7852 LY_ERR ret = LY_ENOTFOUND;
7853 LY_ARRAY_COUNT_TYPE u;
7854
7855 assert(vars && name);
7856
7857 name_len = name_len ? name_len : strlen(name);
7858
7859 LY_ARRAY_FOR(vars, u) {
7860 if (!strncmp(vars[u].name, name, name_len)) {
7861 ret = LY_SUCCESS;
7862 break;
7863 }
7864 }
7865
7866 if (var && !ret) {
7867 *var = &vars[u];
7868 }
7869
7870 return ret;
7871}
7872
Michal Vasko03ff5a72019-09-11 13:49:33 +02007873/**
aPiecekfba75362021-10-07 12:39:48 +02007874 * @brief Evaluate VariableReference.
7875 *
7876 * @param[in] exp Parsed XPath expression.
7877 * @param[in] tok_idx Position in the expression @p exp.
7878 * @param[in] vars [Sized array](@ref sizedarrays) of XPath variables.
7879 * @param[in,out] set Context and result set.
7880 * @param[in] options XPath options.
7881 * @return LY_ERR value.
7882 */
7883static LY_ERR
7884eval_variable_reference(const struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set, uint32_t options)
7885{
7886 LY_ERR ret;
7887 const char *name;
7888 struct lyxp_var *var;
7889 const struct lyxp_var *vars;
7890 struct lyxp_expr *tokens = NULL;
7891 uint16_t token_index;
7892
7893 vars = set->vars;
7894
7895 /* Find out the name and value of the variable. */
7896 name = &exp->expr[exp->tok_pos[*tok_idx]];
7897 ret = lyxp_vars_find((struct lyxp_var *)vars, name, exp->tok_len[*tok_idx], &var);
7898 LY_CHECK_ERR_RET(ret, LOGERR(set->ctx, ret,
7899 "XPath variable \"%s\" not defined.", name), ret);
7900
7901 /* Parse value. */
7902 ret = lyxp_expr_parse(set->ctx, var->value, 0, 1, &tokens);
7903 LY_CHECK_GOTO(ret, cleanup);
7904
7905 /* Evaluate value. */
7906 token_index = 0;
7907 ret = eval_expr_select(tokens, &token_index, 0, set, options);
7908 LY_CHECK_GOTO(ret, cleanup);
7909
7910cleanup:
7911 lyxp_expr_free(set->ctx, tokens);
7912
7913 return ret;
7914}
7915
7916/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02007917 * @brief Evaluate PathExpr. Logs directly on error.
7918 *
Michal Vaskod3678892020-05-21 10:06:58 +02007919 * [12] PathExpr ::= LocationPath | PrimaryExpr Predicate*
Michal Vasko03ff5a72019-09-11 13:49:33 +02007920 * | PrimaryExpr Predicate* '/' RelativeLocationPath
7921 * | PrimaryExpr Predicate* '//' RelativeLocationPath
7922 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
aPiecekfba75362021-10-07 12:39:48 +02007923 * [10] PrimaryExpr ::= VariableReference | '(' Expr ')' | Literal | Number | FunctionCall
Michal Vasko03ff5a72019-09-11 13:49:33 +02007924 *
7925 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007926 * @param[in] tok_idx Position in the expression @p exp.
aPiecek8b0cc152021-05-31 16:40:31 +02007927 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007928 * @param[in] options XPath options.
7929 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7930 */
7931static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007932eval_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 +02007933{
Radek Krejci857189e2020-09-01 13:26:36 +02007934 ly_bool all_desc, parent_pos_pred;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007935 LY_ERR rc;
7936
Michal Vasko004d3152020-06-11 19:59:22 +02007937 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007938 case LYXP_TOKEN_PAR1:
7939 /* '(' Expr ')' */
7940
7941 /* '(' */
aPiecek8b0cc152021-05-31 16:40:31 +02007942 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007943 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007944 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007945
7946 /* Expr */
Michal Vasko004d3152020-06-11 19:59:22 +02007947 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007948 LY_CHECK_RET(rc);
7949
7950 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007951 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
aPiecek8b0cc152021-05-31 16:40:31 +02007952 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007953 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007954 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007955
7956 parent_pos_pred = 0;
7957 goto predicate;
7958
7959 case LYXP_TOKEN_DOT:
7960 case LYXP_TOKEN_DDOT:
7961 case LYXP_TOKEN_AT:
7962 case LYXP_TOKEN_NAMETEST:
7963 case LYXP_TOKEN_NODETYPE:
7964 /* RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007965 rc = eval_relative_location_path(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007966 LY_CHECK_RET(rc);
7967 break;
7968
aPiecekfba75362021-10-07 12:39:48 +02007969 case LYXP_TOKEN_VARREF:
7970 /* VariableReference */
7971 rc = eval_variable_reference(exp, tok_idx, set, options);
7972 LY_CHECK_RET(rc);
7973 ++(*tok_idx);
7974
7975 parent_pos_pred = 1;
7976 goto predicate;
7977
Michal Vasko03ff5a72019-09-11 13:49:33 +02007978 case LYXP_TOKEN_FUNCNAME:
7979 /* FunctionCall */
aPiecek8b0cc152021-05-31 16:40:31 +02007980 rc = eval_function_call(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007981 LY_CHECK_RET(rc);
7982
7983 parent_pos_pred = 1;
7984 goto predicate;
7985
Michal Vasko3e48bf32020-06-01 08:39:07 +02007986 case LYXP_TOKEN_OPER_PATH:
7987 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +02007988 /* AbsoluteLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007989 rc = eval_absolute_location_path(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007990 LY_CHECK_RET(rc);
7991 break;
7992
7993 case LYXP_TOKEN_LITERAL:
7994 /* Literal */
aPiecek8b0cc152021-05-31 16:40:31 +02007995 if ((options & LYXP_SKIP_EXPR) || (options & LYXP_SCNODE_ALL)) {
7996 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007997 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007998 }
Michal Vasko004d3152020-06-11 19:59:22 +02007999 eval_literal(exp, tok_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008000 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02008001 eval_literal(exp, tok_idx, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008002 }
8003
8004 parent_pos_pred = 1;
8005 goto predicate;
8006
8007 case LYXP_TOKEN_NUMBER:
8008 /* Number */
aPiecek8b0cc152021-05-31 16:40:31 +02008009 if ((options & LYXP_SKIP_EXPR) || (options & LYXP_SCNODE_ALL)) {
8010 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008011 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008012 }
Michal Vasko004d3152020-06-11 19:59:22 +02008013 rc = eval_number(NULL, exp, tok_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008014 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02008015 rc = eval_number(set->ctx, exp, tok_idx, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008016 }
8017 LY_CHECK_RET(rc);
8018
8019 parent_pos_pred = 1;
8020 goto predicate;
8021
8022 default:
Radek Krejci2efc45b2020-12-22 16:25:44 +01008023 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 +02008024 return LY_EVALID;
8025 }
8026
8027 return LY_SUCCESS;
8028
8029predicate:
8030 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02008031 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
8032 rc = eval_predicate(exp, tok_idx, set, options, parent_pos_pred);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008033 LY_CHECK_RET(rc);
8034 }
8035
8036 /* ('/' or '//') RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02008037 if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008038
8039 /* evaluate '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02008040 if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008041 all_desc = 0;
8042 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008043 all_desc = 1;
8044 }
8045
aPiecek8b0cc152021-05-31 16:40:31 +02008046 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02008047 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008048 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008049
Michal Vasko004d3152020-06-11 19:59:22 +02008050 rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008051 LY_CHECK_RET(rc);
8052 }
8053
8054 return LY_SUCCESS;
8055}
8056
8057/**
8058 * @brief Evaluate UnionExpr. Logs directly on error.
8059 *
Michal Vaskod3678892020-05-21 10:06:58 +02008060 * [20] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008061 *
8062 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008063 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008064 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008065 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008066 * @param[in] options XPath options.
8067 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8068 */
8069static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008070eval_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 +02008071{
8072 LY_ERR rc = LY_SUCCESS;
8073 struct lyxp_set orig_set, set2;
8074 uint16_t i;
8075
8076 assert(repeat);
8077
8078 set_init(&orig_set, set);
8079 set_init(&set2, set);
8080
8081 set_fill_set(&orig_set, set);
8082
Michal Vasko004d3152020-06-11 19:59:22 +02008083 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008084 LY_CHECK_GOTO(rc, cleanup);
8085
8086 /* ('|' PathExpr)* */
8087 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008088 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_UNI);
aPiecek8b0cc152021-05-31 16:40:31 +02008089 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02008090 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008091 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008092
aPiecek8b0cc152021-05-31 16:40:31 +02008093 if (options & LYXP_SKIP_EXPR) {
8094 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008095 LY_CHECK_GOTO(rc, cleanup);
8096 continue;
8097 }
8098
8099 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008100 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008101 LY_CHECK_GOTO(rc, cleanup);
8102
8103 /* eval */
8104 if (options & LYXP_SCNODE_ALL) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01008105 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008106 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008107 rc = moveto_union(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008108 LY_CHECK_GOTO(rc, cleanup);
8109 }
8110 }
8111
8112cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008113 lyxp_set_free_content(&orig_set);
8114 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008115 return rc;
8116}
8117
8118/**
8119 * @brief Evaluate UnaryExpr. Logs directly on error.
8120 *
Michal Vaskod3678892020-05-21 10:06:58 +02008121 * [19] UnaryExpr ::= UnionExpr | '-' UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008122 *
8123 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008124 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008125 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008126 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008127 * @param[in] options XPath options.
8128 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8129 */
8130static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008131eval_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 +02008132{
8133 LY_ERR rc;
8134 uint16_t this_op, i;
8135
8136 assert(repeat);
8137
8138 /* ('-')+ */
Michal Vasko004d3152020-06-11 19:59:22 +02008139 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008140 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008141 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 +02008142
aPiecek8b0cc152021-05-31 16:40:31 +02008143 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02008144 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008145 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008146 }
8147
Michal Vasko004d3152020-06-11 19:59:22 +02008148 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNARY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008149 LY_CHECK_RET(rc);
8150
aPiecek8b0cc152021-05-31 16:40:31 +02008151 if (!(options & LYXP_SKIP_EXPR) && (repeat % 2)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008152 if (options & LYXP_SCNODE_ALL) {
8153 warn_operands(set->ctx, set, NULL, 1, exp->expr, exp->tok_pos[this_op]);
8154 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008155 rc = moveto_op_math(set, NULL, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008156 LY_CHECK_RET(rc);
8157 }
8158 }
8159
8160 return LY_SUCCESS;
8161}
8162
8163/**
8164 * @brief Evaluate MultiplicativeExpr. Logs directly on error.
8165 *
Michal Vaskod3678892020-05-21 10:06:58 +02008166 * [18] MultiplicativeExpr ::= UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008167 * | MultiplicativeExpr '*' UnaryExpr
8168 * | MultiplicativeExpr 'div' UnaryExpr
8169 * | MultiplicativeExpr 'mod' UnaryExpr
8170 *
8171 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008172 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008173 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008174 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008175 * @param[in] options XPath options.
8176 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8177 */
8178static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008179eval_multiplicative_expr(const struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set,
8180 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008181{
8182 LY_ERR rc;
8183 uint16_t this_op;
8184 struct lyxp_set orig_set, set2;
8185 uint16_t i;
8186
8187 assert(repeat);
8188
8189 set_init(&orig_set, set);
8190 set_init(&set2, set);
8191
8192 set_fill_set(&orig_set, set);
8193
Michal Vasko004d3152020-06-11 19:59:22 +02008194 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008195 LY_CHECK_GOTO(rc, cleanup);
8196
8197 /* ('*' / 'div' / 'mod' UnaryExpr)* */
8198 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008199 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008200
Michal Vasko004d3152020-06-11 19:59:22 +02008201 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH);
aPiecek8b0cc152021-05-31 16:40:31 +02008202 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02008203 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008204 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008205
aPiecek8b0cc152021-05-31 16:40:31 +02008206 if (options & LYXP_SKIP_EXPR) {
8207 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008208 LY_CHECK_GOTO(rc, cleanup);
8209 continue;
8210 }
8211
8212 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008213 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008214 LY_CHECK_GOTO(rc, cleanup);
8215
8216 /* eval */
8217 if (options & LYXP_SCNODE_ALL) {
8218 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008219 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02008220 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008221 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008222 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008223 LY_CHECK_GOTO(rc, cleanup);
8224 }
8225 }
8226
8227cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008228 lyxp_set_free_content(&orig_set);
8229 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008230 return rc;
8231}
8232
8233/**
8234 * @brief Evaluate AdditiveExpr. Logs directly on error.
8235 *
Michal Vaskod3678892020-05-21 10:06:58 +02008236 * [17] AdditiveExpr ::= MultiplicativeExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008237 * | AdditiveExpr '+' MultiplicativeExpr
8238 * | AdditiveExpr '-' MultiplicativeExpr
8239 *
8240 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008241 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008242 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008243 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008244 * @param[in] options XPath options.
8245 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8246 */
8247static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008248eval_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 +02008249{
8250 LY_ERR rc;
8251 uint16_t this_op;
8252 struct lyxp_set orig_set, set2;
8253 uint16_t i;
8254
8255 assert(repeat);
8256
8257 set_init(&orig_set, set);
8258 set_init(&set2, set);
8259
8260 set_fill_set(&orig_set, set);
8261
Michal Vasko004d3152020-06-11 19:59:22 +02008262 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008263 LY_CHECK_GOTO(rc, cleanup);
8264
8265 /* ('+' / '-' MultiplicativeExpr)* */
8266 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008267 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008268
Michal Vasko004d3152020-06-11 19:59:22 +02008269 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008270 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02008271 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008272 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008273
aPiecek8b0cc152021-05-31 16:40:31 +02008274 if (options & LYXP_SKIP_EXPR) {
8275 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008276 LY_CHECK_GOTO(rc, cleanup);
8277 continue;
8278 }
8279
8280 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008281 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008282 LY_CHECK_GOTO(rc, cleanup);
8283
8284 /* eval */
8285 if (options & LYXP_SCNODE_ALL) {
8286 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008287 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02008288 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008289 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008290 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008291 LY_CHECK_GOTO(rc, cleanup);
8292 }
8293 }
8294
8295cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008296 lyxp_set_free_content(&orig_set);
8297 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008298 return rc;
8299}
8300
8301/**
8302 * @brief Evaluate RelationalExpr. Logs directly on error.
8303 *
Michal Vaskod3678892020-05-21 10:06:58 +02008304 * [16] RelationalExpr ::= AdditiveExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008305 * | RelationalExpr '<' AdditiveExpr
8306 * | RelationalExpr '>' AdditiveExpr
8307 * | RelationalExpr '<=' AdditiveExpr
8308 * | RelationalExpr '>=' AdditiveExpr
8309 *
8310 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008311 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008312 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008313 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008314 * @param[in] options XPath options.
8315 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8316 */
8317static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008318eval_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 +02008319{
8320 LY_ERR rc;
8321 uint16_t this_op;
8322 struct lyxp_set orig_set, set2;
8323 uint16_t i;
8324
8325 assert(repeat);
8326
8327 set_init(&orig_set, set);
8328 set_init(&set2, set);
8329
8330 set_fill_set(&orig_set, set);
8331
Michal Vasko004d3152020-06-11 19:59:22 +02008332 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008333 LY_CHECK_GOTO(rc, cleanup);
8334
8335 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
8336 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008337 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008338
Michal Vasko004d3152020-06-11 19:59:22 +02008339 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_COMP);
aPiecek8b0cc152021-05-31 16:40:31 +02008340 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02008341 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008342 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008343
aPiecek8b0cc152021-05-31 16:40:31 +02008344 if (options & LYXP_SKIP_EXPR) {
8345 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008346 LY_CHECK_GOTO(rc, cleanup);
8347 continue;
8348 }
8349
8350 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008351 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008352 LY_CHECK_GOTO(rc, cleanup);
8353
8354 /* eval */
8355 if (options & LYXP_SCNODE_ALL) {
8356 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008357 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02008358 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008359 } else {
8360 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options);
8361 LY_CHECK_GOTO(rc, cleanup);
8362 }
8363 }
8364
8365cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008366 lyxp_set_free_content(&orig_set);
8367 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008368 return rc;
8369}
8370
8371/**
8372 * @brief Evaluate EqualityExpr. Logs directly on error.
8373 *
Michal Vaskod3678892020-05-21 10:06:58 +02008374 * [15] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008375 * | EqualityExpr '!=' RelationalExpr
8376 *
8377 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008378 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008379 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008380 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008381 * @param[in] options XPath options.
8382 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8383 */
8384static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008385eval_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 +02008386{
8387 LY_ERR rc;
8388 uint16_t this_op;
8389 struct lyxp_set orig_set, set2;
8390 uint16_t i;
8391
8392 assert(repeat);
8393
8394 set_init(&orig_set, set);
8395 set_init(&set2, set);
8396
8397 set_fill_set(&orig_set, set);
8398
Michal Vasko004d3152020-06-11 19:59:22 +02008399 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008400 LY_CHECK_GOTO(rc, cleanup);
8401
8402 /* ('=' / '!=' RelationalExpr)* */
8403 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008404 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008405
Michal Vasko004d3152020-06-11 19:59:22 +02008406 assert((exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_EQUAL) || (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_NEQUAL));
aPiecek8b0cc152021-05-31 16:40:31 +02008407 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02008408 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008409 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008410
aPiecek8b0cc152021-05-31 16:40:31 +02008411 if (options & LYXP_SKIP_EXPR) {
8412 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008413 LY_CHECK_GOTO(rc, cleanup);
8414 continue;
8415 }
8416
8417 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008418 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008419 LY_CHECK_GOTO(rc, cleanup);
8420
8421 /* eval */
8422 if (options & LYXP_SCNODE_ALL) {
8423 warn_operands(set->ctx, set, &set2, 0, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vasko004d3152020-06-11 19:59:22 +02008424 warn_equality_value(exp, set, *tok_idx - 1, this_op - 1, *tok_idx - 1);
8425 warn_equality_value(exp, &set2, this_op - 1, this_op - 1, *tok_idx - 1);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008426 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02008427 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008428 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008429 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options);
8430 LY_CHECK_GOTO(rc, cleanup);
8431 }
8432 }
8433
8434cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008435 lyxp_set_free_content(&orig_set);
8436 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008437 return rc;
8438}
8439
8440/**
8441 * @brief Evaluate AndExpr. Logs directly on error.
8442 *
Michal Vaskod3678892020-05-21 10:06:58 +02008443 * [14] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008444 *
8445 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008446 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008447 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008448 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008449 * @param[in] options XPath options.
8450 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8451 */
8452static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008453eval_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 +02008454{
8455 LY_ERR rc;
8456 struct lyxp_set orig_set, set2;
8457 uint16_t i;
8458
8459 assert(repeat);
8460
8461 set_init(&orig_set, set);
8462 set_init(&set2, set);
8463
8464 set_fill_set(&orig_set, set);
8465
Michal Vasko004d3152020-06-11 19:59:22 +02008466 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008467 LY_CHECK_GOTO(rc, cleanup);
8468
8469 /* cast to boolean, we know that will be the final result */
aPiecek8b0cc152021-05-31 16:40:31 +02008470 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008471 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008472 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008473 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008474 }
8475
8476 /* ('and' EqualityExpr)* */
8477 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008478 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG);
aPiecek8b0cc152021-05-31 16:40:31 +02008479 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, ((options & LYXP_SKIP_EXPR) || !set->val.bln ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02008480 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008481 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008482
8483 /* lazy evaluation */
aPiecek8b0cc152021-05-31 16:40:31 +02008484 if ((options & LYXP_SKIP_EXPR) || ((set->type == LYXP_SET_BOOLEAN) && !set->val.bln)) {
8485 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008486 LY_CHECK_GOTO(rc, cleanup);
8487 continue;
8488 }
8489
8490 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008491 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008492 LY_CHECK_GOTO(rc, cleanup);
8493
8494 /* eval - just get boolean value actually */
8495 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008496 set_scnode_clear_ctx(&set2, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008497 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008498 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008499 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008500 set_fill_set(set, &set2);
8501 }
8502 }
8503
8504cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008505 lyxp_set_free_content(&orig_set);
8506 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008507 return rc;
8508}
8509
8510/**
8511 * @brief Evaluate OrExpr. Logs directly on error.
8512 *
Michal Vaskod3678892020-05-21 10:06:58 +02008513 * [13] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008514 *
8515 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008516 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008517 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008518 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008519 * @param[in] options XPath options.
8520 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8521 */
8522static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008523eval_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 +02008524{
8525 LY_ERR rc;
8526 struct lyxp_set orig_set, set2;
8527 uint16_t i;
8528
8529 assert(repeat);
8530
8531 set_init(&orig_set, set);
8532 set_init(&set2, set);
8533
8534 set_fill_set(&orig_set, set);
8535
Michal Vasko004d3152020-06-11 19:59:22 +02008536 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008537 LY_CHECK_GOTO(rc, cleanup);
8538
8539 /* cast to boolean, we know that will be the final result */
aPiecek8b0cc152021-05-31 16:40:31 +02008540 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008541 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008542 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008543 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008544 }
8545
8546 /* ('or' AndExpr)* */
8547 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008548 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG);
aPiecek8b0cc152021-05-31 16:40:31 +02008549 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, ((options & LYXP_SKIP_EXPR) || set->val.bln ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02008550 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008551 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008552
8553 /* lazy evaluation */
aPiecek8b0cc152021-05-31 16:40:31 +02008554 if ((options & LYXP_SKIP_EXPR) || ((set->type == LYXP_SET_BOOLEAN) && set->val.bln)) {
8555 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008556 LY_CHECK_GOTO(rc, cleanup);
8557 continue;
8558 }
8559
8560 set_fill_set(&set2, &orig_set);
8561 /* expr_type cound have been LYXP_EXPR_NONE in all these later calls (except for the first one),
8562 * but it does not matter */
Michal Vasko004d3152020-06-11 19:59:22 +02008563 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008564 LY_CHECK_GOTO(rc, cleanup);
8565
8566 /* eval - just get boolean value actually */
8567 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008568 set_scnode_clear_ctx(&set2, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008569 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008570 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008571 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008572 set_fill_set(set, &set2);
8573 }
8574 }
8575
8576cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008577 lyxp_set_free_content(&orig_set);
8578 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008579 return rc;
8580}
8581
8582/**
Michal Vasko004d3152020-06-11 19:59:22 +02008583 * @brief Decide what expression is at the pointer @p tok_idx and evaluate it accordingly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008584 *
8585 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008586 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008587 * @param[in] etype Expression type to evaluate.
aPiecek8b0cc152021-05-31 16:40:31 +02008588 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008589 * @param[in] options XPath options.
8590 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8591 */
8592static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008593eval_expr_select(const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_expr_type etype, struct lyxp_set *set,
8594 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008595{
8596 uint16_t i, count;
8597 enum lyxp_expr_type next_etype;
8598 LY_ERR rc;
8599
8600 /* process operator repeats */
Michal Vasko004d3152020-06-11 19:59:22 +02008601 if (!exp->repeat[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008602 next_etype = LYXP_EXPR_NONE;
8603 } else {
8604 /* find etype repeat */
Radek Krejci1e008d22020-08-17 11:37:37 +02008605 for (i = 0; exp->repeat[*tok_idx][i] > etype; ++i) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02008606
8607 /* select one-priority lower because etype expression called us */
8608 if (i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008609 next_etype = exp->repeat[*tok_idx][i - 1];
Michal Vasko03ff5a72019-09-11 13:49:33 +02008610 /* count repeats for that expression */
Radek Krejci1e008d22020-08-17 11:37:37 +02008611 for (count = 0; i && exp->repeat[*tok_idx][i - 1] == next_etype; ++count, --i) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02008612 } else {
8613 next_etype = LYXP_EXPR_NONE;
8614 }
8615 }
8616
8617 /* decide what expression are we parsing based on the repeat */
8618 switch (next_etype) {
8619 case LYXP_EXPR_OR:
Michal Vasko004d3152020-06-11 19:59:22 +02008620 rc = eval_or_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008621 break;
8622 case LYXP_EXPR_AND:
Michal Vasko004d3152020-06-11 19:59:22 +02008623 rc = eval_and_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008624 break;
8625 case LYXP_EXPR_EQUALITY:
Michal Vasko004d3152020-06-11 19:59:22 +02008626 rc = eval_equality_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008627 break;
8628 case LYXP_EXPR_RELATIONAL:
Michal Vasko004d3152020-06-11 19:59:22 +02008629 rc = eval_relational_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008630 break;
8631 case LYXP_EXPR_ADDITIVE:
Michal Vasko004d3152020-06-11 19:59:22 +02008632 rc = eval_additive_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008633 break;
8634 case LYXP_EXPR_MULTIPLICATIVE:
Michal Vasko004d3152020-06-11 19:59:22 +02008635 rc = eval_multiplicative_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008636 break;
8637 case LYXP_EXPR_UNARY:
Michal Vasko004d3152020-06-11 19:59:22 +02008638 rc = eval_unary_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008639 break;
8640 case LYXP_EXPR_UNION:
Michal Vasko004d3152020-06-11 19:59:22 +02008641 rc = eval_union_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008642 break;
8643 case LYXP_EXPR_NONE:
Michal Vasko004d3152020-06-11 19:59:22 +02008644 rc = eval_path_expr(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008645 break;
8646 default:
8647 LOGINT_RET(set->ctx);
8648 }
8649
8650 return rc;
8651}
8652
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008653/**
8654 * @brief Get root type.
8655 *
8656 * @param[in] ctx_node Context node.
8657 * @param[in] ctx_scnode Schema context node.
8658 * @param[in] options XPath options.
8659 * @return Root type.
8660 */
8661static enum lyxp_node_type
Radek Krejci1deb5be2020-08-26 16:43:36 +02008662lyxp_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 +01008663{
Michal Vasko6b26e742020-07-17 15:02:10 +02008664 const struct lysc_node *op;
8665
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008666 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008667 /* schema */
Radek Krejci1e008d22020-08-17 11:37:37 +02008668 for (op = ctx_scnode; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent) {}
Michal Vasko6b26e742020-07-17 15:02:10 +02008669
8670 if (op || (options & LYXP_SCNODE)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008671 /* general root that can access everything */
8672 return LYXP_NODE_ROOT;
8673 } else if (!ctx_scnode || (ctx_scnode->flags & LYS_CONFIG_W)) {
8674 /* root context node can access only config data (because we said so, it is unspecified) */
8675 return LYXP_NODE_ROOT_CONFIG;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008676 }
Michal Vasko6b26e742020-07-17 15:02:10 +02008677 return LYXP_NODE_ROOT;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008678 }
8679
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008680 /* data */
Michal Vasko6b26e742020-07-17 15:02:10 +02008681 op = ctx_node ? ctx_node->schema : NULL;
Michal Vaskod989ba02020-08-24 10:59:24 +02008682 for ( ; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent) {}
Michal Vasko6b26e742020-07-17 15:02:10 +02008683
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008684 if (op || !(options & LYXP_SCHEMA)) {
8685 /* general root that can access everything */
8686 return LYXP_NODE_ROOT;
Christian Hoppsb6ecaea2021-02-06 09:45:38 -05008687 } else if (!ctx_node || !ctx_node->schema || (ctx_node->schema->flags & LYS_CONFIG_W)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008688 /* root context node can access only config data (because we said so, it is unspecified) */
8689 return LYXP_NODE_ROOT_CONFIG;
8690 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008691 return LYXP_NODE_ROOT;
8692}
8693
Michal Vasko03ff5a72019-09-11 13:49:33 +02008694LY_ERR
Michal Vasko400e9672021-01-11 13:39:17 +01008695lyxp_eval(const struct ly_ctx *ctx, const struct lyxp_expr *exp, const struct lys_module *cur_mod,
Radek Krejci8df109d2021-04-23 12:19:08 +02008696 LY_VALUE_FORMAT format, void *prefix_data, const struct lyd_node *ctx_node, const struct lyd_node *tree,
aPiecekfba75362021-10-07 12:39:48 +02008697 const struct lyxp_var *vars, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008698{
Michal Vasko004d3152020-06-11 19:59:22 +02008699 uint16_t tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008700 LY_ERR rc;
8701
Michal Vasko400e9672021-01-11 13:39:17 +01008702 LY_CHECK_ARG_RET(ctx, ctx, exp, set, LY_EINVAL);
Radek Krejci8df109d2021-04-23 12:19:08 +02008703 if (!cur_mod && ((format == LY_VALUE_SCHEMA) || (format == LY_VALUE_SCHEMA_RESOLVED))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008704 LOGARG(NULL, "Current module must be set if schema format is used.");
8705 return LY_EINVAL;
Michal Vasko004d3152020-06-11 19:59:22 +02008706 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008707
Michal Vaskod3bb12f2020-12-04 14:33:09 +01008708 if (tree) {
8709 /* adjust the pointer to be the first top-level sibling */
8710 while (tree->parent) {
8711 tree = lyd_parent(tree);
8712 }
8713 tree = lyd_first_sibling(tree);
8714 }
8715
Michal Vasko03ff5a72019-09-11 13:49:33 +02008716 /* prepare set for evaluation */
Michal Vasko03ff5a72019-09-11 13:49:33 +02008717 memset(set, 0, sizeof *set);
Michal Vaskod3678892020-05-21 10:06:58 +02008718 set->type = LYXP_SET_NODE_SET;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008719 set->root_type = lyxp_get_root_type(ctx_node, NULL, options);
8720 set_insert_node(set, (struct lyd_node *)ctx_node, 0, ctx_node ? LYXP_NODE_ELEM : set->root_type, 0);
8721
Michal Vasko400e9672021-01-11 13:39:17 +01008722 set->ctx = (struct ly_ctx *)ctx;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008723 set->cur_node = ctx_node;
8724 for (set->context_op = ctx_node ? ctx_node->schema : NULL;
Radek Krejci0f969882020-08-21 16:56:47 +02008725 set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF));
8726 set->context_op = set->context_op->parent) {}
Michal Vaskof03ed032020-03-04 13:31:44 +01008727 set->tree = tree;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008728 set->cur_mod = cur_mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008729 set->format = format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008730 set->prefix_data = prefix_data;
aPiecekfba75362021-10-07 12:39:48 +02008731 set->vars = vars;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008732
Radek Krejciddace2c2021-01-08 11:30:56 +01008733 LOG_LOCSET(NULL, set->cur_node, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01008734
Michal Vasko03ff5a72019-09-11 13:49:33 +02008735 /* evaluate */
Michal Vasko004d3152020-06-11 19:59:22 +02008736 rc = eval_expr_select(exp, &tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008737 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02008738 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008739 }
8740
Radek Krejciddace2c2021-01-08 11:30:56 +01008741 LOG_LOCBACK(0, 1, 0, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008742 return rc;
8743}
8744
8745#if 0
8746
8747/* full xml printing of set elements, not used currently */
8748
8749void
8750lyxp_set_print_xml(FILE *f, struct lyxp_set *set)
8751{
8752 uint32_t i;
8753 char *str_num;
8754 struct lyout out;
8755
8756 memset(&out, 0, sizeof out);
8757
8758 out.type = LYOUT_STREAM;
8759 out.method.f = f;
8760
8761 switch (set->type) {
8762 case LYXP_SET_EMPTY:
Michal Vasko5233e962020-08-14 14:26:20 +02008763 ly_print_(&out, "Empty XPath set\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008764 break;
8765 case LYXP_SET_BOOLEAN:
Michal Vasko5233e962020-08-14 14:26:20 +02008766 ly_print_(&out, "Boolean XPath set:\n");
8767 ly_print_(&out, "%s\n\n", set->value.bool ? "true" : "false");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008768 break;
8769 case LYXP_SET_STRING:
Michal Vasko5233e962020-08-14 14:26:20 +02008770 ly_print_(&out, "String XPath set:\n");
8771 ly_print_(&out, "\"%s\"\n\n", set->value.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008772 break;
8773 case LYXP_SET_NUMBER:
Michal Vasko5233e962020-08-14 14:26:20 +02008774 ly_print_(&out, "Number XPath set:\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008775
8776 if (isnan(set->value.num)) {
8777 str_num = strdup("NaN");
8778 } else if ((set->value.num == 0) || (set->value.num == -0.0f)) {
8779 str_num = strdup("0");
8780 } else if (isinf(set->value.num) && !signbit(set->value.num)) {
8781 str_num = strdup("Infinity");
8782 } else if (isinf(set->value.num) && signbit(set->value.num)) {
8783 str_num = strdup("-Infinity");
8784 } else if ((long long)set->value.num == set->value.num) {
8785 if (asprintf(&str_num, "%lld", (long long)set->value.num) == -1) {
8786 str_num = NULL;
8787 }
8788 } else {
8789 if (asprintf(&str_num, "%03.1Lf", set->value.num) == -1) {
8790 str_num = NULL;
8791 }
8792 }
8793 if (!str_num) {
8794 LOGMEM;
8795 return;
8796 }
Michal Vasko5233e962020-08-14 14:26:20 +02008797 ly_print_(&out, "%s\n\n", str_num);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008798 free(str_num);
8799 break;
8800 case LYXP_SET_NODE_SET:
Michal Vasko5233e962020-08-14 14:26:20 +02008801 ly_print_(&out, "Node XPath set:\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008802
8803 for (i = 0; i < set->used; ++i) {
Michal Vasko5233e962020-08-14 14:26:20 +02008804 ly_print_(&out, "%d. ", i + 1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008805 switch (set->node_type[i]) {
8806 case LYXP_NODE_ROOT_ALL:
Michal Vasko5233e962020-08-14 14:26:20 +02008807 ly_print_(&out, "ROOT all\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008808 break;
8809 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko5233e962020-08-14 14:26:20 +02008810 ly_print_(&out, "ROOT config\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008811 break;
8812 case LYXP_NODE_ROOT_STATE:
Michal Vasko5233e962020-08-14 14:26:20 +02008813 ly_print_(&out, "ROOT state\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008814 break;
8815 case LYXP_NODE_ROOT_NOTIF:
Michal Vasko5233e962020-08-14 14:26:20 +02008816 ly_print_(&out, "ROOT notification \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008817 break;
8818 case LYXP_NODE_ROOT_RPC:
Michal Vasko5233e962020-08-14 14:26:20 +02008819 ly_print_(&out, "ROOT rpc \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008820 break;
8821 case LYXP_NODE_ROOT_OUTPUT:
Michal Vasko5233e962020-08-14 14:26:20 +02008822 ly_print_(&out, "ROOT output \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008823 break;
8824 case LYXP_NODE_ELEM:
Michal Vasko5233e962020-08-14 14:26:20 +02008825 ly_print_(&out, "ELEM \"%s\"\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008826 xml_print_node(&out, 1, set->value.nodes[i], 1, LYP_FORMAT);
Michal Vasko5233e962020-08-14 14:26:20 +02008827 ly_print_(&out, "\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008828 break;
8829 case LYXP_NODE_TEXT:
Michal Vasko5233e962020-08-14 14:26:20 +02008830 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 +02008831 break;
8832 case LYXP_NODE_ATTR:
Michal Vasko5233e962020-08-14 14:26:20 +02008833 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 +02008834 break;
8835 }
8836 }
8837 break;
8838 }
8839}
8840
8841#endif
8842
8843LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008844lyxp_set_cast(struct lyxp_set *set, enum lyxp_set_type target)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008845{
8846 long double num;
8847 char *str;
8848 LY_ERR rc;
8849
8850 if (!set || (set->type == target)) {
8851 return LY_SUCCESS;
8852 }
8853
8854 /* it's not possible to convert anything into a node set */
Michal Vaskod3678892020-05-21 10:06:58 +02008855 assert(target != LYXP_SET_NODE_SET);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008856
8857 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vaskod3678892020-05-21 10:06:58 +02008858 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008859 return LY_EINVAL;
8860 }
8861
8862 /* to STRING */
Michal Vaskod3678892020-05-21 10:06:58 +02008863 if ((target == LYXP_SET_STRING) || ((target == LYXP_SET_NUMBER) && (set->type == LYXP_SET_NODE_SET))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008864 switch (set->type) {
8865 case LYXP_SET_NUMBER:
8866 if (isnan(set->val.num)) {
8867 set->val.str = strdup("NaN");
8868 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8869 } else if ((set->val.num == 0) || (set->val.num == -0.0f)) {
8870 set->val.str = strdup("0");
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 (isinf(set->val.num) && signbit(set->val.num)) {
8876 set->val.str = strdup("-Infinity");
8877 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8878 } else if ((long long)set->val.num == set->val.num) {
8879 if (asprintf(&str, "%lld", (long long)set->val.num) == -1) {
8880 LOGMEM_RET(set->ctx);
8881 }
8882 set->val.str = str;
8883 } else {
8884 if (asprintf(&str, "%03.1Lf", set->val.num) == -1) {
8885 LOGMEM_RET(set->ctx);
8886 }
8887 set->val.str = str;
8888 }
8889 break;
8890 case LYXP_SET_BOOLEAN:
Michal Vasko004d3152020-06-11 19:59:22 +02008891 if (set->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008892 set->val.str = strdup("true");
8893 } else {
8894 set->val.str = strdup("false");
8895 }
8896 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), LY_EMEM);
8897 break;
8898 case LYXP_SET_NODE_SET:
Michal Vasko03ff5a72019-09-11 13:49:33 +02008899 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008900 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008901
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008902 rc = cast_node_set_to_string(set, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008903 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02008904 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008905 set->val.str = str;
8906 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008907 default:
8908 LOGINT_RET(set->ctx);
8909 }
8910 set->type = LYXP_SET_STRING;
8911 }
8912
8913 /* to NUMBER */
8914 if (target == LYXP_SET_NUMBER) {
8915 switch (set->type) {
8916 case LYXP_SET_STRING:
8917 num = cast_string_to_number(set->val.str);
Michal Vaskod3678892020-05-21 10:06:58 +02008918 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008919 set->val.num = num;
8920 break;
8921 case LYXP_SET_BOOLEAN:
Michal Vasko004d3152020-06-11 19:59:22 +02008922 if (set->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008923 set->val.num = 1;
8924 } else {
8925 set->val.num = 0;
8926 }
8927 break;
8928 default:
8929 LOGINT_RET(set->ctx);
8930 }
8931 set->type = LYXP_SET_NUMBER;
8932 }
8933
8934 /* to BOOLEAN */
8935 if (target == LYXP_SET_BOOLEAN) {
8936 switch (set->type) {
8937 case LYXP_SET_NUMBER:
8938 if ((set->val.num == 0) || (set->val.num == -0.0f) || isnan(set->val.num)) {
Michal Vasko004d3152020-06-11 19:59:22 +02008939 set->val.bln = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008940 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02008941 set->val.bln = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008942 }
8943 break;
8944 case LYXP_SET_STRING:
8945 if (set->val.str[0]) {
Michal Vaskod3678892020-05-21 10:06:58 +02008946 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008947 set->val.bln = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008948 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02008949 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008950 set->val.bln = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008951 }
8952 break;
8953 case LYXP_SET_NODE_SET:
Michal Vaskod3678892020-05-21 10:06:58 +02008954 if (set->used) {
8955 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008956 set->val.bln = 1;
Michal Vaskod3678892020-05-21 10:06:58 +02008957 } else {
8958 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008959 set->val.bln = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02008960 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008961 break;
8962 default:
8963 LOGINT_RET(set->ctx);
8964 }
8965 set->type = LYXP_SET_BOOLEAN;
8966 }
8967
Michal Vasko03ff5a72019-09-11 13:49:33 +02008968 return LY_SUCCESS;
8969}
8970
8971LY_ERR
Michal Vasko400e9672021-01-11 13:39:17 +01008972lyxp_atomize(const struct ly_ctx *ctx, const struct lyxp_expr *exp, const struct lys_module *cur_mod,
Radek Krejci8df109d2021-04-23 12:19:08 +02008973 LY_VALUE_FORMAT format, void *prefix_data, const struct lysc_node *ctx_scnode, struct lyxp_set *set,
Michal Vasko400e9672021-01-11 13:39:17 +01008974 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008975{
Radek Krejci2efc45b2020-12-22 16:25:44 +01008976 LY_ERR ret;
Michal Vasko004d3152020-06-11 19:59:22 +02008977 uint16_t tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008978
Michal Vasko400e9672021-01-11 13:39:17 +01008979 LY_CHECK_ARG_RET(ctx, ctx, exp, set, LY_EINVAL);
Radek Krejci8df109d2021-04-23 12:19:08 +02008980 if (!cur_mod && ((format == LY_VALUE_SCHEMA) || (format == LY_VALUE_SCHEMA_RESOLVED))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008981 LOGARG(NULL, "Current module must be set if schema format is used.");
8982 return LY_EINVAL;
Michal Vasko004d3152020-06-11 19:59:22 +02008983 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008984
8985 /* prepare set for evaluation */
Michal Vasko03ff5a72019-09-11 13:49:33 +02008986 memset(set, 0, sizeof *set);
8987 set->type = LYXP_SET_SCNODE_SET;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008988 set->root_type = lyxp_get_root_type(NULL, ctx_scnode, options);
8989 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 +01008990 set->val.scnodes[0].in_ctx = LYXP_SET_SCNODE_START;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008991
Michal Vasko400e9672021-01-11 13:39:17 +01008992 set->ctx = (struct ly_ctx *)ctx;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008993 set->cur_scnode = ctx_scnode;
Michal Vasko6b26e742020-07-17 15:02:10 +02008994 for (set->context_op = ctx_scnode;
Radek Krejci0f969882020-08-21 16:56:47 +02008995 set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF));
8996 set->context_op = set->context_op->parent) {}
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008997 set->cur_mod = cur_mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008998 set->format = format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008999 set->prefix_data = prefix_data;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009000
Radek Krejciddace2c2021-01-08 11:30:56 +01009001 LOG_LOCSET(set->cur_scnode, NULL, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01009002
Michal Vasko03ff5a72019-09-11 13:49:33 +02009003 /* evaluate */
Radek Krejci2efc45b2020-12-22 16:25:44 +01009004 ret = eval_expr_select(exp, &tok_idx, 0, set, options);
9005
Radek Krejciddace2c2021-01-08 11:30:56 +01009006 LOG_LOCBACK(1, 0, 0, 0);
Radek Krejci2efc45b2020-12-22 16:25:44 +01009007 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009008}
Michal Vaskod43d71a2020-08-07 14:54:58 +02009009
9010API const char *
9011lyxp_get_expr(const struct lyxp_expr *path)
9012{
9013 if (!path) {
9014 return NULL;
9015 }
9016
9017 return path->expr;
9018}