blob: 8681a85e52f9b4316fd846266f47f994e69da7ce [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
Michal Vaskoddd76592022-01-17 13:34:48 +0100346cast_string_recursive(const struct lyd_node *node, ly_bool fake_cont, enum lyxp_node_type root_type, uint16_t indent,
347 char **str, 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 Vaskoddd76592022-01-17 13:34:48 +0100418 if (lyd_parse_data_mem((struct ly_ctx *)LYD_CTX(node), any->value.mem, LYD_LYB,
419 LYD_PARSE_ONLY | LYD_PARSE_STRICT, 0, &tree) == LY_SUCCESS) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200420 /* successfully parsed */
421 free(any->value.mem);
422 any->value.tree = tree;
423 any->value_type = LYD_ANYDATA_DATATREE;
424 }
Radek Krejci7931b192020-06-25 17:05:03 +0200425 /* error is covered by the following switch where LYD_ANYDATA_LYB causes failure */
Michal Vasko60ea6352020-06-29 13:39:39 +0200426 }
427
Michal Vasko03ff5a72019-09-11 13:49:33 +0200428 switch (any->value_type) {
429 case LYD_ANYDATA_STRING:
430 case LYD_ANYDATA_XML:
431 case LYD_ANYDATA_JSON:
432 buf = strdup(any->value.json);
Michal Vaskob7be7a82020-08-20 09:09:04 +0200433 LY_CHECK_ERR_RET(!buf, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200434 break;
435 case LYD_ANYDATA_DATATREE:
Radek Krejci84ce7b12020-06-11 17:28:25 +0200436 LY_CHECK_RET(ly_out_new_memory(&buf, 0, &out));
Michal Vasko3a41dff2020-07-15 14:30:28 +0200437 rc = lyd_print_all(out, any->value.tree, LYD_XML, 0);
Radek Krejci241f6b52020-05-21 18:13:49 +0200438 ly_out_free(out, NULL, 0);
Radek Krejcia5bba312020-01-09 15:41:20 +0100439 LY_CHECK_RET(rc < 0, -rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200440 break;
Michal Vasko60ea6352020-06-29 13:39:39 +0200441 case LYD_ANYDATA_LYB:
Michal Vaskob7be7a82020-08-20 09:09:04 +0200442 LOGERR(LYD_CTX(node), LY_EINVAL, "Cannot convert LYB anydata into string.");
Michal Vasko60ea6352020-06-29 13:39:39 +0200443 return LY_EINVAL;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200444 }
445 }
446
447 line = strtok_r(buf, "\n", &ptr);
448 do {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200449 rc = cast_string_realloc(LYD_CTX(node), indent * 2 + strlen(line) + 1, str, used, size);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200450 if (rc != LY_SUCCESS) {
451 free(buf);
452 return rc;
453 }
454 memset(*str + (*used - 1), ' ', indent * 2);
455 *used += indent * 2;
456
457 strcpy(*str + (*used - 1), line);
458 *used += strlen(line);
459
460 strcpy(*str + (*used - 1), "\n");
461 *used += 1;
462 } while ((line = strtok_r(NULL, "\n", &ptr)));
463
464 free(buf);
465 break;
466
467 default:
Michal Vaskob7be7a82020-08-20 09:09:04 +0200468 LOGINT_RET(LYD_CTX(node));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200469 }
470
471 if (fake_cont) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200472 rc = cast_string_realloc(LYD_CTX(node), 1, str, used, size);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200473 LY_CHECK_RET(rc);
474 strcpy(*str + (*used - 1), "\n");
475 ++(*used);
476
477 --indent;
478 }
479
480 return LY_SUCCESS;
481}
482
483/**
484 * @brief Cast an element into a string.
485 *
486 * @param[in] node Node to cast.
487 * @param[in] fake_cont Whether to put the data into a "fake" container.
488 * @param[in] root_type Type of the XPath root.
489 * @param[out] str Element cast to dynamically-allocated string.
490 * @return LY_ERR
491 */
492static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200493cast_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 +0200494{
495 uint16_t used, size;
496 LY_ERR rc;
497
498 *str = malloc(LYXP_STRING_CAST_SIZE_START * sizeof(char));
Michal Vaskob7be7a82020-08-20 09:09:04 +0200499 LY_CHECK_ERR_RET(!*str, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200500 (*str)[0] = '\0';
501 used = 1;
502 size = LYXP_STRING_CAST_SIZE_START;
503
504 rc = cast_string_recursive(node, fake_cont, root_type, 0, str, &used, &size);
505 if (rc != LY_SUCCESS) {
506 free(*str);
507 return rc;
508 }
509
510 if (size > used) {
511 *str = ly_realloc(*str, used * sizeof(char));
Michal Vaskob7be7a82020-08-20 09:09:04 +0200512 LY_CHECK_ERR_RET(!*str, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200513 }
514 return LY_SUCCESS;
515}
516
517/**
518 * @brief Cast a LYXP_SET_NODE_SET set into a string.
519 * Context position aware.
520 *
521 * @param[in] set Set to cast.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200522 * @param[out] str Cast dynamically-allocated string.
523 * @return LY_ERR
524 */
525static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100526cast_node_set_to_string(struct lyxp_set *set, char **str)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200527{
Michal Vaskoaa677062021-03-09 13:52:53 +0100528 if (!set->used) {
529 *str = strdup("");
530 if (!*str) {
531 LOGMEM_RET(set->ctx);
532 }
533 return LY_SUCCESS;
534 }
535
Michal Vasko03ff5a72019-09-11 13:49:33 +0200536 switch (set->val.nodes[0].type) {
Michal Vasko2caefc12019-11-14 16:07:56 +0100537 case LYXP_NODE_NONE:
538 /* invalid */
539 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200540 case LYXP_NODE_ROOT:
541 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100542 return cast_string_elem(set->val.nodes[0].node, 1, set->root_type, str);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200543 case LYXP_NODE_ELEM:
544 case LYXP_NODE_TEXT:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100545 return cast_string_elem(set->val.nodes[0].node, 0, set->root_type, str);
Michal Vasko9f96a052020-03-10 09:41:45 +0100546 case LYXP_NODE_META:
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200547 *str = strdup(lyd_get_meta_value(set->val.meta[0].meta));
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200548 if (!*str) {
549 LOGMEM_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200550 }
551 return LY_SUCCESS;
552 }
553
554 LOGINT_RET(set->ctx);
555}
556
557/**
558 * @brief Cast a string into an XPath number.
559 *
560 * @param[in] str String to use.
561 * @return Cast number.
562 */
563static long double
564cast_string_to_number(const char *str)
565{
566 long double num;
567 char *ptr;
568
569 errno = 0;
570 num = strtold(str, &ptr);
571 if (errno || *ptr) {
572 num = NAN;
573 }
574 return num;
575}
576
577/**
578 * @brief Callback for checking value equality.
579 *
Michal Vasko62524a92021-02-26 10:08:50 +0100580 * Implementation of ::lyht_value_equal_cb.
Radek Krejci857189e2020-09-01 13:26:36 +0200581 *
Michal Vasko03ff5a72019-09-11 13:49:33 +0200582 * @param[in] val1_p First value.
583 * @param[in] val2_p Second value.
584 * @param[in] mod Whether hash table is being modified.
585 * @param[in] cb_data Callback data.
Radek Krejci857189e2020-09-01 13:26:36 +0200586 * @return Boolean value whether values are equal or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200587 */
Radek Krejci857189e2020-09-01 13:26:36 +0200588static ly_bool
589set_values_equal_cb(void *val1_p, void *val2_p, ly_bool UNUSED(mod), void *UNUSED(cb_data))
Michal Vasko03ff5a72019-09-11 13:49:33 +0200590{
591 struct lyxp_set_hash_node *val1, *val2;
592
593 val1 = (struct lyxp_set_hash_node *)val1_p;
594 val2 = (struct lyxp_set_hash_node *)val2_p;
595
596 if ((val1->node == val2->node) && (val1->type == val2->type)) {
597 return 1;
598 }
599
600 return 0;
601}
602
603/**
604 * @brief Insert node and its hash into set.
605 *
606 * @param[in] set et to insert to.
607 * @param[in] node Node with hash.
608 * @param[in] type Node type.
609 */
610static void
611set_insert_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type)
612{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200613 LY_ERR r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200614 uint32_t i, hash;
615 struct lyxp_set_hash_node hnode;
616
617 if (!set->ht && (set->used >= LYD_HT_MIN_ITEMS)) {
618 /* create hash table and add all the nodes */
619 set->ht = lyht_new(1, sizeof(struct lyxp_set_hash_node), set_values_equal_cb, NULL, 1);
620 for (i = 0; i < set->used; ++i) {
621 hnode.node = set->val.nodes[i].node;
622 hnode.type = set->val.nodes[i].type;
623
624 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
625 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
626 hash = dict_hash_multi(hash, NULL, 0);
627
628 r = lyht_insert(set->ht, &hnode, hash, NULL);
629 assert(!r);
630 (void)r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200631
Michal Vasko9d6befd2019-12-11 14:56:56 +0100632 if (hnode.node == node) {
633 /* it was just added, do not add it twice */
634 node = NULL;
635 }
636 }
637 }
638
639 if (set->ht && node) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200640 /* add the new node into hash table */
641 hnode.node = node;
642 hnode.type = type;
643
644 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
645 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
646 hash = dict_hash_multi(hash, NULL, 0);
647
648 r = lyht_insert(set->ht, &hnode, hash, NULL);
649 assert(!r);
650 (void)r;
651 }
652}
653
654/**
655 * @brief Remove node and its hash from set.
656 *
657 * @param[in] set Set to remove from.
658 * @param[in] node Node to remove.
659 * @param[in] type Node type.
660 */
661static void
662set_remove_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type)
663{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200664 LY_ERR r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200665 struct lyxp_set_hash_node hnode;
666 uint32_t hash;
667
668 if (set->ht) {
669 hnode.node = node;
670 hnode.type = type;
671
672 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
673 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
674 hash = dict_hash_multi(hash, NULL, 0);
675
676 r = lyht_remove(set->ht, &hnode, hash);
677 assert(!r);
678 (void)r;
679
680 if (!set->ht->used) {
681 lyht_free(set->ht);
682 set->ht = NULL;
683 }
684 }
685}
686
687/**
688 * @brief Check whether node is in set based on its hash.
689 *
690 * @param[in] set Set to search in.
691 * @param[in] node Node to search for.
692 * @param[in] type Node type.
693 * @param[in] skip_idx Index in @p set to skip.
694 * @return LY_ERR
695 */
696static LY_ERR
697set_dup_node_hash_check(const struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type, int skip_idx)
698{
699 struct lyxp_set_hash_node hnode, *match_p;
700 uint32_t hash;
701
702 hnode.node = node;
703 hnode.type = type;
704
705 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
706 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
707 hash = dict_hash_multi(hash, NULL, 0);
708
709 if (!lyht_find(set->ht, &hnode, hash, (void **)&match_p)) {
710 if ((skip_idx > -1) && (set->val.nodes[skip_idx].node == match_p->node) && (set->val.nodes[skip_idx].type == match_p->type)) {
711 /* we found it on the index that should be skipped, find another */
712 hnode = *match_p;
713 if (lyht_find_next(set->ht, &hnode, hash, (void **)&match_p)) {
714 /* none other found */
715 return LY_SUCCESS;
716 }
717 }
718
719 return LY_EEXIST;
720 }
721
722 /* not found */
723 return LY_SUCCESS;
724}
725
Michal Vaskod3678892020-05-21 10:06:58 +0200726void
727lyxp_set_free_content(struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200728{
729 if (!set) {
730 return;
731 }
732
733 if (set->type == LYXP_SET_NODE_SET) {
734 free(set->val.nodes);
735 lyht_free(set->ht);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200736 } else if (set->type == LYXP_SET_SCNODE_SET) {
737 free(set->val.scnodes);
Michal Vaskod3678892020-05-21 10:06:58 +0200738 lyht_free(set->ht);
739 } else {
740 if (set->type == LYXP_SET_STRING) {
741 free(set->val.str);
742 }
743 set->type = LYXP_SET_NODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200744 }
Michal Vaskod3678892020-05-21 10:06:58 +0200745
746 set->val.nodes = NULL;
747 set->used = 0;
748 set->size = 0;
749 set->ht = NULL;
750 set->ctx_pos = 0;
aPiecek748da732021-06-01 09:56:43 +0200751 set->ctx_size = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200752}
753
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100754/**
755 * @brief Free dynamically-allocated set.
756 *
757 * @param[in] set Set to free.
758 */
759static void
Michal Vasko03ff5a72019-09-11 13:49:33 +0200760lyxp_set_free(struct lyxp_set *set)
761{
762 if (!set) {
763 return;
764 }
765
Michal Vaskod3678892020-05-21 10:06:58 +0200766 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200767 free(set);
768}
769
770/**
771 * @brief Initialize set context.
772 *
773 * @param[in] new Set to initialize.
774 * @param[in] set Arbitrary initialized set.
775 */
776static void
Michal Vasko4c7763f2020-07-27 17:40:37 +0200777set_init(struct lyxp_set *new, const struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200778{
779 memset(new, 0, sizeof *new);
Michal Vasko02a77382019-09-12 11:47:35 +0200780 if (set) {
781 new->ctx = set->ctx;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200782 new->cur_node = set->cur_node;
Michal Vasko588112f2019-12-10 14:51:53 +0100783 new->root_type = set->root_type;
Michal Vasko6b26e742020-07-17 15:02:10 +0200784 new->context_op = set->context_op;
Michal Vaskof03ed032020-03-04 13:31:44 +0100785 new->tree = set->tree;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200786 new->cur_mod = set->cur_mod;
Michal Vasko02a77382019-09-12 11:47:35 +0200787 new->format = set->format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200788 new->prefix_data = set->prefix_data;
aPiecekfba75362021-10-07 12:39:48 +0200789 new->vars = set->vars;
Michal Vasko02a77382019-09-12 11:47:35 +0200790 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200791}
792
793/**
794 * @brief Create a deep copy of a set.
795 *
796 * @param[in] set Set to copy.
797 * @return Copy of @p set.
798 */
799static struct lyxp_set *
800set_copy(struct lyxp_set *set)
801{
802 struct lyxp_set *ret;
Michal Vasko1fdd8fa2021-01-08 09:21:45 +0100803 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200804
805 if (!set) {
806 return NULL;
807 }
808
809 ret = malloc(sizeof *ret);
810 LY_CHECK_ERR_RET(!ret, LOGMEM(set->ctx), NULL);
811 set_init(ret, set);
812
813 if (set->type == LYXP_SET_SCNODE_SET) {
814 ret->type = set->type;
815
816 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100817 if ((set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) ||
818 (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +0200819 uint32_t idx;
820 LY_CHECK_ERR_RET(lyxp_set_scnode_insert_node(ret, set->val.scnodes[i].scnode, set->val.scnodes[i].type, &idx),
821 lyxp_set_free(ret), NULL);
Michal Vasko3f27c522020-01-06 08:37:49 +0100822 /* coverity seems to think scnodes can be NULL */
Radek Krejciaa6b53f2020-08-27 15:19:03 +0200823 if (!ret->val.scnodes) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200824 lyxp_set_free(ret);
825 return NULL;
826 }
Michal Vaskoba716542019-12-16 10:01:58 +0100827 ret->val.scnodes[idx].in_ctx = set->val.scnodes[i].in_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200828 }
829 }
830 } else if (set->type == LYXP_SET_NODE_SET) {
831 ret->type = set->type;
Michal Vasko08e9b112021-06-11 15:41:17 +0200832 if (set->used) {
833 ret->val.nodes = malloc(set->used * sizeof *ret->val.nodes);
834 LY_CHECK_ERR_RET(!ret->val.nodes, LOGMEM(set->ctx); free(ret), NULL);
835 memcpy(ret->val.nodes, set->val.nodes, set->used * sizeof *ret->val.nodes);
836 } else {
837 ret->val.nodes = NULL;
838 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200839
840 ret->used = ret->size = set->used;
841 ret->ctx_pos = set->ctx_pos;
842 ret->ctx_size = set->ctx_size;
Michal Vasko4a04e542021-02-01 08:58:30 +0100843 if (set->ht) {
844 ret->ht = lyht_dup(set->ht);
845 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200846 } else {
Radek Krejci0f969882020-08-21 16:56:47 +0200847 memcpy(ret, set, sizeof *ret);
848 if (set->type == LYXP_SET_STRING) {
849 ret->val.str = strdup(set->val.str);
850 LY_CHECK_ERR_RET(!ret->val.str, LOGMEM(set->ctx); free(ret), NULL);
851 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200852 }
853
854 return ret;
855}
856
857/**
858 * @brief Fill XPath set with a string. Any current data are disposed of.
859 *
860 * @param[in] set Set to fill.
861 * @param[in] string String to fill into \p set.
862 * @param[in] str_len Length of \p string. 0 is a valid value!
863 */
864static void
865set_fill_string(struct lyxp_set *set, const char *string, uint16_t str_len)
866{
Michal Vaskod3678892020-05-21 10:06:58 +0200867 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200868
869 set->type = LYXP_SET_STRING;
870 if ((str_len == 0) && (string[0] != '\0')) {
871 string = "";
872 }
873 set->val.str = strndup(string, str_len);
874}
875
876/**
877 * @brief Fill XPath set with a number. Any current data are disposed of.
878 *
879 * @param[in] set Set to fill.
880 * @param[in] number Number to fill into \p set.
881 */
882static void
883set_fill_number(struct lyxp_set *set, long double number)
884{
Michal Vaskod3678892020-05-21 10:06:58 +0200885 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200886
887 set->type = LYXP_SET_NUMBER;
888 set->val.num = number;
889}
890
891/**
892 * @brief Fill XPath set with a boolean. Any current data are disposed of.
893 *
894 * @param[in] set Set to fill.
895 * @param[in] boolean Boolean to fill into \p set.
896 */
897static void
Radek Krejci857189e2020-09-01 13:26:36 +0200898set_fill_boolean(struct lyxp_set *set, ly_bool boolean)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200899{
Michal Vaskod3678892020-05-21 10:06:58 +0200900 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200901
902 set->type = LYXP_SET_BOOLEAN;
Michal Vasko004d3152020-06-11 19:59:22 +0200903 set->val.bln = boolean;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200904}
905
906/**
907 * @brief Fill XPath set with the value from another set (deep assign).
908 * Any current data are disposed of.
909 *
910 * @param[in] trg Set to fill.
911 * @param[in] src Source set to copy into \p trg.
912 */
913static void
Michal Vasko4c7763f2020-07-27 17:40:37 +0200914set_fill_set(struct lyxp_set *trg, const struct lyxp_set *src)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200915{
916 if (!trg || !src) {
917 return;
918 }
919
920 if (trg->type == LYXP_SET_NODE_SET) {
921 free(trg->val.nodes);
922 } else if (trg->type == LYXP_SET_STRING) {
923 free(trg->val.str);
924 }
925 set_init(trg, src);
926
927 if (src->type == LYXP_SET_SCNODE_SET) {
928 trg->type = LYXP_SET_SCNODE_SET;
929 trg->used = src->used;
930 trg->size = src->used;
931
Michal Vasko08e9b112021-06-11 15:41:17 +0200932 if (trg->size) {
933 trg->val.scnodes = ly_realloc(trg->val.scnodes, trg->size * sizeof *trg->val.scnodes);
934 LY_CHECK_ERR_RET(!trg->val.scnodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), );
935 memcpy(trg->val.scnodes, src->val.scnodes, src->used * sizeof *src->val.scnodes);
936 } else {
937 trg->val.scnodes = NULL;
938 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200939 } else if (src->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +0200940 set_fill_boolean(trg, src->val.bln);
Michal Vasko44f3d2c2020-08-24 09:49:38 +0200941 } else if (src->type == LYXP_SET_NUMBER) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200942 set_fill_number(trg, src->val.num);
943 } else if (src->type == LYXP_SET_STRING) {
944 set_fill_string(trg, src->val.str, strlen(src->val.str));
945 } else {
946 if (trg->type == LYXP_SET_NODE_SET) {
947 free(trg->val.nodes);
948 } else if (trg->type == LYXP_SET_STRING) {
949 free(trg->val.str);
950 }
951
Michal Vaskod3678892020-05-21 10:06:58 +0200952 assert(src->type == LYXP_SET_NODE_SET);
953
954 trg->type = LYXP_SET_NODE_SET;
955 trg->used = src->used;
956 trg->size = src->used;
957 trg->ctx_pos = src->ctx_pos;
958 trg->ctx_size = src->ctx_size;
959
Michal Vasko08e9b112021-06-11 15:41:17 +0200960 if (trg->size) {
961 trg->val.nodes = malloc(trg->size * sizeof *trg->val.nodes);
962 LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), );
963 memcpy(trg->val.nodes, src->val.nodes, src->used * sizeof *src->val.nodes);
964 } else {
965 trg->val.nodes = NULL;
966 }
Michal Vaskod3678892020-05-21 10:06:58 +0200967 if (src->ht) {
968 trg->ht = lyht_dup(src->ht);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200969 } else {
Michal Vaskod3678892020-05-21 10:06:58 +0200970 trg->ht = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200971 }
972 }
973}
974
975/**
976 * @brief Clear context of all schema nodes.
977 *
978 * @param[in] set Set to clear.
Michal Vasko1a09b212021-05-06 13:00:10 +0200979 * @param[in] new_ctx New context state for all the nodes currently in the context.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200980 */
981static void
Michal Vasko1a09b212021-05-06 13:00:10 +0200982set_scnode_clear_ctx(struct lyxp_set *set, int32_t new_ctx)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200983{
984 uint32_t i;
985
986 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100987 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko1a09b212021-05-06 13:00:10 +0200988 set->val.scnodes[i].in_ctx = new_ctx;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100989 } else if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START) {
990 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200991 }
992 }
993}
994
995/**
996 * @brief Remove a node from a set. Removing last node changes
997 * set into LYXP_SET_EMPTY. Context position aware.
998 *
999 * @param[in] set Set to use.
1000 * @param[in] idx Index from @p set of the node to be removed.
1001 */
1002static void
1003set_remove_node(struct lyxp_set *set, uint32_t idx)
1004{
1005 assert(set && (set->type == LYXP_SET_NODE_SET));
1006 assert(idx < set->used);
1007
1008 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
1009
1010 --set->used;
Michal Vasko08e9b112021-06-11 15:41:17 +02001011 if (idx < set->used) {
1012 memmove(&set->val.nodes[idx], &set->val.nodes[idx + 1], (set->used - idx) * sizeof *set->val.nodes);
1013 } else if (!set->used) {
Michal Vaskod3678892020-05-21 10:06:58 +02001014 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001015 }
1016}
1017
1018/**
Michal Vasko2caefc12019-11-14 16:07:56 +01001019 * @brief Remove a node from a set by setting its type to LYXP_NODE_NONE.
Michal Vasko57eab132019-09-24 11:46:26 +02001020 *
1021 * @param[in] set Set to use.
1022 * @param[in] idx Index from @p set of the node to be removed.
1023 */
1024static void
Michal Vasko2caefc12019-11-14 16:07:56 +01001025set_remove_node_none(struct lyxp_set *set, uint32_t idx)
Michal Vasko57eab132019-09-24 11:46:26 +02001026{
1027 assert(set && (set->type == LYXP_SET_NODE_SET));
1028 assert(idx < set->used);
1029
Michal Vasko2caefc12019-11-14 16:07:56 +01001030 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1031 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
1032 }
1033 set->val.nodes[idx].type = LYXP_NODE_NONE;
Michal Vasko57eab132019-09-24 11:46:26 +02001034}
1035
1036/**
Michal Vasko2caefc12019-11-14 16:07:56 +01001037 * @brief Remove all LYXP_NODE_NONE nodes from a set. Removing last node changes
Michal Vasko57eab132019-09-24 11:46:26 +02001038 * set into LYXP_SET_EMPTY. Context position aware.
1039 *
1040 * @param[in] set Set to consolidate.
1041 */
1042static void
Michal Vasko2caefc12019-11-14 16:07:56 +01001043set_remove_nodes_none(struct lyxp_set *set)
Michal Vasko57eab132019-09-24 11:46:26 +02001044{
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01001045 uint32_t i, orig_used, end = 0;
1046 int64_t start;
Michal Vasko57eab132019-09-24 11:46:26 +02001047
Michal Vaskod3678892020-05-21 10:06:58 +02001048 assert(set);
Michal Vasko57eab132019-09-24 11:46:26 +02001049
1050 orig_used = set->used;
1051 set->used = 0;
Michal Vaskod989ba02020-08-24 10:59:24 +02001052 for (i = 0; i < orig_used; ) {
Michal Vasko57eab132019-09-24 11:46:26 +02001053 start = -1;
1054 do {
Michal Vasko2caefc12019-11-14 16:07:56 +01001055 if ((set->val.nodes[i].type != LYXP_NODE_NONE) && (start == -1)) {
Michal Vasko57eab132019-09-24 11:46:26 +02001056 start = i;
Michal Vasko2caefc12019-11-14 16:07:56 +01001057 } else if ((start > -1) && (set->val.nodes[i].type == LYXP_NODE_NONE)) {
Michal Vasko57eab132019-09-24 11:46:26 +02001058 end = i;
1059 ++i;
1060 break;
1061 }
1062
1063 ++i;
1064 if (i == orig_used) {
1065 end = i;
1066 }
1067 } while (i < orig_used);
1068
1069 if (start > -1) {
1070 /* move the whole chunk of valid nodes together */
1071 if (set->used != (unsigned)start) {
1072 memmove(&set->val.nodes[set->used], &set->val.nodes[start], (end - start) * sizeof *set->val.nodes);
1073 }
1074 set->used += end - start;
1075 }
1076 }
Michal Vasko57eab132019-09-24 11:46:26 +02001077}
1078
1079/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02001080 * @brief Check for duplicates in a node set.
1081 *
1082 * @param[in] set Set to check.
1083 * @param[in] node Node to look for in @p set.
1084 * @param[in] node_type Type of @p node.
1085 * @param[in] skip_idx Index from @p set to skip.
1086 * @return LY_ERR
1087 */
1088static LY_ERR
1089set_dup_node_check(const struct lyxp_set *set, const struct lyd_node *node, enum lyxp_node_type node_type, int skip_idx)
1090{
1091 uint32_t i;
1092
Michal Vasko2caefc12019-11-14 16:07:56 +01001093 if (set->ht && node) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001094 return set_dup_node_hash_check(set, (struct lyd_node *)node, node_type, skip_idx);
1095 }
1096
1097 for (i = 0; i < set->used; ++i) {
1098 if ((skip_idx > -1) && (i == (unsigned)skip_idx)) {
1099 continue;
1100 }
1101
1102 if ((set->val.nodes[i].node == node) && (set->val.nodes[i].type == node_type)) {
1103 return LY_EEXIST;
1104 }
1105 }
1106
1107 return LY_SUCCESS;
1108}
1109
Radek Krejci857189e2020-09-01 13:26:36 +02001110ly_bool
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001111lyxp_set_scnode_contains(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type, int skip_idx,
1112 uint32_t *index_p)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001113{
1114 uint32_t i;
1115
1116 for (i = 0; i < set->used; ++i) {
1117 if ((skip_idx > -1) && (i == (unsigned)skip_idx)) {
1118 continue;
1119 }
1120
1121 if ((set->val.scnodes[i].scnode == node) && (set->val.scnodes[i].type == node_type)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001122 if (index_p) {
1123 *index_p = i;
1124 }
1125 return 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001126 }
1127 }
1128
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001129 return 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001130}
1131
Michal Vaskoecd62de2019-11-13 12:35:11 +01001132void
1133lyxp_set_scnode_merge(struct lyxp_set *set1, struct lyxp_set *set2)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001134{
1135 uint32_t orig_used, i, j;
1136
Michal Vaskod3678892020-05-21 10:06:58 +02001137 assert((set1->type == LYXP_SET_SCNODE_SET) && (set2->type == LYXP_SET_SCNODE_SET));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001138
Michal Vaskod3678892020-05-21 10:06:58 +02001139 if (!set2->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001140 return;
1141 }
1142
Michal Vaskod3678892020-05-21 10:06:58 +02001143 if (!set1->used) {
aPiecekadc1e4f2021-10-07 11:15:12 +02001144 /* release hidden allocated data (lyxp_set.size) */
1145 lyxp_set_free_content(set1);
1146 /* direct copying of the entire structure */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001147 memcpy(set1, set2, sizeof *set1);
1148 return;
1149 }
1150
1151 if (set1->used + set2->used > set1->size) {
1152 set1->size = set1->used + set2->used;
1153 set1->val.scnodes = ly_realloc(set1->val.scnodes, set1->size * sizeof *set1->val.scnodes);
1154 LY_CHECK_ERR_RET(!set1->val.scnodes, LOGMEM(set1->ctx), );
1155 }
1156
1157 orig_used = set1->used;
1158
1159 for (i = 0; i < set2->used; ++i) {
1160 for (j = 0; j < orig_used; ++j) {
1161 /* detect duplicities */
1162 if (set1->val.scnodes[j].scnode == set2->val.scnodes[i].scnode) {
1163 break;
1164 }
1165 }
1166
Michal Vasko0587bce2020-12-10 12:19:21 +01001167 if (j < orig_used) {
1168 /* node is there, but update its status if needed */
1169 if (set1->val.scnodes[j].in_ctx == LYXP_SET_SCNODE_START_USED) {
1170 set1->val.scnodes[j].in_ctx = set2->val.scnodes[i].in_ctx;
Michal Vasko1a09b212021-05-06 13:00:10 +02001171 } else if ((set1->val.scnodes[j].in_ctx == LYXP_SET_SCNODE_ATOM_NODE) &&
1172 (set2->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_VAL)) {
1173 set1->val.scnodes[j].in_ctx = set2->val.scnodes[i].in_ctx;
Michal Vasko0587bce2020-12-10 12:19:21 +01001174 }
1175 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001176 memcpy(&set1->val.scnodes[set1->used], &set2->val.scnodes[i], sizeof *set2->val.scnodes);
1177 ++set1->used;
1178 }
1179 }
1180
Michal Vaskod3678892020-05-21 10:06:58 +02001181 lyxp_set_free_content(set2);
1182 set2->type = LYXP_SET_SCNODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001183}
1184
1185/**
1186 * @brief Insert a node into a set. Context position aware.
1187 *
1188 * @param[in] set Set to use.
1189 * @param[in] node Node to insert to @p set.
1190 * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting.
1191 * @param[in] node_type Node type of @p node.
1192 * @param[in] idx Index in @p set to insert into.
1193 */
1194static void
1195set_insert_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx)
1196{
Michal Vaskod3678892020-05-21 10:06:58 +02001197 assert(set && (set->type == LYXP_SET_NODE_SET));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001198
Michal Vaskod3678892020-05-21 10:06:58 +02001199 if (!set->size) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001200 /* first item */
1201 if (idx) {
1202 /* no real harm done, but it is a bug */
1203 LOGINT(set->ctx);
1204 idx = 0;
1205 }
1206 set->val.nodes = malloc(LYXP_SET_SIZE_START * sizeof *set->val.nodes);
1207 LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), );
1208 set->type = LYXP_SET_NODE_SET;
1209 set->used = 0;
1210 set->size = LYXP_SET_SIZE_START;
1211 set->ctx_pos = 1;
1212 set->ctx_size = 1;
1213 set->ht = NULL;
1214 } else {
1215 /* not an empty set */
1216 if (set->used == set->size) {
1217
1218 /* set is full */
Michal Vasko871df522022-04-06 12:14:41 +02001219 set->val.nodes = ly_realloc(set->val.nodes, (set->size * LYXP_SET_SIZE_MUL_STEP) * sizeof *set->val.nodes);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001220 LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), );
Michal Vasko871df522022-04-06 12:14:41 +02001221 set->size *= LYXP_SET_SIZE_MUL_STEP;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001222 }
1223
1224 if (idx > set->used) {
1225 LOGINT(set->ctx);
1226 idx = set->used;
1227 }
1228
1229 /* make space for the new node */
1230 if (idx < set->used) {
1231 memmove(&set->val.nodes[idx + 1], &set->val.nodes[idx], (set->used - idx) * sizeof *set->val.nodes);
1232 }
1233 }
1234
1235 /* finally assign the value */
1236 set->val.nodes[idx].node = (struct lyd_node *)node;
1237 set->val.nodes[idx].type = node_type;
1238 set->val.nodes[idx].pos = pos;
1239 ++set->used;
1240
Michal Vasko2416fdd2021-10-01 11:07:10 +02001241 /* add into hash table */
1242 set_insert_node_hash(set, (struct lyd_node *)node, node_type);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001243}
1244
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001245LY_ERR
Michal Vaskoddd76592022-01-17 13:34:48 +01001246lyxp_set_scnode_insert_node(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type,
1247 uint32_t *index_p)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001248{
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001249 uint32_t index;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001250
1251 assert(set->type == LYXP_SET_SCNODE_SET);
1252
Michal Vasko871df522022-04-06 12:14:41 +02001253 if (!set->size) {
1254 /* first item */
1255 set->val.scnodes = malloc(LYXP_SET_SIZE_START * sizeof *set->val.scnodes);
1256 LY_CHECK_ERR_RET(!set->val.scnodes, LOGMEM(set->ctx), LY_EMEM);
1257 set->type = LYXP_SET_SCNODE_SET;
1258 set->used = 0;
1259 set->size = LYXP_SET_SIZE_START;
1260 set->ctx_pos = 1;
1261 set->ctx_size = 1;
1262 set->ht = NULL;
1263 }
1264
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001265 if (lyxp_set_scnode_contains(set, node, node_type, -1, &index)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001266 set->val.scnodes[index].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001267 } else {
1268 if (set->used == set->size) {
Michal Vasko871df522022-04-06 12:14:41 +02001269 set->val.scnodes = ly_realloc(set->val.scnodes, (set->size * LYXP_SET_SIZE_MUL_STEP) * sizeof *set->val.scnodes);
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001270 LY_CHECK_ERR_RET(!set->val.scnodes, LOGMEM(set->ctx), LY_EMEM);
Michal Vasko871df522022-04-06 12:14:41 +02001271 set->size *= LYXP_SET_SIZE_MUL_STEP;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001272 }
1273
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001274 index = set->used;
1275 set->val.scnodes[index].scnode = (struct lysc_node *)node;
1276 set->val.scnodes[index].type = node_type;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001277 set->val.scnodes[index].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001278 ++set->used;
1279 }
1280
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001281 if (index_p) {
1282 *index_p = index;
1283 }
1284
1285 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001286}
1287
1288/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02001289 * @brief Set all nodes with ctx 1 to a new unique context value.
1290 *
1291 * @param[in] set Set to modify.
1292 * @return New context value.
1293 */
Michal Vasko5c4e5892019-11-14 12:31:38 +01001294static int32_t
Michal Vasko03ff5a72019-09-11 13:49:33 +02001295set_scnode_new_in_ctx(struct lyxp_set *set)
1296{
Michal Vasko5c4e5892019-11-14 12:31:38 +01001297 uint32_t i;
1298 int32_t ret_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001299
1300 assert(set->type == LYXP_SET_SCNODE_SET);
1301
Radek Krejcif13b87b2020-12-01 22:02:17 +01001302 ret_ctx = LYXP_SET_SCNODE_ATOM_PRED_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001303retry:
1304 for (i = 0; i < set->used; ++i) {
1305 if (set->val.scnodes[i].in_ctx >= ret_ctx) {
1306 ret_ctx = set->val.scnodes[i].in_ctx + 1;
1307 goto retry;
1308 }
1309 }
1310 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001311 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001312 set->val.scnodes[i].in_ctx = ret_ctx;
1313 }
1314 }
1315
1316 return ret_ctx;
1317}
1318
1319/**
1320 * @brief Get unique @p node position in the data.
1321 *
1322 * @param[in] node Node to find.
1323 * @param[in] node_type Node type of @p node.
1324 * @param[in] root Root node.
1325 * @param[in] root_type Type of the XPath @p root node.
1326 * @param[in] prev Node that we think is before @p node in DFS from @p root. Can optionally
1327 * be used to increase efficiency and start the DFS from this node.
1328 * @param[in] prev_pos Node @p prev position. Optional, but must be set if @p prev is set.
1329 * @return Node position.
1330 */
1331static uint32_t
1332get_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 +02001333 enum lyxp_node_type root_type, const struct lyd_node **prev, uint32_t *prev_pos)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001334{
Michal Vasko56daf732020-08-10 10:57:18 +02001335 const struct lyd_node *elem = NULL, *top_sibling;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001336 uint32_t pos = 1;
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001337 ly_bool found = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001338
1339 assert(prev && prev_pos && !root->prev->next);
1340
1341 if ((node_type == LYXP_NODE_ROOT) || (node_type == LYXP_NODE_ROOT_CONFIG)) {
1342 return 0;
1343 }
1344
1345 if (*prev) {
1346 /* start from the previous element instead from the root */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001347 pos = *prev_pos;
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001348 for (top_sibling = *prev; top_sibling->parent; top_sibling = lyd_parent(top_sibling)) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001349 goto dfs_search;
1350 }
1351
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001352 LY_LIST_FOR(root, top_sibling) {
Michal Vasko56daf732020-08-10 10:57:18 +02001353 LYD_TREE_DFS_BEGIN(top_sibling, elem) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001354dfs_search:
Michal Vaskoa9309bb2021-07-09 09:31:55 +02001355 LYD_TREE_DFS_continue = 0;
1356
Michal Vasko56daf732020-08-10 10:57:18 +02001357 if (*prev && !elem) {
1358 /* resume previous DFS */
1359 elem = LYD_TREE_DFS_next = (struct lyd_node *)*prev;
1360 LYD_TREE_DFS_continue = 0;
1361 }
1362
Michal Vasko482a6822022-05-06 08:56:12 +02001363 if (!elem->schema || ((root_type == LYXP_NODE_ROOT_CONFIG) && (elem->schema->flags & LYS_CONFIG_R))) {
Michal Vasko56daf732020-08-10 10:57:18 +02001364 /* skip */
1365 LYD_TREE_DFS_continue = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001366 } else {
Michal Vasko56daf732020-08-10 10:57:18 +02001367 if (elem == node) {
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001368 found = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001369 break;
1370 }
Michal Vasko56daf732020-08-10 10:57:18 +02001371 ++pos;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001372 }
Michal Vasko56daf732020-08-10 10:57:18 +02001373
1374 LYD_TREE_DFS_END(top_sibling, elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001375 }
1376
1377 /* node found */
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001378 if (found) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001379 break;
1380 }
1381 }
1382
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001383 if (!found) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001384 if (!(*prev)) {
1385 /* we went from root and failed to find it, cannot be */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001386 LOGINT(LYD_CTX(node));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001387 return 0;
1388 } else {
Michal Vasko56daf732020-08-10 10:57:18 +02001389 /* start the search again from the beginning */
1390 *prev = root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001391
Michal Vasko56daf732020-08-10 10:57:18 +02001392 top_sibling = root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001393 pos = 1;
1394 goto dfs_search;
1395 }
1396 }
1397
1398 /* remember the last found node for next time */
1399 *prev = node;
1400 *prev_pos = pos;
1401
1402 return pos;
1403}
1404
1405/**
1406 * @brief Assign (fill) missing node positions.
1407 *
1408 * @param[in] set Set to fill positions in.
1409 * @param[in] root Context root node.
1410 * @param[in] root_type Context root type.
1411 * @return LY_ERR
1412 */
1413static LY_ERR
1414set_assign_pos(struct lyxp_set *set, const struct lyd_node *root, enum lyxp_node_type root_type)
1415{
1416 const struct lyd_node *prev = NULL, *tmp_node;
1417 uint32_t i, tmp_pos = 0;
1418
1419 for (i = 0; i < set->used; ++i) {
1420 if (!set->val.nodes[i].pos) {
1421 tmp_node = NULL;
1422 switch (set->val.nodes[i].type) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001423 case LYXP_NODE_META:
1424 tmp_node = set->val.meta[i].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001425 if (!tmp_node) {
1426 LOGINT_RET(root->schema->module->ctx);
1427 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01001428 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001429 case LYXP_NODE_ELEM:
1430 case LYXP_NODE_TEXT:
1431 if (!tmp_node) {
1432 tmp_node = set->val.nodes[i].node;
1433 }
1434 set->val.nodes[i].pos = get_node_pos(tmp_node, set->val.nodes[i].type, root, root_type, &prev, &tmp_pos);
1435 break;
1436 default:
1437 /* all roots have position 0 */
1438 break;
1439 }
1440 }
1441 }
1442
1443 return LY_SUCCESS;
1444}
1445
1446/**
Michal Vasko9f96a052020-03-10 09:41:45 +01001447 * @brief Get unique @p meta position in the parent metadata.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001448 *
Michal Vasko9f96a052020-03-10 09:41:45 +01001449 * @param[in] meta Metadata to use.
1450 * @return Metadata position.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001451 */
1452static uint16_t
Michal Vasko9f96a052020-03-10 09:41:45 +01001453get_meta_pos(struct lyd_meta *meta)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001454{
1455 uint16_t pos = 0;
Michal Vasko9f96a052020-03-10 09:41:45 +01001456 struct lyd_meta *meta2;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001457
Michal Vasko9f96a052020-03-10 09:41:45 +01001458 for (meta2 = meta->parent->meta; meta2 && (meta2 != meta); meta2 = meta2->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001459 ++pos;
1460 }
1461
Michal Vasko9f96a052020-03-10 09:41:45 +01001462 assert(meta2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001463 return pos;
1464}
1465
1466/**
1467 * @brief Compare 2 nodes in respect to XPath document order.
1468 *
1469 * @param[in] item1 1st node.
1470 * @param[in] item2 2nd node.
1471 * @return If 1st > 2nd returns 1, 1st == 2nd returns 0, and 1st < 2nd returns -1.
1472 */
1473static int
1474set_sort_compare(struct lyxp_set_node *item1, struct lyxp_set_node *item2)
1475{
Michal Vasko9f96a052020-03-10 09:41:45 +01001476 uint32_t meta_pos1 = 0, meta_pos2 = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001477
1478 if (item1->pos < item2->pos) {
1479 return -1;
1480 }
1481
1482 if (item1->pos > item2->pos) {
1483 return 1;
1484 }
1485
1486 /* node positions are equal, the fun case */
1487
1488 /* 1st ELEM - == - 2nd TEXT, 1st TEXT - == - 2nd ELEM */
1489 /* special case since text nodes are actually saved as their parents */
1490 if ((item1->node == item2->node) && (item1->type != item2->type)) {
1491 if (item1->type == LYXP_NODE_ELEM) {
1492 assert(item2->type == LYXP_NODE_TEXT);
1493 return -1;
1494 } else {
1495 assert((item1->type == LYXP_NODE_TEXT) && (item2->type == LYXP_NODE_ELEM));
1496 return 1;
1497 }
1498 }
1499
Michal Vasko9f96a052020-03-10 09:41:45 +01001500 /* we need meta positions now */
1501 if (item1->type == LYXP_NODE_META) {
1502 meta_pos1 = get_meta_pos((struct lyd_meta *)item1->node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001503 }
Michal Vasko9f96a052020-03-10 09:41:45 +01001504 if (item2->type == LYXP_NODE_META) {
1505 meta_pos2 = get_meta_pos((struct lyd_meta *)item2->node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001506 }
1507
Michal Vasko9f96a052020-03-10 09:41:45 +01001508 /* 1st ROOT - 2nd ROOT, 1st ELEM - 2nd ELEM, 1st TEXT - 2nd TEXT, 1st META - =pos= - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001509 /* check for duplicates */
1510 if (item1->node == item2->node) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001511 assert((item1->type == item2->type) && ((item1->type != LYXP_NODE_META) || (meta_pos1 == meta_pos2)));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001512 return 0;
1513 }
1514
Michal Vasko9f96a052020-03-10 09:41:45 +01001515 /* 1st ELEM - 2nd TEXT, 1st ELEM - any pos - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001516 /* elem is always first, 2nd node is after it */
1517 if (item1->type == LYXP_NODE_ELEM) {
1518 assert(item2->type != LYXP_NODE_ELEM);
1519 return -1;
1520 }
1521
Michal Vasko9f96a052020-03-10 09:41:45 +01001522 /* 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 +02001523 /* 2nd is before 1st */
Michal Vasko69730152020-10-09 16:30:07 +02001524 if (((item1->type == LYXP_NODE_TEXT) &&
1525 ((item2->type == LYXP_NODE_ELEM) || (item2->type == LYXP_NODE_META))) ||
1526 ((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_ELEM)) ||
1527 (((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_META)) &&
1528 (meta_pos1 > meta_pos2))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001529 return 1;
1530 }
1531
Michal Vasko9f96a052020-03-10 09:41:45 +01001532 /* 1st META - any pos - 2nd TEXT, 1st META <pos< - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001533 /* 2nd is after 1st */
1534 return -1;
1535}
1536
1537/**
1538 * @brief Set cast for comparisons.
1539 *
1540 * @param[in] trg Target set to cast source into.
1541 * @param[in] src Source set.
1542 * @param[in] type Target set type.
1543 * @param[in] src_idx Source set node index.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001544 * @return LY_ERR
1545 */
1546static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001547set_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 +02001548{
1549 assert(src->type == LYXP_SET_NODE_SET);
1550
1551 set_init(trg, src);
1552
1553 /* insert node into target set */
1554 set_insert_node(trg, src->val.nodes[src_idx].node, src->val.nodes[src_idx].pos, src->val.nodes[src_idx].type, 0);
1555
1556 /* cast target set appropriately */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001557 return lyxp_set_cast(trg, type);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001558}
1559
Michal Vasko4c7763f2020-07-27 17:40:37 +02001560/**
1561 * @brief Set content canonization for comparisons.
1562 *
1563 * @param[in] trg Target set to put the canononized source into.
1564 * @param[in] src Source set.
1565 * @param[in] xp_node Source XPath node/meta to use for canonization.
1566 * @return LY_ERR
1567 */
1568static LY_ERR
1569set_comp_canonize(struct lyxp_set *trg, const struct lyxp_set *src, const struct lyxp_set_node *xp_node)
1570{
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001571 const struct lysc_type *type = NULL;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001572 struct lyd_value val;
1573 struct ly_err_item *err = NULL;
1574 char *str, *ptr;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001575 LY_ERR rc;
1576
1577 /* is there anything to canonize even? */
1578 if ((src->type == LYXP_SET_NUMBER) || (src->type == LYXP_SET_STRING)) {
1579 /* do we have a type to use for canonization? */
1580 if ((xp_node->type == LYXP_NODE_ELEM) && (xp_node->node->schema->nodetype & LYD_NODE_TERM)) {
1581 type = ((struct lyd_node_term *)xp_node->node)->value.realtype;
1582 } else if (xp_node->type == LYXP_NODE_META) {
1583 type = ((struct lyd_meta *)xp_node->node)->value.realtype;
1584 }
1585 }
1586 if (!type) {
1587 goto fill;
1588 }
1589
1590 if (src->type == LYXP_SET_NUMBER) {
1591 /* canonize number */
1592 if (asprintf(&str, "%Lf", src->val.num) == -1) {
1593 LOGMEM(src->ctx);
1594 return LY_EMEM;
1595 }
1596 } else {
1597 /* canonize string */
1598 str = strdup(src->val.str);
1599 }
1600
1601 /* ignore errors, the value may not satisfy schema constraints */
Radek Krejci0b013302021-03-29 15:22:32 +02001602 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 +01001603 LYD_HINT_DATA, xp_node->node->schema, &val, NULL, &err);
Michal Vasko4c7763f2020-07-27 17:40:37 +02001604 ly_err_free(err);
1605 if (rc) {
1606 /* invalid value */
Radek Iša48460222021-03-02 15:18:32 +01001607 /* function store automaticaly dealloc value when fail */
Michal Vasko4c7763f2020-07-27 17:40:37 +02001608 goto fill;
1609 }
1610
Michal Vasko4c7763f2020-07-27 17:40:37 +02001611 /* use the canonized value */
1612 set_init(trg, src);
1613 trg->type = src->type;
1614 if (src->type == LYXP_SET_NUMBER) {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001615 trg->val.num = strtold(type->plugin->print(src->ctx, &val, LY_VALUE_CANON, NULL, NULL, NULL), &ptr);
Michal Vasko4c7763f2020-07-27 17:40:37 +02001616 LY_CHECK_ERR_RET(ptr[0], LOGINT(src->ctx), LY_EINT);
1617 } else {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001618 trg->val.str = strdup(type->plugin->print(src->ctx, &val, LY_VALUE_CANON, NULL, NULL, NULL));
Michal Vasko4c7763f2020-07-27 17:40:37 +02001619 }
1620 type->plugin->free(src->ctx, &val);
1621 return LY_SUCCESS;
1622
1623fill:
1624 /* no canonization needed/possible */
1625 set_fill_set(trg, src);
1626 return LY_SUCCESS;
1627}
1628
Michal Vasko03ff5a72019-09-11 13:49:33 +02001629#ifndef NDEBUG
1630
1631/**
1632 * @brief Bubble sort @p set into XPath document order.
1633 * Context position aware. Unused in the 'Release' build target.
1634 *
1635 * @param[in] set Set to sort.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001636 * @return How many times the whole set was traversed - 1 (if set was sorted, returns 0).
1637 */
1638static int
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001639set_sort(struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001640{
1641 uint32_t i, j;
Radek Krejci1deb5be2020-08-26 16:43:36 +02001642 int ret = 0, cmp;
Radek Krejci857189e2020-09-01 13:26:36 +02001643 ly_bool inverted, change;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001644 const struct lyd_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001645 struct lyxp_set_node item;
1646 struct lyxp_set_hash_node hnode;
1647 uint64_t hash;
1648
Michal Vasko3cf8fbf2020-05-27 15:21:21 +02001649 if ((set->type != LYXP_SET_NODE_SET) || (set->used < 2)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001650 return 0;
1651 }
1652
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001653 /* find first top-level node to be used as anchor for positions */
Michal Vasko4a7d4d62021-12-13 17:05:06 +01001654 for (root = set->tree; root->parent; root = lyd_parent(root)) {}
Michal Vaskod989ba02020-08-24 10:59:24 +02001655 for ( ; root->prev->next; root = root->prev) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001656
1657 /* fill positions */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001658 if (set_assign_pos(set, root, set->root_type)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001659 return -1;
1660 }
1661
1662 LOGDBG(LY_LDGXPATH, "SORT BEGIN");
1663 print_set_debug(set);
1664
1665 for (i = 0; i < set->used; ++i) {
1666 inverted = 0;
1667 change = 0;
1668
1669 for (j = 1; j < set->used - i; ++j) {
1670 /* compare node positions */
1671 if (inverted) {
1672 cmp = set_sort_compare(&set->val.nodes[j], &set->val.nodes[j - 1]);
1673 } else {
1674 cmp = set_sort_compare(&set->val.nodes[j - 1], &set->val.nodes[j]);
1675 }
1676
1677 /* swap if needed */
1678 if ((inverted && (cmp < 0)) || (!inverted && (cmp > 0))) {
1679 change = 1;
1680
1681 item = set->val.nodes[j - 1];
1682 set->val.nodes[j - 1] = set->val.nodes[j];
1683 set->val.nodes[j] = item;
1684 } else {
1685 /* whether node_pos1 should be smaller than node_pos2 or the other way around */
1686 inverted = !inverted;
1687 }
1688 }
1689
1690 ++ret;
1691
1692 if (!change) {
1693 break;
1694 }
1695 }
1696
1697 LOGDBG(LY_LDGXPATH, "SORT END %d", ret);
1698 print_set_debug(set);
1699
1700 /* check node hashes */
1701 if (set->used >= LYD_HT_MIN_ITEMS) {
1702 assert(set->ht);
1703 for (i = 0; i < set->used; ++i) {
1704 hnode.node = set->val.nodes[i].node;
1705 hnode.type = set->val.nodes[i].type;
1706
1707 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
1708 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
1709 hash = dict_hash_multi(hash, NULL, 0);
1710
1711 assert(!lyht_find(set->ht, &hnode, hash, NULL));
1712 }
1713 }
1714
1715 return ret - 1;
1716}
1717
Michal Vasko03ff5a72019-09-11 13:49:33 +02001718#endif
1719
1720/**
1721 * @brief Merge 2 sorted sets into one.
1722 *
1723 * @param[in,out] trg Set to merge into. Duplicates are removed.
1724 * @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 +02001725 * @return LY_ERR
1726 */
1727static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001728set_sorted_merge(struct lyxp_set *trg, struct lyxp_set *src)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001729{
1730 uint32_t i, j, k, count, dup_count;
1731 int cmp;
1732 const struct lyd_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001733
Michal Vaskod3678892020-05-21 10:06:58 +02001734 if ((trg->type != LYXP_SET_NODE_SET) || (src->type != LYXP_SET_NODE_SET)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001735 return LY_EINVAL;
1736 }
1737
Michal Vaskod3678892020-05-21 10:06:58 +02001738 if (!src->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001739 return LY_SUCCESS;
Michal Vaskod3678892020-05-21 10:06:58 +02001740 } else if (!trg->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001741 set_fill_set(trg, src);
Michal Vaskod3678892020-05-21 10:06:58 +02001742 lyxp_set_free_content(src);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001743 return LY_SUCCESS;
1744 }
1745
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001746 /* find first top-level node to be used as anchor for positions */
Michal Vasko0143b682021-12-13 17:13:09 +01001747 for (root = trg->tree; root->parent; root = lyd_parent(root)) {}
Michal Vaskod989ba02020-08-24 10:59:24 +02001748 for ( ; root->prev->next; root = root->prev) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001749
1750 /* fill positions */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001751 if (set_assign_pos(trg, root, trg->root_type) || set_assign_pos(src, root, src->root_type)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001752 return LY_EINT;
1753 }
1754
1755#ifndef NDEBUG
1756 LOGDBG(LY_LDGXPATH, "MERGE target");
1757 print_set_debug(trg);
1758 LOGDBG(LY_LDGXPATH, "MERGE source");
1759 print_set_debug(src);
1760#endif
1761
1762 /* make memory for the merge (duplicates are not detected yet, so space
1763 * will likely be wasted on them, too bad) */
1764 if (trg->size - trg->used < src->used) {
1765 trg->size = trg->used + src->used;
1766
1767 trg->val.nodes = ly_realloc(trg->val.nodes, trg->size * sizeof *trg->val.nodes);
1768 LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx), LY_EMEM);
1769 }
1770
1771 i = 0;
1772 j = 0;
1773 count = 0;
1774 dup_count = 0;
1775 do {
1776 cmp = set_sort_compare(&src->val.nodes[i], &trg->val.nodes[j]);
1777 if (!cmp) {
1778 if (!count) {
1779 /* duplicate, just skip it */
1780 ++i;
1781 ++j;
1782 } else {
1783 /* we are copying something already, so let's copy the duplicate too,
1784 * we are hoping that afterwards there are some more nodes to
1785 * copy and this way we can copy them all together */
1786 ++count;
1787 ++dup_count;
1788 ++i;
1789 ++j;
1790 }
1791 } else if (cmp < 0) {
1792 /* inserting src node into trg, just remember it for now */
1793 ++count;
1794 ++i;
1795
1796 /* insert the hash now */
1797 set_insert_node_hash(trg, src->val.nodes[i - 1].node, src->val.nodes[i - 1].type);
1798 } else if (count) {
1799copy_nodes:
1800 /* time to actually copy the nodes, we have found the largest block of nodes */
1801 memmove(&trg->val.nodes[j + (count - dup_count)],
1802 &trg->val.nodes[j],
1803 (trg->used - j) * sizeof *trg->val.nodes);
1804 memcpy(&trg->val.nodes[j - dup_count], &src->val.nodes[i - count], count * sizeof *src->val.nodes);
1805
1806 trg->used += count - dup_count;
1807 /* do not change i, except the copying above, we are basically doing exactly what is in the else branch below */
1808 j += count - dup_count;
1809
1810 count = 0;
1811 dup_count = 0;
1812 } else {
1813 ++j;
1814 }
1815 } while ((i < src->used) && (j < trg->used));
1816
1817 if ((i < src->used) || count) {
1818 /* insert all the hashes first */
1819 for (k = i; k < src->used; ++k) {
1820 set_insert_node_hash(trg, src->val.nodes[k].node, src->val.nodes[k].type);
1821 }
1822
1823 /* loop ended, but we need to copy something at trg end */
1824 count += src->used - i;
1825 i = src->used;
1826 goto copy_nodes;
1827 }
1828
1829 /* we are inserting hashes before the actual node insert, which causes
1830 * situations when there were initially not enough items for a hash table,
1831 * but even after some were inserted, hash table was not created (during
1832 * insertion the number of items is not updated yet) */
1833 if (!trg->ht && (trg->used >= LYD_HT_MIN_ITEMS)) {
1834 set_insert_node_hash(trg, NULL, 0);
1835 }
1836
1837#ifndef NDEBUG
1838 LOGDBG(LY_LDGXPATH, "MERGE result");
1839 print_set_debug(trg);
1840#endif
1841
Michal Vaskod3678892020-05-21 10:06:58 +02001842 lyxp_set_free_content(src);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001843 return LY_SUCCESS;
1844}
1845
Michal Vasko14676352020-05-29 11:35:55 +02001846LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02001847lyxp_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 +02001848{
Michal Vasko004d3152020-06-11 19:59:22 +02001849 if (exp->used == tok_idx) {
Michal Vasko14676352020-05-29 11:35:55 +02001850 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001851 LOGVAL(ctx, LY_VCODE_XP_EOF);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001852 }
Michal Vasko14676352020-05-29 11:35:55 +02001853 return LY_EINCOMPLETE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001854 }
1855
Michal Vasko004d3152020-06-11 19:59:22 +02001856 if (want_tok && (exp->tokens[tok_idx] != want_tok)) {
Michal Vasko14676352020-05-29 11:35:55 +02001857 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001858 LOGVAL(ctx, LY_VCODE_XP_INTOK2, lyxp_print_token(exp->tokens[tok_idx]),
Michal Vasko0b468e62020-10-19 09:33:04 +02001859 &exp->expr[exp->tok_pos[tok_idx]], lyxp_print_token(want_tok));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001860 }
Michal Vasko14676352020-05-29 11:35:55 +02001861 return LY_ENOT;
1862 }
1863
1864 return LY_SUCCESS;
1865}
1866
Michal Vasko004d3152020-06-11 19:59:22 +02001867LY_ERR
1868lyxp_next_token(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_token want_tok)
1869{
1870 LY_CHECK_RET(lyxp_check_token(ctx, exp, *tok_idx, want_tok));
1871
1872 /* skip the token */
1873 ++(*tok_idx);
1874
1875 return LY_SUCCESS;
1876}
1877
Michal Vasko14676352020-05-29 11:35:55 +02001878/* just like lyxp_check_token() but tests for 2 tokens */
1879static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02001880exp_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 +02001881 enum lyxp_token want_tok2)
Michal Vasko14676352020-05-29 11:35:55 +02001882{
Michal Vasko004d3152020-06-11 19:59:22 +02001883 if (exp->used == tok_idx) {
Michal Vasko14676352020-05-29 11:35:55 +02001884 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001885 LOGVAL(ctx, LY_VCODE_XP_EOF);
Michal Vasko14676352020-05-29 11:35:55 +02001886 }
1887 return LY_EINCOMPLETE;
1888 }
1889
Michal Vasko004d3152020-06-11 19:59:22 +02001890 if ((exp->tokens[tok_idx] != want_tok1) && (exp->tokens[tok_idx] != want_tok2)) {
Michal Vasko14676352020-05-29 11:35:55 +02001891 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001892 LOGVAL(ctx, LY_VCODE_XP_INTOK, lyxp_print_token(exp->tokens[tok_idx]),
Michal Vasko0b468e62020-10-19 09:33:04 +02001893 &exp->expr[exp->tok_pos[tok_idx]]);
Michal Vasko14676352020-05-29 11:35:55 +02001894 }
1895 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001896 }
1897
1898 return LY_SUCCESS;
1899}
1900
Michal Vasko4911eeb2021-06-28 11:23:05 +02001901LY_ERR
1902lyxp_next_token2(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_token want_tok1,
1903 enum lyxp_token want_tok2)
1904{
1905 LY_CHECK_RET(exp_check_token2(ctx, exp, *tok_idx, want_tok1, want_tok2));
1906
1907 /* skip the token */
1908 ++(*tok_idx);
1909
1910 return LY_SUCCESS;
1911}
1912
Michal Vasko03ff5a72019-09-11 13:49:33 +02001913/**
1914 * @brief Stack operation push on the repeat array.
1915 *
1916 * @param[in] exp Expression to use.
Michal Vasko004d3152020-06-11 19:59:22 +02001917 * @param[in] tok_idx Position in the expresion \p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001918 * @param[in] repeat_op_idx Index from \p exp of the operator token. This value is pushed.
1919 */
1920static void
Michal Vasko004d3152020-06-11 19:59:22 +02001921exp_repeat_push(struct lyxp_expr *exp, uint16_t tok_idx, uint16_t repeat_op_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001922{
1923 uint16_t i;
1924
Michal Vasko004d3152020-06-11 19:59:22 +02001925 if (exp->repeat[tok_idx]) {
Radek Krejci1e008d22020-08-17 11:37:37 +02001926 for (i = 0; exp->repeat[tok_idx][i]; ++i) {}
Michal Vasko004d3152020-06-11 19:59:22 +02001927 exp->repeat[tok_idx] = realloc(exp->repeat[tok_idx], (i + 2) * sizeof *exp->repeat[tok_idx]);
1928 LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), );
1929 exp->repeat[tok_idx][i] = repeat_op_idx;
1930 exp->repeat[tok_idx][i + 1] = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001931 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02001932 exp->repeat[tok_idx] = calloc(2, sizeof *exp->repeat[tok_idx]);
1933 LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), );
1934 exp->repeat[tok_idx][0] = repeat_op_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001935 }
1936}
1937
1938/**
1939 * @brief Reparse Predicate. Logs directly on error.
1940 *
1941 * [7] Predicate ::= '[' Expr ']'
1942 *
1943 * @param[in] ctx Context for logging.
1944 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02001945 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02001946 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001947 * @return LY_ERR
1948 */
1949static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02001950reparse_predicate(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t depth)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001951{
1952 LY_ERR rc;
1953
Michal Vasko004d3152020-06-11 19:59:22 +02001954 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001955 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02001956 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001957
aPiecekbf968d92021-05-27 14:35:05 +02001958 rc = reparse_or_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001959 LY_CHECK_RET(rc);
1960
Michal Vasko004d3152020-06-11 19:59:22 +02001961 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001962 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02001963 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001964
1965 return LY_SUCCESS;
1966}
1967
1968/**
1969 * @brief Reparse RelativeLocationPath. Logs directly on error.
1970 *
1971 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
1972 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
1973 * [6] NodeTest ::= NameTest | NodeType '(' ')'
1974 *
1975 * @param[in] ctx Context for logging.
1976 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02001977 * @param[in] tok_idx Position in the expression \p exp.
aPiecekbf968d92021-05-27 14:35:05 +02001978 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001979 * @return LY_ERR (LY_EINCOMPLETE on forward reference)
1980 */
1981static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02001982reparse_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 +02001983{
1984 LY_ERR rc;
1985
Michal Vasko004d3152020-06-11 19:59:22 +02001986 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001987 LY_CHECK_RET(rc);
1988
1989 goto step;
1990 do {
1991 /* '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02001992 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001993
Michal Vasko004d3152020-06-11 19:59:22 +02001994 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001995 LY_CHECK_RET(rc);
1996step:
1997 /* Step */
Michal Vasko004d3152020-06-11 19:59:22 +02001998 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001999 case LYXP_TOKEN_DOT:
Michal Vasko004d3152020-06-11 19:59:22 +02002000 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002001 break;
2002
2003 case LYXP_TOKEN_DDOT:
Michal Vasko004d3152020-06-11 19:59:22 +02002004 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002005 break;
2006
2007 case LYXP_TOKEN_AT:
Michal Vasko004d3152020-06-11 19:59:22 +02002008 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002009
Michal Vasko004d3152020-06-11 19:59:22 +02002010 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002011 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002012 if ((exp->tokens[*tok_idx] != LYXP_TOKEN_NAMETEST) && (exp->tokens[*tok_idx] != LYXP_TOKEN_NODETYPE)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002013 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 +02002014 return LY_EVALID;
2015 }
Radek Krejci0f969882020-08-21 16:56:47 +02002016 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02002017 case LYXP_TOKEN_NAMETEST:
Michal Vasko004d3152020-06-11 19:59:22 +02002018 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002019 goto reparse_predicate;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002020
2021 case LYXP_TOKEN_NODETYPE:
Michal Vasko004d3152020-06-11 19:59:22 +02002022 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002023
2024 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02002025 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002026 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002027 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002028
2029 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02002030 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002031 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002032 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002033
2034reparse_predicate:
2035 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002036 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
aPiecekbf968d92021-05-27 14:35:05 +02002037 rc = reparse_predicate(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002038 LY_CHECK_RET(rc);
2039 }
2040 break;
2041 default:
Radek Krejci2efc45b2020-12-22 16:25:44 +01002042 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 +02002043 return LY_EVALID;
2044 }
Michal Vasko004d3152020-06-11 19:59:22 +02002045 } while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02002046
2047 return LY_SUCCESS;
2048}
2049
2050/**
2051 * @brief Reparse AbsoluteLocationPath. Logs directly on error.
2052 *
2053 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
2054 *
2055 * @param[in] ctx Context for logging.
2056 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002057 * @param[in] tok_idx Position in the expression \p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002058 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002059 * @return LY_ERR
2060 */
2061static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002062reparse_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 +02002063{
2064 LY_ERR rc;
2065
Michal Vasko004d3152020-06-11 19:59:22 +02002066 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 +02002067
2068 /* '/' RelativeLocationPath? */
Michal Vasko004d3152020-06-11 19:59:22 +02002069 if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002070 /* '/' */
Michal Vasko004d3152020-06-11 19:59:22 +02002071 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002072
Michal Vasko004d3152020-06-11 19:59:22 +02002073 if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002074 return LY_SUCCESS;
2075 }
Michal Vasko004d3152020-06-11 19:59:22 +02002076 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002077 case LYXP_TOKEN_DOT:
2078 case LYXP_TOKEN_DDOT:
2079 case LYXP_TOKEN_AT:
2080 case LYXP_TOKEN_NAMETEST:
2081 case LYXP_TOKEN_NODETYPE:
aPiecekbf968d92021-05-27 14:35:05 +02002082 rc = reparse_relative_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002083 LY_CHECK_RET(rc);
Radek Krejci0f969882020-08-21 16:56:47 +02002084 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02002085 default:
2086 break;
2087 }
2088
Michal Vasko03ff5a72019-09-11 13:49:33 +02002089 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02002090 /* '//' RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002091 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002092
aPiecekbf968d92021-05-27 14:35:05 +02002093 rc = reparse_relative_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002094 LY_CHECK_RET(rc);
2095 }
2096
2097 return LY_SUCCESS;
2098}
2099
2100/**
2101 * @brief Reparse FunctionCall. Logs directly on error.
2102 *
2103 * [9] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
2104 *
2105 * @param[in] ctx Context for logging.
2106 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002107 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002108 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002109 * @return LY_ERR
2110 */
2111static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002112reparse_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 +02002113{
Radek Krejci1deb5be2020-08-26 16:43:36 +02002114 int8_t min_arg_count = -1;
2115 uint32_t arg_count, max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002116 uint16_t func_tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002117 LY_ERR rc;
2118
Michal Vasko004d3152020-06-11 19:59:22 +02002119 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_FUNCNAME);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002120 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002121 func_tok_idx = *tok_idx;
2122 switch (exp->tok_len[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002123 case 3:
Michal Vasko004d3152020-06-11 19:59:22 +02002124 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002125 min_arg_count = 1;
2126 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002127 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002128 min_arg_count = 1;
2129 max_arg_count = 1;
2130 }
2131 break;
2132 case 4:
Michal Vasko004d3152020-06-11 19:59:22 +02002133 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002134 min_arg_count = 1;
2135 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002136 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002137 min_arg_count = 0;
2138 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002139 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002140 min_arg_count = 0;
2141 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002142 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002143 min_arg_count = 0;
2144 max_arg_count = 0;
2145 }
2146 break;
2147 case 5:
Michal Vasko004d3152020-06-11 19:59:22 +02002148 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002149 min_arg_count = 1;
2150 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002151 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002152 min_arg_count = 0;
2153 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002154 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002155 min_arg_count = 1;
2156 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002157 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002158 min_arg_count = 1;
2159 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002160 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002161 min_arg_count = 1;
2162 max_arg_count = 1;
2163 }
2164 break;
2165 case 6:
Michal Vasko004d3152020-06-11 19:59:22 +02002166 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002167 min_arg_count = 2;
Radek Krejci1deb5be2020-08-26 16:43:36 +02002168 max_arg_count = UINT32_MAX;
Michal Vasko004d3152020-06-11 19:59:22 +02002169 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002170 min_arg_count = 0;
2171 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002172 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002173 min_arg_count = 0;
2174 max_arg_count = 1;
2175 }
2176 break;
2177 case 7:
Michal Vasko004d3152020-06-11 19:59:22 +02002178 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002179 min_arg_count = 1;
2180 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002181 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002182 min_arg_count = 1;
2183 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002184 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002185 min_arg_count = 0;
2186 max_arg_count = 0;
2187 }
2188 break;
2189 case 8:
Michal Vasko004d3152020-06-11 19:59:22 +02002190 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002191 min_arg_count = 2;
2192 max_arg_count = 2;
Michal Vasko004d3152020-06-11 19:59:22 +02002193 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002194 min_arg_count = 0;
2195 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002196 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "re-match", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002197 min_arg_count = 2;
2198 max_arg_count = 2;
2199 }
2200 break;
2201 case 9:
Michal Vasko004d3152020-06-11 19:59:22 +02002202 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002203 min_arg_count = 2;
2204 max_arg_count = 3;
Michal Vasko004d3152020-06-11 19:59:22 +02002205 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002206 min_arg_count = 3;
2207 max_arg_count = 3;
2208 }
2209 break;
2210 case 10:
Michal Vasko004d3152020-06-11 19:59:22 +02002211 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002212 min_arg_count = 0;
2213 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002214 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002215 min_arg_count = 1;
2216 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002217 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002218 min_arg_count = 2;
2219 max_arg_count = 2;
2220 }
2221 break;
2222 case 11:
Michal Vasko004d3152020-06-11 19:59:22 +02002223 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002224 min_arg_count = 2;
2225 max_arg_count = 2;
2226 }
2227 break;
2228 case 12:
Michal Vasko004d3152020-06-11 19:59:22 +02002229 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002230 min_arg_count = 2;
2231 max_arg_count = 2;
2232 }
2233 break;
2234 case 13:
Michal Vasko004d3152020-06-11 19:59:22 +02002235 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002236 min_arg_count = 0;
2237 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002238 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string-length", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002239 min_arg_count = 0;
2240 max_arg_count = 1;
2241 }
2242 break;
2243 case 15:
Michal Vasko004d3152020-06-11 19:59:22 +02002244 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002245 min_arg_count = 0;
2246 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002247 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-after", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002248 min_arg_count = 2;
2249 max_arg_count = 2;
2250 }
2251 break;
2252 case 16:
Michal Vasko004d3152020-06-11 19:59:22 +02002253 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002254 min_arg_count = 2;
2255 max_arg_count = 2;
2256 }
2257 break;
2258 case 20:
Michal Vasko004d3152020-06-11 19:59:22 +02002259 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from-or-self", 20)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002260 min_arg_count = 2;
2261 max_arg_count = 2;
2262 }
2263 break;
2264 }
2265 if (min_arg_count == -1) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002266 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 +02002267 return LY_EINVAL;
2268 }
Michal Vasko004d3152020-06-11 19:59:22 +02002269 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002270
2271 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02002272 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002273 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002274 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002275
2276 /* ( Expr ( ',' Expr )* )? */
2277 arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002278 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002279 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002280 if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002281 ++arg_count;
aPiecekbf968d92021-05-27 14:35:05 +02002282 rc = reparse_or_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002283 LY_CHECK_RET(rc);
2284 }
Michal Vasko004d3152020-06-11 19:59:22 +02002285 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) {
2286 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002287
2288 ++arg_count;
aPiecekbf968d92021-05-27 14:35:05 +02002289 rc = reparse_or_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002290 LY_CHECK_RET(rc);
2291 }
2292
2293 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02002294 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002295 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002296 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002297
Radek Krejci857189e2020-09-01 13:26:36 +02002298 if ((arg_count < (uint32_t)min_arg_count) || (arg_count > max_arg_count)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002299 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 +02002300 return LY_EVALID;
2301 }
2302
2303 return LY_SUCCESS;
2304}
2305
2306/**
2307 * @brief Reparse PathExpr. Logs directly on error.
2308 *
2309 * [10] PathExpr ::= LocationPath | PrimaryExpr Predicate*
2310 * | PrimaryExpr Predicate* '/' RelativeLocationPath
2311 * | PrimaryExpr Predicate* '//' RelativeLocationPath
2312 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
aPiecekfba75362021-10-07 12:39:48 +02002313 * [8] PrimaryExpr ::= VariableReference | '(' Expr ')' | Literal | Number | FunctionCall
Michal Vasko03ff5a72019-09-11 13:49:33 +02002314 *
2315 * @param[in] ctx Context for logging.
2316 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002317 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002318 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002319 * @return LY_ERR
2320 */
2321static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002322reparse_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 +02002323{
2324 LY_ERR rc;
2325
Michal Vasko004d3152020-06-11 19:59:22 +02002326 if (lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko14676352020-05-29 11:35:55 +02002327 return LY_EVALID;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002328 }
2329
Michal Vasko004d3152020-06-11 19:59:22 +02002330 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002331 case LYXP_TOKEN_PAR1:
2332 /* '(' Expr ')' Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002333 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002334
aPiecekbf968d92021-05-27 14:35:05 +02002335 rc = reparse_or_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002336 LY_CHECK_RET(rc);
2337
Michal Vasko004d3152020-06-11 19:59:22 +02002338 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002339 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002340 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002341 goto predicate;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002342 case LYXP_TOKEN_DOT:
2343 case LYXP_TOKEN_DDOT:
2344 case LYXP_TOKEN_AT:
2345 case LYXP_TOKEN_NAMETEST:
2346 case LYXP_TOKEN_NODETYPE:
2347 /* RelativeLocationPath */
aPiecekbf968d92021-05-27 14:35:05 +02002348 rc = reparse_relative_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002349 LY_CHECK_RET(rc);
2350 break;
aPiecekfba75362021-10-07 12:39:48 +02002351 case LYXP_TOKEN_VARREF:
2352 /* VariableReference */
2353 ++(*tok_idx);
2354 goto predicate;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002355 case LYXP_TOKEN_FUNCNAME:
2356 /* FunctionCall */
aPiecekbf968d92021-05-27 14:35:05 +02002357 rc = reparse_function_call(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002358 LY_CHECK_RET(rc);
2359 goto predicate;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002360 case LYXP_TOKEN_OPER_PATH:
2361 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +02002362 /* AbsoluteLocationPath */
aPiecekbf968d92021-05-27 14:35:05 +02002363 rc = reparse_absolute_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002364 LY_CHECK_RET(rc);
2365 break;
2366 case LYXP_TOKEN_LITERAL:
2367 /* Literal */
Michal Vasko004d3152020-06-11 19:59:22 +02002368 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002369 goto predicate;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002370 case LYXP_TOKEN_NUMBER:
2371 /* Number */
Michal Vasko004d3152020-06-11 19:59:22 +02002372 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002373 goto predicate;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002374 default:
Radek Krejci2efc45b2020-12-22 16:25:44 +01002375 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 +02002376 return LY_EVALID;
2377 }
2378
2379 return LY_SUCCESS;
2380
2381predicate:
2382 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002383 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
aPiecekbf968d92021-05-27 14:35:05 +02002384 rc = reparse_predicate(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002385 LY_CHECK_RET(rc);
2386 }
2387
2388 /* ('/' or '//') RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002389 if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002390
2391 /* '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02002392 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002393
aPiecekbf968d92021-05-27 14:35:05 +02002394 rc = reparse_relative_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002395 LY_CHECK_RET(rc);
2396 }
2397
2398 return LY_SUCCESS;
2399}
2400
2401/**
2402 * @brief Reparse UnaryExpr. Logs directly on error.
2403 *
2404 * [17] UnaryExpr ::= UnionExpr | '-' UnaryExpr
2405 * [18] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
2406 *
2407 * @param[in] ctx Context for logging.
2408 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002409 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002410 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002411 * @return LY_ERR
2412 */
2413static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002414reparse_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 +02002415{
2416 uint16_t prev_exp;
2417 LY_ERR rc;
2418
2419 /* ('-')* */
Michal Vasko004d3152020-06-11 19:59:22 +02002420 prev_exp = *tok_idx;
Michal Vasko69730152020-10-09 16:30:07 +02002421 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2422 (exp->expr[exp->tok_pos[*tok_idx]] == '-')) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002423 exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNARY);
Michal Vasko004d3152020-06-11 19:59:22 +02002424 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002425 }
2426
2427 /* PathExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002428 prev_exp = *tok_idx;
aPiecekbf968d92021-05-27 14:35:05 +02002429 rc = reparse_path_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002430 LY_CHECK_RET(rc);
2431
2432 /* ('|' PathExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002433 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_UNI)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002434 exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNION);
Michal Vasko004d3152020-06-11 19:59:22 +02002435 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002436
aPiecekbf968d92021-05-27 14:35:05 +02002437 rc = reparse_path_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002438 LY_CHECK_RET(rc);
2439 }
2440
2441 return LY_SUCCESS;
2442}
2443
2444/**
2445 * @brief Reparse AdditiveExpr. Logs directly on error.
2446 *
2447 * [15] AdditiveExpr ::= MultiplicativeExpr
2448 * | AdditiveExpr '+' MultiplicativeExpr
2449 * | AdditiveExpr '-' MultiplicativeExpr
2450 * [16] MultiplicativeExpr ::= UnaryExpr
2451 * | MultiplicativeExpr '*' UnaryExpr
2452 * | MultiplicativeExpr 'div' UnaryExpr
2453 * | MultiplicativeExpr 'mod' UnaryExpr
2454 *
2455 * @param[in] ctx Context for logging.
2456 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002457 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002458 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002459 * @return LY_ERR
2460 */
2461static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002462reparse_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 +02002463{
2464 uint16_t prev_add_exp, prev_mul_exp;
2465 LY_ERR rc;
2466
Michal Vasko004d3152020-06-11 19:59:22 +02002467 prev_add_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002468 goto reparse_multiplicative_expr;
2469
2470 /* ('+' / '-' MultiplicativeExpr)* */
Michal Vasko69730152020-10-09 16:30:07 +02002471 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2472 ((exp->expr[exp->tok_pos[*tok_idx]] == '+') || (exp->expr[exp->tok_pos[*tok_idx]] == '-'))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002473 exp_repeat_push(exp, prev_add_exp, LYXP_EXPR_ADDITIVE);
Michal Vasko004d3152020-06-11 19:59:22 +02002474 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002475
2476reparse_multiplicative_expr:
2477 /* UnaryExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002478 prev_mul_exp = *tok_idx;
aPiecekbf968d92021-05-27 14:35:05 +02002479 rc = reparse_unary_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002480 LY_CHECK_RET(rc);
2481
2482 /* ('*' / 'div' / 'mod' UnaryExpr)* */
Michal Vasko69730152020-10-09 16:30:07 +02002483 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2484 ((exp->expr[exp->tok_pos[*tok_idx]] == '*') || (exp->tok_len[*tok_idx] == 3))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002485 exp_repeat_push(exp, prev_mul_exp, LYXP_EXPR_MULTIPLICATIVE);
Michal Vasko004d3152020-06-11 19:59:22 +02002486 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002487
aPiecekbf968d92021-05-27 14:35:05 +02002488 rc = reparse_unary_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002489 LY_CHECK_RET(rc);
2490 }
2491 }
2492
2493 return LY_SUCCESS;
2494}
2495
2496/**
2497 * @brief Reparse EqualityExpr. Logs directly on error.
2498 *
2499 * [13] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
2500 * | EqualityExpr '!=' RelationalExpr
2501 * [14] RelationalExpr ::= AdditiveExpr
2502 * | RelationalExpr '<' AdditiveExpr
2503 * | RelationalExpr '>' AdditiveExpr
2504 * | RelationalExpr '<=' AdditiveExpr
2505 * | RelationalExpr '>=' AdditiveExpr
2506 *
2507 * @param[in] ctx Context for logging.
2508 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002509 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002510 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002511 * @return LY_ERR
2512 */
2513static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002514reparse_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 +02002515{
2516 uint16_t prev_eq_exp, prev_rel_exp;
2517 LY_ERR rc;
2518
Michal Vasko004d3152020-06-11 19:59:22 +02002519 prev_eq_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002520 goto reparse_additive_expr;
2521
2522 /* ('=' / '!=' RelationalExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002523 while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_EQUAL, LYXP_TOKEN_OPER_NEQUAL)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002524 exp_repeat_push(exp, prev_eq_exp, LYXP_EXPR_EQUALITY);
Michal Vasko004d3152020-06-11 19:59:22 +02002525 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002526
2527reparse_additive_expr:
2528 /* AdditiveExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002529 prev_rel_exp = *tok_idx;
aPiecekbf968d92021-05-27 14:35:05 +02002530 rc = reparse_additive_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002531 LY_CHECK_RET(rc);
2532
2533 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002534 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_COMP)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002535 exp_repeat_push(exp, prev_rel_exp, LYXP_EXPR_RELATIONAL);
Michal Vasko004d3152020-06-11 19:59:22 +02002536 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002537
aPiecekbf968d92021-05-27 14:35:05 +02002538 rc = reparse_additive_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002539 LY_CHECK_RET(rc);
2540 }
2541 }
2542
2543 return LY_SUCCESS;
2544}
2545
2546/**
2547 * @brief Reparse OrExpr. Logs directly on error.
2548 *
2549 * [11] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
2550 * [12] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
2551 *
2552 * @param[in] ctx Context for logging.
2553 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002554 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002555 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002556 * @return LY_ERR
2557 */
2558static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002559reparse_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 +02002560{
2561 uint16_t prev_or_exp, prev_and_exp;
2562 LY_ERR rc;
2563
aPiecekbf968d92021-05-27 14:35:05 +02002564 ++depth;
2565 LY_CHECK_ERR_RET(depth > LYXP_MAX_BLOCK_DEPTH, LOGVAL(ctx, LY_VCODE_XP_DEPTH), LY_EINVAL);
2566
Michal Vasko004d3152020-06-11 19:59:22 +02002567 prev_or_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002568 goto reparse_equality_expr;
2569
2570 /* ('or' AndExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002571 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 +02002572 exp_repeat_push(exp, prev_or_exp, LYXP_EXPR_OR);
Michal Vasko004d3152020-06-11 19:59:22 +02002573 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002574
2575reparse_equality_expr:
2576 /* EqualityExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002577 prev_and_exp = *tok_idx;
aPiecekbf968d92021-05-27 14:35:05 +02002578 rc = reparse_equality_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002579 LY_CHECK_RET(rc);
2580
2581 /* ('and' EqualityExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002582 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 +02002583 exp_repeat_push(exp, prev_and_exp, LYXP_EXPR_AND);
Michal Vasko004d3152020-06-11 19:59:22 +02002584 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002585
aPiecekbf968d92021-05-27 14:35:05 +02002586 rc = reparse_equality_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002587 LY_CHECK_RET(rc);
2588 }
2589 }
2590
2591 return LY_SUCCESS;
2592}
Radek Krejcib1646a92018-11-02 16:08:26 +01002593
2594/**
2595 * @brief Parse NCName.
2596 *
2597 * @param[in] ncname Name to parse.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002598 * @return Length of @p ncname valid bytes.
Radek Krejcib1646a92018-11-02 16:08:26 +01002599 */
Radek Krejcid4270262019-01-07 15:07:25 +01002600static long int
Radek Krejcib1646a92018-11-02 16:08:26 +01002601parse_ncname(const char *ncname)
2602{
Radek Krejci1deb5be2020-08-26 16:43:36 +02002603 uint32_t uc;
Radek Krejcid4270262019-01-07 15:07:25 +01002604 size_t size;
2605 long int len = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01002606
2607 LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), 0);
2608 if (!is_xmlqnamestartchar(uc) || (uc == ':')) {
2609 return len;
2610 }
2611
2612 do {
2613 len += size;
Radek Krejci9a564c92019-01-07 14:53:57 +01002614 if (!*ncname) {
2615 break;
2616 }
Radek Krejcid4270262019-01-07 15:07:25 +01002617 LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), -len);
Radek Krejcib1646a92018-11-02 16:08:26 +01002618 } while (is_xmlqnamechar(uc) && (uc != ':'));
2619
2620 return len;
2621}
2622
2623/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02002624 * @brief Add @p token into the expression @p exp.
Radek Krejcib1646a92018-11-02 16:08:26 +01002625 *
Michal Vasko03ff5a72019-09-11 13:49:33 +02002626 * @param[in] ctx Context for logging.
Radek Krejcib1646a92018-11-02 16:08:26 +01002627 * @param[in] exp Expression to use.
2628 * @param[in] token Token to add.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002629 * @param[in] tok_pos Token position in the XPath expression.
Radek Krejcib1646a92018-11-02 16:08:26 +01002630 * @param[in] tok_len Token length in the XPath expression.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002631 * @return LY_ERR
Radek Krejcib1646a92018-11-02 16:08:26 +01002632 */
2633static LY_ERR
Michal Vasko14676352020-05-29 11:35:55 +02002634exp_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 +01002635{
2636 uint32_t prev;
2637
2638 if (exp->used == exp->size) {
2639 prev = exp->size;
2640 exp->size += LYXP_EXPR_SIZE_STEP;
2641 if (prev > exp->size) {
2642 LOGINT(ctx);
2643 return LY_EINT;
2644 }
2645
2646 exp->tokens = ly_realloc(exp->tokens, exp->size * sizeof *exp->tokens);
2647 LY_CHECK_ERR_RET(!exp->tokens, LOGMEM(ctx), LY_EMEM);
2648 exp->tok_pos = ly_realloc(exp->tok_pos, exp->size * sizeof *exp->tok_pos);
2649 LY_CHECK_ERR_RET(!exp->tok_pos, LOGMEM(ctx), LY_EMEM);
2650 exp->tok_len = ly_realloc(exp->tok_len, exp->size * sizeof *exp->tok_len);
2651 LY_CHECK_ERR_RET(!exp->tok_len, LOGMEM(ctx), LY_EMEM);
2652 }
2653
2654 exp->tokens[exp->used] = token;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002655 exp->tok_pos[exp->used] = tok_pos;
Radek Krejcib1646a92018-11-02 16:08:26 +01002656 exp->tok_len[exp->used] = tok_len;
2657 ++exp->used;
2658 return LY_SUCCESS;
2659}
2660
2661void
Michal Vasko14676352020-05-29 11:35:55 +02002662lyxp_expr_free(const struct ly_ctx *ctx, struct lyxp_expr *expr)
Radek Krejcib1646a92018-11-02 16:08:26 +01002663{
2664 uint16_t i;
2665
2666 if (!expr) {
2667 return;
2668 }
2669
2670 lydict_remove(ctx, expr->expr);
2671 free(expr->tokens);
2672 free(expr->tok_pos);
2673 free(expr->tok_len);
2674 if (expr->repeat) {
2675 for (i = 0; i < expr->used; ++i) {
2676 free(expr->repeat[i]);
2677 }
2678 }
2679 free(expr->repeat);
2680 free(expr);
2681}
2682
Radek Krejcif03a9e22020-09-18 20:09:31 +02002683LY_ERR
2684lyxp_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 +01002685{
Radek Krejcif03a9e22020-09-18 20:09:31 +02002686 LY_ERR ret = LY_SUCCESS;
2687 struct lyxp_expr *expr;
Radek Krejcid4270262019-01-07 15:07:25 +01002688 size_t parsed = 0, tok_len;
Radek Krejcib1646a92018-11-02 16:08:26 +01002689 enum lyxp_token tok_type;
Radek Krejci857189e2020-09-01 13:26:36 +02002690 ly_bool prev_function_check = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002691 uint16_t tok_idx = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01002692
Radek Krejcif03a9e22020-09-18 20:09:31 +02002693 assert(expr_p);
2694
2695 if (!expr_str[0]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002696 LOGVAL(ctx, LY_VCODE_XP_EOF);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002697 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02002698 }
2699
2700 if (!expr_len) {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002701 expr_len = strlen(expr_str);
Michal Vasko004d3152020-06-11 19:59:22 +02002702 }
2703 if (expr_len > UINT16_MAX) {
Michal Vasko623ac7b2021-04-09 12:44:23 +02002704 LOGVAL(ctx, LYVE_XPATH, "XPath expression cannot be longer than %u characters.", UINT16_MAX);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002705 return LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01002706 }
2707
2708 /* init lyxp_expr structure */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002709 expr = calloc(1, sizeof *expr);
2710 LY_CHECK_ERR_GOTO(!expr, LOGMEM(ctx); ret = LY_EMEM, error);
2711 LY_CHECK_GOTO(ret = lydict_insert(ctx, expr_str, expr_len, &expr->expr), error);
2712 expr->used = 0;
2713 expr->size = LYXP_EXPR_SIZE_START;
2714 expr->tokens = malloc(expr->size * sizeof *expr->tokens);
2715 LY_CHECK_ERR_GOTO(!expr->tokens, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002716
Radek Krejcif03a9e22020-09-18 20:09:31 +02002717 expr->tok_pos = malloc(expr->size * sizeof *expr->tok_pos);
2718 LY_CHECK_ERR_GOTO(!expr->tok_pos, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002719
Radek Krejcif03a9e22020-09-18 20:09:31 +02002720 expr->tok_len = malloc(expr->size * sizeof *expr->tok_len);
2721 LY_CHECK_ERR_GOTO(!expr->tok_len, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002722
Michal Vasko004d3152020-06-11 19:59:22 +02002723 /* make expr 0-terminated */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002724 expr_str = expr->expr;
Michal Vasko004d3152020-06-11 19:59:22 +02002725
Radek Krejcif03a9e22020-09-18 20:09:31 +02002726 while (is_xmlws(expr_str[parsed])) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002727 ++parsed;
2728 }
2729
2730 do {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002731 if (expr_str[parsed] == '(') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002732
2733 /* '(' */
2734 tok_len = 1;
2735 tok_type = LYXP_TOKEN_PAR1;
2736
Radek Krejcif03a9e22020-09-18 20:09:31 +02002737 if (prev_function_check && expr->used && (expr->tokens[expr->used - 1] == LYXP_TOKEN_NAMETEST)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002738 /* it is a NodeType/FunctionName after all */
Michal Vasko69730152020-10-09 16:30:07 +02002739 if (((expr->tok_len[expr->used - 1] == 4) &&
2740 (!strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "node", 4) ||
2741 !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "text", 4))) ||
2742 ((expr->tok_len[expr->used - 1] == 7) &&
2743 !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "comment", 7))) {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002744 expr->tokens[expr->used - 1] = LYXP_TOKEN_NODETYPE;
Radek Krejcib1646a92018-11-02 16:08:26 +01002745 } else {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002746 expr->tokens[expr->used - 1] = LYXP_TOKEN_FUNCNAME;
Radek Krejcib1646a92018-11-02 16:08:26 +01002747 }
2748 prev_function_check = 0;
2749 }
2750
Radek Krejcif03a9e22020-09-18 20:09:31 +02002751 } else if (expr_str[parsed] == ')') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002752
2753 /* ')' */
2754 tok_len = 1;
2755 tok_type = LYXP_TOKEN_PAR2;
2756
Radek Krejcif03a9e22020-09-18 20:09:31 +02002757 } else if (expr_str[parsed] == '[') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002758
2759 /* '[' */
2760 tok_len = 1;
2761 tok_type = LYXP_TOKEN_BRACK1;
2762
Radek Krejcif03a9e22020-09-18 20:09:31 +02002763 } else if (expr_str[parsed] == ']') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002764
2765 /* ']' */
2766 tok_len = 1;
2767 tok_type = LYXP_TOKEN_BRACK2;
2768
Radek Krejcif03a9e22020-09-18 20:09:31 +02002769 } else if (!strncmp(&expr_str[parsed], "..", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002770
2771 /* '..' */
2772 tok_len = 2;
2773 tok_type = LYXP_TOKEN_DDOT;
2774
Radek Krejcif03a9e22020-09-18 20:09:31 +02002775 } else if ((expr_str[parsed] == '.') && (!isdigit(expr_str[parsed + 1]))) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002776
2777 /* '.' */
2778 tok_len = 1;
2779 tok_type = LYXP_TOKEN_DOT;
2780
Radek Krejcif03a9e22020-09-18 20:09:31 +02002781 } else if (expr_str[parsed] == '@') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002782
2783 /* '@' */
2784 tok_len = 1;
2785 tok_type = LYXP_TOKEN_AT;
2786
Radek Krejcif03a9e22020-09-18 20:09:31 +02002787 } else if (expr_str[parsed] == ',') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002788
2789 /* ',' */
2790 tok_len = 1;
2791 tok_type = LYXP_TOKEN_COMMA;
2792
Radek Krejcif03a9e22020-09-18 20:09:31 +02002793 } else if (expr_str[parsed] == '\'') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002794
2795 /* Literal with ' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002796 for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\''); ++tok_len) {}
2797 LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0',
Radek Krejci2efc45b2020-12-22 16:25:44 +01002798 LOGVAL(ctx, LY_VCODE_XP_EOE, expr_str[parsed], &expr_str[parsed]); ret = LY_EVALID,
Michal Vasko69730152020-10-09 16:30:07 +02002799 error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002800 ++tok_len;
2801 tok_type = LYXP_TOKEN_LITERAL;
2802
Radek Krejcif03a9e22020-09-18 20:09:31 +02002803 } else if (expr_str[parsed] == '\"') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002804
2805 /* Literal with " */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002806 for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\"'); ++tok_len) {}
2807 LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0',
Radek Krejci2efc45b2020-12-22 16:25:44 +01002808 LOGVAL(ctx, LY_VCODE_XP_EOE, expr_str[parsed], &expr_str[parsed]); ret = LY_EVALID,
Michal Vasko69730152020-10-09 16:30:07 +02002809 error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002810 ++tok_len;
2811 tok_type = LYXP_TOKEN_LITERAL;
2812
Radek Krejcif03a9e22020-09-18 20:09:31 +02002813 } else if ((expr_str[parsed] == '.') || (isdigit(expr_str[parsed]))) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002814
2815 /* Number */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002816 for (tok_len = 0; isdigit(expr_str[parsed + tok_len]); ++tok_len) {}
2817 if (expr_str[parsed + tok_len] == '.') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002818 ++tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02002819 for ( ; isdigit(expr_str[parsed + tok_len]); ++tok_len) {}
Radek Krejcib1646a92018-11-02 16:08:26 +01002820 }
2821 tok_type = LYXP_TOKEN_NUMBER;
2822
aPiecekfba75362021-10-07 12:39:48 +02002823 } else if (expr_str[parsed] == '$') {
2824
2825 /* VariableReference */
2826 parsed++;
2827 long int ncname_len = parse_ncname(&expr_str[parsed]);
2828 LY_CHECK_ERR_GOTO(ncname_len < 1, LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed - ncname_len],
2829 parsed - ncname_len + 1, expr_str); ret = LY_EVALID, error);
2830 tok_len = ncname_len;
2831 LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == ':',
2832 LOGVAL(ctx, LYVE_XPATH, "Variable with prefix is not supported."); ret = LY_EVALID,
2833 error);
2834 tok_type = LYXP_TOKEN_VARREF;
2835
Radek Krejcif03a9e22020-09-18 20:09:31 +02002836 } else if (expr_str[parsed] == '/') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002837
2838 /* Operator '/', '//' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002839 if (!strncmp(&expr_str[parsed], "//", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002840 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002841 tok_type = LYXP_TOKEN_OPER_RPATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002842 } else {
2843 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002844 tok_type = LYXP_TOKEN_OPER_PATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002845 }
Radek Krejcib1646a92018-11-02 16:08:26 +01002846
Radek Krejcif03a9e22020-09-18 20:09:31 +02002847 } else if (!strncmp(&expr_str[parsed], "!=", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002848
Michal Vasko3e48bf32020-06-01 08:39:07 +02002849 /* Operator '!=' */
Radek Krejcib1646a92018-11-02 16:08:26 +01002850 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002851 tok_type = LYXP_TOKEN_OPER_NEQUAL;
2852
Radek Krejcif03a9e22020-09-18 20:09:31 +02002853 } else if (!strncmp(&expr_str[parsed], "<=", 2) || !strncmp(&expr_str[parsed], ">=", 2)) {
Michal Vasko3e48bf32020-06-01 08:39:07 +02002854
2855 /* Operator '<=', '>=' */
2856 tok_len = 2;
2857 tok_type = LYXP_TOKEN_OPER_COMP;
Radek Krejcib1646a92018-11-02 16:08:26 +01002858
Radek Krejcif03a9e22020-09-18 20:09:31 +02002859 } else if (expr_str[parsed] == '|') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002860
2861 /* Operator '|' */
2862 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002863 tok_type = LYXP_TOKEN_OPER_UNI;
Radek Krejcib1646a92018-11-02 16:08:26 +01002864
Radek Krejcif03a9e22020-09-18 20:09:31 +02002865 } else if ((expr_str[parsed] == '+') || (expr_str[parsed] == '-')) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002866
2867 /* Operator '+', '-' */
2868 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002869 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002870
Radek Krejcif03a9e22020-09-18 20:09:31 +02002871 } else if (expr_str[parsed] == '=') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002872
Michal Vasko3e48bf32020-06-01 08:39:07 +02002873 /* Operator '=' */
Radek Krejcib1646a92018-11-02 16:08:26 +01002874 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002875 tok_type = LYXP_TOKEN_OPER_EQUAL;
2876
Radek Krejcif03a9e22020-09-18 20:09:31 +02002877 } else if ((expr_str[parsed] == '<') || (expr_str[parsed] == '>')) {
Michal Vasko3e48bf32020-06-01 08:39:07 +02002878
2879 /* Operator '<', '>' */
2880 tok_len = 1;
2881 tok_type = LYXP_TOKEN_OPER_COMP;
Radek Krejcib1646a92018-11-02 16:08:26 +01002882
Michal Vasko69730152020-10-09 16:30:07 +02002883 } else if (expr->used && (expr->tokens[expr->used - 1] != LYXP_TOKEN_AT) &&
2884 (expr->tokens[expr->used - 1] != LYXP_TOKEN_PAR1) &&
2885 (expr->tokens[expr->used - 1] != LYXP_TOKEN_BRACK1) &&
2886 (expr->tokens[expr->used - 1] != LYXP_TOKEN_COMMA) &&
2887 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_LOG) &&
2888 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_EQUAL) &&
2889 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_NEQUAL) &&
2890 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_COMP) &&
2891 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_MATH) &&
2892 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_UNI) &&
2893 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_PATH) &&
2894 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_RPATH)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002895
2896 /* Operator '*', 'or', 'and', 'mod', or 'div' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002897 if (expr_str[parsed] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002898 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002899 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002900
Radek Krejcif03a9e22020-09-18 20:09:31 +02002901 } else if (!strncmp(&expr_str[parsed], "or", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002902 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002903 tok_type = LYXP_TOKEN_OPER_LOG;
Radek Krejcib1646a92018-11-02 16:08:26 +01002904
Radek Krejcif03a9e22020-09-18 20:09:31 +02002905 } else if (!strncmp(&expr_str[parsed], "and", 3)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002906 tok_len = 3;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002907 tok_type = LYXP_TOKEN_OPER_LOG;
Radek Krejcib1646a92018-11-02 16:08:26 +01002908
Radek Krejcif03a9e22020-09-18 20:09:31 +02002909 } else if (!strncmp(&expr_str[parsed], "mod", 3) || !strncmp(&expr_str[parsed], "div", 3)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002910 tok_len = 3;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002911 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002912
2913 } else if (prev_function_check) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002914 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 +02002915 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 +02002916 ret = LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01002917 goto error;
2918 } else {
Michal Vasko774ce402021-04-14 15:35:06 +02002919 LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed], parsed + 1, expr_str);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002920 ret = LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01002921 goto error;
2922 }
Radek Krejcif03a9e22020-09-18 20:09:31 +02002923 } else if (expr_str[parsed] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002924
2925 /* NameTest '*' */
2926 tok_len = 1;
2927 tok_type = LYXP_TOKEN_NAMETEST;
2928
2929 } else {
2930
2931 /* NameTest (NCName ':' '*' | QName) or NodeType/FunctionName */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002932 long int ncname_len = parse_ncname(&expr_str[parsed]);
Michal Vaskoe2be5462021-08-04 10:49:42 +02002933 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 +02002934 parsed - ncname_len + 1, expr_str); ret = LY_EVALID, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002935 tok_len = ncname_len;
2936
Radek Krejcif03a9e22020-09-18 20:09:31 +02002937 if (expr_str[parsed + tok_len] == ':') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002938 ++tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02002939 if (expr_str[parsed + tok_len] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002940 ++tok_len;
2941 } else {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002942 ncname_len = parse_ncname(&expr_str[parsed + tok_len]);
Michal Vaskoe2be5462021-08-04 10:49:42 +02002943 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 +02002944 parsed - ncname_len + 1, expr_str); ret = LY_EVALID, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002945 tok_len += ncname_len;
2946 }
2947 /* remove old flag to prevent ambiguities */
2948 prev_function_check = 0;
2949 tok_type = LYXP_TOKEN_NAMETEST;
2950 } else {
2951 /* there is no prefix so it can still be NodeType/FunctionName, we can't finally decide now */
2952 prev_function_check = 1;
2953 tok_type = LYXP_TOKEN_NAMETEST;
2954 }
2955 }
2956
2957 /* store the token, move on to the next one */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002958 LY_CHECK_GOTO(ret = exp_add_token(ctx, expr, tok_type, parsed, tok_len), error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002959 parsed += tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02002960 while (is_xmlws(expr_str[parsed])) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002961 ++parsed;
2962 }
2963
Radek Krejcif03a9e22020-09-18 20:09:31 +02002964 } while (expr_str[parsed]);
Radek Krejcib1646a92018-11-02 16:08:26 +01002965
Michal Vasko004d3152020-06-11 19:59:22 +02002966 if (reparse) {
2967 /* prealloc repeat */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002968 expr->repeat = calloc(expr->size, sizeof *expr->repeat);
2969 LY_CHECK_ERR_GOTO(!expr->repeat, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002970
Michal Vasko004d3152020-06-11 19:59:22 +02002971 /* fill repeat */
aPiecekbf968d92021-05-27 14:35:05 +02002972 LY_CHECK_ERR_GOTO(reparse_or_expr(ctx, expr, &tok_idx, 0), ret = LY_EVALID, error);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002973 if (expr->used > tok_idx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002974 LOGVAL(ctx, LYVE_XPATH, "Unparsed characters \"%s\" left at the end of an XPath expression.",
Michal Vasko69730152020-10-09 16:30:07 +02002975 &expr->expr[expr->tok_pos[tok_idx]]);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002976 ret = LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02002977 goto error;
2978 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02002979 }
2980
Radek Krejcif03a9e22020-09-18 20:09:31 +02002981 print_expr_struct_debug(expr);
2982 *expr_p = expr;
2983 return LY_SUCCESS;
Radek Krejcib1646a92018-11-02 16:08:26 +01002984
2985error:
Radek Krejcif03a9e22020-09-18 20:09:31 +02002986 lyxp_expr_free(ctx, expr);
2987 return ret;
Radek Krejcib1646a92018-11-02 16:08:26 +01002988}
2989
Michal Vasko1734be92020-09-22 08:55:10 +02002990LY_ERR
2991lyxp_expr_dup(const struct ly_ctx *ctx, const struct lyxp_expr *exp, struct lyxp_expr **dup_p)
Michal Vasko004d3152020-06-11 19:59:22 +02002992{
Michal Vasko1734be92020-09-22 08:55:10 +02002993 LY_ERR ret = LY_SUCCESS;
2994 struct lyxp_expr *dup = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02002995 uint32_t i, j;
2996
Michal Vasko7f45cf22020-10-01 12:49:44 +02002997 if (!exp) {
2998 goto cleanup;
2999 }
3000
Michal Vasko004d3152020-06-11 19:59:22 +02003001 dup = calloc(1, sizeof *dup);
Michal Vasko1734be92020-09-22 08:55:10 +02003002 LY_CHECK_ERR_GOTO(!dup, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02003003
Michal Vasko08e9b112021-06-11 15:41:17 +02003004 if (exp->used) {
3005 dup->tokens = malloc(exp->used * sizeof *dup->tokens);
3006 LY_CHECK_ERR_GOTO(!dup->tokens, LOGMEM(ctx); ret = LY_EMEM, cleanup);
3007 memcpy(dup->tokens, exp->tokens, exp->used * sizeof *dup->tokens);
Michal Vasko004d3152020-06-11 19:59:22 +02003008
Michal Vasko08e9b112021-06-11 15:41:17 +02003009 dup->tok_pos = malloc(exp->used * sizeof *dup->tok_pos);
3010 LY_CHECK_ERR_GOTO(!dup->tok_pos, LOGMEM(ctx); ret = LY_EMEM, cleanup);
3011 memcpy(dup->tok_pos, exp->tok_pos, exp->used * sizeof *dup->tok_pos);
Michal Vasko004d3152020-06-11 19:59:22 +02003012
Michal Vasko08e9b112021-06-11 15:41:17 +02003013 dup->tok_len = malloc(exp->used * sizeof *dup->tok_len);
3014 LY_CHECK_ERR_GOTO(!dup->tok_len, LOGMEM(ctx); ret = LY_EMEM, cleanup);
3015 memcpy(dup->tok_len, exp->tok_len, exp->used * sizeof *dup->tok_len);
Michal Vasko004d3152020-06-11 19:59:22 +02003016
Michal Vasko08e9b112021-06-11 15:41:17 +02003017 dup->repeat = malloc(exp->used * sizeof *dup->repeat);
3018 LY_CHECK_ERR_GOTO(!dup->repeat, LOGMEM(ctx); ret = LY_EMEM, cleanup);
3019 for (i = 0; i < exp->used; ++i) {
3020 if (!exp->repeat[i]) {
3021 dup->repeat[i] = NULL;
3022 } else {
3023 for (j = 0; exp->repeat[i][j]; ++j) {}
3024 /* the ending 0 as well */
3025 ++j;
Michal Vasko004d3152020-06-11 19:59:22 +02003026
Michal Vasko08e9b112021-06-11 15:41:17 +02003027 dup->repeat[i] = malloc(j * sizeof **dup->repeat);
3028 LY_CHECK_ERR_GOTO(!dup->repeat[i], LOGMEM(ctx); ret = LY_EMEM, cleanup);
3029 memcpy(dup->repeat[i], exp->repeat[i], j * sizeof **dup->repeat);
3030 dup->repeat[i][j - 1] = 0;
3031 }
Michal Vasko004d3152020-06-11 19:59:22 +02003032 }
3033 }
3034
3035 dup->used = exp->used;
3036 dup->size = exp->used;
Michal Vasko1734be92020-09-22 08:55:10 +02003037 LY_CHECK_GOTO(ret = lydict_insert(ctx, exp->expr, 0, &dup->expr), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02003038
Michal Vasko1734be92020-09-22 08:55:10 +02003039cleanup:
3040 if (ret) {
3041 lyxp_expr_free(ctx, dup);
3042 } else {
3043 *dup_p = dup;
3044 }
3045 return ret;
Michal Vasko004d3152020-06-11 19:59:22 +02003046}
3047
Michal Vasko03ff5a72019-09-11 13:49:33 +02003048/**
3049 * @brief Get the last-added schema node that is currently in the context.
3050 *
3051 * @param[in] set Set to search in.
3052 * @return Last-added schema context node, NULL if no node is in context.
3053 */
3054static struct lysc_node *
3055warn_get_scnode_in_ctx(struct lyxp_set *set)
3056{
3057 uint32_t i;
3058
3059 if (!set || (set->type != LYXP_SET_SCNODE_SET)) {
3060 return NULL;
3061 }
3062
3063 i = set->used;
3064 do {
3065 --i;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003066 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003067 /* if there are more, simply return the first found (last added) */
3068 return set->val.scnodes[i].scnode;
3069 }
3070 } while (i);
3071
3072 return NULL;
3073}
3074
3075/**
3076 * @brief Test whether a type is numeric - integer type or decimal64.
3077 *
3078 * @param[in] type Type to test.
Radek Krejci857189e2020-09-01 13:26:36 +02003079 * @return Boolean value whether @p type is numeric type or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003080 */
Radek Krejci857189e2020-09-01 13:26:36 +02003081static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003082warn_is_numeric_type(struct lysc_type *type)
3083{
3084 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003085 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003086 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003087
3088 switch (type->basetype) {
3089 case LY_TYPE_DEC64:
3090 case LY_TYPE_INT8:
3091 case LY_TYPE_UINT8:
3092 case LY_TYPE_INT16:
3093 case LY_TYPE_UINT16:
3094 case LY_TYPE_INT32:
3095 case LY_TYPE_UINT32:
3096 case LY_TYPE_INT64:
3097 case LY_TYPE_UINT64:
3098 return 1;
3099 case LY_TYPE_UNION:
3100 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003101 LY_ARRAY_FOR(uni->types, u) {
3102 ret = warn_is_numeric_type(uni->types[u]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003103 if (ret) {
3104 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003105 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003106 }
3107 }
3108 /* did not find any suitable type */
3109 return 0;
3110 case LY_TYPE_LEAFREF:
3111 return warn_is_numeric_type(((struct lysc_type_leafref *)type)->realtype);
3112 default:
3113 return 0;
3114 }
3115}
3116
3117/**
3118 * @brief Test whether a type is string-like - no integers, decimal64 or binary.
3119 *
3120 * @param[in] type Type to test.
Radek Krejci857189e2020-09-01 13:26:36 +02003121 * @return Boolean value whether @p type's basetype is string type or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003122 */
Radek Krejci857189e2020-09-01 13:26:36 +02003123static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003124warn_is_string_type(struct lysc_type *type)
3125{
3126 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003127 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003128 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003129
3130 switch (type->basetype) {
3131 case LY_TYPE_BITS:
3132 case LY_TYPE_ENUM:
3133 case LY_TYPE_IDENT:
3134 case LY_TYPE_INST:
3135 case LY_TYPE_STRING:
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_string_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_string_type(((struct lysc_type_leafref *)type)->realtype);
3150 default:
3151 return 0;
3152 }
3153}
3154
3155/**
3156 * @brief Test whether a type is one specific type.
3157 *
3158 * @param[in] type Type to test.
3159 * @param[in] base Expected type.
Radek Krejci857189e2020-09-01 13:26:36 +02003160 * @return Boolean value whether the given @p type is of the specific basetype @p base.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003161 */
Radek Krejci857189e2020-09-01 13:26:36 +02003162static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003163warn_is_specific_type(struct lysc_type *type, LY_DATA_TYPE base)
3164{
3165 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003166 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003167 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003168
3169 if (type->basetype == base) {
3170 return 1;
3171 } else if (type->basetype == LY_TYPE_UNION) {
3172 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003173 LY_ARRAY_FOR(uni->types, u) {
3174 ret = warn_is_specific_type(uni->types[u], base);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003175 if (ret) {
3176 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003177 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003178 }
3179 }
3180 /* did not find any suitable type */
3181 return 0;
3182 } else if (type->basetype == LY_TYPE_LEAFREF) {
3183 return warn_is_specific_type(((struct lysc_type_leafref *)type)->realtype, base);
3184 }
3185
3186 return 0;
3187}
3188
3189/**
3190 * @brief Get next type of a (union) type.
3191 *
3192 * @param[in] type Base type.
3193 * @param[in] prev_type Previously returned type.
3194 * @return Next type or NULL.
3195 */
3196static struct lysc_type *
3197warn_is_equal_type_next_type(struct lysc_type *type, struct lysc_type *prev_type)
3198{
3199 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003200 ly_bool found = 0;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003201 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003202
3203 switch (type->basetype) {
3204 case LY_TYPE_UNION:
3205 uni = (struct lysc_type_union *)type;
3206 if (!prev_type) {
3207 return uni->types[0];
3208 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003209 LY_ARRAY_FOR(uni->types, u) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003210 if (found) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003211 return uni->types[u];
Michal Vasko03ff5a72019-09-11 13:49:33 +02003212 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003213 if (prev_type == uni->types[u]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003214 found = 1;
3215 }
3216 }
3217 return NULL;
3218 default:
3219 if (prev_type) {
3220 assert(type == prev_type);
3221 return NULL;
3222 } else {
3223 return type;
3224 }
3225 }
3226}
3227
3228/**
3229 * @brief Test whether 2 types have a common type.
3230 *
3231 * @param[in] type1 First type.
3232 * @param[in] type2 Second type.
3233 * @return 1 if they do, 0 otherwise.
3234 */
3235static int
3236warn_is_equal_type(struct lysc_type *type1, struct lysc_type *type2)
3237{
3238 struct lysc_type *t1, *rt1, *t2, *rt2;
3239
3240 t1 = NULL;
3241 while ((t1 = warn_is_equal_type_next_type(type1, t1))) {
3242 if (t1->basetype == LY_TYPE_LEAFREF) {
3243 rt1 = ((struct lysc_type_leafref *)t1)->realtype;
3244 } else {
3245 rt1 = t1;
3246 }
3247
3248 t2 = NULL;
3249 while ((t2 = warn_is_equal_type_next_type(type2, t2))) {
3250 if (t2->basetype == LY_TYPE_LEAFREF) {
3251 rt2 = ((struct lysc_type_leafref *)t2)->realtype;
3252 } else {
3253 rt2 = t2;
3254 }
3255
3256 if (rt2->basetype == rt1->basetype) {
3257 /* match found */
3258 return 1;
3259 }
3260 }
3261 }
3262
3263 return 0;
3264}
3265
3266/**
Michal Vaskoaa956522021-11-11 10:45:34 +01003267 * @brief Print warning with information about the XPath subexpression that caused previous warning.
3268 *
3269 * @param[in] ctx Context for logging.
3270 * @param[in] tok_pos Index of the subexpression in the whole expression.
3271 * @param[in] subexpr Subexpression start.
3272 * @param[in] subexpr_len Length of @p subexpr to print.
3273 * @param[in] cur_scnode Expression context node.
3274 */
3275static void
3276warn_subexpr_log(const struct ly_ctx *ctx, uint16_t tok_pos, const char *subexpr, int subexpr_len,
3277 const struct lysc_node *cur_scnode)
3278{
3279 char *path;
3280
3281 path = lysc_path(cur_scnode, LYSC_PATH_LOG, NULL, 0);
3282 LOGWRN(ctx, "Previous warning generated by XPath subexpression[%" PRIu16 "] \"%.*s\" with context node \"%s\".",
3283 tok_pos, subexpr_len, subexpr, path);
3284 free(path);
3285}
3286
3287/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02003288 * @brief Check both operands of comparison operators.
3289 *
3290 * @param[in] ctx Context for errors.
3291 * @param[in] set1 First operand set.
3292 * @param[in] set2 Second operand set.
3293 * @param[in] numbers_only Whether accept only numbers or other types are fine too (for '=' and '!=').
3294 * @param[in] expr Start of the expression to print with the warning.
3295 * @param[in] tok_pos Token position.
3296 */
3297static void
Michal Vaskoaa956522021-11-11 10:45:34 +01003298warn_operands(struct ly_ctx *ctx, struct lyxp_set *set1, struct lyxp_set *set2, ly_bool numbers_only, const char *expr,
3299 uint16_t tok_pos)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003300{
3301 struct lysc_node_leaf *node1, *node2;
Radek Krejci857189e2020-09-01 13:26:36 +02003302 ly_bool leaves = 1, warning = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003303
3304 node1 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set1);
3305 node2 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set2);
3306
3307 if (!node1 && !node2) {
3308 /* no node-sets involved, nothing to do */
3309 return;
3310 }
3311
3312 if (node1) {
3313 if (!(node1->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3314 LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node1->nodetype), node1->name);
3315 warning = 1;
3316 leaves = 0;
3317 } else if (numbers_only && !warn_is_numeric_type(node1->type)) {
3318 LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node1->name);
3319 warning = 1;
3320 }
3321 }
3322
3323 if (node2) {
3324 if (!(node2->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3325 LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node2->nodetype), node2->name);
3326 warning = 1;
3327 leaves = 0;
3328 } else if (numbers_only && !warn_is_numeric_type(node2->type)) {
3329 LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node2->name);
3330 warning = 1;
3331 }
3332 }
3333
3334 if (node1 && node2 && leaves && !numbers_only) {
Michal Vasko69730152020-10-09 16:30:07 +02003335 if ((warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type)) ||
3336 (!warn_is_numeric_type(node1->type) && warn_is_numeric_type(node2->type)) ||
3337 (!warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type) &&
3338 !warn_is_equal_type(node1->type, node2->type))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003339 LOGWRN(ctx, "Incompatible types of operands \"%s\" and \"%s\" for comparison.", node1->name, node2->name);
3340 warning = 1;
3341 }
3342 }
3343
3344 if (warning) {
Michal Vaskoaa956522021-11-11 10:45:34 +01003345 warn_subexpr_log(ctx, tok_pos, expr + tok_pos, 20, set1->cur_scnode);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003346 }
3347}
3348
3349/**
3350 * @brief Check that a value is valid for a leaf. If not applicable, does nothing.
3351 *
3352 * @param[in] exp Parsed XPath expression.
3353 * @param[in] set Set with the leaf/leaf-list.
3354 * @param[in] val_exp Index of the value (literal/number) in @p exp.
3355 * @param[in] equal_exp Index of the start of the equality expression in @p exp.
3356 * @param[in] last_equal_exp Index of the end of the equality expression in @p exp.
3357 */
3358static void
Michal Vasko40308e72020-10-20 16:38:40 +02003359warn_equality_value(const struct lyxp_expr *exp, struct lyxp_set *set, uint16_t val_exp, uint16_t equal_exp,
3360 uint16_t last_equal_exp)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003361{
3362 struct lysc_node *scnode;
3363 struct lysc_type *type;
3364 char *value;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003365 struct lyd_value storage;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003366 LY_ERR rc;
3367 struct ly_err_item *err = NULL;
3368
Michal Vasko69730152020-10-09 16:30:07 +02003369 if ((scnode = warn_get_scnode_in_ctx(set)) && (scnode->nodetype & (LYS_LEAF | LYS_LEAFLIST)) &&
3370 ((exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) || (exp->tokens[val_exp] == LYXP_TOKEN_NUMBER))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003371 /* check that the node can have the specified value */
3372 if (exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) {
3373 value = strndup(exp->expr + exp->tok_pos[val_exp] + 1, exp->tok_len[val_exp] - 2);
3374 } else {
3375 value = strndup(exp->expr + exp->tok_pos[val_exp], exp->tok_len[val_exp]);
3376 }
3377 if (!value) {
3378 LOGMEM(set->ctx);
3379 return;
3380 }
3381
3382 if ((((struct lysc_node_leaf *)scnode)->type->basetype == LY_TYPE_IDENT) && !strchr(value, ':')) {
3383 LOGWRN(set->ctx, "Identityref \"%s\" comparison with identity \"%s\" without prefix, consider adding"
Michal Vasko69730152020-10-09 16:30:07 +02003384 " a prefix or best using \"derived-from(-or-self)()\" functions.", scnode->name, value);
Michal Vaskoaa956522021-11-11 10:45:34 +01003385 warn_subexpr_log(set->ctx, exp->tok_pos[equal_exp], exp->expr + exp->tok_pos[equal_exp],
Radek Krejci0f969882020-08-21 16:56:47 +02003386 (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp],
Michal Vaskoaa956522021-11-11 10:45:34 +01003387 set->cur_scnode);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003388 }
3389
3390 type = ((struct lysc_node_leaf *)scnode)->type;
3391 if (type->basetype != LY_TYPE_IDENT) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003392 rc = type->plugin->store(set->ctx, type, value, strlen(value), 0, set->format, set->prefix_data,
Michal Vasko405cc9e2020-12-01 12:01:27 +01003393 LYD_HINT_DATA, scnode, &storage, NULL, &err);
Michal Vaskobf42e832020-11-23 16:59:42 +01003394 if (rc == LY_EINCOMPLETE) {
3395 rc = LY_SUCCESS;
3396 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003397
3398 if (err) {
3399 LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type (%s).", value, err->msg);
3400 ly_err_free(err);
3401 } else if (rc != LY_SUCCESS) {
3402 LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type.", value);
3403 }
3404 if (rc != LY_SUCCESS) {
Michal Vaskoaa956522021-11-11 10:45:34 +01003405 warn_subexpr_log(set->ctx, exp->tok_pos[equal_exp], exp->expr + exp->tok_pos[equal_exp],
Radek Krejci0f969882020-08-21 16:56:47 +02003406 (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp],
Michal Vaskoaa956522021-11-11 10:45:34 +01003407 set->cur_scnode);
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003408 } else {
3409 type->plugin->free(set->ctx, &storage);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003410 }
3411 }
3412 free(value);
3413 }
3414}
3415
3416/*
3417 * XPath functions
3418 */
3419
3420/**
3421 * @brief Execute the YANG 1.1 bit-is-set(node-set, string) function. Returns LYXP_SET_BOOLEAN
3422 * depending on whether the first node bit value from the second argument is set.
3423 *
3424 * @param[in] args Array of arguments.
3425 * @param[in] arg_count Count of elements in @p args.
3426 * @param[in,out] set Context and result set at the same time.
3427 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003428 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003429 */
3430static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003431xpath_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 +02003432{
3433 struct lyd_node_term *leaf;
3434 struct lysc_node_leaf *sleaf;
Michal Vasko2588b952021-07-29 07:43:26 +02003435 struct lyd_value_bits *bits;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003436 LY_ERR rc = LY_SUCCESS;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003437 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003438
3439 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003440 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003441 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003442 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3443 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3444 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3445 sleaf->name);
3446 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_BITS)) {
3447 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"bits\".", __func__, sleaf->name);
3448 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003449 }
3450
3451 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3452 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003453 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3454 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003455 } else if (!warn_is_string_type(sleaf->type)) {
3456 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003457 }
3458 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003459 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003460 return rc;
3461 }
3462
Michal Vaskod3678892020-05-21 10:06:58 +02003463 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003464 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 +02003465 return LY_EVALID;
3466 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003467 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003468 LY_CHECK_RET(rc);
3469
3470 set_fill_boolean(set, 0);
Michal Vaskod3678892020-05-21 10:06:58 +02003471 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003472 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
Michal Vasko2588b952021-07-29 07:43:26 +02003473 if ((leaf->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (leaf->value.realtype->basetype == LY_TYPE_BITS)) {
3474 LYD_VALUE_GET(&leaf->value, bits);
3475 LY_ARRAY_FOR(bits->items, u) {
3476 if (!strcmp(bits->items[u]->name, args[1]->val.str)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003477 set_fill_boolean(set, 1);
3478 break;
3479 }
3480 }
3481 }
3482 }
3483
3484 return LY_SUCCESS;
3485}
3486
3487/**
3488 * @brief Execute the XPath boolean(object) function. Returns LYXP_SET_BOOLEAN
3489 * with the argument converted to boolean.
3490 *
3491 * @param[in] args Array of arguments.
3492 * @param[in] arg_count Count of elements in @p args.
3493 * @param[in,out] set Context and result set at the same time.
3494 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003495 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003496 */
3497static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003498xpath_boolean(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003499{
3500 LY_ERR rc;
3501
3502 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02003503 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003504 return LY_SUCCESS;
3505 }
3506
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003507 rc = lyxp_set_cast(args[0], LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003508 LY_CHECK_RET(rc);
3509 set_fill_set(set, args[0]);
3510
3511 return LY_SUCCESS;
3512}
3513
3514/**
3515 * @brief Execute the XPath ceiling(number) function. Returns LYXP_SET_NUMBER
3516 * with the first argument rounded up to the nearest integer.
3517 *
3518 * @param[in] args Array of arguments.
3519 * @param[in] arg_count Count of elements in @p args.
3520 * @param[in,out] set Context and result set at the same time.
3521 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003522 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003523 */
3524static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003525xpath_ceiling(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003526{
3527 struct lysc_node_leaf *sleaf;
3528 LY_ERR rc = LY_SUCCESS;
3529
3530 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003531 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003532 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003533 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3534 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3535 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3536 sleaf->name);
3537 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
3538 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
3539 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003540 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003541 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003542 return rc;
3543 }
3544
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003545 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003546 LY_CHECK_RET(rc);
3547 if ((long long)args[0]->val.num != args[0]->val.num) {
3548 set_fill_number(set, ((long long)args[0]->val.num) + 1);
3549 } else {
3550 set_fill_number(set, args[0]->val.num);
3551 }
3552
3553 return LY_SUCCESS;
3554}
3555
3556/**
3557 * @brief Execute the XPath concat(string, string, string*) function.
3558 * Returns LYXP_SET_STRING with the concatenation of all the arguments.
3559 *
3560 * @param[in] args Array of arguments.
3561 * @param[in] arg_count Count of elements in @p args.
3562 * @param[in,out] set Context and result set at the same time.
3563 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003564 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003565 */
3566static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003567xpath_concat(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003568{
3569 uint16_t i;
3570 char *str = NULL;
3571 size_t used = 1;
3572 LY_ERR rc = LY_SUCCESS;
3573 struct lysc_node_leaf *sleaf;
3574
3575 if (options & LYXP_SCNODE_ALL) {
3576 for (i = 0; i < arg_count; ++i) {
3577 if ((args[i]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[i]))) {
3578 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3579 LOGWRN(set->ctx, "Argument #%u of %s is a %s node \"%s\".",
Michal Vasko69730152020-10-09 16:30:07 +02003580 i + 1, __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003581 } else if (!warn_is_string_type(sleaf->type)) {
Radek Krejci70124c82020-08-14 22:17:03 +02003582 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 +02003583 }
3584 }
3585 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003586 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003587 return rc;
3588 }
3589
3590 for (i = 0; i < arg_count; ++i) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003591 rc = lyxp_set_cast(args[i], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003592 if (rc != LY_SUCCESS) {
3593 free(str);
3594 return rc;
3595 }
3596
3597 str = ly_realloc(str, (used + strlen(args[i]->val.str)) * sizeof(char));
3598 LY_CHECK_ERR_RET(!str, LOGMEM(set->ctx), LY_EMEM);
3599 strcpy(str + used - 1, args[i]->val.str);
3600 used += strlen(args[i]->val.str);
3601 }
3602
3603 /* free, kind of */
Michal Vaskod3678892020-05-21 10:06:58 +02003604 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003605 set->type = LYXP_SET_STRING;
3606 set->val.str = str;
3607
3608 return LY_SUCCESS;
3609}
3610
3611/**
3612 * @brief Execute the XPath contains(string, string) function.
3613 * Returns LYXP_SET_BOOLEAN whether the second argument can
3614 * be found in the first or not.
3615 *
3616 * @param[in] args Array of arguments.
3617 * @param[in] arg_count Count of elements in @p args.
3618 * @param[in,out] set Context and result set at the same time.
3619 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003620 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003621 */
3622static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003623xpath_contains(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003624{
3625 struct lysc_node_leaf *sleaf;
3626 LY_ERR rc = LY_SUCCESS;
3627
3628 if (options & LYXP_SCNODE_ALL) {
3629 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3630 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003631 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3632 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003633 } else if (!warn_is_string_type(sleaf->type)) {
3634 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003635 }
3636 }
3637
3638 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3639 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003640 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3641 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003642 } else if (!warn_is_string_type(sleaf->type)) {
3643 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003644 }
3645 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003646 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003647 return rc;
3648 }
3649
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003650 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003651 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003652 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003653 LY_CHECK_RET(rc);
3654
3655 if (strstr(args[0]->val.str, args[1]->val.str)) {
3656 set_fill_boolean(set, 1);
3657 } else {
3658 set_fill_boolean(set, 0);
3659 }
3660
3661 return LY_SUCCESS;
3662}
3663
3664/**
3665 * @brief Execute the XPath count(node-set) function. Returns LYXP_SET_NUMBER
3666 * with the size of the node-set from the argument.
3667 *
3668 * @param[in] args Array of arguments.
3669 * @param[in] arg_count Count of elements in @p args.
3670 * @param[in,out] set Context and result set at the same time.
3671 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003672 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003673 */
3674static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003675xpath_count(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003676{
Michal Vasko03ff5a72019-09-11 13:49:33 +02003677 LY_ERR rc = LY_SUCCESS;
3678
3679 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003680 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003681 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003682 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003683 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003684 return rc;
3685 }
3686
Michal Vasko03ff5a72019-09-11 13:49:33 +02003687 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003688 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "count(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003689 return LY_EVALID;
3690 }
3691
3692 set_fill_number(set, args[0]->used);
3693 return LY_SUCCESS;
3694}
3695
3696/**
3697 * @brief Execute the XPath current() function. Returns LYXP_SET_NODE_SET
3698 * with the context with the intial node.
3699 *
3700 * @param[in] args Array of arguments.
3701 * @param[in] arg_count Count of elements in @p args.
3702 * @param[in,out] set Context and result set at the same time.
3703 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003704 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003705 */
3706static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003707xpath_current(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003708{
3709 if (arg_count || args) {
Radek Krejcie87c7dc2021-06-02 21:25:42 +02003710 LOGVAL(set->ctx, LY_VCODE_XP_INARGCOUNT, arg_count, LY_PRI_LENSTR("current()"));
Michal Vasko03ff5a72019-09-11 13:49:33 +02003711 return LY_EVALID;
3712 }
3713
3714 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02003715 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003716
Michal Vasko296dfaf2021-12-13 16:57:42 +01003717 if (set->cur_scnode) {
3718 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, set->cur_scnode, LYXP_NODE_ELEM, NULL));
3719 } else {
3720 /* root node */
3721 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, NULL, set->root_type, NULL));
3722 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003723 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02003724 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003725
Michal Vasko296dfaf2021-12-13 16:57:42 +01003726 if (set->cur_node) {
3727 /* position is filled later */
3728 set_insert_node(set, set->cur_node, 0, LYXP_NODE_ELEM, 0);
3729 } else {
3730 /* root node */
3731 set_insert_node(set, NULL, 0, set->root_type, 0);
3732 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003733 }
3734
3735 return LY_SUCCESS;
3736}
3737
3738/**
3739 * @brief Execute the YANG 1.1 deref(node-set) function. Returns LYXP_SET_NODE_SET with either
3740 * leafref or instance-identifier target node(s).
3741 *
3742 * @param[in] args Array of arguments.
3743 * @param[in] arg_count Count of elements in @p args.
3744 * @param[in,out] set Context and result set at the same time.
3745 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003746 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003747 */
3748static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003749xpath_deref(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003750{
3751 struct lyd_node_term *leaf;
Michal Vasko42e497c2020-01-06 08:38:25 +01003752 struct lysc_node_leaf *sleaf = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02003753 struct lysc_type_leafref *lref;
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003754 const struct lysc_node *target;
Michal Vasko004d3152020-06-11 19:59:22 +02003755 struct ly_path *p;
3756 struct lyd_node *node;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003757 char *errmsg = NULL;
Michal Vasko00cbf532020-06-15 13:58:47 +02003758 uint8_t oper;
Michal Vasko741bb562021-06-24 11:59:50 +02003759 LY_ERR r;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003760
3761 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003762 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003763 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003764 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
Michal Vasko423ba3f2021-07-19 13:08:50 +02003765 if (!(sleaf->nodetype & LYD_NODE_TERM)) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003766 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3767 sleaf->name);
Michal Vaskoed725d72021-06-23 12:03:45 +02003768 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_LEAFREF) &&
3769 !warn_is_specific_type(sleaf->type, LY_TYPE_INST)) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003770 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"leafref\" nor \"instance-identifier\".",
3771 __func__, sleaf->name);
3772 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003773 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003774 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko423ba3f2021-07-19 13:08:50 +02003775 if (sleaf && (sleaf->nodetype & LYD_NODE_TERM) && (sleaf->type->basetype == LY_TYPE_LEAFREF)) {
Michal Vasko004d3152020-06-11 19:59:22 +02003776 lref = (struct lysc_type_leafref *)sleaf->type;
Michal Vaskod1e53b92021-01-28 13:11:06 +01003777 oper = (sleaf->flags & LYS_IS_OUTPUT) ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT;
Michal Vasko004d3152020-06-11 19:59:22 +02003778
3779 /* it was already evaluated on schema, it must succeed */
Michal Vasko741bb562021-06-24 11:59:50 +02003780 r = ly_path_compile_leafref(set->ctx, &sleaf->node, NULL, lref->path, oper, LY_PATH_TARGET_MANY,
Michal Vasko24fc4d12021-07-12 14:41:20 +02003781 LY_VALUE_SCHEMA_RESOLVED, lref->prefixes, &p);
Michal Vasko741bb562021-06-24 11:59:50 +02003782 if (!r) {
3783 /* get the target node */
3784 target = p[LY_ARRAY_COUNT(p) - 1].node;
3785 ly_path_free(set->ctx, p);
Michal Vasko004d3152020-06-11 19:59:22 +02003786
Michal Vasko741bb562021-06-24 11:59:50 +02003787 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, target, LYXP_NODE_ELEM, NULL));
3788 } /* else the target was found before but is disabled so it was removed */
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003789 }
3790
Michal Vasko741bb562021-06-24 11:59:50 +02003791 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003792 }
3793
Michal Vaskod3678892020-05-21 10:06:58 +02003794 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003795 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "deref(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003796 return LY_EVALID;
3797 }
3798
Michal Vaskod3678892020-05-21 10:06:58 +02003799 lyxp_set_free_content(set);
3800 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003801 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
3802 sleaf = (struct lysc_node_leaf *)leaf->schema;
3803 if (sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
3804 if (sleaf->type->basetype == LY_TYPE_LEAFREF) {
3805 /* find leafref target */
Radek Krejci0b013302021-03-29 15:22:32 +02003806 if (lyplg_type_resolve_leafref((struct lysc_type_leafref *)sleaf->type, &leaf->node, &leaf->value, set->tree,
Michal Vasko9e685082021-01-29 14:49:09 +01003807 &node, &errmsg)) {
Michal Vasko004d3152020-06-11 19:59:22 +02003808 LOGERR(set->ctx, LY_EVALID, errmsg);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003809 free(errmsg);
Michal Vasko004d3152020-06-11 19:59:22 +02003810 return LY_EVALID;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003811 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003812 } else {
3813 assert(sleaf->type->basetype == LY_TYPE_INST);
Michal Vasko004d3152020-06-11 19:59:22 +02003814 if (ly_path_eval(leaf->value.target, set->tree, &node)) {
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003815 LOGERR(set->ctx, LY_EVALID, "Invalid instance-identifier \"%s\" value - required instance not found.",
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02003816 lyd_get_value(&leaf->node));
Michal Vasko03ff5a72019-09-11 13:49:33 +02003817 return LY_EVALID;
3818 }
3819 }
Michal Vasko004d3152020-06-11 19:59:22 +02003820
3821 /* insert it */
3822 set_insert_node(set, node, 0, LYXP_NODE_ELEM, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003823 }
3824 }
3825
3826 return LY_SUCCESS;
3827}
3828
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003829static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02003830xpath_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 +02003831{
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01003832 uint32_t i;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003833 LY_ARRAY_COUNT_TYPE u;
3834 struct lyd_node_term *leaf;
3835 struct lysc_node_leaf *sleaf;
3836 struct lyd_meta *meta;
Michal Vasko93923692021-05-07 15:28:02 +02003837 struct lyd_value *val;
3838 const struct lys_module *mod;
3839 const char *id_name;
3840 uint16_t id_len;
3841 struct lysc_ident *id;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003842 LY_ERR rc = LY_SUCCESS;
Radek Krejci857189e2020-09-01 13:26:36 +02003843 ly_bool found;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003844
3845 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003846 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003847 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", func);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003848 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3849 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3850 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype),
3851 sleaf->name);
3852 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_IDENT)) {
3853 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"identityref\".", func, sleaf->name);
3854 }
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003855 }
3856
3857 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3858 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3859 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype),
Michal Vasko69730152020-10-09 16:30:07 +02003860 sleaf->name);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003861 } else if (!warn_is_string_type(sleaf->type)) {
3862 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", func, sleaf->name);
3863 }
3864 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003865 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003866 return rc;
3867 }
3868
3869 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003870 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "derived-from(-or-self)(node-set, string)");
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003871 return LY_EVALID;
3872 }
3873 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
3874 LY_CHECK_RET(rc);
3875
Michal Vasko93923692021-05-07 15:28:02 +02003876 /* parse the identity */
3877 id_name = args[1]->val.str;
3878 id_len = strlen(id_name);
3879 rc = moveto_resolve_model(&id_name, &id_len, set, set->cur_node ? set->cur_node->schema : NULL, &mod);
3880 LY_CHECK_RET(rc);
3881 if (!mod) {
3882 LOGVAL(set->ctx, LYVE_XPATH, "Identity \"%.*s\" without a prefix.", (int)id_len, id_name);
3883 return LY_EVALID;
3884 }
3885
3886 /* find the identity */
3887 found = 0;
3888 LY_ARRAY_FOR(mod->identities, u) {
3889 if (!ly_strncmp(mod->identities[u].name, id_name, id_len)) {
3890 /* we have match */
3891 found = 1;
3892 break;
3893 }
3894 }
3895 if (!found) {
3896 LOGVAL(set->ctx, LYVE_XPATH, "Identity \"%.*s\" not found in module \"%s\".", (int)id_len, id_name, mod->name);
3897 return LY_EVALID;
3898 }
3899 id = &mod->identities[u];
3900
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003901 set_fill_boolean(set, 0);
3902 found = 0;
3903 for (i = 0; i < args[0]->used; ++i) {
3904 if ((args[0]->val.nodes[i].type != LYXP_NODE_ELEM) && (args[0]->val.nodes[i].type != LYXP_NODE_META)) {
3905 continue;
3906 }
3907
3908 if (args[0]->val.nodes[i].type == LYXP_NODE_ELEM) {
3909 leaf = (struct lyd_node_term *)args[0]->val.nodes[i].node;
3910 sleaf = (struct lysc_node_leaf *)leaf->schema;
3911 val = &leaf->value;
3912 if (!(sleaf->nodetype & LYD_NODE_TERM) || (leaf->value.realtype->basetype != LY_TYPE_IDENT)) {
3913 /* uninteresting */
3914 continue;
3915 }
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003916 } else {
3917 meta = args[0]->val.meta[i].meta;
3918 val = &meta->value;
3919 if (val->realtype->basetype != LY_TYPE_IDENT) {
3920 /* uninteresting */
3921 continue;
3922 }
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003923 }
3924
Michal Vasko93923692021-05-07 15:28:02 +02003925 /* check the identity itself */
3926 if (self_match && (id == val->ident)) {
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003927 set_fill_boolean(set, 1);
3928 found = 1;
3929 }
Michal Vasko93923692021-05-07 15:28:02 +02003930 if (!found && !lyplg_type_identity_isderived(id, val->ident)) {
3931 set_fill_boolean(set, 1);
3932 found = 1;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003933 }
3934
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003935 if (found) {
3936 break;
3937 }
3938 }
3939
3940 return LY_SUCCESS;
3941}
3942
Michal Vasko03ff5a72019-09-11 13:49:33 +02003943/**
3944 * @brief Execute the YANG 1.1 derived-from(node-set, string) function. Returns LYXP_SET_BOOLEAN depending
3945 * on whether the first argument nodes contain a node of an identity derived from the second
3946 * argument identity.
3947 *
3948 * @param[in] args Array of arguments.
3949 * @param[in] arg_count Count of elements in @p args.
3950 * @param[in,out] set Context and result set at the same time.
3951 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003952 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003953 */
3954static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003955xpath_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 +02003956{
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003957 return xpath_derived_(args, set, options, 0, __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003958}
3959
3960/**
3961 * @brief Execute the YANG 1.1 derived-from-or-self(node-set, string) function. Returns LYXP_SET_BOOLEAN depending
3962 * on whether the first argument nodes contain a node of an identity that either is or is derived from
3963 * the second argument identity.
3964 *
3965 * @param[in] args Array of arguments.
3966 * @param[in] arg_count Count of elements in @p args.
3967 * @param[in,out] set Context and result set at the same time.
3968 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003969 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003970 */
3971static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003972xpath_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 +02003973{
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003974 return xpath_derived_(args, set, options, 1, __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003975}
3976
3977/**
3978 * @brief Execute the YANG 1.1 enum-value(node-set) function. Returns LYXP_SET_NUMBER
3979 * with the integer value of the first node's enum value, otherwise NaN.
3980 *
3981 * @param[in] args Array of arguments.
3982 * @param[in] arg_count Count of elements in @p args.
3983 * @param[in,out] set Context and result set at the same time.
3984 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003985 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003986 */
3987static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003988xpath_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 +02003989{
3990 struct lyd_node_term *leaf;
3991 struct lysc_node_leaf *sleaf;
3992 LY_ERR rc = LY_SUCCESS;
3993
3994 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003995 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003996 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003997 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3998 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3999 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
4000 sleaf->name);
4001 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_ENUM)) {
4002 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"enumeration\".", __func__, sleaf->name);
4003 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004004 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004005 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004006 return rc;
4007 }
4008
Michal Vaskod3678892020-05-21 10:06:58 +02004009 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004010 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "enum-value(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004011 return LY_EVALID;
4012 }
4013
4014 set_fill_number(set, NAN);
Michal Vaskod3678892020-05-21 10:06:58 +02004015 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004016 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
4017 sleaf = (struct lysc_node_leaf *)leaf->schema;
4018 if ((sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (sleaf->type->basetype == LY_TYPE_ENUM)) {
4019 set_fill_number(set, leaf->value.enum_item->value);
4020 }
4021 }
4022
4023 return LY_SUCCESS;
4024}
4025
4026/**
4027 * @brief Execute the XPath false() function. Returns LYXP_SET_BOOLEAN
4028 * with false value.
4029 *
4030 * @param[in] args Array of arguments.
4031 * @param[in] arg_count Count of elements in @p args.
4032 * @param[in,out] set Context and result set at the same time.
4033 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004034 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004035 */
4036static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004037xpath_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 +02004038{
4039 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004040 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004041 return LY_SUCCESS;
4042 }
4043
4044 set_fill_boolean(set, 0);
4045 return LY_SUCCESS;
4046}
4047
4048/**
4049 * @brief Execute the XPath floor(number) function. Returns LYXP_SET_NUMBER
4050 * with the first argument floored (truncated).
4051 *
4052 * @param[in] args Array of arguments.
4053 * @param[in] arg_count Count of elements in @p args.
4054 * @param[in,out] set Context and result set at the same time.
4055 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004056 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004057 */
4058static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004059xpath_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 +02004060{
4061 LY_ERR rc;
4062
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004063 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004064 LY_CHECK_RET(rc);
4065 if (isfinite(args[0]->val.num)) {
4066 set_fill_number(set, (long long)args[0]->val.num);
4067 }
4068
4069 return LY_SUCCESS;
4070}
4071
4072/**
4073 * @brief Execute the XPath lang(string) function. Returns LYXP_SET_BOOLEAN
4074 * whether the language of the text matches the one from the argument.
4075 *
4076 * @param[in] args Array of arguments.
4077 * @param[in] arg_count Count of elements in @p args.
4078 * @param[in,out] set Context and result set at the same time.
4079 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004080 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004081 */
4082static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004083xpath_lang(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004084{
4085 const struct lyd_node *node;
4086 struct lysc_node_leaf *sleaf;
Michal Vasko9f96a052020-03-10 09:41:45 +01004087 struct lyd_meta *meta = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004088 const char *val;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004089 LY_ERR rc = LY_SUCCESS;
4090
4091 if (options & LYXP_SCNODE_ALL) {
4092 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4093 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004094 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
4095 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004096 } else if (!warn_is_string_type(sleaf->type)) {
4097 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004098 }
4099 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004100 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004101 return rc;
4102 }
4103
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004104 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004105 LY_CHECK_RET(rc);
4106
Michal Vasko03ff5a72019-09-11 13:49:33 +02004107 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004108 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "lang(string)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004109 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004110 } else if (!set->used) {
4111 set_fill_boolean(set, 0);
4112 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004113 }
4114
4115 switch (set->val.nodes[0].type) {
4116 case LYXP_NODE_ELEM:
4117 case LYXP_NODE_TEXT:
4118 node = set->val.nodes[0].node;
4119 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004120 case LYXP_NODE_META:
4121 node = set->val.meta[0].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004122 break;
4123 default:
4124 /* nothing to do with roots */
4125 set_fill_boolean(set, 0);
4126 return LY_SUCCESS;
4127 }
4128
Michal Vasko9f96a052020-03-10 09:41:45 +01004129 /* find lang metadata */
Michal Vasko9e685082021-01-29 14:49:09 +01004130 for ( ; node; node = lyd_parent(node)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01004131 for (meta = node->meta; meta; meta = meta->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004132 /* annotations */
Michal Vasko9f96a052020-03-10 09:41:45 +01004133 if (meta->name && !strcmp(meta->name, "lang") && !strcmp(meta->annotation->module->name, "xml")) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004134 break;
4135 }
4136 }
4137
Michal Vasko9f96a052020-03-10 09:41:45 +01004138 if (meta) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004139 break;
4140 }
4141 }
4142
4143 /* compare languages */
Michal Vasko9f96a052020-03-10 09:41:45 +01004144 if (!meta) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004145 set_fill_boolean(set, 0);
4146 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004147 uint64_t i;
4148
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02004149 val = lyd_get_meta_value(meta);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004150 for (i = 0; args[0]->val.str[i]; ++i) {
4151 if (tolower(args[0]->val.str[i]) != tolower(val[i])) {
4152 set_fill_boolean(set, 0);
4153 break;
4154 }
4155 }
4156 if (!args[0]->val.str[i]) {
4157 if (!val[i] || (val[i] == '-')) {
4158 set_fill_boolean(set, 1);
4159 } else {
4160 set_fill_boolean(set, 0);
4161 }
4162 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004163 }
4164
4165 return LY_SUCCESS;
4166}
4167
4168/**
4169 * @brief Execute the XPath last() function. Returns LYXP_SET_NUMBER
4170 * with the context size.
4171 *
4172 * @param[in] args Array of arguments.
4173 * @param[in] arg_count Count of elements in @p args.
4174 * @param[in,out] set Context and result set at the same time.
4175 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004176 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004177 */
4178static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004179xpath_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 +02004180{
4181 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004182 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004183 return LY_SUCCESS;
4184 }
4185
Michal Vasko03ff5a72019-09-11 13:49:33 +02004186 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004187 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "last()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004188 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004189 } else if (!set->used) {
4190 set_fill_number(set, 0);
4191 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004192 }
4193
4194 set_fill_number(set, set->ctx_size);
4195 return LY_SUCCESS;
4196}
4197
4198/**
4199 * @brief Execute the XPath local-name(node-set?) function. Returns LYXP_SET_STRING
4200 * with the node name without namespace from the argument or the context.
4201 *
4202 * @param[in] args Array of arguments.
4203 * @param[in] arg_count Count of elements in @p args.
4204 * @param[in,out] set Context and result set at the same time.
4205 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004206 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004207 */
4208static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004209xpath_local_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004210{
4211 struct lyxp_set_node *item;
Michal Vasko69730152020-10-09 16:30:07 +02004212
Michal Vasko03ff5a72019-09-11 13:49:33 +02004213 /* suppress unused variable warning */
4214 (void)options;
4215
4216 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004217 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004218 return LY_SUCCESS;
4219 }
4220
4221 if (arg_count) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004222 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004223 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004224 "local-name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004225 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004226 } else if (!args[0]->used) {
4227 set_fill_string(set, "", 0);
4228 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004229 }
4230
4231 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004232 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004233
4234 item = &args[0]->val.nodes[0];
4235 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004236 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004237 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "local-name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004238 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004239 } else if (!set->used) {
4240 set_fill_string(set, "", 0);
4241 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004242 }
4243
4244 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004245 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004246
4247 item = &set->val.nodes[0];
4248 }
4249
4250 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004251 case LYXP_NODE_NONE:
4252 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004253 case LYXP_NODE_ROOT:
4254 case LYXP_NODE_ROOT_CONFIG:
4255 case LYXP_NODE_TEXT:
4256 set_fill_string(set, "", 0);
4257 break;
4258 case LYXP_NODE_ELEM:
4259 set_fill_string(set, item->node->schema->name, strlen(item->node->schema->name));
4260 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004261 case LYXP_NODE_META:
4262 set_fill_string(set, ((struct lyd_meta *)item->node)->name, strlen(((struct lyd_meta *)item->node)->name));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004263 break;
4264 }
4265
4266 return LY_SUCCESS;
4267}
4268
4269/**
4270 * @brief Execute the XPath name(node-set?) function. Returns LYXP_SET_STRING
4271 * with the node name fully qualified (with namespace) from the argument or the context.
4272 *
4273 * @param[in] args Array of arguments.
4274 * @param[in] arg_count Count of elements in @p args.
4275 * @param[in,out] set Context and result set at the same time.
4276 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004277 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004278 */
4279static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004280xpath_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004281{
4282 struct lyxp_set_node *item;
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004283 struct lys_module *mod = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004284 char *str;
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004285 const char *name = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004286
4287 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004288 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004289 return LY_SUCCESS;
4290 }
4291
4292 if (arg_count) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004293 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004294 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004295 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004296 } else if (!args[0]->used) {
4297 set_fill_string(set, "", 0);
4298 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004299 }
4300
4301 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004302 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004303
4304 item = &args[0]->val.nodes[0];
4305 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004306 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004307 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004308 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004309 } else if (!set->used) {
4310 set_fill_string(set, "", 0);
4311 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004312 }
4313
4314 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004315 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004316
4317 item = &set->val.nodes[0];
4318 }
4319
4320 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004321 case LYXP_NODE_NONE:
4322 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004323 case LYXP_NODE_ROOT:
4324 case LYXP_NODE_ROOT_CONFIG:
4325 case LYXP_NODE_TEXT:
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004326 /* keep NULL */
Michal Vasko03ff5a72019-09-11 13:49:33 +02004327 break;
4328 case LYXP_NODE_ELEM:
4329 mod = item->node->schema->module;
4330 name = item->node->schema->name;
4331 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004332 case LYXP_NODE_META:
4333 mod = ((struct lyd_meta *)item->node)->annotation->module;
4334 name = ((struct lyd_meta *)item->node)->name;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004335 break;
4336 }
4337
4338 if (mod && name) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004339 int rc = asprintf(&str, "%s:%s", ly_get_prefix(mod, set->format, set->prefix_data), name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004340 LY_CHECK_ERR_RET(rc == -1, LOGMEM(set->ctx), LY_EMEM);
4341 set_fill_string(set, str, strlen(str));
4342 free(str);
4343 } else {
4344 set_fill_string(set, "", 0);
4345 }
4346
4347 return LY_SUCCESS;
4348}
4349
4350/**
4351 * @brief Execute the XPath namespace-uri(node-set?) function. Returns LYXP_SET_STRING
4352 * with the namespace of the node from the argument or the context.
4353 *
4354 * @param[in] args Array of arguments.
4355 * @param[in] arg_count Count of elements in @p args.
4356 * @param[in,out] set Context and result set at the same time.
4357 * @param[in] options XPath options.
4358 * @return LY_ERR (LY_EINVAL for wrong arguments on schema)
4359 */
4360static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004361xpath_namespace_uri(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004362{
4363 struct lyxp_set_node *item;
4364 struct lys_module *mod;
Michal Vasko69730152020-10-09 16:30:07 +02004365
Michal Vasko03ff5a72019-09-11 13:49:33 +02004366 /* suppress unused variable warning */
4367 (void)options;
4368
4369 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004370 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004371 return LY_SUCCESS;
4372 }
4373
4374 if (arg_count) {
Michal Vaskod3678892020-05-21 10:06:58 +02004375 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004376 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
Michal Vasko69730152020-10-09 16:30:07 +02004377 "namespace-uri(node-set?)");
Michal Vaskod3678892020-05-21 10:06:58 +02004378 return LY_EVALID;
4379 } else if (!args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004380 set_fill_string(set, "", 0);
4381 return LY_SUCCESS;
4382 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004383
4384 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004385 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004386
4387 item = &args[0]->val.nodes[0];
4388 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004389 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004390 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "namespace-uri(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004391 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004392 } else if (!set->used) {
4393 set_fill_string(set, "", 0);
4394 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004395 }
4396
4397 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004398 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004399
4400 item = &set->val.nodes[0];
4401 }
4402
4403 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004404 case LYXP_NODE_NONE:
4405 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004406 case LYXP_NODE_ROOT:
4407 case LYXP_NODE_ROOT_CONFIG:
4408 case LYXP_NODE_TEXT:
4409 set_fill_string(set, "", 0);
4410 break;
4411 case LYXP_NODE_ELEM:
Michal Vasko9f96a052020-03-10 09:41:45 +01004412 case LYXP_NODE_META:
Michal Vasko03ff5a72019-09-11 13:49:33 +02004413 if (item->type == LYXP_NODE_ELEM) {
4414 mod = item->node->schema->module;
Michal Vasko9f96a052020-03-10 09:41:45 +01004415 } else { /* LYXP_NODE_META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02004416 /* annotations */
Michal Vasko9f96a052020-03-10 09:41:45 +01004417 mod = ((struct lyd_meta *)item->node)->annotation->module;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004418 }
4419
4420 set_fill_string(set, mod->ns, strlen(mod->ns));
4421 break;
4422 }
4423
4424 return LY_SUCCESS;
4425}
4426
4427/**
4428 * @brief Execute the XPath node() function (node type). Returns LYXP_SET_NODE_SET
4429 * with only nodes from the context. In practice it either leaves the context
4430 * as it is or returns an empty node set.
4431 *
4432 * @param[in] args Array of arguments.
4433 * @param[in] arg_count Count of elements in @p args.
4434 * @param[in,out] set Context and result set at the same time.
4435 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004436 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004437 */
4438static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004439xpath_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 +02004440{
4441 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004442 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004443 return LY_SUCCESS;
4444 }
4445
4446 if (set->type != LYXP_SET_NODE_SET) {
Michal Vaskod3678892020-05-21 10:06:58 +02004447 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004448 }
4449 return LY_SUCCESS;
4450}
4451
4452/**
4453 * @brief Execute the XPath normalize-space(string?) function. Returns LYXP_SET_STRING
4454 * with normalized value (no leading, trailing, double white spaces) of the node
4455 * from the argument or the context.
4456 *
4457 * @param[in] args Array of arguments.
4458 * @param[in] arg_count Count of elements in @p args.
4459 * @param[in,out] set Context and result set at the same time.
4460 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004461 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004462 */
4463static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004464xpath_normalize_space(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004465{
4466 uint16_t i, new_used;
4467 char *new;
Radek Krejci857189e2020-09-01 13:26:36 +02004468 ly_bool have_spaces = 0, space_before = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004469 struct lysc_node_leaf *sleaf;
4470 LY_ERR rc = LY_SUCCESS;
4471
4472 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004473 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) &&
4474 (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004475 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004476 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
4477 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004478 } else if (!warn_is_string_type(sleaf->type)) {
4479 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004480 }
4481 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004482 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004483 return rc;
4484 }
4485
4486 if (arg_count) {
4487 set_fill_set(set, args[0]);
4488 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004489 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004490 LY_CHECK_RET(rc);
4491
4492 /* is there any normalization necessary? */
4493 for (i = 0; set->val.str[i]; ++i) {
4494 if (is_xmlws(set->val.str[i])) {
4495 if ((i == 0) || space_before || (!set->val.str[i + 1])) {
4496 have_spaces = 1;
4497 break;
4498 }
4499 space_before = 1;
4500 } else {
4501 space_before = 0;
4502 }
4503 }
4504
4505 /* yep, there is */
4506 if (have_spaces) {
4507 /* it's enough, at least one character will go, makes space for ending '\0' */
4508 new = malloc(strlen(set->val.str) * sizeof(char));
4509 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4510 new_used = 0;
4511
4512 space_before = 0;
4513 for (i = 0; set->val.str[i]; ++i) {
4514 if (is_xmlws(set->val.str[i])) {
4515 if ((i == 0) || space_before) {
4516 space_before = 1;
4517 continue;
4518 } else {
4519 space_before = 1;
4520 }
4521 } else {
4522 space_before = 0;
4523 }
4524
4525 new[new_used] = (space_before ? ' ' : set->val.str[i]);
4526 ++new_used;
4527 }
4528
4529 /* at worst there is one trailing space now */
4530 if (new_used && is_xmlws(new[new_used - 1])) {
4531 --new_used;
4532 }
4533
4534 new = ly_realloc(new, (new_used + 1) * sizeof(char));
4535 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4536 new[new_used] = '\0';
4537
4538 free(set->val.str);
4539 set->val.str = new;
4540 }
4541
4542 return LY_SUCCESS;
4543}
4544
4545/**
4546 * @brief Execute the XPath not(boolean) function. Returns LYXP_SET_BOOLEAN
4547 * with the argument converted to boolean and logically inverted.
4548 *
4549 * @param[in] args Array of arguments.
4550 * @param[in] arg_count Count of elements in @p args.
4551 * @param[in,out] set Context and result set at the same time.
4552 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004553 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004554 */
4555static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004556xpath_not(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004557{
4558 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004559 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004560 return LY_SUCCESS;
4561 }
4562
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004563 lyxp_set_cast(args[0], LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +02004564 if (args[0]->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004565 set_fill_boolean(set, 0);
4566 } else {
4567 set_fill_boolean(set, 1);
4568 }
4569
4570 return LY_SUCCESS;
4571}
4572
4573/**
4574 * @brief Execute the XPath number(object?) function. Returns LYXP_SET_NUMBER
4575 * with the number representation of either the argument or the context.
4576 *
4577 * @param[in] args Array of arguments.
4578 * @param[in] arg_count Count of elements in @p args.
4579 * @param[in,out] set Context and result set at the same time.
4580 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004581 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004582 */
4583static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004584xpath_number(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004585{
4586 LY_ERR rc;
4587
4588 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004589 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004590 return LY_SUCCESS;
4591 }
4592
4593 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004594 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004595 LY_CHECK_RET(rc);
4596 set_fill_set(set, args[0]);
4597 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004598 rc = lyxp_set_cast(set, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004599 LY_CHECK_RET(rc);
4600 }
4601
4602 return LY_SUCCESS;
4603}
4604
4605/**
4606 * @brief Execute the XPath position() function. Returns LYXP_SET_NUMBER
4607 * with the context position.
4608 *
4609 * @param[in] args Array of arguments.
4610 * @param[in] arg_count Count of elements in @p args.
4611 * @param[in,out] set Context and result set at the same time.
4612 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004613 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004614 */
4615static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004616xpath_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 +02004617{
4618 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004619 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004620 return LY_SUCCESS;
4621 }
4622
Michal Vasko03ff5a72019-09-11 13:49:33 +02004623 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004624 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "position()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004625 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004626 } else if (!set->used) {
4627 set_fill_number(set, 0);
4628 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004629 }
4630
4631 set_fill_number(set, set->ctx_pos);
4632
4633 /* UNUSED in 'Release' build type */
4634 (void)options;
4635 return LY_SUCCESS;
4636}
4637
4638/**
4639 * @brief Execute the YANG 1.1 re-match(string, string) function. Returns LYXP_SET_BOOLEAN
4640 * depending on whether the second argument regex matches the first argument string. For details refer to
4641 * YANG 1.1 RFC section 10.2.1.
4642 *
4643 * @param[in] args Array of arguments.
4644 * @param[in] arg_count Count of elements in @p args.
4645 * @param[in,out] set Context and result set at the same time.
4646 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004647 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004648 */
4649static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004650xpath_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 +02004651{
4652 struct lysc_pattern **patterns = NULL, **pattern;
4653 struct lysc_node_leaf *sleaf;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004654 LY_ERR rc = LY_SUCCESS;
4655 struct ly_err_item *err;
4656
4657 if (options & LYXP_SCNODE_ALL) {
4658 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4659 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4660 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 +02004661 } else if (!warn_is_string_type(sleaf->type)) {
4662 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004663 }
4664 }
4665
4666 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4667 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4668 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 +02004669 } else if (!warn_is_string_type(sleaf->type)) {
4670 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004671 }
4672 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004673 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004674 return rc;
4675 }
4676
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004677 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004678 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004679 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004680 LY_CHECK_RET(rc);
4681
4682 LY_ARRAY_NEW_RET(set->ctx, patterns, pattern, LY_EMEM);
Radek Iša45802b52021-02-09 09:21:58 +01004683 *pattern = calloc(1, sizeof **pattern);
Radek Krejciddace2c2021-01-08 11:30:56 +01004684 LOG_LOCSET(NULL, set->cur_node, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01004685 rc = lys_compile_type_pattern_check(set->ctx, args[1]->val.str, &(*pattern)->code);
Michal Vasko4a7d4d62021-12-13 17:05:06 +01004686 if (set->cur_node) {
4687 LOG_LOCBACK(0, 1, 0, 0);
4688 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004689 if (rc != LY_SUCCESS) {
4690 LY_ARRAY_FREE(patterns);
4691 return rc;
4692 }
4693
Radek Krejci0b013302021-03-29 15:22:32 +02004694 rc = lyplg_type_validate_patterns(patterns, args[0]->val.str, strlen(args[0]->val.str), &err);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004695 pcre2_code_free((*pattern)->code);
4696 free(*pattern);
4697 LY_ARRAY_FREE(patterns);
4698 if (rc && (rc != LY_EVALID)) {
Michal Vasko177d0ed2020-11-23 16:43:03 +01004699 ly_err_print(set->ctx, err);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004700 ly_err_free(err);
4701 return rc;
4702 }
4703
4704 if (rc == LY_EVALID) {
4705 ly_err_free(err);
4706 set_fill_boolean(set, 0);
4707 } else {
4708 set_fill_boolean(set, 1);
4709 }
4710
4711 return LY_SUCCESS;
4712}
4713
4714/**
4715 * @brief Execute the XPath round(number) function. Returns LYXP_SET_NUMBER
4716 * with the rounded first argument. For details refer to
4717 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-round.
4718 *
4719 * @param[in] args Array of arguments.
4720 * @param[in] arg_count Count of elements in @p args.
4721 * @param[in,out] set Context and result set at the same time.
4722 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004723 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004724 */
4725static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004726xpath_round(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004727{
4728 struct lysc_node_leaf *sleaf;
4729 LY_ERR rc = LY_SUCCESS;
4730
4731 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02004732 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004733 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02004734 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4735 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4736 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
4737 sleaf->name);
4738 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
4739 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
4740 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004741 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004742 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004743 return rc;
4744 }
4745
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004746 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004747 LY_CHECK_RET(rc);
4748
4749 /* cover only the cases where floor can't be used */
4750 if ((args[0]->val.num == -0.0f) || ((args[0]->val.num < 0) && (args[0]->val.num >= -0.5))) {
4751 set_fill_number(set, -0.0f);
4752 } else {
4753 args[0]->val.num += 0.5;
4754 rc = xpath_floor(args, 1, args[0], options);
4755 LY_CHECK_RET(rc);
4756 set_fill_number(set, args[0]->val.num);
4757 }
4758
4759 return LY_SUCCESS;
4760}
4761
4762/**
4763 * @brief Execute the XPath starts-with(string, string) function.
4764 * Returns LYXP_SET_BOOLEAN whether the second argument is
4765 * the prefix of the first or not.
4766 *
4767 * @param[in] args Array of arguments.
4768 * @param[in] arg_count Count of elements in @p args.
4769 * @param[in,out] set Context and result set at the same time.
4770 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004771 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004772 */
4773static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004774xpath_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 +02004775{
4776 struct lysc_node_leaf *sleaf;
4777 LY_ERR rc = LY_SUCCESS;
4778
4779 if (options & LYXP_SCNODE_ALL) {
4780 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4781 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4782 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 +02004783 } else if (!warn_is_string_type(sleaf->type)) {
4784 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004785 }
4786 }
4787
4788 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4789 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4790 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 +02004791 } else if (!warn_is_string_type(sleaf->type)) {
4792 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004793 }
4794 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004795 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004796 return rc;
4797 }
4798
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004799 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004800 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004801 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004802 LY_CHECK_RET(rc);
4803
4804 if (strncmp(args[0]->val.str, args[1]->val.str, strlen(args[1]->val.str))) {
4805 set_fill_boolean(set, 0);
4806 } else {
4807 set_fill_boolean(set, 1);
4808 }
4809
4810 return LY_SUCCESS;
4811}
4812
4813/**
4814 * @brief Execute the XPath string(object?) function. Returns LYXP_SET_STRING
4815 * with the string representation of either the argument or the context.
4816 *
4817 * @param[in] args Array of arguments.
4818 * @param[in] arg_count Count of elements in @p args.
4819 * @param[in,out] set Context and result set at the same time.
4820 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004821 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004822 */
4823static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004824xpath_string(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004825{
4826 LY_ERR rc;
4827
4828 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004829 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004830 return LY_SUCCESS;
4831 }
4832
4833 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004834 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004835 LY_CHECK_RET(rc);
4836 set_fill_set(set, args[0]);
4837 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004838 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004839 LY_CHECK_RET(rc);
4840 }
4841
4842 return LY_SUCCESS;
4843}
4844
4845/**
4846 * @brief Execute the XPath string-length(string?) function. Returns LYXP_SET_NUMBER
4847 * with the length of the string in either the argument or the context.
4848 *
4849 * @param[in] args Array of arguments.
4850 * @param[in] arg_count Count of elements in @p args.
4851 * @param[in,out] set Context and result set at the same time.
4852 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004853 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004854 */
4855static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004856xpath_string_length(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004857{
4858 struct lysc_node_leaf *sleaf;
4859 LY_ERR rc = LY_SUCCESS;
4860
4861 if (options & LYXP_SCNODE_ALL) {
4862 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4863 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4864 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 +02004865 } else if (!warn_is_string_type(sleaf->type)) {
4866 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004867 }
4868 }
4869 if (!arg_count && (set->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set))) {
4870 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4871 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 +02004872 } else if (!warn_is_string_type(sleaf->type)) {
4873 LOGWRN(set->ctx, "Argument #0 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004874 }
4875 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004876 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004877 return rc;
4878 }
4879
4880 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004881 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004882 LY_CHECK_RET(rc);
4883 set_fill_number(set, strlen(args[0]->val.str));
4884 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004885 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004886 LY_CHECK_RET(rc);
4887 set_fill_number(set, strlen(set->val.str));
4888 }
4889
4890 return LY_SUCCESS;
4891}
4892
4893/**
4894 * @brief Execute the XPath substring(string, number, number?) function.
4895 * Returns LYXP_SET_STRING substring of the first argument starting
4896 * on the second argument index ending on the third argument index,
4897 * indexed from 1. For exact definition refer to
4898 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-substring.
4899 *
4900 * @param[in] args Array of arguments.
4901 * @param[in] arg_count Count of elements in @p args.
4902 * @param[in,out] set Context and result set at the same time.
4903 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004904 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004905 */
4906static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004907xpath_substring(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004908{
Radek Krejci1deb5be2020-08-26 16:43:36 +02004909 int32_t start, len;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004910 uint16_t str_start, str_len, pos;
4911 struct lysc_node_leaf *sleaf;
4912 LY_ERR rc = LY_SUCCESS;
4913
4914 if (options & LYXP_SCNODE_ALL) {
4915 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4916 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4917 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 +02004918 } else if (!warn_is_string_type(sleaf->type)) {
4919 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004920 }
4921 }
4922
4923 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4924 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4925 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 +02004926 } else if (!warn_is_numeric_type(sleaf->type)) {
4927 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004928 }
4929 }
4930
Michal Vasko69730152020-10-09 16:30:07 +02004931 if ((arg_count == 3) && (args[2]->type == LYXP_SET_SCNODE_SET) &&
4932 (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004933 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4934 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 +02004935 } else if (!warn_is_numeric_type(sleaf->type)) {
4936 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004937 }
4938 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004939 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004940 return rc;
4941 }
4942
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004943 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004944 LY_CHECK_RET(rc);
4945
4946 /* start */
4947 if (xpath_round(&args[1], 1, args[1], options)) {
4948 return -1;
4949 }
4950 if (isfinite(args[1]->val.num)) {
4951 start = args[1]->val.num - 1;
4952 } else if (isinf(args[1]->val.num) && signbit(args[1]->val.num)) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004953 start = INT32_MIN;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004954 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004955 start = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004956 }
4957
4958 /* len */
4959 if (arg_count == 3) {
4960 rc = xpath_round(&args[2], 1, args[2], options);
4961 LY_CHECK_RET(rc);
Radek Krejci1deb5be2020-08-26 16:43:36 +02004962 if (isnan(args[2]->val.num) || signbit(args[2]->val.num)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004963 len = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +02004964 } else if (isfinite(args[2]->val.num)) {
4965 len = args[2]->val.num;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004966 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004967 len = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004968 }
4969 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004970 len = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004971 }
4972
4973 /* find matching character positions */
4974 str_start = 0;
4975 str_len = 0;
4976 for (pos = 0; args[0]->val.str[pos]; ++pos) {
4977 if (pos < start) {
4978 ++str_start;
4979 } else if (pos < start + len) {
4980 ++str_len;
4981 } else {
4982 break;
4983 }
4984 }
4985
4986 set_fill_string(set, args[0]->val.str + str_start, str_len);
4987 return LY_SUCCESS;
4988}
4989
4990/**
4991 * @brief Execute the XPath substring-after(string, string) function.
4992 * Returns LYXP_SET_STRING with the string succeeding the occurance
4993 * of the second argument in the first or an empty string.
4994 *
4995 * @param[in] args Array of arguments.
4996 * @param[in] arg_count Count of elements in @p args.
4997 * @param[in,out] set Context and result set at the same time.
4998 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004999 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005000 */
5001static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005002xpath_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 +02005003{
5004 char *ptr;
5005 struct lysc_node_leaf *sleaf;
5006 LY_ERR rc = LY_SUCCESS;
5007
5008 if (options & LYXP_SCNODE_ALL) {
5009 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5010 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5011 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 +02005012 } else if (!warn_is_string_type(sleaf->type)) {
5013 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005014 }
5015 }
5016
5017 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5018 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5019 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 +02005020 } else if (!warn_is_string_type(sleaf->type)) {
5021 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005022 }
5023 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005024 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005025 return rc;
5026 }
5027
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005028 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005029 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005030 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005031 LY_CHECK_RET(rc);
5032
5033 ptr = strstr(args[0]->val.str, args[1]->val.str);
5034 if (ptr) {
5035 set_fill_string(set, ptr + strlen(args[1]->val.str), strlen(ptr + strlen(args[1]->val.str)));
5036 } else {
5037 set_fill_string(set, "", 0);
5038 }
5039
5040 return LY_SUCCESS;
5041}
5042
5043/**
5044 * @brief Execute the XPath substring-before(string, string) function.
5045 * Returns LYXP_SET_STRING with the string preceding the occurance
5046 * of the second argument in the first or an empty string.
5047 *
5048 * @param[in] args Array of arguments.
5049 * @param[in] arg_count Count of elements in @p args.
5050 * @param[in,out] set Context and result set at the same time.
5051 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005052 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005053 */
5054static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005055xpath_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 +02005056{
5057 char *ptr;
5058 struct lysc_node_leaf *sleaf;
5059 LY_ERR rc = LY_SUCCESS;
5060
5061 if (options & LYXP_SCNODE_ALL) {
5062 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5063 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5064 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 +02005065 } else if (!warn_is_string_type(sleaf->type)) {
5066 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005067 }
5068 }
5069
5070 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5071 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5072 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 +02005073 } else if (!warn_is_string_type(sleaf->type)) {
5074 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005075 }
5076 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005077 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005078 return rc;
5079 }
5080
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005081 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005082 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005083 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005084 LY_CHECK_RET(rc);
5085
5086 ptr = strstr(args[0]->val.str, args[1]->val.str);
5087 if (ptr) {
5088 set_fill_string(set, args[0]->val.str, ptr - args[0]->val.str);
5089 } else {
5090 set_fill_string(set, "", 0);
5091 }
5092
5093 return LY_SUCCESS;
5094}
5095
5096/**
5097 * @brief Execute the XPath sum(node-set) function. Returns LYXP_SET_NUMBER
5098 * with the sum of all the nodes in the context.
5099 *
5100 * @param[in] args Array of arguments.
5101 * @param[in] arg_count Count of elements in @p args.
5102 * @param[in,out] set Context and result set at the same time.
5103 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005104 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005105 */
5106static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005107xpath_sum(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005108{
5109 long double num;
5110 char *str;
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01005111 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005112 struct lyxp_set set_item;
5113 struct lysc_node_leaf *sleaf;
5114 LY_ERR rc = LY_SUCCESS;
5115
5116 if (options & LYXP_SCNODE_ALL) {
5117 if (args[0]->type == LYXP_SET_SCNODE_SET) {
5118 for (i = 0; i < args[0]->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005119 if (args[0]->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005120 sleaf = (struct lysc_node_leaf *)args[0]->val.scnodes[i].scnode;
5121 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5122 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__,
Michal Vasko69730152020-10-09 16:30:07 +02005123 lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005124 } else if (!warn_is_numeric_type(sleaf->type)) {
5125 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005126 }
5127 }
5128 }
5129 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005130 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005131 return rc;
5132 }
5133
5134 set_fill_number(set, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005135
5136 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005137 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "sum(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02005138 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02005139 } else if (!args[0]->used) {
5140 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005141 }
5142
Michal Vasko5c4e5892019-11-14 12:31:38 +01005143 set_init(&set_item, set);
5144
Michal Vasko03ff5a72019-09-11 13:49:33 +02005145 set_item.type = LYXP_SET_NODE_SET;
Michal Vasko41decbf2021-11-02 11:50:21 +01005146 set_item.val.nodes = calloc(1, sizeof *set_item.val.nodes);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005147 LY_CHECK_ERR_RET(!set_item.val.nodes, LOGMEM(set->ctx), LY_EMEM);
5148
5149 set_item.used = 1;
5150 set_item.size = 1;
5151
5152 for (i = 0; i < args[0]->used; ++i) {
5153 set_item.val.nodes[0] = args[0]->val.nodes[i];
5154
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005155 rc = cast_node_set_to_string(&set_item, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005156 LY_CHECK_RET(rc);
5157 num = cast_string_to_number(str);
5158 free(str);
5159 set->val.num += num;
5160 }
5161
5162 free(set_item.val.nodes);
5163
5164 return LY_SUCCESS;
5165}
5166
5167/**
5168 * @brief Execute the XPath text() function (node type). Returns LYXP_SET_NODE_SET
5169 * with the text content of the nodes in the context.
5170 *
5171 * @param[in] args Array of arguments.
5172 * @param[in] arg_count Count of elements in @p args.
5173 * @param[in,out] set Context and result set at the same time.
5174 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005175 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005176 */
5177static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005178xpath_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 +02005179{
5180 uint32_t i;
5181
5182 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02005183 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005184 return LY_SUCCESS;
5185 }
5186
Michal Vasko03ff5a72019-09-11 13:49:33 +02005187 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005188 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "text()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02005189 return LY_EVALID;
5190 }
5191
Michal Vaskod989ba02020-08-24 10:59:24 +02005192 for (i = 0; i < set->used; ) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005193 switch (set->val.nodes[i].type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01005194 case LYXP_NODE_NONE:
5195 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005196 case LYXP_NODE_ELEM:
Michal Vasko03ff5a72019-09-11 13:49:33 +02005197 if (set->val.nodes[i].node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
5198 set->val.nodes[i].type = LYXP_NODE_TEXT;
5199 ++i;
5200 break;
5201 }
Radek Krejci0f969882020-08-21 16:56:47 +02005202 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005203 case LYXP_NODE_ROOT:
5204 case LYXP_NODE_ROOT_CONFIG:
5205 case LYXP_NODE_TEXT:
Michal Vasko9f96a052020-03-10 09:41:45 +01005206 case LYXP_NODE_META:
Michal Vasko03ff5a72019-09-11 13:49:33 +02005207 set_remove_node(set, i);
5208 break;
5209 }
5210 }
5211
5212 return LY_SUCCESS;
5213}
5214
5215/**
5216 * @brief Execute the XPath translate(string, string, string) function.
5217 * Returns LYXP_SET_STRING with the first argument with the characters
5218 * from the second argument replaced by those on the corresponding
5219 * positions in the third argument.
5220 *
5221 * @param[in] args Array of arguments.
5222 * @param[in] arg_count Count of elements in @p args.
5223 * @param[in,out] set Context and result set at the same time.
5224 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005225 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005226 */
5227static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005228xpath_translate(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005229{
5230 uint16_t i, j, new_used;
5231 char *new;
Radek Krejci857189e2020-09-01 13:26:36 +02005232 ly_bool have_removed;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005233 struct lysc_node_leaf *sleaf;
5234 LY_ERR rc = LY_SUCCESS;
5235
5236 if (options & LYXP_SCNODE_ALL) {
5237 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5238 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5239 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 +02005240 } else if (!warn_is_string_type(sleaf->type)) {
5241 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005242 }
5243 }
5244
5245 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5246 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5247 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 +02005248 } else if (!warn_is_string_type(sleaf->type)) {
5249 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005250 }
5251 }
5252
5253 if ((args[2]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
5254 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5255 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 +02005256 } else if (!warn_is_string_type(sleaf->type)) {
5257 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005258 }
5259 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005260 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005261 return rc;
5262 }
5263
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005264 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005265 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005266 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005267 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005268 rc = lyxp_set_cast(args[2], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005269 LY_CHECK_RET(rc);
5270
5271 new = malloc((strlen(args[0]->val.str) + 1) * sizeof(char));
5272 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5273 new_used = 0;
5274
5275 have_removed = 0;
5276 for (i = 0; args[0]->val.str[i]; ++i) {
Radek Krejci857189e2020-09-01 13:26:36 +02005277 ly_bool found = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005278
5279 for (j = 0; args[1]->val.str[j]; ++j) {
5280 if (args[0]->val.str[i] == args[1]->val.str[j]) {
5281 /* removing this char */
5282 if (j >= strlen(args[2]->val.str)) {
5283 have_removed = 1;
5284 found = 1;
5285 break;
5286 }
5287 /* replacing this char */
5288 new[new_used] = args[2]->val.str[j];
5289 ++new_used;
5290 found = 1;
5291 break;
5292 }
5293 }
5294
5295 /* copying this char */
5296 if (!found) {
5297 new[new_used] = args[0]->val.str[i];
5298 ++new_used;
5299 }
5300 }
5301
5302 if (have_removed) {
5303 new = ly_realloc(new, (new_used + 1) * sizeof(char));
5304 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5305 }
5306 new[new_used] = '\0';
5307
Michal Vaskod3678892020-05-21 10:06:58 +02005308 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005309 set->type = LYXP_SET_STRING;
5310 set->val.str = new;
5311
5312 return LY_SUCCESS;
5313}
5314
5315/**
5316 * @brief Execute the XPath true() function. Returns LYXP_SET_BOOLEAN
5317 * with true value.
5318 *
5319 * @param[in] args Array of arguments.
5320 * @param[in] arg_count Count of elements in @p args.
5321 * @param[in,out] set Context and result set at the same time.
5322 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005323 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005324 */
5325static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005326xpath_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 +02005327{
5328 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02005329 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005330 return LY_SUCCESS;
5331 }
5332
5333 set_fill_boolean(set, 1);
5334 return LY_SUCCESS;
5335}
5336
Michal Vasko03ff5a72019-09-11 13:49:33 +02005337/**
Michal Vasko6346ece2019-09-24 13:12:53 +02005338 * @brief Skip prefix and return corresponding model if there is a prefix. Logs directly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005339 *
Michal Vasko2104e9f2020-03-06 08:23:25 +01005340 * XPath @p set is expected to be a (sc)node set!
5341 *
Michal Vasko6346ece2019-09-24 13:12:53 +02005342 * @param[in,out] qname Qualified node name. If includes prefix, it is skipped.
5343 * @param[in,out] qname_len Length of @p qname, is updated accordingly.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005344 * @param[in] set Set with general XPath context.
5345 * @param[in] ctx_scnode Context node to inherit module for unprefixed node for ::LY_PREF_JSON.
Michal Vasko6346ece2019-09-24 13:12:53 +02005346 * @param[out] moveto_mod Expected module of a matching node.
5347 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005348 */
Michal Vasko6346ece2019-09-24 13:12:53 +02005349static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005350moveto_resolve_model(const char **qname, uint16_t *qname_len, const struct lyxp_set *set,
5351 const struct lysc_node *ctx_scnode, const struct lys_module **moveto_mod)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005352{
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02005353 const struct lys_module *mod = NULL;
Michal Vasko6346ece2019-09-24 13:12:53 +02005354 const char *ptr;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005355 size_t pref_len;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005356
Michal Vasko2104e9f2020-03-06 08:23:25 +01005357 assert((set->type == LYXP_SET_NODE_SET) || (set->type == LYXP_SET_SCNODE_SET));
5358
Michal Vasko6346ece2019-09-24 13:12:53 +02005359 if ((ptr = ly_strnchr(*qname, ':', *qname_len))) {
5360 /* specific module */
5361 pref_len = ptr - *qname;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005362 mod = ly_resolve_prefix(set->ctx, *qname, pref_len, set->format, set->prefix_data);
Michal Vasko6346ece2019-09-24 13:12:53 +02005363
Michal Vasko004d3152020-06-11 19:59:22 +02005364 /* check for errors and non-implemented modules, as they are not valid */
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005365 if (!mod || !mod->implemented) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005366 LOGVAL(set->ctx, LY_VCODE_XP_INMOD, pref_len, *qname);
Michal Vasko6346ece2019-09-24 13:12:53 +02005367 return LY_EVALID;
5368 }
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005369
Michal Vasko6346ece2019-09-24 13:12:53 +02005370 *qname += pref_len + 1;
5371 *qname_len -= pref_len + 1;
5372 } else if (((*qname)[0] == '*') && (*qname_len == 1)) {
5373 /* all modules - special case */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005374 mod = NULL;
Michal Vasko6346ece2019-09-24 13:12:53 +02005375 } else {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005376 switch (set->format) {
Radek Krejci8df109d2021-04-23 12:19:08 +02005377 case LY_VALUE_SCHEMA:
5378 case LY_VALUE_SCHEMA_RESOLVED:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005379 /* current module */
5380 mod = set->cur_mod;
5381 break;
Radek Krejci224d4b42021-04-23 13:54:59 +02005382 case LY_VALUE_CANON:
Radek Krejci8df109d2021-04-23 12:19:08 +02005383 case LY_VALUE_JSON:
Radek Krejcif9943642021-04-26 10:18:21 +02005384 case LY_VALUE_LYB:
Michal Vaskoddd76592022-01-17 13:34:48 +01005385 case LY_VALUE_STR_NS:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005386 /* inherit parent (context node) module */
5387 if (ctx_scnode) {
5388 mod = ctx_scnode->module;
5389 } else {
5390 mod = NULL;
5391 }
5392 break;
Radek Krejci8df109d2021-04-23 12:19:08 +02005393 case LY_VALUE_XML:
Michal Vasko52143d12021-04-14 15:36:39 +02005394 /* all nodes need to be prefixed */
5395 LOGVAL(set->ctx, LYVE_DATA, "Non-prefixed node \"%.*s\" in XML xpath found.", *qname_len, *qname);
5396 return LY_EVALID;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005397 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005398 }
5399
Michal Vasko6346ece2019-09-24 13:12:53 +02005400 *moveto_mod = mod;
5401 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005402}
5403
5404/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02005405 * @brief Move context @p set to the root. Handles absolute path.
5406 * Result is LYXP_SET_NODE_SET.
5407 *
5408 * @param[in,out] set Set to use.
5409 * @param[in] options Xpath options.
Michal Vaskob0099a92020-08-31 14:55:23 +02005410 * @return LY_ERR value.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005411 */
Michal Vaskob0099a92020-08-31 14:55:23 +02005412static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005413moveto_root(struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005414{
aPiecek8b0cc152021-05-31 16:40:31 +02005415 assert(!(options & LYXP_SKIP_EXPR));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005416
5417 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02005418 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskob0099a92020-08-31 14:55:23 +02005419 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, NULL, set->root_type, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005420 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005421 set->type = LYXP_SET_NODE_SET;
5422 set->used = 0;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005423 set_insert_node(set, NULL, 0, set->root_type, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005424 }
Michal Vaskob0099a92020-08-31 14:55:23 +02005425
5426 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005427}
5428
5429/**
5430 * @brief Check @p node as a part of NameTest processing.
5431 *
5432 * @param[in] node Node to check.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005433 * @param[in] set Set to read general context from.
Michal Vaskod3678892020-05-21 10:06:58 +02005434 * @param[in] node_name Node name in the dictionary to move to, NULL for any node.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005435 * @param[in] moveto_mod Expected module of the node, NULL for no prefix.
Michal Vaskocdad7122020-11-09 21:04:44 +01005436 * @param[in] options XPath options.
Michal Vasko6346ece2019-09-24 13:12:53 +02005437 * @return LY_ERR (LY_ENOT if node does not match, LY_EINCOMPLETE on unresolved when,
5438 * LY_EINVAL if netither node nor any children match)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005439 */
5440static LY_ERR
Michal Vaskodb08ce52021-10-06 08:57:49 +02005441moveto_node_check(const struct lyd_node *node, const struct lyxp_set *set, const char *node_name,
5442 const struct lys_module *moveto_mod, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005443{
Michal Vaskodca9f122021-07-16 13:56:22 +02005444 if (!node->schema) {
5445 /* opaque node never matches */
5446 return LY_ENOT;
5447 }
5448
Michal Vasko03ff5a72019-09-11 13:49:33 +02005449 /* module check */
5450 if (moveto_mod && (node->schema->module != moveto_mod)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005451 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005452 }
5453
Michal Vasko5c4e5892019-11-14 12:31:38 +01005454 /* context check */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005455 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (node->schema->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005456 return LY_EINVAL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005457 } else if (set->context_op && (node->schema->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) &&
5458 (node->schema != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005459 return LY_EINVAL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005460 }
5461
5462 /* name check */
Michal Vasko61ac2f62020-05-25 12:39:51 +02005463 if (node_name && strcmp(node_name, "*") && (node->schema->name != node_name)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005464 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005465 }
5466
Michal Vaskoa1424542019-11-14 16:08:52 +01005467 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01005468 if (!(options & LYXP_IGNORE_WHEN) && lysc_has_when(node->schema) && !(node->flags & LYD_WHEN_TRUE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005469 return LY_EINCOMPLETE;
Michal Vaskoa1424542019-11-14 16:08:52 +01005470 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005471
5472 /* match */
5473 return LY_SUCCESS;
5474}
5475
5476/**
5477 * @brief Check @p node as a part of schema NameTest processing.
5478 *
5479 * @param[in] node Schema node to check.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005480 * @param[in] ctx_scnode Context node.
5481 * @param[in] set Set to read general context from.
Michal Vaskod3678892020-05-21 10:06:58 +02005482 * @param[in] node_name Node name in the dictionary to move to, NULL for any nodes.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005483 * @param[in] moveto_mod Expected module of the node, NULL for no prefix.
Michal Vasko6346ece2019-09-24 13:12:53 +02005484 * @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 +02005485 */
5486static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005487moveto_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 +02005488 const char *node_name, const struct lys_module *moveto_mod)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005489{
Michal Vasko24bd22f2022-02-02 11:16:56 +01005490 if (!moveto_mod && node_name && strcmp(node_name, "*")) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005491 switch (set->format) {
Radek Krejci8df109d2021-04-23 12:19:08 +02005492 case LY_VALUE_SCHEMA:
5493 case LY_VALUE_SCHEMA_RESOLVED:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005494 /* use current module */
5495 moveto_mod = set->cur_mod;
5496 break;
Radek Krejci8df109d2021-04-23 12:19:08 +02005497 case LY_VALUE_JSON:
Radek Krejcif9943642021-04-26 10:18:21 +02005498 case LY_VALUE_LYB:
Michal Vaskoddd76592022-01-17 13:34:48 +01005499 case LY_VALUE_STR_NS:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005500 /* inherit module of the context node, if any */
5501 if (ctx_scnode) {
5502 moveto_mod = ctx_scnode->module;
5503 }
5504 break;
Radek Krejci224d4b42021-04-23 13:54:59 +02005505 case LY_VALUE_CANON:
Radek Krejci8df109d2021-04-23 12:19:08 +02005506 case LY_VALUE_XML:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005507 /* not defined */
5508 LOGINT(set->ctx);
5509 return LY_EINVAL;
5510 }
5511 }
5512
Michal Vasko03ff5a72019-09-11 13:49:33 +02005513 /* module check */
Michal Vaskod3678892020-05-21 10:06:58 +02005514 if (moveto_mod && (node->module != moveto_mod)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005515 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005516 }
5517
5518 /* context check */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005519 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (node->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005520 return LY_EINVAL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005521 } else if (set->context_op && (node->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && (node != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005522 return LY_EINVAL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005523 }
5524
5525 /* name check */
Michal Vasko61ac2f62020-05-25 12:39:51 +02005526 if (node_name && strcmp(node_name, "*") && (node->name != node_name)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005527 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005528 }
5529
5530 /* match */
5531 return LY_SUCCESS;
5532}
5533
5534/**
Michal Vaskod3678892020-05-21 10:06:58 +02005535 * @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 +02005536 *
5537 * @param[in,out] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005538 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005539 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vaskocdad7122020-11-09 21:04:44 +01005540 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005541 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5542 */
5543static LY_ERR
Michal Vaskocdad7122020-11-09 21:04:44 +01005544moveto_node(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005545{
Michal Vasko230cf972021-12-02 12:31:00 +01005546 LY_ERR r, rc = LY_SUCCESS;
Michal Vaskodb08ce52021-10-06 08:57:49 +02005547 const struct lyd_node *siblings, *sub;
Michal Vasko230cf972021-12-02 12:31:00 +01005548 struct lyxp_set result;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005549
aPiecek8b0cc152021-05-31 16:40:31 +02005550 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005551 return LY_SUCCESS;
5552 }
5553
5554 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005555 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005556 return LY_EVALID;
5557 }
5558
Michal Vasko230cf972021-12-02 12:31:00 +01005559 /* init result set */
5560 set_init(&result, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005561
Michal Vasko230cf972021-12-02 12:31:00 +01005562 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005563 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 +01005564 assert(!set->val.nodes[i].node);
Michal Vaskod3678892020-05-21 10:06:58 +02005565
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005566 /* search in all the trees */
Michal Vaskod3678892020-05-21 10:06:58 +02005567 siblings = set->tree;
Michal Vasko5bfd4be2020-06-23 13:26:19 +02005568 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005569 /* search in children */
Michal Vaskodb08ce52021-10-06 08:57:49 +02005570 siblings = lyd_child(set->val.nodes[i].node);
Michal Vaskod3678892020-05-21 10:06:58 +02005571 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005572
Michal Vaskod3678892020-05-21 10:06:58 +02005573 for (sub = siblings; sub; sub = sub->next) {
Michal Vasko230cf972021-12-02 12:31:00 +01005574 r = moveto_node_check(sub, set, ncname, moveto_mod, options);
5575 if (r == LY_SUCCESS) {
5576 /* matching node */
5577 set_insert_node(&result, sub, 0, LYXP_NODE_ELEM, result.used);
5578 } else if (r == LY_EINCOMPLETE) {
5579 rc = r;
5580 goto cleanup;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005581 }
5582 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005583 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005584
Michal Vasko230cf972021-12-02 12:31:00 +01005585 /* move result to the set */
5586 lyxp_set_free_content(set);
5587 *set = result;
5588 result.type = LYXP_SET_NUMBER;
5589 assert(!set_sort(set));
5590
5591cleanup:
5592 lyxp_set_free_content(&result);
5593 return rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005594}
5595
5596/**
Michal Vaskod3678892020-05-21 10:06:58 +02005597 * @brief Move context @p set to a node using hashes. Result is LYXP_SET_NODE_SET (or LYXP_SET_EMPTY).
5598 * Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005599 *
5600 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005601 * @param[in] scnode Matching node schema.
Michal Vasko004d3152020-06-11 19:59:22 +02005602 * @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 +01005603 * @param[in] options XPath options.
Michal Vaskod3678892020-05-21 10:06:58 +02005604 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5605 */
5606static LY_ERR
Michal Vaskocdad7122020-11-09 21:04:44 +01005607moveto_node_hash(struct lyxp_set *set, const struct lysc_node *scnode, const struct ly_path_predicate *predicates,
5608 uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02005609{
Michal Vaskoddd76592022-01-17 13:34:48 +01005610 LY_ERR ret = LY_SUCCESS, r;
Michal Vaskod3678892020-05-21 10:06:58 +02005611 uint32_t i;
Michal Vaskod3678892020-05-21 10:06:58 +02005612 const struct lyd_node *siblings;
Michal Vasko230cf972021-12-02 12:31:00 +01005613 struct lyxp_set result;
Michal Vasko004d3152020-06-11 19:59:22 +02005614 struct lyd_node *sub, *inst = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02005615
Michal Vasko004d3152020-06-11 19:59:22 +02005616 assert(scnode && (!(scnode->nodetype & (LYS_LIST | LYS_LEAFLIST)) || predicates));
Michal Vaskod3678892020-05-21 10:06:58 +02005617
Michal Vasko50aaa072021-12-02 13:11:56 +01005618 /* init result set */
5619 set_init(&result, set);
5620
aPiecek8b0cc152021-05-31 16:40:31 +02005621 if (options & LYXP_SKIP_EXPR) {
Michal Vasko004d3152020-06-11 19:59:22 +02005622 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005623 }
5624
5625 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005626 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko004d3152020-06-11 19:59:22 +02005627 ret = LY_EVALID;
5628 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005629 }
5630
5631 /* context check for all the nodes since we have the schema node */
5632 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) {
5633 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02005634 goto cleanup;
Michal Vasko69730152020-10-09 16:30:07 +02005635 } else if (set->context_op && (scnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) &&
5636 (scnode != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005637 lyxp_set_free_content(set);
5638 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +02005639 }
5640
5641 /* create specific data instance if needed */
5642 if (scnode->nodetype == LYS_LIST) {
5643 LY_CHECK_GOTO(ret = lyd_create_list(scnode, predicates, &inst), cleanup);
5644 } else if (scnode->nodetype == LYS_LEAFLIST) {
5645 LY_CHECK_GOTO(ret = lyd_create_term2(scnode, &predicates[0].value, &inst), cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02005646 }
5647
Michal Vasko230cf972021-12-02 12:31:00 +01005648 for (i = 0; i < set->used; ++i) {
Michal Vaskod3678892020-05-21 10:06:58 +02005649 siblings = NULL;
5650
5651 if ((set->val.nodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.nodes[i].type == LYXP_NODE_ROOT)) {
5652 assert(!set->val.nodes[i].node);
5653
5654 /* search in all the trees */
5655 siblings = set->tree;
5656 } else if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
5657 /* search in children */
Radek Krejcia1c1e542020-09-29 16:06:52 +02005658 siblings = lyd_child(set->val.nodes[i].node);
Michal Vaskod3678892020-05-21 10:06:58 +02005659 }
5660
5661 /* find the node using hashes */
Michal Vasko004d3152020-06-11 19:59:22 +02005662 if (inst) {
Michal Vaskoddd76592022-01-17 13:34:48 +01005663 r = lyd_find_sibling_first(siblings, inst, &sub);
Michal Vaskod3678892020-05-21 10:06:58 +02005664 } else {
Michal Vaskoddd76592022-01-17 13:34:48 +01005665 r = lyd_find_sibling_val(siblings, scnode, NULL, 0, &sub);
Michal Vaskod3678892020-05-21 10:06:58 +02005666 }
Michal Vaskoddd76592022-01-17 13:34:48 +01005667 LY_CHECK_ERR_GOTO(r && (r != LY_ENOTFOUND), ret = r, cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02005668
5669 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01005670 if (!(options & LYXP_IGNORE_WHEN) && sub && lysc_has_when(sub->schema) && !(sub->flags & LYD_WHEN_TRUE)) {
Michal Vasko004d3152020-06-11 19:59:22 +02005671 ret = LY_EINCOMPLETE;
5672 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005673 }
5674
5675 if (sub) {
5676 /* pos filled later */
Michal Vasko230cf972021-12-02 12:31:00 +01005677 set_insert_node(&result, sub, 0, LYXP_NODE_ELEM, result.used);
Michal Vaskod3678892020-05-21 10:06:58 +02005678 }
5679 }
5680
Michal Vasko230cf972021-12-02 12:31:00 +01005681 /* move result to the set */
5682 lyxp_set_free_content(set);
5683 *set = result;
5684 result.type = LYXP_SET_NUMBER;
5685 assert(!set_sort(set));
5686
Michal Vasko004d3152020-06-11 19:59:22 +02005687cleanup:
Michal Vasko230cf972021-12-02 12:31:00 +01005688 lyxp_set_free_content(&result);
Michal Vasko004d3152020-06-11 19:59:22 +02005689 lyd_free_tree(inst);
5690 return ret;
Michal Vaskod3678892020-05-21 10:06:58 +02005691}
5692
5693/**
5694 * @brief Move context @p set to a schema node. Result is LYXP_SET_SCNODE_SET (or LYXP_SET_EMPTY).
5695 *
5696 * @param[in,out] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005697 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005698 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005699 * @param[in] options XPath options.
5700 * @return LY_ERR
5701 */
5702static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005703moveto_scnode(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005704{
Radek Krejci857189e2020-09-01 13:26:36 +02005705 ly_bool temp_ctx = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005706 uint32_t getnext_opts;
5707 uint32_t orig_used, i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005708 uint32_t mod_idx;
Michal Vasko519fd602020-05-26 12:17:39 +02005709 const struct lysc_node *iter, *start_parent;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005710 const struct lys_module *mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005711
aPiecek8b0cc152021-05-31 16:40:31 +02005712 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005713 return LY_SUCCESS;
5714 }
5715
5716 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005717 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005718 return LY_EVALID;
5719 }
5720
Michal Vaskocafad9d2019-11-07 15:20:03 +01005721 /* getnext opts */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01005722 getnext_opts = 0;
Michal Vaskocafad9d2019-11-07 15:20:03 +01005723 if (options & LYXP_SCNODE_OUTPUT) {
5724 getnext_opts |= LYS_GETNEXT_OUTPUT;
5725 }
5726
Michal Vasko03ff5a72019-09-11 13:49:33 +02005727 orig_used = set->used;
5728 for (i = 0; i < orig_used; ++i) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005729 uint32_t idx;
5730
Radek Krejcif13b87b2020-12-01 22:02:17 +01005731 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
5732 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01005733 continue;
5734 }
5735
5736 /* remember context node */
Radek Krejcif13b87b2020-12-01 22:02:17 +01005737 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vaskoec4df482019-12-16 10:02:18 +01005738 } else {
Michal Vasko1a09b212021-05-06 13:00:10 +02005739 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005740 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005741
5742 start_parent = set->val.scnodes[i].scnode;
5743
5744 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 +02005745 /* 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 +02005746 * it can be in a top-level augment (the root node itself is useless in this case) */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005747 mod_idx = 0;
Michal Vaskoa51ef072021-07-02 10:40:30 +02005748 while ((mod = ly_ctx_get_module_iter(set->ctx, &mod_idx))) {
Michal Vasko519fd602020-05-26 12:17:39 +02005749 iter = NULL;
Michal Vasko919954a2021-07-26 09:21:42 +02005750 /* module may not be implemented or not compiled yet */
5751 while (mod->compiled && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005752 if (!moveto_scnode_check(iter, NULL, set, ncname, moveto_mod)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005753 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, &idx));
5754
Michal Vasko03ff5a72019-09-11 13:49:33 +02005755 /* we need to prevent these nodes from being considered in this moveto */
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005756 if ((idx < orig_used) && (idx > i)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005757 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005758 temp_ctx = 1;
5759 }
5760 }
5761 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005762 }
5763
Michal Vasko519fd602020-05-26 12:17:39 +02005764 } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
5765 iter = NULL;
5766 while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005767 if (!moveto_scnode_check(iter, start_parent, set, ncname, moveto_mod)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005768 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, &idx));
5769
5770 if ((idx < orig_used) && (idx > i)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005771 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005772 temp_ctx = 1;
5773 }
5774 }
5775 }
5776 }
5777 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005778
5779 /* correct temporary in_ctx values */
5780 if (temp_ctx) {
5781 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005782 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_NEW_CTX) {
5783 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005784 }
5785 }
5786 }
5787
5788 return LY_SUCCESS;
5789}
5790
5791/**
Michal Vaskod3678892020-05-21 10:06:58 +02005792 * @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 +02005793 * Context position aware.
5794 *
5795 * @param[in] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005796 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005797 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vaskocdad7122020-11-09 21:04:44 +01005798 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005799 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5800 */
5801static LY_ERR
Michal Vaskocdad7122020-11-09 21:04:44 +01005802moveto_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 +02005803{
5804 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005805 const struct lyd_node *next, *elem, *start;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005806 struct lyxp_set ret_set;
5807 LY_ERR rc;
5808
aPiecek8b0cc152021-05-31 16:40:31 +02005809 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005810 return LY_SUCCESS;
5811 }
5812
5813 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005814 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005815 return LY_EVALID;
5816 }
5817
Michal Vasko9f96a052020-03-10 09:41:45 +01005818 /* 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 +01005819 rc = moveto_node(set, NULL, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005820 LY_CHECK_RET(rc);
5821
Michal Vasko6346ece2019-09-24 13:12:53 +02005822 /* this loop traverses all the nodes in the set and adds/keeps only those that match qname */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005823 set_init(&ret_set, set);
5824 for (i = 0; i < set->used; ++i) {
5825
5826 /* TREE DFS */
5827 start = set->val.nodes[i].node;
5828 for (elem = next = start; elem; elem = next) {
Michal Vaskodb08ce52021-10-06 08:57:49 +02005829 rc = moveto_node_check(elem, set, ncname, moveto_mod, options);
Michal Vasko6346ece2019-09-24 13:12:53 +02005830 if (!rc) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005831 /* add matching node into result set */
5832 set_insert_node(&ret_set, elem, 0, LYXP_NODE_ELEM, ret_set.used);
5833 if (set_dup_node_check(set, elem, LYXP_NODE_ELEM, i)) {
5834 /* the node is a duplicate, we'll process it later in the set */
5835 goto skip_children;
5836 }
Michal Vasko6346ece2019-09-24 13:12:53 +02005837 } else if (rc == LY_EINCOMPLETE) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005838 return rc;
5839 } else if (rc == LY_EINVAL) {
5840 goto skip_children;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005841 }
5842
5843 /* TREE DFS NEXT ELEM */
5844 /* select element for the next run - children first */
Radek Krejcia1c1e542020-09-29 16:06:52 +02005845 next = lyd_child(elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005846 if (!next) {
5847skip_children:
5848 /* no children, so try siblings, but only if it's not the start,
5849 * that is considered to be the root and it's siblings are not traversed */
5850 if (elem != start) {
5851 next = elem->next;
5852 } else {
5853 break;
5854 }
5855 }
5856 while (!next) {
5857 /* no siblings, go back through the parents */
Michal Vasko9e685082021-01-29 14:49:09 +01005858 if (lyd_parent(elem) == start) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005859 /* we are done, no next element to process */
5860 break;
5861 }
5862 /* parent is already processed, go to its sibling */
Michal Vasko9e685082021-01-29 14:49:09 +01005863 elem = lyd_parent(elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005864 next = elem->next;
5865 }
5866 }
5867 }
5868
5869 /* make the temporary set the current one */
5870 ret_set.ctx_pos = set->ctx_pos;
5871 ret_set.ctx_size = set->ctx_size;
Michal Vaskod3678892020-05-21 10:06:58 +02005872 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005873 memcpy(set, &ret_set, sizeof *set);
5874
5875 return LY_SUCCESS;
5876}
5877
5878/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02005879 * @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 +02005880 *
5881 * @param[in] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005882 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005883 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005884 * @param[in] options XPath options.
5885 * @return LY_ERR
5886 */
5887static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005888moveto_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 +02005889{
Radek Krejci1deb5be2020-08-26 16:43:36 +02005890 uint32_t i, orig_used;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005891 const struct lysc_node *next, *elem, *start;
Michal Vasko6346ece2019-09-24 13:12:53 +02005892 LY_ERR rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005893
aPiecek8b0cc152021-05-31 16:40:31 +02005894 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005895 return LY_SUCCESS;
5896 }
5897
5898 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005899 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005900 return LY_EVALID;
5901 }
5902
Michal Vasko03ff5a72019-09-11 13:49:33 +02005903 orig_used = set->used;
5904 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005905 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
5906 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01005907 continue;
5908 }
5909
5910 /* remember context node */
Radek Krejcif13b87b2020-12-01 22:02:17 +01005911 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vaskoec4df482019-12-16 10:02:18 +01005912 } else {
Michal Vasko1a09b212021-05-06 13:00:10 +02005913 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005914 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005915
5916 /* TREE DFS */
5917 start = set->val.scnodes[i].scnode;
5918 for (elem = next = start; elem; elem = next) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005919 if ((elem == start) || (elem->nodetype & (LYS_CHOICE | LYS_CASE))) {
5920 /* schema-only nodes, skip root */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005921 goto next_iter;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005922 }
5923
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005924 rc = moveto_scnode_check(elem, start, set, ncname, moveto_mod);
Michal Vasko6346ece2019-09-24 13:12:53 +02005925 if (!rc) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005926 uint32_t idx;
5927
5928 if (lyxp_set_scnode_contains(set, elem, LYXP_NODE_ELEM, i, &idx)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005929 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005930 if ((uint32_t)idx > i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005931 /* we will process it later in the set */
5932 goto skip_children;
5933 }
5934 } else {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005935 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, elem, LYXP_NODE_ELEM, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005936 }
Michal Vasko6346ece2019-09-24 13:12:53 +02005937 } else if (rc == LY_EINVAL) {
5938 goto skip_children;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005939 }
5940
5941next_iter:
5942 /* TREE DFS NEXT ELEM */
5943 /* select element for the next run - children first */
Michal Vasko544e58a2021-01-28 14:33:41 +01005944 next = lysc_node_child(elem);
5945 if (next && (next->nodetype == LYS_INPUT) && (options & LYXP_SCNODE_OUTPUT)) {
5946 next = next->next;
5947 } else if (next && (next->nodetype == LYS_OUTPUT) && !(options & LYXP_SCNODE_OUTPUT)) {
5948 next = next->next;
5949 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005950 if (!next) {
5951skip_children:
5952 /* no children, so try siblings, but only if it's not the start,
5953 * that is considered to be the root and it's siblings are not traversed */
5954 if (elem != start) {
5955 next = elem->next;
5956 } else {
5957 break;
5958 }
5959 }
5960 while (!next) {
5961 /* no siblings, go back through the parents */
5962 if (elem->parent == start) {
5963 /* we are done, no next element to process */
5964 break;
5965 }
5966 /* parent is already processed, go to its sibling */
5967 elem = elem->parent;
5968 next = elem->next;
5969 }
5970 }
5971 }
5972
5973 return LY_SUCCESS;
5974}
5975
5976/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02005977 * @brief Move context @p set to an attribute. Result is LYXP_SET_NODE_SET.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005978 * Indirectly context position aware.
5979 *
5980 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005981 * @param[in] mod Matching metadata module, NULL for any.
5982 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
aPiecek8b0cc152021-05-31 16:40:31 +02005983 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005984 * @return LY_ERR
5985 */
5986static LY_ERR
aPiecek8b0cc152021-05-31 16:40:31 +02005987moveto_attr(struct lyxp_set *set, const struct lys_module *mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005988{
Michal Vasko9f96a052020-03-10 09:41:45 +01005989 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005990
aPiecek8b0cc152021-05-31 16:40:31 +02005991 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005992 return LY_SUCCESS;
5993 }
5994
5995 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005996 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005997 return LY_EVALID;
5998 }
5999
Radek Krejci1deb5be2020-08-26 16:43:36 +02006000 for (uint32_t i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02006001 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006002
6003 /* only attributes of an elem (not dummy) can be in the result, skip all the rest;
6004 * our attributes are always qualified */
Michal Vasko5c4e5892019-11-14 12:31:38 +01006005 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01006006 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006007
6008 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02006009 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006010 continue;
6011 }
6012
Michal Vaskod3678892020-05-21 10:06:58 +02006013 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006014 /* match */
6015 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01006016 set->val.meta[i].meta = sub;
6017 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006018 /* pos does not change */
6019 replaced = 1;
6020 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01006021 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 +02006022 }
6023 ++i;
6024 }
6025 }
6026 }
6027
6028 if (!replaced) {
6029 /* no match */
6030 set_remove_node(set, i);
6031 }
6032 }
6033
6034 return LY_SUCCESS;
6035}
6036
6037/**
6038 * @brief Move context @p set1 to union with @p set2. @p set2 is emptied afterwards.
Michal Vasko61ac2f62020-05-25 12:39:51 +02006039 * Result is LYXP_SET_NODE_SET. Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006040 *
6041 * @param[in,out] set1 Set to use for the result.
6042 * @param[in] set2 Set that is copied to @p set1.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006043 * @return LY_ERR
6044 */
6045static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006046moveto_union(struct lyxp_set *set1, struct lyxp_set *set2)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006047{
6048 LY_ERR rc;
6049
Michal Vaskod3678892020-05-21 10:06:58 +02006050 if ((set1->type != LYXP_SET_NODE_SET) || (set2->type != LYXP_SET_NODE_SET)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006051 LOGVAL(set1->ctx, LY_VCODE_XP_INOP_2, "union", print_set_type(set1), print_set_type(set2));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006052 return LY_EVALID;
6053 }
6054
6055 /* set2 is empty or both set1 and set2 */
Michal Vaskod3678892020-05-21 10:06:58 +02006056 if (!set2->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006057 return LY_SUCCESS;
6058 }
6059
Michal Vaskod3678892020-05-21 10:06:58 +02006060 if (!set1->used) {
aPiecekadc1e4f2021-10-07 11:15:12 +02006061 /* release hidden allocated data (lyxp_set.size) */
6062 lyxp_set_free_content(set1);
6063 /* direct copying of the entire structure */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006064 memcpy(set1, set2, sizeof *set1);
6065 /* dynamic memory belongs to set1 now, do not free */
Michal Vaskod3678892020-05-21 10:06:58 +02006066 memset(set2, 0, sizeof *set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006067 return LY_SUCCESS;
6068 }
6069
6070 /* we assume sets are sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006071 assert(!set_sort(set1) && !set_sort(set2));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006072
6073 /* sort, remove duplicates */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006074 rc = set_sorted_merge(set1, set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006075 LY_CHECK_RET(rc);
6076
6077 /* final set must be sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006078 assert(!set_sort(set1));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006079
6080 return LY_SUCCESS;
6081}
6082
6083/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02006084 * @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 +02006085 * Context position aware.
6086 *
6087 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02006088 * @param[in] mod Matching metadata module, NULL for any.
6089 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
Michal Vaskocdad7122020-11-09 21:04:44 +01006090 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006091 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6092 */
6093static int
Michal Vaskocdad7122020-11-09 21:04:44 +01006094moveto_attr_alldesc(struct lyxp_set *set, const struct lys_module *mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006095{
Michal Vasko9f96a052020-03-10 09:41:45 +01006096 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006097 struct lyxp_set *set_all_desc = NULL;
6098 LY_ERR rc;
6099
aPiecek8b0cc152021-05-31 16:40:31 +02006100 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006101 return LY_SUCCESS;
6102 }
6103
6104 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006105 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006106 return LY_EVALID;
6107 }
6108
Michal Vasko03ff5a72019-09-11 13:49:33 +02006109 /* can be optimized similarly to moveto_node_alldesc() and save considerable amount of memory,
6110 * but it likely won't be used much, so it's a waste of time */
6111 /* copy the context */
6112 set_all_desc = set_copy(set);
6113 /* get all descendant nodes (the original context nodes are removed) */
Michal Vaskocdad7122020-11-09 21:04:44 +01006114 rc = moveto_node_alldesc(set_all_desc, NULL, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006115 if (rc != LY_SUCCESS) {
6116 lyxp_set_free(set_all_desc);
6117 return rc;
6118 }
6119 /* prepend the original context nodes */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006120 rc = moveto_union(set, set_all_desc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006121 if (rc != LY_SUCCESS) {
6122 lyxp_set_free(set_all_desc);
6123 return rc;
6124 }
6125 lyxp_set_free(set_all_desc);
6126
Radek Krejci1deb5be2020-08-26 16:43:36 +02006127 for (uint32_t i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02006128 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006129
6130 /* only attributes of an elem can be in the result, skip all the rest,
6131 * we have all attributes qualified in lyd tree */
6132 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01006133 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006134 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02006135 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006136 continue;
6137 }
6138
Michal Vaskod3678892020-05-21 10:06:58 +02006139 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006140 /* match */
6141 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01006142 set->val.meta[i].meta = sub;
6143 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006144 /* pos does not change */
6145 replaced = 1;
6146 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01006147 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 +02006148 }
6149 ++i;
6150 }
6151 }
6152 }
6153
6154 if (!replaced) {
6155 /* no match */
6156 set_remove_node(set, i);
6157 }
6158 }
6159
6160 return LY_SUCCESS;
6161}
6162
6163/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02006164 * @brief Move context @p set to self and al chilren, recursively. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET.
6165 * Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006166 *
6167 * @param[in] parent Current parent.
6168 * @param[in] parent_pos Position of @p parent.
6169 * @param[in] parent_type Node type of @p parent.
6170 * @param[in,out] to_set Set to use.
6171 * @param[in] dup_check_set Set for checking duplicities.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006172 * @param[in] options XPath options.
6173 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6174 */
6175static LY_ERR
6176moveto_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 +02006177 struct lyxp_set *to_set, const struct lyxp_set *dup_check_set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006178{
Michal Vasko61ac2f62020-05-25 12:39:51 +02006179 const struct lyd_node *iter, *first;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006180 LY_ERR rc;
6181
6182 switch (parent_type) {
6183 case LYXP_NODE_ROOT:
6184 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko61ac2f62020-05-25 12:39:51 +02006185 assert(!parent);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006186
Michal Vasko61ac2f62020-05-25 12:39:51 +02006187 /* add all top-level nodes as elements */
6188 first = to_set->tree;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006189 break;
6190 case LYXP_NODE_ELEM:
Michal Vasko61ac2f62020-05-25 12:39:51 +02006191 /* add just the text node of this term element node */
6192 if (parent->schema->nodetype & (LYD_NODE_TERM | LYD_NODE_ANY)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006193 if (!set_dup_node_check(dup_check_set, parent, LYXP_NODE_TEXT, -1)) {
6194 set_insert_node(to_set, parent, parent_pos, LYXP_NODE_TEXT, to_set->used);
6195 }
Michal Vasko61ac2f62020-05-25 12:39:51 +02006196 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006197 }
Michal Vasko61ac2f62020-05-25 12:39:51 +02006198
6199 /* add all the children of this node */
Radek Krejcia1c1e542020-09-29 16:06:52 +02006200 first = lyd_child(parent);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006201 break;
6202 default:
6203 LOGINT_RET(parent->schema->module->ctx);
6204 }
6205
Michal Vasko61ac2f62020-05-25 12:39:51 +02006206 /* add all top-level nodes as elements */
6207 LY_LIST_FOR(first, iter) {
6208 /* context check */
6209 if ((parent_type == LYXP_NODE_ROOT_CONFIG) && (iter->schema->flags & LYS_CONFIG_R)) {
6210 continue;
6211 }
6212
6213 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01006214 if (!(options & LYXP_IGNORE_WHEN) && lysc_has_when(iter->schema) && !(iter->flags & LYD_WHEN_TRUE)) {
Michal Vasko61ac2f62020-05-25 12:39:51 +02006215 return LY_EINCOMPLETE;
6216 }
6217
6218 if (!set_dup_node_check(dup_check_set, iter, LYXP_NODE_ELEM, -1)) {
6219 set_insert_node(to_set, iter, 0, LYXP_NODE_ELEM, to_set->used);
6220
6221 /* also add all the children of this node, recursively */
6222 rc = moveto_self_add_children_r(iter, 0, LYXP_NODE_ELEM, to_set, dup_check_set, options);
6223 LY_CHECK_RET(rc);
6224 }
6225 }
6226
Michal Vasko03ff5a72019-09-11 13:49:33 +02006227 return LY_SUCCESS;
6228}
6229
6230/**
6231 * @brief Move context @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET
6232 * (or LYXP_SET_EMPTY). Context position aware.
6233 *
6234 * @param[in,out] set Set to use.
6235 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6236 * @param[in] options XPath options.
6237 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6238 */
6239static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006240moveto_self(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006241{
Michal Vasko03ff5a72019-09-11 13:49:33 +02006242 struct lyxp_set ret_set;
6243 LY_ERR rc;
6244
aPiecek8b0cc152021-05-31 16:40:31 +02006245 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006246 return LY_SUCCESS;
6247 }
6248
6249 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006250 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006251 return LY_EVALID;
6252 }
6253
6254 /* nothing to do */
6255 if (!all_desc) {
6256 return LY_SUCCESS;
6257 }
6258
Michal Vasko03ff5a72019-09-11 13:49:33 +02006259 /* add all the children, they get added recursively */
6260 set_init(&ret_set, set);
Radek Krejci1deb5be2020-08-26 16:43:36 +02006261 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006262 /* copy the current node to tmp */
6263 set_insert_node(&ret_set, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, ret_set.used);
6264
6265 /* do not touch attributes and text nodes */
Michal Vasko9f96a052020-03-10 09:41:45 +01006266 if ((set->val.nodes[i].type == LYXP_NODE_TEXT) || (set->val.nodes[i].type == LYXP_NODE_META)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006267 continue;
6268 }
6269
Michal Vasko03ff5a72019-09-11 13:49:33 +02006270 /* add all the children */
6271 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 +02006272 set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006273 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006274 lyxp_set_free_content(&ret_set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006275 return rc;
6276 }
6277 }
6278
6279 /* use the temporary set as the current one */
6280 ret_set.ctx_pos = set->ctx_pos;
6281 ret_set.ctx_size = set->ctx_size;
Michal Vaskod3678892020-05-21 10:06:58 +02006282 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006283 memcpy(set, &ret_set, sizeof *set);
6284
6285 return LY_SUCCESS;
6286}
6287
6288/**
6289 * @brief Move context schema @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_SCNODE_SET
6290 * (or LYXP_SET_EMPTY).
6291 *
6292 * @param[in,out] set Set to use.
6293 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6294 * @param[in] options XPath options.
6295 * @return LY_ERR
6296 */
6297static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006298moveto_scnode_self(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006299{
Radek Krejci1deb5be2020-08-26 16:43:36 +02006300 uint32_t getnext_opts;
6301 uint32_t mod_idx;
Michal Vasko519fd602020-05-26 12:17:39 +02006302 const struct lysc_node *iter, *start_parent;
6303 const struct lys_module *mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006304
aPiecek8b0cc152021-05-31 16:40:31 +02006305 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006306 return LY_SUCCESS;
6307 }
6308
6309 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006310 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006311 return LY_EVALID;
6312 }
6313
Michal Vasko519fd602020-05-26 12:17:39 +02006314 /* getnext opts */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01006315 getnext_opts = 0;
Michal Vasko519fd602020-05-26 12:17:39 +02006316 if (options & LYXP_SCNODE_OUTPUT) {
6317 getnext_opts |= LYS_GETNEXT_OUTPUT;
6318 }
6319
6320 /* add all the children, recursively as they are being added into the same set */
Radek Krejci1deb5be2020-08-26 16:43:36 +02006321 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vaskoe657f962020-12-10 12:19:48 +01006322 if (!all_desc) {
6323 /* traverse the start node */
6324 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START) {
6325 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
6326 }
6327 continue;
6328 }
6329
Radek Krejcif13b87b2020-12-01 22:02:17 +01006330 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
6331 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006332 continue;
6333 }
6334
Michal Vasko519fd602020-05-26 12:17:39 +02006335 /* remember context node */
Radek Krejcif13b87b2020-12-01 22:02:17 +01006336 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vasko519fd602020-05-26 12:17:39 +02006337 } else {
Michal Vasko1a09b212021-05-06 13:00:10 +02006338 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006339 }
6340
Michal Vasko519fd602020-05-26 12:17:39 +02006341 start_parent = set->val.scnodes[i].scnode;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006342
Michal Vasko519fd602020-05-26 12:17:39 +02006343 if ((set->val.scnodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.scnodes[i].type == LYXP_NODE_ROOT)) {
6344 /* it can actually be in any module, it's all <running> */
6345 mod_idx = 0;
Michal Vaskoa51ef072021-07-02 10:40:30 +02006346 while ((mod = ly_ctx_get_module_iter(set->ctx, &mod_idx))) {
Michal Vasko519fd602020-05-26 12:17:39 +02006347 iter = NULL;
6348 /* module may not be implemented */
6349 while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) {
6350 /* context check */
6351 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (iter->flags & LYS_CONFIG_R)) {
6352 continue;
6353 }
6354
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006355 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, NULL));
Michal Vasko519fd602020-05-26 12:17:39 +02006356 /* throw away the insert index, we want to consider that node again, recursively */
6357 }
6358 }
6359
6360 } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
6361 iter = NULL;
6362 while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006363 /* context check */
Michal Vasko519fd602020-05-26 12:17:39 +02006364 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (iter->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006365 continue;
6366 }
6367
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006368 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006369 }
6370 }
6371 }
6372
6373 return LY_SUCCESS;
6374}
6375
6376/**
6377 * @brief Move context @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_NODE_SET
6378 * (or LYXP_SET_EMPTY). Context position aware.
6379 *
6380 * @param[in] set Set to use.
6381 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6382 * @param[in] options XPath options.
6383 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6384 */
6385static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006386moveto_parent(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006387{
Michal Vasko230cf972021-12-02 12:31:00 +01006388 LY_ERR rc = LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006389 struct lyd_node *node, *new_node;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006390 enum lyxp_node_type new_type;
Michal Vasko230cf972021-12-02 12:31:00 +01006391 struct lyxp_set result;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006392
aPiecek8b0cc152021-05-31 16:40:31 +02006393 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006394 return LY_SUCCESS;
6395 }
6396
6397 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006398 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006399 return LY_EVALID;
6400 }
6401
6402 if (all_desc) {
6403 /* <path>//.. == <path>//./.. */
6404 rc = moveto_self(set, 1, options);
6405 LY_CHECK_RET(rc);
6406 }
6407
Michal Vasko230cf972021-12-02 12:31:00 +01006408 /* init result set */
6409 set_init(&result, set);
6410
Radek Krejci1deb5be2020-08-26 16:43:36 +02006411 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006412 node = set->val.nodes[i].node;
6413
6414 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9e685082021-01-29 14:49:09 +01006415 new_node = lyd_parent(node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006416 } else if (set->val.nodes[i].type == LYXP_NODE_TEXT) {
6417 new_node = node;
Michal Vasko9f96a052020-03-10 09:41:45 +01006418 } else if (set->val.nodes[i].type == LYXP_NODE_META) {
6419 new_node = set->val.meta[i].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006420 if (!new_node) {
Michal Vasko230cf972021-12-02 12:31:00 +01006421 LOGINT(set->ctx);
6422 rc = LY_EINT;
6423 goto cleanup;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006424 }
6425 } else {
6426 /* root does not have a parent */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006427 continue;
6428 }
6429
Michal Vaskoa1424542019-11-14 16:08:52 +01006430 /* when check */
Michal Vasko230cf972021-12-02 12:31:00 +01006431 if (!(options & LYXP_IGNORE_WHEN) && new_node && lysc_has_when(new_node->schema) &&
6432 !(new_node->flags & LYD_WHEN_TRUE)) {
6433 rc = LY_EINCOMPLETE;
6434 goto cleanup;
Michal Vaskoa1424542019-11-14 16:08:52 +01006435 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006436
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006437 if (!new_node) {
Radek Krejcif6a11002020-08-21 13:29:07 +02006438 /* node already there can also be the root */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006439 new_type = set->root_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006440 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02006441 /* 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 +02006442 new_type = LYXP_NODE_ELEM;
6443 }
6444
Michal Vasko230cf972021-12-02 12:31:00 +01006445 /* check for duplicates, several nodes may have the same parent */
6446 if (!set_dup_node_check(&result, new_node, new_type, -1)) {
6447 set_insert_node(&result, new_node, 0, new_type, result.used);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006448 }
6449 }
6450
Michal Vasko230cf972021-12-02 12:31:00 +01006451 /* move result to the set */
6452 lyxp_set_free_content(set);
6453 *set = result;
6454 result.type = LYXP_SET_NUMBER;
6455 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006456
Michal Vasko230cf972021-12-02 12:31:00 +01006457cleanup:
6458 lyxp_set_free_content(&result);
6459 return rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006460}
6461
6462/**
6463 * @brief Move context schema @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_SCNODE_SET
6464 * (or LYXP_SET_EMPTY).
6465 *
6466 * @param[in] set Set to use.
6467 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6468 * @param[in] options XPath options.
6469 * @return LY_ERR
6470 */
6471static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006472moveto_scnode_parent(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006473{
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006474 uint32_t i, orig_used, idx;
Radek Krejci857189e2020-09-01 13:26:36 +02006475 ly_bool temp_ctx = 0;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006476 const struct lysc_node *node, *new_node;
6477 enum lyxp_node_type new_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006478
aPiecek8b0cc152021-05-31 16:40:31 +02006479 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006480 return LY_SUCCESS;
6481 }
6482
6483 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006484 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006485 return LY_EVALID;
6486 }
6487
6488 if (all_desc) {
6489 /* <path>//.. == <path>//./.. */
Radek Krejci1deb5be2020-08-26 16:43:36 +02006490 LY_CHECK_RET(moveto_scnode_self(set, 1, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006491 }
6492
Michal Vasko03ff5a72019-09-11 13:49:33 +02006493 orig_used = set->used;
6494 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006495 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
6496 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006497 continue;
6498 }
6499
6500 /* remember context node */
Radek Krejcif13b87b2020-12-01 22:02:17 +01006501 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vaskoec4df482019-12-16 10:02:18 +01006502 } else {
Michal Vasko1a09b212021-05-06 13:00:10 +02006503 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006504 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006505
6506 node = set->val.scnodes[i].scnode;
6507
6508 if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
Michal Vaskod3678892020-05-21 10:06:58 +02006509 new_node = lysc_data_parent(node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006510 } else {
6511 /* root does not have a parent */
6512 continue;
6513 }
6514
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006515 if (!new_node) {
Radek Krejcif6a11002020-08-21 13:29:07 +02006516 /* node has no parent */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006517 new_type = set->root_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006518
Michal Vasko03ff5a72019-09-11 13:49:33 +02006519 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02006520 /* 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 +02006521 new_type = LYXP_NODE_ELEM;
6522 }
6523
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006524 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, new_node, new_type, &idx));
6525 if ((idx < orig_used) && (idx > i)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006526 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006527 temp_ctx = 1;
6528 }
6529 }
6530
6531 if (temp_ctx) {
6532 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006533 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_NEW_CTX) {
6534 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006535 }
6536 }
6537 }
6538
6539 return LY_SUCCESS;
6540}
6541
6542/**
6543 * @brief Move context @p set to the result of a comparison. Handles '=', '!=', '<=', '<', '>=', or '>'.
6544 * Result is LYXP_SET_BOOLEAN. Indirectly context position aware.
6545 *
6546 * @param[in,out] set1 Set to use for the result.
6547 * @param[in] set2 Set acting as the second operand for @p op.
6548 * @param[in] op Comparison operator to process.
6549 * @param[in] options XPath options.
6550 * @return LY_ERR
6551 */
6552static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02006553moveto_op_comp(struct lyxp_set *set1, struct lyxp_set *set2, const char *op, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006554{
6555 /*
6556 * NODE SET + NODE SET = NODE SET + STRING /(1 NODE SET) 2 STRING
6557 * NODE SET + STRING = STRING + STRING /1 STRING (2 STRING)
6558 * NODE SET + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6559 * NODE SET + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6560 * STRING + NODE SET = STRING + STRING /(1 STRING) 2 STRING
6561 * NUMBER + NODE SET = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6562 * BOOLEAN + NODE SET = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6563 *
6564 * '=' or '!='
6565 * BOOLEAN + BOOLEAN
6566 * BOOLEAN + STRING = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6567 * BOOLEAN + NUMBER = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6568 * STRING + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6569 * NUMBER + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6570 * NUMBER + NUMBER
6571 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6572 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6573 * STRING + STRING
6574 *
6575 * '<=', '<', '>=', '>'
6576 * NUMBER + NUMBER
6577 * BOOLEAN + BOOLEAN = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6578 * BOOLEAN + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6579 * BOOLEAN + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6580 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6581 * STRING + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6582 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6583 * NUMBER + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6584 * STRING + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6585 */
6586 struct lyxp_set iter1, iter2;
6587 int result;
6588 int64_t i;
6589 LY_ERR rc;
6590
Michal Vasko004d3152020-06-11 19:59:22 +02006591 memset(&iter1, 0, sizeof iter1);
6592 memset(&iter2, 0, sizeof iter2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006593
6594 /* iterative evaluation with node-sets */
6595 if ((set1->type == LYXP_SET_NODE_SET) || (set2->type == LYXP_SET_NODE_SET)) {
6596 if (set1->type == LYXP_SET_NODE_SET) {
6597 for (i = 0; i < set1->used; ++i) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02006598 /* cast set1 */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006599 switch (set2->type) {
6600 case LYXP_SET_NUMBER:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006601 rc = set_comp_cast(&iter1, set1, LYXP_SET_NUMBER, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006602 break;
6603 case LYXP_SET_BOOLEAN:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006604 rc = set_comp_cast(&iter1, set1, LYXP_SET_BOOLEAN, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006605 break;
6606 default:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006607 rc = set_comp_cast(&iter1, set1, LYXP_SET_STRING, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006608 break;
6609 }
6610 LY_CHECK_RET(rc);
6611
Michal Vasko4c7763f2020-07-27 17:40:37 +02006612 /* canonize set2 */
6613 LY_CHECK_ERR_RET(rc = set_comp_canonize(&iter2, set2, &set1->val.nodes[i]), lyxp_set_free_content(&iter1), rc);
6614
6615 /* compare recursively */
6616 rc = moveto_op_comp(&iter1, &iter2, op, options);
6617 lyxp_set_free_content(&iter2);
6618 LY_CHECK_ERR_RET(rc, lyxp_set_free_content(&iter1), rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006619
6620 /* lazy evaluation until true */
Michal Vasko004d3152020-06-11 19:59:22 +02006621 if (iter1.val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006622 set_fill_boolean(set1, 1);
6623 return LY_SUCCESS;
6624 }
6625 }
6626 } else {
6627 for (i = 0; i < set2->used; ++i) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02006628 /* set set2 */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006629 switch (set1->type) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02006630 case LYXP_SET_NUMBER:
6631 rc = set_comp_cast(&iter2, set2, LYXP_SET_NUMBER, i);
6632 break;
6633 case LYXP_SET_BOOLEAN:
6634 rc = set_comp_cast(&iter2, set2, LYXP_SET_BOOLEAN, i);
6635 break;
6636 default:
6637 rc = set_comp_cast(&iter2, set2, LYXP_SET_STRING, i);
6638 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006639 }
6640 LY_CHECK_RET(rc);
6641
Michal Vasko4c7763f2020-07-27 17:40:37 +02006642 /* canonize set1 */
6643 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 +02006644
Michal Vasko4c7763f2020-07-27 17:40:37 +02006645 /* compare recursively */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006646 rc = moveto_op_comp(&iter1, &iter2, op, options);
Michal Vaskod3678892020-05-21 10:06:58 +02006647 lyxp_set_free_content(&iter2);
Michal Vasko4c7763f2020-07-27 17:40:37 +02006648 LY_CHECK_ERR_RET(rc, lyxp_set_free_content(&iter1), rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006649
6650 /* lazy evaluation until true */
Michal Vasko004d3152020-06-11 19:59:22 +02006651 if (iter1.val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006652 set_fill_boolean(set1, 1);
6653 return LY_SUCCESS;
6654 }
6655 }
6656 }
6657
6658 /* false for all nodes */
6659 set_fill_boolean(set1, 0);
6660 return LY_SUCCESS;
6661 }
6662
6663 /* first convert properly */
6664 if ((op[0] == '=') || (op[0] == '!')) {
6665 if ((set1->type == LYXP_SET_BOOLEAN) || (set2->type == LYXP_SET_BOOLEAN)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006666 lyxp_set_cast(set1, LYXP_SET_BOOLEAN);
6667 lyxp_set_cast(set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006668 } else if ((set1->type == LYXP_SET_NUMBER) || (set2->type == LYXP_SET_NUMBER)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006669 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006670 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006671 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006672 LY_CHECK_RET(rc);
6673 } /* else we have 2 strings */
6674 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006675 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006676 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006677 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006678 LY_CHECK_RET(rc);
6679 }
6680
6681 assert(set1->type == set2->type);
6682
6683 /* compute result */
6684 if (op[0] == '=') {
6685 if (set1->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +02006686 result = (set1->val.bln == set2->val.bln);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006687 } else if (set1->type == LYXP_SET_NUMBER) {
6688 result = (set1->val.num == set2->val.num);
6689 } else {
6690 assert(set1->type == LYXP_SET_STRING);
Michal Vaskoac6c72f2019-11-14 16:09:34 +01006691 result = !strcmp(set1->val.str, set2->val.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006692 }
6693 } else if (op[0] == '!') {
6694 if (set1->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +02006695 result = (set1->val.bln != set2->val.bln);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006696 } else if (set1->type == LYXP_SET_NUMBER) {
6697 result = (set1->val.num != set2->val.num);
6698 } else {
6699 assert(set1->type == LYXP_SET_STRING);
Michal Vaskoc2058432020-11-06 17:26:21 +01006700 result = strcmp(set1->val.str, set2->val.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006701 }
6702 } else {
6703 assert(set1->type == LYXP_SET_NUMBER);
6704 if (op[0] == '<') {
6705 if (op[1] == '=') {
6706 result = (set1->val.num <= set2->val.num);
6707 } else {
6708 result = (set1->val.num < set2->val.num);
6709 }
6710 } else {
6711 if (op[1] == '=') {
6712 result = (set1->val.num >= set2->val.num);
6713 } else {
6714 result = (set1->val.num > set2->val.num);
6715 }
6716 }
6717 }
6718
6719 /* assign result */
6720 if (result) {
6721 set_fill_boolean(set1, 1);
6722 } else {
6723 set_fill_boolean(set1, 0);
6724 }
6725
6726 return LY_SUCCESS;
6727}
6728
6729/**
6730 * @brief Move context @p set to the result of a basic operation. Handles '+', '-', unary '-', '*', 'div',
6731 * or 'mod'. Result is LYXP_SET_NUMBER. Indirectly context position aware.
6732 *
6733 * @param[in,out] set1 Set to use for the result.
6734 * @param[in] set2 Set acting as the second operand for @p op.
6735 * @param[in] op Operator to process.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006736 * @return LY_ERR
6737 */
6738static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006739moveto_op_math(struct lyxp_set *set1, struct lyxp_set *set2, const char *op)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006740{
6741 LY_ERR rc;
6742
6743 /* unary '-' */
6744 if (!set2 && (op[0] == '-')) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006745 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006746 LY_CHECK_RET(rc);
6747 set1->val.num *= -1;
6748 lyxp_set_free(set2);
6749 return LY_SUCCESS;
6750 }
6751
6752 assert(set1 && set2);
6753
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006754 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006755 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006756 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006757 LY_CHECK_RET(rc);
6758
6759 switch (op[0]) {
6760 /* '+' */
6761 case '+':
6762 set1->val.num += set2->val.num;
6763 break;
6764
6765 /* '-' */
6766 case '-':
6767 set1->val.num -= set2->val.num;
6768 break;
6769
6770 /* '*' */
6771 case '*':
6772 set1->val.num *= set2->val.num;
6773 break;
6774
6775 /* 'div' */
6776 case 'd':
6777 set1->val.num /= set2->val.num;
6778 break;
6779
6780 /* 'mod' */
6781 case 'm':
6782 set1->val.num = ((long long)set1->val.num) % ((long long)set2->val.num);
6783 break;
6784
6785 default:
6786 LOGINT_RET(set1->ctx);
6787 }
6788
6789 return LY_SUCCESS;
6790}
6791
Michal Vasko03ff5a72019-09-11 13:49:33 +02006792/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02006793 * @brief Evaluate Predicate. Logs directly on error.
6794 *
Michal Vaskod3678892020-05-21 10:06:58 +02006795 * [9] Predicate ::= '[' Expr ']'
Michal Vasko03ff5a72019-09-11 13:49:33 +02006796 *
6797 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02006798 * @param[in] tok_idx Position in the expression @p exp.
aPiecek8b0cc152021-05-31 16:40:31 +02006799 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006800 * @param[in] options XPath options.
6801 * @param[in] parent_pos_pred Whether parent predicate was a positional one.
6802 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6803 */
6804static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02006805eval_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 +02006806{
6807 LY_ERR rc;
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01006808 uint16_t orig_exp;
6809 uint32_t i, orig_pos, orig_size;
Michal Vasko5c4e5892019-11-14 12:31:38 +01006810 int32_t pred_in_ctx;
aPiecekfff4dca2021-10-07 10:59:53 +02006811 struct lyxp_set set2 = {0};
Michal Vasko03ff5a72019-09-11 13:49:33 +02006812 struct lyd_node *orig_parent;
6813
6814 /* '[' */
aPiecek8b0cc152021-05-31 16:40:31 +02006815 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02006816 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02006817 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006818
aPiecek8b0cc152021-05-31 16:40:31 +02006819 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006820only_parse:
aPiecek8b0cc152021-05-31 16:40:31 +02006821 rc = eval_expr_select(exp, tok_idx, 0, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006822 LY_CHECK_RET(rc);
6823 } else if (set->type == LYXP_SET_NODE_SET) {
6824 /* 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 +01006825 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006826
6827 /* empty set, nothing to evaluate */
6828 if (!set->used) {
6829 goto only_parse;
6830 }
6831
Michal Vasko004d3152020-06-11 19:59:22 +02006832 orig_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006833 orig_pos = 0;
6834 orig_size = set->used;
6835 orig_parent = NULL;
Michal Vasko39dbf352020-05-21 10:08:59 +02006836 for (i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006837 set_init(&set2, set);
6838 set_insert_node(&set2, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, 0);
6839 /* remember the node context position for position() and context size for last(),
6840 * predicates should always be evaluated with respect to the child axis (since we do
6841 * not support explicit axes) so we assign positions based on their parents */
Michal Vasko9e685082021-01-29 14:49:09 +01006842 if (parent_pos_pred && (lyd_parent(set->val.nodes[i].node) != orig_parent)) {
6843 orig_parent = lyd_parent(set->val.nodes[i].node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006844 orig_pos = 1;
6845 } else {
6846 ++orig_pos;
6847 }
6848
6849 set2.ctx_pos = orig_pos;
6850 set2.ctx_size = orig_size;
Michal Vasko004d3152020-06-11 19:59:22 +02006851 *tok_idx = orig_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006852
Michal Vasko004d3152020-06-11 19:59:22 +02006853 rc = eval_expr_select(exp, tok_idx, 0, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006854 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006855 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006856 return rc;
6857 }
6858
6859 /* number is a position */
6860 if (set2.type == LYXP_SET_NUMBER) {
6861 if ((long long)set2.val.num == orig_pos) {
6862 set2.val.num = 1;
6863 } else {
6864 set2.val.num = 0;
6865 }
6866 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006867 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006868
6869 /* predicate satisfied or not? */
Michal Vasko004d3152020-06-11 19:59:22 +02006870 if (!set2.val.bln) {
Michal Vasko2caefc12019-11-14 16:07:56 +01006871 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006872 }
6873 }
Michal Vasko2caefc12019-11-14 16:07:56 +01006874 set_remove_nodes_none(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006875
6876 } else if (set->type == LYXP_SET_SCNODE_SET) {
6877 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006878 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006879 /* there is a currently-valid node */
6880 break;
6881 }
6882 }
6883 /* empty set, nothing to evaluate */
6884 if (i == set->used) {
6885 goto only_parse;
6886 }
6887
Michal Vasko004d3152020-06-11 19:59:22 +02006888 orig_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006889
Michal Vasko03ff5a72019-09-11 13:49:33 +02006890 /* set special in_ctx to all the valid snodes */
6891 pred_in_ctx = set_scnode_new_in_ctx(set);
6892
6893 /* use the valid snodes one-by-one */
6894 for (i = 0; i < set->used; ++i) {
6895 if (set->val.scnodes[i].in_ctx != pred_in_ctx) {
6896 continue;
6897 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01006898 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006899
Michal Vasko004d3152020-06-11 19:59:22 +02006900 *tok_idx = orig_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006901
Michal Vasko004d3152020-06-11 19:59:22 +02006902 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006903 LY_CHECK_RET(rc);
6904
6905 set->val.scnodes[i].in_ctx = pred_in_ctx;
6906 }
6907
6908 /* restore the state as it was before the predicate */
6909 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006910 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko1a09b212021-05-06 13:00:10 +02006911 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006912 } else if (set->val.scnodes[i].in_ctx == pred_in_ctx) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006913 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006914 }
6915 }
6916
6917 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02006918 set2.type = LYXP_SET_NODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006919 set_fill_set(&set2, set);
6920
Michal Vasko004d3152020-06-11 19:59:22 +02006921 rc = eval_expr_select(exp, tok_idx, 0, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006922 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006923 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006924 return rc;
6925 }
6926
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006927 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +02006928 if (!set2.val.bln) {
Michal Vaskod3678892020-05-21 10:06:58 +02006929 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006930 }
Michal Vaskod3678892020-05-21 10:06:58 +02006931 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006932 }
6933
6934 /* ']' */
Michal Vasko004d3152020-06-11 19:59:22 +02006935 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_BRACK2);
aPiecek8b0cc152021-05-31 16:40:31 +02006936 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02006937 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02006938 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006939
6940 return LY_SUCCESS;
6941}
6942
6943/**
Michal Vaskod3678892020-05-21 10:06:58 +02006944 * @brief Evaluate Literal. Logs directly on error.
6945 *
6946 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02006947 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02006948 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
6949 */
6950static void
Michal Vasko40308e72020-10-20 16:38:40 +02006951eval_literal(const struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set)
Michal Vaskod3678892020-05-21 10:06:58 +02006952{
6953 if (set) {
Michal Vasko004d3152020-06-11 19:59:22 +02006954 if (exp->tok_len[*tok_idx] == 2) {
Michal Vaskod3678892020-05-21 10:06:58 +02006955 set_fill_string(set, "", 0);
6956 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02006957 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 +02006958 }
6959 }
6960 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02006961 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02006962 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02006963}
6964
6965/**
Michal Vasko004d3152020-06-11 19:59:22 +02006966 * @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 +02006967 *
Michal Vasko004d3152020-06-11 19:59:22 +02006968 * @param[in] exp Full parsed XPath expression.
6969 * @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 +02006970 * @param[in] ctx_node Found schema node as the context for the predicate.
6971 * @param[in] cur_mod Current module for the expression.
6972 * @param[in] cur_node Current (original context) node.
Michal Vasko004d3152020-06-11 19:59:22 +02006973 * @param[in] format Format of any prefixes in key names/values.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006974 * @param[in] prefix_data Format-specific prefix data (see ::ly_resolve_prefix).
Michal Vasko004d3152020-06-11 19:59:22 +02006975 * @param[out] predicates Parsed predicates.
6976 * @param[out] pred_type Type of @p predicates.
6977 * @return LY_SUCCESS on success,
6978 * @return LY_ERR on any error.
Michal Vaskod3678892020-05-21 10:06:58 +02006979 */
6980static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02006981eval_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 +02006982 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 +02006983 struct ly_path_predicate **predicates, enum ly_path_pred_type *pred_type)
Michal Vaskod3678892020-05-21 10:06:58 +02006984{
Michal Vasko004d3152020-06-11 19:59:22 +02006985 LY_ERR ret = LY_SUCCESS;
6986 uint16_t key_count, e_idx, pred_idx = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02006987 const struct lysc_node *key;
Michal Vasko004d3152020-06-11 19:59:22 +02006988 size_t pred_len;
Radek Krejci1deb5be2020-08-26 16:43:36 +02006989 uint32_t prev_lo;
Michal Vasko004d3152020-06-11 19:59:22 +02006990 struct lyxp_expr *exp2 = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02006991
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006992 assert(ctx_node->nodetype & (LYS_LIST | LYS_LEAFLIST));
Michal Vaskod3678892020-05-21 10:06:58 +02006993
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006994 if (ctx_node->nodetype == LYS_LIST) {
Michal Vasko004d3152020-06-11 19:59:22 +02006995 /* get key count */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006996 if (ctx_node->flags & LYS_KEYLESS) {
Michal Vasko004d3152020-06-11 19:59:22 +02006997 return LY_EINVAL;
6998 }
Michal Vasko544e58a2021-01-28 14:33:41 +01006999 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 +02007000 assert(key_count);
Michal Vaskod3678892020-05-21 10:06:58 +02007001
Michal Vasko004d3152020-06-11 19:59:22 +02007002 /* learn where the predicates end */
7003 e_idx = *tok_idx;
7004 while (key_count) {
7005 /* '[' */
7006 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
7007 return LY_EINVAL;
7008 }
7009 ++e_idx;
7010
Michal Vasko3354d272021-04-06 09:40:06 +02007011 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_NAMETEST)) {
7012 /* definitely not a key */
7013 return LY_EINVAL;
7014 }
7015
Michal Vasko004d3152020-06-11 19:59:22 +02007016 /* ']' */
7017 while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) {
7018 ++e_idx;
7019 }
7020 ++e_idx;
7021
7022 /* another presumably key predicate parsed */
7023 --key_count;
7024 }
Michal Vasko004d3152020-06-11 19:59:22 +02007025 } else {
7026 /* learn just where this single predicate ends */
7027 e_idx = *tok_idx;
7028
Michal Vaskod3678892020-05-21 10:06:58 +02007029 /* '[' */
Michal Vasko004d3152020-06-11 19:59:22 +02007030 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
7031 return LY_EINVAL;
7032 }
7033 ++e_idx;
Michal Vaskod3678892020-05-21 10:06:58 +02007034
Michal Vasko3354d272021-04-06 09:40:06 +02007035 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_DOT)) {
7036 /* definitely not the value */
7037 return LY_EINVAL;
7038 }
7039
Michal Vaskod3678892020-05-21 10:06:58 +02007040 /* ']' */
Michal Vasko004d3152020-06-11 19:59:22 +02007041 while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) {
7042 ++e_idx;
7043 }
7044 ++e_idx;
Michal Vaskod3678892020-05-21 10:06:58 +02007045 }
7046
Michal Vasko004d3152020-06-11 19:59:22 +02007047 /* get the joined length of all the keys predicates/of the single leaf-list predicate */
7048 pred_len = (exp->tok_pos[e_idx - 1] + exp->tok_len[e_idx - 1]) - exp->tok_pos[*tok_idx];
7049
7050 /* turn logging off */
7051 prev_lo = ly_log_options(0);
7052
7053 /* parse the predicate(s) */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007054 LY_CHECK_GOTO(ret = ly_path_parse_predicate(ctx_node->module->ctx, ctx_node, exp->expr + exp->tok_pos[*tok_idx],
7055 pred_len, LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &exp2), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02007056
7057 /* compile */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007058 ret = ly_path_compile_predicate(ctx_node->module->ctx, cur_node, cur_mod, ctx_node, exp2, &pred_idx, format,
7059 prefix_data, predicates, pred_type);
Michal Vasko004d3152020-06-11 19:59:22 +02007060 LY_CHECK_GOTO(ret, cleanup);
7061
7062 /* success, the predicate must include all the needed information for hash-based search */
7063 *tok_idx = e_idx;
7064
7065cleanup:
7066 ly_log_options(prev_lo);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007067 lyxp_expr_free(ctx_node->module->ctx, exp2);
Michal Vasko004d3152020-06-11 19:59:22 +02007068 return ret;
Michal Vaskod3678892020-05-21 10:06:58 +02007069}
7070
7071/**
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007072 * @brief Search for/check the next schema node that could be the only matching schema node meaning the
7073 * data node(s) could be found using a single hash-based search.
7074 *
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007075 * @param[in] ctx libyang context.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007076 * @param[in] node Next context node to check.
7077 * @param[in] name Expected node name.
7078 * @param[in] name_len Length of @p name.
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007079 * @param[in] moveto_mod Expected node module, can be NULL for JSON format with no prefix.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007080 * @param[in] root_type XPath root type.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007081 * @param[in] format Prefix format.
7082 * @param[in,out] found Previously found node, is updated.
7083 * @return LY_SUCCESS on success,
7084 * @return LY_ENOT if the whole check failed and hashes cannot be used.
7085 */
7086static LY_ERR
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007087eval_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 +02007088 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 +02007089 const struct lysc_node **found)
7090{
7091 const struct lysc_node *scnode;
7092 const struct lys_module *mod;
7093 uint32_t idx = 0;
7094
Radek Krejci8df109d2021-04-23 12:19:08 +02007095 assert((format == LY_VALUE_JSON) || moveto_mod);
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007096
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007097continue_search:
Michal Vasko7d1d0912020-10-16 08:38:30 +02007098 scnode = NULL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007099 if (!node) {
Radek Krejci8df109d2021-04-23 12:19:08 +02007100 if ((format == LY_VALUE_JSON) && !moveto_mod) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007101 /* search all modules for a single match */
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007102 while ((mod = ly_ctx_get_module_iter(ctx, &idx))) {
Michal Vasko35a3b1d2021-07-14 09:34:16 +02007103 if (!mod->implemented) {
7104 continue;
7105 }
7106
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007107 scnode = lys_find_child(NULL, mod, name, name_len, 0, 0);
7108 if (scnode) {
7109 /* we have found a match */
7110 break;
7111 }
7112 }
7113
Michal Vasko7d1d0912020-10-16 08:38:30 +02007114 if (!scnode) {
7115 /* all modules searched */
7116 idx = 0;
7117 }
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007118 } else {
7119 /* search in top-level */
7120 scnode = lys_find_child(NULL, moveto_mod, name, name_len, 0, 0);
7121 }
7122 } else if (!*found || (lysc_data_parent(*found) != node->schema)) {
Radek Krejci8df109d2021-04-23 12:19:08 +02007123 if ((format == LY_VALUE_JSON) && !moveto_mod) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007124 /* we must adjust the module to inherit the one from the context node */
7125 moveto_mod = node->schema->module;
7126 }
7127
7128 /* search in children, do not repeat the same search */
7129 scnode = lys_find_child(node->schema, moveto_mod, name, name_len, 0, 0);
Michal Vasko7d1d0912020-10-16 08:38:30 +02007130 } /* else skip redundant search */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007131
7132 /* additional context check */
7133 if (scnode && (root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) {
7134 scnode = NULL;
7135 }
7136
7137 if (scnode) {
7138 if (*found) {
7139 /* we found a schema node with the same name but at different level, give up, too complicated
7140 * (more hash-based searches would be required, not supported) */
7141 return LY_ENOT;
7142 } else {
7143 /* remember the found schema node and continue to make sure it can be used */
7144 *found = scnode;
7145 }
7146 }
7147
7148 if (idx) {
7149 /* continue searching all the following models */
7150 goto continue_search;
7151 }
7152
7153 return LY_SUCCESS;
7154}
7155
7156/**
Michal Vasko4ad69e72021-10-26 16:25:55 +02007157 * @brief Generate message when no matching schema nodes were found for a path segment.
7158 *
7159 * @param[in] set XPath set.
7160 * @param[in] scparent Previous schema parent in the context, if only one.
7161 * @param[in] ncname XPath NCName being evaluated.
7162 * @param[in] ncname_len Length of @p ncname.
7163 * @param[in] expr Whole XPath expression.
7164 * @param[in] options XPath options.
7165 */
7166static void
7167eval_name_test_scnode_no_match_msg(struct lyxp_set *set, const struct lyxp_set_scnode *scparent, const char *ncname,
7168 uint16_t ncname_len, const char *expr, uint32_t options)
7169{
7170 const char *format;
7171 char *path = NULL, *ppath = NULL;
7172
7173 path = lysc_path(set->cur_scnode, LYSC_PATH_LOG, NULL, 0);
7174 if (scparent) {
7175 /* generate path for the parent */
7176 if (scparent->type == LYXP_NODE_ELEM) {
7177 ppath = lysc_path(scparent->scnode, LYSC_PATH_LOG, NULL, 0);
7178 } else if (scparent->type == LYXP_NODE_ROOT) {
7179 ppath = strdup("<root>");
7180 } else if (scparent->type == LYXP_NODE_ROOT_CONFIG) {
7181 ppath = strdup("<config-root>");
7182 }
7183 }
7184 if (ppath) {
7185 format = "Schema node \"%.*s\" for parent \"%s\" not found; in expr \"%.*s\" with context node \"%s\".";
7186 if (options & LYXP_SCNODE_ERROR) {
7187 LOGERR(set->ctx, LY_EVALID, format, ncname_len, ncname, ppath, (ncname - expr) + ncname_len, expr, path);
7188 } else {
7189 LOGWRN(set->ctx, format, ncname_len, ncname, ppath, (ncname - expr) + ncname_len, expr, path);
7190 }
7191 } else {
7192 format = "Schema node \"%.*s\" not found; in expr \"%.*s\" with context node \"%s\".";
7193 if (options & LYXP_SCNODE_ERROR) {
7194 LOGERR(set->ctx, LY_EVALID, format, ncname_len, ncname, (ncname - expr) + ncname_len, expr, path);
7195 } else {
7196 LOGWRN(set->ctx, format, ncname_len, ncname, (ncname - expr) + ncname_len, expr, path);
7197 }
7198 }
7199 free(path);
7200 free(ppath);
7201}
7202
7203/**
Michal Vaskod3678892020-05-21 10:06:58 +02007204 * @brief Evaluate NameTest and any following Predicates. Logs directly on error.
7205 *
7206 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
7207 * [6] NodeTest ::= NameTest | NodeType '(' ')'
7208 * [7] NameTest ::= '*' | NCName ':' '*' | QName
7209 *
7210 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007211 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02007212 * @param[in] attr_axis Whether to search attributes or standard nodes.
7213 * @param[in] all_desc Whether to search all the descendants or children only.
aPiecek8b0cc152021-05-31 16:40:31 +02007214 * @param[in,out] set Context and result set.
Michal Vaskod3678892020-05-21 10:06:58 +02007215 * @param[in] options XPath options.
Michal Vaskoa036a6b2021-10-12 16:05:23 +02007216 * @return LY_ERR (LY_EINCOMPLETE on unresolved when, LY_ENOT for not found schema node)
Michal Vaskod3678892020-05-21 10:06:58 +02007217 */
7218static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007219eval_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 +02007220 struct lyxp_set *set, uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02007221{
Michal Vaskoa036a6b2021-10-12 16:05:23 +02007222 LY_ERR rc = LY_SUCCESS, r;
Michal Vasko004d3152020-06-11 19:59:22 +02007223 const char *ncname, *ncname_dict = NULL;
7224 uint16_t ncname_len;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007225 const struct lys_module *moveto_mod = NULL;
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007226 const struct lysc_node *scnode = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02007227 struct ly_path_predicate *predicates = NULL;
7228 enum ly_path_pred_type pred_type = 0;
Michal Vaskoa036a6b2021-10-12 16:05:23 +02007229 int scnode_skip_pred = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02007230
aPiecek8b0cc152021-05-31 16:40:31 +02007231 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007232 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007233 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007234
aPiecek8b0cc152021-05-31 16:40:31 +02007235 if (options & LYXP_SKIP_EXPR) {
Michal Vaskod3678892020-05-21 10:06:58 +02007236 goto moveto;
7237 }
7238
Michal Vasko004d3152020-06-11 19:59:22 +02007239 ncname = &exp->expr[exp->tok_pos[*tok_idx - 1]];
7240 ncname_len = exp->tok_len[*tok_idx - 1];
Michal Vaskod3678892020-05-21 10:06:58 +02007241
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007242 if ((ncname[0] == '*') && (ncname_len == 1)) {
7243 /* all nodes will match */
7244 goto moveto;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007245 }
7246
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007247 /* parse (and skip) module name */
7248 rc = moveto_resolve_model(&ncname, &ncname_len, set, NULL, &moveto_mod);
Michal Vaskod3678892020-05-21 10:06:58 +02007249 LY_CHECK_GOTO(rc, cleanup);
7250
Radek Krejci8df109d2021-04-23 12:19:08 +02007251 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 +02007252 /* find the matching schema node in some parent in the context */
Radek Krejci1deb5be2020-08-26 16:43:36 +02007253 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007254 if (eval_name_test_with_predicate_get_scnode(set->ctx, set->val.nodes[i].node, ncname, ncname_len,
7255 moveto_mod, set->root_type, set->format, &scnode)) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007256 /* check failed */
7257 scnode = NULL;
7258 break;
Michal Vaskod3678892020-05-21 10:06:58 +02007259 }
7260 }
7261
Michal Vasko004d3152020-06-11 19:59:22 +02007262 if (scnode && (scnode->nodetype & (LYS_LIST | LYS_LEAFLIST))) {
7263 /* try to create the predicates */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007264 if (eval_name_test_try_compile_predicates(exp, tok_idx, scnode, set->cur_mod, set->cur_node ?
7265 set->cur_node->schema : NULL, set->format, set->prefix_data, &predicates, &pred_type)) {
Michal Vasko004d3152020-06-11 19:59:22 +02007266 /* hashes cannot be used */
Michal Vaskod3678892020-05-21 10:06:58 +02007267 scnode = NULL;
7268 }
7269 }
7270 }
7271
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007272 if (!scnode) {
7273 /* we are not able to match based on a schema node and not all the modules match ("*"),
Michal Vasko004d3152020-06-11 19:59:22 +02007274 * use dictionary for efficient comparison */
Radek Krejci011e4aa2020-09-04 15:22:31 +02007275 LY_CHECK_GOTO(rc = lydict_insert(set->ctx, ncname, ncname_len, &ncname_dict), cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02007276 }
7277
7278moveto:
7279 /* move to the attribute(s), data node(s), or schema node(s) */
7280 if (attr_axis) {
aPiecek8b0cc152021-05-31 16:40:31 +02007281 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007282 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskod3678892020-05-21 10:06:58 +02007283 } else {
7284 if (all_desc) {
Michal Vaskocdad7122020-11-09 21:04:44 +01007285 rc = moveto_attr_alldesc(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007286 } else {
aPiecek8b0cc152021-05-31 16:40:31 +02007287 rc = moveto_attr(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007288 }
7289 LY_CHECK_GOTO(rc, cleanup);
7290 }
7291 } else {
aPiecek8b0cc152021-05-31 16:40:31 +02007292 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02007293 int64_t i;
Michal Vaskoa036a6b2021-10-12 16:05:23 +02007294 const struct lyxp_set_scnode *scparent = NULL;
Michal Vaskoa036a6b2021-10-12 16:05:23 +02007295
7296 /* remember parent if there is only one, to print in the warning */
7297 for (i = 0; i < set->used; ++i) {
7298 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
7299 if (!scparent) {
7300 /* remember the context node */
7301 scparent = &set->val.scnodes[i];
7302 } else {
7303 /* several context nodes, no reasonable error possible */
7304 scparent = NULL;
7305 break;
7306 }
7307 }
7308 }
Radek Krejci1deb5be2020-08-26 16:43:36 +02007309
Michal Vaskod3678892020-05-21 10:06:58 +02007310 if (all_desc) {
Michal Vasko004d3152020-06-11 19:59:22 +02007311 rc = moveto_scnode_alldesc(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007312 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007313 rc = moveto_scnode(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007314 }
7315 LY_CHECK_GOTO(rc, cleanup);
7316
7317 for (i = set->used - 1; i > -1; --i) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007318 if (set->val.scnodes[i].in_ctx > LYXP_SET_SCNODE_ATOM_NODE) {
Michal Vaskod3678892020-05-21 10:06:58 +02007319 break;
7320 }
7321 }
7322 if (i == -1) {
Michal Vasko4ad69e72021-10-26 16:25:55 +02007323 /* generate message */
7324 eval_name_test_scnode_no_match_msg(set, scparent, ncname, ncname_len, exp->expr, options);
7325
7326 if (options & LYXP_SCNODE_ERROR) {
7327 /* error */
7328 rc = LY_EVALID;
7329 goto cleanup;
Michal Vaskoa036a6b2021-10-12 16:05:23 +02007330 }
Michal Vaskoa036a6b2021-10-12 16:05:23 +02007331
7332 /* skip the predicates and the rest of this path to not generate invalid warnings */
7333 rc = LY_ENOT;
7334 scnode_skip_pred = 1;
Michal Vaskod3678892020-05-21 10:06:58 +02007335 }
7336 } else {
7337 if (all_desc) {
Michal Vaskocdad7122020-11-09 21:04:44 +01007338 rc = moveto_node_alldesc(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007339 } else {
7340 if (scnode) {
7341 /* we can find the nodes using hashes */
Michal Vaskocdad7122020-11-09 21:04:44 +01007342 rc = moveto_node_hash(set, scnode, predicates, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007343 } else {
Michal Vaskocdad7122020-11-09 21:04:44 +01007344 rc = moveto_node(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007345 }
7346 }
7347 LY_CHECK_GOTO(rc, cleanup);
7348 }
7349 }
7350
Michal Vaskoa036a6b2021-10-12 16:05:23 +02007351 if (scnode_skip_pred) {
7352 /* skip predicates */
7353 options |= LYXP_SKIP_EXPR;
7354 }
7355
Michal Vaskod3678892020-05-21 10:06:58 +02007356 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007357 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
Michal Vaskoa036a6b2021-10-12 16:05:23 +02007358 r = eval_predicate(exp, tok_idx, set, options, 1);
7359 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02007360 }
7361
7362cleanup:
Michal Vaskoa036a6b2021-10-12 16:05:23 +02007363 if (scnode_skip_pred) {
7364 /* restore options */
7365 options &= ~LYXP_SKIP_EXPR;
7366 }
aPiecek8b0cc152021-05-31 16:40:31 +02007367 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko004d3152020-06-11 19:59:22 +02007368 lydict_remove(set->ctx, ncname_dict);
Michal Vaskof7e16e22020-10-21 09:24:39 +02007369 ly_path_predicates_free(set->ctx, pred_type, predicates);
Michal Vaskodb51a8d2020-05-27 15:22:29 +02007370 }
Michal Vaskod3678892020-05-21 10:06:58 +02007371 return rc;
7372}
7373
7374/**
7375 * @brief Evaluate NodeType and any following Predicates. Logs directly on error.
7376 *
7377 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
7378 * [6] NodeTest ::= NameTest | NodeType '(' ')'
7379 * [8] NodeType ::= 'text' | 'node'
7380 *
7381 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007382 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02007383 * @param[in] attr_axis Whether to search attributes or standard nodes.
7384 * @param[in] all_desc Whether to search all the descendants or children only.
aPiecek8b0cc152021-05-31 16:40:31 +02007385 * @param[in,out] set Context and result set.
Michal Vaskod3678892020-05-21 10:06:58 +02007386 * @param[in] options XPath options.
7387 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7388 */
7389static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007390eval_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 +02007391 struct lyxp_set *set, uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02007392{
7393 LY_ERR rc;
7394
7395 /* TODO */
7396 (void)attr_axis;
7397 (void)all_desc;
7398
aPiecek8b0cc152021-05-31 16:40:31 +02007399 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko004d3152020-06-11 19:59:22 +02007400 assert(exp->tok_len[*tok_idx] == 4);
Michal Vaskod3678892020-05-21 10:06:58 +02007401 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007402 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
aPiecek8b0cc152021-05-31 16:40:31 +02007403 options |= LYXP_SKIP_EXPR;
Michal Vaskod3678892020-05-21 10:06:58 +02007404 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007405 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "node", 4)) {
Michal Vaskod3678892020-05-21 10:06:58 +02007406 rc = xpath_node(NULL, 0, set, options);
7407 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007408 assert(!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "text", 4));
Michal Vaskod3678892020-05-21 10:06:58 +02007409 rc = xpath_text(NULL, 0, set, options);
7410 }
7411 LY_CHECK_RET(rc);
7412 }
7413 }
aPiecek8b0cc152021-05-31 16:40:31 +02007414 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007415 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007416 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007417
7418 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02007419 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
aPiecek8b0cc152021-05-31 16:40:31 +02007420 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007421 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007422 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007423
7424 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007425 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
aPiecek8b0cc152021-05-31 16:40:31 +02007426 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007427 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007428 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007429
7430 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007431 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7432 rc = eval_predicate(exp, tok_idx, set, options, 1);
Michal Vaskod3678892020-05-21 10:06:58 +02007433 LY_CHECK_RET(rc);
7434 }
7435
7436 return LY_SUCCESS;
7437}
7438
7439/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02007440 * @brief Evaluate RelativeLocationPath. Logs directly on error.
7441 *
7442 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
7443 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
Michal Vaskod3678892020-05-21 10:06:58 +02007444 * [6] NodeTest ::= NameTest | NodeType '(' ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02007445 *
7446 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007447 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007448 * @param[in] all_desc Whether to search all the descendants or children only.
aPiecek8b0cc152021-05-31 16:40:31 +02007449 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007450 * @param[in] options XPath options.
7451 * @return LY_ERR (YL_EINCOMPLETE on unresolved when)
7452 */
7453static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007454eval_relative_location_path(const struct lyxp_expr *exp, uint16_t *tok_idx, ly_bool all_desc, struct lyxp_set *set,
7455 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007456{
Radek Krejci857189e2020-09-01 13:26:36 +02007457 ly_bool attr_axis;
Michal Vaskoa036a6b2021-10-12 16:05:23 +02007458 LY_ERR rc = LY_SUCCESS;
7459 int scnode_skip_path = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007460
7461 goto step;
7462 do {
7463 /* evaluate '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02007464 if (exp->tok_len[*tok_idx] == 1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007465 all_desc = 0;
7466 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007467 assert(exp->tok_len[*tok_idx] == 2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007468 all_desc = 1;
7469 }
aPiecek8b0cc152021-05-31 16:40:31 +02007470 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007471 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007472 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007473
7474step:
Michal Vaskod3678892020-05-21 10:06:58 +02007475 /* evaluate abbreviated axis '@'? if any */
Michal Vasko004d3152020-06-11 19:59:22 +02007476 if (exp->tokens[*tok_idx] == LYXP_TOKEN_AT) {
Michal Vaskod3678892020-05-21 10:06:58 +02007477 attr_axis = 1;
7478
aPiecek8b0cc152021-05-31 16:40:31 +02007479 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007480 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007481 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007482 } else {
7483 attr_axis = 0;
7484 }
7485
Michal Vasko03ff5a72019-09-11 13:49:33 +02007486 /* Step */
Michal Vasko004d3152020-06-11 19:59:22 +02007487 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007488 case LYXP_TOKEN_DOT:
7489 /* evaluate '.' */
aPiecek8b0cc152021-05-31 16:40:31 +02007490 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007491 rc = moveto_scnode_self(set, all_desc, options);
7492 } else {
7493 rc = moveto_self(set, all_desc, options);
7494 }
Michal Vaskoa036a6b2021-10-12 16:05:23 +02007495 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007496 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007497 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007498 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007499 break;
7500
7501 case LYXP_TOKEN_DDOT:
7502 /* evaluate '..' */
aPiecek8b0cc152021-05-31 16:40:31 +02007503 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007504 rc = moveto_scnode_parent(set, all_desc, options);
7505 } else {
7506 rc = moveto_parent(set, all_desc, options);
7507 }
Michal Vaskoa036a6b2021-10-12 16:05:23 +02007508 LY_CHECK_GOTO(rc, cleanup);
aPiecek8b0cc152021-05-31 16:40:31 +02007509 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007510 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007511 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007512 break;
7513
Michal Vasko03ff5a72019-09-11 13:49:33 +02007514 case LYXP_TOKEN_NAMETEST:
Michal Vaskod3678892020-05-21 10:06:58 +02007515 /* evaluate NameTest Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007516 rc = eval_name_test_with_predicate(exp, tok_idx, attr_axis, all_desc, set, options);
Michal Vaskoa036a6b2021-10-12 16:05:23 +02007517 if (rc == LY_ENOT) {
7518 assert(options & LYXP_SCNODE_ALL);
7519 /* skip the rest of this path */
7520 rc = LY_SUCCESS;
7521 scnode_skip_path = 1;
7522 options |= LYXP_SKIP_EXPR;
7523 }
7524 LY_CHECK_GOTO(rc, cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02007525 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007526
Michal Vaskod3678892020-05-21 10:06:58 +02007527 case LYXP_TOKEN_NODETYPE:
7528 /* evaluate NodeType Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007529 rc = eval_node_type_with_predicate(exp, tok_idx, attr_axis, all_desc, set, options);
Michal Vaskoa036a6b2021-10-12 16:05:23 +02007530 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007531 break;
7532
7533 default:
Michal Vaskoa036a6b2021-10-12 16:05:23 +02007534 LOGINT(set->ctx);
7535 rc = LY_EINT;
7536 goto cleanup;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007537 }
Michal Vasko004d3152020-06-11 19:59:22 +02007538 } while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007539
Michal Vaskoa036a6b2021-10-12 16:05:23 +02007540cleanup:
7541 if (scnode_skip_path) {
7542 options &= ~LYXP_SKIP_EXPR;
7543 }
7544 return rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007545}
7546
7547/**
7548 * @brief Evaluate AbsoluteLocationPath. Logs directly on error.
7549 *
7550 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
7551 *
7552 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007553 * @param[in] tok_idx Position in the expression @p exp.
aPiecek8b0cc152021-05-31 16:40:31 +02007554 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007555 * @param[in] options XPath options.
7556 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7557 */
7558static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007559eval_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 +02007560{
Radek Krejci857189e2020-09-01 13:26:36 +02007561 ly_bool all_desc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007562
aPiecek8b0cc152021-05-31 16:40:31 +02007563 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007564 /* no matter what tokens follow, we need to be at the root */
Michal Vaskob0099a92020-08-31 14:55:23 +02007565 LY_CHECK_RET(moveto_root(set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007566 }
7567
7568 /* '/' RelativeLocationPath? */
Michal Vasko004d3152020-06-11 19:59:22 +02007569 if (exp->tok_len[*tok_idx] == 1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007570 /* evaluate '/' - deferred */
7571 all_desc = 0;
aPiecek8b0cc152021-05-31 16:40:31 +02007572 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007573 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007574 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007575
Michal Vasko004d3152020-06-11 19:59:22 +02007576 if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007577 return LY_SUCCESS;
7578 }
Michal Vasko004d3152020-06-11 19:59:22 +02007579 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007580 case LYXP_TOKEN_DOT:
7581 case LYXP_TOKEN_DDOT:
7582 case LYXP_TOKEN_AT:
7583 case LYXP_TOKEN_NAMETEST:
7584 case LYXP_TOKEN_NODETYPE:
Michal Vaskob0099a92020-08-31 14:55:23 +02007585 LY_CHECK_RET(eval_relative_location_path(exp, tok_idx, all_desc, set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007586 break;
7587 default:
7588 break;
7589 }
7590
Michal Vasko03ff5a72019-09-11 13:49:33 +02007591 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02007592 /* '//' RelativeLocationPath */
Michal Vasko03ff5a72019-09-11 13:49:33 +02007593 /* evaluate '//' - deferred so as not to waste memory by remembering all the nodes */
7594 all_desc = 1;
aPiecek8b0cc152021-05-31 16:40:31 +02007595 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007596 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007597 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007598
Michal Vaskob0099a92020-08-31 14:55:23 +02007599 LY_CHECK_RET(eval_relative_location_path(exp, tok_idx, all_desc, set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007600 }
7601
7602 return LY_SUCCESS;
7603}
7604
7605/**
7606 * @brief Evaluate FunctionCall. Logs directly on error.
7607 *
Michal Vaskod3678892020-05-21 10:06:58 +02007608 * [11] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02007609 *
7610 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007611 * @param[in] tok_idx Position in the expression @p exp.
aPiecek8b0cc152021-05-31 16:40:31 +02007612 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007613 * @param[in] options XPath options.
7614 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7615 */
7616static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007617eval_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 +02007618{
7619 LY_ERR rc;
Michal Vasko69730152020-10-09 16:30:07 +02007620
Radek Krejci1deb5be2020-08-26 16:43:36 +02007621 LY_ERR (*xpath_func)(struct lyxp_set **, uint16_t, struct lyxp_set *, uint32_t) = NULL;
Michal Vasko0cbf54f2019-12-16 10:01:06 +01007622 uint16_t arg_count = 0, i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007623 struct lyxp_set **args = NULL, **args_aux;
7624
aPiecek8b0cc152021-05-31 16:40:31 +02007625 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007626 /* FunctionName */
Michal Vasko004d3152020-06-11 19:59:22 +02007627 switch (exp->tok_len[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007628 case 3:
Michal Vasko004d3152020-06-11 19:59:22 +02007629 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007630 xpath_func = &xpath_not;
Michal Vasko004d3152020-06-11 19:59:22 +02007631 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007632 xpath_func = &xpath_sum;
7633 }
7634 break;
7635 case 4:
Michal Vasko004d3152020-06-11 19:59:22 +02007636 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007637 xpath_func = &xpath_lang;
Michal Vasko004d3152020-06-11 19:59:22 +02007638 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007639 xpath_func = &xpath_last;
Michal Vasko004d3152020-06-11 19:59:22 +02007640 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007641 xpath_func = &xpath_name;
Michal Vasko004d3152020-06-11 19:59:22 +02007642 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007643 xpath_func = &xpath_true;
7644 }
7645 break;
7646 case 5:
Michal Vasko004d3152020-06-11 19:59:22 +02007647 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007648 xpath_func = &xpath_count;
Michal Vasko004d3152020-06-11 19:59:22 +02007649 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007650 xpath_func = &xpath_false;
Michal Vasko004d3152020-06-11 19:59:22 +02007651 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007652 xpath_func = &xpath_floor;
Michal Vasko004d3152020-06-11 19:59:22 +02007653 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007654 xpath_func = &xpath_round;
Michal Vasko004d3152020-06-11 19:59:22 +02007655 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007656 xpath_func = &xpath_deref;
7657 }
7658 break;
7659 case 6:
Michal Vasko004d3152020-06-11 19:59:22 +02007660 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007661 xpath_func = &xpath_concat;
Michal Vasko004d3152020-06-11 19:59:22 +02007662 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007663 xpath_func = &xpath_number;
Michal Vasko004d3152020-06-11 19:59:22 +02007664 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007665 xpath_func = &xpath_string;
7666 }
7667 break;
7668 case 7:
Michal Vasko004d3152020-06-11 19:59:22 +02007669 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007670 xpath_func = &xpath_boolean;
Michal Vasko004d3152020-06-11 19:59:22 +02007671 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007672 xpath_func = &xpath_ceiling;
Michal Vasko004d3152020-06-11 19:59:22 +02007673 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007674 xpath_func = &xpath_current;
7675 }
7676 break;
7677 case 8:
Michal Vasko004d3152020-06-11 19:59:22 +02007678 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007679 xpath_func = &xpath_contains;
Michal Vasko004d3152020-06-11 19:59:22 +02007680 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007681 xpath_func = &xpath_position;
Michal Vasko004d3152020-06-11 19:59:22 +02007682 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "re-match", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007683 xpath_func = &xpath_re_match;
7684 }
7685 break;
7686 case 9:
Michal Vasko004d3152020-06-11 19:59:22 +02007687 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007688 xpath_func = &xpath_substring;
Michal Vasko004d3152020-06-11 19:59:22 +02007689 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007690 xpath_func = &xpath_translate;
7691 }
7692 break;
7693 case 10:
Michal Vasko004d3152020-06-11 19:59:22 +02007694 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007695 xpath_func = &xpath_local_name;
Michal Vasko004d3152020-06-11 19:59:22 +02007696 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007697 xpath_func = &xpath_enum_value;
Michal Vasko004d3152020-06-11 19:59:22 +02007698 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007699 xpath_func = &xpath_bit_is_set;
7700 }
7701 break;
7702 case 11:
Michal Vasko004d3152020-06-11 19:59:22 +02007703 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007704 xpath_func = &xpath_starts_with;
7705 }
7706 break;
7707 case 12:
Michal Vasko004d3152020-06-11 19:59:22 +02007708 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007709 xpath_func = &xpath_derived_from;
7710 }
7711 break;
7712 case 13:
Michal Vasko004d3152020-06-11 19:59:22 +02007713 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007714 xpath_func = &xpath_namespace_uri;
Michal Vasko004d3152020-06-11 19:59:22 +02007715 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string-length", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007716 xpath_func = &xpath_string_length;
7717 }
7718 break;
7719 case 15:
Michal Vasko004d3152020-06-11 19:59:22 +02007720 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007721 xpath_func = &xpath_normalize_space;
Michal Vasko004d3152020-06-11 19:59:22 +02007722 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-after", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007723 xpath_func = &xpath_substring_after;
7724 }
7725 break;
7726 case 16:
Michal Vasko004d3152020-06-11 19:59:22 +02007727 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007728 xpath_func = &xpath_substring_before;
7729 }
7730 break;
7731 case 20:
Michal Vasko004d3152020-06-11 19:59:22 +02007732 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from-or-self", 20)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007733 xpath_func = &xpath_derived_from_or_self;
7734 }
7735 break;
7736 }
7737
7738 if (!xpath_func) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01007739 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 +02007740 return LY_EVALID;
7741 }
7742 }
7743
aPiecek8b0cc152021-05-31 16:40:31 +02007744 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007745 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007746 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007747
7748 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02007749 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
aPiecek8b0cc152021-05-31 16:40:31 +02007750 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007751 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007752 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007753
7754 /* ( Expr ( ',' Expr )* )? */
Michal Vasko004d3152020-06-11 19:59:22 +02007755 if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) {
aPiecek8b0cc152021-05-31 16:40:31 +02007756 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007757 args = malloc(sizeof *args);
7758 LY_CHECK_ERR_GOTO(!args, LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
7759 arg_count = 1;
7760 args[0] = set_copy(set);
7761 if (!args[0]) {
7762 rc = LY_EMEM;
7763 goto cleanup;
7764 }
7765
Michal Vasko004d3152020-06-11 19:59:22 +02007766 rc = eval_expr_select(exp, tok_idx, 0, args[0], options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007767 LY_CHECK_GOTO(rc, cleanup);
7768 } else {
aPiecek8b0cc152021-05-31 16:40:31 +02007769 rc = eval_expr_select(exp, tok_idx, 0, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007770 LY_CHECK_GOTO(rc, cleanup);
7771 }
7772 }
Michal Vasko004d3152020-06-11 19:59:22 +02007773 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) {
aPiecek8b0cc152021-05-31 16:40:31 +02007774 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007775 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007776 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007777
aPiecek8b0cc152021-05-31 16:40:31 +02007778 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007779 ++arg_count;
7780 args_aux = realloc(args, arg_count * sizeof *args);
7781 LY_CHECK_ERR_GOTO(!args_aux, arg_count--; LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
7782 args = args_aux;
7783 args[arg_count - 1] = set_copy(set);
7784 if (!args[arg_count - 1]) {
7785 rc = LY_EMEM;
7786 goto cleanup;
7787 }
7788
Michal Vasko004d3152020-06-11 19:59:22 +02007789 rc = eval_expr_select(exp, tok_idx, 0, args[arg_count - 1], options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007790 LY_CHECK_GOTO(rc, cleanup);
7791 } else {
aPiecek8b0cc152021-05-31 16:40:31 +02007792 rc = eval_expr_select(exp, tok_idx, 0, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007793 LY_CHECK_GOTO(rc, cleanup);
7794 }
7795 }
7796
7797 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007798 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
aPiecek8b0cc152021-05-31 16:40:31 +02007799 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007800 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007801 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007802
aPiecek8b0cc152021-05-31 16:40:31 +02007803 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007804 /* evaluate function */
7805 rc = xpath_func(args, arg_count, set, options);
7806
7807 if (options & LYXP_SCNODE_ALL) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007808 /* merge all nodes from arg evaluations */
7809 for (i = 0; i < arg_count; ++i) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007810 set_scnode_clear_ctx(args[i], LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007811 lyxp_set_scnode_merge(set, args[i]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007812 }
7813 }
7814 } else {
7815 rc = LY_SUCCESS;
7816 }
7817
7818cleanup:
7819 for (i = 0; i < arg_count; ++i) {
7820 lyxp_set_free(args[i]);
7821 }
7822 free(args);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007823 return rc;
7824}
7825
7826/**
7827 * @brief Evaluate Number. Logs directly on error.
7828 *
7829 * @param[in] ctx Context for errors.
7830 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007831 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007832 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7833 * @return LY_ERR
7834 */
7835static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007836eval_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 +02007837{
7838 long double num;
7839 char *endptr;
7840
7841 if (set) {
7842 errno = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02007843 num = strtold(&exp->expr[exp->tok_pos[*tok_idx]], &endptr);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007844 if (errno) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01007845 LOGVAL(ctx, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]);
7846 LOGVAL(ctx, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double (%s).",
Michal Vasko69730152020-10-09 16:30:07 +02007847 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]], strerror(errno));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007848 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02007849 } else if (endptr - &exp->expr[exp->tok_pos[*tok_idx]] != exp->tok_len[*tok_idx]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01007850 LOGVAL(ctx, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]);
7851 LOGVAL(ctx, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double.",
Michal Vasko69730152020-10-09 16:30:07 +02007852 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007853 return LY_EVALID;
7854 }
7855
7856 set_fill_number(set, num);
7857 }
7858
7859 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007860 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007861 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007862 return LY_SUCCESS;
7863}
7864
aPiecekdf23eee2021-10-07 12:21:50 +02007865LY_ERR
7866lyxp_vars_find(struct lyxp_var *vars, const char *name, size_t name_len, struct lyxp_var **var)
7867{
7868 LY_ERR ret = LY_ENOTFOUND;
7869 LY_ARRAY_COUNT_TYPE u;
7870
7871 assert(vars && name);
7872
7873 name_len = name_len ? name_len : strlen(name);
7874
7875 LY_ARRAY_FOR(vars, u) {
7876 if (!strncmp(vars[u].name, name, name_len)) {
7877 ret = LY_SUCCESS;
7878 break;
7879 }
7880 }
7881
7882 if (var && !ret) {
7883 *var = &vars[u];
7884 }
7885
7886 return ret;
7887}
7888
Michal Vasko03ff5a72019-09-11 13:49:33 +02007889/**
aPiecekfba75362021-10-07 12:39:48 +02007890 * @brief Evaluate VariableReference.
7891 *
7892 * @param[in] exp Parsed XPath expression.
7893 * @param[in] tok_idx Position in the expression @p exp.
7894 * @param[in] vars [Sized array](@ref sizedarrays) of XPath variables.
7895 * @param[in,out] set Context and result set.
7896 * @param[in] options XPath options.
7897 * @return LY_ERR value.
7898 */
7899static LY_ERR
7900eval_variable_reference(const struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set, uint32_t options)
7901{
7902 LY_ERR ret;
7903 const char *name;
7904 struct lyxp_var *var;
7905 const struct lyxp_var *vars;
7906 struct lyxp_expr *tokens = NULL;
7907 uint16_t token_index;
7908
7909 vars = set->vars;
7910
7911 /* Find out the name and value of the variable. */
7912 name = &exp->expr[exp->tok_pos[*tok_idx]];
7913 ret = lyxp_vars_find((struct lyxp_var *)vars, name, exp->tok_len[*tok_idx], &var);
7914 LY_CHECK_ERR_RET(ret, LOGERR(set->ctx, ret,
7915 "XPath variable \"%s\" not defined.", name), ret);
7916
7917 /* Parse value. */
7918 ret = lyxp_expr_parse(set->ctx, var->value, 0, 1, &tokens);
7919 LY_CHECK_GOTO(ret, cleanup);
7920
7921 /* Evaluate value. */
7922 token_index = 0;
7923 ret = eval_expr_select(tokens, &token_index, 0, set, options);
7924 LY_CHECK_GOTO(ret, cleanup);
7925
7926cleanup:
7927 lyxp_expr_free(set->ctx, tokens);
7928
7929 return ret;
7930}
7931
7932/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02007933 * @brief Evaluate PathExpr. Logs directly on error.
7934 *
Michal Vaskod3678892020-05-21 10:06:58 +02007935 * [12] PathExpr ::= LocationPath | PrimaryExpr Predicate*
Michal Vasko03ff5a72019-09-11 13:49:33 +02007936 * | PrimaryExpr Predicate* '/' RelativeLocationPath
7937 * | PrimaryExpr Predicate* '//' RelativeLocationPath
7938 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
aPiecekfba75362021-10-07 12:39:48 +02007939 * [10] PrimaryExpr ::= VariableReference | '(' Expr ')' | Literal | Number | FunctionCall
Michal Vasko03ff5a72019-09-11 13:49:33 +02007940 *
7941 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007942 * @param[in] tok_idx Position in the expression @p exp.
aPiecek8b0cc152021-05-31 16:40:31 +02007943 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007944 * @param[in] options XPath options.
7945 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7946 */
7947static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007948eval_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 +02007949{
Radek Krejci857189e2020-09-01 13:26:36 +02007950 ly_bool all_desc, parent_pos_pred;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007951 LY_ERR rc;
7952
Michal Vasko004d3152020-06-11 19:59:22 +02007953 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007954 case LYXP_TOKEN_PAR1:
7955 /* '(' Expr ')' */
7956
7957 /* '(' */
aPiecek8b0cc152021-05-31 16:40:31 +02007958 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007959 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007960 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007961
7962 /* Expr */
Michal Vasko004d3152020-06-11 19:59:22 +02007963 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007964 LY_CHECK_RET(rc);
7965
7966 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007967 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
aPiecek8b0cc152021-05-31 16:40:31 +02007968 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007969 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007970 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007971
7972 parent_pos_pred = 0;
7973 goto predicate;
7974
7975 case LYXP_TOKEN_DOT:
7976 case LYXP_TOKEN_DDOT:
7977 case LYXP_TOKEN_AT:
7978 case LYXP_TOKEN_NAMETEST:
7979 case LYXP_TOKEN_NODETYPE:
7980 /* RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007981 rc = eval_relative_location_path(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007982 LY_CHECK_RET(rc);
7983 break;
7984
aPiecekfba75362021-10-07 12:39:48 +02007985 case LYXP_TOKEN_VARREF:
7986 /* VariableReference */
7987 rc = eval_variable_reference(exp, tok_idx, set, options);
7988 LY_CHECK_RET(rc);
7989 ++(*tok_idx);
7990
7991 parent_pos_pred = 1;
7992 goto predicate;
7993
Michal Vasko03ff5a72019-09-11 13:49:33 +02007994 case LYXP_TOKEN_FUNCNAME:
7995 /* FunctionCall */
aPiecek8b0cc152021-05-31 16:40:31 +02007996 rc = eval_function_call(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007997 LY_CHECK_RET(rc);
7998
7999 parent_pos_pred = 1;
8000 goto predicate;
8001
Michal Vasko3e48bf32020-06-01 08:39:07 +02008002 case LYXP_TOKEN_OPER_PATH:
8003 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +02008004 /* AbsoluteLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02008005 rc = eval_absolute_location_path(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008006 LY_CHECK_RET(rc);
8007 break;
8008
8009 case LYXP_TOKEN_LITERAL:
8010 /* Literal */
aPiecek8b0cc152021-05-31 16:40:31 +02008011 if ((options & LYXP_SKIP_EXPR) || (options & LYXP_SCNODE_ALL)) {
8012 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008013 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008014 }
Michal Vasko004d3152020-06-11 19:59:22 +02008015 eval_literal(exp, tok_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008016 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02008017 eval_literal(exp, tok_idx, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008018 }
8019
8020 parent_pos_pred = 1;
8021 goto predicate;
8022
8023 case LYXP_TOKEN_NUMBER:
8024 /* Number */
aPiecek8b0cc152021-05-31 16:40:31 +02008025 if ((options & LYXP_SKIP_EXPR) || (options & LYXP_SCNODE_ALL)) {
8026 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008027 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008028 }
Michal Vasko004d3152020-06-11 19:59:22 +02008029 rc = eval_number(NULL, exp, tok_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008030 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02008031 rc = eval_number(set->ctx, exp, tok_idx, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008032 }
8033 LY_CHECK_RET(rc);
8034
8035 parent_pos_pred = 1;
8036 goto predicate;
8037
8038 default:
Radek Krejci2efc45b2020-12-22 16:25:44 +01008039 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 +02008040 return LY_EVALID;
8041 }
8042
8043 return LY_SUCCESS;
8044
8045predicate:
8046 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02008047 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
8048 rc = eval_predicate(exp, tok_idx, set, options, parent_pos_pred);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008049 LY_CHECK_RET(rc);
8050 }
8051
8052 /* ('/' or '//') RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02008053 if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008054
8055 /* evaluate '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02008056 if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008057 all_desc = 0;
8058 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008059 all_desc = 1;
8060 }
8061
aPiecek8b0cc152021-05-31 16:40:31 +02008062 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02008063 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008064 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008065
Michal Vasko004d3152020-06-11 19:59:22 +02008066 rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008067 LY_CHECK_RET(rc);
8068 }
8069
8070 return LY_SUCCESS;
8071}
8072
8073/**
8074 * @brief Evaluate UnionExpr. Logs directly on error.
8075 *
Michal Vaskod3678892020-05-21 10:06:58 +02008076 * [20] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008077 *
8078 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008079 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008080 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008081 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008082 * @param[in] options XPath options.
8083 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8084 */
8085static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008086eval_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 +02008087{
8088 LY_ERR rc = LY_SUCCESS;
8089 struct lyxp_set orig_set, set2;
8090 uint16_t i;
8091
8092 assert(repeat);
8093
8094 set_init(&orig_set, set);
8095 set_init(&set2, set);
8096
8097 set_fill_set(&orig_set, set);
8098
Michal Vasko004d3152020-06-11 19:59:22 +02008099 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008100 LY_CHECK_GOTO(rc, cleanup);
8101
8102 /* ('|' PathExpr)* */
8103 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008104 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_UNI);
aPiecek8b0cc152021-05-31 16:40:31 +02008105 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02008106 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008107 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008108
aPiecek8b0cc152021-05-31 16:40:31 +02008109 if (options & LYXP_SKIP_EXPR) {
8110 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008111 LY_CHECK_GOTO(rc, cleanup);
8112 continue;
8113 }
8114
8115 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008116 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008117 LY_CHECK_GOTO(rc, cleanup);
8118
8119 /* eval */
8120 if (options & LYXP_SCNODE_ALL) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01008121 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008122 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008123 rc = moveto_union(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008124 LY_CHECK_GOTO(rc, cleanup);
8125 }
8126 }
8127
8128cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008129 lyxp_set_free_content(&orig_set);
8130 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008131 return rc;
8132}
8133
8134/**
8135 * @brief Evaluate UnaryExpr. Logs directly on error.
8136 *
Michal Vaskod3678892020-05-21 10:06:58 +02008137 * [19] UnaryExpr ::= UnionExpr | '-' UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008138 *
8139 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008140 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008141 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008142 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008143 * @param[in] options XPath options.
8144 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8145 */
8146static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008147eval_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 +02008148{
8149 LY_ERR rc;
8150 uint16_t this_op, i;
8151
8152 assert(repeat);
8153
8154 /* ('-')+ */
Michal Vasko004d3152020-06-11 19:59:22 +02008155 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008156 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008157 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 +02008158
aPiecek8b0cc152021-05-31 16:40:31 +02008159 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02008160 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008161 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008162 }
8163
Michal Vasko004d3152020-06-11 19:59:22 +02008164 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNARY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008165 LY_CHECK_RET(rc);
8166
aPiecek8b0cc152021-05-31 16:40:31 +02008167 if (!(options & LYXP_SKIP_EXPR) && (repeat % 2)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008168 if (options & LYXP_SCNODE_ALL) {
8169 warn_operands(set->ctx, set, NULL, 1, exp->expr, exp->tok_pos[this_op]);
8170 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008171 rc = moveto_op_math(set, NULL, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008172 LY_CHECK_RET(rc);
8173 }
8174 }
8175
8176 return LY_SUCCESS;
8177}
8178
8179/**
8180 * @brief Evaluate MultiplicativeExpr. Logs directly on error.
8181 *
Michal Vaskod3678892020-05-21 10:06:58 +02008182 * [18] MultiplicativeExpr ::= UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008183 * | MultiplicativeExpr '*' UnaryExpr
8184 * | MultiplicativeExpr 'div' UnaryExpr
8185 * | MultiplicativeExpr 'mod' UnaryExpr
8186 *
8187 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008188 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008189 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008190 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008191 * @param[in] options XPath options.
8192 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8193 */
8194static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008195eval_multiplicative_expr(const struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set,
8196 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008197{
8198 LY_ERR rc;
8199 uint16_t this_op;
8200 struct lyxp_set orig_set, set2;
8201 uint16_t i;
8202
8203 assert(repeat);
8204
8205 set_init(&orig_set, set);
8206 set_init(&set2, set);
8207
8208 set_fill_set(&orig_set, set);
8209
Michal Vasko004d3152020-06-11 19:59:22 +02008210 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008211 LY_CHECK_GOTO(rc, cleanup);
8212
8213 /* ('*' / 'div' / 'mod' UnaryExpr)* */
8214 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008215 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008216
Michal Vasko004d3152020-06-11 19:59:22 +02008217 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH);
aPiecek8b0cc152021-05-31 16:40:31 +02008218 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02008219 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008220 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008221
aPiecek8b0cc152021-05-31 16:40:31 +02008222 if (options & LYXP_SKIP_EXPR) {
8223 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008224 LY_CHECK_GOTO(rc, cleanup);
8225 continue;
8226 }
8227
8228 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008229 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008230 LY_CHECK_GOTO(rc, cleanup);
8231
8232 /* eval */
8233 if (options & LYXP_SCNODE_ALL) {
8234 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008235 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02008236 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008237 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008238 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008239 LY_CHECK_GOTO(rc, cleanup);
8240 }
8241 }
8242
8243cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008244 lyxp_set_free_content(&orig_set);
8245 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008246 return rc;
8247}
8248
8249/**
8250 * @brief Evaluate AdditiveExpr. Logs directly on error.
8251 *
Michal Vaskod3678892020-05-21 10:06:58 +02008252 * [17] AdditiveExpr ::= MultiplicativeExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008253 * | AdditiveExpr '+' MultiplicativeExpr
8254 * | AdditiveExpr '-' MultiplicativeExpr
8255 *
8256 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008257 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008258 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008259 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008260 * @param[in] options XPath options.
8261 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8262 */
8263static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008264eval_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 +02008265{
8266 LY_ERR rc;
8267 uint16_t this_op;
8268 struct lyxp_set orig_set, set2;
8269 uint16_t i;
8270
8271 assert(repeat);
8272
8273 set_init(&orig_set, set);
8274 set_init(&set2, set);
8275
8276 set_fill_set(&orig_set, set);
8277
Michal Vasko004d3152020-06-11 19:59:22 +02008278 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008279 LY_CHECK_GOTO(rc, cleanup);
8280
8281 /* ('+' / '-' MultiplicativeExpr)* */
8282 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008283 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008284
Michal Vasko004d3152020-06-11 19:59:22 +02008285 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008286 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02008287 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008288 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008289
aPiecek8b0cc152021-05-31 16:40:31 +02008290 if (options & LYXP_SKIP_EXPR) {
8291 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008292 LY_CHECK_GOTO(rc, cleanup);
8293 continue;
8294 }
8295
8296 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008297 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008298 LY_CHECK_GOTO(rc, cleanup);
8299
8300 /* eval */
8301 if (options & LYXP_SCNODE_ALL) {
8302 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008303 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02008304 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008305 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008306 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008307 LY_CHECK_GOTO(rc, cleanup);
8308 }
8309 }
8310
8311cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008312 lyxp_set_free_content(&orig_set);
8313 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008314 return rc;
8315}
8316
8317/**
8318 * @brief Evaluate RelationalExpr. Logs directly on error.
8319 *
Michal Vaskod3678892020-05-21 10:06:58 +02008320 * [16] RelationalExpr ::= AdditiveExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008321 * | RelationalExpr '<' AdditiveExpr
8322 * | RelationalExpr '>' AdditiveExpr
8323 * | RelationalExpr '<=' AdditiveExpr
8324 * | RelationalExpr '>=' AdditiveExpr
8325 *
8326 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008327 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008328 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008329 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008330 * @param[in] options XPath options.
8331 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8332 */
8333static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008334eval_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 +02008335{
8336 LY_ERR rc;
8337 uint16_t this_op;
8338 struct lyxp_set orig_set, set2;
8339 uint16_t i;
8340
8341 assert(repeat);
8342
8343 set_init(&orig_set, set);
8344 set_init(&set2, set);
8345
8346 set_fill_set(&orig_set, set);
8347
Michal Vasko004d3152020-06-11 19:59:22 +02008348 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008349 LY_CHECK_GOTO(rc, cleanup);
8350
8351 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
8352 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008353 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008354
Michal Vasko004d3152020-06-11 19:59:22 +02008355 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_COMP);
aPiecek8b0cc152021-05-31 16:40:31 +02008356 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02008357 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008358 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008359
aPiecek8b0cc152021-05-31 16:40:31 +02008360 if (options & LYXP_SKIP_EXPR) {
8361 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008362 LY_CHECK_GOTO(rc, cleanup);
8363 continue;
8364 }
8365
8366 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008367 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008368 LY_CHECK_GOTO(rc, cleanup);
8369
8370 /* eval */
8371 if (options & LYXP_SCNODE_ALL) {
8372 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008373 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02008374 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008375 } else {
8376 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options);
8377 LY_CHECK_GOTO(rc, cleanup);
8378 }
8379 }
8380
8381cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008382 lyxp_set_free_content(&orig_set);
8383 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008384 return rc;
8385}
8386
8387/**
8388 * @brief Evaluate EqualityExpr. Logs directly on error.
8389 *
Michal Vaskod3678892020-05-21 10:06:58 +02008390 * [15] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008391 * | EqualityExpr '!=' RelationalExpr
8392 *
8393 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008394 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008395 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008396 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008397 * @param[in] options XPath options.
8398 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8399 */
8400static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008401eval_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 +02008402{
8403 LY_ERR rc;
8404 uint16_t this_op;
8405 struct lyxp_set orig_set, set2;
8406 uint16_t i;
8407
8408 assert(repeat);
8409
8410 set_init(&orig_set, set);
8411 set_init(&set2, set);
8412
8413 set_fill_set(&orig_set, set);
8414
Michal Vasko004d3152020-06-11 19:59:22 +02008415 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008416 LY_CHECK_GOTO(rc, cleanup);
8417
8418 /* ('=' / '!=' RelationalExpr)* */
8419 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008420 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008421
Michal Vasko004d3152020-06-11 19:59:22 +02008422 assert((exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_EQUAL) || (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_NEQUAL));
aPiecek8b0cc152021-05-31 16:40:31 +02008423 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02008424 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008425 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008426
aPiecek8b0cc152021-05-31 16:40:31 +02008427 if (options & LYXP_SKIP_EXPR) {
8428 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008429 LY_CHECK_GOTO(rc, cleanup);
8430 continue;
8431 }
8432
8433 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008434 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008435 LY_CHECK_GOTO(rc, cleanup);
8436
8437 /* eval */
8438 if (options & LYXP_SCNODE_ALL) {
8439 warn_operands(set->ctx, set, &set2, 0, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vasko004d3152020-06-11 19:59:22 +02008440 warn_equality_value(exp, set, *tok_idx - 1, this_op - 1, *tok_idx - 1);
8441 warn_equality_value(exp, &set2, this_op - 1, this_op - 1, *tok_idx - 1);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008442 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02008443 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008444 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008445 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options);
8446 LY_CHECK_GOTO(rc, cleanup);
8447 }
8448 }
8449
8450cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008451 lyxp_set_free_content(&orig_set);
8452 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008453 return rc;
8454}
8455
8456/**
8457 * @brief Evaluate AndExpr. Logs directly on error.
8458 *
Michal Vaskod3678892020-05-21 10:06:58 +02008459 * [14] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008460 *
8461 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008462 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008463 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008464 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008465 * @param[in] options XPath options.
8466 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8467 */
8468static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008469eval_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 +02008470{
8471 LY_ERR rc;
8472 struct lyxp_set orig_set, set2;
8473 uint16_t i;
8474
8475 assert(repeat);
8476
8477 set_init(&orig_set, set);
8478 set_init(&set2, set);
8479
8480 set_fill_set(&orig_set, set);
8481
Michal Vasko004d3152020-06-11 19:59:22 +02008482 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008483 LY_CHECK_GOTO(rc, cleanup);
8484
8485 /* cast to boolean, we know that will be the final result */
aPiecek8b0cc152021-05-31 16:40:31 +02008486 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008487 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008488 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008489 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008490 }
8491
8492 /* ('and' EqualityExpr)* */
8493 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008494 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG);
aPiecek8b0cc152021-05-31 16:40:31 +02008495 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, ((options & LYXP_SKIP_EXPR) || !set->val.bln ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02008496 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008497 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008498
8499 /* lazy evaluation */
aPiecek8b0cc152021-05-31 16:40:31 +02008500 if ((options & LYXP_SKIP_EXPR) || ((set->type == LYXP_SET_BOOLEAN) && !set->val.bln)) {
8501 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008502 LY_CHECK_GOTO(rc, cleanup);
8503 continue;
8504 }
8505
8506 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008507 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008508 LY_CHECK_GOTO(rc, cleanup);
8509
8510 /* eval - just get boolean value actually */
8511 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008512 set_scnode_clear_ctx(&set2, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008513 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008514 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008515 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008516 set_fill_set(set, &set2);
8517 }
8518 }
8519
8520cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008521 lyxp_set_free_content(&orig_set);
8522 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008523 return rc;
8524}
8525
8526/**
8527 * @brief Evaluate OrExpr. Logs directly on error.
8528 *
Michal Vaskod3678892020-05-21 10:06:58 +02008529 * [13] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008530 *
8531 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008532 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008533 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008534 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008535 * @param[in] options XPath options.
8536 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8537 */
8538static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008539eval_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 +02008540{
8541 LY_ERR rc;
8542 struct lyxp_set orig_set, set2;
8543 uint16_t i;
8544
8545 assert(repeat);
8546
8547 set_init(&orig_set, set);
8548 set_init(&set2, set);
8549
8550 set_fill_set(&orig_set, set);
8551
Michal Vasko004d3152020-06-11 19:59:22 +02008552 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008553 LY_CHECK_GOTO(rc, cleanup);
8554
8555 /* cast to boolean, we know that will be the final result */
aPiecek8b0cc152021-05-31 16:40:31 +02008556 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008557 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008558 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008559 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008560 }
8561
8562 /* ('or' AndExpr)* */
8563 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008564 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG);
aPiecek8b0cc152021-05-31 16:40:31 +02008565 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, ((options & LYXP_SKIP_EXPR) || set->val.bln ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02008566 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008567 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008568
8569 /* lazy evaluation */
aPiecek8b0cc152021-05-31 16:40:31 +02008570 if ((options & LYXP_SKIP_EXPR) || ((set->type == LYXP_SET_BOOLEAN) && set->val.bln)) {
8571 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008572 LY_CHECK_GOTO(rc, cleanup);
8573 continue;
8574 }
8575
8576 set_fill_set(&set2, &orig_set);
8577 /* expr_type cound have been LYXP_EXPR_NONE in all these later calls (except for the first one),
8578 * but it does not matter */
Michal Vasko004d3152020-06-11 19:59:22 +02008579 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008580 LY_CHECK_GOTO(rc, cleanup);
8581
8582 /* eval - just get boolean value actually */
8583 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008584 set_scnode_clear_ctx(&set2, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008585 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008586 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008587 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008588 set_fill_set(set, &set2);
8589 }
8590 }
8591
8592cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008593 lyxp_set_free_content(&orig_set);
8594 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008595 return rc;
8596}
8597
8598/**
Michal Vasko004d3152020-06-11 19:59:22 +02008599 * @brief Decide what expression is at the pointer @p tok_idx and evaluate it accordingly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008600 *
8601 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008602 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008603 * @param[in] etype Expression type to evaluate.
aPiecek8b0cc152021-05-31 16:40:31 +02008604 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008605 * @param[in] options XPath options.
8606 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8607 */
8608static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008609eval_expr_select(const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_expr_type etype, struct lyxp_set *set,
8610 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008611{
8612 uint16_t i, count;
8613 enum lyxp_expr_type next_etype;
8614 LY_ERR rc;
8615
8616 /* process operator repeats */
Michal Vasko004d3152020-06-11 19:59:22 +02008617 if (!exp->repeat[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008618 next_etype = LYXP_EXPR_NONE;
8619 } else {
8620 /* find etype repeat */
Radek Krejci1e008d22020-08-17 11:37:37 +02008621 for (i = 0; exp->repeat[*tok_idx][i] > etype; ++i) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02008622
8623 /* select one-priority lower because etype expression called us */
8624 if (i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008625 next_etype = exp->repeat[*tok_idx][i - 1];
Michal Vasko03ff5a72019-09-11 13:49:33 +02008626 /* count repeats for that expression */
Radek Krejci1e008d22020-08-17 11:37:37 +02008627 for (count = 0; i && exp->repeat[*tok_idx][i - 1] == next_etype; ++count, --i) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02008628 } else {
8629 next_etype = LYXP_EXPR_NONE;
8630 }
8631 }
8632
8633 /* decide what expression are we parsing based on the repeat */
8634 switch (next_etype) {
8635 case LYXP_EXPR_OR:
Michal Vasko004d3152020-06-11 19:59:22 +02008636 rc = eval_or_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008637 break;
8638 case LYXP_EXPR_AND:
Michal Vasko004d3152020-06-11 19:59:22 +02008639 rc = eval_and_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008640 break;
8641 case LYXP_EXPR_EQUALITY:
Michal Vasko004d3152020-06-11 19:59:22 +02008642 rc = eval_equality_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008643 break;
8644 case LYXP_EXPR_RELATIONAL:
Michal Vasko004d3152020-06-11 19:59:22 +02008645 rc = eval_relational_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008646 break;
8647 case LYXP_EXPR_ADDITIVE:
Michal Vasko004d3152020-06-11 19:59:22 +02008648 rc = eval_additive_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008649 break;
8650 case LYXP_EXPR_MULTIPLICATIVE:
Michal Vasko004d3152020-06-11 19:59:22 +02008651 rc = eval_multiplicative_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008652 break;
8653 case LYXP_EXPR_UNARY:
Michal Vasko004d3152020-06-11 19:59:22 +02008654 rc = eval_unary_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008655 break;
8656 case LYXP_EXPR_UNION:
Michal Vasko004d3152020-06-11 19:59:22 +02008657 rc = eval_union_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008658 break;
8659 case LYXP_EXPR_NONE:
Michal Vasko004d3152020-06-11 19:59:22 +02008660 rc = eval_path_expr(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008661 break;
8662 default:
8663 LOGINT_RET(set->ctx);
8664 }
8665
8666 return rc;
8667}
8668
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008669/**
8670 * @brief Get root type.
8671 *
8672 * @param[in] ctx_node Context node.
8673 * @param[in] ctx_scnode Schema context node.
8674 * @param[in] options XPath options.
8675 * @return Root type.
8676 */
8677static enum lyxp_node_type
Radek Krejci1deb5be2020-08-26 16:43:36 +02008678lyxp_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 +01008679{
Michal Vasko6b26e742020-07-17 15:02:10 +02008680 const struct lysc_node *op;
8681
Michal Vaskoa27245c2022-05-02 09:01:35 +02008682 /* explicit */
8683 if (options & LYXP_ACCESS_TREE_ALL) {
8684 return LYXP_NODE_ROOT;
8685 } else if (options & LYXP_ACCESS_TREE_CONFIG) {
8686 return LYXP_NODE_ROOT_CONFIG;
8687 }
8688
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008689 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008690 /* schema */
Radek Krejci1e008d22020-08-17 11:37:37 +02008691 for (op = ctx_scnode; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent) {}
Michal Vasko6b26e742020-07-17 15:02:10 +02008692
8693 if (op || (options & LYXP_SCNODE)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008694 /* general root that can access everything */
8695 return LYXP_NODE_ROOT;
8696 } else if (!ctx_scnode || (ctx_scnode->flags & LYS_CONFIG_W)) {
8697 /* root context node can access only config data (because we said so, it is unspecified) */
8698 return LYXP_NODE_ROOT_CONFIG;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008699 }
Michal Vasko6b26e742020-07-17 15:02:10 +02008700 return LYXP_NODE_ROOT;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008701 }
8702
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008703 /* data */
Michal Vasko6b26e742020-07-17 15:02:10 +02008704 op = ctx_node ? ctx_node->schema : NULL;
Michal Vaskod989ba02020-08-24 10:59:24 +02008705 for ( ; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent) {}
Michal Vasko6b26e742020-07-17 15:02:10 +02008706
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008707 if (op || !(options & LYXP_SCHEMA)) {
8708 /* general root that can access everything */
8709 return LYXP_NODE_ROOT;
Christian Hoppsb6ecaea2021-02-06 09:45:38 -05008710 } else if (!ctx_node || !ctx_node->schema || (ctx_node->schema->flags & LYS_CONFIG_W)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008711 /* root context node can access only config data (because we said so, it is unspecified) */
8712 return LYXP_NODE_ROOT_CONFIG;
8713 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008714 return LYXP_NODE_ROOT;
8715}
8716
Michal Vasko03ff5a72019-09-11 13:49:33 +02008717LY_ERR
Michal Vasko400e9672021-01-11 13:39:17 +01008718lyxp_eval(const struct ly_ctx *ctx, const struct lyxp_expr *exp, const struct lys_module *cur_mod,
Radek Krejci8df109d2021-04-23 12:19:08 +02008719 LY_VALUE_FORMAT format, void *prefix_data, const struct lyd_node *ctx_node, const struct lyd_node *tree,
aPiecekfba75362021-10-07 12:39:48 +02008720 const struct lyxp_var *vars, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008721{
Michal Vasko004d3152020-06-11 19:59:22 +02008722 uint16_t tok_idx = 0;
Michal Vaskoddd76592022-01-17 13:34:48 +01008723 const struct lysc_node *snode;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008724 LY_ERR rc;
8725
Michal Vasko400e9672021-01-11 13:39:17 +01008726 LY_CHECK_ARG_RET(ctx, ctx, exp, set, LY_EINVAL);
Radek Krejci8df109d2021-04-23 12:19:08 +02008727 if (!cur_mod && ((format == LY_VALUE_SCHEMA) || (format == LY_VALUE_SCHEMA_RESOLVED))) {
Michal Vaskoddd76592022-01-17 13:34:48 +01008728 LOGERR(ctx, LY_EINVAL, "Current module must be set if schema format is used.");
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008729 return LY_EINVAL;
Michal Vasko004d3152020-06-11 19:59:22 +02008730 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008731
Michal Vaskod3bb12f2020-12-04 14:33:09 +01008732 if (tree) {
8733 /* adjust the pointer to be the first top-level sibling */
8734 while (tree->parent) {
8735 tree = lyd_parent(tree);
8736 }
8737 tree = lyd_first_sibling(tree);
Michal Vaskoddd76592022-01-17 13:34:48 +01008738
8739 for (snode = tree->schema->parent; snode && (snode->nodetype & (LYS_CASE | LYS_CHOICE)); snode = snode->parent) {}
8740 if (snode) {
8741 /* unable to evaluate absolute paths */
8742 LOGERR(ctx, LY_EINVAL, "Data node \"%s\" has no parent but is not instance of a top-level schema node.",
8743 LYD_NAME(tree));
8744 return LY_EINVAL;
8745 }
Michal Vaskod3bb12f2020-12-04 14:33:09 +01008746 }
8747
Michal Vasko03ff5a72019-09-11 13:49:33 +02008748 /* prepare set for evaluation */
Michal Vasko03ff5a72019-09-11 13:49:33 +02008749 memset(set, 0, sizeof *set);
Michal Vaskod3678892020-05-21 10:06:58 +02008750 set->type = LYXP_SET_NODE_SET;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008751 set->root_type = lyxp_get_root_type(ctx_node, NULL, options);
8752 set_insert_node(set, (struct lyd_node *)ctx_node, 0, ctx_node ? LYXP_NODE_ELEM : set->root_type, 0);
8753
Michal Vasko400e9672021-01-11 13:39:17 +01008754 set->ctx = (struct ly_ctx *)ctx;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008755 set->cur_node = ctx_node;
8756 for (set->context_op = ctx_node ? ctx_node->schema : NULL;
Radek Krejci0f969882020-08-21 16:56:47 +02008757 set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF));
8758 set->context_op = set->context_op->parent) {}
Michal Vaskof03ed032020-03-04 13:31:44 +01008759 set->tree = tree;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008760 set->cur_mod = cur_mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008761 set->format = format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008762 set->prefix_data = prefix_data;
aPiecekfba75362021-10-07 12:39:48 +02008763 set->vars = vars;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008764
Radek Krejciddace2c2021-01-08 11:30:56 +01008765 LOG_LOCSET(NULL, set->cur_node, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01008766
Michal Vasko03ff5a72019-09-11 13:49:33 +02008767 /* evaluate */
Michal Vasko004d3152020-06-11 19:59:22 +02008768 rc = eval_expr_select(exp, &tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008769 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02008770 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008771 }
8772
Michal Vasko4a7d4d62021-12-13 17:05:06 +01008773 if (set->cur_node) {
8774 LOG_LOCBACK(0, 1, 0, 0);
8775 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008776 return rc;
8777}
8778
8779#if 0
8780
8781/* full xml printing of set elements, not used currently */
8782
8783void
8784lyxp_set_print_xml(FILE *f, struct lyxp_set *set)
8785{
8786 uint32_t i;
8787 char *str_num;
8788 struct lyout out;
8789
8790 memset(&out, 0, sizeof out);
8791
8792 out.type = LYOUT_STREAM;
8793 out.method.f = f;
8794
8795 switch (set->type) {
8796 case LYXP_SET_EMPTY:
Michal Vasko5233e962020-08-14 14:26:20 +02008797 ly_print_(&out, "Empty XPath set\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008798 break;
8799 case LYXP_SET_BOOLEAN:
Michal Vasko5233e962020-08-14 14:26:20 +02008800 ly_print_(&out, "Boolean XPath set:\n");
8801 ly_print_(&out, "%s\n\n", set->value.bool ? "true" : "false");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008802 break;
8803 case LYXP_SET_STRING:
Michal Vasko5233e962020-08-14 14:26:20 +02008804 ly_print_(&out, "String XPath set:\n");
8805 ly_print_(&out, "\"%s\"\n\n", set->value.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008806 break;
8807 case LYXP_SET_NUMBER:
Michal Vasko5233e962020-08-14 14:26:20 +02008808 ly_print_(&out, "Number XPath set:\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008809
8810 if (isnan(set->value.num)) {
8811 str_num = strdup("NaN");
8812 } else if ((set->value.num == 0) || (set->value.num == -0.0f)) {
8813 str_num = strdup("0");
8814 } else if (isinf(set->value.num) && !signbit(set->value.num)) {
8815 str_num = strdup("Infinity");
8816 } else if (isinf(set->value.num) && signbit(set->value.num)) {
8817 str_num = strdup("-Infinity");
8818 } else if ((long long)set->value.num == set->value.num) {
8819 if (asprintf(&str_num, "%lld", (long long)set->value.num) == -1) {
8820 str_num = NULL;
8821 }
8822 } else {
8823 if (asprintf(&str_num, "%03.1Lf", set->value.num) == -1) {
8824 str_num = NULL;
8825 }
8826 }
8827 if (!str_num) {
8828 LOGMEM;
8829 return;
8830 }
Michal Vasko5233e962020-08-14 14:26:20 +02008831 ly_print_(&out, "%s\n\n", str_num);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008832 free(str_num);
8833 break;
8834 case LYXP_SET_NODE_SET:
Michal Vasko5233e962020-08-14 14:26:20 +02008835 ly_print_(&out, "Node XPath set:\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008836
8837 for (i = 0; i < set->used; ++i) {
Michal Vasko5233e962020-08-14 14:26:20 +02008838 ly_print_(&out, "%d. ", i + 1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008839 switch (set->node_type[i]) {
8840 case LYXP_NODE_ROOT_ALL:
Michal Vasko5233e962020-08-14 14:26:20 +02008841 ly_print_(&out, "ROOT all\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008842 break;
8843 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko5233e962020-08-14 14:26:20 +02008844 ly_print_(&out, "ROOT config\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008845 break;
8846 case LYXP_NODE_ROOT_STATE:
Michal Vasko5233e962020-08-14 14:26:20 +02008847 ly_print_(&out, "ROOT state\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008848 break;
8849 case LYXP_NODE_ROOT_NOTIF:
Michal Vasko5233e962020-08-14 14:26:20 +02008850 ly_print_(&out, "ROOT notification \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008851 break;
8852 case LYXP_NODE_ROOT_RPC:
Michal Vasko5233e962020-08-14 14:26:20 +02008853 ly_print_(&out, "ROOT rpc \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008854 break;
8855 case LYXP_NODE_ROOT_OUTPUT:
Michal Vasko5233e962020-08-14 14:26:20 +02008856 ly_print_(&out, "ROOT output \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008857 break;
8858 case LYXP_NODE_ELEM:
Michal Vasko5233e962020-08-14 14:26:20 +02008859 ly_print_(&out, "ELEM \"%s\"\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008860 xml_print_node(&out, 1, set->value.nodes[i], 1, LYP_FORMAT);
Michal Vasko5233e962020-08-14 14:26:20 +02008861 ly_print_(&out, "\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008862 break;
8863 case LYXP_NODE_TEXT:
Michal Vasko5233e962020-08-14 14:26:20 +02008864 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 +02008865 break;
8866 case LYXP_NODE_ATTR:
Michal Vasko5233e962020-08-14 14:26:20 +02008867 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 +02008868 break;
8869 }
8870 }
8871 break;
8872 }
8873}
8874
8875#endif
8876
8877LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008878lyxp_set_cast(struct lyxp_set *set, enum lyxp_set_type target)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008879{
8880 long double num;
8881 char *str;
8882 LY_ERR rc;
8883
8884 if (!set || (set->type == target)) {
8885 return LY_SUCCESS;
8886 }
8887
8888 /* it's not possible to convert anything into a node set */
Michal Vaskod3678892020-05-21 10:06:58 +02008889 assert(target != LYXP_SET_NODE_SET);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008890
8891 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vaskod3678892020-05-21 10:06:58 +02008892 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008893 return LY_EINVAL;
8894 }
8895
8896 /* to STRING */
Michal Vaskod3678892020-05-21 10:06:58 +02008897 if ((target == LYXP_SET_STRING) || ((target == LYXP_SET_NUMBER) && (set->type == LYXP_SET_NODE_SET))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008898 switch (set->type) {
8899 case LYXP_SET_NUMBER:
8900 if (isnan(set->val.num)) {
8901 set->val.str = strdup("NaN");
8902 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8903 } else if ((set->val.num == 0) || (set->val.num == -0.0f)) {
8904 set->val.str = strdup("0");
8905 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8906 } else if (isinf(set->val.num) && !signbit(set->val.num)) {
8907 set->val.str = strdup("Infinity");
8908 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8909 } else if (isinf(set->val.num) && signbit(set->val.num)) {
8910 set->val.str = strdup("-Infinity");
8911 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8912 } else if ((long long)set->val.num == set->val.num) {
8913 if (asprintf(&str, "%lld", (long long)set->val.num) == -1) {
8914 LOGMEM_RET(set->ctx);
8915 }
8916 set->val.str = str;
8917 } else {
8918 if (asprintf(&str, "%03.1Lf", set->val.num) == -1) {
8919 LOGMEM_RET(set->ctx);
8920 }
8921 set->val.str = str;
8922 }
8923 break;
8924 case LYXP_SET_BOOLEAN:
Michal Vasko004d3152020-06-11 19:59:22 +02008925 if (set->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008926 set->val.str = strdup("true");
8927 } else {
8928 set->val.str = strdup("false");
8929 }
8930 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), LY_EMEM);
8931 break;
8932 case LYXP_SET_NODE_SET:
Michal Vasko03ff5a72019-09-11 13:49:33 +02008933 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008934 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008935
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008936 rc = cast_node_set_to_string(set, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008937 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02008938 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008939 set->val.str = str;
8940 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008941 default:
8942 LOGINT_RET(set->ctx);
8943 }
8944 set->type = LYXP_SET_STRING;
8945 }
8946
8947 /* to NUMBER */
8948 if (target == LYXP_SET_NUMBER) {
8949 switch (set->type) {
8950 case LYXP_SET_STRING:
8951 num = cast_string_to_number(set->val.str);
Michal Vaskod3678892020-05-21 10:06:58 +02008952 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008953 set->val.num = num;
8954 break;
8955 case LYXP_SET_BOOLEAN:
Michal Vasko004d3152020-06-11 19:59:22 +02008956 if (set->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008957 set->val.num = 1;
8958 } else {
8959 set->val.num = 0;
8960 }
8961 break;
8962 default:
8963 LOGINT_RET(set->ctx);
8964 }
8965 set->type = LYXP_SET_NUMBER;
8966 }
8967
8968 /* to BOOLEAN */
8969 if (target == LYXP_SET_BOOLEAN) {
8970 switch (set->type) {
8971 case LYXP_SET_NUMBER:
8972 if ((set->val.num == 0) || (set->val.num == -0.0f) || isnan(set->val.num)) {
Michal Vasko004d3152020-06-11 19:59:22 +02008973 set->val.bln = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008974 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02008975 set->val.bln = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008976 }
8977 break;
8978 case LYXP_SET_STRING:
8979 if (set->val.str[0]) {
Michal Vaskod3678892020-05-21 10:06:58 +02008980 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008981 set->val.bln = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008982 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02008983 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008984 set->val.bln = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008985 }
8986 break;
8987 case LYXP_SET_NODE_SET:
Michal Vaskod3678892020-05-21 10:06:58 +02008988 if (set->used) {
8989 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008990 set->val.bln = 1;
Michal Vaskod3678892020-05-21 10:06:58 +02008991 } else {
8992 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008993 set->val.bln = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02008994 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008995 break;
8996 default:
8997 LOGINT_RET(set->ctx);
8998 }
8999 set->type = LYXP_SET_BOOLEAN;
9000 }
9001
Michal Vasko03ff5a72019-09-11 13:49:33 +02009002 return LY_SUCCESS;
9003}
9004
9005LY_ERR
Michal Vasko400e9672021-01-11 13:39:17 +01009006lyxp_atomize(const struct ly_ctx *ctx, const struct lyxp_expr *exp, const struct lys_module *cur_mod,
Radek Krejci8df109d2021-04-23 12:19:08 +02009007 LY_VALUE_FORMAT format, void *prefix_data, const struct lysc_node *ctx_scnode, struct lyxp_set *set,
Michal Vasko400e9672021-01-11 13:39:17 +01009008 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02009009{
Radek Krejci2efc45b2020-12-22 16:25:44 +01009010 LY_ERR ret;
Michal Vasko004d3152020-06-11 19:59:22 +02009011 uint16_t tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009012
Michal Vasko400e9672021-01-11 13:39:17 +01009013 LY_CHECK_ARG_RET(ctx, ctx, exp, set, LY_EINVAL);
Radek Krejci8df109d2021-04-23 12:19:08 +02009014 if (!cur_mod && ((format == LY_VALUE_SCHEMA) || (format == LY_VALUE_SCHEMA_RESOLVED))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009015 LOGARG(NULL, "Current module must be set if schema format is used.");
9016 return LY_EINVAL;
Michal Vasko004d3152020-06-11 19:59:22 +02009017 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02009018
9019 /* prepare set for evaluation */
Michal Vasko03ff5a72019-09-11 13:49:33 +02009020 memset(set, 0, sizeof *set);
9021 set->type = LYXP_SET_SCNODE_SET;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009022 set->root_type = lyxp_get_root_type(NULL, ctx_scnode, options);
9023 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 +01009024 set->val.scnodes[0].in_ctx = LYXP_SET_SCNODE_START;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009025
Michal Vasko400e9672021-01-11 13:39:17 +01009026 set->ctx = (struct ly_ctx *)ctx;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009027 set->cur_scnode = ctx_scnode;
Michal Vasko6b26e742020-07-17 15:02:10 +02009028 for (set->context_op = ctx_scnode;
Radek Krejci0f969882020-08-21 16:56:47 +02009029 set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF));
9030 set->context_op = set->context_op->parent) {}
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009031 set->cur_mod = cur_mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009032 set->format = format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009033 set->prefix_data = prefix_data;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009034
Radek Krejciddace2c2021-01-08 11:30:56 +01009035 LOG_LOCSET(set->cur_scnode, NULL, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01009036
Michal Vasko03ff5a72019-09-11 13:49:33 +02009037 /* evaluate */
Radek Krejci2efc45b2020-12-22 16:25:44 +01009038 ret = eval_expr_select(exp, &tok_idx, 0, set, options);
9039
Radek Krejciddace2c2021-01-08 11:30:56 +01009040 LOG_LOCBACK(1, 0, 0, 0);
Radek Krejci2efc45b2020-12-22 16:25:44 +01009041 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009042}
Michal Vaskod43d71a2020-08-07 14:54:58 +02009043
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01009044LIBYANG_API_DEF const char *
Michal Vaskod43d71a2020-08-07 14:54:58 +02009045lyxp_get_expr(const struct lyxp_expr *path)
9046{
9047 if (!path) {
9048 return NULL;
9049 }
9050
9051 return path->expr;
9052}