blob: 328fb3a454ae4207d6969785572e8e968dcd436f [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 Vasko03ff5a72019-09-11 13:49:33 +020056
aPiecek96dc1e32021-10-08 15:45:59 +020057/* Functions are divided into the following basic classes:
58 *
59 * (re)parse functions:
60 * Parse functions parse the expression into
61 * tokens (syntactic analysis).
62 * Reparse functions perform semantic analysis
63 * (do not save the result, just a check) of
64 * the expression and fill repeat indices.
65 *
66 * warn functions:
67 * Warn functions check specific reasonable conditions for schema XPath
68 * and print a warning if they are not satisfied.
69 *
70 * moveto functions:
71 * They and only they actually change the context (set).
72 *
73 * eval functions:
74 * They execute a parsed XPath expression on some data subtree.
75 */
76
Michal Vasko03ff5a72019-09-11 13:49:33 +020077/**
78 * @brief Print the type of an XPath \p set.
79 *
80 * @param[in] set Set to use.
81 * @return Set type string.
82 */
83static const char *
84print_set_type(struct lyxp_set *set)
85{
86 switch (set->type) {
Michal Vasko03ff5a72019-09-11 13:49:33 +020087 case LYXP_SET_NODE_SET:
88 return "node set";
89 case LYXP_SET_SCNODE_SET:
90 return "schema node set";
91 case LYXP_SET_BOOLEAN:
92 return "boolean";
93 case LYXP_SET_NUMBER:
94 return "number";
95 case LYXP_SET_STRING:
96 return "string";
97 }
98
99 return NULL;
100}
101
Michal Vasko24cddf82020-06-01 08:17:01 +0200102const char *
Michal Vasko49fec8e2022-05-24 10:28:33 +0200103lyxp_token2str(enum lyxp_token tok)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200104{
105 switch (tok) {
106 case LYXP_TOKEN_PAR1:
107 return "(";
108 case LYXP_TOKEN_PAR2:
109 return ")";
110 case LYXP_TOKEN_BRACK1:
111 return "[";
112 case LYXP_TOKEN_BRACK2:
113 return "]";
114 case LYXP_TOKEN_DOT:
115 return ".";
116 case LYXP_TOKEN_DDOT:
117 return "..";
118 case LYXP_TOKEN_AT:
119 return "@";
120 case LYXP_TOKEN_COMMA:
121 return ",";
122 case LYXP_TOKEN_NAMETEST:
123 return "NameTest";
124 case LYXP_TOKEN_NODETYPE:
125 return "NodeType";
aPiecekfba75362021-10-07 12:39:48 +0200126 case LYXP_TOKEN_VARREF:
127 return "VariableReference";
Michal Vasko03ff5a72019-09-11 13:49:33 +0200128 case LYXP_TOKEN_FUNCNAME:
129 return "FunctionName";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200130 case LYXP_TOKEN_OPER_LOG:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200131 return "Operator(Logic)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200132 case LYXP_TOKEN_OPER_EQUAL:
133 return "Operator(Equal)";
134 case LYXP_TOKEN_OPER_NEQUAL:
135 return "Operator(Non-equal)";
136 case LYXP_TOKEN_OPER_COMP:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200137 return "Operator(Comparison)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200138 case LYXP_TOKEN_OPER_MATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200139 return "Operator(Math)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200140 case LYXP_TOKEN_OPER_UNI:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200141 return "Operator(Union)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200142 case LYXP_TOKEN_OPER_PATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200143 return "Operator(Path)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200144 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko14676352020-05-29 11:35:55 +0200145 return "Operator(Recursive Path)";
Michal Vasko03ff5a72019-09-11 13:49:33 +0200146 case LYXP_TOKEN_LITERAL:
147 return "Literal";
148 case LYXP_TOKEN_NUMBER:
149 return "Number";
150 default:
151 LOGINT(NULL);
152 return "";
153 }
154}
155
156/**
Michal Vasko49fec8e2022-05-24 10:28:33 +0200157 * @brief Transform string into an axis.
158 *
159 * @param[in] str String to transform.
160 * @param[in] str_len Length of @p str.
161 * @return Transformed axis.
162 */
163static enum lyxp_axis
164str2axis(const char *str, uint16_t str_len)
165{
166 switch (str_len) {
167 case 4:
168 assert(!strncmp("self", str, str_len));
169 return LYXP_AXIS_SELF;
170 case 5:
171 assert(!strncmp("child", str, str_len));
172 return LYXP_AXIS_CHILD;
173 case 6:
174 assert(!strncmp("parent", str, str_len));
175 return LYXP_AXIS_PARENT;
176 case 8:
177 assert(!strncmp("ancestor", str, str_len));
178 return LYXP_AXIS_ANCESTOR;
179 case 9:
180 if (str[0] == 'a') {
181 assert(!strncmp("attribute", str, str_len));
182 return LYXP_AXIS_ATTRIBUTE;
183 } else if (str[0] == 'f') {
184 assert(!strncmp("following", str, str_len));
185 return LYXP_AXIS_FOLLOWING;
186 } else {
187 assert(!strncmp("preceding", str, str_len));
188 return LYXP_AXIS_PRECEDING;
189 }
190 break;
191 case 10:
192 assert(!strncmp("descendant", str, str_len));
193 return LYXP_AXIS_DESCENDANT;
194 case 16:
195 assert(!strncmp("ancestor-or-self", str, str_len));
196 return LYXP_AXIS_ANCESTOR_OR_SELF;
197 case 17:
198 if (str[0] == 'f') {
199 assert(!strncmp("following-sibling", str, str_len));
200 return LYXP_AXIS_FOLLOWING_SIBLING;
201 } else {
202 assert(!strncmp("preceding-sibling", str, str_len));
203 return LYXP_AXIS_PRECEDING_SIBLING;
204 }
205 break;
206 case 18:
207 assert(!strncmp("descendant-or-self", str, str_len));
208 return LYXP_AXIS_DESCENDANT_OR_SELF;
209 }
210
211 LOGINT(NULL);
212 return 0;
213}
214
215/**
Michal Vasko03ff5a72019-09-11 13:49:33 +0200216 * @brief Print the whole expression \p exp to debug output.
217 *
218 * @param[in] exp Expression to use.
219 */
220static void
Michal Vasko40308e72020-10-20 16:38:40 +0200221print_expr_struct_debug(const struct lyxp_expr *exp)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200222{
Radek Krejcif13b87b2020-12-01 22:02:17 +0100223#define MSG_BUFFER_SIZE 128
224 char tmp[MSG_BUFFER_SIZE];
Michal Vasko1fdd8fa2021-01-08 09:21:45 +0100225 uint32_t i, j;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200226
Radek Krejci52b6d512020-10-12 12:33:17 +0200227 if (!exp || (ly_ll < LY_LLDBG)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200228 return;
229 }
230
231 LOGDBG(LY_LDGXPATH, "expression \"%s\":", exp->expr);
232 for (i = 0; i < exp->used; ++i) {
Michal Vasko49fec8e2022-05-24 10:28:33 +0200233 sprintf(tmp, "\ttoken %s, in expression \"%.*s\"", lyxp_token2str(exp->tokens[i]), exp->tok_len[i],
Michal Vasko69730152020-10-09 16:30:07 +0200234 &exp->expr[exp->tok_pos[i]]);
Michal Vasko23049552021-03-04 15:57:44 +0100235 if (exp->repeat && exp->repeat[i]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200236 sprintf(tmp + strlen(tmp), " (repeat %d", exp->repeat[i][0]);
237 for (j = 1; exp->repeat[i][j]; ++j) {
238 sprintf(tmp + strlen(tmp), ", %d", exp->repeat[i][j]);
239 }
240 strcat(tmp, ")");
241 }
242 LOGDBG(LY_LDGXPATH, tmp);
243 }
Radek Krejcif13b87b2020-12-01 22:02:17 +0100244#undef MSG_BUFFER_SIZE
Michal Vasko03ff5a72019-09-11 13:49:33 +0200245}
246
247#ifndef NDEBUG
248
249/**
250 * @brief Print XPath set content to debug output.
251 *
252 * @param[in] set Set to print.
253 */
254static void
255print_set_debug(struct lyxp_set *set)
256{
257 uint32_t i;
258 char *str;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200259 struct lyxp_set_node *item;
260 struct lyxp_set_scnode *sitem;
261
Radek Krejci52b6d512020-10-12 12:33:17 +0200262 if (ly_ll < LY_LLDBG) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200263 return;
264 }
265
266 switch (set->type) {
267 case LYXP_SET_NODE_SET:
268 LOGDBG(LY_LDGXPATH, "set NODE SET:");
269 for (i = 0; i < set->used; ++i) {
270 item = &set->val.nodes[i];
271
272 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +0100273 case LYXP_NODE_NONE:
274 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): NONE", i + 1, item->pos);
275 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200276 case LYXP_NODE_ROOT:
277 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT", i + 1, item->pos);
278 break;
279 case LYXP_NODE_ROOT_CONFIG:
280 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT CONFIG", i + 1, item->pos);
281 break;
282 case LYXP_NODE_ELEM:
Michal Vasko9e685082021-01-29 14:49:09 +0100283 if ((item->node->schema->nodetype == LYS_LIST) && (lyd_child(item->node)->schema->nodetype == LYS_LEAF)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200284 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (1st child val: %s)", i + 1, item->pos,
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200285 item->node->schema->name, lyd_get_value(lyd_child(item->node)));
Michal Vasko9e685082021-01-29 14:49:09 +0100286 } else if (item->node->schema->nodetype == LYS_LEAFLIST) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200287 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (val: %s)", i + 1, item->pos,
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200288 item->node->schema->name, lyd_get_value(item->node));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200289 } else {
290 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s", i + 1, item->pos, item->node->schema->name);
291 }
292 break;
293 case LYXP_NODE_TEXT:
294 if (item->node->schema->nodetype & LYS_ANYDATA) {
295 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): TEXT <%s>", i + 1, item->pos,
Michal Vasko69730152020-10-09 16:30:07 +0200296 item->node->schema->nodetype == LYS_ANYXML ? "anyxml" : "anydata");
Michal Vasko03ff5a72019-09-11 13:49:33 +0200297 } else {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200298 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 +0200299 }
300 break;
Michal Vasko9f96a052020-03-10 09:41:45 +0100301 case LYXP_NODE_META:
302 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 +0200303 set->val.meta[i].meta->value);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200304 break;
305 }
306 }
307 break;
308
309 case LYXP_SET_SCNODE_SET:
310 LOGDBG(LY_LDGXPATH, "set SCNODE SET:");
311 for (i = 0; i < set->used; ++i) {
312 sitem = &set->val.scnodes[i];
313
314 switch (sitem->type) {
315 case LYXP_NODE_ROOT:
316 LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT", i + 1, sitem->in_ctx);
317 break;
318 case LYXP_NODE_ROOT_CONFIG:
319 LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT CONFIG", i + 1, sitem->in_ctx);
320 break;
321 case LYXP_NODE_ELEM:
322 LOGDBG(LY_LDGXPATH, "\t%d (%u): ELEM %s", i + 1, sitem->in_ctx, sitem->scnode->name);
323 break;
324 default:
325 LOGINT(NULL);
326 break;
327 }
328 }
329 break;
330
Michal Vasko03ff5a72019-09-11 13:49:33 +0200331 case LYXP_SET_BOOLEAN:
332 LOGDBG(LY_LDGXPATH, "set BOOLEAN");
Michal Vasko004d3152020-06-11 19:59:22 +0200333 LOGDBG(LY_LDGXPATH, "\t%s", (set->val.bln ? "true" : "false"));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200334 break;
335
336 case LYXP_SET_STRING:
337 LOGDBG(LY_LDGXPATH, "set STRING");
338 LOGDBG(LY_LDGXPATH, "\t%s", set->val.str);
339 break;
340
341 case LYXP_SET_NUMBER:
342 LOGDBG(LY_LDGXPATH, "set NUMBER");
343
344 if (isnan(set->val.num)) {
345 str = strdup("NaN");
346 } else if ((set->val.num == 0) || (set->val.num == -0.0f)) {
347 str = strdup("0");
348 } else if (isinf(set->val.num) && !signbit(set->val.num)) {
349 str = strdup("Infinity");
350 } else if (isinf(set->val.num) && signbit(set->val.num)) {
351 str = strdup("-Infinity");
352 } else if ((long long)set->val.num == set->val.num) {
353 if (asprintf(&str, "%lld", (long long)set->val.num) == -1) {
354 str = NULL;
355 }
356 } else {
357 if (asprintf(&str, "%03.1Lf", set->val.num) == -1) {
358 str = NULL;
359 }
360 }
361 LY_CHECK_ERR_RET(!str, LOGMEM(NULL), );
362
363 LOGDBG(LY_LDGXPATH, "\t%s", str);
364 free(str);
365 }
366}
367
368#endif
369
370/**
371 * @brief Realloc the string \p str.
372 *
373 * @param[in] ctx libyang context for logging.
374 * @param[in] needed How much free space is required.
375 * @param[in,out] str Pointer to the string to use.
376 * @param[in,out] used Used bytes in \p str.
377 * @param[in,out] size Allocated bytes in \p str.
378 * @return LY_ERR
379 */
380static LY_ERR
Michal Vasko52927e22020-03-16 17:26:14 +0100381cast_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 +0200382{
383 if (*size - *used < needed) {
384 do {
385 if ((UINT16_MAX - *size) < LYXP_STRING_CAST_SIZE_STEP) {
386 LOGERR(ctx, LY_EINVAL, "XPath string length limit (%u) reached.", UINT16_MAX);
387 return LY_EINVAL;
388 }
389 *size += LYXP_STRING_CAST_SIZE_STEP;
390 } while (*size - *used < needed);
391 *str = ly_realloc(*str, *size * sizeof(char));
392 LY_CHECK_ERR_RET(!(*str), LOGMEM(ctx), LY_EMEM);
393 }
394
395 return LY_SUCCESS;
396}
397
398/**
399 * @brief Cast nodes recursively to one string @p str.
400 *
Michal Vasko47da6562022-07-14 15:43:15 +0200401 * @param[in] node Node to cast, NULL if root.
402 * @param[in] set XPath set.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200403 * @param[in] indent Current indent.
404 * @param[in,out] str Resulting string.
405 * @param[in,out] used Used bytes in @p str.
406 * @param[in,out] size Allocated bytes in @p str.
Michal Vasko47da6562022-07-14 15:43:15 +0200407 * @return LY_ERR value.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200408 */
409static LY_ERR
Michal Vasko47da6562022-07-14 15:43:15 +0200410cast_string_recursive(const struct lyd_node *node, struct lyxp_set *set, uint16_t indent, char **str, uint16_t *used,
411 uint16_t *size)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200412{
Radek Krejci7f769d72020-07-11 23:13:56 +0200413 char *buf, *line, *ptr = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200414 const char *value_str;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200415 const struct lyd_node *child;
Michal Vasko47da6562022-07-14 15:43:15 +0200416 enum lyxp_node_type child_type;
Michal Vasko60ea6352020-06-29 13:39:39 +0200417 struct lyd_node *tree;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200418 struct lyd_node_any *any;
419 LY_ERR rc;
420
Michal Vasko47da6562022-07-14 15:43:15 +0200421 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && node && (node->schema->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200422 return LY_SUCCESS;
423 }
424
Michal Vasko47da6562022-07-14 15:43:15 +0200425 if (!node) {
426 /* fake container */
427 LY_CHECK_RET(cast_string_realloc(set->ctx, 1, str, used, size));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200428 strcpy(*str + (*used - 1), "\n");
429 ++(*used);
430
431 ++indent;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200432
Michal Vasko47da6562022-07-14 15:43:15 +0200433 /* print all the top-level nodes */
434 child = NULL;
435 child_type = 0;
436 while (!moveto_axis_node_next(&child, &child_type, NULL, set->root_type, LYXP_AXIS_CHILD, set)) {
437 LY_CHECK_RET(cast_string_recursive(child, set, indent, str, used, size));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200438 }
439
Michal Vasko47da6562022-07-14 15:43:15 +0200440 /* end fake container */
441 LY_CHECK_RET(cast_string_realloc(set->ctx, 1, str, used, size));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200442 strcpy(*str + (*used - 1), "\n");
443 ++(*used);
444
445 --indent;
Michal Vasko47da6562022-07-14 15:43:15 +0200446 } else {
447 switch (node->schema->nodetype) {
448 case LYS_CONTAINER:
449 case LYS_LIST:
450 case LYS_RPC:
451 case LYS_NOTIF:
452 LY_CHECK_RET(cast_string_realloc(set->ctx, 1, str, used, size));
453 strcpy(*str + (*used - 1), "\n");
454 ++(*used);
455
456 for (child = lyd_child(node); child; child = child->next) {
457 LY_CHECK_RET(cast_string_recursive(child, set, indent + 1, str, used, size));
458 }
459
460 break;
461
462 case LYS_LEAF:
463 case LYS_LEAFLIST:
464 value_str = lyd_get_value(node);
465
466 /* print indent */
467 LY_CHECK_RET(cast_string_realloc(set->ctx, indent * 2 + strlen(value_str) + 1, str, used, size));
468 memset(*str + (*used - 1), ' ', indent * 2);
469 *used += indent * 2;
470
471 /* print value */
472 if (*used == 1) {
473 sprintf(*str + (*used - 1), "%s", value_str);
474 *used += strlen(value_str);
475 } else {
476 sprintf(*str + (*used - 1), "%s\n", value_str);
477 *used += strlen(value_str) + 1;
478 }
479
480 break;
481
482 case LYS_ANYXML:
483 case LYS_ANYDATA:
484 any = (struct lyd_node_any *)node;
485 if (!(void *)any->value.tree) {
486 /* no content */
487 buf = strdup("");
488 LY_CHECK_ERR_RET(!buf, LOGMEM(set->ctx), LY_EMEM);
489 } else {
490 struct ly_out *out;
491
492 if (any->value_type == LYD_ANYDATA_LYB) {
493 /* try to parse it into a data tree */
494 if (lyd_parse_data_mem((struct ly_ctx *)set->ctx, any->value.mem, LYD_LYB,
495 LYD_PARSE_ONLY | LYD_PARSE_STRICT, 0, &tree) == LY_SUCCESS) {
496 /* successfully parsed */
497 free(any->value.mem);
498 any->value.tree = tree;
499 any->value_type = LYD_ANYDATA_DATATREE;
500 }
501 /* error is covered by the following switch where LYD_ANYDATA_LYB causes failure */
502 }
503
504 switch (any->value_type) {
505 case LYD_ANYDATA_STRING:
506 case LYD_ANYDATA_XML:
507 case LYD_ANYDATA_JSON:
508 buf = strdup(any->value.json);
509 LY_CHECK_ERR_RET(!buf, LOGMEM(set->ctx), LY_EMEM);
510 break;
511 case LYD_ANYDATA_DATATREE:
512 LY_CHECK_RET(ly_out_new_memory(&buf, 0, &out));
513 rc = lyd_print_all(out, any->value.tree, LYD_XML, 0);
514 ly_out_free(out, NULL, 0);
515 LY_CHECK_RET(rc < 0, -rc);
516 break;
517 case LYD_ANYDATA_LYB:
518 LOGERR(set->ctx, LY_EINVAL, "Cannot convert LYB anydata into string.");
519 return LY_EINVAL;
520 }
521 }
522
523 line = strtok_r(buf, "\n", &ptr);
524 do {
525 rc = cast_string_realloc(set->ctx, indent * 2 + strlen(line) + 1, str, used, size);
526 if (rc != LY_SUCCESS) {
527 free(buf);
528 return rc;
529 }
530 memset(*str + (*used - 1), ' ', indent * 2);
531 *used += indent * 2;
532
533 strcpy(*str + (*used - 1), line);
534 *used += strlen(line);
535
536 strcpy(*str + (*used - 1), "\n");
537 *used += 1;
538 } while ((line = strtok_r(NULL, "\n", &ptr)));
539
540 free(buf);
541 break;
542
543 default:
544 LOGINT_RET(set->ctx);
545 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200546 }
547
548 return LY_SUCCESS;
549}
550
551/**
552 * @brief Cast an element into a string.
553 *
Michal Vasko47da6562022-07-14 15:43:15 +0200554 * @param[in] node Node to cast, NULL if root.
555 * @param[in] set XPath set.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200556 * @param[out] str Element cast to dynamically-allocated string.
557 * @return LY_ERR
558 */
559static LY_ERR
Michal Vasko47da6562022-07-14 15:43:15 +0200560cast_string_elem(const struct lyd_node *node, struct lyxp_set *set, char **str)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200561{
562 uint16_t used, size;
563 LY_ERR rc;
564
565 *str = malloc(LYXP_STRING_CAST_SIZE_START * sizeof(char));
Michal Vasko47da6562022-07-14 15:43:15 +0200566 LY_CHECK_ERR_RET(!*str, LOGMEM(set->ctx), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200567 (*str)[0] = '\0';
568 used = 1;
569 size = LYXP_STRING_CAST_SIZE_START;
570
Michal Vasko47da6562022-07-14 15:43:15 +0200571 rc = cast_string_recursive(node, set, 0, str, &used, &size);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200572 if (rc != LY_SUCCESS) {
573 free(*str);
574 return rc;
575 }
576
577 if (size > used) {
578 *str = ly_realloc(*str, used * sizeof(char));
Michal Vasko47da6562022-07-14 15:43:15 +0200579 LY_CHECK_ERR_RET(!*str, LOGMEM(set->ctx), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200580 }
581 return LY_SUCCESS;
582}
583
584/**
585 * @brief Cast a LYXP_SET_NODE_SET set into a string.
586 * Context position aware.
587 *
588 * @param[in] set Set to cast.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200589 * @param[out] str Cast dynamically-allocated string.
590 * @return LY_ERR
591 */
592static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100593cast_node_set_to_string(struct lyxp_set *set, char **str)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200594{
Michal Vaskoaa677062021-03-09 13:52:53 +0100595 if (!set->used) {
596 *str = strdup("");
597 if (!*str) {
598 LOGMEM_RET(set->ctx);
599 }
600 return LY_SUCCESS;
601 }
602
Michal Vasko03ff5a72019-09-11 13:49:33 +0200603 switch (set->val.nodes[0].type) {
Michal Vasko2caefc12019-11-14 16:07:56 +0100604 case LYXP_NODE_NONE:
605 /* invalid */
606 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200607 case LYXP_NODE_ROOT:
608 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200609 case LYXP_NODE_ELEM:
610 case LYXP_NODE_TEXT:
Michal Vasko47da6562022-07-14 15:43:15 +0200611 return cast_string_elem(set->val.nodes[0].node, set, str);
Michal Vasko9f96a052020-03-10 09:41:45 +0100612 case LYXP_NODE_META:
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200613 *str = strdup(lyd_get_meta_value(set->val.meta[0].meta));
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200614 if (!*str) {
615 LOGMEM_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200616 }
617 return LY_SUCCESS;
618 }
619
620 LOGINT_RET(set->ctx);
621}
622
623/**
624 * @brief Cast a string into an XPath number.
625 *
626 * @param[in] str String to use.
627 * @return Cast number.
628 */
629static long double
630cast_string_to_number(const char *str)
631{
632 long double num;
633 char *ptr;
634
635 errno = 0;
636 num = strtold(str, &ptr);
637 if (errno || *ptr) {
638 num = NAN;
639 }
640 return num;
641}
642
643/**
644 * @brief Callback for checking value equality.
645 *
Michal Vasko62524a92021-02-26 10:08:50 +0100646 * Implementation of ::lyht_value_equal_cb.
Radek Krejci857189e2020-09-01 13:26:36 +0200647 *
Michal Vasko03ff5a72019-09-11 13:49:33 +0200648 * @param[in] val1_p First value.
649 * @param[in] val2_p Second value.
650 * @param[in] mod Whether hash table is being modified.
651 * @param[in] cb_data Callback data.
Radek Krejci857189e2020-09-01 13:26:36 +0200652 * @return Boolean value whether values are equal or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200653 */
Radek Krejci857189e2020-09-01 13:26:36 +0200654static ly_bool
655set_values_equal_cb(void *val1_p, void *val2_p, ly_bool UNUSED(mod), void *UNUSED(cb_data))
Michal Vasko03ff5a72019-09-11 13:49:33 +0200656{
657 struct lyxp_set_hash_node *val1, *val2;
658
659 val1 = (struct lyxp_set_hash_node *)val1_p;
660 val2 = (struct lyxp_set_hash_node *)val2_p;
661
662 if ((val1->node == val2->node) && (val1->type == val2->type)) {
663 return 1;
664 }
665
666 return 0;
667}
668
669/**
670 * @brief Insert node and its hash into set.
671 *
672 * @param[in] set et to insert to.
673 * @param[in] node Node with hash.
674 * @param[in] type Node type.
675 */
676static void
677set_insert_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type)
678{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200679 LY_ERR r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200680 uint32_t i, hash;
681 struct lyxp_set_hash_node hnode;
682
683 if (!set->ht && (set->used >= LYD_HT_MIN_ITEMS)) {
684 /* create hash table and add all the nodes */
685 set->ht = lyht_new(1, sizeof(struct lyxp_set_hash_node), set_values_equal_cb, NULL, 1);
686 for (i = 0; i < set->used; ++i) {
687 hnode.node = set->val.nodes[i].node;
688 hnode.type = set->val.nodes[i].type;
689
690 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
691 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
692 hash = dict_hash_multi(hash, NULL, 0);
693
694 r = lyht_insert(set->ht, &hnode, hash, NULL);
695 assert(!r);
696 (void)r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200697
Michal Vasko9d6befd2019-12-11 14:56:56 +0100698 if (hnode.node == node) {
699 /* it was just added, do not add it twice */
700 node = NULL;
701 }
702 }
703 }
704
705 if (set->ht && node) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200706 /* add the new node into hash table */
707 hnode.node = node;
708 hnode.type = type;
709
710 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
711 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
712 hash = dict_hash_multi(hash, NULL, 0);
713
714 r = lyht_insert(set->ht, &hnode, hash, NULL);
715 assert(!r);
716 (void)r;
717 }
718}
719
720/**
721 * @brief Remove node and its hash from set.
722 *
723 * @param[in] set Set to remove from.
724 * @param[in] node Node to remove.
725 * @param[in] type Node type.
726 */
727static void
728set_remove_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type)
729{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200730 LY_ERR r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200731 struct lyxp_set_hash_node hnode;
732 uint32_t hash;
733
734 if (set->ht) {
735 hnode.node = node;
736 hnode.type = type;
737
738 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
739 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
740 hash = dict_hash_multi(hash, NULL, 0);
741
742 r = lyht_remove(set->ht, &hnode, hash);
743 assert(!r);
744 (void)r;
745
746 if (!set->ht->used) {
747 lyht_free(set->ht);
748 set->ht = NULL;
749 }
750 }
751}
752
753/**
754 * @brief Check whether node is in set based on its hash.
755 *
756 * @param[in] set Set to search in.
757 * @param[in] node Node to search for.
758 * @param[in] type Node type.
759 * @param[in] skip_idx Index in @p set to skip.
760 * @return LY_ERR
761 */
762static LY_ERR
763set_dup_node_hash_check(const struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type, int skip_idx)
764{
765 struct lyxp_set_hash_node hnode, *match_p;
766 uint32_t hash;
767
768 hnode.node = node;
769 hnode.type = type;
770
771 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
772 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
773 hash = dict_hash_multi(hash, NULL, 0);
774
775 if (!lyht_find(set->ht, &hnode, hash, (void **)&match_p)) {
776 if ((skip_idx > -1) && (set->val.nodes[skip_idx].node == match_p->node) && (set->val.nodes[skip_idx].type == match_p->type)) {
777 /* we found it on the index that should be skipped, find another */
778 hnode = *match_p;
779 if (lyht_find_next(set->ht, &hnode, hash, (void **)&match_p)) {
780 /* none other found */
781 return LY_SUCCESS;
782 }
783 }
784
785 return LY_EEXIST;
786 }
787
788 /* not found */
789 return LY_SUCCESS;
790}
791
Michal Vaskod3678892020-05-21 10:06:58 +0200792void
793lyxp_set_free_content(struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200794{
795 if (!set) {
796 return;
797 }
798
799 if (set->type == LYXP_SET_NODE_SET) {
800 free(set->val.nodes);
801 lyht_free(set->ht);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200802 } else if (set->type == LYXP_SET_SCNODE_SET) {
803 free(set->val.scnodes);
Michal Vaskod3678892020-05-21 10:06:58 +0200804 lyht_free(set->ht);
805 } else {
806 if (set->type == LYXP_SET_STRING) {
807 free(set->val.str);
808 }
809 set->type = LYXP_SET_NODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200810 }
Michal Vaskod3678892020-05-21 10:06:58 +0200811
812 set->val.nodes = NULL;
813 set->used = 0;
814 set->size = 0;
815 set->ht = NULL;
816 set->ctx_pos = 0;
aPiecek748da732021-06-01 09:56:43 +0200817 set->ctx_size = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200818}
819
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100820/**
821 * @brief Free dynamically-allocated set.
822 *
823 * @param[in] set Set to free.
824 */
825static void
Michal Vasko03ff5a72019-09-11 13:49:33 +0200826lyxp_set_free(struct lyxp_set *set)
827{
828 if (!set) {
829 return;
830 }
831
Michal Vaskod3678892020-05-21 10:06:58 +0200832 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200833 free(set);
834}
835
836/**
837 * @brief Initialize set context.
838 *
839 * @param[in] new Set to initialize.
840 * @param[in] set Arbitrary initialized set.
841 */
842static void
Michal Vasko4c7763f2020-07-27 17:40:37 +0200843set_init(struct lyxp_set *new, const struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200844{
845 memset(new, 0, sizeof *new);
Michal Vasko02a77382019-09-12 11:47:35 +0200846 if (set) {
847 new->ctx = set->ctx;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200848 new->cur_node = set->cur_node;
Michal Vasko588112f2019-12-10 14:51:53 +0100849 new->root_type = set->root_type;
Michal Vasko6b26e742020-07-17 15:02:10 +0200850 new->context_op = set->context_op;
Michal Vaskof03ed032020-03-04 13:31:44 +0100851 new->tree = set->tree;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200852 new->cur_mod = set->cur_mod;
Michal Vasko02a77382019-09-12 11:47:35 +0200853 new->format = set->format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200854 new->prefix_data = set->prefix_data;
aPiecekfba75362021-10-07 12:39:48 +0200855 new->vars = set->vars;
Michal Vasko02a77382019-09-12 11:47:35 +0200856 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200857}
858
859/**
860 * @brief Create a deep copy of a set.
861 *
862 * @param[in] set Set to copy.
863 * @return Copy of @p set.
864 */
865static struct lyxp_set *
866set_copy(struct lyxp_set *set)
867{
868 struct lyxp_set *ret;
Michal Vasko1fdd8fa2021-01-08 09:21:45 +0100869 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200870
871 if (!set) {
872 return NULL;
873 }
874
875 ret = malloc(sizeof *ret);
876 LY_CHECK_ERR_RET(!ret, LOGMEM(set->ctx), NULL);
877 set_init(ret, set);
878
879 if (set->type == LYXP_SET_SCNODE_SET) {
880 ret->type = set->type;
881
882 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100883 if ((set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) ||
884 (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +0200885 uint32_t idx;
886 LY_CHECK_ERR_RET(lyxp_set_scnode_insert_node(ret, set->val.scnodes[i].scnode, set->val.scnodes[i].type, &idx),
887 lyxp_set_free(ret), NULL);
Michal Vasko3f27c522020-01-06 08:37:49 +0100888 /* coverity seems to think scnodes can be NULL */
Radek Krejciaa6b53f2020-08-27 15:19:03 +0200889 if (!ret->val.scnodes) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200890 lyxp_set_free(ret);
891 return NULL;
892 }
Michal Vaskoba716542019-12-16 10:01:58 +0100893 ret->val.scnodes[idx].in_ctx = set->val.scnodes[i].in_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200894 }
895 }
896 } else if (set->type == LYXP_SET_NODE_SET) {
897 ret->type = set->type;
Michal Vasko08e9b112021-06-11 15:41:17 +0200898 if (set->used) {
899 ret->val.nodes = malloc(set->used * sizeof *ret->val.nodes);
900 LY_CHECK_ERR_RET(!ret->val.nodes, LOGMEM(set->ctx); free(ret), NULL);
901 memcpy(ret->val.nodes, set->val.nodes, set->used * sizeof *ret->val.nodes);
902 } else {
903 ret->val.nodes = NULL;
904 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200905
906 ret->used = ret->size = set->used;
907 ret->ctx_pos = set->ctx_pos;
908 ret->ctx_size = set->ctx_size;
Michal Vasko4a04e542021-02-01 08:58:30 +0100909 if (set->ht) {
910 ret->ht = lyht_dup(set->ht);
911 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200912 } else {
Radek Krejci0f969882020-08-21 16:56:47 +0200913 memcpy(ret, set, sizeof *ret);
914 if (set->type == LYXP_SET_STRING) {
915 ret->val.str = strdup(set->val.str);
916 LY_CHECK_ERR_RET(!ret->val.str, LOGMEM(set->ctx); free(ret), NULL);
917 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200918 }
919
920 return ret;
921}
922
923/**
924 * @brief Fill XPath set with a string. Any current data are disposed of.
925 *
926 * @param[in] set Set to fill.
927 * @param[in] string String to fill into \p set.
928 * @param[in] str_len Length of \p string. 0 is a valid value!
929 */
930static void
931set_fill_string(struct lyxp_set *set, const char *string, uint16_t str_len)
932{
Michal Vaskod3678892020-05-21 10:06:58 +0200933 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200934
935 set->type = LYXP_SET_STRING;
936 if ((str_len == 0) && (string[0] != '\0')) {
937 string = "";
938 }
939 set->val.str = strndup(string, str_len);
940}
941
942/**
943 * @brief Fill XPath set with a number. Any current data are disposed of.
944 *
945 * @param[in] set Set to fill.
946 * @param[in] number Number to fill into \p set.
947 */
948static void
949set_fill_number(struct lyxp_set *set, long double number)
950{
Michal Vaskod3678892020-05-21 10:06:58 +0200951 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200952
953 set->type = LYXP_SET_NUMBER;
954 set->val.num = number;
955}
956
957/**
958 * @brief Fill XPath set with a boolean. Any current data are disposed of.
959 *
960 * @param[in] set Set to fill.
961 * @param[in] boolean Boolean to fill into \p set.
962 */
963static void
Radek Krejci857189e2020-09-01 13:26:36 +0200964set_fill_boolean(struct lyxp_set *set, ly_bool boolean)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200965{
Michal Vaskod3678892020-05-21 10:06:58 +0200966 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200967
968 set->type = LYXP_SET_BOOLEAN;
Michal Vasko004d3152020-06-11 19:59:22 +0200969 set->val.bln = boolean;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200970}
971
972/**
973 * @brief Fill XPath set with the value from another set (deep assign).
974 * Any current data are disposed of.
975 *
976 * @param[in] trg Set to fill.
977 * @param[in] src Source set to copy into \p trg.
978 */
979static void
Michal Vasko4c7763f2020-07-27 17:40:37 +0200980set_fill_set(struct lyxp_set *trg, const struct lyxp_set *src)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200981{
982 if (!trg || !src) {
983 return;
984 }
985
986 if (trg->type == LYXP_SET_NODE_SET) {
987 free(trg->val.nodes);
988 } else if (trg->type == LYXP_SET_STRING) {
989 free(trg->val.str);
990 }
991 set_init(trg, src);
992
993 if (src->type == LYXP_SET_SCNODE_SET) {
994 trg->type = LYXP_SET_SCNODE_SET;
995 trg->used = src->used;
996 trg->size = src->used;
997
Michal Vasko08e9b112021-06-11 15:41:17 +0200998 if (trg->size) {
999 trg->val.scnodes = ly_realloc(trg->val.scnodes, trg->size * sizeof *trg->val.scnodes);
1000 LY_CHECK_ERR_RET(!trg->val.scnodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), );
1001 memcpy(trg->val.scnodes, src->val.scnodes, src->used * sizeof *src->val.scnodes);
1002 } else {
1003 trg->val.scnodes = NULL;
1004 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001005 } else if (src->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +02001006 set_fill_boolean(trg, src->val.bln);
Michal Vasko44f3d2c2020-08-24 09:49:38 +02001007 } else if (src->type == LYXP_SET_NUMBER) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001008 set_fill_number(trg, src->val.num);
1009 } else if (src->type == LYXP_SET_STRING) {
1010 set_fill_string(trg, src->val.str, strlen(src->val.str));
1011 } else {
1012 if (trg->type == LYXP_SET_NODE_SET) {
1013 free(trg->val.nodes);
1014 } else if (trg->type == LYXP_SET_STRING) {
1015 free(trg->val.str);
1016 }
1017
Michal Vaskod3678892020-05-21 10:06:58 +02001018 assert(src->type == LYXP_SET_NODE_SET);
1019
1020 trg->type = LYXP_SET_NODE_SET;
1021 trg->used = src->used;
1022 trg->size = src->used;
1023 trg->ctx_pos = src->ctx_pos;
1024 trg->ctx_size = src->ctx_size;
1025
Michal Vasko08e9b112021-06-11 15:41:17 +02001026 if (trg->size) {
1027 trg->val.nodes = malloc(trg->size * sizeof *trg->val.nodes);
1028 LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), );
1029 memcpy(trg->val.nodes, src->val.nodes, src->used * sizeof *src->val.nodes);
1030 } else {
1031 trg->val.nodes = NULL;
1032 }
Michal Vaskod3678892020-05-21 10:06:58 +02001033 if (src->ht) {
1034 trg->ht = lyht_dup(src->ht);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001035 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02001036 trg->ht = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001037 }
1038 }
1039}
1040
1041/**
1042 * @brief Clear context of all schema nodes.
1043 *
1044 * @param[in] set Set to clear.
Michal Vasko1a09b212021-05-06 13:00:10 +02001045 * @param[in] new_ctx New context state for all the nodes currently in the context.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001046 */
1047static void
Michal Vasko1a09b212021-05-06 13:00:10 +02001048set_scnode_clear_ctx(struct lyxp_set *set, int32_t new_ctx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001049{
1050 uint32_t i;
1051
1052 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001053 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko1a09b212021-05-06 13:00:10 +02001054 set->val.scnodes[i].in_ctx = new_ctx;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001055 } else if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START) {
1056 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001057 }
1058 }
1059}
1060
1061/**
1062 * @brief Remove a node from a set. Removing last node changes
1063 * set into LYXP_SET_EMPTY. Context position aware.
1064 *
1065 * @param[in] set Set to use.
1066 * @param[in] idx Index from @p set of the node to be removed.
1067 */
1068static void
1069set_remove_node(struct lyxp_set *set, uint32_t idx)
1070{
1071 assert(set && (set->type == LYXP_SET_NODE_SET));
1072 assert(idx < set->used);
1073
1074 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
1075
1076 --set->used;
Michal Vasko08e9b112021-06-11 15:41:17 +02001077 if (idx < set->used) {
1078 memmove(&set->val.nodes[idx], &set->val.nodes[idx + 1], (set->used - idx) * sizeof *set->val.nodes);
1079 } else if (!set->used) {
Michal Vaskod3678892020-05-21 10:06:58 +02001080 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001081 }
1082}
1083
1084/**
Michal Vasko2caefc12019-11-14 16:07:56 +01001085 * @brief Remove a node from a set by setting its type to LYXP_NODE_NONE.
Michal Vasko57eab132019-09-24 11:46:26 +02001086 *
1087 * @param[in] set Set to use.
1088 * @param[in] idx Index from @p set of the node to be removed.
1089 */
1090static void
Michal Vasko2caefc12019-11-14 16:07:56 +01001091set_remove_node_none(struct lyxp_set *set, uint32_t idx)
Michal Vasko57eab132019-09-24 11:46:26 +02001092{
1093 assert(set && (set->type == LYXP_SET_NODE_SET));
1094 assert(idx < set->used);
1095
Michal Vasko2caefc12019-11-14 16:07:56 +01001096 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1097 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
1098 }
1099 set->val.nodes[idx].type = LYXP_NODE_NONE;
Michal Vasko57eab132019-09-24 11:46:26 +02001100}
1101
1102/**
Michal Vasko2caefc12019-11-14 16:07:56 +01001103 * @brief Remove all LYXP_NODE_NONE nodes from a set. Removing last node changes
Michal Vasko57eab132019-09-24 11:46:26 +02001104 * set into LYXP_SET_EMPTY. Context position aware.
1105 *
1106 * @param[in] set Set to consolidate.
1107 */
1108static void
Michal Vasko2caefc12019-11-14 16:07:56 +01001109set_remove_nodes_none(struct lyxp_set *set)
Michal Vasko57eab132019-09-24 11:46:26 +02001110{
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01001111 uint32_t i, orig_used, end = 0;
1112 int64_t start;
Michal Vasko57eab132019-09-24 11:46:26 +02001113
Michal Vaskod3678892020-05-21 10:06:58 +02001114 assert(set);
Michal Vasko57eab132019-09-24 11:46:26 +02001115
1116 orig_used = set->used;
1117 set->used = 0;
Michal Vaskod989ba02020-08-24 10:59:24 +02001118 for (i = 0; i < orig_used; ) {
Michal Vasko57eab132019-09-24 11:46:26 +02001119 start = -1;
1120 do {
Michal Vasko2caefc12019-11-14 16:07:56 +01001121 if ((set->val.nodes[i].type != LYXP_NODE_NONE) && (start == -1)) {
Michal Vasko57eab132019-09-24 11:46:26 +02001122 start = i;
Michal Vasko2caefc12019-11-14 16:07:56 +01001123 } else if ((start > -1) && (set->val.nodes[i].type == LYXP_NODE_NONE)) {
Michal Vasko57eab132019-09-24 11:46:26 +02001124 end = i;
1125 ++i;
1126 break;
1127 }
1128
1129 ++i;
1130 if (i == orig_used) {
1131 end = i;
1132 }
1133 } while (i < orig_used);
1134
1135 if (start > -1) {
1136 /* move the whole chunk of valid nodes together */
1137 if (set->used != (unsigned)start) {
1138 memmove(&set->val.nodes[set->used], &set->val.nodes[start], (end - start) * sizeof *set->val.nodes);
1139 }
1140 set->used += end - start;
1141 }
1142 }
Michal Vasko57eab132019-09-24 11:46:26 +02001143}
1144
1145/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02001146 * @brief Check for duplicates in a node set.
1147 *
1148 * @param[in] set Set to check.
1149 * @param[in] node Node to look for in @p set.
1150 * @param[in] node_type Type of @p node.
1151 * @param[in] skip_idx Index from @p set to skip.
1152 * @return LY_ERR
1153 */
1154static LY_ERR
1155set_dup_node_check(const struct lyxp_set *set, const struct lyd_node *node, enum lyxp_node_type node_type, int skip_idx)
1156{
1157 uint32_t i;
1158
Michal Vasko2caefc12019-11-14 16:07:56 +01001159 if (set->ht && node) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001160 return set_dup_node_hash_check(set, (struct lyd_node *)node, node_type, skip_idx);
1161 }
1162
1163 for (i = 0; i < set->used; ++i) {
1164 if ((skip_idx > -1) && (i == (unsigned)skip_idx)) {
1165 continue;
1166 }
1167
1168 if ((set->val.nodes[i].node == node) && (set->val.nodes[i].type == node_type)) {
1169 return LY_EEXIST;
1170 }
1171 }
1172
1173 return LY_SUCCESS;
1174}
1175
Radek Krejci857189e2020-09-01 13:26:36 +02001176ly_bool
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001177lyxp_set_scnode_contains(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type, int skip_idx,
1178 uint32_t *index_p)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001179{
1180 uint32_t i;
1181
1182 for (i = 0; i < set->used; ++i) {
1183 if ((skip_idx > -1) && (i == (unsigned)skip_idx)) {
1184 continue;
1185 }
1186
1187 if ((set->val.scnodes[i].scnode == node) && (set->val.scnodes[i].type == node_type)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001188 if (index_p) {
1189 *index_p = i;
1190 }
1191 return 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001192 }
1193 }
1194
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001195 return 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001196}
1197
Michal Vaskoecd62de2019-11-13 12:35:11 +01001198void
1199lyxp_set_scnode_merge(struct lyxp_set *set1, struct lyxp_set *set2)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001200{
1201 uint32_t orig_used, i, j;
1202
Michal Vaskod3678892020-05-21 10:06:58 +02001203 assert((set1->type == LYXP_SET_SCNODE_SET) && (set2->type == LYXP_SET_SCNODE_SET));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001204
Michal Vaskod3678892020-05-21 10:06:58 +02001205 if (!set2->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001206 return;
1207 }
1208
Michal Vaskod3678892020-05-21 10:06:58 +02001209 if (!set1->used) {
aPiecekadc1e4f2021-10-07 11:15:12 +02001210 /* release hidden allocated data (lyxp_set.size) */
1211 lyxp_set_free_content(set1);
1212 /* direct copying of the entire structure */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001213 memcpy(set1, set2, sizeof *set1);
1214 return;
1215 }
1216
1217 if (set1->used + set2->used > set1->size) {
1218 set1->size = set1->used + set2->used;
1219 set1->val.scnodes = ly_realloc(set1->val.scnodes, set1->size * sizeof *set1->val.scnodes);
1220 LY_CHECK_ERR_RET(!set1->val.scnodes, LOGMEM(set1->ctx), );
1221 }
1222
1223 orig_used = set1->used;
1224
1225 for (i = 0; i < set2->used; ++i) {
1226 for (j = 0; j < orig_used; ++j) {
1227 /* detect duplicities */
1228 if (set1->val.scnodes[j].scnode == set2->val.scnodes[i].scnode) {
1229 break;
1230 }
1231 }
1232
Michal Vasko0587bce2020-12-10 12:19:21 +01001233 if (j < orig_used) {
1234 /* node is there, but update its status if needed */
1235 if (set1->val.scnodes[j].in_ctx == LYXP_SET_SCNODE_START_USED) {
1236 set1->val.scnodes[j].in_ctx = set2->val.scnodes[i].in_ctx;
Michal Vasko1a09b212021-05-06 13:00:10 +02001237 } else if ((set1->val.scnodes[j].in_ctx == LYXP_SET_SCNODE_ATOM_NODE) &&
1238 (set2->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_VAL)) {
1239 set1->val.scnodes[j].in_ctx = set2->val.scnodes[i].in_ctx;
Michal Vasko0587bce2020-12-10 12:19:21 +01001240 }
1241 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001242 memcpy(&set1->val.scnodes[set1->used], &set2->val.scnodes[i], sizeof *set2->val.scnodes);
1243 ++set1->used;
1244 }
1245 }
1246
Michal Vaskod3678892020-05-21 10:06:58 +02001247 lyxp_set_free_content(set2);
1248 set2->type = LYXP_SET_SCNODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001249}
1250
1251/**
1252 * @brief Insert a node into a set. Context position aware.
1253 *
1254 * @param[in] set Set to use.
1255 * @param[in] node Node to insert to @p set.
1256 * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting.
1257 * @param[in] node_type Node type of @p node.
1258 * @param[in] idx Index in @p set to insert into.
1259 */
1260static void
1261set_insert_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx)
1262{
Michal Vaskod3678892020-05-21 10:06:58 +02001263 assert(set && (set->type == LYXP_SET_NODE_SET));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001264
Michal Vaskod3678892020-05-21 10:06:58 +02001265 if (!set->size) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001266 /* first item */
1267 if (idx) {
1268 /* no real harm done, but it is a bug */
1269 LOGINT(set->ctx);
1270 idx = 0;
1271 }
1272 set->val.nodes = malloc(LYXP_SET_SIZE_START * sizeof *set->val.nodes);
1273 LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), );
1274 set->type = LYXP_SET_NODE_SET;
1275 set->used = 0;
1276 set->size = LYXP_SET_SIZE_START;
1277 set->ctx_pos = 1;
1278 set->ctx_size = 1;
1279 set->ht = NULL;
1280 } else {
1281 /* not an empty set */
1282 if (set->used == set->size) {
1283
1284 /* set is full */
Michal Vasko871df522022-04-06 12:14:41 +02001285 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 +02001286 LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), );
Michal Vasko871df522022-04-06 12:14:41 +02001287 set->size *= LYXP_SET_SIZE_MUL_STEP;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001288 }
1289
1290 if (idx > set->used) {
1291 LOGINT(set->ctx);
1292 idx = set->used;
1293 }
1294
1295 /* make space for the new node */
1296 if (idx < set->used) {
1297 memmove(&set->val.nodes[idx + 1], &set->val.nodes[idx], (set->used - idx) * sizeof *set->val.nodes);
1298 }
1299 }
1300
1301 /* finally assign the value */
1302 set->val.nodes[idx].node = (struct lyd_node *)node;
1303 set->val.nodes[idx].type = node_type;
1304 set->val.nodes[idx].pos = pos;
1305 ++set->used;
1306
Michal Vasko2416fdd2021-10-01 11:07:10 +02001307 /* add into hash table */
1308 set_insert_node_hash(set, (struct lyd_node *)node, node_type);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001309}
1310
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001311LY_ERR
Michal Vaskoddd76592022-01-17 13:34:48 +01001312lyxp_set_scnode_insert_node(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type,
1313 uint32_t *index_p)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001314{
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001315 uint32_t index;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001316
1317 assert(set->type == LYXP_SET_SCNODE_SET);
1318
Michal Vasko871df522022-04-06 12:14:41 +02001319 if (!set->size) {
1320 /* first item */
1321 set->val.scnodes = malloc(LYXP_SET_SIZE_START * sizeof *set->val.scnodes);
1322 LY_CHECK_ERR_RET(!set->val.scnodes, LOGMEM(set->ctx), LY_EMEM);
1323 set->type = LYXP_SET_SCNODE_SET;
1324 set->used = 0;
1325 set->size = LYXP_SET_SIZE_START;
1326 set->ctx_pos = 1;
1327 set->ctx_size = 1;
1328 set->ht = NULL;
1329 }
1330
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001331 if (lyxp_set_scnode_contains(set, node, node_type, -1, &index)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001332 set->val.scnodes[index].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001333 } else {
1334 if (set->used == set->size) {
Michal Vasko871df522022-04-06 12:14:41 +02001335 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 +02001336 LY_CHECK_ERR_RET(!set->val.scnodes, LOGMEM(set->ctx), LY_EMEM);
Michal Vasko871df522022-04-06 12:14:41 +02001337 set->size *= LYXP_SET_SIZE_MUL_STEP;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001338 }
1339
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001340 index = set->used;
1341 set->val.scnodes[index].scnode = (struct lysc_node *)node;
1342 set->val.scnodes[index].type = node_type;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001343 set->val.scnodes[index].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001344 ++set->used;
1345 }
1346
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001347 if (index_p) {
1348 *index_p = index;
1349 }
1350
1351 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001352}
1353
1354/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02001355 * @brief Set all nodes with ctx 1 to a new unique context value.
1356 *
1357 * @param[in] set Set to modify.
1358 * @return New context value.
1359 */
Michal Vasko5c4e5892019-11-14 12:31:38 +01001360static int32_t
Michal Vasko03ff5a72019-09-11 13:49:33 +02001361set_scnode_new_in_ctx(struct lyxp_set *set)
1362{
Michal Vasko5c4e5892019-11-14 12:31:38 +01001363 uint32_t i;
1364 int32_t ret_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001365
1366 assert(set->type == LYXP_SET_SCNODE_SET);
1367
Radek Krejcif13b87b2020-12-01 22:02:17 +01001368 ret_ctx = LYXP_SET_SCNODE_ATOM_PRED_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001369retry:
1370 for (i = 0; i < set->used; ++i) {
1371 if (set->val.scnodes[i].in_ctx >= ret_ctx) {
1372 ret_ctx = set->val.scnodes[i].in_ctx + 1;
1373 goto retry;
1374 }
1375 }
1376 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001377 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001378 set->val.scnodes[i].in_ctx = ret_ctx;
1379 }
1380 }
1381
1382 return ret_ctx;
1383}
1384
1385/**
1386 * @brief Get unique @p node position in the data.
1387 *
1388 * @param[in] node Node to find.
1389 * @param[in] node_type Node type of @p node.
1390 * @param[in] root Root node.
1391 * @param[in] root_type Type of the XPath @p root node.
1392 * @param[in] prev Node that we think is before @p node in DFS from @p root. Can optionally
1393 * be used to increase efficiency and start the DFS from this node.
1394 * @param[in] prev_pos Node @p prev position. Optional, but must be set if @p prev is set.
1395 * @return Node position.
1396 */
1397static uint32_t
1398get_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 +02001399 enum lyxp_node_type root_type, const struct lyd_node **prev, uint32_t *prev_pos)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001400{
Michal Vasko56daf732020-08-10 10:57:18 +02001401 const struct lyd_node *elem = NULL, *top_sibling;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001402 uint32_t pos = 1;
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001403 ly_bool found = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001404
1405 assert(prev && prev_pos && !root->prev->next);
1406
1407 if ((node_type == LYXP_NODE_ROOT) || (node_type == LYXP_NODE_ROOT_CONFIG)) {
1408 return 0;
1409 }
1410
1411 if (*prev) {
1412 /* start from the previous element instead from the root */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001413 pos = *prev_pos;
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001414 for (top_sibling = *prev; top_sibling->parent; top_sibling = lyd_parent(top_sibling)) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001415 goto dfs_search;
1416 }
1417
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001418 LY_LIST_FOR(root, top_sibling) {
Michal Vasko56daf732020-08-10 10:57:18 +02001419 LYD_TREE_DFS_BEGIN(top_sibling, elem) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001420dfs_search:
Michal Vaskoa9309bb2021-07-09 09:31:55 +02001421 LYD_TREE_DFS_continue = 0;
1422
Michal Vasko56daf732020-08-10 10:57:18 +02001423 if (*prev && !elem) {
1424 /* resume previous DFS */
1425 elem = LYD_TREE_DFS_next = (struct lyd_node *)*prev;
1426 LYD_TREE_DFS_continue = 0;
1427 }
1428
Michal Vasko482a6822022-05-06 08:56:12 +02001429 if (!elem->schema || ((root_type == LYXP_NODE_ROOT_CONFIG) && (elem->schema->flags & LYS_CONFIG_R))) {
Michal Vasko56daf732020-08-10 10:57:18 +02001430 /* skip */
1431 LYD_TREE_DFS_continue = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001432 } else {
Michal Vasko56daf732020-08-10 10:57:18 +02001433 if (elem == node) {
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001434 found = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001435 break;
1436 }
Michal Vasko56daf732020-08-10 10:57:18 +02001437 ++pos;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001438 }
Michal Vasko56daf732020-08-10 10:57:18 +02001439
1440 LYD_TREE_DFS_END(top_sibling, elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001441 }
1442
1443 /* node found */
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001444 if (found) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001445 break;
1446 }
1447 }
1448
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001449 if (!found) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001450 if (!(*prev)) {
1451 /* we went from root and failed to find it, cannot be */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001452 LOGINT(LYD_CTX(node));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001453 return 0;
1454 } else {
Michal Vasko56daf732020-08-10 10:57:18 +02001455 /* start the search again from the beginning */
1456 *prev = root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001457
Michal Vasko56daf732020-08-10 10:57:18 +02001458 top_sibling = root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001459 pos = 1;
1460 goto dfs_search;
1461 }
1462 }
1463
1464 /* remember the last found node for next time */
1465 *prev = node;
1466 *prev_pos = pos;
1467
1468 return pos;
1469}
1470
1471/**
1472 * @brief Assign (fill) missing node positions.
1473 *
1474 * @param[in] set Set to fill positions in.
1475 * @param[in] root Context root node.
1476 * @param[in] root_type Context root type.
1477 * @return LY_ERR
1478 */
1479static LY_ERR
1480set_assign_pos(struct lyxp_set *set, const struct lyd_node *root, enum lyxp_node_type root_type)
1481{
1482 const struct lyd_node *prev = NULL, *tmp_node;
1483 uint32_t i, tmp_pos = 0;
1484
1485 for (i = 0; i < set->used; ++i) {
1486 if (!set->val.nodes[i].pos) {
1487 tmp_node = NULL;
1488 switch (set->val.nodes[i].type) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001489 case LYXP_NODE_META:
1490 tmp_node = set->val.meta[i].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001491 if (!tmp_node) {
1492 LOGINT_RET(root->schema->module->ctx);
1493 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01001494 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001495 case LYXP_NODE_ELEM:
1496 case LYXP_NODE_TEXT:
1497 if (!tmp_node) {
1498 tmp_node = set->val.nodes[i].node;
1499 }
1500 set->val.nodes[i].pos = get_node_pos(tmp_node, set->val.nodes[i].type, root, root_type, &prev, &tmp_pos);
1501 break;
1502 default:
1503 /* all roots have position 0 */
1504 break;
1505 }
1506 }
1507 }
1508
1509 return LY_SUCCESS;
1510}
1511
1512/**
Michal Vasko9f96a052020-03-10 09:41:45 +01001513 * @brief Get unique @p meta position in the parent metadata.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001514 *
Michal Vasko9f96a052020-03-10 09:41:45 +01001515 * @param[in] meta Metadata to use.
1516 * @return Metadata position.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001517 */
1518static uint16_t
Michal Vasko9f96a052020-03-10 09:41:45 +01001519get_meta_pos(struct lyd_meta *meta)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001520{
1521 uint16_t pos = 0;
Michal Vasko9f96a052020-03-10 09:41:45 +01001522 struct lyd_meta *meta2;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001523
Michal Vasko9f96a052020-03-10 09:41:45 +01001524 for (meta2 = meta->parent->meta; meta2 && (meta2 != meta); meta2 = meta2->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001525 ++pos;
1526 }
1527
Michal Vasko9f96a052020-03-10 09:41:45 +01001528 assert(meta2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001529 return pos;
1530}
1531
1532/**
1533 * @brief Compare 2 nodes in respect to XPath document order.
1534 *
1535 * @param[in] item1 1st node.
1536 * @param[in] item2 2nd node.
1537 * @return If 1st > 2nd returns 1, 1st == 2nd returns 0, and 1st < 2nd returns -1.
1538 */
1539static int
1540set_sort_compare(struct lyxp_set_node *item1, struct lyxp_set_node *item2)
1541{
Michal Vasko9f96a052020-03-10 09:41:45 +01001542 uint32_t meta_pos1 = 0, meta_pos2 = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001543
1544 if (item1->pos < item2->pos) {
1545 return -1;
1546 }
1547
1548 if (item1->pos > item2->pos) {
1549 return 1;
1550 }
1551
1552 /* node positions are equal, the fun case */
1553
1554 /* 1st ELEM - == - 2nd TEXT, 1st TEXT - == - 2nd ELEM */
1555 /* special case since text nodes are actually saved as their parents */
1556 if ((item1->node == item2->node) && (item1->type != item2->type)) {
1557 if (item1->type == LYXP_NODE_ELEM) {
1558 assert(item2->type == LYXP_NODE_TEXT);
1559 return -1;
1560 } else {
1561 assert((item1->type == LYXP_NODE_TEXT) && (item2->type == LYXP_NODE_ELEM));
1562 return 1;
1563 }
1564 }
1565
Michal Vasko9f96a052020-03-10 09:41:45 +01001566 /* we need meta positions now */
1567 if (item1->type == LYXP_NODE_META) {
1568 meta_pos1 = get_meta_pos((struct lyd_meta *)item1->node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001569 }
Michal Vasko9f96a052020-03-10 09:41:45 +01001570 if (item2->type == LYXP_NODE_META) {
1571 meta_pos2 = get_meta_pos((struct lyd_meta *)item2->node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001572 }
1573
Michal Vasko9f96a052020-03-10 09:41:45 +01001574 /* 1st ROOT - 2nd ROOT, 1st ELEM - 2nd ELEM, 1st TEXT - 2nd TEXT, 1st META - =pos= - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001575 /* check for duplicates */
1576 if (item1->node == item2->node) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001577 assert((item1->type == item2->type) && ((item1->type != LYXP_NODE_META) || (meta_pos1 == meta_pos2)));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001578 return 0;
1579 }
1580
Michal Vasko9f96a052020-03-10 09:41:45 +01001581 /* 1st ELEM - 2nd TEXT, 1st ELEM - any pos - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001582 /* elem is always first, 2nd node is after it */
1583 if (item1->type == LYXP_NODE_ELEM) {
1584 assert(item2->type != LYXP_NODE_ELEM);
1585 return -1;
1586 }
1587
Michal Vasko9f96a052020-03-10 09:41:45 +01001588 /* 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 +02001589 /* 2nd is before 1st */
Michal Vasko69730152020-10-09 16:30:07 +02001590 if (((item1->type == LYXP_NODE_TEXT) &&
1591 ((item2->type == LYXP_NODE_ELEM) || (item2->type == LYXP_NODE_META))) ||
1592 ((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_ELEM)) ||
1593 (((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_META)) &&
1594 (meta_pos1 > meta_pos2))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001595 return 1;
1596 }
1597
Michal Vasko9f96a052020-03-10 09:41:45 +01001598 /* 1st META - any pos - 2nd TEXT, 1st META <pos< - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001599 /* 2nd is after 1st */
1600 return -1;
1601}
1602
1603/**
1604 * @brief Set cast for comparisons.
1605 *
1606 * @param[in] trg Target set to cast source into.
1607 * @param[in] src Source set.
1608 * @param[in] type Target set type.
1609 * @param[in] src_idx Source set node index.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001610 * @return LY_ERR
1611 */
1612static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001613set_comp_cast(struct lyxp_set *trg, struct lyxp_set *src, enum lyxp_set_type type, uint32_t src_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001614{
1615 assert(src->type == LYXP_SET_NODE_SET);
1616
1617 set_init(trg, src);
1618
1619 /* insert node into target set */
1620 set_insert_node(trg, src->val.nodes[src_idx].node, src->val.nodes[src_idx].pos, src->val.nodes[src_idx].type, 0);
1621
1622 /* cast target set appropriately */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001623 return lyxp_set_cast(trg, type);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001624}
1625
Michal Vasko4c7763f2020-07-27 17:40:37 +02001626/**
1627 * @brief Set content canonization for comparisons.
1628 *
1629 * @param[in] trg Target set to put the canononized source into.
1630 * @param[in] src Source set.
1631 * @param[in] xp_node Source XPath node/meta to use for canonization.
1632 * @return LY_ERR
1633 */
1634static LY_ERR
1635set_comp_canonize(struct lyxp_set *trg, const struct lyxp_set *src, const struct lyxp_set_node *xp_node)
1636{
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001637 const struct lysc_type *type = NULL;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001638 struct lyd_value val;
1639 struct ly_err_item *err = NULL;
1640 char *str, *ptr;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001641 LY_ERR rc;
1642
1643 /* is there anything to canonize even? */
1644 if ((src->type == LYXP_SET_NUMBER) || (src->type == LYXP_SET_STRING)) {
1645 /* do we have a type to use for canonization? */
1646 if ((xp_node->type == LYXP_NODE_ELEM) && (xp_node->node->schema->nodetype & LYD_NODE_TERM)) {
1647 type = ((struct lyd_node_term *)xp_node->node)->value.realtype;
1648 } else if (xp_node->type == LYXP_NODE_META) {
1649 type = ((struct lyd_meta *)xp_node->node)->value.realtype;
1650 }
1651 }
1652 if (!type) {
1653 goto fill;
1654 }
1655
1656 if (src->type == LYXP_SET_NUMBER) {
1657 /* canonize number */
1658 if (asprintf(&str, "%Lf", src->val.num) == -1) {
1659 LOGMEM(src->ctx);
1660 return LY_EMEM;
1661 }
1662 } else {
1663 /* canonize string */
1664 str = strdup(src->val.str);
1665 }
1666
1667 /* ignore errors, the value may not satisfy schema constraints */
Radek Krejci0b013302021-03-29 15:22:32 +02001668 rc = type->plugin->store(src->ctx, type, str, strlen(str), LYPLG_TYPE_STORE_DYNAMIC, src->format, src->prefix_data,
Michal Vasko405cc9e2020-12-01 12:01:27 +01001669 LYD_HINT_DATA, xp_node->node->schema, &val, NULL, &err);
Michal Vasko4c7763f2020-07-27 17:40:37 +02001670 ly_err_free(err);
1671 if (rc) {
1672 /* invalid value */
Radek Iša48460222021-03-02 15:18:32 +01001673 /* function store automaticaly dealloc value when fail */
Michal Vasko4c7763f2020-07-27 17:40:37 +02001674 goto fill;
1675 }
1676
Michal Vasko4c7763f2020-07-27 17:40:37 +02001677 /* use the canonized value */
1678 set_init(trg, src);
1679 trg->type = src->type;
1680 if (src->type == LYXP_SET_NUMBER) {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001681 trg->val.num = strtold(type->plugin->print(src->ctx, &val, LY_VALUE_CANON, NULL, NULL, NULL), &ptr);
Michal Vasko4c7763f2020-07-27 17:40:37 +02001682 LY_CHECK_ERR_RET(ptr[0], LOGINT(src->ctx), LY_EINT);
1683 } else {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001684 trg->val.str = strdup(type->plugin->print(src->ctx, &val, LY_VALUE_CANON, NULL, NULL, NULL));
Michal Vasko4c7763f2020-07-27 17:40:37 +02001685 }
1686 type->plugin->free(src->ctx, &val);
1687 return LY_SUCCESS;
1688
1689fill:
1690 /* no canonization needed/possible */
1691 set_fill_set(trg, src);
1692 return LY_SUCCESS;
1693}
1694
Michal Vasko03ff5a72019-09-11 13:49:33 +02001695/**
1696 * @brief Bubble sort @p set into XPath document order.
Michal Vasko49fec8e2022-05-24 10:28:33 +02001697 * Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001698 *
1699 * @param[in] set Set to sort.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001700 * @return How many times the whole set was traversed - 1 (if set was sorted, returns 0).
1701 */
1702static int
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001703set_sort(struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001704{
1705 uint32_t i, j;
Radek Krejci1deb5be2020-08-26 16:43:36 +02001706 int ret = 0, cmp;
Radek Krejci857189e2020-09-01 13:26:36 +02001707 ly_bool inverted, change;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001708 const struct lyd_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001709 struct lyxp_set_node item;
1710 struct lyxp_set_hash_node hnode;
1711 uint64_t hash;
1712
Michal Vasko3cf8fbf2020-05-27 15:21:21 +02001713 if ((set->type != LYXP_SET_NODE_SET) || (set->used < 2)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001714 return 0;
1715 }
1716
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001717 /* find first top-level node to be used as anchor for positions */
Michal Vasko4a7d4d62021-12-13 17:05:06 +01001718 for (root = set->tree; root->parent; root = lyd_parent(root)) {}
Michal Vaskod989ba02020-08-24 10:59:24 +02001719 for ( ; root->prev->next; root = root->prev) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001720
1721 /* fill positions */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001722 if (set_assign_pos(set, root, set->root_type)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001723 return -1;
1724 }
1725
Michal Vasko88a9e802022-05-24 10:50:28 +02001726#ifndef NDEBUG
Michal Vasko03ff5a72019-09-11 13:49:33 +02001727 LOGDBG(LY_LDGXPATH, "SORT BEGIN");
1728 print_set_debug(set);
Michal Vasko88a9e802022-05-24 10:50:28 +02001729#endif
Michal Vasko03ff5a72019-09-11 13:49:33 +02001730
1731 for (i = 0; i < set->used; ++i) {
1732 inverted = 0;
1733 change = 0;
1734
1735 for (j = 1; j < set->used - i; ++j) {
1736 /* compare node positions */
1737 if (inverted) {
1738 cmp = set_sort_compare(&set->val.nodes[j], &set->val.nodes[j - 1]);
1739 } else {
1740 cmp = set_sort_compare(&set->val.nodes[j - 1], &set->val.nodes[j]);
1741 }
1742
1743 /* swap if needed */
1744 if ((inverted && (cmp < 0)) || (!inverted && (cmp > 0))) {
1745 change = 1;
1746
1747 item = set->val.nodes[j - 1];
1748 set->val.nodes[j - 1] = set->val.nodes[j];
1749 set->val.nodes[j] = item;
1750 } else {
1751 /* whether node_pos1 should be smaller than node_pos2 or the other way around */
1752 inverted = !inverted;
1753 }
1754 }
1755
1756 ++ret;
1757
1758 if (!change) {
1759 break;
1760 }
1761 }
1762
Michal Vasko88a9e802022-05-24 10:50:28 +02001763#ifndef NDEBUG
Michal Vasko03ff5a72019-09-11 13:49:33 +02001764 LOGDBG(LY_LDGXPATH, "SORT END %d", ret);
1765 print_set_debug(set);
Michal Vasko88a9e802022-05-24 10:50:28 +02001766#endif
Michal Vasko03ff5a72019-09-11 13:49:33 +02001767
1768 /* check node hashes */
1769 if (set->used >= LYD_HT_MIN_ITEMS) {
1770 assert(set->ht);
1771 for (i = 0; i < set->used; ++i) {
1772 hnode.node = set->val.nodes[i].node;
1773 hnode.type = set->val.nodes[i].type;
1774
1775 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
1776 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
1777 hash = dict_hash_multi(hash, NULL, 0);
1778
1779 assert(!lyht_find(set->ht, &hnode, hash, NULL));
1780 }
1781 }
1782
1783 return ret - 1;
1784}
1785
Michal Vasko03ff5a72019-09-11 13:49:33 +02001786/**
1787 * @brief Merge 2 sorted sets into one.
1788 *
1789 * @param[in,out] trg Set to merge into. Duplicates are removed.
1790 * @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 +02001791 * @return LY_ERR
1792 */
1793static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001794set_sorted_merge(struct lyxp_set *trg, struct lyxp_set *src)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001795{
1796 uint32_t i, j, k, count, dup_count;
1797 int cmp;
1798 const struct lyd_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001799
Michal Vaskod3678892020-05-21 10:06:58 +02001800 if ((trg->type != LYXP_SET_NODE_SET) || (src->type != LYXP_SET_NODE_SET)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001801 return LY_EINVAL;
1802 }
1803
Michal Vaskod3678892020-05-21 10:06:58 +02001804 if (!src->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001805 return LY_SUCCESS;
Michal Vaskod3678892020-05-21 10:06:58 +02001806 } else if (!trg->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001807 set_fill_set(trg, src);
Michal Vaskod3678892020-05-21 10:06:58 +02001808 lyxp_set_free_content(src);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001809 return LY_SUCCESS;
1810 }
1811
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001812 /* find first top-level node to be used as anchor for positions */
Michal Vasko0143b682021-12-13 17:13:09 +01001813 for (root = trg->tree; root->parent; root = lyd_parent(root)) {}
Michal Vaskod989ba02020-08-24 10:59:24 +02001814 for ( ; root->prev->next; root = root->prev) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001815
1816 /* fill positions */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001817 if (set_assign_pos(trg, root, trg->root_type) || set_assign_pos(src, root, src->root_type)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001818 return LY_EINT;
1819 }
1820
1821#ifndef NDEBUG
1822 LOGDBG(LY_LDGXPATH, "MERGE target");
1823 print_set_debug(trg);
1824 LOGDBG(LY_LDGXPATH, "MERGE source");
1825 print_set_debug(src);
1826#endif
1827
1828 /* make memory for the merge (duplicates are not detected yet, so space
1829 * will likely be wasted on them, too bad) */
1830 if (trg->size - trg->used < src->used) {
1831 trg->size = trg->used + src->used;
1832
1833 trg->val.nodes = ly_realloc(trg->val.nodes, trg->size * sizeof *trg->val.nodes);
1834 LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx), LY_EMEM);
1835 }
1836
1837 i = 0;
1838 j = 0;
1839 count = 0;
1840 dup_count = 0;
1841 do {
1842 cmp = set_sort_compare(&src->val.nodes[i], &trg->val.nodes[j]);
1843 if (!cmp) {
1844 if (!count) {
1845 /* duplicate, just skip it */
1846 ++i;
1847 ++j;
1848 } else {
1849 /* we are copying something already, so let's copy the duplicate too,
1850 * we are hoping that afterwards there are some more nodes to
1851 * copy and this way we can copy them all together */
1852 ++count;
1853 ++dup_count;
1854 ++i;
1855 ++j;
1856 }
1857 } else if (cmp < 0) {
1858 /* inserting src node into trg, just remember it for now */
1859 ++count;
1860 ++i;
1861
1862 /* insert the hash now */
1863 set_insert_node_hash(trg, src->val.nodes[i - 1].node, src->val.nodes[i - 1].type);
1864 } else if (count) {
1865copy_nodes:
1866 /* time to actually copy the nodes, we have found the largest block of nodes */
1867 memmove(&trg->val.nodes[j + (count - dup_count)],
1868 &trg->val.nodes[j],
1869 (trg->used - j) * sizeof *trg->val.nodes);
1870 memcpy(&trg->val.nodes[j - dup_count], &src->val.nodes[i - count], count * sizeof *src->val.nodes);
1871
1872 trg->used += count - dup_count;
1873 /* do not change i, except the copying above, we are basically doing exactly what is in the else branch below */
1874 j += count - dup_count;
1875
1876 count = 0;
1877 dup_count = 0;
1878 } else {
1879 ++j;
1880 }
1881 } while ((i < src->used) && (j < trg->used));
1882
1883 if ((i < src->used) || count) {
1884 /* insert all the hashes first */
1885 for (k = i; k < src->used; ++k) {
1886 set_insert_node_hash(trg, src->val.nodes[k].node, src->val.nodes[k].type);
1887 }
1888
1889 /* loop ended, but we need to copy something at trg end */
1890 count += src->used - i;
1891 i = src->used;
1892 goto copy_nodes;
1893 }
1894
1895 /* we are inserting hashes before the actual node insert, which causes
1896 * situations when there were initially not enough items for a hash table,
1897 * but even after some were inserted, hash table was not created (during
1898 * insertion the number of items is not updated yet) */
1899 if (!trg->ht && (trg->used >= LYD_HT_MIN_ITEMS)) {
1900 set_insert_node_hash(trg, NULL, 0);
1901 }
1902
1903#ifndef NDEBUG
1904 LOGDBG(LY_LDGXPATH, "MERGE result");
1905 print_set_debug(trg);
1906#endif
1907
Michal Vaskod3678892020-05-21 10:06:58 +02001908 lyxp_set_free_content(src);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001909 return LY_SUCCESS;
1910}
1911
Michal Vasko14676352020-05-29 11:35:55 +02001912LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02001913lyxp_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 +02001914{
Michal Vasko004d3152020-06-11 19:59:22 +02001915 if (exp->used == tok_idx) {
Michal Vasko14676352020-05-29 11:35:55 +02001916 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001917 LOGVAL(ctx, LY_VCODE_XP_EOF);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001918 }
Michal Vasko14676352020-05-29 11:35:55 +02001919 return LY_EINCOMPLETE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001920 }
1921
Michal Vasko004d3152020-06-11 19:59:22 +02001922 if (want_tok && (exp->tokens[tok_idx] != want_tok)) {
Michal Vasko14676352020-05-29 11:35:55 +02001923 if (ctx) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02001924 LOGVAL(ctx, LY_VCODE_XP_INTOK2, lyxp_token2str(exp->tokens[tok_idx]),
1925 &exp->expr[exp->tok_pos[tok_idx]], lyxp_token2str(want_tok));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001926 }
Michal Vasko14676352020-05-29 11:35:55 +02001927 return LY_ENOT;
1928 }
1929
1930 return LY_SUCCESS;
1931}
1932
Michal Vasko004d3152020-06-11 19:59:22 +02001933LY_ERR
1934lyxp_next_token(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_token want_tok)
1935{
1936 LY_CHECK_RET(lyxp_check_token(ctx, exp, *tok_idx, want_tok));
1937
1938 /* skip the token */
1939 ++(*tok_idx);
1940
1941 return LY_SUCCESS;
1942}
1943
Michal Vasko14676352020-05-29 11:35:55 +02001944/* just like lyxp_check_token() but tests for 2 tokens */
1945static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02001946exp_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 +02001947 enum lyxp_token want_tok2)
Michal Vasko14676352020-05-29 11:35:55 +02001948{
Michal Vasko004d3152020-06-11 19:59:22 +02001949 if (exp->used == tok_idx) {
Michal Vasko14676352020-05-29 11:35:55 +02001950 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001951 LOGVAL(ctx, LY_VCODE_XP_EOF);
Michal Vasko14676352020-05-29 11:35:55 +02001952 }
1953 return LY_EINCOMPLETE;
1954 }
1955
Michal Vasko004d3152020-06-11 19:59:22 +02001956 if ((exp->tokens[tok_idx] != want_tok1) && (exp->tokens[tok_idx] != want_tok2)) {
Michal Vasko14676352020-05-29 11:35:55 +02001957 if (ctx) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02001958 LOGVAL(ctx, LY_VCODE_XP_INTOK, lyxp_token2str(exp->tokens[tok_idx]),
Michal Vasko0b468e62020-10-19 09:33:04 +02001959 &exp->expr[exp->tok_pos[tok_idx]]);
Michal Vasko14676352020-05-29 11:35:55 +02001960 }
1961 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001962 }
1963
1964 return LY_SUCCESS;
1965}
1966
Michal Vasko4911eeb2021-06-28 11:23:05 +02001967LY_ERR
1968lyxp_next_token2(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_token want_tok1,
1969 enum lyxp_token want_tok2)
1970{
1971 LY_CHECK_RET(exp_check_token2(ctx, exp, *tok_idx, want_tok1, want_tok2));
1972
1973 /* skip the token */
1974 ++(*tok_idx);
1975
1976 return LY_SUCCESS;
1977}
1978
Michal Vasko03ff5a72019-09-11 13:49:33 +02001979/**
1980 * @brief Stack operation push on the repeat array.
1981 *
1982 * @param[in] exp Expression to use.
Michal Vasko004d3152020-06-11 19:59:22 +02001983 * @param[in] tok_idx Position in the expresion \p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001984 * @param[in] repeat_op_idx Index from \p exp of the operator token. This value is pushed.
1985 */
1986static void
Michal Vasko004d3152020-06-11 19:59:22 +02001987exp_repeat_push(struct lyxp_expr *exp, uint16_t tok_idx, uint16_t repeat_op_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001988{
1989 uint16_t i;
1990
Michal Vasko004d3152020-06-11 19:59:22 +02001991 if (exp->repeat[tok_idx]) {
Radek Krejci1e008d22020-08-17 11:37:37 +02001992 for (i = 0; exp->repeat[tok_idx][i]; ++i) {}
Michal Vasko004d3152020-06-11 19:59:22 +02001993 exp->repeat[tok_idx] = realloc(exp->repeat[tok_idx], (i + 2) * sizeof *exp->repeat[tok_idx]);
1994 LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), );
1995 exp->repeat[tok_idx][i] = repeat_op_idx;
1996 exp->repeat[tok_idx][i + 1] = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001997 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02001998 exp->repeat[tok_idx] = calloc(2, sizeof *exp->repeat[tok_idx]);
1999 LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), );
2000 exp->repeat[tok_idx][0] = repeat_op_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002001 }
2002}
2003
2004/**
2005 * @brief Reparse Predicate. Logs directly on error.
2006 *
2007 * [7] Predicate ::= '[' Expr ']'
2008 *
2009 * @param[in] ctx Context for logging.
2010 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002011 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002012 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002013 * @return LY_ERR
2014 */
2015static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002016reparse_predicate(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t depth)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002017{
2018 LY_ERR rc;
2019
Michal Vasko004d3152020-06-11 19:59:22 +02002020 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002021 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002022 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002023
aPiecekbf968d92021-05-27 14:35:05 +02002024 rc = reparse_or_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002025 LY_CHECK_RET(rc);
2026
Michal Vasko004d3152020-06-11 19:59:22 +02002027 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002028 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002029 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002030
2031 return LY_SUCCESS;
2032}
2033
2034/**
2035 * @brief Reparse RelativeLocationPath. Logs directly on error.
2036 *
2037 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
2038 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
2039 * [6] NodeTest ::= NameTest | NodeType '(' ')'
2040 *
2041 * @param[in] ctx Context for logging.
2042 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002043 * @param[in] tok_idx Position in the expression \p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002044 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002045 * @return LY_ERR (LY_EINCOMPLETE on forward reference)
2046 */
2047static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002048reparse_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 +02002049{
2050 LY_ERR rc;
2051
Michal Vasko004d3152020-06-11 19:59:22 +02002052 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002053 LY_CHECK_RET(rc);
2054
2055 goto step;
2056 do {
2057 /* '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02002058 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002059
Michal Vasko004d3152020-06-11 19:59:22 +02002060 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002061 LY_CHECK_RET(rc);
2062step:
2063 /* Step */
Michal Vasko004d3152020-06-11 19:59:22 +02002064 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002065 case LYXP_TOKEN_DOT:
Michal Vasko004d3152020-06-11 19:59:22 +02002066 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002067 break;
2068
2069 case LYXP_TOKEN_DDOT:
Michal Vasko004d3152020-06-11 19:59:22 +02002070 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002071 break;
2072
Michal Vasko49fec8e2022-05-24 10:28:33 +02002073 case LYXP_TOKEN_AXISNAME:
2074 ++(*tok_idx);
2075
2076 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_DCOLON);
2077 LY_CHECK_RET(rc);
2078
2079 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02002080 case LYXP_TOKEN_AT:
Michal Vasko004d3152020-06-11 19:59:22 +02002081 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002082
Michal Vasko004d3152020-06-11 19:59:22 +02002083 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002084 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002085 if ((exp->tokens[*tok_idx] != LYXP_TOKEN_NAMETEST) && (exp->tokens[*tok_idx] != LYXP_TOKEN_NODETYPE)) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02002086 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 +02002087 return LY_EVALID;
2088 }
Michal Vasko49fec8e2022-05-24 10:28:33 +02002089 if (exp->tokens[*tok_idx] == LYXP_TOKEN_NODETYPE) {
2090 goto reparse_nodetype;
2091 }
Radek Krejci0f969882020-08-21 16:56:47 +02002092 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02002093 case LYXP_TOKEN_NAMETEST:
Michal Vasko004d3152020-06-11 19:59:22 +02002094 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002095 goto reparse_predicate;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002096
2097 case LYXP_TOKEN_NODETYPE:
Michal Vasko49fec8e2022-05-24 10:28:33 +02002098reparse_nodetype:
Michal Vasko004d3152020-06-11 19:59:22 +02002099 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002100
2101 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02002102 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002103 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002104 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002105
2106 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02002107 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002108 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002109 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002110
2111reparse_predicate:
2112 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002113 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
aPiecekbf968d92021-05-27 14:35:05 +02002114 rc = reparse_predicate(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002115 LY_CHECK_RET(rc);
2116 }
2117 break;
2118 default:
Michal Vasko49fec8e2022-05-24 10:28:33 +02002119 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 +02002120 return LY_EVALID;
2121 }
Michal Vasko004d3152020-06-11 19:59:22 +02002122 } while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02002123
2124 return LY_SUCCESS;
2125}
2126
2127/**
2128 * @brief Reparse AbsoluteLocationPath. Logs directly on error.
2129 *
2130 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
2131 *
2132 * @param[in] ctx Context for logging.
2133 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002134 * @param[in] tok_idx Position in the expression \p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002135 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002136 * @return LY_ERR
2137 */
2138static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002139reparse_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 +02002140{
2141 LY_ERR rc;
2142
Michal Vasko004d3152020-06-11 19:59:22 +02002143 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 +02002144
2145 /* '/' RelativeLocationPath? */
Michal Vasko004d3152020-06-11 19:59:22 +02002146 if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002147 /* '/' */
Michal Vasko004d3152020-06-11 19:59:22 +02002148 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002149
Michal Vasko004d3152020-06-11 19:59:22 +02002150 if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002151 return LY_SUCCESS;
2152 }
Michal Vasko004d3152020-06-11 19:59:22 +02002153 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002154 case LYXP_TOKEN_DOT:
2155 case LYXP_TOKEN_DDOT:
Michal Vasko49fec8e2022-05-24 10:28:33 +02002156 case LYXP_TOKEN_AXISNAME:
Michal Vasko03ff5a72019-09-11 13:49:33 +02002157 case LYXP_TOKEN_AT:
2158 case LYXP_TOKEN_NAMETEST:
2159 case LYXP_TOKEN_NODETYPE:
aPiecekbf968d92021-05-27 14:35:05 +02002160 rc = reparse_relative_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002161 LY_CHECK_RET(rc);
Radek Krejci0f969882020-08-21 16:56:47 +02002162 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02002163 default:
2164 break;
2165 }
2166
Michal Vasko03ff5a72019-09-11 13:49:33 +02002167 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02002168 /* '//' RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002169 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002170
aPiecekbf968d92021-05-27 14:35:05 +02002171 rc = reparse_relative_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002172 LY_CHECK_RET(rc);
2173 }
2174
2175 return LY_SUCCESS;
2176}
2177
2178/**
2179 * @brief Reparse FunctionCall. Logs directly on error.
2180 *
2181 * [9] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
2182 *
2183 * @param[in] ctx Context for logging.
2184 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002185 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002186 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002187 * @return LY_ERR
2188 */
2189static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002190reparse_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 +02002191{
Radek Krejci1deb5be2020-08-26 16:43:36 +02002192 int8_t min_arg_count = -1;
2193 uint32_t arg_count, max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002194 uint16_t func_tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002195 LY_ERR rc;
2196
Michal Vasko004d3152020-06-11 19:59:22 +02002197 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_FUNCNAME);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002198 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002199 func_tok_idx = *tok_idx;
2200 switch (exp->tok_len[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002201 case 3:
Michal Vasko004d3152020-06-11 19:59:22 +02002202 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002203 min_arg_count = 1;
2204 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002205 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002206 min_arg_count = 1;
2207 max_arg_count = 1;
2208 }
2209 break;
2210 case 4:
Michal Vasko004d3152020-06-11 19:59:22 +02002211 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002212 min_arg_count = 1;
2213 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002214 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002215 min_arg_count = 0;
2216 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002217 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002218 min_arg_count = 0;
2219 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002220 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002221 min_arg_count = 0;
2222 max_arg_count = 0;
2223 }
2224 break;
2225 case 5:
Michal Vasko004d3152020-06-11 19:59:22 +02002226 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002227 min_arg_count = 1;
2228 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002229 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002230 min_arg_count = 0;
2231 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002232 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002233 min_arg_count = 1;
2234 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002235 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002236 min_arg_count = 1;
2237 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002238 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002239 min_arg_count = 1;
2240 max_arg_count = 1;
2241 }
2242 break;
2243 case 6:
Michal Vasko004d3152020-06-11 19:59:22 +02002244 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002245 min_arg_count = 2;
Radek Krejci1deb5be2020-08-26 16:43:36 +02002246 max_arg_count = UINT32_MAX;
Michal Vasko004d3152020-06-11 19:59:22 +02002247 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002248 min_arg_count = 0;
2249 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002250 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002251 min_arg_count = 0;
2252 max_arg_count = 1;
2253 }
2254 break;
2255 case 7:
Michal Vasko004d3152020-06-11 19:59:22 +02002256 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002257 min_arg_count = 1;
2258 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002259 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002260 min_arg_count = 1;
2261 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002262 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002263 min_arg_count = 0;
2264 max_arg_count = 0;
2265 }
2266 break;
2267 case 8:
Michal Vasko004d3152020-06-11 19:59:22 +02002268 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002269 min_arg_count = 2;
2270 max_arg_count = 2;
Michal Vasko004d3152020-06-11 19:59:22 +02002271 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002272 min_arg_count = 0;
2273 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002274 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "re-match", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002275 min_arg_count = 2;
2276 max_arg_count = 2;
2277 }
2278 break;
2279 case 9:
Michal Vasko004d3152020-06-11 19:59:22 +02002280 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002281 min_arg_count = 2;
2282 max_arg_count = 3;
Michal Vasko004d3152020-06-11 19:59:22 +02002283 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002284 min_arg_count = 3;
2285 max_arg_count = 3;
2286 }
2287 break;
2288 case 10:
Michal Vasko004d3152020-06-11 19:59:22 +02002289 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002290 min_arg_count = 0;
2291 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002292 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002293 min_arg_count = 1;
2294 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002295 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002296 min_arg_count = 2;
2297 max_arg_count = 2;
2298 }
2299 break;
2300 case 11:
Michal Vasko004d3152020-06-11 19:59:22 +02002301 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002302 min_arg_count = 2;
2303 max_arg_count = 2;
2304 }
2305 break;
2306 case 12:
Michal Vasko004d3152020-06-11 19:59:22 +02002307 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002308 min_arg_count = 2;
2309 max_arg_count = 2;
2310 }
2311 break;
2312 case 13:
Michal Vasko004d3152020-06-11 19:59:22 +02002313 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002314 min_arg_count = 0;
2315 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002316 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string-length", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002317 min_arg_count = 0;
2318 max_arg_count = 1;
2319 }
2320 break;
2321 case 15:
Michal Vasko004d3152020-06-11 19:59:22 +02002322 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002323 min_arg_count = 0;
2324 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002325 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-after", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002326 min_arg_count = 2;
2327 max_arg_count = 2;
2328 }
2329 break;
2330 case 16:
Michal Vasko004d3152020-06-11 19:59:22 +02002331 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002332 min_arg_count = 2;
2333 max_arg_count = 2;
2334 }
2335 break;
2336 case 20:
Michal Vasko004d3152020-06-11 19:59:22 +02002337 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from-or-self", 20)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002338 min_arg_count = 2;
2339 max_arg_count = 2;
2340 }
2341 break;
2342 }
2343 if (min_arg_count == -1) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002344 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 +02002345 return LY_EINVAL;
2346 }
Michal Vasko004d3152020-06-11 19:59:22 +02002347 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002348
2349 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02002350 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002351 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002352 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002353
2354 /* ( Expr ( ',' Expr )* )? */
2355 arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002356 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002357 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002358 if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002359 ++arg_count;
aPiecekbf968d92021-05-27 14:35:05 +02002360 rc = reparse_or_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002361 LY_CHECK_RET(rc);
2362 }
Michal Vasko004d3152020-06-11 19:59:22 +02002363 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) {
2364 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002365
2366 ++arg_count;
aPiecekbf968d92021-05-27 14:35:05 +02002367 rc = reparse_or_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002368 LY_CHECK_RET(rc);
2369 }
2370
2371 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02002372 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002373 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002374 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002375
Radek Krejci857189e2020-09-01 13:26:36 +02002376 if ((arg_count < (uint32_t)min_arg_count) || (arg_count > max_arg_count)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002377 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 +02002378 return LY_EVALID;
2379 }
2380
2381 return LY_SUCCESS;
2382}
2383
2384/**
2385 * @brief Reparse PathExpr. Logs directly on error.
2386 *
2387 * [10] PathExpr ::= LocationPath | PrimaryExpr Predicate*
2388 * | PrimaryExpr Predicate* '/' RelativeLocationPath
2389 * | PrimaryExpr Predicate* '//' RelativeLocationPath
2390 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
aPiecekfba75362021-10-07 12:39:48 +02002391 * [8] PrimaryExpr ::= VariableReference | '(' Expr ')' | Literal | Number | FunctionCall
Michal Vasko03ff5a72019-09-11 13:49:33 +02002392 *
2393 * @param[in] ctx Context for logging.
2394 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002395 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002396 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002397 * @return LY_ERR
2398 */
2399static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002400reparse_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 +02002401{
2402 LY_ERR rc;
2403
Michal Vasko004d3152020-06-11 19:59:22 +02002404 if (lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko14676352020-05-29 11:35:55 +02002405 return LY_EVALID;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002406 }
2407
Michal Vasko004d3152020-06-11 19:59:22 +02002408 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002409 case LYXP_TOKEN_PAR1:
2410 /* '(' Expr ')' Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002411 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002412
aPiecekbf968d92021-05-27 14:35:05 +02002413 rc = reparse_or_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002414 LY_CHECK_RET(rc);
2415
Michal Vasko004d3152020-06-11 19:59:22 +02002416 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002417 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002418 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002419 goto predicate;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002420 case LYXP_TOKEN_DOT:
2421 case LYXP_TOKEN_DDOT:
Michal Vasko49fec8e2022-05-24 10:28:33 +02002422 case LYXP_TOKEN_AXISNAME:
Michal Vasko03ff5a72019-09-11 13:49:33 +02002423 case LYXP_TOKEN_AT:
2424 case LYXP_TOKEN_NAMETEST:
2425 case LYXP_TOKEN_NODETYPE:
2426 /* RelativeLocationPath */
aPiecekbf968d92021-05-27 14:35:05 +02002427 rc = reparse_relative_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002428 LY_CHECK_RET(rc);
2429 break;
aPiecekfba75362021-10-07 12:39:48 +02002430 case LYXP_TOKEN_VARREF:
2431 /* VariableReference */
2432 ++(*tok_idx);
2433 goto predicate;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002434 case LYXP_TOKEN_FUNCNAME:
2435 /* FunctionCall */
aPiecekbf968d92021-05-27 14:35:05 +02002436 rc = reparse_function_call(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002437 LY_CHECK_RET(rc);
2438 goto predicate;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002439 case LYXP_TOKEN_OPER_PATH:
2440 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +02002441 /* AbsoluteLocationPath */
aPiecekbf968d92021-05-27 14:35:05 +02002442 rc = reparse_absolute_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002443 LY_CHECK_RET(rc);
2444 break;
2445 case LYXP_TOKEN_LITERAL:
2446 /* Literal */
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 case LYXP_TOKEN_NUMBER:
2450 /* Number */
Michal Vasko004d3152020-06-11 19:59:22 +02002451 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002452 goto predicate;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002453 default:
Michal Vasko49fec8e2022-05-24 10:28:33 +02002454 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 +02002455 return LY_EVALID;
2456 }
2457
2458 return LY_SUCCESS;
2459
2460predicate:
2461 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002462 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
aPiecekbf968d92021-05-27 14:35:05 +02002463 rc = reparse_predicate(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002464 LY_CHECK_RET(rc);
2465 }
2466
2467 /* ('/' or '//') RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002468 if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002469
2470 /* '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02002471 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002472
aPiecekbf968d92021-05-27 14:35:05 +02002473 rc = reparse_relative_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002474 LY_CHECK_RET(rc);
2475 }
2476
2477 return LY_SUCCESS;
2478}
2479
2480/**
2481 * @brief Reparse UnaryExpr. Logs directly on error.
2482 *
2483 * [17] UnaryExpr ::= UnionExpr | '-' UnaryExpr
2484 * [18] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
2485 *
2486 * @param[in] ctx Context for logging.
2487 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002488 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002489 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002490 * @return LY_ERR
2491 */
2492static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002493reparse_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 +02002494{
2495 uint16_t prev_exp;
2496 LY_ERR rc;
2497
2498 /* ('-')* */
Michal Vasko004d3152020-06-11 19:59:22 +02002499 prev_exp = *tok_idx;
Michal Vasko69730152020-10-09 16:30:07 +02002500 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2501 (exp->expr[exp->tok_pos[*tok_idx]] == '-')) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002502 exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNARY);
Michal Vasko004d3152020-06-11 19:59:22 +02002503 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002504 }
2505
2506 /* PathExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002507 prev_exp = *tok_idx;
aPiecekbf968d92021-05-27 14:35:05 +02002508 rc = reparse_path_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002509 LY_CHECK_RET(rc);
2510
2511 /* ('|' PathExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002512 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_UNI)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002513 exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNION);
Michal Vasko004d3152020-06-11 19:59:22 +02002514 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002515
aPiecekbf968d92021-05-27 14:35:05 +02002516 rc = reparse_path_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002517 LY_CHECK_RET(rc);
2518 }
2519
2520 return LY_SUCCESS;
2521}
2522
2523/**
2524 * @brief Reparse AdditiveExpr. Logs directly on error.
2525 *
2526 * [15] AdditiveExpr ::= MultiplicativeExpr
2527 * | AdditiveExpr '+' MultiplicativeExpr
2528 * | AdditiveExpr '-' MultiplicativeExpr
2529 * [16] MultiplicativeExpr ::= UnaryExpr
2530 * | MultiplicativeExpr '*' UnaryExpr
2531 * | MultiplicativeExpr 'div' UnaryExpr
2532 * | MultiplicativeExpr 'mod' UnaryExpr
2533 *
2534 * @param[in] ctx Context for logging.
2535 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002536 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002537 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002538 * @return LY_ERR
2539 */
2540static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002541reparse_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 +02002542{
2543 uint16_t prev_add_exp, prev_mul_exp;
2544 LY_ERR rc;
2545
Michal Vasko004d3152020-06-11 19:59:22 +02002546 prev_add_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002547 goto reparse_multiplicative_expr;
2548
2549 /* ('+' / '-' MultiplicativeExpr)* */
Michal Vasko69730152020-10-09 16:30:07 +02002550 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2551 ((exp->expr[exp->tok_pos[*tok_idx]] == '+') || (exp->expr[exp->tok_pos[*tok_idx]] == '-'))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002552 exp_repeat_push(exp, prev_add_exp, LYXP_EXPR_ADDITIVE);
Michal Vasko004d3152020-06-11 19:59:22 +02002553 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002554
2555reparse_multiplicative_expr:
2556 /* UnaryExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002557 prev_mul_exp = *tok_idx;
aPiecekbf968d92021-05-27 14:35:05 +02002558 rc = reparse_unary_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002559 LY_CHECK_RET(rc);
2560
2561 /* ('*' / 'div' / 'mod' UnaryExpr)* */
Michal Vasko69730152020-10-09 16:30:07 +02002562 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2563 ((exp->expr[exp->tok_pos[*tok_idx]] == '*') || (exp->tok_len[*tok_idx] == 3))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002564 exp_repeat_push(exp, prev_mul_exp, LYXP_EXPR_MULTIPLICATIVE);
Michal Vasko004d3152020-06-11 19:59:22 +02002565 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002566
aPiecekbf968d92021-05-27 14:35:05 +02002567 rc = reparse_unary_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002568 LY_CHECK_RET(rc);
2569 }
2570 }
2571
2572 return LY_SUCCESS;
2573}
2574
2575/**
2576 * @brief Reparse EqualityExpr. Logs directly on error.
2577 *
2578 * [13] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
2579 * | EqualityExpr '!=' RelationalExpr
2580 * [14] RelationalExpr ::= AdditiveExpr
2581 * | RelationalExpr '<' AdditiveExpr
2582 * | RelationalExpr '>' AdditiveExpr
2583 * | RelationalExpr '<=' AdditiveExpr
2584 * | RelationalExpr '>=' AdditiveExpr
2585 *
2586 * @param[in] ctx Context for logging.
2587 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002588 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002589 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002590 * @return LY_ERR
2591 */
2592static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002593reparse_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 +02002594{
2595 uint16_t prev_eq_exp, prev_rel_exp;
2596 LY_ERR rc;
2597
Michal Vasko004d3152020-06-11 19:59:22 +02002598 prev_eq_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002599 goto reparse_additive_expr;
2600
2601 /* ('=' / '!=' RelationalExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002602 while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_EQUAL, LYXP_TOKEN_OPER_NEQUAL)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002603 exp_repeat_push(exp, prev_eq_exp, LYXP_EXPR_EQUALITY);
Michal Vasko004d3152020-06-11 19:59:22 +02002604 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002605
2606reparse_additive_expr:
2607 /* AdditiveExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002608 prev_rel_exp = *tok_idx;
aPiecekbf968d92021-05-27 14:35:05 +02002609 rc = reparse_additive_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002610 LY_CHECK_RET(rc);
2611
2612 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002613 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_COMP)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002614 exp_repeat_push(exp, prev_rel_exp, LYXP_EXPR_RELATIONAL);
Michal Vasko004d3152020-06-11 19:59:22 +02002615 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002616
aPiecekbf968d92021-05-27 14:35:05 +02002617 rc = reparse_additive_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002618 LY_CHECK_RET(rc);
2619 }
2620 }
2621
2622 return LY_SUCCESS;
2623}
2624
2625/**
2626 * @brief Reparse OrExpr. Logs directly on error.
2627 *
2628 * [11] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
2629 * [12] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
2630 *
2631 * @param[in] ctx Context for logging.
2632 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002633 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002634 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002635 * @return LY_ERR
2636 */
2637static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002638reparse_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 +02002639{
2640 uint16_t prev_or_exp, prev_and_exp;
2641 LY_ERR rc;
2642
aPiecekbf968d92021-05-27 14:35:05 +02002643 ++depth;
2644 LY_CHECK_ERR_RET(depth > LYXP_MAX_BLOCK_DEPTH, LOGVAL(ctx, LY_VCODE_XP_DEPTH), LY_EINVAL);
2645
Michal Vasko004d3152020-06-11 19:59:22 +02002646 prev_or_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002647 goto reparse_equality_expr;
2648
2649 /* ('or' AndExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002650 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 +02002651 exp_repeat_push(exp, prev_or_exp, LYXP_EXPR_OR);
Michal Vasko004d3152020-06-11 19:59:22 +02002652 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002653
2654reparse_equality_expr:
2655 /* EqualityExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002656 prev_and_exp = *tok_idx;
aPiecekbf968d92021-05-27 14:35:05 +02002657 rc = reparse_equality_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002658 LY_CHECK_RET(rc);
2659
2660 /* ('and' EqualityExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002661 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 +02002662 exp_repeat_push(exp, prev_and_exp, LYXP_EXPR_AND);
Michal Vasko004d3152020-06-11 19:59:22 +02002663 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002664
aPiecekbf968d92021-05-27 14:35:05 +02002665 rc = reparse_equality_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002666 LY_CHECK_RET(rc);
2667 }
2668 }
2669
2670 return LY_SUCCESS;
2671}
Radek Krejcib1646a92018-11-02 16:08:26 +01002672
2673/**
2674 * @brief Parse NCName.
2675 *
2676 * @param[in] ncname Name to parse.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002677 * @return Length of @p ncname valid bytes.
Radek Krejcib1646a92018-11-02 16:08:26 +01002678 */
Michal Vasko49fec8e2022-05-24 10:28:33 +02002679static ssize_t
Radek Krejcib1646a92018-11-02 16:08:26 +01002680parse_ncname(const char *ncname)
2681{
Radek Krejci1deb5be2020-08-26 16:43:36 +02002682 uint32_t uc;
Radek Krejcid4270262019-01-07 15:07:25 +01002683 size_t size;
Michal Vasko49fec8e2022-05-24 10:28:33 +02002684 ssize_t len = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01002685
2686 LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), 0);
2687 if (!is_xmlqnamestartchar(uc) || (uc == ':')) {
2688 return len;
2689 }
2690
2691 do {
2692 len += size;
Radek Krejci9a564c92019-01-07 14:53:57 +01002693 if (!*ncname) {
2694 break;
2695 }
Radek Krejcid4270262019-01-07 15:07:25 +01002696 LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), -len);
Radek Krejcib1646a92018-11-02 16:08:26 +01002697 } while (is_xmlqnamechar(uc) && (uc != ':'));
2698
2699 return len;
2700}
2701
2702/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02002703 * @brief Add @p token into the expression @p exp.
Radek Krejcib1646a92018-11-02 16:08:26 +01002704 *
Michal Vasko03ff5a72019-09-11 13:49:33 +02002705 * @param[in] ctx Context for logging.
Radek Krejcib1646a92018-11-02 16:08:26 +01002706 * @param[in] exp Expression to use.
2707 * @param[in] token Token to add.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002708 * @param[in] tok_pos Token position in the XPath expression.
Radek Krejcib1646a92018-11-02 16:08:26 +01002709 * @param[in] tok_len Token length in the XPath expression.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002710 * @return LY_ERR
Radek Krejcib1646a92018-11-02 16:08:26 +01002711 */
2712static LY_ERR
Michal Vasko14676352020-05-29 11:35:55 +02002713exp_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 +01002714{
2715 uint32_t prev;
2716
2717 if (exp->used == exp->size) {
2718 prev = exp->size;
2719 exp->size += LYXP_EXPR_SIZE_STEP;
2720 if (prev > exp->size) {
2721 LOGINT(ctx);
2722 return LY_EINT;
2723 }
2724
2725 exp->tokens = ly_realloc(exp->tokens, exp->size * sizeof *exp->tokens);
2726 LY_CHECK_ERR_RET(!exp->tokens, LOGMEM(ctx), LY_EMEM);
2727 exp->tok_pos = ly_realloc(exp->tok_pos, exp->size * sizeof *exp->tok_pos);
2728 LY_CHECK_ERR_RET(!exp->tok_pos, LOGMEM(ctx), LY_EMEM);
2729 exp->tok_len = ly_realloc(exp->tok_len, exp->size * sizeof *exp->tok_len);
2730 LY_CHECK_ERR_RET(!exp->tok_len, LOGMEM(ctx), LY_EMEM);
2731 }
2732
2733 exp->tokens[exp->used] = token;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002734 exp->tok_pos[exp->used] = tok_pos;
Radek Krejcib1646a92018-11-02 16:08:26 +01002735 exp->tok_len[exp->used] = tok_len;
2736 ++exp->used;
2737 return LY_SUCCESS;
2738}
2739
2740void
Michal Vasko14676352020-05-29 11:35:55 +02002741lyxp_expr_free(const struct ly_ctx *ctx, struct lyxp_expr *expr)
Radek Krejcib1646a92018-11-02 16:08:26 +01002742{
2743 uint16_t i;
2744
2745 if (!expr) {
2746 return;
2747 }
2748
2749 lydict_remove(ctx, expr->expr);
2750 free(expr->tokens);
2751 free(expr->tok_pos);
2752 free(expr->tok_len);
2753 if (expr->repeat) {
2754 for (i = 0; i < expr->used; ++i) {
2755 free(expr->repeat[i]);
2756 }
2757 }
2758 free(expr->repeat);
2759 free(expr);
2760}
2761
Michal Vasko49fec8e2022-05-24 10:28:33 +02002762/**
2763 * @brief Parse Axis name.
2764 *
2765 * @param[in] str String to parse.
2766 * @param[in] str_len Length of @p str.
2767 * @return LY_SUCCESS if an axis.
2768 * @return LY_ENOT otherwise.
2769 */
2770static LY_ERR
2771expr_parse_axis(const char *str, size_t str_len)
2772{
2773 switch (str_len) {
2774 case 4:
2775 if (!strncmp("self", str, str_len)) {
2776 return LY_SUCCESS;
2777 }
2778 break;
2779 case 5:
2780 if (!strncmp("child", str, str_len)) {
2781 return LY_SUCCESS;
2782 }
2783 break;
2784 case 6:
2785 if (!strncmp("parent", str, str_len)) {
2786 return LY_SUCCESS;
2787 }
2788 break;
2789 case 8:
2790 if (!strncmp("ancestor", str, str_len)) {
2791 return LY_SUCCESS;
2792 }
2793 break;
2794 case 9:
2795 if (!strncmp("attribute", str, str_len)) {
2796 return LY_SUCCESS;
2797 } else if (!strncmp("following", str, str_len)) {
2798 return LY_SUCCESS;
2799 } else if (!strncmp("namespace", str, str_len)) {
2800 LOGERR(NULL, LY_EINVAL, "Axis \"namespace\" not supported.");
2801 return LY_ENOT;
2802 } else if (!strncmp("preceding", str, str_len)) {
2803 return LY_SUCCESS;
2804 }
2805 break;
2806 case 10:
2807 if (!strncmp("descendant", str, str_len)) {
2808 return LY_SUCCESS;
2809 }
2810 break;
2811 case 16:
2812 if (!strncmp("ancestor-or-self", str, str_len)) {
2813 return LY_SUCCESS;
2814 }
2815 break;
2816 case 17:
2817 if (!strncmp("following-sibling", str, str_len)) {
2818 return LY_SUCCESS;
2819 } else if (!strncmp("preceding-sibling", str, str_len)) {
2820 return LY_SUCCESS;
2821 }
2822 break;
2823 case 18:
2824 if (!strncmp("descendant-or-self", str, str_len)) {
2825 return LY_SUCCESS;
2826 }
2827 break;
2828 }
2829
2830 return LY_ENOT;
2831}
2832
Radek Krejcif03a9e22020-09-18 20:09:31 +02002833LY_ERR
2834lyxp_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 +01002835{
Radek Krejcif03a9e22020-09-18 20:09:31 +02002836 LY_ERR ret = LY_SUCCESS;
2837 struct lyxp_expr *expr;
Radek Krejcid4270262019-01-07 15:07:25 +01002838 size_t parsed = 0, tok_len;
Radek Krejcib1646a92018-11-02 16:08:26 +01002839 enum lyxp_token tok_type;
Michal Vasko49fec8e2022-05-24 10:28:33 +02002840 ly_bool prev_func_check = 0, prev_ntype_check = 0, has_axis;
Michal Vasko004d3152020-06-11 19:59:22 +02002841 uint16_t tok_idx = 0;
Michal Vasko49fec8e2022-05-24 10:28:33 +02002842 ssize_t ncname_len;
Radek Krejcib1646a92018-11-02 16:08:26 +01002843
Radek Krejcif03a9e22020-09-18 20:09:31 +02002844 assert(expr_p);
2845
2846 if (!expr_str[0]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002847 LOGVAL(ctx, LY_VCODE_XP_EOF);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002848 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02002849 }
2850
2851 if (!expr_len) {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002852 expr_len = strlen(expr_str);
Michal Vasko004d3152020-06-11 19:59:22 +02002853 }
2854 if (expr_len > UINT16_MAX) {
Michal Vasko623ac7b2021-04-09 12:44:23 +02002855 LOGVAL(ctx, LYVE_XPATH, "XPath expression cannot be longer than %u characters.", UINT16_MAX);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002856 return LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01002857 }
2858
2859 /* init lyxp_expr structure */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002860 expr = calloc(1, sizeof *expr);
2861 LY_CHECK_ERR_GOTO(!expr, LOGMEM(ctx); ret = LY_EMEM, error);
2862 LY_CHECK_GOTO(ret = lydict_insert(ctx, expr_str, expr_len, &expr->expr), error);
2863 expr->used = 0;
2864 expr->size = LYXP_EXPR_SIZE_START;
2865 expr->tokens = malloc(expr->size * sizeof *expr->tokens);
2866 LY_CHECK_ERR_GOTO(!expr->tokens, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002867
Radek Krejcif03a9e22020-09-18 20:09:31 +02002868 expr->tok_pos = malloc(expr->size * sizeof *expr->tok_pos);
2869 LY_CHECK_ERR_GOTO(!expr->tok_pos, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002870
Radek Krejcif03a9e22020-09-18 20:09:31 +02002871 expr->tok_len = malloc(expr->size * sizeof *expr->tok_len);
2872 LY_CHECK_ERR_GOTO(!expr->tok_len, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002873
Michal Vasko004d3152020-06-11 19:59:22 +02002874 /* make expr 0-terminated */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002875 expr_str = expr->expr;
Michal Vasko004d3152020-06-11 19:59:22 +02002876
Radek Krejcif03a9e22020-09-18 20:09:31 +02002877 while (is_xmlws(expr_str[parsed])) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002878 ++parsed;
2879 }
2880
2881 do {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002882 if (expr_str[parsed] == '(') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002883
2884 /* '(' */
2885 tok_len = 1;
2886 tok_type = LYXP_TOKEN_PAR1;
2887
Michal Vasko49fec8e2022-05-24 10:28:33 +02002888 if (prev_ntype_check && expr->used && (expr->tokens[expr->used - 1] == LYXP_TOKEN_NAMETEST) &&
2889 (((expr->tok_len[expr->used - 1] == 4) &&
2890 (!strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "node", 4) ||
2891 !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "text", 4))) ||
2892 ((expr->tok_len[expr->used - 1] == 7) &&
2893 !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "comment", 7)))) {
2894 /* it is NodeType after all */
2895 expr->tokens[expr->used - 1] = LYXP_TOKEN_NODETYPE;
2896
2897 prev_ntype_check = 0;
2898 prev_func_check = 0;
2899 } else if (prev_func_check && expr->used && (expr->tokens[expr->used - 1] == LYXP_TOKEN_NAMETEST)) {
2900 /* it is FunctionName after all */
2901 expr->tokens[expr->used - 1] = LYXP_TOKEN_FUNCNAME;
2902
2903 prev_ntype_check = 0;
2904 prev_func_check = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01002905 }
2906
Radek Krejcif03a9e22020-09-18 20:09:31 +02002907 } else if (expr_str[parsed] == ')') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002908
2909 /* ')' */
2910 tok_len = 1;
2911 tok_type = LYXP_TOKEN_PAR2;
2912
Radek Krejcif03a9e22020-09-18 20:09:31 +02002913 } else if (expr_str[parsed] == '[') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002914
2915 /* '[' */
2916 tok_len = 1;
2917 tok_type = LYXP_TOKEN_BRACK1;
2918
Radek Krejcif03a9e22020-09-18 20:09:31 +02002919 } else if (expr_str[parsed] == ']') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002920
2921 /* ']' */
2922 tok_len = 1;
2923 tok_type = LYXP_TOKEN_BRACK2;
2924
Radek Krejcif03a9e22020-09-18 20:09:31 +02002925 } else if (!strncmp(&expr_str[parsed], "..", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002926
2927 /* '..' */
2928 tok_len = 2;
2929 tok_type = LYXP_TOKEN_DDOT;
2930
Radek Krejcif03a9e22020-09-18 20:09:31 +02002931 } else if ((expr_str[parsed] == '.') && (!isdigit(expr_str[parsed + 1]))) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002932
2933 /* '.' */
2934 tok_len = 1;
2935 tok_type = LYXP_TOKEN_DOT;
2936
Radek Krejcif03a9e22020-09-18 20:09:31 +02002937 } else if (expr_str[parsed] == '@') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002938
2939 /* '@' */
2940 tok_len = 1;
2941 tok_type = LYXP_TOKEN_AT;
2942
Radek Krejcif03a9e22020-09-18 20:09:31 +02002943 } else if (expr_str[parsed] == ',') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002944
2945 /* ',' */
2946 tok_len = 1;
2947 tok_type = LYXP_TOKEN_COMMA;
2948
Radek Krejcif03a9e22020-09-18 20:09:31 +02002949 } else if (expr_str[parsed] == '\'') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002950
2951 /* Literal with ' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002952 for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\''); ++tok_len) {}
2953 LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0',
Radek Krejci2efc45b2020-12-22 16:25:44 +01002954 LOGVAL(ctx, LY_VCODE_XP_EOE, expr_str[parsed], &expr_str[parsed]); ret = LY_EVALID,
Michal Vasko69730152020-10-09 16:30:07 +02002955 error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002956 ++tok_len;
2957 tok_type = LYXP_TOKEN_LITERAL;
2958
Radek Krejcif03a9e22020-09-18 20:09:31 +02002959 } else if (expr_str[parsed] == '\"') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002960
2961 /* Literal with " */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002962 for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\"'); ++tok_len) {}
2963 LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0',
Radek Krejci2efc45b2020-12-22 16:25:44 +01002964 LOGVAL(ctx, LY_VCODE_XP_EOE, expr_str[parsed], &expr_str[parsed]); ret = LY_EVALID,
Michal Vasko69730152020-10-09 16:30:07 +02002965 error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002966 ++tok_len;
2967 tok_type = LYXP_TOKEN_LITERAL;
2968
Radek Krejcif03a9e22020-09-18 20:09:31 +02002969 } else if ((expr_str[parsed] == '.') || (isdigit(expr_str[parsed]))) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002970
2971 /* Number */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002972 for (tok_len = 0; isdigit(expr_str[parsed + tok_len]); ++tok_len) {}
2973 if (expr_str[parsed + tok_len] == '.') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002974 ++tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02002975 for ( ; isdigit(expr_str[parsed + tok_len]); ++tok_len) {}
Radek Krejcib1646a92018-11-02 16:08:26 +01002976 }
2977 tok_type = LYXP_TOKEN_NUMBER;
2978
aPiecekfba75362021-10-07 12:39:48 +02002979 } else if (expr_str[parsed] == '$') {
2980
2981 /* VariableReference */
2982 parsed++;
Michal Vasko49fec8e2022-05-24 10:28:33 +02002983 ncname_len = parse_ncname(&expr_str[parsed]);
aPiecekfba75362021-10-07 12:39:48 +02002984 LY_CHECK_ERR_GOTO(ncname_len < 1, LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed - ncname_len],
2985 parsed - ncname_len + 1, expr_str); ret = LY_EVALID, error);
2986 tok_len = ncname_len;
2987 LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == ':',
2988 LOGVAL(ctx, LYVE_XPATH, "Variable with prefix is not supported."); ret = LY_EVALID,
2989 error);
2990 tok_type = LYXP_TOKEN_VARREF;
2991
Radek Krejcif03a9e22020-09-18 20:09:31 +02002992 } else if (expr_str[parsed] == '/') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002993
2994 /* Operator '/', '//' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002995 if (!strncmp(&expr_str[parsed], "//", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002996 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002997 tok_type = LYXP_TOKEN_OPER_RPATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002998 } else {
2999 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003000 tok_type = LYXP_TOKEN_OPER_PATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01003001 }
Radek Krejcib1646a92018-11-02 16:08:26 +01003002
Radek Krejcif03a9e22020-09-18 20:09:31 +02003003 } else if (!strncmp(&expr_str[parsed], "!=", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01003004
Michal Vasko3e48bf32020-06-01 08:39:07 +02003005 /* Operator '!=' */
Radek Krejcib1646a92018-11-02 16:08:26 +01003006 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003007 tok_type = LYXP_TOKEN_OPER_NEQUAL;
3008
Radek Krejcif03a9e22020-09-18 20:09:31 +02003009 } else if (!strncmp(&expr_str[parsed], "<=", 2) || !strncmp(&expr_str[parsed], ">=", 2)) {
Michal Vasko3e48bf32020-06-01 08:39:07 +02003010
3011 /* Operator '<=', '>=' */
3012 tok_len = 2;
3013 tok_type = LYXP_TOKEN_OPER_COMP;
Radek Krejcib1646a92018-11-02 16:08:26 +01003014
Radek Krejcif03a9e22020-09-18 20:09:31 +02003015 } else if (expr_str[parsed] == '|') {
Radek Krejcib1646a92018-11-02 16:08:26 +01003016
3017 /* Operator '|' */
3018 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003019 tok_type = LYXP_TOKEN_OPER_UNI;
Radek Krejcib1646a92018-11-02 16:08:26 +01003020
Radek Krejcif03a9e22020-09-18 20:09:31 +02003021 } else if ((expr_str[parsed] == '+') || (expr_str[parsed] == '-')) {
Radek Krejcib1646a92018-11-02 16:08:26 +01003022
3023 /* Operator '+', '-' */
3024 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003025 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01003026
Radek Krejcif03a9e22020-09-18 20:09:31 +02003027 } else if (expr_str[parsed] == '=') {
Radek Krejcib1646a92018-11-02 16:08:26 +01003028
Michal Vasko3e48bf32020-06-01 08:39:07 +02003029 /* Operator '=' */
Radek Krejcib1646a92018-11-02 16:08:26 +01003030 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003031 tok_type = LYXP_TOKEN_OPER_EQUAL;
3032
Radek Krejcif03a9e22020-09-18 20:09:31 +02003033 } else if ((expr_str[parsed] == '<') || (expr_str[parsed] == '>')) {
Michal Vasko3e48bf32020-06-01 08:39:07 +02003034
3035 /* Operator '<', '>' */
3036 tok_len = 1;
3037 tok_type = LYXP_TOKEN_OPER_COMP;
Radek Krejcib1646a92018-11-02 16:08:26 +01003038
Michal Vasko69730152020-10-09 16:30:07 +02003039 } else if (expr->used && (expr->tokens[expr->used - 1] != LYXP_TOKEN_AT) &&
3040 (expr->tokens[expr->used - 1] != LYXP_TOKEN_PAR1) &&
3041 (expr->tokens[expr->used - 1] != LYXP_TOKEN_BRACK1) &&
3042 (expr->tokens[expr->used - 1] != LYXP_TOKEN_COMMA) &&
3043 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_LOG) &&
3044 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_EQUAL) &&
3045 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_NEQUAL) &&
3046 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_COMP) &&
3047 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_MATH) &&
3048 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_UNI) &&
3049 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_PATH) &&
3050 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_RPATH)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01003051
3052 /* Operator '*', 'or', 'and', 'mod', or 'div' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02003053 if (expr_str[parsed] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01003054 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003055 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01003056
Radek Krejcif03a9e22020-09-18 20:09:31 +02003057 } else if (!strncmp(&expr_str[parsed], "or", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01003058 tok_len = 2;
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], "and", 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_LOG;
Radek Krejcib1646a92018-11-02 16:08:26 +01003064
Radek Krejcif03a9e22020-09-18 20:09:31 +02003065 } else if (!strncmp(&expr_str[parsed], "mod", 3) || !strncmp(&expr_str[parsed], "div", 3)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01003066 tok_len = 3;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003067 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01003068
Michal Vasko49fec8e2022-05-24 10:28:33 +02003069 } else if (prev_ntype_check || prev_func_check) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003070 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 +02003071 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 +02003072 ret = LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01003073 goto error;
3074 } else {
Michal Vasko774ce402021-04-14 15:35:06 +02003075 LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed], parsed + 1, expr_str);
Radek Krejcif03a9e22020-09-18 20:09:31 +02003076 ret = LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01003077 goto error;
3078 }
Radek Krejcib1646a92018-11-02 16:08:26 +01003079 } else {
3080
Michal Vasko49fec8e2022-05-24 10:28:33 +02003081 /* (AxisName '::')? ((NCName ':')? '*' | QName) or NodeType/FunctionName */
3082 if (expr_str[parsed] == '*') {
3083 ncname_len = 1;
3084 } else {
3085 ncname_len = parse_ncname(&expr_str[parsed]);
3086 LY_CHECK_ERR_GOTO(ncname_len < 1, LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed - ncname_len],
3087 parsed - ncname_len + 1, expr_str); ret = LY_EVALID, error);
3088 }
Radek Krejcib1646a92018-11-02 16:08:26 +01003089 tok_len = ncname_len;
3090
Michal Vasko49fec8e2022-05-24 10:28:33 +02003091 has_axis = 0;
3092 if (!strncmp(&expr_str[parsed + tok_len], "::", 2)) {
3093 /* axis */
3094 LY_CHECK_ERR_GOTO(expr_parse_axis(&expr_str[parsed], ncname_len),
3095 LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed], parsed + 1, expr_str); ret = LY_EVALID, error);
3096 tok_type = LYXP_TOKEN_AXISNAME;
3097
3098 LY_CHECK_GOTO(ret = exp_add_token(ctx, expr, tok_type, parsed, tok_len), error);
3099 parsed += tok_len;
3100
3101 /* '::' */
3102 tok_len = 2;
3103 tok_type = LYXP_TOKEN_DCOLON;
3104
3105 LY_CHECK_GOTO(ret = exp_add_token(ctx, expr, tok_type, parsed, tok_len), error);
3106 parsed += tok_len;
3107
3108 if (expr_str[parsed] == '*') {
3109 ncname_len = 1;
3110 } else {
3111 ncname_len = parse_ncname(&expr_str[parsed]);
3112 LY_CHECK_ERR_GOTO(ncname_len < 1, LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed - ncname_len],
3113 parsed - ncname_len + 1, expr_str); ret = LY_EVALID, error);
3114 }
3115 tok_len = ncname_len;
3116
3117 has_axis = 1;
3118 }
3119
Radek Krejcif03a9e22020-09-18 20:09:31 +02003120 if (expr_str[parsed + tok_len] == ':') {
Radek Krejcib1646a92018-11-02 16:08:26 +01003121 ++tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02003122 if (expr_str[parsed + tok_len] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01003123 ++tok_len;
3124 } else {
Radek Krejcif03a9e22020-09-18 20:09:31 +02003125 ncname_len = parse_ncname(&expr_str[parsed + tok_len]);
Michal Vaskoe2be5462021-08-04 10:49:42 +02003126 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 +02003127 parsed - ncname_len + 1, expr_str); ret = LY_EVALID, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01003128 tok_len += ncname_len;
3129 }
Michal Vasko49fec8e2022-05-24 10:28:33 +02003130 /* remove old flags to prevent ambiguities */
3131 prev_ntype_check = 0;
3132 prev_func_check = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01003133 tok_type = LYXP_TOKEN_NAMETEST;
3134 } else {
Michal Vasko49fec8e2022-05-24 10:28:33 +02003135 /* if not '*', there is no prefix so it can still be NodeType/FunctionName, we can't finally decide now */
3136 prev_ntype_check = (expr_str[parsed] == '*') ? 0 : 1;
3137 prev_func_check = (prev_ntype_check && !has_axis) ? 1 : 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01003138 tok_type = LYXP_TOKEN_NAMETEST;
3139 }
3140 }
3141
3142 /* store the token, move on to the next one */
Radek Krejcif03a9e22020-09-18 20:09:31 +02003143 LY_CHECK_GOTO(ret = exp_add_token(ctx, expr, tok_type, parsed, tok_len), error);
Radek Krejcib1646a92018-11-02 16:08:26 +01003144 parsed += tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02003145 while (is_xmlws(expr_str[parsed])) {
Radek Krejcib1646a92018-11-02 16:08:26 +01003146 ++parsed;
3147 }
3148
Radek Krejcif03a9e22020-09-18 20:09:31 +02003149 } while (expr_str[parsed]);
Radek Krejcib1646a92018-11-02 16:08:26 +01003150
Michal Vasko004d3152020-06-11 19:59:22 +02003151 if (reparse) {
3152 /* prealloc repeat */
Radek Krejcif03a9e22020-09-18 20:09:31 +02003153 expr->repeat = calloc(expr->size, sizeof *expr->repeat);
3154 LY_CHECK_ERR_GOTO(!expr->repeat, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01003155
Michal Vasko004d3152020-06-11 19:59:22 +02003156 /* fill repeat */
aPiecekbf968d92021-05-27 14:35:05 +02003157 LY_CHECK_ERR_GOTO(reparse_or_expr(ctx, expr, &tok_idx, 0), ret = LY_EVALID, error);
Radek Krejcif03a9e22020-09-18 20:09:31 +02003158 if (expr->used > tok_idx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003159 LOGVAL(ctx, LYVE_XPATH, "Unparsed characters \"%s\" left at the end of an XPath expression.",
Michal Vasko69730152020-10-09 16:30:07 +02003160 &expr->expr[expr->tok_pos[tok_idx]]);
Radek Krejcif03a9e22020-09-18 20:09:31 +02003161 ret = LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02003162 goto error;
3163 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003164 }
3165
Radek Krejcif03a9e22020-09-18 20:09:31 +02003166 print_expr_struct_debug(expr);
3167 *expr_p = expr;
3168 return LY_SUCCESS;
Radek Krejcib1646a92018-11-02 16:08:26 +01003169
3170error:
Radek Krejcif03a9e22020-09-18 20:09:31 +02003171 lyxp_expr_free(ctx, expr);
3172 return ret;
Radek Krejcib1646a92018-11-02 16:08:26 +01003173}
3174
Michal Vasko1734be92020-09-22 08:55:10 +02003175LY_ERR
3176lyxp_expr_dup(const struct ly_ctx *ctx, const struct lyxp_expr *exp, struct lyxp_expr **dup_p)
Michal Vasko004d3152020-06-11 19:59:22 +02003177{
Michal Vasko1734be92020-09-22 08:55:10 +02003178 LY_ERR ret = LY_SUCCESS;
3179 struct lyxp_expr *dup = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02003180 uint32_t i, j;
3181
Michal Vasko7f45cf22020-10-01 12:49:44 +02003182 if (!exp) {
3183 goto cleanup;
3184 }
3185
Michal Vasko004d3152020-06-11 19:59:22 +02003186 dup = calloc(1, sizeof *dup);
Michal Vasko1734be92020-09-22 08:55:10 +02003187 LY_CHECK_ERR_GOTO(!dup, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02003188
Michal Vasko08e9b112021-06-11 15:41:17 +02003189 if (exp->used) {
3190 dup->tokens = malloc(exp->used * sizeof *dup->tokens);
3191 LY_CHECK_ERR_GOTO(!dup->tokens, LOGMEM(ctx); ret = LY_EMEM, cleanup);
3192 memcpy(dup->tokens, exp->tokens, exp->used * sizeof *dup->tokens);
Michal Vasko004d3152020-06-11 19:59:22 +02003193
Michal Vasko08e9b112021-06-11 15:41:17 +02003194 dup->tok_pos = malloc(exp->used * sizeof *dup->tok_pos);
3195 LY_CHECK_ERR_GOTO(!dup->tok_pos, LOGMEM(ctx); ret = LY_EMEM, cleanup);
3196 memcpy(dup->tok_pos, exp->tok_pos, exp->used * sizeof *dup->tok_pos);
Michal Vasko004d3152020-06-11 19:59:22 +02003197
Michal Vasko08e9b112021-06-11 15:41:17 +02003198 dup->tok_len = malloc(exp->used * sizeof *dup->tok_len);
3199 LY_CHECK_ERR_GOTO(!dup->tok_len, LOGMEM(ctx); ret = LY_EMEM, cleanup);
3200 memcpy(dup->tok_len, exp->tok_len, exp->used * sizeof *dup->tok_len);
Michal Vasko004d3152020-06-11 19:59:22 +02003201
Michal Vasko79a7a872022-06-17 09:00:48 +02003202 if (exp->repeat) {
3203 dup->repeat = malloc(exp->used * sizeof *dup->repeat);
3204 LY_CHECK_ERR_GOTO(!dup->repeat, LOGMEM(ctx); ret = LY_EMEM, cleanup);
3205 for (i = 0; i < exp->used; ++i) {
3206 if (!exp->repeat[i]) {
3207 dup->repeat[i] = NULL;
3208 } else {
3209 for (j = 0; exp->repeat[i][j]; ++j) {}
3210 /* the ending 0 as well */
3211 ++j;
Michal Vasko004d3152020-06-11 19:59:22 +02003212
Michal Vasko79a7a872022-06-17 09:00:48 +02003213 dup->repeat[i] = malloc(j * sizeof **dup->repeat);
3214 LY_CHECK_ERR_GOTO(!dup->repeat[i], LOGMEM(ctx); ret = LY_EMEM, cleanup);
3215 memcpy(dup->repeat[i], exp->repeat[i], j * sizeof **dup->repeat);
3216 dup->repeat[i][j - 1] = 0;
3217 }
Michal Vasko08e9b112021-06-11 15:41:17 +02003218 }
Michal Vasko004d3152020-06-11 19:59:22 +02003219 }
3220 }
3221
3222 dup->used = exp->used;
3223 dup->size = exp->used;
Michal Vasko1734be92020-09-22 08:55:10 +02003224 LY_CHECK_GOTO(ret = lydict_insert(ctx, exp->expr, 0, &dup->expr), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02003225
Michal Vasko1734be92020-09-22 08:55:10 +02003226cleanup:
3227 if (ret) {
3228 lyxp_expr_free(ctx, dup);
3229 } else {
3230 *dup_p = dup;
3231 }
3232 return ret;
Michal Vasko004d3152020-06-11 19:59:22 +02003233}
3234
Michal Vasko03ff5a72019-09-11 13:49:33 +02003235/**
3236 * @brief Get the last-added schema node that is currently in the context.
3237 *
3238 * @param[in] set Set to search in.
3239 * @return Last-added schema context node, NULL if no node is in context.
3240 */
3241static struct lysc_node *
3242warn_get_scnode_in_ctx(struct lyxp_set *set)
3243{
3244 uint32_t i;
3245
3246 if (!set || (set->type != LYXP_SET_SCNODE_SET)) {
3247 return NULL;
3248 }
3249
3250 i = set->used;
3251 do {
3252 --i;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003253 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003254 /* if there are more, simply return the first found (last added) */
3255 return set->val.scnodes[i].scnode;
3256 }
3257 } while (i);
3258
3259 return NULL;
3260}
3261
3262/**
3263 * @brief Test whether a type is numeric - integer type or decimal64.
3264 *
3265 * @param[in] type Type to test.
Radek Krejci857189e2020-09-01 13:26:36 +02003266 * @return Boolean value whether @p type is numeric type or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003267 */
Radek Krejci857189e2020-09-01 13:26:36 +02003268static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003269warn_is_numeric_type(struct lysc_type *type)
3270{
3271 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003272 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003273 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003274
3275 switch (type->basetype) {
3276 case LY_TYPE_DEC64:
3277 case LY_TYPE_INT8:
3278 case LY_TYPE_UINT8:
3279 case LY_TYPE_INT16:
3280 case LY_TYPE_UINT16:
3281 case LY_TYPE_INT32:
3282 case LY_TYPE_UINT32:
3283 case LY_TYPE_INT64:
3284 case LY_TYPE_UINT64:
3285 return 1;
3286 case LY_TYPE_UNION:
3287 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003288 LY_ARRAY_FOR(uni->types, u) {
3289 ret = warn_is_numeric_type(uni->types[u]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003290 if (ret) {
3291 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003292 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003293 }
3294 }
3295 /* did not find any suitable type */
3296 return 0;
3297 case LY_TYPE_LEAFREF:
3298 return warn_is_numeric_type(((struct lysc_type_leafref *)type)->realtype);
3299 default:
3300 return 0;
3301 }
3302}
3303
3304/**
3305 * @brief Test whether a type is string-like - no integers, decimal64 or binary.
3306 *
3307 * @param[in] type Type to test.
Radek Krejci857189e2020-09-01 13:26:36 +02003308 * @return Boolean value whether @p type's basetype is string type or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003309 */
Radek Krejci857189e2020-09-01 13:26:36 +02003310static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003311warn_is_string_type(struct lysc_type *type)
3312{
3313 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003314 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003315 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003316
3317 switch (type->basetype) {
3318 case LY_TYPE_BITS:
3319 case LY_TYPE_ENUM:
3320 case LY_TYPE_IDENT:
3321 case LY_TYPE_INST:
3322 case LY_TYPE_STRING:
3323 return 1;
3324 case LY_TYPE_UNION:
3325 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003326 LY_ARRAY_FOR(uni->types, u) {
3327 ret = warn_is_string_type(uni->types[u]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003328 if (ret) {
3329 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003330 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003331 }
3332 }
3333 /* did not find any suitable type */
3334 return 0;
3335 case LY_TYPE_LEAFREF:
3336 return warn_is_string_type(((struct lysc_type_leafref *)type)->realtype);
3337 default:
3338 return 0;
3339 }
3340}
3341
3342/**
3343 * @brief Test whether a type is one specific type.
3344 *
3345 * @param[in] type Type to test.
3346 * @param[in] base Expected type.
Radek Krejci857189e2020-09-01 13:26:36 +02003347 * @return Boolean value whether the given @p type is of the specific basetype @p base.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003348 */
Radek Krejci857189e2020-09-01 13:26:36 +02003349static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003350warn_is_specific_type(struct lysc_type *type, LY_DATA_TYPE base)
3351{
3352 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003353 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003354 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003355
3356 if (type->basetype == base) {
3357 return 1;
3358 } else if (type->basetype == LY_TYPE_UNION) {
3359 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003360 LY_ARRAY_FOR(uni->types, u) {
3361 ret = warn_is_specific_type(uni->types[u], base);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003362 if (ret) {
3363 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003364 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003365 }
3366 }
3367 /* did not find any suitable type */
3368 return 0;
3369 } else if (type->basetype == LY_TYPE_LEAFREF) {
3370 return warn_is_specific_type(((struct lysc_type_leafref *)type)->realtype, base);
3371 }
3372
3373 return 0;
3374}
3375
3376/**
3377 * @brief Get next type of a (union) type.
3378 *
3379 * @param[in] type Base type.
3380 * @param[in] prev_type Previously returned type.
3381 * @return Next type or NULL.
3382 */
3383static struct lysc_type *
3384warn_is_equal_type_next_type(struct lysc_type *type, struct lysc_type *prev_type)
3385{
3386 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003387 ly_bool found = 0;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003388 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003389
3390 switch (type->basetype) {
3391 case LY_TYPE_UNION:
3392 uni = (struct lysc_type_union *)type;
3393 if (!prev_type) {
3394 return uni->types[0];
3395 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003396 LY_ARRAY_FOR(uni->types, u) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003397 if (found) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003398 return uni->types[u];
Michal Vasko03ff5a72019-09-11 13:49:33 +02003399 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003400 if (prev_type == uni->types[u]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003401 found = 1;
3402 }
3403 }
3404 return NULL;
3405 default:
3406 if (prev_type) {
3407 assert(type == prev_type);
3408 return NULL;
3409 } else {
3410 return type;
3411 }
3412 }
3413}
3414
3415/**
3416 * @brief Test whether 2 types have a common type.
3417 *
3418 * @param[in] type1 First type.
3419 * @param[in] type2 Second type.
3420 * @return 1 if they do, 0 otherwise.
3421 */
3422static int
3423warn_is_equal_type(struct lysc_type *type1, struct lysc_type *type2)
3424{
3425 struct lysc_type *t1, *rt1, *t2, *rt2;
3426
3427 t1 = NULL;
3428 while ((t1 = warn_is_equal_type_next_type(type1, t1))) {
3429 if (t1->basetype == LY_TYPE_LEAFREF) {
3430 rt1 = ((struct lysc_type_leafref *)t1)->realtype;
3431 } else {
3432 rt1 = t1;
3433 }
3434
3435 t2 = NULL;
3436 while ((t2 = warn_is_equal_type_next_type(type2, t2))) {
3437 if (t2->basetype == LY_TYPE_LEAFREF) {
3438 rt2 = ((struct lysc_type_leafref *)t2)->realtype;
3439 } else {
3440 rt2 = t2;
3441 }
3442
3443 if (rt2->basetype == rt1->basetype) {
3444 /* match found */
3445 return 1;
3446 }
3447 }
3448 }
3449
3450 return 0;
3451}
3452
3453/**
Michal Vaskoaa956522021-11-11 10:45:34 +01003454 * @brief Print warning with information about the XPath subexpression that caused previous warning.
3455 *
3456 * @param[in] ctx Context for logging.
3457 * @param[in] tok_pos Index of the subexpression in the whole expression.
3458 * @param[in] subexpr Subexpression start.
3459 * @param[in] subexpr_len Length of @p subexpr to print.
3460 * @param[in] cur_scnode Expression context node.
3461 */
3462static void
3463warn_subexpr_log(const struct ly_ctx *ctx, uint16_t tok_pos, const char *subexpr, int subexpr_len,
3464 const struct lysc_node *cur_scnode)
3465{
3466 char *path;
3467
3468 path = lysc_path(cur_scnode, LYSC_PATH_LOG, NULL, 0);
3469 LOGWRN(ctx, "Previous warning generated by XPath subexpression[%" PRIu16 "] \"%.*s\" with context node \"%s\".",
3470 tok_pos, subexpr_len, subexpr, path);
3471 free(path);
3472}
3473
3474/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02003475 * @brief Check both operands of comparison operators.
3476 *
3477 * @param[in] ctx Context for errors.
3478 * @param[in] set1 First operand set.
3479 * @param[in] set2 Second operand set.
3480 * @param[in] numbers_only Whether accept only numbers or other types are fine too (for '=' and '!=').
3481 * @param[in] expr Start of the expression to print with the warning.
3482 * @param[in] tok_pos Token position.
3483 */
3484static void
Michal Vaskoaa956522021-11-11 10:45:34 +01003485warn_operands(struct ly_ctx *ctx, struct lyxp_set *set1, struct lyxp_set *set2, ly_bool numbers_only, const char *expr,
3486 uint16_t tok_pos)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003487{
3488 struct lysc_node_leaf *node1, *node2;
Radek Krejci857189e2020-09-01 13:26:36 +02003489 ly_bool leaves = 1, warning = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003490
3491 node1 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set1);
3492 node2 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set2);
3493
3494 if (!node1 && !node2) {
3495 /* no node-sets involved, nothing to do */
3496 return;
3497 }
3498
3499 if (node1) {
3500 if (!(node1->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3501 LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node1->nodetype), node1->name);
3502 warning = 1;
3503 leaves = 0;
3504 } else if (numbers_only && !warn_is_numeric_type(node1->type)) {
3505 LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node1->name);
3506 warning = 1;
3507 }
3508 }
3509
3510 if (node2) {
3511 if (!(node2->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3512 LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node2->nodetype), node2->name);
3513 warning = 1;
3514 leaves = 0;
3515 } else if (numbers_only && !warn_is_numeric_type(node2->type)) {
3516 LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node2->name);
3517 warning = 1;
3518 }
3519 }
3520
3521 if (node1 && node2 && leaves && !numbers_only) {
Michal Vasko69730152020-10-09 16:30:07 +02003522 if ((warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type)) ||
3523 (!warn_is_numeric_type(node1->type) && warn_is_numeric_type(node2->type)) ||
3524 (!warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type) &&
3525 !warn_is_equal_type(node1->type, node2->type))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003526 LOGWRN(ctx, "Incompatible types of operands \"%s\" and \"%s\" for comparison.", node1->name, node2->name);
3527 warning = 1;
3528 }
3529 }
3530
3531 if (warning) {
Michal Vaskoaa956522021-11-11 10:45:34 +01003532 warn_subexpr_log(ctx, tok_pos, expr + tok_pos, 20, set1->cur_scnode);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003533 }
3534}
3535
3536/**
3537 * @brief Check that a value is valid for a leaf. If not applicable, does nothing.
3538 *
3539 * @param[in] exp Parsed XPath expression.
3540 * @param[in] set Set with the leaf/leaf-list.
3541 * @param[in] val_exp Index of the value (literal/number) in @p exp.
3542 * @param[in] equal_exp Index of the start of the equality expression in @p exp.
3543 * @param[in] last_equal_exp Index of the end of the equality expression in @p exp.
3544 */
3545static void
Michal Vasko40308e72020-10-20 16:38:40 +02003546warn_equality_value(const struct lyxp_expr *exp, struct lyxp_set *set, uint16_t val_exp, uint16_t equal_exp,
3547 uint16_t last_equal_exp)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003548{
3549 struct lysc_node *scnode;
3550 struct lysc_type *type;
3551 char *value;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003552 struct lyd_value storage;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003553 LY_ERR rc;
3554 struct ly_err_item *err = NULL;
3555
Michal Vasko69730152020-10-09 16:30:07 +02003556 if ((scnode = warn_get_scnode_in_ctx(set)) && (scnode->nodetype & (LYS_LEAF | LYS_LEAFLIST)) &&
3557 ((exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) || (exp->tokens[val_exp] == LYXP_TOKEN_NUMBER))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003558 /* check that the node can have the specified value */
3559 if (exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) {
3560 value = strndup(exp->expr + exp->tok_pos[val_exp] + 1, exp->tok_len[val_exp] - 2);
3561 } else {
3562 value = strndup(exp->expr + exp->tok_pos[val_exp], exp->tok_len[val_exp]);
3563 }
3564 if (!value) {
3565 LOGMEM(set->ctx);
3566 return;
3567 }
3568
3569 if ((((struct lysc_node_leaf *)scnode)->type->basetype == LY_TYPE_IDENT) && !strchr(value, ':')) {
3570 LOGWRN(set->ctx, "Identityref \"%s\" comparison with identity \"%s\" without prefix, consider adding"
Michal Vasko69730152020-10-09 16:30:07 +02003571 " a prefix or best using \"derived-from(-or-self)()\" functions.", scnode->name, value);
Michal Vaskoaa956522021-11-11 10:45:34 +01003572 warn_subexpr_log(set->ctx, exp->tok_pos[equal_exp], exp->expr + exp->tok_pos[equal_exp],
Radek Krejci0f969882020-08-21 16:56:47 +02003573 (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp],
Michal Vaskoaa956522021-11-11 10:45:34 +01003574 set->cur_scnode);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003575 }
3576
3577 type = ((struct lysc_node_leaf *)scnode)->type;
3578 if (type->basetype != LY_TYPE_IDENT) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003579 rc = type->plugin->store(set->ctx, type, value, strlen(value), 0, set->format, set->prefix_data,
Michal Vasko405cc9e2020-12-01 12:01:27 +01003580 LYD_HINT_DATA, scnode, &storage, NULL, &err);
Michal Vaskobf42e832020-11-23 16:59:42 +01003581 if (rc == LY_EINCOMPLETE) {
3582 rc = LY_SUCCESS;
3583 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003584
3585 if (err) {
3586 LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type (%s).", value, err->msg);
3587 ly_err_free(err);
3588 } else if (rc != LY_SUCCESS) {
3589 LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type.", value);
3590 }
3591 if (rc != LY_SUCCESS) {
Michal Vaskoaa956522021-11-11 10:45:34 +01003592 warn_subexpr_log(set->ctx, exp->tok_pos[equal_exp], exp->expr + exp->tok_pos[equal_exp],
Radek Krejci0f969882020-08-21 16:56:47 +02003593 (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp],
Michal Vaskoaa956522021-11-11 10:45:34 +01003594 set->cur_scnode);
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003595 } else {
3596 type->plugin->free(set->ctx, &storage);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003597 }
3598 }
3599 free(value);
3600 }
3601}
3602
3603/*
3604 * XPath functions
3605 */
3606
3607/**
3608 * @brief Execute the YANG 1.1 bit-is-set(node-set, string) function. Returns LYXP_SET_BOOLEAN
3609 * depending on whether the first node bit value from the second argument is set.
3610 *
3611 * @param[in] args Array of arguments.
3612 * @param[in] arg_count Count of elements in @p args.
3613 * @param[in,out] set Context and result set at the same time.
3614 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003615 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003616 */
3617static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003618xpath_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 +02003619{
3620 struct lyd_node_term *leaf;
3621 struct lysc_node_leaf *sleaf;
Michal Vasko2588b952021-07-29 07:43:26 +02003622 struct lyd_value_bits *bits;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003623 LY_ERR rc = LY_SUCCESS;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003624 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003625
3626 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003627 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003628 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003629 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3630 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3631 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3632 sleaf->name);
3633 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_BITS)) {
3634 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"bits\".", __func__, sleaf->name);
3635 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003636 }
3637
3638 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3639 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003640 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3641 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003642 } else if (!warn_is_string_type(sleaf->type)) {
3643 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003644 }
3645 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003646 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003647 return rc;
3648 }
3649
Michal Vaskod3678892020-05-21 10:06:58 +02003650 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003651 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 +02003652 return LY_EVALID;
3653 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003654 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003655 LY_CHECK_RET(rc);
3656
3657 set_fill_boolean(set, 0);
Michal Vaskod3678892020-05-21 10:06:58 +02003658 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003659 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
Michal Vasko2588b952021-07-29 07:43:26 +02003660 if ((leaf->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (leaf->value.realtype->basetype == LY_TYPE_BITS)) {
3661 LYD_VALUE_GET(&leaf->value, bits);
3662 LY_ARRAY_FOR(bits->items, u) {
3663 if (!strcmp(bits->items[u]->name, args[1]->val.str)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003664 set_fill_boolean(set, 1);
3665 break;
3666 }
3667 }
3668 }
3669 }
3670
3671 return LY_SUCCESS;
3672}
3673
3674/**
3675 * @brief Execute the XPath boolean(object) function. Returns LYXP_SET_BOOLEAN
3676 * with the argument converted to boolean.
3677 *
3678 * @param[in] args Array of arguments.
3679 * @param[in] arg_count Count of elements in @p args.
3680 * @param[in,out] set Context and result set at the same time.
3681 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003682 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003683 */
3684static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003685xpath_boolean(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003686{
3687 LY_ERR rc;
3688
3689 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02003690 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003691 return LY_SUCCESS;
3692 }
3693
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003694 rc = lyxp_set_cast(args[0], LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003695 LY_CHECK_RET(rc);
3696 set_fill_set(set, args[0]);
3697
3698 return LY_SUCCESS;
3699}
3700
3701/**
3702 * @brief Execute the XPath ceiling(number) function. Returns LYXP_SET_NUMBER
3703 * with the first argument rounded up to the nearest integer.
3704 *
3705 * @param[in] args Array of arguments.
3706 * @param[in] arg_count Count of elements in @p args.
3707 * @param[in,out] set Context and result set at the same time.
3708 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003709 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003710 */
3711static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003712xpath_ceiling(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003713{
3714 struct lysc_node_leaf *sleaf;
3715 LY_ERR rc = LY_SUCCESS;
3716
3717 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003718 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003719 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003720 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3721 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3722 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3723 sleaf->name);
3724 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
3725 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
3726 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003727 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003728 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003729 return rc;
3730 }
3731
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003732 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003733 LY_CHECK_RET(rc);
3734 if ((long long)args[0]->val.num != args[0]->val.num) {
3735 set_fill_number(set, ((long long)args[0]->val.num) + 1);
3736 } else {
3737 set_fill_number(set, args[0]->val.num);
3738 }
3739
3740 return LY_SUCCESS;
3741}
3742
3743/**
3744 * @brief Execute the XPath concat(string, string, string*) function.
3745 * Returns LYXP_SET_STRING with the concatenation of all the arguments.
3746 *
3747 * @param[in] args Array of arguments.
3748 * @param[in] arg_count Count of elements in @p args.
3749 * @param[in,out] set Context and result set at the same time.
3750 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003751 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003752 */
3753static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003754xpath_concat(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003755{
3756 uint16_t i;
3757 char *str = NULL;
3758 size_t used = 1;
3759 LY_ERR rc = LY_SUCCESS;
3760 struct lysc_node_leaf *sleaf;
3761
3762 if (options & LYXP_SCNODE_ALL) {
3763 for (i = 0; i < arg_count; ++i) {
3764 if ((args[i]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[i]))) {
3765 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3766 LOGWRN(set->ctx, "Argument #%u of %s is a %s node \"%s\".",
Michal Vasko69730152020-10-09 16:30:07 +02003767 i + 1, __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003768 } else if (!warn_is_string_type(sleaf->type)) {
Radek Krejci70124c82020-08-14 22:17:03 +02003769 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 +02003770 }
3771 }
3772 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003773 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003774 return rc;
3775 }
3776
3777 for (i = 0; i < arg_count; ++i) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003778 rc = lyxp_set_cast(args[i], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003779 if (rc != LY_SUCCESS) {
3780 free(str);
3781 return rc;
3782 }
3783
3784 str = ly_realloc(str, (used + strlen(args[i]->val.str)) * sizeof(char));
3785 LY_CHECK_ERR_RET(!str, LOGMEM(set->ctx), LY_EMEM);
3786 strcpy(str + used - 1, args[i]->val.str);
3787 used += strlen(args[i]->val.str);
3788 }
3789
3790 /* free, kind of */
Michal Vaskod3678892020-05-21 10:06:58 +02003791 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003792 set->type = LYXP_SET_STRING;
3793 set->val.str = str;
3794
3795 return LY_SUCCESS;
3796}
3797
3798/**
3799 * @brief Execute the XPath contains(string, string) function.
3800 * Returns LYXP_SET_BOOLEAN whether the second argument can
3801 * be found in the first or not.
3802 *
3803 * @param[in] args Array of arguments.
3804 * @param[in] arg_count Count of elements in @p args.
3805 * @param[in,out] set Context and result set at the same time.
3806 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003807 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003808 */
3809static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003810xpath_contains(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003811{
3812 struct lysc_node_leaf *sleaf;
3813 LY_ERR rc = LY_SUCCESS;
3814
3815 if (options & LYXP_SCNODE_ALL) {
3816 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3817 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003818 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3819 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003820 } else if (!warn_is_string_type(sleaf->type)) {
3821 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003822 }
3823 }
3824
3825 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3826 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003827 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3828 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003829 } else if (!warn_is_string_type(sleaf->type)) {
3830 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003831 }
3832 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003833 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003834 return rc;
3835 }
3836
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003837 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003838 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003839 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003840 LY_CHECK_RET(rc);
3841
3842 if (strstr(args[0]->val.str, args[1]->val.str)) {
3843 set_fill_boolean(set, 1);
3844 } else {
3845 set_fill_boolean(set, 0);
3846 }
3847
3848 return LY_SUCCESS;
3849}
3850
3851/**
3852 * @brief Execute the XPath count(node-set) function. Returns LYXP_SET_NUMBER
3853 * with the size of the node-set from the argument.
3854 *
3855 * @param[in] args Array of arguments.
3856 * @param[in] arg_count Count of elements in @p args.
3857 * @param[in,out] set Context and result set at the same time.
3858 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003859 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003860 */
3861static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003862xpath_count(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003863{
Michal Vasko03ff5a72019-09-11 13:49:33 +02003864 LY_ERR rc = LY_SUCCESS;
3865
3866 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003867 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003868 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003869 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003870 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003871 return rc;
3872 }
3873
Michal Vasko03ff5a72019-09-11 13:49:33 +02003874 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003875 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "count(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003876 return LY_EVALID;
3877 }
3878
3879 set_fill_number(set, args[0]->used);
3880 return LY_SUCCESS;
3881}
3882
3883/**
3884 * @brief Execute the XPath current() function. Returns LYXP_SET_NODE_SET
3885 * with the context with the intial node.
3886 *
3887 * @param[in] args Array of arguments.
3888 * @param[in] arg_count Count of elements in @p args.
3889 * @param[in,out] set Context and result set at the same time.
3890 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003891 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003892 */
3893static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003894xpath_current(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003895{
3896 if (arg_count || args) {
Radek Krejcie87c7dc2021-06-02 21:25:42 +02003897 LOGVAL(set->ctx, LY_VCODE_XP_INARGCOUNT, arg_count, LY_PRI_LENSTR("current()"));
Michal Vasko03ff5a72019-09-11 13:49:33 +02003898 return LY_EVALID;
3899 }
3900
3901 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02003902 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003903
Michal Vasko296dfaf2021-12-13 16:57:42 +01003904 if (set->cur_scnode) {
3905 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, set->cur_scnode, LYXP_NODE_ELEM, NULL));
3906 } else {
3907 /* root node */
3908 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, NULL, set->root_type, NULL));
3909 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003910 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02003911 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003912
Michal Vasko296dfaf2021-12-13 16:57:42 +01003913 if (set->cur_node) {
3914 /* position is filled later */
3915 set_insert_node(set, set->cur_node, 0, LYXP_NODE_ELEM, 0);
3916 } else {
3917 /* root node */
3918 set_insert_node(set, NULL, 0, set->root_type, 0);
3919 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003920 }
3921
3922 return LY_SUCCESS;
3923}
3924
3925/**
3926 * @brief Execute the YANG 1.1 deref(node-set) function. Returns LYXP_SET_NODE_SET with either
3927 * leafref or instance-identifier target node(s).
3928 *
3929 * @param[in] args Array of arguments.
3930 * @param[in] arg_count Count of elements in @p args.
3931 * @param[in,out] set Context and result set at the same time.
3932 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003933 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003934 */
3935static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003936xpath_deref(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003937{
3938 struct lyd_node_term *leaf;
Michal Vasko42e497c2020-01-06 08:38:25 +01003939 struct lysc_node_leaf *sleaf = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02003940 struct lysc_type_leafref *lref;
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003941 const struct lysc_node *target;
Michal Vasko004d3152020-06-11 19:59:22 +02003942 struct ly_path *p;
3943 struct lyd_node *node;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003944 char *errmsg = NULL;
Michal Vasko00cbf532020-06-15 13:58:47 +02003945 uint8_t oper;
Michal Vasko741bb562021-06-24 11:59:50 +02003946 LY_ERR r;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003947
3948 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003949 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003950 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003951 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
Michal Vasko423ba3f2021-07-19 13:08:50 +02003952 if (!(sleaf->nodetype & LYD_NODE_TERM)) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003953 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3954 sleaf->name);
Michal Vaskoed725d72021-06-23 12:03:45 +02003955 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_LEAFREF) &&
3956 !warn_is_specific_type(sleaf->type, LY_TYPE_INST)) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003957 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"leafref\" nor \"instance-identifier\".",
3958 __func__, sleaf->name);
3959 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003960 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003961 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko423ba3f2021-07-19 13:08:50 +02003962 if (sleaf && (sleaf->nodetype & LYD_NODE_TERM) && (sleaf->type->basetype == LY_TYPE_LEAFREF)) {
Michal Vasko004d3152020-06-11 19:59:22 +02003963 lref = (struct lysc_type_leafref *)sleaf->type;
Michal Vaskod1e53b92021-01-28 13:11:06 +01003964 oper = (sleaf->flags & LYS_IS_OUTPUT) ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT;
Michal Vasko004d3152020-06-11 19:59:22 +02003965
3966 /* it was already evaluated on schema, it must succeed */
Michal Vasko741bb562021-06-24 11:59:50 +02003967 r = ly_path_compile_leafref(set->ctx, &sleaf->node, NULL, lref->path, oper, LY_PATH_TARGET_MANY,
Michal Vasko24fc4d12021-07-12 14:41:20 +02003968 LY_VALUE_SCHEMA_RESOLVED, lref->prefixes, &p);
Michal Vasko741bb562021-06-24 11:59:50 +02003969 if (!r) {
3970 /* get the target node */
3971 target = p[LY_ARRAY_COUNT(p) - 1].node;
3972 ly_path_free(set->ctx, p);
Michal Vasko004d3152020-06-11 19:59:22 +02003973
Michal Vasko741bb562021-06-24 11:59:50 +02003974 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, target, LYXP_NODE_ELEM, NULL));
3975 } /* else the target was found before but is disabled so it was removed */
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003976 }
3977
Michal Vasko741bb562021-06-24 11:59:50 +02003978 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003979 }
3980
Michal Vaskod3678892020-05-21 10:06:58 +02003981 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003982 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "deref(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003983 return LY_EVALID;
3984 }
3985
Michal Vaskod3678892020-05-21 10:06:58 +02003986 lyxp_set_free_content(set);
3987 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003988 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
3989 sleaf = (struct lysc_node_leaf *)leaf->schema;
3990 if (sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
3991 if (sleaf->type->basetype == LY_TYPE_LEAFREF) {
3992 /* find leafref target */
Radek Krejci0b013302021-03-29 15:22:32 +02003993 if (lyplg_type_resolve_leafref((struct lysc_type_leafref *)sleaf->type, &leaf->node, &leaf->value, set->tree,
Michal Vasko9e685082021-01-29 14:49:09 +01003994 &node, &errmsg)) {
Michal Vasko004d3152020-06-11 19:59:22 +02003995 LOGERR(set->ctx, LY_EVALID, errmsg);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003996 free(errmsg);
Michal Vasko004d3152020-06-11 19:59:22 +02003997 return LY_EVALID;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003998 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003999 } else {
4000 assert(sleaf->type->basetype == LY_TYPE_INST);
Michal Vasko004d3152020-06-11 19:59:22 +02004001 if (ly_path_eval(leaf->value.target, set->tree, &node)) {
Michal Vaskoba99a3e2020-08-18 15:50:05 +02004002 LOGERR(set->ctx, LY_EVALID, "Invalid instance-identifier \"%s\" value - required instance not found.",
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02004003 lyd_get_value(&leaf->node));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004004 return LY_EVALID;
4005 }
4006 }
Michal Vasko004d3152020-06-11 19:59:22 +02004007
4008 /* insert it */
4009 set_insert_node(set, node, 0, LYXP_NODE_ELEM, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004010 }
4011 }
4012
4013 return LY_SUCCESS;
4014}
4015
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004016static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02004017xpath_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 +02004018{
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01004019 uint32_t i;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004020 LY_ARRAY_COUNT_TYPE u;
4021 struct lyd_node_term *leaf;
4022 struct lysc_node_leaf *sleaf;
4023 struct lyd_meta *meta;
Michal Vasko93923692021-05-07 15:28:02 +02004024 struct lyd_value *val;
4025 const struct lys_module *mod;
4026 const char *id_name;
4027 uint16_t id_len;
4028 struct lysc_ident *id;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004029 LY_ERR rc = LY_SUCCESS;
Radek Krejci857189e2020-09-01 13:26:36 +02004030 ly_bool found;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004031
4032 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02004033 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004034 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", func);
Michal Vasko5676f4e2021-04-06 17:14:45 +02004035 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4036 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4037 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype),
4038 sleaf->name);
4039 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_IDENT)) {
4040 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"identityref\".", func, sleaf->name);
4041 }
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004042 }
4043
4044 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4045 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4046 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype),
Michal Vasko69730152020-10-09 16:30:07 +02004047 sleaf->name);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004048 } else if (!warn_is_string_type(sleaf->type)) {
4049 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", func, sleaf->name);
4050 }
4051 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004052 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004053 return rc;
4054 }
4055
4056 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004057 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 +02004058 return LY_EVALID;
4059 }
4060 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
4061 LY_CHECK_RET(rc);
4062
Michal Vasko93923692021-05-07 15:28:02 +02004063 /* parse the identity */
4064 id_name = args[1]->val.str;
4065 id_len = strlen(id_name);
4066 rc = moveto_resolve_model(&id_name, &id_len, set, set->cur_node ? set->cur_node->schema : NULL, &mod);
4067 LY_CHECK_RET(rc);
4068 if (!mod) {
4069 LOGVAL(set->ctx, LYVE_XPATH, "Identity \"%.*s\" without a prefix.", (int)id_len, id_name);
4070 return LY_EVALID;
4071 }
4072
4073 /* find the identity */
4074 found = 0;
4075 LY_ARRAY_FOR(mod->identities, u) {
4076 if (!ly_strncmp(mod->identities[u].name, id_name, id_len)) {
4077 /* we have match */
4078 found = 1;
4079 break;
4080 }
4081 }
4082 if (!found) {
4083 LOGVAL(set->ctx, LYVE_XPATH, "Identity \"%.*s\" not found in module \"%s\".", (int)id_len, id_name, mod->name);
4084 return LY_EVALID;
4085 }
4086 id = &mod->identities[u];
4087
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004088 set_fill_boolean(set, 0);
4089 found = 0;
4090 for (i = 0; i < args[0]->used; ++i) {
4091 if ((args[0]->val.nodes[i].type != LYXP_NODE_ELEM) && (args[0]->val.nodes[i].type != LYXP_NODE_META)) {
4092 continue;
4093 }
4094
4095 if (args[0]->val.nodes[i].type == LYXP_NODE_ELEM) {
4096 leaf = (struct lyd_node_term *)args[0]->val.nodes[i].node;
4097 sleaf = (struct lysc_node_leaf *)leaf->schema;
4098 val = &leaf->value;
4099 if (!(sleaf->nodetype & LYD_NODE_TERM) || (leaf->value.realtype->basetype != LY_TYPE_IDENT)) {
4100 /* uninteresting */
4101 continue;
4102 }
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004103 } else {
4104 meta = args[0]->val.meta[i].meta;
4105 val = &meta->value;
4106 if (val->realtype->basetype != LY_TYPE_IDENT) {
4107 /* uninteresting */
4108 continue;
4109 }
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004110 }
4111
Michal Vasko93923692021-05-07 15:28:02 +02004112 /* check the identity itself */
4113 if (self_match && (id == val->ident)) {
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004114 set_fill_boolean(set, 1);
4115 found = 1;
4116 }
Michal Vasko93923692021-05-07 15:28:02 +02004117 if (!found && !lyplg_type_identity_isderived(id, val->ident)) {
4118 set_fill_boolean(set, 1);
4119 found = 1;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004120 }
4121
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004122 if (found) {
4123 break;
4124 }
4125 }
4126
4127 return LY_SUCCESS;
4128}
4129
Michal Vasko03ff5a72019-09-11 13:49:33 +02004130/**
4131 * @brief Execute the YANG 1.1 derived-from(node-set, string) function. Returns LYXP_SET_BOOLEAN depending
4132 * on whether the first argument nodes contain a node of an identity derived from the second
4133 * argument identity.
4134 *
4135 * @param[in] args Array of arguments.
4136 * @param[in] arg_count Count of elements in @p args.
4137 * @param[in,out] set Context and result set at the same time.
4138 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004139 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004140 */
4141static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004142xpath_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 +02004143{
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004144 return xpath_derived_(args, set, options, 0, __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004145}
4146
4147/**
4148 * @brief Execute the YANG 1.1 derived-from-or-self(node-set, string) function. Returns LYXP_SET_BOOLEAN depending
4149 * on whether the first argument nodes contain a node of an identity that either is or is derived from
4150 * the second argument identity.
4151 *
4152 * @param[in] args Array of arguments.
4153 * @param[in] arg_count Count of elements in @p args.
4154 * @param[in,out] set Context and result set at the same time.
4155 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004156 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004157 */
4158static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004159xpath_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 +02004160{
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004161 return xpath_derived_(args, set, options, 1, __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004162}
4163
4164/**
4165 * @brief Execute the YANG 1.1 enum-value(node-set) function. Returns LYXP_SET_NUMBER
4166 * with the integer value of the first node's enum value, otherwise NaN.
4167 *
4168 * @param[in] args Array of arguments.
4169 * @param[in] arg_count Count of elements in @p args.
4170 * @param[in,out] set Context and result set at the same time.
4171 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004172 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004173 */
4174static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004175xpath_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 +02004176{
4177 struct lyd_node_term *leaf;
4178 struct lysc_node_leaf *sleaf;
4179 LY_ERR rc = LY_SUCCESS;
4180
4181 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02004182 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004183 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02004184 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4185 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4186 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
4187 sleaf->name);
4188 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_ENUM)) {
4189 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"enumeration\".", __func__, sleaf->name);
4190 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004191 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004192 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004193 return rc;
4194 }
4195
Michal Vaskod3678892020-05-21 10:06:58 +02004196 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004197 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "enum-value(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004198 return LY_EVALID;
4199 }
4200
4201 set_fill_number(set, NAN);
Michal Vaskod3678892020-05-21 10:06:58 +02004202 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004203 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
4204 sleaf = (struct lysc_node_leaf *)leaf->schema;
4205 if ((sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (sleaf->type->basetype == LY_TYPE_ENUM)) {
4206 set_fill_number(set, leaf->value.enum_item->value);
4207 }
4208 }
4209
4210 return LY_SUCCESS;
4211}
4212
4213/**
4214 * @brief Execute the XPath false() function. Returns LYXP_SET_BOOLEAN
4215 * with false value.
4216 *
4217 * @param[in] args Array of arguments.
4218 * @param[in] arg_count Count of elements in @p args.
4219 * @param[in,out] set Context and result set at the same time.
4220 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004221 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004222 */
4223static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004224xpath_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 +02004225{
4226 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004227 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004228 return LY_SUCCESS;
4229 }
4230
4231 set_fill_boolean(set, 0);
4232 return LY_SUCCESS;
4233}
4234
4235/**
4236 * @brief Execute the XPath floor(number) function. Returns LYXP_SET_NUMBER
4237 * with the first argument floored (truncated).
4238 *
4239 * @param[in] args Array of arguments.
4240 * @param[in] arg_count Count of elements in @p args.
4241 * @param[in,out] set Context and result set at the same time.
4242 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004243 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004244 */
4245static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004246xpath_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 +02004247{
4248 LY_ERR rc;
4249
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004250 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004251 LY_CHECK_RET(rc);
4252 if (isfinite(args[0]->val.num)) {
4253 set_fill_number(set, (long long)args[0]->val.num);
4254 }
4255
4256 return LY_SUCCESS;
4257}
4258
4259/**
4260 * @brief Execute the XPath lang(string) function. Returns LYXP_SET_BOOLEAN
4261 * whether the language of the text matches the one from the argument.
4262 *
4263 * @param[in] args Array of arguments.
4264 * @param[in] arg_count Count of elements in @p args.
4265 * @param[in,out] set Context and result set at the same time.
4266 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004267 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004268 */
4269static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004270xpath_lang(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004271{
4272 const struct lyd_node *node;
4273 struct lysc_node_leaf *sleaf;
Michal Vasko9f96a052020-03-10 09:41:45 +01004274 struct lyd_meta *meta = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004275 const char *val;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004276 LY_ERR rc = LY_SUCCESS;
4277
4278 if (options & LYXP_SCNODE_ALL) {
4279 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4280 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004281 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
4282 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004283 } else if (!warn_is_string_type(sleaf->type)) {
4284 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004285 }
4286 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004287 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004288 return rc;
4289 }
4290
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004291 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004292 LY_CHECK_RET(rc);
4293
Michal Vasko03ff5a72019-09-11 13:49:33 +02004294 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004295 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "lang(string)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004296 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004297 } else if (!set->used) {
4298 set_fill_boolean(set, 0);
4299 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004300 }
4301
4302 switch (set->val.nodes[0].type) {
4303 case LYXP_NODE_ELEM:
4304 case LYXP_NODE_TEXT:
4305 node = set->val.nodes[0].node;
4306 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004307 case LYXP_NODE_META:
4308 node = set->val.meta[0].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004309 break;
4310 default:
4311 /* nothing to do with roots */
4312 set_fill_boolean(set, 0);
4313 return LY_SUCCESS;
4314 }
4315
Michal Vasko9f96a052020-03-10 09:41:45 +01004316 /* find lang metadata */
Michal Vasko9e685082021-01-29 14:49:09 +01004317 for ( ; node; node = lyd_parent(node)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01004318 for (meta = node->meta; meta; meta = meta->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004319 /* annotations */
Michal Vasko9f96a052020-03-10 09:41:45 +01004320 if (meta->name && !strcmp(meta->name, "lang") && !strcmp(meta->annotation->module->name, "xml")) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004321 break;
4322 }
4323 }
4324
Michal Vasko9f96a052020-03-10 09:41:45 +01004325 if (meta) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004326 break;
4327 }
4328 }
4329
4330 /* compare languages */
Michal Vasko9f96a052020-03-10 09:41:45 +01004331 if (!meta) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004332 set_fill_boolean(set, 0);
4333 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004334 uint64_t i;
4335
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02004336 val = lyd_get_meta_value(meta);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004337 for (i = 0; args[0]->val.str[i]; ++i) {
4338 if (tolower(args[0]->val.str[i]) != tolower(val[i])) {
4339 set_fill_boolean(set, 0);
4340 break;
4341 }
4342 }
4343 if (!args[0]->val.str[i]) {
4344 if (!val[i] || (val[i] == '-')) {
4345 set_fill_boolean(set, 1);
4346 } else {
4347 set_fill_boolean(set, 0);
4348 }
4349 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004350 }
4351
4352 return LY_SUCCESS;
4353}
4354
4355/**
4356 * @brief Execute the XPath last() function. Returns LYXP_SET_NUMBER
4357 * with the context size.
4358 *
4359 * @param[in] args Array of arguments.
4360 * @param[in] arg_count Count of elements in @p args.
4361 * @param[in,out] set Context and result set at the same time.
4362 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004363 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004364 */
4365static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004366xpath_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 +02004367{
4368 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004369 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004370 return LY_SUCCESS;
4371 }
4372
Michal Vasko03ff5a72019-09-11 13:49:33 +02004373 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004374 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "last()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004375 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004376 } else if (!set->used) {
4377 set_fill_number(set, 0);
4378 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004379 }
4380
4381 set_fill_number(set, set->ctx_size);
4382 return LY_SUCCESS;
4383}
4384
4385/**
4386 * @brief Execute the XPath local-name(node-set?) function. Returns LYXP_SET_STRING
4387 * with the node name without namespace from the argument or the context.
4388 *
4389 * @param[in] args Array of arguments.
4390 * @param[in] arg_count Count of elements in @p args.
4391 * @param[in,out] set Context and result set at the same time.
4392 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004393 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004394 */
4395static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004396xpath_local_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004397{
4398 struct lyxp_set_node *item;
Michal Vasko69730152020-10-09 16:30:07 +02004399
Michal Vasko03ff5a72019-09-11 13:49:33 +02004400 /* suppress unused variable warning */
4401 (void)options;
4402
4403 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004404 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004405 return LY_SUCCESS;
4406 }
4407
4408 if (arg_count) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004409 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004410 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004411 "local-name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004412 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004413 } else if (!args[0]->used) {
4414 set_fill_string(set, "", 0);
4415 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004416 }
4417
4418 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004419 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004420
4421 item = &args[0]->val.nodes[0];
4422 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004423 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004424 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "local-name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004425 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004426 } else if (!set->used) {
4427 set_fill_string(set, "", 0);
4428 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004429 }
4430
4431 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004432 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004433
4434 item = &set->val.nodes[0];
4435 }
4436
4437 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004438 case LYXP_NODE_NONE:
4439 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004440 case LYXP_NODE_ROOT:
4441 case LYXP_NODE_ROOT_CONFIG:
4442 case LYXP_NODE_TEXT:
4443 set_fill_string(set, "", 0);
4444 break;
4445 case LYXP_NODE_ELEM:
4446 set_fill_string(set, item->node->schema->name, strlen(item->node->schema->name));
4447 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004448 case LYXP_NODE_META:
4449 set_fill_string(set, ((struct lyd_meta *)item->node)->name, strlen(((struct lyd_meta *)item->node)->name));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004450 break;
4451 }
4452
4453 return LY_SUCCESS;
4454}
4455
4456/**
4457 * @brief Execute the XPath name(node-set?) function. Returns LYXP_SET_STRING
4458 * with the node name fully qualified (with namespace) from the argument or the context.
4459 *
4460 * @param[in] args Array of arguments.
4461 * @param[in] arg_count Count of elements in @p args.
4462 * @param[in,out] set Context and result set at the same time.
4463 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004464 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004465 */
4466static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004467xpath_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004468{
4469 struct lyxp_set_node *item;
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004470 struct lys_module *mod = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004471 char *str;
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004472 const char *name = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004473
4474 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004475 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004476 return LY_SUCCESS;
4477 }
4478
4479 if (arg_count) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004480 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004481 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004482 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004483 } else if (!args[0]->used) {
4484 set_fill_string(set, "", 0);
4485 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004486 }
4487
4488 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004489 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004490
4491 item = &args[0]->val.nodes[0];
4492 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004493 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004494 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004495 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004496 } else if (!set->used) {
4497 set_fill_string(set, "", 0);
4498 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004499 }
4500
4501 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004502 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004503
4504 item = &set->val.nodes[0];
4505 }
4506
4507 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004508 case LYXP_NODE_NONE:
4509 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004510 case LYXP_NODE_ROOT:
4511 case LYXP_NODE_ROOT_CONFIG:
4512 case LYXP_NODE_TEXT:
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004513 /* keep NULL */
Michal Vasko03ff5a72019-09-11 13:49:33 +02004514 break;
4515 case LYXP_NODE_ELEM:
4516 mod = item->node->schema->module;
4517 name = item->node->schema->name;
4518 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004519 case LYXP_NODE_META:
4520 mod = ((struct lyd_meta *)item->node)->annotation->module;
4521 name = ((struct lyd_meta *)item->node)->name;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004522 break;
4523 }
4524
4525 if (mod && name) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004526 int rc = asprintf(&str, "%s:%s", ly_get_prefix(mod, set->format, set->prefix_data), name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004527 LY_CHECK_ERR_RET(rc == -1, LOGMEM(set->ctx), LY_EMEM);
4528 set_fill_string(set, str, strlen(str));
4529 free(str);
4530 } else {
4531 set_fill_string(set, "", 0);
4532 }
4533
4534 return LY_SUCCESS;
4535}
4536
4537/**
4538 * @brief Execute the XPath namespace-uri(node-set?) function. Returns LYXP_SET_STRING
4539 * with the namespace of the node from the argument or the context.
4540 *
4541 * @param[in] args Array of arguments.
4542 * @param[in] arg_count Count of elements in @p args.
4543 * @param[in,out] set Context and result set at the same time.
4544 * @param[in] options XPath options.
4545 * @return LY_ERR (LY_EINVAL for wrong arguments on schema)
4546 */
4547static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004548xpath_namespace_uri(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004549{
4550 struct lyxp_set_node *item;
4551 struct lys_module *mod;
Michal Vasko69730152020-10-09 16:30:07 +02004552
Michal Vasko03ff5a72019-09-11 13:49:33 +02004553 /* suppress unused variable warning */
4554 (void)options;
4555
4556 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004557 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004558 return LY_SUCCESS;
4559 }
4560
4561 if (arg_count) {
Michal Vaskod3678892020-05-21 10:06:58 +02004562 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004563 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
Michal Vasko69730152020-10-09 16:30:07 +02004564 "namespace-uri(node-set?)");
Michal Vaskod3678892020-05-21 10:06:58 +02004565 return LY_EVALID;
4566 } else if (!args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004567 set_fill_string(set, "", 0);
4568 return LY_SUCCESS;
4569 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004570
4571 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004572 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004573
4574 item = &args[0]->val.nodes[0];
4575 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004576 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004577 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "namespace-uri(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004578 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004579 } else if (!set->used) {
4580 set_fill_string(set, "", 0);
4581 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004582 }
4583
4584 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004585 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004586
4587 item = &set->val.nodes[0];
4588 }
4589
4590 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004591 case LYXP_NODE_NONE:
4592 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004593 case LYXP_NODE_ROOT:
4594 case LYXP_NODE_ROOT_CONFIG:
4595 case LYXP_NODE_TEXT:
4596 set_fill_string(set, "", 0);
4597 break;
4598 case LYXP_NODE_ELEM:
Michal Vasko9f96a052020-03-10 09:41:45 +01004599 case LYXP_NODE_META:
Michal Vasko03ff5a72019-09-11 13:49:33 +02004600 if (item->type == LYXP_NODE_ELEM) {
4601 mod = item->node->schema->module;
Michal Vasko9f96a052020-03-10 09:41:45 +01004602 } else { /* LYXP_NODE_META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02004603 /* annotations */
Michal Vasko9f96a052020-03-10 09:41:45 +01004604 mod = ((struct lyd_meta *)item->node)->annotation->module;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004605 }
4606
4607 set_fill_string(set, mod->ns, strlen(mod->ns));
4608 break;
4609 }
4610
4611 return LY_SUCCESS;
4612}
4613
4614/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02004615 * @brief Execute the XPath normalize-space(string?) function. Returns LYXP_SET_STRING
4616 * with normalized value (no leading, trailing, double white spaces) of the node
4617 * from the argument or the context.
4618 *
4619 * @param[in] args Array of arguments.
4620 * @param[in] arg_count Count of elements in @p args.
4621 * @param[in,out] set Context and result set at the same time.
4622 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004623 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004624 */
4625static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004626xpath_normalize_space(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004627{
4628 uint16_t i, new_used;
4629 char *new;
Radek Krejci857189e2020-09-01 13:26:36 +02004630 ly_bool have_spaces = 0, space_before = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004631 struct lysc_node_leaf *sleaf;
4632 LY_ERR rc = LY_SUCCESS;
4633
4634 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004635 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) &&
4636 (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004637 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004638 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
4639 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004640 } else if (!warn_is_string_type(sleaf->type)) {
4641 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004642 }
4643 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004644 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004645 return rc;
4646 }
4647
4648 if (arg_count) {
4649 set_fill_set(set, args[0]);
4650 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004651 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004652 LY_CHECK_RET(rc);
4653
4654 /* is there any normalization necessary? */
4655 for (i = 0; set->val.str[i]; ++i) {
4656 if (is_xmlws(set->val.str[i])) {
4657 if ((i == 0) || space_before || (!set->val.str[i + 1])) {
4658 have_spaces = 1;
4659 break;
4660 }
4661 space_before = 1;
4662 } else {
4663 space_before = 0;
4664 }
4665 }
4666
4667 /* yep, there is */
4668 if (have_spaces) {
4669 /* it's enough, at least one character will go, makes space for ending '\0' */
4670 new = malloc(strlen(set->val.str) * sizeof(char));
4671 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4672 new_used = 0;
4673
4674 space_before = 0;
4675 for (i = 0; set->val.str[i]; ++i) {
4676 if (is_xmlws(set->val.str[i])) {
4677 if ((i == 0) || space_before) {
4678 space_before = 1;
4679 continue;
4680 } else {
4681 space_before = 1;
4682 }
4683 } else {
4684 space_before = 0;
4685 }
4686
4687 new[new_used] = (space_before ? ' ' : set->val.str[i]);
4688 ++new_used;
4689 }
4690
4691 /* at worst there is one trailing space now */
4692 if (new_used && is_xmlws(new[new_used - 1])) {
4693 --new_used;
4694 }
4695
4696 new = ly_realloc(new, (new_used + 1) * sizeof(char));
4697 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4698 new[new_used] = '\0';
4699
4700 free(set->val.str);
4701 set->val.str = new;
4702 }
4703
4704 return LY_SUCCESS;
4705}
4706
4707/**
4708 * @brief Execute the XPath not(boolean) function. Returns LYXP_SET_BOOLEAN
4709 * with the argument converted to boolean and logically inverted.
4710 *
4711 * @param[in] args Array of arguments.
4712 * @param[in] arg_count Count of elements in @p args.
4713 * @param[in,out] set Context and result set at the same time.
4714 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004715 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004716 */
4717static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004718xpath_not(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004719{
4720 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004721 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004722 return LY_SUCCESS;
4723 }
4724
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004725 lyxp_set_cast(args[0], LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +02004726 if (args[0]->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004727 set_fill_boolean(set, 0);
4728 } else {
4729 set_fill_boolean(set, 1);
4730 }
4731
4732 return LY_SUCCESS;
4733}
4734
4735/**
4736 * @brief Execute the XPath number(object?) function. Returns LYXP_SET_NUMBER
4737 * with the number representation of either the argument or the context.
4738 *
4739 * @param[in] args Array of arguments.
4740 * @param[in] arg_count Count of elements in @p args.
4741 * @param[in,out] set Context and result set at the same time.
4742 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004743 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004744 */
4745static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004746xpath_number(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004747{
4748 LY_ERR rc;
4749
4750 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004751 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004752 return LY_SUCCESS;
4753 }
4754
4755 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004756 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004757 LY_CHECK_RET(rc);
4758 set_fill_set(set, args[0]);
4759 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004760 rc = lyxp_set_cast(set, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004761 LY_CHECK_RET(rc);
4762 }
4763
4764 return LY_SUCCESS;
4765}
4766
4767/**
4768 * @brief Execute the XPath position() function. Returns LYXP_SET_NUMBER
4769 * with the context position.
4770 *
4771 * @param[in] args Array of arguments.
4772 * @param[in] arg_count Count of elements in @p args.
4773 * @param[in,out] set Context and result set at the same time.
4774 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004775 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004776 */
4777static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004778xpath_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 +02004779{
4780 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004781 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004782 return LY_SUCCESS;
4783 }
4784
Michal Vasko03ff5a72019-09-11 13:49:33 +02004785 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004786 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "position()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004787 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004788 } else if (!set->used) {
4789 set_fill_number(set, 0);
4790 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004791 }
4792
4793 set_fill_number(set, set->ctx_pos);
4794
4795 /* UNUSED in 'Release' build type */
4796 (void)options;
4797 return LY_SUCCESS;
4798}
4799
4800/**
4801 * @brief Execute the YANG 1.1 re-match(string, string) function. Returns LYXP_SET_BOOLEAN
4802 * depending on whether the second argument regex matches the first argument string. For details refer to
4803 * YANG 1.1 RFC section 10.2.1.
4804 *
4805 * @param[in] args Array of arguments.
4806 * @param[in] arg_count Count of elements in @p args.
4807 * @param[in,out] set Context and result set at the same time.
4808 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004809 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004810 */
4811static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004812xpath_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 +02004813{
4814 struct lysc_pattern **patterns = NULL, **pattern;
4815 struct lysc_node_leaf *sleaf;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004816 LY_ERR rc = LY_SUCCESS;
4817 struct ly_err_item *err;
4818
4819 if (options & LYXP_SCNODE_ALL) {
4820 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4821 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4822 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 +02004823 } else if (!warn_is_string_type(sleaf->type)) {
4824 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004825 }
4826 }
4827
4828 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4829 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4830 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 +02004831 } else if (!warn_is_string_type(sleaf->type)) {
4832 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004833 }
4834 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004835 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004836 return rc;
4837 }
4838
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004839 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004840 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004841 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004842 LY_CHECK_RET(rc);
4843
4844 LY_ARRAY_NEW_RET(set->ctx, patterns, pattern, LY_EMEM);
Radek Iša45802b52021-02-09 09:21:58 +01004845 *pattern = calloc(1, sizeof **pattern);
Radek Krejciddace2c2021-01-08 11:30:56 +01004846 LOG_LOCSET(NULL, set->cur_node, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01004847 rc = lys_compile_type_pattern_check(set->ctx, args[1]->val.str, &(*pattern)->code);
Michal Vasko4a7d4d62021-12-13 17:05:06 +01004848 if (set->cur_node) {
4849 LOG_LOCBACK(0, 1, 0, 0);
4850 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004851 if (rc != LY_SUCCESS) {
4852 LY_ARRAY_FREE(patterns);
4853 return rc;
4854 }
4855
Radek Krejci0b013302021-03-29 15:22:32 +02004856 rc = lyplg_type_validate_patterns(patterns, args[0]->val.str, strlen(args[0]->val.str), &err);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004857 pcre2_code_free((*pattern)->code);
4858 free(*pattern);
4859 LY_ARRAY_FREE(patterns);
4860 if (rc && (rc != LY_EVALID)) {
Michal Vasko177d0ed2020-11-23 16:43:03 +01004861 ly_err_print(set->ctx, err);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004862 ly_err_free(err);
4863 return rc;
4864 }
4865
4866 if (rc == LY_EVALID) {
4867 ly_err_free(err);
4868 set_fill_boolean(set, 0);
4869 } else {
4870 set_fill_boolean(set, 1);
4871 }
4872
4873 return LY_SUCCESS;
4874}
4875
4876/**
4877 * @brief Execute the XPath round(number) function. Returns LYXP_SET_NUMBER
4878 * with the rounded first argument. For details refer to
4879 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-round.
4880 *
4881 * @param[in] args Array of arguments.
4882 * @param[in] arg_count Count of elements in @p args.
4883 * @param[in,out] set Context and result set at the same time.
4884 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004885 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004886 */
4887static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004888xpath_round(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004889{
4890 struct lysc_node_leaf *sleaf;
4891 LY_ERR rc = LY_SUCCESS;
4892
4893 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02004894 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004895 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02004896 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4897 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4898 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
4899 sleaf->name);
4900 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
4901 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
4902 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004903 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004904 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004905 return rc;
4906 }
4907
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004908 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004909 LY_CHECK_RET(rc);
4910
4911 /* cover only the cases where floor can't be used */
4912 if ((args[0]->val.num == -0.0f) || ((args[0]->val.num < 0) && (args[0]->val.num >= -0.5))) {
4913 set_fill_number(set, -0.0f);
4914 } else {
4915 args[0]->val.num += 0.5;
4916 rc = xpath_floor(args, 1, args[0], options);
4917 LY_CHECK_RET(rc);
4918 set_fill_number(set, args[0]->val.num);
4919 }
4920
4921 return LY_SUCCESS;
4922}
4923
4924/**
4925 * @brief Execute the XPath starts-with(string, string) function.
4926 * Returns LYXP_SET_BOOLEAN whether the second argument is
4927 * the prefix of the first or not.
4928 *
4929 * @param[in] args Array of arguments.
4930 * @param[in] arg_count Count of elements in @p args.
4931 * @param[in,out] set Context and result set at the same time.
4932 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004933 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004934 */
4935static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004936xpath_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 +02004937{
4938 struct lysc_node_leaf *sleaf;
4939 LY_ERR rc = LY_SUCCESS;
4940
4941 if (options & LYXP_SCNODE_ALL) {
4942 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4943 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4944 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 +02004945 } else if (!warn_is_string_type(sleaf->type)) {
4946 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004947 }
4948 }
4949
4950 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4951 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4952 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 +02004953 } else if (!warn_is_string_type(sleaf->type)) {
4954 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004955 }
4956 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004957 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004958 return rc;
4959 }
4960
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004961 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004962 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004963 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004964 LY_CHECK_RET(rc);
4965
4966 if (strncmp(args[0]->val.str, args[1]->val.str, strlen(args[1]->val.str))) {
4967 set_fill_boolean(set, 0);
4968 } else {
4969 set_fill_boolean(set, 1);
4970 }
4971
4972 return LY_SUCCESS;
4973}
4974
4975/**
4976 * @brief Execute the XPath string(object?) function. Returns LYXP_SET_STRING
4977 * with the string representation of either the argument or the context.
4978 *
4979 * @param[in] args Array of arguments.
4980 * @param[in] arg_count Count of elements in @p args.
4981 * @param[in,out] set Context and result set at the same time.
4982 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004983 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004984 */
4985static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004986xpath_string(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004987{
4988 LY_ERR rc;
4989
4990 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004991 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004992 return LY_SUCCESS;
4993 }
4994
4995 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004996 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004997 LY_CHECK_RET(rc);
4998 set_fill_set(set, args[0]);
4999 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005000 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005001 LY_CHECK_RET(rc);
5002 }
5003
5004 return LY_SUCCESS;
5005}
5006
5007/**
5008 * @brief Execute the XPath string-length(string?) function. Returns LYXP_SET_NUMBER
5009 * with the length of the string in either the argument or the context.
5010 *
5011 * @param[in] args Array of arguments.
5012 * @param[in] arg_count Count of elements in @p args.
5013 * @param[in,out] set Context and result set at the same time.
5014 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005015 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005016 */
5017static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005018xpath_string_length(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005019{
5020 struct lysc_node_leaf *sleaf;
5021 LY_ERR rc = LY_SUCCESS;
5022
5023 if (options & LYXP_SCNODE_ALL) {
5024 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5025 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5026 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 +02005027 } else if (!warn_is_string_type(sleaf->type)) {
5028 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005029 }
5030 }
5031 if (!arg_count && (set->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set))) {
5032 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5033 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 +02005034 } else if (!warn_is_string_type(sleaf->type)) {
5035 LOGWRN(set->ctx, "Argument #0 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005036 }
5037 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005038 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005039 return rc;
5040 }
5041
5042 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005043 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005044 LY_CHECK_RET(rc);
5045 set_fill_number(set, strlen(args[0]->val.str));
5046 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005047 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005048 LY_CHECK_RET(rc);
5049 set_fill_number(set, strlen(set->val.str));
5050 }
5051
5052 return LY_SUCCESS;
5053}
5054
5055/**
5056 * @brief Execute the XPath substring(string, number, number?) function.
5057 * Returns LYXP_SET_STRING substring of the first argument starting
5058 * on the second argument index ending on the third argument index,
5059 * indexed from 1. For exact definition refer to
5060 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-substring.
5061 *
5062 * @param[in] args Array of arguments.
5063 * @param[in] arg_count Count of elements in @p args.
5064 * @param[in,out] set Context and result set at the same time.
5065 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005066 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005067 */
5068static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005069xpath_substring(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005070{
Radek Krejci1deb5be2020-08-26 16:43:36 +02005071 int32_t start, len;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005072 uint16_t str_start, str_len, pos;
5073 struct lysc_node_leaf *sleaf;
5074 LY_ERR rc = LY_SUCCESS;
5075
5076 if (options & LYXP_SCNODE_ALL) {
5077 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5078 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5079 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 +02005080 } else if (!warn_is_string_type(sleaf->type)) {
5081 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005082 }
5083 }
5084
5085 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5086 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5087 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 +02005088 } else if (!warn_is_numeric_type(sleaf->type)) {
5089 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005090 }
5091 }
5092
Michal Vasko69730152020-10-09 16:30:07 +02005093 if ((arg_count == 3) && (args[2]->type == LYXP_SET_SCNODE_SET) &&
5094 (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005095 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5096 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 +02005097 } else if (!warn_is_numeric_type(sleaf->type)) {
5098 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005099 }
5100 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005101 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005102 return rc;
5103 }
5104
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005105 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005106 LY_CHECK_RET(rc);
5107
5108 /* start */
5109 if (xpath_round(&args[1], 1, args[1], options)) {
5110 return -1;
5111 }
5112 if (isfinite(args[1]->val.num)) {
5113 start = args[1]->val.num - 1;
5114 } else if (isinf(args[1]->val.num) && signbit(args[1]->val.num)) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02005115 start = INT32_MIN;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005116 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02005117 start = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005118 }
5119
5120 /* len */
5121 if (arg_count == 3) {
5122 rc = xpath_round(&args[2], 1, args[2], options);
5123 LY_CHECK_RET(rc);
Radek Krejci1deb5be2020-08-26 16:43:36 +02005124 if (isnan(args[2]->val.num) || signbit(args[2]->val.num)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005125 len = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005126 } else if (isfinite(args[2]->val.num)) {
5127 len = args[2]->val.num;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005128 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02005129 len = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005130 }
5131 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02005132 len = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005133 }
5134
5135 /* find matching character positions */
5136 str_start = 0;
5137 str_len = 0;
5138 for (pos = 0; args[0]->val.str[pos]; ++pos) {
5139 if (pos < start) {
5140 ++str_start;
5141 } else if (pos < start + len) {
5142 ++str_len;
5143 } else {
5144 break;
5145 }
5146 }
5147
5148 set_fill_string(set, args[0]->val.str + str_start, str_len);
5149 return LY_SUCCESS;
5150}
5151
5152/**
5153 * @brief Execute the XPath substring-after(string, string) function.
5154 * Returns LYXP_SET_STRING with the string succeeding the occurance
5155 * of the second argument in the first or an empty string.
5156 *
5157 * @param[in] args Array of arguments.
5158 * @param[in] arg_count Count of elements in @p args.
5159 * @param[in,out] set Context and result set at the same time.
5160 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005161 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005162 */
5163static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005164xpath_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 +02005165{
5166 char *ptr;
5167 struct lysc_node_leaf *sleaf;
5168 LY_ERR rc = LY_SUCCESS;
5169
5170 if (options & LYXP_SCNODE_ALL) {
5171 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5172 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5173 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 +02005174 } else if (!warn_is_string_type(sleaf->type)) {
5175 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005176 }
5177 }
5178
5179 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5180 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5181 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 +02005182 } else if (!warn_is_string_type(sleaf->type)) {
5183 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005184 }
5185 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005186 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005187 return rc;
5188 }
5189
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005190 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005191 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005192 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005193 LY_CHECK_RET(rc);
5194
5195 ptr = strstr(args[0]->val.str, args[1]->val.str);
5196 if (ptr) {
5197 set_fill_string(set, ptr + strlen(args[1]->val.str), strlen(ptr + strlen(args[1]->val.str)));
5198 } else {
5199 set_fill_string(set, "", 0);
5200 }
5201
5202 return LY_SUCCESS;
5203}
5204
5205/**
5206 * @brief Execute the XPath substring-before(string, string) function.
5207 * Returns LYXP_SET_STRING with the string preceding the occurance
5208 * of the second argument in the first or an empty string.
5209 *
5210 * @param[in] args Array of arguments.
5211 * @param[in] arg_count Count of elements in @p args.
5212 * @param[in,out] set Context and result set at the same time.
5213 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005214 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005215 */
5216static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005217xpath_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 +02005218{
5219 char *ptr;
5220 struct lysc_node_leaf *sleaf;
5221 LY_ERR rc = LY_SUCCESS;
5222
5223 if (options & LYXP_SCNODE_ALL) {
5224 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5225 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5226 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 +02005227 } else if (!warn_is_string_type(sleaf->type)) {
5228 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005229 }
5230 }
5231
5232 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5233 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5234 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 +02005235 } else if (!warn_is_string_type(sleaf->type)) {
5236 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005237 }
5238 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005239 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005240 return rc;
5241 }
5242
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005243 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005244 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005245 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005246 LY_CHECK_RET(rc);
5247
5248 ptr = strstr(args[0]->val.str, args[1]->val.str);
5249 if (ptr) {
5250 set_fill_string(set, args[0]->val.str, ptr - args[0]->val.str);
5251 } else {
5252 set_fill_string(set, "", 0);
5253 }
5254
5255 return LY_SUCCESS;
5256}
5257
5258/**
5259 * @brief Execute the XPath sum(node-set) function. Returns LYXP_SET_NUMBER
5260 * with the sum of all the nodes in the context.
5261 *
5262 * @param[in] args Array of arguments.
5263 * @param[in] arg_count Count of elements in @p args.
5264 * @param[in,out] set Context and result set at the same time.
5265 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005266 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005267 */
5268static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005269xpath_sum(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005270{
5271 long double num;
5272 char *str;
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01005273 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005274 struct lyxp_set set_item;
5275 struct lysc_node_leaf *sleaf;
5276 LY_ERR rc = LY_SUCCESS;
5277
5278 if (options & LYXP_SCNODE_ALL) {
5279 if (args[0]->type == LYXP_SET_SCNODE_SET) {
5280 for (i = 0; i < args[0]->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005281 if (args[0]->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005282 sleaf = (struct lysc_node_leaf *)args[0]->val.scnodes[i].scnode;
5283 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5284 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__,
Michal Vasko69730152020-10-09 16:30:07 +02005285 lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005286 } else if (!warn_is_numeric_type(sleaf->type)) {
5287 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005288 }
5289 }
5290 }
5291 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005292 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005293 return rc;
5294 }
5295
5296 set_fill_number(set, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005297
5298 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005299 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "sum(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02005300 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02005301 } else if (!args[0]->used) {
5302 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005303 }
5304
Michal Vasko5c4e5892019-11-14 12:31:38 +01005305 set_init(&set_item, set);
5306
Michal Vasko03ff5a72019-09-11 13:49:33 +02005307 set_item.type = LYXP_SET_NODE_SET;
Michal Vasko41decbf2021-11-02 11:50:21 +01005308 set_item.val.nodes = calloc(1, sizeof *set_item.val.nodes);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005309 LY_CHECK_ERR_RET(!set_item.val.nodes, LOGMEM(set->ctx), LY_EMEM);
5310
5311 set_item.used = 1;
5312 set_item.size = 1;
5313
5314 for (i = 0; i < args[0]->used; ++i) {
5315 set_item.val.nodes[0] = args[0]->val.nodes[i];
5316
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005317 rc = cast_node_set_to_string(&set_item, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005318 LY_CHECK_RET(rc);
5319 num = cast_string_to_number(str);
5320 free(str);
5321 set->val.num += num;
5322 }
5323
5324 free(set_item.val.nodes);
5325
5326 return LY_SUCCESS;
5327}
5328
5329/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02005330 * @brief Execute the XPath translate(string, string, string) function.
5331 * Returns LYXP_SET_STRING with the first argument with the characters
5332 * from the second argument replaced by those on the corresponding
5333 * positions in the third argument.
5334 *
5335 * @param[in] args Array of arguments.
5336 * @param[in] arg_count Count of elements in @p args.
5337 * @param[in,out] set Context and result set at the same time.
5338 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005339 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005340 */
5341static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005342xpath_translate(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005343{
5344 uint16_t i, j, new_used;
5345 char *new;
Radek Krejci857189e2020-09-01 13:26:36 +02005346 ly_bool have_removed;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005347 struct lysc_node_leaf *sleaf;
5348 LY_ERR rc = LY_SUCCESS;
5349
5350 if (options & LYXP_SCNODE_ALL) {
5351 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5352 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5353 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 +02005354 } else if (!warn_is_string_type(sleaf->type)) {
5355 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005356 }
5357 }
5358
5359 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5360 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5361 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 +02005362 } else if (!warn_is_string_type(sleaf->type)) {
5363 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005364 }
5365 }
5366
5367 if ((args[2]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
5368 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5369 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 +02005370 } else if (!warn_is_string_type(sleaf->type)) {
5371 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005372 }
5373 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005374 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005375 return rc;
5376 }
5377
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005378 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005379 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005380 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005381 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005382 rc = lyxp_set_cast(args[2], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005383 LY_CHECK_RET(rc);
5384
5385 new = malloc((strlen(args[0]->val.str) + 1) * sizeof(char));
5386 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5387 new_used = 0;
5388
5389 have_removed = 0;
5390 for (i = 0; args[0]->val.str[i]; ++i) {
Radek Krejci857189e2020-09-01 13:26:36 +02005391 ly_bool found = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005392
5393 for (j = 0; args[1]->val.str[j]; ++j) {
5394 if (args[0]->val.str[i] == args[1]->val.str[j]) {
5395 /* removing this char */
5396 if (j >= strlen(args[2]->val.str)) {
5397 have_removed = 1;
5398 found = 1;
5399 break;
5400 }
5401 /* replacing this char */
5402 new[new_used] = args[2]->val.str[j];
5403 ++new_used;
5404 found = 1;
5405 break;
5406 }
5407 }
5408
5409 /* copying this char */
5410 if (!found) {
5411 new[new_used] = args[0]->val.str[i];
5412 ++new_used;
5413 }
5414 }
5415
5416 if (have_removed) {
5417 new = ly_realloc(new, (new_used + 1) * sizeof(char));
5418 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5419 }
5420 new[new_used] = '\0';
5421
Michal Vaskod3678892020-05-21 10:06:58 +02005422 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005423 set->type = LYXP_SET_STRING;
5424 set->val.str = new;
5425
5426 return LY_SUCCESS;
5427}
5428
5429/**
5430 * @brief Execute the XPath true() function. Returns LYXP_SET_BOOLEAN
5431 * with true value.
5432 *
5433 * @param[in] args Array of arguments.
5434 * @param[in] arg_count Count of elements in @p args.
5435 * @param[in,out] set Context and result set at the same time.
5436 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005437 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005438 */
5439static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005440xpath_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 +02005441{
5442 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02005443 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005444 return LY_SUCCESS;
5445 }
5446
5447 set_fill_boolean(set, 1);
5448 return LY_SUCCESS;
5449}
5450
Michal Vasko03ff5a72019-09-11 13:49:33 +02005451/**
Michal Vasko49fec8e2022-05-24 10:28:33 +02005452 * @brief Execute the XPath node() processing instruction (node type). Returns LYXP_SET_NODE_SET
5453 * with only nodes from the context.
5454 *
5455 * @param[in,out] set Context and result set at the same time.
5456 * @param[in] axis Axis to search on.
5457 * @param[in] options XPath options.
5458 * @return LY_ERR
5459 */
5460static LY_ERR
5461xpath_pi_node(struct lyxp_set *set, enum lyxp_axis axis, uint32_t options)
5462{
5463 if (options & LYXP_SCNODE_ALL) {
5464 return moveto_scnode(set, NULL, NULL, axis, options);
5465 }
5466
5467 if (set->type != LYXP_SET_NODE_SET) {
5468 lyxp_set_free_content(set);
5469 return LY_SUCCESS;
5470 }
5471
5472 /* just like moving to a node with no restrictions */
5473 return moveto_node(set, NULL, NULL, axis, options);
5474}
5475
5476/**
5477 * @brief Execute the XPath text() processing instruction (node type). Returns LYXP_SET_NODE_SET
5478 * with the text content of the nodes in the context.
5479 *
5480 * @param[in,out] set Context and result set at the same time.
5481 * @param[in] axis Axis to search on.
5482 * @param[in] options XPath options.
5483 * @return LY_ERR
5484 */
5485static LY_ERR
5486xpath_pi_text(struct lyxp_set *set, enum lyxp_axis axis, uint32_t options)
5487{
5488 uint32_t i;
5489
5490 if (options & LYXP_SCNODE_ALL) {
5491 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
5492 return LY_SUCCESS;
5493 }
5494
5495 if (set->type != LYXP_SET_NODE_SET) {
5496 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "text()");
5497 return LY_EVALID;
5498 }
5499
5500 if (axis != LYXP_AXIS_CHILD) {
5501 /* even following and preceding axescan return text nodes, but whatever */
5502 lyxp_set_free_content(set);
5503 return LY_SUCCESS;
5504 }
5505
5506 for (i = 0; i < set->used; ++i) {
5507 switch (set->val.nodes[i].type) {
5508 case LYXP_NODE_NONE:
5509 LOGINT_RET(set->ctx);
5510 case LYXP_NODE_ELEM:
5511 if (set->val.nodes[i].node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
5512 set->val.nodes[i].type = LYXP_NODE_TEXT;
5513 break;
5514 }
5515 /* fall through */
5516 case LYXP_NODE_ROOT:
5517 case LYXP_NODE_ROOT_CONFIG:
5518 case LYXP_NODE_TEXT:
5519 case LYXP_NODE_META:
5520 set_remove_node_none(set, i);
5521 break;
5522 }
5523 }
5524 set_remove_nodes_none(set);
5525
5526 return LY_SUCCESS;
5527}
5528
5529/**
Michal Vasko6346ece2019-09-24 13:12:53 +02005530 * @brief Skip prefix and return corresponding model if there is a prefix. Logs directly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005531 *
Michal Vasko2104e9f2020-03-06 08:23:25 +01005532 * XPath @p set is expected to be a (sc)node set!
5533 *
Michal Vasko6346ece2019-09-24 13:12:53 +02005534 * @param[in,out] qname Qualified node name. If includes prefix, it is skipped.
5535 * @param[in,out] qname_len Length of @p qname, is updated accordingly.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005536 * @param[in] set Set with general XPath context.
5537 * @param[in] ctx_scnode Context node to inherit module for unprefixed node for ::LY_PREF_JSON.
Michal Vasko6346ece2019-09-24 13:12:53 +02005538 * @param[out] moveto_mod Expected module of a matching node.
5539 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005540 */
Michal Vasko6346ece2019-09-24 13:12:53 +02005541static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005542moveto_resolve_model(const char **qname, uint16_t *qname_len, const struct lyxp_set *set,
5543 const struct lysc_node *ctx_scnode, const struct lys_module **moveto_mod)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005544{
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02005545 const struct lys_module *mod = NULL;
Michal Vasko6346ece2019-09-24 13:12:53 +02005546 const char *ptr;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005547 size_t pref_len;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005548
Michal Vasko2104e9f2020-03-06 08:23:25 +01005549 assert((set->type == LYXP_SET_NODE_SET) || (set->type == LYXP_SET_SCNODE_SET));
5550
Michal Vasko6346ece2019-09-24 13:12:53 +02005551 if ((ptr = ly_strnchr(*qname, ':', *qname_len))) {
5552 /* specific module */
5553 pref_len = ptr - *qname;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005554 mod = ly_resolve_prefix(set->ctx, *qname, pref_len, set->format, set->prefix_data);
Michal Vasko6346ece2019-09-24 13:12:53 +02005555
Michal Vasko004d3152020-06-11 19:59:22 +02005556 /* check for errors and non-implemented modules, as they are not valid */
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005557 if (!mod || !mod->implemented) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005558 LOGVAL(set->ctx, LY_VCODE_XP_INMOD, pref_len, *qname);
Michal Vasko6346ece2019-09-24 13:12:53 +02005559 return LY_EVALID;
5560 }
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005561
Michal Vasko6346ece2019-09-24 13:12:53 +02005562 *qname += pref_len + 1;
5563 *qname_len -= pref_len + 1;
5564 } else if (((*qname)[0] == '*') && (*qname_len == 1)) {
5565 /* all modules - special case */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005566 mod = NULL;
Michal Vasko6346ece2019-09-24 13:12:53 +02005567 } else {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005568 switch (set->format) {
Radek Krejci8df109d2021-04-23 12:19:08 +02005569 case LY_VALUE_SCHEMA:
5570 case LY_VALUE_SCHEMA_RESOLVED:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005571 /* current module */
5572 mod = set->cur_mod;
5573 break;
Radek Krejci224d4b42021-04-23 13:54:59 +02005574 case LY_VALUE_CANON:
Radek Krejci8df109d2021-04-23 12:19:08 +02005575 case LY_VALUE_JSON:
Radek Krejcif9943642021-04-26 10:18:21 +02005576 case LY_VALUE_LYB:
Michal Vaskoddd76592022-01-17 13:34:48 +01005577 case LY_VALUE_STR_NS:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005578 /* inherit parent (context node) module */
5579 if (ctx_scnode) {
5580 mod = ctx_scnode->module;
5581 } else {
5582 mod = NULL;
5583 }
5584 break;
Radek Krejci8df109d2021-04-23 12:19:08 +02005585 case LY_VALUE_XML:
Michal Vasko52143d12021-04-14 15:36:39 +02005586 /* all nodes need to be prefixed */
5587 LOGVAL(set->ctx, LYVE_DATA, "Non-prefixed node \"%.*s\" in XML xpath found.", *qname_len, *qname);
5588 return LY_EVALID;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005589 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005590 }
5591
Michal Vasko6346ece2019-09-24 13:12:53 +02005592 *moveto_mod = mod;
5593 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005594}
5595
5596/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02005597 * @brief Move context @p set to the root. Handles absolute path.
5598 * Result is LYXP_SET_NODE_SET.
5599 *
5600 * @param[in,out] set Set to use.
5601 * @param[in] options Xpath options.
Michal Vaskob0099a92020-08-31 14:55:23 +02005602 * @return LY_ERR value.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005603 */
Michal Vaskob0099a92020-08-31 14:55:23 +02005604static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005605moveto_root(struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005606{
aPiecek8b0cc152021-05-31 16:40:31 +02005607 assert(!(options & LYXP_SKIP_EXPR));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005608
5609 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02005610 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskob0099a92020-08-31 14:55:23 +02005611 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, NULL, set->root_type, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005612 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005613 set->type = LYXP_SET_NODE_SET;
5614 set->used = 0;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005615 set_insert_node(set, NULL, 0, set->root_type, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005616 }
Michal Vaskob0099a92020-08-31 14:55:23 +02005617
5618 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005619}
5620
5621/**
5622 * @brief Check @p node as a part of NameTest processing.
5623 *
5624 * @param[in] node Node to check.
Michal Vasko49fec8e2022-05-24 10:28:33 +02005625 * @param[in] node_type Node type of @p node.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005626 * @param[in] set Set to read general context from.
Michal Vaskod3678892020-05-21 10:06:58 +02005627 * @param[in] node_name Node name in the dictionary to move to, NULL for any node.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005628 * @param[in] moveto_mod Expected module of the node, NULL for no prefix.
Michal Vaskocdad7122020-11-09 21:04:44 +01005629 * @param[in] options XPath options.
Michal Vasko6346ece2019-09-24 13:12:53 +02005630 * @return LY_ERR (LY_ENOT if node does not match, LY_EINCOMPLETE on unresolved when,
Michal Vaskocd2c88a2022-06-07 10:54:34 +02005631 * LY_EINVAL if neither node nor any children match)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005632 */
5633static LY_ERR
Michal Vasko49fec8e2022-05-24 10:28:33 +02005634moveto_node_check(const struct lyd_node *node, enum lyxp_node_type node_type, const struct lyxp_set *set,
5635 const char *node_name, const struct lys_module *moveto_mod, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005636{
Michal Vasko49fec8e2022-05-24 10:28:33 +02005637 if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT)) {
5638 assert(node_type == set->root_type);
5639
5640 if (node_name || moveto_mod) {
5641 /* root will not match a specific node */
5642 return LY_ENOT;
5643 }
5644 return LY_SUCCESS;
5645 } else if (node_type != LYXP_NODE_ELEM) {
5646 /* other types will not match */
5647 return LY_ENOT;
5648 }
5649
Michal Vaskodca9f122021-07-16 13:56:22 +02005650 if (!node->schema) {
5651 /* opaque node never matches */
5652 return LY_ENOT;
5653 }
5654
Michal Vasko03ff5a72019-09-11 13:49:33 +02005655 /* module check */
Michal Vasko19089f02022-06-07 11:02:11 +02005656 if (moveto_mod) {
5657 if (!(node->flags & LYD_EXT) && (node->schema->module != moveto_mod)) {
5658 return LY_ENOT;
5659 } else if ((node->flags & LYD_EXT) && strcmp(node->schema->module->name, moveto_mod->name)) {
5660 return LY_ENOT;
5661 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005662 }
5663
Michal Vasko5c4e5892019-11-14 12:31:38 +01005664 /* context check */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005665 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (node->schema->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005666 return LY_EINVAL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005667 } else if (set->context_op && (node->schema->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) &&
5668 (node->schema != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005669 return LY_EINVAL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005670 }
5671
5672 /* name check */
Michal Vasko19089f02022-06-07 11:02:11 +02005673 if (node_name) {
5674 if (!(node->flags & LYD_EXT) && (node->schema->name != node_name)) {
5675 return LY_ENOT;
5676 } else if ((node->flags & LYD_EXT) && strcmp(node->schema->name, node_name)) {
5677 return LY_ENOT;
5678 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005679 }
5680
Michal Vaskoa1424542019-11-14 16:08:52 +01005681 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01005682 if (!(options & LYXP_IGNORE_WHEN) && lysc_has_when(node->schema) && !(node->flags & LYD_WHEN_TRUE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005683 return LY_EINCOMPLETE;
Michal Vaskoa1424542019-11-14 16:08:52 +01005684 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005685
5686 /* match */
5687 return LY_SUCCESS;
5688}
5689
5690/**
Michal Vasko49fec8e2022-05-24 10:28:33 +02005691 * @brief Get the next node in a forward DFS.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005692 *
Michal Vasko49fec8e2022-05-24 10:28:33 +02005693 * @param[in] iter Last returned node.
5694 * @param[in] stop Node to stop the search on and not return.
5695 * @return Next node, NULL if there are no more.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005696 */
Michal Vasko49fec8e2022-05-24 10:28:33 +02005697static const struct lyd_node *
5698moveto_axis_node_next_dfs_forward(const struct lyd_node *iter, const struct lyd_node *stop)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005699{
Michal Vasko49fec8e2022-05-24 10:28:33 +02005700 const struct lyd_node *next = NULL;
5701
5702 /* 1) child */
5703 next = lyd_child(iter);
5704 if (!next) {
5705 if (iter == stop) {
5706 /* reached stop, no more descendants */
5707 return NULL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005708 }
Michal Vasko49fec8e2022-05-24 10:28:33 +02005709 /* 2) child next sibling */
5710 next = iter->next;
5711 }
5712 while (!next) {
5713 iter = lyd_parent(iter);
5714 if ((!stop && !iter) || (stop && (lyd_parent(iter) == lyd_parent(stop)))) {
5715 return NULL;
5716 }
5717 next = iter->next;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005718 }
5719
Michal Vasko49fec8e2022-05-24 10:28:33 +02005720 return next;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005721}
5722
5723/**
Michal Vasko49fec8e2022-05-24 10:28:33 +02005724 * @brief Get the next node in a backward DFS.
5725 *
5726 * @param[in] iter Last returned node.
5727 * @param[in] stop Node to stop the search on and not return.
5728 * @return Next node, NULL if there are no more.
5729 */
5730static const struct lyd_node *
5731moveto_axis_node_next_dfs_backward(const struct lyd_node *iter, const struct lyd_node *stop)
5732{
5733 const struct lyd_node *next = NULL;
5734
5735 /* 1) previous sibling innermost last child */
5736 next = iter->prev->next ? iter->prev : NULL;
5737 while (next && lyd_child(next)) {
5738 next = lyd_child(next);
5739 next = next->prev;
5740 }
5741
5742 if (!next) {
5743 /* 2) parent */
5744 iter = lyd_parent(iter);
5745 if ((!stop && !iter) || (stop && (lyd_parent(iter) == lyd_parent(stop)))) {
5746 return NULL;
5747 }
5748 next = iter;
5749 }
5750
5751 return next;
5752}
5753
5754/**
5755 * @brief Get the first node on an axis for a context node.
5756 *
5757 * @param[in,out] iter NULL, updated to the next node.
5758 * @param[in,out] iter_type Node type 0 of @p iter, updated to the node type of the next node.
5759 * @param[in] node Context node.
5760 * @param[in] node_type Type of @p node.
5761 * @param[in] axis Axis to use.
5762 * @param[in] set XPath set with the general context.
5763 * @return LY_SUCCESS on success.
5764 * @return LY_ENOTFOUND if no next node found.
5765 */
5766static LY_ERR
5767moveto_axis_node_next_first(const struct lyd_node **iter, enum lyxp_node_type *iter_type, const struct lyd_node *node,
5768 enum lyxp_node_type node_type, enum lyxp_axis axis, struct lyxp_set *set)
5769{
5770 const struct lyd_node *next = NULL;
5771 enum lyxp_node_type next_type = 0;
5772
5773 assert(!*iter);
5774 assert(!*iter_type);
5775
5776 switch (axis) {
5777 case LYXP_AXIS_ANCESTOR_OR_SELF:
5778 case LYXP_AXIS_DESCENDANT_OR_SELF:
5779 case LYXP_AXIS_SELF:
5780 /* return the context node */
5781 next = node;
5782 next_type = node_type;
5783 break;
5784
5785 case LYXP_AXIS_ANCESTOR:
5786 case LYXP_AXIS_PARENT:
5787 if (node_type == LYXP_NODE_ELEM) {
5788 next = lyd_parent(node);
5789 next_type = next ? LYXP_NODE_ELEM : set->root_type;
5790 } else if (node_type == LYXP_NODE_TEXT) {
5791 next = node;
5792 next_type = LYXP_NODE_ELEM;
5793 } else if (node_type == LYXP_NODE_META) {
5794 next = ((struct lyd_meta *)node)->parent;
5795 next_type = LYXP_NODE_ELEM;
5796 } /* else root does not have a parent */
5797 break;
5798
5799 case LYXP_AXIS_CHILD:
5800 if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT)) {
5801 assert(!node);
5802
5803 /* search in all the trees */
5804 next = set->tree;
5805 next_type = next ? LYXP_NODE_ELEM : 0;
5806 } else {
5807 /* search in children */
5808 next = lyd_child(node);
5809 next_type = next ? LYXP_NODE_ELEM : 0;
5810 }
5811 break;
5812
5813 case LYXP_AXIS_DESCENDANT:
5814 if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT)) {
5815 /* top-level nodes */
5816 next = set->tree;
5817 next_type = LYXP_NODE_ELEM;
5818 } else if (node_type == LYXP_NODE_ELEM) {
5819 /* start from the context node */
5820 next = moveto_axis_node_next_dfs_forward(node, node);
5821 next_type = next ? LYXP_NODE_ELEM : 0;
5822 } /* else no children */
5823 break;
5824
5825 case LYXP_AXIS_FOLLOWING:
5826 case LYXP_AXIS_FOLLOWING_SIBLING:
5827 if (node_type == LYXP_NODE_ELEM) {
5828 /* first next sibling */
5829 next = node->next;
5830 next_type = next ? LYXP_NODE_ELEM : 0;
5831 } /* else no sibling */
5832 break;
5833
5834 case LYXP_AXIS_PRECEDING:
5835 if ((node_type == LYXP_NODE_ELEM) && node->prev->next) {
5836 /* skip ancestors */
5837 next = moveto_axis_node_next_dfs_backward(node, NULL);
5838 assert(next);
5839 next_type = LYXP_NODE_ELEM;
5840 } /* else no sibling */
5841 break;
5842
5843 case LYXP_AXIS_PRECEDING_SIBLING:
5844 if (node_type == LYXP_NODE_ELEM) {
5845 /* first previous sibling */
5846 next = node->prev->next ? node->prev : NULL;
5847 next_type = next ? LYXP_NODE_ELEM : 0;
5848 } /* else no sibling */
5849 break;
5850
5851 case LYXP_AXIS_ATTRIBUTE:
5852 /* handled specially */
5853 assert(0);
5854 LOGINT(set->ctx);
5855 break;
5856 }
5857
5858 *iter = next;
5859 *iter_type = next_type;
5860 return next_type ? LY_SUCCESS : LY_ENOTFOUND;
5861}
5862
5863/**
5864 * @brief Iterate over all nodes on an axis for a context node.
5865 *
5866 * @param[in,out] iter Last returned node, start with NULL, updated to the next node.
5867 * @param[in,out] iter_type Node type of @p iter, start with 0, updated to the node type of the next node.
5868 * @param[in] node Context node.
5869 * @param[in] node_type Type of @p node.
5870 * @param[in] axis Axis to use.
5871 * @param[in] set XPath set with the general context.
5872 * @return LY_SUCCESS on success.
5873 * @return LY_ENOTFOUND if no next node found.
5874 */
5875static LY_ERR
5876moveto_axis_node_next(const struct lyd_node **iter, enum lyxp_node_type *iter_type, const struct lyd_node *node,
5877 enum lyxp_node_type node_type, enum lyxp_axis axis, struct lyxp_set *set)
5878{
5879 const struct lyd_node *next = NULL;
5880 enum lyxp_node_type next_type = 0;
5881
5882 if (!*iter_type) {
5883 /* first returned node */
5884 return moveto_axis_node_next_first(iter, iter_type, node, node_type, axis, set);
5885 }
5886
5887 switch (axis) {
5888 case LYXP_AXIS_ANCESTOR_OR_SELF:
5889 if ((*iter == node) && (*iter_type == node_type)) {
5890 /* fake first ancestor, we returned self before */
5891 *iter = NULL;
5892 *iter_type = 0;
5893 return moveto_axis_node_next_first(iter, iter_type, node, node_type, LYXP_AXIS_ANCESTOR, set);
5894 } /* else continue ancestor */
5895
5896 /* fallthrough */
5897 case LYXP_AXIS_ANCESTOR:
5898 if (*iter_type == LYXP_NODE_ELEM) {
5899 /* iter parent */
5900 next = lyd_parent(*iter);
5901 next_type = next ? LYXP_NODE_ELEM : set->root_type;
5902 } /* else root, no ancestors */
5903 break;
5904
5905 case LYXP_AXIS_CHILD:
5906 assert(*iter_type == LYXP_NODE_ELEM);
5907
5908 /* next sibling (child) */
5909 next = (*iter)->next;
5910 next_type = next ? LYXP_NODE_ELEM : 0;
5911 break;
5912
5913 case LYXP_AXIS_DESCENDANT_OR_SELF:
5914 if ((*iter == node) && (*iter_type == node_type)) {
5915 /* fake first descendant, we returned self before */
5916 *iter = NULL;
5917 *iter_type = 0;
5918 return moveto_axis_node_next_first(iter, iter_type, node, node_type, LYXP_AXIS_DESCENDANT, set);
5919 } /* else continue descendant */
5920
5921 /* fallthrough */
5922 case LYXP_AXIS_DESCENDANT:
5923 assert(*iter_type == LYXP_NODE_ELEM);
5924 next = moveto_axis_node_next_dfs_forward(*iter, node);
5925 next_type = next ? LYXP_NODE_ELEM : 0;
5926 break;
5927
5928 case LYXP_AXIS_FOLLOWING:
5929 assert(*iter_type == LYXP_NODE_ELEM);
5930 next = moveto_axis_node_next_dfs_forward(*iter, NULL);
5931 next_type = next ? LYXP_NODE_ELEM : 0;
5932 break;
5933
5934 case LYXP_AXIS_FOLLOWING_SIBLING:
5935 assert(*iter_type == LYXP_NODE_ELEM);
5936
5937 /* next sibling */
5938 next = (*iter)->next;
5939 next_type = next ? LYXP_NODE_ELEM : 0;
5940 break;
5941
5942 case LYXP_AXIS_PARENT:
5943 case LYXP_AXIS_SELF:
5944 /* parent/self was returned before */
5945 break;
5946
5947 case LYXP_AXIS_PRECEDING:
5948 assert(*iter_type == LYXP_NODE_ELEM);
5949 next = moveto_axis_node_next_dfs_backward(*iter, NULL);
5950 next_type = next ? LYXP_NODE_ELEM : 0;
5951 break;
5952
5953 case LYXP_AXIS_PRECEDING_SIBLING:
5954 assert(*iter_type == LYXP_NODE_ELEM);
5955
5956 /* previous sibling */
5957 next = (*iter)->prev->next ? (*iter)->prev : NULL;
5958 next_type = next ? LYXP_NODE_ELEM : 0;
5959 break;
5960
5961 case LYXP_AXIS_ATTRIBUTE:
5962 /* handled specially */
5963 assert(0);
5964 LOGINT(set->ctx);
5965 break;
5966 }
5967
5968 *iter = next;
5969 *iter_type = next_type;
5970 return next_type ? LY_SUCCESS : LY_ENOTFOUND;
5971}
5972
5973/**
5974 * @brief Move context @p set to a node. Result is LYXP_SET_NODE_SET. Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005975 *
5976 * @param[in,out] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005977 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005978 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko49fec8e2022-05-24 10:28:33 +02005979 * @param[in] axis Axis to search on.
Michal Vaskocdad7122020-11-09 21:04:44 +01005980 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005981 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5982 */
5983static LY_ERR
Michal Vasko49fec8e2022-05-24 10:28:33 +02005984moveto_node(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, enum lyxp_axis axis,
5985 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005986{
Michal Vasko230cf972021-12-02 12:31:00 +01005987 LY_ERR r, rc = LY_SUCCESS;
Michal Vasko49fec8e2022-05-24 10:28:33 +02005988 const struct lyd_node *iter;
5989 enum lyxp_node_type iter_type;
Michal Vasko230cf972021-12-02 12:31:00 +01005990 struct lyxp_set result;
Michal Vasko49fec8e2022-05-24 10:28:33 +02005991 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005992
aPiecek8b0cc152021-05-31 16:40:31 +02005993 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005994 return LY_SUCCESS;
5995 }
5996
5997 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005998 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005999 return LY_EVALID;
6000 }
6001
Michal Vasko230cf972021-12-02 12:31:00 +01006002 /* init result set */
6003 set_init(&result, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006004
Michal Vasko49fec8e2022-05-24 10:28:33 +02006005 for (i = 0; i < set->used; ++i) {
6006 /* iterate over all the nodes on the axis of the node */
6007 iter = NULL;
6008 iter_type = 0;
6009 while (!moveto_axis_node_next(&iter, &iter_type, set->val.nodes[i].node, set->val.nodes[i].type, axis, set)) {
6010 r = moveto_node_check(iter, iter_type, set, ncname, moveto_mod, options);
6011 if (r == LY_EINCOMPLETE) {
Michal Vasko230cf972021-12-02 12:31:00 +01006012 rc = r;
6013 goto cleanup;
Michal Vasko49fec8e2022-05-24 10:28:33 +02006014 } else if (r) {
6015 continue;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006016 }
Michal Vasko49fec8e2022-05-24 10:28:33 +02006017
6018 /* check for duplicates if they are possible */
6019 switch (axis) {
6020 case LYXP_AXIS_ANCESTOR:
6021 case LYXP_AXIS_ANCESTOR_OR_SELF:
6022 case LYXP_AXIS_DESCENDANT:
6023 case LYXP_AXIS_DESCENDANT_OR_SELF:
6024 case LYXP_AXIS_FOLLOWING:
6025 case LYXP_AXIS_FOLLOWING_SIBLING:
6026 case LYXP_AXIS_PARENT:
6027 case LYXP_AXIS_PRECEDING:
6028 case LYXP_AXIS_PRECEDING_SIBLING:
6029 if (set_dup_node_check(&result, iter, iter_type, -1)) {
6030 continue;
6031 }
6032 break;
6033 case LYXP_AXIS_CHILD:
6034 case LYXP_AXIS_SELF:
6035 break;
6036 case LYXP_AXIS_ATTRIBUTE:
6037 /* handled specially */
6038 assert(0);
6039 LOGINT(set->ctx);
6040 break;
6041 }
6042
6043 /* matching node */
6044 set_insert_node(&result, iter, 0, iter_type, result.used);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006045 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006046 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006047
Michal Vasko230cf972021-12-02 12:31:00 +01006048 /* move result to the set */
6049 lyxp_set_free_content(set);
6050 *set = result;
6051 result.type = LYXP_SET_NUMBER;
Michal Vasko49fec8e2022-05-24 10:28:33 +02006052
6053 /* sort the final set */
6054 set_sort(set);
Michal Vasko230cf972021-12-02 12:31:00 +01006055
6056cleanup:
6057 lyxp_set_free_content(&result);
6058 return rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006059}
6060
6061/**
Michal Vasko49fec8e2022-05-24 10:28:33 +02006062 * @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 +02006063 *
6064 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02006065 * @param[in] scnode Matching node schema.
Michal Vasko004d3152020-06-11 19:59:22 +02006066 * @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 +01006067 * @param[in] options XPath options.
Michal Vaskod3678892020-05-21 10:06:58 +02006068 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6069 */
6070static LY_ERR
Michal Vasko49fec8e2022-05-24 10:28:33 +02006071moveto_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 +01006072 uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02006073{
Michal Vaskoddd76592022-01-17 13:34:48 +01006074 LY_ERR ret = LY_SUCCESS, r;
Michal Vaskod3678892020-05-21 10:06:58 +02006075 uint32_t i;
Michal Vaskod3678892020-05-21 10:06:58 +02006076 const struct lyd_node *siblings;
Michal Vasko230cf972021-12-02 12:31:00 +01006077 struct lyxp_set result;
Michal Vasko004d3152020-06-11 19:59:22 +02006078 struct lyd_node *sub, *inst = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02006079
Michal Vasko004d3152020-06-11 19:59:22 +02006080 assert(scnode && (!(scnode->nodetype & (LYS_LIST | LYS_LEAFLIST)) || predicates));
Michal Vaskod3678892020-05-21 10:06:58 +02006081
Michal Vasko50aaa072021-12-02 13:11:56 +01006082 /* init result set */
6083 set_init(&result, set);
6084
aPiecek8b0cc152021-05-31 16:40:31 +02006085 if (options & LYXP_SKIP_EXPR) {
Michal Vasko004d3152020-06-11 19:59:22 +02006086 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02006087 }
6088
6089 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006090 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko004d3152020-06-11 19:59:22 +02006091 ret = LY_EVALID;
6092 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02006093 }
6094
6095 /* context check for all the nodes since we have the schema node */
6096 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) {
6097 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02006098 goto cleanup;
Michal Vasko69730152020-10-09 16:30:07 +02006099 } else if (set->context_op && (scnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) &&
6100 (scnode != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02006101 lyxp_set_free_content(set);
6102 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +02006103 }
6104
6105 /* create specific data instance if needed */
6106 if (scnode->nodetype == LYS_LIST) {
6107 LY_CHECK_GOTO(ret = lyd_create_list(scnode, predicates, &inst), cleanup);
6108 } else if (scnode->nodetype == LYS_LEAFLIST) {
6109 LY_CHECK_GOTO(ret = lyd_create_term2(scnode, &predicates[0].value, &inst), cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02006110 }
6111
Michal Vasko230cf972021-12-02 12:31:00 +01006112 for (i = 0; i < set->used; ++i) {
Michal Vaskod3678892020-05-21 10:06:58 +02006113 siblings = NULL;
6114
6115 if ((set->val.nodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.nodes[i].type == LYXP_NODE_ROOT)) {
6116 assert(!set->val.nodes[i].node);
6117
6118 /* search in all the trees */
6119 siblings = set->tree;
6120 } else if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
6121 /* search in children */
Radek Krejcia1c1e542020-09-29 16:06:52 +02006122 siblings = lyd_child(set->val.nodes[i].node);
Michal Vaskod3678892020-05-21 10:06:58 +02006123 }
6124
6125 /* find the node using hashes */
Michal Vasko004d3152020-06-11 19:59:22 +02006126 if (inst) {
Michal Vaskoddd76592022-01-17 13:34:48 +01006127 r = lyd_find_sibling_first(siblings, inst, &sub);
Michal Vaskod3678892020-05-21 10:06:58 +02006128 } else {
Michal Vaskoddd76592022-01-17 13:34:48 +01006129 r = lyd_find_sibling_val(siblings, scnode, NULL, 0, &sub);
Michal Vaskod3678892020-05-21 10:06:58 +02006130 }
Michal Vaskoddd76592022-01-17 13:34:48 +01006131 LY_CHECK_ERR_GOTO(r && (r != LY_ENOTFOUND), ret = r, cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02006132
6133 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01006134 if (!(options & LYXP_IGNORE_WHEN) && sub && lysc_has_when(sub->schema) && !(sub->flags & LYD_WHEN_TRUE)) {
Michal Vasko004d3152020-06-11 19:59:22 +02006135 ret = LY_EINCOMPLETE;
6136 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02006137 }
6138
6139 if (sub) {
6140 /* pos filled later */
Michal Vasko230cf972021-12-02 12:31:00 +01006141 set_insert_node(&result, sub, 0, LYXP_NODE_ELEM, result.used);
Michal Vaskod3678892020-05-21 10:06:58 +02006142 }
6143 }
6144
Michal Vasko230cf972021-12-02 12:31:00 +01006145 /* move result to the set */
6146 lyxp_set_free_content(set);
6147 *set = result;
6148 result.type = LYXP_SET_NUMBER;
6149 assert(!set_sort(set));
6150
Michal Vasko004d3152020-06-11 19:59:22 +02006151cleanup:
Michal Vasko230cf972021-12-02 12:31:00 +01006152 lyxp_set_free_content(&result);
Michal Vasko004d3152020-06-11 19:59:22 +02006153 lyd_free_tree(inst);
6154 return ret;
Michal Vaskod3678892020-05-21 10:06:58 +02006155}
6156
6157/**
Michal Vasko49fec8e2022-05-24 10:28:33 +02006158 * @brief Check @p node as a part of schema NameTest processing.
6159 *
6160 * @param[in] node Schema node to check.
6161 * @param[in] ctx_scnode Context node.
6162 * @param[in] set Set to read general context from.
6163 * @param[in] node_name Node name in the dictionary to move to, NULL for any nodes.
6164 * @param[in] moveto_mod Expected module of the node, NULL for no prefix.
6165 * @return LY_ERR (LY_ENOT if node does not match, LY_EINVAL if neither node nor any children match)
6166 */
6167static LY_ERR
6168moveto_scnode_check(const struct lysc_node *node, const struct lysc_node *ctx_scnode, const struct lyxp_set *set,
6169 const char *node_name, const struct lys_module *moveto_mod)
6170{
6171 if (!moveto_mod && node_name) {
6172 switch (set->format) {
6173 case LY_VALUE_SCHEMA:
6174 case LY_VALUE_SCHEMA_RESOLVED:
6175 /* use current module */
6176 moveto_mod = set->cur_mod;
6177 break;
6178 case LY_VALUE_JSON:
6179 case LY_VALUE_LYB:
6180 case LY_VALUE_STR_NS:
6181 /* inherit module of the context node, if any */
6182 if (ctx_scnode) {
6183 moveto_mod = ctx_scnode->module;
6184 }
6185 break;
6186 case LY_VALUE_CANON:
6187 case LY_VALUE_XML:
6188 /* not defined */
6189 LOGINT(set->ctx);
6190 return LY_EINVAL;
6191 }
6192 }
6193
6194 if (!node) {
6195 /* root will not match a specific node */
6196 if (node_name || moveto_mod) {
6197 return LY_ENOT;
6198 }
6199 return LY_SUCCESS;
6200 }
6201
6202 /* module check */
6203 if (moveto_mod && (node->module != moveto_mod)) {
6204 return LY_ENOT;
6205 }
6206
6207 /* context check */
6208 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (node->flags & LYS_CONFIG_R)) {
6209 return LY_EINVAL;
6210 } else if (set->context_op && (node->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && (node != set->context_op)) {
6211 return LY_EINVAL;
6212 }
6213
6214 /* name check */
6215 if (node_name && (node->name != node_name)) {
6216 return LY_ENOT;
6217 }
6218
6219 /* match */
6220 return LY_SUCCESS;
6221}
6222
6223/**
6224 * @brief Get the next node in a forward schema node DFS.
6225 *
6226 * @param[in] iter Last returned node.
6227 * @param[in] stop Node to stop the search on and not return.
6228 * @param[in] getnext_opts Options for ::lys_getnext().
6229 * @return Next node, NULL if there are no more.
6230 */
6231static const struct lysc_node *
6232moveto_axis_scnode_next_dfs_forward(const struct lysc_node *iter, const struct lysc_node *stop, uint32_t getnext_opts)
6233{
6234 const struct lysc_node *next = NULL;
6235
6236 next = lysc_node_child(iter);
6237 if (!next) {
6238 /* no children, try siblings */
Michal Vasko34a22fe2022-06-15 07:58:55 +02006239 if ((iter == stop) || !lysc_data_parent(iter)) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02006240 /* we are done, no next element to process */
6241 return NULL;
6242 }
6243
6244 next = lys_getnext(iter, lysc_data_parent(iter), NULL, getnext_opts);
6245 }
6246 while (!next && iter) {
6247 /* parent is already processed, go to its sibling */
6248 iter = iter->parent;
Michal Vasko34a22fe2022-06-15 07:58:55 +02006249 if ((iter == stop) || !lysc_data_parent(iter)) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02006250 /* we are done, no next element to process */
6251 return NULL;
6252 }
6253 next = lys_getnext(iter, lysc_data_parent(iter), NULL, getnext_opts);
6254 }
6255
6256 return next;
6257}
6258
6259/**
6260 * @brief Consider schema node based on its in_ctx enum value.
6261 *
6262 * @param[in,out] in_ctx In_ctx enum of the schema node, may be updated.
6263 * @param[in] axis Axis to use.
6264 * @return LY_SUCCESS on success.
6265 * @return LY_ENOT if the node should not be returned.
6266 */
6267static LY_ERR
6268moveto_axis_scnode_next_in_ctx(int32_t *in_ctx, enum lyxp_axis axis)
6269{
6270 switch (axis) {
6271 case LYXP_AXIS_SELF:
6272 if ((*in_ctx == LYXP_SET_SCNODE_START) || (*in_ctx == LYXP_SET_SCNODE_ATOM_CTX)) {
6273 /* additionally put the start node into context */
6274 *in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
6275 return LY_SUCCESS;
6276 }
6277 break;
6278 case LYXP_AXIS_PARENT:
6279 case LYXP_AXIS_ANCESTOR_OR_SELF:
6280 case LYXP_AXIS_ANCESTOR:
6281 case LYXP_AXIS_DESCENDANT_OR_SELF:
6282 case LYXP_AXIS_DESCENDANT:
6283 case LYXP_AXIS_FOLLOWING:
6284 case LYXP_AXIS_FOLLOWING_SIBLING:
6285 case LYXP_AXIS_PRECEDING:
6286 case LYXP_AXIS_PRECEDING_SIBLING:
6287 case LYXP_AXIS_CHILD:
6288 if (*in_ctx == LYXP_SET_SCNODE_START) {
6289 /* remember that context node was used */
6290 *in_ctx = LYXP_SET_SCNODE_START_USED;
6291 return LY_SUCCESS;
6292 } else if (*in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
6293 /* traversed */
6294 *in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
6295 return LY_SUCCESS;
6296 }
6297 break;
6298 case LYXP_AXIS_ATTRIBUTE:
6299 /* unreachable */
6300 assert(0);
6301 LOGINT(NULL);
6302 break;
6303 }
6304
6305 return LY_ENOT;
6306}
6307
6308/**
6309 * @brief Get previous sibling for a schema node.
6310 *
6311 * @param[in] scnode Schema node.
6312 * @param[in] getnext_opts Options for ::lys_getnext().
6313 * @return Previous sibling, NULL if none.
6314 */
6315static const struct lysc_node *
6316moveto_axis_scnode_preceding_sibling(const struct lysc_node *scnode, uint32_t getnext_opts)
6317{
6318 const struct lysc_node *next = NULL, *prev = NULL;
6319
6320 while ((next = lys_getnext(next, lysc_data_parent(scnode), scnode->module->compiled, getnext_opts))) {
6321 if (next == scnode) {
6322 break;
6323 }
6324
6325 prev = next;
6326 }
6327
6328 return prev;
6329}
6330
6331/**
6332 * @brief Get the first schema node on an axis for a context node.
6333 *
6334 * @param[in,out] iter Last returned node, start with NULL, updated to the next node.
6335 * @param[in,out] iter_type Node type of @p iter, start with 0, updated to the node type of the next node.
6336 * @param[in,out] iter_mod Internal module iterator, do not change.
6337 * @param[in,out] iter_mod_idx Internal module index iterator, do not change.
6338 * @param[in] scnode Context node.
6339 * @param[in] node_type Type of @p scnode.
6340 * @param[in] in_ctx In_ctx enum of @p scnode.
6341 * @param[in] axis Axis to use.
6342 * @param[in] set XPath set with the general context.
6343 * @param[in] getnext_opts Options for ::lys_getnext().
6344 * @return LY_SUCCESS on success.
6345 * @return LY_ENOTFOUND if no next node found.
6346 */
6347static LY_ERR
6348moveto_axis_scnode_next_first(const struct lysc_node **iter, enum lyxp_node_type *iter_type, const struct lys_module **iter_mod,
6349 uint32_t *iter_mod_idx, const struct lysc_node *scnode, enum lyxp_node_type node_type, enum lyxp_axis axis,
6350 struct lyxp_set *set, uint32_t getnext_opts)
6351{
6352 const struct lysc_node *next = NULL;
6353 enum lyxp_node_type next_type = 0;
6354
6355 assert(!*iter);
6356 assert(!*iter_type);
6357
6358 *iter_mod = NULL;
6359 *iter_mod_idx = 0;
6360
6361 switch (axis) {
6362 case LYXP_AXIS_ANCESTOR_OR_SELF:
6363 case LYXP_AXIS_DESCENDANT_OR_SELF:
6364 case LYXP_AXIS_SELF:
6365 if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT) || (node_type == LYXP_NODE_ELEM)) {
6366 /* just return the node */
6367 next = scnode;
6368 next_type = node_type;
6369 }
6370 break;
6371
6372 case LYXP_AXIS_ANCESTOR:
6373 case LYXP_AXIS_PARENT:
6374 if (node_type == LYXP_NODE_ELEM) {
6375 next = lysc_data_parent(scnode);
6376 next_type = next ? LYXP_NODE_ELEM : set->root_type;
6377 } /* else no parent */
6378 break;
6379
6380 case LYXP_AXIS_DESCENDANT:
6381 case LYXP_AXIS_CHILD:
6382 if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT)) {
6383 /* it can actually be in any module, it's all <running>, and even if it's moveto_mod (if set),
6384 * it can be in a top-level augment */
6385 while ((*iter_mod = ly_ctx_get_module_iter(set->ctx, iter_mod_idx))) {
6386 /* module may not be implemented or not compiled yet */
6387 if (!(*iter_mod)->compiled) {
6388 continue;
6389 }
6390
6391 /* get next node */
6392 if ((next = lys_getnext(NULL, NULL, (*iter_mod)->compiled, getnext_opts))) {
6393 next_type = LYXP_NODE_ELEM;
6394 break;
6395 }
6396 }
6397 } else if (node_type == LYXP_NODE_ELEM) {
6398 /* get next node */
6399 next = lys_getnext(NULL, scnode, NULL, getnext_opts);
6400 next_type = next ? LYXP_NODE_ELEM : 0;
6401 }
6402 break;
6403
6404 case LYXP_AXIS_FOLLOWING:
6405 case LYXP_AXIS_FOLLOWING_SIBLING:
6406 if (node_type == LYXP_NODE_ELEM) {
6407 /* first next sibling */
6408 next = lys_getnext(scnode, lysc_data_parent(scnode), scnode->module->compiled, getnext_opts);
6409 next_type = next ? LYXP_NODE_ELEM : 0;
6410 } /* else no sibling */
6411 break;
6412
6413 case LYXP_AXIS_PRECEDING:
6414 case LYXP_AXIS_PRECEDING_SIBLING:
6415 if (node_type == LYXP_NODE_ELEM) {
6416 /* first parent sibling */
6417 next = lys_getnext(NULL, lysc_data_parent(scnode), scnode->module->compiled, getnext_opts);
6418 if (next == scnode) {
6419 /* no preceding sibling */
6420 next = NULL;
6421 }
6422 next_type = next ? LYXP_NODE_ELEM : 0;
6423 } /* else no sibling */
6424 break;
6425
6426 case LYXP_AXIS_ATTRIBUTE:
6427 /* unreachable */
6428 assert(0);
6429 LOGINT(set->ctx);
6430 break;
6431 }
6432
6433 *iter = next;
6434 *iter_type = next_type;
6435 return next_type ? LY_SUCCESS : LY_ENOTFOUND;
6436}
6437
6438/**
6439 * @brief Iterate over all schema nodes on an axis for a context node.
6440 *
6441 * @param[in,out] iter Last returned node, start with NULL, updated to the next node.
6442 * @param[in,out] iter_type Node type of @p iter, start with 0, updated to the node type of the next node.
6443 * @param[in,out] iter_mod Internal module iterator, do not change.
6444 * @param[in,out] iter_mod_idx Internal module index iterator, do not change.
6445 * @param[in] scnode Context node.
6446 * @param[in] node_type Type of @p scnode.
6447 * @param[in] axis Axis to use.
6448 * @param[in] set XPath set with the general context.
6449 * @param[in] getnext_opts Options for ::lys_getnext().
6450 * @return LY_SUCCESS on success.
6451 * @return LY_ENOTFOUND if no next node found.
6452 */
6453static LY_ERR
6454moveto_axis_scnode_next(const struct lysc_node **iter, enum lyxp_node_type *iter_type, const struct lys_module **iter_mod,
6455 uint32_t *iter_mod_idx, const struct lysc_node *scnode, enum lyxp_node_type node_type, enum lyxp_axis axis,
6456 struct lyxp_set *set, uint32_t getnext_opts)
6457{
6458 const struct lysc_node *next = NULL, *dfs_stop;
6459 enum lyxp_node_type next_type = 0;
6460
6461 if (!*iter_type) {
6462 /* first returned node */
6463 return moveto_axis_scnode_next_first(iter, iter_type, iter_mod, iter_mod_idx, scnode, node_type, axis, set,
6464 getnext_opts);
6465 }
6466
6467 switch (axis) {
6468 case LYXP_AXIS_PARENT:
6469 case LYXP_AXIS_SELF:
6470 /* parent/self was returned before */
6471 break;
6472
6473 case LYXP_AXIS_ANCESTOR_OR_SELF:
6474 if ((*iter == scnode) && (*iter_type == node_type)) {
6475 /* fake first ancestor, we returned self before */
6476 *iter = NULL;
6477 *iter_type = 0;
6478 return moveto_axis_scnode_next_first(iter, iter_type, iter_mod, iter_mod_idx, scnode, node_type,
6479 LYXP_AXIS_ANCESTOR, set, getnext_opts);
6480 } /* else continue ancestor */
6481
6482 /* fallthrough */
6483 case LYXP_AXIS_ANCESTOR:
6484 if (*iter_type == LYXP_NODE_ELEM) {
6485 next = lysc_data_parent(*iter);
6486 next_type = next ? LYXP_NODE_ELEM : set->root_type;
6487 } /* else no ancestor */
6488 break;
6489
6490 case LYXP_AXIS_DESCENDANT_OR_SELF:
6491 if ((*iter == scnode) && (*iter_type == node_type)) {
6492 /* fake first descendant, we returned self before */
6493 *iter = NULL;
6494 *iter_type = 0;
6495 return moveto_axis_scnode_next_first(iter, iter_type, iter_mod, iter_mod_idx, scnode, node_type,
6496 LYXP_AXIS_DESCENDANT, set, getnext_opts);
6497 } /* else DFS until context node */
6498 dfs_stop = scnode;
6499
6500 /* fallthrough */
6501 case LYXP_AXIS_DESCENDANT:
6502 if (axis == LYXP_AXIS_DESCENDANT) {
6503 /* DFS until the context node */
6504 dfs_stop = scnode;
6505 }
6506
6507 /* fallthrough */
6508 case LYXP_AXIS_PRECEDING:
6509 if (axis == LYXP_AXIS_PRECEDING) {
6510 /* DFS until the previous sibling */
6511 dfs_stop = moveto_axis_scnode_preceding_sibling(scnode, getnext_opts);
6512 assert(dfs_stop);
6513
6514 if (*iter == dfs_stop) {
6515 /* we are done */
6516 break;
6517 }
6518 }
6519
6520 /* fallthrough */
6521 case LYXP_AXIS_FOLLOWING:
6522 if (axis == LYXP_AXIS_FOLLOWING) {
6523 /* DFS through the whole module */
6524 dfs_stop = NULL;
6525 }
6526
6527 /* nested nodes */
6528 assert(*iter);
6529 next = moveto_axis_scnode_next_dfs_forward(*iter, dfs_stop, getnext_opts);
6530 if (next) {
6531 next_type = LYXP_NODE_ELEM;
6532 break;
6533 } /* else get next top-level node just like a child */
6534
6535 /* fallthrough */
6536 case LYXP_AXIS_CHILD:
6537 case LYXP_AXIS_FOLLOWING_SIBLING:
6538 if (!*iter_mod) {
6539 /* nodes from a single module */
6540 if ((next = lys_getnext(*iter, lysc_data_parent(*iter), (*iter)->module->compiled, getnext_opts))) {
6541 next_type = LYXP_NODE_ELEM;
6542 break;
6543 }
6544
6545 assert(scnode);
6546 if (!lysc_data_parent(scnode)) {
6547 /* iterating over top-level nodes, find next */
6548 while (lysc_data_parent(*iter)) {
6549 *iter = lysc_data_parent(*iter);
6550 }
6551 if ((next = lys_getnext(*iter, NULL, (*iter)->module->compiled, getnext_opts))) {
6552 next_type = LYXP_NODE_ELEM;
6553 break;
6554 }
6555 }
6556 }
6557
6558 while (*iter_mod) {
6559 /* module top-level nodes */
6560 if ((next = lys_getnext(*iter, NULL, (*iter_mod)->compiled, getnext_opts))) {
6561 next_type = LYXP_NODE_ELEM;
6562 break;
6563 }
6564
6565 /* get next module */
6566 while ((*iter_mod = ly_ctx_get_module_iter(set->ctx, iter_mod_idx))) {
6567 /* module may not be implemented or not compiled yet */
6568 if ((*iter_mod)->compiled) {
6569 break;
6570 }
6571 }
6572
6573 /* new module, start over */
6574 *iter = NULL;
6575 }
6576 break;
6577
6578 case LYXP_AXIS_PRECEDING_SIBLING:
6579 assert(*iter);
6580
6581 /* next parent sibling until scnode */
6582 next = lys_getnext(*iter, lysc_data_parent(*iter), (*iter)->module->compiled, getnext_opts);
6583 if (next == scnode) {
6584 /* no previous sibling */
6585 next = NULL;
6586 }
6587 next_type = next ? LYXP_NODE_ELEM : 0;
6588 break;
6589
6590 case LYXP_AXIS_ATTRIBUTE:
6591 /* unreachable */
6592 assert(0);
6593 LOGINT(set->ctx);
6594 break;
6595 }
6596
6597 *iter = next;
6598 *iter_type = next_type;
6599 return next_type ? LY_SUCCESS : LY_ENOTFOUND;
6600}
6601
6602/**
Michal Vaskod3678892020-05-21 10:06:58 +02006603 * @brief Move context @p set to a schema node. Result is LYXP_SET_SCNODE_SET (or LYXP_SET_EMPTY).
6604 *
6605 * @param[in,out] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006606 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02006607 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko49fec8e2022-05-24 10:28:33 +02006608 * @param[in] axis Axis to search on.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006609 * @param[in] options XPath options.
6610 * @return LY_ERR
6611 */
6612static LY_ERR
Michal Vasko49fec8e2022-05-24 10:28:33 +02006613moveto_scnode(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, enum lyxp_axis axis,
6614 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006615{
Radek Krejci857189e2020-09-01 13:26:36 +02006616 ly_bool temp_ctx = 0;
Michal Vasko49fec8e2022-05-24 10:28:33 +02006617 uint32_t getnext_opts, orig_used, i, mod_idx, idx;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006618 const struct lys_module *mod;
Michal Vasko49fec8e2022-05-24 10:28:33 +02006619 const struct lysc_node *iter;
6620 enum lyxp_node_type iter_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006621
aPiecek8b0cc152021-05-31 16:40:31 +02006622 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006623 return LY_SUCCESS;
6624 }
6625
6626 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006627 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006628 return LY_EVALID;
6629 }
6630
Michal Vaskocafad9d2019-11-07 15:20:03 +01006631 /* getnext opts */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01006632 getnext_opts = 0;
Michal Vaskocafad9d2019-11-07 15:20:03 +01006633 if (options & LYXP_SCNODE_OUTPUT) {
6634 getnext_opts |= LYS_GETNEXT_OUTPUT;
6635 }
6636
Michal Vasko03ff5a72019-09-11 13:49:33 +02006637 orig_used = set->used;
6638 for (i = 0; i < orig_used; ++i) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02006639 /* update in_ctx first */
6640 if (moveto_axis_scnode_next_in_ctx(&set->val.scnodes[i].in_ctx, axis)) {
6641 /* not usable, skip */
6642 continue;
6643 }
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006644
Michal Vasko49fec8e2022-05-24 10:28:33 +02006645 iter = NULL;
6646 iter_type = 0;
6647 while (!moveto_axis_scnode_next(&iter, &iter_type, &mod, &mod_idx, set->val.scnodes[i].scnode,
6648 set->val.scnodes[i].type, axis, set, getnext_opts)) {
6649 if (moveto_scnode_check(iter, NULL, set, ncname, moveto_mod)) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006650 continue;
6651 }
6652
Michal Vasko49fec8e2022-05-24 10:28:33 +02006653 /* insert */
6654 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, iter_type, &idx));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006655
Michal Vasko49fec8e2022-05-24 10:28:33 +02006656 /* we need to prevent these nodes from being considered in this moveto */
6657 if ((idx < orig_used) && (idx > i)) {
6658 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX;
6659 temp_ctx = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006660 }
6661 }
6662 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006663
6664 /* correct temporary in_ctx values */
6665 if (temp_ctx) {
6666 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006667 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_NEW_CTX) {
6668 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006669 }
6670 }
6671 }
6672
6673 return LY_SUCCESS;
6674}
6675
6676/**
Michal Vasko49fec8e2022-05-24 10:28:33 +02006677 * @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 +02006678 * Context position aware.
6679 *
6680 * @param[in] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006681 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02006682 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vaskocdad7122020-11-09 21:04:44 +01006683 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006684 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6685 */
6686static LY_ERR
Michal Vasko49fec8e2022-05-24 10:28:33 +02006687moveto_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 +02006688{
6689 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006690 const struct lyd_node *next, *elem, *start;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006691 struct lyxp_set ret_set;
6692 LY_ERR rc;
6693
aPiecek8b0cc152021-05-31 16:40:31 +02006694 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006695 return LY_SUCCESS;
6696 }
6697
6698 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006699 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006700 return LY_EVALID;
6701 }
6702
Michal Vasko9f96a052020-03-10 09:41:45 +01006703 /* 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 +02006704 rc = xpath_pi_node(set, LYXP_AXIS_CHILD, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006705 LY_CHECK_RET(rc);
6706
Michal Vasko6346ece2019-09-24 13:12:53 +02006707 /* this loop traverses all the nodes in the set and adds/keeps only those that match qname */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006708 set_init(&ret_set, set);
6709 for (i = 0; i < set->used; ++i) {
6710
6711 /* TREE DFS */
6712 start = set->val.nodes[i].node;
6713 for (elem = next = start; elem; elem = next) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02006714 rc = moveto_node_check(elem, LYXP_NODE_ELEM, set, ncname, moveto_mod, options);
Michal Vasko6346ece2019-09-24 13:12:53 +02006715 if (!rc) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006716 /* add matching node into result set */
6717 set_insert_node(&ret_set, elem, 0, LYXP_NODE_ELEM, ret_set.used);
6718 if (set_dup_node_check(set, elem, LYXP_NODE_ELEM, i)) {
6719 /* the node is a duplicate, we'll process it later in the set */
6720 goto skip_children;
6721 }
Michal Vasko6346ece2019-09-24 13:12:53 +02006722 } else if (rc == LY_EINCOMPLETE) {
Michal Vasko6346ece2019-09-24 13:12:53 +02006723 return rc;
6724 } else if (rc == LY_EINVAL) {
6725 goto skip_children;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006726 }
6727
6728 /* TREE DFS NEXT ELEM */
6729 /* select element for the next run - children first */
Radek Krejcia1c1e542020-09-29 16:06:52 +02006730 next = lyd_child(elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006731 if (!next) {
6732skip_children:
6733 /* no children, so try siblings, but only if it's not the start,
6734 * that is considered to be the root and it's siblings are not traversed */
6735 if (elem != start) {
6736 next = elem->next;
6737 } else {
6738 break;
6739 }
6740 }
6741 while (!next) {
6742 /* no siblings, go back through the parents */
Michal Vasko9e685082021-01-29 14:49:09 +01006743 if (lyd_parent(elem) == start) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006744 /* we are done, no next element to process */
6745 break;
6746 }
6747 /* parent is already processed, go to its sibling */
Michal Vasko9e685082021-01-29 14:49:09 +01006748 elem = lyd_parent(elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006749 next = elem->next;
6750 }
6751 }
6752 }
6753
6754 /* make the temporary set the current one */
6755 ret_set.ctx_pos = set->ctx_pos;
6756 ret_set.ctx_size = set->ctx_size;
Michal Vaskod3678892020-05-21 10:06:58 +02006757 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006758 memcpy(set, &ret_set, sizeof *set);
6759
6760 return LY_SUCCESS;
6761}
6762
6763/**
Michal Vasko49fec8e2022-05-24 10:28:33 +02006764 * @brief Move context @p set to a child schema node and all its descendants starting from a node.
6765 * Result is LYXP_SET_NODE_SET.
6766 *
6767 * @param[in] set Set to use.
6768 * @param[in] start Start node whose subtree to add.
6769 * @param[in] start_idx Index of @p start in @p set.
6770 * @param[in] moveto_mod Matching node module, NULL for no prefix.
6771 * @param[in] ncname Matching node name in the dictionary, NULL for any.
6772 * @param[in] options XPath options.
6773 * @return LY_ERR value.
6774 */
6775static LY_ERR
6776moveto_scnode_dfs(struct lyxp_set *set, const struct lysc_node *start, uint32_t start_idx,
6777 const struct lys_module *moveto_mod, const char *ncname, uint32_t options)
6778{
6779 const struct lysc_node *next, *elem;
6780 uint32_t idx;
6781 LY_ERR rc;
6782
6783 /* TREE DFS */
6784 for (elem = next = start; elem; elem = next) {
6785 if ((elem == start) || (elem->nodetype & (LYS_CHOICE | LYS_CASE))) {
6786 /* schema-only nodes, skip root */
6787 goto next_iter;
6788 }
6789
6790 rc = moveto_scnode_check(elem, start, set, ncname, moveto_mod);
6791 if (!rc) {
6792 if (lyxp_set_scnode_contains(set, elem, LYXP_NODE_ELEM, start_idx, &idx)) {
6793 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
6794 if (idx > start_idx) {
6795 /* we will process it later in the set */
6796 goto skip_children;
6797 }
6798 } else {
6799 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, elem, LYXP_NODE_ELEM, NULL));
6800 }
6801 } else if (rc == LY_EINVAL) {
6802 goto skip_children;
6803 }
6804
6805next_iter:
6806 /* TREE DFS NEXT ELEM */
6807 /* select element for the next run - children first */
6808 next = lysc_node_child(elem);
6809 if (next && (next->nodetype == LYS_INPUT) && (options & LYXP_SCNODE_OUTPUT)) {
6810 next = next->next;
6811 } else if (next && (next->nodetype == LYS_OUTPUT) && !(options & LYXP_SCNODE_OUTPUT)) {
6812 next = next->next;
6813 }
6814 if (!next) {
6815skip_children:
6816 /* no children, so try siblings, but only if it's not the start,
6817 * that is considered to be the root and it's siblings are not traversed */
6818 if (elem != start) {
6819 next = elem->next;
6820 } else {
6821 break;
6822 }
6823 }
6824 while (!next) {
6825 /* no siblings, go back through the parents */
6826 if (elem->parent == start) {
6827 /* we are done, no next element to process */
6828 break;
6829 }
6830 /* parent is already processed, go to its sibling */
6831 elem = elem->parent;
6832 next = elem->next;
6833 }
6834 }
6835
6836 return LY_SUCCESS;
6837}
6838
6839/**
6840 * @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 +02006841 *
6842 * @param[in] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006843 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02006844 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006845 * @param[in] options XPath options.
Michal Vasko49fec8e2022-05-24 10:28:33 +02006846 * @return LY_ERR value.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006847 */
6848static LY_ERR
Michal Vasko49fec8e2022-05-24 10:28:33 +02006849moveto_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 +02006850{
Michal Vasko49fec8e2022-05-24 10:28:33 +02006851 uint32_t i, orig_used, mod_idx;
6852 const struct lys_module *mod;
6853 const struct lysc_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006854
aPiecek8b0cc152021-05-31 16:40:31 +02006855 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006856 return LY_SUCCESS;
6857 }
6858
6859 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006860 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006861 return LY_EVALID;
6862 }
6863
Michal Vasko03ff5a72019-09-11 13:49:33 +02006864 orig_used = set->used;
6865 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006866 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
6867 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006868 continue;
6869 }
6870
6871 /* remember context node */
Radek Krejcif13b87b2020-12-01 22:02:17 +01006872 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vaskoec4df482019-12-16 10:02:18 +01006873 } else {
Michal Vasko1a09b212021-05-06 13:00:10 +02006874 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006875 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006876
Michal Vasko49fec8e2022-05-24 10:28:33 +02006877 if ((set->val.scnodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.scnodes[i].type == LYXP_NODE_ROOT)) {
6878 /* traverse all top-level nodes in all the modules */
6879 mod_idx = 0;
6880 while ((mod = ly_ctx_get_module_iter(set->ctx, &mod_idx))) {
6881 /* module may not be implemented or not compiled yet */
6882 if (!mod->compiled) {
6883 continue;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006884 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006885
Michal Vasko49fec8e2022-05-24 10:28:33 +02006886 root = NULL;
6887 /* no getnext opts needed */
6888 while ((root = lys_getnext(root, NULL, mod->compiled, 0))) {
6889 LY_CHECK_RET(moveto_scnode_dfs(set, root, i, moveto_mod, ncname, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006890 }
6891 }
Michal Vasko49fec8e2022-05-24 10:28:33 +02006892
6893 } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
6894 /* add all the descendants recursively */
6895 LY_CHECK_RET(moveto_scnode_dfs(set, set->val.scnodes[i].scnode, i, moveto_mod, ncname, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006896 }
6897 }
6898
6899 return LY_SUCCESS;
6900}
6901
6902/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02006903 * @brief Move context @p set to an attribute. Result is LYXP_SET_NODE_SET.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006904 * Indirectly context position aware.
6905 *
6906 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02006907 * @param[in] mod Matching metadata module, NULL for any.
6908 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
aPiecek8b0cc152021-05-31 16:40:31 +02006909 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006910 * @return LY_ERR
6911 */
6912static LY_ERR
aPiecek8b0cc152021-05-31 16:40:31 +02006913moveto_attr(struct lyxp_set *set, const struct lys_module *mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006914{
Michal Vasko9f96a052020-03-10 09:41:45 +01006915 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006916
aPiecek8b0cc152021-05-31 16:40:31 +02006917 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006918 return LY_SUCCESS;
6919 }
6920
6921 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006922 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006923 return LY_EVALID;
6924 }
6925
Radek Krejci1deb5be2020-08-26 16:43:36 +02006926 for (uint32_t i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02006927 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006928
6929 /* only attributes of an elem (not dummy) can be in the result, skip all the rest;
6930 * our attributes are always qualified */
Michal Vasko5c4e5892019-11-14 12:31:38 +01006931 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01006932 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006933
6934 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02006935 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006936 continue;
6937 }
6938
Michal Vaskod3678892020-05-21 10:06:58 +02006939 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006940 /* match */
6941 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01006942 set->val.meta[i].meta = sub;
6943 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006944 /* pos does not change */
6945 replaced = 1;
6946 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01006947 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 +02006948 }
6949 ++i;
6950 }
6951 }
6952 }
6953
6954 if (!replaced) {
6955 /* no match */
6956 set_remove_node(set, i);
6957 }
6958 }
6959
6960 return LY_SUCCESS;
6961}
6962
6963/**
6964 * @brief Move context @p set1 to union with @p set2. @p set2 is emptied afterwards.
Michal Vasko61ac2f62020-05-25 12:39:51 +02006965 * Result is LYXP_SET_NODE_SET. Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006966 *
6967 * @param[in,out] set1 Set to use for the result.
6968 * @param[in] set2 Set that is copied to @p set1.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006969 * @return LY_ERR
6970 */
6971static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006972moveto_union(struct lyxp_set *set1, struct lyxp_set *set2)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006973{
6974 LY_ERR rc;
6975
Michal Vaskod3678892020-05-21 10:06:58 +02006976 if ((set1->type != LYXP_SET_NODE_SET) || (set2->type != LYXP_SET_NODE_SET)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006977 LOGVAL(set1->ctx, LY_VCODE_XP_INOP_2, "union", print_set_type(set1), print_set_type(set2));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006978 return LY_EVALID;
6979 }
6980
6981 /* set2 is empty or both set1 and set2 */
Michal Vaskod3678892020-05-21 10:06:58 +02006982 if (!set2->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006983 return LY_SUCCESS;
6984 }
6985
Michal Vaskod3678892020-05-21 10:06:58 +02006986 if (!set1->used) {
aPiecekadc1e4f2021-10-07 11:15:12 +02006987 /* release hidden allocated data (lyxp_set.size) */
6988 lyxp_set_free_content(set1);
6989 /* direct copying of the entire structure */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006990 memcpy(set1, set2, sizeof *set1);
6991 /* dynamic memory belongs to set1 now, do not free */
Michal Vaskod3678892020-05-21 10:06:58 +02006992 memset(set2, 0, sizeof *set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006993 return LY_SUCCESS;
6994 }
6995
6996 /* we assume sets are sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006997 assert(!set_sort(set1) && !set_sort(set2));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006998
6999 /* sort, remove duplicates */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007000 rc = set_sorted_merge(set1, set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007001 LY_CHECK_RET(rc);
7002
7003 /* final set must be sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007004 assert(!set_sort(set1));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007005
7006 return LY_SUCCESS;
7007}
7008
7009/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02007010 * @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 +02007011 * Context position aware.
7012 *
7013 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02007014 * @param[in] mod Matching metadata module, NULL for any.
7015 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
Michal Vaskocdad7122020-11-09 21:04:44 +01007016 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007017 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7018 */
7019static int
Michal Vaskocdad7122020-11-09 21:04:44 +01007020moveto_attr_alldesc(struct lyxp_set *set, const struct lys_module *mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007021{
Michal Vasko9f96a052020-03-10 09:41:45 +01007022 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007023 struct lyxp_set *set_all_desc = NULL;
7024 LY_ERR rc;
7025
aPiecek8b0cc152021-05-31 16:40:31 +02007026 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007027 return LY_SUCCESS;
7028 }
7029
7030 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01007031 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007032 return LY_EVALID;
7033 }
7034
Michal Vasko03ff5a72019-09-11 13:49:33 +02007035 /* can be optimized similarly to moveto_node_alldesc() and save considerable amount of memory,
7036 * but it likely won't be used much, so it's a waste of time */
7037 /* copy the context */
7038 set_all_desc = set_copy(set);
7039 /* get all descendant nodes (the original context nodes are removed) */
Michal Vasko49fec8e2022-05-24 10:28:33 +02007040 rc = moveto_node_alldesc_child(set_all_desc, NULL, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007041 if (rc != LY_SUCCESS) {
7042 lyxp_set_free(set_all_desc);
7043 return rc;
7044 }
7045 /* prepend the original context nodes */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007046 rc = moveto_union(set, set_all_desc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007047 if (rc != LY_SUCCESS) {
7048 lyxp_set_free(set_all_desc);
7049 return rc;
7050 }
7051 lyxp_set_free(set_all_desc);
7052
Radek Krejci1deb5be2020-08-26 16:43:36 +02007053 for (uint32_t i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02007054 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007055
7056 /* only attributes of an elem can be in the result, skip all the rest,
7057 * we have all attributes qualified in lyd tree */
7058 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01007059 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007060 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02007061 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007062 continue;
7063 }
7064
Michal Vaskod3678892020-05-21 10:06:58 +02007065 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007066 /* match */
7067 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01007068 set->val.meta[i].meta = sub;
7069 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007070 /* pos does not change */
7071 replaced = 1;
7072 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01007073 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 +02007074 }
7075 ++i;
7076 }
7077 }
7078 }
7079
7080 if (!replaced) {
7081 /* no match */
7082 set_remove_node(set, i);
7083 }
7084 }
7085
7086 return LY_SUCCESS;
7087}
7088
7089/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02007090 * @brief Move context @p set to the result of a comparison. Handles '=', '!=', '<=', '<', '>=', or '>'.
7091 * Result is LYXP_SET_BOOLEAN. Indirectly context position aware.
7092 *
7093 * @param[in,out] set1 Set to use for the result.
7094 * @param[in] set2 Set acting as the second operand for @p op.
7095 * @param[in] op Comparison operator to process.
7096 * @param[in] options XPath options.
7097 * @return LY_ERR
7098 */
7099static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02007100moveto_op_comp(struct lyxp_set *set1, struct lyxp_set *set2, const char *op, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007101{
7102 /*
7103 * NODE SET + NODE SET = NODE SET + STRING /(1 NODE SET) 2 STRING
7104 * NODE SET + STRING = STRING + STRING /1 STRING (2 STRING)
7105 * NODE SET + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
7106 * NODE SET + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
7107 * STRING + NODE SET = STRING + STRING /(1 STRING) 2 STRING
7108 * NUMBER + NODE SET = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
7109 * BOOLEAN + NODE SET = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
7110 *
7111 * '=' or '!='
7112 * BOOLEAN + BOOLEAN
7113 * BOOLEAN + STRING = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
7114 * BOOLEAN + NUMBER = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
7115 * STRING + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
7116 * NUMBER + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
7117 * NUMBER + NUMBER
7118 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
7119 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
7120 * STRING + STRING
7121 *
7122 * '<=', '<', '>=', '>'
7123 * NUMBER + NUMBER
7124 * BOOLEAN + BOOLEAN = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
7125 * BOOLEAN + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
7126 * BOOLEAN + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
7127 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
7128 * STRING + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
7129 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
7130 * NUMBER + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
7131 * STRING + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
7132 */
7133 struct lyxp_set iter1, iter2;
7134 int result;
7135 int64_t i;
7136 LY_ERR rc;
7137
Michal Vasko004d3152020-06-11 19:59:22 +02007138 memset(&iter1, 0, sizeof iter1);
7139 memset(&iter2, 0, sizeof iter2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007140
7141 /* iterative evaluation with node-sets */
7142 if ((set1->type == LYXP_SET_NODE_SET) || (set2->type == LYXP_SET_NODE_SET)) {
7143 if (set1->type == LYXP_SET_NODE_SET) {
7144 for (i = 0; i < set1->used; ++i) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02007145 /* cast set1 */
Michal Vasko03ff5a72019-09-11 13:49:33 +02007146 switch (set2->type) {
7147 case LYXP_SET_NUMBER:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007148 rc = set_comp_cast(&iter1, set1, LYXP_SET_NUMBER, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007149 break;
7150 case LYXP_SET_BOOLEAN:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007151 rc = set_comp_cast(&iter1, set1, LYXP_SET_BOOLEAN, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007152 break;
7153 default:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007154 rc = set_comp_cast(&iter1, set1, LYXP_SET_STRING, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007155 break;
7156 }
7157 LY_CHECK_RET(rc);
7158
Michal Vasko4c7763f2020-07-27 17:40:37 +02007159 /* canonize set2 */
7160 LY_CHECK_ERR_RET(rc = set_comp_canonize(&iter2, set2, &set1->val.nodes[i]), lyxp_set_free_content(&iter1), rc);
7161
7162 /* compare recursively */
7163 rc = moveto_op_comp(&iter1, &iter2, op, options);
7164 lyxp_set_free_content(&iter2);
7165 LY_CHECK_ERR_RET(rc, lyxp_set_free_content(&iter1), rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007166
7167 /* lazy evaluation until true */
Michal Vasko004d3152020-06-11 19:59:22 +02007168 if (iter1.val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007169 set_fill_boolean(set1, 1);
7170 return LY_SUCCESS;
7171 }
7172 }
7173 } else {
7174 for (i = 0; i < set2->used; ++i) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02007175 /* set set2 */
Michal Vasko03ff5a72019-09-11 13:49:33 +02007176 switch (set1->type) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02007177 case LYXP_SET_NUMBER:
7178 rc = set_comp_cast(&iter2, set2, LYXP_SET_NUMBER, i);
7179 break;
7180 case LYXP_SET_BOOLEAN:
7181 rc = set_comp_cast(&iter2, set2, LYXP_SET_BOOLEAN, i);
7182 break;
7183 default:
7184 rc = set_comp_cast(&iter2, set2, LYXP_SET_STRING, i);
7185 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007186 }
7187 LY_CHECK_RET(rc);
7188
Michal Vasko4c7763f2020-07-27 17:40:37 +02007189 /* canonize set1 */
7190 LY_CHECK_ERR_RET(rc = set_comp_canonize(&iter1, set1, &set2->val.nodes[i]), lyxp_set_free_content(&iter2), rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007191
Michal Vasko4c7763f2020-07-27 17:40:37 +02007192 /* compare recursively */
Michal Vasko03ff5a72019-09-11 13:49:33 +02007193 rc = moveto_op_comp(&iter1, &iter2, op, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007194 lyxp_set_free_content(&iter2);
Michal Vasko4c7763f2020-07-27 17:40:37 +02007195 LY_CHECK_ERR_RET(rc, lyxp_set_free_content(&iter1), rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007196
7197 /* lazy evaluation until true */
Michal Vasko004d3152020-06-11 19:59:22 +02007198 if (iter1.val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007199 set_fill_boolean(set1, 1);
7200 return LY_SUCCESS;
7201 }
7202 }
7203 }
7204
7205 /* false for all nodes */
7206 set_fill_boolean(set1, 0);
7207 return LY_SUCCESS;
7208 }
7209
7210 /* first convert properly */
7211 if ((op[0] == '=') || (op[0] == '!')) {
7212 if ((set1->type == LYXP_SET_BOOLEAN) || (set2->type == LYXP_SET_BOOLEAN)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007213 lyxp_set_cast(set1, LYXP_SET_BOOLEAN);
7214 lyxp_set_cast(set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007215 } else if ((set1->type == LYXP_SET_NUMBER) || (set2->type == LYXP_SET_NUMBER)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007216 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007217 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007218 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007219 LY_CHECK_RET(rc);
7220 } /* else we have 2 strings */
7221 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007222 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007223 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007224 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007225 LY_CHECK_RET(rc);
7226 }
7227
7228 assert(set1->type == set2->type);
7229
7230 /* compute result */
7231 if (op[0] == '=') {
7232 if (set1->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +02007233 result = (set1->val.bln == set2->val.bln);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007234 } else if (set1->type == LYXP_SET_NUMBER) {
7235 result = (set1->val.num == set2->val.num);
7236 } else {
7237 assert(set1->type == LYXP_SET_STRING);
Michal Vaskoac6c72f2019-11-14 16:09:34 +01007238 result = !strcmp(set1->val.str, set2->val.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007239 }
7240 } else if (op[0] == '!') {
7241 if (set1->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +02007242 result = (set1->val.bln != set2->val.bln);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007243 } else if (set1->type == LYXP_SET_NUMBER) {
7244 result = (set1->val.num != set2->val.num);
7245 } else {
7246 assert(set1->type == LYXP_SET_STRING);
Michal Vaskoc2058432020-11-06 17:26:21 +01007247 result = strcmp(set1->val.str, set2->val.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007248 }
7249 } else {
7250 assert(set1->type == LYXP_SET_NUMBER);
7251 if (op[0] == '<') {
7252 if (op[1] == '=') {
7253 result = (set1->val.num <= set2->val.num);
7254 } else {
7255 result = (set1->val.num < set2->val.num);
7256 }
7257 } else {
7258 if (op[1] == '=') {
7259 result = (set1->val.num >= set2->val.num);
7260 } else {
7261 result = (set1->val.num > set2->val.num);
7262 }
7263 }
7264 }
7265
7266 /* assign result */
7267 if (result) {
7268 set_fill_boolean(set1, 1);
7269 } else {
7270 set_fill_boolean(set1, 0);
7271 }
7272
7273 return LY_SUCCESS;
7274}
7275
7276/**
7277 * @brief Move context @p set to the result of a basic operation. Handles '+', '-', unary '-', '*', 'div',
7278 * or 'mod'. Result is LYXP_SET_NUMBER. Indirectly context position aware.
7279 *
7280 * @param[in,out] set1 Set to use for the result.
7281 * @param[in] set2 Set acting as the second operand for @p op.
7282 * @param[in] op Operator to process.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007283 * @return LY_ERR
7284 */
7285static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007286moveto_op_math(struct lyxp_set *set1, struct lyxp_set *set2, const char *op)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007287{
7288 LY_ERR rc;
7289
7290 /* unary '-' */
7291 if (!set2 && (op[0] == '-')) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007292 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007293 LY_CHECK_RET(rc);
7294 set1->val.num *= -1;
7295 lyxp_set_free(set2);
7296 return LY_SUCCESS;
7297 }
7298
7299 assert(set1 && set2);
7300
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007301 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007302 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007303 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007304 LY_CHECK_RET(rc);
7305
7306 switch (op[0]) {
7307 /* '+' */
7308 case '+':
7309 set1->val.num += set2->val.num;
7310 break;
7311
7312 /* '-' */
7313 case '-':
7314 set1->val.num -= set2->val.num;
7315 break;
7316
7317 /* '*' */
7318 case '*':
7319 set1->val.num *= set2->val.num;
7320 break;
7321
7322 /* 'div' */
7323 case 'd':
7324 set1->val.num /= set2->val.num;
7325 break;
7326
7327 /* 'mod' */
7328 case 'm':
7329 set1->val.num = ((long long)set1->val.num) % ((long long)set2->val.num);
7330 break;
7331
7332 default:
7333 LOGINT_RET(set1->ctx);
7334 }
7335
7336 return LY_SUCCESS;
7337}
7338
Michal Vasko03ff5a72019-09-11 13:49:33 +02007339/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02007340 * @brief Evaluate Predicate. Logs directly on error.
7341 *
Michal Vaskod3678892020-05-21 10:06:58 +02007342 * [9] Predicate ::= '[' Expr ']'
Michal Vasko03ff5a72019-09-11 13:49:33 +02007343 *
7344 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007345 * @param[in] tok_idx Position in the expression @p exp.
aPiecek8b0cc152021-05-31 16:40:31 +02007346 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007347 * @param[in] options XPath options.
Michal Vasko49fec8e2022-05-24 10:28:33 +02007348 * @param[in] axis Axis to search on.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007349 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7350 */
7351static LY_ERR
Michal Vasko49fec8e2022-05-24 10:28:33 +02007352eval_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 +02007353{
7354 LY_ERR rc;
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01007355 uint16_t orig_exp;
7356 uint32_t i, orig_pos, orig_size;
Michal Vasko5c4e5892019-11-14 12:31:38 +01007357 int32_t pred_in_ctx;
Michal Vasko88a9e802022-05-24 10:50:28 +02007358 ly_bool reverse_axis = 0;
aPiecekfff4dca2021-10-07 10:59:53 +02007359 struct lyxp_set set2 = {0};
Michal Vasko03ff5a72019-09-11 13:49:33 +02007360
7361 /* '[' */
aPiecek8b0cc152021-05-31 16:40:31 +02007362 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02007363 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007364 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007365
aPiecek8b0cc152021-05-31 16:40:31 +02007366 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007367only_parse:
aPiecek8b0cc152021-05-31 16:40:31 +02007368 rc = eval_expr_select(exp, tok_idx, 0, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007369 LY_CHECK_RET(rc);
7370 } else if (set->type == LYXP_SET_NODE_SET) {
7371 /* 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 +01007372 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007373
7374 /* empty set, nothing to evaluate */
7375 if (!set->used) {
7376 goto only_parse;
7377 }
7378
Michal Vasko49fec8e2022-05-24 10:28:33 +02007379 /* decide forward or reverse axis */
7380 switch (axis) {
7381 case LYXP_AXIS_ANCESTOR:
7382 case LYXP_AXIS_ANCESTOR_OR_SELF:
7383 case LYXP_AXIS_PRECEDING:
7384 case LYXP_AXIS_PRECEDING_SIBLING:
7385 reverse_axis = 1;
7386 break;
7387 case LYXP_AXIS_DESCENDANT:
7388 case LYXP_AXIS_DESCENDANT_OR_SELF:
7389 case LYXP_AXIS_FOLLOWING:
7390 case LYXP_AXIS_FOLLOWING_SIBLING:
7391 case LYXP_AXIS_PARENT:
7392 case LYXP_AXIS_CHILD:
7393 case LYXP_AXIS_SELF:
7394 case LYXP_AXIS_ATTRIBUTE:
7395 reverse_axis = 0;
7396 break;
7397 }
7398
Michal Vasko004d3152020-06-11 19:59:22 +02007399 orig_exp = *tok_idx;
Michal Vasko49fec8e2022-05-24 10:28:33 +02007400 orig_pos = reverse_axis ? set->used + 1 : 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007401 orig_size = set->used;
Michal Vasko39dbf352020-05-21 10:08:59 +02007402 for (i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007403 set_init(&set2, set);
7404 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 +02007405
7406 /* remember the node context position for position() and context size for last() */
7407 orig_pos += reverse_axis ? -1 : 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007408
7409 set2.ctx_pos = orig_pos;
7410 set2.ctx_size = orig_size;
Michal Vasko004d3152020-06-11 19:59:22 +02007411 *tok_idx = orig_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007412
Michal Vasko004d3152020-06-11 19:59:22 +02007413 rc = eval_expr_select(exp, tok_idx, 0, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007414 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02007415 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007416 return rc;
7417 }
7418
Michal Vasko49fec8e2022-05-24 10:28:33 +02007419 /* number is a proximity position */
Michal Vasko03ff5a72019-09-11 13:49:33 +02007420 if (set2.type == LYXP_SET_NUMBER) {
7421 if ((long long)set2.val.num == orig_pos) {
7422 set2.val.num = 1;
7423 } else {
7424 set2.val.num = 0;
7425 }
7426 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007427 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007428
7429 /* predicate satisfied or not? */
Michal Vasko004d3152020-06-11 19:59:22 +02007430 if (!set2.val.bln) {
Michal Vasko2caefc12019-11-14 16:07:56 +01007431 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007432 }
7433 }
Michal Vasko2caefc12019-11-14 16:07:56 +01007434 set_remove_nodes_none(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007435
7436 } else if (set->type == LYXP_SET_SCNODE_SET) {
7437 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01007438 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007439 /* there is a currently-valid node */
7440 break;
7441 }
7442 }
7443 /* empty set, nothing to evaluate */
7444 if (i == set->used) {
7445 goto only_parse;
7446 }
7447
Michal Vasko004d3152020-06-11 19:59:22 +02007448 orig_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007449
Michal Vasko03ff5a72019-09-11 13:49:33 +02007450 /* set special in_ctx to all the valid snodes */
7451 pred_in_ctx = set_scnode_new_in_ctx(set);
7452
7453 /* use the valid snodes one-by-one */
7454 for (i = 0; i < set->used; ++i) {
7455 if (set->val.scnodes[i].in_ctx != pred_in_ctx) {
7456 continue;
7457 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01007458 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007459
Michal Vasko004d3152020-06-11 19:59:22 +02007460 *tok_idx = orig_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007461
Michal Vasko004d3152020-06-11 19:59:22 +02007462 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007463 LY_CHECK_RET(rc);
7464
7465 set->val.scnodes[i].in_ctx = pred_in_ctx;
7466 }
7467
7468 /* restore the state as it was before the predicate */
7469 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01007470 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007471 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007472 } else if (set->val.scnodes[i].in_ctx == pred_in_ctx) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01007473 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007474 }
7475 }
7476
7477 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02007478 set2.type = LYXP_SET_NODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007479 set_fill_set(&set2, set);
7480
Michal Vasko004d3152020-06-11 19:59:22 +02007481 rc = eval_expr_select(exp, tok_idx, 0, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007482 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02007483 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007484 return rc;
7485 }
7486
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007487 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +02007488 if (!set2.val.bln) {
Michal Vaskod3678892020-05-21 10:06:58 +02007489 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007490 }
Michal Vaskod3678892020-05-21 10:06:58 +02007491 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007492 }
7493
7494 /* ']' */
Michal Vasko004d3152020-06-11 19:59:22 +02007495 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_BRACK2);
aPiecek8b0cc152021-05-31 16:40:31 +02007496 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02007497 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007498 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007499
7500 return LY_SUCCESS;
7501}
7502
7503/**
Michal Vaskod3678892020-05-21 10:06:58 +02007504 * @brief Evaluate Literal. Logs directly on error.
7505 *
7506 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007507 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02007508 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7509 */
7510static void
Michal Vasko40308e72020-10-20 16:38:40 +02007511eval_literal(const struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set)
Michal Vaskod3678892020-05-21 10:06:58 +02007512{
7513 if (set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007514 if (exp->tok_len[*tok_idx] == 2) {
Michal Vaskod3678892020-05-21 10:06:58 +02007515 set_fill_string(set, "", 0);
7516 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007517 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 +02007518 }
7519 }
7520 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02007521 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007522 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007523}
7524
7525/**
Michal Vasko004d3152020-06-11 19:59:22 +02007526 * @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 +02007527 *
Michal Vasko004d3152020-06-11 19:59:22 +02007528 * @param[in] exp Full parsed XPath expression.
7529 * @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 +02007530 * @param[in] ctx_node Found schema node as the context for the predicate.
7531 * @param[in] cur_mod Current module for the expression.
7532 * @param[in] cur_node Current (original context) node.
Michal Vasko004d3152020-06-11 19:59:22 +02007533 * @param[in] format Format of any prefixes in key names/values.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007534 * @param[in] prefix_data Format-specific prefix data (see ::ly_resolve_prefix).
Michal Vasko004d3152020-06-11 19:59:22 +02007535 * @param[out] predicates Parsed predicates.
7536 * @param[out] pred_type Type of @p predicates.
7537 * @return LY_SUCCESS on success,
7538 * @return LY_ERR on any error.
Michal Vaskod3678892020-05-21 10:06:58 +02007539 */
7540static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007541eval_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 +02007542 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 +02007543 struct ly_path_predicate **predicates, enum ly_path_pred_type *pred_type)
Michal Vaskod3678892020-05-21 10:06:58 +02007544{
Michal Vasko004d3152020-06-11 19:59:22 +02007545 LY_ERR ret = LY_SUCCESS;
7546 uint16_t key_count, e_idx, pred_idx = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02007547 const struct lysc_node *key;
Michal Vasko004d3152020-06-11 19:59:22 +02007548 size_t pred_len;
Radek Krejci1deb5be2020-08-26 16:43:36 +02007549 uint32_t prev_lo;
Michal Vasko004d3152020-06-11 19:59:22 +02007550 struct lyxp_expr *exp2 = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02007551
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007552 assert(ctx_node->nodetype & (LYS_LIST | LYS_LEAFLIST));
Michal Vaskod3678892020-05-21 10:06:58 +02007553
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007554 if (ctx_node->nodetype == LYS_LIST) {
Michal Vasko004d3152020-06-11 19:59:22 +02007555 /* get key count */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007556 if (ctx_node->flags & LYS_KEYLESS) {
Michal Vasko004d3152020-06-11 19:59:22 +02007557 return LY_EINVAL;
7558 }
Michal Vasko544e58a2021-01-28 14:33:41 +01007559 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 +02007560 assert(key_count);
Michal Vaskod3678892020-05-21 10:06:58 +02007561
Michal Vasko004d3152020-06-11 19:59:22 +02007562 /* learn where the predicates end */
7563 e_idx = *tok_idx;
7564 while (key_count) {
7565 /* '[' */
7566 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
7567 return LY_EINVAL;
7568 }
7569 ++e_idx;
7570
Michal Vasko3354d272021-04-06 09:40:06 +02007571 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_NAMETEST)) {
7572 /* definitely not a key */
7573 return LY_EINVAL;
7574 }
7575
Michal Vasko004d3152020-06-11 19:59:22 +02007576 /* ']' */
7577 while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) {
7578 ++e_idx;
7579 }
7580 ++e_idx;
7581
7582 /* another presumably key predicate parsed */
7583 --key_count;
7584 }
Michal Vasko004d3152020-06-11 19:59:22 +02007585 } else {
7586 /* learn just where this single predicate ends */
7587 e_idx = *tok_idx;
7588
Michal Vaskod3678892020-05-21 10:06:58 +02007589 /* '[' */
Michal Vasko004d3152020-06-11 19:59:22 +02007590 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
7591 return LY_EINVAL;
7592 }
7593 ++e_idx;
Michal Vaskod3678892020-05-21 10:06:58 +02007594
Michal Vasko3354d272021-04-06 09:40:06 +02007595 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_DOT)) {
7596 /* definitely not the value */
7597 return LY_EINVAL;
7598 }
7599
Michal Vaskod3678892020-05-21 10:06:58 +02007600 /* ']' */
Michal Vasko004d3152020-06-11 19:59:22 +02007601 while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) {
7602 ++e_idx;
7603 }
7604 ++e_idx;
Michal Vaskod3678892020-05-21 10:06:58 +02007605 }
7606
Michal Vasko004d3152020-06-11 19:59:22 +02007607 /* get the joined length of all the keys predicates/of the single leaf-list predicate */
7608 pred_len = (exp->tok_pos[e_idx - 1] + exp->tok_len[e_idx - 1]) - exp->tok_pos[*tok_idx];
7609
7610 /* turn logging off */
7611 prev_lo = ly_log_options(0);
7612
7613 /* parse the predicate(s) */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007614 LY_CHECK_GOTO(ret = ly_path_parse_predicate(ctx_node->module->ctx, ctx_node, exp->expr + exp->tok_pos[*tok_idx],
7615 pred_len, LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &exp2), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02007616
7617 /* compile */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007618 ret = ly_path_compile_predicate(ctx_node->module->ctx, cur_node, cur_mod, ctx_node, exp2, &pred_idx, format,
7619 prefix_data, predicates, pred_type);
Michal Vasko004d3152020-06-11 19:59:22 +02007620 LY_CHECK_GOTO(ret, cleanup);
7621
7622 /* success, the predicate must include all the needed information for hash-based search */
7623 *tok_idx = e_idx;
7624
7625cleanup:
7626 ly_log_options(prev_lo);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007627 lyxp_expr_free(ctx_node->module->ctx, exp2);
Michal Vasko004d3152020-06-11 19:59:22 +02007628 return ret;
Michal Vaskod3678892020-05-21 10:06:58 +02007629}
7630
7631/**
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007632 * @brief Search for/check the next schema node that could be the only matching schema node meaning the
7633 * data node(s) could be found using a single hash-based search.
7634 *
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007635 * @param[in] ctx libyang context.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007636 * @param[in] node Next context node to check.
7637 * @param[in] name Expected node name.
7638 * @param[in] name_len Length of @p name.
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007639 * @param[in] moveto_mod Expected node module, can be NULL for JSON format with no prefix.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007640 * @param[in] root_type XPath root type.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007641 * @param[in] format Prefix format.
7642 * @param[in,out] found Previously found node, is updated.
7643 * @return LY_SUCCESS on success,
7644 * @return LY_ENOT if the whole check failed and hashes cannot be used.
7645 */
7646static LY_ERR
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007647eval_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 +02007648 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 +02007649 const struct lysc_node **found)
7650{
7651 const struct lysc_node *scnode;
7652 const struct lys_module *mod;
7653 uint32_t idx = 0;
7654
Radek Krejci8df109d2021-04-23 12:19:08 +02007655 assert((format == LY_VALUE_JSON) || moveto_mod);
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007656
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007657continue_search:
Michal Vasko7d1d0912020-10-16 08:38:30 +02007658 scnode = NULL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007659 if (!node) {
Radek Krejci8df109d2021-04-23 12:19:08 +02007660 if ((format == LY_VALUE_JSON) && !moveto_mod) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007661 /* search all modules for a single match */
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007662 while ((mod = ly_ctx_get_module_iter(ctx, &idx))) {
Michal Vasko35a3b1d2021-07-14 09:34:16 +02007663 if (!mod->implemented) {
7664 continue;
7665 }
7666
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007667 scnode = lys_find_child(NULL, mod, name, name_len, 0, 0);
7668 if (scnode) {
7669 /* we have found a match */
7670 break;
7671 }
7672 }
7673
Michal Vasko7d1d0912020-10-16 08:38:30 +02007674 if (!scnode) {
7675 /* all modules searched */
7676 idx = 0;
7677 }
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007678 } else {
7679 /* search in top-level */
7680 scnode = lys_find_child(NULL, moveto_mod, name, name_len, 0, 0);
7681 }
7682 } else if (!*found || (lysc_data_parent(*found) != node->schema)) {
Radek Krejci8df109d2021-04-23 12:19:08 +02007683 if ((format == LY_VALUE_JSON) && !moveto_mod) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007684 /* we must adjust the module to inherit the one from the context node */
7685 moveto_mod = node->schema->module;
7686 }
7687
7688 /* search in children, do not repeat the same search */
7689 scnode = lys_find_child(node->schema, moveto_mod, name, name_len, 0, 0);
Michal Vasko7d1d0912020-10-16 08:38:30 +02007690 } /* else skip redundant search */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007691
7692 /* additional context check */
7693 if (scnode && (root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) {
7694 scnode = NULL;
7695 }
7696
7697 if (scnode) {
7698 if (*found) {
7699 /* we found a schema node with the same name but at different level, give up, too complicated
7700 * (more hash-based searches would be required, not supported) */
7701 return LY_ENOT;
7702 } else {
7703 /* remember the found schema node and continue to make sure it can be used */
7704 *found = scnode;
7705 }
7706 }
7707
7708 if (idx) {
7709 /* continue searching all the following models */
7710 goto continue_search;
7711 }
7712
7713 return LY_SUCCESS;
7714}
7715
7716/**
Michal Vasko4ad69e72021-10-26 16:25:55 +02007717 * @brief Generate message when no matching schema nodes were found for a path segment.
7718 *
7719 * @param[in] set XPath set.
7720 * @param[in] scparent Previous schema parent in the context, if only one.
7721 * @param[in] ncname XPath NCName being evaluated.
7722 * @param[in] ncname_len Length of @p ncname.
7723 * @param[in] expr Whole XPath expression.
7724 * @param[in] options XPath options.
7725 */
7726static void
7727eval_name_test_scnode_no_match_msg(struct lyxp_set *set, const struct lyxp_set_scnode *scparent, const char *ncname,
7728 uint16_t ncname_len, const char *expr, uint32_t options)
7729{
7730 const char *format;
7731 char *path = NULL, *ppath = NULL;
7732
7733 path = lysc_path(set->cur_scnode, LYSC_PATH_LOG, NULL, 0);
7734 if (scparent) {
7735 /* generate path for the parent */
7736 if (scparent->type == LYXP_NODE_ELEM) {
7737 ppath = lysc_path(scparent->scnode, LYSC_PATH_LOG, NULL, 0);
7738 } else if (scparent->type == LYXP_NODE_ROOT) {
7739 ppath = strdup("<root>");
7740 } else if (scparent->type == LYXP_NODE_ROOT_CONFIG) {
7741 ppath = strdup("<config-root>");
7742 }
7743 }
7744 if (ppath) {
7745 format = "Schema node \"%.*s\" for parent \"%s\" not found; in expr \"%.*s\" with context node \"%s\".";
7746 if (options & LYXP_SCNODE_ERROR) {
7747 LOGERR(set->ctx, LY_EVALID, format, ncname_len, ncname, ppath, (ncname - expr) + ncname_len, expr, path);
7748 } else {
7749 LOGWRN(set->ctx, format, ncname_len, ncname, ppath, (ncname - expr) + ncname_len, expr, path);
7750 }
7751 } else {
7752 format = "Schema node \"%.*s\" not found; in expr \"%.*s\" with context node \"%s\".";
7753 if (options & LYXP_SCNODE_ERROR) {
7754 LOGERR(set->ctx, LY_EVALID, format, ncname_len, ncname, (ncname - expr) + ncname_len, expr, path);
7755 } else {
7756 LOGWRN(set->ctx, format, ncname_len, ncname, (ncname - expr) + ncname_len, expr, path);
7757 }
7758 }
7759 free(path);
7760 free(ppath);
7761}
7762
7763/**
Michal Vaskod3678892020-05-21 10:06:58 +02007764 * @brief Evaluate NameTest and any following Predicates. Logs directly on error.
7765 *
7766 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
7767 * [6] NodeTest ::= NameTest | NodeType '(' ')'
7768 * [7] NameTest ::= '*' | NCName ':' '*' | QName
7769 *
7770 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007771 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko49fec8e2022-05-24 10:28:33 +02007772 * @param[in] axis What axis to search on.
Michal Vaskod3678892020-05-21 10:06:58 +02007773 * @param[in] all_desc Whether to search all the descendants or children only.
aPiecek8b0cc152021-05-31 16:40:31 +02007774 * @param[in,out] set Context and result set.
Michal Vaskod3678892020-05-21 10:06:58 +02007775 * @param[in] options XPath options.
Michal Vaskoa036a6b2021-10-12 16:05:23 +02007776 * @return LY_ERR (LY_EINCOMPLETE on unresolved when, LY_ENOT for not found schema node)
Michal Vaskod3678892020-05-21 10:06:58 +02007777 */
7778static LY_ERR
Michal Vasko49fec8e2022-05-24 10:28:33 +02007779eval_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 +02007780 struct lyxp_set *set, uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02007781{
Michal Vaskoa036a6b2021-10-12 16:05:23 +02007782 LY_ERR rc = LY_SUCCESS, r;
Michal Vasko004d3152020-06-11 19:59:22 +02007783 const char *ncname, *ncname_dict = NULL;
7784 uint16_t ncname_len;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007785 const struct lys_module *moveto_mod = NULL;
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007786 const struct lysc_node *scnode = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02007787 struct ly_path_predicate *predicates = NULL;
7788 enum ly_path_pred_type pred_type = 0;
Michal Vaskoa036a6b2021-10-12 16:05:23 +02007789 int scnode_skip_pred = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02007790
aPiecek8b0cc152021-05-31 16:40:31 +02007791 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02007792 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007793 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007794
aPiecek8b0cc152021-05-31 16:40:31 +02007795 if (options & LYXP_SKIP_EXPR) {
Michal Vaskod3678892020-05-21 10:06:58 +02007796 goto moveto;
7797 }
7798
Michal Vasko004d3152020-06-11 19:59:22 +02007799 ncname = &exp->expr[exp->tok_pos[*tok_idx - 1]];
7800 ncname_len = exp->tok_len[*tok_idx - 1];
Michal Vaskod3678892020-05-21 10:06:58 +02007801
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007802 if ((ncname[0] == '*') && (ncname_len == 1)) {
7803 /* all nodes will match */
7804 goto moveto;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007805 }
7806
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007807 /* parse (and skip) module name */
7808 rc = moveto_resolve_model(&ncname, &ncname_len, set, NULL, &moveto_mod);
Michal Vaskod3678892020-05-21 10:06:58 +02007809 LY_CHECK_GOTO(rc, cleanup);
7810
Michal Vasko49fec8e2022-05-24 10:28:33 +02007811 if ((ncname[0] == '*') && (ncname_len == 1)) {
7812 /* all nodes from the module will match */
7813 goto moveto;
7814 }
7815
7816 if (((set->format == LY_VALUE_JSON) || moveto_mod) && (axis == LYXP_AXIS_CHILD) && !all_desc &&
7817 (set->type == LYXP_SET_NODE_SET)) {
Michal Vaskod3678892020-05-21 10:06:58 +02007818 /* find the matching schema node in some parent in the context */
Radek Krejci1deb5be2020-08-26 16:43:36 +02007819 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007820 if (eval_name_test_with_predicate_get_scnode(set->ctx, set->val.nodes[i].node, ncname, ncname_len,
7821 moveto_mod, set->root_type, set->format, &scnode)) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007822 /* check failed */
7823 scnode = NULL;
7824 break;
Michal Vaskod3678892020-05-21 10:06:58 +02007825 }
7826 }
7827
Michal Vasko004d3152020-06-11 19:59:22 +02007828 if (scnode && (scnode->nodetype & (LYS_LIST | LYS_LEAFLIST))) {
7829 /* try to create the predicates */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007830 if (eval_name_test_try_compile_predicates(exp, tok_idx, scnode, set->cur_mod, set->cur_node ?
7831 set->cur_node->schema : NULL, set->format, set->prefix_data, &predicates, &pred_type)) {
Michal Vasko004d3152020-06-11 19:59:22 +02007832 /* hashes cannot be used */
Michal Vaskod3678892020-05-21 10:06:58 +02007833 scnode = NULL;
7834 }
7835 }
7836 }
7837
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007838 if (!scnode) {
7839 /* we are not able to match based on a schema node and not all the modules match ("*"),
Michal Vasko004d3152020-06-11 19:59:22 +02007840 * use dictionary for efficient comparison */
Radek Krejci011e4aa2020-09-04 15:22:31 +02007841 LY_CHECK_GOTO(rc = lydict_insert(set->ctx, ncname, ncname_len, &ncname_dict), cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02007842 }
7843
7844moveto:
7845 /* move to the attribute(s), data node(s), or schema node(s) */
Michal Vasko49fec8e2022-05-24 10:28:33 +02007846 if (axis == LYXP_AXIS_ATTRIBUTE) {
aPiecek8b0cc152021-05-31 16:40:31 +02007847 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007848 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskod3678892020-05-21 10:06:58 +02007849 } else {
7850 if (all_desc) {
Michal Vaskocdad7122020-11-09 21:04:44 +01007851 rc = moveto_attr_alldesc(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007852 } else {
aPiecek8b0cc152021-05-31 16:40:31 +02007853 rc = moveto_attr(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007854 }
7855 LY_CHECK_GOTO(rc, cleanup);
7856 }
7857 } else {
aPiecek8b0cc152021-05-31 16:40:31 +02007858 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02007859 int64_t i;
Michal Vaskoa036a6b2021-10-12 16:05:23 +02007860 const struct lyxp_set_scnode *scparent = NULL;
Michal Vaskoa036a6b2021-10-12 16:05:23 +02007861
7862 /* remember parent if there is only one, to print in the warning */
7863 for (i = 0; i < set->used; ++i) {
7864 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
7865 if (!scparent) {
7866 /* remember the context node */
7867 scparent = &set->val.scnodes[i];
7868 } else {
7869 /* several context nodes, no reasonable error possible */
7870 scparent = NULL;
7871 break;
7872 }
7873 }
7874 }
Radek Krejci1deb5be2020-08-26 16:43:36 +02007875
Michal Vasko49fec8e2022-05-24 10:28:33 +02007876 if (all_desc && (axis == LYXP_AXIS_CHILD)) {
7877 /* efficient evaluation that does not add all the descendants into the set */
7878 rc = moveto_scnode_alldesc_child(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007879 } else {
Michal Vasko49fec8e2022-05-24 10:28:33 +02007880 if (all_desc) {
7881 /* "//" == "/descendant-or-self::node()/" */
7882 rc = xpath_pi_node(set, LYXP_AXIS_DESCENDANT_OR_SELF, options);
7883 LY_CHECK_GOTO(rc, cleanup);
7884 }
7885 rc = moveto_scnode(set, moveto_mod, ncname_dict, axis, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007886 }
7887 LY_CHECK_GOTO(rc, cleanup);
7888
7889 for (i = set->used - 1; i > -1; --i) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007890 if (set->val.scnodes[i].in_ctx > LYXP_SET_SCNODE_ATOM_NODE) {
Michal Vaskod3678892020-05-21 10:06:58 +02007891 break;
7892 }
7893 }
7894 if (i == -1) {
Michal Vasko4ad69e72021-10-26 16:25:55 +02007895 /* generate message */
7896 eval_name_test_scnode_no_match_msg(set, scparent, ncname, ncname_len, exp->expr, options);
7897
7898 if (options & LYXP_SCNODE_ERROR) {
7899 /* error */
7900 rc = LY_EVALID;
7901 goto cleanup;
Michal Vaskoa036a6b2021-10-12 16:05:23 +02007902 }
Michal Vaskoa036a6b2021-10-12 16:05:23 +02007903
7904 /* skip the predicates and the rest of this path to not generate invalid warnings */
7905 rc = LY_ENOT;
7906 scnode_skip_pred = 1;
Michal Vaskod3678892020-05-21 10:06:58 +02007907 }
7908 } else {
Michal Vasko49fec8e2022-05-24 10:28:33 +02007909 if (all_desc && (axis == LYXP_AXIS_CHILD)) {
7910 /* efficient evaluation */
7911 rc = moveto_node_alldesc_child(set, moveto_mod, ncname_dict, options);
7912 } else if (scnode && (axis == LYXP_AXIS_CHILD)) {
7913 /* we can find the child nodes using hashes */
7914 rc = moveto_node_hash_child(set, scnode, predicates, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007915 } else {
Michal Vasko49fec8e2022-05-24 10:28:33 +02007916 if (all_desc) {
7917 /* "//" == "/descendant-or-self::node()/" */
7918 rc = xpath_pi_node(set, LYXP_AXIS_DESCENDANT_OR_SELF, options);
7919 LY_CHECK_GOTO(rc, cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02007920 }
Michal Vasko49fec8e2022-05-24 10:28:33 +02007921 rc = moveto_node(set, moveto_mod, ncname_dict, axis, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007922 }
7923 LY_CHECK_GOTO(rc, cleanup);
7924 }
7925 }
7926
Michal Vaskoa036a6b2021-10-12 16:05:23 +02007927 if (scnode_skip_pred) {
7928 /* skip predicates */
7929 options |= LYXP_SKIP_EXPR;
7930 }
7931
Michal Vaskod3678892020-05-21 10:06:58 +02007932 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007933 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02007934 r = eval_predicate(exp, tok_idx, set, options, axis);
Michal Vaskoa036a6b2021-10-12 16:05:23 +02007935 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02007936 }
7937
7938cleanup:
Michal Vaskoa036a6b2021-10-12 16:05:23 +02007939 if (scnode_skip_pred) {
7940 /* restore options */
7941 options &= ~LYXP_SKIP_EXPR;
7942 }
aPiecek8b0cc152021-05-31 16:40:31 +02007943 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko004d3152020-06-11 19:59:22 +02007944 lydict_remove(set->ctx, ncname_dict);
Michal Vaskof7e16e22020-10-21 09:24:39 +02007945 ly_path_predicates_free(set->ctx, pred_type, predicates);
Michal Vaskodb51a8d2020-05-27 15:22:29 +02007946 }
Michal Vaskod3678892020-05-21 10:06:58 +02007947 return rc;
7948}
7949
7950/**
7951 * @brief Evaluate NodeType and any following Predicates. Logs directly on error.
7952 *
7953 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
7954 * [6] NodeTest ::= NameTest | NodeType '(' ')'
7955 * [8] NodeType ::= 'text' | 'node'
7956 *
7957 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007958 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko49fec8e2022-05-24 10:28:33 +02007959 * @param[in] axis Axis to search on.
7960 * @param[in] all_desc Whether to search all the descendants or axis only.
aPiecek8b0cc152021-05-31 16:40:31 +02007961 * @param[in,out] set Context and result set.
Michal Vaskod3678892020-05-21 10:06:58 +02007962 * @param[in] options XPath options.
7963 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7964 */
7965static LY_ERR
Michal Vasko49fec8e2022-05-24 10:28:33 +02007966eval_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 +02007967 struct lyxp_set *set, uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02007968{
7969 LY_ERR rc;
7970
Michal Vaskod3678892020-05-21 10:06:58 +02007971 (void)all_desc;
7972
aPiecek8b0cc152021-05-31 16:40:31 +02007973 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko004d3152020-06-11 19:59:22 +02007974 assert(exp->tok_len[*tok_idx] == 4);
Michal Vasko49fec8e2022-05-24 10:28:33 +02007975 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "node", 4)) {
7976 rc = xpath_pi_node(set, axis, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007977 } else {
Michal Vasko49fec8e2022-05-24 10:28:33 +02007978 assert(!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "text", 4));
7979 rc = xpath_pi_text(set, axis, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007980 }
Michal Vasko49fec8e2022-05-24 10:28:33 +02007981 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02007982 }
aPiecek8b0cc152021-05-31 16:40:31 +02007983 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02007984 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007985 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007986
7987 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02007988 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
aPiecek8b0cc152021-05-31 16:40:31 +02007989 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02007990 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007991 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007992
7993 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007994 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
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 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02008000 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02008001 rc = eval_predicate(exp, tok_idx, set, options, axis);
Michal Vaskod3678892020-05-21 10:06:58 +02008002 LY_CHECK_RET(rc);
8003 }
8004
8005 return LY_SUCCESS;
8006}
8007
8008/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02008009 * @brief Evaluate RelativeLocationPath. Logs directly on error.
8010 *
8011 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
8012 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
Michal Vaskod3678892020-05-21 10:06:58 +02008013 * [6] NodeTest ::= NameTest | NodeType '(' ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02008014 *
8015 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008016 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008017 * @param[in] all_desc Whether to search all the descendants or children only.
aPiecek8b0cc152021-05-31 16:40:31 +02008018 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008019 * @param[in] options XPath options.
8020 * @return LY_ERR (YL_EINCOMPLETE on unresolved when)
8021 */
8022static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008023eval_relative_location_path(const struct lyxp_expr *exp, uint16_t *tok_idx, ly_bool all_desc, struct lyxp_set *set,
8024 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008025{
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008026 LY_ERR rc = LY_SUCCESS;
Michal Vasko49fec8e2022-05-24 10:28:33 +02008027 enum lyxp_axis axis;
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008028 int scnode_skip_path = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008029
8030 goto step;
8031 do {
8032 /* evaluate '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02008033 if (exp->tok_len[*tok_idx] == 1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008034 all_desc = 0;
8035 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02008036 assert(exp->tok_len[*tok_idx] == 2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008037 all_desc = 1;
8038 }
aPiecek8b0cc152021-05-31 16:40:31 +02008039 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008040 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008041 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008042
8043step:
Michal Vasko49fec8e2022-05-24 10:28:33 +02008044 /* AxisSpecifier */
8045 if (exp->tokens[*tok_idx] == LYXP_TOKEN_AXISNAME) {
8046 axis = str2axis(exp->expr + exp->tok_pos[*tok_idx], exp->tok_len[*tok_idx]);
Michal Vaskod3678892020-05-21 10:06:58 +02008047
aPiecek8b0cc152021-05-31 16:40:31 +02008048 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008049 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
8050 ++(*tok_idx);
8051
8052 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_DCOLON);
8053 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
8054 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
8055 ++(*tok_idx);
8056 } else if (exp->tokens[*tok_idx] == LYXP_TOKEN_AT) {
8057 axis = LYXP_AXIS_ATTRIBUTE;
8058
8059 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
8060 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008061 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02008062 } else {
Michal Vasko49fec8e2022-05-24 10:28:33 +02008063 /* default */
8064 axis = LYXP_AXIS_CHILD;
Michal Vaskod3678892020-05-21 10:06:58 +02008065 }
8066
Michal Vasko49fec8e2022-05-24 10:28:33 +02008067 /* NodeTest Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02008068 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008069 case LYXP_TOKEN_DOT:
8070 /* evaluate '.' */
Michal Vasko49fec8e2022-05-24 10:28:33 +02008071 if (!(options & LYXP_SKIP_EXPR)) {
8072 if (((options & LYXP_SCNODE_ALL) && (set->type != LYXP_SET_SCNODE_SET)) ||
8073 (!(options & LYXP_SCNODE_ALL) && (set->type != LYXP_SET_NODE_SET))) {
8074 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
8075 rc = LY_EVALID;
8076 goto cleanup;
8077 }
8078
8079 if (all_desc) {
8080 rc = xpath_pi_node(set, LYXP_AXIS_DESCENDANT_OR_SELF, options);
8081 LY_CHECK_GOTO(rc, cleanup);
8082 }
8083 rc = xpath_pi_node(set, LYXP_AXIS_SELF, options);
8084 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008085 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008086 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008087 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008088 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008089 break;
8090
8091 case LYXP_TOKEN_DDOT:
8092 /* evaluate '..' */
Michal Vasko49fec8e2022-05-24 10:28:33 +02008093 if (!(options & LYXP_SKIP_EXPR)) {
8094 if (((options & LYXP_SCNODE_ALL) && (set->type != LYXP_SET_SCNODE_SET)) ||
8095 (!(options & LYXP_SCNODE_ALL) && (set->type != LYXP_SET_NODE_SET))) {
8096 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
8097 rc = LY_EVALID;
8098 goto cleanup;
8099 }
8100
8101 if (all_desc) {
8102 rc = xpath_pi_node(set, LYXP_AXIS_DESCENDANT_OR_SELF, options);
8103 LY_CHECK_GOTO(rc, cleanup);
8104 }
8105 rc = xpath_pi_node(set, LYXP_AXIS_PARENT, options);
8106 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008107 }
aPiecek8b0cc152021-05-31 16:40:31 +02008108 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008109 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008110 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008111 break;
8112
Michal Vasko03ff5a72019-09-11 13:49:33 +02008113 case LYXP_TOKEN_NAMETEST:
Michal Vaskod3678892020-05-21 10:06:58 +02008114 /* evaluate NameTest Predicate* */
Michal Vasko49fec8e2022-05-24 10:28:33 +02008115 rc = eval_name_test_with_predicate(exp, tok_idx, axis, all_desc, set, options);
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008116 if (rc == LY_ENOT) {
8117 assert(options & LYXP_SCNODE_ALL);
8118 /* skip the rest of this path */
8119 rc = LY_SUCCESS;
8120 scnode_skip_path = 1;
8121 options |= LYXP_SKIP_EXPR;
8122 }
8123 LY_CHECK_GOTO(rc, cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02008124 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008125
Michal Vaskod3678892020-05-21 10:06:58 +02008126 case LYXP_TOKEN_NODETYPE:
8127 /* evaluate NodeType Predicate* */
Michal Vasko49fec8e2022-05-24 10:28:33 +02008128 rc = eval_node_type_with_predicate(exp, tok_idx, axis, all_desc, set, options);
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008129 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008130 break;
8131
8132 default:
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008133 LOGINT(set->ctx);
8134 rc = LY_EINT;
8135 goto cleanup;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008136 }
Michal Vasko004d3152020-06-11 19:59:22 +02008137 } while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008138
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008139cleanup:
8140 if (scnode_skip_path) {
8141 options &= ~LYXP_SKIP_EXPR;
8142 }
8143 return rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008144}
8145
8146/**
8147 * @brief Evaluate AbsoluteLocationPath. Logs directly on error.
8148 *
8149 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
8150 *
8151 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008152 * @param[in] tok_idx Position in the expression @p exp.
aPiecek8b0cc152021-05-31 16:40:31 +02008153 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008154 * @param[in] options XPath options.
8155 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8156 */
8157static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008158eval_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 +02008159{
Radek Krejci857189e2020-09-01 13:26:36 +02008160 ly_bool all_desc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008161
aPiecek8b0cc152021-05-31 16:40:31 +02008162 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008163 /* no matter what tokens follow, we need to be at the root */
Michal Vaskob0099a92020-08-31 14:55:23 +02008164 LY_CHECK_RET(moveto_root(set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008165 }
8166
8167 /* '/' RelativeLocationPath? */
Michal Vasko004d3152020-06-11 19:59:22 +02008168 if (exp->tok_len[*tok_idx] == 1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008169 /* evaluate '/' - deferred */
8170 all_desc = 0;
aPiecek8b0cc152021-05-31 16:40:31 +02008171 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008172 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008173 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008174
Michal Vasko004d3152020-06-11 19:59:22 +02008175 if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008176 return LY_SUCCESS;
8177 }
Michal Vasko004d3152020-06-11 19:59:22 +02008178 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008179 case LYXP_TOKEN_DOT:
8180 case LYXP_TOKEN_DDOT:
Michal Vasko49fec8e2022-05-24 10:28:33 +02008181 case LYXP_TOKEN_AXISNAME:
Michal Vasko03ff5a72019-09-11 13:49:33 +02008182 case LYXP_TOKEN_AT:
8183 case LYXP_TOKEN_NAMETEST:
8184 case LYXP_TOKEN_NODETYPE:
Michal Vaskob0099a92020-08-31 14:55:23 +02008185 LY_CHECK_RET(eval_relative_location_path(exp, tok_idx, all_desc, set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008186 break;
8187 default:
8188 break;
8189 }
8190
Michal Vasko03ff5a72019-09-11 13:49:33 +02008191 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02008192 /* '//' RelativeLocationPath */
Michal Vasko03ff5a72019-09-11 13:49:33 +02008193 /* evaluate '//' - deferred so as not to waste memory by remembering all the nodes */
8194 all_desc = 1;
aPiecek8b0cc152021-05-31 16:40:31 +02008195 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008196 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008197 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008198
Michal Vaskob0099a92020-08-31 14:55:23 +02008199 LY_CHECK_RET(eval_relative_location_path(exp, tok_idx, all_desc, set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008200 }
8201
8202 return LY_SUCCESS;
8203}
8204
8205/**
8206 * @brief Evaluate FunctionCall. Logs directly on error.
8207 *
Michal Vaskod3678892020-05-21 10:06:58 +02008208 * [11] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02008209 *
8210 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008211 * @param[in] tok_idx Position in the expression @p exp.
aPiecek8b0cc152021-05-31 16:40:31 +02008212 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008213 * @param[in] options XPath options.
8214 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8215 */
8216static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008217eval_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 +02008218{
8219 LY_ERR rc;
Michal Vasko69730152020-10-09 16:30:07 +02008220
Radek Krejci1deb5be2020-08-26 16:43:36 +02008221 LY_ERR (*xpath_func)(struct lyxp_set **, uint16_t, struct lyxp_set *, uint32_t) = NULL;
Michal Vasko0cbf54f2019-12-16 10:01:06 +01008222 uint16_t arg_count = 0, i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008223 struct lyxp_set **args = NULL, **args_aux;
8224
aPiecek8b0cc152021-05-31 16:40:31 +02008225 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008226 /* FunctionName */
Michal Vasko004d3152020-06-11 19:59:22 +02008227 switch (exp->tok_len[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008228 case 3:
Michal Vasko004d3152020-06-11 19:59:22 +02008229 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008230 xpath_func = &xpath_not;
Michal Vasko004d3152020-06-11 19:59:22 +02008231 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008232 xpath_func = &xpath_sum;
8233 }
8234 break;
8235 case 4:
Michal Vasko004d3152020-06-11 19:59:22 +02008236 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008237 xpath_func = &xpath_lang;
Michal Vasko004d3152020-06-11 19:59:22 +02008238 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008239 xpath_func = &xpath_last;
Michal Vasko004d3152020-06-11 19:59:22 +02008240 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008241 xpath_func = &xpath_name;
Michal Vasko004d3152020-06-11 19:59:22 +02008242 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008243 xpath_func = &xpath_true;
8244 }
8245 break;
8246 case 5:
Michal Vasko004d3152020-06-11 19:59:22 +02008247 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008248 xpath_func = &xpath_count;
Michal Vasko004d3152020-06-11 19:59:22 +02008249 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008250 xpath_func = &xpath_false;
Michal Vasko004d3152020-06-11 19:59:22 +02008251 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008252 xpath_func = &xpath_floor;
Michal Vasko004d3152020-06-11 19:59:22 +02008253 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008254 xpath_func = &xpath_round;
Michal Vasko004d3152020-06-11 19:59:22 +02008255 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008256 xpath_func = &xpath_deref;
8257 }
8258 break;
8259 case 6:
Michal Vasko004d3152020-06-11 19:59:22 +02008260 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008261 xpath_func = &xpath_concat;
Michal Vasko004d3152020-06-11 19:59:22 +02008262 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008263 xpath_func = &xpath_number;
Michal Vasko004d3152020-06-11 19:59:22 +02008264 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008265 xpath_func = &xpath_string;
8266 }
8267 break;
8268 case 7:
Michal Vasko004d3152020-06-11 19:59:22 +02008269 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008270 xpath_func = &xpath_boolean;
Michal Vasko004d3152020-06-11 19:59:22 +02008271 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008272 xpath_func = &xpath_ceiling;
Michal Vasko004d3152020-06-11 19:59:22 +02008273 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008274 xpath_func = &xpath_current;
8275 }
8276 break;
8277 case 8:
Michal Vasko004d3152020-06-11 19:59:22 +02008278 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008279 xpath_func = &xpath_contains;
Michal Vasko004d3152020-06-11 19:59:22 +02008280 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008281 xpath_func = &xpath_position;
Michal Vasko004d3152020-06-11 19:59:22 +02008282 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "re-match", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008283 xpath_func = &xpath_re_match;
8284 }
8285 break;
8286 case 9:
Michal Vasko004d3152020-06-11 19:59:22 +02008287 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008288 xpath_func = &xpath_substring;
Michal Vasko004d3152020-06-11 19:59:22 +02008289 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008290 xpath_func = &xpath_translate;
8291 }
8292 break;
8293 case 10:
Michal Vasko004d3152020-06-11 19:59:22 +02008294 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008295 xpath_func = &xpath_local_name;
Michal Vasko004d3152020-06-11 19:59:22 +02008296 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008297 xpath_func = &xpath_enum_value;
Michal Vasko004d3152020-06-11 19:59:22 +02008298 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008299 xpath_func = &xpath_bit_is_set;
8300 }
8301 break;
8302 case 11:
Michal Vasko004d3152020-06-11 19:59:22 +02008303 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008304 xpath_func = &xpath_starts_with;
8305 }
8306 break;
8307 case 12:
Michal Vasko004d3152020-06-11 19:59:22 +02008308 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008309 xpath_func = &xpath_derived_from;
8310 }
8311 break;
8312 case 13:
Michal Vasko004d3152020-06-11 19:59:22 +02008313 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008314 xpath_func = &xpath_namespace_uri;
Michal Vasko004d3152020-06-11 19:59:22 +02008315 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string-length", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008316 xpath_func = &xpath_string_length;
8317 }
8318 break;
8319 case 15:
Michal Vasko004d3152020-06-11 19:59:22 +02008320 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008321 xpath_func = &xpath_normalize_space;
Michal Vasko004d3152020-06-11 19:59:22 +02008322 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-after", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008323 xpath_func = &xpath_substring_after;
8324 }
8325 break;
8326 case 16:
Michal Vasko004d3152020-06-11 19:59:22 +02008327 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008328 xpath_func = &xpath_substring_before;
8329 }
8330 break;
8331 case 20:
Michal Vasko004d3152020-06-11 19:59:22 +02008332 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from-or-self", 20)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008333 xpath_func = &xpath_derived_from_or_self;
8334 }
8335 break;
8336 }
8337
8338 if (!xpath_func) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01008339 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 +02008340 return LY_EVALID;
8341 }
8342 }
8343
aPiecek8b0cc152021-05-31 16:40:31 +02008344 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008345 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008346 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008347
8348 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02008349 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
aPiecek8b0cc152021-05-31 16:40:31 +02008350 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008351 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008352 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008353
8354 /* ( Expr ( ',' Expr )* )? */
Michal Vasko004d3152020-06-11 19:59:22 +02008355 if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) {
aPiecek8b0cc152021-05-31 16:40:31 +02008356 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008357 args = malloc(sizeof *args);
8358 LY_CHECK_ERR_GOTO(!args, LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
8359 arg_count = 1;
8360 args[0] = set_copy(set);
8361 if (!args[0]) {
8362 rc = LY_EMEM;
8363 goto cleanup;
8364 }
8365
Michal Vasko004d3152020-06-11 19:59:22 +02008366 rc = eval_expr_select(exp, tok_idx, 0, args[0], options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008367 LY_CHECK_GOTO(rc, cleanup);
8368 } else {
aPiecek8b0cc152021-05-31 16:40:31 +02008369 rc = eval_expr_select(exp, tok_idx, 0, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008370 LY_CHECK_GOTO(rc, cleanup);
8371 }
8372 }
Michal Vasko004d3152020-06-11 19:59:22 +02008373 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) {
aPiecek8b0cc152021-05-31 16:40:31 +02008374 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008375 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008376 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008377
aPiecek8b0cc152021-05-31 16:40:31 +02008378 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008379 ++arg_count;
8380 args_aux = realloc(args, arg_count * sizeof *args);
8381 LY_CHECK_ERR_GOTO(!args_aux, arg_count--; LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
8382 args = args_aux;
8383 args[arg_count - 1] = set_copy(set);
8384 if (!args[arg_count - 1]) {
8385 rc = LY_EMEM;
8386 goto cleanup;
8387 }
8388
Michal Vasko004d3152020-06-11 19:59:22 +02008389 rc = eval_expr_select(exp, tok_idx, 0, args[arg_count - 1], options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008390 LY_CHECK_GOTO(rc, cleanup);
8391 } else {
aPiecek8b0cc152021-05-31 16:40:31 +02008392 rc = eval_expr_select(exp, tok_idx, 0, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008393 LY_CHECK_GOTO(rc, cleanup);
8394 }
8395 }
8396
8397 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02008398 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
aPiecek8b0cc152021-05-31 16:40:31 +02008399 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008400 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008401 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008402
aPiecek8b0cc152021-05-31 16:40:31 +02008403 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008404 /* evaluate function */
8405 rc = xpath_func(args, arg_count, set, options);
8406
8407 if (options & LYXP_SCNODE_ALL) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008408 /* merge all nodes from arg evaluations */
8409 for (i = 0; i < arg_count; ++i) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008410 set_scnode_clear_ctx(args[i], LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008411 lyxp_set_scnode_merge(set, args[i]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008412 }
8413 }
8414 } else {
8415 rc = LY_SUCCESS;
8416 }
8417
8418cleanup:
8419 for (i = 0; i < arg_count; ++i) {
8420 lyxp_set_free(args[i]);
8421 }
8422 free(args);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008423 return rc;
8424}
8425
8426/**
8427 * @brief Evaluate Number. Logs directly on error.
8428 *
8429 * @param[in] ctx Context for errors.
8430 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008431 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008432 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8433 * @return LY_ERR
8434 */
8435static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008436eval_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 +02008437{
8438 long double num;
8439 char *endptr;
8440
8441 if (set) {
8442 errno = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02008443 num = strtold(&exp->expr[exp->tok_pos[*tok_idx]], &endptr);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008444 if (errno) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01008445 LOGVAL(ctx, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]);
8446 LOGVAL(ctx, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double (%s).",
Michal Vasko69730152020-10-09 16:30:07 +02008447 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]], strerror(errno));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008448 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02008449 } else if (endptr - &exp->expr[exp->tok_pos[*tok_idx]] != exp->tok_len[*tok_idx]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01008450 LOGVAL(ctx, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]);
8451 LOGVAL(ctx, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double.",
Michal Vasko69730152020-10-09 16:30:07 +02008452 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008453 return LY_EVALID;
8454 }
8455
8456 set_fill_number(set, num);
8457 }
8458
8459 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008460 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008461 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008462 return LY_SUCCESS;
8463}
8464
aPiecekdf23eee2021-10-07 12:21:50 +02008465LY_ERR
8466lyxp_vars_find(struct lyxp_var *vars, const char *name, size_t name_len, struct lyxp_var **var)
8467{
8468 LY_ERR ret = LY_ENOTFOUND;
8469 LY_ARRAY_COUNT_TYPE u;
8470
8471 assert(vars && name);
8472
8473 name_len = name_len ? name_len : strlen(name);
8474
8475 LY_ARRAY_FOR(vars, u) {
8476 if (!strncmp(vars[u].name, name, name_len)) {
8477 ret = LY_SUCCESS;
8478 break;
8479 }
8480 }
8481
8482 if (var && !ret) {
8483 *var = &vars[u];
8484 }
8485
8486 return ret;
8487}
8488
Michal Vasko03ff5a72019-09-11 13:49:33 +02008489/**
aPiecekfba75362021-10-07 12:39:48 +02008490 * @brief Evaluate VariableReference.
8491 *
8492 * @param[in] exp Parsed XPath expression.
8493 * @param[in] tok_idx Position in the expression @p exp.
8494 * @param[in] vars [Sized array](@ref sizedarrays) of XPath variables.
8495 * @param[in,out] set Context and result set.
8496 * @param[in] options XPath options.
8497 * @return LY_ERR value.
8498 */
8499static LY_ERR
8500eval_variable_reference(const struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set, uint32_t options)
8501{
8502 LY_ERR ret;
8503 const char *name;
8504 struct lyxp_var *var;
8505 const struct lyxp_var *vars;
8506 struct lyxp_expr *tokens = NULL;
8507 uint16_t token_index;
8508
8509 vars = set->vars;
8510
Michal Vasko49fec8e2022-05-24 10:28:33 +02008511 /* find out the name and value of the variable */
aPiecekfba75362021-10-07 12:39:48 +02008512 name = &exp->expr[exp->tok_pos[*tok_idx]];
8513 ret = lyxp_vars_find((struct lyxp_var *)vars, name, exp->tok_len[*tok_idx], &var);
8514 LY_CHECK_ERR_RET(ret, LOGERR(set->ctx, ret,
8515 "XPath variable \"%s\" not defined.", name), ret);
8516
Michal Vasko49fec8e2022-05-24 10:28:33 +02008517 /* parse value */
aPiecekfba75362021-10-07 12:39:48 +02008518 ret = lyxp_expr_parse(set->ctx, var->value, 0, 1, &tokens);
8519 LY_CHECK_GOTO(ret, cleanup);
8520
Michal Vasko49fec8e2022-05-24 10:28:33 +02008521 /* evaluate value */
aPiecekfba75362021-10-07 12:39:48 +02008522 token_index = 0;
8523 ret = eval_expr_select(tokens, &token_index, 0, set, options);
8524 LY_CHECK_GOTO(ret, cleanup);
8525
8526cleanup:
8527 lyxp_expr_free(set->ctx, tokens);
8528
8529 return ret;
8530}
8531
8532/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02008533 * @brief Evaluate PathExpr. Logs directly on error.
8534 *
Michal Vaskod3678892020-05-21 10:06:58 +02008535 * [12] PathExpr ::= LocationPath | PrimaryExpr Predicate*
Michal Vasko03ff5a72019-09-11 13:49:33 +02008536 * | PrimaryExpr Predicate* '/' RelativeLocationPath
8537 * | PrimaryExpr Predicate* '//' RelativeLocationPath
8538 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
aPiecekfba75362021-10-07 12:39:48 +02008539 * [10] PrimaryExpr ::= VariableReference | '(' Expr ')' | Literal | Number | FunctionCall
Michal Vasko03ff5a72019-09-11 13:49:33 +02008540 *
8541 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008542 * @param[in] tok_idx Position in the expression @p exp.
aPiecek8b0cc152021-05-31 16:40:31 +02008543 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008544 * @param[in] options XPath options.
8545 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8546 */
8547static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008548eval_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 +02008549{
Michal Vasko49fec8e2022-05-24 10:28:33 +02008550 ly_bool all_desc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008551 LY_ERR rc;
8552
Michal Vasko004d3152020-06-11 19:59:22 +02008553 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008554 case LYXP_TOKEN_PAR1:
8555 /* '(' Expr ')' */
8556
8557 /* '(' */
aPiecek8b0cc152021-05-31 16:40:31 +02008558 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008559 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008560 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008561
8562 /* Expr */
Michal Vasko004d3152020-06-11 19:59:22 +02008563 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008564 LY_CHECK_RET(rc);
8565
8566 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02008567 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
aPiecek8b0cc152021-05-31 16:40:31 +02008568 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008569 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008570 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008571
Michal Vasko03ff5a72019-09-11 13:49:33 +02008572 goto predicate;
8573
8574 case LYXP_TOKEN_DOT:
8575 case LYXP_TOKEN_DDOT:
Michal Vasko49fec8e2022-05-24 10:28:33 +02008576 case LYXP_TOKEN_AXISNAME:
Michal Vasko03ff5a72019-09-11 13:49:33 +02008577 case LYXP_TOKEN_AT:
8578 case LYXP_TOKEN_NAMETEST:
8579 case LYXP_TOKEN_NODETYPE:
8580 /* RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02008581 rc = eval_relative_location_path(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008582 LY_CHECK_RET(rc);
8583 break;
8584
aPiecekfba75362021-10-07 12:39:48 +02008585 case LYXP_TOKEN_VARREF:
8586 /* VariableReference */
8587 rc = eval_variable_reference(exp, tok_idx, set, options);
8588 LY_CHECK_RET(rc);
8589 ++(*tok_idx);
8590
aPiecekfba75362021-10-07 12:39:48 +02008591 goto predicate;
8592
Michal Vasko03ff5a72019-09-11 13:49:33 +02008593 case LYXP_TOKEN_FUNCNAME:
8594 /* FunctionCall */
aPiecek8b0cc152021-05-31 16:40:31 +02008595 rc = eval_function_call(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008596 LY_CHECK_RET(rc);
8597
Michal Vasko03ff5a72019-09-11 13:49:33 +02008598 goto predicate;
8599
Michal Vasko3e48bf32020-06-01 08:39:07 +02008600 case LYXP_TOKEN_OPER_PATH:
8601 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +02008602 /* AbsoluteLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02008603 rc = eval_absolute_location_path(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008604 LY_CHECK_RET(rc);
8605 break;
8606
8607 case LYXP_TOKEN_LITERAL:
8608 /* Literal */
aPiecek8b0cc152021-05-31 16:40:31 +02008609 if ((options & LYXP_SKIP_EXPR) || (options & LYXP_SCNODE_ALL)) {
8610 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008611 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008612 }
Michal Vasko004d3152020-06-11 19:59:22 +02008613 eval_literal(exp, tok_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008614 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02008615 eval_literal(exp, tok_idx, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008616 }
8617
Michal Vasko03ff5a72019-09-11 13:49:33 +02008618 goto predicate;
8619
8620 case LYXP_TOKEN_NUMBER:
8621 /* Number */
aPiecek8b0cc152021-05-31 16:40:31 +02008622 if ((options & LYXP_SKIP_EXPR) || (options & LYXP_SCNODE_ALL)) {
8623 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008624 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008625 }
Michal Vasko004d3152020-06-11 19:59:22 +02008626 rc = eval_number(NULL, exp, tok_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008627 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02008628 rc = eval_number(set->ctx, exp, tok_idx, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008629 }
8630 LY_CHECK_RET(rc);
8631
Michal Vasko03ff5a72019-09-11 13:49:33 +02008632 goto predicate;
8633
8634 default:
Michal Vasko49fec8e2022-05-24 10:28:33 +02008635 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 +02008636 return LY_EVALID;
8637 }
8638
8639 return LY_SUCCESS;
8640
8641predicate:
8642 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02008643 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02008644 rc = eval_predicate(exp, tok_idx, set, options, LYXP_AXIS_CHILD);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008645 LY_CHECK_RET(rc);
8646 }
8647
8648 /* ('/' or '//') RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02008649 if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008650
8651 /* evaluate '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02008652 if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008653 all_desc = 0;
8654 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008655 all_desc = 1;
8656 }
8657
aPiecek8b0cc152021-05-31 16:40:31 +02008658 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008659 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008660 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008661
Michal Vasko004d3152020-06-11 19:59:22 +02008662 rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008663 LY_CHECK_RET(rc);
8664 }
8665
8666 return LY_SUCCESS;
8667}
8668
8669/**
8670 * @brief Evaluate UnionExpr. Logs directly on error.
8671 *
Michal Vaskod3678892020-05-21 10:06:58 +02008672 * [20] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008673 *
8674 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008675 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008676 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008677 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008678 * @param[in] options XPath options.
8679 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8680 */
8681static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008682eval_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 +02008683{
8684 LY_ERR rc = LY_SUCCESS;
8685 struct lyxp_set orig_set, set2;
8686 uint16_t i;
8687
8688 assert(repeat);
8689
8690 set_init(&orig_set, set);
8691 set_init(&set2, set);
8692
8693 set_fill_set(&orig_set, set);
8694
Michal Vasko004d3152020-06-11 19:59:22 +02008695 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008696 LY_CHECK_GOTO(rc, cleanup);
8697
8698 /* ('|' PathExpr)* */
8699 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008700 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_UNI);
aPiecek8b0cc152021-05-31 16:40:31 +02008701 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008702 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008703 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008704
aPiecek8b0cc152021-05-31 16:40:31 +02008705 if (options & LYXP_SKIP_EXPR) {
8706 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008707 LY_CHECK_GOTO(rc, cleanup);
8708 continue;
8709 }
8710
8711 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008712 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008713 LY_CHECK_GOTO(rc, cleanup);
8714
8715 /* eval */
8716 if (options & LYXP_SCNODE_ALL) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01008717 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008718 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008719 rc = moveto_union(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008720 LY_CHECK_GOTO(rc, cleanup);
8721 }
8722 }
8723
8724cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008725 lyxp_set_free_content(&orig_set);
8726 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008727 return rc;
8728}
8729
8730/**
8731 * @brief Evaluate UnaryExpr. Logs directly on error.
8732 *
Michal Vaskod3678892020-05-21 10:06:58 +02008733 * [19] UnaryExpr ::= UnionExpr | '-' UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008734 *
8735 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008736 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008737 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008738 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008739 * @param[in] options XPath options.
8740 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8741 */
8742static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008743eval_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 +02008744{
8745 LY_ERR rc;
8746 uint16_t this_op, i;
8747
8748 assert(repeat);
8749
8750 /* ('-')+ */
Michal Vasko004d3152020-06-11 19:59:22 +02008751 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008752 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008753 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 +02008754
aPiecek8b0cc152021-05-31 16:40:31 +02008755 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008756 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008757 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008758 }
8759
Michal Vasko004d3152020-06-11 19:59:22 +02008760 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNARY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008761 LY_CHECK_RET(rc);
8762
aPiecek8b0cc152021-05-31 16:40:31 +02008763 if (!(options & LYXP_SKIP_EXPR) && (repeat % 2)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008764 if (options & LYXP_SCNODE_ALL) {
8765 warn_operands(set->ctx, set, NULL, 1, exp->expr, exp->tok_pos[this_op]);
8766 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008767 rc = moveto_op_math(set, NULL, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008768 LY_CHECK_RET(rc);
8769 }
8770 }
8771
8772 return LY_SUCCESS;
8773}
8774
8775/**
8776 * @brief Evaluate MultiplicativeExpr. Logs directly on error.
8777 *
Michal Vaskod3678892020-05-21 10:06:58 +02008778 * [18] MultiplicativeExpr ::= UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008779 * | MultiplicativeExpr '*' UnaryExpr
8780 * | MultiplicativeExpr 'div' UnaryExpr
8781 * | MultiplicativeExpr 'mod' UnaryExpr
8782 *
8783 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008784 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008785 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008786 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008787 * @param[in] options XPath options.
8788 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8789 */
8790static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008791eval_multiplicative_expr(const struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set,
8792 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008793{
8794 LY_ERR rc;
8795 uint16_t this_op;
8796 struct lyxp_set orig_set, set2;
8797 uint16_t i;
8798
8799 assert(repeat);
8800
8801 set_init(&orig_set, set);
8802 set_init(&set2, set);
8803
8804 set_fill_set(&orig_set, set);
8805
Michal Vasko004d3152020-06-11 19:59:22 +02008806 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008807 LY_CHECK_GOTO(rc, cleanup);
8808
8809 /* ('*' / 'div' / 'mod' UnaryExpr)* */
8810 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008811 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008812
Michal Vasko004d3152020-06-11 19:59:22 +02008813 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH);
aPiecek8b0cc152021-05-31 16:40:31 +02008814 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008815 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008816 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008817
aPiecek8b0cc152021-05-31 16:40:31 +02008818 if (options & LYXP_SKIP_EXPR) {
8819 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008820 LY_CHECK_GOTO(rc, cleanup);
8821 continue;
8822 }
8823
8824 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008825 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008826 LY_CHECK_GOTO(rc, cleanup);
8827
8828 /* eval */
8829 if (options & LYXP_SCNODE_ALL) {
8830 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008831 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02008832 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008833 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008834 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008835 LY_CHECK_GOTO(rc, cleanup);
8836 }
8837 }
8838
8839cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008840 lyxp_set_free_content(&orig_set);
8841 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008842 return rc;
8843}
8844
8845/**
8846 * @brief Evaluate AdditiveExpr. Logs directly on error.
8847 *
Michal Vaskod3678892020-05-21 10:06:58 +02008848 * [17] AdditiveExpr ::= MultiplicativeExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008849 * | AdditiveExpr '+' MultiplicativeExpr
8850 * | AdditiveExpr '-' MultiplicativeExpr
8851 *
8852 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008853 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008854 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008855 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008856 * @param[in] options XPath options.
8857 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8858 */
8859static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008860eval_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 +02008861{
8862 LY_ERR rc;
8863 uint16_t this_op;
8864 struct lyxp_set orig_set, set2;
8865 uint16_t i;
8866
8867 assert(repeat);
8868
8869 set_init(&orig_set, set);
8870 set_init(&set2, set);
8871
8872 set_fill_set(&orig_set, set);
8873
Michal Vasko004d3152020-06-11 19:59:22 +02008874 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008875 LY_CHECK_GOTO(rc, cleanup);
8876
8877 /* ('+' / '-' MultiplicativeExpr)* */
8878 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008879 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008880
Michal Vasko004d3152020-06-11 19:59:22 +02008881 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008882 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008883 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008884 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008885
aPiecek8b0cc152021-05-31 16:40:31 +02008886 if (options & LYXP_SKIP_EXPR) {
8887 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008888 LY_CHECK_GOTO(rc, cleanup);
8889 continue;
8890 }
8891
8892 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008893 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008894 LY_CHECK_GOTO(rc, cleanup);
8895
8896 /* eval */
8897 if (options & LYXP_SCNODE_ALL) {
8898 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008899 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02008900 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008901 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008902 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008903 LY_CHECK_GOTO(rc, cleanup);
8904 }
8905 }
8906
8907cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008908 lyxp_set_free_content(&orig_set);
8909 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008910 return rc;
8911}
8912
8913/**
8914 * @brief Evaluate RelationalExpr. Logs directly on error.
8915 *
Michal Vaskod3678892020-05-21 10:06:58 +02008916 * [16] RelationalExpr ::= AdditiveExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008917 * | RelationalExpr '<' AdditiveExpr
8918 * | RelationalExpr '>' AdditiveExpr
8919 * | RelationalExpr '<=' AdditiveExpr
8920 * | RelationalExpr '>=' AdditiveExpr
8921 *
8922 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008923 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008924 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008925 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008926 * @param[in] options XPath options.
8927 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8928 */
8929static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008930eval_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 +02008931{
8932 LY_ERR rc;
8933 uint16_t this_op;
8934 struct lyxp_set orig_set, set2;
8935 uint16_t i;
8936
8937 assert(repeat);
8938
8939 set_init(&orig_set, set);
8940 set_init(&set2, set);
8941
8942 set_fill_set(&orig_set, set);
8943
Michal Vasko004d3152020-06-11 19:59:22 +02008944 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008945 LY_CHECK_GOTO(rc, cleanup);
8946
8947 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
8948 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008949 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008950
Michal Vasko004d3152020-06-11 19:59:22 +02008951 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_COMP);
aPiecek8b0cc152021-05-31 16:40:31 +02008952 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008953 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008954 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008955
aPiecek8b0cc152021-05-31 16:40:31 +02008956 if (options & LYXP_SKIP_EXPR) {
8957 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008958 LY_CHECK_GOTO(rc, cleanup);
8959 continue;
8960 }
8961
8962 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008963 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008964 LY_CHECK_GOTO(rc, cleanup);
8965
8966 /* eval */
8967 if (options & LYXP_SCNODE_ALL) {
8968 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008969 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02008970 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008971 } else {
8972 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options);
8973 LY_CHECK_GOTO(rc, cleanup);
8974 }
8975 }
8976
8977cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008978 lyxp_set_free_content(&orig_set);
8979 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008980 return rc;
8981}
8982
8983/**
8984 * @brief Evaluate EqualityExpr. Logs directly on error.
8985 *
Michal Vaskod3678892020-05-21 10:06:58 +02008986 * [15] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008987 * | EqualityExpr '!=' RelationalExpr
8988 *
8989 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008990 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008991 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008992 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008993 * @param[in] options XPath options.
8994 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8995 */
8996static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008997eval_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 +02008998{
8999 LY_ERR rc;
9000 uint16_t this_op;
9001 struct lyxp_set orig_set, set2;
9002 uint16_t i;
9003
9004 assert(repeat);
9005
9006 set_init(&orig_set, set);
9007 set_init(&set2, set);
9008
9009 set_fill_set(&orig_set, set);
9010
Michal Vasko004d3152020-06-11 19:59:22 +02009011 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009012 LY_CHECK_GOTO(rc, cleanup);
9013
9014 /* ('=' / '!=' RelationalExpr)* */
9015 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02009016 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009017
Michal Vasko004d3152020-06-11 19:59:22 +02009018 assert((exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_EQUAL) || (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_NEQUAL));
aPiecek8b0cc152021-05-31 16:40:31 +02009019 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02009020 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02009021 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009022
aPiecek8b0cc152021-05-31 16:40:31 +02009023 if (options & LYXP_SKIP_EXPR) {
9024 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009025 LY_CHECK_GOTO(rc, cleanup);
9026 continue;
9027 }
9028
9029 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02009030 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009031 LY_CHECK_GOTO(rc, cleanup);
9032
9033 /* eval */
9034 if (options & LYXP_SCNODE_ALL) {
9035 warn_operands(set->ctx, set, &set2, 0, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vasko004d3152020-06-11 19:59:22 +02009036 warn_equality_value(exp, set, *tok_idx - 1, this_op - 1, *tok_idx - 1);
9037 warn_equality_value(exp, &set2, this_op - 1, this_op - 1, *tok_idx - 1);
Michal Vaskoecd62de2019-11-13 12:35:11 +01009038 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02009039 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009040 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02009041 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options);
9042 LY_CHECK_GOTO(rc, cleanup);
9043 }
9044 }
9045
9046cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02009047 lyxp_set_free_content(&orig_set);
9048 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009049 return rc;
9050}
9051
9052/**
9053 * @brief Evaluate AndExpr. Logs directly on error.
9054 *
Michal Vaskod3678892020-05-21 10:06:58 +02009055 * [14] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02009056 *
9057 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02009058 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009059 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02009060 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009061 * @param[in] options XPath options.
9062 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
9063 */
9064static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02009065eval_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 +02009066{
9067 LY_ERR rc;
9068 struct lyxp_set orig_set, set2;
9069 uint16_t i;
9070
9071 assert(repeat);
9072
9073 set_init(&orig_set, set);
9074 set_init(&set2, set);
9075
9076 set_fill_set(&orig_set, set);
9077
Michal Vasko004d3152020-06-11 19:59:22 +02009078 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009079 LY_CHECK_GOTO(rc, cleanup);
9080
9081 /* cast to boolean, we know that will be the final result */
aPiecek8b0cc152021-05-31 16:40:31 +02009082 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02009083 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009084 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009085 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009086 }
9087
9088 /* ('and' EqualityExpr)* */
9089 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02009090 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG);
aPiecek8b0cc152021-05-31 16:40:31 +02009091 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, ((options & LYXP_SKIP_EXPR) || !set->val.bln ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02009092 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02009093 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009094
9095 /* lazy evaluation */
aPiecek8b0cc152021-05-31 16:40:31 +02009096 if ((options & LYXP_SKIP_EXPR) || ((set->type == LYXP_SET_BOOLEAN) && !set->val.bln)) {
9097 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009098 LY_CHECK_GOTO(rc, cleanup);
9099 continue;
9100 }
9101
9102 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02009103 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009104 LY_CHECK_GOTO(rc, cleanup);
9105
9106 /* eval - just get boolean value actually */
9107 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vasko1a09b212021-05-06 13:00:10 +02009108 set_scnode_clear_ctx(&set2, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskoecd62de2019-11-13 12:35:11 +01009109 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009110 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009111 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009112 set_fill_set(set, &set2);
9113 }
9114 }
9115
9116cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02009117 lyxp_set_free_content(&orig_set);
9118 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009119 return rc;
9120}
9121
9122/**
9123 * @brief Evaluate OrExpr. Logs directly on error.
9124 *
Michal Vaskod3678892020-05-21 10:06:58 +02009125 * [13] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02009126 *
9127 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02009128 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009129 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02009130 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009131 * @param[in] options XPath options.
9132 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
9133 */
9134static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02009135eval_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 +02009136{
9137 LY_ERR rc;
9138 struct lyxp_set orig_set, set2;
9139 uint16_t i;
9140
9141 assert(repeat);
9142
9143 set_init(&orig_set, set);
9144 set_init(&set2, set);
9145
9146 set_fill_set(&orig_set, set);
9147
Michal Vasko004d3152020-06-11 19:59:22 +02009148 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009149 LY_CHECK_GOTO(rc, cleanup);
9150
9151 /* cast to boolean, we know that will be the final result */
aPiecek8b0cc152021-05-31 16:40:31 +02009152 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02009153 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009154 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009155 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009156 }
9157
9158 /* ('or' AndExpr)* */
9159 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02009160 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG);
aPiecek8b0cc152021-05-31 16:40:31 +02009161 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, ((options & LYXP_SKIP_EXPR) || set->val.bln ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02009162 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02009163 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009164
9165 /* lazy evaluation */
aPiecek8b0cc152021-05-31 16:40:31 +02009166 if ((options & LYXP_SKIP_EXPR) || ((set->type == LYXP_SET_BOOLEAN) && set->val.bln)) {
9167 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009168 LY_CHECK_GOTO(rc, cleanup);
9169 continue;
9170 }
9171
9172 set_fill_set(&set2, &orig_set);
9173 /* expr_type cound have been LYXP_EXPR_NONE in all these later calls (except for the first one),
9174 * but it does not matter */
Michal Vasko004d3152020-06-11 19:59:22 +02009175 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009176 LY_CHECK_GOTO(rc, cleanup);
9177
9178 /* eval - just get boolean value actually */
9179 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vasko1a09b212021-05-06 13:00:10 +02009180 set_scnode_clear_ctx(&set2, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskoecd62de2019-11-13 12:35:11 +01009181 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009182 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009183 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009184 set_fill_set(set, &set2);
9185 }
9186 }
9187
9188cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02009189 lyxp_set_free_content(&orig_set);
9190 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009191 return rc;
9192}
9193
9194/**
Michal Vasko004d3152020-06-11 19:59:22 +02009195 * @brief Decide what expression is at the pointer @p tok_idx and evaluate it accordingly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009196 *
9197 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02009198 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009199 * @param[in] etype Expression type to evaluate.
aPiecek8b0cc152021-05-31 16:40:31 +02009200 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009201 * @param[in] options XPath options.
9202 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
9203 */
9204static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02009205eval_expr_select(const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_expr_type etype, struct lyxp_set *set,
9206 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02009207{
9208 uint16_t i, count;
9209 enum lyxp_expr_type next_etype;
9210 LY_ERR rc;
9211
9212 /* process operator repeats */
Michal Vasko004d3152020-06-11 19:59:22 +02009213 if (!exp->repeat[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02009214 next_etype = LYXP_EXPR_NONE;
9215 } else {
9216 /* find etype repeat */
Radek Krejci1e008d22020-08-17 11:37:37 +02009217 for (i = 0; exp->repeat[*tok_idx][i] > etype; ++i) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02009218
9219 /* select one-priority lower because etype expression called us */
9220 if (i) {
Michal Vasko004d3152020-06-11 19:59:22 +02009221 next_etype = exp->repeat[*tok_idx][i - 1];
Michal Vasko03ff5a72019-09-11 13:49:33 +02009222 /* count repeats for that expression */
Radek Krejci1e008d22020-08-17 11:37:37 +02009223 for (count = 0; i && exp->repeat[*tok_idx][i - 1] == next_etype; ++count, --i) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02009224 } else {
9225 next_etype = LYXP_EXPR_NONE;
9226 }
9227 }
9228
9229 /* decide what expression are we parsing based on the repeat */
9230 switch (next_etype) {
9231 case LYXP_EXPR_OR:
Michal Vasko004d3152020-06-11 19:59:22 +02009232 rc = eval_or_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009233 break;
9234 case LYXP_EXPR_AND:
Michal Vasko004d3152020-06-11 19:59:22 +02009235 rc = eval_and_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009236 break;
9237 case LYXP_EXPR_EQUALITY:
Michal Vasko004d3152020-06-11 19:59:22 +02009238 rc = eval_equality_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009239 break;
9240 case LYXP_EXPR_RELATIONAL:
Michal Vasko004d3152020-06-11 19:59:22 +02009241 rc = eval_relational_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009242 break;
9243 case LYXP_EXPR_ADDITIVE:
Michal Vasko004d3152020-06-11 19:59:22 +02009244 rc = eval_additive_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009245 break;
9246 case LYXP_EXPR_MULTIPLICATIVE:
Michal Vasko004d3152020-06-11 19:59:22 +02009247 rc = eval_multiplicative_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009248 break;
9249 case LYXP_EXPR_UNARY:
Michal Vasko004d3152020-06-11 19:59:22 +02009250 rc = eval_unary_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009251 break;
9252 case LYXP_EXPR_UNION:
Michal Vasko004d3152020-06-11 19:59:22 +02009253 rc = eval_union_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009254 break;
9255 case LYXP_EXPR_NONE:
Michal Vasko004d3152020-06-11 19:59:22 +02009256 rc = eval_path_expr(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009257 break;
9258 default:
9259 LOGINT_RET(set->ctx);
9260 }
9261
9262 return rc;
9263}
9264
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009265/**
9266 * @brief Get root type.
9267 *
9268 * @param[in] ctx_node Context node.
9269 * @param[in] ctx_scnode Schema context node.
9270 * @param[in] options XPath options.
9271 * @return Root type.
9272 */
9273static enum lyxp_node_type
Radek Krejci1deb5be2020-08-26 16:43:36 +02009274lyxp_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 +01009275{
Michal Vasko6b26e742020-07-17 15:02:10 +02009276 const struct lysc_node *op;
9277
Michal Vaskoa27245c2022-05-02 09:01:35 +02009278 /* explicit */
9279 if (options & LYXP_ACCESS_TREE_ALL) {
9280 return LYXP_NODE_ROOT;
9281 } else if (options & LYXP_ACCESS_TREE_CONFIG) {
9282 return LYXP_NODE_ROOT_CONFIG;
9283 }
9284
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009285 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009286 /* schema */
Radek Krejci1e008d22020-08-17 11:37:37 +02009287 for (op = ctx_scnode; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent) {}
Michal Vasko6b26e742020-07-17 15:02:10 +02009288
9289 if (op || (options & LYXP_SCNODE)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009290 /* general root that can access everything */
9291 return LYXP_NODE_ROOT;
9292 } else if (!ctx_scnode || (ctx_scnode->flags & LYS_CONFIG_W)) {
9293 /* root context node can access only config data (because we said so, it is unspecified) */
9294 return LYXP_NODE_ROOT_CONFIG;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009295 }
Michal Vasko6b26e742020-07-17 15:02:10 +02009296 return LYXP_NODE_ROOT;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009297 }
9298
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009299 /* data */
Michal Vasko6b26e742020-07-17 15:02:10 +02009300 op = ctx_node ? ctx_node->schema : NULL;
Michal Vaskod989ba02020-08-24 10:59:24 +02009301 for ( ; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent) {}
Michal Vasko6b26e742020-07-17 15:02:10 +02009302
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009303 if (op || !(options & LYXP_SCHEMA)) {
9304 /* general root that can access everything */
9305 return LYXP_NODE_ROOT;
Christian Hoppsb6ecaea2021-02-06 09:45:38 -05009306 } else if (!ctx_node || !ctx_node->schema || (ctx_node->schema->flags & LYS_CONFIG_W)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009307 /* root context node can access only config data (because we said so, it is unspecified) */
9308 return LYXP_NODE_ROOT_CONFIG;
9309 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009310 return LYXP_NODE_ROOT;
9311}
9312
Michal Vasko03ff5a72019-09-11 13:49:33 +02009313LY_ERR
Michal Vasko400e9672021-01-11 13:39:17 +01009314lyxp_eval(const struct ly_ctx *ctx, const struct lyxp_expr *exp, const struct lys_module *cur_mod,
Radek Krejci8df109d2021-04-23 12:19:08 +02009315 LY_VALUE_FORMAT format, void *prefix_data, const struct lyd_node *ctx_node, const struct lyd_node *tree,
aPiecekfba75362021-10-07 12:39:48 +02009316 const struct lyxp_var *vars, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02009317{
Michal Vasko004d3152020-06-11 19:59:22 +02009318 uint16_t tok_idx = 0;
Michal Vaskoddd76592022-01-17 13:34:48 +01009319 const struct lysc_node *snode;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009320 LY_ERR rc;
9321
Michal Vasko400e9672021-01-11 13:39:17 +01009322 LY_CHECK_ARG_RET(ctx, ctx, exp, set, LY_EINVAL);
Radek Krejci8df109d2021-04-23 12:19:08 +02009323 if (!cur_mod && ((format == LY_VALUE_SCHEMA) || (format == LY_VALUE_SCHEMA_RESOLVED))) {
Michal Vaskoddd76592022-01-17 13:34:48 +01009324 LOGERR(ctx, LY_EINVAL, "Current module must be set if schema format is used.");
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009325 return LY_EINVAL;
Michal Vasko004d3152020-06-11 19:59:22 +02009326 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02009327
Michal Vaskod3bb12f2020-12-04 14:33:09 +01009328 if (tree) {
9329 /* adjust the pointer to be the first top-level sibling */
9330 while (tree->parent) {
9331 tree = lyd_parent(tree);
9332 }
9333 tree = lyd_first_sibling(tree);
Michal Vaskoddd76592022-01-17 13:34:48 +01009334
9335 for (snode = tree->schema->parent; snode && (snode->nodetype & (LYS_CASE | LYS_CHOICE)); snode = snode->parent) {}
9336 if (snode) {
9337 /* unable to evaluate absolute paths */
9338 LOGERR(ctx, LY_EINVAL, "Data node \"%s\" has no parent but is not instance of a top-level schema node.",
9339 LYD_NAME(tree));
9340 return LY_EINVAL;
9341 }
Michal Vaskod3bb12f2020-12-04 14:33:09 +01009342 }
9343
Michal Vasko03ff5a72019-09-11 13:49:33 +02009344 /* prepare set for evaluation */
Michal Vasko03ff5a72019-09-11 13:49:33 +02009345 memset(set, 0, sizeof *set);
Michal Vaskod3678892020-05-21 10:06:58 +02009346 set->type = LYXP_SET_NODE_SET;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009347 set->root_type = lyxp_get_root_type(ctx_node, NULL, options);
9348 set_insert_node(set, (struct lyd_node *)ctx_node, 0, ctx_node ? LYXP_NODE_ELEM : set->root_type, 0);
9349
Michal Vasko400e9672021-01-11 13:39:17 +01009350 set->ctx = (struct ly_ctx *)ctx;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009351 set->cur_node = ctx_node;
9352 for (set->context_op = ctx_node ? ctx_node->schema : NULL;
Radek Krejci0f969882020-08-21 16:56:47 +02009353 set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF));
9354 set->context_op = set->context_op->parent) {}
Michal Vaskof03ed032020-03-04 13:31:44 +01009355 set->tree = tree;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009356 set->cur_mod = cur_mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009357 set->format = format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009358 set->prefix_data = prefix_data;
aPiecekfba75362021-10-07 12:39:48 +02009359 set->vars = vars;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009360
Radek Krejciddace2c2021-01-08 11:30:56 +01009361 LOG_LOCSET(NULL, set->cur_node, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01009362
Michal Vasko03ff5a72019-09-11 13:49:33 +02009363 /* evaluate */
Michal Vasko004d3152020-06-11 19:59:22 +02009364 rc = eval_expr_select(exp, &tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009365 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02009366 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009367 }
9368
Michal Vasko4a7d4d62021-12-13 17:05:06 +01009369 if (set->cur_node) {
9370 LOG_LOCBACK(0, 1, 0, 0);
9371 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02009372 return rc;
9373}
9374
9375#if 0
9376
9377/* full xml printing of set elements, not used currently */
9378
9379void
9380lyxp_set_print_xml(FILE *f, struct lyxp_set *set)
9381{
9382 uint32_t i;
9383 char *str_num;
9384 struct lyout out;
9385
9386 memset(&out, 0, sizeof out);
9387
9388 out.type = LYOUT_STREAM;
9389 out.method.f = f;
9390
9391 switch (set->type) {
9392 case LYXP_SET_EMPTY:
Michal Vasko5233e962020-08-14 14:26:20 +02009393 ly_print_(&out, "Empty XPath set\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02009394 break;
9395 case LYXP_SET_BOOLEAN:
Michal Vasko5233e962020-08-14 14:26:20 +02009396 ly_print_(&out, "Boolean XPath set:\n");
9397 ly_print_(&out, "%s\n\n", set->value.bool ? "true" : "false");
Michal Vasko03ff5a72019-09-11 13:49:33 +02009398 break;
9399 case LYXP_SET_STRING:
Michal Vasko5233e962020-08-14 14:26:20 +02009400 ly_print_(&out, "String XPath set:\n");
9401 ly_print_(&out, "\"%s\"\n\n", set->value.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009402 break;
9403 case LYXP_SET_NUMBER:
Michal Vasko5233e962020-08-14 14:26:20 +02009404 ly_print_(&out, "Number XPath set:\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02009405
9406 if (isnan(set->value.num)) {
9407 str_num = strdup("NaN");
9408 } else if ((set->value.num == 0) || (set->value.num == -0.0f)) {
9409 str_num = strdup("0");
9410 } else if (isinf(set->value.num) && !signbit(set->value.num)) {
9411 str_num = strdup("Infinity");
9412 } else if (isinf(set->value.num) && signbit(set->value.num)) {
9413 str_num = strdup("-Infinity");
9414 } else if ((long long)set->value.num == set->value.num) {
9415 if (asprintf(&str_num, "%lld", (long long)set->value.num) == -1) {
9416 str_num = NULL;
9417 }
9418 } else {
9419 if (asprintf(&str_num, "%03.1Lf", set->value.num) == -1) {
9420 str_num = NULL;
9421 }
9422 }
9423 if (!str_num) {
9424 LOGMEM;
9425 return;
9426 }
Michal Vasko5233e962020-08-14 14:26:20 +02009427 ly_print_(&out, "%s\n\n", str_num);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009428 free(str_num);
9429 break;
9430 case LYXP_SET_NODE_SET:
Michal Vasko5233e962020-08-14 14:26:20 +02009431 ly_print_(&out, "Node XPath set:\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02009432
9433 for (i = 0; i < set->used; ++i) {
Michal Vasko5233e962020-08-14 14:26:20 +02009434 ly_print_(&out, "%d. ", i + 1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009435 switch (set->node_type[i]) {
9436 case LYXP_NODE_ROOT_ALL:
Michal Vasko5233e962020-08-14 14:26:20 +02009437 ly_print_(&out, "ROOT all\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02009438 break;
9439 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko5233e962020-08-14 14:26:20 +02009440 ly_print_(&out, "ROOT config\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02009441 break;
9442 case LYXP_NODE_ROOT_STATE:
Michal Vasko5233e962020-08-14 14:26:20 +02009443 ly_print_(&out, "ROOT state\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02009444 break;
9445 case LYXP_NODE_ROOT_NOTIF:
Michal Vasko5233e962020-08-14 14:26:20 +02009446 ly_print_(&out, "ROOT notification \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009447 break;
9448 case LYXP_NODE_ROOT_RPC:
Michal Vasko5233e962020-08-14 14:26:20 +02009449 ly_print_(&out, "ROOT rpc \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009450 break;
9451 case LYXP_NODE_ROOT_OUTPUT:
Michal Vasko5233e962020-08-14 14:26:20 +02009452 ly_print_(&out, "ROOT output \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009453 break;
9454 case LYXP_NODE_ELEM:
Michal Vasko5233e962020-08-14 14:26:20 +02009455 ly_print_(&out, "ELEM \"%s\"\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009456 xml_print_node(&out, 1, set->value.nodes[i], 1, LYP_FORMAT);
Michal Vasko5233e962020-08-14 14:26:20 +02009457 ly_print_(&out, "\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02009458 break;
9459 case LYXP_NODE_TEXT:
Michal Vasko5233e962020-08-14 14:26:20 +02009460 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 +02009461 break;
9462 case LYXP_NODE_ATTR:
Michal Vasko5233e962020-08-14 14:26:20 +02009463 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 +02009464 break;
9465 }
9466 }
9467 break;
9468 }
9469}
9470
9471#endif
9472
9473LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009474lyxp_set_cast(struct lyxp_set *set, enum lyxp_set_type target)
Michal Vasko03ff5a72019-09-11 13:49:33 +02009475{
9476 long double num;
9477 char *str;
9478 LY_ERR rc;
9479
9480 if (!set || (set->type == target)) {
9481 return LY_SUCCESS;
9482 }
9483
9484 /* it's not possible to convert anything into a node set */
Michal Vaskod3678892020-05-21 10:06:58 +02009485 assert(target != LYXP_SET_NODE_SET);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009486
9487 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vaskod3678892020-05-21 10:06:58 +02009488 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009489 return LY_EINVAL;
9490 }
9491
9492 /* to STRING */
Michal Vaskod3678892020-05-21 10:06:58 +02009493 if ((target == LYXP_SET_STRING) || ((target == LYXP_SET_NUMBER) && (set->type == LYXP_SET_NODE_SET))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02009494 switch (set->type) {
9495 case LYXP_SET_NUMBER:
9496 if (isnan(set->val.num)) {
9497 set->val.str = strdup("NaN");
9498 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
9499 } else if ((set->val.num == 0) || (set->val.num == -0.0f)) {
9500 set->val.str = strdup("0");
9501 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
9502 } else if (isinf(set->val.num) && !signbit(set->val.num)) {
9503 set->val.str = strdup("Infinity");
9504 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
9505 } else if (isinf(set->val.num) && signbit(set->val.num)) {
9506 set->val.str = strdup("-Infinity");
9507 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
9508 } else if ((long long)set->val.num == set->val.num) {
9509 if (asprintf(&str, "%lld", (long long)set->val.num) == -1) {
9510 LOGMEM_RET(set->ctx);
9511 }
9512 set->val.str = str;
9513 } else {
9514 if (asprintf(&str, "%03.1Lf", set->val.num) == -1) {
9515 LOGMEM_RET(set->ctx);
9516 }
9517 set->val.str = str;
9518 }
9519 break;
9520 case LYXP_SET_BOOLEAN:
Michal Vasko004d3152020-06-11 19:59:22 +02009521 if (set->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02009522 set->val.str = strdup("true");
9523 } else {
9524 set->val.str = strdup("false");
9525 }
9526 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), LY_EMEM);
9527 break;
9528 case LYXP_SET_NODE_SET:
Michal Vasko03ff5a72019-09-11 13:49:33 +02009529 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009530 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02009531
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009532 rc = cast_node_set_to_string(set, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009533 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02009534 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009535 set->val.str = str;
9536 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009537 default:
9538 LOGINT_RET(set->ctx);
9539 }
9540 set->type = LYXP_SET_STRING;
9541 }
9542
9543 /* to NUMBER */
9544 if (target == LYXP_SET_NUMBER) {
9545 switch (set->type) {
9546 case LYXP_SET_STRING:
9547 num = cast_string_to_number(set->val.str);
Michal Vaskod3678892020-05-21 10:06:58 +02009548 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009549 set->val.num = num;
9550 break;
9551 case LYXP_SET_BOOLEAN:
Michal Vasko004d3152020-06-11 19:59:22 +02009552 if (set->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02009553 set->val.num = 1;
9554 } else {
9555 set->val.num = 0;
9556 }
9557 break;
9558 default:
9559 LOGINT_RET(set->ctx);
9560 }
9561 set->type = LYXP_SET_NUMBER;
9562 }
9563
9564 /* to BOOLEAN */
9565 if (target == LYXP_SET_BOOLEAN) {
9566 switch (set->type) {
9567 case LYXP_SET_NUMBER:
9568 if ((set->val.num == 0) || (set->val.num == -0.0f) || isnan(set->val.num)) {
Michal Vasko004d3152020-06-11 19:59:22 +02009569 set->val.bln = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009570 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02009571 set->val.bln = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009572 }
9573 break;
9574 case LYXP_SET_STRING:
9575 if (set->val.str[0]) {
Michal Vaskod3678892020-05-21 10:06:58 +02009576 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02009577 set->val.bln = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009578 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02009579 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02009580 set->val.bln = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009581 }
9582 break;
9583 case LYXP_SET_NODE_SET:
Michal Vaskod3678892020-05-21 10:06:58 +02009584 if (set->used) {
9585 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02009586 set->val.bln = 1;
Michal Vaskod3678892020-05-21 10:06:58 +02009587 } else {
9588 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02009589 set->val.bln = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02009590 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02009591 break;
9592 default:
9593 LOGINT_RET(set->ctx);
9594 }
9595 set->type = LYXP_SET_BOOLEAN;
9596 }
9597
Michal Vasko03ff5a72019-09-11 13:49:33 +02009598 return LY_SUCCESS;
9599}
9600
9601LY_ERR
Michal Vasko400e9672021-01-11 13:39:17 +01009602lyxp_atomize(const struct ly_ctx *ctx, const struct lyxp_expr *exp, const struct lys_module *cur_mod,
Radek Krejci8df109d2021-04-23 12:19:08 +02009603 LY_VALUE_FORMAT format, void *prefix_data, const struct lysc_node *ctx_scnode, struct lyxp_set *set,
Michal Vasko400e9672021-01-11 13:39:17 +01009604 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02009605{
Radek Krejci2efc45b2020-12-22 16:25:44 +01009606 LY_ERR ret;
Michal Vasko004d3152020-06-11 19:59:22 +02009607 uint16_t tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009608
Michal Vasko400e9672021-01-11 13:39:17 +01009609 LY_CHECK_ARG_RET(ctx, ctx, exp, set, LY_EINVAL);
Radek Krejci8df109d2021-04-23 12:19:08 +02009610 if (!cur_mod && ((format == LY_VALUE_SCHEMA) || (format == LY_VALUE_SCHEMA_RESOLVED))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009611 LOGARG(NULL, "Current module must be set if schema format is used.");
9612 return LY_EINVAL;
Michal Vasko004d3152020-06-11 19:59:22 +02009613 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02009614
9615 /* prepare set for evaluation */
Michal Vasko03ff5a72019-09-11 13:49:33 +02009616 memset(set, 0, sizeof *set);
9617 set->type = LYXP_SET_SCNODE_SET;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009618 set->root_type = lyxp_get_root_type(NULL, ctx_scnode, options);
9619 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 +01009620 set->val.scnodes[0].in_ctx = LYXP_SET_SCNODE_START;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009621
Michal Vasko400e9672021-01-11 13:39:17 +01009622 set->ctx = (struct ly_ctx *)ctx;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009623 set->cur_scnode = ctx_scnode;
Michal Vasko6b26e742020-07-17 15:02:10 +02009624 for (set->context_op = ctx_scnode;
Radek Krejci0f969882020-08-21 16:56:47 +02009625 set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF));
9626 set->context_op = set->context_op->parent) {}
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009627 set->cur_mod = cur_mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009628 set->format = format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009629 set->prefix_data = prefix_data;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009630
Radek Krejciddace2c2021-01-08 11:30:56 +01009631 LOG_LOCSET(set->cur_scnode, NULL, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01009632
Michal Vasko03ff5a72019-09-11 13:49:33 +02009633 /* evaluate */
Radek Krejci2efc45b2020-12-22 16:25:44 +01009634 ret = eval_expr_select(exp, &tok_idx, 0, set, options);
9635
Radek Krejciddace2c2021-01-08 11:30:56 +01009636 LOG_LOCBACK(1, 0, 0, 0);
Radek Krejci2efc45b2020-12-22 16:25:44 +01009637 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009638}
Michal Vaskod43d71a2020-08-07 14:54:58 +02009639
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01009640LIBYANG_API_DEF const char *
Michal Vaskod43d71a2020-08-07 14:54:58 +02009641lyxp_get_expr(const struct lyxp_expr *path)
9642{
9643 if (!path) {
9644 return NULL;
9645 }
9646
9647 return path->expr;
9648}