blob: 3cf8539ef6138398aeaf1d1c6f0b37be4b20a311 [file] [log] [blame]
Radek Krejcib1646a92018-11-02 16:08:26 +01001/**
2 * @file xpath.c
3 * @author Michal Vasko <mvasko@cesnet.cz>
4 * @brief YANG XPath evaluation functions
5 *
Michal Vasko49fec8e2022-05-24 10:28:33 +02006 * Copyright (c) 2015 - 2022 CESNET, z.s.p.o.
Radek Krejcib1646a92018-11-02 16:08:26 +01007 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
Christian Hopps32874e12021-05-01 09:43:54 -040014#define _GNU_SOURCE /* asprintf, strdup */
Radek Krejcib1646a92018-11-02 16:08:26 +010015
Radek Krejci535ea9f2020-05-29 16:01:05 +020016#include "xpath.h"
Radek Krejcib1646a92018-11-02 16:08:26 +010017
Radek Krejci535ea9f2020-05-29 16:01:05 +020018#include <assert.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010019#include <ctype.h>
Radek Krejci535ea9f2020-05-29 16:01:05 +020020#include <errno.h>
Radek Krejci535ea9f2020-05-29 16:01:05 +020021#include <math.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020022#include <stdint.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010023#include <stdio.h>
24#include <stdlib.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010025#include <string.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010026
Radek Krejci535ea9f2020-05-29 16:01:05 +020027#include "common.h"
Michal Vasko5aa44c02020-06-29 11:47:02 +020028#include "compat.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020029#include "context.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020030#include "dict.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020031#include "hash_table.h"
Radek Krejci47fab892020-11-05 17:02:41 +010032#include "out.h"
Radek Krejci7931b192020-06-25 17:05:03 +020033#include "parser_data.h"
Michal Vasko004d3152020-06-11 19:59:22 +020034#include "path.h"
Michal Vasko03ff5a72019-09-11 13:49:33 +020035#include "plugins_types.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020036#include "printer_data.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020037#include "schema_compile_node.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020038#include "tree.h"
Radek Krejci77114102021-03-10 15:21:57 +010039#include "tree_data.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020040#include "tree_data_internal.h"
Radek Krejci859a15a2021-03-05 20:56:59 +010041#include "tree_edit.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020042#include "tree_schema_internal.h"
43#include "xml.h"
Michal Vasko03ff5a72019-09-11 13:49:33 +020044
aPiecekbf968d92021-05-27 14:35:05 +020045static LY_ERR reparse_or_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t depth);
Michal Vasko40308e72020-10-20 16:38:40 +020046static LY_ERR eval_expr_select(const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_expr_type etype,
47 struct lyxp_set *set, uint32_t options);
Michal Vasko93923692021-05-07 15:28:02 +020048static LY_ERR moveto_resolve_model(const char **qname, uint16_t *qname_len, const struct lyxp_set *set,
49 const struct lysc_node *ctx_scnode, const struct lys_module **moveto_mod);
Michal Vasko47da6562022-07-14 15:43:15 +020050static LY_ERR moveto_axis_node_next(const struct lyd_node **iter, enum lyxp_node_type *iter_type,
51 const struct lyd_node *node, enum lyxp_node_type node_type, enum lyxp_axis axis, struct lyxp_set *set);
Michal Vasko49fec8e2022-05-24 10:28:33 +020052static LY_ERR moveto_node(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname,
53 enum lyxp_axis axis, uint32_t options);
54static LY_ERR moveto_scnode(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname,
55 enum lyxp_axis axis, uint32_t options);
Michal Vasko8abcecc2022-07-28 09:55:01 +020056static LY_ERR moveto_op_comp(struct lyxp_set *set1, struct lyxp_set *set2, const char *op);
Michal Vasko03ff5a72019-09-11 13:49:33 +020057
aPiecek96dc1e32021-10-08 15:45:59 +020058/* Functions are divided into the following basic classes:
59 *
60 * (re)parse functions:
61 * Parse functions parse the expression into
62 * tokens (syntactic analysis).
63 * Reparse functions perform semantic analysis
64 * (do not save the result, just a check) of
65 * the expression and fill repeat indices.
66 *
67 * warn functions:
68 * Warn functions check specific reasonable conditions for schema XPath
69 * and print a warning if they are not satisfied.
70 *
71 * moveto functions:
72 * They and only they actually change the context (set).
73 *
74 * eval functions:
75 * They execute a parsed XPath expression on some data subtree.
76 */
77
Michal Vasko03ff5a72019-09-11 13:49:33 +020078/**
79 * @brief Print the type of an XPath \p set.
80 *
81 * @param[in] set Set to use.
82 * @return Set type string.
83 */
84static const char *
85print_set_type(struct lyxp_set *set)
86{
87 switch (set->type) {
Michal Vasko03ff5a72019-09-11 13:49:33 +020088 case LYXP_SET_NODE_SET:
89 return "node set";
90 case LYXP_SET_SCNODE_SET:
91 return "schema node set";
92 case LYXP_SET_BOOLEAN:
93 return "boolean";
94 case LYXP_SET_NUMBER:
95 return "number";
96 case LYXP_SET_STRING:
97 return "string";
98 }
99
100 return NULL;
101}
102
Michal Vasko24cddf82020-06-01 08:17:01 +0200103const char *
Michal Vasko49fec8e2022-05-24 10:28:33 +0200104lyxp_token2str(enum lyxp_token tok)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200105{
106 switch (tok) {
107 case LYXP_TOKEN_PAR1:
108 return "(";
109 case LYXP_TOKEN_PAR2:
110 return ")";
111 case LYXP_TOKEN_BRACK1:
112 return "[";
113 case LYXP_TOKEN_BRACK2:
114 return "]";
115 case LYXP_TOKEN_DOT:
116 return ".";
117 case LYXP_TOKEN_DDOT:
118 return "..";
119 case LYXP_TOKEN_AT:
120 return "@";
121 case LYXP_TOKEN_COMMA:
122 return ",";
123 case LYXP_TOKEN_NAMETEST:
124 return "NameTest";
125 case LYXP_TOKEN_NODETYPE:
126 return "NodeType";
aPiecekfba75362021-10-07 12:39:48 +0200127 case LYXP_TOKEN_VARREF:
128 return "VariableReference";
Michal Vasko03ff5a72019-09-11 13:49:33 +0200129 case LYXP_TOKEN_FUNCNAME:
130 return "FunctionName";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200131 case LYXP_TOKEN_OPER_LOG:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200132 return "Operator(Logic)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200133 case LYXP_TOKEN_OPER_EQUAL:
134 return "Operator(Equal)";
135 case LYXP_TOKEN_OPER_NEQUAL:
136 return "Operator(Non-equal)";
137 case LYXP_TOKEN_OPER_COMP:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200138 return "Operator(Comparison)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200139 case LYXP_TOKEN_OPER_MATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200140 return "Operator(Math)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200141 case LYXP_TOKEN_OPER_UNI:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200142 return "Operator(Union)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200143 case LYXP_TOKEN_OPER_PATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200144 return "Operator(Path)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200145 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko14676352020-05-29 11:35:55 +0200146 return "Operator(Recursive Path)";
Michal Vasko03ff5a72019-09-11 13:49:33 +0200147 case LYXP_TOKEN_LITERAL:
148 return "Literal";
149 case LYXP_TOKEN_NUMBER:
150 return "Number";
151 default:
152 LOGINT(NULL);
153 return "";
154 }
155}
156
157/**
Michal Vasko49fec8e2022-05-24 10:28:33 +0200158 * @brief Transform string into an axis.
159 *
160 * @param[in] str String to transform.
161 * @param[in] str_len Length of @p str.
162 * @return Transformed axis.
163 */
164static enum lyxp_axis
165str2axis(const char *str, uint16_t str_len)
166{
167 switch (str_len) {
168 case 4:
169 assert(!strncmp("self", str, str_len));
170 return LYXP_AXIS_SELF;
171 case 5:
172 assert(!strncmp("child", str, str_len));
173 return LYXP_AXIS_CHILD;
174 case 6:
175 assert(!strncmp("parent", str, str_len));
176 return LYXP_AXIS_PARENT;
177 case 8:
178 assert(!strncmp("ancestor", str, str_len));
179 return LYXP_AXIS_ANCESTOR;
180 case 9:
181 if (str[0] == 'a') {
182 assert(!strncmp("attribute", str, str_len));
183 return LYXP_AXIS_ATTRIBUTE;
184 } else if (str[0] == 'f') {
185 assert(!strncmp("following", str, str_len));
186 return LYXP_AXIS_FOLLOWING;
187 } else {
188 assert(!strncmp("preceding", str, str_len));
189 return LYXP_AXIS_PRECEDING;
190 }
191 break;
192 case 10:
193 assert(!strncmp("descendant", str, str_len));
194 return LYXP_AXIS_DESCENDANT;
195 case 16:
196 assert(!strncmp("ancestor-or-self", str, str_len));
197 return LYXP_AXIS_ANCESTOR_OR_SELF;
198 case 17:
199 if (str[0] == 'f') {
200 assert(!strncmp("following-sibling", str, str_len));
201 return LYXP_AXIS_FOLLOWING_SIBLING;
202 } else {
203 assert(!strncmp("preceding-sibling", str, str_len));
204 return LYXP_AXIS_PRECEDING_SIBLING;
205 }
206 break;
207 case 18:
208 assert(!strncmp("descendant-or-self", str, str_len));
209 return LYXP_AXIS_DESCENDANT_OR_SELF;
210 }
211
212 LOGINT(NULL);
213 return 0;
214}
215
216/**
Michal Vasko03ff5a72019-09-11 13:49:33 +0200217 * @brief Print the whole expression \p exp to debug output.
218 *
219 * @param[in] exp Expression to use.
220 */
221static void
Michal Vasko40308e72020-10-20 16:38:40 +0200222print_expr_struct_debug(const struct lyxp_expr *exp)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200223{
Radek Krejcif13b87b2020-12-01 22:02:17 +0100224#define MSG_BUFFER_SIZE 128
225 char tmp[MSG_BUFFER_SIZE];
Michal Vasko1fdd8fa2021-01-08 09:21:45 +0100226 uint32_t i, j;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200227
Radek Krejci52b6d512020-10-12 12:33:17 +0200228 if (!exp || (ly_ll < LY_LLDBG)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200229 return;
230 }
231
232 LOGDBG(LY_LDGXPATH, "expression \"%s\":", exp->expr);
233 for (i = 0; i < exp->used; ++i) {
Michal Vasko49fec8e2022-05-24 10:28:33 +0200234 sprintf(tmp, "\ttoken %s, in expression \"%.*s\"", lyxp_token2str(exp->tokens[i]), exp->tok_len[i],
Michal Vasko69730152020-10-09 16:30:07 +0200235 &exp->expr[exp->tok_pos[i]]);
Michal Vasko23049552021-03-04 15:57:44 +0100236 if (exp->repeat && exp->repeat[i]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200237 sprintf(tmp + strlen(tmp), " (repeat %d", exp->repeat[i][0]);
238 for (j = 1; exp->repeat[i][j]; ++j) {
239 sprintf(tmp + strlen(tmp), ", %d", exp->repeat[i][j]);
240 }
241 strcat(tmp, ")");
242 }
243 LOGDBG(LY_LDGXPATH, tmp);
244 }
Radek Krejcif13b87b2020-12-01 22:02:17 +0100245#undef MSG_BUFFER_SIZE
Michal Vasko03ff5a72019-09-11 13:49:33 +0200246}
247
248#ifndef NDEBUG
249
250/**
251 * @brief Print XPath set content to debug output.
252 *
253 * @param[in] set Set to print.
254 */
255static void
256print_set_debug(struct lyxp_set *set)
257{
258 uint32_t i;
259 char *str;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200260 struct lyxp_set_node *item;
261 struct lyxp_set_scnode *sitem;
262
Radek Krejci52b6d512020-10-12 12:33:17 +0200263 if (ly_ll < LY_LLDBG) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200264 return;
265 }
266
267 switch (set->type) {
268 case LYXP_SET_NODE_SET:
269 LOGDBG(LY_LDGXPATH, "set NODE SET:");
270 for (i = 0; i < set->used; ++i) {
271 item = &set->val.nodes[i];
272
273 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +0100274 case LYXP_NODE_NONE:
275 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): NONE", i + 1, item->pos);
276 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200277 case LYXP_NODE_ROOT:
278 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT", i + 1, item->pos);
279 break;
280 case LYXP_NODE_ROOT_CONFIG:
281 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT CONFIG", i + 1, item->pos);
282 break;
283 case LYXP_NODE_ELEM:
Michal Vasko9e685082021-01-29 14:49:09 +0100284 if ((item->node->schema->nodetype == LYS_LIST) && (lyd_child(item->node)->schema->nodetype == LYS_LEAF)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200285 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (1st child val: %s)", i + 1, item->pos,
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200286 item->node->schema->name, lyd_get_value(lyd_child(item->node)));
Michal Vasko9e685082021-01-29 14:49:09 +0100287 } else if (item->node->schema->nodetype == LYS_LEAFLIST) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200288 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (val: %s)", i + 1, item->pos,
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200289 item->node->schema->name, lyd_get_value(item->node));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200290 } else {
291 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s", i + 1, item->pos, item->node->schema->name);
292 }
293 break;
294 case LYXP_NODE_TEXT:
295 if (item->node->schema->nodetype & LYS_ANYDATA) {
296 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): TEXT <%s>", i + 1, item->pos,
Michal Vasko69730152020-10-09 16:30:07 +0200297 item->node->schema->nodetype == LYS_ANYXML ? "anyxml" : "anydata");
Michal Vasko03ff5a72019-09-11 13:49:33 +0200298 } else {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200299 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): TEXT %s", i + 1, item->pos, lyd_get_value(item->node));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200300 }
301 break;
Michal Vasko9f96a052020-03-10 09:41:45 +0100302 case LYXP_NODE_META:
303 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): META %s = %s", i + 1, item->pos, set->val.meta[i].meta->name,
Michal Vasko69730152020-10-09 16:30:07 +0200304 set->val.meta[i].meta->value);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200305 break;
306 }
307 }
308 break;
309
310 case LYXP_SET_SCNODE_SET:
311 LOGDBG(LY_LDGXPATH, "set SCNODE SET:");
312 for (i = 0; i < set->used; ++i) {
313 sitem = &set->val.scnodes[i];
314
315 switch (sitem->type) {
316 case LYXP_NODE_ROOT:
317 LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT", i + 1, sitem->in_ctx);
318 break;
319 case LYXP_NODE_ROOT_CONFIG:
320 LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT CONFIG", i + 1, sitem->in_ctx);
321 break;
322 case LYXP_NODE_ELEM:
323 LOGDBG(LY_LDGXPATH, "\t%d (%u): ELEM %s", i + 1, sitem->in_ctx, sitem->scnode->name);
324 break;
325 default:
326 LOGINT(NULL);
327 break;
328 }
329 }
330 break;
331
Michal Vasko03ff5a72019-09-11 13:49:33 +0200332 case LYXP_SET_BOOLEAN:
333 LOGDBG(LY_LDGXPATH, "set BOOLEAN");
Michal Vasko004d3152020-06-11 19:59:22 +0200334 LOGDBG(LY_LDGXPATH, "\t%s", (set->val.bln ? "true" : "false"));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200335 break;
336
337 case LYXP_SET_STRING:
338 LOGDBG(LY_LDGXPATH, "set STRING");
339 LOGDBG(LY_LDGXPATH, "\t%s", set->val.str);
340 break;
341
342 case LYXP_SET_NUMBER:
343 LOGDBG(LY_LDGXPATH, "set NUMBER");
344
345 if (isnan(set->val.num)) {
346 str = strdup("NaN");
347 } else if ((set->val.num == 0) || (set->val.num == -0.0f)) {
348 str = strdup("0");
349 } else if (isinf(set->val.num) && !signbit(set->val.num)) {
350 str = strdup("Infinity");
351 } else if (isinf(set->val.num) && signbit(set->val.num)) {
352 str = strdup("-Infinity");
353 } else if ((long long)set->val.num == set->val.num) {
354 if (asprintf(&str, "%lld", (long long)set->val.num) == -1) {
355 str = NULL;
356 }
357 } else {
358 if (asprintf(&str, "%03.1Lf", set->val.num) == -1) {
359 str = NULL;
360 }
361 }
362 LY_CHECK_ERR_RET(!str, LOGMEM(NULL), );
363
364 LOGDBG(LY_LDGXPATH, "\t%s", str);
365 free(str);
366 }
367}
368
369#endif
370
371/**
372 * @brief Realloc the string \p str.
373 *
374 * @param[in] ctx libyang context for logging.
375 * @param[in] needed How much free space is required.
376 * @param[in,out] str Pointer to the string to use.
377 * @param[in,out] used Used bytes in \p str.
378 * @param[in,out] size Allocated bytes in \p str.
379 * @return LY_ERR
380 */
381static LY_ERR
Michal Vasko52927e22020-03-16 17:26:14 +0100382cast_string_realloc(const struct ly_ctx *ctx, uint16_t needed, char **str, uint16_t *used, uint16_t *size)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200383{
384 if (*size - *used < needed) {
385 do {
386 if ((UINT16_MAX - *size) < LYXP_STRING_CAST_SIZE_STEP) {
387 LOGERR(ctx, LY_EINVAL, "XPath string length limit (%u) reached.", UINT16_MAX);
388 return LY_EINVAL;
389 }
390 *size += LYXP_STRING_CAST_SIZE_STEP;
391 } while (*size - *used < needed);
392 *str = ly_realloc(*str, *size * sizeof(char));
393 LY_CHECK_ERR_RET(!(*str), LOGMEM(ctx), LY_EMEM);
394 }
395
396 return LY_SUCCESS;
397}
398
399/**
400 * @brief Cast nodes recursively to one string @p str.
401 *
Michal Vasko47da6562022-07-14 15:43:15 +0200402 * @param[in] node Node to cast, NULL if root.
403 * @param[in] set XPath set.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200404 * @param[in] indent Current indent.
405 * @param[in,out] str Resulting string.
406 * @param[in,out] used Used bytes in @p str.
407 * @param[in,out] size Allocated bytes in @p str.
Michal Vasko47da6562022-07-14 15:43:15 +0200408 * @return LY_ERR value.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200409 */
410static LY_ERR
Michal Vasko47da6562022-07-14 15:43:15 +0200411cast_string_recursive(const struct lyd_node *node, struct lyxp_set *set, uint16_t indent, char **str, uint16_t *used,
412 uint16_t *size)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200413{
Radek Krejci7f769d72020-07-11 23:13:56 +0200414 char *buf, *line, *ptr = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200415 const char *value_str;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200416 const struct lyd_node *child;
Michal Vasko47da6562022-07-14 15:43:15 +0200417 enum lyxp_node_type child_type;
Michal Vasko60ea6352020-06-29 13:39:39 +0200418 struct lyd_node *tree;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200419 struct lyd_node_any *any;
420 LY_ERR rc;
421
Michal Vasko47da6562022-07-14 15:43:15 +0200422 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && node && (node->schema->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200423 return LY_SUCCESS;
424 }
425
Michal Vasko47da6562022-07-14 15:43:15 +0200426 if (!node) {
427 /* fake container */
428 LY_CHECK_RET(cast_string_realloc(set->ctx, 1, str, used, size));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200429 strcpy(*str + (*used - 1), "\n");
430 ++(*used);
431
432 ++indent;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200433
Michal Vasko47da6562022-07-14 15:43:15 +0200434 /* print all the top-level nodes */
435 child = NULL;
436 child_type = 0;
437 while (!moveto_axis_node_next(&child, &child_type, NULL, set->root_type, LYXP_AXIS_CHILD, set)) {
438 LY_CHECK_RET(cast_string_recursive(child, set, indent, str, used, size));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200439 }
440
Michal Vasko47da6562022-07-14 15:43:15 +0200441 /* end fake container */
442 LY_CHECK_RET(cast_string_realloc(set->ctx, 1, str, used, size));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200443 strcpy(*str + (*used - 1), "\n");
444 ++(*used);
445
446 --indent;
Michal Vasko47da6562022-07-14 15:43:15 +0200447 } else {
448 switch (node->schema->nodetype) {
449 case LYS_CONTAINER:
450 case LYS_LIST:
451 case LYS_RPC:
452 case LYS_NOTIF:
453 LY_CHECK_RET(cast_string_realloc(set->ctx, 1, str, used, size));
454 strcpy(*str + (*used - 1), "\n");
455 ++(*used);
456
457 for (child = lyd_child(node); child; child = child->next) {
458 LY_CHECK_RET(cast_string_recursive(child, set, indent + 1, str, used, size));
459 }
460
461 break;
462
463 case LYS_LEAF:
464 case LYS_LEAFLIST:
465 value_str = lyd_get_value(node);
466
467 /* print indent */
468 LY_CHECK_RET(cast_string_realloc(set->ctx, indent * 2 + strlen(value_str) + 1, str, used, size));
469 memset(*str + (*used - 1), ' ', indent * 2);
470 *used += indent * 2;
471
472 /* print value */
473 if (*used == 1) {
474 sprintf(*str + (*used - 1), "%s", value_str);
475 *used += strlen(value_str);
476 } else {
477 sprintf(*str + (*used - 1), "%s\n", value_str);
478 *used += strlen(value_str) + 1;
479 }
480
481 break;
482
483 case LYS_ANYXML:
484 case LYS_ANYDATA:
485 any = (struct lyd_node_any *)node;
486 if (!(void *)any->value.tree) {
487 /* no content */
488 buf = strdup("");
489 LY_CHECK_ERR_RET(!buf, LOGMEM(set->ctx), LY_EMEM);
490 } else {
491 struct ly_out *out;
492
493 if (any->value_type == LYD_ANYDATA_LYB) {
494 /* try to parse it into a data tree */
495 if (lyd_parse_data_mem((struct ly_ctx *)set->ctx, any->value.mem, LYD_LYB,
496 LYD_PARSE_ONLY | LYD_PARSE_STRICT, 0, &tree) == LY_SUCCESS) {
497 /* successfully parsed */
498 free(any->value.mem);
499 any->value.tree = tree;
500 any->value_type = LYD_ANYDATA_DATATREE;
501 }
502 /* error is covered by the following switch where LYD_ANYDATA_LYB causes failure */
503 }
504
505 switch (any->value_type) {
506 case LYD_ANYDATA_STRING:
507 case LYD_ANYDATA_XML:
508 case LYD_ANYDATA_JSON:
509 buf = strdup(any->value.json);
510 LY_CHECK_ERR_RET(!buf, LOGMEM(set->ctx), LY_EMEM);
511 break;
512 case LYD_ANYDATA_DATATREE:
513 LY_CHECK_RET(ly_out_new_memory(&buf, 0, &out));
514 rc = lyd_print_all(out, any->value.tree, LYD_XML, 0);
515 ly_out_free(out, NULL, 0);
516 LY_CHECK_RET(rc < 0, -rc);
517 break;
518 case LYD_ANYDATA_LYB:
519 LOGERR(set->ctx, LY_EINVAL, "Cannot convert LYB anydata into string.");
520 return LY_EINVAL;
521 }
522 }
523
524 line = strtok_r(buf, "\n", &ptr);
525 do {
526 rc = cast_string_realloc(set->ctx, indent * 2 + strlen(line) + 1, str, used, size);
527 if (rc != LY_SUCCESS) {
528 free(buf);
529 return rc;
530 }
531 memset(*str + (*used - 1), ' ', indent * 2);
532 *used += indent * 2;
533
534 strcpy(*str + (*used - 1), line);
535 *used += strlen(line);
536
537 strcpy(*str + (*used - 1), "\n");
538 *used += 1;
539 } while ((line = strtok_r(NULL, "\n", &ptr)));
540
541 free(buf);
542 break;
543
544 default:
545 LOGINT_RET(set->ctx);
546 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200547 }
548
549 return LY_SUCCESS;
550}
551
552/**
553 * @brief Cast an element into a string.
554 *
Michal Vasko47da6562022-07-14 15:43:15 +0200555 * @param[in] node Node to cast, NULL if root.
556 * @param[in] set XPath set.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200557 * @param[out] str Element cast to dynamically-allocated string.
558 * @return LY_ERR
559 */
560static LY_ERR
Michal Vasko47da6562022-07-14 15:43:15 +0200561cast_string_elem(const struct lyd_node *node, struct lyxp_set *set, char **str)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200562{
563 uint16_t used, size;
564 LY_ERR rc;
565
566 *str = malloc(LYXP_STRING_CAST_SIZE_START * sizeof(char));
Michal Vasko47da6562022-07-14 15:43:15 +0200567 LY_CHECK_ERR_RET(!*str, LOGMEM(set->ctx), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200568 (*str)[0] = '\0';
569 used = 1;
570 size = LYXP_STRING_CAST_SIZE_START;
571
Michal Vasko47da6562022-07-14 15:43:15 +0200572 rc = cast_string_recursive(node, set, 0, str, &used, &size);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200573 if (rc != LY_SUCCESS) {
574 free(*str);
575 return rc;
576 }
577
578 if (size > used) {
579 *str = ly_realloc(*str, used * sizeof(char));
Michal Vasko47da6562022-07-14 15:43:15 +0200580 LY_CHECK_ERR_RET(!*str, LOGMEM(set->ctx), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200581 }
582 return LY_SUCCESS;
583}
584
585/**
586 * @brief Cast a LYXP_SET_NODE_SET set into a string.
587 * Context position aware.
588 *
589 * @param[in] set Set to cast.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200590 * @param[out] str Cast dynamically-allocated string.
591 * @return LY_ERR
592 */
593static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100594cast_node_set_to_string(struct lyxp_set *set, char **str)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200595{
Michal Vaskoaa677062021-03-09 13:52:53 +0100596 if (!set->used) {
597 *str = strdup("");
598 if (!*str) {
599 LOGMEM_RET(set->ctx);
600 }
601 return LY_SUCCESS;
602 }
603
Michal Vasko03ff5a72019-09-11 13:49:33 +0200604 switch (set->val.nodes[0].type) {
Michal Vasko2caefc12019-11-14 16:07:56 +0100605 case LYXP_NODE_NONE:
606 /* invalid */
607 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200608 case LYXP_NODE_ROOT:
609 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200610 case LYXP_NODE_ELEM:
611 case LYXP_NODE_TEXT:
Michal Vasko47da6562022-07-14 15:43:15 +0200612 return cast_string_elem(set->val.nodes[0].node, set, str);
Michal Vasko9f96a052020-03-10 09:41:45 +0100613 case LYXP_NODE_META:
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200614 *str = strdup(lyd_get_meta_value(set->val.meta[0].meta));
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200615 if (!*str) {
616 LOGMEM_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200617 }
618 return LY_SUCCESS;
619 }
620
621 LOGINT_RET(set->ctx);
622}
623
624/**
625 * @brief Cast a string into an XPath number.
626 *
627 * @param[in] str String to use.
628 * @return Cast number.
629 */
630static long double
631cast_string_to_number(const char *str)
632{
633 long double num;
634 char *ptr;
635
636 errno = 0;
637 num = strtold(str, &ptr);
638 if (errno || *ptr) {
639 num = NAN;
640 }
641 return num;
642}
643
644/**
645 * @brief Callback for checking value equality.
646 *
Michal Vasko62524a92021-02-26 10:08:50 +0100647 * Implementation of ::lyht_value_equal_cb.
Radek Krejci857189e2020-09-01 13:26:36 +0200648 *
Michal Vasko03ff5a72019-09-11 13:49:33 +0200649 * @param[in] val1_p First value.
650 * @param[in] val2_p Second value.
651 * @param[in] mod Whether hash table is being modified.
652 * @param[in] cb_data Callback data.
Radek Krejci857189e2020-09-01 13:26:36 +0200653 * @return Boolean value whether values are equal or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200654 */
Radek Krejci857189e2020-09-01 13:26:36 +0200655static ly_bool
656set_values_equal_cb(void *val1_p, void *val2_p, ly_bool UNUSED(mod), void *UNUSED(cb_data))
Michal Vasko03ff5a72019-09-11 13:49:33 +0200657{
658 struct lyxp_set_hash_node *val1, *val2;
659
660 val1 = (struct lyxp_set_hash_node *)val1_p;
661 val2 = (struct lyxp_set_hash_node *)val2_p;
662
663 if ((val1->node == val2->node) && (val1->type == val2->type)) {
664 return 1;
665 }
666
667 return 0;
668}
669
670/**
671 * @brief Insert node and its hash into set.
672 *
673 * @param[in] set et to insert to.
674 * @param[in] node Node with hash.
675 * @param[in] type Node type.
676 */
677static void
678set_insert_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type)
679{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200680 LY_ERR r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200681 uint32_t i, hash;
682 struct lyxp_set_hash_node hnode;
683
684 if (!set->ht && (set->used >= LYD_HT_MIN_ITEMS)) {
685 /* create hash table and add all the nodes */
686 set->ht = lyht_new(1, sizeof(struct lyxp_set_hash_node), set_values_equal_cb, NULL, 1);
687 for (i = 0; i < set->used; ++i) {
688 hnode.node = set->val.nodes[i].node;
689 hnode.type = set->val.nodes[i].type;
690
691 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
692 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
693 hash = dict_hash_multi(hash, NULL, 0);
694
695 r = lyht_insert(set->ht, &hnode, hash, NULL);
696 assert(!r);
697 (void)r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200698
Michal Vasko9d6befd2019-12-11 14:56:56 +0100699 if (hnode.node == node) {
700 /* it was just added, do not add it twice */
701 node = NULL;
702 }
703 }
704 }
705
706 if (set->ht && node) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200707 /* add the new node into hash table */
708 hnode.node = node;
709 hnode.type = type;
710
711 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
712 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
713 hash = dict_hash_multi(hash, NULL, 0);
714
715 r = lyht_insert(set->ht, &hnode, hash, NULL);
716 assert(!r);
717 (void)r;
718 }
719}
720
721/**
722 * @brief Remove node and its hash from set.
723 *
724 * @param[in] set Set to remove from.
725 * @param[in] node Node to remove.
726 * @param[in] type Node type.
727 */
728static void
729set_remove_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type)
730{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200731 LY_ERR r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200732 struct lyxp_set_hash_node hnode;
733 uint32_t hash;
734
735 if (set->ht) {
736 hnode.node = node;
737 hnode.type = type;
738
739 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
740 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
741 hash = dict_hash_multi(hash, NULL, 0);
742
743 r = lyht_remove(set->ht, &hnode, hash);
744 assert(!r);
745 (void)r;
746
747 if (!set->ht->used) {
748 lyht_free(set->ht);
749 set->ht = NULL;
750 }
751 }
752}
753
754/**
755 * @brief Check whether node is in set based on its hash.
756 *
757 * @param[in] set Set to search in.
758 * @param[in] node Node to search for.
759 * @param[in] type Node type.
760 * @param[in] skip_idx Index in @p set to skip.
761 * @return LY_ERR
762 */
763static LY_ERR
764set_dup_node_hash_check(const struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type, int skip_idx)
765{
766 struct lyxp_set_hash_node hnode, *match_p;
767 uint32_t hash;
768
769 hnode.node = node;
770 hnode.type = type;
771
772 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
773 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
774 hash = dict_hash_multi(hash, NULL, 0);
775
776 if (!lyht_find(set->ht, &hnode, hash, (void **)&match_p)) {
777 if ((skip_idx > -1) && (set->val.nodes[skip_idx].node == match_p->node) && (set->val.nodes[skip_idx].type == match_p->type)) {
778 /* we found it on the index that should be skipped, find another */
779 hnode = *match_p;
780 if (lyht_find_next(set->ht, &hnode, hash, (void **)&match_p)) {
781 /* none other found */
782 return LY_SUCCESS;
783 }
784 }
785
786 return LY_EEXIST;
787 }
788
789 /* not found */
790 return LY_SUCCESS;
791}
792
Michal Vaskod3678892020-05-21 10:06:58 +0200793void
794lyxp_set_free_content(struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200795{
796 if (!set) {
797 return;
798 }
799
800 if (set->type == LYXP_SET_NODE_SET) {
801 free(set->val.nodes);
802 lyht_free(set->ht);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200803 } else if (set->type == LYXP_SET_SCNODE_SET) {
804 free(set->val.scnodes);
Michal Vaskod3678892020-05-21 10:06:58 +0200805 lyht_free(set->ht);
806 } else {
807 if (set->type == LYXP_SET_STRING) {
808 free(set->val.str);
809 }
810 set->type = LYXP_SET_NODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200811 }
Michal Vaskod3678892020-05-21 10:06:58 +0200812
813 set->val.nodes = NULL;
814 set->used = 0;
815 set->size = 0;
816 set->ht = NULL;
817 set->ctx_pos = 0;
aPiecek748da732021-06-01 09:56:43 +0200818 set->ctx_size = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200819}
820
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100821/**
822 * @brief Free dynamically-allocated set.
823 *
824 * @param[in] set Set to free.
825 */
826static void
Michal Vasko03ff5a72019-09-11 13:49:33 +0200827lyxp_set_free(struct lyxp_set *set)
828{
829 if (!set) {
830 return;
831 }
832
Michal Vaskod3678892020-05-21 10:06:58 +0200833 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200834 free(set);
835}
836
837/**
838 * @brief Initialize set context.
839 *
840 * @param[in] new Set to initialize.
841 * @param[in] set Arbitrary initialized set.
842 */
843static void
Michal Vasko4c7763f2020-07-27 17:40:37 +0200844set_init(struct lyxp_set *new, const struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200845{
846 memset(new, 0, sizeof *new);
Michal Vasko02a77382019-09-12 11:47:35 +0200847 if (set) {
Michal Vasko306e2832022-07-25 09:15:17 +0200848 new->non_child_axis = set->non_child_axis;
Michal Vasko02a77382019-09-12 11:47:35 +0200849 new->ctx = set->ctx;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200850 new->cur_node = set->cur_node;
Michal Vasko588112f2019-12-10 14:51:53 +0100851 new->root_type = set->root_type;
Michal Vasko6b26e742020-07-17 15:02:10 +0200852 new->context_op = set->context_op;
Michal Vaskof03ed032020-03-04 13:31:44 +0100853 new->tree = set->tree;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200854 new->cur_mod = set->cur_mod;
Michal Vasko02a77382019-09-12 11:47:35 +0200855 new->format = set->format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200856 new->prefix_data = set->prefix_data;
aPiecekfba75362021-10-07 12:39:48 +0200857 new->vars = set->vars;
Michal Vasko02a77382019-09-12 11:47:35 +0200858 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200859}
860
861/**
862 * @brief Create a deep copy of a set.
863 *
864 * @param[in] set Set to copy.
865 * @return Copy of @p set.
866 */
867static struct lyxp_set *
868set_copy(struct lyxp_set *set)
869{
870 struct lyxp_set *ret;
Michal Vasko1fdd8fa2021-01-08 09:21:45 +0100871 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200872
873 if (!set) {
874 return NULL;
875 }
876
877 ret = malloc(sizeof *ret);
878 LY_CHECK_ERR_RET(!ret, LOGMEM(set->ctx), NULL);
879 set_init(ret, set);
880
881 if (set->type == LYXP_SET_SCNODE_SET) {
882 ret->type = set->type;
883
884 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100885 if ((set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) ||
886 (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +0200887 uint32_t idx;
888 LY_CHECK_ERR_RET(lyxp_set_scnode_insert_node(ret, set->val.scnodes[i].scnode, set->val.scnodes[i].type, &idx),
889 lyxp_set_free(ret), NULL);
Michal Vasko3f27c522020-01-06 08:37:49 +0100890 /* coverity seems to think scnodes can be NULL */
Radek Krejciaa6b53f2020-08-27 15:19:03 +0200891 if (!ret->val.scnodes) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200892 lyxp_set_free(ret);
893 return NULL;
894 }
Michal Vaskoba716542019-12-16 10:01:58 +0100895 ret->val.scnodes[idx].in_ctx = set->val.scnodes[i].in_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200896 }
897 }
898 } else if (set->type == LYXP_SET_NODE_SET) {
899 ret->type = set->type;
Michal Vasko08e9b112021-06-11 15:41:17 +0200900 if (set->used) {
901 ret->val.nodes = malloc(set->used * sizeof *ret->val.nodes);
902 LY_CHECK_ERR_RET(!ret->val.nodes, LOGMEM(set->ctx); free(ret), NULL);
903 memcpy(ret->val.nodes, set->val.nodes, set->used * sizeof *ret->val.nodes);
904 } else {
905 ret->val.nodes = NULL;
906 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200907
908 ret->used = ret->size = set->used;
909 ret->ctx_pos = set->ctx_pos;
910 ret->ctx_size = set->ctx_size;
Michal Vasko4a04e542021-02-01 08:58:30 +0100911 if (set->ht) {
912 ret->ht = lyht_dup(set->ht);
913 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200914 } else {
Radek Krejci0f969882020-08-21 16:56:47 +0200915 memcpy(ret, set, sizeof *ret);
916 if (set->type == LYXP_SET_STRING) {
917 ret->val.str = strdup(set->val.str);
918 LY_CHECK_ERR_RET(!ret->val.str, LOGMEM(set->ctx); free(ret), NULL);
919 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200920 }
921
922 return ret;
923}
924
925/**
926 * @brief Fill XPath set with a string. Any current data are disposed of.
927 *
928 * @param[in] set Set to fill.
929 * @param[in] string String to fill into \p set.
930 * @param[in] str_len Length of \p string. 0 is a valid value!
931 */
932static void
933set_fill_string(struct lyxp_set *set, const char *string, uint16_t str_len)
934{
Michal Vaskod3678892020-05-21 10:06:58 +0200935 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200936
937 set->type = LYXP_SET_STRING;
938 if ((str_len == 0) && (string[0] != '\0')) {
939 string = "";
940 }
941 set->val.str = strndup(string, str_len);
942}
943
944/**
945 * @brief Fill XPath set with a number. Any current data are disposed of.
946 *
947 * @param[in] set Set to fill.
948 * @param[in] number Number to fill into \p set.
949 */
950static void
951set_fill_number(struct lyxp_set *set, long double number)
952{
Michal Vaskod3678892020-05-21 10:06:58 +0200953 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200954
955 set->type = LYXP_SET_NUMBER;
956 set->val.num = number;
957}
958
959/**
960 * @brief Fill XPath set with a boolean. Any current data are disposed of.
961 *
962 * @param[in] set Set to fill.
963 * @param[in] boolean Boolean to fill into \p set.
964 */
965static void
Radek Krejci857189e2020-09-01 13:26:36 +0200966set_fill_boolean(struct lyxp_set *set, ly_bool boolean)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200967{
Michal Vaskod3678892020-05-21 10:06:58 +0200968 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200969
970 set->type = LYXP_SET_BOOLEAN;
Michal Vasko004d3152020-06-11 19:59:22 +0200971 set->val.bln = boolean;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200972}
973
974/**
975 * @brief Fill XPath set with the value from another set (deep assign).
976 * Any current data are disposed of.
977 *
978 * @param[in] trg Set to fill.
979 * @param[in] src Source set to copy into \p trg.
980 */
981static void
Michal Vasko4c7763f2020-07-27 17:40:37 +0200982set_fill_set(struct lyxp_set *trg, const struct lyxp_set *src)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200983{
984 if (!trg || !src) {
985 return;
986 }
987
988 if (trg->type == LYXP_SET_NODE_SET) {
989 free(trg->val.nodes);
990 } else if (trg->type == LYXP_SET_STRING) {
991 free(trg->val.str);
992 }
993 set_init(trg, src);
994
995 if (src->type == LYXP_SET_SCNODE_SET) {
996 trg->type = LYXP_SET_SCNODE_SET;
997 trg->used = src->used;
998 trg->size = src->used;
999
Michal Vasko08e9b112021-06-11 15:41:17 +02001000 if (trg->size) {
1001 trg->val.scnodes = ly_realloc(trg->val.scnodes, trg->size * sizeof *trg->val.scnodes);
1002 LY_CHECK_ERR_RET(!trg->val.scnodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), );
1003 memcpy(trg->val.scnodes, src->val.scnodes, src->used * sizeof *src->val.scnodes);
1004 } else {
1005 trg->val.scnodes = NULL;
1006 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001007 } else if (src->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +02001008 set_fill_boolean(trg, src->val.bln);
Michal Vasko44f3d2c2020-08-24 09:49:38 +02001009 } else if (src->type == LYXP_SET_NUMBER) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001010 set_fill_number(trg, src->val.num);
1011 } else if (src->type == LYXP_SET_STRING) {
1012 set_fill_string(trg, src->val.str, strlen(src->val.str));
1013 } else {
1014 if (trg->type == LYXP_SET_NODE_SET) {
1015 free(trg->val.nodes);
1016 } else if (trg->type == LYXP_SET_STRING) {
1017 free(trg->val.str);
1018 }
1019
Michal Vaskod3678892020-05-21 10:06:58 +02001020 assert(src->type == LYXP_SET_NODE_SET);
1021
1022 trg->type = LYXP_SET_NODE_SET;
1023 trg->used = src->used;
1024 trg->size = src->used;
1025 trg->ctx_pos = src->ctx_pos;
1026 trg->ctx_size = src->ctx_size;
1027
Michal Vasko08e9b112021-06-11 15:41:17 +02001028 if (trg->size) {
1029 trg->val.nodes = malloc(trg->size * sizeof *trg->val.nodes);
1030 LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), );
1031 memcpy(trg->val.nodes, src->val.nodes, src->used * sizeof *src->val.nodes);
1032 } else {
1033 trg->val.nodes = NULL;
1034 }
Michal Vaskod3678892020-05-21 10:06:58 +02001035 if (src->ht) {
1036 trg->ht = lyht_dup(src->ht);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001037 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02001038 trg->ht = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001039 }
1040 }
1041}
1042
1043/**
1044 * @brief Clear context of all schema nodes.
1045 *
1046 * @param[in] set Set to clear.
Michal Vasko1a09b212021-05-06 13:00:10 +02001047 * @param[in] new_ctx New context state for all the nodes currently in the context.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001048 */
1049static void
Michal Vasko1a09b212021-05-06 13:00:10 +02001050set_scnode_clear_ctx(struct lyxp_set *set, int32_t new_ctx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001051{
1052 uint32_t i;
1053
1054 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001055 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko1a09b212021-05-06 13:00:10 +02001056 set->val.scnodes[i].in_ctx = new_ctx;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001057 } else if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START) {
1058 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001059 }
1060 }
1061}
1062
1063/**
1064 * @brief Remove a node from a set. Removing last node changes
1065 * set into LYXP_SET_EMPTY. Context position aware.
1066 *
1067 * @param[in] set Set to use.
1068 * @param[in] idx Index from @p set of the node to be removed.
1069 */
1070static void
1071set_remove_node(struct lyxp_set *set, uint32_t idx)
1072{
1073 assert(set && (set->type == LYXP_SET_NODE_SET));
1074 assert(idx < set->used);
1075
1076 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
1077
1078 --set->used;
Michal Vasko08e9b112021-06-11 15:41:17 +02001079 if (idx < set->used) {
1080 memmove(&set->val.nodes[idx], &set->val.nodes[idx + 1], (set->used - idx) * sizeof *set->val.nodes);
1081 } else if (!set->used) {
Michal Vaskod3678892020-05-21 10:06:58 +02001082 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001083 }
1084}
1085
1086/**
Michal Vasko2caefc12019-11-14 16:07:56 +01001087 * @brief Remove a node from a set by setting its type to LYXP_NODE_NONE.
Michal Vasko57eab132019-09-24 11:46:26 +02001088 *
1089 * @param[in] set Set to use.
1090 * @param[in] idx Index from @p set of the node to be removed.
1091 */
1092static void
Michal Vasko2caefc12019-11-14 16:07:56 +01001093set_remove_node_none(struct lyxp_set *set, uint32_t idx)
Michal Vasko57eab132019-09-24 11:46:26 +02001094{
1095 assert(set && (set->type == LYXP_SET_NODE_SET));
1096 assert(idx < set->used);
1097
Michal Vasko2caefc12019-11-14 16:07:56 +01001098 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1099 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
1100 }
1101 set->val.nodes[idx].type = LYXP_NODE_NONE;
Michal Vasko57eab132019-09-24 11:46:26 +02001102}
1103
1104/**
Michal Vasko2caefc12019-11-14 16:07:56 +01001105 * @brief Remove all LYXP_NODE_NONE nodes from a set. Removing last node changes
Michal Vasko57eab132019-09-24 11:46:26 +02001106 * set into LYXP_SET_EMPTY. Context position aware.
1107 *
1108 * @param[in] set Set to consolidate.
1109 */
1110static void
Michal Vasko2caefc12019-11-14 16:07:56 +01001111set_remove_nodes_none(struct lyxp_set *set)
Michal Vasko57eab132019-09-24 11:46:26 +02001112{
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01001113 uint32_t i, orig_used, end = 0;
1114 int64_t start;
Michal Vasko57eab132019-09-24 11:46:26 +02001115
Michal Vaskod3678892020-05-21 10:06:58 +02001116 assert(set);
Michal Vasko57eab132019-09-24 11:46:26 +02001117
1118 orig_used = set->used;
1119 set->used = 0;
Michal Vaskod989ba02020-08-24 10:59:24 +02001120 for (i = 0; i < orig_used; ) {
Michal Vasko57eab132019-09-24 11:46:26 +02001121 start = -1;
1122 do {
Michal Vasko2caefc12019-11-14 16:07:56 +01001123 if ((set->val.nodes[i].type != LYXP_NODE_NONE) && (start == -1)) {
Michal Vasko57eab132019-09-24 11:46:26 +02001124 start = i;
Michal Vasko2caefc12019-11-14 16:07:56 +01001125 } else if ((start > -1) && (set->val.nodes[i].type == LYXP_NODE_NONE)) {
Michal Vasko57eab132019-09-24 11:46:26 +02001126 end = i;
1127 ++i;
1128 break;
1129 }
1130
1131 ++i;
1132 if (i == orig_used) {
1133 end = i;
1134 }
1135 } while (i < orig_used);
1136
1137 if (start > -1) {
1138 /* move the whole chunk of valid nodes together */
1139 if (set->used != (unsigned)start) {
1140 memmove(&set->val.nodes[set->used], &set->val.nodes[start], (end - start) * sizeof *set->val.nodes);
1141 }
1142 set->used += end - start;
1143 }
1144 }
Michal Vasko57eab132019-09-24 11:46:26 +02001145}
1146
1147/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02001148 * @brief Check for duplicates in a node set.
1149 *
1150 * @param[in] set Set to check.
1151 * @param[in] node Node to look for in @p set.
1152 * @param[in] node_type Type of @p node.
1153 * @param[in] skip_idx Index from @p set to skip.
1154 * @return LY_ERR
1155 */
1156static LY_ERR
1157set_dup_node_check(const struct lyxp_set *set, const struct lyd_node *node, enum lyxp_node_type node_type, int skip_idx)
1158{
1159 uint32_t i;
1160
Michal Vasko2caefc12019-11-14 16:07:56 +01001161 if (set->ht && node) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001162 return set_dup_node_hash_check(set, (struct lyd_node *)node, node_type, skip_idx);
1163 }
1164
1165 for (i = 0; i < set->used; ++i) {
1166 if ((skip_idx > -1) && (i == (unsigned)skip_idx)) {
1167 continue;
1168 }
1169
1170 if ((set->val.nodes[i].node == node) && (set->val.nodes[i].type == node_type)) {
1171 return LY_EEXIST;
1172 }
1173 }
1174
1175 return LY_SUCCESS;
1176}
1177
Radek Krejci857189e2020-09-01 13:26:36 +02001178ly_bool
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001179lyxp_set_scnode_contains(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type, int skip_idx,
1180 uint32_t *index_p)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001181{
1182 uint32_t i;
1183
1184 for (i = 0; i < set->used; ++i) {
1185 if ((skip_idx > -1) && (i == (unsigned)skip_idx)) {
1186 continue;
1187 }
1188
1189 if ((set->val.scnodes[i].scnode == node) && (set->val.scnodes[i].type == node_type)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001190 if (index_p) {
1191 *index_p = i;
1192 }
1193 return 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001194 }
1195 }
1196
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001197 return 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001198}
1199
Michal Vaskoecd62de2019-11-13 12:35:11 +01001200void
1201lyxp_set_scnode_merge(struct lyxp_set *set1, struct lyxp_set *set2)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001202{
1203 uint32_t orig_used, i, j;
1204
Michal Vaskod3678892020-05-21 10:06:58 +02001205 assert((set1->type == LYXP_SET_SCNODE_SET) && (set2->type == LYXP_SET_SCNODE_SET));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001206
Michal Vaskod3678892020-05-21 10:06:58 +02001207 if (!set2->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001208 return;
1209 }
1210
Michal Vaskod3678892020-05-21 10:06:58 +02001211 if (!set1->used) {
aPiecekadc1e4f2021-10-07 11:15:12 +02001212 /* release hidden allocated data (lyxp_set.size) */
1213 lyxp_set_free_content(set1);
1214 /* direct copying of the entire structure */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001215 memcpy(set1, set2, sizeof *set1);
1216 return;
1217 }
1218
1219 if (set1->used + set2->used > set1->size) {
1220 set1->size = set1->used + set2->used;
1221 set1->val.scnodes = ly_realloc(set1->val.scnodes, set1->size * sizeof *set1->val.scnodes);
1222 LY_CHECK_ERR_RET(!set1->val.scnodes, LOGMEM(set1->ctx), );
1223 }
1224
1225 orig_used = set1->used;
1226
1227 for (i = 0; i < set2->used; ++i) {
1228 for (j = 0; j < orig_used; ++j) {
1229 /* detect duplicities */
1230 if (set1->val.scnodes[j].scnode == set2->val.scnodes[i].scnode) {
1231 break;
1232 }
1233 }
1234
Michal Vasko0587bce2020-12-10 12:19:21 +01001235 if (j < orig_used) {
1236 /* node is there, but update its status if needed */
1237 if (set1->val.scnodes[j].in_ctx == LYXP_SET_SCNODE_START_USED) {
1238 set1->val.scnodes[j].in_ctx = set2->val.scnodes[i].in_ctx;
Michal Vasko1a09b212021-05-06 13:00:10 +02001239 } else if ((set1->val.scnodes[j].in_ctx == LYXP_SET_SCNODE_ATOM_NODE) &&
1240 (set2->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_VAL)) {
1241 set1->val.scnodes[j].in_ctx = set2->val.scnodes[i].in_ctx;
Michal Vasko0587bce2020-12-10 12:19:21 +01001242 }
1243 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001244 memcpy(&set1->val.scnodes[set1->used], &set2->val.scnodes[i], sizeof *set2->val.scnodes);
1245 ++set1->used;
1246 }
1247 }
1248
Michal Vaskod3678892020-05-21 10:06:58 +02001249 lyxp_set_free_content(set2);
1250 set2->type = LYXP_SET_SCNODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001251}
1252
1253/**
1254 * @brief Insert a node into a set. Context position aware.
1255 *
1256 * @param[in] set Set to use.
1257 * @param[in] node Node to insert to @p set.
1258 * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting.
1259 * @param[in] node_type Node type of @p node.
1260 * @param[in] idx Index in @p set to insert into.
1261 */
1262static void
1263set_insert_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx)
1264{
Michal Vaskod3678892020-05-21 10:06:58 +02001265 assert(set && (set->type == LYXP_SET_NODE_SET));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001266
Michal Vaskod3678892020-05-21 10:06:58 +02001267 if (!set->size) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001268 /* first item */
1269 if (idx) {
1270 /* no real harm done, but it is a bug */
1271 LOGINT(set->ctx);
1272 idx = 0;
1273 }
1274 set->val.nodes = malloc(LYXP_SET_SIZE_START * sizeof *set->val.nodes);
1275 LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), );
1276 set->type = LYXP_SET_NODE_SET;
1277 set->used = 0;
1278 set->size = LYXP_SET_SIZE_START;
1279 set->ctx_pos = 1;
1280 set->ctx_size = 1;
1281 set->ht = NULL;
1282 } else {
1283 /* not an empty set */
1284 if (set->used == set->size) {
1285
1286 /* set is full */
Michal Vasko871df522022-04-06 12:14:41 +02001287 set->val.nodes = ly_realloc(set->val.nodes, (set->size * LYXP_SET_SIZE_MUL_STEP) * sizeof *set->val.nodes);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001288 LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), );
Michal Vasko871df522022-04-06 12:14:41 +02001289 set->size *= LYXP_SET_SIZE_MUL_STEP;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001290 }
1291
1292 if (idx > set->used) {
1293 LOGINT(set->ctx);
1294 idx = set->used;
1295 }
1296
1297 /* make space for the new node */
1298 if (idx < set->used) {
1299 memmove(&set->val.nodes[idx + 1], &set->val.nodes[idx], (set->used - idx) * sizeof *set->val.nodes);
1300 }
1301 }
1302
1303 /* finally assign the value */
1304 set->val.nodes[idx].node = (struct lyd_node *)node;
1305 set->val.nodes[idx].type = node_type;
1306 set->val.nodes[idx].pos = pos;
1307 ++set->used;
1308
Michal Vasko2416fdd2021-10-01 11:07:10 +02001309 /* add into hash table */
1310 set_insert_node_hash(set, (struct lyd_node *)node, node_type);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001311}
1312
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001313LY_ERR
Michal Vaskoddd76592022-01-17 13:34:48 +01001314lyxp_set_scnode_insert_node(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type,
1315 uint32_t *index_p)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001316{
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001317 uint32_t index;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001318
1319 assert(set->type == LYXP_SET_SCNODE_SET);
1320
Michal Vasko871df522022-04-06 12:14:41 +02001321 if (!set->size) {
1322 /* first item */
1323 set->val.scnodes = malloc(LYXP_SET_SIZE_START * sizeof *set->val.scnodes);
1324 LY_CHECK_ERR_RET(!set->val.scnodes, LOGMEM(set->ctx), LY_EMEM);
1325 set->type = LYXP_SET_SCNODE_SET;
1326 set->used = 0;
1327 set->size = LYXP_SET_SIZE_START;
1328 set->ctx_pos = 1;
1329 set->ctx_size = 1;
1330 set->ht = NULL;
1331 }
1332
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001333 if (lyxp_set_scnode_contains(set, node, node_type, -1, &index)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001334 set->val.scnodes[index].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001335 } else {
1336 if (set->used == set->size) {
Michal Vasko871df522022-04-06 12:14:41 +02001337 set->val.scnodes = ly_realloc(set->val.scnodes, (set->size * LYXP_SET_SIZE_MUL_STEP) * sizeof *set->val.scnodes);
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001338 LY_CHECK_ERR_RET(!set->val.scnodes, LOGMEM(set->ctx), LY_EMEM);
Michal Vasko871df522022-04-06 12:14:41 +02001339 set->size *= LYXP_SET_SIZE_MUL_STEP;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001340 }
1341
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001342 index = set->used;
1343 set->val.scnodes[index].scnode = (struct lysc_node *)node;
1344 set->val.scnodes[index].type = node_type;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001345 set->val.scnodes[index].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001346 ++set->used;
1347 }
1348
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001349 if (index_p) {
1350 *index_p = index;
1351 }
1352
1353 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001354}
1355
1356/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02001357 * @brief Set all nodes with ctx 1 to a new unique context value.
1358 *
1359 * @param[in] set Set to modify.
1360 * @return New context value.
1361 */
Michal Vasko5c4e5892019-11-14 12:31:38 +01001362static int32_t
Michal Vasko03ff5a72019-09-11 13:49:33 +02001363set_scnode_new_in_ctx(struct lyxp_set *set)
1364{
Michal Vasko5c4e5892019-11-14 12:31:38 +01001365 uint32_t i;
1366 int32_t ret_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001367
1368 assert(set->type == LYXP_SET_SCNODE_SET);
1369
Radek Krejcif13b87b2020-12-01 22:02:17 +01001370 ret_ctx = LYXP_SET_SCNODE_ATOM_PRED_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001371retry:
1372 for (i = 0; i < set->used; ++i) {
1373 if (set->val.scnodes[i].in_ctx >= ret_ctx) {
1374 ret_ctx = set->val.scnodes[i].in_ctx + 1;
1375 goto retry;
1376 }
1377 }
1378 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001379 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001380 set->val.scnodes[i].in_ctx = ret_ctx;
1381 }
1382 }
1383
1384 return ret_ctx;
1385}
1386
1387/**
1388 * @brief Get unique @p node position in the data.
1389 *
1390 * @param[in] node Node to find.
1391 * @param[in] node_type Node type of @p node.
1392 * @param[in] root Root node.
1393 * @param[in] root_type Type of the XPath @p root node.
1394 * @param[in] prev Node that we think is before @p node in DFS from @p root. Can optionally
1395 * be used to increase efficiency and start the DFS from this node.
1396 * @param[in] prev_pos Node @p prev position. Optional, but must be set if @p prev is set.
1397 * @return Node position.
1398 */
1399static uint32_t
1400get_node_pos(const struct lyd_node *node, enum lyxp_node_type node_type, const struct lyd_node *root,
Radek Krejci0f969882020-08-21 16:56:47 +02001401 enum lyxp_node_type root_type, const struct lyd_node **prev, uint32_t *prev_pos)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001402{
Michal Vasko56daf732020-08-10 10:57:18 +02001403 const struct lyd_node *elem = NULL, *top_sibling;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001404 uint32_t pos = 1;
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001405 ly_bool found = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001406
1407 assert(prev && prev_pos && !root->prev->next);
1408
1409 if ((node_type == LYXP_NODE_ROOT) || (node_type == LYXP_NODE_ROOT_CONFIG)) {
1410 return 0;
1411 }
1412
1413 if (*prev) {
1414 /* start from the previous element instead from the root */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001415 pos = *prev_pos;
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001416 for (top_sibling = *prev; top_sibling->parent; top_sibling = lyd_parent(top_sibling)) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001417 goto dfs_search;
1418 }
1419
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001420 LY_LIST_FOR(root, top_sibling) {
Michal Vasko56daf732020-08-10 10:57:18 +02001421 LYD_TREE_DFS_BEGIN(top_sibling, elem) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001422dfs_search:
Michal Vaskoa9309bb2021-07-09 09:31:55 +02001423 LYD_TREE_DFS_continue = 0;
1424
Michal Vasko56daf732020-08-10 10:57:18 +02001425 if (*prev && !elem) {
1426 /* resume previous DFS */
1427 elem = LYD_TREE_DFS_next = (struct lyd_node *)*prev;
1428 LYD_TREE_DFS_continue = 0;
1429 }
1430
Michal Vasko482a6822022-05-06 08:56:12 +02001431 if (!elem->schema || ((root_type == LYXP_NODE_ROOT_CONFIG) && (elem->schema->flags & LYS_CONFIG_R))) {
Michal Vasko56daf732020-08-10 10:57:18 +02001432 /* skip */
1433 LYD_TREE_DFS_continue = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001434 } else {
Michal Vasko56daf732020-08-10 10:57:18 +02001435 if (elem == node) {
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001436 found = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001437 break;
1438 }
Michal Vasko56daf732020-08-10 10:57:18 +02001439 ++pos;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001440 }
Michal Vasko56daf732020-08-10 10:57:18 +02001441
1442 LYD_TREE_DFS_END(top_sibling, elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001443 }
1444
1445 /* node found */
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001446 if (found) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001447 break;
1448 }
1449 }
1450
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001451 if (!found) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001452 if (!(*prev)) {
1453 /* we went from root and failed to find it, cannot be */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001454 LOGINT(LYD_CTX(node));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001455 return 0;
1456 } else {
Michal Vasko56daf732020-08-10 10:57:18 +02001457 /* start the search again from the beginning */
1458 *prev = root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001459
Michal Vasko56daf732020-08-10 10:57:18 +02001460 top_sibling = root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001461 pos = 1;
1462 goto dfs_search;
1463 }
1464 }
1465
1466 /* remember the last found node for next time */
1467 *prev = node;
1468 *prev_pos = pos;
1469
1470 return pos;
1471}
1472
1473/**
1474 * @brief Assign (fill) missing node positions.
1475 *
1476 * @param[in] set Set to fill positions in.
1477 * @param[in] root Context root node.
1478 * @param[in] root_type Context root type.
1479 * @return LY_ERR
1480 */
1481static LY_ERR
1482set_assign_pos(struct lyxp_set *set, const struct lyd_node *root, enum lyxp_node_type root_type)
1483{
1484 const struct lyd_node *prev = NULL, *tmp_node;
1485 uint32_t i, tmp_pos = 0;
1486
1487 for (i = 0; i < set->used; ++i) {
1488 if (!set->val.nodes[i].pos) {
1489 tmp_node = NULL;
1490 switch (set->val.nodes[i].type) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001491 case LYXP_NODE_META:
1492 tmp_node = set->val.meta[i].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001493 if (!tmp_node) {
1494 LOGINT_RET(root->schema->module->ctx);
1495 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01001496 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001497 case LYXP_NODE_ELEM:
1498 case LYXP_NODE_TEXT:
1499 if (!tmp_node) {
1500 tmp_node = set->val.nodes[i].node;
1501 }
1502 set->val.nodes[i].pos = get_node_pos(tmp_node, set->val.nodes[i].type, root, root_type, &prev, &tmp_pos);
1503 break;
1504 default:
1505 /* all roots have position 0 */
1506 break;
1507 }
1508 }
1509 }
1510
1511 return LY_SUCCESS;
1512}
1513
1514/**
Michal Vasko9f96a052020-03-10 09:41:45 +01001515 * @brief Get unique @p meta position in the parent metadata.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001516 *
Michal Vasko9f96a052020-03-10 09:41:45 +01001517 * @param[in] meta Metadata to use.
1518 * @return Metadata position.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001519 */
1520static uint16_t
Michal Vasko9f96a052020-03-10 09:41:45 +01001521get_meta_pos(struct lyd_meta *meta)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001522{
1523 uint16_t pos = 0;
Michal Vasko9f96a052020-03-10 09:41:45 +01001524 struct lyd_meta *meta2;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001525
Michal Vasko9f96a052020-03-10 09:41:45 +01001526 for (meta2 = meta->parent->meta; meta2 && (meta2 != meta); meta2 = meta2->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001527 ++pos;
1528 }
1529
Michal Vasko9f96a052020-03-10 09:41:45 +01001530 assert(meta2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001531 return pos;
1532}
1533
1534/**
1535 * @brief Compare 2 nodes in respect to XPath document order.
1536 *
1537 * @param[in] item1 1st node.
1538 * @param[in] item2 2nd node.
1539 * @return If 1st > 2nd returns 1, 1st == 2nd returns 0, and 1st < 2nd returns -1.
1540 */
1541static int
1542set_sort_compare(struct lyxp_set_node *item1, struct lyxp_set_node *item2)
1543{
Michal Vasko9f96a052020-03-10 09:41:45 +01001544 uint32_t meta_pos1 = 0, meta_pos2 = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001545
1546 if (item1->pos < item2->pos) {
1547 return -1;
1548 }
1549
1550 if (item1->pos > item2->pos) {
1551 return 1;
1552 }
1553
1554 /* node positions are equal, the fun case */
1555
1556 /* 1st ELEM - == - 2nd TEXT, 1st TEXT - == - 2nd ELEM */
1557 /* special case since text nodes are actually saved as their parents */
1558 if ((item1->node == item2->node) && (item1->type != item2->type)) {
1559 if (item1->type == LYXP_NODE_ELEM) {
1560 assert(item2->type == LYXP_NODE_TEXT);
1561 return -1;
1562 } else {
1563 assert((item1->type == LYXP_NODE_TEXT) && (item2->type == LYXP_NODE_ELEM));
1564 return 1;
1565 }
1566 }
1567
Michal Vasko9f96a052020-03-10 09:41:45 +01001568 /* we need meta positions now */
1569 if (item1->type == LYXP_NODE_META) {
1570 meta_pos1 = get_meta_pos((struct lyd_meta *)item1->node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001571 }
Michal Vasko9f96a052020-03-10 09:41:45 +01001572 if (item2->type == LYXP_NODE_META) {
1573 meta_pos2 = get_meta_pos((struct lyd_meta *)item2->node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001574 }
1575
Michal Vasko9f96a052020-03-10 09:41:45 +01001576 /* 1st ROOT - 2nd ROOT, 1st ELEM - 2nd ELEM, 1st TEXT - 2nd TEXT, 1st META - =pos= - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001577 /* check for duplicates */
1578 if (item1->node == item2->node) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001579 assert((item1->type == item2->type) && ((item1->type != LYXP_NODE_META) || (meta_pos1 == meta_pos2)));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001580 return 0;
1581 }
1582
Michal Vasko9f96a052020-03-10 09:41:45 +01001583 /* 1st ELEM - 2nd TEXT, 1st ELEM - any pos - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001584 /* elem is always first, 2nd node is after it */
1585 if (item1->type == LYXP_NODE_ELEM) {
1586 assert(item2->type != LYXP_NODE_ELEM);
1587 return -1;
1588 }
1589
Michal Vasko9f96a052020-03-10 09:41:45 +01001590 /* 1st TEXT - 2nd ELEM, 1st TEXT - any pos - 2nd META, 1st META - any pos - 2nd ELEM, 1st META - >pos> - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001591 /* 2nd is before 1st */
Michal Vasko69730152020-10-09 16:30:07 +02001592 if (((item1->type == LYXP_NODE_TEXT) &&
1593 ((item2->type == LYXP_NODE_ELEM) || (item2->type == LYXP_NODE_META))) ||
1594 ((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_ELEM)) ||
1595 (((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_META)) &&
1596 (meta_pos1 > meta_pos2))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001597 return 1;
1598 }
1599
Michal Vasko9f96a052020-03-10 09:41:45 +01001600 /* 1st META - any pos - 2nd TEXT, 1st META <pos< - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001601 /* 2nd is after 1st */
1602 return -1;
1603}
1604
1605/**
1606 * @brief Set cast for comparisons.
1607 *
Michal Vasko8abcecc2022-07-28 09:55:01 +02001608 * @param[in,out] trg Target set to cast source into.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001609 * @param[in] src Source set.
1610 * @param[in] type Target set type.
1611 * @param[in] src_idx Source set node index.
Michal Vasko8abcecc2022-07-28 09:55:01 +02001612 * @return LY_SUCCESS on success.
1613 * @return LY_ERR value on error.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001614 */
1615static LY_ERR
Michal Vasko8abcecc2022-07-28 09:55:01 +02001616set_comp_cast(struct lyxp_set *trg, const struct lyxp_set *src, enum lyxp_set_type type, uint32_t src_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001617{
1618 assert(src->type == LYXP_SET_NODE_SET);
1619
1620 set_init(trg, src);
1621
1622 /* insert node into target set */
1623 set_insert_node(trg, src->val.nodes[src_idx].node, src->val.nodes[src_idx].pos, src->val.nodes[src_idx].type, 0);
1624
1625 /* cast target set appropriately */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001626 return lyxp_set_cast(trg, type);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001627}
1628
Michal Vasko4c7763f2020-07-27 17:40:37 +02001629/**
1630 * @brief Set content canonization for comparisons.
1631 *
Michal Vasko8abcecc2022-07-28 09:55:01 +02001632 * @param[in,out] set Set to canonize.
Michal Vasko4c7763f2020-07-27 17:40:37 +02001633 * @param[in] xp_node Source XPath node/meta to use for canonization.
Michal Vasko8abcecc2022-07-28 09:55:01 +02001634 * @return LY_SUCCESS on success.
1635 * @return LY_ERR value on error.
Michal Vasko4c7763f2020-07-27 17:40:37 +02001636 */
1637static LY_ERR
Michal Vasko8abcecc2022-07-28 09:55:01 +02001638set_comp_canonize(struct lyxp_set *set, const struct lyxp_set_node *xp_node)
Michal Vasko4c7763f2020-07-27 17:40:37 +02001639{
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001640 const struct lysc_type *type = NULL;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001641 struct lyd_value val;
1642 struct ly_err_item *err = NULL;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001643 LY_ERR rc;
1644
1645 /* is there anything to canonize even? */
Michal Vasko8abcecc2022-07-28 09:55:01 +02001646 if (set->type == LYXP_SET_STRING) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02001647 /* do we have a type to use for canonization? */
1648 if ((xp_node->type == LYXP_NODE_ELEM) && (xp_node->node->schema->nodetype & LYD_NODE_TERM)) {
1649 type = ((struct lyd_node_term *)xp_node->node)->value.realtype;
1650 } else if (xp_node->type == LYXP_NODE_META) {
1651 type = ((struct lyd_meta *)xp_node->node)->value.realtype;
1652 }
1653 }
1654 if (!type) {
Michal Vasko8abcecc2022-07-28 09:55:01 +02001655 /* no canonization needed/possible */
1656 return LY_SUCCESS;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001657 }
1658
Michal Vasko8abcecc2022-07-28 09:55:01 +02001659 /* check for built-in types without required canonization */
1660 if ((type->basetype == LY_TYPE_STRING) && (type->plugin->store == lyplg_type_store_string)) {
1661 /* string */
1662 return LY_SUCCESS;
1663 }
1664 if ((type->basetype == LY_TYPE_BOOL) && (type->plugin->store == lyplg_type_store_boolean)) {
1665 /* boolean */
1666 return LY_SUCCESS;
1667 }
1668 if ((type->basetype == LY_TYPE_ENUM) && (type->plugin->store == lyplg_type_store_enum)) {
1669 /* enumeration */
1670 return LY_SUCCESS;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001671 }
1672
Michal Vasko8abcecc2022-07-28 09:55:01 +02001673 /* print canonized string, ignore errors, the value may not satisfy schema constraints */
1674 rc = type->plugin->store(set->ctx, type, set->val.str, strlen(set->val.str), 0, set->format, set->prefix_data,
Michal Vasko405cc9e2020-12-01 12:01:27 +01001675 LYD_HINT_DATA, xp_node->node->schema, &val, NULL, &err);
Michal Vasko4c7763f2020-07-27 17:40:37 +02001676 ly_err_free(err);
1677 if (rc) {
Michal Vasko8abcecc2022-07-28 09:55:01 +02001678 /* invalid value, function store automaticaly dealloc value when fail */
1679 return LY_SUCCESS;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001680 }
1681
Michal Vasko8abcecc2022-07-28 09:55:01 +02001682 /* use the canonized string value */
1683 free(set->val.str);
1684 set->val.str = strdup(lyd_value_get_canonical(set->ctx, &val));
1685 type->plugin->free(set->ctx, &val);
1686 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), LY_EMEM);
Michal Vasko4c7763f2020-07-27 17:40:37 +02001687
Michal Vasko4c7763f2020-07-27 17:40:37 +02001688 return LY_SUCCESS;
1689}
1690
Michal Vasko03ff5a72019-09-11 13:49:33 +02001691/**
1692 * @brief Bubble sort @p set into XPath document order.
Michal Vasko49fec8e2022-05-24 10:28:33 +02001693 * Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001694 *
1695 * @param[in] set Set to sort.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001696 * @return How many times the whole set was traversed - 1 (if set was sorted, returns 0).
1697 */
1698static int
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001699set_sort(struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001700{
1701 uint32_t i, j;
Radek Krejci1deb5be2020-08-26 16:43:36 +02001702 int ret = 0, cmp;
Radek Krejci857189e2020-09-01 13:26:36 +02001703 ly_bool inverted, change;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001704 const struct lyd_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001705 struct lyxp_set_node item;
1706 struct lyxp_set_hash_node hnode;
1707 uint64_t hash;
1708
Michal Vasko3cf8fbf2020-05-27 15:21:21 +02001709 if ((set->type != LYXP_SET_NODE_SET) || (set->used < 2)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001710 return 0;
1711 }
1712
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001713 /* find first top-level node to be used as anchor for positions */
Michal Vasko4a7d4d62021-12-13 17:05:06 +01001714 for (root = set->tree; root->parent; root = lyd_parent(root)) {}
Michal Vaskod989ba02020-08-24 10:59:24 +02001715 for ( ; root->prev->next; root = root->prev) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001716
1717 /* fill positions */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001718 if (set_assign_pos(set, root, set->root_type)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001719 return -1;
1720 }
1721
Michal Vasko88a9e802022-05-24 10:50:28 +02001722#ifndef NDEBUG
Michal Vasko03ff5a72019-09-11 13:49:33 +02001723 LOGDBG(LY_LDGXPATH, "SORT BEGIN");
1724 print_set_debug(set);
Michal Vasko88a9e802022-05-24 10:50:28 +02001725#endif
Michal Vasko03ff5a72019-09-11 13:49:33 +02001726
1727 for (i = 0; i < set->used; ++i) {
1728 inverted = 0;
1729 change = 0;
1730
1731 for (j = 1; j < set->used - i; ++j) {
1732 /* compare node positions */
1733 if (inverted) {
1734 cmp = set_sort_compare(&set->val.nodes[j], &set->val.nodes[j - 1]);
1735 } else {
1736 cmp = set_sort_compare(&set->val.nodes[j - 1], &set->val.nodes[j]);
1737 }
1738
1739 /* swap if needed */
1740 if ((inverted && (cmp < 0)) || (!inverted && (cmp > 0))) {
1741 change = 1;
1742
1743 item = set->val.nodes[j - 1];
1744 set->val.nodes[j - 1] = set->val.nodes[j];
1745 set->val.nodes[j] = item;
1746 } else {
1747 /* whether node_pos1 should be smaller than node_pos2 or the other way around */
1748 inverted = !inverted;
1749 }
1750 }
1751
1752 ++ret;
1753
1754 if (!change) {
1755 break;
1756 }
1757 }
1758
Michal Vasko88a9e802022-05-24 10:50:28 +02001759#ifndef NDEBUG
Michal Vasko03ff5a72019-09-11 13:49:33 +02001760 LOGDBG(LY_LDGXPATH, "SORT END %d", ret);
1761 print_set_debug(set);
Michal Vasko88a9e802022-05-24 10:50:28 +02001762#endif
Michal Vasko03ff5a72019-09-11 13:49:33 +02001763
1764 /* check node hashes */
1765 if (set->used >= LYD_HT_MIN_ITEMS) {
1766 assert(set->ht);
1767 for (i = 0; i < set->used; ++i) {
1768 hnode.node = set->val.nodes[i].node;
1769 hnode.type = set->val.nodes[i].type;
1770
1771 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
1772 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
1773 hash = dict_hash_multi(hash, NULL, 0);
1774
1775 assert(!lyht_find(set->ht, &hnode, hash, NULL));
1776 }
1777 }
1778
1779 return ret - 1;
1780}
1781
Michal Vasko03ff5a72019-09-11 13:49:33 +02001782/**
1783 * @brief Merge 2 sorted sets into one.
1784 *
1785 * @param[in,out] trg Set to merge into. Duplicates are removed.
1786 * @param[in] src Set to be merged into @p trg. It is cast to #LYXP_SET_EMPTY on success.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001787 * @return LY_ERR
1788 */
1789static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001790set_sorted_merge(struct lyxp_set *trg, struct lyxp_set *src)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001791{
1792 uint32_t i, j, k, count, dup_count;
1793 int cmp;
1794 const struct lyd_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001795
Michal Vaskod3678892020-05-21 10:06:58 +02001796 if ((trg->type != LYXP_SET_NODE_SET) || (src->type != LYXP_SET_NODE_SET)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001797 return LY_EINVAL;
1798 }
1799
Michal Vaskod3678892020-05-21 10:06:58 +02001800 if (!src->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001801 return LY_SUCCESS;
Michal Vaskod3678892020-05-21 10:06:58 +02001802 } else if (!trg->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001803 set_fill_set(trg, src);
Michal Vaskod3678892020-05-21 10:06:58 +02001804 lyxp_set_free_content(src);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001805 return LY_SUCCESS;
1806 }
1807
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001808 /* find first top-level node to be used as anchor for positions */
Michal Vasko0143b682021-12-13 17:13:09 +01001809 for (root = trg->tree; root->parent; root = lyd_parent(root)) {}
Michal Vaskod989ba02020-08-24 10:59:24 +02001810 for ( ; root->prev->next; root = root->prev) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001811
1812 /* fill positions */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001813 if (set_assign_pos(trg, root, trg->root_type) || set_assign_pos(src, root, src->root_type)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001814 return LY_EINT;
1815 }
1816
1817#ifndef NDEBUG
1818 LOGDBG(LY_LDGXPATH, "MERGE target");
1819 print_set_debug(trg);
1820 LOGDBG(LY_LDGXPATH, "MERGE source");
1821 print_set_debug(src);
1822#endif
1823
1824 /* make memory for the merge (duplicates are not detected yet, so space
1825 * will likely be wasted on them, too bad) */
1826 if (trg->size - trg->used < src->used) {
1827 trg->size = trg->used + src->used;
1828
1829 trg->val.nodes = ly_realloc(trg->val.nodes, trg->size * sizeof *trg->val.nodes);
1830 LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx), LY_EMEM);
1831 }
1832
1833 i = 0;
1834 j = 0;
1835 count = 0;
1836 dup_count = 0;
1837 do {
1838 cmp = set_sort_compare(&src->val.nodes[i], &trg->val.nodes[j]);
1839 if (!cmp) {
1840 if (!count) {
1841 /* duplicate, just skip it */
1842 ++i;
1843 ++j;
1844 } else {
1845 /* we are copying something already, so let's copy the duplicate too,
1846 * we are hoping that afterwards there are some more nodes to
1847 * copy and this way we can copy them all together */
1848 ++count;
1849 ++dup_count;
1850 ++i;
1851 ++j;
1852 }
1853 } else if (cmp < 0) {
1854 /* inserting src node into trg, just remember it for now */
1855 ++count;
1856 ++i;
1857
1858 /* insert the hash now */
1859 set_insert_node_hash(trg, src->val.nodes[i - 1].node, src->val.nodes[i - 1].type);
1860 } else if (count) {
1861copy_nodes:
1862 /* time to actually copy the nodes, we have found the largest block of nodes */
1863 memmove(&trg->val.nodes[j + (count - dup_count)],
1864 &trg->val.nodes[j],
1865 (trg->used - j) * sizeof *trg->val.nodes);
1866 memcpy(&trg->val.nodes[j - dup_count], &src->val.nodes[i - count], count * sizeof *src->val.nodes);
1867
1868 trg->used += count - dup_count;
1869 /* do not change i, except the copying above, we are basically doing exactly what is in the else branch below */
1870 j += count - dup_count;
1871
1872 count = 0;
1873 dup_count = 0;
1874 } else {
1875 ++j;
1876 }
1877 } while ((i < src->used) && (j < trg->used));
1878
1879 if ((i < src->used) || count) {
1880 /* insert all the hashes first */
1881 for (k = i; k < src->used; ++k) {
1882 set_insert_node_hash(trg, src->val.nodes[k].node, src->val.nodes[k].type);
1883 }
1884
1885 /* loop ended, but we need to copy something at trg end */
1886 count += src->used - i;
1887 i = src->used;
1888 goto copy_nodes;
1889 }
1890
1891 /* we are inserting hashes before the actual node insert, which causes
1892 * situations when there were initially not enough items for a hash table,
1893 * but even after some were inserted, hash table was not created (during
1894 * insertion the number of items is not updated yet) */
1895 if (!trg->ht && (trg->used >= LYD_HT_MIN_ITEMS)) {
1896 set_insert_node_hash(trg, NULL, 0);
1897 }
1898
1899#ifndef NDEBUG
1900 LOGDBG(LY_LDGXPATH, "MERGE result");
1901 print_set_debug(trg);
1902#endif
1903
Michal Vaskod3678892020-05-21 10:06:58 +02001904 lyxp_set_free_content(src);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001905 return LY_SUCCESS;
1906}
1907
Michal Vasko14676352020-05-29 11:35:55 +02001908LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02001909lyxp_check_token(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t tok_idx, enum lyxp_token want_tok)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001910{
Michal Vasko004d3152020-06-11 19:59:22 +02001911 if (exp->used == tok_idx) {
Michal Vasko14676352020-05-29 11:35:55 +02001912 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001913 LOGVAL(ctx, LY_VCODE_XP_EOF);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001914 }
Michal Vasko14676352020-05-29 11:35:55 +02001915 return LY_EINCOMPLETE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001916 }
1917
Michal Vasko004d3152020-06-11 19:59:22 +02001918 if (want_tok && (exp->tokens[tok_idx] != want_tok)) {
Michal Vasko14676352020-05-29 11:35:55 +02001919 if (ctx) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02001920 LOGVAL(ctx, LY_VCODE_XP_INTOK2, lyxp_token2str(exp->tokens[tok_idx]),
1921 &exp->expr[exp->tok_pos[tok_idx]], lyxp_token2str(want_tok));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001922 }
Michal Vasko14676352020-05-29 11:35:55 +02001923 return LY_ENOT;
1924 }
1925
1926 return LY_SUCCESS;
1927}
1928
Michal Vasko004d3152020-06-11 19:59:22 +02001929LY_ERR
1930lyxp_next_token(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_token want_tok)
1931{
1932 LY_CHECK_RET(lyxp_check_token(ctx, exp, *tok_idx, want_tok));
1933
1934 /* skip the token */
1935 ++(*tok_idx);
1936
1937 return LY_SUCCESS;
1938}
1939
Michal Vasko14676352020-05-29 11:35:55 +02001940/* just like lyxp_check_token() but tests for 2 tokens */
1941static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02001942exp_check_token2(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t tok_idx, enum lyxp_token want_tok1,
Radek Krejci0f969882020-08-21 16:56:47 +02001943 enum lyxp_token want_tok2)
Michal Vasko14676352020-05-29 11:35:55 +02001944{
Michal Vasko004d3152020-06-11 19:59:22 +02001945 if (exp->used == tok_idx) {
Michal Vasko14676352020-05-29 11:35:55 +02001946 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001947 LOGVAL(ctx, LY_VCODE_XP_EOF);
Michal Vasko14676352020-05-29 11:35:55 +02001948 }
1949 return LY_EINCOMPLETE;
1950 }
1951
Michal Vasko004d3152020-06-11 19:59:22 +02001952 if ((exp->tokens[tok_idx] != want_tok1) && (exp->tokens[tok_idx] != want_tok2)) {
Michal Vasko14676352020-05-29 11:35:55 +02001953 if (ctx) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02001954 LOGVAL(ctx, LY_VCODE_XP_INTOK, lyxp_token2str(exp->tokens[tok_idx]),
Michal Vasko0b468e62020-10-19 09:33:04 +02001955 &exp->expr[exp->tok_pos[tok_idx]]);
Michal Vasko14676352020-05-29 11:35:55 +02001956 }
1957 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001958 }
1959
1960 return LY_SUCCESS;
1961}
1962
Michal Vasko4911eeb2021-06-28 11:23:05 +02001963LY_ERR
1964lyxp_next_token2(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_token want_tok1,
1965 enum lyxp_token want_tok2)
1966{
1967 LY_CHECK_RET(exp_check_token2(ctx, exp, *tok_idx, want_tok1, want_tok2));
1968
1969 /* skip the token */
1970 ++(*tok_idx);
1971
1972 return LY_SUCCESS;
1973}
1974
Michal Vasko03ff5a72019-09-11 13:49:33 +02001975/**
1976 * @brief Stack operation push on the repeat array.
1977 *
1978 * @param[in] exp Expression to use.
Michal Vasko004d3152020-06-11 19:59:22 +02001979 * @param[in] tok_idx Position in the expresion \p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001980 * @param[in] repeat_op_idx Index from \p exp of the operator token. This value is pushed.
1981 */
1982static void
Michal Vasko004d3152020-06-11 19:59:22 +02001983exp_repeat_push(struct lyxp_expr *exp, uint16_t tok_idx, uint16_t repeat_op_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001984{
1985 uint16_t i;
1986
Michal Vasko004d3152020-06-11 19:59:22 +02001987 if (exp->repeat[tok_idx]) {
Radek Krejci1e008d22020-08-17 11:37:37 +02001988 for (i = 0; exp->repeat[tok_idx][i]; ++i) {}
Michal Vasko004d3152020-06-11 19:59:22 +02001989 exp->repeat[tok_idx] = realloc(exp->repeat[tok_idx], (i + 2) * sizeof *exp->repeat[tok_idx]);
1990 LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), );
1991 exp->repeat[tok_idx][i] = repeat_op_idx;
1992 exp->repeat[tok_idx][i + 1] = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001993 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02001994 exp->repeat[tok_idx] = calloc(2, sizeof *exp->repeat[tok_idx]);
1995 LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), );
1996 exp->repeat[tok_idx][0] = repeat_op_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001997 }
1998}
1999
2000/**
2001 * @brief Reparse Predicate. Logs directly on error.
2002 *
2003 * [7] Predicate ::= '[' Expr ']'
2004 *
2005 * @param[in] ctx Context for logging.
2006 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002007 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002008 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002009 * @return LY_ERR
2010 */
2011static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002012reparse_predicate(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t depth)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002013{
2014 LY_ERR rc;
2015
Michal Vasko004d3152020-06-11 19:59:22 +02002016 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002017 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002018 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002019
aPiecekbf968d92021-05-27 14:35:05 +02002020 rc = reparse_or_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002021 LY_CHECK_RET(rc);
2022
Michal Vasko004d3152020-06-11 19:59:22 +02002023 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002024 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002025 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002026
2027 return LY_SUCCESS;
2028}
2029
2030/**
2031 * @brief Reparse RelativeLocationPath. Logs directly on error.
2032 *
2033 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
2034 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
2035 * [6] NodeTest ::= NameTest | NodeType '(' ')'
2036 *
2037 * @param[in] ctx Context for logging.
2038 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002039 * @param[in] tok_idx Position in the expression \p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002040 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002041 * @return LY_ERR (LY_EINCOMPLETE on forward reference)
2042 */
2043static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002044reparse_relative_location_path(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t depth)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002045{
2046 LY_ERR rc;
2047
Michal Vasko004d3152020-06-11 19:59:22 +02002048 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002049 LY_CHECK_RET(rc);
2050
2051 goto step;
2052 do {
2053 /* '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02002054 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002055
Michal Vasko004d3152020-06-11 19:59:22 +02002056 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002057 LY_CHECK_RET(rc);
2058step:
2059 /* Step */
Michal Vasko004d3152020-06-11 19:59:22 +02002060 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002061 case LYXP_TOKEN_DOT:
Michal Vasko004d3152020-06-11 19:59:22 +02002062 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002063 break;
2064
2065 case LYXP_TOKEN_DDOT:
Michal Vasko004d3152020-06-11 19:59:22 +02002066 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002067 break;
2068
Michal Vasko49fec8e2022-05-24 10:28:33 +02002069 case LYXP_TOKEN_AXISNAME:
2070 ++(*tok_idx);
2071
2072 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_DCOLON);
2073 LY_CHECK_RET(rc);
2074
2075 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02002076 case LYXP_TOKEN_AT:
Michal Vasko004d3152020-06-11 19:59:22 +02002077 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002078
Michal Vasko004d3152020-06-11 19:59:22 +02002079 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002080 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002081 if ((exp->tokens[*tok_idx] != LYXP_TOKEN_NAMETEST) && (exp->tokens[*tok_idx] != LYXP_TOKEN_NODETYPE)) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02002082 LOGVAL(ctx, LY_VCODE_XP_INTOK, lyxp_token2str(exp->tokens[*tok_idx]), &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002083 return LY_EVALID;
2084 }
Michal Vasko49fec8e2022-05-24 10:28:33 +02002085 if (exp->tokens[*tok_idx] == LYXP_TOKEN_NODETYPE) {
2086 goto reparse_nodetype;
2087 }
Radek Krejci0f969882020-08-21 16:56:47 +02002088 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02002089 case LYXP_TOKEN_NAMETEST:
Michal Vasko004d3152020-06-11 19:59:22 +02002090 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002091 goto reparse_predicate;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002092
2093 case LYXP_TOKEN_NODETYPE:
Michal Vasko49fec8e2022-05-24 10:28:33 +02002094reparse_nodetype:
Michal Vasko004d3152020-06-11 19:59:22 +02002095 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002096
2097 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02002098 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002099 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002100 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002101
2102 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02002103 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002104 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002105 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002106
2107reparse_predicate:
2108 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002109 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
aPiecekbf968d92021-05-27 14:35:05 +02002110 rc = reparse_predicate(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002111 LY_CHECK_RET(rc);
2112 }
2113 break;
2114 default:
Michal Vasko49fec8e2022-05-24 10:28:33 +02002115 LOGVAL(ctx, LY_VCODE_XP_INTOK, lyxp_token2str(exp->tokens[*tok_idx]), &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002116 return LY_EVALID;
2117 }
Michal Vasko004d3152020-06-11 19:59:22 +02002118 } while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02002119
2120 return LY_SUCCESS;
2121}
2122
2123/**
2124 * @brief Reparse AbsoluteLocationPath. Logs directly on error.
2125 *
2126 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
2127 *
2128 * @param[in] ctx Context for logging.
2129 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002130 * @param[in] tok_idx Position in the expression \p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002131 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002132 * @return LY_ERR
2133 */
2134static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002135reparse_absolute_location_path(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t depth)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002136{
2137 LY_ERR rc;
2138
Michal Vasko004d3152020-06-11 19:59:22 +02002139 LY_CHECK_RET(exp_check_token2(ctx, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02002140
2141 /* '/' RelativeLocationPath? */
Michal Vasko004d3152020-06-11 19:59:22 +02002142 if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002143 /* '/' */
Michal Vasko004d3152020-06-11 19:59:22 +02002144 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002145
Michal Vasko004d3152020-06-11 19:59:22 +02002146 if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002147 return LY_SUCCESS;
2148 }
Michal Vasko004d3152020-06-11 19:59:22 +02002149 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002150 case LYXP_TOKEN_DOT:
2151 case LYXP_TOKEN_DDOT:
Michal Vasko49fec8e2022-05-24 10:28:33 +02002152 case LYXP_TOKEN_AXISNAME:
Michal Vasko03ff5a72019-09-11 13:49:33 +02002153 case LYXP_TOKEN_AT:
2154 case LYXP_TOKEN_NAMETEST:
2155 case LYXP_TOKEN_NODETYPE:
aPiecekbf968d92021-05-27 14:35:05 +02002156 rc = reparse_relative_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002157 LY_CHECK_RET(rc);
Radek Krejci0f969882020-08-21 16:56:47 +02002158 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02002159 default:
2160 break;
2161 }
2162
Michal Vasko03ff5a72019-09-11 13:49:33 +02002163 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02002164 /* '//' RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002165 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002166
aPiecekbf968d92021-05-27 14:35:05 +02002167 rc = reparse_relative_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002168 LY_CHECK_RET(rc);
2169 }
2170
2171 return LY_SUCCESS;
2172}
2173
2174/**
2175 * @brief Reparse FunctionCall. Logs directly on error.
2176 *
2177 * [9] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
2178 *
2179 * @param[in] ctx Context for logging.
2180 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002181 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002182 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002183 * @return LY_ERR
2184 */
2185static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002186reparse_function_call(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t depth)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002187{
Radek Krejci1deb5be2020-08-26 16:43:36 +02002188 int8_t min_arg_count = -1;
2189 uint32_t arg_count, max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002190 uint16_t func_tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002191 LY_ERR rc;
2192
Michal Vasko004d3152020-06-11 19:59:22 +02002193 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_FUNCNAME);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002194 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002195 func_tok_idx = *tok_idx;
2196 switch (exp->tok_len[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002197 case 3:
Michal Vasko004d3152020-06-11 19:59:22 +02002198 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002199 min_arg_count = 1;
2200 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002201 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002202 min_arg_count = 1;
2203 max_arg_count = 1;
2204 }
2205 break;
2206 case 4:
Michal Vasko004d3152020-06-11 19:59:22 +02002207 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002208 min_arg_count = 1;
2209 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002210 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002211 min_arg_count = 0;
2212 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002213 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002214 min_arg_count = 0;
2215 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002216 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002217 min_arg_count = 0;
2218 max_arg_count = 0;
2219 }
2220 break;
2221 case 5:
Michal Vasko004d3152020-06-11 19:59:22 +02002222 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002223 min_arg_count = 1;
2224 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002225 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002226 min_arg_count = 0;
2227 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002228 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002229 min_arg_count = 1;
2230 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002231 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002232 min_arg_count = 1;
2233 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002234 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002235 min_arg_count = 1;
2236 max_arg_count = 1;
2237 }
2238 break;
2239 case 6:
Michal Vasko004d3152020-06-11 19:59:22 +02002240 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002241 min_arg_count = 2;
Radek Krejci1deb5be2020-08-26 16:43:36 +02002242 max_arg_count = UINT32_MAX;
Michal Vasko004d3152020-06-11 19:59:22 +02002243 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002244 min_arg_count = 0;
2245 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002246 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002247 min_arg_count = 0;
2248 max_arg_count = 1;
2249 }
2250 break;
2251 case 7:
Michal Vasko004d3152020-06-11 19:59:22 +02002252 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002253 min_arg_count = 1;
2254 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002255 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002256 min_arg_count = 1;
2257 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002258 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002259 min_arg_count = 0;
2260 max_arg_count = 0;
2261 }
2262 break;
2263 case 8:
Michal Vasko004d3152020-06-11 19:59:22 +02002264 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002265 min_arg_count = 2;
2266 max_arg_count = 2;
Michal Vasko004d3152020-06-11 19:59:22 +02002267 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002268 min_arg_count = 0;
2269 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002270 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "re-match", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002271 min_arg_count = 2;
2272 max_arg_count = 2;
2273 }
2274 break;
2275 case 9:
Michal Vasko004d3152020-06-11 19:59:22 +02002276 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002277 min_arg_count = 2;
2278 max_arg_count = 3;
Michal Vasko004d3152020-06-11 19:59:22 +02002279 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002280 min_arg_count = 3;
2281 max_arg_count = 3;
2282 }
2283 break;
2284 case 10:
Michal Vasko004d3152020-06-11 19:59:22 +02002285 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002286 min_arg_count = 0;
2287 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002288 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002289 min_arg_count = 1;
2290 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002291 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002292 min_arg_count = 2;
2293 max_arg_count = 2;
2294 }
2295 break;
2296 case 11:
Michal Vasko004d3152020-06-11 19:59:22 +02002297 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002298 min_arg_count = 2;
2299 max_arg_count = 2;
2300 }
2301 break;
2302 case 12:
Michal Vasko004d3152020-06-11 19:59:22 +02002303 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002304 min_arg_count = 2;
2305 max_arg_count = 2;
2306 }
2307 break;
2308 case 13:
Michal Vasko004d3152020-06-11 19:59:22 +02002309 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002310 min_arg_count = 0;
2311 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002312 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string-length", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002313 min_arg_count = 0;
2314 max_arg_count = 1;
2315 }
2316 break;
2317 case 15:
Michal Vasko004d3152020-06-11 19:59:22 +02002318 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002319 min_arg_count = 0;
2320 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002321 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-after", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002322 min_arg_count = 2;
2323 max_arg_count = 2;
2324 }
2325 break;
2326 case 16:
Michal Vasko004d3152020-06-11 19:59:22 +02002327 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002328 min_arg_count = 2;
2329 max_arg_count = 2;
2330 }
2331 break;
2332 case 20:
Michal Vasko004d3152020-06-11 19:59:22 +02002333 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from-or-self", 20)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002334 min_arg_count = 2;
2335 max_arg_count = 2;
2336 }
2337 break;
2338 }
2339 if (min_arg_count == -1) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002340 LOGVAL(ctx, LY_VCODE_XP_INFUNC, exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002341 return LY_EINVAL;
2342 }
Michal Vasko004d3152020-06-11 19:59:22 +02002343 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002344
2345 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02002346 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002347 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002348 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002349
2350 /* ( Expr ( ',' Expr )* )? */
2351 arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002352 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002353 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002354 if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002355 ++arg_count;
aPiecekbf968d92021-05-27 14:35:05 +02002356 rc = reparse_or_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002357 LY_CHECK_RET(rc);
2358 }
Michal Vasko004d3152020-06-11 19:59:22 +02002359 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) {
2360 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002361
2362 ++arg_count;
aPiecekbf968d92021-05-27 14:35:05 +02002363 rc = reparse_or_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002364 LY_CHECK_RET(rc);
2365 }
2366
2367 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02002368 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002369 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002370 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002371
Radek Krejci857189e2020-09-01 13:26:36 +02002372 if ((arg_count < (uint32_t)min_arg_count) || (arg_count > max_arg_count)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002373 LOGVAL(ctx, LY_VCODE_XP_INARGCOUNT, arg_count, exp->tok_len[func_tok_idx], &exp->expr[exp->tok_pos[func_tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002374 return LY_EVALID;
2375 }
2376
2377 return LY_SUCCESS;
2378}
2379
2380/**
2381 * @brief Reparse PathExpr. Logs directly on error.
2382 *
2383 * [10] PathExpr ::= LocationPath | PrimaryExpr Predicate*
2384 * | PrimaryExpr Predicate* '/' RelativeLocationPath
2385 * | PrimaryExpr Predicate* '//' RelativeLocationPath
2386 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
aPiecekfba75362021-10-07 12:39:48 +02002387 * [8] PrimaryExpr ::= VariableReference | '(' Expr ')' | Literal | Number | FunctionCall
Michal Vasko03ff5a72019-09-11 13:49:33 +02002388 *
2389 * @param[in] ctx Context for logging.
2390 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002391 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002392 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002393 * @return LY_ERR
2394 */
2395static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002396reparse_path_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t depth)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002397{
2398 LY_ERR rc;
2399
Michal Vasko004d3152020-06-11 19:59:22 +02002400 if (lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko14676352020-05-29 11:35:55 +02002401 return LY_EVALID;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002402 }
2403
Michal Vasko004d3152020-06-11 19:59:22 +02002404 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002405 case LYXP_TOKEN_PAR1:
2406 /* '(' Expr ')' Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002407 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002408
aPiecekbf968d92021-05-27 14:35:05 +02002409 rc = reparse_or_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002410 LY_CHECK_RET(rc);
2411
Michal Vasko004d3152020-06-11 19:59:22 +02002412 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002413 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002414 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002415 goto predicate;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002416 case LYXP_TOKEN_DOT:
2417 case LYXP_TOKEN_DDOT:
Michal Vasko49fec8e2022-05-24 10:28:33 +02002418 case LYXP_TOKEN_AXISNAME:
Michal Vasko03ff5a72019-09-11 13:49:33 +02002419 case LYXP_TOKEN_AT:
2420 case LYXP_TOKEN_NAMETEST:
2421 case LYXP_TOKEN_NODETYPE:
2422 /* RelativeLocationPath */
aPiecekbf968d92021-05-27 14:35:05 +02002423 rc = reparse_relative_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002424 LY_CHECK_RET(rc);
2425 break;
aPiecekfba75362021-10-07 12:39:48 +02002426 case LYXP_TOKEN_VARREF:
2427 /* VariableReference */
2428 ++(*tok_idx);
2429 goto predicate;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002430 case LYXP_TOKEN_FUNCNAME:
2431 /* FunctionCall */
aPiecekbf968d92021-05-27 14:35:05 +02002432 rc = reparse_function_call(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002433 LY_CHECK_RET(rc);
2434 goto predicate;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002435 case LYXP_TOKEN_OPER_PATH:
2436 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +02002437 /* AbsoluteLocationPath */
aPiecekbf968d92021-05-27 14:35:05 +02002438 rc = reparse_absolute_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002439 LY_CHECK_RET(rc);
2440 break;
2441 case LYXP_TOKEN_LITERAL:
2442 /* Literal */
Michal Vasko004d3152020-06-11 19:59:22 +02002443 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002444 goto predicate;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002445 case LYXP_TOKEN_NUMBER:
2446 /* Number */
Michal Vasko004d3152020-06-11 19:59:22 +02002447 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002448 goto predicate;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002449 default:
Michal Vasko49fec8e2022-05-24 10:28:33 +02002450 LOGVAL(ctx, LY_VCODE_XP_INTOK, lyxp_token2str(exp->tokens[*tok_idx]), &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002451 return LY_EVALID;
2452 }
2453
2454 return LY_SUCCESS;
2455
2456predicate:
2457 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002458 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
aPiecekbf968d92021-05-27 14:35:05 +02002459 rc = reparse_predicate(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002460 LY_CHECK_RET(rc);
2461 }
2462
2463 /* ('/' or '//') RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002464 if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002465
2466 /* '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02002467 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002468
aPiecekbf968d92021-05-27 14:35:05 +02002469 rc = reparse_relative_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002470 LY_CHECK_RET(rc);
2471 }
2472
2473 return LY_SUCCESS;
2474}
2475
2476/**
2477 * @brief Reparse UnaryExpr. Logs directly on error.
2478 *
2479 * [17] UnaryExpr ::= UnionExpr | '-' UnaryExpr
2480 * [18] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
2481 *
2482 * @param[in] ctx Context for logging.
2483 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002484 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002485 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002486 * @return LY_ERR
2487 */
2488static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002489reparse_unary_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t depth)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002490{
2491 uint16_t prev_exp;
2492 LY_ERR rc;
2493
2494 /* ('-')* */
Michal Vasko004d3152020-06-11 19:59:22 +02002495 prev_exp = *tok_idx;
Michal Vasko69730152020-10-09 16:30:07 +02002496 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2497 (exp->expr[exp->tok_pos[*tok_idx]] == '-')) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002498 exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNARY);
Michal Vasko004d3152020-06-11 19:59:22 +02002499 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002500 }
2501
2502 /* PathExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002503 prev_exp = *tok_idx;
aPiecekbf968d92021-05-27 14:35:05 +02002504 rc = reparse_path_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002505 LY_CHECK_RET(rc);
2506
2507 /* ('|' PathExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002508 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_UNI)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002509 exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNION);
Michal Vasko004d3152020-06-11 19:59:22 +02002510 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002511
aPiecekbf968d92021-05-27 14:35:05 +02002512 rc = reparse_path_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002513 LY_CHECK_RET(rc);
2514 }
2515
2516 return LY_SUCCESS;
2517}
2518
2519/**
2520 * @brief Reparse AdditiveExpr. Logs directly on error.
2521 *
2522 * [15] AdditiveExpr ::= MultiplicativeExpr
2523 * | AdditiveExpr '+' MultiplicativeExpr
2524 * | AdditiveExpr '-' MultiplicativeExpr
2525 * [16] MultiplicativeExpr ::= UnaryExpr
2526 * | MultiplicativeExpr '*' UnaryExpr
2527 * | MultiplicativeExpr 'div' UnaryExpr
2528 * | MultiplicativeExpr 'mod' UnaryExpr
2529 *
2530 * @param[in] ctx Context for logging.
2531 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002532 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002533 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002534 * @return LY_ERR
2535 */
2536static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002537reparse_additive_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t depth)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002538{
2539 uint16_t prev_add_exp, prev_mul_exp;
2540 LY_ERR rc;
2541
Michal Vasko004d3152020-06-11 19:59:22 +02002542 prev_add_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002543 goto reparse_multiplicative_expr;
2544
2545 /* ('+' / '-' MultiplicativeExpr)* */
Michal Vasko69730152020-10-09 16:30:07 +02002546 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2547 ((exp->expr[exp->tok_pos[*tok_idx]] == '+') || (exp->expr[exp->tok_pos[*tok_idx]] == '-'))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002548 exp_repeat_push(exp, prev_add_exp, LYXP_EXPR_ADDITIVE);
Michal Vasko004d3152020-06-11 19:59:22 +02002549 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002550
2551reparse_multiplicative_expr:
2552 /* UnaryExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002553 prev_mul_exp = *tok_idx;
aPiecekbf968d92021-05-27 14:35:05 +02002554 rc = reparse_unary_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002555 LY_CHECK_RET(rc);
2556
2557 /* ('*' / 'div' / 'mod' UnaryExpr)* */
Michal Vasko69730152020-10-09 16:30:07 +02002558 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2559 ((exp->expr[exp->tok_pos[*tok_idx]] == '*') || (exp->tok_len[*tok_idx] == 3))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002560 exp_repeat_push(exp, prev_mul_exp, LYXP_EXPR_MULTIPLICATIVE);
Michal Vasko004d3152020-06-11 19:59:22 +02002561 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002562
aPiecekbf968d92021-05-27 14:35:05 +02002563 rc = reparse_unary_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002564 LY_CHECK_RET(rc);
2565 }
2566 }
2567
2568 return LY_SUCCESS;
2569}
2570
2571/**
2572 * @brief Reparse EqualityExpr. Logs directly on error.
2573 *
2574 * [13] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
2575 * | EqualityExpr '!=' RelationalExpr
2576 * [14] RelationalExpr ::= AdditiveExpr
2577 * | RelationalExpr '<' AdditiveExpr
2578 * | RelationalExpr '>' AdditiveExpr
2579 * | RelationalExpr '<=' AdditiveExpr
2580 * | RelationalExpr '>=' AdditiveExpr
2581 *
2582 * @param[in] ctx Context for logging.
2583 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002584 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002585 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002586 * @return LY_ERR
2587 */
2588static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002589reparse_equality_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t depth)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002590{
2591 uint16_t prev_eq_exp, prev_rel_exp;
2592 LY_ERR rc;
2593
Michal Vasko004d3152020-06-11 19:59:22 +02002594 prev_eq_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002595 goto reparse_additive_expr;
2596
2597 /* ('=' / '!=' RelationalExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002598 while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_EQUAL, LYXP_TOKEN_OPER_NEQUAL)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002599 exp_repeat_push(exp, prev_eq_exp, LYXP_EXPR_EQUALITY);
Michal Vasko004d3152020-06-11 19:59:22 +02002600 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002601
2602reparse_additive_expr:
2603 /* AdditiveExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002604 prev_rel_exp = *tok_idx;
aPiecekbf968d92021-05-27 14:35:05 +02002605 rc = reparse_additive_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002606 LY_CHECK_RET(rc);
2607
2608 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002609 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_COMP)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002610 exp_repeat_push(exp, prev_rel_exp, LYXP_EXPR_RELATIONAL);
Michal Vasko004d3152020-06-11 19:59:22 +02002611 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002612
aPiecekbf968d92021-05-27 14:35:05 +02002613 rc = reparse_additive_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002614 LY_CHECK_RET(rc);
2615 }
2616 }
2617
2618 return LY_SUCCESS;
2619}
2620
2621/**
2622 * @brief Reparse OrExpr. Logs directly on error.
2623 *
2624 * [11] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
2625 * [12] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
2626 *
2627 * @param[in] ctx Context for logging.
2628 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002629 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002630 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002631 * @return LY_ERR
2632 */
2633static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002634reparse_or_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t depth)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002635{
2636 uint16_t prev_or_exp, prev_and_exp;
2637 LY_ERR rc;
2638
aPiecekbf968d92021-05-27 14:35:05 +02002639 ++depth;
2640 LY_CHECK_ERR_RET(depth > LYXP_MAX_BLOCK_DEPTH, LOGVAL(ctx, LY_VCODE_XP_DEPTH), LY_EINVAL);
2641
Michal Vasko004d3152020-06-11 19:59:22 +02002642 prev_or_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002643 goto reparse_equality_expr;
2644
2645 /* ('or' AndExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002646 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_LOG) && (exp->tok_len[*tok_idx] == 2)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002647 exp_repeat_push(exp, prev_or_exp, LYXP_EXPR_OR);
Michal Vasko004d3152020-06-11 19:59:22 +02002648 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002649
2650reparse_equality_expr:
2651 /* EqualityExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002652 prev_and_exp = *tok_idx;
aPiecekbf968d92021-05-27 14:35:05 +02002653 rc = reparse_equality_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002654 LY_CHECK_RET(rc);
2655
2656 /* ('and' EqualityExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002657 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_LOG) && (exp->tok_len[*tok_idx] == 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002658 exp_repeat_push(exp, prev_and_exp, LYXP_EXPR_AND);
Michal Vasko004d3152020-06-11 19:59:22 +02002659 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002660
aPiecekbf968d92021-05-27 14:35:05 +02002661 rc = reparse_equality_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002662 LY_CHECK_RET(rc);
2663 }
2664 }
2665
2666 return LY_SUCCESS;
2667}
Radek Krejcib1646a92018-11-02 16:08:26 +01002668
2669/**
2670 * @brief Parse NCName.
2671 *
2672 * @param[in] ncname Name to parse.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002673 * @return Length of @p ncname valid bytes.
Radek Krejcib1646a92018-11-02 16:08:26 +01002674 */
Michal Vasko49fec8e2022-05-24 10:28:33 +02002675static ssize_t
Radek Krejcib1646a92018-11-02 16:08:26 +01002676parse_ncname(const char *ncname)
2677{
Radek Krejci1deb5be2020-08-26 16:43:36 +02002678 uint32_t uc;
Radek Krejcid4270262019-01-07 15:07:25 +01002679 size_t size;
Michal Vasko49fec8e2022-05-24 10:28:33 +02002680 ssize_t len = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01002681
2682 LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), 0);
2683 if (!is_xmlqnamestartchar(uc) || (uc == ':')) {
2684 return len;
2685 }
2686
2687 do {
2688 len += size;
Radek Krejci9a564c92019-01-07 14:53:57 +01002689 if (!*ncname) {
2690 break;
2691 }
Radek Krejcid4270262019-01-07 15:07:25 +01002692 LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), -len);
Radek Krejcib1646a92018-11-02 16:08:26 +01002693 } while (is_xmlqnamechar(uc) && (uc != ':'));
2694
2695 return len;
2696}
2697
2698/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02002699 * @brief Add @p token into the expression @p exp.
Radek Krejcib1646a92018-11-02 16:08:26 +01002700 *
Michal Vasko03ff5a72019-09-11 13:49:33 +02002701 * @param[in] ctx Context for logging.
Radek Krejcib1646a92018-11-02 16:08:26 +01002702 * @param[in] exp Expression to use.
2703 * @param[in] token Token to add.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002704 * @param[in] tok_pos Token position in the XPath expression.
Radek Krejcib1646a92018-11-02 16:08:26 +01002705 * @param[in] tok_len Token length in the XPath expression.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002706 * @return LY_ERR
Radek Krejcib1646a92018-11-02 16:08:26 +01002707 */
2708static LY_ERR
Michal Vasko14676352020-05-29 11:35:55 +02002709exp_add_token(const struct ly_ctx *ctx, struct lyxp_expr *exp, enum lyxp_token token, uint16_t tok_pos, uint16_t tok_len)
Radek Krejcib1646a92018-11-02 16:08:26 +01002710{
2711 uint32_t prev;
2712
2713 if (exp->used == exp->size) {
2714 prev = exp->size;
2715 exp->size += LYXP_EXPR_SIZE_STEP;
2716 if (prev > exp->size) {
2717 LOGINT(ctx);
2718 return LY_EINT;
2719 }
2720
2721 exp->tokens = ly_realloc(exp->tokens, exp->size * sizeof *exp->tokens);
2722 LY_CHECK_ERR_RET(!exp->tokens, LOGMEM(ctx), LY_EMEM);
2723 exp->tok_pos = ly_realloc(exp->tok_pos, exp->size * sizeof *exp->tok_pos);
2724 LY_CHECK_ERR_RET(!exp->tok_pos, LOGMEM(ctx), LY_EMEM);
2725 exp->tok_len = ly_realloc(exp->tok_len, exp->size * sizeof *exp->tok_len);
2726 LY_CHECK_ERR_RET(!exp->tok_len, LOGMEM(ctx), LY_EMEM);
2727 }
2728
2729 exp->tokens[exp->used] = token;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002730 exp->tok_pos[exp->used] = tok_pos;
Radek Krejcib1646a92018-11-02 16:08:26 +01002731 exp->tok_len[exp->used] = tok_len;
2732 ++exp->used;
2733 return LY_SUCCESS;
2734}
2735
2736void
Michal Vasko14676352020-05-29 11:35:55 +02002737lyxp_expr_free(const struct ly_ctx *ctx, struct lyxp_expr *expr)
Radek Krejcib1646a92018-11-02 16:08:26 +01002738{
2739 uint16_t i;
2740
2741 if (!expr) {
2742 return;
2743 }
2744
2745 lydict_remove(ctx, expr->expr);
2746 free(expr->tokens);
2747 free(expr->tok_pos);
2748 free(expr->tok_len);
2749 if (expr->repeat) {
2750 for (i = 0; i < expr->used; ++i) {
2751 free(expr->repeat[i]);
2752 }
2753 }
2754 free(expr->repeat);
2755 free(expr);
2756}
2757
Michal Vasko49fec8e2022-05-24 10:28:33 +02002758/**
2759 * @brief Parse Axis name.
2760 *
2761 * @param[in] str String to parse.
2762 * @param[in] str_len Length of @p str.
2763 * @return LY_SUCCESS if an axis.
2764 * @return LY_ENOT otherwise.
2765 */
2766static LY_ERR
2767expr_parse_axis(const char *str, size_t str_len)
2768{
2769 switch (str_len) {
2770 case 4:
2771 if (!strncmp("self", str, str_len)) {
2772 return LY_SUCCESS;
2773 }
2774 break;
2775 case 5:
2776 if (!strncmp("child", str, str_len)) {
2777 return LY_SUCCESS;
2778 }
2779 break;
2780 case 6:
2781 if (!strncmp("parent", str, str_len)) {
2782 return LY_SUCCESS;
2783 }
2784 break;
2785 case 8:
2786 if (!strncmp("ancestor", str, str_len)) {
2787 return LY_SUCCESS;
2788 }
2789 break;
2790 case 9:
2791 if (!strncmp("attribute", str, str_len)) {
2792 return LY_SUCCESS;
2793 } else if (!strncmp("following", str, str_len)) {
2794 return LY_SUCCESS;
2795 } else if (!strncmp("namespace", str, str_len)) {
2796 LOGERR(NULL, LY_EINVAL, "Axis \"namespace\" not supported.");
2797 return LY_ENOT;
2798 } else if (!strncmp("preceding", str, str_len)) {
2799 return LY_SUCCESS;
2800 }
2801 break;
2802 case 10:
2803 if (!strncmp("descendant", str, str_len)) {
2804 return LY_SUCCESS;
2805 }
2806 break;
2807 case 16:
2808 if (!strncmp("ancestor-or-self", str, str_len)) {
2809 return LY_SUCCESS;
2810 }
2811 break;
2812 case 17:
2813 if (!strncmp("following-sibling", str, str_len)) {
2814 return LY_SUCCESS;
2815 } else if (!strncmp("preceding-sibling", str, str_len)) {
2816 return LY_SUCCESS;
2817 }
2818 break;
2819 case 18:
2820 if (!strncmp("descendant-or-self", str, str_len)) {
2821 return LY_SUCCESS;
2822 }
2823 break;
2824 }
2825
2826 return LY_ENOT;
2827}
2828
Radek Krejcif03a9e22020-09-18 20:09:31 +02002829LY_ERR
2830lyxp_expr_parse(const struct ly_ctx *ctx, const char *expr_str, size_t expr_len, ly_bool reparse, struct lyxp_expr **expr_p)
Radek Krejcib1646a92018-11-02 16:08:26 +01002831{
Radek Krejcif03a9e22020-09-18 20:09:31 +02002832 LY_ERR ret = LY_SUCCESS;
2833 struct lyxp_expr *expr;
Radek Krejcid4270262019-01-07 15:07:25 +01002834 size_t parsed = 0, tok_len;
Radek Krejcib1646a92018-11-02 16:08:26 +01002835 enum lyxp_token tok_type;
Michal Vasko49fec8e2022-05-24 10:28:33 +02002836 ly_bool prev_func_check = 0, prev_ntype_check = 0, has_axis;
Michal Vasko004d3152020-06-11 19:59:22 +02002837 uint16_t tok_idx = 0;
Michal Vasko49fec8e2022-05-24 10:28:33 +02002838 ssize_t ncname_len;
Radek Krejcib1646a92018-11-02 16:08:26 +01002839
Radek Krejcif03a9e22020-09-18 20:09:31 +02002840 assert(expr_p);
2841
2842 if (!expr_str[0]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002843 LOGVAL(ctx, LY_VCODE_XP_EOF);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002844 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02002845 }
2846
2847 if (!expr_len) {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002848 expr_len = strlen(expr_str);
Michal Vasko004d3152020-06-11 19:59:22 +02002849 }
2850 if (expr_len > UINT16_MAX) {
Michal Vasko623ac7b2021-04-09 12:44:23 +02002851 LOGVAL(ctx, LYVE_XPATH, "XPath expression cannot be longer than %u characters.", UINT16_MAX);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002852 return LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01002853 }
2854
2855 /* init lyxp_expr structure */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002856 expr = calloc(1, sizeof *expr);
2857 LY_CHECK_ERR_GOTO(!expr, LOGMEM(ctx); ret = LY_EMEM, error);
2858 LY_CHECK_GOTO(ret = lydict_insert(ctx, expr_str, expr_len, &expr->expr), error);
2859 expr->used = 0;
2860 expr->size = LYXP_EXPR_SIZE_START;
2861 expr->tokens = malloc(expr->size * sizeof *expr->tokens);
2862 LY_CHECK_ERR_GOTO(!expr->tokens, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002863
Radek Krejcif03a9e22020-09-18 20:09:31 +02002864 expr->tok_pos = malloc(expr->size * sizeof *expr->tok_pos);
2865 LY_CHECK_ERR_GOTO(!expr->tok_pos, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002866
Radek Krejcif03a9e22020-09-18 20:09:31 +02002867 expr->tok_len = malloc(expr->size * sizeof *expr->tok_len);
2868 LY_CHECK_ERR_GOTO(!expr->tok_len, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002869
Michal Vasko004d3152020-06-11 19:59:22 +02002870 /* make expr 0-terminated */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002871 expr_str = expr->expr;
Michal Vasko004d3152020-06-11 19:59:22 +02002872
Radek Krejcif03a9e22020-09-18 20:09:31 +02002873 while (is_xmlws(expr_str[parsed])) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002874 ++parsed;
2875 }
2876
2877 do {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002878 if (expr_str[parsed] == '(') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002879
2880 /* '(' */
2881 tok_len = 1;
2882 tok_type = LYXP_TOKEN_PAR1;
2883
Michal Vasko49fec8e2022-05-24 10:28:33 +02002884 if (prev_ntype_check && expr->used && (expr->tokens[expr->used - 1] == LYXP_TOKEN_NAMETEST) &&
2885 (((expr->tok_len[expr->used - 1] == 4) &&
2886 (!strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "node", 4) ||
2887 !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "text", 4))) ||
2888 ((expr->tok_len[expr->used - 1] == 7) &&
2889 !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "comment", 7)))) {
2890 /* it is NodeType after all */
2891 expr->tokens[expr->used - 1] = LYXP_TOKEN_NODETYPE;
2892
2893 prev_ntype_check = 0;
2894 prev_func_check = 0;
2895 } else if (prev_func_check && expr->used && (expr->tokens[expr->used - 1] == LYXP_TOKEN_NAMETEST)) {
2896 /* it is FunctionName after all */
2897 expr->tokens[expr->used - 1] = LYXP_TOKEN_FUNCNAME;
2898
2899 prev_ntype_check = 0;
2900 prev_func_check = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01002901 }
2902
Radek Krejcif03a9e22020-09-18 20:09:31 +02002903 } else if (expr_str[parsed] == ')') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002904
2905 /* ')' */
2906 tok_len = 1;
2907 tok_type = LYXP_TOKEN_PAR2;
2908
Radek Krejcif03a9e22020-09-18 20:09:31 +02002909 } else if (expr_str[parsed] == '[') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002910
2911 /* '[' */
2912 tok_len = 1;
2913 tok_type = LYXP_TOKEN_BRACK1;
2914
Radek Krejcif03a9e22020-09-18 20:09:31 +02002915 } else if (expr_str[parsed] == ']') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002916
2917 /* ']' */
2918 tok_len = 1;
2919 tok_type = LYXP_TOKEN_BRACK2;
2920
Radek Krejcif03a9e22020-09-18 20:09:31 +02002921 } else if (!strncmp(&expr_str[parsed], "..", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002922
2923 /* '..' */
2924 tok_len = 2;
2925 tok_type = LYXP_TOKEN_DDOT;
2926
Radek Krejcif03a9e22020-09-18 20:09:31 +02002927 } else if ((expr_str[parsed] == '.') && (!isdigit(expr_str[parsed + 1]))) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002928
2929 /* '.' */
2930 tok_len = 1;
2931 tok_type = LYXP_TOKEN_DOT;
2932
Radek Krejcif03a9e22020-09-18 20:09:31 +02002933 } else if (expr_str[parsed] == '@') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002934
2935 /* '@' */
2936 tok_len = 1;
2937 tok_type = LYXP_TOKEN_AT;
2938
Radek Krejcif03a9e22020-09-18 20:09:31 +02002939 } else if (expr_str[parsed] == ',') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002940
2941 /* ',' */
2942 tok_len = 1;
2943 tok_type = LYXP_TOKEN_COMMA;
2944
Radek Krejcif03a9e22020-09-18 20:09:31 +02002945 } else if (expr_str[parsed] == '\'') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002946
2947 /* Literal with ' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002948 for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\''); ++tok_len) {}
2949 LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0',
Radek Krejci2efc45b2020-12-22 16:25:44 +01002950 LOGVAL(ctx, LY_VCODE_XP_EOE, expr_str[parsed], &expr_str[parsed]); ret = LY_EVALID,
Michal Vasko69730152020-10-09 16:30:07 +02002951 error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002952 ++tok_len;
2953 tok_type = LYXP_TOKEN_LITERAL;
2954
Radek Krejcif03a9e22020-09-18 20:09:31 +02002955 } else if (expr_str[parsed] == '\"') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002956
2957 /* Literal with " */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002958 for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\"'); ++tok_len) {}
2959 LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0',
Radek Krejci2efc45b2020-12-22 16:25:44 +01002960 LOGVAL(ctx, LY_VCODE_XP_EOE, expr_str[parsed], &expr_str[parsed]); ret = LY_EVALID,
Michal Vasko69730152020-10-09 16:30:07 +02002961 error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002962 ++tok_len;
2963 tok_type = LYXP_TOKEN_LITERAL;
2964
Radek Krejcif03a9e22020-09-18 20:09:31 +02002965 } else if ((expr_str[parsed] == '.') || (isdigit(expr_str[parsed]))) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002966
2967 /* Number */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002968 for (tok_len = 0; isdigit(expr_str[parsed + tok_len]); ++tok_len) {}
2969 if (expr_str[parsed + tok_len] == '.') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002970 ++tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02002971 for ( ; isdigit(expr_str[parsed + tok_len]); ++tok_len) {}
Radek Krejcib1646a92018-11-02 16:08:26 +01002972 }
2973 tok_type = LYXP_TOKEN_NUMBER;
2974
aPiecekfba75362021-10-07 12:39:48 +02002975 } else if (expr_str[parsed] == '$') {
2976
2977 /* VariableReference */
2978 parsed++;
Michal Vasko49fec8e2022-05-24 10:28:33 +02002979 ncname_len = parse_ncname(&expr_str[parsed]);
aPiecekfba75362021-10-07 12:39:48 +02002980 LY_CHECK_ERR_GOTO(ncname_len < 1, LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed - ncname_len],
2981 parsed - ncname_len + 1, expr_str); ret = LY_EVALID, error);
2982 tok_len = ncname_len;
2983 LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == ':',
2984 LOGVAL(ctx, LYVE_XPATH, "Variable with prefix is not supported."); ret = LY_EVALID,
2985 error);
2986 tok_type = LYXP_TOKEN_VARREF;
2987
Radek Krejcif03a9e22020-09-18 20:09:31 +02002988 } else if (expr_str[parsed] == '/') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002989
2990 /* Operator '/', '//' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002991 if (!strncmp(&expr_str[parsed], "//", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002992 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002993 tok_type = LYXP_TOKEN_OPER_RPATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002994 } else {
2995 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002996 tok_type = LYXP_TOKEN_OPER_PATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002997 }
Radek Krejcib1646a92018-11-02 16:08:26 +01002998
Radek Krejcif03a9e22020-09-18 20:09:31 +02002999 } else if (!strncmp(&expr_str[parsed], "!=", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01003000
Michal Vasko3e48bf32020-06-01 08:39:07 +02003001 /* Operator '!=' */
Radek Krejcib1646a92018-11-02 16:08:26 +01003002 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003003 tok_type = LYXP_TOKEN_OPER_NEQUAL;
3004
Radek Krejcif03a9e22020-09-18 20:09:31 +02003005 } else if (!strncmp(&expr_str[parsed], "<=", 2) || !strncmp(&expr_str[parsed], ">=", 2)) {
Michal Vasko3e48bf32020-06-01 08:39:07 +02003006
3007 /* Operator '<=', '>=' */
3008 tok_len = 2;
3009 tok_type = LYXP_TOKEN_OPER_COMP;
Radek Krejcib1646a92018-11-02 16:08:26 +01003010
Radek Krejcif03a9e22020-09-18 20:09:31 +02003011 } else if (expr_str[parsed] == '|') {
Radek Krejcib1646a92018-11-02 16:08:26 +01003012
3013 /* Operator '|' */
3014 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003015 tok_type = LYXP_TOKEN_OPER_UNI;
Radek Krejcib1646a92018-11-02 16:08:26 +01003016
Radek Krejcif03a9e22020-09-18 20:09:31 +02003017 } else if ((expr_str[parsed] == '+') || (expr_str[parsed] == '-')) {
Radek Krejcib1646a92018-11-02 16:08:26 +01003018
3019 /* Operator '+', '-' */
3020 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003021 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01003022
Radek Krejcif03a9e22020-09-18 20:09:31 +02003023 } else if (expr_str[parsed] == '=') {
Radek Krejcib1646a92018-11-02 16:08:26 +01003024
Michal Vasko3e48bf32020-06-01 08:39:07 +02003025 /* Operator '=' */
Radek Krejcib1646a92018-11-02 16:08:26 +01003026 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003027 tok_type = LYXP_TOKEN_OPER_EQUAL;
3028
Radek Krejcif03a9e22020-09-18 20:09:31 +02003029 } else if ((expr_str[parsed] == '<') || (expr_str[parsed] == '>')) {
Michal Vasko3e48bf32020-06-01 08:39:07 +02003030
3031 /* Operator '<', '>' */
3032 tok_len = 1;
3033 tok_type = LYXP_TOKEN_OPER_COMP;
Radek Krejcib1646a92018-11-02 16:08:26 +01003034
Michal Vasko69730152020-10-09 16:30:07 +02003035 } else if (expr->used && (expr->tokens[expr->used - 1] != LYXP_TOKEN_AT) &&
3036 (expr->tokens[expr->used - 1] != LYXP_TOKEN_PAR1) &&
3037 (expr->tokens[expr->used - 1] != LYXP_TOKEN_BRACK1) &&
3038 (expr->tokens[expr->used - 1] != LYXP_TOKEN_COMMA) &&
3039 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_LOG) &&
3040 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_EQUAL) &&
3041 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_NEQUAL) &&
3042 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_COMP) &&
3043 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_MATH) &&
3044 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_UNI) &&
3045 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_PATH) &&
3046 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_RPATH)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01003047
3048 /* Operator '*', 'or', 'and', 'mod', or 'div' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02003049 if (expr_str[parsed] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01003050 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003051 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01003052
Radek Krejcif03a9e22020-09-18 20:09:31 +02003053 } else if (!strncmp(&expr_str[parsed], "or", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01003054 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003055 tok_type = LYXP_TOKEN_OPER_LOG;
Radek Krejcib1646a92018-11-02 16:08:26 +01003056
Radek Krejcif03a9e22020-09-18 20:09:31 +02003057 } else if (!strncmp(&expr_str[parsed], "and", 3)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01003058 tok_len = 3;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003059 tok_type = LYXP_TOKEN_OPER_LOG;
Radek Krejcib1646a92018-11-02 16:08:26 +01003060
Radek Krejcif03a9e22020-09-18 20:09:31 +02003061 } else if (!strncmp(&expr_str[parsed], "mod", 3) || !strncmp(&expr_str[parsed], "div", 3)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01003062 tok_len = 3;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003063 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01003064
Michal Vasko49fec8e2022-05-24 10:28:33 +02003065 } else if (prev_ntype_check || prev_func_check) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003066 LOGVAL(ctx, LYVE_XPATH, "Invalid character 0x%x ('%c'), perhaps \"%.*s\" is supposed to be a function call.",
Michal Vasko69730152020-10-09 16:30:07 +02003067 expr_str[parsed], expr_str[parsed], expr->tok_len[expr->used - 1], &expr->expr[expr->tok_pos[expr->used - 1]]);
Radek Krejcif03a9e22020-09-18 20:09:31 +02003068 ret = LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01003069 goto error;
3070 } else {
Michal Vasko774ce402021-04-14 15:35:06 +02003071 LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed], parsed + 1, expr_str);
Radek Krejcif03a9e22020-09-18 20:09:31 +02003072 ret = LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01003073 goto error;
3074 }
Radek Krejcib1646a92018-11-02 16:08:26 +01003075 } else {
3076
Michal Vasko49fec8e2022-05-24 10:28:33 +02003077 /* (AxisName '::')? ((NCName ':')? '*' | QName) or NodeType/FunctionName */
3078 if (expr_str[parsed] == '*') {
3079 ncname_len = 1;
3080 } else {
3081 ncname_len = parse_ncname(&expr_str[parsed]);
3082 LY_CHECK_ERR_GOTO(ncname_len < 1, LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed - ncname_len],
3083 parsed - ncname_len + 1, expr_str); ret = LY_EVALID, error);
3084 }
Radek Krejcib1646a92018-11-02 16:08:26 +01003085 tok_len = ncname_len;
3086
Michal Vasko49fec8e2022-05-24 10:28:33 +02003087 has_axis = 0;
3088 if (!strncmp(&expr_str[parsed + tok_len], "::", 2)) {
3089 /* axis */
3090 LY_CHECK_ERR_GOTO(expr_parse_axis(&expr_str[parsed], ncname_len),
3091 LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed], parsed + 1, expr_str); ret = LY_EVALID, error);
3092 tok_type = LYXP_TOKEN_AXISNAME;
3093
3094 LY_CHECK_GOTO(ret = exp_add_token(ctx, expr, tok_type, parsed, tok_len), error);
3095 parsed += tok_len;
3096
3097 /* '::' */
3098 tok_len = 2;
3099 tok_type = LYXP_TOKEN_DCOLON;
3100
3101 LY_CHECK_GOTO(ret = exp_add_token(ctx, expr, tok_type, parsed, tok_len), error);
3102 parsed += tok_len;
3103
3104 if (expr_str[parsed] == '*') {
3105 ncname_len = 1;
3106 } else {
3107 ncname_len = parse_ncname(&expr_str[parsed]);
3108 LY_CHECK_ERR_GOTO(ncname_len < 1, LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed - ncname_len],
3109 parsed - ncname_len + 1, expr_str); ret = LY_EVALID, error);
3110 }
3111 tok_len = ncname_len;
3112
3113 has_axis = 1;
3114 }
3115
Radek Krejcif03a9e22020-09-18 20:09:31 +02003116 if (expr_str[parsed + tok_len] == ':') {
Radek Krejcib1646a92018-11-02 16:08:26 +01003117 ++tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02003118 if (expr_str[parsed + tok_len] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01003119 ++tok_len;
3120 } else {
Radek Krejcif03a9e22020-09-18 20:09:31 +02003121 ncname_len = parse_ncname(&expr_str[parsed + tok_len]);
Michal Vaskoe2be5462021-08-04 10:49:42 +02003122 LY_CHECK_ERR_GOTO(ncname_len < 1, LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed - ncname_len],
Michal Vasko774ce402021-04-14 15:35:06 +02003123 parsed - ncname_len + 1, expr_str); ret = LY_EVALID, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01003124 tok_len += ncname_len;
3125 }
Michal Vasko49fec8e2022-05-24 10:28:33 +02003126 /* remove old flags to prevent ambiguities */
3127 prev_ntype_check = 0;
3128 prev_func_check = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01003129 tok_type = LYXP_TOKEN_NAMETEST;
3130 } else {
Michal Vasko49fec8e2022-05-24 10:28:33 +02003131 /* if not '*', there is no prefix so it can still be NodeType/FunctionName, we can't finally decide now */
3132 prev_ntype_check = (expr_str[parsed] == '*') ? 0 : 1;
3133 prev_func_check = (prev_ntype_check && !has_axis) ? 1 : 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01003134 tok_type = LYXP_TOKEN_NAMETEST;
3135 }
3136 }
3137
3138 /* store the token, move on to the next one */
Radek Krejcif03a9e22020-09-18 20:09:31 +02003139 LY_CHECK_GOTO(ret = exp_add_token(ctx, expr, tok_type, parsed, tok_len), error);
Radek Krejcib1646a92018-11-02 16:08:26 +01003140 parsed += tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02003141 while (is_xmlws(expr_str[parsed])) {
Radek Krejcib1646a92018-11-02 16:08:26 +01003142 ++parsed;
3143 }
3144
Radek Krejcif03a9e22020-09-18 20:09:31 +02003145 } while (expr_str[parsed]);
Radek Krejcib1646a92018-11-02 16:08:26 +01003146
Michal Vasko004d3152020-06-11 19:59:22 +02003147 if (reparse) {
3148 /* prealloc repeat */
Radek Krejcif03a9e22020-09-18 20:09:31 +02003149 expr->repeat = calloc(expr->size, sizeof *expr->repeat);
3150 LY_CHECK_ERR_GOTO(!expr->repeat, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01003151
Michal Vasko004d3152020-06-11 19:59:22 +02003152 /* fill repeat */
aPiecekbf968d92021-05-27 14:35:05 +02003153 LY_CHECK_ERR_GOTO(reparse_or_expr(ctx, expr, &tok_idx, 0), ret = LY_EVALID, error);
Radek Krejcif03a9e22020-09-18 20:09:31 +02003154 if (expr->used > tok_idx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003155 LOGVAL(ctx, LYVE_XPATH, "Unparsed characters \"%s\" left at the end of an XPath expression.",
Michal Vasko69730152020-10-09 16:30:07 +02003156 &expr->expr[expr->tok_pos[tok_idx]]);
Radek Krejcif03a9e22020-09-18 20:09:31 +02003157 ret = LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02003158 goto error;
3159 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003160 }
3161
Radek Krejcif03a9e22020-09-18 20:09:31 +02003162 print_expr_struct_debug(expr);
3163 *expr_p = expr;
3164 return LY_SUCCESS;
Radek Krejcib1646a92018-11-02 16:08:26 +01003165
3166error:
Radek Krejcif03a9e22020-09-18 20:09:31 +02003167 lyxp_expr_free(ctx, expr);
3168 return ret;
Radek Krejcib1646a92018-11-02 16:08:26 +01003169}
3170
Michal Vasko1734be92020-09-22 08:55:10 +02003171LY_ERR
3172lyxp_expr_dup(const struct ly_ctx *ctx, const struct lyxp_expr *exp, struct lyxp_expr **dup_p)
Michal Vasko004d3152020-06-11 19:59:22 +02003173{
Michal Vasko1734be92020-09-22 08:55:10 +02003174 LY_ERR ret = LY_SUCCESS;
3175 struct lyxp_expr *dup = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02003176 uint32_t i, j;
3177
Michal Vasko7f45cf22020-10-01 12:49:44 +02003178 if (!exp) {
3179 goto cleanup;
3180 }
3181
Michal Vasko004d3152020-06-11 19:59:22 +02003182 dup = calloc(1, sizeof *dup);
Michal Vasko1734be92020-09-22 08:55:10 +02003183 LY_CHECK_ERR_GOTO(!dup, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02003184
Michal Vasko08e9b112021-06-11 15:41:17 +02003185 if (exp->used) {
3186 dup->tokens = malloc(exp->used * sizeof *dup->tokens);
3187 LY_CHECK_ERR_GOTO(!dup->tokens, LOGMEM(ctx); ret = LY_EMEM, cleanup);
3188 memcpy(dup->tokens, exp->tokens, exp->used * sizeof *dup->tokens);
Michal Vasko004d3152020-06-11 19:59:22 +02003189
Michal Vasko08e9b112021-06-11 15:41:17 +02003190 dup->tok_pos = malloc(exp->used * sizeof *dup->tok_pos);
3191 LY_CHECK_ERR_GOTO(!dup->tok_pos, LOGMEM(ctx); ret = LY_EMEM, cleanup);
3192 memcpy(dup->tok_pos, exp->tok_pos, exp->used * sizeof *dup->tok_pos);
Michal Vasko004d3152020-06-11 19:59:22 +02003193
Michal Vasko08e9b112021-06-11 15:41:17 +02003194 dup->tok_len = malloc(exp->used * sizeof *dup->tok_len);
3195 LY_CHECK_ERR_GOTO(!dup->tok_len, LOGMEM(ctx); ret = LY_EMEM, cleanup);
3196 memcpy(dup->tok_len, exp->tok_len, exp->used * sizeof *dup->tok_len);
Michal Vasko004d3152020-06-11 19:59:22 +02003197
Michal Vasko79a7a872022-06-17 09:00:48 +02003198 if (exp->repeat) {
3199 dup->repeat = malloc(exp->used * sizeof *dup->repeat);
3200 LY_CHECK_ERR_GOTO(!dup->repeat, LOGMEM(ctx); ret = LY_EMEM, cleanup);
3201 for (i = 0; i < exp->used; ++i) {
3202 if (!exp->repeat[i]) {
3203 dup->repeat[i] = NULL;
3204 } else {
3205 for (j = 0; exp->repeat[i][j]; ++j) {}
3206 /* the ending 0 as well */
3207 ++j;
Michal Vasko004d3152020-06-11 19:59:22 +02003208
Michal Vasko79a7a872022-06-17 09:00:48 +02003209 dup->repeat[i] = malloc(j * sizeof **dup->repeat);
3210 LY_CHECK_ERR_GOTO(!dup->repeat[i], LOGMEM(ctx); ret = LY_EMEM, cleanup);
3211 memcpy(dup->repeat[i], exp->repeat[i], j * sizeof **dup->repeat);
3212 dup->repeat[i][j - 1] = 0;
3213 }
Michal Vasko08e9b112021-06-11 15:41:17 +02003214 }
Michal Vasko004d3152020-06-11 19:59:22 +02003215 }
3216 }
3217
3218 dup->used = exp->used;
3219 dup->size = exp->used;
Michal Vasko1734be92020-09-22 08:55:10 +02003220 LY_CHECK_GOTO(ret = lydict_insert(ctx, exp->expr, 0, &dup->expr), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02003221
Michal Vasko1734be92020-09-22 08:55:10 +02003222cleanup:
3223 if (ret) {
3224 lyxp_expr_free(ctx, dup);
3225 } else {
3226 *dup_p = dup;
3227 }
3228 return ret;
Michal Vasko004d3152020-06-11 19:59:22 +02003229}
3230
Michal Vasko03ff5a72019-09-11 13:49:33 +02003231/**
3232 * @brief Get the last-added schema node that is currently in the context.
3233 *
3234 * @param[in] set Set to search in.
3235 * @return Last-added schema context node, NULL if no node is in context.
3236 */
3237static struct lysc_node *
3238warn_get_scnode_in_ctx(struct lyxp_set *set)
3239{
3240 uint32_t i;
3241
3242 if (!set || (set->type != LYXP_SET_SCNODE_SET)) {
3243 return NULL;
3244 }
3245
3246 i = set->used;
3247 do {
3248 --i;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003249 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003250 /* if there are more, simply return the first found (last added) */
3251 return set->val.scnodes[i].scnode;
3252 }
3253 } while (i);
3254
3255 return NULL;
3256}
3257
3258/**
3259 * @brief Test whether a type is numeric - integer type or decimal64.
3260 *
3261 * @param[in] type Type to test.
Radek Krejci857189e2020-09-01 13:26:36 +02003262 * @return Boolean value whether @p type is numeric type or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003263 */
Radek Krejci857189e2020-09-01 13:26:36 +02003264static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003265warn_is_numeric_type(struct lysc_type *type)
3266{
3267 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003268 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003269 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003270
3271 switch (type->basetype) {
3272 case LY_TYPE_DEC64:
3273 case LY_TYPE_INT8:
3274 case LY_TYPE_UINT8:
3275 case LY_TYPE_INT16:
3276 case LY_TYPE_UINT16:
3277 case LY_TYPE_INT32:
3278 case LY_TYPE_UINT32:
3279 case LY_TYPE_INT64:
3280 case LY_TYPE_UINT64:
3281 return 1;
3282 case LY_TYPE_UNION:
3283 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003284 LY_ARRAY_FOR(uni->types, u) {
3285 ret = warn_is_numeric_type(uni->types[u]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003286 if (ret) {
3287 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003288 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003289 }
3290 }
3291 /* did not find any suitable type */
3292 return 0;
3293 case LY_TYPE_LEAFREF:
3294 return warn_is_numeric_type(((struct lysc_type_leafref *)type)->realtype);
3295 default:
3296 return 0;
3297 }
3298}
3299
3300/**
3301 * @brief Test whether a type is string-like - no integers, decimal64 or binary.
3302 *
3303 * @param[in] type Type to test.
Radek Krejci857189e2020-09-01 13:26:36 +02003304 * @return Boolean value whether @p type's basetype is string type or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003305 */
Radek Krejci857189e2020-09-01 13:26:36 +02003306static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003307warn_is_string_type(struct lysc_type *type)
3308{
3309 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003310 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003311 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003312
3313 switch (type->basetype) {
3314 case LY_TYPE_BITS:
3315 case LY_TYPE_ENUM:
3316 case LY_TYPE_IDENT:
3317 case LY_TYPE_INST:
3318 case LY_TYPE_STRING:
3319 return 1;
3320 case LY_TYPE_UNION:
3321 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003322 LY_ARRAY_FOR(uni->types, u) {
3323 ret = warn_is_string_type(uni->types[u]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003324 if (ret) {
3325 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003326 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003327 }
3328 }
3329 /* did not find any suitable type */
3330 return 0;
3331 case LY_TYPE_LEAFREF:
3332 return warn_is_string_type(((struct lysc_type_leafref *)type)->realtype);
3333 default:
3334 return 0;
3335 }
3336}
3337
3338/**
3339 * @brief Test whether a type is one specific type.
3340 *
3341 * @param[in] type Type to test.
3342 * @param[in] base Expected type.
Radek Krejci857189e2020-09-01 13:26:36 +02003343 * @return Boolean value whether the given @p type is of the specific basetype @p base.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003344 */
Radek Krejci857189e2020-09-01 13:26:36 +02003345static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003346warn_is_specific_type(struct lysc_type *type, LY_DATA_TYPE base)
3347{
3348 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003349 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003350 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003351
3352 if (type->basetype == base) {
3353 return 1;
3354 } else if (type->basetype == LY_TYPE_UNION) {
3355 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003356 LY_ARRAY_FOR(uni->types, u) {
3357 ret = warn_is_specific_type(uni->types[u], base);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003358 if (ret) {
3359 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003360 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003361 }
3362 }
3363 /* did not find any suitable type */
3364 return 0;
3365 } else if (type->basetype == LY_TYPE_LEAFREF) {
3366 return warn_is_specific_type(((struct lysc_type_leafref *)type)->realtype, base);
3367 }
3368
3369 return 0;
3370}
3371
3372/**
3373 * @brief Get next type of a (union) type.
3374 *
3375 * @param[in] type Base type.
3376 * @param[in] prev_type Previously returned type.
3377 * @return Next type or NULL.
3378 */
3379static struct lysc_type *
3380warn_is_equal_type_next_type(struct lysc_type *type, struct lysc_type *prev_type)
3381{
3382 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003383 ly_bool found = 0;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003384 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003385
3386 switch (type->basetype) {
3387 case LY_TYPE_UNION:
3388 uni = (struct lysc_type_union *)type;
3389 if (!prev_type) {
3390 return uni->types[0];
3391 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003392 LY_ARRAY_FOR(uni->types, u) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003393 if (found) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003394 return uni->types[u];
Michal Vasko03ff5a72019-09-11 13:49:33 +02003395 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003396 if (prev_type == uni->types[u]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003397 found = 1;
3398 }
3399 }
3400 return NULL;
3401 default:
3402 if (prev_type) {
3403 assert(type == prev_type);
3404 return NULL;
3405 } else {
3406 return type;
3407 }
3408 }
3409}
3410
3411/**
3412 * @brief Test whether 2 types have a common type.
3413 *
3414 * @param[in] type1 First type.
3415 * @param[in] type2 Second type.
3416 * @return 1 if they do, 0 otherwise.
3417 */
3418static int
3419warn_is_equal_type(struct lysc_type *type1, struct lysc_type *type2)
3420{
3421 struct lysc_type *t1, *rt1, *t2, *rt2;
3422
3423 t1 = NULL;
3424 while ((t1 = warn_is_equal_type_next_type(type1, t1))) {
3425 if (t1->basetype == LY_TYPE_LEAFREF) {
3426 rt1 = ((struct lysc_type_leafref *)t1)->realtype;
3427 } else {
3428 rt1 = t1;
3429 }
3430
3431 t2 = NULL;
3432 while ((t2 = warn_is_equal_type_next_type(type2, t2))) {
3433 if (t2->basetype == LY_TYPE_LEAFREF) {
3434 rt2 = ((struct lysc_type_leafref *)t2)->realtype;
3435 } else {
3436 rt2 = t2;
3437 }
3438
3439 if (rt2->basetype == rt1->basetype) {
3440 /* match found */
3441 return 1;
3442 }
3443 }
3444 }
3445
3446 return 0;
3447}
3448
3449/**
Michal Vaskoaa956522021-11-11 10:45:34 +01003450 * @brief Print warning with information about the XPath subexpression that caused previous warning.
3451 *
3452 * @param[in] ctx Context for logging.
3453 * @param[in] tok_pos Index of the subexpression in the whole expression.
3454 * @param[in] subexpr Subexpression start.
3455 * @param[in] subexpr_len Length of @p subexpr to print.
3456 * @param[in] cur_scnode Expression context node.
3457 */
3458static void
3459warn_subexpr_log(const struct ly_ctx *ctx, uint16_t tok_pos, const char *subexpr, int subexpr_len,
3460 const struct lysc_node *cur_scnode)
3461{
3462 char *path;
3463
3464 path = lysc_path(cur_scnode, LYSC_PATH_LOG, NULL, 0);
3465 LOGWRN(ctx, "Previous warning generated by XPath subexpression[%" PRIu16 "] \"%.*s\" with context node \"%s\".",
3466 tok_pos, subexpr_len, subexpr, path);
3467 free(path);
3468}
3469
3470/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02003471 * @brief Check both operands of comparison operators.
3472 *
3473 * @param[in] ctx Context for errors.
3474 * @param[in] set1 First operand set.
3475 * @param[in] set2 Second operand set.
3476 * @param[in] numbers_only Whether accept only numbers or other types are fine too (for '=' and '!=').
3477 * @param[in] expr Start of the expression to print with the warning.
3478 * @param[in] tok_pos Token position.
3479 */
3480static void
Michal Vaskoaa956522021-11-11 10:45:34 +01003481warn_operands(struct ly_ctx *ctx, struct lyxp_set *set1, struct lyxp_set *set2, ly_bool numbers_only, const char *expr,
3482 uint16_t tok_pos)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003483{
3484 struct lysc_node_leaf *node1, *node2;
Radek Krejci857189e2020-09-01 13:26:36 +02003485 ly_bool leaves = 1, warning = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003486
3487 node1 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set1);
3488 node2 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set2);
3489
3490 if (!node1 && !node2) {
3491 /* no node-sets involved, nothing to do */
3492 return;
3493 }
3494
3495 if (node1) {
3496 if (!(node1->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3497 LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node1->nodetype), node1->name);
3498 warning = 1;
3499 leaves = 0;
3500 } else if (numbers_only && !warn_is_numeric_type(node1->type)) {
3501 LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node1->name);
3502 warning = 1;
3503 }
3504 }
3505
3506 if (node2) {
3507 if (!(node2->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3508 LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node2->nodetype), node2->name);
3509 warning = 1;
3510 leaves = 0;
3511 } else if (numbers_only && !warn_is_numeric_type(node2->type)) {
3512 LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node2->name);
3513 warning = 1;
3514 }
3515 }
3516
3517 if (node1 && node2 && leaves && !numbers_only) {
Michal Vasko69730152020-10-09 16:30:07 +02003518 if ((warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type)) ||
3519 (!warn_is_numeric_type(node1->type) && warn_is_numeric_type(node2->type)) ||
3520 (!warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type) &&
3521 !warn_is_equal_type(node1->type, node2->type))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003522 LOGWRN(ctx, "Incompatible types of operands \"%s\" and \"%s\" for comparison.", node1->name, node2->name);
3523 warning = 1;
3524 }
3525 }
3526
3527 if (warning) {
Michal Vaskoaa956522021-11-11 10:45:34 +01003528 warn_subexpr_log(ctx, tok_pos, expr + tok_pos, 20, set1->cur_scnode);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003529 }
3530}
3531
3532/**
3533 * @brief Check that a value is valid for a leaf. If not applicable, does nothing.
3534 *
3535 * @param[in] exp Parsed XPath expression.
3536 * @param[in] set Set with the leaf/leaf-list.
3537 * @param[in] val_exp Index of the value (literal/number) in @p exp.
3538 * @param[in] equal_exp Index of the start of the equality expression in @p exp.
3539 * @param[in] last_equal_exp Index of the end of the equality expression in @p exp.
3540 */
3541static void
Michal Vasko40308e72020-10-20 16:38:40 +02003542warn_equality_value(const struct lyxp_expr *exp, struct lyxp_set *set, uint16_t val_exp, uint16_t equal_exp,
3543 uint16_t last_equal_exp)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003544{
3545 struct lysc_node *scnode;
3546 struct lysc_type *type;
3547 char *value;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003548 struct lyd_value storage;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003549 LY_ERR rc;
3550 struct ly_err_item *err = NULL;
3551
Michal Vasko69730152020-10-09 16:30:07 +02003552 if ((scnode = warn_get_scnode_in_ctx(set)) && (scnode->nodetype & (LYS_LEAF | LYS_LEAFLIST)) &&
3553 ((exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) || (exp->tokens[val_exp] == LYXP_TOKEN_NUMBER))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003554 /* check that the node can have the specified value */
3555 if (exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) {
3556 value = strndup(exp->expr + exp->tok_pos[val_exp] + 1, exp->tok_len[val_exp] - 2);
3557 } else {
3558 value = strndup(exp->expr + exp->tok_pos[val_exp], exp->tok_len[val_exp]);
3559 }
3560 if (!value) {
3561 LOGMEM(set->ctx);
3562 return;
3563 }
3564
3565 if ((((struct lysc_node_leaf *)scnode)->type->basetype == LY_TYPE_IDENT) && !strchr(value, ':')) {
3566 LOGWRN(set->ctx, "Identityref \"%s\" comparison with identity \"%s\" without prefix, consider adding"
Michal Vasko69730152020-10-09 16:30:07 +02003567 " a prefix or best using \"derived-from(-or-self)()\" functions.", scnode->name, value);
Michal Vaskoaa956522021-11-11 10:45:34 +01003568 warn_subexpr_log(set->ctx, exp->tok_pos[equal_exp], exp->expr + exp->tok_pos[equal_exp],
Radek Krejci0f969882020-08-21 16:56:47 +02003569 (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp],
Michal Vaskoaa956522021-11-11 10:45:34 +01003570 set->cur_scnode);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003571 }
3572
3573 type = ((struct lysc_node_leaf *)scnode)->type;
3574 if (type->basetype != LY_TYPE_IDENT) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003575 rc = type->plugin->store(set->ctx, type, value, strlen(value), 0, set->format, set->prefix_data,
Michal Vasko405cc9e2020-12-01 12:01:27 +01003576 LYD_HINT_DATA, scnode, &storage, NULL, &err);
Michal Vaskobf42e832020-11-23 16:59:42 +01003577 if (rc == LY_EINCOMPLETE) {
3578 rc = LY_SUCCESS;
3579 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003580
3581 if (err) {
3582 LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type (%s).", value, err->msg);
3583 ly_err_free(err);
3584 } else if (rc != LY_SUCCESS) {
3585 LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type.", value);
3586 }
3587 if (rc != LY_SUCCESS) {
Michal Vaskoaa956522021-11-11 10:45:34 +01003588 warn_subexpr_log(set->ctx, exp->tok_pos[equal_exp], exp->expr + exp->tok_pos[equal_exp],
Radek Krejci0f969882020-08-21 16:56:47 +02003589 (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp],
Michal Vaskoaa956522021-11-11 10:45:34 +01003590 set->cur_scnode);
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003591 } else {
3592 type->plugin->free(set->ctx, &storage);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003593 }
3594 }
3595 free(value);
3596 }
3597}
3598
3599/*
3600 * XPath functions
3601 */
3602
3603/**
3604 * @brief Execute the YANG 1.1 bit-is-set(node-set, string) function. Returns LYXP_SET_BOOLEAN
3605 * depending on whether the first node bit value from the second argument is set.
3606 *
3607 * @param[in] args Array of arguments.
3608 * @param[in] arg_count Count of elements in @p args.
3609 * @param[in,out] set Context and result set at the same time.
3610 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003611 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003612 */
3613static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003614xpath_bit_is_set(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003615{
3616 struct lyd_node_term *leaf;
3617 struct lysc_node_leaf *sleaf;
Michal Vasko2588b952021-07-29 07:43:26 +02003618 struct lyd_value_bits *bits;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003619 LY_ERR rc = LY_SUCCESS;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003620 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003621
3622 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003623 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003624 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003625 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3626 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3627 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3628 sleaf->name);
3629 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_BITS)) {
3630 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"bits\".", __func__, sleaf->name);
3631 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003632 }
3633
3634 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3635 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003636 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3637 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003638 } else if (!warn_is_string_type(sleaf->type)) {
3639 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003640 }
3641 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003642 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003643 return rc;
3644 }
3645
Michal Vaskod3678892020-05-21 10:06:58 +02003646 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003647 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "bit-is-set(node-set, string)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003648 return LY_EVALID;
3649 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003650 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003651 LY_CHECK_RET(rc);
3652
3653 set_fill_boolean(set, 0);
Michal Vaskod3678892020-05-21 10:06:58 +02003654 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003655 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
Michal Vasko2588b952021-07-29 07:43:26 +02003656 if ((leaf->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (leaf->value.realtype->basetype == LY_TYPE_BITS)) {
3657 LYD_VALUE_GET(&leaf->value, bits);
3658 LY_ARRAY_FOR(bits->items, u) {
3659 if (!strcmp(bits->items[u]->name, args[1]->val.str)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003660 set_fill_boolean(set, 1);
3661 break;
3662 }
3663 }
3664 }
3665 }
3666
3667 return LY_SUCCESS;
3668}
3669
3670/**
3671 * @brief Execute the XPath boolean(object) function. Returns LYXP_SET_BOOLEAN
3672 * with the argument converted to boolean.
3673 *
3674 * @param[in] args Array of arguments.
3675 * @param[in] arg_count Count of elements in @p args.
3676 * @param[in,out] set Context and result set at the same time.
3677 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003678 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003679 */
3680static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003681xpath_boolean(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003682{
3683 LY_ERR rc;
3684
3685 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02003686 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003687 return LY_SUCCESS;
3688 }
3689
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003690 rc = lyxp_set_cast(args[0], LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003691 LY_CHECK_RET(rc);
3692 set_fill_set(set, args[0]);
3693
3694 return LY_SUCCESS;
3695}
3696
3697/**
3698 * @brief Execute the XPath ceiling(number) function. Returns LYXP_SET_NUMBER
3699 * with the first argument rounded up to the nearest integer.
3700 *
3701 * @param[in] args Array of arguments.
3702 * @param[in] arg_count Count of elements in @p args.
3703 * @param[in,out] set Context and result set at the same time.
3704 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003705 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003706 */
3707static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003708xpath_ceiling(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003709{
3710 struct lysc_node_leaf *sleaf;
3711 LY_ERR rc = LY_SUCCESS;
3712
3713 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003714 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003715 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003716 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3717 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3718 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3719 sleaf->name);
3720 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
3721 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
3722 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003723 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003724 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003725 return rc;
3726 }
3727
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003728 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003729 LY_CHECK_RET(rc);
3730 if ((long long)args[0]->val.num != args[0]->val.num) {
3731 set_fill_number(set, ((long long)args[0]->val.num) + 1);
3732 } else {
3733 set_fill_number(set, args[0]->val.num);
3734 }
3735
3736 return LY_SUCCESS;
3737}
3738
3739/**
3740 * @brief Execute the XPath concat(string, string, string*) function.
3741 * Returns LYXP_SET_STRING with the concatenation of all the arguments.
3742 *
3743 * @param[in] args Array of arguments.
3744 * @param[in] arg_count Count of elements in @p args.
3745 * @param[in,out] set Context and result set at the same time.
3746 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003747 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003748 */
3749static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003750xpath_concat(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003751{
3752 uint16_t i;
3753 char *str = NULL;
3754 size_t used = 1;
3755 LY_ERR rc = LY_SUCCESS;
3756 struct lysc_node_leaf *sleaf;
3757
3758 if (options & LYXP_SCNODE_ALL) {
3759 for (i = 0; i < arg_count; ++i) {
3760 if ((args[i]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[i]))) {
3761 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3762 LOGWRN(set->ctx, "Argument #%u of %s is a %s node \"%s\".",
Michal Vasko69730152020-10-09 16:30:07 +02003763 i + 1, __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003764 } else if (!warn_is_string_type(sleaf->type)) {
Radek Krejci70124c82020-08-14 22:17:03 +02003765 LOGWRN(set->ctx, "Argument #%u of %s is node \"%s\", not of string-type.", i + 1, __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003766 }
3767 }
3768 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003769 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003770 return rc;
3771 }
3772
3773 for (i = 0; i < arg_count; ++i) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003774 rc = lyxp_set_cast(args[i], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003775 if (rc != LY_SUCCESS) {
3776 free(str);
3777 return rc;
3778 }
3779
3780 str = ly_realloc(str, (used + strlen(args[i]->val.str)) * sizeof(char));
3781 LY_CHECK_ERR_RET(!str, LOGMEM(set->ctx), LY_EMEM);
3782 strcpy(str + used - 1, args[i]->val.str);
3783 used += strlen(args[i]->val.str);
3784 }
3785
3786 /* free, kind of */
Michal Vaskod3678892020-05-21 10:06:58 +02003787 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003788 set->type = LYXP_SET_STRING;
3789 set->val.str = str;
3790
3791 return LY_SUCCESS;
3792}
3793
3794/**
3795 * @brief Execute the XPath contains(string, string) function.
3796 * Returns LYXP_SET_BOOLEAN whether the second argument can
3797 * be found in the first or not.
3798 *
3799 * @param[in] args Array of arguments.
3800 * @param[in] arg_count Count of elements in @p args.
3801 * @param[in,out] set Context and result set at the same time.
3802 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003803 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003804 */
3805static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003806xpath_contains(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003807{
3808 struct lysc_node_leaf *sleaf;
3809 LY_ERR rc = LY_SUCCESS;
3810
3811 if (options & LYXP_SCNODE_ALL) {
3812 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3813 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003814 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3815 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003816 } else if (!warn_is_string_type(sleaf->type)) {
3817 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003818 }
3819 }
3820
3821 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3822 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003823 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3824 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003825 } else if (!warn_is_string_type(sleaf->type)) {
3826 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003827 }
3828 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003829 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003830 return rc;
3831 }
3832
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003833 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003834 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003835 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003836 LY_CHECK_RET(rc);
3837
3838 if (strstr(args[0]->val.str, args[1]->val.str)) {
3839 set_fill_boolean(set, 1);
3840 } else {
3841 set_fill_boolean(set, 0);
3842 }
3843
3844 return LY_SUCCESS;
3845}
3846
3847/**
3848 * @brief Execute the XPath count(node-set) function. Returns LYXP_SET_NUMBER
3849 * with the size of the node-set from the argument.
3850 *
3851 * @param[in] args Array of arguments.
3852 * @param[in] arg_count Count of elements in @p args.
3853 * @param[in,out] set Context and result set at the same time.
3854 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003855 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003856 */
3857static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003858xpath_count(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003859{
Michal Vasko03ff5a72019-09-11 13:49:33 +02003860 LY_ERR rc = LY_SUCCESS;
3861
3862 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003863 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003864 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003865 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003866 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003867 return rc;
3868 }
3869
Michal Vasko03ff5a72019-09-11 13:49:33 +02003870 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003871 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "count(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003872 return LY_EVALID;
3873 }
3874
3875 set_fill_number(set, args[0]->used);
3876 return LY_SUCCESS;
3877}
3878
3879/**
3880 * @brief Execute the XPath current() function. Returns LYXP_SET_NODE_SET
3881 * with the context with the intial node.
3882 *
3883 * @param[in] args Array of arguments.
3884 * @param[in] arg_count Count of elements in @p args.
3885 * @param[in,out] set Context and result set at the same time.
3886 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003887 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003888 */
3889static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003890xpath_current(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003891{
3892 if (arg_count || args) {
Radek Krejcie87c7dc2021-06-02 21:25:42 +02003893 LOGVAL(set->ctx, LY_VCODE_XP_INARGCOUNT, arg_count, LY_PRI_LENSTR("current()"));
Michal Vasko03ff5a72019-09-11 13:49:33 +02003894 return LY_EVALID;
3895 }
3896
3897 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02003898 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003899
Michal Vasko296dfaf2021-12-13 16:57:42 +01003900 if (set->cur_scnode) {
3901 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, set->cur_scnode, LYXP_NODE_ELEM, NULL));
3902 } else {
3903 /* root node */
3904 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, NULL, set->root_type, NULL));
3905 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003906 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02003907 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003908
Michal Vasko296dfaf2021-12-13 16:57:42 +01003909 if (set->cur_node) {
3910 /* position is filled later */
3911 set_insert_node(set, set->cur_node, 0, LYXP_NODE_ELEM, 0);
3912 } else {
3913 /* root node */
3914 set_insert_node(set, NULL, 0, set->root_type, 0);
3915 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003916 }
3917
3918 return LY_SUCCESS;
3919}
3920
3921/**
3922 * @brief Execute the YANG 1.1 deref(node-set) function. Returns LYXP_SET_NODE_SET with either
3923 * leafref or instance-identifier target node(s).
3924 *
3925 * @param[in] args Array of arguments.
3926 * @param[in] arg_count Count of elements in @p args.
3927 * @param[in,out] set Context and result set at the same time.
3928 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003929 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003930 */
3931static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003932xpath_deref(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003933{
3934 struct lyd_node_term *leaf;
Michal Vasko42e497c2020-01-06 08:38:25 +01003935 struct lysc_node_leaf *sleaf = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02003936 struct lysc_type_leafref *lref;
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003937 const struct lysc_node *target;
Michal Vasko004d3152020-06-11 19:59:22 +02003938 struct ly_path *p;
3939 struct lyd_node *node;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003940 char *errmsg = NULL;
Michal Vasko00cbf532020-06-15 13:58:47 +02003941 uint8_t oper;
Michal Vasko741bb562021-06-24 11:59:50 +02003942 LY_ERR r;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003943
3944 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003945 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003946 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003947 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
Michal Vasko423ba3f2021-07-19 13:08:50 +02003948 if (!(sleaf->nodetype & LYD_NODE_TERM)) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003949 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3950 sleaf->name);
Michal Vaskoed725d72021-06-23 12:03:45 +02003951 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_LEAFREF) &&
3952 !warn_is_specific_type(sleaf->type, LY_TYPE_INST)) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003953 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"leafref\" nor \"instance-identifier\".",
3954 __func__, sleaf->name);
3955 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003956 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003957 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko423ba3f2021-07-19 13:08:50 +02003958 if (sleaf && (sleaf->nodetype & LYD_NODE_TERM) && (sleaf->type->basetype == LY_TYPE_LEAFREF)) {
Michal Vasko004d3152020-06-11 19:59:22 +02003959 lref = (struct lysc_type_leafref *)sleaf->type;
Michal Vaskod1e53b92021-01-28 13:11:06 +01003960 oper = (sleaf->flags & LYS_IS_OUTPUT) ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT;
Michal Vasko004d3152020-06-11 19:59:22 +02003961
3962 /* it was already evaluated on schema, it must succeed */
Michal Vasko741bb562021-06-24 11:59:50 +02003963 r = ly_path_compile_leafref(set->ctx, &sleaf->node, NULL, lref->path, oper, LY_PATH_TARGET_MANY,
Michal Vasko24fc4d12021-07-12 14:41:20 +02003964 LY_VALUE_SCHEMA_RESOLVED, lref->prefixes, &p);
Michal Vasko741bb562021-06-24 11:59:50 +02003965 if (!r) {
3966 /* get the target node */
3967 target = p[LY_ARRAY_COUNT(p) - 1].node;
3968 ly_path_free(set->ctx, p);
Michal Vasko004d3152020-06-11 19:59:22 +02003969
Michal Vasko741bb562021-06-24 11:59:50 +02003970 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, target, LYXP_NODE_ELEM, NULL));
3971 } /* else the target was found before but is disabled so it was removed */
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003972 }
3973
Michal Vasko741bb562021-06-24 11:59:50 +02003974 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003975 }
3976
Michal Vaskod3678892020-05-21 10:06:58 +02003977 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003978 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "deref(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003979 return LY_EVALID;
3980 }
3981
Michal Vaskod3678892020-05-21 10:06:58 +02003982 lyxp_set_free_content(set);
3983 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003984 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
3985 sleaf = (struct lysc_node_leaf *)leaf->schema;
3986 if (sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
3987 if (sleaf->type->basetype == LY_TYPE_LEAFREF) {
3988 /* find leafref target */
Radek Krejci0b013302021-03-29 15:22:32 +02003989 if (lyplg_type_resolve_leafref((struct lysc_type_leafref *)sleaf->type, &leaf->node, &leaf->value, set->tree,
Michal Vasko9e685082021-01-29 14:49:09 +01003990 &node, &errmsg)) {
Michal Vasko004d3152020-06-11 19:59:22 +02003991 LOGERR(set->ctx, LY_EVALID, errmsg);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003992 free(errmsg);
Michal Vasko004d3152020-06-11 19:59:22 +02003993 return LY_EVALID;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003994 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003995 } else {
3996 assert(sleaf->type->basetype == LY_TYPE_INST);
Michal Vasko004d3152020-06-11 19:59:22 +02003997 if (ly_path_eval(leaf->value.target, set->tree, &node)) {
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003998 LOGERR(set->ctx, LY_EVALID, "Invalid instance-identifier \"%s\" value - required instance not found.",
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02003999 lyd_get_value(&leaf->node));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004000 return LY_EVALID;
4001 }
4002 }
Michal Vasko004d3152020-06-11 19:59:22 +02004003
4004 /* insert it */
4005 set_insert_node(set, node, 0, LYXP_NODE_ELEM, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004006 }
4007 }
4008
4009 return LY_SUCCESS;
4010}
4011
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004012static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02004013xpath_derived_(struct lyxp_set **args, struct lyxp_set *set, uint32_t options, ly_bool self_match, const char *func)
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004014{
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01004015 uint32_t i;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004016 LY_ARRAY_COUNT_TYPE u;
4017 struct lyd_node_term *leaf;
4018 struct lysc_node_leaf *sleaf;
4019 struct lyd_meta *meta;
Michal Vasko93923692021-05-07 15:28:02 +02004020 struct lyd_value *val;
4021 const struct lys_module *mod;
4022 const char *id_name;
4023 uint16_t id_len;
4024 struct lysc_ident *id;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004025 LY_ERR rc = LY_SUCCESS;
Radek Krejci857189e2020-09-01 13:26:36 +02004026 ly_bool found;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004027
4028 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02004029 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004030 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", func);
Michal Vasko5676f4e2021-04-06 17:14:45 +02004031 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4032 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4033 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype),
4034 sleaf->name);
4035 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_IDENT)) {
4036 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"identityref\".", func, sleaf->name);
4037 }
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004038 }
4039
4040 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4041 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4042 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype),
Michal Vasko69730152020-10-09 16:30:07 +02004043 sleaf->name);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004044 } else if (!warn_is_string_type(sleaf->type)) {
4045 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", func, sleaf->name);
4046 }
4047 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004048 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004049 return rc;
4050 }
4051
4052 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004053 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "derived-from(-or-self)(node-set, string)");
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004054 return LY_EVALID;
4055 }
4056 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
4057 LY_CHECK_RET(rc);
4058
Michal Vasko93923692021-05-07 15:28:02 +02004059 /* parse the identity */
4060 id_name = args[1]->val.str;
4061 id_len = strlen(id_name);
4062 rc = moveto_resolve_model(&id_name, &id_len, set, set->cur_node ? set->cur_node->schema : NULL, &mod);
4063 LY_CHECK_RET(rc);
4064 if (!mod) {
4065 LOGVAL(set->ctx, LYVE_XPATH, "Identity \"%.*s\" without a prefix.", (int)id_len, id_name);
4066 return LY_EVALID;
4067 }
4068
4069 /* find the identity */
4070 found = 0;
4071 LY_ARRAY_FOR(mod->identities, u) {
4072 if (!ly_strncmp(mod->identities[u].name, id_name, id_len)) {
4073 /* we have match */
4074 found = 1;
4075 break;
4076 }
4077 }
4078 if (!found) {
4079 LOGVAL(set->ctx, LYVE_XPATH, "Identity \"%.*s\" not found in module \"%s\".", (int)id_len, id_name, mod->name);
4080 return LY_EVALID;
4081 }
4082 id = &mod->identities[u];
4083
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004084 set_fill_boolean(set, 0);
4085 found = 0;
4086 for (i = 0; i < args[0]->used; ++i) {
4087 if ((args[0]->val.nodes[i].type != LYXP_NODE_ELEM) && (args[0]->val.nodes[i].type != LYXP_NODE_META)) {
4088 continue;
4089 }
4090
4091 if (args[0]->val.nodes[i].type == LYXP_NODE_ELEM) {
4092 leaf = (struct lyd_node_term *)args[0]->val.nodes[i].node;
4093 sleaf = (struct lysc_node_leaf *)leaf->schema;
4094 val = &leaf->value;
4095 if (!(sleaf->nodetype & LYD_NODE_TERM) || (leaf->value.realtype->basetype != LY_TYPE_IDENT)) {
4096 /* uninteresting */
4097 continue;
4098 }
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004099 } else {
4100 meta = args[0]->val.meta[i].meta;
4101 val = &meta->value;
4102 if (val->realtype->basetype != LY_TYPE_IDENT) {
4103 /* uninteresting */
4104 continue;
4105 }
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004106 }
4107
Michal Vasko93923692021-05-07 15:28:02 +02004108 /* check the identity itself */
4109 if (self_match && (id == val->ident)) {
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004110 set_fill_boolean(set, 1);
4111 found = 1;
4112 }
Michal Vasko93923692021-05-07 15:28:02 +02004113 if (!found && !lyplg_type_identity_isderived(id, val->ident)) {
4114 set_fill_boolean(set, 1);
4115 found = 1;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004116 }
4117
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004118 if (found) {
4119 break;
4120 }
4121 }
4122
4123 return LY_SUCCESS;
4124}
4125
Michal Vasko03ff5a72019-09-11 13:49:33 +02004126/**
4127 * @brief Execute the YANG 1.1 derived-from(node-set, string) function. Returns LYXP_SET_BOOLEAN depending
4128 * on whether the first argument nodes contain a node of an identity derived from the second
4129 * argument identity.
4130 *
4131 * @param[in] args Array of arguments.
4132 * @param[in] arg_count Count of elements in @p args.
4133 * @param[in,out] set Context and result set at the same time.
4134 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004135 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004136 */
4137static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004138xpath_derived_from(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004139{
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004140 return xpath_derived_(args, set, options, 0, __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004141}
4142
4143/**
4144 * @brief Execute the YANG 1.1 derived-from-or-self(node-set, string) function. Returns LYXP_SET_BOOLEAN depending
4145 * on whether the first argument nodes contain a node of an identity that either is or is derived from
4146 * the second argument identity.
4147 *
4148 * @param[in] args Array of arguments.
4149 * @param[in] arg_count Count of elements in @p args.
4150 * @param[in,out] set Context and result set at the same time.
4151 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004152 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004153 */
4154static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004155xpath_derived_from_or_self(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004156{
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004157 return xpath_derived_(args, set, options, 1, __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004158}
4159
4160/**
4161 * @brief Execute the YANG 1.1 enum-value(node-set) function. Returns LYXP_SET_NUMBER
4162 * with the integer value of the first node's enum value, otherwise NaN.
4163 *
4164 * @param[in] args Array of arguments.
4165 * @param[in] arg_count Count of elements in @p args.
4166 * @param[in,out] set Context and result set at the same time.
4167 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004168 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004169 */
4170static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004171xpath_enum_value(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004172{
4173 struct lyd_node_term *leaf;
4174 struct lysc_node_leaf *sleaf;
4175 LY_ERR rc = LY_SUCCESS;
4176
4177 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02004178 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004179 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02004180 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4181 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4182 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
4183 sleaf->name);
4184 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_ENUM)) {
4185 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"enumeration\".", __func__, sleaf->name);
4186 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004187 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004188 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004189 return rc;
4190 }
4191
Michal Vaskod3678892020-05-21 10:06:58 +02004192 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004193 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "enum-value(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004194 return LY_EVALID;
4195 }
4196
4197 set_fill_number(set, NAN);
Michal Vaskod3678892020-05-21 10:06:58 +02004198 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004199 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
4200 sleaf = (struct lysc_node_leaf *)leaf->schema;
4201 if ((sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (sleaf->type->basetype == LY_TYPE_ENUM)) {
4202 set_fill_number(set, leaf->value.enum_item->value);
4203 }
4204 }
4205
4206 return LY_SUCCESS;
4207}
4208
4209/**
4210 * @brief Execute the XPath false() function. Returns LYXP_SET_BOOLEAN
4211 * with false value.
4212 *
4213 * @param[in] args Array of arguments.
4214 * @param[in] arg_count Count of elements in @p args.
4215 * @param[in,out] set Context and result set at the same time.
4216 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004217 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004218 */
4219static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004220xpath_false(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004221{
4222 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004223 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004224 return LY_SUCCESS;
4225 }
4226
4227 set_fill_boolean(set, 0);
4228 return LY_SUCCESS;
4229}
4230
4231/**
4232 * @brief Execute the XPath floor(number) function. Returns LYXP_SET_NUMBER
4233 * with the first argument floored (truncated).
4234 *
4235 * @param[in] args Array of arguments.
4236 * @param[in] arg_count Count of elements in @p args.
4237 * @param[in,out] set Context and result set at the same time.
4238 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004239 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004240 */
4241static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004242xpath_floor(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t UNUSED(options))
Michal Vasko03ff5a72019-09-11 13:49:33 +02004243{
4244 LY_ERR rc;
4245
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004246 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004247 LY_CHECK_RET(rc);
4248 if (isfinite(args[0]->val.num)) {
4249 set_fill_number(set, (long long)args[0]->val.num);
4250 }
4251
4252 return LY_SUCCESS;
4253}
4254
4255/**
4256 * @brief Execute the XPath lang(string) function. Returns LYXP_SET_BOOLEAN
4257 * whether the language of the text matches the one from the argument.
4258 *
4259 * @param[in] args Array of arguments.
4260 * @param[in] arg_count Count of elements in @p args.
4261 * @param[in,out] set Context and result set at the same time.
4262 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004263 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004264 */
4265static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004266xpath_lang(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004267{
4268 const struct lyd_node *node;
4269 struct lysc_node_leaf *sleaf;
Michal Vasko9f96a052020-03-10 09:41:45 +01004270 struct lyd_meta *meta = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004271 const char *val;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004272 LY_ERR rc = LY_SUCCESS;
4273
4274 if (options & LYXP_SCNODE_ALL) {
4275 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4276 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004277 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
4278 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004279 } else if (!warn_is_string_type(sleaf->type)) {
4280 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004281 }
4282 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004283 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004284 return rc;
4285 }
4286
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004287 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004288 LY_CHECK_RET(rc);
4289
Michal Vasko03ff5a72019-09-11 13:49:33 +02004290 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004291 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "lang(string)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004292 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004293 } else if (!set->used) {
4294 set_fill_boolean(set, 0);
4295 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004296 }
4297
4298 switch (set->val.nodes[0].type) {
4299 case LYXP_NODE_ELEM:
4300 case LYXP_NODE_TEXT:
4301 node = set->val.nodes[0].node;
4302 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004303 case LYXP_NODE_META:
4304 node = set->val.meta[0].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004305 break;
4306 default:
4307 /* nothing to do with roots */
4308 set_fill_boolean(set, 0);
4309 return LY_SUCCESS;
4310 }
4311
Michal Vasko9f96a052020-03-10 09:41:45 +01004312 /* find lang metadata */
Michal Vasko9e685082021-01-29 14:49:09 +01004313 for ( ; node; node = lyd_parent(node)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01004314 for (meta = node->meta; meta; meta = meta->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004315 /* annotations */
Michal Vasko9f96a052020-03-10 09:41:45 +01004316 if (meta->name && !strcmp(meta->name, "lang") && !strcmp(meta->annotation->module->name, "xml")) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004317 break;
4318 }
4319 }
4320
Michal Vasko9f96a052020-03-10 09:41:45 +01004321 if (meta) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004322 break;
4323 }
4324 }
4325
4326 /* compare languages */
Michal Vasko9f96a052020-03-10 09:41:45 +01004327 if (!meta) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004328 set_fill_boolean(set, 0);
4329 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004330 uint64_t i;
4331
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02004332 val = lyd_get_meta_value(meta);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004333 for (i = 0; args[0]->val.str[i]; ++i) {
4334 if (tolower(args[0]->val.str[i]) != tolower(val[i])) {
4335 set_fill_boolean(set, 0);
4336 break;
4337 }
4338 }
4339 if (!args[0]->val.str[i]) {
4340 if (!val[i] || (val[i] == '-')) {
4341 set_fill_boolean(set, 1);
4342 } else {
4343 set_fill_boolean(set, 0);
4344 }
4345 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004346 }
4347
4348 return LY_SUCCESS;
4349}
4350
4351/**
4352 * @brief Execute the XPath last() function. Returns LYXP_SET_NUMBER
4353 * with the context size.
4354 *
4355 * @param[in] args Array of arguments.
4356 * @param[in] arg_count Count of elements in @p args.
4357 * @param[in,out] set Context and result set at the same time.
4358 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004359 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004360 */
4361static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004362xpath_last(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004363{
4364 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004365 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004366 return LY_SUCCESS;
4367 }
4368
Michal Vasko03ff5a72019-09-11 13:49:33 +02004369 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004370 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "last()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004371 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004372 } else if (!set->used) {
4373 set_fill_number(set, 0);
4374 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004375 }
4376
4377 set_fill_number(set, set->ctx_size);
4378 return LY_SUCCESS;
4379}
4380
4381/**
4382 * @brief Execute the XPath local-name(node-set?) function. Returns LYXP_SET_STRING
4383 * with the node name without namespace from the argument or the context.
4384 *
4385 * @param[in] args Array of arguments.
4386 * @param[in] arg_count Count of elements in @p args.
4387 * @param[in,out] set Context and result set at the same time.
4388 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004389 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004390 */
4391static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004392xpath_local_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004393{
4394 struct lyxp_set_node *item;
Michal Vasko69730152020-10-09 16:30:07 +02004395
Michal Vasko03ff5a72019-09-11 13:49:33 +02004396 /* suppress unused variable warning */
4397 (void)options;
4398
4399 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004400 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004401 return LY_SUCCESS;
4402 }
4403
4404 if (arg_count) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004405 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004406 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004407 "local-name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004408 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004409 } else if (!args[0]->used) {
4410 set_fill_string(set, "", 0);
4411 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004412 }
4413
4414 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004415 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004416
4417 item = &args[0]->val.nodes[0];
4418 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004419 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004420 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "local-name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004421 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004422 } else if (!set->used) {
4423 set_fill_string(set, "", 0);
4424 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004425 }
4426
4427 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004428 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004429
4430 item = &set->val.nodes[0];
4431 }
4432
4433 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004434 case LYXP_NODE_NONE:
4435 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004436 case LYXP_NODE_ROOT:
4437 case LYXP_NODE_ROOT_CONFIG:
4438 case LYXP_NODE_TEXT:
4439 set_fill_string(set, "", 0);
4440 break;
4441 case LYXP_NODE_ELEM:
4442 set_fill_string(set, item->node->schema->name, strlen(item->node->schema->name));
4443 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004444 case LYXP_NODE_META:
4445 set_fill_string(set, ((struct lyd_meta *)item->node)->name, strlen(((struct lyd_meta *)item->node)->name));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004446 break;
4447 }
4448
4449 return LY_SUCCESS;
4450}
4451
4452/**
4453 * @brief Execute the XPath name(node-set?) function. Returns LYXP_SET_STRING
4454 * with the node name fully qualified (with namespace) from the argument or the context.
4455 *
4456 * @param[in] args Array of arguments.
4457 * @param[in] arg_count Count of elements in @p args.
4458 * @param[in,out] set Context and result set at the same time.
4459 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004460 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004461 */
4462static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004463xpath_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004464{
4465 struct lyxp_set_node *item;
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004466 struct lys_module *mod = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004467 char *str;
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004468 const char *name = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004469
4470 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004471 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004472 return LY_SUCCESS;
4473 }
4474
4475 if (arg_count) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004476 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004477 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004478 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004479 } else if (!args[0]->used) {
4480 set_fill_string(set, "", 0);
4481 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004482 }
4483
4484 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004485 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004486
4487 item = &args[0]->val.nodes[0];
4488 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004489 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004490 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004491 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004492 } else if (!set->used) {
4493 set_fill_string(set, "", 0);
4494 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004495 }
4496
4497 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004498 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004499
4500 item = &set->val.nodes[0];
4501 }
4502
4503 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004504 case LYXP_NODE_NONE:
4505 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004506 case LYXP_NODE_ROOT:
4507 case LYXP_NODE_ROOT_CONFIG:
4508 case LYXP_NODE_TEXT:
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004509 /* keep NULL */
Michal Vasko03ff5a72019-09-11 13:49:33 +02004510 break;
4511 case LYXP_NODE_ELEM:
4512 mod = item->node->schema->module;
4513 name = item->node->schema->name;
4514 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004515 case LYXP_NODE_META:
4516 mod = ((struct lyd_meta *)item->node)->annotation->module;
4517 name = ((struct lyd_meta *)item->node)->name;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004518 break;
4519 }
4520
4521 if (mod && name) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004522 int rc = asprintf(&str, "%s:%s", ly_get_prefix(mod, set->format, set->prefix_data), name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004523 LY_CHECK_ERR_RET(rc == -1, LOGMEM(set->ctx), LY_EMEM);
4524 set_fill_string(set, str, strlen(str));
4525 free(str);
4526 } else {
4527 set_fill_string(set, "", 0);
4528 }
4529
4530 return LY_SUCCESS;
4531}
4532
4533/**
4534 * @brief Execute the XPath namespace-uri(node-set?) function. Returns LYXP_SET_STRING
4535 * with the namespace of the node from the argument or the context.
4536 *
4537 * @param[in] args Array of arguments.
4538 * @param[in] arg_count Count of elements in @p args.
4539 * @param[in,out] set Context and result set at the same time.
4540 * @param[in] options XPath options.
4541 * @return LY_ERR (LY_EINVAL for wrong arguments on schema)
4542 */
4543static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004544xpath_namespace_uri(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004545{
4546 struct lyxp_set_node *item;
4547 struct lys_module *mod;
Michal Vasko69730152020-10-09 16:30:07 +02004548
Michal Vasko03ff5a72019-09-11 13:49:33 +02004549 /* suppress unused variable warning */
4550 (void)options;
4551
4552 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004553 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004554 return LY_SUCCESS;
4555 }
4556
4557 if (arg_count) {
Michal Vaskod3678892020-05-21 10:06:58 +02004558 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004559 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
Michal Vasko69730152020-10-09 16:30:07 +02004560 "namespace-uri(node-set?)");
Michal Vaskod3678892020-05-21 10:06:58 +02004561 return LY_EVALID;
4562 } else if (!args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004563 set_fill_string(set, "", 0);
4564 return LY_SUCCESS;
4565 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004566
4567 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004568 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004569
4570 item = &args[0]->val.nodes[0];
4571 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004572 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004573 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "namespace-uri(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004574 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004575 } else if (!set->used) {
4576 set_fill_string(set, "", 0);
4577 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004578 }
4579
4580 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004581 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004582
4583 item = &set->val.nodes[0];
4584 }
4585
4586 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004587 case LYXP_NODE_NONE:
4588 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004589 case LYXP_NODE_ROOT:
4590 case LYXP_NODE_ROOT_CONFIG:
4591 case LYXP_NODE_TEXT:
4592 set_fill_string(set, "", 0);
4593 break;
4594 case LYXP_NODE_ELEM:
Michal Vasko9f96a052020-03-10 09:41:45 +01004595 case LYXP_NODE_META:
Michal Vasko03ff5a72019-09-11 13:49:33 +02004596 if (item->type == LYXP_NODE_ELEM) {
4597 mod = item->node->schema->module;
Michal Vasko9f96a052020-03-10 09:41:45 +01004598 } else { /* LYXP_NODE_META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02004599 /* annotations */
Michal Vasko9f96a052020-03-10 09:41:45 +01004600 mod = ((struct lyd_meta *)item->node)->annotation->module;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004601 }
4602
4603 set_fill_string(set, mod->ns, strlen(mod->ns));
4604 break;
4605 }
4606
4607 return LY_SUCCESS;
4608}
4609
4610/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02004611 * @brief Execute the XPath normalize-space(string?) function. Returns LYXP_SET_STRING
4612 * with normalized value (no leading, trailing, double white spaces) of the node
4613 * from the argument or the context.
4614 *
4615 * @param[in] args Array of arguments.
4616 * @param[in] arg_count Count of elements in @p args.
4617 * @param[in,out] set Context and result set at the same time.
4618 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004619 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004620 */
4621static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004622xpath_normalize_space(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004623{
4624 uint16_t i, new_used;
4625 char *new;
Radek Krejci857189e2020-09-01 13:26:36 +02004626 ly_bool have_spaces = 0, space_before = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004627 struct lysc_node_leaf *sleaf;
4628 LY_ERR rc = LY_SUCCESS;
4629
4630 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004631 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) &&
4632 (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004633 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004634 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
4635 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004636 } else if (!warn_is_string_type(sleaf->type)) {
4637 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004638 }
4639 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004640 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004641 return rc;
4642 }
4643
4644 if (arg_count) {
4645 set_fill_set(set, args[0]);
4646 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004647 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004648 LY_CHECK_RET(rc);
4649
4650 /* is there any normalization necessary? */
4651 for (i = 0; set->val.str[i]; ++i) {
4652 if (is_xmlws(set->val.str[i])) {
4653 if ((i == 0) || space_before || (!set->val.str[i + 1])) {
4654 have_spaces = 1;
4655 break;
4656 }
4657 space_before = 1;
4658 } else {
4659 space_before = 0;
4660 }
4661 }
4662
4663 /* yep, there is */
4664 if (have_spaces) {
4665 /* it's enough, at least one character will go, makes space for ending '\0' */
4666 new = malloc(strlen(set->val.str) * sizeof(char));
4667 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4668 new_used = 0;
4669
4670 space_before = 0;
4671 for (i = 0; set->val.str[i]; ++i) {
4672 if (is_xmlws(set->val.str[i])) {
4673 if ((i == 0) || space_before) {
4674 space_before = 1;
4675 continue;
4676 } else {
4677 space_before = 1;
4678 }
4679 } else {
4680 space_before = 0;
4681 }
4682
4683 new[new_used] = (space_before ? ' ' : set->val.str[i]);
4684 ++new_used;
4685 }
4686
4687 /* at worst there is one trailing space now */
4688 if (new_used && is_xmlws(new[new_used - 1])) {
4689 --new_used;
4690 }
4691
4692 new = ly_realloc(new, (new_used + 1) * sizeof(char));
4693 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4694 new[new_used] = '\0';
4695
4696 free(set->val.str);
4697 set->val.str = new;
4698 }
4699
4700 return LY_SUCCESS;
4701}
4702
4703/**
4704 * @brief Execute the XPath not(boolean) function. Returns LYXP_SET_BOOLEAN
4705 * with the argument converted to boolean and logically inverted.
4706 *
4707 * @param[in] args Array of arguments.
4708 * @param[in] arg_count Count of elements in @p args.
4709 * @param[in,out] set Context and result set at the same time.
4710 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004711 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004712 */
4713static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004714xpath_not(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004715{
4716 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004717 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004718 return LY_SUCCESS;
4719 }
4720
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004721 lyxp_set_cast(args[0], LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +02004722 if (args[0]->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004723 set_fill_boolean(set, 0);
4724 } else {
4725 set_fill_boolean(set, 1);
4726 }
4727
4728 return LY_SUCCESS;
4729}
4730
4731/**
4732 * @brief Execute the XPath number(object?) function. Returns LYXP_SET_NUMBER
4733 * with the number representation of either the argument or the context.
4734 *
4735 * @param[in] args Array of arguments.
4736 * @param[in] arg_count Count of elements in @p args.
4737 * @param[in,out] set Context and result set at the same time.
4738 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004739 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004740 */
4741static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004742xpath_number(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004743{
4744 LY_ERR rc;
4745
4746 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004747 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004748 return LY_SUCCESS;
4749 }
4750
4751 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004752 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004753 LY_CHECK_RET(rc);
4754 set_fill_set(set, args[0]);
4755 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004756 rc = lyxp_set_cast(set, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004757 LY_CHECK_RET(rc);
4758 }
4759
4760 return LY_SUCCESS;
4761}
4762
4763/**
4764 * @brief Execute the XPath position() function. Returns LYXP_SET_NUMBER
4765 * with the context position.
4766 *
4767 * @param[in] args Array of arguments.
4768 * @param[in] arg_count Count of elements in @p args.
4769 * @param[in,out] set Context and result set at the same time.
4770 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004771 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004772 */
4773static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004774xpath_position(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004775{
4776 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004777 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004778 return LY_SUCCESS;
4779 }
4780
Michal Vasko03ff5a72019-09-11 13:49:33 +02004781 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004782 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "position()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004783 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004784 } else if (!set->used) {
4785 set_fill_number(set, 0);
4786 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004787 }
4788
4789 set_fill_number(set, set->ctx_pos);
4790
4791 /* UNUSED in 'Release' build type */
4792 (void)options;
4793 return LY_SUCCESS;
4794}
4795
4796/**
4797 * @brief Execute the YANG 1.1 re-match(string, string) function. Returns LYXP_SET_BOOLEAN
4798 * depending on whether the second argument regex matches the first argument string. For details refer to
4799 * YANG 1.1 RFC section 10.2.1.
4800 *
4801 * @param[in] args Array of arguments.
4802 * @param[in] arg_count Count of elements in @p args.
4803 * @param[in,out] set Context and result set at the same time.
4804 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004805 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004806 */
4807static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004808xpath_re_match(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004809{
4810 struct lysc_pattern **patterns = NULL, **pattern;
4811 struct lysc_node_leaf *sleaf;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004812 LY_ERR rc = LY_SUCCESS;
4813 struct ly_err_item *err;
4814
4815 if (options & LYXP_SCNODE_ALL) {
4816 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4817 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4818 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004819 } else if (!warn_is_string_type(sleaf->type)) {
4820 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004821 }
4822 }
4823
4824 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4825 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4826 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004827 } else if (!warn_is_string_type(sleaf->type)) {
4828 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004829 }
4830 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004831 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004832 return rc;
4833 }
4834
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004835 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004836 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004837 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004838 LY_CHECK_RET(rc);
4839
4840 LY_ARRAY_NEW_RET(set->ctx, patterns, pattern, LY_EMEM);
Radek Iša45802b52021-02-09 09:21:58 +01004841 *pattern = calloc(1, sizeof **pattern);
Radek Krejciddace2c2021-01-08 11:30:56 +01004842 LOG_LOCSET(NULL, set->cur_node, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01004843 rc = lys_compile_type_pattern_check(set->ctx, args[1]->val.str, &(*pattern)->code);
Michal Vasko4a7d4d62021-12-13 17:05:06 +01004844 if (set->cur_node) {
4845 LOG_LOCBACK(0, 1, 0, 0);
4846 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004847 if (rc != LY_SUCCESS) {
4848 LY_ARRAY_FREE(patterns);
4849 return rc;
4850 }
4851
Radek Krejci0b013302021-03-29 15:22:32 +02004852 rc = lyplg_type_validate_patterns(patterns, args[0]->val.str, strlen(args[0]->val.str), &err);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004853 pcre2_code_free((*pattern)->code);
4854 free(*pattern);
4855 LY_ARRAY_FREE(patterns);
4856 if (rc && (rc != LY_EVALID)) {
Michal Vasko177d0ed2020-11-23 16:43:03 +01004857 ly_err_print(set->ctx, err);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004858 ly_err_free(err);
4859 return rc;
4860 }
4861
4862 if (rc == LY_EVALID) {
4863 ly_err_free(err);
4864 set_fill_boolean(set, 0);
4865 } else {
4866 set_fill_boolean(set, 1);
4867 }
4868
4869 return LY_SUCCESS;
4870}
4871
4872/**
4873 * @brief Execute the XPath round(number) function. Returns LYXP_SET_NUMBER
4874 * with the rounded first argument. For details refer to
4875 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-round.
4876 *
4877 * @param[in] args Array of arguments.
4878 * @param[in] arg_count Count of elements in @p args.
4879 * @param[in,out] set Context and result set at the same time.
4880 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004881 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004882 */
4883static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004884xpath_round(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004885{
4886 struct lysc_node_leaf *sleaf;
4887 LY_ERR rc = LY_SUCCESS;
4888
4889 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02004890 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004891 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02004892 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4893 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4894 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
4895 sleaf->name);
4896 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
4897 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
4898 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004899 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004900 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004901 return rc;
4902 }
4903
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004904 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004905 LY_CHECK_RET(rc);
4906
4907 /* cover only the cases where floor can't be used */
4908 if ((args[0]->val.num == -0.0f) || ((args[0]->val.num < 0) && (args[0]->val.num >= -0.5))) {
4909 set_fill_number(set, -0.0f);
4910 } else {
4911 args[0]->val.num += 0.5;
4912 rc = xpath_floor(args, 1, args[0], options);
4913 LY_CHECK_RET(rc);
4914 set_fill_number(set, args[0]->val.num);
4915 }
4916
4917 return LY_SUCCESS;
4918}
4919
4920/**
4921 * @brief Execute the XPath starts-with(string, string) function.
4922 * Returns LYXP_SET_BOOLEAN whether the second argument is
4923 * the prefix of the first or not.
4924 *
4925 * @param[in] args Array of arguments.
4926 * @param[in] arg_count Count of elements in @p args.
4927 * @param[in,out] set Context and result set at the same time.
4928 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004929 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004930 */
4931static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004932xpath_starts_with(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004933{
4934 struct lysc_node_leaf *sleaf;
4935 LY_ERR rc = LY_SUCCESS;
4936
4937 if (options & LYXP_SCNODE_ALL) {
4938 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4939 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4940 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004941 } else if (!warn_is_string_type(sleaf->type)) {
4942 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004943 }
4944 }
4945
4946 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4947 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4948 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004949 } else if (!warn_is_string_type(sleaf->type)) {
4950 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004951 }
4952 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004953 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004954 return rc;
4955 }
4956
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004957 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004958 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004959 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004960 LY_CHECK_RET(rc);
4961
4962 if (strncmp(args[0]->val.str, args[1]->val.str, strlen(args[1]->val.str))) {
4963 set_fill_boolean(set, 0);
4964 } else {
4965 set_fill_boolean(set, 1);
4966 }
4967
4968 return LY_SUCCESS;
4969}
4970
4971/**
4972 * @brief Execute the XPath string(object?) function. Returns LYXP_SET_STRING
4973 * with the string representation of either the argument or the context.
4974 *
4975 * @param[in] args Array of arguments.
4976 * @param[in] arg_count Count of elements in @p args.
4977 * @param[in,out] set Context and result set at the same time.
4978 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004979 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004980 */
4981static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004982xpath_string(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004983{
4984 LY_ERR rc;
4985
4986 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004987 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004988 return LY_SUCCESS;
4989 }
4990
4991 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004992 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004993 LY_CHECK_RET(rc);
4994 set_fill_set(set, args[0]);
4995 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004996 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004997 LY_CHECK_RET(rc);
4998 }
4999
5000 return LY_SUCCESS;
5001}
5002
5003/**
5004 * @brief Execute the XPath string-length(string?) function. Returns LYXP_SET_NUMBER
5005 * with the length of the string in either the argument or the context.
5006 *
5007 * @param[in] args Array of arguments.
5008 * @param[in] arg_count Count of elements in @p args.
5009 * @param[in,out] set Context and result set at the same time.
5010 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005011 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005012 */
5013static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005014xpath_string_length(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005015{
5016 struct lysc_node_leaf *sleaf;
5017 LY_ERR rc = LY_SUCCESS;
5018
5019 if (options & LYXP_SCNODE_ALL) {
5020 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5021 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5022 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005023 } else if (!warn_is_string_type(sleaf->type)) {
5024 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005025 }
5026 }
5027 if (!arg_count && (set->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set))) {
5028 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5029 LOGWRN(set->ctx, "Argument #0 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005030 } else if (!warn_is_string_type(sleaf->type)) {
5031 LOGWRN(set->ctx, "Argument #0 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005032 }
5033 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005034 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005035 return rc;
5036 }
5037
5038 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005039 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005040 LY_CHECK_RET(rc);
5041 set_fill_number(set, strlen(args[0]->val.str));
5042 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005043 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005044 LY_CHECK_RET(rc);
5045 set_fill_number(set, strlen(set->val.str));
5046 }
5047
5048 return LY_SUCCESS;
5049}
5050
5051/**
5052 * @brief Execute the XPath substring(string, number, number?) function.
5053 * Returns LYXP_SET_STRING substring of the first argument starting
5054 * on the second argument index ending on the third argument index,
5055 * indexed from 1. For exact definition refer to
5056 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-substring.
5057 *
5058 * @param[in] args Array of arguments.
5059 * @param[in] arg_count Count of elements in @p args.
5060 * @param[in,out] set Context and result set at the same time.
5061 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005062 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005063 */
5064static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005065xpath_substring(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005066{
Michal Vasko6db996e2022-07-28 10:28:04 +02005067 int64_t start;
5068 int32_t len;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005069 uint16_t str_start, str_len, pos;
5070 struct lysc_node_leaf *sleaf;
5071 LY_ERR rc = LY_SUCCESS;
5072
5073 if (options & LYXP_SCNODE_ALL) {
5074 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5075 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5076 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 +02005077 } else if (!warn_is_string_type(sleaf->type)) {
5078 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005079 }
5080 }
5081
5082 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5083 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5084 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 +02005085 } else if (!warn_is_numeric_type(sleaf->type)) {
5086 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005087 }
5088 }
5089
Michal Vasko69730152020-10-09 16:30:07 +02005090 if ((arg_count == 3) && (args[2]->type == LYXP_SET_SCNODE_SET) &&
5091 (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005092 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5093 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 +02005094 } else if (!warn_is_numeric_type(sleaf->type)) {
5095 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005096 }
5097 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005098 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005099 return rc;
5100 }
5101
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005102 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005103 LY_CHECK_RET(rc);
5104
5105 /* start */
5106 if (xpath_round(&args[1], 1, args[1], options)) {
5107 return -1;
5108 }
5109 if (isfinite(args[1]->val.num)) {
5110 start = args[1]->val.num - 1;
5111 } else if (isinf(args[1]->val.num) && signbit(args[1]->val.num)) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02005112 start = INT32_MIN;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005113 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02005114 start = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005115 }
5116
5117 /* len */
5118 if (arg_count == 3) {
5119 rc = xpath_round(&args[2], 1, args[2], options);
5120 LY_CHECK_RET(rc);
Radek Krejci1deb5be2020-08-26 16:43:36 +02005121 if (isnan(args[2]->val.num) || signbit(args[2]->val.num)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005122 len = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005123 } else if (isfinite(args[2]->val.num)) {
5124 len = args[2]->val.num;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005125 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02005126 len = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005127 }
5128 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02005129 len = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005130 }
5131
5132 /* find matching character positions */
5133 str_start = 0;
5134 str_len = 0;
5135 for (pos = 0; args[0]->val.str[pos]; ++pos) {
5136 if (pos < start) {
5137 ++str_start;
5138 } else if (pos < start + len) {
5139 ++str_len;
5140 } else {
5141 break;
5142 }
5143 }
5144
5145 set_fill_string(set, args[0]->val.str + str_start, str_len);
5146 return LY_SUCCESS;
5147}
5148
5149/**
5150 * @brief Execute the XPath substring-after(string, string) function.
5151 * Returns LYXP_SET_STRING with the string succeeding the occurance
5152 * of the second argument in the first or an empty string.
5153 *
5154 * @param[in] args Array of arguments.
5155 * @param[in] arg_count Count of elements in @p args.
5156 * @param[in,out] set Context and result set at the same time.
5157 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005158 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005159 */
5160static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005161xpath_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 +02005162{
5163 char *ptr;
5164 struct lysc_node_leaf *sleaf;
5165 LY_ERR rc = LY_SUCCESS;
5166
5167 if (options & LYXP_SCNODE_ALL) {
5168 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5169 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5170 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 +02005171 } else if (!warn_is_string_type(sleaf->type)) {
5172 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005173 }
5174 }
5175
5176 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5177 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5178 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 +02005179 } else if (!warn_is_string_type(sleaf->type)) {
5180 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005181 }
5182 }
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 rc;
5185 }
5186
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005187 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005188 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005189 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005190 LY_CHECK_RET(rc);
5191
5192 ptr = strstr(args[0]->val.str, args[1]->val.str);
5193 if (ptr) {
5194 set_fill_string(set, ptr + strlen(args[1]->val.str), strlen(ptr + strlen(args[1]->val.str)));
5195 } else {
5196 set_fill_string(set, "", 0);
5197 }
5198
5199 return LY_SUCCESS;
5200}
5201
5202/**
5203 * @brief Execute the XPath substring-before(string, string) function.
5204 * Returns LYXP_SET_STRING with the string preceding the occurance
5205 * of the second argument in the first or an empty string.
5206 *
5207 * @param[in] args Array of arguments.
5208 * @param[in] arg_count Count of elements in @p args.
5209 * @param[in,out] set Context and result set at the same time.
5210 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005211 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005212 */
5213static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005214xpath_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 +02005215{
5216 char *ptr;
5217 struct lysc_node_leaf *sleaf;
5218 LY_ERR rc = LY_SUCCESS;
5219
5220 if (options & LYXP_SCNODE_ALL) {
5221 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5222 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5223 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005224 } else if (!warn_is_string_type(sleaf->type)) {
5225 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005226 }
5227 }
5228
5229 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5230 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5231 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005232 } else if (!warn_is_string_type(sleaf->type)) {
5233 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005234 }
5235 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005236 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005237 return rc;
5238 }
5239
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005240 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005241 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005242 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005243 LY_CHECK_RET(rc);
5244
5245 ptr = strstr(args[0]->val.str, args[1]->val.str);
5246 if (ptr) {
5247 set_fill_string(set, args[0]->val.str, ptr - args[0]->val.str);
5248 } else {
5249 set_fill_string(set, "", 0);
5250 }
5251
5252 return LY_SUCCESS;
5253}
5254
5255/**
5256 * @brief Execute the XPath sum(node-set) function. Returns LYXP_SET_NUMBER
5257 * with the sum of all the nodes in the context.
5258 *
5259 * @param[in] args Array of arguments.
5260 * @param[in] arg_count Count of elements in @p args.
5261 * @param[in,out] set Context and result set at the same time.
5262 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005263 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005264 */
5265static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005266xpath_sum(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005267{
5268 long double num;
5269 char *str;
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01005270 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005271 struct lyxp_set set_item;
5272 struct lysc_node_leaf *sleaf;
5273 LY_ERR rc = LY_SUCCESS;
5274
5275 if (options & LYXP_SCNODE_ALL) {
5276 if (args[0]->type == LYXP_SET_SCNODE_SET) {
5277 for (i = 0; i < args[0]->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005278 if (args[0]->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005279 sleaf = (struct lysc_node_leaf *)args[0]->val.scnodes[i].scnode;
5280 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5281 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__,
Michal Vasko69730152020-10-09 16:30:07 +02005282 lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005283 } else if (!warn_is_numeric_type(sleaf->type)) {
5284 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005285 }
5286 }
5287 }
5288 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005289 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005290 return rc;
5291 }
5292
5293 set_fill_number(set, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005294
5295 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005296 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "sum(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02005297 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02005298 } else if (!args[0]->used) {
5299 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005300 }
5301
Michal Vasko5c4e5892019-11-14 12:31:38 +01005302 set_init(&set_item, set);
5303
Michal Vasko03ff5a72019-09-11 13:49:33 +02005304 set_item.type = LYXP_SET_NODE_SET;
Michal Vasko41decbf2021-11-02 11:50:21 +01005305 set_item.val.nodes = calloc(1, sizeof *set_item.val.nodes);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005306 LY_CHECK_ERR_RET(!set_item.val.nodes, LOGMEM(set->ctx), LY_EMEM);
5307
5308 set_item.used = 1;
5309 set_item.size = 1;
5310
5311 for (i = 0; i < args[0]->used; ++i) {
5312 set_item.val.nodes[0] = args[0]->val.nodes[i];
5313
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005314 rc = cast_node_set_to_string(&set_item, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005315 LY_CHECK_RET(rc);
5316 num = cast_string_to_number(str);
5317 free(str);
5318 set->val.num += num;
5319 }
5320
5321 free(set_item.val.nodes);
5322
5323 return LY_SUCCESS;
5324}
5325
5326/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02005327 * @brief Execute the XPath translate(string, string, string) function.
5328 * Returns LYXP_SET_STRING with the first argument with the characters
5329 * from the second argument replaced by those on the corresponding
5330 * positions in the third argument.
5331 *
5332 * @param[in] args Array of arguments.
5333 * @param[in] arg_count Count of elements in @p args.
5334 * @param[in,out] set Context and result set at the same time.
5335 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005336 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005337 */
5338static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005339xpath_translate(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005340{
5341 uint16_t i, j, new_used;
5342 char *new;
Radek Krejci857189e2020-09-01 13:26:36 +02005343 ly_bool have_removed;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005344 struct lysc_node_leaf *sleaf;
5345 LY_ERR rc = LY_SUCCESS;
5346
5347 if (options & LYXP_SCNODE_ALL) {
5348 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5349 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5350 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 +02005351 } else if (!warn_is_string_type(sleaf->type)) {
5352 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005353 }
5354 }
5355
5356 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5357 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5358 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 +02005359 } else if (!warn_is_string_type(sleaf->type)) {
5360 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005361 }
5362 }
5363
5364 if ((args[2]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
5365 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5366 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 +02005367 } else if (!warn_is_string_type(sleaf->type)) {
5368 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005369 }
5370 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005371 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005372 return rc;
5373 }
5374
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005375 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005376 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005377 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005378 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005379 rc = lyxp_set_cast(args[2], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005380 LY_CHECK_RET(rc);
5381
5382 new = malloc((strlen(args[0]->val.str) + 1) * sizeof(char));
5383 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5384 new_used = 0;
5385
5386 have_removed = 0;
5387 for (i = 0; args[0]->val.str[i]; ++i) {
Radek Krejci857189e2020-09-01 13:26:36 +02005388 ly_bool found = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005389
5390 for (j = 0; args[1]->val.str[j]; ++j) {
5391 if (args[0]->val.str[i] == args[1]->val.str[j]) {
5392 /* removing this char */
5393 if (j >= strlen(args[2]->val.str)) {
5394 have_removed = 1;
5395 found = 1;
5396 break;
5397 }
5398 /* replacing this char */
5399 new[new_used] = args[2]->val.str[j];
5400 ++new_used;
5401 found = 1;
5402 break;
5403 }
5404 }
5405
5406 /* copying this char */
5407 if (!found) {
5408 new[new_used] = args[0]->val.str[i];
5409 ++new_used;
5410 }
5411 }
5412
5413 if (have_removed) {
5414 new = ly_realloc(new, (new_used + 1) * sizeof(char));
5415 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5416 }
5417 new[new_used] = '\0';
5418
Michal Vaskod3678892020-05-21 10:06:58 +02005419 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005420 set->type = LYXP_SET_STRING;
5421 set->val.str = new;
5422
5423 return LY_SUCCESS;
5424}
5425
5426/**
5427 * @brief Execute the XPath true() function. Returns LYXP_SET_BOOLEAN
5428 * with true value.
5429 *
5430 * @param[in] args Array of arguments.
5431 * @param[in] arg_count Count of elements in @p args.
5432 * @param[in,out] set Context and result set at the same time.
5433 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005434 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005435 */
5436static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005437xpath_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 +02005438{
5439 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02005440 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005441 return LY_SUCCESS;
5442 }
5443
5444 set_fill_boolean(set, 1);
5445 return LY_SUCCESS;
5446}
5447
Michal Vasko03ff5a72019-09-11 13:49:33 +02005448/**
Michal Vasko49fec8e2022-05-24 10:28:33 +02005449 * @brief Execute the XPath node() processing instruction (node type). Returns LYXP_SET_NODE_SET
5450 * with only nodes from the context.
5451 *
5452 * @param[in,out] set Context and result set at the same time.
5453 * @param[in] axis Axis to search on.
5454 * @param[in] options XPath options.
5455 * @return LY_ERR
5456 */
5457static LY_ERR
5458xpath_pi_node(struct lyxp_set *set, enum lyxp_axis axis, uint32_t options)
5459{
5460 if (options & LYXP_SCNODE_ALL) {
5461 return moveto_scnode(set, NULL, NULL, axis, options);
5462 }
5463
5464 if (set->type != LYXP_SET_NODE_SET) {
5465 lyxp_set_free_content(set);
5466 return LY_SUCCESS;
5467 }
5468
5469 /* just like moving to a node with no restrictions */
5470 return moveto_node(set, NULL, NULL, axis, options);
5471}
5472
5473/**
5474 * @brief Execute the XPath text() processing instruction (node type). Returns LYXP_SET_NODE_SET
5475 * with the text content of the nodes in the context.
5476 *
5477 * @param[in,out] set Context and result set at the same time.
5478 * @param[in] axis Axis to search on.
5479 * @param[in] options XPath options.
5480 * @return LY_ERR
5481 */
5482static LY_ERR
5483xpath_pi_text(struct lyxp_set *set, enum lyxp_axis axis, uint32_t options)
5484{
5485 uint32_t i;
5486
5487 if (options & LYXP_SCNODE_ALL) {
5488 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
5489 return LY_SUCCESS;
5490 }
5491
5492 if (set->type != LYXP_SET_NODE_SET) {
5493 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "text()");
5494 return LY_EVALID;
5495 }
5496
5497 if (axis != LYXP_AXIS_CHILD) {
5498 /* even following and preceding axescan return text nodes, but whatever */
5499 lyxp_set_free_content(set);
5500 return LY_SUCCESS;
5501 }
5502
5503 for (i = 0; i < set->used; ++i) {
5504 switch (set->val.nodes[i].type) {
5505 case LYXP_NODE_NONE:
5506 LOGINT_RET(set->ctx);
5507 case LYXP_NODE_ELEM:
5508 if (set->val.nodes[i].node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
5509 set->val.nodes[i].type = LYXP_NODE_TEXT;
5510 break;
5511 }
5512 /* fall through */
5513 case LYXP_NODE_ROOT:
5514 case LYXP_NODE_ROOT_CONFIG:
5515 case LYXP_NODE_TEXT:
5516 case LYXP_NODE_META:
5517 set_remove_node_none(set, i);
5518 break;
5519 }
5520 }
5521 set_remove_nodes_none(set);
5522
5523 return LY_SUCCESS;
5524}
5525
5526/**
Michal Vasko6346ece2019-09-24 13:12:53 +02005527 * @brief Skip prefix and return corresponding model if there is a prefix. Logs directly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005528 *
Michal Vasko2104e9f2020-03-06 08:23:25 +01005529 * XPath @p set is expected to be a (sc)node set!
5530 *
Michal Vasko6346ece2019-09-24 13:12:53 +02005531 * @param[in,out] qname Qualified node name. If includes prefix, it is skipped.
5532 * @param[in,out] qname_len Length of @p qname, is updated accordingly.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005533 * @param[in] set Set with general XPath context.
5534 * @param[in] ctx_scnode Context node to inherit module for unprefixed node for ::LY_PREF_JSON.
Michal Vasko6346ece2019-09-24 13:12:53 +02005535 * @param[out] moveto_mod Expected module of a matching node.
5536 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005537 */
Michal Vasko6346ece2019-09-24 13:12:53 +02005538static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005539moveto_resolve_model(const char **qname, uint16_t *qname_len, const struct lyxp_set *set,
5540 const struct lysc_node *ctx_scnode, const struct lys_module **moveto_mod)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005541{
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02005542 const struct lys_module *mod = NULL;
Michal Vasko6346ece2019-09-24 13:12:53 +02005543 const char *ptr;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005544 size_t pref_len;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005545
Michal Vasko2104e9f2020-03-06 08:23:25 +01005546 assert((set->type == LYXP_SET_NODE_SET) || (set->type == LYXP_SET_SCNODE_SET));
5547
Michal Vasko6346ece2019-09-24 13:12:53 +02005548 if ((ptr = ly_strnchr(*qname, ':', *qname_len))) {
5549 /* specific module */
5550 pref_len = ptr - *qname;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005551 mod = ly_resolve_prefix(set->ctx, *qname, pref_len, set->format, set->prefix_data);
Michal Vasko6346ece2019-09-24 13:12:53 +02005552
Michal Vasko004d3152020-06-11 19:59:22 +02005553 /* check for errors and non-implemented modules, as they are not valid */
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005554 if (!mod || !mod->implemented) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005555 LOGVAL(set->ctx, LY_VCODE_XP_INMOD, pref_len, *qname);
Michal Vasko6346ece2019-09-24 13:12:53 +02005556 return LY_EVALID;
5557 }
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005558
Michal Vasko6346ece2019-09-24 13:12:53 +02005559 *qname += pref_len + 1;
5560 *qname_len -= pref_len + 1;
5561 } else if (((*qname)[0] == '*') && (*qname_len == 1)) {
5562 /* all modules - special case */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005563 mod = NULL;
Michal Vasko6346ece2019-09-24 13:12:53 +02005564 } else {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005565 switch (set->format) {
Radek Krejci8df109d2021-04-23 12:19:08 +02005566 case LY_VALUE_SCHEMA:
5567 case LY_VALUE_SCHEMA_RESOLVED:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005568 /* current module */
5569 mod = set->cur_mod;
5570 break;
Radek Krejci224d4b42021-04-23 13:54:59 +02005571 case LY_VALUE_CANON:
Radek Krejci8df109d2021-04-23 12:19:08 +02005572 case LY_VALUE_JSON:
Radek Krejcif9943642021-04-26 10:18:21 +02005573 case LY_VALUE_LYB:
Michal Vaskoddd76592022-01-17 13:34:48 +01005574 case LY_VALUE_STR_NS:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005575 /* inherit parent (context node) module */
5576 if (ctx_scnode) {
5577 mod = ctx_scnode->module;
5578 } else {
5579 mod = NULL;
5580 }
5581 break;
Radek Krejci8df109d2021-04-23 12:19:08 +02005582 case LY_VALUE_XML:
Michal Vasko52143d12021-04-14 15:36:39 +02005583 /* all nodes need to be prefixed */
5584 LOGVAL(set->ctx, LYVE_DATA, "Non-prefixed node \"%.*s\" in XML xpath found.", *qname_len, *qname);
5585 return LY_EVALID;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005586 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005587 }
5588
Michal Vasko6346ece2019-09-24 13:12:53 +02005589 *moveto_mod = mod;
5590 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005591}
5592
5593/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02005594 * @brief Move context @p set to the root. Handles absolute path.
5595 * Result is LYXP_SET_NODE_SET.
5596 *
5597 * @param[in,out] set Set to use.
5598 * @param[in] options Xpath options.
Michal Vaskob0099a92020-08-31 14:55:23 +02005599 * @return LY_ERR value.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005600 */
Michal Vaskob0099a92020-08-31 14:55:23 +02005601static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005602moveto_root(struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005603{
aPiecek8b0cc152021-05-31 16:40:31 +02005604 assert(!(options & LYXP_SKIP_EXPR));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005605
5606 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02005607 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskob0099a92020-08-31 14:55:23 +02005608 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, NULL, set->root_type, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005609 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005610 set->type = LYXP_SET_NODE_SET;
5611 set->used = 0;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005612 set_insert_node(set, NULL, 0, set->root_type, 0);
Michal Vasko306e2832022-07-25 09:15:17 +02005613 set->non_child_axis = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005614 }
Michal Vaskob0099a92020-08-31 14:55:23 +02005615
5616 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005617}
5618
5619/**
5620 * @brief Check @p node as a part of NameTest processing.
5621 *
5622 * @param[in] node Node to check.
Michal Vasko49fec8e2022-05-24 10:28:33 +02005623 * @param[in] node_type Node type of @p node.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005624 * @param[in] set Set to read general context from.
Michal Vaskod3678892020-05-21 10:06:58 +02005625 * @param[in] node_name Node name in the dictionary to move to, NULL for any node.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005626 * @param[in] moveto_mod Expected module of the node, NULL for no prefix.
Michal Vaskocdad7122020-11-09 21:04:44 +01005627 * @param[in] options XPath options.
Michal Vasko6346ece2019-09-24 13:12:53 +02005628 * @return LY_ERR (LY_ENOT if node does not match, LY_EINCOMPLETE on unresolved when,
Michal Vaskocd2c88a2022-06-07 10:54:34 +02005629 * LY_EINVAL if neither node nor any children match)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005630 */
5631static LY_ERR
Michal Vasko49fec8e2022-05-24 10:28:33 +02005632moveto_node_check(const struct lyd_node *node, enum lyxp_node_type node_type, const struct lyxp_set *set,
5633 const char *node_name, const struct lys_module *moveto_mod, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005634{
Michal Vasko49fec8e2022-05-24 10:28:33 +02005635 if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT)) {
5636 assert(node_type == set->root_type);
5637
5638 if (node_name || moveto_mod) {
5639 /* root will not match a specific node */
5640 return LY_ENOT;
5641 }
5642 return LY_SUCCESS;
5643 } else if (node_type != LYXP_NODE_ELEM) {
5644 /* other types will not match */
5645 return LY_ENOT;
5646 }
5647
Michal Vaskodca9f122021-07-16 13:56:22 +02005648 if (!node->schema) {
5649 /* opaque node never matches */
5650 return LY_ENOT;
5651 }
5652
Michal Vasko03ff5a72019-09-11 13:49:33 +02005653 /* module check */
Michal Vasko19089f02022-06-07 11:02:11 +02005654 if (moveto_mod) {
5655 if (!(node->flags & LYD_EXT) && (node->schema->module != moveto_mod)) {
5656 return LY_ENOT;
5657 } else if ((node->flags & LYD_EXT) && strcmp(node->schema->module->name, moveto_mod->name)) {
5658 return LY_ENOT;
5659 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005660 }
5661
Michal Vasko5c4e5892019-11-14 12:31:38 +01005662 /* context check */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005663 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (node->schema->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005664 return LY_EINVAL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005665 } else if (set->context_op && (node->schema->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) &&
5666 (node->schema != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005667 return LY_EINVAL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005668 }
5669
5670 /* name check */
Michal Vasko19089f02022-06-07 11:02:11 +02005671 if (node_name) {
5672 if (!(node->flags & LYD_EXT) && (node->schema->name != node_name)) {
5673 return LY_ENOT;
5674 } else if ((node->flags & LYD_EXT) && strcmp(node->schema->name, node_name)) {
5675 return LY_ENOT;
5676 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005677 }
5678
Michal Vaskoa1424542019-11-14 16:08:52 +01005679 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01005680 if (!(options & LYXP_IGNORE_WHEN) && lysc_has_when(node->schema) && !(node->flags & LYD_WHEN_TRUE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005681 return LY_EINCOMPLETE;
Michal Vaskoa1424542019-11-14 16:08:52 +01005682 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005683
5684 /* match */
5685 return LY_SUCCESS;
5686}
5687
5688/**
Michal Vasko49fec8e2022-05-24 10:28:33 +02005689 * @brief Get the next node in a forward DFS.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005690 *
Michal Vasko49fec8e2022-05-24 10:28:33 +02005691 * @param[in] iter Last returned node.
5692 * @param[in] stop Node to stop the search on and not return.
5693 * @return Next node, NULL if there are no more.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005694 */
Michal Vasko49fec8e2022-05-24 10:28:33 +02005695static const struct lyd_node *
5696moveto_axis_node_next_dfs_forward(const struct lyd_node *iter, const struct lyd_node *stop)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005697{
Michal Vasko49fec8e2022-05-24 10:28:33 +02005698 const struct lyd_node *next = NULL;
5699
5700 /* 1) child */
5701 next = lyd_child(iter);
5702 if (!next) {
5703 if (iter == stop) {
5704 /* reached stop, no more descendants */
5705 return NULL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005706 }
Michal Vasko49fec8e2022-05-24 10:28:33 +02005707 /* 2) child next sibling */
5708 next = iter->next;
5709 }
5710 while (!next) {
5711 iter = lyd_parent(iter);
5712 if ((!stop && !iter) || (stop && (lyd_parent(iter) == lyd_parent(stop)))) {
5713 return NULL;
5714 }
5715 next = iter->next;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005716 }
5717
Michal Vasko49fec8e2022-05-24 10:28:33 +02005718 return next;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005719}
5720
5721/**
Michal Vasko49fec8e2022-05-24 10:28:33 +02005722 * @brief Get the next node in a backward DFS.
5723 *
5724 * @param[in] iter Last returned node.
5725 * @param[in] stop Node to stop the search on and not return.
5726 * @return Next node, NULL if there are no more.
5727 */
5728static const struct lyd_node *
5729moveto_axis_node_next_dfs_backward(const struct lyd_node *iter, const struct lyd_node *stop)
5730{
5731 const struct lyd_node *next = NULL;
5732
5733 /* 1) previous sibling innermost last child */
5734 next = iter->prev->next ? iter->prev : NULL;
5735 while (next && lyd_child(next)) {
5736 next = lyd_child(next);
5737 next = next->prev;
5738 }
5739
5740 if (!next) {
5741 /* 2) parent */
5742 iter = lyd_parent(iter);
5743 if ((!stop && !iter) || (stop && (lyd_parent(iter) == lyd_parent(stop)))) {
5744 return NULL;
5745 }
5746 next = iter;
5747 }
5748
5749 return next;
5750}
5751
5752/**
5753 * @brief Get the first node on an axis for a context node.
5754 *
5755 * @param[in,out] iter NULL, updated to the next node.
5756 * @param[in,out] iter_type Node type 0 of @p iter, updated to the node type of the next node.
5757 * @param[in] node Context node.
5758 * @param[in] node_type Type of @p node.
5759 * @param[in] axis Axis to use.
5760 * @param[in] set XPath set with the general context.
5761 * @return LY_SUCCESS on success.
5762 * @return LY_ENOTFOUND if no next node found.
5763 */
5764static LY_ERR
5765moveto_axis_node_next_first(const struct lyd_node **iter, enum lyxp_node_type *iter_type, const struct lyd_node *node,
5766 enum lyxp_node_type node_type, enum lyxp_axis axis, struct lyxp_set *set)
5767{
5768 const struct lyd_node *next = NULL;
5769 enum lyxp_node_type next_type = 0;
5770
5771 assert(!*iter);
5772 assert(!*iter_type);
5773
5774 switch (axis) {
5775 case LYXP_AXIS_ANCESTOR_OR_SELF:
5776 case LYXP_AXIS_DESCENDANT_OR_SELF:
5777 case LYXP_AXIS_SELF:
5778 /* return the context node */
5779 next = node;
5780 next_type = node_type;
5781 break;
5782
5783 case LYXP_AXIS_ANCESTOR:
5784 case LYXP_AXIS_PARENT:
5785 if (node_type == LYXP_NODE_ELEM) {
5786 next = lyd_parent(node);
5787 next_type = next ? LYXP_NODE_ELEM : set->root_type;
5788 } else if (node_type == LYXP_NODE_TEXT) {
5789 next = node;
5790 next_type = LYXP_NODE_ELEM;
5791 } else if (node_type == LYXP_NODE_META) {
5792 next = ((struct lyd_meta *)node)->parent;
5793 next_type = LYXP_NODE_ELEM;
5794 } /* else root does not have a parent */
5795 break;
5796
5797 case LYXP_AXIS_CHILD:
5798 if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT)) {
5799 assert(!node);
5800
5801 /* search in all the trees */
5802 next = set->tree;
5803 next_type = next ? LYXP_NODE_ELEM : 0;
5804 } else {
5805 /* search in children */
5806 next = lyd_child(node);
5807 next_type = next ? LYXP_NODE_ELEM : 0;
5808 }
5809 break;
5810
5811 case LYXP_AXIS_DESCENDANT:
5812 if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT)) {
5813 /* top-level nodes */
5814 next = set->tree;
5815 next_type = LYXP_NODE_ELEM;
5816 } else if (node_type == LYXP_NODE_ELEM) {
5817 /* start from the context node */
5818 next = moveto_axis_node_next_dfs_forward(node, node);
5819 next_type = next ? LYXP_NODE_ELEM : 0;
5820 } /* else no children */
5821 break;
5822
5823 case LYXP_AXIS_FOLLOWING:
5824 case LYXP_AXIS_FOLLOWING_SIBLING:
5825 if (node_type == LYXP_NODE_ELEM) {
5826 /* first next sibling */
5827 next = node->next;
5828 next_type = next ? LYXP_NODE_ELEM : 0;
5829 } /* else no sibling */
5830 break;
5831
5832 case LYXP_AXIS_PRECEDING:
5833 if ((node_type == LYXP_NODE_ELEM) && node->prev->next) {
5834 /* skip ancestors */
5835 next = moveto_axis_node_next_dfs_backward(node, NULL);
5836 assert(next);
5837 next_type = LYXP_NODE_ELEM;
5838 } /* else no sibling */
5839 break;
5840
5841 case LYXP_AXIS_PRECEDING_SIBLING:
5842 if (node_type == LYXP_NODE_ELEM) {
5843 /* first previous sibling */
5844 next = node->prev->next ? node->prev : NULL;
5845 next_type = next ? LYXP_NODE_ELEM : 0;
5846 } /* else no sibling */
5847 break;
5848
5849 case LYXP_AXIS_ATTRIBUTE:
5850 /* handled specially */
5851 assert(0);
5852 LOGINT(set->ctx);
5853 break;
5854 }
5855
5856 *iter = next;
5857 *iter_type = next_type;
5858 return next_type ? LY_SUCCESS : LY_ENOTFOUND;
5859}
5860
5861/**
5862 * @brief Iterate over all nodes on an axis for a context node.
5863 *
5864 * @param[in,out] iter Last returned node, start with NULL, updated to the next node.
5865 * @param[in,out] iter_type Node type of @p iter, start with 0, updated to the node type of the next node.
5866 * @param[in] node Context node.
5867 * @param[in] node_type Type of @p node.
5868 * @param[in] axis Axis to use.
5869 * @param[in] set XPath set with the general context.
5870 * @return LY_SUCCESS on success.
5871 * @return LY_ENOTFOUND if no next node found.
5872 */
5873static LY_ERR
5874moveto_axis_node_next(const struct lyd_node **iter, enum lyxp_node_type *iter_type, const struct lyd_node *node,
5875 enum lyxp_node_type node_type, enum lyxp_axis axis, struct lyxp_set *set)
5876{
5877 const struct lyd_node *next = NULL;
5878 enum lyxp_node_type next_type = 0;
5879
5880 if (!*iter_type) {
5881 /* first returned node */
5882 return moveto_axis_node_next_first(iter, iter_type, node, node_type, axis, set);
5883 }
5884
5885 switch (axis) {
5886 case LYXP_AXIS_ANCESTOR_OR_SELF:
5887 if ((*iter == node) && (*iter_type == node_type)) {
5888 /* fake first ancestor, we returned self before */
5889 *iter = NULL;
5890 *iter_type = 0;
5891 return moveto_axis_node_next_first(iter, iter_type, node, node_type, LYXP_AXIS_ANCESTOR, set);
5892 } /* else continue ancestor */
5893
5894 /* fallthrough */
5895 case LYXP_AXIS_ANCESTOR:
5896 if (*iter_type == LYXP_NODE_ELEM) {
5897 /* iter parent */
5898 next = lyd_parent(*iter);
5899 next_type = next ? LYXP_NODE_ELEM : set->root_type;
5900 } /* else root, no ancestors */
5901 break;
5902
5903 case LYXP_AXIS_CHILD:
5904 assert(*iter_type == LYXP_NODE_ELEM);
5905
5906 /* next sibling (child) */
5907 next = (*iter)->next;
5908 next_type = next ? LYXP_NODE_ELEM : 0;
5909 break;
5910
5911 case LYXP_AXIS_DESCENDANT_OR_SELF:
5912 if ((*iter == node) && (*iter_type == node_type)) {
5913 /* fake first descendant, we returned self before */
5914 *iter = NULL;
5915 *iter_type = 0;
5916 return moveto_axis_node_next_first(iter, iter_type, node, node_type, LYXP_AXIS_DESCENDANT, set);
5917 } /* else continue descendant */
5918
5919 /* fallthrough */
5920 case LYXP_AXIS_DESCENDANT:
5921 assert(*iter_type == LYXP_NODE_ELEM);
5922 next = moveto_axis_node_next_dfs_forward(*iter, node);
5923 next_type = next ? LYXP_NODE_ELEM : 0;
5924 break;
5925
5926 case LYXP_AXIS_FOLLOWING:
5927 assert(*iter_type == LYXP_NODE_ELEM);
5928 next = moveto_axis_node_next_dfs_forward(*iter, NULL);
5929 next_type = next ? LYXP_NODE_ELEM : 0;
5930 break;
5931
5932 case LYXP_AXIS_FOLLOWING_SIBLING:
5933 assert(*iter_type == LYXP_NODE_ELEM);
5934
5935 /* next sibling */
5936 next = (*iter)->next;
5937 next_type = next ? LYXP_NODE_ELEM : 0;
5938 break;
5939
5940 case LYXP_AXIS_PARENT:
5941 case LYXP_AXIS_SELF:
5942 /* parent/self was returned before */
5943 break;
5944
5945 case LYXP_AXIS_PRECEDING:
5946 assert(*iter_type == LYXP_NODE_ELEM);
5947 next = moveto_axis_node_next_dfs_backward(*iter, NULL);
5948 next_type = next ? LYXP_NODE_ELEM : 0;
5949 break;
5950
5951 case LYXP_AXIS_PRECEDING_SIBLING:
5952 assert(*iter_type == LYXP_NODE_ELEM);
5953
5954 /* previous sibling */
5955 next = (*iter)->prev->next ? (*iter)->prev : NULL;
5956 next_type = next ? LYXP_NODE_ELEM : 0;
5957 break;
5958
5959 case LYXP_AXIS_ATTRIBUTE:
5960 /* handled specially */
5961 assert(0);
5962 LOGINT(set->ctx);
5963 break;
5964 }
5965
5966 *iter = next;
5967 *iter_type = next_type;
5968 return next_type ? LY_SUCCESS : LY_ENOTFOUND;
5969}
5970
5971/**
5972 * @brief Move context @p set to a node. Result is LYXP_SET_NODE_SET. Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005973 *
5974 * @param[in,out] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005975 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005976 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko49fec8e2022-05-24 10:28:33 +02005977 * @param[in] axis Axis to search on.
Michal Vaskocdad7122020-11-09 21:04:44 +01005978 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005979 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5980 */
5981static LY_ERR
Michal Vasko49fec8e2022-05-24 10:28:33 +02005982moveto_node(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, enum lyxp_axis axis,
5983 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005984{
Michal Vasko230cf972021-12-02 12:31:00 +01005985 LY_ERR r, rc = LY_SUCCESS;
Michal Vasko49fec8e2022-05-24 10:28:33 +02005986 const struct lyd_node *iter;
5987 enum lyxp_node_type iter_type;
Michal Vasko230cf972021-12-02 12:31:00 +01005988 struct lyxp_set result;
Michal Vasko49fec8e2022-05-24 10:28:33 +02005989 uint32_t i;
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
Michal Vasko230cf972021-12-02 12:31:00 +01006000 /* init result set */
6001 set_init(&result, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006002
Michal Vasko49fec8e2022-05-24 10:28:33 +02006003 for (i = 0; i < set->used; ++i) {
6004 /* iterate over all the nodes on the axis of the node */
6005 iter = NULL;
6006 iter_type = 0;
6007 while (!moveto_axis_node_next(&iter, &iter_type, set->val.nodes[i].node, set->val.nodes[i].type, axis, set)) {
6008 r = moveto_node_check(iter, iter_type, set, ncname, moveto_mod, options);
6009 if (r == LY_EINCOMPLETE) {
Michal Vasko230cf972021-12-02 12:31:00 +01006010 rc = r;
6011 goto cleanup;
Michal Vasko49fec8e2022-05-24 10:28:33 +02006012 } else if (r) {
6013 continue;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006014 }
Michal Vasko49fec8e2022-05-24 10:28:33 +02006015
6016 /* check for duplicates if they are possible */
6017 switch (axis) {
6018 case LYXP_AXIS_ANCESTOR:
6019 case LYXP_AXIS_ANCESTOR_OR_SELF:
6020 case LYXP_AXIS_DESCENDANT:
6021 case LYXP_AXIS_DESCENDANT_OR_SELF:
6022 case LYXP_AXIS_FOLLOWING:
6023 case LYXP_AXIS_FOLLOWING_SIBLING:
6024 case LYXP_AXIS_PARENT:
6025 case LYXP_AXIS_PRECEDING:
6026 case LYXP_AXIS_PRECEDING_SIBLING:
Michal Vasko306e2832022-07-25 09:15:17 +02006027 result.non_child_axis = 1;
Michal Vasko49fec8e2022-05-24 10:28:33 +02006028 if (set_dup_node_check(&result, iter, iter_type, -1)) {
6029 continue;
6030 }
6031 break;
6032 case LYXP_AXIS_CHILD:
6033 case LYXP_AXIS_SELF:
6034 break;
6035 case LYXP_AXIS_ATTRIBUTE:
6036 /* handled specially */
6037 assert(0);
6038 LOGINT(set->ctx);
6039 break;
6040 }
6041
6042 /* matching node */
6043 set_insert_node(&result, iter, 0, iter_type, result.used);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006044 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006045 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006046
Michal Vasko230cf972021-12-02 12:31:00 +01006047 /* move result to the set */
6048 lyxp_set_free_content(set);
6049 *set = result;
6050 result.type = LYXP_SET_NUMBER;
Michal Vasko49fec8e2022-05-24 10:28:33 +02006051
Michal Vasko306e2832022-07-25 09:15:17 +02006052 /* sort the final set if the document order could have been broken */
6053 if (set->non_child_axis) {
6054 set_sort(set);
6055 } else {
6056 assert(!set_sort(set));
6057 }
Michal Vasko230cf972021-12-02 12:31:00 +01006058
6059cleanup:
6060 lyxp_set_free_content(&result);
6061 return rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006062}
6063
6064/**
Michal Vasko49fec8e2022-05-24 10:28:33 +02006065 * @brief Move context @p set to child nodes using hashes. Result is LYXP_SET_NODE_SET. Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006066 *
6067 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02006068 * @param[in] scnode Matching node schema.
Michal Vasko004d3152020-06-11 19:59:22 +02006069 * @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 +01006070 * @param[in] options XPath options.
Michal Vaskod3678892020-05-21 10:06:58 +02006071 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6072 */
6073static LY_ERR
Michal Vasko49fec8e2022-05-24 10:28:33 +02006074moveto_node_hash_child(struct lyxp_set *set, const struct lysc_node *scnode, const struct ly_path_predicate *predicates,
Michal Vaskocdad7122020-11-09 21:04:44 +01006075 uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02006076{
Michal Vaskoddd76592022-01-17 13:34:48 +01006077 LY_ERR ret = LY_SUCCESS, r;
Michal Vaskod3678892020-05-21 10:06:58 +02006078 uint32_t i;
Michal Vaskod3678892020-05-21 10:06:58 +02006079 const struct lyd_node *siblings;
Michal Vasko230cf972021-12-02 12:31:00 +01006080 struct lyxp_set result;
Michal Vasko004d3152020-06-11 19:59:22 +02006081 struct lyd_node *sub, *inst = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02006082
Michal Vasko004d3152020-06-11 19:59:22 +02006083 assert(scnode && (!(scnode->nodetype & (LYS_LIST | LYS_LEAFLIST)) || predicates));
Michal Vaskod3678892020-05-21 10:06:58 +02006084
Michal Vasko50aaa072021-12-02 13:11:56 +01006085 /* init result set */
6086 set_init(&result, set);
6087
aPiecek8b0cc152021-05-31 16:40:31 +02006088 if (options & LYXP_SKIP_EXPR) {
Michal Vasko004d3152020-06-11 19:59:22 +02006089 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02006090 }
6091
6092 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006093 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko004d3152020-06-11 19:59:22 +02006094 ret = LY_EVALID;
6095 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02006096 }
6097
6098 /* context check for all the nodes since we have the schema node */
6099 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) {
6100 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02006101 goto cleanup;
Michal Vasko69730152020-10-09 16:30:07 +02006102 } else if (set->context_op && (scnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) &&
6103 (scnode != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02006104 lyxp_set_free_content(set);
6105 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +02006106 }
6107
6108 /* create specific data instance if needed */
6109 if (scnode->nodetype == LYS_LIST) {
6110 LY_CHECK_GOTO(ret = lyd_create_list(scnode, predicates, &inst), cleanup);
6111 } else if (scnode->nodetype == LYS_LEAFLIST) {
6112 LY_CHECK_GOTO(ret = lyd_create_term2(scnode, &predicates[0].value, &inst), cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02006113 }
6114
Michal Vasko230cf972021-12-02 12:31:00 +01006115 for (i = 0; i < set->used; ++i) {
Michal Vaskod3678892020-05-21 10:06:58 +02006116 siblings = NULL;
6117
6118 if ((set->val.nodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.nodes[i].type == LYXP_NODE_ROOT)) {
6119 assert(!set->val.nodes[i].node);
6120
6121 /* search in all the trees */
6122 siblings = set->tree;
6123 } else if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
6124 /* search in children */
Radek Krejcia1c1e542020-09-29 16:06:52 +02006125 siblings = lyd_child(set->val.nodes[i].node);
Michal Vaskod3678892020-05-21 10:06:58 +02006126 }
6127
6128 /* find the node using hashes */
Michal Vasko004d3152020-06-11 19:59:22 +02006129 if (inst) {
Michal Vaskoddd76592022-01-17 13:34:48 +01006130 r = lyd_find_sibling_first(siblings, inst, &sub);
Michal Vaskod3678892020-05-21 10:06:58 +02006131 } else {
Michal Vaskoddd76592022-01-17 13:34:48 +01006132 r = lyd_find_sibling_val(siblings, scnode, NULL, 0, &sub);
Michal Vaskod3678892020-05-21 10:06:58 +02006133 }
Michal Vaskoddd76592022-01-17 13:34:48 +01006134 LY_CHECK_ERR_GOTO(r && (r != LY_ENOTFOUND), ret = r, cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02006135
6136 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01006137 if (!(options & LYXP_IGNORE_WHEN) && sub && lysc_has_when(sub->schema) && !(sub->flags & LYD_WHEN_TRUE)) {
Michal Vasko004d3152020-06-11 19:59:22 +02006138 ret = LY_EINCOMPLETE;
6139 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02006140 }
6141
6142 if (sub) {
6143 /* pos filled later */
Michal Vasko230cf972021-12-02 12:31:00 +01006144 set_insert_node(&result, sub, 0, LYXP_NODE_ELEM, result.used);
Michal Vaskod3678892020-05-21 10:06:58 +02006145 }
6146 }
6147
Michal Vasko230cf972021-12-02 12:31:00 +01006148 /* move result to the set */
6149 lyxp_set_free_content(set);
6150 *set = result;
6151 result.type = LYXP_SET_NUMBER;
6152 assert(!set_sort(set));
6153
Michal Vasko004d3152020-06-11 19:59:22 +02006154cleanup:
Michal Vasko230cf972021-12-02 12:31:00 +01006155 lyxp_set_free_content(&result);
Michal Vasko004d3152020-06-11 19:59:22 +02006156 lyd_free_tree(inst);
6157 return ret;
Michal Vaskod3678892020-05-21 10:06:58 +02006158}
6159
6160/**
Michal Vasko49fec8e2022-05-24 10:28:33 +02006161 * @brief Check @p node as a part of schema NameTest processing.
6162 *
6163 * @param[in] node Schema node to check.
6164 * @param[in] ctx_scnode Context node.
6165 * @param[in] set Set to read general context from.
6166 * @param[in] node_name Node name in the dictionary to move to, NULL for any nodes.
6167 * @param[in] moveto_mod Expected module of the node, NULL for no prefix.
6168 * @return LY_ERR (LY_ENOT if node does not match, LY_EINVAL if neither node nor any children match)
6169 */
6170static LY_ERR
6171moveto_scnode_check(const struct lysc_node *node, const struct lysc_node *ctx_scnode, const struct lyxp_set *set,
6172 const char *node_name, const struct lys_module *moveto_mod)
6173{
6174 if (!moveto_mod && node_name) {
6175 switch (set->format) {
6176 case LY_VALUE_SCHEMA:
6177 case LY_VALUE_SCHEMA_RESOLVED:
6178 /* use current module */
6179 moveto_mod = set->cur_mod;
6180 break;
6181 case LY_VALUE_JSON:
6182 case LY_VALUE_LYB:
6183 case LY_VALUE_STR_NS:
6184 /* inherit module of the context node, if any */
6185 if (ctx_scnode) {
6186 moveto_mod = ctx_scnode->module;
6187 }
6188 break;
6189 case LY_VALUE_CANON:
6190 case LY_VALUE_XML:
6191 /* not defined */
6192 LOGINT(set->ctx);
6193 return LY_EINVAL;
6194 }
6195 }
6196
6197 if (!node) {
6198 /* root will not match a specific node */
6199 if (node_name || moveto_mod) {
6200 return LY_ENOT;
6201 }
6202 return LY_SUCCESS;
6203 }
6204
6205 /* module check */
6206 if (moveto_mod && (node->module != moveto_mod)) {
6207 return LY_ENOT;
6208 }
6209
6210 /* context check */
6211 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (node->flags & LYS_CONFIG_R)) {
6212 return LY_EINVAL;
6213 } else if (set->context_op && (node->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && (node != set->context_op)) {
6214 return LY_EINVAL;
6215 }
6216
6217 /* name check */
6218 if (node_name && (node->name != node_name)) {
6219 return LY_ENOT;
6220 }
6221
6222 /* match */
6223 return LY_SUCCESS;
6224}
6225
6226/**
6227 * @brief Get the next node in a forward schema node DFS.
6228 *
6229 * @param[in] iter Last returned node.
6230 * @param[in] stop Node to stop the search on and not return.
6231 * @param[in] getnext_opts Options for ::lys_getnext().
6232 * @return Next node, NULL if there are no more.
6233 */
6234static const struct lysc_node *
6235moveto_axis_scnode_next_dfs_forward(const struct lysc_node *iter, const struct lysc_node *stop, uint32_t getnext_opts)
6236{
6237 const struct lysc_node *next = NULL;
6238
6239 next = lysc_node_child(iter);
6240 if (!next) {
6241 /* no children, try siblings */
Michal Vasko34a22fe2022-06-15 07:58:55 +02006242 if ((iter == stop) || !lysc_data_parent(iter)) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02006243 /* we are done, no next element to process */
6244 return NULL;
6245 }
6246
6247 next = lys_getnext(iter, lysc_data_parent(iter), NULL, getnext_opts);
6248 }
6249 while (!next && iter) {
6250 /* parent is already processed, go to its sibling */
6251 iter = iter->parent;
Michal Vasko34a22fe2022-06-15 07:58:55 +02006252 if ((iter == stop) || !lysc_data_parent(iter)) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02006253 /* we are done, no next element to process */
6254 return NULL;
6255 }
6256 next = lys_getnext(iter, lysc_data_parent(iter), NULL, getnext_opts);
6257 }
6258
6259 return next;
6260}
6261
6262/**
6263 * @brief Consider schema node based on its in_ctx enum value.
6264 *
6265 * @param[in,out] in_ctx In_ctx enum of the schema node, may be updated.
6266 * @param[in] axis Axis to use.
6267 * @return LY_SUCCESS on success.
6268 * @return LY_ENOT if the node should not be returned.
6269 */
6270static LY_ERR
6271moveto_axis_scnode_next_in_ctx(int32_t *in_ctx, enum lyxp_axis axis)
6272{
6273 switch (axis) {
6274 case LYXP_AXIS_SELF:
6275 if ((*in_ctx == LYXP_SET_SCNODE_START) || (*in_ctx == LYXP_SET_SCNODE_ATOM_CTX)) {
6276 /* additionally put the start node into context */
6277 *in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
6278 return LY_SUCCESS;
6279 }
6280 break;
6281 case LYXP_AXIS_PARENT:
6282 case LYXP_AXIS_ANCESTOR_OR_SELF:
6283 case LYXP_AXIS_ANCESTOR:
6284 case LYXP_AXIS_DESCENDANT_OR_SELF:
6285 case LYXP_AXIS_DESCENDANT:
6286 case LYXP_AXIS_FOLLOWING:
6287 case LYXP_AXIS_FOLLOWING_SIBLING:
6288 case LYXP_AXIS_PRECEDING:
6289 case LYXP_AXIS_PRECEDING_SIBLING:
6290 case LYXP_AXIS_CHILD:
6291 if (*in_ctx == LYXP_SET_SCNODE_START) {
6292 /* remember that context node was used */
6293 *in_ctx = LYXP_SET_SCNODE_START_USED;
6294 return LY_SUCCESS;
6295 } else if (*in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
6296 /* traversed */
6297 *in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
6298 return LY_SUCCESS;
6299 }
6300 break;
6301 case LYXP_AXIS_ATTRIBUTE:
6302 /* unreachable */
6303 assert(0);
6304 LOGINT(NULL);
6305 break;
6306 }
6307
6308 return LY_ENOT;
6309}
6310
6311/**
6312 * @brief Get previous sibling for a schema node.
6313 *
6314 * @param[in] scnode Schema node.
6315 * @param[in] getnext_opts Options for ::lys_getnext().
6316 * @return Previous sibling, NULL if none.
6317 */
6318static const struct lysc_node *
6319moveto_axis_scnode_preceding_sibling(const struct lysc_node *scnode, uint32_t getnext_opts)
6320{
6321 const struct lysc_node *next = NULL, *prev = NULL;
6322
6323 while ((next = lys_getnext(next, lysc_data_parent(scnode), scnode->module->compiled, getnext_opts))) {
6324 if (next == scnode) {
6325 break;
6326 }
6327
6328 prev = next;
6329 }
6330
6331 return prev;
6332}
6333
6334/**
6335 * @brief Get the first schema node on an axis for a context node.
6336 *
6337 * @param[in,out] iter Last returned node, start with NULL, updated to the next node.
6338 * @param[in,out] iter_type Node type of @p iter, start with 0, updated to the node type of the next node.
6339 * @param[in,out] iter_mod Internal module iterator, do not change.
6340 * @param[in,out] iter_mod_idx Internal module index iterator, do not change.
6341 * @param[in] scnode Context node.
6342 * @param[in] node_type Type of @p scnode.
6343 * @param[in] in_ctx In_ctx enum of @p scnode.
6344 * @param[in] axis Axis to use.
6345 * @param[in] set XPath set with the general context.
6346 * @param[in] getnext_opts Options for ::lys_getnext().
6347 * @return LY_SUCCESS on success.
6348 * @return LY_ENOTFOUND if no next node found.
6349 */
6350static LY_ERR
6351moveto_axis_scnode_next_first(const struct lysc_node **iter, enum lyxp_node_type *iter_type, const struct lys_module **iter_mod,
6352 uint32_t *iter_mod_idx, const struct lysc_node *scnode, enum lyxp_node_type node_type, enum lyxp_axis axis,
6353 struct lyxp_set *set, uint32_t getnext_opts)
6354{
6355 const struct lysc_node *next = NULL;
6356 enum lyxp_node_type next_type = 0;
6357
6358 assert(!*iter);
6359 assert(!*iter_type);
6360
6361 *iter_mod = NULL;
6362 *iter_mod_idx = 0;
6363
6364 switch (axis) {
6365 case LYXP_AXIS_ANCESTOR_OR_SELF:
6366 case LYXP_AXIS_DESCENDANT_OR_SELF:
6367 case LYXP_AXIS_SELF:
6368 if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT) || (node_type == LYXP_NODE_ELEM)) {
6369 /* just return the node */
6370 next = scnode;
6371 next_type = node_type;
6372 }
6373 break;
6374
6375 case LYXP_AXIS_ANCESTOR:
6376 case LYXP_AXIS_PARENT:
6377 if (node_type == LYXP_NODE_ELEM) {
6378 next = lysc_data_parent(scnode);
6379 next_type = next ? LYXP_NODE_ELEM : set->root_type;
6380 } /* else no parent */
6381 break;
6382
6383 case LYXP_AXIS_DESCENDANT:
6384 case LYXP_AXIS_CHILD:
6385 if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT)) {
6386 /* it can actually be in any module, it's all <running>, and even if it's moveto_mod (if set),
6387 * it can be in a top-level augment */
6388 while ((*iter_mod = ly_ctx_get_module_iter(set->ctx, iter_mod_idx))) {
6389 /* module may not be implemented or not compiled yet */
6390 if (!(*iter_mod)->compiled) {
6391 continue;
6392 }
6393
6394 /* get next node */
6395 if ((next = lys_getnext(NULL, NULL, (*iter_mod)->compiled, getnext_opts))) {
6396 next_type = LYXP_NODE_ELEM;
6397 break;
6398 }
6399 }
6400 } else if (node_type == LYXP_NODE_ELEM) {
6401 /* get next node */
6402 next = lys_getnext(NULL, scnode, NULL, getnext_opts);
6403 next_type = next ? LYXP_NODE_ELEM : 0;
6404 }
6405 break;
6406
6407 case LYXP_AXIS_FOLLOWING:
6408 case LYXP_AXIS_FOLLOWING_SIBLING:
6409 if (node_type == LYXP_NODE_ELEM) {
6410 /* first next sibling */
6411 next = lys_getnext(scnode, lysc_data_parent(scnode), scnode->module->compiled, getnext_opts);
6412 next_type = next ? LYXP_NODE_ELEM : 0;
6413 } /* else no sibling */
6414 break;
6415
6416 case LYXP_AXIS_PRECEDING:
6417 case LYXP_AXIS_PRECEDING_SIBLING:
6418 if (node_type == LYXP_NODE_ELEM) {
6419 /* first parent sibling */
6420 next = lys_getnext(NULL, lysc_data_parent(scnode), scnode->module->compiled, getnext_opts);
6421 if (next == scnode) {
6422 /* no preceding sibling */
6423 next = NULL;
6424 }
6425 next_type = next ? LYXP_NODE_ELEM : 0;
6426 } /* else no sibling */
6427 break;
6428
6429 case LYXP_AXIS_ATTRIBUTE:
6430 /* unreachable */
6431 assert(0);
6432 LOGINT(set->ctx);
6433 break;
6434 }
6435
6436 *iter = next;
6437 *iter_type = next_type;
6438 return next_type ? LY_SUCCESS : LY_ENOTFOUND;
6439}
6440
6441/**
6442 * @brief Iterate over all schema nodes on an axis for a context node.
6443 *
6444 * @param[in,out] iter Last returned node, start with NULL, updated to the next node.
6445 * @param[in,out] iter_type Node type of @p iter, start with 0, updated to the node type of the next node.
6446 * @param[in,out] iter_mod Internal module iterator, do not change.
6447 * @param[in,out] iter_mod_idx Internal module index iterator, do not change.
6448 * @param[in] scnode Context node.
6449 * @param[in] node_type Type of @p scnode.
6450 * @param[in] axis Axis to use.
6451 * @param[in] set XPath set with the general context.
6452 * @param[in] getnext_opts Options for ::lys_getnext().
6453 * @return LY_SUCCESS on success.
6454 * @return LY_ENOTFOUND if no next node found.
6455 */
6456static LY_ERR
6457moveto_axis_scnode_next(const struct lysc_node **iter, enum lyxp_node_type *iter_type, const struct lys_module **iter_mod,
6458 uint32_t *iter_mod_idx, const struct lysc_node *scnode, enum lyxp_node_type node_type, enum lyxp_axis axis,
6459 struct lyxp_set *set, uint32_t getnext_opts)
6460{
6461 const struct lysc_node *next = NULL, *dfs_stop;
6462 enum lyxp_node_type next_type = 0;
6463
6464 if (!*iter_type) {
6465 /* first returned node */
6466 return moveto_axis_scnode_next_first(iter, iter_type, iter_mod, iter_mod_idx, scnode, node_type, axis, set,
6467 getnext_opts);
6468 }
6469
6470 switch (axis) {
6471 case LYXP_AXIS_PARENT:
6472 case LYXP_AXIS_SELF:
6473 /* parent/self was returned before */
6474 break;
6475
6476 case LYXP_AXIS_ANCESTOR_OR_SELF:
6477 if ((*iter == scnode) && (*iter_type == node_type)) {
6478 /* fake first ancestor, we returned self before */
6479 *iter = NULL;
6480 *iter_type = 0;
6481 return moveto_axis_scnode_next_first(iter, iter_type, iter_mod, iter_mod_idx, scnode, node_type,
6482 LYXP_AXIS_ANCESTOR, set, getnext_opts);
6483 } /* else continue ancestor */
6484
6485 /* fallthrough */
6486 case LYXP_AXIS_ANCESTOR:
6487 if (*iter_type == LYXP_NODE_ELEM) {
6488 next = lysc_data_parent(*iter);
6489 next_type = next ? LYXP_NODE_ELEM : set->root_type;
6490 } /* else no ancestor */
6491 break;
6492
6493 case LYXP_AXIS_DESCENDANT_OR_SELF:
6494 if ((*iter == scnode) && (*iter_type == node_type)) {
6495 /* fake first descendant, we returned self before */
6496 *iter = NULL;
6497 *iter_type = 0;
6498 return moveto_axis_scnode_next_first(iter, iter_type, iter_mod, iter_mod_idx, scnode, node_type,
6499 LYXP_AXIS_DESCENDANT, set, getnext_opts);
6500 } /* else DFS until context node */
6501 dfs_stop = scnode;
6502
6503 /* fallthrough */
6504 case LYXP_AXIS_DESCENDANT:
6505 if (axis == LYXP_AXIS_DESCENDANT) {
6506 /* DFS until the context node */
6507 dfs_stop = scnode;
6508 }
6509
6510 /* fallthrough */
6511 case LYXP_AXIS_PRECEDING:
6512 if (axis == LYXP_AXIS_PRECEDING) {
6513 /* DFS until the previous sibling */
6514 dfs_stop = moveto_axis_scnode_preceding_sibling(scnode, getnext_opts);
6515 assert(dfs_stop);
6516
6517 if (*iter == dfs_stop) {
6518 /* we are done */
6519 break;
6520 }
6521 }
6522
6523 /* fallthrough */
6524 case LYXP_AXIS_FOLLOWING:
6525 if (axis == LYXP_AXIS_FOLLOWING) {
6526 /* DFS through the whole module */
6527 dfs_stop = NULL;
6528 }
6529
6530 /* nested nodes */
6531 assert(*iter);
6532 next = moveto_axis_scnode_next_dfs_forward(*iter, dfs_stop, getnext_opts);
6533 if (next) {
6534 next_type = LYXP_NODE_ELEM;
6535 break;
6536 } /* else get next top-level node just like a child */
6537
6538 /* fallthrough */
6539 case LYXP_AXIS_CHILD:
6540 case LYXP_AXIS_FOLLOWING_SIBLING:
6541 if (!*iter_mod) {
6542 /* nodes from a single module */
6543 if ((next = lys_getnext(*iter, lysc_data_parent(*iter), (*iter)->module->compiled, getnext_opts))) {
6544 next_type = LYXP_NODE_ELEM;
6545 break;
6546 }
6547
6548 assert(scnode);
6549 if (!lysc_data_parent(scnode)) {
6550 /* iterating over top-level nodes, find next */
6551 while (lysc_data_parent(*iter)) {
6552 *iter = lysc_data_parent(*iter);
6553 }
6554 if ((next = lys_getnext(*iter, NULL, (*iter)->module->compiled, getnext_opts))) {
6555 next_type = LYXP_NODE_ELEM;
6556 break;
6557 }
6558 }
6559 }
6560
6561 while (*iter_mod) {
6562 /* module top-level nodes */
6563 if ((next = lys_getnext(*iter, NULL, (*iter_mod)->compiled, getnext_opts))) {
6564 next_type = LYXP_NODE_ELEM;
6565 break;
6566 }
6567
6568 /* get next module */
6569 while ((*iter_mod = ly_ctx_get_module_iter(set->ctx, iter_mod_idx))) {
6570 /* module may not be implemented or not compiled yet */
6571 if ((*iter_mod)->compiled) {
6572 break;
6573 }
6574 }
6575
6576 /* new module, start over */
6577 *iter = NULL;
6578 }
6579 break;
6580
6581 case LYXP_AXIS_PRECEDING_SIBLING:
6582 assert(*iter);
6583
6584 /* next parent sibling until scnode */
6585 next = lys_getnext(*iter, lysc_data_parent(*iter), (*iter)->module->compiled, getnext_opts);
6586 if (next == scnode) {
6587 /* no previous sibling */
6588 next = NULL;
6589 }
6590 next_type = next ? LYXP_NODE_ELEM : 0;
6591 break;
6592
6593 case LYXP_AXIS_ATTRIBUTE:
6594 /* unreachable */
6595 assert(0);
6596 LOGINT(set->ctx);
6597 break;
6598 }
6599
6600 *iter = next;
6601 *iter_type = next_type;
6602 return next_type ? LY_SUCCESS : LY_ENOTFOUND;
6603}
6604
6605/**
Michal Vaskod3678892020-05-21 10:06:58 +02006606 * @brief Move context @p set to a schema node. Result is LYXP_SET_SCNODE_SET (or LYXP_SET_EMPTY).
6607 *
6608 * @param[in,out] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006609 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02006610 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko49fec8e2022-05-24 10:28:33 +02006611 * @param[in] axis Axis to search on.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006612 * @param[in] options XPath options.
6613 * @return LY_ERR
6614 */
6615static LY_ERR
Michal Vasko49fec8e2022-05-24 10:28:33 +02006616moveto_scnode(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, enum lyxp_axis axis,
6617 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006618{
Radek Krejci857189e2020-09-01 13:26:36 +02006619 ly_bool temp_ctx = 0;
Michal Vasko49fec8e2022-05-24 10:28:33 +02006620 uint32_t getnext_opts, orig_used, i, mod_idx, idx;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006621 const struct lys_module *mod;
Michal Vasko49fec8e2022-05-24 10:28:33 +02006622 const struct lysc_node *iter;
6623 enum lyxp_node_type iter_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006624
aPiecek8b0cc152021-05-31 16:40:31 +02006625 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006626 return LY_SUCCESS;
6627 }
6628
6629 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006630 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006631 return LY_EVALID;
6632 }
6633
Michal Vaskocafad9d2019-11-07 15:20:03 +01006634 /* getnext opts */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01006635 getnext_opts = 0;
Michal Vaskocafad9d2019-11-07 15:20:03 +01006636 if (options & LYXP_SCNODE_OUTPUT) {
6637 getnext_opts |= LYS_GETNEXT_OUTPUT;
6638 }
6639
Michal Vasko03ff5a72019-09-11 13:49:33 +02006640 orig_used = set->used;
6641 for (i = 0; i < orig_used; ++i) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02006642 /* update in_ctx first */
6643 if (moveto_axis_scnode_next_in_ctx(&set->val.scnodes[i].in_ctx, axis)) {
6644 /* not usable, skip */
6645 continue;
6646 }
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006647
Michal Vasko49fec8e2022-05-24 10:28:33 +02006648 iter = NULL;
6649 iter_type = 0;
6650 while (!moveto_axis_scnode_next(&iter, &iter_type, &mod, &mod_idx, set->val.scnodes[i].scnode,
6651 set->val.scnodes[i].type, axis, set, getnext_opts)) {
6652 if (moveto_scnode_check(iter, NULL, set, ncname, moveto_mod)) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006653 continue;
6654 }
6655
Michal Vasko49fec8e2022-05-24 10:28:33 +02006656 /* insert */
6657 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, iter_type, &idx));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006658
Michal Vasko49fec8e2022-05-24 10:28:33 +02006659 /* we need to prevent these nodes from being considered in this moveto */
6660 if ((idx < orig_used) && (idx > i)) {
6661 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX;
6662 temp_ctx = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006663 }
6664 }
6665 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006666
6667 /* correct temporary in_ctx values */
6668 if (temp_ctx) {
6669 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006670 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_NEW_CTX) {
6671 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006672 }
6673 }
6674 }
6675
6676 return LY_SUCCESS;
6677}
6678
6679/**
Michal Vasko49fec8e2022-05-24 10:28:33 +02006680 * @brief Move context @p set to a child node and all its descendants. Result is LYXP_SET_NODE_SET.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006681 * Context position aware.
6682 *
6683 * @param[in] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006684 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02006685 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vaskocdad7122020-11-09 21:04:44 +01006686 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006687 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6688 */
6689static LY_ERR
Michal Vasko49fec8e2022-05-24 10:28:33 +02006690moveto_node_alldesc_child(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006691{
6692 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006693 const struct lyd_node *next, *elem, *start;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006694 struct lyxp_set ret_set;
6695 LY_ERR rc;
6696
aPiecek8b0cc152021-05-31 16:40:31 +02006697 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006698 return LY_SUCCESS;
6699 }
6700
6701 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006702 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006703 return LY_EVALID;
6704 }
6705
Michal Vasko9f96a052020-03-10 09:41:45 +01006706 /* replace the original nodes (and throws away all text and meta nodes, root is replaced by a child) */
Michal Vasko49fec8e2022-05-24 10:28:33 +02006707 rc = xpath_pi_node(set, LYXP_AXIS_CHILD, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006708 LY_CHECK_RET(rc);
6709
Michal Vasko6346ece2019-09-24 13:12:53 +02006710 /* this loop traverses all the nodes in the set and adds/keeps only those that match qname */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006711 set_init(&ret_set, set);
6712 for (i = 0; i < set->used; ++i) {
6713
6714 /* TREE DFS */
6715 start = set->val.nodes[i].node;
6716 for (elem = next = start; elem; elem = next) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02006717 rc = moveto_node_check(elem, LYXP_NODE_ELEM, set, ncname, moveto_mod, options);
Michal Vasko6346ece2019-09-24 13:12:53 +02006718 if (!rc) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006719 /* add matching node into result set */
6720 set_insert_node(&ret_set, elem, 0, LYXP_NODE_ELEM, ret_set.used);
6721 if (set_dup_node_check(set, elem, LYXP_NODE_ELEM, i)) {
6722 /* the node is a duplicate, we'll process it later in the set */
6723 goto skip_children;
6724 }
Michal Vasko6346ece2019-09-24 13:12:53 +02006725 } else if (rc == LY_EINCOMPLETE) {
Michal Vasko6346ece2019-09-24 13:12:53 +02006726 return rc;
6727 } else if (rc == LY_EINVAL) {
6728 goto skip_children;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006729 }
6730
6731 /* TREE DFS NEXT ELEM */
6732 /* select element for the next run - children first */
Radek Krejcia1c1e542020-09-29 16:06:52 +02006733 next = lyd_child(elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006734 if (!next) {
6735skip_children:
6736 /* no children, so try siblings, but only if it's not the start,
6737 * that is considered to be the root and it's siblings are not traversed */
6738 if (elem != start) {
6739 next = elem->next;
6740 } else {
6741 break;
6742 }
6743 }
6744 while (!next) {
6745 /* no siblings, go back through the parents */
Michal Vasko9e685082021-01-29 14:49:09 +01006746 if (lyd_parent(elem) == start) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006747 /* we are done, no next element to process */
6748 break;
6749 }
6750 /* parent is already processed, go to its sibling */
Michal Vasko9e685082021-01-29 14:49:09 +01006751 elem = lyd_parent(elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006752 next = elem->next;
6753 }
6754 }
6755 }
6756
6757 /* make the temporary set the current one */
6758 ret_set.ctx_pos = set->ctx_pos;
6759 ret_set.ctx_size = set->ctx_size;
Michal Vaskod3678892020-05-21 10:06:58 +02006760 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006761 memcpy(set, &ret_set, sizeof *set);
Michal Vasko306e2832022-07-25 09:15:17 +02006762 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006763
6764 return LY_SUCCESS;
6765}
6766
6767/**
Michal Vasko49fec8e2022-05-24 10:28:33 +02006768 * @brief Move context @p set to a child schema node and all its descendants starting from a node.
6769 * Result is LYXP_SET_NODE_SET.
6770 *
6771 * @param[in] set Set to use.
6772 * @param[in] start Start node whose subtree to add.
6773 * @param[in] start_idx Index of @p start in @p set.
6774 * @param[in] moveto_mod Matching node module, NULL for no prefix.
6775 * @param[in] ncname Matching node name in the dictionary, NULL for any.
6776 * @param[in] options XPath options.
6777 * @return LY_ERR value.
6778 */
6779static LY_ERR
6780moveto_scnode_dfs(struct lyxp_set *set, const struct lysc_node *start, uint32_t start_idx,
6781 const struct lys_module *moveto_mod, const char *ncname, uint32_t options)
6782{
6783 const struct lysc_node *next, *elem;
6784 uint32_t idx;
6785 LY_ERR rc;
6786
6787 /* TREE DFS */
6788 for (elem = next = start; elem; elem = next) {
6789 if ((elem == start) || (elem->nodetype & (LYS_CHOICE | LYS_CASE))) {
6790 /* schema-only nodes, skip root */
6791 goto next_iter;
6792 }
6793
6794 rc = moveto_scnode_check(elem, start, set, ncname, moveto_mod);
6795 if (!rc) {
6796 if (lyxp_set_scnode_contains(set, elem, LYXP_NODE_ELEM, start_idx, &idx)) {
6797 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
6798 if (idx > start_idx) {
6799 /* we will process it later in the set */
6800 goto skip_children;
6801 }
6802 } else {
6803 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, elem, LYXP_NODE_ELEM, NULL));
6804 }
6805 } else if (rc == LY_EINVAL) {
6806 goto skip_children;
6807 }
6808
6809next_iter:
6810 /* TREE DFS NEXT ELEM */
6811 /* select element for the next run - children first */
6812 next = lysc_node_child(elem);
6813 if (next && (next->nodetype == LYS_INPUT) && (options & LYXP_SCNODE_OUTPUT)) {
6814 next = next->next;
6815 } else if (next && (next->nodetype == LYS_OUTPUT) && !(options & LYXP_SCNODE_OUTPUT)) {
6816 next = next->next;
6817 }
6818 if (!next) {
6819skip_children:
6820 /* no children, so try siblings, but only if it's not the start,
6821 * that is considered to be the root and it's siblings are not traversed */
6822 if (elem != start) {
6823 next = elem->next;
6824 } else {
6825 break;
6826 }
6827 }
6828 while (!next) {
6829 /* no siblings, go back through the parents */
6830 if (elem->parent == start) {
6831 /* we are done, no next element to process */
6832 break;
6833 }
6834 /* parent is already processed, go to its sibling */
6835 elem = elem->parent;
6836 next = elem->next;
6837 }
6838 }
6839
6840 return LY_SUCCESS;
6841}
6842
6843/**
6844 * @brief Move context @p set to a child schema node and all its descendants. Result is LYXP_SET_NODE_SET.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006845 *
6846 * @param[in] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006847 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02006848 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006849 * @param[in] options XPath options.
Michal Vasko49fec8e2022-05-24 10:28:33 +02006850 * @return LY_ERR value.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006851 */
6852static LY_ERR
Michal Vasko49fec8e2022-05-24 10:28:33 +02006853moveto_scnode_alldesc_child(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006854{
Michal Vasko49fec8e2022-05-24 10:28:33 +02006855 uint32_t i, orig_used, mod_idx;
6856 const struct lys_module *mod;
6857 const struct lysc_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006858
aPiecek8b0cc152021-05-31 16:40:31 +02006859 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006860 return LY_SUCCESS;
6861 }
6862
6863 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006864 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006865 return LY_EVALID;
6866 }
6867
Michal Vasko03ff5a72019-09-11 13:49:33 +02006868 orig_used = set->used;
6869 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006870 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
6871 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006872 continue;
6873 }
6874
6875 /* remember context node */
Radek Krejcif13b87b2020-12-01 22:02:17 +01006876 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vaskoec4df482019-12-16 10:02:18 +01006877 } else {
Michal Vasko1a09b212021-05-06 13:00:10 +02006878 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006879 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006880
Michal Vasko49fec8e2022-05-24 10:28:33 +02006881 if ((set->val.scnodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.scnodes[i].type == LYXP_NODE_ROOT)) {
6882 /* traverse all top-level nodes in all the modules */
6883 mod_idx = 0;
6884 while ((mod = ly_ctx_get_module_iter(set->ctx, &mod_idx))) {
6885 /* module may not be implemented or not compiled yet */
6886 if (!mod->compiled) {
6887 continue;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006888 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006889
Michal Vasko49fec8e2022-05-24 10:28:33 +02006890 root = NULL;
6891 /* no getnext opts needed */
6892 while ((root = lys_getnext(root, NULL, mod->compiled, 0))) {
6893 LY_CHECK_RET(moveto_scnode_dfs(set, root, i, moveto_mod, ncname, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006894 }
6895 }
Michal Vasko49fec8e2022-05-24 10:28:33 +02006896
6897 } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
6898 /* add all the descendants recursively */
6899 LY_CHECK_RET(moveto_scnode_dfs(set, set->val.scnodes[i].scnode, i, moveto_mod, ncname, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006900 }
6901 }
6902
6903 return LY_SUCCESS;
6904}
6905
6906/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02006907 * @brief Move context @p set to an attribute. Result is LYXP_SET_NODE_SET.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006908 * Indirectly context position aware.
6909 *
6910 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02006911 * @param[in] mod Matching metadata module, NULL for any.
6912 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
aPiecek8b0cc152021-05-31 16:40:31 +02006913 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006914 * @return LY_ERR
6915 */
6916static LY_ERR
aPiecek8b0cc152021-05-31 16:40:31 +02006917moveto_attr(struct lyxp_set *set, const struct lys_module *mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006918{
Michal Vasko9f96a052020-03-10 09:41:45 +01006919 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006920
aPiecek8b0cc152021-05-31 16:40:31 +02006921 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006922 return LY_SUCCESS;
6923 }
6924
6925 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006926 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006927 return LY_EVALID;
6928 }
6929
Radek Krejci1deb5be2020-08-26 16:43:36 +02006930 for (uint32_t i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02006931 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006932
6933 /* only attributes of an elem (not dummy) can be in the result, skip all the rest;
6934 * our attributes are always qualified */
Michal Vasko5c4e5892019-11-14 12:31:38 +01006935 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01006936 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006937
6938 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02006939 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006940 continue;
6941 }
6942
Michal Vaskod3678892020-05-21 10:06:58 +02006943 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006944 /* match */
6945 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01006946 set->val.meta[i].meta = sub;
6947 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006948 /* pos does not change */
6949 replaced = 1;
6950 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01006951 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 +02006952 }
6953 ++i;
6954 }
6955 }
6956 }
6957
6958 if (!replaced) {
6959 /* no match */
6960 set_remove_node(set, i);
6961 }
6962 }
6963
6964 return LY_SUCCESS;
6965}
6966
6967/**
6968 * @brief Move context @p set1 to union with @p set2. @p set2 is emptied afterwards.
Michal Vasko61ac2f62020-05-25 12:39:51 +02006969 * Result is LYXP_SET_NODE_SET. Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006970 *
6971 * @param[in,out] set1 Set to use for the result.
6972 * @param[in] set2 Set that is copied to @p set1.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006973 * @return LY_ERR
6974 */
6975static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006976moveto_union(struct lyxp_set *set1, struct lyxp_set *set2)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006977{
6978 LY_ERR rc;
6979
Michal Vaskod3678892020-05-21 10:06:58 +02006980 if ((set1->type != LYXP_SET_NODE_SET) || (set2->type != LYXP_SET_NODE_SET)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006981 LOGVAL(set1->ctx, LY_VCODE_XP_INOP_2, "union", print_set_type(set1), print_set_type(set2));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006982 return LY_EVALID;
6983 }
6984
6985 /* set2 is empty or both set1 and set2 */
Michal Vaskod3678892020-05-21 10:06:58 +02006986 if (!set2->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006987 return LY_SUCCESS;
6988 }
6989
Michal Vaskod3678892020-05-21 10:06:58 +02006990 if (!set1->used) {
aPiecekadc1e4f2021-10-07 11:15:12 +02006991 /* release hidden allocated data (lyxp_set.size) */
6992 lyxp_set_free_content(set1);
6993 /* direct copying of the entire structure */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006994 memcpy(set1, set2, sizeof *set1);
6995 /* dynamic memory belongs to set1 now, do not free */
Michal Vaskod3678892020-05-21 10:06:58 +02006996 memset(set2, 0, sizeof *set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006997 return LY_SUCCESS;
6998 }
6999
7000 /* we assume sets are sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007001 assert(!set_sort(set1) && !set_sort(set2));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007002
7003 /* sort, remove duplicates */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007004 rc = set_sorted_merge(set1, set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007005 LY_CHECK_RET(rc);
7006
7007 /* final set must be sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007008 assert(!set_sort(set1));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007009
7010 return LY_SUCCESS;
7011}
7012
7013/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02007014 * @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 +02007015 * Context position aware.
7016 *
7017 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02007018 * @param[in] mod Matching metadata module, NULL for any.
7019 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
Michal Vaskocdad7122020-11-09 21:04:44 +01007020 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007021 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7022 */
7023static int
Michal Vaskocdad7122020-11-09 21:04:44 +01007024moveto_attr_alldesc(struct lyxp_set *set, const struct lys_module *mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007025{
Michal Vasko9f96a052020-03-10 09:41:45 +01007026 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007027 struct lyxp_set *set_all_desc = NULL;
7028 LY_ERR rc;
7029
aPiecek8b0cc152021-05-31 16:40:31 +02007030 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007031 return LY_SUCCESS;
7032 }
7033
7034 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01007035 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007036 return LY_EVALID;
7037 }
7038
Michal Vasko03ff5a72019-09-11 13:49:33 +02007039 /* can be optimized similarly to moveto_node_alldesc() and save considerable amount of memory,
7040 * but it likely won't be used much, so it's a waste of time */
7041 /* copy the context */
7042 set_all_desc = set_copy(set);
7043 /* get all descendant nodes (the original context nodes are removed) */
Michal Vasko49fec8e2022-05-24 10:28:33 +02007044 rc = moveto_node_alldesc_child(set_all_desc, NULL, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007045 if (rc != LY_SUCCESS) {
7046 lyxp_set_free(set_all_desc);
7047 return rc;
7048 }
7049 /* prepend the original context nodes */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007050 rc = moveto_union(set, set_all_desc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007051 if (rc != LY_SUCCESS) {
7052 lyxp_set_free(set_all_desc);
7053 return rc;
7054 }
7055 lyxp_set_free(set_all_desc);
7056
Radek Krejci1deb5be2020-08-26 16:43:36 +02007057 for (uint32_t i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02007058 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007059
7060 /* only attributes of an elem can be in the result, skip all the rest,
7061 * we have all attributes qualified in lyd tree */
7062 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01007063 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007064 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02007065 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007066 continue;
7067 }
7068
Michal Vaskod3678892020-05-21 10:06:58 +02007069 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007070 /* match */
7071 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01007072 set->val.meta[i].meta = sub;
7073 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007074 /* pos does not change */
7075 replaced = 1;
7076 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01007077 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 +02007078 }
7079 ++i;
7080 }
7081 }
7082 }
7083
7084 if (!replaced) {
7085 /* no match */
7086 set_remove_node(set, i);
7087 }
7088 }
7089
7090 return LY_SUCCESS;
7091}
7092
7093/**
Michal Vasko8abcecc2022-07-28 09:55:01 +02007094 * @brief Move context @p set1 single item to the result of a comparison.
7095 *
7096 * @param[in] set1 First set with the item to compare.
7097 * @param[in] idx1 Index of the item in @p set1.
7098 * @param[in] set2 Second set.
7099 * @param[in] op Comparison operator to process.
7100 * @param[in] switch_operands Whether to switch sets as operands; whether it is `set1 op set2` or `set2 op set1`.
7101 * @param[out] result Result of the comparison.
7102 * @return LY_ERR value.
7103 */
7104static LY_ERR
7105moveto_op_comp_item(const struct lyxp_set *set1, uint32_t idx1, struct lyxp_set *set2, const char *op,
7106 ly_bool switch_operands, ly_bool *result)
7107{
7108 struct lyxp_set tmp1 = {0};
7109 LY_ERR rc = LY_SUCCESS;
7110
7111 assert(set1->type == LYXP_SET_NODE_SET);
7112
7113 /* cast set1 */
7114 switch (set2->type) {
7115 case LYXP_SET_NUMBER:
7116 rc = set_comp_cast(&tmp1, set1, LYXP_SET_NUMBER, idx1);
7117 break;
7118 case LYXP_SET_BOOLEAN:
7119 rc = set_comp_cast(&tmp1, set1, LYXP_SET_BOOLEAN, idx1);
7120 break;
7121 default:
7122 rc = set_comp_cast(&tmp1, set1, LYXP_SET_STRING, idx1);
7123 break;
7124 }
7125 LY_CHECK_GOTO(rc, cleanup);
7126
7127 /* canonize set2 */
7128 LY_CHECK_GOTO(rc = set_comp_canonize(set2, &set1->val.nodes[idx1]), cleanup);
7129
7130 /* compare recursively and store the result */
7131 if (switch_operands) {
7132 LY_CHECK_GOTO(rc = moveto_op_comp(set2, &tmp1, op), cleanup);
7133 *result = set2->val.bln;
7134 } else {
7135 LY_CHECK_GOTO(rc = moveto_op_comp(&tmp1, set2, op), cleanup);
7136 *result = tmp1.val.bln;
7137 }
7138
7139cleanup:
7140 lyxp_set_free_content(&tmp1);
7141 return rc;
7142}
7143
7144/**
7145 * @brief Move context @p set1 to the result of a comparison. Handles '=', '!=', '<=', '<', '>=', or '>'.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007146 * Result is LYXP_SET_BOOLEAN. Indirectly context position aware.
7147 *
7148 * @param[in,out] set1 Set to use for the result.
7149 * @param[in] set2 Set acting as the second operand for @p op.
7150 * @param[in] op Comparison operator to process.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007151 * @return LY_ERR
7152 */
7153static LY_ERR
Michal Vasko8abcecc2022-07-28 09:55:01 +02007154moveto_op_comp(struct lyxp_set *set1, struct lyxp_set *set2, const char *op)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007155{
7156 /*
7157 * NODE SET + NODE SET = NODE SET + STRING /(1 NODE SET) 2 STRING
7158 * NODE SET + STRING = STRING + STRING /1 STRING (2 STRING)
7159 * NODE SET + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
7160 * NODE SET + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
7161 * STRING + NODE SET = STRING + STRING /(1 STRING) 2 STRING
7162 * NUMBER + NODE SET = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
7163 * BOOLEAN + NODE SET = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
7164 *
7165 * '=' or '!='
7166 * BOOLEAN + BOOLEAN
7167 * BOOLEAN + STRING = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
7168 * BOOLEAN + NUMBER = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
7169 * STRING + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
7170 * NUMBER + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
7171 * NUMBER + NUMBER
7172 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
7173 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
7174 * STRING + STRING
7175 *
7176 * '<=', '<', '>=', '>'
7177 * NUMBER + NUMBER
7178 * BOOLEAN + BOOLEAN = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
7179 * BOOLEAN + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
7180 * BOOLEAN + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
7181 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
7182 * STRING + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
7183 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
7184 * NUMBER + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
7185 * STRING + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
7186 */
Michal Vasko8abcecc2022-07-28 09:55:01 +02007187 ly_bool result;
7188 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007189 LY_ERR rc;
7190
Michal Vasko03ff5a72019-09-11 13:49:33 +02007191 /* iterative evaluation with node-sets */
7192 if ((set1->type == LYXP_SET_NODE_SET) || (set2->type == LYXP_SET_NODE_SET)) {
7193 if (set1->type == LYXP_SET_NODE_SET) {
7194 for (i = 0; i < set1->used; ++i) {
Michal Vasko8abcecc2022-07-28 09:55:01 +02007195 /* evaluate for the single item */
7196 LY_CHECK_RET(moveto_op_comp_item(set1, i, set2, op, 0, &result));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007197
7198 /* lazy evaluation until true */
Michal Vasko8abcecc2022-07-28 09:55:01 +02007199 if (result) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007200 set_fill_boolean(set1, 1);
7201 return LY_SUCCESS;
7202 }
7203 }
7204 } else {
7205 for (i = 0; i < set2->used; ++i) {
Michal Vasko8abcecc2022-07-28 09:55:01 +02007206 /* evaluate for the single item */
7207 LY_CHECK_RET(moveto_op_comp_item(set2, i, set1, op, 1, &result));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007208
7209 /* lazy evaluation until true */
Michal Vasko8abcecc2022-07-28 09:55:01 +02007210 if (result) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007211 set_fill_boolean(set1, 1);
7212 return LY_SUCCESS;
7213 }
7214 }
7215 }
7216
Michal Vasko8abcecc2022-07-28 09:55:01 +02007217 /* false for all the nodes */
Michal Vasko03ff5a72019-09-11 13:49:33 +02007218 set_fill_boolean(set1, 0);
7219 return LY_SUCCESS;
7220 }
7221
7222 /* first convert properly */
7223 if ((op[0] == '=') || (op[0] == '!')) {
7224 if ((set1->type == LYXP_SET_BOOLEAN) || (set2->type == LYXP_SET_BOOLEAN)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007225 lyxp_set_cast(set1, LYXP_SET_BOOLEAN);
7226 lyxp_set_cast(set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007227 } else if ((set1->type == LYXP_SET_NUMBER) || (set2->type == LYXP_SET_NUMBER)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007228 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007229 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007230 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007231 LY_CHECK_RET(rc);
7232 } /* else we have 2 strings */
7233 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007234 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007235 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007236 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007237 LY_CHECK_RET(rc);
7238 }
7239
7240 assert(set1->type == set2->type);
7241
7242 /* compute result */
7243 if (op[0] == '=') {
7244 if (set1->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +02007245 result = (set1->val.bln == set2->val.bln);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007246 } else if (set1->type == LYXP_SET_NUMBER) {
7247 result = (set1->val.num == set2->val.num);
7248 } else {
7249 assert(set1->type == LYXP_SET_STRING);
Michal Vaskoac6c72f2019-11-14 16:09:34 +01007250 result = !strcmp(set1->val.str, set2->val.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007251 }
7252 } else if (op[0] == '!') {
7253 if (set1->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +02007254 result = (set1->val.bln != set2->val.bln);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007255 } else if (set1->type == LYXP_SET_NUMBER) {
7256 result = (set1->val.num != set2->val.num);
7257 } else {
7258 assert(set1->type == LYXP_SET_STRING);
Michal Vaskoc2058432020-11-06 17:26:21 +01007259 result = strcmp(set1->val.str, set2->val.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007260 }
7261 } else {
7262 assert(set1->type == LYXP_SET_NUMBER);
7263 if (op[0] == '<') {
7264 if (op[1] == '=') {
7265 result = (set1->val.num <= set2->val.num);
7266 } else {
7267 result = (set1->val.num < set2->val.num);
7268 }
7269 } else {
7270 if (op[1] == '=') {
7271 result = (set1->val.num >= set2->val.num);
7272 } else {
7273 result = (set1->val.num > set2->val.num);
7274 }
7275 }
7276 }
7277
7278 /* assign result */
7279 if (result) {
7280 set_fill_boolean(set1, 1);
7281 } else {
7282 set_fill_boolean(set1, 0);
7283 }
7284
7285 return LY_SUCCESS;
7286}
7287
7288/**
7289 * @brief Move context @p set to the result of a basic operation. Handles '+', '-', unary '-', '*', 'div',
7290 * or 'mod'. Result is LYXP_SET_NUMBER. Indirectly context position aware.
7291 *
7292 * @param[in,out] set1 Set to use for the result.
7293 * @param[in] set2 Set acting as the second operand for @p op.
7294 * @param[in] op Operator to process.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007295 * @return LY_ERR
7296 */
7297static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007298moveto_op_math(struct lyxp_set *set1, struct lyxp_set *set2, const char *op)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007299{
7300 LY_ERR rc;
7301
7302 /* unary '-' */
7303 if (!set2 && (op[0] == '-')) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007304 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007305 LY_CHECK_RET(rc);
7306 set1->val.num *= -1;
7307 lyxp_set_free(set2);
7308 return LY_SUCCESS;
7309 }
7310
7311 assert(set1 && set2);
7312
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007313 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007314 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007315 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007316 LY_CHECK_RET(rc);
7317
7318 switch (op[0]) {
7319 /* '+' */
7320 case '+':
7321 set1->val.num += set2->val.num;
7322 break;
7323
7324 /* '-' */
7325 case '-':
7326 set1->val.num -= set2->val.num;
7327 break;
7328
7329 /* '*' */
7330 case '*':
7331 set1->val.num *= set2->val.num;
7332 break;
7333
7334 /* 'div' */
7335 case 'd':
7336 set1->val.num /= set2->val.num;
7337 break;
7338
7339 /* 'mod' */
7340 case 'm':
7341 set1->val.num = ((long long)set1->val.num) % ((long long)set2->val.num);
7342 break;
7343
7344 default:
7345 LOGINT_RET(set1->ctx);
7346 }
7347
7348 return LY_SUCCESS;
7349}
7350
Michal Vasko03ff5a72019-09-11 13:49:33 +02007351/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02007352 * @brief Evaluate Predicate. Logs directly on error.
7353 *
Michal Vaskod3678892020-05-21 10:06:58 +02007354 * [9] Predicate ::= '[' Expr ']'
Michal Vasko03ff5a72019-09-11 13:49:33 +02007355 *
7356 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007357 * @param[in] tok_idx Position in the expression @p exp.
aPiecek8b0cc152021-05-31 16:40:31 +02007358 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007359 * @param[in] options XPath options.
Michal Vasko49fec8e2022-05-24 10:28:33 +02007360 * @param[in] axis Axis to search on.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007361 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7362 */
7363static LY_ERR
Michal Vasko49fec8e2022-05-24 10:28:33 +02007364eval_predicate(const struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set, uint32_t options, enum lyxp_axis axis)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007365{
7366 LY_ERR rc;
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01007367 uint16_t orig_exp;
7368 uint32_t i, orig_pos, orig_size;
Michal Vasko5c4e5892019-11-14 12:31:38 +01007369 int32_t pred_in_ctx;
Michal Vasko88a9e802022-05-24 10:50:28 +02007370 ly_bool reverse_axis = 0;
aPiecekfff4dca2021-10-07 10:59:53 +02007371 struct lyxp_set set2 = {0};
Michal Vasko03ff5a72019-09-11 13:49:33 +02007372
7373 /* '[' */
aPiecek8b0cc152021-05-31 16:40:31 +02007374 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02007375 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007376 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007377
aPiecek8b0cc152021-05-31 16:40:31 +02007378 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007379only_parse:
aPiecek8b0cc152021-05-31 16:40:31 +02007380 rc = eval_expr_select(exp, tok_idx, 0, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007381 LY_CHECK_RET(rc);
7382 } else if (set->type == LYXP_SET_NODE_SET) {
7383 /* 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 +01007384 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007385
7386 /* empty set, nothing to evaluate */
7387 if (!set->used) {
7388 goto only_parse;
7389 }
7390
Michal Vasko49fec8e2022-05-24 10:28:33 +02007391 /* decide forward or reverse axis */
7392 switch (axis) {
7393 case LYXP_AXIS_ANCESTOR:
7394 case LYXP_AXIS_ANCESTOR_OR_SELF:
7395 case LYXP_AXIS_PRECEDING:
7396 case LYXP_AXIS_PRECEDING_SIBLING:
7397 reverse_axis = 1;
7398 break;
7399 case LYXP_AXIS_DESCENDANT:
7400 case LYXP_AXIS_DESCENDANT_OR_SELF:
7401 case LYXP_AXIS_FOLLOWING:
7402 case LYXP_AXIS_FOLLOWING_SIBLING:
7403 case LYXP_AXIS_PARENT:
7404 case LYXP_AXIS_CHILD:
7405 case LYXP_AXIS_SELF:
7406 case LYXP_AXIS_ATTRIBUTE:
7407 reverse_axis = 0;
7408 break;
7409 }
7410
Michal Vasko004d3152020-06-11 19:59:22 +02007411 orig_exp = *tok_idx;
Michal Vasko49fec8e2022-05-24 10:28:33 +02007412 orig_pos = reverse_axis ? set->used + 1 : 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007413 orig_size = set->used;
Michal Vasko39dbf352020-05-21 10:08:59 +02007414 for (i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007415 set_init(&set2, set);
7416 set_insert_node(&set2, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, 0);
Michal Vasko49fec8e2022-05-24 10:28:33 +02007417
7418 /* remember the node context position for position() and context size for last() */
7419 orig_pos += reverse_axis ? -1 : 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007420
7421 set2.ctx_pos = orig_pos;
7422 set2.ctx_size = orig_size;
Michal Vasko004d3152020-06-11 19:59:22 +02007423 *tok_idx = orig_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007424
Michal Vasko004d3152020-06-11 19:59:22 +02007425 rc = eval_expr_select(exp, tok_idx, 0, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007426 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02007427 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007428 return rc;
7429 }
7430
Michal Vasko49fec8e2022-05-24 10:28:33 +02007431 /* number is a proximity position */
Michal Vasko03ff5a72019-09-11 13:49:33 +02007432 if (set2.type == LYXP_SET_NUMBER) {
7433 if ((long long)set2.val.num == orig_pos) {
7434 set2.val.num = 1;
7435 } else {
7436 set2.val.num = 0;
7437 }
7438 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007439 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007440
7441 /* predicate satisfied or not? */
Michal Vasko004d3152020-06-11 19:59:22 +02007442 if (!set2.val.bln) {
Michal Vasko2caefc12019-11-14 16:07:56 +01007443 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007444 }
7445 }
Michal Vasko2caefc12019-11-14 16:07:56 +01007446 set_remove_nodes_none(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007447
7448 } else if (set->type == LYXP_SET_SCNODE_SET) {
7449 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01007450 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007451 /* there is a currently-valid node */
7452 break;
7453 }
7454 }
7455 /* empty set, nothing to evaluate */
7456 if (i == set->used) {
7457 goto only_parse;
7458 }
7459
Michal Vasko004d3152020-06-11 19:59:22 +02007460 orig_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007461
Michal Vasko03ff5a72019-09-11 13:49:33 +02007462 /* set special in_ctx to all the valid snodes */
7463 pred_in_ctx = set_scnode_new_in_ctx(set);
7464
7465 /* use the valid snodes one-by-one */
7466 for (i = 0; i < set->used; ++i) {
7467 if (set->val.scnodes[i].in_ctx != pred_in_ctx) {
7468 continue;
7469 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01007470 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007471
Michal Vasko004d3152020-06-11 19:59:22 +02007472 *tok_idx = orig_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007473
Michal Vasko004d3152020-06-11 19:59:22 +02007474 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007475 LY_CHECK_RET(rc);
7476
7477 set->val.scnodes[i].in_ctx = pred_in_ctx;
7478 }
7479
7480 /* restore the state as it was before the predicate */
7481 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01007482 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007483 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007484 } else if (set->val.scnodes[i].in_ctx == pred_in_ctx) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01007485 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007486 }
7487 }
7488
7489 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02007490 set2.type = LYXP_SET_NODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007491 set_fill_set(&set2, set);
7492
Michal Vasko004d3152020-06-11 19:59:22 +02007493 rc = eval_expr_select(exp, tok_idx, 0, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007494 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02007495 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007496 return rc;
7497 }
7498
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007499 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +02007500 if (!set2.val.bln) {
Michal Vaskod3678892020-05-21 10:06:58 +02007501 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007502 }
Michal Vaskod3678892020-05-21 10:06:58 +02007503 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007504 }
7505
7506 /* ']' */
Michal Vasko004d3152020-06-11 19:59:22 +02007507 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_BRACK2);
aPiecek8b0cc152021-05-31 16:40:31 +02007508 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02007509 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007510 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007511
7512 return LY_SUCCESS;
7513}
7514
7515/**
Michal Vaskod3678892020-05-21 10:06:58 +02007516 * @brief Evaluate Literal. Logs directly on error.
7517 *
7518 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007519 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02007520 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7521 */
7522static void
Michal Vasko40308e72020-10-20 16:38:40 +02007523eval_literal(const struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set)
Michal Vaskod3678892020-05-21 10:06:58 +02007524{
7525 if (set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007526 if (exp->tok_len[*tok_idx] == 2) {
Michal Vaskod3678892020-05-21 10:06:58 +02007527 set_fill_string(set, "", 0);
7528 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007529 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 +02007530 }
7531 }
7532 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02007533 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007534 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007535}
7536
7537/**
Michal Vasko004d3152020-06-11 19:59:22 +02007538 * @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 +02007539 *
Michal Vasko004d3152020-06-11 19:59:22 +02007540 * @param[in] exp Full parsed XPath expression.
7541 * @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 +02007542 * @param[in] ctx_node Found schema node as the context for the predicate.
7543 * @param[in] cur_mod Current module for the expression.
7544 * @param[in] cur_node Current (original context) node.
Michal Vasko004d3152020-06-11 19:59:22 +02007545 * @param[in] format Format of any prefixes in key names/values.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007546 * @param[in] prefix_data Format-specific prefix data (see ::ly_resolve_prefix).
Michal Vasko004d3152020-06-11 19:59:22 +02007547 * @param[out] predicates Parsed predicates.
7548 * @param[out] pred_type Type of @p predicates.
7549 * @return LY_SUCCESS on success,
7550 * @return LY_ERR on any error.
Michal Vaskod3678892020-05-21 10:06:58 +02007551 */
7552static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007553eval_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 +02007554 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 +02007555 struct ly_path_predicate **predicates, enum ly_path_pred_type *pred_type)
Michal Vaskod3678892020-05-21 10:06:58 +02007556{
Michal Vasko004d3152020-06-11 19:59:22 +02007557 LY_ERR ret = LY_SUCCESS;
7558 uint16_t key_count, e_idx, pred_idx = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02007559 const struct lysc_node *key;
Michal Vasko004d3152020-06-11 19:59:22 +02007560 size_t pred_len;
Radek Krejci1deb5be2020-08-26 16:43:36 +02007561 uint32_t prev_lo;
Michal Vasko004d3152020-06-11 19:59:22 +02007562 struct lyxp_expr *exp2 = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02007563
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007564 assert(ctx_node->nodetype & (LYS_LIST | LYS_LEAFLIST));
Michal Vaskod3678892020-05-21 10:06:58 +02007565
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007566 if (ctx_node->nodetype == LYS_LIST) {
Michal Vasko004d3152020-06-11 19:59:22 +02007567 /* get key count */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007568 if (ctx_node->flags & LYS_KEYLESS) {
Michal Vasko004d3152020-06-11 19:59:22 +02007569 return LY_EINVAL;
7570 }
Michal Vasko544e58a2021-01-28 14:33:41 +01007571 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 +02007572 assert(key_count);
Michal Vaskod3678892020-05-21 10:06:58 +02007573
Michal Vasko004d3152020-06-11 19:59:22 +02007574 /* learn where the predicates end */
7575 e_idx = *tok_idx;
7576 while (key_count) {
7577 /* '[' */
7578 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
7579 return LY_EINVAL;
7580 }
7581 ++e_idx;
7582
Michal Vasko3354d272021-04-06 09:40:06 +02007583 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_NAMETEST)) {
7584 /* definitely not a key */
7585 return LY_EINVAL;
7586 }
7587
Michal Vasko004d3152020-06-11 19:59:22 +02007588 /* ']' */
7589 while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) {
7590 ++e_idx;
7591 }
7592 ++e_idx;
7593
7594 /* another presumably key predicate parsed */
7595 --key_count;
7596 }
Michal Vasko004d3152020-06-11 19:59:22 +02007597 } else {
7598 /* learn just where this single predicate ends */
7599 e_idx = *tok_idx;
7600
Michal Vaskod3678892020-05-21 10:06:58 +02007601 /* '[' */
Michal Vasko004d3152020-06-11 19:59:22 +02007602 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
7603 return LY_EINVAL;
7604 }
7605 ++e_idx;
Michal Vaskod3678892020-05-21 10:06:58 +02007606
Michal Vasko3354d272021-04-06 09:40:06 +02007607 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_DOT)) {
7608 /* definitely not the value */
7609 return LY_EINVAL;
7610 }
7611
Michal Vaskod3678892020-05-21 10:06:58 +02007612 /* ']' */
Michal Vasko004d3152020-06-11 19:59:22 +02007613 while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) {
7614 ++e_idx;
7615 }
7616 ++e_idx;
Michal Vaskod3678892020-05-21 10:06:58 +02007617 }
7618
Michal Vasko004d3152020-06-11 19:59:22 +02007619 /* get the joined length of all the keys predicates/of the single leaf-list predicate */
7620 pred_len = (exp->tok_pos[e_idx - 1] + exp->tok_len[e_idx - 1]) - exp->tok_pos[*tok_idx];
7621
7622 /* turn logging off */
7623 prev_lo = ly_log_options(0);
7624
7625 /* parse the predicate(s) */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007626 LY_CHECK_GOTO(ret = ly_path_parse_predicate(ctx_node->module->ctx, ctx_node, exp->expr + exp->tok_pos[*tok_idx],
7627 pred_len, LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &exp2), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02007628
7629 /* compile */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007630 ret = ly_path_compile_predicate(ctx_node->module->ctx, cur_node, cur_mod, ctx_node, exp2, &pred_idx, format,
7631 prefix_data, predicates, pred_type);
Michal Vasko004d3152020-06-11 19:59:22 +02007632 LY_CHECK_GOTO(ret, cleanup);
7633
7634 /* success, the predicate must include all the needed information for hash-based search */
7635 *tok_idx = e_idx;
7636
7637cleanup:
7638 ly_log_options(prev_lo);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007639 lyxp_expr_free(ctx_node->module->ctx, exp2);
Michal Vasko004d3152020-06-11 19:59:22 +02007640 return ret;
Michal Vaskod3678892020-05-21 10:06:58 +02007641}
7642
7643/**
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007644 * @brief Search for/check the next schema node that could be the only matching schema node meaning the
7645 * data node(s) could be found using a single hash-based search.
7646 *
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007647 * @param[in] ctx libyang context.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007648 * @param[in] node Next context node to check.
7649 * @param[in] name Expected node name.
7650 * @param[in] name_len Length of @p name.
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007651 * @param[in] moveto_mod Expected node module, can be NULL for JSON format with no prefix.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007652 * @param[in] root_type XPath root type.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007653 * @param[in] format Prefix format.
7654 * @param[in,out] found Previously found node, is updated.
7655 * @return LY_SUCCESS on success,
7656 * @return LY_ENOT if the whole check failed and hashes cannot be used.
7657 */
7658static LY_ERR
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007659eval_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 +02007660 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 +02007661 const struct lysc_node **found)
7662{
7663 const struct lysc_node *scnode;
7664 const struct lys_module *mod;
7665 uint32_t idx = 0;
7666
Radek Krejci8df109d2021-04-23 12:19:08 +02007667 assert((format == LY_VALUE_JSON) || moveto_mod);
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007668
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007669continue_search:
Michal Vasko7d1d0912020-10-16 08:38:30 +02007670 scnode = NULL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007671 if (!node) {
Radek Krejci8df109d2021-04-23 12:19:08 +02007672 if ((format == LY_VALUE_JSON) && !moveto_mod) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007673 /* search all modules for a single match */
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007674 while ((mod = ly_ctx_get_module_iter(ctx, &idx))) {
Michal Vasko35a3b1d2021-07-14 09:34:16 +02007675 if (!mod->implemented) {
7676 continue;
7677 }
7678
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007679 scnode = lys_find_child(NULL, mod, name, name_len, 0, 0);
7680 if (scnode) {
7681 /* we have found a match */
7682 break;
7683 }
7684 }
7685
Michal Vasko7d1d0912020-10-16 08:38:30 +02007686 if (!scnode) {
7687 /* all modules searched */
7688 idx = 0;
7689 }
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007690 } else {
7691 /* search in top-level */
7692 scnode = lys_find_child(NULL, moveto_mod, name, name_len, 0, 0);
7693 }
7694 } else if (!*found || (lysc_data_parent(*found) != node->schema)) {
Radek Krejci8df109d2021-04-23 12:19:08 +02007695 if ((format == LY_VALUE_JSON) && !moveto_mod) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007696 /* we must adjust the module to inherit the one from the context node */
7697 moveto_mod = node->schema->module;
7698 }
7699
7700 /* search in children, do not repeat the same search */
7701 scnode = lys_find_child(node->schema, moveto_mod, name, name_len, 0, 0);
Michal Vasko7d1d0912020-10-16 08:38:30 +02007702 } /* else skip redundant search */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007703
7704 /* additional context check */
7705 if (scnode && (root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) {
7706 scnode = NULL;
7707 }
7708
7709 if (scnode) {
7710 if (*found) {
7711 /* we found a schema node with the same name but at different level, give up, too complicated
7712 * (more hash-based searches would be required, not supported) */
7713 return LY_ENOT;
7714 } else {
7715 /* remember the found schema node and continue to make sure it can be used */
7716 *found = scnode;
7717 }
7718 }
7719
7720 if (idx) {
7721 /* continue searching all the following models */
7722 goto continue_search;
7723 }
7724
7725 return LY_SUCCESS;
7726}
7727
7728/**
Michal Vasko4ad69e72021-10-26 16:25:55 +02007729 * @brief Generate message when no matching schema nodes were found for a path segment.
7730 *
7731 * @param[in] set XPath set.
7732 * @param[in] scparent Previous schema parent in the context, if only one.
7733 * @param[in] ncname XPath NCName being evaluated.
7734 * @param[in] ncname_len Length of @p ncname.
7735 * @param[in] expr Whole XPath expression.
7736 * @param[in] options XPath options.
7737 */
7738static void
7739eval_name_test_scnode_no_match_msg(struct lyxp_set *set, const struct lyxp_set_scnode *scparent, const char *ncname,
7740 uint16_t ncname_len, const char *expr, uint32_t options)
7741{
7742 const char *format;
7743 char *path = NULL, *ppath = NULL;
7744
7745 path = lysc_path(set->cur_scnode, LYSC_PATH_LOG, NULL, 0);
7746 if (scparent) {
7747 /* generate path for the parent */
7748 if (scparent->type == LYXP_NODE_ELEM) {
7749 ppath = lysc_path(scparent->scnode, LYSC_PATH_LOG, NULL, 0);
7750 } else if (scparent->type == LYXP_NODE_ROOT) {
7751 ppath = strdup("<root>");
7752 } else if (scparent->type == LYXP_NODE_ROOT_CONFIG) {
7753 ppath = strdup("<config-root>");
7754 }
7755 }
7756 if (ppath) {
7757 format = "Schema node \"%.*s\" for parent \"%s\" not found; in expr \"%.*s\" with context node \"%s\".";
7758 if (options & LYXP_SCNODE_ERROR) {
7759 LOGERR(set->ctx, LY_EVALID, format, ncname_len, ncname, ppath, (ncname - expr) + ncname_len, expr, path);
7760 } else {
7761 LOGWRN(set->ctx, format, ncname_len, ncname, ppath, (ncname - expr) + ncname_len, expr, path);
7762 }
7763 } else {
7764 format = "Schema node \"%.*s\" not found; in expr \"%.*s\" with context node \"%s\".";
7765 if (options & LYXP_SCNODE_ERROR) {
7766 LOGERR(set->ctx, LY_EVALID, format, ncname_len, ncname, (ncname - expr) + ncname_len, expr, path);
7767 } else {
7768 LOGWRN(set->ctx, format, ncname_len, ncname, (ncname - expr) + ncname_len, expr, path);
7769 }
7770 }
7771 free(path);
7772 free(ppath);
7773}
7774
7775/**
Michal Vaskod3678892020-05-21 10:06:58 +02007776 * @brief Evaluate NameTest and any following Predicates. Logs directly on error.
7777 *
7778 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
7779 * [6] NodeTest ::= NameTest | NodeType '(' ')'
7780 * [7] NameTest ::= '*' | NCName ':' '*' | QName
7781 *
7782 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007783 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko49fec8e2022-05-24 10:28:33 +02007784 * @param[in] axis What axis to search on.
Michal Vaskod3678892020-05-21 10:06:58 +02007785 * @param[in] all_desc Whether to search all the descendants or children only.
aPiecek8b0cc152021-05-31 16:40:31 +02007786 * @param[in,out] set Context and result set.
Michal Vaskod3678892020-05-21 10:06:58 +02007787 * @param[in] options XPath options.
Michal Vaskoa036a6b2021-10-12 16:05:23 +02007788 * @return LY_ERR (LY_EINCOMPLETE on unresolved when, LY_ENOT for not found schema node)
Michal Vaskod3678892020-05-21 10:06:58 +02007789 */
7790static LY_ERR
Michal Vasko49fec8e2022-05-24 10:28:33 +02007791eval_name_test_with_predicate(const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_axis axis, ly_bool all_desc,
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007792 struct lyxp_set *set, uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02007793{
Michal Vaskoa036a6b2021-10-12 16:05:23 +02007794 LY_ERR rc = LY_SUCCESS, r;
Michal Vasko004d3152020-06-11 19:59:22 +02007795 const char *ncname, *ncname_dict = NULL;
7796 uint16_t ncname_len;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007797 const struct lys_module *moveto_mod = NULL;
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007798 const struct lysc_node *scnode = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02007799 struct ly_path_predicate *predicates = NULL;
7800 enum ly_path_pred_type pred_type = 0;
Michal Vaskoa036a6b2021-10-12 16:05:23 +02007801 int scnode_skip_pred = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02007802
aPiecek8b0cc152021-05-31 16:40:31 +02007803 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02007804 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007805 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007806
aPiecek8b0cc152021-05-31 16:40:31 +02007807 if (options & LYXP_SKIP_EXPR) {
Michal Vaskod3678892020-05-21 10:06:58 +02007808 goto moveto;
7809 }
7810
Michal Vasko004d3152020-06-11 19:59:22 +02007811 ncname = &exp->expr[exp->tok_pos[*tok_idx - 1]];
7812 ncname_len = exp->tok_len[*tok_idx - 1];
Michal Vaskod3678892020-05-21 10:06:58 +02007813
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007814 if ((ncname[0] == '*') && (ncname_len == 1)) {
7815 /* all nodes will match */
7816 goto moveto;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007817 }
7818
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007819 /* parse (and skip) module name */
7820 rc = moveto_resolve_model(&ncname, &ncname_len, set, NULL, &moveto_mod);
Michal Vaskod3678892020-05-21 10:06:58 +02007821 LY_CHECK_GOTO(rc, cleanup);
7822
Michal Vasko49fec8e2022-05-24 10:28:33 +02007823 if ((ncname[0] == '*') && (ncname_len == 1)) {
7824 /* all nodes from the module will match */
7825 goto moveto;
7826 }
7827
7828 if (((set->format == LY_VALUE_JSON) || moveto_mod) && (axis == LYXP_AXIS_CHILD) && !all_desc &&
7829 (set->type == LYXP_SET_NODE_SET)) {
Michal Vaskod3678892020-05-21 10:06:58 +02007830 /* find the matching schema node in some parent in the context */
Radek Krejci1deb5be2020-08-26 16:43:36 +02007831 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007832 if (eval_name_test_with_predicate_get_scnode(set->ctx, set->val.nodes[i].node, ncname, ncname_len,
7833 moveto_mod, set->root_type, set->format, &scnode)) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007834 /* check failed */
7835 scnode = NULL;
7836 break;
Michal Vaskod3678892020-05-21 10:06:58 +02007837 }
7838 }
7839
Michal Vasko004d3152020-06-11 19:59:22 +02007840 if (scnode && (scnode->nodetype & (LYS_LIST | LYS_LEAFLIST))) {
7841 /* try to create the predicates */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007842 if (eval_name_test_try_compile_predicates(exp, tok_idx, scnode, set->cur_mod, set->cur_node ?
7843 set->cur_node->schema : NULL, set->format, set->prefix_data, &predicates, &pred_type)) {
Michal Vasko004d3152020-06-11 19:59:22 +02007844 /* hashes cannot be used */
Michal Vaskod3678892020-05-21 10:06:58 +02007845 scnode = NULL;
7846 }
7847 }
7848 }
7849
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007850 if (!scnode) {
7851 /* we are not able to match based on a schema node and not all the modules match ("*"),
Michal Vasko004d3152020-06-11 19:59:22 +02007852 * use dictionary for efficient comparison */
Radek Krejci011e4aa2020-09-04 15:22:31 +02007853 LY_CHECK_GOTO(rc = lydict_insert(set->ctx, ncname, ncname_len, &ncname_dict), cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02007854 }
7855
7856moveto:
7857 /* move to the attribute(s), data node(s), or schema node(s) */
Michal Vasko49fec8e2022-05-24 10:28:33 +02007858 if (axis == LYXP_AXIS_ATTRIBUTE) {
aPiecek8b0cc152021-05-31 16:40:31 +02007859 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007860 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskod3678892020-05-21 10:06:58 +02007861 } else {
7862 if (all_desc) {
Michal Vaskocdad7122020-11-09 21:04:44 +01007863 rc = moveto_attr_alldesc(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007864 } else {
aPiecek8b0cc152021-05-31 16:40:31 +02007865 rc = moveto_attr(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007866 }
7867 LY_CHECK_GOTO(rc, cleanup);
7868 }
7869 } else {
aPiecek8b0cc152021-05-31 16:40:31 +02007870 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02007871 int64_t i;
Michal Vaskoa036a6b2021-10-12 16:05:23 +02007872 const struct lyxp_set_scnode *scparent = NULL;
Michal Vaskoa036a6b2021-10-12 16:05:23 +02007873
7874 /* remember parent if there is only one, to print in the warning */
7875 for (i = 0; i < set->used; ++i) {
7876 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
7877 if (!scparent) {
7878 /* remember the context node */
7879 scparent = &set->val.scnodes[i];
7880 } else {
7881 /* several context nodes, no reasonable error possible */
7882 scparent = NULL;
7883 break;
7884 }
7885 }
7886 }
Radek Krejci1deb5be2020-08-26 16:43:36 +02007887
Michal Vasko49fec8e2022-05-24 10:28:33 +02007888 if (all_desc && (axis == LYXP_AXIS_CHILD)) {
7889 /* efficient evaluation that does not add all the descendants into the set */
7890 rc = moveto_scnode_alldesc_child(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007891 } else {
Michal Vasko49fec8e2022-05-24 10:28:33 +02007892 if (all_desc) {
7893 /* "//" == "/descendant-or-self::node()/" */
7894 rc = xpath_pi_node(set, LYXP_AXIS_DESCENDANT_OR_SELF, options);
7895 LY_CHECK_GOTO(rc, cleanup);
7896 }
7897 rc = moveto_scnode(set, moveto_mod, ncname_dict, axis, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007898 }
7899 LY_CHECK_GOTO(rc, cleanup);
7900
7901 for (i = set->used - 1; i > -1; --i) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007902 if (set->val.scnodes[i].in_ctx > LYXP_SET_SCNODE_ATOM_NODE) {
Michal Vaskod3678892020-05-21 10:06:58 +02007903 break;
7904 }
7905 }
7906 if (i == -1) {
Michal Vasko4ad69e72021-10-26 16:25:55 +02007907 /* generate message */
7908 eval_name_test_scnode_no_match_msg(set, scparent, ncname, ncname_len, exp->expr, options);
7909
7910 if (options & LYXP_SCNODE_ERROR) {
7911 /* error */
7912 rc = LY_EVALID;
7913 goto cleanup;
Michal Vaskoa036a6b2021-10-12 16:05:23 +02007914 }
Michal Vaskoa036a6b2021-10-12 16:05:23 +02007915
7916 /* skip the predicates and the rest of this path to not generate invalid warnings */
7917 rc = LY_ENOT;
7918 scnode_skip_pred = 1;
Michal Vaskod3678892020-05-21 10:06:58 +02007919 }
7920 } else {
Michal Vasko49fec8e2022-05-24 10:28:33 +02007921 if (all_desc && (axis == LYXP_AXIS_CHILD)) {
7922 /* efficient evaluation */
7923 rc = moveto_node_alldesc_child(set, moveto_mod, ncname_dict, options);
7924 } else if (scnode && (axis == LYXP_AXIS_CHILD)) {
7925 /* we can find the child nodes using hashes */
7926 rc = moveto_node_hash_child(set, scnode, predicates, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007927 } else {
Michal Vasko49fec8e2022-05-24 10:28:33 +02007928 if (all_desc) {
7929 /* "//" == "/descendant-or-self::node()/" */
7930 rc = xpath_pi_node(set, LYXP_AXIS_DESCENDANT_OR_SELF, options);
7931 LY_CHECK_GOTO(rc, cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02007932 }
Michal Vasko49fec8e2022-05-24 10:28:33 +02007933 rc = moveto_node(set, moveto_mod, ncname_dict, axis, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007934 }
7935 LY_CHECK_GOTO(rc, cleanup);
7936 }
7937 }
7938
Michal Vaskoa036a6b2021-10-12 16:05:23 +02007939 if (scnode_skip_pred) {
7940 /* skip predicates */
7941 options |= LYXP_SKIP_EXPR;
7942 }
7943
Michal Vaskod3678892020-05-21 10:06:58 +02007944 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007945 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02007946 r = eval_predicate(exp, tok_idx, set, options, axis);
Michal Vaskoa036a6b2021-10-12 16:05:23 +02007947 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02007948 }
7949
7950cleanup:
Michal Vaskoa036a6b2021-10-12 16:05:23 +02007951 if (scnode_skip_pred) {
7952 /* restore options */
7953 options &= ~LYXP_SKIP_EXPR;
7954 }
aPiecek8b0cc152021-05-31 16:40:31 +02007955 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko004d3152020-06-11 19:59:22 +02007956 lydict_remove(set->ctx, ncname_dict);
Michal Vaskof7e16e22020-10-21 09:24:39 +02007957 ly_path_predicates_free(set->ctx, pred_type, predicates);
Michal Vaskodb51a8d2020-05-27 15:22:29 +02007958 }
Michal Vaskod3678892020-05-21 10:06:58 +02007959 return rc;
7960}
7961
7962/**
7963 * @brief Evaluate NodeType and any following Predicates. Logs directly on error.
7964 *
7965 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
7966 * [6] NodeTest ::= NameTest | NodeType '(' ')'
7967 * [8] NodeType ::= 'text' | 'node'
7968 *
7969 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007970 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko49fec8e2022-05-24 10:28:33 +02007971 * @param[in] axis Axis to search on.
7972 * @param[in] all_desc Whether to search all the descendants or axis only.
aPiecek8b0cc152021-05-31 16:40:31 +02007973 * @param[in,out] set Context and result set.
Michal Vaskod3678892020-05-21 10:06:58 +02007974 * @param[in] options XPath options.
7975 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7976 */
7977static LY_ERR
Michal Vasko49fec8e2022-05-24 10:28:33 +02007978eval_node_type_with_predicate(const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_axis axis, ly_bool all_desc,
Radek Krejci1deb5be2020-08-26 16:43:36 +02007979 struct lyxp_set *set, uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02007980{
7981 LY_ERR rc;
7982
Michal Vaskod3678892020-05-21 10:06:58 +02007983 (void)all_desc;
7984
aPiecek8b0cc152021-05-31 16:40:31 +02007985 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko004d3152020-06-11 19:59:22 +02007986 assert(exp->tok_len[*tok_idx] == 4);
Michal Vasko49fec8e2022-05-24 10:28:33 +02007987 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "node", 4)) {
7988 rc = xpath_pi_node(set, axis, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007989 } else {
Michal Vasko49fec8e2022-05-24 10:28:33 +02007990 assert(!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "text", 4));
7991 rc = xpath_pi_text(set, axis, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007992 }
Michal Vasko49fec8e2022-05-24 10:28:33 +02007993 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02007994 }
aPiecek8b0cc152021-05-31 16:40:31 +02007995 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02007996 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007997 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007998
7999 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02008000 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
aPiecek8b0cc152021-05-31 16:40:31 +02008001 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008002 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008003 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02008004
8005 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02008006 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
aPiecek8b0cc152021-05-31 16:40:31 +02008007 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008008 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008009 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02008010
8011 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02008012 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02008013 rc = eval_predicate(exp, tok_idx, set, options, axis);
Michal Vaskod3678892020-05-21 10:06:58 +02008014 LY_CHECK_RET(rc);
8015 }
8016
8017 return LY_SUCCESS;
8018}
8019
8020/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02008021 * @brief Evaluate RelativeLocationPath. Logs directly on error.
8022 *
8023 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
8024 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
Michal Vaskod3678892020-05-21 10:06:58 +02008025 * [6] NodeTest ::= NameTest | NodeType '(' ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02008026 *
8027 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008028 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008029 * @param[in] all_desc Whether to search all the descendants or children only.
aPiecek8b0cc152021-05-31 16:40:31 +02008030 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008031 * @param[in] options XPath options.
8032 * @return LY_ERR (YL_EINCOMPLETE on unresolved when)
8033 */
8034static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008035eval_relative_location_path(const struct lyxp_expr *exp, uint16_t *tok_idx, ly_bool all_desc, struct lyxp_set *set,
8036 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008037{
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008038 LY_ERR rc = LY_SUCCESS;
Michal Vasko49fec8e2022-05-24 10:28:33 +02008039 enum lyxp_axis axis;
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008040 int scnode_skip_path = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008041
8042 goto step;
8043 do {
8044 /* evaluate '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02008045 if (exp->tok_len[*tok_idx] == 1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008046 all_desc = 0;
8047 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02008048 assert(exp->tok_len[*tok_idx] == 2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008049 all_desc = 1;
8050 }
aPiecek8b0cc152021-05-31 16:40:31 +02008051 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008052 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008053 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008054
8055step:
Michal Vasko49fec8e2022-05-24 10:28:33 +02008056 /* AxisSpecifier */
8057 if (exp->tokens[*tok_idx] == LYXP_TOKEN_AXISNAME) {
8058 axis = str2axis(exp->expr + exp->tok_pos[*tok_idx], exp->tok_len[*tok_idx]);
Michal Vaskod3678892020-05-21 10:06:58 +02008059
aPiecek8b0cc152021-05-31 16:40:31 +02008060 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008061 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
8062 ++(*tok_idx);
8063
8064 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_DCOLON);
8065 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
8066 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
8067 ++(*tok_idx);
8068 } else if (exp->tokens[*tok_idx] == LYXP_TOKEN_AT) {
8069 axis = LYXP_AXIS_ATTRIBUTE;
8070
8071 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
8072 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008073 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02008074 } else {
Michal Vasko49fec8e2022-05-24 10:28:33 +02008075 /* default */
8076 axis = LYXP_AXIS_CHILD;
Michal Vaskod3678892020-05-21 10:06:58 +02008077 }
8078
Michal Vasko49fec8e2022-05-24 10:28:33 +02008079 /* NodeTest Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02008080 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008081 case LYXP_TOKEN_DOT:
8082 /* evaluate '.' */
Michal Vasko49fec8e2022-05-24 10:28:33 +02008083 if (!(options & LYXP_SKIP_EXPR)) {
8084 if (((options & LYXP_SCNODE_ALL) && (set->type != LYXP_SET_SCNODE_SET)) ||
8085 (!(options & LYXP_SCNODE_ALL) && (set->type != LYXP_SET_NODE_SET))) {
8086 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
8087 rc = LY_EVALID;
8088 goto cleanup;
8089 }
8090
8091 if (all_desc) {
8092 rc = xpath_pi_node(set, LYXP_AXIS_DESCENDANT_OR_SELF, options);
8093 LY_CHECK_GOTO(rc, cleanup);
8094 }
8095 rc = xpath_pi_node(set, LYXP_AXIS_SELF, options);
8096 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008097 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008098 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008099 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008100 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008101 break;
8102
8103 case LYXP_TOKEN_DDOT:
8104 /* evaluate '..' */
Michal Vasko49fec8e2022-05-24 10:28:33 +02008105 if (!(options & LYXP_SKIP_EXPR)) {
8106 if (((options & LYXP_SCNODE_ALL) && (set->type != LYXP_SET_SCNODE_SET)) ||
8107 (!(options & LYXP_SCNODE_ALL) && (set->type != LYXP_SET_NODE_SET))) {
8108 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
8109 rc = LY_EVALID;
8110 goto cleanup;
8111 }
8112
8113 if (all_desc) {
8114 rc = xpath_pi_node(set, LYXP_AXIS_DESCENDANT_OR_SELF, options);
8115 LY_CHECK_GOTO(rc, cleanup);
8116 }
8117 rc = xpath_pi_node(set, LYXP_AXIS_PARENT, options);
8118 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008119 }
aPiecek8b0cc152021-05-31 16:40:31 +02008120 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008121 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008122 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008123 break;
8124
Michal Vasko03ff5a72019-09-11 13:49:33 +02008125 case LYXP_TOKEN_NAMETEST:
Michal Vaskod3678892020-05-21 10:06:58 +02008126 /* evaluate NameTest Predicate* */
Michal Vasko49fec8e2022-05-24 10:28:33 +02008127 rc = eval_name_test_with_predicate(exp, tok_idx, axis, all_desc, set, options);
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008128 if (rc == LY_ENOT) {
8129 assert(options & LYXP_SCNODE_ALL);
8130 /* skip the rest of this path */
8131 rc = LY_SUCCESS;
8132 scnode_skip_path = 1;
8133 options |= LYXP_SKIP_EXPR;
8134 }
8135 LY_CHECK_GOTO(rc, cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02008136 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008137
Michal Vaskod3678892020-05-21 10:06:58 +02008138 case LYXP_TOKEN_NODETYPE:
8139 /* evaluate NodeType Predicate* */
Michal Vasko49fec8e2022-05-24 10:28:33 +02008140 rc = eval_node_type_with_predicate(exp, tok_idx, axis, all_desc, set, options);
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008141 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008142 break;
8143
8144 default:
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008145 LOGINT(set->ctx);
8146 rc = LY_EINT;
8147 goto cleanup;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008148 }
Michal Vasko004d3152020-06-11 19:59:22 +02008149 } while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008150
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008151cleanup:
8152 if (scnode_skip_path) {
8153 options &= ~LYXP_SKIP_EXPR;
8154 }
8155 return rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008156}
8157
8158/**
8159 * @brief Evaluate AbsoluteLocationPath. Logs directly on error.
8160 *
8161 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
8162 *
8163 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008164 * @param[in] tok_idx Position in the expression @p exp.
aPiecek8b0cc152021-05-31 16:40:31 +02008165 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008166 * @param[in] options XPath options.
8167 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8168 */
8169static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008170eval_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 +02008171{
Radek Krejci857189e2020-09-01 13:26:36 +02008172 ly_bool all_desc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008173
aPiecek8b0cc152021-05-31 16:40:31 +02008174 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008175 /* no matter what tokens follow, we need to be at the root */
Michal Vaskob0099a92020-08-31 14:55:23 +02008176 LY_CHECK_RET(moveto_root(set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008177 }
8178
8179 /* '/' RelativeLocationPath? */
Michal Vasko004d3152020-06-11 19:59:22 +02008180 if (exp->tok_len[*tok_idx] == 1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008181 /* evaluate '/' - deferred */
8182 all_desc = 0;
aPiecek8b0cc152021-05-31 16:40:31 +02008183 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008184 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008185 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008186
Michal Vasko004d3152020-06-11 19:59:22 +02008187 if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008188 return LY_SUCCESS;
8189 }
Michal Vasko004d3152020-06-11 19:59:22 +02008190 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008191 case LYXP_TOKEN_DOT:
8192 case LYXP_TOKEN_DDOT:
Michal Vasko49fec8e2022-05-24 10:28:33 +02008193 case LYXP_TOKEN_AXISNAME:
Michal Vasko03ff5a72019-09-11 13:49:33 +02008194 case LYXP_TOKEN_AT:
8195 case LYXP_TOKEN_NAMETEST:
8196 case LYXP_TOKEN_NODETYPE:
Michal Vaskob0099a92020-08-31 14:55:23 +02008197 LY_CHECK_RET(eval_relative_location_path(exp, tok_idx, all_desc, set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008198 break;
8199 default:
8200 break;
8201 }
8202
Michal Vasko03ff5a72019-09-11 13:49:33 +02008203 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02008204 /* '//' RelativeLocationPath */
Michal Vasko03ff5a72019-09-11 13:49:33 +02008205 /* evaluate '//' - deferred so as not to waste memory by remembering all the nodes */
8206 all_desc = 1;
aPiecek8b0cc152021-05-31 16:40:31 +02008207 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008208 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008209 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008210
Michal Vaskob0099a92020-08-31 14:55:23 +02008211 LY_CHECK_RET(eval_relative_location_path(exp, tok_idx, all_desc, set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008212 }
8213
8214 return LY_SUCCESS;
8215}
8216
8217/**
8218 * @brief Evaluate FunctionCall. Logs directly on error.
8219 *
Michal Vaskod3678892020-05-21 10:06:58 +02008220 * [11] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02008221 *
8222 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008223 * @param[in] tok_idx Position in the expression @p exp.
aPiecek8b0cc152021-05-31 16:40:31 +02008224 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008225 * @param[in] options XPath options.
8226 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8227 */
8228static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008229eval_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 +02008230{
8231 LY_ERR rc;
Michal Vasko69730152020-10-09 16:30:07 +02008232
Radek Krejci1deb5be2020-08-26 16:43:36 +02008233 LY_ERR (*xpath_func)(struct lyxp_set **, uint16_t, struct lyxp_set *, uint32_t) = NULL;
Michal Vasko0cbf54f2019-12-16 10:01:06 +01008234 uint16_t arg_count = 0, i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008235 struct lyxp_set **args = NULL, **args_aux;
8236
aPiecek8b0cc152021-05-31 16:40:31 +02008237 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008238 /* FunctionName */
Michal Vasko004d3152020-06-11 19:59:22 +02008239 switch (exp->tok_len[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008240 case 3:
Michal Vasko004d3152020-06-11 19:59:22 +02008241 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008242 xpath_func = &xpath_not;
Michal Vasko004d3152020-06-11 19:59:22 +02008243 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008244 xpath_func = &xpath_sum;
8245 }
8246 break;
8247 case 4:
Michal Vasko004d3152020-06-11 19:59:22 +02008248 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008249 xpath_func = &xpath_lang;
Michal Vasko004d3152020-06-11 19:59:22 +02008250 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008251 xpath_func = &xpath_last;
Michal Vasko004d3152020-06-11 19:59:22 +02008252 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008253 xpath_func = &xpath_name;
Michal Vasko004d3152020-06-11 19:59:22 +02008254 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008255 xpath_func = &xpath_true;
8256 }
8257 break;
8258 case 5:
Michal Vasko004d3152020-06-11 19:59:22 +02008259 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008260 xpath_func = &xpath_count;
Michal Vasko004d3152020-06-11 19:59:22 +02008261 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008262 xpath_func = &xpath_false;
Michal Vasko004d3152020-06-11 19:59:22 +02008263 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008264 xpath_func = &xpath_floor;
Michal Vasko004d3152020-06-11 19:59:22 +02008265 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008266 xpath_func = &xpath_round;
Michal Vasko004d3152020-06-11 19:59:22 +02008267 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008268 xpath_func = &xpath_deref;
8269 }
8270 break;
8271 case 6:
Michal Vasko004d3152020-06-11 19:59:22 +02008272 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008273 xpath_func = &xpath_concat;
Michal Vasko004d3152020-06-11 19:59:22 +02008274 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008275 xpath_func = &xpath_number;
Michal Vasko004d3152020-06-11 19:59:22 +02008276 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008277 xpath_func = &xpath_string;
8278 }
8279 break;
8280 case 7:
Michal Vasko004d3152020-06-11 19:59:22 +02008281 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008282 xpath_func = &xpath_boolean;
Michal Vasko004d3152020-06-11 19:59:22 +02008283 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008284 xpath_func = &xpath_ceiling;
Michal Vasko004d3152020-06-11 19:59:22 +02008285 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008286 xpath_func = &xpath_current;
8287 }
8288 break;
8289 case 8:
Michal Vasko004d3152020-06-11 19:59:22 +02008290 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008291 xpath_func = &xpath_contains;
Michal Vasko004d3152020-06-11 19:59:22 +02008292 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008293 xpath_func = &xpath_position;
Michal Vasko004d3152020-06-11 19:59:22 +02008294 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "re-match", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008295 xpath_func = &xpath_re_match;
8296 }
8297 break;
8298 case 9:
Michal Vasko004d3152020-06-11 19:59:22 +02008299 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008300 xpath_func = &xpath_substring;
Michal Vasko004d3152020-06-11 19:59:22 +02008301 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008302 xpath_func = &xpath_translate;
8303 }
8304 break;
8305 case 10:
Michal Vasko004d3152020-06-11 19:59:22 +02008306 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008307 xpath_func = &xpath_local_name;
Michal Vasko004d3152020-06-11 19:59:22 +02008308 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008309 xpath_func = &xpath_enum_value;
Michal Vasko004d3152020-06-11 19:59:22 +02008310 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008311 xpath_func = &xpath_bit_is_set;
8312 }
8313 break;
8314 case 11:
Michal Vasko004d3152020-06-11 19:59:22 +02008315 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008316 xpath_func = &xpath_starts_with;
8317 }
8318 break;
8319 case 12:
Michal Vasko004d3152020-06-11 19:59:22 +02008320 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008321 xpath_func = &xpath_derived_from;
8322 }
8323 break;
8324 case 13:
Michal Vasko004d3152020-06-11 19:59:22 +02008325 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008326 xpath_func = &xpath_namespace_uri;
Michal Vasko004d3152020-06-11 19:59:22 +02008327 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string-length", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008328 xpath_func = &xpath_string_length;
8329 }
8330 break;
8331 case 15:
Michal Vasko004d3152020-06-11 19:59:22 +02008332 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008333 xpath_func = &xpath_normalize_space;
Michal Vasko004d3152020-06-11 19:59:22 +02008334 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-after", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008335 xpath_func = &xpath_substring_after;
8336 }
8337 break;
8338 case 16:
Michal Vasko004d3152020-06-11 19:59:22 +02008339 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008340 xpath_func = &xpath_substring_before;
8341 }
8342 break;
8343 case 20:
Michal Vasko004d3152020-06-11 19:59:22 +02008344 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from-or-self", 20)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008345 xpath_func = &xpath_derived_from_or_self;
8346 }
8347 break;
8348 }
8349
8350 if (!xpath_func) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01008351 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 +02008352 return LY_EVALID;
8353 }
8354 }
8355
aPiecek8b0cc152021-05-31 16:40:31 +02008356 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008357 lyxp_token2str(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
8360 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02008361 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
aPiecek8b0cc152021-05-31 16:40:31 +02008362 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008363 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008364 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008365
8366 /* ( Expr ( ',' Expr )* )? */
Michal Vasko004d3152020-06-11 19:59:22 +02008367 if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) {
aPiecek8b0cc152021-05-31 16:40:31 +02008368 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008369 args = malloc(sizeof *args);
8370 LY_CHECK_ERR_GOTO(!args, LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
8371 arg_count = 1;
8372 args[0] = set_copy(set);
8373 if (!args[0]) {
8374 rc = LY_EMEM;
8375 goto cleanup;
8376 }
8377
Michal Vasko004d3152020-06-11 19:59:22 +02008378 rc = eval_expr_select(exp, tok_idx, 0, args[0], options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008379 LY_CHECK_GOTO(rc, cleanup);
8380 } else {
aPiecek8b0cc152021-05-31 16:40:31 +02008381 rc = eval_expr_select(exp, tok_idx, 0, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008382 LY_CHECK_GOTO(rc, cleanup);
8383 }
8384 }
Michal Vasko004d3152020-06-11 19:59:22 +02008385 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) {
aPiecek8b0cc152021-05-31 16:40:31 +02008386 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008387 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008388 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008389
aPiecek8b0cc152021-05-31 16:40:31 +02008390 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008391 ++arg_count;
8392 args_aux = realloc(args, arg_count * sizeof *args);
8393 LY_CHECK_ERR_GOTO(!args_aux, arg_count--; LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
8394 args = args_aux;
8395 args[arg_count - 1] = set_copy(set);
8396 if (!args[arg_count - 1]) {
8397 rc = LY_EMEM;
8398 goto cleanup;
8399 }
8400
Michal Vasko004d3152020-06-11 19:59:22 +02008401 rc = eval_expr_select(exp, tok_idx, 0, args[arg_count - 1], options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008402 LY_CHECK_GOTO(rc, cleanup);
8403 } else {
aPiecek8b0cc152021-05-31 16:40:31 +02008404 rc = eval_expr_select(exp, tok_idx, 0, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008405 LY_CHECK_GOTO(rc, cleanup);
8406 }
8407 }
8408
8409 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02008410 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
aPiecek8b0cc152021-05-31 16:40:31 +02008411 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008412 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008413 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008414
aPiecek8b0cc152021-05-31 16:40:31 +02008415 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008416 /* evaluate function */
8417 rc = xpath_func(args, arg_count, set, options);
8418
8419 if (options & LYXP_SCNODE_ALL) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008420 /* merge all nodes from arg evaluations */
8421 for (i = 0; i < arg_count; ++i) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008422 set_scnode_clear_ctx(args[i], LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008423 lyxp_set_scnode_merge(set, args[i]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008424 }
8425 }
8426 } else {
8427 rc = LY_SUCCESS;
8428 }
8429
8430cleanup:
8431 for (i = 0; i < arg_count; ++i) {
8432 lyxp_set_free(args[i]);
8433 }
8434 free(args);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008435 return rc;
8436}
8437
8438/**
8439 * @brief Evaluate Number. Logs directly on error.
8440 *
8441 * @param[in] ctx Context for errors.
8442 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008443 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008444 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8445 * @return LY_ERR
8446 */
8447static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008448eval_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 +02008449{
8450 long double num;
8451 char *endptr;
8452
8453 if (set) {
8454 errno = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02008455 num = strtold(&exp->expr[exp->tok_pos[*tok_idx]], &endptr);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008456 if (errno) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01008457 LOGVAL(ctx, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]);
8458 LOGVAL(ctx, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double (%s).",
Michal Vasko69730152020-10-09 16:30:07 +02008459 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]], strerror(errno));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008460 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02008461 } else if (endptr - &exp->expr[exp->tok_pos[*tok_idx]] != exp->tok_len[*tok_idx]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01008462 LOGVAL(ctx, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]);
8463 LOGVAL(ctx, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double.",
Michal Vasko69730152020-10-09 16:30:07 +02008464 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008465 return LY_EVALID;
8466 }
8467
8468 set_fill_number(set, num);
8469 }
8470
8471 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008472 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008473 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008474 return LY_SUCCESS;
8475}
8476
aPiecekdf23eee2021-10-07 12:21:50 +02008477LY_ERR
8478lyxp_vars_find(struct lyxp_var *vars, const char *name, size_t name_len, struct lyxp_var **var)
8479{
8480 LY_ERR ret = LY_ENOTFOUND;
8481 LY_ARRAY_COUNT_TYPE u;
8482
8483 assert(vars && name);
8484
8485 name_len = name_len ? name_len : strlen(name);
8486
8487 LY_ARRAY_FOR(vars, u) {
8488 if (!strncmp(vars[u].name, name, name_len)) {
8489 ret = LY_SUCCESS;
8490 break;
8491 }
8492 }
8493
8494 if (var && !ret) {
8495 *var = &vars[u];
8496 }
8497
8498 return ret;
8499}
8500
Michal Vasko03ff5a72019-09-11 13:49:33 +02008501/**
aPiecekfba75362021-10-07 12:39:48 +02008502 * @brief Evaluate VariableReference.
8503 *
8504 * @param[in] exp Parsed XPath expression.
8505 * @param[in] tok_idx Position in the expression @p exp.
8506 * @param[in] vars [Sized array](@ref sizedarrays) of XPath variables.
8507 * @param[in,out] set Context and result set.
8508 * @param[in] options XPath options.
8509 * @return LY_ERR value.
8510 */
8511static LY_ERR
8512eval_variable_reference(const struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set, uint32_t options)
8513{
8514 LY_ERR ret;
8515 const char *name;
8516 struct lyxp_var *var;
8517 const struct lyxp_var *vars;
8518 struct lyxp_expr *tokens = NULL;
8519 uint16_t token_index;
8520
8521 vars = set->vars;
8522
Michal Vasko49fec8e2022-05-24 10:28:33 +02008523 /* find out the name and value of the variable */
aPiecekfba75362021-10-07 12:39:48 +02008524 name = &exp->expr[exp->tok_pos[*tok_idx]];
8525 ret = lyxp_vars_find((struct lyxp_var *)vars, name, exp->tok_len[*tok_idx], &var);
8526 LY_CHECK_ERR_RET(ret, LOGERR(set->ctx, ret,
8527 "XPath variable \"%s\" not defined.", name), ret);
8528
Michal Vasko49fec8e2022-05-24 10:28:33 +02008529 /* parse value */
aPiecekfba75362021-10-07 12:39:48 +02008530 ret = lyxp_expr_parse(set->ctx, var->value, 0, 1, &tokens);
8531 LY_CHECK_GOTO(ret, cleanup);
8532
Michal Vasko49fec8e2022-05-24 10:28:33 +02008533 /* evaluate value */
aPiecekfba75362021-10-07 12:39:48 +02008534 token_index = 0;
8535 ret = eval_expr_select(tokens, &token_index, 0, set, options);
8536 LY_CHECK_GOTO(ret, cleanup);
8537
8538cleanup:
8539 lyxp_expr_free(set->ctx, tokens);
8540
8541 return ret;
8542}
8543
8544/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02008545 * @brief Evaluate PathExpr. Logs directly on error.
8546 *
Michal Vaskod3678892020-05-21 10:06:58 +02008547 * [12] PathExpr ::= LocationPath | PrimaryExpr Predicate*
Michal Vasko03ff5a72019-09-11 13:49:33 +02008548 * | PrimaryExpr Predicate* '/' RelativeLocationPath
8549 * | PrimaryExpr Predicate* '//' RelativeLocationPath
8550 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
aPiecekfba75362021-10-07 12:39:48 +02008551 * [10] PrimaryExpr ::= VariableReference | '(' Expr ')' | Literal | Number | FunctionCall
Michal Vasko03ff5a72019-09-11 13:49:33 +02008552 *
8553 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008554 * @param[in] tok_idx Position in the expression @p exp.
aPiecek8b0cc152021-05-31 16:40:31 +02008555 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008556 * @param[in] options XPath options.
8557 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8558 */
8559static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008560eval_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 +02008561{
Michal Vasko49fec8e2022-05-24 10:28:33 +02008562 ly_bool all_desc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008563 LY_ERR rc;
8564
Michal Vasko004d3152020-06-11 19:59:22 +02008565 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008566 case LYXP_TOKEN_PAR1:
8567 /* '(' Expr ')' */
8568
8569 /* '(' */
aPiecek8b0cc152021-05-31 16:40:31 +02008570 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008571 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008572 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008573
8574 /* Expr */
Michal Vasko004d3152020-06-11 19:59:22 +02008575 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008576 LY_CHECK_RET(rc);
8577
8578 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02008579 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
aPiecek8b0cc152021-05-31 16:40:31 +02008580 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008581 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008582 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008583
Michal Vasko03ff5a72019-09-11 13:49:33 +02008584 goto predicate;
8585
8586 case LYXP_TOKEN_DOT:
8587 case LYXP_TOKEN_DDOT:
Michal Vasko49fec8e2022-05-24 10:28:33 +02008588 case LYXP_TOKEN_AXISNAME:
Michal Vasko03ff5a72019-09-11 13:49:33 +02008589 case LYXP_TOKEN_AT:
8590 case LYXP_TOKEN_NAMETEST:
8591 case LYXP_TOKEN_NODETYPE:
8592 /* RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02008593 rc = eval_relative_location_path(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008594 LY_CHECK_RET(rc);
8595 break;
8596
aPiecekfba75362021-10-07 12:39:48 +02008597 case LYXP_TOKEN_VARREF:
8598 /* VariableReference */
8599 rc = eval_variable_reference(exp, tok_idx, set, options);
8600 LY_CHECK_RET(rc);
8601 ++(*tok_idx);
8602
aPiecekfba75362021-10-07 12:39:48 +02008603 goto predicate;
8604
Michal Vasko03ff5a72019-09-11 13:49:33 +02008605 case LYXP_TOKEN_FUNCNAME:
8606 /* FunctionCall */
aPiecek8b0cc152021-05-31 16:40:31 +02008607 rc = eval_function_call(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008608 LY_CHECK_RET(rc);
8609
Michal Vasko03ff5a72019-09-11 13:49:33 +02008610 goto predicate;
8611
Michal Vasko3e48bf32020-06-01 08:39:07 +02008612 case LYXP_TOKEN_OPER_PATH:
8613 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +02008614 /* AbsoluteLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02008615 rc = eval_absolute_location_path(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008616 LY_CHECK_RET(rc);
8617 break;
8618
8619 case LYXP_TOKEN_LITERAL:
8620 /* Literal */
aPiecek8b0cc152021-05-31 16:40:31 +02008621 if ((options & LYXP_SKIP_EXPR) || (options & LYXP_SCNODE_ALL)) {
8622 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008623 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008624 }
Michal Vasko004d3152020-06-11 19:59:22 +02008625 eval_literal(exp, tok_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008626 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02008627 eval_literal(exp, tok_idx, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008628 }
8629
Michal Vasko03ff5a72019-09-11 13:49:33 +02008630 goto predicate;
8631
8632 case LYXP_TOKEN_NUMBER:
8633 /* Number */
aPiecek8b0cc152021-05-31 16:40:31 +02008634 if ((options & LYXP_SKIP_EXPR) || (options & LYXP_SCNODE_ALL)) {
8635 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008636 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008637 }
Michal Vasko004d3152020-06-11 19:59:22 +02008638 rc = eval_number(NULL, exp, tok_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008639 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02008640 rc = eval_number(set->ctx, exp, tok_idx, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008641 }
8642 LY_CHECK_RET(rc);
8643
Michal Vasko03ff5a72019-09-11 13:49:33 +02008644 goto predicate;
8645
8646 default:
Michal Vasko49fec8e2022-05-24 10:28:33 +02008647 LOGVAL(set->ctx, LY_VCODE_XP_INTOK, lyxp_token2str(exp->tokens[*tok_idx]), &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008648 return LY_EVALID;
8649 }
8650
8651 return LY_SUCCESS;
8652
8653predicate:
8654 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02008655 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02008656 rc = eval_predicate(exp, tok_idx, set, options, LYXP_AXIS_CHILD);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008657 LY_CHECK_RET(rc);
8658 }
8659
8660 /* ('/' or '//') RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02008661 if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008662
8663 /* evaluate '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02008664 if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008665 all_desc = 0;
8666 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008667 all_desc = 1;
8668 }
8669
aPiecek8b0cc152021-05-31 16:40:31 +02008670 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008671 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008672 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008673
Michal Vasko004d3152020-06-11 19:59:22 +02008674 rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008675 LY_CHECK_RET(rc);
8676 }
8677
8678 return LY_SUCCESS;
8679}
8680
8681/**
8682 * @brief Evaluate UnionExpr. Logs directly on error.
8683 *
Michal Vaskod3678892020-05-21 10:06:58 +02008684 * [20] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008685 *
8686 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008687 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008688 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008689 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008690 * @param[in] options XPath options.
8691 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8692 */
8693static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008694eval_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 +02008695{
8696 LY_ERR rc = LY_SUCCESS;
8697 struct lyxp_set orig_set, set2;
8698 uint16_t i;
8699
8700 assert(repeat);
8701
8702 set_init(&orig_set, set);
8703 set_init(&set2, set);
8704
8705 set_fill_set(&orig_set, set);
8706
Michal Vasko004d3152020-06-11 19:59:22 +02008707 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008708 LY_CHECK_GOTO(rc, cleanup);
8709
8710 /* ('|' PathExpr)* */
8711 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008712 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_UNI);
aPiecek8b0cc152021-05-31 16:40:31 +02008713 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008714 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008715 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008716
aPiecek8b0cc152021-05-31 16:40:31 +02008717 if (options & LYXP_SKIP_EXPR) {
8718 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008719 LY_CHECK_GOTO(rc, cleanup);
8720 continue;
8721 }
8722
8723 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008724 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008725 LY_CHECK_GOTO(rc, cleanup);
8726
8727 /* eval */
8728 if (options & LYXP_SCNODE_ALL) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01008729 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008730 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008731 rc = moveto_union(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008732 LY_CHECK_GOTO(rc, cleanup);
8733 }
8734 }
8735
8736cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008737 lyxp_set_free_content(&orig_set);
8738 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008739 return rc;
8740}
8741
8742/**
8743 * @brief Evaluate UnaryExpr. Logs directly on error.
8744 *
Michal Vaskod3678892020-05-21 10:06:58 +02008745 * [19] UnaryExpr ::= UnionExpr | '-' UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008746 *
8747 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008748 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008749 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008750 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008751 * @param[in] options XPath options.
8752 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8753 */
8754static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008755eval_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 +02008756{
8757 LY_ERR rc;
8758 uint16_t this_op, i;
8759
8760 assert(repeat);
8761
8762 /* ('-')+ */
Michal Vasko004d3152020-06-11 19:59:22 +02008763 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008764 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008765 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 +02008766
aPiecek8b0cc152021-05-31 16:40:31 +02008767 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008768 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008769 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008770 }
8771
Michal Vasko004d3152020-06-11 19:59:22 +02008772 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNARY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008773 LY_CHECK_RET(rc);
8774
aPiecek8b0cc152021-05-31 16:40:31 +02008775 if (!(options & LYXP_SKIP_EXPR) && (repeat % 2)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008776 if (options & LYXP_SCNODE_ALL) {
8777 warn_operands(set->ctx, set, NULL, 1, exp->expr, exp->tok_pos[this_op]);
8778 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008779 rc = moveto_op_math(set, NULL, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008780 LY_CHECK_RET(rc);
8781 }
8782 }
8783
8784 return LY_SUCCESS;
8785}
8786
8787/**
8788 * @brief Evaluate MultiplicativeExpr. Logs directly on error.
8789 *
Michal Vaskod3678892020-05-21 10:06:58 +02008790 * [18] MultiplicativeExpr ::= UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008791 * | MultiplicativeExpr '*' UnaryExpr
8792 * | MultiplicativeExpr 'div' UnaryExpr
8793 * | MultiplicativeExpr 'mod' UnaryExpr
8794 *
8795 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008796 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008797 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008798 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008799 * @param[in] options XPath options.
8800 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8801 */
8802static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008803eval_multiplicative_expr(const struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set,
8804 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008805{
8806 LY_ERR rc;
8807 uint16_t this_op;
8808 struct lyxp_set orig_set, set2;
8809 uint16_t i;
8810
8811 assert(repeat);
8812
8813 set_init(&orig_set, set);
8814 set_init(&set2, set);
8815
8816 set_fill_set(&orig_set, set);
8817
Michal Vasko004d3152020-06-11 19:59:22 +02008818 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008819 LY_CHECK_GOTO(rc, cleanup);
8820
8821 /* ('*' / 'div' / 'mod' UnaryExpr)* */
8822 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008823 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008824
Michal Vasko004d3152020-06-11 19:59:22 +02008825 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH);
aPiecek8b0cc152021-05-31 16:40:31 +02008826 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008827 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008828 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008829
aPiecek8b0cc152021-05-31 16:40:31 +02008830 if (options & LYXP_SKIP_EXPR) {
8831 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008832 LY_CHECK_GOTO(rc, cleanup);
8833 continue;
8834 }
8835
8836 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008837 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008838 LY_CHECK_GOTO(rc, cleanup);
8839
8840 /* eval */
8841 if (options & LYXP_SCNODE_ALL) {
8842 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008843 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02008844 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008845 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008846 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008847 LY_CHECK_GOTO(rc, cleanup);
8848 }
8849 }
8850
8851cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008852 lyxp_set_free_content(&orig_set);
8853 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008854 return rc;
8855}
8856
8857/**
8858 * @brief Evaluate AdditiveExpr. Logs directly on error.
8859 *
Michal Vaskod3678892020-05-21 10:06:58 +02008860 * [17] AdditiveExpr ::= MultiplicativeExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008861 * | AdditiveExpr '+' MultiplicativeExpr
8862 * | AdditiveExpr '-' MultiplicativeExpr
8863 *
8864 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008865 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008866 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008867 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008868 * @param[in] options XPath options.
8869 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8870 */
8871static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008872eval_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 +02008873{
8874 LY_ERR rc;
8875 uint16_t this_op;
8876 struct lyxp_set orig_set, set2;
8877 uint16_t i;
8878
8879 assert(repeat);
8880
8881 set_init(&orig_set, set);
8882 set_init(&set2, set);
8883
8884 set_fill_set(&orig_set, set);
8885
Michal Vasko004d3152020-06-11 19:59:22 +02008886 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008887 LY_CHECK_GOTO(rc, cleanup);
8888
8889 /* ('+' / '-' MultiplicativeExpr)* */
8890 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008891 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008892
Michal Vasko004d3152020-06-11 19:59:22 +02008893 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008894 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008895 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008896 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008897
aPiecek8b0cc152021-05-31 16:40:31 +02008898 if (options & LYXP_SKIP_EXPR) {
8899 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008900 LY_CHECK_GOTO(rc, cleanup);
8901 continue;
8902 }
8903
8904 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008905 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008906 LY_CHECK_GOTO(rc, cleanup);
8907
8908 /* eval */
8909 if (options & LYXP_SCNODE_ALL) {
8910 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008911 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02008912 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008913 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008914 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008915 LY_CHECK_GOTO(rc, cleanup);
8916 }
8917 }
8918
8919cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008920 lyxp_set_free_content(&orig_set);
8921 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008922 return rc;
8923}
8924
8925/**
8926 * @brief Evaluate RelationalExpr. Logs directly on error.
8927 *
Michal Vaskod3678892020-05-21 10:06:58 +02008928 * [16] RelationalExpr ::= AdditiveExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008929 * | RelationalExpr '<' AdditiveExpr
8930 * | RelationalExpr '>' AdditiveExpr
8931 * | RelationalExpr '<=' AdditiveExpr
8932 * | RelationalExpr '>=' AdditiveExpr
8933 *
8934 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008935 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008936 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008937 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008938 * @param[in] options XPath options.
8939 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8940 */
8941static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008942eval_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 +02008943{
8944 LY_ERR rc;
8945 uint16_t this_op;
8946 struct lyxp_set orig_set, set2;
8947 uint16_t i;
8948
8949 assert(repeat);
8950
8951 set_init(&orig_set, set);
8952 set_init(&set2, set);
8953
8954 set_fill_set(&orig_set, set);
8955
Michal Vasko004d3152020-06-11 19:59:22 +02008956 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008957 LY_CHECK_GOTO(rc, cleanup);
8958
8959 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
8960 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008961 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008962
Michal Vasko004d3152020-06-11 19:59:22 +02008963 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_COMP);
aPiecek8b0cc152021-05-31 16:40:31 +02008964 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008965 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008966 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008967
aPiecek8b0cc152021-05-31 16:40:31 +02008968 if (options & LYXP_SKIP_EXPR) {
8969 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008970 LY_CHECK_GOTO(rc, cleanup);
8971 continue;
8972 }
8973
8974 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008975 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008976 LY_CHECK_GOTO(rc, cleanup);
8977
8978 /* eval */
8979 if (options & LYXP_SCNODE_ALL) {
8980 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008981 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02008982 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008983 } else {
Michal Vasko8abcecc2022-07-28 09:55:01 +02008984 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008985 LY_CHECK_GOTO(rc, cleanup);
8986 }
8987 }
8988
8989cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008990 lyxp_set_free_content(&orig_set);
8991 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008992 return rc;
8993}
8994
8995/**
8996 * @brief Evaluate EqualityExpr. Logs directly on error.
8997 *
Michal Vaskod3678892020-05-21 10:06:58 +02008998 * [15] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008999 * | EqualityExpr '!=' RelationalExpr
9000 *
9001 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02009002 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009003 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02009004 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009005 * @param[in] options XPath options.
9006 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
9007 */
9008static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02009009eval_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 +02009010{
9011 LY_ERR rc;
9012 uint16_t this_op;
9013 struct lyxp_set orig_set, set2;
9014 uint16_t i;
9015
9016 assert(repeat);
9017
9018 set_init(&orig_set, set);
9019 set_init(&set2, set);
9020
9021 set_fill_set(&orig_set, set);
9022
Michal Vasko004d3152020-06-11 19:59:22 +02009023 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009024 LY_CHECK_GOTO(rc, cleanup);
9025
9026 /* ('=' / '!=' RelationalExpr)* */
9027 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02009028 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009029
Michal Vasko004d3152020-06-11 19:59:22 +02009030 assert((exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_EQUAL) || (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_NEQUAL));
aPiecek8b0cc152021-05-31 16:40:31 +02009031 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02009032 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02009033 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009034
aPiecek8b0cc152021-05-31 16:40:31 +02009035 if (options & LYXP_SKIP_EXPR) {
9036 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009037 LY_CHECK_GOTO(rc, cleanup);
9038 continue;
9039 }
9040
9041 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02009042 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009043 LY_CHECK_GOTO(rc, cleanup);
9044
9045 /* eval */
9046 if (options & LYXP_SCNODE_ALL) {
9047 warn_operands(set->ctx, set, &set2, 0, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vasko004d3152020-06-11 19:59:22 +02009048 warn_equality_value(exp, set, *tok_idx - 1, this_op - 1, *tok_idx - 1);
9049 warn_equality_value(exp, &set2, this_op - 1, this_op - 1, *tok_idx - 1);
Michal Vaskoecd62de2019-11-13 12:35:11 +01009050 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02009051 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009052 } else {
Michal Vasko8abcecc2022-07-28 09:55:01 +02009053 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009054 LY_CHECK_GOTO(rc, cleanup);
9055 }
9056 }
9057
9058cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02009059 lyxp_set_free_content(&orig_set);
9060 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009061 return rc;
9062}
9063
9064/**
9065 * @brief Evaluate AndExpr. Logs directly on error.
9066 *
Michal Vaskod3678892020-05-21 10:06:58 +02009067 * [14] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02009068 *
9069 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02009070 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009071 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02009072 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009073 * @param[in] options XPath options.
9074 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
9075 */
9076static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02009077eval_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 +02009078{
9079 LY_ERR rc;
9080 struct lyxp_set orig_set, set2;
9081 uint16_t i;
9082
9083 assert(repeat);
9084
9085 set_init(&orig_set, set);
9086 set_init(&set2, set);
9087
9088 set_fill_set(&orig_set, set);
9089
Michal Vasko004d3152020-06-11 19:59:22 +02009090 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009091 LY_CHECK_GOTO(rc, cleanup);
9092
9093 /* cast to boolean, we know that will be the final result */
aPiecek8b0cc152021-05-31 16:40:31 +02009094 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02009095 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009096 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009097 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009098 }
9099
9100 /* ('and' EqualityExpr)* */
9101 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02009102 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG);
aPiecek8b0cc152021-05-31 16:40:31 +02009103 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, ((options & LYXP_SKIP_EXPR) || !set->val.bln ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02009104 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02009105 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009106
9107 /* lazy evaluation */
aPiecek8b0cc152021-05-31 16:40:31 +02009108 if ((options & LYXP_SKIP_EXPR) || ((set->type == LYXP_SET_BOOLEAN) && !set->val.bln)) {
9109 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009110 LY_CHECK_GOTO(rc, cleanup);
9111 continue;
9112 }
9113
9114 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02009115 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009116 LY_CHECK_GOTO(rc, cleanup);
9117
9118 /* eval - just get boolean value actually */
9119 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vasko1a09b212021-05-06 13:00:10 +02009120 set_scnode_clear_ctx(&set2, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskoecd62de2019-11-13 12:35:11 +01009121 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009122 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009123 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009124 set_fill_set(set, &set2);
9125 }
9126 }
9127
9128cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02009129 lyxp_set_free_content(&orig_set);
9130 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009131 return rc;
9132}
9133
9134/**
9135 * @brief Evaluate OrExpr. Logs directly on error.
9136 *
Michal Vaskod3678892020-05-21 10:06:58 +02009137 * [13] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02009138 *
9139 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02009140 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009141 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02009142 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009143 * @param[in] options XPath options.
9144 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
9145 */
9146static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02009147eval_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 +02009148{
9149 LY_ERR rc;
9150 struct lyxp_set orig_set, set2;
9151 uint16_t i;
9152
9153 assert(repeat);
9154
9155 set_init(&orig_set, set);
9156 set_init(&set2, set);
9157
9158 set_fill_set(&orig_set, set);
9159
Michal Vasko004d3152020-06-11 19:59:22 +02009160 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009161 LY_CHECK_GOTO(rc, cleanup);
9162
9163 /* cast to boolean, we know that will be the final result */
aPiecek8b0cc152021-05-31 16:40:31 +02009164 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02009165 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009166 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009167 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009168 }
9169
9170 /* ('or' AndExpr)* */
9171 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02009172 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG);
aPiecek8b0cc152021-05-31 16:40:31 +02009173 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, ((options & LYXP_SKIP_EXPR) || set->val.bln ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02009174 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02009175 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009176
9177 /* lazy evaluation */
aPiecek8b0cc152021-05-31 16:40:31 +02009178 if ((options & LYXP_SKIP_EXPR) || ((set->type == LYXP_SET_BOOLEAN) && set->val.bln)) {
9179 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009180 LY_CHECK_GOTO(rc, cleanup);
9181 continue;
9182 }
9183
9184 set_fill_set(&set2, &orig_set);
9185 /* expr_type cound have been LYXP_EXPR_NONE in all these later calls (except for the first one),
9186 * but it does not matter */
Michal Vasko004d3152020-06-11 19:59:22 +02009187 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009188 LY_CHECK_GOTO(rc, cleanup);
9189
9190 /* eval - just get boolean value actually */
9191 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vasko1a09b212021-05-06 13:00:10 +02009192 set_scnode_clear_ctx(&set2, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskoecd62de2019-11-13 12:35:11 +01009193 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009194 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009195 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009196 set_fill_set(set, &set2);
9197 }
9198 }
9199
9200cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02009201 lyxp_set_free_content(&orig_set);
9202 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009203 return rc;
9204}
9205
9206/**
Michal Vasko004d3152020-06-11 19:59:22 +02009207 * @brief Decide what expression is at the pointer @p tok_idx and evaluate it accordingly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009208 *
9209 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02009210 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009211 * @param[in] etype Expression type to evaluate.
aPiecek8b0cc152021-05-31 16:40:31 +02009212 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009213 * @param[in] options XPath options.
9214 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
9215 */
9216static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02009217eval_expr_select(const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_expr_type etype, struct lyxp_set *set,
9218 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02009219{
9220 uint16_t i, count;
9221 enum lyxp_expr_type next_etype;
9222 LY_ERR rc;
9223
9224 /* process operator repeats */
Michal Vasko004d3152020-06-11 19:59:22 +02009225 if (!exp->repeat[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02009226 next_etype = LYXP_EXPR_NONE;
9227 } else {
9228 /* find etype repeat */
Radek Krejci1e008d22020-08-17 11:37:37 +02009229 for (i = 0; exp->repeat[*tok_idx][i] > etype; ++i) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02009230
9231 /* select one-priority lower because etype expression called us */
9232 if (i) {
Michal Vasko004d3152020-06-11 19:59:22 +02009233 next_etype = exp->repeat[*tok_idx][i - 1];
Michal Vasko03ff5a72019-09-11 13:49:33 +02009234 /* count repeats for that expression */
Radek Krejci1e008d22020-08-17 11:37:37 +02009235 for (count = 0; i && exp->repeat[*tok_idx][i - 1] == next_etype; ++count, --i) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02009236 } else {
9237 next_etype = LYXP_EXPR_NONE;
9238 }
9239 }
9240
9241 /* decide what expression are we parsing based on the repeat */
9242 switch (next_etype) {
9243 case LYXP_EXPR_OR:
Michal Vasko004d3152020-06-11 19:59:22 +02009244 rc = eval_or_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009245 break;
9246 case LYXP_EXPR_AND:
Michal Vasko004d3152020-06-11 19:59:22 +02009247 rc = eval_and_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009248 break;
9249 case LYXP_EXPR_EQUALITY:
Michal Vasko004d3152020-06-11 19:59:22 +02009250 rc = eval_equality_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009251 break;
9252 case LYXP_EXPR_RELATIONAL:
Michal Vasko004d3152020-06-11 19:59:22 +02009253 rc = eval_relational_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009254 break;
9255 case LYXP_EXPR_ADDITIVE:
Michal Vasko004d3152020-06-11 19:59:22 +02009256 rc = eval_additive_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009257 break;
9258 case LYXP_EXPR_MULTIPLICATIVE:
Michal Vasko004d3152020-06-11 19:59:22 +02009259 rc = eval_multiplicative_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009260 break;
9261 case LYXP_EXPR_UNARY:
Michal Vasko004d3152020-06-11 19:59:22 +02009262 rc = eval_unary_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009263 break;
9264 case LYXP_EXPR_UNION:
Michal Vasko004d3152020-06-11 19:59:22 +02009265 rc = eval_union_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009266 break;
9267 case LYXP_EXPR_NONE:
Michal Vasko004d3152020-06-11 19:59:22 +02009268 rc = eval_path_expr(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009269 break;
9270 default:
9271 LOGINT_RET(set->ctx);
9272 }
9273
9274 return rc;
9275}
9276
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009277/**
9278 * @brief Get root type.
9279 *
9280 * @param[in] ctx_node Context node.
9281 * @param[in] ctx_scnode Schema context node.
9282 * @param[in] options XPath options.
9283 * @return Root type.
9284 */
9285static enum lyxp_node_type
Radek Krejci1deb5be2020-08-26 16:43:36 +02009286lyxp_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 +01009287{
Michal Vasko6b26e742020-07-17 15:02:10 +02009288 const struct lysc_node *op;
9289
Michal Vaskoa27245c2022-05-02 09:01:35 +02009290 /* explicit */
9291 if (options & LYXP_ACCESS_TREE_ALL) {
9292 return LYXP_NODE_ROOT;
9293 } else if (options & LYXP_ACCESS_TREE_CONFIG) {
9294 return LYXP_NODE_ROOT_CONFIG;
9295 }
9296
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009297 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009298 /* schema */
Radek Krejci1e008d22020-08-17 11:37:37 +02009299 for (op = ctx_scnode; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent) {}
Michal Vasko6b26e742020-07-17 15:02:10 +02009300
9301 if (op || (options & LYXP_SCNODE)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009302 /* general root that can access everything */
9303 return LYXP_NODE_ROOT;
9304 } else if (!ctx_scnode || (ctx_scnode->flags & LYS_CONFIG_W)) {
9305 /* root context node can access only config data (because we said so, it is unspecified) */
9306 return LYXP_NODE_ROOT_CONFIG;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009307 }
Michal Vasko6b26e742020-07-17 15:02:10 +02009308 return LYXP_NODE_ROOT;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009309 }
9310
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009311 /* data */
Michal Vasko6b26e742020-07-17 15:02:10 +02009312 op = ctx_node ? ctx_node->schema : NULL;
Michal Vaskod989ba02020-08-24 10:59:24 +02009313 for ( ; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent) {}
Michal Vasko6b26e742020-07-17 15:02:10 +02009314
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009315 if (op || !(options & LYXP_SCHEMA)) {
9316 /* general root that can access everything */
9317 return LYXP_NODE_ROOT;
Christian Hoppsb6ecaea2021-02-06 09:45:38 -05009318 } else if (!ctx_node || !ctx_node->schema || (ctx_node->schema->flags & LYS_CONFIG_W)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009319 /* root context node can access only config data (because we said so, it is unspecified) */
9320 return LYXP_NODE_ROOT_CONFIG;
9321 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009322 return LYXP_NODE_ROOT;
9323}
9324
Michal Vasko03ff5a72019-09-11 13:49:33 +02009325LY_ERR
Michal Vasko400e9672021-01-11 13:39:17 +01009326lyxp_eval(const struct ly_ctx *ctx, const struct lyxp_expr *exp, const struct lys_module *cur_mod,
Radek Krejci8df109d2021-04-23 12:19:08 +02009327 LY_VALUE_FORMAT format, void *prefix_data, const struct lyd_node *ctx_node, const struct lyd_node *tree,
aPiecekfba75362021-10-07 12:39:48 +02009328 const struct lyxp_var *vars, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02009329{
Michal Vasko004d3152020-06-11 19:59:22 +02009330 uint16_t tok_idx = 0;
Michal Vaskoddd76592022-01-17 13:34:48 +01009331 const struct lysc_node *snode;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009332 LY_ERR rc;
9333
Michal Vasko400e9672021-01-11 13:39:17 +01009334 LY_CHECK_ARG_RET(ctx, ctx, exp, set, LY_EINVAL);
Radek Krejci8df109d2021-04-23 12:19:08 +02009335 if (!cur_mod && ((format == LY_VALUE_SCHEMA) || (format == LY_VALUE_SCHEMA_RESOLVED))) {
Michal Vaskoddd76592022-01-17 13:34:48 +01009336 LOGERR(ctx, LY_EINVAL, "Current module must be set if schema format is used.");
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009337 return LY_EINVAL;
Michal Vasko004d3152020-06-11 19:59:22 +02009338 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02009339
Michal Vaskod3bb12f2020-12-04 14:33:09 +01009340 if (tree) {
9341 /* adjust the pointer to be the first top-level sibling */
9342 while (tree->parent) {
9343 tree = lyd_parent(tree);
9344 }
9345 tree = lyd_first_sibling(tree);
Michal Vaskoddd76592022-01-17 13:34:48 +01009346
9347 for (snode = tree->schema->parent; snode && (snode->nodetype & (LYS_CASE | LYS_CHOICE)); snode = snode->parent) {}
9348 if (snode) {
9349 /* unable to evaluate absolute paths */
9350 LOGERR(ctx, LY_EINVAL, "Data node \"%s\" has no parent but is not instance of a top-level schema node.",
9351 LYD_NAME(tree));
9352 return LY_EINVAL;
9353 }
Michal Vaskod3bb12f2020-12-04 14:33:09 +01009354 }
9355
Michal Vasko03ff5a72019-09-11 13:49:33 +02009356 /* prepare set for evaluation */
Michal Vasko03ff5a72019-09-11 13:49:33 +02009357 memset(set, 0, sizeof *set);
Michal Vaskod3678892020-05-21 10:06:58 +02009358 set->type = LYXP_SET_NODE_SET;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009359 set->root_type = lyxp_get_root_type(ctx_node, NULL, options);
9360 set_insert_node(set, (struct lyd_node *)ctx_node, 0, ctx_node ? LYXP_NODE_ELEM : set->root_type, 0);
9361
Michal Vasko400e9672021-01-11 13:39:17 +01009362 set->ctx = (struct ly_ctx *)ctx;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009363 set->cur_node = ctx_node;
9364 for (set->context_op = ctx_node ? ctx_node->schema : NULL;
Radek Krejci0f969882020-08-21 16:56:47 +02009365 set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF));
9366 set->context_op = set->context_op->parent) {}
Michal Vaskof03ed032020-03-04 13:31:44 +01009367 set->tree = tree;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009368 set->cur_mod = cur_mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009369 set->format = format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009370 set->prefix_data = prefix_data;
aPiecekfba75362021-10-07 12:39:48 +02009371 set->vars = vars;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009372
Radek Krejciddace2c2021-01-08 11:30:56 +01009373 LOG_LOCSET(NULL, set->cur_node, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01009374
Michal Vasko03ff5a72019-09-11 13:49:33 +02009375 /* evaluate */
Michal Vasko004d3152020-06-11 19:59:22 +02009376 rc = eval_expr_select(exp, &tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009377 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02009378 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009379 }
9380
Michal Vasko4a7d4d62021-12-13 17:05:06 +01009381 if (set->cur_node) {
9382 LOG_LOCBACK(0, 1, 0, 0);
9383 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02009384 return rc;
9385}
9386
9387#if 0
9388
9389/* full xml printing of set elements, not used currently */
9390
9391void
9392lyxp_set_print_xml(FILE *f, struct lyxp_set *set)
9393{
9394 uint32_t i;
9395 char *str_num;
9396 struct lyout out;
9397
9398 memset(&out, 0, sizeof out);
9399
9400 out.type = LYOUT_STREAM;
9401 out.method.f = f;
9402
9403 switch (set->type) {
9404 case LYXP_SET_EMPTY:
Michal Vasko5233e962020-08-14 14:26:20 +02009405 ly_print_(&out, "Empty XPath set\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02009406 break;
9407 case LYXP_SET_BOOLEAN:
Michal Vasko5233e962020-08-14 14:26:20 +02009408 ly_print_(&out, "Boolean XPath set:\n");
9409 ly_print_(&out, "%s\n\n", set->value.bool ? "true" : "false");
Michal Vasko03ff5a72019-09-11 13:49:33 +02009410 break;
9411 case LYXP_SET_STRING:
Michal Vasko5233e962020-08-14 14:26:20 +02009412 ly_print_(&out, "String XPath set:\n");
9413 ly_print_(&out, "\"%s\"\n\n", set->value.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009414 break;
9415 case LYXP_SET_NUMBER:
Michal Vasko5233e962020-08-14 14:26:20 +02009416 ly_print_(&out, "Number XPath set:\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02009417
9418 if (isnan(set->value.num)) {
9419 str_num = strdup("NaN");
9420 } else if ((set->value.num == 0) || (set->value.num == -0.0f)) {
9421 str_num = strdup("0");
9422 } else if (isinf(set->value.num) && !signbit(set->value.num)) {
9423 str_num = strdup("Infinity");
9424 } else if (isinf(set->value.num) && signbit(set->value.num)) {
9425 str_num = strdup("-Infinity");
9426 } else if ((long long)set->value.num == set->value.num) {
9427 if (asprintf(&str_num, "%lld", (long long)set->value.num) == -1) {
9428 str_num = NULL;
9429 }
9430 } else {
9431 if (asprintf(&str_num, "%03.1Lf", set->value.num) == -1) {
9432 str_num = NULL;
9433 }
9434 }
9435 if (!str_num) {
9436 LOGMEM;
9437 return;
9438 }
Michal Vasko5233e962020-08-14 14:26:20 +02009439 ly_print_(&out, "%s\n\n", str_num);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009440 free(str_num);
9441 break;
9442 case LYXP_SET_NODE_SET:
Michal Vasko5233e962020-08-14 14:26:20 +02009443 ly_print_(&out, "Node XPath set:\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02009444
9445 for (i = 0; i < set->used; ++i) {
Michal Vasko5233e962020-08-14 14:26:20 +02009446 ly_print_(&out, "%d. ", i + 1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009447 switch (set->node_type[i]) {
9448 case LYXP_NODE_ROOT_ALL:
Michal Vasko5233e962020-08-14 14:26:20 +02009449 ly_print_(&out, "ROOT all\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02009450 break;
9451 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko5233e962020-08-14 14:26:20 +02009452 ly_print_(&out, "ROOT config\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02009453 break;
9454 case LYXP_NODE_ROOT_STATE:
Michal Vasko5233e962020-08-14 14:26:20 +02009455 ly_print_(&out, "ROOT state\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02009456 break;
9457 case LYXP_NODE_ROOT_NOTIF:
Michal Vasko5233e962020-08-14 14:26:20 +02009458 ly_print_(&out, "ROOT notification \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009459 break;
9460 case LYXP_NODE_ROOT_RPC:
Michal Vasko5233e962020-08-14 14:26:20 +02009461 ly_print_(&out, "ROOT rpc \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009462 break;
9463 case LYXP_NODE_ROOT_OUTPUT:
Michal Vasko5233e962020-08-14 14:26:20 +02009464 ly_print_(&out, "ROOT output \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009465 break;
9466 case LYXP_NODE_ELEM:
Michal Vasko5233e962020-08-14 14:26:20 +02009467 ly_print_(&out, "ELEM \"%s\"\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009468 xml_print_node(&out, 1, set->value.nodes[i], 1, LYP_FORMAT);
Michal Vasko5233e962020-08-14 14:26:20 +02009469 ly_print_(&out, "\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02009470 break;
9471 case LYXP_NODE_TEXT:
Michal Vasko5233e962020-08-14 14:26:20 +02009472 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 +02009473 break;
9474 case LYXP_NODE_ATTR:
Michal Vasko5233e962020-08-14 14:26:20 +02009475 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 +02009476 break;
9477 }
9478 }
9479 break;
9480 }
9481}
9482
9483#endif
9484
9485LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009486lyxp_set_cast(struct lyxp_set *set, enum lyxp_set_type target)
Michal Vasko03ff5a72019-09-11 13:49:33 +02009487{
9488 long double num;
9489 char *str;
9490 LY_ERR rc;
9491
9492 if (!set || (set->type == target)) {
9493 return LY_SUCCESS;
9494 }
9495
9496 /* it's not possible to convert anything into a node set */
Michal Vaskod3678892020-05-21 10:06:58 +02009497 assert(target != LYXP_SET_NODE_SET);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009498
9499 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vaskod3678892020-05-21 10:06:58 +02009500 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009501 return LY_EINVAL;
9502 }
9503
9504 /* to STRING */
Michal Vaskod3678892020-05-21 10:06:58 +02009505 if ((target == LYXP_SET_STRING) || ((target == LYXP_SET_NUMBER) && (set->type == LYXP_SET_NODE_SET))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02009506 switch (set->type) {
9507 case LYXP_SET_NUMBER:
9508 if (isnan(set->val.num)) {
9509 set->val.str = strdup("NaN");
9510 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
9511 } else if ((set->val.num == 0) || (set->val.num == -0.0f)) {
9512 set->val.str = strdup("0");
9513 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
9514 } else if (isinf(set->val.num) && !signbit(set->val.num)) {
9515 set->val.str = strdup("Infinity");
9516 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
9517 } else if (isinf(set->val.num) && signbit(set->val.num)) {
9518 set->val.str = strdup("-Infinity");
9519 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
9520 } else if ((long long)set->val.num == set->val.num) {
9521 if (asprintf(&str, "%lld", (long long)set->val.num) == -1) {
9522 LOGMEM_RET(set->ctx);
9523 }
9524 set->val.str = str;
9525 } else {
9526 if (asprintf(&str, "%03.1Lf", set->val.num) == -1) {
9527 LOGMEM_RET(set->ctx);
9528 }
9529 set->val.str = str;
9530 }
9531 break;
9532 case LYXP_SET_BOOLEAN:
Michal Vasko004d3152020-06-11 19:59:22 +02009533 if (set->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02009534 set->val.str = strdup("true");
9535 } else {
9536 set->val.str = strdup("false");
9537 }
9538 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), LY_EMEM);
9539 break;
9540 case LYXP_SET_NODE_SET:
Michal Vasko03ff5a72019-09-11 13:49:33 +02009541 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009542 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02009543
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009544 rc = cast_node_set_to_string(set, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009545 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02009546 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009547 set->val.str = str;
9548 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009549 default:
9550 LOGINT_RET(set->ctx);
9551 }
9552 set->type = LYXP_SET_STRING;
9553 }
9554
9555 /* to NUMBER */
9556 if (target == LYXP_SET_NUMBER) {
9557 switch (set->type) {
9558 case LYXP_SET_STRING:
9559 num = cast_string_to_number(set->val.str);
Michal Vaskod3678892020-05-21 10:06:58 +02009560 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009561 set->val.num = num;
9562 break;
9563 case LYXP_SET_BOOLEAN:
Michal Vasko004d3152020-06-11 19:59:22 +02009564 if (set->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02009565 set->val.num = 1;
9566 } else {
9567 set->val.num = 0;
9568 }
9569 break;
9570 default:
9571 LOGINT_RET(set->ctx);
9572 }
9573 set->type = LYXP_SET_NUMBER;
9574 }
9575
9576 /* to BOOLEAN */
9577 if (target == LYXP_SET_BOOLEAN) {
9578 switch (set->type) {
9579 case LYXP_SET_NUMBER:
9580 if ((set->val.num == 0) || (set->val.num == -0.0f) || isnan(set->val.num)) {
Michal Vasko004d3152020-06-11 19:59:22 +02009581 set->val.bln = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009582 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02009583 set->val.bln = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009584 }
9585 break;
9586 case LYXP_SET_STRING:
9587 if (set->val.str[0]) {
Michal Vaskod3678892020-05-21 10:06:58 +02009588 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02009589 set->val.bln = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009590 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02009591 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02009592 set->val.bln = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009593 }
9594 break;
9595 case LYXP_SET_NODE_SET:
Michal Vaskod3678892020-05-21 10:06:58 +02009596 if (set->used) {
9597 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02009598 set->val.bln = 1;
Michal Vaskod3678892020-05-21 10:06:58 +02009599 } else {
9600 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02009601 set->val.bln = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02009602 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02009603 break;
9604 default:
9605 LOGINT_RET(set->ctx);
9606 }
9607 set->type = LYXP_SET_BOOLEAN;
9608 }
9609
Michal Vasko03ff5a72019-09-11 13:49:33 +02009610 return LY_SUCCESS;
9611}
9612
9613LY_ERR
Michal Vasko400e9672021-01-11 13:39:17 +01009614lyxp_atomize(const struct ly_ctx *ctx, const struct lyxp_expr *exp, const struct lys_module *cur_mod,
Radek Krejci8df109d2021-04-23 12:19:08 +02009615 LY_VALUE_FORMAT format, void *prefix_data, const struct lysc_node *ctx_scnode, struct lyxp_set *set,
Michal Vasko400e9672021-01-11 13:39:17 +01009616 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02009617{
Radek Krejci2efc45b2020-12-22 16:25:44 +01009618 LY_ERR ret;
Michal Vasko004d3152020-06-11 19:59:22 +02009619 uint16_t tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009620
Michal Vasko400e9672021-01-11 13:39:17 +01009621 LY_CHECK_ARG_RET(ctx, ctx, exp, set, LY_EINVAL);
Radek Krejci8df109d2021-04-23 12:19:08 +02009622 if (!cur_mod && ((format == LY_VALUE_SCHEMA) || (format == LY_VALUE_SCHEMA_RESOLVED))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009623 LOGARG(NULL, "Current module must be set if schema format is used.");
9624 return LY_EINVAL;
Michal Vasko004d3152020-06-11 19:59:22 +02009625 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02009626
9627 /* prepare set for evaluation */
Michal Vasko03ff5a72019-09-11 13:49:33 +02009628 memset(set, 0, sizeof *set);
9629 set->type = LYXP_SET_SCNODE_SET;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009630 set->root_type = lyxp_get_root_type(NULL, ctx_scnode, options);
9631 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 +01009632 set->val.scnodes[0].in_ctx = LYXP_SET_SCNODE_START;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009633
Michal Vasko400e9672021-01-11 13:39:17 +01009634 set->ctx = (struct ly_ctx *)ctx;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009635 set->cur_scnode = ctx_scnode;
Michal Vasko6b26e742020-07-17 15:02:10 +02009636 for (set->context_op = ctx_scnode;
Radek Krejci0f969882020-08-21 16:56:47 +02009637 set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF));
9638 set->context_op = set->context_op->parent) {}
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009639 set->cur_mod = cur_mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009640 set->format = format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009641 set->prefix_data = prefix_data;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009642
Radek Krejciddace2c2021-01-08 11:30:56 +01009643 LOG_LOCSET(set->cur_scnode, NULL, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01009644
Michal Vasko03ff5a72019-09-11 13:49:33 +02009645 /* evaluate */
Radek Krejci2efc45b2020-12-22 16:25:44 +01009646 ret = eval_expr_select(exp, &tok_idx, 0, set, options);
9647
Radek Krejciddace2c2021-01-08 11:30:56 +01009648 LOG_LOCBACK(1, 0, 0, 0);
Radek Krejci2efc45b2020-12-22 16:25:44 +01009649 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009650}
Michal Vaskod43d71a2020-08-07 14:54:58 +02009651
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01009652LIBYANG_API_DEF const char *
Michal Vaskod43d71a2020-08-07 14:54:58 +02009653lyxp_get_expr(const struct lyxp_expr *path)
9654{
9655 if (!path) {
9656 return NULL;
9657 }
9658
9659 return path->expr;
9660}